meerkat-runtime 0.8.14

v9 runtime control-plane for Meerkat agent lifecycle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
//! RuntimeStore — atomic persistence for runtime state.
//!
//! Machine-owned runtime commands durably persist [`RunBoundaryReceipt`] values
//! atomically with their session and input-state effects.
#![cfg_attr(target_arch = "wasm32", allow(dead_code))]

pub mod memory;
#[cfg(feature = "sqlite-store")]
pub mod sqlite;
mod whole_blob_rewrite;

pub use meerkat_core::{HeadCanonicalProvisionalTailAuthority, WholeBlobProvisionalTailAuthority};
pub use whole_blob_rewrite::{
    PreparedWholeBlobRewriteBoundary, PreparedWholeBlobRewriteStoreParts,
    VerifiedCommittedWholeBlobPayload,
};

use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;

use meerkat_core::lifecycle::core_executor::BoundSessionCommit;
use meerkat_core::lifecycle::{InputId, RunBoundaryReceipt, RunId};
use sha2::{Digest, Sha256};

use crate::identifiers::{IdempotencyKey, LogicalRuntimeId};
use crate::input_state::{InputStatePersistenceRecord, StoredInputState};
use crate::runtime_state::RuntimeState;

const LEGACY_MACHINE_LIFECYCLE_STORE_RECORD_VERSION: u16 = 1;
const SUPERVISOR_MACHINE_LIFECYCLE_STORE_RECORD_VERSION: u16 = 2;
const UNREGISTER_MACHINE_LIFECYCLE_STORE_RECORD_VERSION: u16 = 3;
pub(crate) const MACHINE_LIFECYCLE_STORE_RECORD_VERSION: u16 = 4;

/// Maximum number of exact input-state rows admitted by one compare-and-swap
/// boundary. Directed-terminal outbox batches share the same 256-row bound as
/// their publication seam.
pub const MAX_INPUT_STATE_BATCH_CAS: usize = 256;

/// Maximum number of canonical pending-terminal owner ids returned by one
/// discovery page.
pub const MAX_PENDING_TERMINAL_OWNER_PAGE: usize = 256;

pub(crate) fn input_state_is_pending_terminal_owner(
    state: &crate::input_state::InputState,
) -> bool {
    let owns_pending_completion = state
        .terminal_completion
        .as_ref()
        .is_some_and(|completion| {
            completion.owner_input_id == state.input_id
                && matches!(
                    &completion.phase,
                    crate::input_state::InputTerminalCompletionPhase::Pending
                )
        });
    let owns_unpublished_interaction =
        state
            .interaction_terminal_outbox
            .as_ref()
            .is_some_and(|outbox| {
                outbox.candidate_owner_input_id == state.input_id
                    && !matches!(
                        &outbox.phase,
                        crate::input_state::InteractionTerminalOutboxPhase::Published { .. }
                    )
            });
    owns_pending_completion || owns_unpublished_interaction
}

pub(crate) fn input_state_is_recovery_nonterminal(state: &StoredInputState) -> bool {
    !matches!(
        state.seed.phase,
        crate::input_state::InputLifecycleState::Consumed
            | crate::input_state::InputLifecycleState::Superseded
            | crate::input_state::InputLifecycleState::Coalesced
            | crate::input_state::InputLifecycleState::Abandoned
    )
}

/// Whether an input row has enough durable terminal evidence to retire its
/// original ingress payload.
///
/// The payload is crash-redelivery and durable-tail-attribution material, not
/// terminal history. Keep it until the generated lifecycle is terminal and
/// every row-local completion/publication saga is closed. Callers must apply
/// the resulting omission in the same transaction that commits the closing
/// evidence (or in a later exact-CAS compaction); serialize-time projection
/// alone would leave the live shell and durable row divergent.
pub(crate) fn input_state_payload_is_retirable(state: &StoredInputState) -> bool {
    let lifecycle_terminal = matches!(
        state.seed.phase,
        crate::input_state::InputLifecycleState::Consumed
            | crate::input_state::InputLifecycleState::Superseded
            | crate::input_state::InputLifecycleState::Coalesced
            | crate::input_state::InputLifecycleState::Abandoned
    ) && state.seed.terminal_outcome.is_some();
    let completion_closed = state
        .state
        .terminal_completion
        .as_ref()
        .is_none_or(|completion| {
            matches!(
                completion.phase,
                crate::input_state::InputTerminalCompletionPhase::Finalized { .. }
            )
        });
    let publication_closed =
        state
            .state
            .interaction_terminal_outbox
            .as_ref()
            .is_none_or(|outbox| {
                matches!(
                    outbox.phase,
                    crate::input_state::InteractionTerminalOutboxPhase::Published { .. }
                )
            });
    // A directed input's payload is itself the only pre-outbox carrier of
    // the Interaction identity. A terminal row without an outbox therefore
    // has an unmaterialized publication obligation and must retain content.
    // Malformed payloads also fail closed here; their validation error must
    // remain observable rather than being erased as if they were ordinary
    // terminal inputs.
    let has_unmaterialized_directed_terminal =
        state.state.persisted_input.as_ref().is_some_and(|input| {
            crate::input::validated_directed_interaction_id(input)
                .map(|interaction_id| {
                    interaction_id.is_some() && state.state.interaction_terminal_outbox.is_none()
                })
                .unwrap_or(true)
        });
    let has_compact_directed_attribution = state
        .state
        .interaction_terminal_outbox
        .as_ref()
        .is_none_or(|_| {
            state
                .state
                .directed_run_started_attribution
                .as_ref()
                .is_some_and(|attribution| !attribution.content_digest().is_empty())
        });
    lifecycle_terminal
        && completion_closed
        && publication_closed
        && !has_unmaterialized_directed_terminal
        && has_compact_directed_attribution
}

#[cfg(test)]
mod terminal_payload_retirement_tests {
    use super::*;
    use crate::input::{Input, PromptInput};
    use crate::input_state::{InputLifecycleState, InputTerminalOutcome, StoredInputState};

    fn prompt_payload() -> Input {
        Input::Prompt(PromptInput::new("large durable prompt", None))
    }

    fn with_terminal_seed(mut stored: StoredInputState) -> StoredInputState {
        stored.seed.phase = InputLifecycleState::Consumed;
        stored.seed.terminal_outcome = Some(InputTerminalOutcome::Consumed);
        stored.seed.recovery_lane = None;
        stored
    }

    #[test]
    fn payload_retirement_requires_terminal_lifecycle_and_closed_obligations() {
        let input_id = InputId::new();
        let mut accepted = StoredInputState::new_accepted(input_id.clone());
        accepted.state.persisted_input = Some(prompt_payload());
        assert!(!input_state_payload_is_retirable(&accepted));

        let mut staged = accepted.clone();
        staged.seed.phase = InputLifecycleState::Staged;
        assert!(!input_state_payload_is_retirable(&staged));

        let terminal = with_terminal_seed(accepted);
        assert!(input_state_payload_is_retirable(&terminal));

        let (mut pending, _) = pending_terminal_owner_fixture(input_id.clone(), false);
        pending.state.persisted_input = Some(prompt_payload());
        pending = with_terminal_seed(pending);
        assert!(!input_state_payload_is_retirable(&pending));

        let (mut published, _) = pending_terminal_owner_fixture(input_id, true);
        published.state.persisted_input = Some(prompt_payload());
        published = with_terminal_seed(published);
        assert!(input_state_payload_is_retirable(&published));
    }

    #[test]
    fn retired_terminal_wire_image_omits_only_the_payload() {
        let mut terminal = StoredInputState::new_accepted(InputId::new());
        terminal.state.persisted_input = Some(prompt_payload());
        terminal = with_terminal_seed(terminal);
        let before_seed = terminal.seed.clone();

        assert!(input_state_payload_is_retirable(&terminal));
        terminal.state.persisted_input = None;
        let encoded = serde_json::to_value(&terminal).unwrap();
        let decoded: StoredInputState = serde_json::from_value(encoded.clone()).unwrap();

        assert!(encoded.get("persisted_input").is_none());
        assert_eq!(decoded.seed, before_seed);
        assert_eq!(decoded.seed.phase, InputLifecycleState::Consumed);
        assert_eq!(
            decoded.seed.terminal_outcome,
            Some(InputTerminalOutcome::Consumed)
        );
    }
}

pub(crate) fn validate_pending_terminal_owner_page(
    after: Option<&InputId>,
    limit: usize,
    owner_input_ids: &[InputId],
) -> Result<(), RuntimeStoreError> {
    if limit == 0 || limit > MAX_PENDING_TERMINAL_OWNER_PAGE {
        return Err(RuntimeStoreError::InvalidInputStateBatchCas {
            reason: format!(
                "pending-terminal owner page limit {limit} is outside 1..={MAX_PENDING_TERMINAL_OWNER_PAGE}"
            ),
        });
    }
    if owner_input_ids.len() > limit {
        return Err(RuntimeStoreError::ReadFailed(format!(
            "pending-terminal owner page returned {} ids for limit {limit}",
            owner_input_ids.len()
        )));
    }
    if owner_input_ids
        .windows(2)
        .any(|window| window[0].0 >= window[1].0)
    {
        return Err(RuntimeStoreError::ReadFailed(
            "pending-terminal owner page is not strictly ordered".to_string(),
        ));
    }
    if let (Some(after), Some(first)) = (after, owner_input_ids.first())
        && first.0 <= after.0
    {
        return Err(RuntimeStoreError::ReadFailed(
            "pending-terminal owner page did not advance its stable cursor".to_string(),
        ));
    }
    Ok(())
}

/// Result of an exact input-state batch compare-and-swap.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputStateBatchCasOutcome {
    /// Every expected durable row matched and every replacement committed, or
    /// every durable row was already byte-identical to its replacement from an
    /// earlier invocation whose acknowledgement was lost.
    Swapped,
    /// At least one expected row was missing or no longer byte-identical; no
    /// replacement was written.
    Stale,
}

/// Backend realization profile for logical exact-batch input-state CAS.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputStateBatchCasImplementationProfile {
    /// The store does not provide the exact atomic batch contract.
    Unsupported,
    /// Per-row comparison and the complete replacement set commit in one
    /// durable multi-writer transaction.
    MultiWriter,
    /// A whole-batch backend write is safe only while every write validates a
    /// durable exclusive-writer fence epoch.
    ///
    /// A process-local mutex alone never satisfies this profile. The epoch
    /// token must be conditionally checked by the backing store, the complete
    /// batch must persist in one conditional write, and publication must wait
    /// for that durable success. A crash leaves the receipt Pending for cold
    /// deterministic retry.
    ExclusiveWriterFenced,
}

/// Result of an exact input-state batch compare-and-swap performed under an
/// external authority fence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FencedInputStateBatchCasOutcome {
    /// The target rows matched and the replacements committed, or were already
    /// byte-identical, while the external authority was current.
    Swapped,
    /// At least one target row no longer matched. The external fence was not
    /// consulted and no replacement was written.
    Stale,
    /// The external authority was superseded. No replacement was written.
    FenceConflict { reason: String },
    /// The external authority could not be checked temporarily. No replacement
    /// was written.
    FenceBackoff { reason: String },
}

#[derive(Debug)]
struct PreparedInputStateBatchCasRow {
    input_id: InputId,
    expected_json: Vec<u8>,
    replacement: StoredInputState,
    // SQLite writes the already-validated bytes; the in-memory implementation
    // stores the typed replacement directly.
    #[cfg_attr(not(feature = "sqlite-store"), allow(dead_code))]
    replacement_json: Vec<u8>,
}

fn prepare_input_state_batch_cas(
    expected: &[StoredInputState],
    replacements: &[InputStatePersistenceRecord],
) -> Result<Vec<PreparedInputStateBatchCasRow>, RuntimeStoreError> {
    if expected.len() != replacements.len() {
        return Err(RuntimeStoreError::InvalidInputStateBatchCas {
            reason: format!(
                "expected row count {} does not match replacement row count {}",
                expected.len(),
                replacements.len()
            ),
        });
    }
    if expected.len() > MAX_INPUT_STATE_BATCH_CAS {
        return Err(RuntimeStoreError::InvalidInputStateBatchCas {
            reason: format!(
                "batch contains {} rows, exceeding the maximum of {MAX_INPUT_STATE_BATCH_CAS}",
                expected.len()
            ),
        });
    }

    let mut expected_ids = HashSet::with_capacity(expected.len());
    for row in expected {
        if !expected_ids.insert(row.state.input_id.clone()) {
            return Err(RuntimeStoreError::InvalidInputStateBatchCas {
                reason: format!("expected batch repeats input {}", row.state.input_id),
            });
        }
    }

    let mut replacement_by_id = HashMap::with_capacity(replacements.len());
    for record in replacements {
        let replacement = record.clone_stored();
        let input_id = replacement.state.input_id.clone();
        let replacement_json = serde_json::to_vec(&replacement)
            .map_err(|error| RuntimeStoreError::WriteFailed(error.to_string()))?;
        if replacement_by_id
            .insert(input_id.clone(), (replacement, replacement_json))
            .is_some()
        {
            return Err(RuntimeStoreError::InvalidInputStateBatchCas {
                reason: format!("replacement batch repeats input {input_id}"),
            });
        }
    }

    let mut prepared = Vec::with_capacity(expected.len());
    for expected_row in expected {
        let input_id = expected_row.state.input_id.clone();
        let Some((replacement, replacement_json)) = replacement_by_id.remove(&input_id) else {
            return Err(RuntimeStoreError::InvalidInputStateBatchCas {
                reason: format!("replacement batch does not contain expected input {input_id}"),
            });
        };
        let expected_json = serde_json::to_vec(expected_row)
            .map_err(|error| RuntimeStoreError::WriteFailed(error.to_string()))?;
        prepared.push(PreparedInputStateBatchCasRow {
            input_id,
            expected_json,
            replacement,
            replacement_json,
        });
    }
    if let Some(extra) = replacement_by_id.keys().next() {
        return Err(RuntimeStoreError::InvalidInputStateBatchCas {
            reason: format!("replacement batch contains unexpected input {extra}"),
        });
    }
    Ok(prepared)
}

pub(crate) fn validate_input_state_batch_read_ids(
    input_ids: &[InputId],
) -> Result<(), RuntimeStoreError> {
    if input_ids.len() > MAX_INPUT_STATE_BATCH_CAS {
        return Err(RuntimeStoreError::InvalidInputStateBatchCas {
            reason: format!(
                "batch read contains {} rows, exceeding the maximum of {MAX_INPUT_STATE_BATCH_CAS}",
                input_ids.len()
            ),
        });
    }
    let mut unique = HashSet::with_capacity(input_ids.len());
    for input_id in input_ids {
        if !unique.insert(input_id.clone()) {
            return Err(RuntimeStoreError::InvalidInputStateBatchCas {
                reason: format!("batch read repeats input {input_id}"),
            });
        }
    }
    Ok(())
}

/// Durable representation selected for runtime-owned session authority.
///
/// The profile is explicit because the physical commit is materially
/// different: a whole-blob store owns one lazy body encoding plus its exact
/// row authority, while a head-canonical store owns delta rows and the small
/// head inside the runtime transaction itself.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum RuntimeSessionPersistenceProfile {
    /// RuntimeStore retains an authoritative serialized Session document.
    WholeBlobV1,
    /// RuntimeStore atomically commits canonical strand rows and a small head.
    HeadCanonicalV1,
}

/// Small RuntimeStore-owned catalog projection for session listing and
/// lifecycle discovery.
///
/// This is intentionally incapable of carrying transcript, graph, component,
/// or serialized Session body data. WholeBlob and HeadCanonical stores update
/// it in the same atomic commit as their physical session authority.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct RuntimeSessionCatalogEntry {
    session_id: meerkat_core::types::SessionId,
    persistence_profile: RuntimeSessionPersistenceProfile,
    created_at: meerkat_core::time_compat::SystemTime,
    updated_at: meerkat_core::time_compat::SystemTime,
    message_count: usize,
    total_tokens: u64,
    labels: BTreeMap<String, String>,
    lifecycle_terminal: Option<meerkat_core::SessionLifecycleTerminal>,
    runtime_state: Option<RuntimeState>,
}

impl RuntimeSessionCatalogEntry {
    const SESSION_LABELS_KEY: &'static str = "session_labels";

    /// Derive a validated, body-free catalog projection from a typed Session.
    ///
    /// This value is listing metadata only. It does not certify the Session,
    /// issue store authority, authorize adoption, or prove that any physical
    /// row is current. A custom [`RuntimeStore`] must commit the returned entry
    /// atomically with the physical body and its own store-issued authority.
    ///
    /// Malformed catalog-owned metadata is rejected rather than omitted.
    pub fn from_session(
        session: &meerkat_core::Session,
        persistence_profile: RuntimeSessionPersistenceProfile,
        runtime_state: Option<RuntimeState>,
    ) -> Result<Self, RuntimeStoreError> {
        let labels = session
            .metadata()
            .get(Self::SESSION_LABELS_KEY)
            .map(|value| {
                serde_json::from_value::<BTreeMap<String, String>>(value.clone()).map_err(|error| {
                    RuntimeStoreError::WriteFailed(format!(
                        "session {} has malformed catalog labels: {error}",
                        session.id()
                    ))
                })
            })
            .transpose()?
            .unwrap_or_default();
        let lifecycle_terminal = session.try_lifecycle_terminal().map_err(|error| {
            RuntimeStoreError::WriteFailed(format!(
                "session {} has malformed lifecycle-terminal metadata: {error}",
                session.id()
            ))
        })?;
        Ok(Self {
            session_id: session.id().clone(),
            persistence_profile,
            created_at: session.created_at(),
            updated_at: session.updated_at(),
            message_count: session.messages().len(),
            total_tokens: session.total_tokens(),
            labels,
            lifecycle_terminal,
            runtime_state,
        })
    }

    /// Derive a validated, body-free catalog projection from a canonical head.
    ///
    /// This is the HeadCanonical counterpart of [`Self::from_session`]. It
    /// materializes only the head's bounded catalog metadata projection and
    /// does not mint CAS authority or authorize a store transition. The custom
    /// backend remains responsible for atomically committing this projection
    /// with its exact physical head and store-issued authority.
    pub fn from_head(
        head: &meerkat_core::session_store::SessionHead,
        persistence_profile: RuntimeSessionPersistenceProfile,
        runtime_state: Option<RuntimeState>,
    ) -> Result<Self, RuntimeStoreError> {
        let metadata = head.materialized_metadata().map_err(|error| {
            RuntimeStoreError::WriteFailed(format!(
                "session {} head has no exact catalog metadata projection: {error}",
                head.id
            ))
        })?;
        let labels = metadata
            .get(Self::SESSION_LABELS_KEY)
            .map(|value| {
                serde_json::from_value::<BTreeMap<String, String>>(value.clone()).map_err(|error| {
                    RuntimeStoreError::WriteFailed(format!(
                        "session {} head has malformed catalog labels: {error}",
                        head.id
                    ))
                })
            })
            .transpose()?
            .unwrap_or_default();
        let lifecycle_terminal =
            meerkat_core::try_lifecycle_terminal_from_map(&metadata).map_err(|error| {
                RuntimeStoreError::WriteFailed(format!(
                    "session {} head has malformed lifecycle-terminal metadata: {error}",
                    head.id
                ))
            })?;
        Self::from_head_facts(
            head,
            labels,
            lifecycle_terminal,
            persistence_profile,
            runtime_state,
        )
    }

    pub(crate) fn from_head_facts(
        head: &meerkat_core::session_store::SessionHead,
        labels: BTreeMap<String, String>,
        lifecycle_terminal: Option<meerkat_core::SessionLifecycleTerminal>,
        persistence_profile: RuntimeSessionPersistenceProfile,
        runtime_state: Option<RuntimeState>,
    ) -> Result<Self, RuntimeStoreError> {
        Ok(Self {
            session_id: head.id.clone(),
            persistence_profile,
            created_at: head.created_at,
            updated_at: head.updated_at,
            message_count: usize::try_from(head.message_count).map_err(|_| {
                RuntimeStoreError::WriteFailed(format!(
                    "session {} head message count exceeds host range",
                    head.id
                ))
            })?,
            total_tokens: head.usage.total_tokens(),
            labels,
            lifecycle_terminal,
            runtime_state,
        })
    }

    #[must_use]
    pub fn session_id(&self) -> &meerkat_core::types::SessionId {
        &self.session_id
    }

    #[must_use]
    pub const fn persistence_profile(&self) -> RuntimeSessionPersistenceProfile {
        self.persistence_profile
    }

    #[must_use]
    pub const fn created_at(&self) -> meerkat_core::time_compat::SystemTime {
        self.created_at
    }

    #[must_use]
    pub const fn updated_at(&self) -> meerkat_core::time_compat::SystemTime {
        self.updated_at
    }

    #[must_use]
    pub const fn message_count(&self) -> usize {
        self.message_count
    }

    #[must_use]
    pub const fn total_tokens(&self) -> u64 {
        self.total_tokens
    }

    #[must_use]
    pub fn labels(&self) -> &BTreeMap<String, String> {
        &self.labels
    }

    #[must_use]
    pub const fn lifecycle_terminal(&self) -> Option<meerkat_core::SessionLifecycleTerminal> {
        self.lifecycle_terminal
    }

    #[must_use]
    pub const fn runtime_state(&self) -> Option<RuntimeState> {
        self.runtime_state
    }

    pub(crate) fn set_runtime_state(&mut self, runtime_state: Option<RuntimeState>) {
        self.runtime_state = runtime_state;
    }
}

/// Upper bound for observing the currently committed session authority.
///
/// Reconciliation and health probes may poll this seam. A store must therefore
/// state whether the observation is a bounded metadata read; silently parsing
/// an accumulated WholeBlob document here recreates an O(document) hot loop.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeSessionAuthorityReadCost {
    /// Reads only a fixed-size authority row or in-memory authority value.
    Bounded,
    /// This implementation has no bounded authority observation.
    Unsupported,
}

/// Store-issued physical identity of one committed WholeBlob row.
///
/// The Session is ordinary domain payload. Currentness is owned only by the
/// store revision and digest of the exact bytes in the row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WholeBlobStoreAuthority {
    authority_version: u16,
    session_id: meerkat_core::types::SessionId,
    store_revision: u64,
    blob_sha256: String,
}

fn is_canonical_row_sha256_token(token: &str) -> bool {
    let Some(hex) = token.strip_prefix("row-sha256:") else {
        return false;
    };
    hex.len() == 64
        && hex
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}

impl WholeBlobStoreAuthority {
    /// Current fixed-size ledger format.
    pub const VERSION: u16 = 1;

    /// Validate the fixed-size fields decoded from one backend store record.
    ///
    /// This constructs a value carrier only. Calling it does not make the
    /// record current and does not mint authority for a caller. The value is
    /// authoritative only when an honest [`RuntimeStore`] returns it from the
    /// same atomic observation or commit that proved the named physical row.
    pub fn from_store_record(
        authority_version: u16,
        session_id: meerkat_core::types::SessionId,
        store_revision: u64,
        blob_sha256: String,
    ) -> Result<Self, RuntimeStoreError> {
        if authority_version != Self::VERSION
            || store_revision == 0
            || !is_canonical_row_sha256_token(&blob_sha256)
        {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: session_id.to_string(),
                detail: "WholeBlob store authority requires the current version, nonzero \
                         revision, and canonical physical row digest"
                    .to_string(),
            });
        }
        Ok(Self {
            authority_version,
            session_id,
            store_revision,
            blob_sha256,
        })
    }

    pub(crate) fn issued(
        session_id: meerkat_core::types::SessionId,
        store_revision: u64,
        blob_sha256: String,
    ) -> Result<Self, RuntimeStoreError> {
        Self::from_store_record(Self::VERSION, session_id, store_revision, blob_sha256)
    }

    #[must_use]
    pub const fn authority_version(&self) -> u16 {
        self.authority_version
    }

    #[must_use]
    pub fn session_id(&self) -> &meerkat_core::types::SessionId {
        &self.session_id
    }

    #[must_use]
    pub const fn store_revision(&self) -> u64 {
        self.store_revision
    }

    #[must_use]
    pub fn blob_sha256(&self) -> &str {
        &self.blob_sha256
    }
}

/// One atomically observed WholeBlob row and its store-issued identity.
///
/// Backends construct this under one lock/transaction so rewrite preparation
/// cannot accidentally pair bytes from one revision with another revision's
/// digest.
#[derive(Debug, Clone)]
pub struct CommittedWholeBlobSnapshot {
    session: Arc<meerkat_core::Session>,
    bytes: Arc<Vec<u8>>,
    authority: WholeBlobStoreAuthority,
}

/// One typed whole-document successor bound to an exact store-issued
/// predecessor.
///
/// This carrier is intentionally free of Session checkpoint facts. The
/// predecessor is identified only by the store revision and exact physical
/// digest, while the successor is encoded and hashed once inside this
/// WholeBlob preparation boundary.
#[derive(Debug, Clone)]
pub struct PreparedWholeBlobSnapshotCas {
    expected_authority: WholeBlobStoreAuthority,
    candidate_session: Arc<meerkat_core::Session>,
    candidate_bytes: Arc<Vec<u8>>,
    candidate_blob_sha256: String,
}

/// Result of one exact-authority WholeBlob snapshot compare-and-swap.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WholeBlobSnapshotCasOutcome {
    /// The candidate is current, either after this write or as an exact
    /// idempotent observation.
    Committed(WholeBlobStoreAuthority),
    /// The expected store-issued predecessor is no longer current.
    Conflict,
}

impl PreparedWholeBlobSnapshotCas {
    pub fn prepare(
        expected_authority: WholeBlobStoreAuthority,
        candidate: BoundSessionCommit,
    ) -> Result<Self, RuntimeStoreError> {
        let candidate_session = candidate.into_session_arc().ok_or_else(|| {
            RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: expected_authority.session_id().to_string(),
                detail: "WholeBlob snapshot CAS requires a sealed typed Session".to_string(),
            }
        })?;
        if candidate_session.id() != expected_authority.session_id() {
            return Err(RuntimeStoreError::SessionKeyMismatch {
                expected: expected_authority.session_id().clone(),
                actual: candidate_session.id().clone(),
            });
        }
        let (candidate_bytes, candidate_blob_sha256) =
            encode_whole_blob_session(candidate_session.as_ref())?;
        Ok(Self {
            expected_authority,
            candidate_session,
            candidate_bytes,
            candidate_blob_sha256,
        })
    }

    #[must_use]
    pub fn expected_authority(&self) -> &WholeBlobStoreAuthority {
        &self.expected_authority
    }

    #[must_use]
    pub fn candidate_blob_sha256(&self) -> &str {
        &self.candidate_blob_sha256
    }

    /// Whether a bounded store observation proves this candidate current.
    #[must_use]
    pub fn accepts_committed_authority(&self, observed: &WholeBlobStoreAuthority) -> bool {
        if observed.session_id() != self.expected_authority.session_id()
            || observed.blob_sha256() != self.candidate_blob_sha256
        {
            return false;
        }
        (observed.store_revision() == self.expected_authority.store_revision()
            && self.candidate_blob_sha256 == self.expected_authority.blob_sha256())
            || self
                .expected_authority
                .store_revision()
                .checked_add(1)
                .is_some_and(|successor| observed.store_revision() == successor)
    }

    pub(crate) fn into_parts(
        self,
    ) -> (
        WholeBlobStoreAuthority,
        Arc<meerkat_core::Session>,
        Arc<Vec<u8>>,
        String,
    ) {
        (
            self.expected_authority,
            self.candidate_session,
            self.candidate_bytes,
            self.candidate_blob_sha256,
        )
    }
}

struct WholeBlobSessionWriter {
    bytes: Vec<u8>,
    hasher: Sha256,
}

impl std::io::Write for WholeBlobSessionWriter {
    fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> {
        self.bytes.extend_from_slice(buffer);
        self.hasher.update(buffer);
        Ok(buffer.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

fn encode_whole_blob_session(
    session: &meerkat_core::Session,
) -> Result<(Arc<Vec<u8>>, String), RuntimeStoreError> {
    let mut writer = WholeBlobSessionWriter {
        bytes: Vec::new(),
        hasher: Sha256::new(),
    };
    serde_json::to_writer(&mut writer, session)
        .map_err(|error| RuntimeStoreError::WriteFailed(error.to_string()))?;
    let blob_sha256 = format!("row-sha256:{:x}", writer.hasher.finalize());
    Ok((Arc::new(writer.bytes), blob_sha256))
}

/// Opaque typed candidate prepared for one provisional WholeBlob body write.
#[derive(Debug, Clone)]
pub struct PreparedWholeBlobProvisionalTail {
    authority: WholeBlobProvisionalTailAuthority,
    candidate_artifact: meerkat_core::SerializedSessionArtifact,
    conversation_digest: String,
    message_count: u64,
    catalog_entry: RuntimeSessionCatalogEntry,
    compaction_projection_intents: Vec<meerkat_core::CompactionProjectionIntent>,
    #[cfg(test)]
    whole_blob_encode_count: Arc<std::sync::atomic::AtomicUsize>,
}

impl PreparedWholeBlobProvisionalTail {
    /// Prepare from an existing sealed boundary.
    ///
    /// Compatibility callers may already own a `BoundSessionCommit`; the
    /// artifact's single-assignment cache is reused without retaining the
    /// candidate `Session` in this carrier.
    pub fn prepare(
        base: WholeBlobStoreAuthority,
        run_id: RunId,
        candidate_sequence: u64,
        candidate: &BoundSessionCommit,
    ) -> Result<Self, RuntimeStoreError> {
        let candidate_session = candidate.session_arc_cloned().ok_or_else(|| {
            RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: base.session_id().to_string(),
                detail: "WholeBlob provisional candidate requires a sealed typed Session"
                    .to_string(),
            }
        })?;
        let artifact = candidate
            .whole_blob_artifact()
            .map_err(|error| {
                RuntimeStoreError::WriteFailed(format!(
                    "failed to materialize WholeBlob provisional candidate: {error}"
                ))
            })?
            .clone();
        Self::prepare_from_artifact(
            base,
            run_id,
            candidate_sequence,
            candidate_session.as_ref(),
            artifact,
            #[cfg(test)]
            Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        )
    }

    /// Borrow one live candidate while deriving the exact persisted artifact
    /// and every bounded projection.
    ///
    /// JSON bytes and SHA-256 are produced in one streaming pass. The returned
    /// carrier owns only the sealed artifact and bounded metadata; it does not
    /// clone or retain the accumulated `Session`.
    pub fn prepare_from_session(
        base: WholeBlobStoreAuthority,
        run_id: RunId,
        candidate_sequence: u64,
        candidate: &meerkat_core::Session,
    ) -> Result<Self, RuntimeStoreError> {
        #[cfg(test)]
        let whole_blob_encode_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let artifact = candidate.to_persisted_artifact().map_err(|error| {
            RuntimeStoreError::WriteFailed(format!(
                "failed to materialize WholeBlob provisional candidate: {error}"
            ))
        })?;
        #[cfg(test)]
        whole_blob_encode_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        Self::prepare_from_artifact(
            base,
            run_id,
            candidate_sequence,
            candidate,
            artifact,
            #[cfg(test)]
            whole_blob_encode_count,
        )
    }

    fn prepare_from_artifact(
        base: WholeBlobStoreAuthority,
        run_id: RunId,
        candidate_sequence: u64,
        candidate_session: &meerkat_core::Session,
        candidate_artifact: meerkat_core::SerializedSessionArtifact,
        #[cfg(test)] whole_blob_encode_count: Arc<std::sync::atomic::AtomicUsize>,
    ) -> Result<Self, RuntimeStoreError> {
        if candidate_session.id() != base.session_id() {
            return Err(RuntimeStoreError::SessionKeyMismatch {
                expected: base.session_id().clone(),
                actual: candidate_session.id().clone(),
            });
        }
        let authority = WholeBlobProvisionalTailAuthority::issued(
            base.session_id().clone(),
            base.store_revision(),
            base.blob_sha256().to_string(),
            run_id,
            candidate_artifact.row_sha256_token().to_string(),
            candidate_sequence,
        )
        .map_err(
            |error| RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: base.session_id().to_string(),
                detail: error.to_string(),
            },
        )?;
        let catalog_entry = RuntimeSessionCatalogEntry::from_session(
            candidate_session,
            RuntimeSessionPersistenceProfile::WholeBlobV1,
            None,
        )?;
        let conversation_digest =
            candidate_session
                .transcript_content_digest()
                .map_err(
                    |error| RuntimeStoreError::SessionPersistenceAuthorityConflict {
                        runtime_id: base.session_id().to_string(),
                        detail: format!(
                            "failed to derive WholeBlob provisional conversation digest: {error}"
                        ),
                    },
                )?;
        let message_count = u64::try_from(candidate_session.messages().len()).map_err(|_| {
            RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: base.session_id().to_string(),
                detail: "WholeBlob provisional message count exceeds the durable range".to_string(),
            }
        })?;
        let compaction_projection_intents =
            validated_compaction_projection_intents(candidate_session)?;
        Ok(Self {
            authority,
            candidate_artifact,
            conversation_digest,
            message_count,
            catalog_entry,
            compaction_projection_intents,
            #[cfg(test)]
            whole_blob_encode_count,
        })
    }

    #[must_use]
    pub fn authority(&self) -> &WholeBlobProvisionalTailAuthority {
        &self.authority
    }

    #[must_use]
    pub fn conversation_digest(&self) -> &str {
        &self.conversation_digest
    }

    #[must_use]
    pub const fn message_count(&self) -> u64 {
        self.message_count
    }

    #[cfg(test)]
    fn whole_blob_encode_count(&self) -> usize {
        self.whole_blob_encode_count
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    pub(crate) fn into_parts(
        self,
    ) -> (
        WholeBlobProvisionalTailAuthority,
        meerkat_core::SerializedSessionArtifact,
        String,
        u64,
        RuntimeSessionCatalogEntry,
        Vec<meerkat_core::CompactionProjectionIntent>,
    ) {
        (
            self.authority,
            self.candidate_artifact,
            self.conversation_digest,
            self.message_count,
            self.catalog_entry,
            self.compaction_projection_intents,
        )
    }
}

/// One atomically observed provisional authority and its exact candidate body.
#[derive(Debug, Clone)]
pub struct CommittedWholeBlobProvisionalTail {
    authority: WholeBlobProvisionalTailAuthority,
    candidate_bytes: Arc<Vec<u8>>,
}

/// Exact metadata-only request to promote one store-owned WholeBlob candidate.
///
/// The candidate body, digest, catalog facts, and compaction intents were
/// already committed by [`PreparedWholeBlobProvisionalTail`]. This carrier
/// deliberately has no `Session` or serialized artifact, so the final boundary
/// cannot encode or hash the accumulated document a second time.
#[derive(Debug, Clone)]
pub struct PreparedWholeBlobProvisionalPromotion {
    authority: WholeBlobProvisionalTailAuthority,
    conversation_digest: String,
    message_count: u64,
}

impl PreparedWholeBlobProvisionalPromotion {
    pub fn prepare(
        checkpoint: meerkat_core::RunCheckpointReceipt,
        run_id: &RunId,
    ) -> Result<Self, RuntimeStoreError> {
        let authority = checkpoint.whole_blob().cloned().ok_or_else(|| {
            RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: checkpoint.session_id().to_string(),
                detail: "HeadCanonical checkpoint cannot authorize WholeBlob promotion".to_string(),
            }
        })?;
        if authority.run_id() != run_id {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: authority.session_id().to_string(),
                detail: "WholeBlob promotion run differs from store-issued candidate run"
                    .to_string(),
            });
        }
        Ok(Self {
            authority,
            conversation_digest: checkpoint.conversation_digest().to_string(),
            message_count: checkpoint.message_count(),
        })
    }

    #[must_use]
    pub fn authority(&self) -> &WholeBlobProvisionalTailAuthority {
        &self.authority
    }

    #[must_use]
    pub(crate) fn into_parts(self) -> (WholeBlobProvisionalTailAuthority, String, u64) {
        (self.authority, self.conversation_digest, self.message_count)
    }
}

/// Exact WholeBlob recovery action bound to one store-owned provisional row.
///
/// A completed candidate carries no body: the store promotes its existing
/// candidate allocation with metadata-only writes. An interrupted repair
/// carries the one sealed successor artifact produced during classification;
/// the backend installs those exact bytes without encoding or hashing again.
#[derive(Debug, Clone)]
pub(crate) struct PreparedWholeBlobRecoveryPromotion {
    authority: WholeBlobProvisionalTailAuthority,
    repaired_snapshot: Option<PreparedWholeBlobSnapshot>,
}

impl PreparedWholeBlobRecoveryPromotion {
    fn prepare(
        repaired_document: Option<&BoundSessionCommit>,
        evidence: &PreparedRecoveryEvidence,
    ) -> Result<Self, RuntimeStoreError> {
        let (
            base_store_revision,
            base_blob_sha256,
            candidate_blob_sha256,
            candidate_sequence,
            recovered_blob_sha256,
        ) = evidence.whole_blob_authority_transition().ok_or_else(|| {
            RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: evidence.session_id().to_string(),
                detail: "HeadCanonical recovery evidence cannot authorize WholeBlob promotion"
                    .to_string(),
            }
        })?;
        let authority = WholeBlobProvisionalTailAuthority::issued(
            evidence.session_id().clone(),
            base_store_revision,
            base_blob_sha256.to_string(),
            evidence.candidate_run_id().clone(),
            candidate_blob_sha256.to_string(),
            candidate_sequence,
        )
        .map_err(
            |error| RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: evidence.session_id().to_string(),
                detail: error.to_string(),
            },
        )?;
        let repaired_snapshot = if recovered_blob_sha256 == candidate_blob_sha256 {
            if repaired_document.is_some() {
                return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                    runtime_id: evidence.session_id().to_string(),
                    detail: "completed WholeBlob recovery must not carry a materialized body"
                        .to_string(),
                });
            }
            None
        } else {
            let document = repaired_document.ok_or_else(|| {
                RuntimeStoreError::SessionPersistenceAuthorityConflict {
                    runtime_id: evidence.session_id().to_string(),
                    detail: "WholeBlob repair has no sealed successor artifact".to_string(),
                }
            })?;
            let prepared = prepared_whole_blob_snapshot(document)?;
            if prepared.session().id() != evidence.session_id()
                || prepared.blob_sha256() != recovered_blob_sha256
            {
                return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                    runtime_id: evidence.session_id().to_string(),
                    detail: "WholeBlob repaired artifact differs from sealed recovery authority"
                        .to_string(),
                });
            }
            Some(prepared)
        };
        Ok(Self {
            authority,
            repaired_snapshot,
        })
    }

    pub(crate) fn into_parts(
        self,
    ) -> (
        WholeBlobProvisionalTailAuthority,
        Option<PreparedWholeBlobSnapshot>,
    ) {
        (self.authority, self.repaired_snapshot)
    }
}

/// Opaque metadata-only promotion of an already-applied HeadCanonical tail.
///
/// The physical rows were committed by the checkpoint CAS named by
/// `authority`; final runtime commit consumes only this fixed-size receipt.
#[derive(Debug, Clone)]
pub struct PreparedHeadCanonicalProvisionalPromotion {
    checkpoint: meerkat_core::RunCheckpointReceipt,
    authority: HeadCanonicalProvisionalTailAuthority,
}

