pointbreak 0.10.0

Durable terminal code review for changes humans and coding agents collaborate on together
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
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::canonical_hash::{canonical_json_bytes, sha256_bytes_hex, sha256_json_prefixed};
use crate::crypto::EventSigner;
use crate::error::{Result, ShoreError};
use crate::model::{
    ActorId, ChangeId, ChangeIdentityDescriptorV1, JournalId, RevisionId, RevisionRefV1,
    current_revisions, replacement_heads_diverge, revision_graph_has_cycle,
};
use crate::session::event::{
    ChangeMembershipAssertedPayload, ChangeRevisionRelationAssertedPayload, EventPayload,
    EventTarget, EventType, ShoreEvent, WorkObjectProposal, WorkObjectProposedPayload, Writer,
    WriterProducer, build_change_declared, build_membership_asserted,
    build_revision_relation_asserted,
};
use crate::session::store::authority_lock::{STORE_AUTHORITY_LOCK_FILE, StoreAuthorityLock};
use crate::session::store::capabilities::{
    BulkAdoptionCompletionV1, REVIEW_CHANGE_REVISION_COHORT_V1, StoreCapabilityActivationV1,
    build_signed_activation, build_signed_completion, inspect_journal_records,
    publish_control_record,
};
use crate::session::store::resolution::resolve_change_read_store;
use crate::session::store::{BulkAdoptionManifestV1, ReservedCohortRecordV1};
use crate::session::{AuthorityCursorV2, StoreCapabilityStatus, StoreIdentityOptions};
use crate::storage::{CreateOutcome, Durability, LocalStorage};

pub const BULK_ADOPTION_DRY_RUN_SCHEMA_V1: &str = "pointbreak.bulk-adoption-dry-run.v1";
pub const BULK_ADOPTION_MIGRATION_RECEIPT_SCHEMA_V1: &str =
    "pointbreak.bulk-adoption-migration-receipt.v1";
pub const BULK_ADOPTION_MINIMUM_READER_PROFILE_V1: &str = REVIEW_CHANGE_REVISION_COHORT_V1;
pub const BULK_ADOPTION_BACKUP_MANIFEST_FILE_V1: &str = "backup-manifest.json";
pub const BULK_ADOPTION_BACKUP_RECEIPT_FILE_V1: &str = "backup-receipt.json";
pub const BULK_ADOPTION_EXECUTION_PLAN_FILE_V1: &str = "migration-plan.json";
const BULK_ADOPTION_MIGRATION_RECEIPT_FILE_V1: &str = "migration-receipt.json";

const BULK_ADOPTION_BACKUP_MANIFEST_SCHEMA_V1: &str = "pointbreak.bulk-adoption-backup-manifest.v1";
const BULK_ADOPTION_BACKUP_RECEIPT_SCHEMA_V1: &str = "pointbreak.bulk-adoption-backup-receipt.v1";
const BULK_ADOPTION_EXECUTION_PLAN_SCHEMA_V1: &str = "pointbreak.bulk-adoption-execution-plan.v1";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BulkAdoptionDryRunOptions {
    roots: Vec<PathBuf>,
    actor_id: ActorId,
    owner_decisions: Option<BulkAdoptionOwnerDecisionManifestV1>,
}

impl BulkAdoptionDryRunOptions {
    pub fn new(root: impl AsRef<Path>) -> Self {
        Self {
            roots: vec![root.as_ref().to_path_buf()],
            actor_id: ActorId::new("actor:bulk-adoption-dry-run"),
            owner_decisions: None,
        }
    }

    pub fn with_root(mut self, root: impl AsRef<Path>) -> Self {
        self.roots.push(root.as_ref().to_path_buf());
        self
    }

    pub fn with_actor_id(mut self, actor_id: ActorId) -> Self {
        self.actor_id = actor_id;
        self
    }

    pub fn with_owner_decisions(mut self, decisions: BulkAdoptionOwnerDecisionManifestV1) -> Self {
        self.owner_decisions = Some(decisions);
        self
    }
}