impl PreparedHeadCanonicalProvisionalPromotion {
    pub fn prepare(
        checkpoint: meerkat_core::RunCheckpointReceipt,
        run_id: &RunId,
    ) -> Result<Self, RuntimeStoreError> {
        let authority = checkpoint.head_canonical().cloned().ok_or_else(|| {
            RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: checkpoint.session_id().to_string(),
                detail: "HeadCanonical promotion received a WholeBlob checkpoint".to_string(),
            }
        })?;
        if authority.run_id() != run_id {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: authority.session_id().to_string(),
                detail: "HeadCanonical promotion run differs from store-issued physical tail"
                    .to_string(),
            });
        }
        Ok(Self {
            checkpoint,
            authority,
        })
    }

    #[must_use]
    pub fn authority(&self) -> &HeadCanonicalProvisionalTailAuthority {
        &self.authority
    }

    #[must_use]
    pub fn checkpoint(&self) -> &meerkat_core::RunCheckpointReceipt {
        &self.checkpoint
    }

    #[must_use]
    pub(crate) fn into_parts(
        self,
    ) -> (
        meerkat_core::RunCheckpointReceipt,
        HeadCanonicalProvisionalTailAuthority,
    ) {
        (self.checkpoint, self.authority)
    }
}

impl CommittedWholeBlobProvisionalTail {
    pub(crate) fn new(
        authority: WholeBlobProvisionalTailAuthority,
        candidate_bytes: Arc<Vec<u8>>,
    ) -> Self {
        Self {
            authority,
            candidate_bytes,
        }
    }

    #[must_use]
    pub fn authority(&self) -> &WholeBlobProvisionalTailAuthority {
        &self.authority
    }

    #[must_use]
    pub fn candidate_bytes(&self) -> &[u8] {
        self.candidate_bytes.as_ref()
    }

    #[must_use]
    pub fn candidate_bytes_arc(&self) -> Arc<Vec<u8>> {
        Arc::clone(&self.candidate_bytes)
    }
}

impl CommittedWholeBlobSnapshot {
    pub(crate) fn new(
        bytes: Arc<Vec<u8>>,
        authority: WholeBlobStoreAuthority,
    ) -> Result<Self, RuntimeStoreError> {
        let decoded =
            meerkat_core::Session::decode_whole_blob_document(bytes.as_ref()).map_err(|error| {
                RuntimeStoreError::ReadFailed(format!(
                    "WholeBlob body is not a valid current Session: {error}"
                ))
            })?;
        if decoded.row_sha256_token() != authority.blob_sha256() {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: authority.session_id().to_string(),
                detail: "WholeBlob body digest differs from store authority".to_string(),
            });
        }
        let session = Arc::new(decoded.into_session());
        if session.id() != authority.session_id() {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: authority.session_id().to_string(),
                detail: "WholeBlob body session differs from store authority".to_string(),
            });
        }
        Ok(Self {
            session,
            bytes,
            authority,
        })
    }

    /// Typed domain document decoded from these exact committed bytes.
    #[must_use]
    pub fn session(&self) -> &meerkat_core::Session {
        self.session.as_ref()
    }

    /// Shared typed domain document decoded from these exact committed bytes.
    #[must_use]
    pub fn session_arc(&self) -> Arc<meerkat_core::Session> {
        Arc::clone(&self.session)
    }

    #[must_use]
    pub fn bytes(&self) -> &[u8] {
        self.bytes.as_ref()
    }

    #[must_use]
    pub fn bytes_arc(&self) -> Arc<Vec<u8>> {
        Arc::clone(&self.bytes)
    }

    #[must_use]
    pub fn authority(&self) -> &WholeBlobStoreAuthority {
        &self.authority
    }

    #[must_use]
    pub fn into_parts(
        self,
    ) -> (
        Arc<meerkat_core::Session>,
        Arc<Vec<u8>>,
        WholeBlobStoreAuthority,
    ) {
        (self.session, self.bytes, self.authority)
    }
}

impl std::fmt::Display for RuntimeSessionPersistenceProfile {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::WholeBlobV1 => f.write_str("whole_blob_v1"),
            Self::HeadCanonicalV1 => f.write_str("head_canonical_v1"),
        }
    }
}

/// Store-issued identity of one committed HeadCanonical boundary.
///
/// The small head carries the exact row, rewrite, graph, component, and
/// metadata-prefix facts. Currentness is the store revision plus the exact
/// committed head token; no fact inside a materialized `Session` participates
/// in authority.
#[derive(Debug, Clone, PartialEq)]
pub struct HeadCanonicalStoreAuthority {
    authority_version: u16,
    session_id: meerkat_core::types::SessionId,
    store_revision: u64,
    boundary_head: meerkat_core::session_store::SessionHead,
    committed_head_token: String,
}

impl HeadCanonicalStoreAuthority {
    pub const VERSION: u16 = 1;

    /// Validate one fixed-size authority record and its exact canonical head.
    ///
    /// Construction validates representation and identity only; it does not
    /// certify that the record is current or authorize a transition. The value
    /// becomes authority only when an honest [`RuntimeStore`] returns it from
    /// the atomic observation or commit that proved the matching physical
    /// head and store revision.
    pub fn from_store_record(
        authority_version: u16,
        session_id: meerkat_core::types::SessionId,
        store_revision: u64,
        boundary_head: meerkat_core::session_store::SessionHead,
        committed_head_token: String,
    ) -> Result<Self, RuntimeStoreError> {
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: session_id.to_string(),
            detail,
        };
        if authority_version != Self::VERSION
            || store_revision == 0
            || committed_head_token.is_empty()
        {
            return Err(conflict(
                "HeadCanonical authority requires the current version, nonzero store revision, \
                 and head token"
                    .to_string(),
            ));
        }
        if boundary_head.id != session_id {
            return Err(conflict(format!(
                "HeadCanonical boundary belongs to {}, not {session_id}",
                boundary_head.id
            )));
        }
        let row_prefix = boundary_head.message_row_prefix.as_ref().ok_or_else(|| {
            conflict("HeadCanonical boundary has no exact message-row prefix".to_string())
        })?;
        if row_prefix.row_count() != boundary_head.message_count {
            return Err(conflict(
                "HeadCanonical boundary message count and row prefix differ".to_string(),
            ));
        }
        if boundary_head.rewrite_prefix.occurrence_count() != boundary_head.rewrite_count {
            return Err(conflict(
                "HeadCanonical boundary rewrite count and prefix differ".to_string(),
            ));
        }
        let derived = meerkat_core::session_head_cas_token(&boundary_head)
            .map_err(|error| conflict(format!("HeadCanonical head token is invalid: {error}")))?;
        if derived != committed_head_token {
            return Err(conflict(
                "store-issued HeadCanonical token differs from the exact boundary head".to_string(),
            ));
        }
        Ok(Self {
            authority_version,
            session_id,
            store_revision,
            boundary_head,
            committed_head_token,
        })
    }

    pub(crate) fn issued(
        session_id: meerkat_core::types::SessionId,
        store_revision: u64,
        boundary_head: meerkat_core::session_store::SessionHead,
        committed_head_token: String,
    ) -> Result<Self, RuntimeStoreError> {
        Self::from_store_record(
            Self::VERSION,
            session_id,
            store_revision,
            boundary_head,
            committed_head_token,
        )
    }

    #[must_use]
    pub const fn authority_version(&self) -> u16 {
        self.authority_version
    }

    #[must_use]
    pub fn session_id(&self) -> &meerkat_core::types::SessionId {
        &self.session_id
    }

    #[must_use]
    pub const fn store_revision(&self) -> u64 {
        self.store_revision
    }

    #[must_use]
    pub fn boundary_head(&self) -> &meerkat_core::session_store::SessionHead {
        &self.boundary_head
    }

    #[must_use]
    pub fn committed_head_token(&self) -> &str {
        &self.committed_head_token
    }
}

/// Opaque intent written before a HeadCanonical physical-head CAS.
///
/// The carrier binds the exact committed parent, explicit run identity, and
/// exact successor head/token. It also captures the bounded catalog,
/// compaction, conversation-digest, and message-count projections from that
/// same live successor. Backends persist those facts with the fixed-size
/// authority; the physical SessionStore CAS realizes the already-bound head
/// separately.
#[derive(Debug, Clone)]
pub struct PreparedHeadCanonicalProvisionalTail {
    committed: HeadCanonicalStoreAuthority,
    run_id: RunId,
    successor_head: meerkat_core::session_store::SessionHead,
    successor_head_token: String,
    candidate_message_count: usize,
    candidate_conversation_digest: String,
    catalog_entry: RuntimeSessionCatalogEntry,
    compaction_projection_intents: Vec<meerkat_core::CompactionProjectionIntent>,
}

impl PreparedHeadCanonicalProvisionalTail {
    pub fn prepare(
        committed: HeadCanonicalStoreAuthority,
        run_id: RunId,
        successor_head: &meerkat_core::session_store::SessionHead,
        successor_head_token: &str,
        candidate_session: &meerkat_core::Session,
    ) -> Result<Self, RuntimeStoreError> {
        let session_id = committed.session_id().clone();
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: session_id.to_string(),
            detail,
        };
        if successor_head.id != session_id || successor_head_token.is_empty() {
            return Err(conflict(
                "HeadCanonical provisional intent names the wrong session or an empty successor"
                    .to_string(),
            ));
        }
        let derived = meerkat_core::session_head_cas_token(successor_head).map_err(|error| {
            conflict(format!(
                "HeadCanonical provisional successor is invalid: {error}"
            ))
        })?;
        if derived != successor_head_token
            || successor_head_token == committed.committed_head_token()
        {
            return Err(conflict(
                "HeadCanonical provisional successor token is not the exact distinct target head"
                    .to_string(),
            ));
        }
        if candidate_session.id() != &session_id
            || candidate_session.messages().len() as u64 != successor_head.message_count
            || candidate_session.version() != successor_head.version
            || candidate_session.created_at() != successor_head.created_at
            || candidate_session.updated_at() != successor_head.updated_at
            || candidate_session.total_usage() != successor_head.usage
            || !successor_head
                .matches_session_metadata(candidate_session)
                .map_err(|error| {
                    conflict(format!(
                        "HeadCanonical provisional candidate metadata is invalid: {error}"
                    ))
                })?
        {
            return Err(conflict(
                "HeadCanonical provisional successor does not describe the exact candidate Session"
                    .to_string(),
            ));
        }
        let candidate_conversation_digest =
            candidate_session
                .transcript_content_digest()
                .map_err(|error| {
                    conflict(format!(
                        "HeadCanonical provisional candidate transcript is invalid: {error}"
                    ))
                })?;
        if candidate_conversation_digest != successor_head.head_revision {
            return Err(conflict(
                "HeadCanonical provisional candidate digest differs from its successor head"
                    .to_string(),
            ));
        }
        let catalog_entry = RuntimeSessionCatalogEntry::from_session(
            candidate_session,
            RuntimeSessionPersistenceProfile::HeadCanonicalV1,
            None,
        )?;
        let compaction_projection_intents =
            validated_compaction_projection_intents(candidate_session)?;
        Ok(Self {
            committed,
            run_id,
            successor_head: successor_head.clone(),
            successor_head_token: successor_head_token.to_string(),
            candidate_message_count: candidate_session.messages().len(),
            candidate_conversation_digest,
            catalog_entry,
            compaction_projection_intents,
        })
    }

    #[must_use]
    pub(crate) fn committed(&self) -> &HeadCanonicalStoreAuthority {
        &self.committed
    }

    #[must_use]
    pub(crate) fn run_id(&self) -> &RunId {
        &self.run_id
    }

    #[must_use]
    pub(crate) fn successor_head(&self) -> &meerkat_core::session_store::SessionHead {
        &self.successor_head
    }

    #[must_use]
    pub(crate) fn successor_head_token(&self) -> &str {
        &self.successor_head_token
    }

    #[must_use]
    pub(crate) const fn candidate_message_count(&self) -> usize {
        self.candidate_message_count
    }

    #[must_use]
    pub(crate) fn candidate_conversation_digest(&self) -> &str {
        &self.candidate_conversation_digest
    }

    #[must_use]
    pub(crate) fn catalog_entry(&self) -> &RuntimeSessionCatalogEntry {
        &self.catalog_entry
    }

    #[must_use]
    pub(crate) fn compaction_projection_intents(
        &self,
    ) -> &[meerkat_core::CompactionProjectionIntent] {
        &self.compaction_projection_intents
    }
}

/// Singular store-issued committed authority for a runtime session.
#[derive(Debug, Clone, PartialEq)]
pub enum RuntimeSessionAuthority {
    WholeBlob(WholeBlobStoreAuthority),
    HeadCanonical(HeadCanonicalStoreAuthority),
}

impl RuntimeSessionAuthority {
    #[must_use]
    pub const fn profile(&self) -> RuntimeSessionPersistenceProfile {
        match self {
            Self::WholeBlob(_) => RuntimeSessionPersistenceProfile::WholeBlobV1,
            Self::HeadCanonical(_) => RuntimeSessionPersistenceProfile::HeadCanonicalV1,
        }
    }

    #[must_use]
    pub fn session_id(&self) -> &meerkat_core::types::SessionId {
        match self {
            Self::WholeBlob(authority) => authority.session_id(),
            Self::HeadCanonical(authority) => authority.session_id(),
        }
    }

    #[must_use]
    pub fn whole_blob(&self) -> Option<&WholeBlobStoreAuthority> {
        match self {
            Self::WholeBlob(authority) => Some(authority),
            Self::HeadCanonical(_) => None,
        }
    }

    #[must_use]
    pub fn head_canonical(&self) -> Option<&HeadCanonicalStoreAuthority> {
        match self {
            Self::WholeBlob(_) => None,
            Self::HeadCanonical(authority) => Some(authority),
        }
    }
}

/// Store-owned source for one durable-tail recovery pass.
///
/// The public recovery API accepts only a session identity. A backend that
/// owns both runtime authority and canonical session rows constructs this
/// opaque carrier from one transactional snapshot after checking the exact
/// retained boundary head, current physical head, and both complete
/// materializations. Callers cannot substitute a hand-authored Session or
/// head row.
#[derive(Debug, Clone)]
pub struct PreparedDurableTailRecoverySource {
    runtime_authority: RuntimeSessionAuthority,
    provisional_authority: Option<HeadCanonicalProvisionalTailAuthority>,
    provisional_target_applied: bool,
    committed_session: Arc<meerkat_core::Session>,
    physical_head: meerkat_core::session_store::SessionHead,
    physical_head_cas_token: String,
    physical_session: Arc<meerkat_core::Session>,
}

impl PreparedDurableTailRecoverySource {
    pub(crate) fn new(
        runtime_authority: RuntimeSessionAuthority,
        provisional_authority: Option<HeadCanonicalProvisionalTailAuthority>,
        committed_materialization: meerkat_core::VerifiedSessionHeadMaterialization,
        physical_materialization: meerkat_core::VerifiedSessionHeadMaterialization,
    ) -> Result<Self, RuntimeStoreError> {
        let committed_session = Arc::clone(committed_materialization.session());
        let physical_head = physical_materialization.head().clone();
        let physical_session = Arc::clone(physical_materialization.session());
        let runtime_id = runtime_authority.session_id().to_string();
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: runtime_id.clone(),
            detail,
        };
        if runtime_authority.profile() != RuntimeSessionPersistenceProfile::HeadCanonicalV1 {
            return Err(conflict(
                "durable-tail source requires head-canonical session ownership".to_string(),
            ));
        }
        let committed_authority = runtime_authority
            .head_canonical()
            .ok_or_else(|| conflict("runtime authority is not HeadCanonical".to_string()))?;
        let boundary_head = committed_authority.boundary_head();
        if committed_materialization.head() != boundary_head {
            return Err(conflict(
                "verified committed materialization belongs to a different retained boundary head"
                    .to_string(),
            ));
        }
        let boundary_row_prefix = boundary_head.message_row_prefix.as_ref().ok_or_else(|| {
            conflict("runtime boundary has no exact message-row prefix authority".to_string())
        })?;
        if physical_materialization
            .exact_row_prefix_at(boundary_head.message_count)
            .as_ref()
            != Some(boundary_row_prefix)
        {
            return Err(conflict(
                "physical recovery materialization does not retain the runtime boundary's exact row prefix"
                    .to_string(),
            ));
        }
        if committed_session.id() != runtime_authority.session_id()
            || physical_session.id() != runtime_authority.session_id()
            || &physical_head.id != runtime_authority.session_id()
        {
            return Err(conflict(
                "durable-tail source identities do not all match runtime authority".to_string(),
            ));
        }
        let committed_revision = committed_session
            .transcript_content_digest()
            .map_err(|error| conflict(format!("committed transcript is invalid: {error}")))?;
        let committed_metadata_matches = boundary_head
            .matches_session_metadata(&committed_session)
            .map_err(|error| {
                conflict(format!(
                    "committed recovery metadata identity is invalid: {error}"
                ))
            })?;
        if committed_session.messages().len() as u64 != boundary_head.message_count
            || committed_revision != boundary_head.head_revision
            || committed_session.version() != boundary_head.version
            || committed_session.created_at() != boundary_head.created_at
            || committed_session.updated_at() != boundary_head.updated_at
            || committed_session.total_usage() != boundary_head.usage
            || !committed_metadata_matches
        {
            return Err(conflict(format!(
                "committed recovery materialization differs from the exact retained boundary envelope \
                     (message_count={}, revision={}, version={}, created_at={}, updated_at={}, usage={}, metadata={})",
                committed_session.messages().len() as u64 == boundary_head.message_count,
                committed_revision == boundary_head.head_revision,
                committed_session.version() == boundary_head.version,
                committed_session.created_at() == boundary_head.created_at,
                committed_session.updated_at() == boundary_head.updated_at,
                committed_session.total_usage() == boundary_head.usage,
                committed_metadata_matches,
            )));
        }
        let physical_revision = physical_session
            .transcript_content_digest()
            .map_err(|error| conflict(format!("physical transcript is invalid: {error}")))?;
        let physical_metadata_matches = physical_head
            .matches_session_metadata(&physical_session)
            .map_err(|error| {
            conflict(format!(
                "physical recovery metadata identity is invalid: {error}"
            ))
        })?;
        if physical_session.messages().len() as u64 != physical_head.message_count
            || physical_revision != physical_head.head_revision
            || physical_session.version() != physical_head.version
            || physical_session.created_at() != physical_head.created_at
            || physical_session.updated_at() != physical_head.updated_at
            || physical_session.total_usage() != physical_head.usage
            || !physical_metadata_matches
        {
            return Err(conflict(
                "physical recovery materialization differs from the exact current canonical envelope"
                    .to_string(),
            ));
        }
        let Some(physical_row_prefix) = physical_head.message_row_prefix.as_ref() else {
            return Err(conflict(
                "physical recovery head has no exact message-row prefix authority".to_string(),
            ));
        };
        if physical_row_prefix.row_count() != physical_head.message_count {
            return Err(conflict(
                "physical recovery head message count and exact row prefix differ".to_string(),
            ));
        }
        let physical_head_cas_token = meerkat_core::session_head_cas_token(&physical_head)
            .map_err(|error| {
                conflict(format!("physical recovery head token is invalid: {error}"))
            })?;
        if committed_authority.committed_head_token()
            != meerkat_core::session_head_cas_token(boundary_head)
                .map_err(|error| conflict(format!("committed head token is invalid: {error}")))?
        {
            return Err(conflict(
                "committed runtime authority token differs from its retained boundary head"
                    .to_string(),
            ));
        }
        let provisional_target_applied = match (
            &provisional_authority,
            physical_head == *boundary_head,
        ) {
            (None, true) => false,
            (None, false) => {
                return Err(conflict(
                    "newer physical head has no store-issued provisional authority".to_string(),
                ));
            }
            (Some(provisional), aligned) => {
                let first_provisional_revision = committed_authority
                    .store_revision()
                    .checked_add(1)
                    .ok_or_else(|| {
                        conflict("HeadCanonical store revision exhausted".to_string())
                    })?;
                let target_applied = provisional.physical_head_token() == physical_head_cas_token;
                if provisional.authority_version() != HeadCanonicalProvisionalTailAuthority::VERSION
                    || provisional.session_id() != runtime_authority.session_id()
                    || provisional.base_store_revision() != committed_authority.store_revision()
                    || provisional.base_committed_head_token()
                        != committed_authority.committed_head_token()
                    || (aligned
                        && (physical_head_cas_token != committed_authority.committed_head_token()
                            || provisional.physical_store_revision() != first_provisional_revision))
                    || (!aligned
                        && !target_applied
                        && provisional.physical_store_revision() <= first_provisional_revision)
                {
                    return Err(conflict(
                        "provisional authority does not bind the exact committed parent and physical head"
                        .to_string(),
                    ));
                }
                target_applied
            }
        };
        Ok(Self {
            runtime_authority,
            provisional_authority,
            provisional_target_applied,
            committed_session,
            physical_head,
            physical_head_cas_token,
            physical_session,
        })
    }

    pub(crate) fn runtime_authority(&self) -> &RuntimeSessionAuthority {
        &self.runtime_authority
    }

    pub(crate) fn committed_session(&self) -> &Arc<meerkat_core::Session> {
        &self.committed_session
    }

    pub(crate) fn provisional_authority(&self) -> Option<&HeadCanonicalProvisionalTailAuthority> {
        self.provisional_authority.as_ref()
    }

    pub(crate) const fn provisional_target_applied(&self) -> bool {
        self.provisional_target_applied
    }

    pub(crate) fn physical_head(&self) -> &meerkat_core::session_store::SessionHead {
        &self.physical_head
    }

    pub(crate) fn physical_head_cas_token(&self) -> &str {
        &self.physical_head_cas_token
    }

    pub(crate) fn physical_session(&self) -> &Arc<meerkat_core::Session> {
        &self.physical_session
    }
}

/// One exact durable receipt row admitted to recovery classification.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreparedRecoveryReceiptSource {
    receipt: RunBoundaryReceipt,
    exact_row_token: String,
}

impl PreparedRecoveryReceiptSource {
    pub(crate) fn from_serialized_row(bytes: &[u8]) -> Result<Self, RuntimeStoreError> {
        let receipt = serde_json::from_slice(bytes).map_err(|error| {
            RuntimeStoreError::ReadFailed(format!("invalid durable recovery receipt row: {error}"))
        })?;
        Ok(Self {
            receipt,
            exact_row_token: format!("receipt-row-sha256:{:x}", Sha256::digest(bytes)),
        })
    }

    pub(crate) fn receipt(&self) -> &RunBoundaryReceipt {
        &self.receipt
    }

    pub(crate) fn exact_row_token(&self) -> &str {
        &self.exact_row_token
    }
}

/// Sealed one-time enrichment of a supported-floor digestless receipt.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreparedRecoveryReceiptDigestEnrichment {
    original_receipt: RunBoundaryReceipt,
    original_exact_row_token: String,
    derived_conversation_digest: String,
}

impl PreparedRecoveryReceiptDigestEnrichment {
    pub(crate) fn new(
        source: &PreparedRecoveryReceiptSource,
        derived_conversation_digest: String,
    ) -> Result<Self, RuntimeStoreError> {
        if source.receipt.conversation_digest.is_some() || derived_conversation_digest.is_empty() {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: source.receipt.run_id.to_string(),
                detail: "receipt enrichment must replace exactly one missing digest".to_string(),
            });
        }
        Ok(Self {
            original_receipt: source.receipt.clone(),
            original_exact_row_token: source.exact_row_token.clone(),
            derived_conversation_digest,
        })
    }

    pub(crate) fn original_receipt(&self) -> &RunBoundaryReceipt {
        &self.original_receipt
    }

    pub(crate) fn original_exact_row_token(&self) -> &str {
        &self.original_exact_row_token
    }

    pub(crate) fn derived_conversation_digest(&self) -> &str {
        &self.derived_conversation_digest
    }

    pub(crate) fn enriched_receipt(&self) -> RunBoundaryReceipt {
        let mut receipt = self.original_receipt.clone();
        receipt.conversation_digest = Some(self.derived_conversation_digest.clone());
        receipt
    }
}

/// Whether a prepared boundary was newly written or proven from durable store
/// authority.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PreparedRuntimeSessionCommitOutcome {
    /// This call installed the boundary.
    Applied,
    /// All durable authority and receipt witnesses already matched exactly.
    AlreadyAppliedExact,
    /// The exact released 0.8.10 receipt and every still-observable committed
    /// effect matched, and the v1 -> v2 migration marker authorized minting the
    /// first current request witness. The released schema did not retain prior
    /// CAS preconditions, so this is deliberately not reported as an exact
    /// retry of the original request.
    AlreadyAppliedReleasedEquivalent,
}

/// Exact convergence result for a machine-authorized recovery boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryCommitStatus {
    /// This invocation installed the recovery boundary.
    Committed,
    /// A prior invocation installed byte-exact evidence and receipt state.
    AlreadyCommittedExact,
}

/// Result of an atomic prepared session-boundary commit.
#[derive(Debug, Clone, PartialEq)]
pub struct PreparedRuntimeSessionCommitResult {
    profile: RuntimeSessionPersistenceProfile,
    outcome: PreparedRuntimeSessionCommitOutcome,
    recovery_status: Option<RecoveryCommitStatus>,
    downstream_projection_required: bool,
    authority: Option<RuntimeSessionAuthority>,
}

impl PreparedRuntimeSessionCommitResult {
    /// Construct the result of a boundary that committed session authority.
    #[must_use]
    pub fn committed(authority: RuntimeSessionAuthority) -> Self {
        let profile = authority.profile();
        Self {
            profile,
            outcome: PreparedRuntimeSessionCommitOutcome::Applied,
            recovery_status: None,
            // RuntimeStore is the sole full-body authority for WholeBlob and
            // owns the small catalog projection for both profiles. No boundary
            // requires a downstream SessionStore body mirror.
            downstream_projection_required: false,
            authority: Some(authority),
        }
    }

    /// Construct the result of a receipt/input-only boundary.
    #[must_use]
    pub const fn receipt_only(profile: RuntimeSessionPersistenceProfile) -> Self {
        Self {
            profile,
            outcome: PreparedRuntimeSessionCommitOutcome::Applied,
            recovery_status: None,
            downstream_projection_required: false,
            authority: None,
        }
    }

    /// Construct the exact result of an atomic recovery boundary.
    #[must_use]
    pub fn recovery(authority: RuntimeSessionAuthority, status: RecoveryCommitStatus) -> Self {
        let mut result = Self::committed(authority);
        result.recovery_status = Some(status);
        if status == RecoveryCommitStatus::AlreadyCommittedExact {
            result.outcome = PreparedRuntimeSessionCommitOutcome::AlreadyAppliedExact;
        }
        result
    }

    /// Reclassify a result after the store proved an exact durable retry.
    #[must_use]
    pub fn already_applied_exact(mut self) -> Self {
        self.outcome = PreparedRuntimeSessionCommitOutcome::AlreadyAppliedExact;
        self
    }

    /// Reclassify a result after one migration-authorized released-boundary
    /// adoption. Subsequent retries of the newly witnessed request are exact.
    #[must_use]
    pub fn already_applied_released_equivalent(mut self) -> Self {
        self.outcome = PreparedRuntimeSessionCommitOutcome::AlreadyAppliedReleasedEquivalent;
        self
    }

    /// Representation that became authoritative in the atomic boundary.
    #[must_use]
    pub const fn profile(&self) -> RuntimeSessionPersistenceProfile {
        self.profile
    }

    /// Whether this invocation installed the boundary, proved an exact retry,
    /// or adopted an effect-equivalent released boundary.
    #[must_use]
    pub const fn outcome(&self) -> PreparedRuntimeSessionCommitOutcome {
        self.outcome
    }

    /// Exact recovery convergence, when this was a recovery boundary.
    #[must_use]
    pub const fn recovery_status(&self) -> Option<RecoveryCommitStatus> {
        self.recovery_status
    }

    /// Whether the caller must publish a separate compatibility projection
    /// after this commit.
    #[must_use]
    pub const fn downstream_projection_required(&self) -> bool {
        self.downstream_projection_required
    }

    /// Exact session authority committed in this boundary, or `None` when the
    /// successful boundary carried receipt/input state only.
    #[must_use]
    pub fn authority(&self) -> Option<&RuntimeSessionAuthority> {
        self.authority.as_ref()
    }
}

/// Errors from RuntimeStore operations.
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum RuntimeStoreError {
    /// Write failed.
    #[error("Store write failed: {0}")]
    WriteFailed(String),
    /// Read failed.
    #[error("Store read failed: {0}")]
    ReadFailed(String),
    /// The explicit session-store key does not match the serialized session.
    #[error("Session store key mismatch: expected {expected}, actual {actual}")]
    SessionKeyMismatch {
        expected: meerkat_core::types::SessionId,
        actual: meerkat_core::types::SessionId,
    },
    /// Not found.
    #[error("Not found: {0}")]
    NotFound(String),
    /// Operation is not supported by this store implementation.
    #[error("Unsupported store operation: {0}")]
    Unsupported(String),
    /// A non-whole-blob store declared a profile but did not implement the
    /// prepared boundary method required to commit that representation.
    #[error("runtime store profile '{profile}' must override commit_prepared_session_boundary")]
    PreparedSessionBoundaryRequiresOverride {
        profile: RuntimeSessionPersistenceProfile,
    },
    /// Recovery needs a backend that can CAS both runtime authority and the
    /// independently observed physical session head in one transaction.
    #[error(
        "runtime store profile '{profile}' cannot atomically CAS the physical session head for prepared recovery"
    )]
    PreparedRecoveryRequiresAtomicPhysicalHeadCas {
        profile: RuntimeSessionPersistenceProfile,
    },
    /// A head-canonical store encountered durable whole-blob state that has
    /// not completed its explicit, observable profile-activation conversion.
    ///
    /// Ordinary boundary application must never perform this conversion
    /// implicitly: it can be O(document) and may take long enough to require
    /// deploy-facing progress reporting. Open the store through its explicit
    /// activation seam and retry only after that seam reports completion.
    #[error(
        "head-canonical profile activation is required for runtime '{runtime_id}' (state: {state})"
    )]
    HeadCanonicalActivationRequired {
        /// Logical runtime whose frozen predecessor is not activated.
        runtime_id: String,
        /// Durable activation state (`not_started`, `in_progress`, or a
        /// backend-specific refusal detail).
        state: String,
    },
    /// Persisted session authority conflicts with the configured profile,
    /// checkpoint, canonical head, frozen legacy BLOB, or mutation shape.
    #[error("session persistence authority conflict for runtime '{runtime_id}': {detail}")]
    SessionPersistenceAuthorityConflict { runtime_id: String, detail: String },
    /// A detached producer attempted to persist an ops snapshot after the
    /// matching epoch was atomically retired by unregister.
    #[error("Ops lifecycle epoch {epoch_id} for runtime {runtime_id} is retired")]
    OpsLifecycleEpochRetired {
        runtime_id: String,
        epoch_id: meerkat_core::RuntimeEpochId,
    },
    /// An unregister-finalization commit may have become durable, but the
    /// backend could not authoritatively classify its outcome.
    ///
    /// Callers must retry the idempotent atomic finalization and must not
    /// publish a compensating lifecycle rollback for this error.
    #[error("Unregister finalization outcome is unknown: {0}")]
    UnregisterFinalizationOutcomeUnknown(String),
    /// Runtime snapshot CAS rejected a stale transcript rewrite.
    #[error("Transcript revision conflict: expected {expected}, actual {actual}")]
    TranscriptRevisionConflict { expected: String, actual: String },
    /// An atomic boundary commit carried a session snapshot that was already
    /// superseded by the durable append-only head. Callers must observe this
    /// as a failed commit rather than mistaking a no-op for publication.
    #[error("Session snapshot for runtime '{runtime_id}' was superseded by the durable head")]
    SessionSnapshotSuperseded { runtime_id: String },
    /// The requested exact input-state batch CAS has an invalid row/key shape.
    #[error("Invalid input-state batch compare-and-swap: {reason}")]
    InvalidInputStateBatchCas { reason: String },
    /// The maintained idempotency-key index cannot prove a unique answer while
    /// a source input row's key identity is unindexable.
    ///
    /// This is durable corruption evidence, not an authoritative miss and not
    /// a transient read failure. Callers must fail closed until the named row
    /// is repaired or quarantined through an operator-authorized workflow.
    #[error(
        "input idempotency index for runtime '{runtime_id}' cannot prove key '{key}' while \
         input row '{evidence_input_id}' is unindexable: {reason}"
    )]
    InputIdempotencyIndexUncertain {
        runtime_id: String,
        key: String,
        evidence_input_id: String,
        reason: String,
    },
    /// A lifecycle record was observed exactly, but replacing it would risk
    /// lowering or fabricating durable runtime fencing authority.
    ///
    /// This is a permanent reconciliation result for the observed row, not a
    /// transport retry. Callers should project RepairBlocked while retaining
    /// the evidence digest for operator repair.
    #[error("Machine lifecycle repair is blocked: {detail}")]
    MachineLifecycleRepairBlocked {
        evidence_digest: Option<String>,
        detail: String,
    },
    /// The file's schema ledger records a version newer than this binary
    /// supports: a newer binary migrated the file and this one must refuse
    /// it (typed, health-visible refusal — never a crash loop).
    #[error(
        "schema for domain '{domain}' is from the future: file has version {found}, \
         this binary supports up to {supported}"
    )]
    SchemaFromTheFuture {
        domain: String,
        found: i64,
        supported: i64,
    },
    /// The exclusive maintenance fence is held for this database; storage is
    /// under offline maintenance.
    #[error("maintenance fence is held for '{path}'; storage is under offline maintenance")]
    MaintenanceFenceHeld { path: String },
    /// An input-state update carried an expected prior row version that no
    /// longer matches the stored row. The whole atomic boundary fails stale;
    /// nothing is written.
    #[error(
        "Input row version conflict for input '{input_id}': the stored row changed since it was observed"
    )]
    InputRowVersionConflict { input_id: String },
    /// The complete set of nonterminal input rows changed after recovery
    /// classified it. The whole recovery boundary fails stale; nothing is
    /// written.
    #[error(
        "Recovery input-set conflict for runtime '{runtime_id}': the nonterminal input set changed since it was observed"
    )]
    RecoveryInputSetConflict { runtime_id: String },
    /// A machine-lifecycle commit carried an expected prior row version that
    /// no longer matches the stored row. The whole atomic boundary fails
    /// stale; nothing is written.
    #[error(
        "Machine lifecycle version conflict for runtime '{runtime_id}': the stored row changed since it was observed"
    )]
    MachineLifecycleVersionConflict { runtime_id: String },
    /// Internal error.
    #[error("Internal error: {0}")]
    Internal(String),
}

/// Transactional updater for the runtime-owned OAuth login-flow payload snapshot.
pub type AuthOAuthFlowSnapshotUpdate<'a> =
    dyn FnMut(Option<&[u8]>) -> Result<Vec<u8>, RuntimeStoreError> + 'a;

/// Describes a serialized session snapshot for boundary and snapshot-only commits.
#[derive(Debug, Clone)]
pub struct SerializedSessionSnapshot {
    /// Immutable serialized session snapshot (opaque to RuntimeStore).
    ///
    /// The shared owner is part of the WholeBlob cost contract: a prepared
    /// boundary must carry the one materialized document through the atomic
    /// store verb without allocating and copying a second full buffer.
    pub session_snapshot: std::sync::Arc<Vec<u8>>,
}

fn recovery_class_name(
    class: crate::meerkat_machine::dsl::DurableTailRecoveryClass,
) -> &'static str {
    use crate::meerkat_machine::dsl::DurableTailRecoveryClass;
    match class {
        DurableTailRecoveryClass::CompletedCandidate => "completed_candidate",
        DurableTailRecoveryClass::InterruptedRepairableCandidate => {
            "interrupted_repairable_candidate"
        }
        DurableTailRecoveryClass::Ambiguous => "ambiguous",
    }
}

fn recovery_class_from_name(
    name: &str,
) -> Result<crate::meerkat_machine::dsl::DurableTailRecoveryClass, RuntimeStoreError> {
    use crate::meerkat_machine::dsl::DurableTailRecoveryClass;
    match name {
        "completed_candidate" => Ok(DurableTailRecoveryClass::CompletedCandidate),
        "interrupted_repairable_candidate" => {
            Ok(DurableTailRecoveryClass::InterruptedRepairableCandidate)
        }
        "ambiguous" => Ok(DurableTailRecoveryClass::Ambiguous),
        other => Err(RuntimeStoreError::ReadFailed(format!(
            "unknown committed recovery class '{other}'"
        ))),
    }
}

fn recovery_disposition_name(
    disposition: crate::meerkat_machine::dsl::DurableTailRecoveryDisposition,
) -> &'static str {
    use crate::meerkat_machine::dsl::DurableTailRecoveryDisposition;
    match disposition {
        DurableTailRecoveryDisposition::RefuseRecovery => "refuse_recovery",
        DurableTailRecoveryDisposition::CommitCompleted => "commit_completed",
        DurableTailRecoveryDisposition::RepairAndCommitInterrupted => {
            "repair_and_commit_interrupted"
        }
        DurableTailRecoveryDisposition::CommitCompletedRetainInputs => {
            "commit_completed_retain_inputs"
        }
        DurableTailRecoveryDisposition::HoldIntact => "hold_intact",
    }
}

fn recovery_disposition_from_name(
    name: &str,
) -> Result<crate::meerkat_machine::dsl::DurableTailRecoveryDisposition, RuntimeStoreError> {
    use crate::meerkat_machine::dsl::DurableTailRecoveryDisposition;
    match name {
        "refuse_recovery" => Ok(DurableTailRecoveryDisposition::RefuseRecovery),
        "commit_completed" => Ok(DurableTailRecoveryDisposition::CommitCompleted),
        "repair_and_commit_interrupted" => {
            Ok(DurableTailRecoveryDisposition::RepairAndCommitInterrupted)
        }
        "commit_completed_retain_inputs" => {
            Ok(DurableTailRecoveryDisposition::CommitCompletedRetainInputs)
        }
        "hold_intact" => Ok(DurableTailRecoveryDisposition::HoldIntact),
        other => Err(RuntimeStoreError::ReadFailed(format!(
            "unknown committed recovery disposition '{other}'"
        ))),
    }
}

fn recovery_hash_part(hasher: &mut Sha256, label: &str, bytes: &[u8]) {
    hasher.update((label.len() as u64).to_be_bytes());
    hasher.update(label.as_bytes());
    hasher.update((bytes.len() as u64).to_be_bytes());
    hasher.update(bytes);
}

fn lifecycle_expected_version_token(
    lifecycle: &MachineLifecycleCommit,
) -> Result<String, RuntimeStoreError> {
    match lifecycle.expected_version() {
        Some(MachineLifecycleExpectedVersion::Missing) => Ok("missing".to_string()),
        Some(MachineLifecycleExpectedVersion::Version(version)) => {
            Ok(format!("version:{}", version.as_str()))
        }
        None => Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: "prepared-recovery".to_string(),
            detail: "recovery lifecycle commit is not fenced on an exact observed row".to_string(),
        }),
    }
}

fn recovery_sha256_token(bytes: &[u8]) -> String {
    format!("sha256:{:x}", Sha256::digest(bytes))
}

fn is_canonical_sha256_token(token: &str) -> bool {
    let Some(hex) = token.strip_prefix("sha256:") else {
        return false;
    };
    hex.len() == 64
        && hex
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}

/// Exact stored input row resolved through the durable idempotency-key index.
#[derive(Debug, Clone)]
pub struct ExactInputStateObservation {
    state: StoredInputState,
    exact_row_digest: String,
}

impl ExactInputStateObservation {
    /// Bind a decoded state to the exact bytes the store resolved.
    pub fn from_exact_stored_row(
        state: StoredInputState,
        exact_row_digest: String,
    ) -> Result<Self, RuntimeStoreError> {
        if !is_canonical_sha256_token(&exact_row_digest) {
            return Err(RuntimeStoreError::ReadFailed(format!(
                "input {} has a malformed exact-row digest",
                state.state.input_id
            )));
        }
        Ok(Self {
            state,
            exact_row_digest,
        })
    }