pub const BULK_ADOPTION_OWNER_DECISIONS_SCHEMA_V1: &str =
    "pointbreak.bulk-adoption-owner-decisions.v1";

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "snake_case", deny_unknown_fields)]
pub enum BulkAdoptionOverlapIdentityDecisionV1 {
    Shared {
        identity_descriptor: ChangeIdentityDescriptorV1,
    },
    Distinct,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionRetainedAllocationV1 {
    pub change_id: ChangeId,
    pub identity_descriptor: ChangeIdentityDescriptorV1,
    pub members: BTreeSet<RevisionId>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionRetainedManifestV1 {
    pub manifest_hash: String,
    pub allocations: Vec<BulkAdoptionRetainedAllocationV1>,
}

/// Owner-authored amendment to a dry-run proposal. It carries identities and
/// anomaly acknowledgements only—never paths or corpus bytes—and is itself
/// hashed into the amended dry-run document.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionOwnerDecisionManifestV1 {
    pub schema: String,
    #[serde(default)]
    pub approved_anomaly_ids: BTreeSet<String>,
    #[serde(default)]
    pub overlap_identity: BTreeMap<RevisionId, BulkAdoptionOverlapIdentityDecisionV1>,
    #[serde(default)]
    pub retained_manifests: Vec<BulkAdoptionRetainedManifestV1>,
    pub claim_occurred_at: String,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionDryRunRootV1 {
    pub root_identity_hash: String,
    pub source_authority_cursor: AuthorityCursorV2,
    pub revision_count: usize,
    pub proposed_changes: Vec<BulkAdoptionDryRunChangeV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cohort_manifest_hash: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionDryRunChangeV1 {
    pub change_id: ChangeId,
    pub members: BTreeSet<RevisionId>,
    pub admitted_relations: BTreeSet<(RevisionId, RevisionId)>,
    pub membership_claim_ids: BTreeSet<String>,
    pub relation_claim_ids: BTreeSet<String>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionDryRunAnomalyV1 {
    pub anomaly_id: String,
    pub code: String,
    pub root_identity_hashes: BTreeSet<String>,
    pub revision_ids: BTreeSet<RevisionId>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionDryRunDocumentV1 {
    pub schema: String,
    pub manifest_hash: String,
    pub roots: Vec<BulkAdoptionDryRunRootV1>,
    pub anomalies: Vec<BulkAdoptionDryRunAnomalyV1>,
    pub requires_owner_decision: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner_decision_manifest_hash: Option<String>,
    pub writer: Writer,
    pub claim_occurred_at: String,
    pub signature_policy: String,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct LegacyRevisionV1 {
    revision: RevisionRefV1,
    legacy_group: String,
    supersedes: Vec<RevisionId>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct LegacyRootInventoryV1 {
    root_identity_hash: String,
    journal_id: JournalId,
    source_authority_cursor: AuthorityCursorV2,
    revisions: Vec<LegacyRevisionV1>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "snake_case")]
enum OverlapIdentityDecisionV1 {
    Shared {
        identity_descriptor: ChangeIdentityDescriptorV1,
    },
    Distinct,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct RetainedChangeAllocationV1 {
    change_id: ChangeId,
    identity_descriptor: ChangeIdentityDescriptorV1,
    members: BTreeSet<RevisionId>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct RetainedAdoptionManifestV1 {
    manifest_hash: String,
    allocations: Vec<RetainedChangeAllocationV1>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct OwnerManifestDecisionsV1 {
    approved_anomaly_ids: BTreeSet<String>,
    overlap_identity: BTreeMap<RevisionId, OverlapIdentityDecisionV1>,
    writer: Writer,
    occurred_at: String,
}

impl Default for OwnerManifestDecisionsV1 {
    fn default() -> Self {
        Self {
            approved_anomaly_ids: BTreeSet::new(),
            overlap_identity: BTreeMap::new(),
            writer: Writer::shore_local("bulk-adoption-proposal-v1"),
            occurred_at: "1970-01-01T00:00:00Z".to_owned(),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MigrationAnomalyV1 {
    anomaly_id: String,
    code: String,
    root_identity_hashes: BTreeSet<String>,
    revision_ids: BTreeSet<RevisionId>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AdoptionGroupProposalV1 {
    change_id: ChangeId,
    members: BTreeSet<RevisionId>,
    admitted_relations: BTreeSet<(RevisionId, RevisionId)>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct RootAdoptionProposalV1 {
    root_identity_hash: String,
    covered_revisions: BTreeSet<RevisionId>,
    groups: Vec<AdoptionGroupProposalV1>,
    planned_events: Vec<ShoreEvent>,
    manifest: Option<BulkAdoptionManifestV1>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct BulkAdoptionProposalV1 {
    roots: Vec<RootAdoptionProposalV1>,
    anomalies: Vec<MigrationAnomalyV1>,
    requires_owner_decision: bool,
}

/// Produce a path-private, deterministic migration proposal without mutating
/// any supplied root. Every root must still be L0; M1/L2 input is refused
/// before the planner reads legacy Revision payloads.
pub fn dry_run_bulk_adoption(
    options: BulkAdoptionDryRunOptions,
) -> Result<BulkAdoptionDryRunDocumentV1> {
    if options.roots.is_empty() {
        return Err(invalid_migration("at least one L0 root is required"));
    }
    let mut inventories = options
        .roots
        .iter()
        .map(|root| legacy_inventory_for_repo(root))
        .collect::<Result<Vec<_>>>()?;
    inventories.sort_by(|left, right| left.root_identity_hash.cmp(&right.root_identity_hash));
    if inventories
        .windows(2)
        .any(|pair| pair[0].root_identity_hash == pair[1].root_identity_hash)
    {
        return Err(invalid_migration(
            "the same resolved store was supplied more than once",
        ));
    }
    let writer = Writer {
        actor_id: options.actor_id,
        producer: WriterProducer {
            name: "pointbreak".to_owned(),
            version: env!("CARGO_PKG_VERSION").to_owned(),
        },
    };
    let owner_decision_manifest_hash = options
        .owner_decisions
        .as_ref()
        .map(validate_owner_decisions)
        .transpose()?;
    let retained_manifests = options
        .owner_decisions
        .as_ref()
        .map(|manifest| {
            manifest
                .retained_manifests
                .iter()
                .map(|retained| RetainedAdoptionManifestV1 {
                    manifest_hash: retained.manifest_hash.clone(),
                    allocations: retained
                        .allocations
                        .iter()
                        .map(|allocation| RetainedChangeAllocationV1 {
                            change_id: allocation.change_id.clone(),
                            identity_descriptor: allocation.identity_descriptor.clone(),
                            members: allocation.members.clone(),
                        })
                        .collect(),
                })
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();
    let decisions = options
        .owner_decisions
        .as_ref()
        .map(|manifest| OwnerManifestDecisionsV1 {
            approved_anomaly_ids: manifest.approved_anomaly_ids.clone(),
            overlap_identity: manifest
                .overlap_identity
                .iter()
                .map(|(revision, decision)| {
                    let decision = match decision {
                        BulkAdoptionOverlapIdentityDecisionV1::Shared {
                            identity_descriptor,
                        } => OverlapIdentityDecisionV1::Shared {
                            identity_descriptor: identity_descriptor.clone(),
                        },
                        BulkAdoptionOverlapIdentityDecisionV1::Distinct => {
                            OverlapIdentityDecisionV1::Distinct
                        }
                    };
                    (revision.clone(), decision)
                })
                .collect(),
            writer: writer.clone(),
            occurred_at: manifest.claim_occurred_at.clone(),
        })
        .unwrap_or_else(|| OwnerManifestDecisionsV1 {
            writer: writer.clone(),
            ..OwnerManifestDecisionsV1::default()
        });
    let proposal = plan_bulk_adoption(&inventories, &retained_manifests, &decisions)?;
    let mut roots = Vec::with_capacity(proposal.roots.len());
    for root in &proposal.roots {
        let inventory = inventories
            .iter()
            .find(|inventory| inventory.root_identity_hash == root.root_identity_hash)
            .expect("proposal root came from the supplied inventory");
        let mut proposed_changes = Vec::with_capacity(root.groups.len());
        for group in &root.groups {
            let mut membership_claim_ids = BTreeSet::new();
            let mut relation_claim_ids = BTreeSet::new();
            for event in &root.planned_events {
                match event.event_type {
                    EventType::ChangeMembershipAsserted => {
                        let payload: ChangeMembershipAssertedPayload =
                            serde_json::from_value(event.payload.clone())?;
                        if payload.change_id == group.change_id
                            && group.members.contains(&payload.revision_id)
                        {
                            membership_claim_ids
                                .insert(payload.membership_claim_id.as_str().to_owned());
                        }
                    }
                    EventType::ChangeRevisionRelationAsserted => {
                        let payload: ChangeRevisionRelationAssertedPayload =
                            serde_json::from_value(event.payload.clone())?;
                        if payload.change_id == group.change_id {
                            relation_claim_ids
                                .insert(payload.relation_claim_id.as_str().to_owned());
                        }
                    }
                    _ => {}
                }
            }
            proposed_changes.push(BulkAdoptionDryRunChangeV1 {
                change_id: group.change_id.clone(),
                members: group.members.clone(),
                admitted_relations: group.admitted_relations.clone(),
                membership_claim_ids,
                relation_claim_ids,
            });
        }
        proposed_changes.sort_by(|left, right| left.change_id.cmp(&right.change_id));
        roots.push(BulkAdoptionDryRunRootV1 {
            root_identity_hash: root.root_identity_hash.clone(),
            source_authority_cursor: inventory.source_authority_cursor.clone(),
            revision_count: root.covered_revisions.len(),
            proposed_changes,
            cohort_manifest_hash: root
                .manifest
                .as_ref()
                .map(BulkAdoptionManifestV1::canonical_hash)
                .transpose()?,
        });
    }
    let anomalies = proposal
        .anomalies
        .into_iter()
        .map(|anomaly| BulkAdoptionDryRunAnomalyV1 {
            anomaly_id: anomaly.anomaly_id,
            code: anomaly.code,
            root_identity_hashes: anomaly.root_identity_hashes,
            revision_ids: anomaly.revision_ids,
        })
        .collect::<Vec<_>>();
    let material = serde_json::json!({
        "schema": BULK_ADOPTION_DRY_RUN_SCHEMA_V1,
        "roots": &roots,
        "anomalies": &anomalies,
        "requiresOwnerDecision": proposal.requires_owner_decision,
        "ownerDecisionManifestHash": &owner_decision_manifest_hash,
        "writer": &writer,
        "claimOccurredAt": &decisions.occurred_at,
        "signaturePolicy": "unsigned_change_claims_v1",
    });
    Ok(BulkAdoptionDryRunDocumentV1 {
        schema: BULK_ADOPTION_DRY_RUN_SCHEMA_V1.to_owned(),
        manifest_hash: sha256_json_prefixed(&material)?,
        roots,
        anomalies,
        requires_owner_decision: proposal.requires_owner_decision,
        owner_decision_manifest_hash,
        writer,
        claim_occurred_at: decisions.occurred_at,
        signature_policy: "unsigned_change_claims_v1".to_owned(),
    })
}

fn validate_owner_decisions(manifest: &BulkAdoptionOwnerDecisionManifestV1) -> Result<String> {
    if manifest.schema != BULK_ADOPTION_OWNER_DECISIONS_SCHEMA_V1
        || manifest.claim_occurred_at.trim().is_empty()
        || manifest
            .approved_anomaly_ids
            .iter()
            .any(|identity| !is_prefixed_sha256(identity))
    {
        return Err(invalid_migration(
            "owner decision manifest has an unsupported schema or invalid identity",
        ));
    }
    sha256_json_prefixed(&serde_json::to_value(manifest)?)
}

fn legacy_inventory_for_repo(repo: &Path) -> Result<LegacyRootInventoryV1> {
    let (_store, inspection) = resolve_change_read_store(repo)?;
    if !matches!(inspection.status, StoreCapabilityStatus::MigrationRequired) {
        return Err(invalid_migration(
            "bulk-adoption dry run requires an untouched L0 store",
        ));
    }
    let identity = crate::session::store_identity(StoreIdentityOptions::new(repo))?;
    let events = inspection
        .event_entries
        .into_iter()
        .map(|entry| {
            crate::session::EventStore::decode_qualification_entry(entry.key_digest, entry.bytes)
        })
        .collect::<Result<Vec<_>>>()?;
    let journal_id = events
        .first()
        .map(|event| event.target.journal_id.clone())
        .unwrap_or_else(|| JournalId::new("journal:default"));
    if events
        .iter()
        .any(|event| event.target.journal_id != journal_id)
    {
        return Err(invalid_migration(
            "legacy root contains more than one Journal identity",
        ));
    }
    let mut revisions = Vec::new();
    for event in &events {
        if event.event_type != EventType::WorkObjectProposed {
            continue;
        }
        let payload: WorkObjectProposedPayload = serde_json::from_value(event.payload.clone())?;
        if let WorkObjectProposal::Revision {
            revision,
            object_artifact_content_hash,
            supersedes,
            ..
        } = payload.work_object
        {
            revisions.push(LegacyRevisionV1 {
                revision: RevisionRefV1::new(revision.id, object_artifact_content_hash)?,
                legacy_group: payload.engagement_id.as_str().to_owned(),
                supersedes,
            });
        }
    }
    Ok(LegacyRootInventoryV1 {
        root_identity_hash: identity.store_identity,
        journal_id,
        source_authority_cursor: inspection.cursor,
        revisions,
    })
}

fn invalid_migration(reason: impl Into<String>) -> ShoreError {
    ShoreError::WorkflowInputInvalid {
        reason: reason.into(),
    }
}

fn minimum_reader_profile_v1() -> String {
    REVIEW_CHANGE_REVISION_COHORT_V1.to_owned()
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BulkAdoptionMigrationDispositionV1 {
    Created,
    Existing,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionMigrationReceiptV1 {
    pub schema: String,
    pub approved_dry_run_hash: String,
    pub cohort_manifest_hash: String,
    pub minimum_reader_profile: String,
    pub activation_id: String,
    pub completion_id: String,
    pub backup_manifest_hash: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub derived_generation_id: Option<String>,
    pub disposition: BulkAdoptionMigrationDispositionV1,
}

pub const BULK_ADOPTION_BACKUP_RESTORE_RECEIPT_SCHEMA_V1: &str =
    "pointbreak.bulk-adoption-backup-restore-receipt.v1";

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BulkAdoptionBackupRestoreDispositionV1 {
    Created,
    Existing,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BulkAdoptionBackupRestoreReceiptV1 {
    pub schema: String,
    pub backup_manifest_hash: String,
    pub restored_store_identity: String,
    pub restored_authority_cursor: AuthorityCursorV2,
    pub file_count: u64,
    pub total_bytes: u64,
    pub disposition: BulkAdoptionBackupRestoreDispositionV1,
}

pub struct BulkAdoptionMigrationOptions {
    repo: PathBuf,
    dry_run: BulkAdoptionDryRunDocumentV1,
    acknowledged_dry_run_hash: String,
    acknowledged_cohort_manifest_hash: String,
    backup_root: PathBuf,
    operation_id: String,
    minimum_reader_ack: Option<String>,
    legacy_reader_unsupported_ack: bool,
    owner_decisions: Option<BulkAdoptionOwnerDecisionManifestV1>,
    signer: Option<Box<dyn EventSigner + Send + Sync>>,
    fixed_occurred_at: Option<String>,
    derived_enabled: bool,
    interruption_after_append: Option<usize>,
}

impl BulkAdoptionMigrationOptions {
    pub fn new(
        repo: impl AsRef<Path>,
        dry_run: BulkAdoptionDryRunDocumentV1,
        acknowledged_dry_run_hash: impl Into<String>,
        acknowledged_cohort_manifest_hash: impl Into<String>,
        backup_root: impl AsRef<Path>,
        operation_id: impl Into<String>,
    ) -> Self {
        Self {
            repo: repo.as_ref().to_path_buf(),
            dry_run,
            acknowledged_dry_run_hash: acknowledged_dry_run_hash.into(),
            acknowledged_cohort_manifest_hash: acknowledged_cohort_manifest_hash.into(),
            backup_root: backup_root.as_ref().to_path_buf(),
            operation_id: operation_id.into(),
            minimum_reader_ack: None,
            legacy_reader_unsupported_ack: false,
            owner_decisions: None,
            signer: None,
            fixed_occurred_at: None,
            derived_enabled: true,
            interruption_after_append: None,
        }
    }

    pub fn with_minimum_reader_ack(mut self, profile: impl Into<String>) -> Self {
        self.minimum_reader_ack = Some(profile.into());
        self
    }

    pub fn with_legacy_reader_unsupported_ack(mut self) -> Self {
        self.legacy_reader_unsupported_ack = true;
        self
    }

    pub fn with_owner_decisions(mut self, decisions: BulkAdoptionOwnerDecisionManifestV1) -> Self {
        self.owner_decisions = Some(decisions);
        self
    }

    pub fn sign_with<S>(mut self, signer: S) -> Self
    where
        S: EventSigner + Send + Sync + 'static,
    {
        self.signer = Some(Box::new(signer));
        self
    }

    #[cfg(test)]
    pub(crate) fn with_fixed_occurred_at(mut self, occurred_at: impl Into<String>) -> Self {
        self.fixed_occurred_at = Some(occurred_at.into());
        self
    }

    pub fn with_derived_enabled(mut self, enabled: bool) -> Self {
        self.derived_enabled = enabled;
        self
    }

    #[cfg(test)]
    fn with_interruption_after_append(mut self, append: usize) -> Self {
        self.interruption_after_append = Some(append);
        self
    }
}

/// Restore a verified pre-activation backup into one empty, separately
/// identified repository store. This never rolls an activated store backward;
/// the restored L0 root has its own placement identity and must receive a fresh
/// dry run before any later activation.
pub fn restore_bulk_adoption_backup(
    backup_root: impl AsRef<Path>,
    target_repo: impl AsRef<Path>,
) -> Result<BulkAdoptionBackupRestoreReceiptV1> {
    let backup_root = backup_root.as_ref();
    let target_repo = target_repo.as_ref();
    let manifest = verify_backup(backup_root)?;
    let (target, _) = resolve_change_read_store(target_repo)?;
    let target_root = target.store_dir().to_path_buf();
    ensure_external_backup_path(&target_root, backup_root)?;
    let _authority = StoreAuthorityLock::acquire(&target_root)?;
    let (_, initial) = resolve_change_read_store(target_repo)?;
    let target_identity = crate::session::store_identity(StoreIdentityOptions::new(target_repo))?;
    if target_identity.store_identity == manifest.source_store_identity {
        return Err(invalid_migration(
            "backup restore requires a separately identified destination store",
        ));
    }
    if !matches!(initial.status, StoreCapabilityStatus::MigrationRequired) {
        return Err(invalid_migration(
            "backup restore requires an empty untouched L0 destination",
        ));
    }
    let existing = inventory_store_files(&target_root)?;
    let disposition = if existing == manifest.entries {
        BulkAdoptionBackupRestoreDispositionV1::Existing
    } else {
        if !existing.is_empty() {
            return Err(invalid_migration(
                "backup restore destination contains different durable store files",
            ));
        }
        let storage = LocalStorage::new(&target_root);
        for entry in &manifest.entries {
            let source = backup_root.join("store").join(&entry.path);
            let bytes = fs::read(&source).map_err(|error| {
                invalid_migration(format!(
                    "could not read verified backup file {}: {error}",
                    entry.path
                ))
            })?;
            storage.create_file_exclusive(Path::new(&entry.path), &bytes, Durability::Durable)?;
        }
        BulkAdoptionBackupRestoreDispositionV1::Created
    };
    let (_, restored) = resolve_change_read_store(target_repo)?;
    if !matches!(restored.status, StoreCapabilityStatus::MigrationRequired)
        || restored.cursor != manifest.source_authority_cursor
    {
        return Err(invalid_migration(
            "restored backup did not reproduce the verified L0 authority cursor",
        ));
    }
    Ok(BulkAdoptionBackupRestoreReceiptV1 {
        schema: BULK_ADOPTION_BACKUP_RESTORE_RECEIPT_SCHEMA_V1.to_owned(),
        backup_manifest_hash: manifest.manifest_hash,
        restored_store_identity: target_identity.store_identity,
        restored_authority_cursor: restored.cursor,
        file_count: manifest.entries.len() as u64,
        total_bytes: manifest.entries.iter().map(|entry| entry.bytes).sum(),
        disposition,
    })
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BulkAdoptionBackupEntryV1 {
    path: String,
    bytes: u64,
    sha256: String,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BulkAdoptionBackupManifestV1 {
    schema: String,
    source_store_identity: String,
    source_authority_cursor: AuthorityCursorV2,
    entries: Vec<BulkAdoptionBackupEntryV1>,
    manifest_hash: String,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct BulkAdoptionBackupReceiptV1 {
    schema: String,
    source_store_identity: String,
    source_authority_cursor: AuthorityCursorV2,
    manifest_hash: String,
    file_count: u64,
    total_bytes: u64,
}

pub fn migrate_bulk_adoption(
    options: BulkAdoptionMigrationOptions,
) -> Result<BulkAdoptionMigrationReceiptV1> {
    validate_migration_options(&options)?;
    let (resolved, _) = resolve_change_read_store(&options.repo)?;
    let store_root = resolved.store_dir().to_path_buf();
    ensure_external_backup_path(&store_root, &options.backup_root)?;
    let _authority = StoreAuthorityLock::acquire(&store_root)?;

    let (_, initial) = resolve_change_read_store(&options.repo)?;
    let current_identity =
        crate::session::store_identity(StoreIdentityOptions::new(&options.repo))?;
    let approved_root = approved_root_for_identity(&options, &current_identity.store_identity)?;
    if matches!(&initial.status, StoreCapabilityStatus::MigrationRequired) {
        if initial.cursor != approved_root.source_authority_cursor {
            return Err(invalid_migration(
                "current L0 authority does not match the approved dry-run cursor",
            ));
        }
        if !options
            .backup_root
            .join(BULK_ADOPTION_EXECUTION_PLAN_FILE_V1)
            .is_file()
            && options.signer.is_none()
        {
            return Err(invalid_migration(
                "initial activation requires an available strict signing key",
            ));
        }
    }
    let backup = match &initial.status {
        StoreCapabilityStatus::MigrationRequired => ensure_backup(
            &store_root,
            &options.backup_root,
            &approved_root.root_identity_hash,
            &initial.cursor,
        )?,
        StoreCapabilityStatus::MigrationInProgress { .. } | StoreCapabilityStatus::Ready { .. } => {
            verify_backup(&options.backup_root)?
        }
    };
    if backup.source_store_identity != approved_root.root_identity_hash
        || backup.source_authority_cursor != approved_root.source_authority_cursor
    {
        return Err(invalid_migration(
            "verified backup does not match the approved pre-activation authority",
        ));
    }

    let plan_path = options
        .backup_root
        .join(BULK_ADOPTION_EXECUTION_PLAN_FILE_V1);
    let plan = if plan_path.is_file() {
        let plan: BulkAdoptionExecutionPlanV1 =
            serde_json::from_slice(&fs::read(&plan_path).map_err(|error| {
                invalid_migration(format!("could not read retained migration plan: {error}"))
            })?)?;
        validate_retained_plan(&plan, &options, approved_root)?;
        plan
    } else {
        if !matches!(&initial.status, StoreCapabilityStatus::MigrationRequired) {
            return Err(invalid_migration(
                "M1/L2 recovery requires the retained pre-activation execution plan",
            ));
        }
        let signer = options.signer.as_ref().ok_or_else(|| {
            invalid_migration("initial activation requires an available strict signing key")
        })?;
        let plan = prepare_approved_bulk_adoption(&options, approved_root, signer.as_ref())?;
        let bytes = canonical_json_bytes(&serde_json::to_value(&plan)?)?;
        match LocalStorage::new(&options.backup_root).create_file_exclusive(
            Path::new(BULK_ADOPTION_EXECUTION_PLAN_FILE_V1),
            &bytes,
            Durability::Durable,
        )? {
            CreateOutcome::Created => {}
            CreateOutcome::AlreadyExists => {
                let retained = fs::read(&plan_path).map_err(|error| {
                    invalid_migration(format!("could not verify retained migration plan: {error}"))
                })?;
                if retained != bytes {
                    return Err(invalid_migration(
                        "retained migration plan conflicts with the approved execution plan",
                    ));
                }
            }
        }
        plan
    };

    let final_receipt_path = options
        .backup_root
        .join(BULK_ADOPTION_MIGRATION_RECEIPT_FILE_V1);
    if final_receipt_path.is_file() {
        if !matches!(&initial.status, StoreCapabilityStatus::Ready { .. }) {
            return Err(invalid_migration(
                "a retained completion receipt exists but the store is not L2",
            ));
        }
        execute_bulk_adoption_plan(&options.repo, &plan, None)?;
        let mut receipt: BulkAdoptionMigrationReceiptV1 =
            serde_json::from_slice(&fs::read(&final_receipt_path).map_err(|error| {
                invalid_migration(format!(
                    "could not read retained migration receipt: {error}"
                ))
            })?)?;
        validate_migration_receipt(&receipt, &plan, &backup)?;
        receipt.disposition = BulkAdoptionMigrationDispositionV1::Existing;
        return Ok(receipt);
    }

    execute_bulk_adoption_plan(&options.repo, &plan, options.interruption_after_append)?;
    let derived_generation_id = if options.derived_enabled {
        // Publish through the product lifecycle so the descriptor, SQLite
        // database, locator, and store identity form one validated generation.
        // The bodyless Change generator is qualification-only and cannot stand
        // in for this complete post-L2 product generation.
        let access =
            crate::session::derived_access::history::DerivedHistoryAccess::resolve(&options.repo)
                .map_err(ShoreError::Message)?;
        access
            .build(|_| crate::session::derived_access::history::DerivedHistoryControl::Continue)
            .map_err(ShoreError::Message)?
            .generation_id
    } else {
        None
    };
    let receipt = BulkAdoptionMigrationReceiptV1 {
        schema: BULK_ADOPTION_MIGRATION_RECEIPT_SCHEMA_V1.to_owned(),
        approved_dry_run_hash: plan.approved_dry_run_hash.clone(),
        cohort_manifest_hash: plan.manifest_hash.clone(),
        minimum_reader_profile: plan.minimum_reader_profile.clone(),
        activation_id: plan.activation.activation_id().to_owned(),
        completion_id: plan.completion.completion_id().to_owned(),
        backup_manifest_hash: backup.manifest_hash,
        derived_generation_id,
        disposition: BulkAdoptionMigrationDispositionV1::Created,
    };
    let receipt_bytes = canonical_json_bytes(&serde_json::to_value(&receipt)?)?;
    let storage = LocalStorage::new(&options.backup_root);
    match storage.create_file_exclusive(
        Path::new(BULK_ADOPTION_MIGRATION_RECEIPT_FILE_V1),
        &receipt_bytes,
        Durability::Durable,
    )? {
        CreateOutcome::Created => {}
        CreateOutcome::AlreadyExists => {
            if storage.read_bytes(Path::new(BULK_ADOPTION_MIGRATION_RECEIPT_FILE_V1))?
                != receipt_bytes
            {
                return Err(invalid_migration(
                    "retained migration completion receipt conflicts with this execution",
                ));
            }
        }
    }
    Ok(receipt)
}

fn validate_migration_receipt(
    receipt: &BulkAdoptionMigrationReceiptV1,
    plan: &BulkAdoptionExecutionPlanV1,
    backup: &BulkAdoptionBackupManifestV1,
) -> Result<()> {
    if receipt.schema != BULK_ADOPTION_MIGRATION_RECEIPT_SCHEMA_V1
        || receipt.approved_dry_run_hash != plan.approved_dry_run_hash
        || receipt.cohort_manifest_hash != plan.manifest_hash
        || receipt.minimum_reader_profile != plan.minimum_reader_profile
        || receipt.activation_id != plan.activation.activation_id()
        || receipt.completion_id != plan.completion.completion_id()
        || receipt.backup_manifest_hash != backup.manifest_hash
        || receipt.disposition != BulkAdoptionMigrationDispositionV1::Created
    {
        return Err(invalid_migration(
            "retained migration receipt does not match the completed execution plan",
        ));
    }
    Ok(())
}

fn validate_migration_options(options: &BulkAdoptionMigrationOptions) -> Result<()> {
    validate_dry_run_document(&options.dry_run)?;
    if options.acknowledged_dry_run_hash != options.dry_run.manifest_hash {
        return Err(invalid_migration(
            "acknowledged dry-run hash does not match the exact dry-run document",
        ));
    }
    if options.dry_run.requires_owner_decision {
        return Err(invalid_migration(
            "approved dry run still requires an explicit owner decision",
        ));
    }
    if options.operation_id.trim().is_empty() {
        return Err(invalid_migration("migration operation id cannot be empty"));
    }
    if options.minimum_reader_ack.as_deref() != Some(REVIEW_CHANGE_REVISION_COHORT_V1) {
        return Err(invalid_migration(format!(
            "minimum reader acknowledgement must be {REVIEW_CHANGE_REVISION_COHORT_V1}",
        )));
    }
    if !options.legacy_reader_unsupported_ack {
        return Err(invalid_migration(
            "activation requires explicit acknowledgement that v0.9 readers become unsupported",
        ));
    }
    Ok(())
}

fn validate_dry_run_document(document: &BulkAdoptionDryRunDocumentV1) -> Result<()> {
    if document.schema != BULK_ADOPTION_DRY_RUN_SCHEMA_V1 {
        return Err(invalid_migration(
            "unsupported bulk-adoption dry-run schema",
        ));
    }
    if document.roots.is_empty()
        || document
            .roots
            .windows(2)
            .any(|pair| pair[0].root_identity_hash >= pair[1].root_identity_hash)
    {
        return Err(invalid_migration(
            "bulk-adoption dry run has a non-canonical root set",
        ));
    }
    let material = serde_json::json!({
        "schema": document.schema,
        "roots": document.roots,
        "anomalies": document.anomalies,
        "requiresOwnerDecision": document.requires_owner_decision,
        "ownerDecisionManifestHash": document.owner_decision_manifest_hash,
        "writer": document.writer,
        "claimOccurredAt": document.claim_occurred_at,
        "signaturePolicy": document.signature_policy,
    });
    if sha256_json_prefixed(&material)? != document.manifest_hash {
        return Err(invalid_migration(
            "bulk-adoption dry-run self-hash mismatch",
        ));
    }
    Ok(())
}

fn prepare_approved_bulk_adoption(
    options: &BulkAdoptionMigrationOptions,
    approved_root: &BulkAdoptionDryRunRootV1,
    signer: &(dyn EventSigner + Send + Sync),
) -> Result<BulkAdoptionExecutionPlanV1> {
    let inventory = legacy_inventory_for_repo(&options.repo)?;
    if inventory.root_identity_hash != approved_root.root_identity_hash
        || inventory.source_authority_cursor != approved_root.source_authority_cursor
    {
        return Err(invalid_migration(
            "current L0 authority does not match the approved dry-run cursor",
        ));
    }
    let decisions = decisions_for_execution(options)?;
    let retained = options
        .owner_decisions
        .as_ref()
        .map(|manifest| {
            manifest
                .retained_manifests
                .iter()
                .map(|retained| RetainedAdoptionManifestV1 {
                    manifest_hash: retained.manifest_hash.clone(),
                    allocations: retained
                        .allocations
                        .iter()
                        .map(|allocation| RetainedChangeAllocationV1 {
                            change_id: allocation.change_id.clone(),
                            identity_descriptor: allocation.identity_descriptor.clone(),
                            members: allocation.members.clone(),
                        })
                        .collect(),
                })
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();
    let proposal = plan_bulk_adoption(std::slice::from_ref(&inventory), &retained, &decisions)?;
    if proposal.requires_owner_decision {
        return Err(invalid_migration(
            "exact activation re-plan still requires an owner decision",
        ));
    }
    let root = proposal
        .roots
        .into_iter()
        .next()
        .ok_or_else(|| invalid_migration("approved activation has no root"))?;
    let manifest = root
        .manifest
        .ok_or_else(|| invalid_migration("approved activation has no executable manifest"))?;
    let manifest_hash = manifest.canonical_hash()?;
    if manifest_hash != options.acknowledged_cohort_manifest_hash {
        return Err(invalid_migration(
            "exact activation re-plan changed the acknowledged cohort manifest",
        ));
    }
    let occurred_at = options
        .fixed_occurred_at
        .clone()
        .unwrap_or_else(crate::session::current_timestamp);
    let nonce = sha256_json_prefixed(&serde_json::json!({
        "operationId": options.operation_id,
        "rootIdentityHash": inventory.root_identity_hash,
        "approvedDryRunHash": options.acknowledged_dry_run_hash,
    }))?;
    let activation = build_signed_activation(
        signer,
        manifest,
        nonce,
        options.dry_run.writer.clone(),
        occurred_at.clone(),
    )?;
    let completion = build_signed_completion(
        signer,
        &activation,
        options.dry_run.writer.clone(),
        occurred_at,
    )?;
    Ok(BulkAdoptionExecutionPlanV1 {
        schema: BULK_ADOPTION_EXECUTION_PLAN_SCHEMA_V1.to_owned(),
        operation_id: options.operation_id.clone(),
        approved_dry_run_hash: options.acknowledged_dry_run_hash.clone(),
        minimum_reader_profile: REVIEW_CHANGE_REVISION_COHORT_V1.to_owned(),
        root_identity_hash: inventory.root_identity_hash,
        manifest_hash,
        source_authority_cursor: inventory.source_authority_cursor,
        events: root.planned_events,
        activation,
        completion,
    })
}

fn decisions_for_execution(
    options: &BulkAdoptionMigrationOptions,
) -> Result<OwnerManifestDecisionsV1> {
    let Some(manifest) = options.owner_decisions.as_ref() else {
        return Ok(OwnerManifestDecisionsV1 {
            writer: options.dry_run.writer.clone(),
            occurred_at: options.dry_run.claim_occurred_at.clone(),
            ..OwnerManifestDecisionsV1::default()
        });
    };
    let hash = validate_owner_decisions(manifest)?;
    if options.dry_run.owner_decision_manifest_hash.as_deref() != Some(hash.as_str()) {
        return Err(invalid_migration(
            "owner decisions do not match the approved dry-run document",
        ));
    }
    Ok(OwnerManifestDecisionsV1 {
        approved_anomaly_ids: manifest.approved_anomaly_ids.clone(),
        overlap_identity: manifest
            .overlap_identity
            .iter()
            .map(|(revision, decision)| {
                let decision = match decision {
                    BulkAdoptionOverlapIdentityDecisionV1::Shared {
                        identity_descriptor,
                    } => OverlapIdentityDecisionV1::Shared {
                        identity_descriptor: identity_descriptor.clone(),
                    },
                    BulkAdoptionOverlapIdentityDecisionV1::Distinct => {
                        OverlapIdentityDecisionV1::Distinct
                    }
                };
                (revision.clone(), decision)
            })
            .collect(),
        writer: options.dry_run.writer.clone(),
        occurred_at: manifest.claim_occurred_at.clone(),
    })
}

fn validate_retained_plan(
    plan: &BulkAdoptionExecutionPlanV1,
    options: &BulkAdoptionMigrationOptions,
    root: &BulkAdoptionDryRunRootV1,
) -> Result<()> {
    validate_execution_plan(plan)?;
    if plan.operation_id != options.operation_id
        || plan.approved_dry_run_hash != options.acknowledged_dry_run_hash
        || plan.root_identity_hash != root.root_identity_hash
        || plan.manifest_hash != options.acknowledged_cohort_manifest_hash
        || plan.source_authority_cursor != root.source_authority_cursor
    {
        return Err(invalid_migration(
            "retained execution plan does not match the approved migration inputs",
        ));
    }
    Ok(())
}

fn validate_execution_plan(plan: &BulkAdoptionExecutionPlanV1) -> Result<()> {
    if plan.schema != BULK_ADOPTION_EXECUTION_PLAN_SCHEMA_V1
        || plan.minimum_reader_profile != REVIEW_CHANGE_REVISION_COHORT_V1
    {
        return Err(invalid_migration(
            "unsupported bulk-adoption execution plan",
        ));
    }
    plan.activation.validate_for_execution()?;
    plan.completion.validate_for_execution()?;
    let manifest = plan.activation.manifest();
    if manifest.canonical_hash()? != plan.manifest_hash
        || manifest.source_authority_cursor != plan.source_authority_cursor
    {
        return Err(invalid_migration(
            "execution plan control authority does not match its frozen manifest",
        ));
    }
    let mut reservations = plan
        .events
        .iter()
        .map(reserved_record)
        .collect::<Result<Vec<_>>>()?;
    reservations.sort_by(|left, right| left.logical_key.cmp(&right.logical_key));
    if reservations != manifest.reserved_records {
        return Err(invalid_migration(
            "execution plan event bytes do not match the signed reservation manifest",
        ));
    }
    Ok(())
}

fn approved_root_for_identity<'a>(
    options: &'a BulkAdoptionMigrationOptions,
    store_identity: &str,
) -> Result<&'a BulkAdoptionDryRunRootV1> {
    let root = options
        .dry_run
        .roots
        .iter()
        .find(|root| root.root_identity_hash == store_identity)
        .ok_or_else(|| {
            invalid_migration("resolved store is absent from the approved dry-run document")
        })?;
    if root.cohort_manifest_hash.as_deref()
        != Some(options.acknowledged_cohort_manifest_hash.as_str())
    {
        return Err(invalid_migration(
            "acknowledged cohort manifest does not match the approved root",
        ));
    }
    Ok(root)
}

fn ensure_external_backup_path(store_root: &Path, backup_root: &Path) -> Result<()> {
    let store = absolute_path(store_root)?;
    let backup = absolute_path(backup_root)?;
    if backup.starts_with(&store) || store.starts_with(&backup) {
        return Err(invalid_migration(
            "migration backup must be outside the authoritative store root",
        ));
    }
    Ok(())
}

fn absolute_path(path: &Path) -> Result<PathBuf> {
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir()
            .map_err(|error| {
                invalid_migration(format!("could not resolve current directory: {error}"))
            })?
            .join(path)
    };
    let mut existing = absolute.as_path();
    let mut suffix = Vec::new();
    while !existing.try_exists().map_err(|error| {
        invalid_migration(format!(
            "could not inspect migration path boundary: {error}"
        ))
    })? {
        suffix.push(
            existing
                .file_name()
                .ok_or_else(|| invalid_migration("migration path has no existing ancestor"))?
                .to_os_string(),
        );
        existing = existing
            .parent()
            .ok_or_else(|| invalid_migration("migration path has no existing ancestor"))?;
    }
    let mut resolved = fs::canonicalize(existing).map_err(|error| {
        invalid_migration(format!(
            "could not canonicalize migration path boundary: {error}"
        ))
    })?;
    for component in suffix.into_iter().rev() {
        resolved.push(component);
    }
    Ok(resolved)
}

fn ensure_backup(
    store_root: &Path,
    backup_root: &Path,
    store_identity: &str,
    source_cursor: &AuthorityCursorV2,
) -> Result<BulkAdoptionBackupManifestV1> {
    if backup_root
        .join(BULK_ADOPTION_BACKUP_RECEIPT_FILE_V1)
        .is_file()
    {
        let manifest = verify_backup(backup_root)?;
        if manifest.source_store_identity != store_identity
            || manifest.source_authority_cursor != *source_cursor
        {
            return Err(invalid_migration(
                "existing backup belongs to a different source authority",
            ));
        }
        return Ok(manifest);
    }
    fs::create_dir_all(backup_root.join("store")).map_err(|error| {
        invalid_migration(format!(
            "could not create external backup directory: {error}"
        ))
    })?;
    let before = inventory_store_files(store_root)?;
    validate_partial_backup(backup_root, &before)?;
    let storage = LocalStorage::new(backup_root);
    for entry in &before {
        let bytes = fs::read(store_root.join(&entry.path)).map_err(|error| {
            invalid_migration(format!(
                "could not read source backup file {}: {error}",
                entry.path
            ))
        })?;
        let target = Path::new("store").join(&entry.path);
        match storage.create_file_exclusive(&target, &bytes, Durability::Durable)? {
            CreateOutcome::Created => {}
            CreateOutcome::AlreadyExists => {
                if storage.read_bytes(&target)? != bytes {
                    return Err(invalid_migration(format!(
                        "backup file conflicts with source authority: {}",
                        entry.path
                    )));
                }
            }
        }
    }
    let after = inventory_store_files(store_root)?;
    if before != after {
        return Err(invalid_migration(
            "source authority changed while the external backup was being written",
        ));
    }
    let material = serde_json::json!({
        "schema": BULK_ADOPTION_BACKUP_MANIFEST_SCHEMA_V1,
        "sourceStoreIdentity": store_identity,
        "sourceAuthorityCursor": source_cursor,
        "entries": before,
    });
    let manifest = BulkAdoptionBackupManifestV1 {
        schema: BULK_ADOPTION_BACKUP_MANIFEST_SCHEMA_V1.to_owned(),
        source_store_identity: store_identity.to_owned(),
        source_authority_cursor: source_cursor.clone(),
        entries: before,
        manifest_hash: sha256_json_prefixed(&material)?,
    };
    let manifest_bytes = canonical_json_bytes(&serde_json::to_value(&manifest)?)?;
    match storage.create_file_exclusive(
        Path::new(BULK_ADOPTION_BACKUP_MANIFEST_FILE_V1),
        &manifest_bytes,
        Durability::Durable,
    )? {
        CreateOutcome::Created => {}
        CreateOutcome::AlreadyExists => {
            if storage.read_bytes(Path::new(BULK_ADOPTION_BACKUP_MANIFEST_FILE_V1))?
                != manifest_bytes
            {
                return Err(invalid_migration(
                    "partial backup carries a conflicting manifest",
                ));
            }
        }
    }
    let receipt = BulkAdoptionBackupReceiptV1 {
        schema: BULK_ADOPTION_BACKUP_RECEIPT_SCHEMA_V1.to_owned(),
        source_store_identity: store_identity.to_owned(),
        source_authority_cursor: source_cursor.clone(),
        manifest_hash: manifest.manifest_hash.clone(),
        file_count: manifest.entries.len() as u64,
        total_bytes: manifest.entries.iter().map(|entry| entry.bytes).sum(),
    };
    let receipt_bytes = canonical_json_bytes(&serde_json::to_value(receipt)?)?;
    storage.create_file_exclusive(
        Path::new(BULK_ADOPTION_BACKUP_RECEIPT_FILE_V1),
        &receipt_bytes,
        Durability::Durable,
    )?;
    Ok(manifest)
}

fn validate_partial_backup(
    backup_root: &Path,
    source_entries: &[BulkAdoptionBackupEntryV1],
) -> Result<()> {
    let entries = fs::read_dir(backup_root)
        .map_err(|error| invalid_migration(format!("could not inspect partial backup: {error}")))?
        .collect::<std::result::Result<Vec<_>, _>>()
        .map_err(|error| invalid_migration(format!("could not inspect partial backup: {error}")))?;
    for entry in entries {
        let name = entry.file_name();
        let kind = entry.file_type().map_err(|error| {
            invalid_migration(format!("could not inspect partial backup entry: {error}"))
        })?;
        let allowed = (name == "store" && kind.is_dir())
            || (name == BULK_ADOPTION_BACKUP_MANIFEST_FILE_V1 && kind.is_file())
            || backup_path_is_disposable(Path::new(&name));
        if !allowed {
            return Err(invalid_migration(format!(
                "incomplete backup contains an unexpected entry: {}",
                entry.path().display()
            )));
        }
    }
    for existing in inventory_store_files(&backup_root.join("store"))? {
        if !source_entries.iter().any(|source| source == &existing) {
            return Err(invalid_migration(format!(
                "partial backup conflicts with current source authority: {}",
                existing.path
            )));
        }
    }
    Ok(())
}

fn verify_backup(backup_root: &Path) -> Result<BulkAdoptionBackupManifestV1> {
    let manifest: BulkAdoptionBackupManifestV1 = serde_json::from_slice(
        &fs::read(backup_root.join(BULK_ADOPTION_BACKUP_MANIFEST_FILE_V1)).map_err(|error| {
            invalid_migration(format!("could not read backup manifest: {error}"))
        })?,
    )?;
    let receipt: BulkAdoptionBackupReceiptV1 = serde_json::from_slice(
        &fs::read(backup_root.join(BULK_ADOPTION_BACKUP_RECEIPT_FILE_V1)).map_err(|error| {
            invalid_migration(format!("could not read complete backup receipt: {error}"))
        })?,
    )?;
    if manifest.schema != BULK_ADOPTION_BACKUP_MANIFEST_SCHEMA_V1
        || receipt.schema != BULK_ADOPTION_BACKUP_RECEIPT_SCHEMA_V1
        || receipt.source_store_identity != manifest.source_store_identity
        || receipt.source_authority_cursor != manifest.source_authority_cursor
        || receipt.manifest_hash != manifest.manifest_hash
        || receipt.file_count != manifest.entries.len() as u64
        || receipt.total_bytes
            != manifest
                .entries
                .iter()
                .map(|entry| entry.bytes)
                .sum::<u64>()
    {
        return Err(invalid_migration("backup manifest/receipt mismatch"));
    }
    if manifest
        .entries
        .windows(2)
        .any(|pair| pair[0].path >= pair[1].path)
        || manifest
            .entries
            .iter()
            .any(|entry| !backup_entry_path_is_safe(&entry.path))
    {
        return Err(invalid_migration(
            "backup manifest contains a non-canonical or unsafe relative path",
        ));
    }
    let material = serde_json::json!({
        "schema": manifest.schema,
        "sourceStoreIdentity": manifest.source_store_identity,
        "sourceAuthorityCursor": manifest.source_authority_cursor,
        "entries": manifest.entries,
    });
    if sha256_json_prefixed(&material)? != manifest.manifest_hash {
        return Err(invalid_migration("backup manifest self-hash mismatch"));
    }
    for entry in &manifest.entries {
        let bytes = fs::read(backup_root.join("store").join(&entry.path)).map_err(|error| {
            invalid_migration(format!(
                "could not read retained backup file {}: {error}",
                entry.path
            ))
        })?;
        if bytes.len() as u64 != entry.bytes || sha256_bytes_hex(&bytes) != entry.sha256 {
            return Err(invalid_migration(format!(
                "retained backup file failed verification: {}",
                entry.path
            )));
        }
    }
    Ok(manifest)
}

fn backup_entry_path_is_safe(value: &str) -> bool {
    let path = Path::new(value);
    !path.is_absolute()
        && !value.is_empty()
        && path
            .components()
            .all(|component| matches!(component, std::path::Component::Normal(_)))
        && !backup_path_is_disposable(path)
}

fn inventory_store_files(store_root: &Path) -> Result<Vec<BulkAdoptionBackupEntryV1>> {
    let mut paths = Vec::new();
    collect_store_files(store_root, store_root, &mut paths)?;
    paths.sort();
    let mut entries = Vec::with_capacity(paths.len());
    for path in paths {
        let relative = path
            .strip_prefix(store_root)
            .map_err(|_| invalid_migration("backup inventory escaped the source store"))?;
        let relative = relative.to_str().ok_or_else(|| {
            invalid_migration("backup inventory requires Unicode store-relative paths")
        })?;
        let relative = relative.replace('\\', "/");
        let bytes = fs::read(&path).map_err(|error| {
            invalid_migration(format!(
                "could not inventory source file {relative}: {error}"
            ))
        })?;
        entries.push(BulkAdoptionBackupEntryV1 {
            path: relative,
            bytes: bytes.len() as u64,
            sha256: sha256_bytes_hex(&bytes),
        });
    }
    Ok(entries)
}

fn collect_store_files(root: &Path, directory: &Path, paths: &mut Vec<PathBuf>) -> Result<()> {
    let mut entries = fs::read_dir(directory)
        .map_err(|error| {
            invalid_migration(format!("could not inventory store directory: {error}"))
        })?
        .collect::<std::result::Result<Vec<_>, _>>()
        .map_err(|error| invalid_migration(format!("could not inventory store entry: {error}")))?;
    entries.sort_by_key(std::fs::DirEntry::file_name);
    for entry in entries {
        let path = entry.path();
        let relative = path
            .strip_prefix(root)
            .map_err(|_| invalid_migration("backup inventory escaped the source store"))?;
        if backup_path_is_disposable(relative) {
            continue;
        }
        let metadata = fs::symlink_metadata(&path).map_err(|error| {
            invalid_migration(format!("could not inspect backup source entry: {error}"))
        })?;
        if metadata.file_type().is_symlink() {
            return Err(invalid_migration(format!(
                "refusing symlink in authoritative backup inventory: {}",
                relative.display()
            )));
        }
        if metadata.is_dir() {
            collect_store_files(root, &path, paths)?;
        } else if metadata.is_file() {
            paths.push(path);
        } else {
            return Err(invalid_migration(format!(
                "refusing non-file store entry in backup inventory: {}",
                relative.display()
            )));
        }
    }
    Ok(())
}

fn backup_path_is_disposable(path: &Path) -> bool {
    let first = path
        .components()
        .next()
        .and_then(|component| component.as_os_str().to_str());
    let name = path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or_default();
    first == Some("derived")
        || name == STORE_AUTHORITY_LOCK_FILE
        || name == "derived.writer.lock"
        || name == "derived.rebuild.lock"
        || (name.starts_with(".shore-write.") && name.ends_with(".tmp"))
}

/// Fully signed, exact execution input retained outside the authoritative
/// store before activation. M1 recovery reads this document and never attempts
/// to reconstruct an L0 graph from partially migrated authority.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub(crate) struct BulkAdoptionExecutionPlanV1 {
    schema: String,
    #[serde(default)]
    operation_id: String,
    #[serde(default)]
    approved_dry_run_hash: String,
    #[serde(default = "minimum_reader_profile_v1")]
    minimum_reader_profile: String,
    root_identity_hash: String,
    manifest_hash: String,
    source_authority_cursor: AuthorityCursorV2,
    events: Vec<ShoreEvent>,
    activation: StoreCapabilityActivationV1,
    completion: BulkAdoptionCompletionV1,
}

#[allow(
    clippy::too_many_arguments,
    dead_code,
    reason = "the frozen qualification plan remains available to the feature-built evidence harness"
)]
#[cfg(any(test, feature = "bench"))]
pub(crate) fn prepare_bulk_adoption_for_qualification(
    repo: &Path,
    acknowledged_manifest_hash: &str,
    writer: Writer,
    claim_occurred_at: String,
    activation_nonce: String,
    activation_occurred_at: String,
    completion_occurred_at: String,
    signer: &impl EventSigner,
) -> Result<BulkAdoptionExecutionPlanV1> {
    let inventory = legacy_inventory_for_repo(repo)?;
    let decisions = OwnerManifestDecisionsV1 {
        writer: writer.clone(),
        occurred_at: claim_occurred_at,
        ..OwnerManifestDecisionsV1::default()
    };
    let proposal = plan_bulk_adoption(std::slice::from_ref(&inventory), &[], &decisions)?;
    if proposal.requires_owner_decision {
        return Err(invalid_migration(
            "bulk-adoption proposal still requires explicit anomaly decisions",
        ));
    }
    let root = proposal
        .roots
        .into_iter()
        .next()
        .ok_or_else(|| invalid_migration("bulk-adoption proposal has no root"))?;
    let manifest = root
        .manifest
        .ok_or_else(|| invalid_migration("bulk-adoption proposal has no executable manifest"))?;
    let manifest_hash = manifest.canonical_hash()?;
    if manifest_hash != acknowledged_manifest_hash {
        return Err(invalid_migration(
            "acknowledged bulk-adoption manifest hash does not match the current L0 authority",
        ));
    }
    let activation = build_signed_activation(
        signer,
        manifest,
        activation_nonce,
        writer.clone(),
        activation_occurred_at,
    )?;
    let completion = build_signed_completion(signer, &activation, writer, completion_occurred_at)?;
    Ok(BulkAdoptionExecutionPlanV1 {
        schema: BULK_ADOPTION_EXECUTION_PLAN_SCHEMA_V1.to_owned(),
        operation_id: String::new(),
        approved_dry_run_hash: String::new(),
        minimum_reader_profile: REVIEW_CHANGE_REVISION_COHORT_V1.to_owned(),
        root_identity_hash: inventory.root_identity_hash,
        manifest_hash,
        source_authority_cursor: inventory.source_authority_cursor,
        events: root.planned_events,
        activation,
        completion,
    })
}

/// Execute a previously frozen migration plan against one resolved store.
/// `interrupt_after_append` counts activation, each Change event, then
/// completion. Retrying the same plan converges through exclusive-create
/// records and validates the exact M1/L2 authority after every phase.
fn execute_bulk_adoption_plan(
    repo: &Path,
    plan: &BulkAdoptionExecutionPlanV1,
    interrupt_after_append: Option<usize>,
) -> Result<()> {
    validate_execution_plan(plan)?;
    let identity = crate::session::store_identity(StoreIdentityOptions::new(repo))?;
    if identity.store_identity != plan.root_identity_hash {
        return Err(invalid_migration(
            "bulk-adoption execution plan names a different resolved store",
        ));
    }
    let (store, inspection) = resolve_change_read_store(repo)?;
    let journal = store.backend().journal();
    match &inspection.status {
        StoreCapabilityStatus::MigrationRequired => {
            if inspection.cursor != plan.source_authority_cursor {
                return Err(invalid_migration(
                    "L0 authority changed after the migration plan was frozen",
                ));
            }
            publish_control_record(
                journal.as_ref(),
                &plan.activation.logical_key(),
                &plan.activation,
            )?;
        }
        StoreCapabilityStatus::MigrationInProgress {
            activation_id,
            manifest_hash,
        }
        | StoreCapabilityStatus::Ready {
            activation_id,
            manifest_hash,
            ..
        } if activation_id == plan.activation.activation_id()
            && manifest_hash == plan.activation.manifest_hash() => {}
        _ => {
            return Err(invalid_migration(
                "store capability state does not match the frozen migration plan",
            ));
        }
    }
    if interrupt_after_append == Some(1) {
        return Err(invalid_migration(
            "injected migration interruption after append 1",
        ));
    }

    let after_activation = inspect_journal_records(journal.as_ref())?;
    if !matches!(
        after_activation.status,
        StoreCapabilityStatus::MigrationInProgress { .. } | StoreCapabilityStatus::Ready { .. }
    ) {
        return Err(invalid_migration(
            "migration activation did not produce a validated M1 authority",
        ));
    }
    for (index, event) in plan.events.iter().enumerate() {
        let bytes = serde_json::to_vec(event)?;
        journal.create_record_once(&event.idempotency_key, &bytes)?;
        if interrupt_after_append == Some(index + 2) {
            return Err(invalid_migration(format!(
                "injected migration interruption after append {}",
                index + 2
            )));
        }
    }
    let completion_append = plan.events.len() + 2;
    publish_control_record(
        journal.as_ref(),
        &plan.completion.logical_key(),
        &plan.completion,
    )?;
    if interrupt_after_append == Some(completion_append) {
        return Err(invalid_migration(format!(
            "injected migration interruption after append {completion_append}"
        )));
    }
    let complete = inspect_journal_records(journal.as_ref())?;
    match complete.status {
        StoreCapabilityStatus::Ready {
            activation_id,
            manifest_hash,
            completion_id,
        } if activation_id == plan.activation.activation_id()
            && manifest_hash == plan.manifest_hash
            && completion_id == plan.completion.completion_id() =>
        {
            Ok(())
        }
        _ => Err(invalid_migration(
            "bulk-adoption completion did not produce the frozen L2 authority",
        )),
    }
}

fn plan_bulk_adoption(
    roots: &[LegacyRootInventoryV1],
    retained_manifests: &[RetainedAdoptionManifestV1],
    decisions: &OwnerManifestDecisionsV1,
) -> Result<BulkAdoptionProposalV1> {
    let mut roots = roots.to_vec();
    roots.sort_by(|left, right| left.root_identity_hash.cmp(&right.root_identity_hash));
    for root in &mut roots {
        for revision in &mut root.revisions {
            revision.supersedes.sort();
            revision.supersedes.dedup();
        }
        root.revisions.sort_by(|left, right| {
            (
                &left.revision.revision_id,
                &left.revision.object_artifact_content_hash,
                &left.legacy_group,
                &left.supersedes,
            )
                .cmp(&(
                    &right.revision.revision_id,
                    &right.revision.object_artifact_content_hash,
                    &right.legacy_group,
                    &right.supersedes,
                ))
        });
    }
    let mut retained_manifests = retained_manifests.to_vec();
    retained_manifests.sort_by(|left, right| left.manifest_hash.cmp(&right.manifest_hash));
    for manifest in &mut retained_manifests {
        if !is_prefixed_sha256(&manifest.manifest_hash) {
            return Err(crate::error::ShoreError::Message(
                "retained adoption manifest requires a prefixed SHA-256 identity".to_owned(),
            ));
        }
        manifest.allocations.sort_by(|left, right| {
            (&left.change_id, &left.members).cmp(&(&right.change_id, &right.members))
        });
        for allocation in &manifest.allocations {
            if crate::model::derive_change_id(&allocation.identity_descriptor)?
                != allocation.change_id
            {
                return Err(crate::error::ShoreError::Message(
                    "retained adoption allocation has a mismatched Change identity".to_owned(),
                ));
            }
        }
    }

    let mut anomalies = Vec::new();
    let mut locations: BTreeMap<RevisionId, BTreeSet<String>> = BTreeMap::new();
    let mut root_revisions: BTreeMap<
        RevisionId,
        Vec<(String, RevisionRefV1, BTreeSet<RevisionId>)>,
    > = BTreeMap::new();
    let mut retained_allocations: BTreeMap<RevisionId, Vec<RetainedChangeAllocationV1>> =
        BTreeMap::new();
    for root in &roots {
        for revision in &root.revisions {
            locations
                .entry(revision.revision.revision_id.clone())
                .or_default()
                .insert(root.root_identity_hash.clone());
            root_revisions
                .entry(revision.revision.revision_id.clone())
                .or_default()
                .push((
                    root.root_identity_hash.clone(),
                    revision.revision.clone(),
                    revision.supersedes.iter().cloned().collect(),
                ));
        }
    }
    for manifest in &retained_manifests {
        let source = format!("retained-manifest:{}", manifest.manifest_hash);
        for allocation in &manifest.allocations {
            for revision_id in &allocation.members {
                locations
                    .entry(revision_id.clone())
                    .or_default()
                    .insert(source.clone());
                retained_allocations
                    .entry(revision_id.clone())
                    .or_default()
                    .push(allocation.clone());
            }
        }
    }
    for (revision_id, root_hashes) in locations.iter().filter(|(_, roots)| roots.len() > 1) {
        anomalies.push(anomaly(
            "cross_root_revision_overlap",
            root_hashes.clone(),
            [revision_id.clone()].into(),
        )?);
        let occurrences = root_revisions
            .get(revision_id)
            .map(Vec::as_slice)
            .unwrap_or_default();
        let artifact_hashes: BTreeSet<_> = occurrences
            .iter()
            .map(|(_, revision, _)| revision.object_artifact_content_hash.clone())
            .collect();
        if artifact_hashes.len() > 1 {
            anomalies.push(anomaly(
                "cross_root_revision_disagreement",
                root_hashes.clone(),
                [revision_id.clone()].into(),
            )?);
        }
        let relation_sets: BTreeSet<_> = occurrences
            .iter()
            .map(|(_, _, supersedes)| supersedes.clone())
            .collect();
        if relation_sets.len() > 1 {
            let mut implicated = [revision_id.clone()].into_iter().collect::<BTreeSet<_>>();
            implicated.extend(
                occurrences
                    .iter()
                    .flat_map(|(_, _, supersedes)| supersedes.iter().cloned()),
            );
            anomalies.push(anomaly(
                "cross_root_relation_disagreement",
                root_hashes.clone(),
                implicated,
            )?);
        }
        if let Some(OverlapIdentityDecisionV1::Shared {
            identity_descriptor,
        }) = decisions.overlap_identity.get(revision_id)
            && retained_allocations
                .get(revision_id)
                .is_some_and(|allocations| {
                    !allocations
                        .iter()
                        .any(|allocation| allocation.identity_descriptor == *identity_descriptor)
                })
        {
            anomalies.push(anomaly(
                "retained_identity_mismatch",
                root_hashes.clone(),
                [revision_id.clone()].into(),
            )?);
        }
    }

    let mut root_components = Vec::new();
    for root in &roots {
        let mut by_id = BTreeMap::new();
        for revision in &root.revisions {
            if let Some(existing) =
                by_id.insert(revision.revision.revision_id.clone(), revision.clone())
                && existing.revision != revision.revision
            {
                anomalies.push(anomaly(
                    "revision_identity_collision",
                    [root.root_identity_hash.clone()].into(),
                    [revision.revision.revision_id.clone()].into(),
                )?);
            }
        }
        let mut adjacency: BTreeMap<RevisionId, BTreeSet<RevisionId>> = by_id
            .keys()
            .cloned()
            .map(|revision| (revision, BTreeSet::new()))
            .collect();
        let mut admitted = BTreeSet::new();
        for revision in by_id.values() {
            for predecessor in &revision.supersedes {
                if by_id.contains_key(predecessor) {
                    adjacency
                        .get_mut(&revision.revision.revision_id)
                        .expect("known revision")
                        .insert(predecessor.clone());
                    adjacency
                        .get_mut(predecessor)
                        .expect("known predecessor")
                        .insert(revision.revision.revision_id.clone());
                    admitted.insert((revision.revision.revision_id.clone(), predecessor.clone()));
                } else {
                    anomalies.push(anomaly(
                        "dangling_relation",
                        [root.root_identity_hash.clone()].into(),
                        [revision.revision.revision_id.clone(), predecessor.clone()].into(),
                    )?);
                }
            }
        }
        let components = connected_components(&adjacency);
        for members in &components {
            let groups: BTreeSet<_> = members
                .iter()
                .filter_map(|revision| by_id.get(revision))
                .map(|revision| revision.legacy_group.clone())
                .collect();
            if groups.len() > 1 {
                anomalies.push(anomaly(
                    "legacy_group_bridge",
                    [root.root_identity_hash.clone()].into(),
                    members.clone(),
                )?);
            }
            let component_edges: BTreeSet<_> = admitted
                .iter()
                .filter(|(successor, predecessor)| {
                    members.contains(successor) && members.contains(predecessor)
                })
                .cloned()
                .collect();
            if revision_graph_has_cycle(members, &component_edges) {
                anomalies.push(anomaly(
                    "relation_cycle",
                    [root.root_identity_hash.clone()].into(),
                    members.clone(),
                )?);
            }
            let current = current_revisions(members, &component_edges);
            if replacement_heads_diverge(&current, &component_edges) {
                anomalies.push(anomaly(
                    "replacement_divergent_group",
                    [root.root_identity_hash.clone()].into(),
                    members.clone(),
                )?);
            }
            let shared_descriptors: BTreeSet<_> = members
                .iter()
                .filter_map(|revision| match decisions.overlap_identity.get(revision) {
                    Some(OverlapIdentityDecisionV1::Shared {
                        identity_descriptor,
                    }) => serde_json::to_string(identity_descriptor).ok(),
                    Some(OverlapIdentityDecisionV1::Distinct) | None => None,
                })
                .collect();
            if shared_descriptors.len() > 1 {
                anomalies.push(anomaly(
                    "overlap_identity_decision_conflict",
                    [root.root_identity_hash.clone()].into(),
                    members.clone(),
                )?);
            }
        }
        root_components.push((root.clone(), by_id, components, admitted));
    }

    anomalies.sort_by(|left, right| left.anomaly_id.cmp(&right.anomaly_id));
    anomalies.dedup_by(|left, right| left.anomaly_id == right.anomaly_id);
    let unresolved = anomalies.iter().any(|item| {
        if matches!(
            item.code.as_str(),
            "overlap_identity_decision_conflict" | "retained_identity_mismatch"
        ) {
            true
        } else if item.code == "cross_root_revision_overlap" {
            item.revision_ids
                .iter()
                .any(|revision| !decisions.overlap_identity.contains_key(revision))
        } else {
            !decisions.approved_anomaly_ids.contains(&item.anomaly_id)
        }
    });

    let mut proposals = Vec::new();
    for (root, by_id, components, admitted) in root_components {
        let mut groups = Vec::new();
        let mut planned_events = Vec::new();
        for members in components {
            let shared_descriptor = members.iter().find_map(|revision| {
                match decisions.overlap_identity.get(revision) {
                    Some(OverlapIdentityDecisionV1::Shared {
                        identity_descriptor,
                    }) => Some(identity_descriptor.clone()),
                    Some(OverlapIdentityDecisionV1::Distinct) | None => None,
                }
            });
            let descriptor = if let Some(descriptor) = shared_descriptor {
                descriptor
            } else {
                ChangeIdentityDescriptorV1::opaque_nonce(deterministic_nonce(&serde_json::json!({
                    "rootIdentityHash": root.root_identity_hash,
                    "members": members,
                }))?)
            };
            let declaration = build_change_declared(
                descriptor,
                deterministic_nonce(
                    &serde_json::json!({"claim": "declaration", "members": members}),
                )?,
            )?;
            let change_id = declaration.change_id.clone();
            planned_events.push(planned_event(
                &root,
                &decisions.writer,
                &decisions.occurred_at,
                declaration,
            )?);
            for revision_id in &members {
                planned_events.push(planned_event(
                    &root,
                    &decisions.writer,
                    &decisions.occurred_at,
                    build_membership_asserted(
                        &change_id,
                        revision_id,
                        deterministic_nonce(&serde_json::json!({
                            "claim": "membership",
                            "changeId": change_id,
                            "revisionId": revision_id,
                        }))?,
                    )?,
                )?);
            }
            let relations: BTreeSet<_> = admitted
                .iter()
                .filter(|(successor, predecessor)| {
                    members.contains(successor) && members.contains(predecessor)
                })
                .cloned()
                .collect();
            for (successor, predecessor) in &relations {
                planned_events.push(planned_event(
                    &root,
                    &decisions.writer,
                    &decisions.occurred_at,
                    build_revision_relation_asserted(
                        &change_id,
                        by_id
                            .get(successor)
                            .expect("component successor")
                            .revision
                            .clone(),
                        by_id
                            .get(predecessor)
                            .expect("component predecessor")
                            .revision
                            .clone(),
                        deterministic_nonce(&serde_json::json!({
                            "claim": "relation",
                            "changeId": change_id,
                            "successor": successor,
                            "predecessor": predecessor,
                        }))?,
                    )?,
                )?);
            }
            groups.push(AdoptionGroupProposalV1 {
                change_id,
                members,
                admitted_relations: relations,
            });
        }
        planned_events.sort_by(|left, right| left.idempotency_key.cmp(&right.idempotency_key));
        let manifest = if unresolved {
            None
        } else {
            let records = planned_events
                .iter()
                .map(reserved_record)
                .collect::<Result<Vec<_>>>()?;
            Some(BulkAdoptionManifestV1::from_reserved_records(
                root.source_authority_cursor.clone(),
                records,
            )?)
        };
        proposals.push(RootAdoptionProposalV1 {
            root_identity_hash: root.root_identity_hash,
            covered_revisions: by_id.into_keys().collect(),
            groups,
            planned_events,
            manifest,
        });
    }
    Ok(BulkAdoptionProposalV1 {
        roots: proposals,
        anomalies,
        requires_owner_decision: unresolved,
    })
}

fn planned_event<P: EventPayload>(
    root: &LegacyRootInventoryV1,
    writer: &Writer,
    occurred_at: &str,
    payload: P,
) -> Result<ShoreEvent> {
    let payload_value = serde_json::to_value(&payload)?;
    let payload_hash = sha256_json_prefixed(&payload_value)?;
    ShoreEvent::new(
        payload.event_type(),
        format!(
            "bulk-adoption:{}:{payload_hash}",
            payload.event_type().as_str()
        ),
        EventTarget::for_journal(root.journal_id.clone()),
        writer.clone(),
        payload,
        occurred_at,
    )
}

fn reserved_record(event: &ShoreEvent) -> Result<ReservedCohortRecordV1> {
    // EventStore persists serde's exact compact struct encoding. Reservations
    // bind those bytes, not a separately canonicalized representation.
    let bytes = serde_json::to_vec(event)?;
    let family = match event.event_type {
        crate::session::event::EventType::ChangeDeclared => "change_declared_v1",
        crate::session::event::EventType::ChangeMembershipAsserted => {
            "change_membership_asserted_v1"
        }
        crate::session::event::EventType::ChangeRevisionRelationAsserted => {
            "change_revision_relation_asserted_v1"
        }
        _ => {
            return Err(crate::error::ShoreError::Message(
                "bulk-adoption planner emitted an unsupported event family".to_owned(),
            ));
        }
    };
    Ok(ReservedCohortRecordV1 {
        logical_key: event.idempotency_key.clone(),
        record_family: family.to_owned(),
        record_hash: format!("sha256:{}", sha256_bytes_hex(&bytes)),
    })
}

fn deterministic_nonce(value: &serde_json::Value) -> Result<[u8; 32]> {
    let hash = sha256_json_prefixed(value)?;
    let hex = hash.strip_prefix("sha256:").expect("canonical hash prefix");
    let mut nonce = [0_u8; 32];
    for (index, byte) in nonce.iter_mut().enumerate() {
        *byte = u8::from_str_radix(&hex[index * 2..index * 2 + 2], 16)
            .expect("canonical hash is lowercase hexadecimal");
    }
    Ok(nonce)
}

fn is_prefixed_sha256(value: &str) -> bool {
    value.strip_prefix("sha256:").is_some_and(|hex| {
        hex.len() == 64
            && hex
                .bytes()
                .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
    })
}

fn anomaly(
    code: &str,
    root_identity_hashes: BTreeSet<String>,
    revision_ids: BTreeSet<RevisionId>,
) -> Result<MigrationAnomalyV1> {
    let anomaly_id = sha256_json_prefixed(&serde_json::json!({
        "code": code,
        "rootIdentityHashes": root_identity_hashes,
        "revisionIds": revision_ids,
    }))?;
    Ok(MigrationAnomalyV1 {
        anomaly_id,
        code: code.to_owned(),
        root_identity_hashes,
        revision_ids,
    })
}

fn connected_components(
    adjacency: &BTreeMap<RevisionId, BTreeSet<RevisionId>>,
) -> Vec<BTreeSet<RevisionId>> {
    let mut remaining: BTreeSet<_> = adjacency.keys().cloned().collect();
    let mut result = Vec::new();
    while let Some(start) = remaining.pop_first() {
        let mut component = BTreeSet::new();
        let mut pending = vec![start];
        while let Some(node) = pending.pop() {
            if !component.insert(node.clone()) {
                continue;
            }
            remaining.remove(&node);
            pending.extend(adjacency.get(&node).into_iter().flatten().cloned());
        }
        result.push(component);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{JournalId, RevisionId, RevisionRefV1};
    use crate::session::AuthorityCursorV2;

    fn cursor() -> AuthorityCursorV2 {
        AuthorityCursorV2 {
            schema: "pointbreak.authority-cursor.v2".to_owned(),
            journal_record_count: 3,
            event_count: 3,
            journal_record_set_hash: format!("sha256:{}", "1".repeat(64)),
            event_set_hash: format!("sha256:{}", "2".repeat(64)),
            capability_set_hash: format!("sha256:{}", "3".repeat(64)),
        }
    }

    fn revision(name: &str, byte: char, supersedes: &[&str]) -> LegacyRevisionV1 {
        LegacyRevisionV1 {
            revision: RevisionRefV1::new(
                RevisionId::new(format!("rev:sha256:{name}")),
                format!("sha256:{}", byte.to_string().repeat(64)),
            )
            .unwrap(),
            legacy_group: "engagement:one".to_owned(),
            supersedes: supersedes
                .iter()
                .map(|name| RevisionId::new(format!("rev:sha256:{name}")))
                .collect(),
        }
    }

    fn root(revisions: Vec<LegacyRevisionV1>) -> LegacyRootInventoryV1 {
        LegacyRootInventoryV1 {
            root_identity_hash: format!("sha256:{}", "4".repeat(64)),
            journal_id: JournalId::new("journal:test"),
            source_authority_cursor: cursor(),
            revisions,
        }
    }

    #[test]
    fn component_and_singleton_proposal_is_permutation_stable_and_covers_every_revision() {
        let input = root(vec![
            revision("a", 'a', &[]),
            revision("b", 'b', &["a"]),
            revision("c", 'c', &[]),
        ]);
        let first = plan_bulk_adoption(
            std::slice::from_ref(&input),
            &[],
            &OwnerManifestDecisionsV1::default(),
        )
        .unwrap();
        let mut reversed = input;
        reversed.revisions.reverse();
        let second =
            plan_bulk_adoption(&[reversed], &[], &OwnerManifestDecisionsV1::default()).unwrap();
        assert_eq!(first, second);
        assert_eq!(first.roots[0].covered_revisions.len(), 3);
        assert_eq!(first.roots[0].groups.len(), 2);
        assert!(first.roots[0].manifest.is_some());
    }

    #[test]
    fn dangling_relation_and_cross_root_overlap_require_explicit_owner_decisions() {
        let dangling = root(vec![revision("a", 'a', &["missing"])]);
        let mut other = root(vec![revision("a", 'a', &[])]);
        other.root_identity_hash = format!("sha256:{}", "5".repeat(64));
        other.journal_id = JournalId::new("journal:other");
        let proposal = plan_bulk_adoption(
            &[dangling.clone(), other.clone()],
            &[],
            &OwnerManifestDecisionsV1::default(),
        )
        .unwrap();
        assert!(proposal.requires_owner_decision);
        assert!(proposal.roots.iter().all(|root| root.manifest.is_none()));
        assert!(
            proposal
                .anomalies
                .iter()
                .any(|item| item.code == "dangling_relation")
        );
        assert!(
            proposal
                .anomalies
                .iter()
                .any(|item| item.code == "cross_root_revision_overlap")
        );
        assert!(
            proposal
                .anomalies
                .iter()
                .any(|item| item.code == "cross_root_relation_disagreement")
        );

        let decisions = OwnerManifestDecisionsV1 {
            approved_anomaly_ids: proposal
                .anomalies
                .iter()
                .filter(|item| item.code != "cross_root_revision_overlap")
                .map(|item| item.anomaly_id.clone())
                .collect(),
            overlap_identity: [(
                RevisionId::new("rev:sha256:a"),
                OverlapIdentityDecisionV1::Shared {
                    identity_descriptor: ChangeIdentityDescriptorV1::root_revision(
                        RevisionId::new("rev:sha256:a"),
                    ),
                },
            )]
            .into(),
            ..OwnerManifestDecisionsV1::default()
        };
        let approved = plan_bulk_adoption(&[dangling, other], &[], &decisions).unwrap();
        assert!(!approved.requires_owner_decision);
        assert!(approved.roots.iter().all(|root| root.manifest.is_some()));
        assert_eq!(
            approved.roots[0].groups[0].change_id,
            approved.roots[1].groups[0].change_id
        );
    }

    #[test]
    fn retained_manifest_overlap_requires_and_reuses_an_explicit_identity_decision() {
        let revision_id = RevisionId::new("rev:sha256:a");
        let descriptor = ChangeIdentityDescriptorV1::root_revision(revision_id.clone());
        let retained_change_id = crate::model::derive_change_id(&descriptor).unwrap();
        let retained = RetainedAdoptionManifestV1 {
            manifest_hash: format!("sha256:{}", "9".repeat(64)),
            allocations: vec![RetainedChangeAllocationV1 {
                change_id: retained_change_id.clone(),
                identity_descriptor: descriptor.clone(),
                members: [revision_id.clone()].into(),
            }],
        };
        let input = root(vec![revision("a", 'a', &[])]);

        let unresolved = plan_bulk_adoption(
            std::slice::from_ref(&input),
            std::slice::from_ref(&retained),
            &OwnerManifestDecisionsV1::default(),
        )
        .unwrap();
        assert!(unresolved.requires_owner_decision);
        assert!(unresolved.roots[0].manifest.is_none());

        let decisions = OwnerManifestDecisionsV1 {
            overlap_identity: [(
                revision_id,
                OverlapIdentityDecisionV1::Shared {
                    identity_descriptor: descriptor,
                },
            )]
            .into(),
            ..OwnerManifestDecisionsV1::default()
        };
        let resolved = plan_bulk_adoption(&[input], &[retained], &decisions).unwrap();
        assert!(!resolved.requires_owner_decision);
        assert!(resolved.roots[0].manifest.is_some());
        assert_eq!(resolved.roots[0].groups[0].change_id, retained_change_id);
    }

    #[test]
    fn real_l0_dry_run_is_stable_path_private_and_mutation_free() {
        let repo = real_l0_repo();

        let before = resolve_change_read_store(repo.path()).unwrap().1.cursor;
        let first = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
        let second = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
        let after = resolve_change_read_store(repo.path()).unwrap().1.cursor;

        assert_eq!(first, second);
        assert_eq!(before, after);
        assert_eq!(first.roots.len(), 1);
        assert_eq!(first.roots[0].revision_count, 2);
        assert_eq!(first.roots[0].proposed_changes.len(), 1);
        assert!(!first.requires_owner_decision);
        assert!(first.roots[0].cohort_manifest_hash.is_some());
        let json = serde_json::to_string(&first).unwrap();
        assert!(!json.contains(&repo.path().display().to_string()));
        assert!(!json.contains("private first bytes"));
        assert!(!json.contains("private second bytes"));
    }

    #[test]
    fn private_executor_retries_every_append_boundary_to_exact_l2() {
        use crate::crypto::TestEd25519Signer;

        for boundary in 1..=6 {
            let repo = real_l0_repo();
            let dry_run =
                dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
            let manifest_hash = dry_run.roots[0].cohort_manifest_hash.as_deref().unwrap();
            let signer = TestEd25519Signer::from_seed([0x41; 32]);
            let plan = prepare_bulk_adoption_for_qualification(
                repo.path(),
                manifest_hash,
                dry_run.writer.clone(),
                dry_run.claim_occurred_at.clone(),
                format!("qualification-activation-{boundary:02}"),
                "2026-08-06T00:00:00Z".to_owned(),
                "2026-08-06T00:00:01Z".to_owned(),
                &signer,
            )
            .unwrap();
            let expected_boundaries = plan.events.len() + 2;
            assert_eq!(expected_boundaries, 6);

            let error = execute_bulk_adoption_plan(repo.path(), &plan, Some(boundary)).unwrap_err();
            assert!(
                error
                    .to_string()
                    .contains("injected migration interruption")
            );
            execute_bulk_adoption_plan(repo.path(), &plan, None).unwrap();
            assert!(matches!(
                resolve_change_read_store(repo.path()).unwrap().1.status,
                StoreCapabilityStatus::Ready { .. }
            ));
        }
    }

    #[test]
    fn execution_plan_tampering_fails_before_activation() {
        use crate::crypto::TestEd25519Signer;

        let repo = real_l0_repo();
        let dry_run = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
        let mut plan = prepare_bulk_adoption_for_qualification(
            repo.path(),
            dry_run.roots[0].cohort_manifest_hash.as_deref().unwrap(),
            dry_run.writer.clone(),
            dry_run.claim_occurred_at.clone(),
            "tamper-proof".to_owned(),
            "2026-08-06T18:19:00Z".to_owned(),
            "2026-08-06T18:19:01Z".to_owned(),
            &TestEd25519Signer::from_seed([0x50; 32]),
        )
        .unwrap();
        plan.events[0].payload["changeId"] =
            serde_json::Value::String("change:tampered".to_owned());
        let error = execute_bulk_adoption_plan(repo.path(), &plan, None).unwrap_err();
        assert!(error.to_string().contains("reservation manifest"));
        assert!(matches!(
            resolve_change_read_store(repo.path()).unwrap().1.status,
            StoreCapabilityStatus::MigrationRequired
        ));
    }

    #[test]
    fn production_migration_requires_exact_acks_and_publishes_a_verified_external_backup() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        let repo = real_l0_repo();
        let dry_run = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
        let cohort_hash = dry_run.roots[0].cohort_manifest_hash.clone().unwrap();
        let backup_parent = tempfile::tempdir().unwrap();
        let backup = backup_parent.path().join("pre-activation");

        let missing_reader_ack = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                repo.path(),
                dry_run.clone(),
                dry_run.manifest_hash.clone(),
                cohort_hash.clone(),
                &backup,
                "migration-one",
            )
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x51; 32]))
            .with_fixed_occurred_at("2026-08-06T18:20:00Z")
            .with_derived_enabled(false),
        )
        .unwrap_err();
        assert!(missing_reader_ack.to_string().contains("minimum reader"));
        assert!(!backup.exists());

        let receipt = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                repo.path(),
                dry_run.clone(),
                dry_run.manifest_hash.clone(),
                cohort_hash,
                &backup,
                "migration-one",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x51; 32]))
            .with_fixed_occurred_at("2026-08-06T18:20:00Z")
            .with_derived_enabled(false),
        )
        .unwrap();

        assert_eq!(receipt.schema, BULK_ADOPTION_MIGRATION_RECEIPT_SCHEMA_V1);
        assert_eq!(receipt.approved_dry_run_hash, dry_run.manifest_hash);
        assert_eq!(
            receipt.minimum_reader_profile,
            REVIEW_CHANGE_REVISION_COHORT_V1
        );
        assert!(backup.join("store/events").is_dir());
        assert!(backup.join(BULK_ADOPTION_BACKUP_MANIFEST_FILE_V1).is_file());
        assert!(backup.join(BULK_ADOPTION_BACKUP_RECEIPT_FILE_V1).is_file());
        assert!(backup.join(BULK_ADOPTION_EXECUTION_PLAN_FILE_V1).is_file());
        assert!(matches!(
            resolve_change_read_store(repo.path()).unwrap().1.status,
            StoreCapabilityStatus::Ready { .. }
        ));

        let repeated = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                repo.path(),
                dry_run.clone(),
                dry_run.manifest_hash,
                dry_run.roots[0].cohort_manifest_hash.clone().unwrap(),
                &backup,
                "migration-one",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .with_derived_enabled(false),
        )
        .unwrap();
        assert_eq!(
            repeated.disposition,
            BulkAdoptionMigrationDispositionV1::Existing
        );
        assert_eq!(repeated.completion_id, receipt.completion_id);
    }

    #[test]
    fn production_migration_resumes_each_append_boundary_from_the_retained_plan() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        for boundary in 1..=6 {
            let repo = real_l0_repo();
            let dry_run =
                dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
            let cohort_hash = dry_run.roots[0].cohort_manifest_hash.clone().unwrap();
            let backup_parent = tempfile::tempdir().unwrap();
            let backup = backup_parent
                .path()
                .join(format!("pre-activation-{boundary}"));
            let base = BulkAdoptionMigrationOptions::new(
                repo.path(),
                dry_run.clone(),
                dry_run.manifest_hash.clone(),
                cohort_hash.clone(),
                &backup,
                format!("migration-{boundary}"),
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .with_fixed_occurred_at("2026-08-06T18:21:00Z")
            .with_derived_enabled(false);

            let interrupted = migrate_bulk_adoption(
                base.sign_with(TestEd25519Signer::from_seed([0x52; 32]))
                    .with_interruption_after_append(boundary),
            )
            .unwrap_err();
            assert!(interrupted.to_string().contains("interruption"));
            assert!(backup.join(BULK_ADOPTION_EXECUTION_PLAN_FILE_V1).is_file());

            let resumed = migrate_bulk_adoption(
                BulkAdoptionMigrationOptions::new(
                    repo.path(),
                    dry_run.clone(),
                    dry_run.manifest_hash.clone(),
                    cohort_hash,
                    &backup,
                    format!("migration-{boundary}"),
                )
                .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
                .with_legacy_reader_unsupported_ack()
                .with_fixed_occurred_at("2026-08-06T18:21:00Z")
                .with_derived_enabled(false),
            )
            .unwrap();
            assert!(matches!(
                resolve_change_read_store(repo.path()).unwrap().1.status,
                StoreCapabilityStatus::Ready { .. }
            ));
            assert_eq!(
                resumed.disposition,
                BulkAdoptionMigrationDispositionV1::Created
            );
        }
    }

    #[test]
    fn production_migration_refuses_wrong_acknowledgements_and_authority_drift_before_backup() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        let repo = real_l0_repo();
        let dry_run = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
        let cohort_hash = dry_run.roots[0].cohort_manifest_hash.clone().unwrap();
        let backups = tempfile::tempdir().unwrap();

        for (name, dry_ack, cohort_ack) in [
            ("wrong-dry", "sha256:wrong".to_owned(), cohort_hash.clone()),
            (
                "wrong-cohort",
                dry_run.manifest_hash.clone(),
                "sha256:wrong".to_owned(),
            ),
        ] {
            let backup = backups.path().join(name);
            let error = migrate_bulk_adoption(
                BulkAdoptionMigrationOptions::new(
                    repo.path(),
                    dry_run.clone(),
                    dry_ack,
                    cohort_ack,
                    &backup,
                    name,
                )
                .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
                .with_legacy_reader_unsupported_ack()
                .sign_with(TestEd25519Signer::from_seed([0x61; 32]))
                .with_derived_enabled(false),
            )
            .unwrap_err();
            assert!(error.to_string().contains("acknowledged"));
            assert!(!backup.exists());
        }

        std::fs::write(repo.path().join("sample.txt"), "authority moved\n").unwrap();
        crate::session::capture_review(
            crate::session::CaptureOptions::new(repo.path()).with_summary("authority moved"),
        )
        .unwrap();
        let drift_backup = backups.path().join("authority-drift");
        let error = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                repo.path(),
                dry_run.clone(),
                dry_run.manifest_hash,
                cohort_hash,
                &drift_backup,
                "authority-drift",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x62; 32]))
            .with_derived_enabled(false),
        )
        .unwrap_err();
        assert!(error.to_string().contains("approved dry-run cursor"));
        assert!(!drift_backup.exists());
    }

    #[test]
    fn production_migration_refuses_a_damaged_backup() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        let damaged_repo = real_l0_repo();
        let damaged_dry =
            dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(damaged_repo.path())).unwrap();
        let damaged_backup_parent = tempfile::tempdir().unwrap();
        let damaged_backup = damaged_backup_parent.path().join("damaged");
        let interrupted = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                damaged_repo.path(),
                damaged_dry.clone(),
                damaged_dry.manifest_hash.clone(),
                damaged_dry.roots[0].cohort_manifest_hash.clone().unwrap(),
                &damaged_backup,
                "damaged-backup",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x63; 32]))
            .with_fixed_occurred_at("2026-08-06T18:22:00Z")
            .with_derived_enabled(false)
            .with_interruption_after_append(1),
        )
        .unwrap_err();
        assert!(interrupted.to_string().contains("interruption"));
        let event = std::fs::read_dir(damaged_backup.join("store/events"))
            .unwrap()
            .next()
            .unwrap()
            .unwrap()
            .path();
        std::fs::write(event, b"damaged").unwrap();
        let error = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                damaged_repo.path(),
                damaged_dry.clone(),
                damaged_dry.manifest_hash,
                damaged_dry.roots[0].cohort_manifest_hash.clone().unwrap(),
                &damaged_backup,
                "damaged-backup",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .with_derived_enabled(false),
        )
        .unwrap_err();
        assert!(error.to_string().contains("failed verification"));
    }

    #[test]
    fn production_migration_default_publishes_derived_only_after_l2() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        let repo = real_l0_repo();
        let dry_run = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
        let backup_parent = tempfile::tempdir().unwrap();
        let receipt = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                repo.path(),
                dry_run.clone(),
                dry_run.manifest_hash,
                dry_run.roots[0].cohort_manifest_hash.clone().unwrap(),
                backup_parent.path().join("derived"),
                "derived-after-l2",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x64; 32]))
            // Intentionally do not override `derived_enabled`: this proves the
            // production default while the authority lock spans activation.
            .with_fixed_occurred_at("2026-08-06T18:23:00Z"),
        )
        .unwrap();
        assert!(receipt.derived_generation_id.is_some());
        let (store, inspection) = resolve_change_read_store(repo.path()).unwrap();
        assert!(matches!(
            inspection.status,
            StoreCapabilityStatus::Ready { .. }
        ));
        assert!(
            std::fs::read_dir(store.store_dir().join("derived/publications"))
                .unwrap()
                .next()
                .is_some()
        );
        let status =
            crate::session::derived_access::history::DerivedHistoryAccess::resolve(repo.path())
                .unwrap()
                .lifecycle_status();
        assert_eq!(
            status.availability,
            crate::session::derived_access::history::DerivedHistoryAvailability::Current,
            "{status:?}"
        );
        assert_eq!(receipt.derived_generation_id, status.generation_id);
    }

    #[test]
    fn verified_backup_restores_only_as_a_separately_identified_l0_fork() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        let source = real_l0_repo();
        let dry_run = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(source.path())).unwrap();
        let backup_parent = tempfile::tempdir().unwrap();
        let backup = backup_parent.path().join("restore-source");
        migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                source.path(),
                dry_run.clone(),
                dry_run.manifest_hash,
                dry_run.roots[0].cohort_manifest_hash.clone().unwrap(),
                &backup,
                "restore-source",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x65; 32]))
            .with_fixed_occurred_at("2026-08-06T18:24:00Z")
            .with_derived_enabled(false),
        )
        .unwrap();

        let target = tempfile::tempdir().unwrap();
        git(target.path(), &["init", "--quiet"]);
        git(target.path(), &["config", "user.name", "Pointbreak Test"]);
        git(
            target.path(),
            &["config", "user.email", "pointbreak@example.test"],
        );
        let restored = restore_bulk_adoption_backup(&backup, target.path()).unwrap();
        assert_eq!(
            restored.disposition,
            BulkAdoptionBackupRestoreDispositionV1::Created
        );
        assert_ne!(
            restored.restored_store_identity,
            dry_run.roots[0].root_identity_hash
        );
        let restored_dry =
            dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(target.path())).unwrap();
        assert_eq!(restored_dry.roots[0].revision_count, 2);
        assert!(matches!(
            resolve_change_read_store(target.path()).unwrap().1.status,
            StoreCapabilityStatus::MigrationRequired
        ));
        assert_eq!(
            restore_bulk_adoption_backup(&backup, target.path())
                .unwrap()
                .disposition,
            BulkAdoptionBackupRestoreDispositionV1::Existing
        );
    }

    #[test]
    fn production_migration_repairs_an_exact_interrupted_backup_copy() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        let repo = real_l0_repo();
        let dry_run = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(repo.path())).unwrap();
        let (store, _) = resolve_change_read_store(repo.path()).unwrap();
        let source_entries = inventory_store_files(store.store_dir()).unwrap();
        let first = source_entries.first().unwrap();
        let backup_parent = tempfile::tempdir().unwrap();
        let backup = backup_parent.path().join("partial");
        let partial_target = backup.join("store").join(&first.path);
        std::fs::create_dir_all(partial_target.parent().unwrap()).unwrap();
        std::fs::copy(store.store_dir().join(&first.path), &partial_target).unwrap();

        let receipt = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                repo.path(),
                dry_run.clone(),
                dry_run.manifest_hash,
                dry_run.roots[0].cohort_manifest_hash.clone().unwrap(),
                &backup,
                "partial-backup",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x66; 32]))
            .with_fixed_occurred_at("2026-08-06T18:25:00Z")
            .with_derived_enabled(false),
        )
        .unwrap();
        assert_eq!(
            receipt.disposition,
            BulkAdoptionMigrationDispositionV1::Created
        );
        assert!(backup.join(BULK_ADOPTION_BACKUP_RECEIPT_FILE_V1).is_file());
    }

    #[test]
    fn production_migration_selects_its_exact_root_from_a_multi_root_approval() {
        use crate::crypto::TestEd25519Signer;
        use crate::session::store::capabilities::REVIEW_CHANGE_REVISION_COHORT_V1;

        let first = real_l0_repo();
        let second = real_l0_repo();
        let mut approved =
            dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(first.path())).unwrap();
        let other = dry_run_bulk_adoption(BulkAdoptionDryRunOptions::new(second.path())).unwrap();
        approved.roots.push(other.roots[0].clone());
        approved
            .roots
            .sort_by(|left, right| left.root_identity_hash.cmp(&right.root_identity_hash));
        let material = serde_json::json!({
            "schema": approved.schema,
            "roots": approved.roots,
            "anomalies": approved.anomalies,
            "requiresOwnerDecision": approved.requires_owner_decision,
            "ownerDecisionManifestHash": approved.owner_decision_manifest_hash,
            "writer": approved.writer,
            "claimOccurredAt": approved.claim_occurred_at,
            "signaturePolicy": approved.signature_policy,
        });
        approved.manifest_hash = sha256_json_prefixed(&material).unwrap();
        let first_identity =
            crate::session::store_identity(StoreIdentityOptions::new(first.path()))
                .unwrap()
                .store_identity;
        let cohort = approved
            .roots
            .iter()
            .find(|root| root.root_identity_hash == first_identity)
            .unwrap()
            .cohort_manifest_hash
            .clone()
            .unwrap();
        let backup_parent = tempfile::tempdir().unwrap();
        let backup = backup_parent.path().join("multi-root");
        let receipt = migrate_bulk_adoption(
            BulkAdoptionMigrationOptions::new(
                first.path(),
                approved.clone(),
                approved.manifest_hash.clone(),
                cohort,
                &backup,
                "multi-root",
            )
            .with_minimum_reader_ack(REVIEW_CHANGE_REVISION_COHORT_V1)
            .with_legacy_reader_unsupported_ack()
            .sign_with(TestEd25519Signer::from_seed([0x67; 32]))
            .with_fixed_occurred_at("2026-08-06T18:26:00Z")
            .with_derived_enabled(false),
        )
        .unwrap();
        assert_eq!(receipt.approved_dry_run_hash, approved.manifest_hash);
    }

    fn real_l0_repo() -> tempfile::TempDir {
        let repo = tempfile::tempdir().unwrap();
        git(repo.path(), &["init", "--quiet"]);
        git(repo.path(), &["config", "user.name", "Pointbreak Test"]);
        git(
            repo.path(),
            &["config", "user.email", "pointbreak@example.test"],
        );
        git(repo.path(), &["config", "commit.gpgsign", "false"]);
        std::fs::write(repo.path().join("sample.txt"), "base\n").unwrap();
        git(repo.path(), &["add", "sample.txt"]);
        git(repo.path(), &["commit", "--quiet", "-m", "base"]);
        std::fs::write(repo.path().join("sample.txt"), "private first bytes\n").unwrap();
        let first = crate::session::capture_review(
            crate::session::CaptureOptions::new(repo.path()).with_summary("first"),
        )
        .unwrap();
        std::fs::write(repo.path().join("sample.txt"), "private second bytes\n").unwrap();
        crate::session::capture_review(
            crate::session::CaptureOptions::new(repo.path())
                .with_summary("second")
                .with_supersedes(vec![first.revision_id]),
        )
        .unwrap();
        repo
    }

    fn git(repo: &Path, args: &[&str]) {
        let output = std::process::Command::new("git")
            .args(args)
            .current_dir(repo)
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
}