    /// Decoded stored input state.
    #[must_use]
    pub fn state(&self) -> &StoredInputState {
        &self.state
    }

    /// Exact digest of the backend row bytes observed with the lookup.
    #[must_use]
    pub fn exact_row_digest(&self) -> &str {
        &self.exact_row_digest
    }

    /// Consume the observation into decoded state and exact row digest.
    #[must_use]
    pub fn into_parts(self) -> (StoredInputState, String) {
        (self.state, self.exact_row_digest)
    }
}

/// Store-owned monotonic revision of one logical runtime's input-row set.
///
/// The value is opaque to recovery callers. A store mints it from its own
/// transactionally maintained generation and MUST advance that generation for
/// every insert, update, or delete of an input row for the runtime. Revision
/// zero is the canonical generation for a runtime that has never owned an
/// input row; it is still a real absence fence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RecoveryInputSetRevision(u64);

impl RecoveryInputSetRevision {
    /// Mint a revision from a store-owned monotonic generation.
    #[must_use]
    pub fn from_store_generation(generation: u64) -> Self {
        Self(generation)
    }

    /// Return the opaque generation for an exact store-side comparison.
    #[must_use]
    pub fn store_generation(self) -> u64 {
        self.0
    }
}

/// Exact, store-owned observation of every nonterminal input row that can
/// affect durable-tail recovery for one logical runtime.
///
/// Runtime-store implementations construct this value from an authoritative,
/// complete read of their persisted nonterminal-input set. Every row token
/// must be the canonical `sha256:<lowercase hex>` digest of the exact stored
/// row representation that the backend will compare in its atomic recovery
/// commit. An empty row vector is not an unproved absence: it produces a
/// domain-separated absence token scoped to `runtime_id`.
///
/// The exact set token is durable evidence of what was classified. A
/// recovery-capable backend MUST also compare [`Self::input_set_revision`]
/// against its current store-owned generation inside the transaction that
/// applies recovery. A row inserted, removed, terminalized, reopened, or
/// byte-modified after this observation must make that transaction fail with
/// [`RuntimeStoreError::RecoveryInputSetConflict`] without rescanning the set.
#[derive(Debug, Clone)]
pub struct PreparedRecoveryInputSnapshot {
    runtime_id: LogicalRuntimeId,
    input_set_revision: RecoveryInputSetRevision,
    rows: Vec<(StoredInputState, String)>,
    exact_set_token: String,
}

impl PreparedRecoveryInputSnapshot {
    /// Seal an authoritative complete set of exact nonterminal input rows.
    ///
    /// The constructor canonicalizes row order by [`InputId`], rejects
    /// duplicates, terminal rows, and non-canonical exact-row tokens, then
    /// hashes the runtime id, row count, and every `(input_id, row_token)`
    /// pair with length framing under `meerkat.recovery-input-set.v1`.
    ///
    /// This constructor validates representation, not completeness. The
    /// [`RuntimeStore`] implementation owns the obligation to select all and
    /// only persisted nonterminal rows for the supplied runtime and to observe
    /// `input_set_revision` in the same backend snapshot as those rows.
    pub fn from_exact_nonterminal_rows(
        runtime_id: LogicalRuntimeId,
        input_set_revision: RecoveryInputSetRevision,
        mut rows: Vec<(StoredInputState, String)>,
    ) -> Result<Self, RuntimeStoreError> {
        if runtime_id.0.is_empty() {
            return Err(RuntimeStoreError::ReadFailed(
                "recovery input snapshot has an empty logical runtime id".to_string(),
            ));
        }
        rows.sort_by(|(left, _), (right, _)| {
            left.state
                .input_id
                .to_string()
                .cmp(&right.state.input_id.to_string())
        });
        for (index, (state, row_token)) in rows.iter().enumerate() {
            if !input_state_is_recovery_nonterminal(state) {
                return Err(RuntimeStoreError::ReadFailed(format!(
                    "recovery input snapshot includes terminal input {}",
                    state.state.input_id
                )));
            }
            if !is_canonical_sha256_token(row_token) {
                return Err(RuntimeStoreError::ReadFailed(format!(
                    "recovery input snapshot row {} has a malformed exact-row token",
                    state.state.input_id
                )));
            }
            if index > 0 && rows[index - 1].0.state.input_id == state.state.input_id {
                return Err(RuntimeStoreError::ReadFailed(format!(
                    "recovery input snapshot repeats input {}",
                    state.state.input_id
                )));
            }
        }

        let mut hasher = Sha256::new();
        recovery_hash_part(&mut hasher, "domain", b"meerkat.recovery-input-set.v1");
        recovery_hash_part(&mut hasher, "runtime_id", runtime_id.0.as_bytes());
        recovery_hash_part(
            &mut hasher,
            "nonterminal_row_count",
            &(rows.len() as u64).to_be_bytes(),
        );
        for (state, row_token) in &rows {
            recovery_hash_part(
                &mut hasher,
                "input_id",
                state.state.input_id.to_string().as_bytes(),
            );
            recovery_hash_part(&mut hasher, "exact_row_token", row_token.as_bytes());
        }
        let exact_set_token = format!("sha256:{:x}", hasher.finalize());
        Ok(Self {
            runtime_id,
            input_set_revision,
            rows,
            exact_set_token,
        })
    }

    /// Logical runtime whose complete nonterminal set was observed.
    #[must_use]
    pub fn runtime_id(&self) -> &LogicalRuntimeId {
        &self.runtime_id
    }

    /// Store-owned input-set revision observed with the exact rows.
    #[must_use]
    pub fn input_set_revision(&self) -> RecoveryInputSetRevision {
        self.input_set_revision
    }

    /// Exact set/absence token sealed into prepared recovery evidence.
    #[must_use]
    pub fn exact_set_token(&self) -> &str {
        &self.exact_set_token
    }

    /// Consume the snapshot into canonical rows, store revision, and exact
    /// set/absence token.
    #[must_use]
    pub fn into_parts(
        self,
    ) -> (
        Vec<(StoredInputState, String)>,
        RecoveryInputSetRevision,
        String,
    ) {
        (self.rows, self.input_set_revision, self.exact_set_token)
    }
}

/// Exact predecessor authority for deleting one recovery-discarded input row.
///
/// Construction is crate-owned by the generated recovery path. Public store
/// implementations can inspect the values but callers cannot turn omission
/// from a target image into delete authority.
#[derive(Debug, Clone)]
pub struct PreparedRecoveryInputDelete {
    input_id: InputId,
    expected_row_digest: String,
}

impl PreparedRecoveryInputDelete {
    pub(crate) fn from_exact_observation(
        input_id: InputId,
        expected_row_digest: String,
    ) -> Result<Self, RuntimeStoreError> {
        if !is_canonical_sha256_token(&expected_row_digest) {
            return Err(RuntimeStoreError::InvalidInputStateBatchCas {
                reason: format!(
                    "recovery delete for input {input_id} has a malformed predecessor digest"
                ),
            });
        }
        Ok(Self {
            input_id,
            expected_row_digest,
        })
    }

    /// Input row removed by the machine-authorized recovery disposition.
    #[must_use]
    pub fn input_id(&self) -> &InputId {
        &self.input_id
    }

    /// Exact digest of the predecessor bytes the delete must match.
    #[must_use]
    pub fn expected_row_digest(&self) -> &str {
        &self.expected_row_digest
    }
}

/// One machine-authorized mutation in a cold-recovery input boundary.
#[derive(Debug, Clone)]
pub enum RecoveryInputStateMutation {
    /// Retained input row normalized to its recovered machine image.
    Upsert(InputStatePersistenceRecord),
    /// Ephemeral/discarded input row removed under exact predecessor authority.
    Delete(PreparedRecoveryInputDelete),
}

impl RecoveryInputStateMutation {
    /// Prepare an exact delete from the row digest returned by the recovery
    /// snapshot. Crate-owned because only the generated recovery classifier
    /// can authorize the discard disposition.
    pub(crate) fn delete(
        input_id: InputId,
        expected_row_digest: String,
    ) -> Result<Self, RuntimeStoreError> {
        PreparedRecoveryInputDelete::from_exact_observation(input_id, expected_row_digest)
            .map(Self::Delete)
    }
}

/// One recovery input mutation paired with the exact serialized target and
/// predecessor digest that authorize it.
///
/// `InputStatePersistenceRecord` intentionally does not implement equality:
/// equality here is defined by the exact durable bytes and CAS predecessor
/// sealed into the recovery witness.
#[derive(Debug, Clone)]
struct PreparedRecoveryInputUpdate {
    record: InputStatePersistenceRecord,
    input_id: InputId,
    expected_row_digest: String,
    target_bytes: Vec<u8>,
}

impl PartialEq for PreparedRecoveryInputUpdate {
    fn eq(&self, other: &Self) -> bool {
        self.input_id == other.input_id
            && self.expected_row_digest == other.expected_row_digest
            && self.target_bytes == other.target_bytes
    }
}

impl Eq for PreparedRecoveryInputUpdate {}

impl PreparedRecoveryInputUpdate {
    fn seal(record: InputStatePersistenceRecord) -> Result<Self, String> {
        let input_id = record.as_stored().state.input_id.clone();
        let expected_row_digest = record
            .expected_row_digest()
            .ok_or_else(|| {
                format!("recovery input {input_id} is not fenced on an exact predecessor row")
            })?
            .to_string();
        if !is_canonical_sha256_token(&expected_row_digest) {
            return Err(format!(
                "recovery input {input_id} has a malformed predecessor digest"
            ));
        }
        let target_bytes = serde_json::to_vec(record.as_stored()).map_err(|error| {
            format!("failed to encode exact recovery input target {input_id}: {error}")
        })?;
        Ok(Self {
            record,
            input_id,
            expected_row_digest,
            target_bytes,
        })
    }

    fn decode(
        input_id: InputId,
        expected_row_digest: String,
        target_bytes: Vec<u8>,
    ) -> Result<Self, String> {
        if !is_canonical_sha256_token(&expected_row_digest) {
            return Err(format!(
                "recovery input {input_id} has a malformed predecessor digest"
            ));
        }
        if target_bytes.is_empty() {
            return Err(format!(
                "recovery input {input_id} has an empty serialized target"
            ));
        }
        let bundle: StoredInputState = serde_json::from_slice(&target_bytes)
            .map_err(|error| format!("recovery input {input_id} target is invalid: {error}"))?;
        if bundle.state.input_id != input_id {
            return Err(format!(
                "recovery input target {} differs from sealed input {input_id}",
                bundle.state.input_id
            ));
        }
        let canonical_target_bytes = serde_json::to_vec(&bundle).map_err(|error| {
            format!("failed to canonicalize recovery input target {input_id}: {error}")
        })?;
        if canonical_target_bytes != target_bytes {
            return Err(format!(
                "recovery input {input_id} target is not in its canonical serialized form"
            ));
        }
        let record = InputStatePersistenceRecord::from_machine_snapshot(bundle)
            .map_err(|error| {
                format!("recovery input {input_id} target is not machine-authorized: {error}")
            })?
            .with_expected_row_digest(expected_row_digest.clone());
        Ok(Self {
            record,
            input_id,
            expected_row_digest,
            target_bytes,
        })
    }
}

#[derive(Debug)]
pub(crate) enum PreparedRecoveryInputStateMutation {
    Upsert {
        replacement: StoredInputState,
        expected_row_digest: String,
    },
    Delete {
        input_id: InputId,
        expected_row_digest: String,
    },
}

impl PreparedRecoveryInputStateMutation {
    pub(crate) fn input_id(&self) -> &InputId {
        match self {
            Self::Upsert { replacement, .. } => &replacement.state.input_id,
            Self::Delete { input_id, .. } => input_id,
        }
    }

    pub(crate) fn expected_row_digest(&self) -> &str {
        match self {
            Self::Upsert {
                expected_row_digest,
                ..
            }
            | Self::Delete {
                expected_row_digest,
                ..
            } => expected_row_digest,
        }
    }
}

/// Prepare an unbounded recovery input mutation set in canonical key order.
///
/// Recovery is scoped by the store-owned input-set revision rather than the
/// ordinary directed-terminal batch limit. Every target still carries an
/// exact predecessor-row digest, and duplicate identities are rejected.
pub(crate) fn prepare_recovery_input_state_mutations(
    mutations: &[RecoveryInputStateMutation],
) -> Result<Vec<PreparedRecoveryInputStateMutation>, RuntimeStoreError> {
    let mut prepared = mutations
        .iter()
        .cloned()
        .map(|mutation| match mutation {
            RecoveryInputStateMutation::Upsert(record) => {
                let update = PreparedRecoveryInputUpdate::seal(record)
                    .map_err(|reason| RuntimeStoreError::InvalidInputStateBatchCas { reason })?;
                Ok(PreparedRecoveryInputStateMutation::Upsert {
                    replacement: update.record.clone_stored(),
                    expected_row_digest: update.expected_row_digest,
                })
            }
            RecoveryInputStateMutation::Delete(delete) => {
                if !is_canonical_sha256_token(&delete.expected_row_digest) {
                    return Err(RuntimeStoreError::InvalidInputStateBatchCas {
                        reason: format!(
                            "recovery delete for input {} has a malformed predecessor digest",
                            delete.input_id
                        ),
                    });
                }
                Ok(PreparedRecoveryInputStateMutation::Delete {
                    input_id: delete.input_id,
                    expected_row_digest: delete.expected_row_digest,
                })
            }
        })
        .collect::<Result<Vec<_>, _>>()?;
    prepared.sort_by(|left, right| {
        left.input_id()
            .to_string()
            .cmp(&right.input_id().to_string())
    });
    if prepared
        .windows(2)
        .any(|pair| pair[0].input_id() == pair[1].input_id())
    {
        return Err(RuntimeStoreError::InvalidInputStateBatchCas {
            reason: "recovery input mutations must have unique input ids".to_string(),
        });
    }
    Ok(prepared)
}

fn validate_recovery_input_update_order(
    input_updates: &[PreparedRecoveryInputUpdate],
) -> Result<(), String> {
    if input_updates
        .windows(2)
        .any(|window| window[0].input_id.0 >= window[1].input_id.0)
    {
        return Err(
            "recovery input updates must have unique input ids in canonical order".to_string(),
        );
    }
    Ok(())
}

/// Exact store-issued authority transition sealed into one recovery witness.
#[derive(Debug, Clone, PartialEq, Eq)]
enum PreparedRecoverySessionAuthority {
    WholeBlob {
        base_store_revision: u64,
        base_blob_sha256: String,
        provisional_candidate_blob_sha256: String,
        provisional_candidate_sequence: u64,
        recovered_blob_sha256: String,
    },
    HeadCanonical {
        committed_store_revision: u64,
        committed_head_token: String,
        physical_store_revision: u64,
        physical_head_token: String,
        recovered_head_token: String,
    },
}

/// Machine-authorized recovery evidence sealed to one exact recovered
/// document, receipt, lifecycle target, complete predecessor nonterminal
/// input-set/absence token, and canonically ordered input-update set.
///
/// Fields are private and construction is crate-only. Store implementations
/// may inspect the paired values but cannot mint a recovery classification or
/// replace any one proof independently.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreparedRecoveryEvidence {
    session_id: meerkat_core::types::SessionId,
    candidate_id: String,
    candidate_run_id: RunId,
    class: crate::meerkat_machine::dsl::DurableTailRecoveryClass,
    disposition: crate::meerkat_machine::dsl::DurableTailRecoveryDisposition,
    session_authority: PreparedRecoverySessionAuthority,
    receipt_digest_enrichments: Vec<PreparedRecoveryReceiptDigestEnrichment>,
    predecessor_nonterminal_input_set_revision: RecoveryInputSetRevision,
    predecessor_nonterminal_input_set_token: String,
    input_updates: Vec<PreparedRecoveryInputUpdate>,
    lifecycle_target_token: String,
    lifecycle_target_bytes: Vec<u8>,
    exact_witness: String,
}

impl PreparedRecoveryEvidence {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn seal_head_canonical(
        recovered: &meerkat_core::Session,
        document: &BoundSessionCommit,
        session_id: meerkat_core::types::SessionId,
        candidate_id: String,
        candidate_run_id: RunId,
        class: crate::meerkat_machine::dsl::DurableTailRecoveryClass,
        disposition: crate::meerkat_machine::dsl::DurableTailRecoveryDisposition,
        committed_store_revision: u64,
        committed_head_token: String,
        physical_store_revision: u64,
        physical_head_token: String,
        recovered_head_token: String,
        receipt_digest_enrichments: Vec<PreparedRecoveryReceiptDigestEnrichment>,
        predecessor_nonterminal_input_set_revision: RecoveryInputSetRevision,
        predecessor_nonterminal_input_set_token: String,
        input_updates: Vec<InputStatePersistenceRecord>,
        receipt: &RunBoundaryReceipt,
        lifecycle: &MachineLifecycleCommit,
    ) -> Result<Self, RuntimeStoreError> {
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: session_id.to_string(),
            detail,
        };
        if candidate_id.is_empty()
            || committed_store_revision == 0
            || physical_store_revision <= committed_store_revision
            || committed_head_token.is_empty()
            || physical_head_token.is_empty()
            || recovered_head_token.is_empty()
            || committed_head_token == physical_head_token
            || !is_canonical_sha256_token(&predecessor_nonterminal_input_set_token)
        {
            return Err(conflict(
                "prepared recovery contains an invalid store-issued authority transition"
                    .to_string(),
            ));
        }
        let valid_disposition = matches!(
            (class, disposition),
            (
                crate::meerkat_machine::dsl::DurableTailRecoveryClass::CompletedCandidate,
                crate::meerkat_machine::dsl::DurableTailRecoveryDisposition::CommitCompleted
                    | crate::meerkat_machine::dsl::DurableTailRecoveryDisposition::CommitCompletedRetainInputs
            ) | (
                crate::meerkat_machine::dsl::DurableTailRecoveryClass::InterruptedRepairableCandidate,
                crate::meerkat_machine::dsl::DurableTailRecoveryDisposition::RepairAndCommitInterrupted
            )
        );
        if !valid_disposition {
            return Err(conflict(format!(
                "recovery class {} cannot realize disposition {}",
                recovery_class_name(class),
                recovery_disposition_name(disposition)
            )));
        }

        if recovered.id() != &session_id {
            return Err(conflict(format!(
                "prepared recovery document belongs to {}, not {session_id}",
                recovered.id()
            )));
        }
        let head_boundary = document.head_canonical().ok_or_else(|| {
            conflict("prepared recovery has no sealed head-canonical mutation".to_string())
        })?;
        let successor_head = head_boundary.mutation().successor_head();
        let recovered_message_count = u64::try_from(recovered.messages().len()).map_err(|_| {
            conflict("recovered document message count exceeds u64 authority".to_string())
        })?;
        let conversation_digest = recovered.transcript_content_digest().map_err(|error| {
            conflict(format!(
                "failed to derive recovered conversation digest: {error}"
            ))
        })?;
        let metadata_matches =
            successor_head
                .matches_session_metadata(recovered)
                .map_err(|error| {
                    conflict(format!(
                        "failed to compare recovered document metadata authority: {error}"
                    ))
                })?;
        if &successor_head.id != recovered.id()
            || successor_head.version != recovered.version()
            || successor_head.head_revision != conversation_digest
            || successor_head.message_count != recovered_message_count
            || successor_head.created_at != recovered.created_at()
            || successor_head.updated_at != recovered.updated_at()
            || successor_head.usage != recovered.total_usage()
            || !metadata_matches
        {
            return Err(conflict(
                "prepared recovered head differs from the exact recovered document".to_string(),
            ));
        }
        let derived_recovered_head_token = meerkat_core::session_head_cas_token(successor_head)
            .map_err(|error| {
                conflict(format!(
                    "failed to derive recovered HeadCanonical token: {error}"
                ))
            })?;
        if derived_recovered_head_token != recovered_head_token {
            return Err(conflict(
                "prepared recovered head differs from the sealed successor token".to_string(),
            ));
        }
        if receipt.run_id != candidate_run_id || receipt.message_count != recovered.messages().len()
        {
            return Err(conflict(
                "recovery receipt does not bind the candidate run and exact message count"
                    .to_string(),
            ));
        }
        if receipt.conversation_digest.as_deref() != Some(conversation_digest.as_str()) {
            return Err(conflict(
                "recovery receipt does not bind the exact recovered conversation".to_string(),
            ));
        }
        let mut previous_enrichment_sequence = None;
        for enrichment in &receipt_digest_enrichments {
            let original = enrichment.original_receipt();
            if original.run_id != candidate_run_id
                || original.conversation_digest.is_some()
                || previous_enrichment_sequence
                    .is_some_and(|previous| original.sequence <= previous)
                || original.message_count > recovered.messages().len()
            {
                return Err(conflict(
                    "prepared recovery receipt enrichment has an invalid run, sequence, count, or pre-migration shape"
                        .to_string(),
                ));
            }
            let derived = recovered
                .transcript_prefix_digest(original.message_count)
                .map_err(|error| {
                    conflict(format!(
                        "failed to verify recovery receipt enrichment prefix: {error}"
                    ))
                })?;
            if derived != enrichment.derived_conversation_digest()
                || enrichment.original_exact_row_token().is_empty()
            {
                return Err(conflict(
                    "prepared recovery receipt enrichment differs from the exact recovered transcript prefix"
                        .to_string(),
                ));
            }
            previous_enrichment_sequence = Some(original.sequence);
        }

        let input_updates = input_updates
            .into_iter()
            .map(PreparedRecoveryInputUpdate::seal)
            .collect::<Result<Vec<_>, _>>()
            .map_err(&conflict)?;
        validate_recovery_input_update_order(&input_updates).map_err(&conflict)?;
        let lifecycle_target_bytes = lifecycle.store_record().encode()?;
        let lifecycle_target_token = recovery_sha256_token(&lifecycle_target_bytes);
        // The expected version is a first-apply fence, not outcome identity:
        // after a successful commit the exact lifecycle target has a new row
        // version. Require the fence to exist, but do not bake that transient
        // predecessor token into the durable retry witness.
        let _ = lifecycle_expected_version_token(lifecycle)?;
        let session_authority = PreparedRecoverySessionAuthority::HeadCanonical {
            committed_store_revision,
            committed_head_token,
            physical_store_revision,
            physical_head_token,
            recovered_head_token,
        };

        let mut evidence = Self {
            session_id,
            candidate_id,
            candidate_run_id,
            class,
            disposition,
            session_authority,
            receipt_digest_enrichments,
            predecessor_nonterminal_input_set_revision,
            predecessor_nonterminal_input_set_token,
            input_updates,
            lifecycle_target_token,
            lifecycle_target_bytes,
            exact_witness: String::new(),
        };
        evidence.exact_witness = evidence.compute_exact_witness(receipt).map_err(|error| {
            RuntimeStoreError::WriteFailed(format!(
                "failed to encode exact recovery witness: {error}"
            ))
        })?;
        evidence.verify_head_canonical_boundary(document, receipt)?;
        Ok(evidence)
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn seal_whole_blob(
        recovered: &meerkat_core::Session,
        repaired_document: Option<&BoundSessionCommit>,
        session_id: meerkat_core::types::SessionId,
        candidate_id: String,
        candidate_run_id: RunId,
        class: crate::meerkat_machine::dsl::DurableTailRecoveryClass,
        disposition: crate::meerkat_machine::dsl::DurableTailRecoveryDisposition,
        base_store_revision: u64,
        base_blob_sha256: String,
        provisional_candidate_blob_sha256: String,
        provisional_candidate_sequence: u64,
        recovered_blob_sha256: String,
        receipt_digest_enrichments: Vec<PreparedRecoveryReceiptDigestEnrichment>,
        predecessor_nonterminal_input_set_revision: RecoveryInputSetRevision,
        predecessor_nonterminal_input_set_token: String,
        input_updates: Vec<InputStatePersistenceRecord>,
        receipt: &RunBoundaryReceipt,
        lifecycle: &MachineLifecycleCommit,
    ) -> Result<Self, RuntimeStoreError> {
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: session_id.to_string(),
            detail,
        };
        if candidate_id.is_empty()
            || base_store_revision == 0
            || base_blob_sha256.is_empty()
            || provisional_candidate_blob_sha256.is_empty()
            || provisional_candidate_sequence == 0
            || recovered_blob_sha256.is_empty()
            || !is_canonical_sha256_token(&predecessor_nonterminal_input_set_token)
        {
            return Err(conflict(
                "prepared WholeBlob recovery contains an invalid store-issued authority transition"
                    .to_string(),
            ));
        }
        if recovered.id() != &session_id {
            return Err(conflict(format!(
                "prepared recovery document belongs to {}, not {session_id}",
                recovered.id()
            )));
        }
        let valid_disposition = matches!(
            (class, disposition),
            (
                crate::meerkat_machine::dsl::DurableTailRecoveryClass::CompletedCandidate,
                crate::meerkat_machine::dsl::DurableTailRecoveryDisposition::CommitCompleted
                    | crate::meerkat_machine::dsl::DurableTailRecoveryDisposition::CommitCompletedRetainInputs
            ) | (
                crate::meerkat_machine::dsl::DurableTailRecoveryClass::InterruptedRepairableCandidate,
                crate::meerkat_machine::dsl::DurableTailRecoveryDisposition::RepairAndCommitInterrupted
            )
        );
        if !valid_disposition {
            return Err(conflict(format!(
                "recovery class {} cannot realize disposition {}",
                recovery_class_name(class),
                recovery_disposition_name(disposition)
            )));
        }
        match class {
            crate::meerkat_machine::dsl::DurableTailRecoveryClass::CompletedCandidate => {
                if recovered_blob_sha256 != provisional_candidate_blob_sha256
                    || repaired_document.is_some()
                {
                    return Err(conflict(
                        "completed WholeBlob recovery must promote the exact provisional candidate without a repaired artifact"
                            .to_string(),
                    ));
                }
            }
            crate::meerkat_machine::dsl::DurableTailRecoveryClass::InterruptedRepairableCandidate => {
                if recovered_blob_sha256 == provisional_candidate_blob_sha256 {
                    return Err(conflict(
                        "interrupted WholeBlob recovery must install a distinct repaired artifact"
                            .to_string(),
                    ));
                }
                let document = repaired_document.ok_or_else(|| {
                    conflict(
                        "interrupted WholeBlob recovery has no sealed repaired artifact"
                            .to_string(),
                    )
                })?;
                if document.head_canonical().is_some() {
                    return Err(conflict(
                        "WholeBlob recovery unexpectedly carries a HeadCanonical mutation"
                            .to_string(),
                    ));
                }
                let artifact = document.whole_blob_artifact().map_err(|error| {
                    conflict(format!(
                        "failed to materialize recovered WholeBlob artifact: {error}"
                    ))
                })?;
                if artifact.row_sha256_token() != recovered_blob_sha256 {
                    return Err(conflict(
                        "recovered WholeBlob bytes differ from the sealed successor digest"
                            .to_string(),
                    ));
                }
            }
            crate::meerkat_machine::dsl::DurableTailRecoveryClass::Ambiguous => {
                return Err(conflict(
                    "ambiguous WholeBlob recovery cannot be sealed".to_string(),
                ));
            }
        }
        if receipt.run_id != candidate_run_id || receipt.message_count != recovered.messages().len()
        {
            return Err(conflict(
                "recovery receipt does not bind the candidate run and exact message count"
                    .to_string(),
            ));
        }
        let conversation_digest = recovered.transcript_content_digest().map_err(|error| {
            conflict(format!(
                "failed to derive recovered conversation digest: {error}"
            ))
        })?;
        if receipt.conversation_digest.as_deref() != Some(conversation_digest.as_str()) {
            return Err(conflict(
                "recovery receipt does not bind the exact recovered conversation".to_string(),
            ));
        }
        let mut previous_enrichment_sequence = None;
        for enrichment in &receipt_digest_enrichments {
            let original = enrichment.original_receipt();
            if original.run_id != candidate_run_id
                || original.conversation_digest.is_some()
                || previous_enrichment_sequence
                    .is_some_and(|previous| original.sequence <= previous)
                || original.message_count > recovered.messages().len()
            {
                return Err(conflict(
                    "prepared recovery receipt enrichment has an invalid run, sequence, count, or pre-migration shape"
                        .to_string(),
                ));
            }
            let derived = recovered
                .transcript_prefix_digest(original.message_count)
                .map_err(|error| {
                    conflict(format!(
                        "failed to verify recovery receipt enrichment prefix: {error}"
                    ))
                })?;
            if derived != enrichment.derived_conversation_digest()
                || enrichment.original_exact_row_token().is_empty()
            {
                return Err(conflict(
                    "prepared recovery receipt enrichment differs from the exact recovered transcript prefix"
                        .to_string(),
                ));
            }
            previous_enrichment_sequence = Some(original.sequence);
        }
        let input_updates = input_updates
            .into_iter()
            .map(PreparedRecoveryInputUpdate::seal)
            .collect::<Result<Vec<_>, _>>()
            .map_err(&conflict)?;
        validate_recovery_input_update_order(&input_updates).map_err(&conflict)?;
        let lifecycle_target_bytes = lifecycle.store_record().encode()?;
        let lifecycle_target_token = recovery_sha256_token(&lifecycle_target_bytes);
        let _ = lifecycle_expected_version_token(lifecycle)?;
        let mut evidence = Self {
            session_id,
            candidate_id,
            candidate_run_id,
            class,
            disposition,
            session_authority: PreparedRecoverySessionAuthority::WholeBlob {
                base_store_revision,
                base_blob_sha256,
                provisional_candidate_blob_sha256,
                provisional_candidate_sequence,
                recovered_blob_sha256,
            },
            receipt_digest_enrichments,
            predecessor_nonterminal_input_set_revision,
            predecessor_nonterminal_input_set_token,
            input_updates,
            lifecycle_target_token,
            lifecycle_target_bytes,
            exact_witness: String::new(),
        };
        evidence.exact_witness = evidence.compute_exact_witness(receipt).map_err(|error| {
            RuntimeStoreError::WriteFailed(format!(
                "failed to encode exact recovery witness: {error}"
            ))
        })?;
        Ok(evidence)
    }

    fn compute_exact_witness(
        &self,
        receipt: &RunBoundaryReceipt,
    ) -> Result<String, serde_json::Error> {
        let receipt_json = serde_json::to_vec(receipt)?;
        let mut hasher = Sha256::new();
        recovery_hash_part(
            &mut hasher,
            "domain",
            b"meerkat.prepared-recovery-evidence.v6",
        );
        recovery_hash_part(
            &mut hasher,
            "session_id",
            self.session_id.to_string().as_bytes(),
        );
        recovery_hash_part(&mut hasher, "candidate_id", self.candidate_id.as_bytes());
        recovery_hash_part(
            &mut hasher,
            "candidate_run_id",
            self.candidate_run_id.to_string().as_bytes(),
        );
        recovery_hash_part(
            &mut hasher,
            "class",
            recovery_class_name(self.class).as_bytes(),
        );
        recovery_hash_part(
            &mut hasher,
            "disposition",
            recovery_disposition_name(self.disposition).as_bytes(),
        );
        match &self.session_authority {
            PreparedRecoverySessionAuthority::WholeBlob {
                base_store_revision,
                base_blob_sha256,
                provisional_candidate_blob_sha256,
                provisional_candidate_sequence,
                recovered_blob_sha256,
            } => {
                recovery_hash_part(&mut hasher, "profile", b"whole_blob_v1");
                recovery_hash_part(
                    &mut hasher,
                    "base_store_revision",
                    &base_store_revision.to_be_bytes(),
                );
                recovery_hash_part(&mut hasher, "base_blob_sha256", base_blob_sha256.as_bytes());
                recovery_hash_part(
                    &mut hasher,
                    "provisional_candidate_blob_sha256",
                    provisional_candidate_blob_sha256.as_bytes(),
                );
                recovery_hash_part(
                    &mut hasher,
                    "provisional_candidate_sequence",
                    &provisional_candidate_sequence.to_be_bytes(),
                );
                recovery_hash_part(
                    &mut hasher,
                    "recovered_blob_sha256",
                    recovered_blob_sha256.as_bytes(),
                );
            }
            PreparedRecoverySessionAuthority::HeadCanonical {
                committed_store_revision,
                committed_head_token,
                physical_store_revision,
                physical_head_token,
                recovered_head_token,
            } => {
                recovery_hash_part(&mut hasher, "profile", b"head_canonical_v1");
                recovery_hash_part(
                    &mut hasher,
                    "committed_store_revision",
                    &committed_store_revision.to_be_bytes(),
                );
                recovery_hash_part(
                    &mut hasher,
                    "committed_head_token",
                    committed_head_token.as_bytes(),
                );
                recovery_hash_part(
                    &mut hasher,
                    "physical_store_revision",
                    &physical_store_revision.to_be_bytes(),
                );
                recovery_hash_part(
                    &mut hasher,
                    "physical_head_token",
                    physical_head_token.as_bytes(),
                );
                recovery_hash_part(
                    &mut hasher,
                    "recovered_head_token",
                    recovered_head_token.as_bytes(),
                );
            }
        }
        recovery_hash_part(
            &mut hasher,
            "receipt_digest_enrichment_count",
            &(self.receipt_digest_enrichments.len() as u64).to_be_bytes(),
        );
        for enrichment in &self.receipt_digest_enrichments {
            let original_json = serde_json::to_vec(enrichment.original_receipt())?;
            recovery_hash_part(
                &mut hasher,
                "receipt_digest_enrichment_original",
                &original_json,
            );
            recovery_hash_part(
                &mut hasher,
                "receipt_digest_enrichment_original_token",
                enrichment.original_exact_row_token().as_bytes(),
            );
            recovery_hash_part(
                &mut hasher,
                "receipt_digest_enrichment_derived_digest",
                enrichment.derived_conversation_digest().as_bytes(),
            );
        }
        recovery_hash_part(
            &mut hasher,
            "predecessor_nonterminal_input_set_revision",
            &self
                .predecessor_nonterminal_input_set_revision
                .store_generation()
                .to_be_bytes(),
        );
        recovery_hash_part(
            &mut hasher,
            "predecessor_nonterminal_input_set_token",
            self.predecessor_nonterminal_input_set_token.as_bytes(),
        );
        recovery_hash_part(
            &mut hasher,
            "input_update_count",
            &(self.input_updates.len() as u64).to_be_bytes(),
        );
        for input_update in &self.input_updates {
            recovery_hash_part(
                &mut hasher,
                "input_update_id",
                input_update.input_id.to_string().as_bytes(),
            );
            recovery_hash_part(
                &mut hasher,
                "input_update_expected_row_digest",
                input_update.expected_row_digest.as_bytes(),
            );
            recovery_hash_part(
                &mut hasher,
                "input_update_target",
                &input_update.target_bytes,
            );
        }
        recovery_hash_part(&mut hasher, "receipt", &receipt_json);
        recovery_hash_part(
            &mut hasher,
            "lifecycle_target_token",
            self.lifecycle_target_token.as_bytes(),
        );
        recovery_hash_part(
            &mut hasher,
            "lifecycle_target",
            &self.lifecycle_target_bytes,
        );
        Ok(format!("sha256:{:x}", hasher.finalize()))
    }

    pub(crate) fn verify_head_canonical_boundary(
        &self,
        document: &BoundSessionCommit,
        receipt: &RunBoundaryReceipt,
    ) -> Result<(), RuntimeStoreError> {
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: self.session_id.to_string(),
            detail,
        };
        let boundary = document.head_canonical().ok_or_else(|| {
            conflict("prepared recovery has no sealed head-canonical mutation".to_string())
        })?;
        let PreparedRecoverySessionAuthority::HeadCanonical {
            physical_head_token,
            recovered_head_token,
            ..
        } = &self.session_authority
        else {
            return Err(conflict(
                "WholeBlob recovery evidence cannot authorize a HeadCanonical mutation".to_string(),
            ));
        };
        let mutation = boundary.mutation();
        if mutation.session_id() != &self.session_id
            || mutation.predecessor_head_token() != Some(physical_head_token.as_str())
        {
            return Err(conflict(
                "prepared recovery head mutation differs from sealed source/successor authority"
                    .to_string(),
            ));
        }
        let successor = mutation.successor_head();
        let successor_token = meerkat_core::session_head_cas_token(successor).map_err(|error| {
            conflict(format!(
                "prepared recovery successor token is invalid: {error}"
            ))
        })?;
        let receipt_count = u64::try_from(receipt.message_count).map_err(|_| {
            conflict("recovery receipt message count does not fit head authority".to_string())
        })?;
        if successor_token != *recovered_head_token
            || successor.message_count != receipt_count
            || receipt.conversation_digest.as_deref() != Some(successor.head_revision.as_str())
        {
            return Err(conflict(
                "prepared recovery head does not bind the receipt's exact transcript".to_string(),
            ));
        }
        Ok(())
    }

    pub(crate) fn verify_input_updates(
        &self,
        input_updates: &[InputStatePersistenceRecord],
    ) -> Result<(), RuntimeStoreError> {
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: self.session_id.to_string(),
            detail,
        };
        let input_updates = input_updates
            .iter()
            .cloned()
            .map(PreparedRecoveryInputUpdate::seal)
            .collect::<Result<Vec<_>, _>>()
            .map_err(&conflict)?;
        validate_recovery_input_update_order(&input_updates).map_err(&conflict)?;
        if input_updates != self.input_updates {
            return Err(conflict(
                "recovery input effects differ from sealed evidence".to_string(),
            ));
        }
        Ok(())
    }

    pub(crate) fn verify_request_effects(
        &self,
        receipt: &RunBoundaryReceipt,
        lifecycle: &MachineLifecycleCommit,
    ) -> Result<(), RuntimeStoreError> {
        let conflict = |detail: String| RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: self.session_id.to_string(),
            detail,
        };
        // The predecessor version remains a first-apply fence. The target
        // bytes, not that transient predecessor token, define retry identity.
        let _ = lifecycle_expected_version_token(lifecycle)?;
        let lifecycle_target_bytes = lifecycle.store_record().encode()?;
        let lifecycle_target_token = recovery_sha256_token(&lifecycle_target_bytes);
        if lifecycle_target_token != self.lifecycle_target_token
            || lifecycle_target_bytes != self.lifecycle_target_bytes
        {
            return Err(conflict(
                "recovery lifecycle target differs from sealed evidence".to_string(),
            ));
        }
        let exact_witness = self.compute_exact_witness(receipt).map_err(|error| {
            RuntimeStoreError::WriteFailed(format!(
                "failed to re-encode exact recovery witness: {error}"
            ))
        })?;
        if exact_witness != self.exact_witness {
            return Err(conflict(
                "recovery receipt or sealed effects differ from exact evidence".to_string(),
            ));
        }
        Ok(())
    }

    pub(crate) fn cloned_input_updates(&self) -> Vec<InputStatePersistenceRecord> {
        self.input_updates
            .iter()
            .map(|input_update| input_update.record.clone())
            .collect()
    }

    pub(crate) fn session_id(&self) -> &meerkat_core::types::SessionId {
        &self.session_id
    }

    pub(crate) fn candidate_id(&self) -> &str {
        &self.candidate_id
    }

    pub(crate) fn candidate_run_id(&self) -> &RunId {
        &self.candidate_run_id
    }

    pub(crate) fn disposition(
        &self,
    ) -> crate::meerkat_machine::dsl::DurableTailRecoveryDisposition {
        self.disposition
    }

    pub(crate) fn head_canonical_authority_transition(
        &self,
    ) -> Option<(u64, &str, u64, &str, &str)> {
        match &self.session_authority {
            PreparedRecoverySessionAuthority::HeadCanonical {
                committed_store_revision,
                committed_head_token,
                physical_store_revision,
                physical_head_token,
                recovered_head_token,
            } => Some((
                *committed_store_revision,
                committed_head_token,
                *physical_store_revision,
                physical_head_token,
                recovered_head_token,
            )),
            PreparedRecoverySessionAuthority::WholeBlob { .. } => None,
        }
    }

    pub(crate) fn whole_blob_authority_transition(&self) -> Option<(u64, &str, &str, u64, &str)> {
        match &self.session_authority {
            PreparedRecoverySessionAuthority::WholeBlob {
                base_store_revision,
                base_blob_sha256,
                provisional_candidate_blob_sha256,
                provisional_candidate_sequence,
                recovered_blob_sha256,
            } => Some((
                *base_store_revision,
                base_blob_sha256,
                provisional_candidate_blob_sha256,
                *provisional_candidate_sequence,
                recovered_blob_sha256,
            )),
            PreparedRecoverySessionAuthority::HeadCanonical { .. } => None,
        }
    }

    pub(crate) fn receipt_digest_enrichments(&self) -> &[PreparedRecoveryReceiptDigestEnrichment] {
        &self.receipt_digest_enrichments
    }

    pub(crate) fn predecessor_nonterminal_input_set_token(&self) -> &str {
        &self.predecessor_nonterminal_input_set_token
    }

    pub(crate) fn predecessor_nonterminal_input_set_revision(&self) -> RecoveryInputSetRevision {
        self.predecessor_nonterminal_input_set_revision
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct CommittedRecoveryReceiptDigestEnrichmentWire {
    original_receipt: RunBoundaryReceipt,
    original_exact_row_token: String,
    derived_conversation_digest: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct CommittedRecoveryInputUpdateWire {
    input_id: InputId,
    expected_row_digest: String,
    target_bytes: Vec<u8>,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(tag = "profile", rename_all = "snake_case", deny_unknown_fields)]
enum CommittedRecoverySessionAuthorityWire {
    WholeBlobV1 {
        base_store_revision: u64,
        base_blob_sha256: String,
        provisional_candidate_blob_sha256: String,
        provisional_candidate_sequence: u64,
        recovered_blob_sha256: String,
    },
    HeadCanonicalV1 {
        committed_store_revision: u64,
        committed_head_token: String,
        physical_store_revision: u64,
        physical_head_token: String,
        recovered_head_token: String,
    },
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct CommittedRecoveryBoundaryWire {
    version: u16,
    session_id: meerkat_core::types::SessionId,
    candidate_id: String,
    candidate_run_id: RunId,
    class: String,
    disposition: String,
    session_authority: CommittedRecoverySessionAuthorityWire,
    receipt_digest_enrichments: Vec<CommittedRecoveryReceiptDigestEnrichmentWire>,
    predecessor_nonterminal_input_set_revision: u64,
    predecessor_nonterminal_input_set_token: String,
    input_updates: Vec<CommittedRecoveryInputUpdateWire>,
    lifecycle_target_token: String,
    lifecycle_target_bytes: Vec<u8>,
    exact_witness: String,
    receipt: RunBoundaryReceipt,
}

/// Durable exact-retry witness for one recovery candidate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommittedRecoveryBoundary {
    evidence: PreparedRecoveryEvidence,
    receipt: RunBoundaryReceipt,
}

impl CommittedRecoveryBoundary {
    const VERSION: u16 = 6;

    pub(crate) fn from_prepared(
        evidence: &PreparedRecoveryEvidence,
        receipt: &RunBoundaryReceipt,
    ) -> Self {
        Self {
            evidence: evidence.clone(),
            receipt: receipt.clone(),
        }
    }

    pub(crate) fn evidence(&self) -> &PreparedRecoveryEvidence {
        &self.evidence
    }

    pub(crate) fn receipt(&self) -> &RunBoundaryReceipt {
        &self.receipt
    }

    pub(crate) fn encode(&self) -> Result<Vec<u8>, RuntimeStoreError> {
        serde_json::to_vec(&CommittedRecoveryBoundaryWire {
            version: Self::VERSION,
            session_id: self.evidence.session_id.clone(),
            candidate_id: self.evidence.candidate_id.clone(),
            candidate_run_id: self.evidence.candidate_run_id.clone(),
            class: recovery_class_name(self.evidence.class).to_string(),
            disposition: recovery_disposition_name(self.evidence.disposition).to_string(),
            session_authority: match &self.evidence.session_authority {
                PreparedRecoverySessionAuthority::WholeBlob {
                    base_store_revision,
                    base_blob_sha256,
                    provisional_candidate_blob_sha256,
                    provisional_candidate_sequence,
                    recovered_blob_sha256,
                } => CommittedRecoverySessionAuthorityWire::WholeBlobV1 {
                    base_store_revision: *base_store_revision,
                    base_blob_sha256: base_blob_sha256.clone(),
                    provisional_candidate_blob_sha256: provisional_candidate_blob_sha256.clone(),
                    provisional_candidate_sequence: *provisional_candidate_sequence,
                    recovered_blob_sha256: recovered_blob_sha256.clone(),
                },
                PreparedRecoverySessionAuthority::HeadCanonical {
                    committed_store_revision,
                    committed_head_token,
                    physical_store_revision,
                    physical_head_token,
                    recovered_head_token,
                } => CommittedRecoverySessionAuthorityWire::HeadCanonicalV1 {
                    committed_store_revision: *committed_store_revision,
                    committed_head_token: committed_head_token.clone(),
                    physical_store_revision: *physical_store_revision,
                    physical_head_token: physical_head_token.clone(),
                    recovered_head_token: recovered_head_token.clone(),
                },
            },
            receipt_digest_enrichments: self
                .evidence
                .receipt_digest_enrichments
                .iter()
                .map(|enrichment| CommittedRecoveryReceiptDigestEnrichmentWire {
                    original_receipt: enrichment.original_receipt.clone(),
                    original_exact_row_token: enrichment.original_exact_row_token.clone(),
                    derived_conversation_digest: enrichment.derived_conversation_digest.clone(),
                })
                .collect(),
            predecessor_nonterminal_input_set_revision: self
                .evidence
                .predecessor_nonterminal_input_set_revision
                .store_generation(),
            predecessor_nonterminal_input_set_token: self
                .evidence
                .predecessor_nonterminal_input_set_token
                .clone(),
            input_updates: self
                .evidence
                .input_updates
                .iter()
                .map(|input_update| CommittedRecoveryInputUpdateWire {
                    input_id: input_update.input_id.clone(),
                    expected_row_digest: input_update.expected_row_digest.clone(),
                    target_bytes: input_update.target_bytes.clone(),
                })
                .collect(),
            lifecycle_target_token: self.evidence.lifecycle_target_token.clone(),
            lifecycle_target_bytes: self.evidence.lifecycle_target_bytes.clone(),
            exact_witness: self.evidence.exact_witness.clone(),
            receipt: self.receipt.clone(),
        })
        .map_err(|error| {
            RuntimeStoreError::WriteFailed(format!(
                "failed to encode committed recovery boundary: {error}"
            ))
        })
    }

    pub(crate) fn decode(bytes: &[u8]) -> Result<Self, RuntimeStoreError> {
        let wire: CommittedRecoveryBoundaryWire =
            serde_json::from_slice(bytes).map_err(|error| {
                RuntimeStoreError::ReadFailed(format!(
                    "invalid committed recovery boundary: {error}"
                ))
            })?;
        if wire.version != Self::VERSION {
            return Err(RuntimeStoreError::ReadFailed(format!(
                "unsupported committed recovery boundary version {}",
                wire.version
            )));
        }
        let CommittedRecoveryBoundaryWire {
            version: _,
            session_id,
            candidate_id,
            candidate_run_id,
            class,
            disposition,
            session_authority: session_authority_wire,
            receipt_digest_enrichments: receipt_digest_enrichment_wires,
            predecessor_nonterminal_input_set_revision,
            predecessor_nonterminal_input_set_token,
            input_updates: input_update_wires,
            lifecycle_target_token,
            lifecycle_target_bytes,
            exact_witness,
            receipt,
        } = wire;
        let session_authority = match session_authority_wire {
            CommittedRecoverySessionAuthorityWire::WholeBlobV1 {
                base_store_revision,
                base_blob_sha256,
                provisional_candidate_blob_sha256,
                provisional_candidate_sequence,
                recovered_blob_sha256,
            } if base_store_revision != 0
                && !base_blob_sha256.is_empty()
                && !provisional_candidate_blob_sha256.is_empty()
                && provisional_candidate_sequence != 0
                && !recovered_blob_sha256.is_empty() =>
            {
                PreparedRecoverySessionAuthority::WholeBlob {
                    base_store_revision,
                    base_blob_sha256,
                    provisional_candidate_blob_sha256,
                    provisional_candidate_sequence,
                    recovered_blob_sha256,
                }
            }
            CommittedRecoverySessionAuthorityWire::HeadCanonicalV1 {
                committed_store_revision,
                committed_head_token,
                physical_store_revision,
                physical_head_token,
                recovered_head_token,
            } if committed_store_revision != 0
                && physical_store_revision > committed_store_revision
                && !committed_head_token.is_empty()
                && !physical_head_token.is_empty()
                && !recovered_head_token.is_empty()
                && committed_head_token != physical_head_token =>
            {
                PreparedRecoverySessionAuthority::HeadCanonical {
                    committed_store_revision,
                    committed_head_token,
                    physical_store_revision,
                    physical_head_token,
                    recovered_head_token,
                }
            }
            _ => {
                return Err(RuntimeStoreError::ReadFailed(
                    "committed recovery boundary contains an invalid store authority transition"
                        .to_string(),
                ));
            }
        };
        if candidate_id.is_empty()
            || !is_canonical_sha256_token(&predecessor_nonterminal_input_set_token)
            || lifecycle_target_bytes.is_empty()
            || !is_canonical_sha256_token(&lifecycle_target_token)
            || !is_canonical_sha256_token(&exact_witness)
        {
            return Err(RuntimeStoreError::ReadFailed(
                "committed recovery boundary contains an empty exact identity".to_string(),
            ));
        }
        if recovery_sha256_token(&lifecycle_target_bytes) != lifecycle_target_token {
            return Err(RuntimeStoreError::ReadFailed(
                "committed recovery lifecycle target token does not match its exact bytes"
                    .to_string(),
            ));
        }
        let lifecycle_target_snapshot =
            decode_machine_lifecycle_store_record(&lifecycle_target_bytes).map_err(|error| {
                RuntimeStoreError::ReadFailed(format!(
                    "committed recovery lifecycle target is invalid: {error}"
                ))
            })?;
        let canonical_lifecycle_target_bytes =
            MachineLifecycleStoreRecord::from_snapshot(&lifecycle_target_snapshot)
                .encode()
                .map_err(|error| {
                    RuntimeStoreError::ReadFailed(format!(
                        "failed to canonicalize committed recovery lifecycle target: {error}"
                    ))
                })?;
        if canonical_lifecycle_target_bytes != lifecycle_target_bytes {
            return Err(RuntimeStoreError::ReadFailed(
                "committed recovery lifecycle target is not in canonical serialized form"
                    .to_string(),
            ));
        }
        if receipt.run_id != candidate_run_id {
            return Err(RuntimeStoreError::ReadFailed(
                "committed recovery receipt run differs from candidate run".to_string(),
            ));
        }
        let mut previous_enrichment_sequence = None;
        let mut receipt_digest_enrichments =
            Vec::with_capacity(receipt_digest_enrichment_wires.len());
        for enrichment in receipt_digest_enrichment_wires {
            if enrichment.original_receipt.run_id != candidate_run_id
                || enrichment.original_receipt.conversation_digest.is_some()
                || previous_enrichment_sequence
                    .is_some_and(|previous| enrichment.original_receipt.sequence <= previous)
                || enrichment.original_exact_row_token.is_empty()
                || enrichment.derived_conversation_digest.is_empty()
            {
                return Err(RuntimeStoreError::ReadFailed(
                    "committed recovery receipt enrichment is malformed".to_string(),
                ));
            }
            previous_enrichment_sequence = Some(enrichment.original_receipt.sequence);
            receipt_digest_enrichments.push(PreparedRecoveryReceiptDigestEnrichment {
                original_receipt: enrichment.original_receipt,
                original_exact_row_token: enrichment.original_exact_row_token,
                derived_conversation_digest: enrichment.derived_conversation_digest,
            });
        }
        let input_updates = input_update_wires
            .into_iter()
            .map(|input_update| {
                PreparedRecoveryInputUpdate::decode(
                    input_update.input_id,
                    input_update.expected_row_digest,
                    input_update.target_bytes,
                )
            })
            .collect::<Result<Vec<_>, _>>()
            .map_err(|detail| {
                RuntimeStoreError::ReadFailed(format!(
                    "committed recovery input update is malformed: {detail}"
                ))
            })?;
        validate_recovery_input_update_order(&input_updates).map_err(|detail| {
            RuntimeStoreError::ReadFailed(format!(
                "committed recovery input update order is malformed: {detail}"
            ))
        })?;
        let class = recovery_class_from_name(&class)?;
        let disposition = recovery_disposition_from_name(&disposition)?;
        let mut evidence = PreparedRecoveryEvidence {
            session_id,
            candidate_id,
            candidate_run_id,
            class,
            disposition,
            session_authority,
            receipt_digest_enrichments,
            predecessor_nonterminal_input_set_revision:
                RecoveryInputSetRevision::from_store_generation(
                    predecessor_nonterminal_input_set_revision,
                ),
            predecessor_nonterminal_input_set_token,
            input_updates,
            lifecycle_target_token,
            lifecycle_target_bytes,
            exact_witness: String::new(),
        };
        let derived_exact_witness = evidence.compute_exact_witness(&receipt).map_err(|error| {
            RuntimeStoreError::ReadFailed(format!(
                "failed to verify committed recovery exact witness: {error}"
            ))
        })?;
        if derived_exact_witness != exact_witness {
            return Err(RuntimeStoreError::ReadFailed(
                "committed recovery exact witness does not match its serialized effects"
                    .to_string(),
            ));
        }
        evidence.exact_witness = exact_witness;
        Ok(Self { evidence, receipt })
    }
}

/// Kind of atomic boundary carried by [`PreparedRuntimeSessionCommit`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PreparedRuntimeSessionCommitKind {
    /// Session-control snapshot without a run receipt.
    SnapshotOnly,
    /// Successfully applied run boundary.
    Success,
    /// Direct service-turn terminal with machine lifecycle authority.
    ServiceTurnTerminal,
    /// Failed-but-applied run boundary with machine lifecycle authority.
    MachineTerminal,
    /// Machine-authorized durable-tail recovery with exact physical-head CAS.
    Recovery,
}

#[derive(Debug, Clone)]
pub(crate) enum PreparedRuntimeSessionCommitPayload {
    SnapshotOnly {
        session: BoundSessionCommit,
    },
    Success {
        session: Option<BoundSessionCommit>,
        receipt: RunBoundaryReceipt,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: Option<meerkat_core::types::SessionId>,
    },
    PromoteWholeBlobSuccess {
        promotion: PreparedWholeBlobProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    },
    PromoteHeadCanonicalSuccess {
        promotion: PreparedHeadCanonicalProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    },
    ServiceTurnTerminal {
        session: BoundSessionCommit,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    },
    PromoteWholeBlobServiceTurnTerminal {
        promotion: PreparedWholeBlobProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    },
    PromoteHeadCanonicalServiceTurnTerminal {
        promotion: PreparedHeadCanonicalProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    },
    MachineTerminal {
        session: BoundSessionCommit,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    },
    PromoteWholeBlobMachineTerminal {
        promotion: PreparedWholeBlobProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    },
    PromoteHeadCanonicalMachineTerminal {
        promotion: PreparedHeadCanonicalProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    },
    Recovery {
        session: BoundSessionCommit,
        evidence: PreparedRecoveryEvidence,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    },
    PromoteWholeBlobRecovery {
        promotion: PreparedWholeBlobRecoveryPromotion,
        evidence: PreparedRecoveryEvidence,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    },
}

/// Opaque, valid-by-construction request for one runtime-owned session
/// boundary.
///
/// The constructors prevent receipt, lifecycle, and session-key values from
/// being combined into a boundary shape that no
/// [`RuntimeStore`] verb can commit. The sealed session remains typed and lazy
/// until the selected persistence profile consumes it.
#[derive(Debug, Clone)]
pub struct PreparedRuntimeSessionCommit {
    payload: PreparedRuntimeSessionCommitPayload,
}

impl PreparedRuntimeSessionCommit {
    fn validate_whole_blob_promotion_binding(
        promotion: &PreparedWholeBlobProvisionalPromotion,
        receipt: &RunBoundaryReceipt,
        session_store_key: &meerkat_core::types::SessionId,
    ) -> Result<(), RuntimeStoreError> {
        if promotion.authority().run_id() != &receipt.run_id {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: promotion.authority().session_id().to_string(),
                detail: "WholeBlob promotion receipt run differs from provisional authority"
                    .to_string(),
            });
        }
        if receipt.conversation_digest.as_deref() != Some(promotion.conversation_digest.as_str())
            || u64::try_from(receipt.message_count).ok() != Some(promotion.message_count)
        {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: promotion.authority().session_id().to_string(),
                detail: "WholeBlob final receipt differs from checkpoint candidate count/digest"
                    .to_string(),
            });
        }
        if promotion.authority().session_id() != session_store_key {
            return Err(RuntimeStoreError::SessionKeyMismatch {
                expected: promotion.authority().session_id().clone(),
                actual: session_store_key.clone(),
            });
        }
        Ok(())
    }

    fn validate_head_canonical_promotion_binding(
        promotion: &PreparedHeadCanonicalProvisionalPromotion,
        receipt: &RunBoundaryReceipt,
        session_store_key: &meerkat_core::types::SessionId,
    ) -> Result<(), RuntimeStoreError> {
        if promotion.authority().run_id() != &receipt.run_id {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: promotion.authority().session_id().to_string(),
                detail: "HeadCanonical promotion receipt run differs from provisional authority"
                    .to_string(),
            });
        }
        if promotion.authority().session_id() != session_store_key {
            return Err(RuntimeStoreError::SessionKeyMismatch {
                expected: promotion.authority().session_id().clone(),
                actual: session_store_key.clone(),
            });
        }
        if receipt.conversation_digest.as_deref()
            != Some(promotion.checkpoint().conversation_digest())
            || u64::try_from(receipt.message_count).ok()
                != Some(promotion.checkpoint().message_count())
        {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: promotion.authority().session_id().to_string(),
                detail:
                    "HeadCanonical promotion terminal receipt differs from checkpoint digest/count"
                        .to_string(),
            });
        }
        Ok(())
    }

    /// Prepare a session-control snapshot without a run receipt.
    #[must_use]
    pub fn snapshot_only(session: BoundSessionCommit) -> Self {
        Self {
            payload: PreparedRuntimeSessionCommitPayload::SnapshotOnly { session },
        }
    }

    /// Prepare a successful run boundary.
    #[must_use]
    pub fn success(
        session: Option<BoundSessionCommit>,
        receipt: RunBoundaryReceipt,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: Option<meerkat_core::types::SessionId>,
    ) -> Self {
        Self {
            payload: PreparedRuntimeSessionCommitPayload::Success {
                session,
                receipt,
                input_updates,
                session_store_key,
            },
        }
    }

    /// Prepare a successful final boundary that promotes an already-written
    /// WholeBlob candidate without carrying or materializing its Session body.
    pub fn promote_whole_blob_success(
        promotion: PreparedWholeBlobProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        Self::validate_whole_blob_promotion_binding(&promotion, &receipt, &session_store_key)?;
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::PromoteWholeBlobSuccess {
                promotion,
                receipt,
                input_updates,
                session_store_key,
            },
        })
    }

    /// Prepare a successful final boundary that promotes an already-applied
    /// HeadCanonical physical checkpoint without reapplying its rows.
    pub fn promote_head_canonical_success(
        promotion: PreparedHeadCanonicalProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        Self::validate_head_canonical_promotion_binding(&promotion, &receipt, &session_store_key)?;
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalSuccess {
                promotion,
                receipt,
                input_updates,
                session_store_key,
            },
        })
    }

    /// Prepare a failed-but-applied run boundary.
    #[must_use]
    pub fn machine_terminal(
        session: BoundSessionCommit,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Self {
        Self {
            payload: PreparedRuntimeSessionCommitPayload::MachineTerminal {
                session,
                receipt,
                machine_lifecycle,
                input_updates,
                session_store_key,
            },
        }
    }

    /// Prepare a failed-but-applied final boundary that promotes the exact
    /// store-owned WholeBlob candidate.
    pub fn promote_whole_blob_machine_terminal(
        promotion: PreparedWholeBlobProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        Self::validate_whole_blob_promotion_binding(&promotion, &receipt, &session_store_key)?;
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::PromoteWholeBlobMachineTerminal {
                promotion,
                receipt,
                machine_lifecycle,
                input_updates,
                session_store_key,
            },
        })
    }

    /// Prepare a failed-but-applied boundary that promotes the exact applied
    /// HeadCanonical provisional checkpoint.
    pub fn promote_head_canonical_machine_terminal(
        promotion: PreparedHeadCanonicalProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        Self::validate_head_canonical_promotion_binding(&promotion, &receipt, &session_store_key)?;
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalMachineTerminal {
                promotion,
                receipt,
                machine_lifecycle,
                input_updates,
                session_store_key,
            },
        })
    }

    /// Prepare a direct service-turn terminal boundary.
    #[must_use]
    pub fn service_turn_terminal(
        session: BoundSessionCommit,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Self {
        Self {
            payload: PreparedRuntimeSessionCommitPayload::ServiceTurnTerminal {
                session,
                receipt,
                machine_lifecycle,
                session_store_key,
            },
        }
    }

    /// Prepare a direct service-turn terminal boundary that promotes the exact
    /// store-owned WholeBlob candidate.
    pub fn promote_whole_blob_service_turn_terminal(
        promotion: PreparedWholeBlobProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        Self::validate_whole_blob_promotion_binding(&promotion, &receipt, &session_store_key)?;
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::PromoteWholeBlobServiceTurnTerminal {
                promotion,
                receipt,
                machine_lifecycle,
                session_store_key,
            },
        })
    }

    /// Prepare a service-turn terminal boundary that promotes the exact
    /// applied HeadCanonical provisional checkpoint.
    pub fn promote_head_canonical_service_turn_terminal(
        promotion: PreparedHeadCanonicalProvisionalPromotion,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        Self::validate_head_canonical_promotion_binding(&promotion, &receipt, &session_store_key)?;
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalServiceTurnTerminal {
                promotion,
                receipt,
                machine_lifecycle,
                session_store_key,
            },
        })
    }

    /// Prepare the only boundary allowed to realize machine-authorized
    /// durable-tail recovery.
    ///
    /// Crate-only because the recovery classifier and generated machine must
    /// seal [`PreparedRecoveryEvidence`]; a caller cannot select a recovery
    /// disposition or physical-head proof.
    pub(crate) fn machine_terminal_recovery(
        session: BoundSessionCommit,
        evidence: PreparedRecoveryEvidence,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        if &session_store_key != evidence.session_id() {
            return Err(RuntimeStoreError::SessionKeyMismatch {
                expected: evidence.session_id().clone(),
                actual: session_store_key,
            });
        }
        if &receipt.run_id != evidence.candidate_run_id() {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: evidence.session_id().to_string(),
                detail: "recovery receipt run differs from sealed candidate run".to_string(),
            });
        }
        evidence.verify_head_canonical_boundary(&session, &receipt)?;
        evidence.verify_request_effects(&receipt, &machine_lifecycle)?;
        let input_updates = evidence.cloned_input_updates();
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::Recovery {
                session,
                evidence,
                receipt,
                machine_lifecycle,
                input_updates,
                session_store_key,
            },
        })
    }

    /// Prepare a WholeBlob recovery without routing the recovered document
    /// through the ordinary whole-document boundary.
    ///
    /// Completed candidates become metadata-only promotions. Interrupted
    /// candidates retain the one already-materialized repaired artifact.
    pub(crate) fn machine_terminal_whole_blob_recovery(
        repaired_document: Option<BoundSessionCommit>,
        evidence: PreparedRecoveryEvidence,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<Self, RuntimeStoreError> {
        if &session_store_key != evidence.session_id() {
            return Err(RuntimeStoreError::SessionKeyMismatch {
                expected: evidence.session_id().clone(),
                actual: session_store_key,
            });
        }
        if &receipt.run_id != evidence.candidate_run_id() {
            return Err(RuntimeStoreError::SessionPersistenceAuthorityConflict {
                runtime_id: evidence.session_id().to_string(),
                detail: "recovery receipt run differs from sealed candidate run".to_string(),
            });
        }
        evidence.verify_request_effects(&receipt, &machine_lifecycle)?;
        let input_updates = evidence.cloned_input_updates();
        evidence.verify_input_updates(&input_updates)?;
        let promotion =
            PreparedWholeBlobRecoveryPromotion::prepare(repaired_document.as_ref(), &evidence)?;
        Ok(Self {
            payload: PreparedRuntimeSessionCommitPayload::PromoteWholeBlobRecovery {
                promotion,
                evidence,
                receipt,
                machine_lifecycle,
                input_updates,
                session_store_key,
            },
        })
    }

    /// Boundary shape selected by the constructor.
    #[must_use]
    pub fn kind(&self) -> PreparedRuntimeSessionCommitKind {
        match &self.payload {
            PreparedRuntimeSessionCommitPayload::SnapshotOnly { .. } => {
                PreparedRuntimeSessionCommitKind::SnapshotOnly
            }
            PreparedRuntimeSessionCommitPayload::Success { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobSuccess { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalSuccess { .. } => {
                PreparedRuntimeSessionCommitKind::Success
            }
            PreparedRuntimeSessionCommitPayload::ServiceTurnTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobServiceTurnTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalServiceTurnTerminal {
                ..
            } => PreparedRuntimeSessionCommitKind::ServiceTurnTerminal,
            PreparedRuntimeSessionCommitPayload::MachineTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobMachineTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalMachineTerminal { .. } => {
                PreparedRuntimeSessionCommitKind::MachineTerminal
            }
            PreparedRuntimeSessionCommitPayload::Recovery { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobRecovery { .. } => {
                PreparedRuntimeSessionCommitKind::Recovery
            }
        }
    }

    /// Prepared session document, when this boundary carries one.
    #[must_use]
    pub fn session(&self) -> Option<&BoundSessionCommit> {
        match &self.payload {
            PreparedRuntimeSessionCommitPayload::SnapshotOnly { session, .. }
            | PreparedRuntimeSessionCommitPayload::ServiceTurnTerminal { session, .. }
            | PreparedRuntimeSessionCommitPayload::MachineTerminal { session, .. }
            | PreparedRuntimeSessionCommitPayload::Recovery { session, .. } => Some(session),
            PreparedRuntimeSessionCommitPayload::Success { session, .. } => session.as_ref(),
            PreparedRuntimeSessionCommitPayload::PromoteWholeBlobSuccess { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobServiceTurnTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobMachineTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalSuccess { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalServiceTurnTerminal {
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalMachineTerminal { .. } => {
                None
            }
            PreparedRuntimeSessionCommitPayload::PromoteWholeBlobRecovery { .. } => None,
        }
    }

    /// Boundary receipt, absent only for snapshot-only commits.
    #[must_use]
    pub fn receipt(&self) -> Option<&RunBoundaryReceipt> {
        match &self.payload {
            PreparedRuntimeSessionCommitPayload::SnapshotOnly { .. } => None,
            PreparedRuntimeSessionCommitPayload::Success { receipt, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobSuccess { receipt, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalSuccess {
                receipt, ..
            }
            | PreparedRuntimeSessionCommitPayload::ServiceTurnTerminal { receipt, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobServiceTurnTerminal {
                receipt,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalServiceTurnTerminal {
                receipt,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::MachineTerminal { receipt, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobMachineTerminal {
                receipt,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalMachineTerminal {
                receipt,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::Recovery { receipt, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobRecovery { receipt, .. } => {
                Some(receipt)
            }
        }
    }

    /// Input-state mutations committed with a run boundary.
    #[must_use]
    pub fn input_updates(&self) -> Option<&[InputStatePersistenceRecord]> {
        match &self.payload {
            PreparedRuntimeSessionCommitPayload::SnapshotOnly { .. } => None,
            PreparedRuntimeSessionCommitPayload::ServiceTurnTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobServiceTurnTerminal { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalServiceTurnTerminal {
                ..
            } => Some(&[]),
            PreparedRuntimeSessionCommitPayload::Success { input_updates, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobSuccess {
                input_updates, ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalSuccess {
                input_updates,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::MachineTerminal { input_updates, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobMachineTerminal {
                input_updates,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalMachineTerminal {
                input_updates,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::Recovery { input_updates, .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobRecovery {
                input_updates, ..
            } => Some(input_updates),
        }
    }

    /// Explicit SessionStore identity carried by a run boundary.
    #[must_use]
    pub fn session_store_key(&self) -> Option<&meerkat_core::types::SessionId> {
        match &self.payload {
            PreparedRuntimeSessionCommitPayload::SnapshotOnly { .. } => None,
            PreparedRuntimeSessionCommitPayload::Success {
                session_store_key, ..
            } => session_store_key.as_ref(),
            PreparedRuntimeSessionCommitPayload::PromoteWholeBlobSuccess {
                session_store_key,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalSuccess {
                session_store_key,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::ServiceTurnTerminal {
                session_store_key, ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobServiceTurnTerminal {
                session_store_key,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalServiceTurnTerminal {
                session_store_key,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::MachineTerminal {
                session_store_key, ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobMachineTerminal {
                session_store_key,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalMachineTerminal {
                session_store_key,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::Recovery {
                session_store_key, ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobRecovery {
                session_store_key,
                ..
            } => Some(session_store_key),
        }
    }

    /// Machine lifecycle authority, present only for a machine-terminal commit.
    #[must_use]
    pub fn machine_lifecycle(&self) -> Option<&MachineLifecycleCommit> {
        match &self.payload {
            PreparedRuntimeSessionCommitPayload::ServiceTurnTerminal {
                machine_lifecycle, ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobServiceTurnTerminal {
                machine_lifecycle,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalServiceTurnTerminal {
                machine_lifecycle,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::MachineTerminal {
                machine_lifecycle, ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobMachineTerminal {
                machine_lifecycle,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalMachineTerminal {
                machine_lifecycle,
                ..
            }
            | PreparedRuntimeSessionCommitPayload::Recovery {
                machine_lifecycle, ..
            }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobRecovery {
                machine_lifecycle,
                ..
            } => Some(machine_lifecycle),
            PreparedRuntimeSessionCommitPayload::SnapshotOnly { .. }
            | PreparedRuntimeSessionCommitPayload::Success { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteWholeBlobSuccess { .. }
            | PreparedRuntimeSessionCommitPayload::PromoteHeadCanonicalSuccess { .. } => None,
        }
    }

    pub(crate) fn into_payload(self) -> PreparedRuntimeSessionCommitPayload {
        self.payload
    }
}

/// Store-internal exact pairing produced only from a sealed typed WholeBlob
/// boundary. Backends consume the typed Session for guards and compaction
/// intents while writing the already-materialized shared bytes and authority.
/// This prevents a prepared boundary from reparsing its own JSON.
#[derive(Debug, Clone)]
pub(crate) struct PreparedWholeBlobSnapshot {
    session: std::sync::Arc<meerkat_core::Session>,
    serialized: SerializedSessionSnapshot,
    blob_sha256: String,
}

impl PreparedWholeBlobSnapshot {
    #[must_use]
    pub(crate) fn session(&self) -> &meerkat_core::Session {
        self.session.as_ref()
    }

    #[must_use]
    pub(crate) fn blob_sha256(&self) -> &str {
        &self.blob_sha256
    }

    #[must_use]
    pub(crate) fn into_parts(
        self,
    ) -> (
        std::sync::Arc<meerkat_core::Session>,
        SerializedSessionSnapshot,
        String,
    ) {
        (self.session, self.serialized, self.blob_sha256)
    }
}

fn prepared_whole_blob_snapshot(
    session: &BoundSessionCommit,
) -> Result<PreparedWholeBlobSnapshot, RuntimeStoreError> {
    let typed_session = session.session_arc_cloned().ok_or_else(|| {
        RuntimeStoreError::SessionPersistenceAuthorityConflict {
            runtime_id: "<untyped-whole-blob-boundary>".to_string(),
            detail: "prepared WholeBlob boundary requires a sealed typed Session".to_string(),
        }
    })?;
    let artifact = session.whole_blob_artifact().map_err(|error| {
        RuntimeStoreError::WriteFailed(format!(
            "failed to materialize whole-blob session boundary: {error}"
        ))
    })?;
    Ok(PreparedWholeBlobSnapshot {
        session: typed_session,
        serialized: whole_blob_serialized_snapshot(artifact.bytes_arc()),
        blob_sha256: artifact.row_sha256_token().to_string(),
    })
}

fn parsed_whole_blob_snapshot(
    serialized: SerializedSessionSnapshot,
) -> Result<PreparedWholeBlobSnapshot, RuntimeStoreError> {
    let session = std::sync::Arc::new(
        meerkat_core::Session::from_persisted_bytes(serialized.session_snapshot.as_ref()).map_err(
            |error| {
                RuntimeStoreError::WriteFailed(format!(
                    "whole-blob snapshot is not a valid Session payload: {error}"
                ))
            },
        )?,
    );
    let blob_sha256 = format!(
        "row-sha256:{:x}",
        sha2::Sha256::digest(serialized.session_snapshot.as_ref())
    );
    Ok(PreparedWholeBlobSnapshot {
        session,
        serialized,
        blob_sha256,
    })
}

fn whole_blob_serialized_snapshot(
    session_snapshot: std::sync::Arc<Vec<u8>>,
) -> SerializedSessionSnapshot {
    SerializedSessionSnapshot { session_snapshot }
}

/// Opaque generated runtime-delivery authority persisted by a
/// [`RuntimeStore`].
///
/// Stores compare the mechanical revision and retain the bytes exactly. They
/// do not interpret delivery lifecycle, sequence assignment, or cursor
/// semantics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeDeliveryAuthorityRecord {
    revision: u64,
    state_json: Vec<u8>,
}

impl RuntimeDeliveryAuthorityRecord {
    #[doc(hidden)]
    pub fn from_parts(revision: u64, state_json: Vec<u8>) -> Self {
        Self {
            revision,
            state_json,
        }
    }

    pub fn revision(&self) -> u64 {
        self.revision
    }

    pub fn state_json(&self) -> &[u8] {
        &self.state_json
    }
}

/// Opaque runtime-inbox row committed alongside generated delivery authority.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuntimeDeliveryStoreRecord {
    delivery_id: String,
    sequence: u64,
    submission_json: Vec<u8>,
}

impl RuntimeDeliveryStoreRecord {
    #[doc(hidden)]
    pub fn from_parts(
        delivery_id: impl Into<String>,
        sequence: u64,
        submission_json: Vec<u8>,
    ) -> Self {
        Self {
            delivery_id: delivery_id.into(),
            sequence,
            submission_json,
        }
    }

    pub fn delivery_id(&self) -> &str {
        &self.delivery_id
    }

    pub fn sequence(&self) -> u64 {
        self.sequence
    }

    pub fn submission_json(&self) -> &[u8] {
        &self.submission_json
    }
}

/// Mechanical compare-and-swap result for runtime-delivery authority.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RuntimeDeliveryAuthorityCasOutcome {
    Applied(RuntimeDeliveryAuthorityRecord),
    Conflict(Option<RuntimeDeliveryAuthorityRecord>),
}

fn validated_compaction_projection_intents(
    session: &meerkat_core::Session,
) -> Result<Vec<meerkat_core::CompactionProjectionIntent>, RuntimeStoreError> {
    session
        .validated_compaction_projection_intents()
        .map_err(|error| RuntimeStoreError::WriteFailed(error.to_string()))
}

/// Clear one finalized compaction intent from the ordinary session document.
///
/// Physical currentness remains store-owned; mutating this domain payload
/// neither consumes nor mints persistence authority.
pub(crate) fn complete_compaction_projection_intent(
    session: &mut meerkat_core::Session,
    projection: &meerkat_core::CompactionProjectionId,
) -> Result<(), RuntimeStoreError> {
    session
        .complete_compaction_projection_intent(projection)
        .map_err(|error| RuntimeStoreError::WriteFailed(error.to_string()))?;
    Ok(())
}

/// Runtime binding facts selected by generated MeerkatMachine authority.
///
/// RuntimeStore implementations persist and read these facts as part of a
/// machine lifecycle snapshot. The commit token that writes these facts stays
/// crate-private so compatibility callers cannot mint replacement lifecycle
/// truth.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MachineLifecycleBindingFacts {
    agent_runtime_id: Option<String>,
    fence_token: Option<u64>,
    runtime_generation: Option<u64>,
    runtime_epoch_id: Option<String>,
}

/// Durable identity receipt for the last completed supervisor revoke.
///
/// This is not a live supervisor binding and carries no route address. It is
/// only the identity/key/epoch witness needed to authorize an exact duplicate
/// revoke response after a cold restart; the current authenticated request
/// supplies its current route.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RevokedSupervisorReceipt {
    peer_id: String,
    signing_public_key: String,
    epoch: u64,
}

/// Durable current supervisor binding used to authenticate terminal retry
/// traffic after a cold runtime restart.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SupervisorBindingReceipt {
    name: String,
    peer_id: String,
    address: String,
    signing_public_key: String,
    epoch: u64,
}

/// Durable in-flight supervisor revocation receipt.
///
/// This is the closed-world hand-off between generated machine authority and
/// the concrete router mutation.  It deliberately retains the complete prior
/// route so a cold runtime can authenticate an exact retry and re-materialize
/// the generated remove obligation without resurrecting a live binding.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SupervisorRevocationPendingReceipt {
    name: String,
    peer_id: String,
    address: String,
    signing_public_key: String,
    epoch: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SupervisorRotationPersistencePhase {
    PreviousRevokePending,
    NextPublishPending,
    Completed,
    Rejected,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SupervisorRotationRejection {
    OperationConflict,
    NotBound,
    SenderMismatch,
    TargetEpochNotAdvanced,
    InvalidTarget,
    UnsupportedProtocolVersion,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SupervisorRotationReceipt {
    operation_id: meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId,
    phase: SupervisorRotationPersistencePhase,
    rejection: Option<SupervisorRotationRejection>,
    previous: SupervisorBindingReceipt,
    next: SupervisorBindingReceipt,
}

impl SupervisorRotationReceipt {
    pub(crate) fn new(
        operation_id: meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId,
        phase: SupervisorRotationPersistencePhase,
        rejection: Option<SupervisorRotationRejection>,
        previous: SupervisorBindingReceipt,
        next: SupervisorBindingReceipt,
    ) -> Self {
        Self {
            operation_id,
            phase,
            rejection,
            previous,
            next,
        }
    }

    pub fn operation_id(
        &self,
    ) -> meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId {
        self.operation_id
    }

    pub fn phase(&self) -> SupervisorRotationPersistencePhase {
        self.phase
    }

    pub fn rejection(&self) -> Option<SupervisorRotationRejection> {
        self.rejection
    }

    pub fn previous(&self) -> &SupervisorBindingReceipt {
        &self.previous
    }

    pub fn next(&self) -> &SupervisorBindingReceipt {
        &self.next
    }
}

impl SupervisorBindingReceipt {
    pub(crate) fn new(
        name: String,
        peer_id: String,
        address: String,
        signing_public_key: String,
        epoch: u64,
    ) -> Self {
        Self {
            name,
            peer_id,
            address,
            signing_public_key,
            epoch,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn peer_id(&self) -> &str {
        &self.peer_id
    }

    pub fn address(&self) -> &str {
        &self.address
    }

    pub fn signing_public_key(&self) -> &str {
        &self.signing_public_key
    }

    pub fn epoch(&self) -> u64 {
        self.epoch
    }
}

impl RevokedSupervisorReceipt {
    pub(crate) fn new(peer_id: String, signing_public_key: String, epoch: u64) -> Self {
        Self {
            peer_id,
            signing_public_key,
            epoch,
        }
    }

    pub fn peer_id(&self) -> &str {
        &self.peer_id
    }

    pub fn signing_public_key(&self) -> &str {
        &self.signing_public_key
    }

    pub fn epoch(&self) -> u64 {
        self.epoch
    }
}

impl SupervisorRevocationPendingReceipt {
    pub(crate) fn new(
        name: String,
        peer_id: String,
        address: String,
        signing_public_key: String,
        epoch: u64,
    ) -> Self {
        Self {
            name,
            peer_id,
            address,
            signing_public_key,
            epoch,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn peer_id(&self) -> &str {
        &self.peer_id
    }

    pub fn address(&self) -> &str {
        &self.address
    }

    pub fn signing_public_key(&self) -> &str {
        &self.signing_public_key
    }

    pub fn epoch(&self) -> u64 {
        self.epoch
    }
}

/// Closed durable supervisor authority state. Each variant owns one complete
/// recovery shape; terminal rotation receipts retain their exact operation and
/// participant descriptors for idempotent submission and later observation.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum SupervisorAuthoritySnapshot {
    #[default]
    UnboundNoReceipt,
    Bound(SupervisorBindingReceipt),
    RevocationPending(SupervisorRevocationPendingReceipt),
    RotationOperation(SupervisorRotationReceipt),
    RevokedReceipt(RevokedSupervisorReceipt),
    WithRotationHistory {
        current: Box<SupervisorAuthoritySnapshot>,
        terminal_receipts: std::collections::BTreeMap<
            meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId,
            SupervisorRotationReceipt,
        >,
    },
}

impl MachineLifecycleBindingFacts {
    pub(crate) fn new(
        agent_runtime_id: Option<String>,
        fence_token: Option<u64>,
        runtime_generation: Option<u64>,
        runtime_epoch_id: Option<String>,
    ) -> Self {
        Self {
            agent_runtime_id,
            fence_token,
            runtime_generation,
            runtime_epoch_id,
        }
    }

    pub fn agent_runtime_id(&self) -> Option<&str> {
        self.agent_runtime_id.as_deref()
    }

    pub fn fence_token(&self) -> Option<u64> {
        self.fence_token
    }

    pub fn runtime_generation(&self) -> Option<u64> {
        self.runtime_generation
    }

    pub fn runtime_epoch_id(&self) -> Option<&str> {
        self.runtime_epoch_id.as_deref()
    }
}

/// Exact content version of one observed machine-lifecycle row.
///
/// The version is the domain-prefixed SHA-256 digest of the raw stored bytes,
/// not a decoded projection. It therefore remains a valid target-local CAS
/// witness for unsupported and malformed rows as well as current records.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MachineLifecycleObservationVersion(String);

impl MachineLifecycleObservationVersion {
    /// Derive the exact target-local compare token for opaque stored bytes.
    ///
    /// Custom [`RuntimeStore`] implementations use the same constructor for
    /// both observations and successful CAS receipts; no decoded lifecycle
    /// shape is allowed to stand in for the physical row version.
    pub fn from_raw_record(bytes: &[u8]) -> Self {
        Self(format!("sha256:{:x}", Sha256::digest(bytes)))
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Independently observed run-binding atoms.
///
/// A torn row may contain exactly one side of this pair. The store preserves
/// that shape; the generated reconciler, not the decoder, decides whether it
/// can be normalized.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MachineLifecyclePreRunPhase {
    Idle,
    Attached,
    Retired,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MachineLifecycleRunFacts {
    current_run_id: Option<RunId>,
    pre_run_phase: Option<MachineLifecyclePreRunPhase>,
}

impl MachineLifecycleRunFacts {
    pub(crate) fn new(
        current_run_id: Option<RunId>,
        pre_run_phase: Option<MachineLifecyclePreRunPhase>,
    ) -> Self {
        Self {
            current_run_id,
            pre_run_phase,
        }
    }

    #[must_use]
    pub fn current_run_id(&self) -> Option<&RunId> {
        self.current_run_id.as_ref()
    }

    #[must_use]
    pub fn pre_run_phase(&self) -> Option<MachineLifecyclePreRunPhase> {
        self.pre_run_phase
    }
}

/// Decoded runtime-lifecycle observation.
///
/// The lifecycle phase, four binding atoms, and two run atoms remain
/// independently optional. This type deliberately represents partial tuples
/// such as `current_run_id = Some` with `pre_run_phase = None` instead of
/// rejecting them as an impossible transition shape.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodedMachineLifecycleObservation {
    record_version: u16,
    runtime_state: Option<RuntimeState>,
    binding: MachineLifecycleBindingFacts,
    run: MachineLifecycleRunFacts,
    supervisor_authority: SupervisorAuthoritySnapshot,
    unregister_progress: Option<MachineUnregisterProgressSnapshot>,
}

impl DecodedMachineLifecycleObservation {
    #[must_use]
    pub fn record_version(&self) -> u16 {
        self.record_version
    }

    #[must_use]
    pub fn runtime_state(&self) -> Option<RuntimeState> {
        self.runtime_state
    }

    #[must_use]
    pub fn binding(&self) -> &MachineLifecycleBindingFacts {
        &self.binding
    }

    #[must_use]
    pub fn run(&self) -> &MachineLifecycleRunFacts {
        &self.run
    }

    #[must_use]
    pub fn supervisor_authority(&self) -> &SupervisorAuthoritySnapshot {
        &self.supervisor_authority
    }

    #[must_use]
    pub fn unregister_progress(&self) -> Option<&MachineUnregisterProgressSnapshot> {
        self.unregister_progress.as_ref()
    }
}

/// Lossless classification of one physical machine-lifecycle row.
///
/// Transport failures remain [`RuntimeStoreError`] values. Every successfully
/// read row is classified without collapsing unsupported or corrupt bytes into
/// absence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MachineLifecycleObservation {
    Missing,
    Decoded {
        record: DecodedMachineLifecycleObservation,
        version: MachineLifecycleObservationVersion,
    },
    Unsupported {
        record_version: u64,
        evidence_digest: String,
        version: MachineLifecycleObservationVersion,
    },
    Malformed {
        record_version: Option<u64>,
        evidence_digest: String,
        version: MachineLifecycleObservationVersion,
        detail: String,
    },
}

impl MachineLifecycleObservation {
    /// Losslessly classify one successfully read physical lifecycle row.
    ///
    /// This is the canonical adapter seam for custom stores. Transport
    /// failure stays an outer [`RuntimeStoreError`]; every byte sequence read
    /// successfully becomes Decoded, Unsupported, or Malformed here.
    #[must_use]
    pub fn from_raw_record(bytes: &[u8]) -> Self {
        classify_machine_lifecycle_record(bytes)
    }

    #[must_use]
    pub fn version(&self) -> Option<&MachineLifecycleObservationVersion> {
        match self {
            Self::Missing => None,
            Self::Decoded { version, .. }
            | Self::Unsupported { version, .. }
            | Self::Malformed { version, .. } => Some(version),
        }
    }

    #[must_use]
    pub fn evidence_digest(&self) -> Option<&str> {
        match self {
            Self::Unsupported {
                evidence_digest, ..
            }
            | Self::Malformed {
                evidence_digest, ..
            } => Some(evidence_digest),
            Self::Missing | Self::Decoded { .. } => None,
        }
    }
}

/// Target-local precondition for lifecycle normalization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MachineLifecycleExpectedVersion {
    Missing,
    Version(MachineLifecycleObservationVersion),
}

impl MachineLifecycleObservation {
    /// Exact target-local precondition represented by this observation.
    ///
    /// Missing is a first-class compare value. Every present row, including
    /// unsupported and malformed bytes, is compared by its raw-content
    /// version rather than by a decoded projection.
    #[must_use]
    pub fn expected_version(&self) -> MachineLifecycleExpectedVersion {
        self.version()
            .map_or(MachineLifecycleExpectedVersion::Missing, |version| {
                MachineLifecycleExpectedVersion::Version(version.clone())
            })
    }
}

/// Result of executing one synchronous target write under an external fence.
///
/// The fence is deliberately runtime-generic. A caller may back it with a
/// lease, process-incarnation lock, or another authority source without the
/// runtime store depending on that owner's domain types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RuntimeStoreWriteFenceOutcome {
    /// The fence was current and invoked the supplied operation exactly once.
    Applied,
    /// Durable authority was superseded. The operation was not invoked.
    Conflict { reason: String },
    /// Authority could not be checked temporarily. The operation was not
    /// invoked and the caller should retry after re-observation.
    Backoff { reason: String },
}

/// Synchronous authority guard for a RuntimeStore target write.
///
/// Implementations MUST retain their authority serialization guard for the
/// full duration of `operation`, invoke it exactly once only when authority is
/// current, and never invoke it for Conflict or Backoff. The operation is
/// synchronous by design so built-in stores can call it inside their own lock
/// or transaction immediately before the target write. Time-bounded authority
/// must be evaluated using the authority store's own clock while that guard is
/// held, never a caller-supplied observation timestamp. Once a successful
/// operation returns, the fence must return Applied without a new fallible
/// boundary. Implementations must not re-enter the same RuntimeStore from this
/// callback.
pub trait RuntimeStoreWriteFence: Send + Sync {
    fn execute_if_current(
        &self,
        operation: Box<dyn FnOnce() -> Result<(), RuntimeStoreError> + '_>,
    ) -> Result<RuntimeStoreWriteFenceOutcome, RuntimeStoreError>;
}

pub(crate) fn execute_runtime_store_write_fence(
    write_fence: &dyn RuntimeStoreWriteFence,
    operation: impl FnOnce() -> Result<(), RuntimeStoreError>,
) -> Result<RuntimeStoreWriteFenceOutcome, RuntimeStoreError> {
    let invoked = std::cell::Cell::new(false);
    let operation_result = std::cell::RefCell::new(None);
    let checked_operation = || {
        invoked.set(true);
        let result = operation();
        *operation_result.borrow_mut() = Some(result.clone());
        result
    };
    let outcome = write_fence.execute_if_current(Box::new(checked_operation))?;
    if let Some(Err(error)) = operation_result.borrow_mut().take() {
        return Err(error);
    }
    let shape_is_valid = matches!(
        (&outcome, invoked.get()),
        (RuntimeStoreWriteFenceOutcome::Applied, true)
            | (
                RuntimeStoreWriteFenceOutcome::Conflict { .. }
                    | RuntimeStoreWriteFenceOutcome::Backoff { .. },
                false,
            )
    );
    if !shape_is_valid {
        return Err(RuntimeStoreError::Internal(
            "runtime write fence returned an outcome inconsistent with operation execution"
                .to_string(),
        ));
    }
    Ok(outcome)
}

/// Result of a target-local lifecycle CAS performed under an external fence.
///
/// Applied and AlreadyExact carry the exact decoded row used to construct the
/// fresh process-local registration. Callers never receive or construct a
/// MachineLifecycleCommit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FencedMachineLifecycleCasOutcome {
    Applied {
        record: DecodedMachineLifecycleObservation,
        version: MachineLifecycleObservationVersion,
    },
    AlreadyExact {
        record: DecodedMachineLifecycleObservation,
        version: MachineLifecycleObservationVersion,
    },
    Conflict {
        current: MachineLifecycleObservation,
    },
    FenceConflict {
        reason: String,
    },
    FenceBackoff {
        reason: String,
    },
}

/// Result of a target-local lifecycle compare-and-swap.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MachineLifecycleCasOutcome {
    Applied {
        version: MachineLifecycleObservationVersion,
    },
    Conflict {
        current: MachineLifecycleObservation,
    },
}

/// Durable read-back shape for machine-owned lifecycle state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MachineLifecycleSnapshot {
    runtime_state: RuntimeState,
    binding: MachineLifecycleBindingFacts,
    run: MachineLifecycleRunFacts,
    supervisor_authority: SupervisorAuthoritySnapshot,
    unregister_progress: Option<MachineUnregisterProgressSnapshot>,
}

/// Durable generated unregister-saga progress needed to resume an interrupted
/// Draining epoch without reconstructing missing producer outcomes in shell
/// code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MachineUnregisterProgressSnapshot {
    runtime_loop_drain_pending: bool,
    comms_drain_exit_pending: bool,
    completion_waiter_drain_pending: bool,
    runtime_loop_forced_abort: bool,
    comms_drain_forced_abort: bool,
}

impl MachineUnregisterProgressSnapshot {
    pub(crate) fn new(
        runtime_loop_drain_pending: bool,
        comms_drain_exit_pending: bool,
        completion_waiter_drain_pending: bool,
        runtime_loop_forced_abort: bool,
        comms_drain_forced_abort: bool,
    ) -> Self {
        Self {
            runtime_loop_drain_pending,
            comms_drain_exit_pending,
            completion_waiter_drain_pending,
            runtime_loop_forced_abort,
            comms_drain_forced_abort,
        }
    }

    pub(crate) fn runtime_loop_drain_pending(&self) -> bool {
        self.runtime_loop_drain_pending
    }

    pub(crate) fn comms_drain_exit_pending(&self) -> bool {
        self.comms_drain_exit_pending
    }

    pub(crate) fn completion_waiter_drain_pending(&self) -> bool {
        self.completion_waiter_drain_pending
    }

    pub(crate) fn runtime_loop_forced_abort(&self) -> bool {
        self.runtime_loop_forced_abort
    }

    pub(crate) fn comms_drain_forced_abort(&self) -> bool {
        self.comms_drain_forced_abort
    }
}

impl MachineLifecycleSnapshot {
    pub(crate) fn new(
        runtime_state: RuntimeState,
        binding: MachineLifecycleBindingFacts,
        supervisor_authority: SupervisorAuthoritySnapshot,
    ) -> Self {
        Self::new_with_unregister_progress(runtime_state, binding, supervisor_authority, None)
    }

    pub(crate) fn new_with_unregister_progress(
        runtime_state: RuntimeState,
        binding: MachineLifecycleBindingFacts,
        supervisor_authority: SupervisorAuthoritySnapshot,
        unregister_progress: Option<MachineUnregisterProgressSnapshot>,
    ) -> Self {
        Self::new_with_run_and_unregister_progress(
            runtime_state,
            binding,
            MachineLifecycleRunFacts::default(),
            supervisor_authority,
            unregister_progress,
        )
    }

    pub(crate) fn new_with_run_and_unregister_progress(
        runtime_state: RuntimeState,
        binding: MachineLifecycleBindingFacts,
        run: MachineLifecycleRunFacts,
        supervisor_authority: SupervisorAuthoritySnapshot,
        unregister_progress: Option<MachineUnregisterProgressSnapshot>,
    ) -> Self {
        Self {
            runtime_state,
            binding,
            run,
            supervisor_authority,
            unregister_progress,
        }
    }

    /// Runtime state selected by the owning MeerkatMachine transition.
    pub fn runtime_state(&self) -> RuntimeState {
        self.runtime_state
    }

    /// Runtime binding facts selected by the owning MeerkatMachine transition.
    pub fn binding(&self) -> &MachineLifecycleBindingFacts {
        &self.binding
    }

    /// Independently persisted run-binding atoms.
    pub fn run(&self) -> &MachineLifecycleRunFacts {
        &self.run
    }

    pub fn supervisor_authority(&self) -> &SupervisorAuthoritySnapshot {
        &self.supervisor_authority
    }

    pub fn unregister_progress(&self) -> Option<&MachineUnregisterProgressSnapshot> {
        self.unregister_progress.as_ref()
    }
}

#[allow(
    clippy::option_option,
    reason = "serde distinguishes missing from explicit null"
)]
fn deserialize_present_nullable<'de, D, T>(deserializer: D) -> Result<Option<Option<T>>, D::Error>
where
    D: serde::Deserializer<'de>,
    T: serde::Deserialize<'de>,
{
    <Option<T> as serde::Deserialize>::deserialize(deserializer).map(Some)
}

#[allow(
    clippy::option_option,
    reason = "serde distinguishes missing from explicit null"
)]
fn require_present_nullable<T>(
    value: Option<Option<T>>,
    field: &str,
) -> Result<Option<T>, RuntimeStoreError> {
    value.ok_or_else(|| {
        RuntimeStoreError::ReadFailed(format!(
            "machine lifecycle field {field} is required (explicit null is allowed)"
        ))
    })
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MachineLifecycleBindingFactsStoreWire {
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes missing from explicit null"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    agent_runtime_id: Option<Option<String>>,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes missing from explicit null"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    fence_token: Option<Option<u64>>,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes missing from explicit null"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    runtime_generation: Option<Option<u64>>,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes missing from explicit null"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    runtime_epoch_id: Option<Option<String>>,
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MachineLifecycleBindingFactsStoreWireV1 {
    agent_runtime_id: Option<String>,
    fence_token: Option<u64>,
    runtime_generation: Option<u64>,
    runtime_epoch_id: Option<String>,
}

impl From<&MachineLifecycleBindingFacts> for MachineLifecycleBindingFactsStoreWire {
    fn from(binding: &MachineLifecycleBindingFacts) -> Self {
        Self {
            agent_runtime_id: Some(binding.agent_runtime_id().map(ToOwned::to_owned)),
            fence_token: Some(binding.fence_token()),
            runtime_generation: Some(binding.runtime_generation()),
            runtime_epoch_id: Some(binding.runtime_epoch_id().map(ToOwned::to_owned)),
        }
    }
}

impl TryFrom<MachineLifecycleBindingFactsStoreWire> for MachineLifecycleBindingFacts {
    type Error = RuntimeStoreError;

    fn try_from(binding: MachineLifecycleBindingFactsStoreWire) -> Result<Self, Self::Error> {
        Ok(Self::new(
            require_present_nullable(binding.agent_runtime_id, "binding.agent_runtime_id")?,
            require_present_nullable(binding.fence_token, "binding.fence_token")?,
            require_present_nullable(binding.runtime_generation, "binding.runtime_generation")?,
            require_present_nullable(binding.runtime_epoch_id, "binding.runtime_epoch_id")?,
        ))
    }
}

impl From<MachineLifecycleBindingFactsStoreWireV1> for MachineLifecycleBindingFacts {
    fn from(binding: MachineLifecycleBindingFactsStoreWireV1) -> Self {
        Self::new(
            binding.agent_runtime_id,
            binding.fence_token,
            binding.runtime_generation,
            binding.runtime_epoch_id,
        )
    }
}

#[derive(serde::Serialize)]
#[serde(deny_unknown_fields)]
struct MachineLifecycleSnapshotStoreWire {
    record_version: u16,
    runtime_state: RuntimeState,
    binding: MachineLifecycleBindingFactsStoreWire,
    current_run_id: Option<RunId>,
    pre_run_phase: Option<MachineLifecyclePreRunPhase>,
    supervisor_authority: SupervisorAuthoritySnapshotStoreWire,
    unregister_progress: Option<MachineUnregisterProgressSnapshotStoreWire>,
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MachineLifecycleObservationStoreWireV4 {
    record_version: u16,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes a missing phase from an explicitly absent observed phase"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    runtime_state: Option<Option<RuntimeState>>,
    binding: MachineLifecycleBindingFactsStoreWire,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes a missing run id from an explicitly absent run id"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    current_run_id: Option<Option<RunId>>,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes a missing pre-run phase from an explicitly absent phase"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    pre_run_phase: Option<Option<MachineLifecyclePreRunPhase>>,
    supervisor_authority: SupervisorAuthoritySnapshotStoreWire,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes a missing v4 field from explicit null progress"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    unregister_progress: Option<Option<MachineUnregisterProgressSnapshotStoreWire>>,
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MachineLifecycleSnapshotStoreWireV3 {
    record_version: u16,
    runtime_state: RuntimeState,
    binding: MachineLifecycleBindingFactsStoreWire,
    supervisor_authority: SupervisorAuthoritySnapshotStoreWire,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes a missing v3 field from explicit null progress"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    unregister_progress: Option<Option<MachineUnregisterProgressSnapshotStoreWire>>,
}

#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MachineLifecycleSnapshotStoreWireV2 {
    record_version: u16,
    runtime_state: RuntimeState,
    binding: MachineLifecycleBindingFactsStoreWire,
    supervisor_authority: SupervisorAuthoritySnapshotStoreWire,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MachineUnregisterProgressSnapshotStoreWire {
    runtime_loop_drain_pending: bool,
    comms_drain_exit_pending: bool,
    completion_waiter_drain_pending: bool,
    runtime_loop_forced_abort: bool,
    comms_drain_forced_abort: bool,
}

impl From<&MachineUnregisterProgressSnapshot> for MachineUnregisterProgressSnapshotStoreWire {
    fn from(snapshot: &MachineUnregisterProgressSnapshot) -> Self {
        Self {
            runtime_loop_drain_pending: snapshot.runtime_loop_drain_pending(),
            comms_drain_exit_pending: snapshot.comms_drain_exit_pending(),
            completion_waiter_drain_pending: snapshot.completion_waiter_drain_pending(),
            runtime_loop_forced_abort: snapshot.runtime_loop_forced_abort(),
            comms_drain_forced_abort: snapshot.comms_drain_forced_abort(),
        }
    }
}

impl From<MachineUnregisterProgressSnapshotStoreWire> for MachineUnregisterProgressSnapshot {
    fn from(snapshot: MachineUnregisterProgressSnapshotStoreWire) -> Self {
        Self::new(
            snapshot.runtime_loop_drain_pending,
            snapshot.comms_drain_exit_pending,
            snapshot.completion_waiter_drain_pending,
            snapshot.runtime_loop_forced_abort,
            snapshot.comms_drain_forced_abort,
        )
    }
}

/// Exact pre-supervisor-authority lifecycle shape. Version 1 is decoded only
/// through this migration carrier so a missing authority on a current record
/// cannot be confused with legacy data.
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct MachineLifecycleSnapshotStoreWireV1 {
    record_version: u16,
    runtime_state: RuntimeState,
    binding: MachineLifecycleBindingFactsStoreWireV1,
}

#[derive(serde::Deserialize)]
struct MachineLifecycleSnapshotStoreVersionProbe {
    record_version: u16,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
enum SupervisorAuthoritySnapshotStoreWire {
    #[default]
    UnboundNoReceipt,
    Bound {
        binding: SupervisorBindingReceiptStoreWire,
    },
    RevocationPending {
        pending: SupervisorRevocationPendingReceiptStoreWire,
    },
    RotationOperation {
        rotation: SupervisorRotationReceiptStoreWire,
    },
    RevokedReceipt {
        receipt: RevokedSupervisorReceiptStoreWire,
    },
    WithRotationHistory {
        current: Box<SupervisorAuthoritySnapshotStoreWire>,
        terminal_receipts: Vec<SupervisorRotationReceiptStoreWire>,
    },
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct SupervisorBindingReceiptStoreWire {
    name: String,
    peer_id: String,
    address: String,
    signing_public_key: String,
    epoch: u64,
}

impl From<&SupervisorBindingReceipt> for SupervisorBindingReceiptStoreWire {
    fn from(receipt: &SupervisorBindingReceipt) -> Self {
        Self {
            name: receipt.name().to_owned(),
            peer_id: receipt.peer_id().to_owned(),
            address: receipt.address().to_owned(),
            signing_public_key: receipt.signing_public_key().to_owned(),
            epoch: receipt.epoch(),
        }
    }
}

impl From<SupervisorBindingReceiptStoreWire> for SupervisorBindingReceipt {
    fn from(receipt: SupervisorBindingReceiptStoreWire) -> Self {
        Self::new(
            receipt.name,
            receipt.peer_id,
            receipt.address,
            receipt.signing_public_key,
            receipt.epoch,
        )
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct RevokedSupervisorReceiptStoreWire {
    peer_id: String,
    signing_public_key: String,
    epoch: u64,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct SupervisorRevocationPendingReceiptStoreWire {
    name: String,
    peer_id: String,
    address: String,
    signing_public_key: String,
    epoch: u64,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct SupervisorRotationReceiptStoreWire {
    operation_id: meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId,
    phase: SupervisorRotationPersistencePhase,
    #[allow(
        clippy::option_option,
        reason = "serde distinguishes missing from explicit null"
    )]
    #[serde(default, deserialize_with = "deserialize_present_nullable")]
    rejection: Option<Option<SupervisorRotationRejection>>,
    previous: SupervisorBindingReceiptStoreWire,
    next: SupervisorBindingReceiptStoreWire,
}

impl From<&SupervisorRotationReceipt> for SupervisorRotationReceiptStoreWire {
    fn from(receipt: &SupervisorRotationReceipt) -> Self {
        Self {
            operation_id: receipt.operation_id(),
            phase: receipt.phase(),
            rejection: Some(receipt.rejection()),
            previous: receipt.previous().into(),
            next: receipt.next().into(),
        }
    }
}

impl TryFrom<SupervisorRotationReceiptStoreWire> for SupervisorRotationReceipt {
    type Error = RuntimeStoreError;

    fn try_from(receipt: SupervisorRotationReceiptStoreWire) -> Result<Self, Self::Error> {
        Ok(Self::new(
            receipt.operation_id,
            receipt.phase,
            require_present_nullable(receipt.rejection, "supervisor_authority.rotation.rejection")?,
            receipt.previous.into(),
            receipt.next.into(),
        ))
    }
}

impl From<&SupervisorRevocationPendingReceipt> for SupervisorRevocationPendingReceiptStoreWire {
    fn from(receipt: &SupervisorRevocationPendingReceipt) -> Self {
        Self {
            name: receipt.name().to_owned(),
            peer_id: receipt.peer_id().to_owned(),
            address: receipt.address().to_owned(),
            signing_public_key: receipt.signing_public_key().to_owned(),
            epoch: receipt.epoch(),
        }
    }
}

impl From<SupervisorRevocationPendingReceiptStoreWire> for SupervisorRevocationPendingReceipt {
    fn from(receipt: SupervisorRevocationPendingReceiptStoreWire) -> Self {
        Self::new(
            receipt.name,
            receipt.peer_id,
            receipt.address,
            receipt.signing_public_key,
            receipt.epoch,
        )
    }
}

impl From<&RevokedSupervisorReceipt> for RevokedSupervisorReceiptStoreWire {
    fn from(receipt: &RevokedSupervisorReceipt) -> Self {
        Self {
            peer_id: receipt.peer_id().to_owned(),
            signing_public_key: receipt.signing_public_key().to_owned(),
            epoch: receipt.epoch(),
        }
    }
}

impl From<RevokedSupervisorReceiptStoreWire> for RevokedSupervisorReceipt {
    fn from(receipt: RevokedSupervisorReceiptStoreWire) -> Self {
        Self::new(receipt.peer_id, receipt.signing_public_key, receipt.epoch)
    }
}

impl From<&SupervisorAuthoritySnapshot> for SupervisorAuthoritySnapshotStoreWire {
    fn from(snapshot: &SupervisorAuthoritySnapshot) -> Self {
        match snapshot {
            SupervisorAuthoritySnapshot::UnboundNoReceipt => Self::UnboundNoReceipt,
            SupervisorAuthoritySnapshot::Bound(binding) => Self::Bound {
                binding: binding.into(),
            },
            SupervisorAuthoritySnapshot::RevocationPending(pending) => Self::RevocationPending {
                pending: pending.into(),
            },
            SupervisorAuthoritySnapshot::RotationOperation(rotation) => Self::RotationOperation {
                rotation: rotation.into(),
            },
            SupervisorAuthoritySnapshot::RevokedReceipt(receipt) => Self::RevokedReceipt {
                receipt: receipt.into(),
            },
            SupervisorAuthoritySnapshot::WithRotationHistory {
                current,
                terminal_receipts,
            } => Self::WithRotationHistory {
                current: Box::new(current.as_ref().into()),
                terminal_receipts: terminal_receipts.values().map(Into::into).collect(),
            },
        }
    }
}

fn supervisor_authority_read_error(
    context: &str,
    detail: impl std::fmt::Display,
) -> RuntimeStoreError {
    RuntimeStoreError::ReadFailed(format!("{context}: {detail}"))
}

fn validate_supervisor_descriptor(
    name: &str,
    peer_id: &str,
    address: &str,
    signing_public_key: &str,
    context: &str,
) -> Result<(), RuntimeStoreError> {
    let pubkey = crate::comms_drain::decode_supervisor_signing_public_key(signing_public_key)
        .map_err(|error| supervisor_authority_read_error(context, error))?;
    let spec = meerkat_contracts::wire::supervisor_bridge::BridgePeerSpec {
        name: name.to_owned(),
        peer_id: peer_id.to_owned(),
        address: address.to_owned(),
        pubkey,
    };
    meerkat_core::comms::TrustedPeerDescriptor::try_from(&spec)
        .map(|_| ())
        .map_err(|error| supervisor_authority_read_error(context, error))
}

fn validate_supervisor_binding_receipt(
    receipt: &SupervisorBindingReceipt,
    context: &str,
) -> Result<(), RuntimeStoreError> {
    validate_supervisor_descriptor(
        receipt.name(),
        receipt.peer_id(),
        receipt.address(),
        receipt.signing_public_key(),
        context,
    )
}

fn validate_revoked_supervisor_receipt(
    receipt: &RevokedSupervisorReceipt,
    context: &str,
) -> Result<(), RuntimeStoreError> {
    let pubkey =
        crate::comms_drain::decode_supervisor_signing_public_key(receipt.signing_public_key())
            .map_err(|error| supervisor_authority_read_error(context, error))?;
    if pubkey.iter().all(|byte| *byte == 0) {
        return Err(supervisor_authority_read_error(
            context,
            "supervisor signing public key must be non-zero",
        ));
    }
    let peer_id = meerkat_core::comms::PeerId::parse(receipt.peer_id())
        .map_err(|error| supervisor_authority_read_error(context, error))?;
    let derived = meerkat_core::comms::PeerId::from_ed25519_pubkey(&pubkey);
    if peer_id != derived {
        return Err(supervisor_authority_read_error(
            context,
            format!("peer id {peer_id} does not match signing-key-derived id {derived}"),
        ));
    }
    Ok(())
}

fn validate_supervisor_rotation_receipt(
    receipt: &SupervisorRotationReceipt,
    terminal_history: bool,
) -> Result<(), RuntimeStoreError> {
    let operation_id = receipt.operation_id();
    if operation_id.as_uuid().is_nil() {
        return Err(supervisor_authority_read_error(
            "supervisor rotation operation",
            "operation id must not be the nil UUID",
        ));
    }
    validate_supervisor_binding_receipt(
        receipt.previous(),
        &format!("supervisor rotation {operation_id} previous authority is invalid"),
    )?;

    let rejection_matches = matches!(
        (receipt.phase(), receipt.rejection()),
        (
            SupervisorRotationPersistencePhase::PreviousRevokePending
                | SupervisorRotationPersistencePhase::NextPublishPending
                | SupervisorRotationPersistencePhase::Completed,
            None
        ) | (SupervisorRotationPersistencePhase::Rejected, Some(_))
    );
    if !rejection_matches {
        return Err(supervisor_authority_read_error(
            "supervisor rotation operation",
            format!("{operation_id} has inconsistent rejection state"),
        ));
    }
    if terminal_history
        && !matches!(
            receipt.phase(),
            SupervisorRotationPersistencePhase::Completed
                | SupervisorRotationPersistencePhase::Rejected
        )
    {
        return Err(supervisor_authority_read_error(
            "supervisor rotation history",
            format!("{operation_id} is not terminal"),
        ));
    }

    match receipt.phase() {
        SupervisorRotationPersistencePhase::PreviousRevokePending
        | SupervisorRotationPersistencePhase::NextPublishPending => {
            validate_supervisor_binding_receipt(
                receipt.next(),
                &format!("supervisor rotation {operation_id} target is invalid"),
            )?;
            if receipt.next().epoch() <= receipt.previous().epoch() {
                return Err(supervisor_authority_read_error(
                    "supervisor rotation operation",
                    format!(
                        "{operation_id} target epoch {} does not advance previous epoch {}",
                        receipt.next().epoch(),
                        receipt.previous().epoch()
                    ),
                ));
            }
        }
        SupervisorRotationPersistencePhase::Completed => {
            validate_supervisor_binding_receipt(
                receipt.next(),
                &format!("supervisor rotation {operation_id} target is invalid"),
            )?;
            // A legacy member may already have the exact target installed
            // before the operation protocol assigns an id. Its adoption
            // receipt is Completed with an exact previous == next witness.
            let exact_current_adoption = receipt.previous() == receipt.next();
            if !exact_current_adoption && receipt.next().epoch() <= receipt.previous().epoch() {
                return Err(supervisor_authority_read_error(
                    "supervisor rotation operation",
                    format!(
                        "{operation_id} completed target epoch {} does not advance previous epoch {}",
                        receipt.next().epoch(),
                        receipt.previous().epoch()
                    ),
                ));
            }
        }
        SupervisorRotationPersistencePhase::Rejected => {
            let Some(rejection) = receipt.rejection() else {
                return Err(supervisor_authority_read_error(
                    "supervisor rotation operation",
                    format!("{operation_id} rejected without a rejection class"),
                ));
            };
            match rejection {
                SupervisorRotationRejection::InvalidTarget
                | SupervisorRotationRejection::UnsupportedProtocolVersion => {
                    // These two rejection classes retain the undecodable target
                    // fields as raw evidence. They are deliberately exempt from
                    // target descriptor validation and epoch comparison.
                }
                SupervisorRotationRejection::TargetEpochNotAdvanced => {
                    validate_supervisor_binding_receipt(
                        receipt.next(),
                        &format!("supervisor rotation {operation_id} rejected target is invalid"),
                    )?;
                    if receipt.next().epoch() > receipt.previous().epoch() {
                        return Err(supervisor_authority_read_error(
                            "supervisor rotation operation",
                            format!(
                                "{operation_id} rejected as non-advancing but target epoch {} advances previous epoch {}",
                                receipt.next().epoch(),
                                receipt.previous().epoch()
                            ),
                        ));
                    }
                }
                SupervisorRotationRejection::OperationConflict
                | SupervisorRotationRejection::NotBound
                | SupervisorRotationRejection::SenderMismatch => {
                    return Err(supervisor_authority_read_error(
                        "supervisor rotation operation",
                        format!(
                            "{operation_id} transient rejection {rejection:?} must not be persisted as a durable receipt"
                        ),
                    ));
                }
            }
        }
    }
    Ok(())
}

type SupervisorEpochKeyIndex = std::collections::BTreeMap<u64, [u8; 32]>;

fn record_supervisor_epoch_key(
    epochs: &mut SupervisorEpochKeyIndex,
    epoch: u64,
    signing_public_key: &str,
    context: &str,
) -> Result<(), RuntimeStoreError> {
    let key = crate::comms_drain::decode_supervisor_signing_public_key(signing_public_key)
        .map_err(|error| supervisor_authority_read_error(context, error))?;
    if let Some(existing) = epochs.get(&epoch) {
        if existing != &key {
            return Err(supervisor_authority_read_error(
                context,
                format!("epoch {epoch} is bound to conflicting supervisor signing keys"),
            ));
        }
    } else {
        epochs.insert(epoch, key);
    }
    Ok(())
}

fn record_supervisor_binding_epoch(
    epochs: &mut SupervisorEpochKeyIndex,
    receipt: &SupervisorBindingReceipt,
    context: &str,
) -> Result<(), RuntimeStoreError> {
    record_supervisor_epoch_key(
        epochs,
        receipt.epoch(),
        receipt.signing_public_key(),
        context,
    )
}

fn record_rotation_authoritative_epochs(
    epochs: &mut SupervisorEpochKeyIndex,
    receipt: &SupervisorRotationReceipt,
    context: &str,
) -> Result<(), RuntimeStoreError> {
    record_supervisor_binding_epoch(epochs, receipt.previous(), context)?;
    if matches!(
        receipt.phase(),
        SupervisorRotationPersistencePhase::PreviousRevokePending
            | SupervisorRotationPersistencePhase::NextPublishPending
            | SupervisorRotationPersistencePhase::Completed
    ) {
        record_supervisor_binding_epoch(epochs, receipt.next(), context)?;
    }
    Ok(())
}

fn record_current_authoritative_epochs(
    epochs: &mut SupervisorEpochKeyIndex,
    current: &SupervisorAuthoritySnapshot,
) -> Result<(), RuntimeStoreError> {
    match current {
        SupervisorAuthoritySnapshot::UnboundNoReceipt => Ok(()),
        SupervisorAuthoritySnapshot::Bound(binding) => {
            record_supervisor_binding_epoch(epochs, binding, "current supervisor authority")
        }
        SupervisorAuthoritySnapshot::RevocationPending(pending) => record_supervisor_epoch_key(
            epochs,
            pending.epoch(),
            pending.signing_public_key(),
            "current pending supervisor revocation authority",
        ),
        SupervisorAuthoritySnapshot::RotationOperation(rotation) => {
            record_rotation_authoritative_epochs(
                epochs,
                rotation,
                "current supervisor rotation authority",
            )
        }
        SupervisorAuthoritySnapshot::RevokedReceipt(receipt) => record_supervisor_epoch_key(
            epochs,
            receipt.epoch(),
            receipt.signing_public_key(),
            "current revoked supervisor authority",
        ),
        SupervisorAuthoritySnapshot::WithRotationHistory { .. } => {
            Err(RuntimeStoreError::ReadFailed(
                "nested supervisor rotation history is not canonical".to_string(),
            ))
        }
    }
}

fn current_supervisor_epoch(current: &SupervisorAuthoritySnapshot) -> Option<u64> {
    match current {
        SupervisorAuthoritySnapshot::UnboundNoReceipt => None,
        SupervisorAuthoritySnapshot::Bound(binding) => Some(binding.epoch()),
        SupervisorAuthoritySnapshot::RevocationPending(pending) => Some(pending.epoch()),
        SupervisorAuthoritySnapshot::RotationOperation(rotation) => Some(match rotation.phase() {
            SupervisorRotationPersistencePhase::PreviousRevokePending
            | SupervisorRotationPersistencePhase::Rejected => rotation.previous().epoch(),
            SupervisorRotationPersistencePhase::NextPublishPending
            | SupervisorRotationPersistencePhase::Completed => rotation.next().epoch(),
        }),
        SupervisorAuthoritySnapshot::RevokedReceipt(receipt) => Some(receipt.epoch()),
        SupervisorAuthoritySnapshot::WithRotationHistory { .. } => None,
    }
}

fn terminal_rotation_authority_epoch(receipt: &SupervisorRotationReceipt) -> u64 {
    match receipt.phase() {
        SupervisorRotationPersistencePhase::Completed => receipt.next().epoch(),
        SupervisorRotationPersistencePhase::Rejected => receipt.previous().epoch(),
        SupervisorRotationPersistencePhase::PreviousRevokePending
        | SupervisorRotationPersistencePhase::NextPublishPending => receipt.previous().epoch(),
    }
}

fn validate_supervisor_rotation_history_coherence(
    current: &SupervisorAuthoritySnapshot,
    terminal_receipts: &std::collections::BTreeMap<
        meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId,
        SupervisorRotationReceipt,
    >,
) -> Result<(), RuntimeStoreError> {
    let Some(current_epoch) = current_supervisor_epoch(current) else {
        return Err(RuntimeStoreError::ReadFailed(
            "supervisor rotation history requires a current authority epoch".to_string(),
        ));
    };

    let mut epochs = SupervisorEpochKeyIndex::new();
    record_current_authoritative_epochs(&mut epochs, current)?;
    let mut history_high_water = 0;
    for receipt in terminal_receipts.values() {
        record_rotation_authoritative_epochs(
            &mut epochs,
            receipt,
            "supervisor rotation history authority",
        )?;
        history_high_water = history_high_water.max(terminal_rotation_authority_epoch(receipt));
    }
    if current_epoch < history_high_water {
        return Err(RuntimeStoreError::ReadFailed(format!(
            "current supervisor epoch {current_epoch} is below terminal rotation history high-water {history_high_water}"
        )));
    }
    Ok(())
}

fn validate_supervisor_authority_snapshot(
    snapshot: &SupervisorAuthoritySnapshot,
) -> Result<(), RuntimeStoreError> {
    match snapshot {
        SupervisorAuthoritySnapshot::UnboundNoReceipt => Ok(()),
        SupervisorAuthoritySnapshot::Bound(binding) => {
            validate_supervisor_binding_receipt(binding, "bound supervisor is invalid")
        }
        SupervisorAuthoritySnapshot::RevocationPending(pending) => validate_supervisor_descriptor(
            pending.name(),
            pending.peer_id(),
            pending.address(),
            pending.signing_public_key(),
            "pending supervisor revocation authority is invalid",
        ),
        SupervisorAuthoritySnapshot::RotationOperation(rotation) => {
            validate_supervisor_rotation_receipt(rotation, false)
        }
        SupervisorAuthoritySnapshot::RevokedReceipt(receipt) => {
            validate_revoked_supervisor_receipt(receipt, "revoked supervisor receipt is invalid")
        }
        SupervisorAuthoritySnapshot::WithRotationHistory {
            current,
            terminal_receipts,
        } => {
            if matches!(
                current.as_ref(),
                SupervisorAuthoritySnapshot::WithRotationHistory { .. }
            ) {
                return Err(RuntimeStoreError::ReadFailed(
                    "nested supervisor rotation history is not canonical".to_string(),
                ));
            }
            if terminal_receipts.is_empty() {
                return Err(RuntimeStoreError::ReadFailed(
                    "empty supervisor rotation history wrapper is not canonical".to_string(),
                ));
            }
            validate_supervisor_authority_snapshot(current)?;
            for (operation_id, receipt) in terminal_receipts {
                if operation_id != &receipt.operation_id() {
                    return Err(RuntimeStoreError::ReadFailed(format!(
                        "supervisor rotation history key {operation_id} does not match receipt id {}",
                        receipt.operation_id()
                    )));
                }
                validate_supervisor_rotation_receipt(receipt, true)?;
            }
            if let SupervisorAuthoritySnapshot::RotationOperation(active) = current.as_ref()
                && terminal_receipts.contains_key(&active.operation_id())
            {
                return Err(RuntimeStoreError::ReadFailed(
                    "active supervisor rotation is duplicated in terminal history".to_string(),
                ));
            }
            validate_supervisor_rotation_history_coherence(current, terminal_receipts)
        }
    }
}

impl TryFrom<SupervisorAuthoritySnapshotStoreWire> for SupervisorAuthoritySnapshot {
    type Error = RuntimeStoreError;

    fn try_from(snapshot: SupervisorAuthoritySnapshotStoreWire) -> Result<Self, Self::Error> {
        match snapshot {
            SupervisorAuthoritySnapshotStoreWire::UnboundNoReceipt => Ok(Self::UnboundNoReceipt),
            SupervisorAuthoritySnapshotStoreWire::Bound { binding } => {
                let binding = binding.into();
                validate_supervisor_binding_receipt(&binding, "bound supervisor is invalid")?;
                Ok(Self::Bound(binding))
            }
            SupervisorAuthoritySnapshotStoreWire::RevocationPending { pending } => {
                let pending: SupervisorRevocationPendingReceipt = pending.into();
                validate_supervisor_descriptor(
                    pending.name(),
                    pending.peer_id(),
                    pending.address(),
                    pending.signing_public_key(),
                    "pending supervisor revocation authority is invalid",
                )?;
                Ok(Self::RevocationPending(pending))
            }
            SupervisorAuthoritySnapshotStoreWire::RotationOperation { rotation } => {
                let receipt: SupervisorRotationReceipt = rotation.try_into()?;
                validate_supervisor_rotation_receipt(&receipt, false)?;
                Ok(Self::RotationOperation(receipt))
            }
            SupervisorAuthoritySnapshotStoreWire::RevokedReceipt { receipt } => {
                let receipt = receipt.into();
                validate_revoked_supervisor_receipt(
                    &receipt,
                    "revoked supervisor receipt is invalid",
                )?;
                Ok(Self::RevokedReceipt(receipt))
            }
            SupervisorAuthoritySnapshotStoreWire::WithRotationHistory {
                current,
                terminal_receipts,
            } => {
                if terminal_receipts.is_empty() {
                    return Err(RuntimeStoreError::ReadFailed(
                        "empty supervisor rotation history wrapper is not canonical".to_string(),
                    ));
                }
                let current = Self::try_from(*current)?;
                if matches!(current, Self::WithRotationHistory { .. }) {
                    return Err(RuntimeStoreError::ReadFailed(
                        "nested supervisor rotation history is not canonical".to_string(),
                    ));
                }
                let mut receipts = std::collections::BTreeMap::new();
                for wire in terminal_receipts {
                    let receipt: SupervisorRotationReceipt = wire.try_into()?;
                    validate_supervisor_rotation_receipt(&receipt, true)?;
                    if receipts.insert(receipt.operation_id(), receipt).is_some() {
                        return Err(RuntimeStoreError::ReadFailed(
                            "supervisor rotation history contains a duplicate operation id"
                                .to_string(),
                        ));
                    }
                }
                if let Self::RotationOperation(active) = &current
                    && receipts.contains_key(&active.operation_id())
                {
                    return Err(RuntimeStoreError::ReadFailed(
                        "active supervisor rotation is duplicated in terminal history".to_string(),
                    ));
                }
                let snapshot = Self::WithRotationHistory {
                    current: Box::new(current),
                    terminal_receipts: receipts,
                };
                validate_supervisor_authority_snapshot(&snapshot)?;
                Ok(snapshot)
            }
        }
    }
}

impl From<&MachineLifecycleSnapshot> for MachineLifecycleSnapshotStoreWire {
    fn from(snapshot: &MachineLifecycleSnapshot) -> Self {
        Self {
            record_version: MACHINE_LIFECYCLE_STORE_RECORD_VERSION,
            runtime_state: snapshot.runtime_state(),
            binding: snapshot.binding().into(),
            current_run_id: snapshot.run().current_run_id().cloned(),
            pre_run_phase: snapshot.run().pre_run_phase(),
            supervisor_authority: snapshot.supervisor_authority().into(),
            unregister_progress: snapshot.unregister_progress().map(Into::into),
        }
    }
}

fn validate_unregister_progress_snapshot(
    progress: Option<&MachineUnregisterProgressSnapshot>,
) -> Result<(), RuntimeStoreError> {
    if let Some(progress) = progress {
        if progress.runtime_loop_drain_pending() && progress.runtime_loop_forced_abort() {
            return Err(RuntimeStoreError::ReadFailed(
                "unregister runtime-loop forced disposition cannot precede obligation closure"
                    .into(),
            ));
        }
        if progress.comms_drain_exit_pending() && progress.comms_drain_forced_abort() {
            return Err(RuntimeStoreError::ReadFailed(
                "unregister comms-drain forced disposition cannot precede obligation closure"
                    .into(),
            ));
        }
    }
    Ok(())
}

impl TryFrom<MachineLifecycleSnapshotStoreWireV3> for MachineLifecycleSnapshot {
    type Error = RuntimeStoreError;

    fn try_from(record: MachineLifecycleSnapshotStoreWireV3) -> Result<Self, Self::Error> {
        if record.record_version != UNREGISTER_MACHINE_LIFECYCLE_STORE_RECORD_VERSION {
            return Err(RuntimeStoreError::ReadFailed(format!(
                "unsupported machine lifecycle store record version {}",
                record.record_version
            )));
        }
        let unregister_progress =
            require_present_nullable(record.unregister_progress, "unregister_progress")?
                .map(Into::into);
        validate_unregister_progress_snapshot(unregister_progress.as_ref())?;
        Ok(Self::new_with_unregister_progress(
            record.runtime_state,
            record.binding.try_into()?,
            record.supervisor_authority.try_into()?,
            unregister_progress,
        ))
    }
}

fn decode_machine_lifecycle_observation_v4(
    bytes: &[u8],
) -> Result<DecodedMachineLifecycleObservation, RuntimeStoreError> {
    let record = serde_json::from_slice::<MachineLifecycleObservationStoreWireV4>(bytes)
        .map_err(|err| RuntimeStoreError::ReadFailed(err.to_string()))?;
    if record.record_version != MACHINE_LIFECYCLE_STORE_RECORD_VERSION {
        return Err(RuntimeStoreError::ReadFailed(format!(
            "unsupported machine lifecycle store record version {}",
            record.record_version
        )));
    }
    let runtime_state = require_present_nullable(record.runtime_state, "runtime_state")?;
    let current_run_id = require_present_nullable(record.current_run_id, "current_run_id")?;
    let pre_run_phase = require_present_nullable(record.pre_run_phase, "pre_run_phase")?;
    let unregister_progress =
        require_present_nullable(record.unregister_progress, "unregister_progress")?
            .map(Into::into);
    validate_unregister_progress_snapshot(unregister_progress.as_ref())?;
    Ok(DecodedMachineLifecycleObservation {
        record_version: record.record_version,
        runtime_state,
        binding: record.binding.try_into()?,
        run: MachineLifecycleRunFacts::new(current_run_id, pre_run_phase),
        supervisor_authority: record.supervisor_authority.try_into()?,
        unregister_progress,
    })
}

fn decoded_machine_lifecycle_from_snapshot(
    record_version: u16,
    snapshot: MachineLifecycleSnapshot,
) -> DecodedMachineLifecycleObservation {
    DecodedMachineLifecycleObservation {
        record_version,
        runtime_state: Some(snapshot.runtime_state),
        binding: snapshot.binding,
        run: snapshot.run,
        supervisor_authority: snapshot.supervisor_authority,
        unregister_progress: snapshot.unregister_progress,
    }
}

fn decode_machine_lifecycle_store_record(
    bytes: &[u8],
) -> Result<MachineLifecycleSnapshot, RuntimeStoreError> {
    let version = serde_json::from_slice::<MachineLifecycleSnapshotStoreVersionProbe>(bytes)
        .map_err(|err| RuntimeStoreError::ReadFailed(err.to_string()))?;
    match version.record_version {
        LEGACY_MACHINE_LIFECYCLE_STORE_RECORD_VERSION => {
            let record = serde_json::from_slice::<MachineLifecycleSnapshotStoreWireV1>(bytes)
                .map_err(|err| RuntimeStoreError::ReadFailed(err.to_string()))?;
            if record.record_version != LEGACY_MACHINE_LIFECYCLE_STORE_RECORD_VERSION {
                return Err(RuntimeStoreError::ReadFailed(format!(
                    "unsupported machine lifecycle store record version {}",
                    record.record_version
                )));
            }
            Ok(MachineLifecycleSnapshot::new(
                record.runtime_state,
                record.binding.into(),
                SupervisorAuthoritySnapshot::UnboundNoReceipt,
            ))
        }
        SUPERVISOR_MACHINE_LIFECYCLE_STORE_RECORD_VERSION => {
            let record = serde_json::from_slice::<MachineLifecycleSnapshotStoreWireV2>(bytes)
                .map_err(|err| RuntimeStoreError::ReadFailed(err.to_string()))?;
            if record.record_version != SUPERVISOR_MACHINE_LIFECYCLE_STORE_RECORD_VERSION {
                return Err(RuntimeStoreError::ReadFailed(format!(
                    "unsupported machine lifecycle store record version {}",
                    record.record_version
                )));
            }
            Ok(MachineLifecycleSnapshot::new(
                record.runtime_state,
                record.binding.try_into()?,
                record.supervisor_authority.try_into()?,
            ))
        }
        UNREGISTER_MACHINE_LIFECYCLE_STORE_RECORD_VERSION => {
            let record = serde_json::from_slice::<MachineLifecycleSnapshotStoreWireV3>(bytes)
                .map_err(|err| RuntimeStoreError::ReadFailed(err.to_string()))?;
            MachineLifecycleSnapshot::try_from(record)
        }
        MACHINE_LIFECYCLE_STORE_RECORD_VERSION => {
            let record = decode_machine_lifecycle_observation_v4(bytes)?;
            let runtime_state = record.runtime_state.ok_or_else(|| {
                RuntimeStoreError::ReadFailed(
                    "machine lifecycle runtime_state cannot be null for strict recovery".into(),
                )
            })?;
            Ok(
                MachineLifecycleSnapshot::new_with_run_and_unregister_progress(
                    runtime_state,
                    record.binding,
                    record.run,
                    record.supervisor_authority,
                    record.unregister_progress,
                ),
            )
        }
        unsupported => Err(RuntimeStoreError::ReadFailed(format!(
            "unsupported machine lifecycle store record version {unsupported}"
        ))),
    }
}

#[derive(serde::Deserialize)]
struct MachineLifecycleRawVersionProbe {
    record_version: u64,
}

fn machine_lifecycle_record_version(bytes: &[u8]) -> Result<u64, String> {
    serde_json::from_slice::<MachineLifecycleRawVersionProbe>(bytes)
        .map(|probe| probe.record_version)
        .map_err(|error| {
            format!("machine lifecycle record_version is not uniquely readable: {error}")
        })
}

fn classify_machine_lifecycle_record(bytes: &[u8]) -> MachineLifecycleObservation {
    let version = MachineLifecycleObservationVersion::from_raw_record(bytes);
    let evidence_digest = version.as_str().to_owned();
    let record_version = match machine_lifecycle_record_version(bytes) {
        Ok(record_version) => record_version,
        Err(detail) => {
            return MachineLifecycleObservation::Malformed {
                record_version: None,
                evidence_digest,
                version,
                detail,
            };
        }
    };

    let supported = [
        u64::from(LEGACY_MACHINE_LIFECYCLE_STORE_RECORD_VERSION),
        u64::from(SUPERVISOR_MACHINE_LIFECYCLE_STORE_RECORD_VERSION),
        u64::from(UNREGISTER_MACHINE_LIFECYCLE_STORE_RECORD_VERSION),
        u64::from(MACHINE_LIFECYCLE_STORE_RECORD_VERSION),
    ];
    if !supported.contains(&record_version) {
        return MachineLifecycleObservation::Unsupported {
            record_version,
            evidence_digest,
            version,
        };
    }

    let decoded = if record_version == u64::from(MACHINE_LIFECYCLE_STORE_RECORD_VERSION) {
        decode_machine_lifecycle_observation_v4(bytes)
    } else {
        decode_machine_lifecycle_store_record(bytes).map(|snapshot| {
            decoded_machine_lifecycle_from_snapshot(record_version as u16, snapshot)
        })
    };
    match decoded {
        Ok(record) => MachineLifecycleObservation::Decoded { record, version },
        Err(error) => MachineLifecycleObservation::Malformed {
            record_version: Some(record_version),
            evidence_digest,
            version,
            detail: error.to_string(),
        },
    }
}

#[cfg(test)]
pub(crate) async fn assert_input_idempotency_final_image_contract(store: &dyn RuntimeStore) {
    fn state_with_key(input_id: InputId, key: &str) -> StoredInputState {
        let mut state = StoredInputState::new_accepted(input_id);
        state.state.idempotency_key = Some(IdempotencyKey::new(key));
        state
    }

    fn record(state: StoredInputState) -> InputStatePersistenceRecord {
        InputStatePersistenceRecord::from_machine_snapshot(state).unwrap()
    }

    let runtime_id =
        LogicalRuntimeId::new(format!("idempotency-final-image-{}", uuid::Uuid::now_v7()));
    let left_id = InputId::new();
    let right_id = InputId::new();
    let left = state_with_key(left_id.clone(), "left-key");
    let right = state_with_key(right_id.clone(), "right-key");
    store
        .persist_input_states_atomically(
            &runtime_id,
            &[record(left.clone()), record(right.clone())],
        )
        .await
        .unwrap();

    let swapped_left = state_with_key(left_id.clone(), "right-key");
    let swapped_right = state_with_key(right_id.clone(), "left-key");
    store
        .persist_input_states_atomically(
            &runtime_id,
            &[record(swapped_left.clone()), record(swapped_right.clone())],
        )
        .await
        .expect("complete-final-image persistence must permit a key swap");
    let left_key_owner = store
        .load_input_state_by_idempotency_key(&runtime_id, &IdempotencyKey::new("left-key"))
        .await
        .unwrap()
        .expect("left key after swap");
    let right_key_owner = store
        .load_input_state_by_idempotency_key(&runtime_id, &IdempotencyKey::new("right-key"))
        .await
        .unwrap()
        .expect("right key after swap");
    assert_eq!(left_key_owner.state().state.input_id, right_id);
    assert_eq!(right_key_owner.state().state.input_id, left_id);

    assert_eq!(
        store
            .compare_and_swap_input_states_atomically(
                &runtime_id,
                &[swapped_left, swapped_right],
                &[record(left), record(right)],
            )
            .await
            .unwrap(),
        InputStateBatchCasOutcome::Swapped,
        "complete-final-image CAS must permit the reverse key swap"
    );
}

#[cfg(test)]
pub(crate) fn pending_terminal_owner_fixture(
    input_id: InputId,
    published: bool,
) -> (StoredInputState, InputStatePersistenceRecord) {
    use crate::input_state::{
        InputState, InputStateSeed, InteractionTerminalBatchKey, InteractionTerminalCandidate,
        InteractionTerminalOutbox, InteractionTerminalOutboxPhase, InteractionTerminalPublication,
        interaction_terminal_payload_digest,
    };

    let candidate = InteractionTerminalCandidate::RuntimeTerminated {
        reason: "indexed terminal recovery fixture".to_string(),
    };
    let recipients = vec![input_id.clone()];
    let candidate_digest = interaction_terminal_payload_digest(&candidate).unwrap();
    let completion_input_ids_digest = interaction_terminal_payload_digest(&recipients).unwrap();
    let phase = if published {
        InteractionTerminalOutboxPhase::Published {
            finalization_failed: false,
            publication: InteractionTerminalPublication {
                terminal_seq: 1,
                payload_digest: "published-payload".to_string(),
            },
        }
    } else {
        InteractionTerminalOutboxPhase::Candidate
    };
    let outbox = InteractionTerminalOutbox {
        interaction_id: meerkat_core::interaction::InteractionId(input_id.0),
        input_id: input_id.clone(),
        batch_ordinal: 0,
        batch_key: InteractionTerminalBatchKey::RuntimeTermination {
            candidate_owner_input_id: input_id.clone(),
        },
        owner_session_id: meerkat_core::types::SessionId::new(),
        owner_agent_runtime_id: Some("indexed-runtime".to_string()),
        owner_fence_token: Some(1),
        owner_runtime_generation: Some(1),
        owner_runtime_epoch_id: Some("indexed-epoch".to_string()),
        candidate_owner_input_id: input_id.clone(),
        candidate: (!published).then_some(candidate),
        candidate_digest,
        completion_input_ids: (!published).then_some(recipients),
        completion_input_ids_digest,
        phase,
    };
    outbox.validate().unwrap();
    let directed_input = crate::mob_adapter::create_tracked_flow_step_input(
        "fixture-step",
        meerkat_core::types::ContentInput::Text("fixture-directed-input".to_string()),
        "fixture-run",
        None,
        &input_id.to_string(),
    )
    .unwrap();
    let mut state = InputState::new_accepted(input_id);
    state.directed_run_started_attribution =
        crate::input_state::DirectedRunStartedAttribution::from_input(&directed_input).unwrap();
    state.interaction_terminal_outbox = Some(outbox);
    let stored = StoredInputState {
        state,
        seed: InputStateSeed::new_accepted(),
    };
    let record = InputStatePersistenceRecord::from_machine_snapshot(stored.clone()).unwrap();
    (stored, record)
}

#[cfg(test)]
pub(crate) async fn assert_pending_terminal_owner_index_contract(store: &dyn RuntimeStore) {
    let runtime_id =
        LogicalRuntimeId::new(format!("pending-terminal-index-{}", uuid::Uuid::now_v7()));
    let mut ids = [InputId::new(), InputId::new(), InputId::new()];
    ids.sort_by_key(|input_id| input_id.0);
    let fixtures = ids
        .iter()
        .cloned()
        .map(|input_id| pending_terminal_owner_fixture(input_id, false))
        .collect::<Vec<_>>();
    for (_, record) in &fixtures {
        store
            .persist_input_state(&runtime_id, record)
            .await
            .unwrap();
    }

    let first_page = store
        .load_pending_terminal_owner_ids_page(&runtime_id, None, 2)
        .await
        .unwrap();
    assert_eq!(first_page, ids[..2]);
    let second_page = store
        .load_pending_terminal_owner_ids_page(&runtime_id, first_page.last(), 2)
        .await
        .unwrap();
    assert_eq!(second_page, ids[2..]);

    let (_, published) = pending_terminal_owner_fixture(ids[1].clone(), true);
    assert_eq!(
        store
            .compare_and_swap_input_states_atomically(
                &runtime_id,
                std::slice::from_ref(&fixtures[1].0),
                std::slice::from_ref(&published),
            )
            .await
            .unwrap(),
        InputStateBatchCasOutcome::Swapped
    );
    assert_eq!(
        store
            .load_pending_terminal_owner_ids_page(&runtime_id, None, 3)
            .await
            .unwrap(),
        vec![ids[0].clone(), ids[2].clone()]
    );
}

fn replacement_repair_blocked(
    evidence_digest: Option<String>,
    detail: impl Into<String>,
) -> RuntimeStoreError {
    RuntimeStoreError::MachineLifecycleRepairBlocked {
        evidence_digest,
        detail: detail.into(),
    }
}

/// Validate whether an exact lifecycle observation may be normalized.
///
/// Binding, fence, generation, and run atoms describe the dead process that
/// authored the observed row; they are not a durable high-water authority and
/// may be cleared by an exact-version cold-normalization CAS. Unsupported and
/// malformed rows remain fail-closed because this slice cannot prove their
/// custody fields safe to preserve.
fn validate_machine_lifecycle_replacement(
    current: &MachineLifecycleObservation,
    _current_raw: Option<&[u8]>,
    _replacement: &MachineLifecycleSnapshot,
) -> Result<(), RuntimeStoreError> {
    match current {
        MachineLifecycleObservation::Missing | MachineLifecycleObservation::Decoded { .. } => {
            Ok(())
        }
        MachineLifecycleObservation::Unsupported {
            evidence_digest,
            record_version,
            ..
        } => Err(replacement_repair_blocked(
            Some(evidence_digest.clone()),
            format!(
                "unsupported lifecycle record version {record_version} cannot prove fencing semantics"
            ),
        )),
        MachineLifecycleObservation::Malformed {
            evidence_digest,
            detail,
            ..
        } => Err(replacement_repair_blocked(
            Some(evidence_digest.clone()),
            format!("malformed lifecycle evidence is not reclaimable: {detail}"),
        )),
    }
}

struct PreparedMachineLifecycleReplacement {
    snapshot: MachineLifecycleSnapshot,
    bytes: Vec<u8>,
    version: MachineLifecycleObservationVersion,
}

impl PreparedMachineLifecycleReplacement {
    /// A runtime-authority normalization owns only lifecycle, binding, and run
    /// atoms. Preserve independent supervisor and unregister custody from the
    /// exact decoded row rather than copying it through the reconciler.
    fn preserve_observed_custody(
        mut self,
        current: &MachineLifecycleObservation,
    ) -> Result<Self, RuntimeStoreError> {
        if let MachineLifecycleObservation::Decoded { record, .. } = current {
            self.snapshot.supervisor_authority = record.supervisor_authority().clone();
            self.snapshot.unregister_progress = record.unregister_progress().cloned();
            self.bytes = MachineLifecycleStoreRecord::from_snapshot(&self.snapshot).encode()?;
            self.version = MachineLifecycleObservationVersion::from_raw_record(&self.bytes);
        }
        Ok(self)
    }
}

fn prepare_machine_lifecycle_replacement(
    commit: MachineLifecycleCommit,
) -> Result<PreparedMachineLifecycleReplacement, RuntimeStoreError> {
    let bytes = commit.store_record().encode()?;
    let version = MachineLifecycleObservationVersion::from_raw_record(&bytes);
    Ok(PreparedMachineLifecycleReplacement {
        snapshot: commit.into_snapshot(),
        bytes,
        version,
    })
}

fn decoded_prepared_machine_lifecycle_replacement(
    replacement: &PreparedMachineLifecycleReplacement,
) -> Result<DecodedMachineLifecycleObservation, RuntimeStoreError> {
    match classify_machine_lifecycle_record(&replacement.bytes) {
        MachineLifecycleObservation::Decoded { record, .. } => Ok(record),
        other => Err(RuntimeStoreError::Internal(format!(
            "machine-authorized lifecycle replacement did not decode: {other:?}"
        ))),
    }
}

/// Load the last persisted runtime-state projection from a generated lifecycle
/// record.
///
/// This is a projection of [`MachineLifecycleCommit`] authority. Store
/// implementations provide only opaque record bytes; the runtime crate owns the
/// decoding and rejects compatibility rows that are not machine lifecycle
/// records.
pub async fn load_runtime_state(
    store: &dyn RuntimeStore,
    runtime_id: &LogicalRuntimeId,
) -> Result<Option<RuntimeState>, RuntimeStoreError> {
    Ok(load_machine_lifecycle(store, runtime_id)
        .await?
        .map(|snapshot| snapshot.runtime_state()))
}

pub(crate) async fn load_machine_lifecycle(
    store: &dyn RuntimeStore,
    runtime_id: &LogicalRuntimeId,
) -> Result<Option<MachineLifecycleSnapshot>, RuntimeStoreError> {
    store
        .load_machine_lifecycle_record(runtime_id)
        .await?
        .map(|bytes| decode_machine_lifecycle_store_record(&bytes))
        .transpose()
}

/// Declared durable store record for generated machine lifecycle truth.
///
/// Stores receive this record from [`MachineLifecycleCommit`] and may persist
/// its encoded form. Loading must decode this exact record shape; compatibility
/// runtime-state projections are not lifecycle authority.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MachineLifecycleStoreRecord {
    snapshot: MachineLifecycleSnapshot,
}

impl MachineLifecycleStoreRecord {
    pub(crate) fn from_snapshot(snapshot: &MachineLifecycleSnapshot) -> Self {
        Self {
            snapshot: snapshot.clone(),
        }
    }

    /// Runtime state carried by this exact machine-authorized store record.
    ///
    /// Custom stores use this bounded fact to advance an existing session
    /// catalog projection in the same transaction as the lifecycle row.
    #[must_use]
    pub fn runtime_state(&self) -> RuntimeState {
        self.snapshot.runtime_state()
    }

    pub fn encode(&self) -> Result<Vec<u8>, RuntimeStoreError> {
        validate_supervisor_authority_snapshot(self.snapshot.supervisor_authority())
            .map_err(|error| RuntimeStoreError::WriteFailed(error.to_string()))?;
        validate_unregister_progress_snapshot(self.snapshot.unregister_progress())
            .map_err(|error| RuntimeStoreError::WriteFailed(error.to_string()))?;
        let wire = MachineLifecycleSnapshotStoreWire::from(&self.snapshot);
        serde_json::to_vec(&wire).map_err(|err| RuntimeStoreError::WriteFailed(err.to_string()))
    }
}

/// Machine-owned lifecycle commit token.
///
/// This token has no public constructor. RuntimeStore implementors can persist
/// the selected state and binding facts, but callers outside the machine/driver
/// commit path cannot select arbitrary lifecycle truth.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MachineLifecycleCommit {
    snapshot: MachineLifecycleSnapshot,
    /// When set, the store must verify the CURRENT persisted lifecycle row
    /// still matches this exact version inside the same transaction that
    /// writes the commit, and fail the whole boundary with
    /// [`RuntimeStoreError::MachineLifecycleVersionConflict`] otherwise.
    /// `None` preserves the historical last-writer semantics of the live
    /// driver commit path, whose exclusive in-process authority already
    /// serializes writers.
    expected_version: Option<MachineLifecycleExpectedVersion>,
}

impl MachineLifecycleCommit {
    #[cfg(test)]
    pub(crate) fn new_with_binding(
        runtime_state: RuntimeState,
        binding: MachineLifecycleBindingFacts,
        supervisor_authority: SupervisorAuthoritySnapshot,
    ) -> Self {
        Self::new_with_binding_and_unregister_progress(
            runtime_state,
            binding,
            supervisor_authority,
            None,
        )
    }

    pub(crate) fn new_with_binding_and_unregister_progress(
        runtime_state: RuntimeState,
        binding: MachineLifecycleBindingFacts,
        supervisor_authority: SupervisorAuthoritySnapshot,
        unregister_progress: Option<MachineUnregisterProgressSnapshot>,
    ) -> Self {
        Self::new_with_binding_run_and_unregister_progress(
            runtime_state,
            binding,
            MachineLifecycleRunFacts::default(),
            supervisor_authority,
            unregister_progress,
        )
    }

    pub(crate) fn new_with_binding_run_and_unregister_progress(
        runtime_state: RuntimeState,
        binding: MachineLifecycleBindingFacts,
        run: MachineLifecycleRunFacts,
        supervisor_authority: SupervisorAuthoritySnapshot,
        unregister_progress: Option<MachineUnregisterProgressSnapshot>,
    ) -> Self {
        Self {
            snapshot: MachineLifecycleSnapshot::new_with_run_and_unregister_progress(
                runtime_state,
                binding,
                run,
                supervisor_authority,
                unregister_progress,
            ),
            expected_version: None,
        }
    }

    /// Fence this commit on the exact lifecycle row version it was derived
    /// from. Used by cold recovery: between observing the persisted row and
    /// committing the recovered boundary another process may register or
    /// advance the runtime, and a blind upsert would stomp its truth.
    pub(crate) fn with_expected_version(
        mut self,
        expected: MachineLifecycleExpectedVersion,
    ) -> Self {
        self.expected_version = Some(expected);
        self
    }

    /// Runtime state selected by the owning MeerkatMachine transition.
    pub fn runtime_state(&self) -> RuntimeState {
        self.snapshot.runtime_state()
    }

    /// Durable lifecycle snapshot selected by the owning MeerkatMachine transition.
    pub fn snapshot(&self) -> &MachineLifecycleSnapshot {
        &self.snapshot
    }

    /// Durable record selected by the owning MeerkatMachine transition.
    pub fn store_record(&self) -> MachineLifecycleStoreRecord {
        MachineLifecycleStoreRecord::from_snapshot(&self.snapshot)
    }

    /// Exact prior row version this commit is fenced on, when the producer
    /// demanded compare-and-swap semantics. Store implementations MUST
    /// enforce it inside the same transaction that writes the commit.
    pub fn expected_version(&self) -> Option<&MachineLifecycleExpectedVersion> {
        self.expected_version.as_ref()
    }

    pub(crate) fn into_snapshot(self) -> MachineLifecycleSnapshot {
        self.snapshot
    }
}

/// Machine-authorized final-unregister persistence token.
///
/// The token bundles terminal lifecycle truth with the exact authorized input
/// snapshot. It has no public constructor and can only be minted by consuming
/// the private-field delete witness derived from the generated
/// `DeleteSnapshot` unregister verdict.
#[derive(Debug, Clone)]
pub struct UnregisterFinalizationCommit {
    machine_lifecycle: MachineLifecycleCommit,
    input_states: Vec<InputStatePersistenceRecord>,
    retired_ops_epoch: meerkat_core::RuntimeEpochId,
}

impl UnregisterFinalizationCommit {
    pub(crate) fn new(
        machine_lifecycle: MachineLifecycleCommit,
        input_states: Vec<InputStatePersistenceRecord>,
        retired_ops_epoch: meerkat_core::RuntimeEpochId,
        _authority: crate::meerkat_machine::DeleteOpsFinalizationAuthority,
    ) -> Self {
        Self {
            machine_lifecycle,
            input_states,
            retired_ops_epoch,
        }
    }

    pub(crate) fn into_parts(
        self,
    ) -> (
        MachineLifecycleSnapshot,
        Vec<InputStatePersistenceRecord>,
        meerkat_core::RuntimeEpochId,
    ) {
        (
            self.machine_lifecycle.into_snapshot(),
            self.input_states,
            self.retired_ops_epoch,
        )
    }

    /// Opaque encoded lifecycle record selected by final-unregister machine
    /// authority. External stores can persist this without gaining a way to
    /// construct or alter the authority token.
    pub fn lifecycle_store_record(&self) -> MachineLifecycleStoreRecord {
        self.machine_lifecycle.store_record()
    }

    /// Authorized input-state rows that must commit in the same transaction.
    pub fn input_states(&self) -> &[InputStatePersistenceRecord] {
        &self.input_states
    }

    /// Exact ops epoch retired by this finalization transaction.
    pub fn retired_ops_epoch(&self) -> &meerkat_core::RuntimeEpochId {
        &self.retired_ops_epoch
    }
}

/// One durable input row observed by [`RuntimeStore::load_input_states`].
#[derive(Debug, Clone)]
pub enum InputStateRow {
    /// The row decoded under this binary's persisted contract.
    Decoded(Box<StoredInputState>),
    /// The row's persisted bytes no longer decode. The row stays on disk
    /// untouched (forensics), and is reported typed so one damaged row does
    /// not make the runtime's other durable inputs unreadable.
    Corrupt {
        /// Row key as stored (the row's JSON no longer parses, so the typed
        /// `InputId` cannot be recovered from it).
        input_id: String,
        /// Decode failure detail.
        detail: String,
    },
}

/// Recovery projection of [`RuntimeStore::load_input_states`]: corrupt rows
/// are reported loudly and skipped so one damaged row cannot make the whole
/// runtime unrecoverable (the v0.8.7 failure mode). The damaged rows stay on
/// disk untouched for forensics.
pub async fn load_input_states_for_recovery(
    store: &dyn RuntimeStore,
    runtime_id: &LogicalRuntimeId,
) -> Result<Vec<StoredInputState>, RuntimeStoreError> {
    let mut states = Vec::new();
    for row in store.load_input_states(runtime_id).await? {
        match row {
            InputStateRow::Decoded(state) => states.push(*state),
            InputStateRow::Corrupt { input_id, detail } => {
                tracing::error!(
                    runtime_id = %runtime_id.0,
                    input_id = %input_id,
                    detail = %detail,
                    "durable input row no longer decodes; recovering the runtime's remaining inputs without it"
                );
            }
        }
    }
    Ok(states)
}

/// Atomic persistence interface for runtime state.
///
/// Implementations:
/// - `InMemoryRuntimeStore` — in-memory, no durability (ephemeral/testing)
/// - `SqliteRuntimeStore` — SQLite-backed durable runtime state
///
/// A store may contain many logical runtime ids, but each id is controlled by
/// one live `MeerkatMachine` authority. Store transactions provide durable
/// atomicity; they are not a distributed lease for two machines concurrently
/// controlling the same logical runtime.
///
/// Every operation that mutates more than one input row evaluates the unique
/// idempotency-key constraint against the batch's complete final image. Target
/// rows relinquish their old keys as one logical set before any target claims
/// its successor key, so a valid key swap is accepted regardless of mutation
/// order. A duplicate final claim or a claim held by a row outside the mutation
/// set rejects the entire operation without exposing any sibling effect.
///
/// This object-safe carrier is implemented only by real persistence backends.
/// Its methods have the same contracts as the corresponding forwarding
/// methods on [`RuntimeStore`]. Every method is required: profile-specific
/// capability refusals are explicit backend behavior, never inherited
/// defaults.
///
/// This is an implementor seam. Operational callers use [`RuntimeStore`] so a
/// decorator's intentional per-operation overrides remain observable.
#[doc(hidden)]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
pub trait RuntimeSessionAuthorityOps: Send + Sync {
    fn session_persistence_profile(&self) -> RuntimeSessionPersistenceProfile;

    fn session_boundary_authority_read_cost(&self) -> RuntimeSessionAuthorityReadCost;

    async fn commit_prepared_session_boundary(
        &self,
        runtime_id: &LogicalRuntimeId,
        request: PreparedRuntimeSessionCommit,
    ) -> Result<PreparedRuntimeSessionCommitResult, RuntimeStoreError>;

    async fn load_session_boundary_authority(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<RuntimeSessionAuthority>, RuntimeStoreError>;

    async fn load_whole_blob_store_authority(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<WholeBlobStoreAuthority>, RuntimeStoreError>;

    async fn load_committed_whole_blob_snapshot(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<CommittedWholeBlobSnapshot>, RuntimeStoreError>;

    async fn commit_prepared_whole_blob_snapshot_cas(
        &self,
        runtime_id: &LogicalRuntimeId,
        prepared: PreparedWholeBlobSnapshotCas,
    ) -> Result<WholeBlobSnapshotCasOutcome, RuntimeStoreError>;

    async fn delete_runtime_session_catalog_entry(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<(), RuntimeStoreError>;

    async fn load_runtime_session_catalog_entry(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<RuntimeSessionCatalogEntry>, RuntimeStoreError>;

    async fn list_runtime_session_catalog_entries(
        &self,
        filter: meerkat_core::SessionFilter,
    ) -> Result<Vec<RuntimeSessionCatalogEntry>, RuntimeStoreError>;

    async fn write_prepared_whole_blob_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        prepared: PreparedWholeBlobProvisionalTail,
    ) -> Result<WholeBlobProvisionalTailAuthority, RuntimeStoreError>;

    async fn load_whole_blob_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<CommittedWholeBlobProvisionalTail>, RuntimeStoreError>;

    async fn discard_whole_blob_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected: &WholeBlobProvisionalTailAuthority,
    ) -> Result<bool, RuntimeStoreError>;

    async fn write_prepared_head_canonical_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        prepared: PreparedHeadCanonicalProvisionalTail,
    ) -> Result<HeadCanonicalProvisionalTailAuthority, RuntimeStoreError>;

    async fn load_head_canonical_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<HeadCanonicalProvisionalTailAuthority>, RuntimeStoreError>;

    async fn discard_head_canonical_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected: &HeadCanonicalProvisionalTailAuthority,
    ) -> Result<bool, RuntimeStoreError>;

    async fn load_durable_tail_recovery_source(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<PreparedDurableTailRecoverySource>, RuntimeStoreError>;

    async fn load_durable_tail_recovery_receipts(
        &self,
        runtime_id: &LogicalRuntimeId,
        run_id: &RunId,
    ) -> Result<Vec<PreparedRecoveryReceiptSource>, RuntimeStoreError>;

    async fn load_committed_recovery_boundary(
        &self,
        runtime_id: &LogicalRuntimeId,
        candidate_id: &str,
    ) -> Result<Option<CommittedRecoveryBoundary>, RuntimeStoreError>;
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
pub trait RuntimeStore: Send + Sync {
    /// Required carrier for the complete store-owned session-authority seam.
    ///
    /// Decorators forward this one accessor. A fault-injection decorator may
    /// still override the individual forwarding method it intentionally
    /// perturbs. Omitting the carrier is therefore a compile error instead of
    /// a runtime `Unsupported` surprise.
    #[doc(hidden)]
    fn session_authority_ops(&self) -> &dyn RuntimeSessionAuthorityOps;

    /// Durable session representation owned by this store.
    ///
    /// Every backend carrier must choose explicitly. `WholeBlobV1`
    /// materializes and writes the accumulated session document at each
    /// boundary, so its ordinary persistence cost is O(document).
    /// `HeadCanonicalV1` commits the prepared head/suffix mutation and small
    /// runtime authority incrementally. Every profile must implement
    /// [`RuntimeSessionAuthorityOps::commit_prepared_session_boundary`]
    /// directly; there is no checkpoint-derived or whole-blob compatibility
    /// bridge.
    fn session_persistence_profile(&self) -> RuntimeSessionPersistenceProfile {
        self.session_authority_ops().session_persistence_profile()
    }

    /// Declared cost of [`Self::load_session_boundary_authority`].
    ///
    /// The carrier default is deliberately unsupported. Backends opt in only
    /// after maintaining authority separately from the document body.
    fn session_boundary_authority_read_cost(&self) -> RuntimeSessionAuthorityReadCost {
        self.session_authority_ops()
            .session_boundary_authority_read_cost()
    }

    /// Commit one valid-by-construction prepared session boundary.
    ///
    /// Every backend carrier must override this operation. Only the backend
    /// can allocate the next physical revision and atomically bind it to the
    /// exact body/head, catalog projection, receipts, input rows, and lifecycle
    /// effects. A generic implementation cannot honestly mint store-issued
    /// authority.
    async fn commit_prepared_session_boundary(
        &self,
        runtime_id: &LogicalRuntimeId,
        request: PreparedRuntimeSessionCommit,
    ) -> Result<PreparedRuntimeSessionCommitResult, RuntimeStoreError> {
        self.session_authority_ops()
            .commit_prepared_session_boundary(runtime_id, request)
            .await
    }

    /// Load the versioned session authority for a runtime.
    ///
    /// Implementations may expose this only as a bounded authority-row read.
    /// There is intentionally no default fallback through
    /// [`Self::load_session_snapshot`]: callers poll this seam during
    /// reconciliation, so parsing a WholeBlob body here would turn degraded
    /// operation into an invisible O(document) loop.
    async fn load_session_boundary_authority(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<RuntimeSessionAuthority>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_session_boundary_authority(runtime_id)
            .await
    }

    /// Observe only the fixed-size store-issued WholeBlob identity.
    async fn load_whole_blob_store_authority(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<WholeBlobStoreAuthority>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_whole_blob_store_authority(runtime_id)
            .await
    }

    /// Atomically pair the WholeBlob body with its store-issued identity.
    ///
    /// This is the source for resume/rewrite payload verification. Polling
    /// callers must use [`Self::load_whole_blob_store_authority`] instead.
    async fn load_committed_whole_blob_snapshot(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<CommittedWholeBlobSnapshot>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_committed_whole_blob_snapshot(runtime_id)
            .await
    }

    /// Commit one typed WholeBlob successor only while its exact store-issued
    /// predecessor remains current.
    ///
    /// Implementations compare only [`WholeBlobStoreAuthority`]. They must not
    /// derive currentness from Session checkpoint metadata or reread/compare a
    /// whole document.
    async fn commit_prepared_whole_blob_snapshot_cas(
        &self,
        runtime_id: &LogicalRuntimeId,
        prepared: PreparedWholeBlobSnapshotCas,
    ) -> Result<WholeBlobSnapshotCasOutcome, RuntimeStoreError> {
        self.session_authority_ops()
            .commit_prepared_whole_blob_snapshot_cas(runtime_id, prepared)
            .await
    }

    /// Delete one exact runtime's catalog projection.
    async fn delete_runtime_session_catalog_entry(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<(), RuntimeStoreError> {
        self.session_authority_ops()
            .delete_runtime_session_catalog_entry(runtime_id)
            .await
    }

    /// Load one bounded, body-free runtime session catalog entry.
    async fn load_runtime_session_catalog_entry(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<RuntimeSessionCatalogEntry>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_runtime_session_catalog_entry(runtime_id)
            .await
    }

    /// List body-free catalog entries in deterministic updated-descending,
    /// session-id-ascending order.
    async fn list_runtime_session_catalog_entries(
        &self,
        filter: meerkat_core::SessionFilter,
    ) -> Result<Vec<RuntimeSessionCatalogEntry>, RuntimeStoreError> {
        self.session_authority_ops()
            .list_runtime_session_catalog_entries(filter)
            .await
    }

    /// Write one typed provisional WholeBlob candidate exactly once.
    async fn write_prepared_whole_blob_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        prepared: PreparedWholeBlobProvisionalTail,
    ) -> Result<WholeBlobProvisionalTailAuthority, RuntimeStoreError> {
        self.session_authority_ops()
            .write_prepared_whole_blob_provisional_tail(runtime_id, prepared)
            .await
    }

    /// Atomically load one provisional authority and its candidate body.
    async fn load_whole_blob_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<CommittedWholeBlobProvisionalTail>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_whole_blob_provisional_tail(runtime_id)
            .await
    }

    /// Discard only the exact provisional candidate named by `expected`.
    async fn discard_whole_blob_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected: &WholeBlobProvisionalTailAuthority,
    ) -> Result<bool, RuntimeStoreError> {
        self.session_authority_ops()
            .discard_whole_blob_provisional_tail(runtime_id, expected)
            .await
    }

    /// Persist one exact HeadCanonical provisional intent before the physical
    /// SessionStore CAS it authorizes.
    async fn write_prepared_head_canonical_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        prepared: PreparedHeadCanonicalProvisionalTail,
    ) -> Result<HeadCanonicalProvisionalTailAuthority, RuntimeStoreError> {
        self.session_authority_ops()
            .write_prepared_head_canonical_provisional_tail(runtime_id, prepared)
            .await
    }

    /// Load only the fixed-size HeadCanonical provisional authority.
    async fn load_head_canonical_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<HeadCanonicalProvisionalTailAuthority>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_head_canonical_provisional_tail(runtime_id)
            .await
    }

    /// Discard only the exact HeadCanonical provisional authority supplied.
    async fn discard_head_canonical_provisional_tail(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected: &HeadCanonicalProvisionalTailAuthority,
    ) -> Result<bool, RuntimeStoreError> {
        self.session_authority_ops()
            .discard_head_canonical_provisional_tail(runtime_id, expected)
            .await
    }

    /// Load one store-owned durable-tail source from a single verified
    /// authority/physical-head snapshot.
    ///
    /// Only a backend that atomically owns runtime authority and canonical
    /// session rows can implement this. The default refuses instead of
    /// accepting caller-supplied session/head facts.
    async fn load_durable_tail_recovery_source(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<PreparedDurableTailRecoverySource>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_durable_tail_recovery_source(runtime_id)
            .await
    }

    /// Load every exact original receipt row for one store-derived recovery
    /// candidate run, ordered by boundary sequence.
    ///
    /// The row token in each opaque result lets a supported-floor missing
    /// conversation digest be enriched in the same transaction as recovery.
    async fn load_durable_tail_recovery_receipts(
        &self,
        runtime_id: &LogicalRuntimeId,
        run_id: &RunId,
    ) -> Result<Vec<PreparedRecoveryReceiptSource>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_durable_tail_recovery_receipts(runtime_id, run_id)
            .await
    }

    /// Load the durable exact-retry witness for one recovery candidate.
    ///
    /// Only a backend that owns runtime authority and the physical session
    /// head in one atomic resource may implement this. The generic WholeBlob
    /// profile has no way to recheck an external SessionStore row and therefore
    /// refuses rather than presenting a partial commit as converged recovery.
    async fn load_committed_recovery_boundary(
        &self,
        runtime_id: &LogicalRuntimeId,
        candidate_id: &str,
    ) -> Result<Option<CommittedRecoveryBoundary>, RuntimeStoreError> {
        self.session_authority_ops()
            .load_committed_recovery_boundary(runtime_id, candidate_id)
            .await
    }

    /// Whether [`RuntimeStore::atomic_apply`] durably records typed compaction
    /// projection intents in the same boundary as the session rewrite.
    /// Unknown/custom stores fail closed by default.
    fn supports_compaction_projection_outbox(&self) -> bool {
        false
    }

    /// Stable key for process-local auth/OAuth authority reuse across reopened
    /// handles for the same durable store.
    fn auth_authority_key(&self) -> Option<String> {
        None
    }

    /// Load the exact generated runtime-delivery authority record.
    async fn load_runtime_delivery_authority(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<RuntimeDeliveryAuthorityRecord>, RuntimeStoreError> {
        let _ = runtime_id;
        Err(RuntimeStoreError::Unsupported(
            "load_runtime_delivery_authority".into(),
        ))
    }

    /// Load one durable runtime-delivery inbox row by stable identity.
    async fn load_runtime_delivery_record(
        &self,
        runtime_id: &LogicalRuntimeId,
        delivery_id: &str,
    ) -> Result<Option<RuntimeDeliveryStoreRecord>, RuntimeStoreError> {
        let _ = (runtime_id, delivery_id);
        Err(RuntimeStoreError::Unsupported(
            "load_runtime_delivery_record".into(),
        ))
    }

    /// Compare-and-swap generated delivery authority and optionally insert one
    /// inbox row in the same atomic boundary.
    ///
    /// `expected_revision = None` means the authority row must be absent.
    /// Stores enforce only exact CAS, row uniqueness, and atomicity; the
    /// generated machine decides sequence allocation and application order.
    async fn compare_and_swap_runtime_delivery_authority(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected_revision: Option<u64>,
        replacement: RuntimeDeliveryAuthorityRecord,
        inserted_delivery: Option<RuntimeDeliveryStoreRecord>,
    ) -> Result<RuntimeDeliveryAuthorityCasOutcome, RuntimeStoreError> {
        let _ = (
            runtime_id,
            expected_revision,
            replacement,
            inserted_delivery,
        );
        Err(RuntimeStoreError::Unsupported(
            "compare_and_swap_runtime_delivery_authority".into(),
        ))
    }

    /// List durable inbox rows in generated sequence order.
    async fn list_runtime_delivery_records(
        &self,
        runtime_id: &LogicalRuntimeId,
        after_sequence: u64,
        limit: usize,
    ) -> Result<Vec<RuntimeDeliveryStoreRecord>, RuntimeStoreError> {
        let _ = (runtime_id, after_sequence, limit);
        Err(RuntimeStoreError::Unsupported(
            "list_runtime_delivery_records".into(),
        ))
    }

    /// Persist the runtime-owned OAuth login-flow payload snapshot.
    ///
    /// The AuthMachine owns admission/consume semantics; this payload snapshot
    /// carries the PKCE verifier and device-code correlation data needed to
    /// rehydrate active flows after a persistent runtime process restart.
    fn persist_auth_oauth_flow_snapshot(
        &self,
        snapshot_json: &[u8],
    ) -> Result<(), RuntimeStoreError> {
        let _ = snapshot_json;
        Err(RuntimeStoreError::Unsupported(
            "persist_auth_oauth_flow_snapshot".into(),
        ))
    }

    /// Load the runtime-owned OAuth login-flow payload snapshot, if present.
    fn load_auth_oauth_flow_snapshot(&self) -> Result<Option<Vec<u8>>, RuntimeStoreError> {
        Err(RuntimeStoreError::Unsupported(
            "load_auth_oauth_flow_snapshot".into(),
        ))
    }

    /// Atomically update the runtime-owned OAuth login-flow payload snapshot.
    ///
    /// Stores that support OAuth snapshots must override this with a lock,
    /// transaction, or compare-and-swap boundary. A load/compute/persist
    /// fallback is not safe for admission, capacity, or consume claims.
    fn update_auth_oauth_flow_snapshot(
        &self,
        _update: &mut AuthOAuthFlowSnapshotUpdate<'_>,
    ) -> Result<(), RuntimeStoreError> {
        Err(RuntimeStoreError::Unsupported(
            "update_auth_oauth_flow_snapshot".into(),
        ))
    }

    /// Atomically persist a session snapshot that is not a run boundary.
    ///
    /// Session-control snapshots update durable session authority without
    /// producing a [`RunBoundaryReceipt`].
    async fn commit_session_snapshot(
        &self,
        runtime_id: &LogicalRuntimeId,
        session_delta: SerializedSessionSnapshot,
    ) -> Result<(), RuntimeStoreError>;

    /// Commit one valid-by-construction WholeBlob transcript-rewrite boundary.
    ///
    /// Implementations compare the exact current authority with
    /// `boundary.expected_authority()` and write the already-materialized
    /// successor bytes once. If the exact successor authority is already
    /// current, return it without another physical write. Any other current
    /// authority conflicts. Stores must not decode the Session or reconstruct
    /// rewrite semantics; those proofs are sealed before this mechanical CAS.
    /// Exact successor compaction intents must match already-committed,
    /// non-finalized outbox rows inside the same lock or transaction.
    async fn commit_prepared_whole_blob_rewrite_boundary(
        &self,
        runtime_id: &LogicalRuntimeId,
        boundary: PreparedWholeBlobRewriteStoreParts,
    ) -> Result<WholeBlobStoreAuthority, RuntimeStoreError>;

    /// Atomically persist session delta + receipt + input state updates.
    ///
    /// All writes MUST commit in a single atomic operation.
    /// If `session_store_key` is `Some`, validates that the snapshot belongs
    /// to that session and, for stores that physically share a `SessionStore`
    /// table, writes that table in the same transaction. Runtime snapshot
    /// authority remains keyed only by `runtime_id`; `session_store_key` must
    /// not create a raw session UUID runtime alias.
    /// Compaction intents must be inserted as pending outbox rows in this same
    /// boundary. An intent whose exact outbox identity is already finalized is
    /// a stale snapshot replay and must be rejected without mutating any part
    /// of the boundary.
    async fn atomic_apply(
        &self,
        runtime_id: &LogicalRuntimeId,
        session_delta: Option<SerializedSessionSnapshot>,
        receipt: RunBoundaryReceipt,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: Option<meerkat_core::types::SessionId>,
    ) -> Result<(), RuntimeStoreError>;

    /// Load exact compaction projection intents committed by atomic_apply but
    /// not yet acknowledged as finalized by the memory store.
    async fn load_pending_compaction_projections(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Vec<meerkat_core::CompactionProjectionIntent>, RuntimeStoreError> {
        let _ = runtime_id;
        Err(RuntimeStoreError::Unsupported(
            "load_pending_compaction_projections".to_string(),
        ))
    }

    /// Idempotently acknowledge post-commit memory finalization.
    ///
    /// The acknowledgement and removal of this exact intent from the
    /// authoritative persisted session snapshot MUST occur in one atomic
    /// boundary. The finalized outbox row remains as a tombstone so later
    /// snapshot writes can reject stale metadata replay.
    async fn mark_compaction_projection_finalized(
        &self,
        runtime_id: &LogicalRuntimeId,
        projection: &meerkat_core::CompactionProjectionId,
    ) -> Result<(), RuntimeStoreError> {
        let _ = (runtime_id, projection);
        Err(RuntimeStoreError::Unsupported(
            "mark_compaction_projection_finalized".to_string(),
        ))
    }

    /// Atomically persist a failed-but-applied runtime turn.
    ///
    /// This is the machine-terminal counterpart to [`Self::atomic_apply`]:
    /// the mutated session snapshot, boundary receipt, generated machine
    /// lifecycle record, and input/outbox state must become visible in one
    /// transaction. Implementations must never compose this from separate
    /// `atomic_apply` and `commit_machine_lifecycle` calls.
    async fn atomic_apply_with_machine_lifecycle(
        &self,
        runtime_id: &LogicalRuntimeId,
        session_delta: SerializedSessionSnapshot,
        receipt: RunBoundaryReceipt,
        machine_lifecycle: MachineLifecycleCommit,
        input_updates: Vec<InputStatePersistenceRecord>,
        session_store_key: meerkat_core::types::SessionId,
    ) -> Result<(), RuntimeStoreError> {
        let _ = (
            runtime_id,
            session_delta,
            receipt,
            machine_lifecycle,
            input_updates,
            session_store_key,
        );
        Err(RuntimeStoreError::Unsupported(
            "atomic_apply_with_machine_lifecycle".to_string(),
        ))
    }

    /// Load all input states for a runtime, one row outcome per stored row.
    ///
    /// A row whose persisted bytes no longer decode under this binary's
    /// contract is surfaced as [`InputStateRow::Corrupt`] instead of failing
    /// the whole load: one damaged row must not make every other durable
    /// input unreadable. The store never drops or rewrites the damaged row;
    /// the caller owns the per-row skip/fail policy
    /// ([`RuntimeStore::load_input_states_strict`] is the fail-on-any
    /// projection).
    async fn load_input_states(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Vec<InputStateRow>, RuntimeStoreError>;

    /// Strict projection of [`RuntimeStore::load_input_states`]: every row
    /// must decode; the first corrupt row fails the whole load with its row
    /// identity in the typed error.
    async fn load_input_states_strict(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Vec<StoredInputState>, RuntimeStoreError> {
        let mut states = Vec::new();
        for row in self.load_input_states(runtime_id).await? {
            match row {
                InputStateRow::Decoded(state) => states.push(*state),
                InputStateRow::Corrupt { input_id, detail } => {
                    return Err(RuntimeStoreError::ReadFailed(format!(
                        "input state row `{input_id}` failed to decode: {detail}"
                    )));
                }
            }
        }
        Ok(states)
    }

    /// Load a specific boundary receipt.
    async fn load_boundary_receipt(
        &self,
        runtime_id: &LogicalRuntimeId,
        run_id: &RunId,
        sequence: u64,
    ) -> Result<Option<RunBoundaryReceipt>, RuntimeStoreError>;

    /// Load every durably committed boundary receipt for one run, in
    /// ascending sequence order.
    ///
    /// Recovery reads these to (a) derive the next boundary sequence for a
    /// recovered commit (an interrupted tool loop can already have committed
    /// `BoundaryContinue` receipts before losing only its final boundary)
    /// and (b) recover the exact contributing input identities the run
    /// already bound durably. The default probes ascending sequences through
    /// [`RuntimeStore::load_boundary_receipt`]; backends with range reads
    /// should override it.
    async fn load_committed_boundary_receipts(
        &self,
        runtime_id: &LogicalRuntimeId,
        run_id: &RunId,
    ) -> Result<Vec<RunBoundaryReceipt>, RuntimeStoreError> {
        // Receipt sequences are minted densely from 1 by the generated
        // machine; a gap therefore terminates the probe. The cap is a
        // corruption backstop, far above any real per-run boundary count.
        const PROBE_CAP: u64 = 100_000;
        let mut receipts = Vec::new();
        for sequence in 1..=PROBE_CAP {
            match self
                .load_boundary_receipt(runtime_id, run_id, sequence)
                .await?
            {
                Some(receipt) => receipts.push(receipt),
                None => return Ok(receipts),
            }
        }
        Err(RuntimeStoreError::ReadFailed(format!(
            "run {run_id} has more than {PROBE_CAP} boundary receipts; refusing to probe further"
        )))
    }

    /// Load one authoritative snapshot of all and only nonterminal input-state
    /// rows, with the exact domain-prefixed SHA-256 digest of each row's
    /// stored bytes and a set/absence token over the complete ordered set.
    ///
    /// Each row digest is a target-local compare token: recovery carries it
    /// back on fenced [`InputStatePersistenceRecord`]s. The snapshot's set
    /// token additionally fences inserts, removals, and terminality changes,
    /// including the empty-set case where there are no per-row tokens to CAS.
    ///
    /// Implementations that apply recovery MUST recompute the snapshot from
    /// the same complete, runtime-scoped nonterminal index/set inside the
    /// transaction that writes the boundary. A different set token fails the
    /// whole boundary with [`RuntimeStoreError::RecoveryInputSetConflict`];
    /// implementations MUST also enforce every
    /// [`InputStatePersistenceRecord::expected_row_digest`] in that
    /// transaction, failing with
    /// [`RuntimeStoreError::InputRowVersionConflict`] on mismatch.
    ///
    /// There is deliberately no compatibility derivation from decoded rows:
    /// reserializing a bundle proves only the current serializer's canonical
    /// representation, not the exact bytes the backend observed and will CAS.
    /// A backend must override this method only when it can return and enforce
    /// tokens for its actual stored-row representation. Wrappers and custom
    /// stores that cannot do so fail closed, and durable-tail recovery maps
    /// this typed absence of fencing capability to `Unfenceable`.
    async fn load_input_states_with_versions(
        &self,
        _runtime_id: &LogicalRuntimeId,
    ) -> Result<PreparedRecoveryInputSnapshot, RuntimeStoreError> {
        Err(RuntimeStoreError::Unsupported(
            "load_input_states_with_versions requires exact stored-row and complete-set tokens"
                .to_string(),
        ))
    }

    /// Load the latest committed whole-blob session snapshot for a runtime.
    ///
    /// Compatibility-only. A head-canonical implementation must return
    /// [`RuntimeStoreError::SessionPersistenceAuthorityConflict`] once a
    /// canonical authority row exists; returning a frozen migration BLOB would
    /// resurrect its predecessor as current truth.
    async fn load_session_snapshot(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<std::sync::Arc<Vec<u8>>>, RuntimeStoreError>;

    /// Remove the latest committed session snapshot for a runtime.
    ///
    /// This is used only as a fail-closed quarantine path when transcript
    /// rewrite audit failure makes the runtime snapshot itself invalid recovery
    /// authority and the service cannot restore the previous snapshot. An
    /// ordinary downstream compatibility-projection failure must retain the
    /// already-committed runtime snapshot for retry.
    /// Head-canonical stores must refuse this whole-document mutation.
    async fn clear_session_snapshot(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<(), RuntimeStoreError>;

    /// Replace the latest committed session snapshot only if it still matches
    /// `expected_current`.
    ///
    /// Used by fail-closed recovery when a rejected transcript-rewrite snapshot
    /// must be restored to its prior audited value. Implementations must compare
    /// and write atomically so recovery cannot overwrite newer runtime authority.
    /// Head-canonical stores must refuse this whole-document mutation.
    async fn replace_session_snapshot_if_current(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected_current: &[u8],
        replacement: Vec<u8>,
    ) -> Result<bool, RuntimeStoreError>;

    /// Remove the latest committed session snapshot only if it still matches
    /// `expected_current`.
    ///
    /// This is the conditional variant of the fail-closed quarantine path.
    /// Head-canonical stores must refuse this whole-document mutation.
    async fn clear_session_snapshot_if_current(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected_current: &[u8],
    ) -> Result<bool, RuntimeStoreError>;

    /// Report whether the runtime-projection fallback for `runtime_id` is
    /// quarantined.
    ///
    /// This is a durable single-owner fact: when
    /// [`clear_session_snapshot_if_current`](Self::clear_session_snapshot_if_current)
    /// matches and DELETEs a rejected runtime snapshot, the same atomic boundary
    /// records the quarantine marker. A subsequent live snapshot write clears it.
    /// Recovery reads this to decide whether a store-only projection may stand in
    /// for an absent runtime snapshot. The default is fail-safe (`false`): stores
    /// that cannot record the marker durably never claim a snapshot is
    /// quarantined.
    async fn is_runtime_projection_quarantined(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<bool, RuntimeStoreError> {
        let _ = runtime_id;
        Ok(false)
    }

    /// Persist a single input state (for durable-before-ack).
    async fn persist_input_state(
        &self,
        runtime_id: &LogicalRuntimeId,
        state: &InputStatePersistenceRecord,
    ) -> Result<(), RuntimeStoreError>;

    /// Atomically persist a batch of machine-authorized input shell updates.
    /// Used by per-input terminal outboxes so an N-input batch can never
    /// expose a mixed provisional/finalized or finalized/published phase.
    /// Idempotency-key ownership follows the trait's complete-final-image
    /// contract, including valid swaps between rows in this batch.
    async fn persist_input_states_atomically(
        &self,
        _runtime_id: &LogicalRuntimeId,
        states: &[InputStatePersistenceRecord],
    ) -> Result<(), RuntimeStoreError> {
        if states.is_empty() {
            return Ok(());
        }
        Err(RuntimeStoreError::Unsupported(
            "persist_input_states_atomically".to_string(),
        ))
    }

    /// Durable realization profile for
    /// [`Self::compare_and_swap_input_states_atomically`].
    fn input_state_batch_cas_implementation_profile(
        &self,
    ) -> InputStateBatchCasImplementationProfile {
        InputStateBatchCasImplementationProfile::Unsupported
    }

    /// Atomically replace an exact set of input-state rows only when every
    /// currently persisted row is byte-identical to its expected
    /// [`StoredInputState`] serialization.
    ///
    /// Expected and replacement batches must contain the same unique keys and
    /// at most [`MAX_INPUT_STATE_BATCH_CAS`] rows. If every current row already
    /// equals its replacement, implementations return
    /// [`InputStateBatchCasOutcome::Swapped`] without rewriting it; this makes
    /// a committed store-first transaction retryable after caller
    /// cancellation or acknowledgement loss. Missing rows, mixed
    /// expected/replacement images, and any other changed durable rows return
    /// [`InputStateBatchCasOutcome::Stale`] without writing a replacement.
    /// Implementations must hold one lock/transaction across the complete
    /// comparison and write set. Replacement idempotency-key ownership follows
    /// the trait's complete-final-image contract, including valid swaps between
    /// rows in this batch.
    async fn compare_and_swap_input_states_atomically(
        &self,
        _runtime_id: &LogicalRuntimeId,
        expected: &[StoredInputState],
        replacements: &[InputStatePersistenceRecord],
    ) -> Result<InputStateBatchCasOutcome, RuntimeStoreError> {
        let prepared = prepare_input_state_batch_cas(expected, replacements)?;
        if prepared.is_empty() {
            return Ok(InputStateBatchCasOutcome::Swapped);
        }
        Err(RuntimeStoreError::Unsupported(
            "compare_and_swap_input_states_atomically".to_string(),
        ))
    }

    /// Atomically replace an exact input-state batch while an external
    /// authority fence is held across the target write.
    ///
    /// Implementations must compare the target rows first, then retain both
    /// the target transaction and the external authority guard until every
    /// replacement is committed. This is the cold-registration recovery seam:
    /// a process whose lease expires or is superseded must never overwrite
    /// input work recovered by its successor.
    async fn compare_and_swap_input_states_atomically_with_fence(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected: &[StoredInputState],
        replacements: &[InputStatePersistenceRecord],
        write_fence: std::sync::Arc<dyn RuntimeStoreWriteFence>,
    ) -> Result<FencedInputStateBatchCasOutcome, RuntimeStoreError> {
        let prepared = prepare_input_state_batch_cas(expected, replacements)?;
        if prepared.is_empty() {
            return Ok(FencedInputStateBatchCasOutcome::Swapped);
        }
        let _ = (runtime_id, write_fence);
        Err(RuntimeStoreError::Unsupported(
            "compare_and_swap_input_states_atomically_with_fence".to_string(),
        ))
    }

    /// Atomically publish machine-normalized recovery input rows only while
    /// the exact store-owned input-set revision observed with the source rows
    /// remains current.
    ///
    /// Unlike ordinary bounded input CAS, this seam has no total-row cap and
    /// MUST compare `expected_revision` even when `replacements` is empty. The
    /// empty case is the absence-fence path: a concurrent first insert must
    /// make it stale. Each replacement additionally carries the exact
    /// predecessor-row digest returned in
    /// [`Self::load_input_states_with_versions`].
    async fn compare_and_swap_recovery_input_states_atomically(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected_revision: RecoveryInputSetRevision,
        mutations: &[RecoveryInputStateMutation],
    ) -> Result<InputStateBatchCasOutcome, RuntimeStoreError> {
        let _ = prepare_recovery_input_state_mutations(mutations)?;
        let _ = (runtime_id, expected_revision);
        Err(RuntimeStoreError::Unsupported(
            "compare_and_swap_recovery_input_states_atomically".to_string(),
        ))
    }

    /// Revision-fenced recovery input publication while an external runtime
    /// authority fence is held across the target transaction.
    ///
    /// Implementations MUST execute the external fence even for an empty
    /// replacement set, after comparing the store-owned revision and before
    /// committing. This prevents a zero-row bootstrap from bypassing either
    /// the absence witness or its runtime-authority lease.
    async fn compare_and_swap_recovery_input_states_atomically_with_fence(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected_revision: RecoveryInputSetRevision,
        mutations: &[RecoveryInputStateMutation],
        write_fence: std::sync::Arc<dyn RuntimeStoreWriteFence>,
    ) -> Result<FencedInputStateBatchCasOutcome, RuntimeStoreError> {
        let _ = prepare_recovery_input_state_mutations(mutations)?;
        let _ = (runtime_id, expected_revision, write_fence);
        Err(RuntimeStoreError::Unsupported(
            "compare_and_swap_recovery_input_states_atomically_with_fence".to_string(),
        ))
    }

    /// Load a single input state.
    async fn load_input_state(
        &self,
        runtime_id: &LogicalRuntimeId,
        input_id: &InputId,
    ) -> Result<Option<StoredInputState>, RuntimeStoreError>;

    /// Resolve one historical or live input through the store-owned
    /// idempotency-key index.
    ///
    /// Implementations MUST maintain a unique `(runtime_id, key) -> input_id`
    /// mapping atomically with every input-row insert, update, and delete.
    /// Presence and absence are authoritative only when the store proves, in
    /// the same backend snapshot as the keyed lookup, that every source row for
    /// the runtime has an unambiguous indexable key identity. A corrupt or
    /// otherwise unindexable row must return
    /// [`RuntimeStoreError::InputIdempotencyIndexUncertain`] for both hits and
    /// misses; it must never be treated as absence or ignored behind another
    /// indexed owner. A full input-history scan is not a conforming
    /// implementation. The returned digest binds the exact stored row bytes
    /// observed with the index lookup.
    async fn load_input_state_by_idempotency_key(
        &self,
        _runtime_id: &LogicalRuntimeId,
        _key: &IdempotencyKey,
    ) -> Result<Option<ExactInputStateObservation>, RuntimeStoreError> {
        Err(RuntimeStoreError::Unsupported(
            "load_input_state_by_idempotency_key requires an exact maintained index".to_string(),
        ))
    }

    /// Load an exact bounded set of input rows from one backend snapshot.
    ///
    /// Results have exactly the request's cardinality and order; a missing key
    /// occupies its corresponding `None` slot. Duplicate keys and batches
    /// larger than [`MAX_INPUT_STATE_BATCH_CAS`] are rejected. Implementations
    /// must perform one bounded backend read rather than repeatedly
    /// materializing a whole-blob ledger.
    async fn load_input_states_by_ids(
        &self,
        _runtime_id: &LogicalRuntimeId,
        input_ids: &[InputId],
    ) -> Result<Vec<Option<StoredInputState>>, RuntimeStoreError> {
        validate_input_state_batch_read_ids(input_ids)?;
        if input_ids.is_empty() {
            return Ok(Vec::new());
        }
        Err(RuntimeStoreError::Unsupported(
            "load_input_states_by_ids".to_string(),
        ))
    }

    /// Discover canonical owner ids for unfinished terminal work.
    ///
    /// Results are strictly ordered by [`InputId`], contain only ids greater
    /// than the stable exclusive `after` cursor, and contain at most `limit`
    /// entries. Implementations must maintain a store-owned index
    /// transactionally with input-state writes; scanning or decoding the
    /// accumulated input ledger inside this method violates the contract.
    ///
    /// The result is discovery only. Callers must hydrate and validate each
    /// owner's exact declared recipient batch through
    /// [`Self::load_input_states_by_ids`].
    async fn load_pending_terminal_owner_ids_page(
        &self,
        _runtime_id: &LogicalRuntimeId,
        after: Option<&InputId>,
        limit: usize,
    ) -> Result<Vec<InputId>, RuntimeStoreError> {
        validate_pending_terminal_owner_page(after, limit, &[])?;
        Err(RuntimeStoreError::Unsupported(
            "load_pending_terminal_owner_ids_page".to_string(),
        ))
    }

    /// Observe one physical machine-lifecycle row without collapsing corrupt
    /// or future-version bytes into absence.
    ///
    /// This is the recovery/reconciliation read surface. Custom stores must
    /// implement it explicitly; the default is capability-unavailable rather
    /// than inferring a total observation from the older strict decoder.
    async fn observe_machine_lifecycle(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<MachineLifecycleObservation, RuntimeStoreError> {
        let _ = runtime_id;
        Err(RuntimeStoreError::Unsupported(
            "observe_machine_lifecycle".to_string(),
        ))
    }

    /// Replace exactly one machine-lifecycle row when it is absent or still
    /// has the observed raw-content version.
    ///
    /// Built-in stores atomically compare the raw-content version and publish
    /// the machine-authorized replacement. Binding, generation, fence, and
    /// run atoms belong to the dead process that wrote the observed row; they
    /// are not durable high-waters and may be cleared by an exact-version
    /// cold-normalization CAS. The caller retains the prior raw digest for
    /// output-only diagnostics. Conflicts are ordinary level-triggered
    /// re-observation; unsupported or malformed bytes return
    /// [`RuntimeStoreError::MachineLifecycleRepairBlocked`].
    ///
    /// When a runtime session catalog entry already exists, an applied CAS
    /// must advance its runtime-state projection in the same atomic operation.
    async fn compare_and_swap_machine_lifecycle(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected: MachineLifecycleExpectedVersion,
        replacement: MachineLifecycleCommit,
    ) -> Result<MachineLifecycleCasOutcome, RuntimeStoreError> {
        let _ = (runtime_id, expected, replacement);
        Err(RuntimeStoreError::Unsupported(
            "compare_and_swap_machine_lifecycle".to_string(),
        ))
    }

    /// Replace exactly one lifecycle row while an external authority fence is
    /// held across the target write.
    ///
    /// This is the conditional-registration store seam. Built-in stores call
    /// `write_fence` inside the row lock/transaction after the exact raw-row
    /// comparison and immediately before publication. Custom stores must opt
    /// in explicitly; the default is capability-unavailable rather than an
    /// unfenced fallback to compare_and_swap_machine_lifecycle. An applied
    /// fence must also advance an existing runtime session catalog entry to
    /// the replacement state in that same operation. This includes an
    /// already-exact lifecycle row: the applied fence heals a stale catalog
    /// projection before returning [`FencedMachineLifecycleCasOutcome::AlreadyExact`].
    async fn compare_and_swap_machine_lifecycle_with_fence(
        &self,
        runtime_id: &LogicalRuntimeId,
        expected: MachineLifecycleExpectedVersion,
        replacement: MachineLifecycleCommit,
        write_fence: std::sync::Arc<dyn RuntimeStoreWriteFence>,
    ) -> Result<FencedMachineLifecycleCasOutcome, RuntimeStoreError> {
        let _ = (runtime_id, expected, replacement, write_fence);
        Err(RuntimeStoreError::Unsupported(
            "compare_and_swap_machine_lifecycle_with_fence".to_string(),
        ))
    }

    /// Load the last persisted machine lifecycle record bytes, if any.
    ///
    /// Implementations return only the opaque bytes previously obtained from
    /// [`MachineLifecycleCommit::store_record`]. The runtime crate decodes
    /// these bytes through `load_runtime_state` or internal recovery helpers;
    /// stores must not promote compatibility rows or bare runtime states into
    /// lifecycle authority.
    async fn load_machine_lifecycle_record(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<Vec<u8>>, RuntimeStoreError>;

    /// Atomically commit machine-owned lifecycle state changes.
    ///
    /// Writes runtime state, generated runtime binding facts, and all input
    /// state updates in a single atomic operation. `MachineLifecycleCommit` has
    /// no public constructor, so this cannot be used by compatibility callers
    /// to pick runtime truth. If a runtime session catalog entry exists, its
    /// runtime-state projection must advance to [`MachineLifecycleCommit::runtime_state`]
    /// in the same operation.
    async fn commit_machine_lifecycle(
        &self,
        runtime_id: &LogicalRuntimeId,
        commit: MachineLifecycleCommit,
        input_states: &[InputStatePersistenceRecord],
    ) -> Result<(), RuntimeStoreError>;

    /// Atomically publish final unregister lifecycle truth and retire the
    /// matching ops-lifecycle epoch.
    ///
    /// The lifecycle record, input-state updates, and ops snapshot deletion
    /// MUST commit in one store transaction (or one indivisible in-memory
    /// critical section). A terminal lifecycle record with the old ops epoch
    /// still present is forbidden: recovery would otherwise resurrect stale
    /// operation/cursor authority after unregister. The commit also carries
    /// the exact retired ops epoch; implementations MUST atomically retain a
    /// durable deletion-wins fence for it, and every later
    /// `persist_ops_lifecycle` for that epoch must return
    /// [`RuntimeStoreError::OpsLifecycleEpochRetired`] rather than recreate the
    /// row. Implementations must also be idempotent so retry after a process
    /// crash following commit converges on the same terminal lifecycle with no
    /// ops snapshot and the same epoch fence.
    ///
    /// `Ok(())` means the whole finalization is visible. Every error except
    /// [`RuntimeStoreError::UnregisterFinalizationOutcomeUnknown`] MUST mean
    /// none of it is visible. A backend with an ambiguous commit
    /// acknowledgement must first resolve that ambiguity internally by
    /// reading its transaction authority. It may use the typed unknown error
    /// only when it cannot prove either the exact final state or the exact
    /// pre-transaction state; callers then retry without a durable rollback.
    /// The opaque token also proves the generated `DeleteSnapshot` verdict and
    /// bundles the exact lifecycle and input rows selected by the machine.
    ///
    /// The returned future is also a cancellation boundary: after it is
    /// dropped, no mutation from that invocation may become visible later.
    /// An implementation may leave the prior pair untouched or finish the
    /// entire atomic commit before cancellation is observable, but it must not
    /// detach a background write that can cross a same-runtime-ID replacement.
    /// If a runtime session catalog entry exists, its runtime-state projection
    /// is part of this same atomic finalization and is selected from
    /// [`UnregisterFinalizationCommit::lifecycle_store_record`].
    async fn commit_unregister_finalization(
        &self,
        runtime_id: &LogicalRuntimeId,
        finalization: UnregisterFinalizationCommit,
    ) -> Result<(), RuntimeStoreError> {
        let _ = (runtime_id, finalization);
        Err(RuntimeStoreError::Unsupported(
            "commit_unregister_finalization".into(),
        ))
    }

    /// Atomically initialize the ops lifecycle row if it is absent and return
    /// the canonical durable snapshot.
    ///
    /// The absence check, optional insert, and canonical read MUST share one
    /// store transaction (or one indivisible in-memory critical section).
    /// Concurrent initializer calls for the same runtime must therefore all
    /// observe the same epoch: exactly one candidate may become durable and
    /// every losing caller receives that winner's snapshot. The machine's
    /// stable registration transaction separately spans this store call
    /// through map publication/removal; this method is not a distributed
    /// machine lease. Implementations must also reject a candidate whose epoch
    /// is already covered by the unregister deletion-wins fence.
    ///
    /// Cancellation may leave the candidate as the canonical empty row: no
    /// bindings escape before this await completes, and the next registrar
    /// adopts the returned durable epoch. A cancelled invocation must never
    /// overwrite a row that was already present.
    ///
    /// There is intentionally no load-then-persist default. Custom stores
    /// that support durable ops lifecycle state must implement this atomic
    /// boundary or fail closed with [`RuntimeStoreError::Unsupported`].
    async fn initialize_ops_lifecycle_if_absent(
        &self,
        runtime_id: &LogicalRuntimeId,
        candidate: &crate::ops_lifecycle::PersistedOpsSnapshot,
    ) -> Result<crate::ops_lifecycle::PersistedOpsSnapshot, RuntimeStoreError> {
        let _ = (runtime_id, candidate);
        Err(RuntimeStoreError::Unsupported(
            "initialize_ops_lifecycle_if_absent".into(),
        ))
    }

    /// Persist a snapshot of the ops lifecycle registry state.
    async fn persist_ops_lifecycle(
        &self,
        runtime_id: &LogicalRuntimeId,
        snapshot: &crate::ops_lifecycle::PersistedOpsSnapshot,
    ) -> Result<(), RuntimeStoreError> {
        let _ = (runtime_id, snapshot);
        Err(RuntimeStoreError::Unsupported(
            "persist_ops_lifecycle".into(),
        ))
    }

    /// Load a previously persisted ops lifecycle snapshot.
    async fn load_ops_lifecycle(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<Option<crate::ops_lifecycle::PersistedOpsSnapshot>, RuntimeStoreError> {
        let _ = runtime_id;
        Err(RuntimeStoreError::Unsupported("load_ops_lifecycle".into()))
    }

    /// Delete a previously persisted ops lifecycle snapshot.
    async fn delete_ops_lifecycle(
        &self,
        runtime_id: &LogicalRuntimeId,
    ) -> Result<(), RuntimeStoreError> {
        let _ = runtime_id;
        Err(RuntimeStoreError::Unsupported(
            "delete_ops_lifecycle".into(),
        ))
    }

    // -----------------------------------------------------------------------
    // Mob host binding rows (`runtime_mob_host_bindings`, multi-host mobs R8)
    // -----------------------------------------------------------------------
    //
    // Raw record-JSON accessors only: the TYPED record and the
    // transition-derived persistence authorities live mob-side
    // (`meerkat-mob/src/runtime/host_actor.rs`); this store never interprets
    // the blob. CAS compares the full serialized record, mirroring the
    // `mob_runtime_supervisors` mechanics.

    /// Load the persisted host-binding record blob for `mob_id`, if any.
    async fn load_mob_host_binding(
        &self,
        mob_id: &str,
    ) -> Result<Option<Vec<u8>>, RuntimeStoreError> {
        let _ = mob_id;
        Err(RuntimeStoreError::Unsupported(
            "load_mob_host_binding".into(),
        ))
    }

    /// List every persisted host-binding row (boot recovery).
    async fn list_mob_host_bindings(&self) -> Result<Vec<(String, Vec<u8>)>, RuntimeStoreError> {
        Err(RuntimeStoreError::Unsupported(
            "list_mob_host_bindings".into(),
        ))
    }

    /// Insert the host-binding row for `mob_id` iff absent. Returns whether
    /// the row was inserted.
    async fn put_mob_host_binding_if_absent(
        &self,
        mob_id: &str,
        record_json: &[u8],
    ) -> Result<bool, RuntimeStoreError> {
        let _ = (mob_id, record_json);
        Err(RuntimeStoreError::Unsupported(
            "put_mob_host_binding_if_absent".into(),
        ))
    }

    /// Replace the host-binding row for `mob_id` iff the stored blob equals
    /// `expected_json`. Returns whether the swap applied.
    async fn compare_and_put_mob_host_binding(
        &self,
        mob_id: &str,
        expected_json: &[u8],
        next_json: &[u8],
    ) -> Result<bool, RuntimeStoreError> {
        let _ = (mob_id, expected_json, next_json);
        Err(RuntimeStoreError::Unsupported(
            "compare_and_put_mob_host_binding".into(),
        ))
    }

    /// Delete the host-binding row for `mob_id` iff the stored blob equals
    /// `expected_json`. Returns whether a row was deleted.
    async fn delete_mob_host_binding(
        &self,
        mob_id: &str,
        expected_json: &[u8],
    ) -> Result<bool, RuntimeStoreError> {
        let _ = (mob_id, expected_json);
        Err(RuntimeStoreError::Unsupported(
            "delete_mob_host_binding".into(),
        ))
    }

    /// Load the durable receipt for an already-completed host revocation.
    ///
    /// The blob is deliberately separate from `runtime_mob_host_bindings`:
    /// boot recovery must never mistake a revoke retry receipt for a live
    /// binding or revive the materialized-member rows that the revoke
    /// removed. The typed receipt and its transition witness live mob-side;
    /// this store treats it as opaque bytes.
    async fn load_mob_host_revocation(
        &self,
        mob_id: &str,
    ) -> Result<Option<Vec<u8>>, RuntimeStoreError> {
        let _ = mob_id;
        Err(RuntimeStoreError::Unsupported(
            "load_mob_host_revocation".into(),
        ))
    }

    /// List durable host-revocation receipts for boot recovery of exact
    /// reply-loss retries. Receipts are not bindings and carry no member
    /// revival rows.
    async fn list_mob_host_revocations(&self) -> Result<Vec<(String, Vec<u8>)>, RuntimeStoreError> {
        Err(RuntimeStoreError::Unsupported(
            "list_mob_host_revocations".into(),
        ))
    }

    /// Atomically delete the expected active binding and publish its revoke
    /// receipt. Returns `false` when the expected binding did not match; in
    /// that case neither write is visible.
    ///
    /// This is the durable terminal boundary for host revocation. A crash
    /// before it leaves the binding retryable; a crash after it leaves no
    /// binding/member rows to revive and an exact receipt to replay.
    async fn revoke_mob_host_binding(
        &self,
        mob_id: &str,
        expected_binding_json: &[u8],
        receipt_json: &[u8],
    ) -> Result<bool, RuntimeStoreError> {
        let _ = (mob_id, expected_binding_json, receipt_json);
        Err(RuntimeStoreError::Unsupported(
            "revoke_mob_host_binding".into(),
        ))
    }
}

pub use memory::InMemoryRuntimeStore;
#[cfg(feature = "sqlite-store")]
pub use sqlite::SqliteRuntimeStore;

#[cfg(test)]
mod store_authority_record_tests {
    use super::*;
    use meerkat_core::session_store::PreparedHeadCanonicalMutation;
    use meerkat_core::types::{Message, UserMessage};

    fn row_digest(byte: char) -> String {
        format!("row-sha256:{}", byte.to_string().repeat(64))
    }

    fn head_record() -> (
        meerkat_core::types::SessionId,
        meerkat_core::session_store::SessionHead,
        String,
    ) {
        let mut session = meerkat_core::Session::new();
        session.push(Message::User(UserMessage::text("canonical head")));
        let session_id = session.id().clone();
        let mutation =
            PreparedHeadCanonicalMutation::prepare(&session, None).expect("prepare canonical head");
        (
            session_id,
            mutation.successor_head().clone(),
            mutation.successor_head_token().to_string(),
        )
    }

    #[test]
    fn borrowed_whole_blob_provisional_prepare_encodes_once_and_retains_no_session() {
        let mut session = meerkat_core::Session::new();
        session.push(Message::User(UserMessage::text("candidate")));
        let session_id = session.id().clone();
        let base = WholeBlobStoreAuthority::issued(session_id.clone(), 7, row_digest('b'))
            .expect("valid base authority");
        let prepared =
            PreparedWholeBlobProvisionalTail::prepare_from_session(base, RunId::new(), 1, &session)
                .expect("prepare borrowed WholeBlob candidate");
        assert_eq!(
            prepared.whole_blob_encode_count(),
            1,
            "borrowed preparation must stream the Session exactly once"
        );
        let retained = prepared.clone();
        drop(prepared);
        drop(session);
        assert_eq!(
            retained.whole_blob_encode_count(),
            1,
            "cloning the bounded carrier must share bytes, not re-encode"
        );

        let (authority, artifact, digest, message_count, catalog, intents) = retained.into_parts();
        assert_eq!(authority.session_id(), &session_id);
        assert_eq!(
            authority.candidate_blob_sha256(),
            artifact.row_sha256_token()
        );
        assert_eq!(catalog.session_id(), &session_id);
        assert_eq!(catalog.message_count(), 1);
        assert_eq!(message_count, 1);
        assert!(!digest.is_empty());
        assert!(intents.is_empty());
        let decoded = meerkat_core::Session::from_persisted_bytes(artifact.bytes())
            .expect("carrier bytes remain independently usable after the Session is dropped");
        assert_eq!(decoded.id(), &session_id);
        assert_eq!(decoded.messages().len(), 1);
    }

    #[test]
    fn committed_whole_blob_decode_installs_store_owned_rewrite_lineage() {
        let mut session = meerkat_core::Session::new();
        session.push(Message::User(UserMessage::text("original")));
        let artifact = session
            .to_persisted_artifact()
            .expect("serialize WholeBlob document");
        let authority = WholeBlobStoreAuthority::issued(
            session.id().clone(),
            1,
            artifact.row_sha256_token().to_string(),
        )
        .expect("issue exact WholeBlob authority");
        let committed = CommittedWholeBlobSnapshot::new(artifact.bytes_arc(), authority)
            .expect("decode store-owned WholeBlob document");

        let mut decoded = committed.session().clone();
        let parent_revision = decoded.transcript_revision().expect("read parent revision");
        decoded
            .commit_transcript_rewrite(
                meerkat_core::TranscriptRewriteSelection::MessageRange { start: 0, end: 1 },
                vec![Message::User(UserMessage::text("edited"))],
                meerkat_core::TranscriptRewriteReason::new("test"),
                Some("runtime-store-test".to_string()),
                Some(parent_revision),
            )
            .expect("store-owned WholeBlob decode must carry exact rewrite lineage");
    }

    #[test]
    fn whole_blob_store_record_constructor_validates_every_fixed_field() {
        let session_id = meerkat_core::types::SessionId::new();
        let valid = WholeBlobStoreAuthority::from_store_record(
            WholeBlobStoreAuthority::VERSION,
            session_id.clone(),
            7,
            row_digest('a'),
        )
        .expect("valid WholeBlob record");
        assert_eq!(valid.authority_version(), WholeBlobStoreAuthority::VERSION);
        assert_eq!(valid.session_id(), &session_id);
        assert_eq!(valid.store_revision(), 7);
        assert_eq!(valid.blob_sha256(), row_digest('a'));

        for (version, revision, digest) in [
            (WholeBlobStoreAuthority::VERSION + 1, 7, row_digest('a')),
            (WholeBlobStoreAuthority::VERSION, 0, row_digest('a')),
            (WholeBlobStoreAuthority::VERSION, 7, String::new()),
            (
                WholeBlobStoreAuthority::VERSION,
                7,
                format!("sha256:{}", "a".repeat(64)),
            ),
            (WholeBlobStoreAuthority::VERSION, 7, row_digest('A')),
            (
                WholeBlobStoreAuthority::VERSION,
                7,
                format!("row-sha256:{}", "a".repeat(63)),
            ),
        ] {
            assert!(
                WholeBlobStoreAuthority::from_store_record(
                    version,
                    session_id.clone(),
                    revision,
                    digest,
                )
                .is_err()
            );
        }
    }

    #[test]
    fn head_canonical_store_record_constructor_validates_every_bound_fact() {
        let (session_id, head, token) = head_record();
        let valid = HeadCanonicalStoreAuthority::from_store_record(
            HeadCanonicalStoreAuthority::VERSION,
            session_id.clone(),
            11,
            head.clone(),
            token.clone(),
        )
        .expect("valid HeadCanonical record");
        assert_eq!(
            valid.authority_version(),
            HeadCanonicalStoreAuthority::VERSION
        );
        assert_eq!(valid.session_id(), &session_id);
        assert_eq!(valid.store_revision(), 11);
        assert_eq!(valid.boundary_head(), &head);
        assert_eq!(valid.committed_head_token(), token);

        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION + 1,
                session_id.clone(),
                11,
                head.clone(),
                token.clone(),
            )
            .is_err()
        );
        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION,
                session_id.clone(),
                0,
                head.clone(),
                token.clone(),
            )
            .is_err()
        );
        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION,
                session_id.clone(),
                11,
                head.clone(),
                String::new(),
            )
            .is_err()
        );
        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION,
                session_id.clone(),
                11,
                head.clone(),
                "head-cas:different".to_string(),
            )
            .is_err()
        );

        let mut wrong_session = head.clone();
        wrong_session.id = meerkat_core::types::SessionId::new();
        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION,
                session_id.clone(),
                11,
                wrong_session,
                token.clone(),
            )
            .is_err()
        );

        let mut missing_row_prefix = head.clone();
        missing_row_prefix.message_row_prefix = None;
        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION,
                session_id.clone(),
                11,
                missing_row_prefix,
                token.clone(),
            )
            .is_err()
        );

        let mut wrong_row_count = head.clone();
        wrong_row_count.message_count = wrong_row_count.message_count.saturating_add(1);
        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION,
                session_id.clone(),
                11,
                wrong_row_count,
                token.clone(),
            )
            .is_err()
        );

        let mut wrong_rewrite_count = head;
        wrong_rewrite_count.rewrite_count = wrong_rewrite_count.rewrite_count.saturating_add(1);
        assert!(
            HeadCanonicalStoreAuthority::from_store_record(
                HeadCanonicalStoreAuthority::VERSION,
                session_id,
                11,
                wrong_rewrite_count,
                token,
            )
            .is_err()
        );
    }
}

#[cfg(test)]
mod runtime_session_catalog_entry_tests {
    use super::*;
    use meerkat_core::session_store::PreparedHeadCanonicalMutation;
    use meerkat_core::types::{Message, UserMessage};

    fn labeled_session() -> meerkat_core::Session {
        let mut session = meerkat_core::Session::new();
        session.push(Message::User(UserMessage::text(
            "transcript body must not enter the catalog",
        )));
        session.set_metadata(
            RuntimeSessionCatalogEntry::SESSION_LABELS_KEY,
            serde_json::json!({
                "owner": "operations",
                "tier": "production"
            }),
        );
        session
    }

    #[test]
    fn public_session_projection_is_validated_and_body_free() {
        let session = labeled_session();
        let entry = RuntimeSessionCatalogEntry::from_session(
            &session,
            RuntimeSessionPersistenceProfile::WholeBlobV1,
            Some(RuntimeState::Idle),
        )
        .expect("typed Session projects to bounded catalog metadata");

        assert_eq!(entry.session_id(), session.id());
        assert_eq!(
            entry.persistence_profile(),
            RuntimeSessionPersistenceProfile::WholeBlobV1
        );
        assert_eq!(entry.created_at(), session.created_at());
        assert_eq!(entry.updated_at(), session.updated_at());
        assert_eq!(entry.message_count(), session.messages().len());
        assert_eq!(entry.total_tokens(), session.total_tokens());
        assert_eq!(
            entry.labels(),
            &BTreeMap::from([
                ("owner".to_string(), "operations".to_string()),
                ("tier".to_string(), "production".to_string()),
            ])
        );
        assert_eq!(entry.runtime_state(), Some(RuntimeState::Idle));

        let encoded = serde_json::to_string(&entry).expect("catalog entry serializes");
        assert!(
            !encoded.contains("transcript body must not enter the catalog"),
            "catalog projection must never carry transcript body data"
        );
    }

    #[test]
    fn public_head_projection_matches_session_catalog_facts() {
        let session = labeled_session();
        let mutation =
            PreparedHeadCanonicalMutation::prepare(&session, None).expect("prepare canonical head");
        let from_session = RuntimeSessionCatalogEntry::from_session(
            &session,
            RuntimeSessionPersistenceProfile::HeadCanonicalV1,
            None,
        )
        .expect("Session catalog projection");
        let from_head = RuntimeSessionCatalogEntry::from_head(
            mutation.successor_head(),
            RuntimeSessionPersistenceProfile::HeadCanonicalV1,
            None,
        )
        .expect("SessionHead catalog projection");

        assert_eq!(from_head, from_session);
    }

    #[test]
    fn public_catalog_projections_reject_malformed_labels() {
        let mut session = labeled_session();
        session.set_metadata(
            RuntimeSessionCatalogEntry::SESSION_LABELS_KEY,
            serde_json::json!(["not", "a", "label", "map"]),
        );

        assert!(matches!(
            RuntimeSessionCatalogEntry::from_session(
                &session,
                RuntimeSessionPersistenceProfile::WholeBlobV1,
                None,
            ),
            Err(RuntimeStoreError::WriteFailed(detail))
                if detail.contains("malformed catalog labels")
        ));

        let mutation =
            PreparedHeadCanonicalMutation::prepare(&session, None).expect("prepare canonical head");
        assert!(matches!(
            RuntimeSessionCatalogEntry::from_head(
                mutation.successor_head(),
                RuntimeSessionPersistenceProfile::HeadCanonicalV1,
                None,
            ),
            Err(RuntimeStoreError::WriteFailed(detail))
                if detail.contains("malformed catalog labels")
        ));
    }
}

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

    fn operation_id(
        value: u128,
    ) -> meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId {
        meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId::from_uuid(
            uuid::Uuid::from_u128(value),
        )
    }

    fn binding(seed: u8, name: &str, epoch: u64) -> SupervisorBindingReceipt {
        let pubkey = [seed; 32];
        SupervisorBindingReceipt::new(
            name.to_string(),
            meerkat_core::comms::PeerId::from_ed25519_pubkey(&pubkey).as_str(),
            format!("inproc://{name}"),
            crate::comms_drain::encode_supervisor_signing_public_key(pubkey),
            epoch,
        )
    }

    fn rotation(
        operation_id: meerkat_contracts::wire::supervisor_bridge::SupervisorRotationOperationId,
        phase: SupervisorRotationPersistencePhase,
        rejection: Option<SupervisorRotationRejection>,
        previous: SupervisorBindingReceipt,
        next: SupervisorBindingReceipt,
    ) -> SupervisorRotationReceipt {
        SupervisorRotationReceipt::new(operation_id, phase, rejection, previous, next)
    }

    fn snapshot(authority: SupervisorAuthoritySnapshot) -> MachineLifecycleSnapshot {
        MachineLifecycleSnapshot::new(
            RuntimeState::Idle,
            MachineLifecycleBindingFacts::new(None, None, None, None),
            authority,
        )
    }

    fn encode_snapshot(snapshot: &MachineLifecycleSnapshot) -> Vec<u8> {
        MachineLifecycleStoreRecord::from_snapshot(snapshot)
            .encode()
            .expect("encode lifecycle snapshot")
    }

    fn encode_unvalidated_snapshot(snapshot: &MachineLifecycleSnapshot) -> Vec<u8> {
        serde_json::to_vec(&MachineLifecycleSnapshotStoreWire::from(snapshot))
            .expect("serialize deliberately corrupt lifecycle snapshot")
    }

    fn encoded_value(snapshot: &MachineLifecycleSnapshot) -> serde_json::Value {
        serde_json::from_slice(&encode_snapshot(snapshot)).expect("decode encoded snapshot as JSON")
    }

    fn assert_decode_fails(value: serde_json::Value) {
        let bytes = serde_json::to_vec(&value).expect("serialize corrupt lifecycle record");
        assert!(
            decode_machine_lifecycle_store_record(&bytes).is_err(),
            "corrupt lifecycle record must fail closed: {value}"
        );
    }

    #[test]
    fn version_one_record_without_supervisor_authority_migrates_explicitly_to_unbound() {
        let bytes = serde_json::to_vec(&serde_json::json!({
            "record_version": LEGACY_MACHINE_LIFECYCLE_STORE_RECORD_VERSION,
            "runtime_state": RuntimeState::Retired,
            "binding": {
                "agent_runtime_id": "rt:session:legacy-v1",
                "fence_token": 19,
                "runtime_generation": 4,
                "runtime_epoch_id": "epoch-legacy-v1"
            }
        }))
        .expect("serialize legacy v1 lifecycle record");

        let decoded = decode_machine_lifecycle_store_record(&bytes)
            .expect("valid v1 record without the additive field must decode");
        assert_eq!(decoded.runtime_state(), RuntimeState::Retired);
        assert_eq!(
            decoded.supervisor_authority(),
            &SupervisorAuthoritySnapshot::UnboundNoReceipt
        );
    }

    #[test]
    fn current_record_requires_supervisor_authority_and_unregister_progress_presence() {
        assert_decode_fails(serde_json::json!({
            "record_version": MACHINE_LIFECYCLE_STORE_RECORD_VERSION,
            "runtime_state": RuntimeState::Idle,
            "binding": {
                "agent_runtime_id": null,
                "fence_token": null,
                "runtime_generation": null,
                "runtime_epoch_id": null
            },
            "unregister_progress": null
        }));
    }

    #[test]
    fn current_nullable_fields_require_presence_but_accept_explicit_null() {
        let unbound = snapshot(SupervisorAuthoritySnapshot::UnboundNoReceipt);
        let encoded = encoded_value(&unbound);
        assert_eq!(
            decode_machine_lifecycle_store_record(
                &serde_json::to_vec(&encoded).expect("serialize valid current record")
            )
            .expect("explicit-null current binding fields must decode"),
            unbound
        );
        let mut missing_progress = encoded.clone();
        missing_progress
            .as_object_mut()
            .expect("lifecycle record object")
            .remove("unregister_progress");
        assert_decode_fails(missing_progress);

        for field in [
            "agent_runtime_id",
            "fence_token",
            "runtime_generation",
            "runtime_epoch_id",
        ] {
            let mut partial = encoded.clone();
            partial["binding"]
                .as_object_mut()
                .expect("binding object")
                .remove(field);
            assert_decode_fails(partial);
        }
        for field in ["current_run_id", "pre_run_phase"] {
            let mut partial = encoded.clone();
            partial
                .as_object_mut()
                .expect("lifecycle record object")
                .remove(field);
            assert_decode_fails(partial);
        }

        let completed = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
            operation_id(101),
            SupervisorRotationPersistencePhase::Completed,
            None,
            binding(30, "required-null-previous", 4),
            binding(31, "required-null-next", 5),
        )));
        let mut missing_rejection = encoded_value(&completed);
        assert!(missing_rejection["supervisor_authority"]["rotation"]["rejection"].is_null());
        missing_rejection["supervisor_authority"]["rotation"]
            .as_object_mut()
            .expect("rotation object")
            .remove("rejection");
        assert_decode_fails(missing_rejection);
    }

    #[test]
    fn lossless_observation_preserves_partial_run_pair_and_nullable_lifecycle() {
        let mut value = encoded_value(&snapshot(SupervisorAuthoritySnapshot::UnboundNoReceipt));
        let run_id = RunId::new();
        value["runtime_state"] = serde_json::Value::Null;
        value["current_run_id"] = serde_json::to_value(&run_id).expect("serialize run id");
        value["pre_run_phase"] = serde_json::Value::Null;
        let bytes = serde_json::to_vec(&value).expect("serialize partial lifecycle row");

        let MachineLifecycleObservation::Decoded { record, version } =
            classify_machine_lifecycle_record(&bytes)
        else {
            panic!("explicitly nullable partial runtime tuple must remain decoded");
        };
        assert_eq!(
            record.record_version(),
            MACHINE_LIFECYCLE_STORE_RECORD_VERSION
        );
        assert_eq!(record.runtime_state(), None);
        assert_eq!(record.run().current_run_id(), Some(&run_id));
        assert_eq!(record.run().pre_run_phase(), None);
        assert_eq!(
            version.as_str(),
            format!("sha256:{:x}", Sha256::digest(&bytes))
        );
        assert!(decode_machine_lifecycle_store_record(&bytes).is_err());
    }

    #[test]
    fn lifecycle_observation_distinguishes_unsupported_and_malformed_raw_rows() {
        let unsupported = br#"{"record_version":99,"opaque":"future"}"#;
        assert!(matches!(
            classify_machine_lifecycle_record(unsupported),
            MachineLifecycleObservation::Unsupported {
                record_version: 99,
                ..
            }
        ));

        let malformed = br#"{"record_version":4,"binding":"torn"}"#;
        assert!(matches!(
            classify_machine_lifecycle_record(malformed),
            MachineLifecycleObservation::Malformed {
                record_version: Some(4),
                ..
            }
        ));

        let undecodable = b"not-json";
        assert!(matches!(
            classify_machine_lifecycle_record(undecodable),
            MachineLifecycleObservation::Malformed {
                record_version: None,
                ..
            }
        ));
    }

    #[test]
    fn version_three_unregister_record_migrates_without_run_binding() {
        let expected = snapshot(SupervisorAuthoritySnapshot::UnboundNoReceipt);
        let mut value = encoded_value(&expected);
        value["record_version"] =
            serde_json::json!(UNREGISTER_MACHINE_LIFECYCLE_STORE_RECORD_VERSION);
        value
            .as_object_mut()
            .expect("lifecycle record object")
            .remove("current_run_id");
        value
            .as_object_mut()
            .expect("lifecycle record object")
            .remove("pre_run_phase");
        let bytes = serde_json::to_vec(&value).expect("serialize v3 row");
        let decoded = decode_machine_lifecycle_store_record(&bytes).expect("decode v3 row");
        assert_eq!(decoded, expected);
        assert_eq!(decoded.run(), &MachineLifecycleRunFacts::default());
    }

    #[test]
    fn version_two_supervisor_record_migrates_with_no_unregister_progress() {
        let bytes = serde_json::to_vec(&serde_json::json!({
            "record_version": SUPERVISOR_MACHINE_LIFECYCLE_STORE_RECORD_VERSION,
            "runtime_state": RuntimeState::Retired,
            "binding": {
                "agent_runtime_id": "rt:session:legacy-v2",
                "fence_token": 23,
                "runtime_generation": 5,
                "runtime_epoch_id": "epoch-legacy-v2"
            },
            "supervisor_authority": { "kind": "unbound_no_receipt" }
        }))
        .expect("serialize v2 lifecycle record");

        let decoded = decode_machine_lifecycle_store_record(&bytes)
            .expect("valid v2 supervisor record must migrate");
        assert_eq!(decoded.runtime_state(), RuntimeState::Retired);
        assert_eq!(decoded.unregister_progress(), None);
    }

    #[test]
    fn current_unregister_progress_rejects_forced_disposition_before_feedback() {
        let mut value = encoded_value(&snapshot(SupervisorAuthoritySnapshot::UnboundNoReceipt));
        value["unregister_progress"] = serde_json::json!({
            "runtime_loop_drain_pending": true,
            "comms_drain_exit_pending": false,
            "completion_waiter_drain_pending": true,
            "runtime_loop_forced_abort": true,
            "comms_drain_forced_abort": false
        });
        assert_decode_fails(value);
    }

    #[test]
    fn version_one_migration_rejects_current_authority_fields() {
        assert_decode_fails(serde_json::json!({
            "record_version": LEGACY_MACHINE_LIFECYCLE_STORE_RECORD_VERSION,
            "runtime_state": RuntimeState::Idle,
            "binding": {
                "agent_runtime_id": null,
                "fence_token": null,
                "runtime_generation": null,
                "runtime_epoch_id": null
            },
            "supervisor_authority": { "kind": "unbound_no_receipt" }
        }));
    }

    #[test]
    fn mixed_or_unknown_supervisor_authority_fields_fail_closed() {
        let current = binding(1, "current-supervisor", 7);
        let mut value = encoded_value(&snapshot(SupervisorAuthoritySnapshot::Bound(current)));
        value["supervisor_authority"]["rotation"] = serde_json::json!({});
        assert_decode_fails(value);
    }

    #[test]
    fn completed_rotation_operation_receipt_round_trips_for_cold_observation() {
        let snapshot = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
            operation_id(1),
            SupervisorRotationPersistencePhase::Completed,
            None,
            binding(1, "previous-supervisor", 7),
            binding(2, "next-supervisor", 8),
        )));

        let encoded = encode_snapshot(&snapshot);
        let decoded = decode_machine_lifecycle_store_record(&encoded)
            .expect("decode completed rotation receipt");

        assert_eq!(decoded, snapshot);
    }

    #[test]
    fn exact_current_completed_adoption_round_trips_but_other_equal_epoch_completion_fails() {
        let current = binding(3, "already-rotated-supervisor", 9);
        let adoption = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
            operation_id(2),
            SupervisorRotationPersistencePhase::Completed,
            None,
            current.clone(),
            current,
        )));
        assert_eq!(
            decode_machine_lifecycle_store_record(&encode_snapshot(&adoption))
                .expect("exact-current legacy adoption receipt must decode"),
            adoption
        );

        let non_advancing = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
            operation_id(3),
            SupervisorRotationPersistencePhase::Completed,
            None,
            binding(3, "previous-supervisor", 9),
            binding(4, "different-supervisor", 9),
        )));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&non_advancing))
                .is_err()
        );
    }

    #[test]
    fn malformed_rotation_descriptors_epochs_and_operation_ids_fail_closed() {
        let invalid_previous = SupervisorBindingReceipt::new(
            String::new(),
            "not-a-uuid".to_string(),
            "not-an-address".to_string(),
            "not-a-key".to_string(),
            1,
        );
        let invalid_previous_receipt =
            snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
                operation_id(4),
                SupervisorRotationPersistencePhase::Rejected,
                Some(SupervisorRotationRejection::InvalidTarget),
                invalid_previous,
                binding(5, "raw-target", 2),
            )));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(
                &invalid_previous_receipt,
            ))
            .is_err()
        );

        let invalid_next = SupervisorBindingReceipt::new(
            "invalid-target".to_string(),
            "not-a-uuid".to_string(),
            "not-an-address".to_string(),
            "not-a-key".to_string(),
            2,
        );
        let invalid_completed_target =
            snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
                operation_id(5),
                SupervisorRotationPersistencePhase::Completed,
                None,
                binding(6, "previous-supervisor", 1),
                invalid_next,
            )));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(
                &invalid_completed_target,
            ))
            .is_err()
        );

        let mut invalid_id = encoded_value(&snapshot(
            SupervisorAuthoritySnapshot::RotationOperation(rotation(
                operation_id(6),
                SupervisorRotationPersistencePhase::PreviousRevokePending,
                None,
                binding(7, "previous-supervisor", 1),
                binding(8, "next-supervisor", 2),
            )),
        ));
        invalid_id["supervisor_authority"]["rotation"]["operation_id"] =
            serde_json::json!("not-a-uuid");
        assert_decode_fails(invalid_id);

        let nil_id = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
            operation_id(0),
            SupervisorRotationPersistencePhase::PreviousRevokePending,
            None,
            binding(7, "previous-supervisor", 1),
            binding(8, "next-supervisor", 2),
        )));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&nil_id)).is_err()
        );

        let non_advancing_pending =
            snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
                operation_id(13),
                SupervisorRotationPersistencePhase::PreviousRevokePending,
                None,
                binding(7, "previous-supervisor", 4),
                binding(8, "next-supervisor", 4),
            )));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(
                &non_advancing_pending,
            ))
            .is_err()
        );
    }

    #[test]
    fn rejected_invalid_or_unsupported_target_preserves_raw_evidence() {
        for (id, rejection) in [
            (7, SupervisorRotationRejection::InvalidTarget),
            (14, SupervisorRotationRejection::UnsupportedProtocolVersion),
        ] {
            let raw_invalid_target = SupervisorBindingReceipt::new(
                "".to_string(),
                "not-a-peer-id".to_string(),
                "not-an-address".to_string(),
                "not-a-signing-key".to_string(),
                0,
            );
            let snapshot = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
                operation_id(id),
                SupervisorRotationPersistencePhase::Rejected,
                Some(rejection),
                binding(9, "retained-supervisor", 11),
                raw_invalid_target,
            )));
            assert_eq!(
                decode_machine_lifecycle_store_record(&encode_snapshot(&snapshot))
                    .expect("rejected raw target evidence must remain durable"),
                snapshot
            );
        }
    }

    #[test]
    fn only_raw_target_rejections_are_durable_and_epoch_rejection_must_be_genuine() {
        for (id, rejection) in [
            (102, SupervisorRotationRejection::OperationConflict),
            (103, SupervisorRotationRejection::NotBound),
            (104, SupervisorRotationRejection::SenderMismatch),
        ] {
            let impossible = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
                operation_id(id),
                SupervisorRotationPersistencePhase::Rejected,
                Some(rejection),
                binding(32, "retained-supervisor", 7),
                binding(33, "requested-supervisor", 8),
            )));
            assert!(
                MachineLifecycleStoreRecord::from_snapshot(&impossible)
                    .encode()
                    .is_err()
            );
            assert!(
                decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&impossible))
                    .is_err()
            );
        }

        let advancing = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
            operation_id(105),
            SupervisorRotationPersistencePhase::Rejected,
            Some(SupervisorRotationRejection::TargetEpochNotAdvanced),
            binding(34, "retained-supervisor", 9),
            binding(35, "advancing-target", 10),
        )));
        assert!(
            MachineLifecycleStoreRecord::from_snapshot(&advancing)
                .encode()
                .is_err()
        );
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&advancing))
                .is_err()
        );

        let non_advancing = snapshot(SupervisorAuthoritySnapshot::RotationOperation(rotation(
            operation_id(106),
            SupervisorRotationPersistencePhase::Rejected,
            Some(SupervisorRotationRejection::TargetEpochNotAdvanced),
            binding(36, "retained-supervisor", 11),
            binding(37, "non-advancing-target", 11),
        )));
        assert_eq!(
            decode_machine_lifecycle_store_record(&encode_snapshot(&non_advancing))
                .expect("genuine target-epoch rejection must remain durable"),
            non_advancing
        );
    }

    #[test]
    fn malformed_current_authority_variants_fail_closed() {
        let malformed = SupervisorBindingReceipt::new(
            String::new(),
            "not-a-peer-id".to_string(),
            "not-an-address".to_string(),
            "not-a-signing-key".to_string(),
            1,
        );
        let bound = snapshot(SupervisorAuthoritySnapshot::Bound(malformed.clone()));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&bound)).is_err()
        );

        let pending = snapshot(SupervisorAuthoritySnapshot::RevocationPending(
            SupervisorRevocationPendingReceipt::new(
                malformed.name().to_owned(),
                malformed.peer_id().to_owned(),
                malformed.address().to_owned(),
                malformed.signing_public_key().to_owned(),
                malformed.epoch(),
            ),
        ));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&pending)).is_err()
        );

        let revoked = snapshot(SupervisorAuthoritySnapshot::RevokedReceipt(
            RevokedSupervisorReceipt::new(
                malformed.peer_id().to_owned(),
                malformed.signing_public_key().to_owned(),
                malformed.epoch(),
            ),
        ));
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&revoked)).is_err()
        );
    }

    #[test]
    fn partial_and_nonterminal_history_records_fail_closed() {
        let receipt = rotation(
            operation_id(8),
            SupervisorRotationPersistencePhase::Completed,
            None,
            binding(10, "history-previous", 1),
            binding(11, "history-next", 2),
        );
        let history = std::collections::BTreeMap::from([(receipt.operation_id(), receipt)]);
        let snapshot = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::Bound(binding(
                12,
                "current-supervisor",
                3,
            ))),
            terminal_receipts: history,
        });

        let mut partial = encoded_value(&snapshot);
        partial["supervisor_authority"]["terminal_receipts"][0]
            .as_object_mut()
            .expect("history receipt object")
            .remove("next");
        assert_decode_fails(partial);

        let mut nonterminal = encoded_value(&snapshot);
        nonterminal["supervisor_authority"]["terminal_receipts"][0]["phase"] =
            serde_json::json!("next_publish_pending");
        assert_decode_fails(nonterminal);
    }

    #[test]
    fn duplicate_nested_and_active_history_conflicts_fail_closed() {
        let history_receipt = rotation(
            operation_id(9),
            SupervisorRotationPersistencePhase::Completed,
            None,
            binding(13, "history-previous", 1),
            binding(14, "history-next", 2),
        );
        let history = std::collections::BTreeMap::from([(
            history_receipt.operation_id(),
            history_receipt.clone(),
        )]);
        let wrapper = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::Bound(binding(
                15,
                "current-supervisor",
                3,
            ))),
            terminal_receipts: history,
        });

        let mut duplicate = encoded_value(&wrapper);
        let receipt = duplicate["supervisor_authority"]["terminal_receipts"][0].clone();
        duplicate["supervisor_authority"]["terminal_receipts"]
            .as_array_mut()
            .expect("history receipt array")
            .push(receipt);
        assert_decode_fails(duplicate);

        let mut nested = encoded_value(&wrapper);
        let nested_current = nested["supervisor_authority"].clone();
        nested["supervisor_authority"]["current"] = nested_current;
        assert_decode_fails(nested);

        let active_conflict = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::RotationOperation(
                history_receipt.clone(),
            )),
            terminal_receipts: std::collections::BTreeMap::from([(
                history_receipt.operation_id(),
                history_receipt,
            )]),
        });
        assert!(
            MachineLifecycleStoreRecord::from_snapshot(&active_conflict)
                .encode()
                .is_err()
        );

        let empty_history = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::Bound(binding(
                20,
                "current-supervisor",
                4,
            ))),
            terminal_receipts: std::collections::BTreeMap::new(),
        });
        assert!(
            MachineLifecycleStoreRecord::from_snapshot(&empty_history)
                .encode()
                .is_err()
        );
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&empty_history))
                .is_err()
        );

        let mismatched_key = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::Bound(binding(
                21,
                "current-supervisor",
                4,
            ))),
            terminal_receipts: std::collections::BTreeMap::from([(
                operation_id(99),
                rotation(
                    operation_id(98),
                    SupervisorRotationPersistencePhase::Completed,
                    None,
                    binding(22, "history-previous", 2),
                    binding(23, "history-next", 3),
                ),
            )]),
        });
        assert!(
            MachineLifecycleStoreRecord::from_snapshot(&mismatched_key)
                .encode()
                .is_err()
        );
    }

    #[test]
    fn history_current_epoch_and_same_epoch_identity_must_cohere() {
        let previous = binding(38, "history-previous", 12);
        let next = binding(39, "history-next", 13);
        let completed = rotation(
            operation_id(107),
            SupervisorRotationPersistencePhase::Completed,
            None,
            previous.clone(),
            next.clone(),
        );
        let history =
            std::collections::BTreeMap::from([(completed.operation_id(), completed.clone())]);

        let stale_current = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::Bound(binding(
                38,
                "refreshed-history-previous",
                12,
            ))),
            terminal_receipts: history.clone(),
        });
        assert!(
            MachineLifecycleStoreRecord::from_snapshot(&stale_current)
                .encode()
                .is_err()
        );
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(&stale_current))
                .is_err()
        );

        let conflicting_current = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::Bound(binding(
                40,
                "conflicting-current",
                13,
            ))),
            terminal_receipts: history.clone(),
        });
        assert!(
            MachineLifecycleStoreRecord::from_snapshot(&conflicting_current)
                .encode()
                .is_err()
        );
        assert!(
            decode_machine_lifecycle_store_record(&encode_unvalidated_snapshot(
                &conflicting_current,
            ))
            .is_err()
        );

        let route_refreshed_current = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::Bound(binding(
                39,
                "route-refreshed-history-next",
                13,
            ))),
            terminal_receipts: history,
        });
        assert_eq!(
            decode_machine_lifecycle_store_record(&encode_snapshot(&route_refreshed_current))
                .expect("same identity may refresh route metadata within one epoch"),
            route_refreshed_current
        );
    }

    #[test]
    fn terminal_history_survives_later_rotation_and_recovery() {
        let first = rotation(
            operation_id(10),
            SupervisorRotationPersistencePhase::Completed,
            None,
            binding(16, "first-supervisor", 1),
            binding(17, "second-supervisor", 2),
        );
        let rejected = rotation(
            operation_id(11),
            SupervisorRotationPersistencePhase::Rejected,
            Some(SupervisorRotationRejection::TargetEpochNotAdvanced),
            binding(17, "second-supervisor", 2),
            binding(18, "rejected-supervisor", 2),
        );
        let later = rotation(
            operation_id(12),
            SupervisorRotationPersistencePhase::Completed,
            None,
            binding(17, "second-supervisor", 2),
            binding(19, "current-supervisor", 3),
        );
        let snapshot = snapshot(SupervisorAuthoritySnapshot::WithRotationHistory {
            current: Box::new(SupervisorAuthoritySnapshot::RotationOperation(later)),
            terminal_receipts: std::collections::BTreeMap::from([
                (first.operation_id(), first),
                (rejected.operation_id(), rejected),
            ]),
        });

        let decoded = decode_machine_lifecycle_store_record(&encode_snapshot(&snapshot))
            .expect("later rotation and old terminal history must recover together");
        assert_eq!(decoded, snapshot);
    }
}