jubarte-edit 0.20.0

The edit product of the jubarte engine: edit plans as tracked changes, text diff views, capabilities and staged publication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! `PreparedEdit`: one preparation path for dry run and apply (V2-08).
//!
//! The plan resolves and applies in memory, the result serializes, and the
//! *actual primary bytes* are chosen before final checks: the serialized
//! candidate for a changed result, or the original source bytes verbatim
//! for a truly unchanged one. Those chosen bytes are what findings, output
//! references and digests describe — a reserialized package is never
//! validated and then swapped for the source at commit. Dry run and apply
//! share this whole path; they differ only at publication.

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

use super::{
    EditError, EditOutcome, EditPlan, EditResult, ExistingRevisions, OperationKind, Resolved,
    StoryPart, Transaction, check_update_fields, comment_holds_kept_text, comment_text_of,
    controls, enclose_deleted_paragraph_comment_references, lift_deletion_comments,
    revision_counts, sections, tracked, whole,
};
use crate::comparer::WmlComparerSettings;
use crate::inspect::{body_paragraph_nodes, project_paragraph, source_sha256};
use crate::namespaces::W;
use crate::result::{Effect, codes};
use crate::validate::{self, Finding};

/// A finding identity the publication policy compares on: rule set, code,
/// part and the entity/range the finding points at. Findings are compared
/// by identity, never by aggregate counts: a baseline warning healed beside
/// a new same-code warning elsewhere still reports the new one.
fn finding_key(f: &Finding) -> (String, String, String, String) {
    (
        f.set.clone(),
        f.code.clone(),
        f.part.clone(),
        f.location.clone(),
    )
}

/// The findings in `candidate` that `baseline` does not carry, first
/// occurrence each, in `candidate` order.
pub fn introduced_findings(baseline: &[Finding], candidate: &[Finding]) -> Vec<Finding> {
    let held: HashSet<_> = baseline.iter().map(finding_key).collect();
    let mut seen = HashSet::new();
    candidate
        .iter()
        .filter(|f| !held.contains(&finding_key(f)))
        .filter(|f| seen.insert(finding_key(f)))
        .cloned()
        .collect()
}

/// A non-`word_fatal` finding code that still blocks publication when the
/// edit introduces it. `BOOKMARK_CROSSES_REVISION` is a protected-range
/// regression the general validator reports as a warning; an introduced
/// crossing is a failed invariant, never a disclosed lead.
/// `CELL_FINAL_MARK_REVISION` opens in Word, but its Accept All or Reject
/// All never returns (#1669).
const BLOCKING_CODES: &[&str] = &["BOOKMARK_CROSSES_REVISION", "CELL_FINAL_MARK_REVISION"];

/// Whether `f` blocks publication when it is newly introduced.
fn blocking(f: &Finding) -> bool {
    f.word_fatal || BLOCKING_CODES.contains(&f.code.as_str())
}

/// Which serialized bytes the prepared edit chose as its primary.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PrimaryBytes {
    /// The original source, byte for byte: a truly unchanged primary is
    /// never a reserialization.
    Source,
    /// The serialized candidate the engine produced.
    Candidate,
}

/// The candidate's findings split by provenance.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CandidateChecks {
    /// Findings the input already carried; disclosed, never blocking on
    /// their own.
    pub baseline: Vec<Finding>,
    /// Findings the edit introduced. Only blocking ones fail preparation;
    /// the rest are disclosed here.
    pub introduced: Vec<Finding>,
    /// Findings persisting in either validated representation, deduplicated by identity.
    pub unresolved: Vec<Finding>,
    /// Both serialized representations passed the required integrity gate.
    pub integrity: bool,
    /// DOM-realized and serialized-story postconditions completed successfully.
    pub operation_postconditions: bool,
}

impl CandidateChecks {
    /// Report only completed checks and their computed findings.
    pub fn verification(&self) -> crate::result::Verification {
        let outcome = |ran| if ran { "passed" } else { "skipped" }.to_string();
        crate::result::Verification {
            integrity: outcome(self.integrity),
            operation_postconditions: outcome(self.operation_postconditions),
            baseline_findings: Some(self.baseline.clone()),
            introduced_findings: Some(self.introduced.clone()),
            unresolved_findings: Some(self.unresolved.clone()),
            ..Default::default()
        }
    }
}

/// Which serialized candidate is the prepared transaction's publication:
/// the clean copy (changes accepted) or the redline (changes tracked).
/// The role drives the bytes `prepare` chooses as `primary`, the digest
/// it derives, and the serialized-story postcondition it runs: validation
/// describes the chosen primary, not a reserialized package swapped in at
/// commit. An unchanged transaction always returns the original source
/// bytes, regardless of role.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PrimaryRole {
    /// Publish the clean (untracked) candidate. The default — preserves
    /// the v1 helper callers, the Python and WASM bindings, and every
    /// caller that has not chosen a publication track yet.
    #[default]
    Clean,
    /// Publish the redline (tracked) candidate. The v1 typed CLI uses it;
    /// `prepare_v2` likewise publishes redline, by construction.
    Redline,
}

/// What to check beyond the shared candidate checks.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PrepareOptions {
    /// Refuse `NO_CHANGES_REQUESTED` after the checks pass when the primary
    /// has no document effect. Renders, reports and other derivatives are
    /// not consulted: the condition judges the primary only.
    pub require_change: bool,
    /// Compare each `rewrite`'s requested text with the realized paragraph
    /// exactly: a request the word-level rewrite algorithm normalizes away
    /// is `POSTCONDITION_FAILED`, not an unchanged success. The v1 `rewrite`
    /// contract documents that normalization, so it stays off there; the v2
    /// exact-text codec sets it.
    pub exact_text: bool,
    /// Which serialized candidate `prepare` chooses as the publication.
    /// `Clean` (the default) keeps the helper-callers' contract, where the
    /// clean copy is the primary and the redline is a side artifact.
    /// `Redline` makes the redline the primary: the chosen bytes the
    /// serialized-story postcondition describes and the receipt digest
    /// names, so a redline-only defect cannot slip through.
    pub role: PrimaryRole,
    /// The request syntax a refusal hint quotes: a flags-built plan names
    /// flags, a JSON plan names JSON (#1108).
    pub syntax: super::PlanSyntax,
    /// `--skip-failed` (#1253): leave out every operation that fails, and
    /// every operation of its `group`, instead of refusing the plan; the
    /// rest is applied as a plan of its own. [`PreparedEdit::skipped`]
    /// lists what was left out. A failure no operation is blamed for is
    /// never skipped, and a plan whose every operation fails is refused.
    pub skip_failed: bool,
}

impl Default for PrepareOptions {
    fn default() -> Self {
        Self {
            require_change: false,
            exact_text: false,
            role: PrimaryRole::Clean,
            syntax: super::PlanSyntax::V1,
            skip_failed: false,
        }
    }
}

/// A plan applied and verified in memory, up to publication.
#[derive(Clone, Debug)]
pub struct PreparedEdit {
    /// The v1 artifacts: clean copy, redline and per-operation report.
    pub result: EditResult,
    /// SHA-256 of the original source.
    pub source_sha256: String,
    /// SHA-256 of the normalized plan JSON the engine applied.
    pub request_sha256: String,
    /// The chosen primary bytes, the same bytes `checks` describe.
    pub primary: Vec<u8>,
    /// Which bytes `primary` holds.
    pub primary_bytes: PrimaryBytes,
    /// SHA-256 of `primary`: output digests come from the checked bytes.
    pub primary_sha256: String,
    /// The primary transaction's document effect (`changed`/`unchanged`
    /// only; `not_applied` belongs to the dry-run envelope).
    pub primary_effect: Effect,
    /// Baseline and introduced findings on the primary's basis.
    pub checks: CandidateChecks,
    /// Source paragraph → chosen-primary correspondence for `--expect`
    /// (V2-13). `None` on the v1 path, whose published primary is a
    /// comparer redline with no tracked correspondence: a v1 plan plus
    /// `--expect` refuses instead of guessing.
    pub correspondence: Option<crate::expect::SourceToOutputCorrespondence>,
    /// The operations [`PrepareOptions::skip_failed`] left out, in plan
    /// order; empty otherwise.
    pub skipped: Vec<super::SkippedOperation>,
}

/// Run the shared preparation: resolve, apply, choose the primary bytes,
/// verify the serialized candidate, then apply `options` last. A dry run is
/// this same result without publication.
///
/// # Errors
///
/// Every error [`crate::edit::apply_plan`] reports, plus
/// `POSTCONDITION_FAILED` when a requested effect is not realized in the
/// serialized candidate (or, with [`PrepareOptions::exact_text`], when the
/// requested text differs from it), `CANDIDATE_INVALID` when the candidate
/// introduces a blocking finding, and `NO_CHANGES_REQUESTED` under
/// [`PrepareOptions::require_change`] when the primary is unchanged.
pub fn prepare(
    source: &[u8],
    plan: &EditPlan,
    options: &PrepareOptions,
) -> Result<PreparedEdit, EditError> {
    if options.skip_failed {
        // Each attempt comes back here with `skip_failed` off: the whole
        // plan, refused at its first failure.
        return super::skip::prepare_skipping(source, plan, options);
    }
    let known = SourceChecks::default();
    prepare_once(source, plan, options, &known).map_err(|failure| {
        // A v1 plan's operations are independent (`check_conflicts`):
        // any of them make a plan of their own.
        let mut reruns = Reruns::new(|ops: &[usize]| {
            let part = EditPlan {
                operations: ops.iter().map(|&i| plan.operations[i].clone()).collect(),
                ..plan.clone()
            };
            prepare_once(source, &part, options, &known)
                .err()
                .and_then(|f| f.refusal)
                .map(|(refusals, _)| refusals)
        })
        .knowing(plan.operations.len(), &failure);
        attributed(*failure, plan.operations.len(), true, &mut reruns)
    })
}

/// `plan` prepared once for `--skip-failed`, which may leave out only its
/// first `count` operations: the rest follow every rerun. A failure the
/// candidate checks did not raise comes back [`Blame::Plain`]; a candidate
/// refusal comes back with every defect attributed at once, the reruns
/// shared, so a retry never bisects again what this one did.
pub(super) fn prepare_blaming(
    source: &[u8],
    plan: &EditPlan,
    count: usize,
    options: &PrepareOptions,
) -> Result<PreparedEdit, Box<Blame>> {
    let known = SourceChecks::default();
    let failure = match prepare_once(source, plan, options, &known) {
        Ok(prepared) => return Ok(prepared),
        Err(failure) => failure,
    };
    let Some((refusals, outcomes)) = failure.refusal.clone() else {
        return Err(Box::new(Blame::Plain(failure.error)));
    };
    let mut reruns = Reruns::new(|ops: &[usize]| {
        let part = EditPlan {
            operations: ops
                .iter()
                .copied()
                .chain(count..plan.operations.len())
                .map(|i| plan.operations[i].clone())
                .collect(),
            ..plan.clone()
        };
        prepare_once(source, &part, options, &known)
            .err()
            .and_then(|f| f.refusal)
            .map(|(refusals, _)| refusals)
    })
    .knowing(count, &failure);
    let mut distinct: Vec<&Refusal> = Vec::new();
    for refusal in &refusals {
        if !distinct.iter().any(|d| d.same(refusal)) {
            distinct.push(refusal);
        }
    }
    // Each operation alone is one short preparation; a defect's bisection
    // about log2(count) long ones. With about as many defects as
    // operations over log2(count), the operations alone cost less: a
    // defect an operation meets alone is blamed on that one alone.
    let bits = (usize::BITS - count.leading_zeros()) as usize;
    let alone = distinct.len() * bits >= count;
    // Every defect, once: one the plan without operations meets too is
    // the plan's own, and no operation left out clears it.
    let mut blamed: Vec<(usize, EditError)> = Vec::new();
    for refusal in distinct {
        let single = alone
            .then(|| (0..count).find(|&i| reruns.reproduces(&[i], refusal)))
            .flatten()
            .map(|i| vec![i]);
        let Some(ops) =
            single.or_else(|| culprits(count, true, |ops| reruns.reproduces(ops, refusal)))
        else {
            return Err(Box::new(Blame::Candidate(
                attributed(*failure, count, true, &mut reruns),
                Vec::new(),
            )));
        };
        if !blamed.iter().any(|(i, _)| *i == ops[0]) {
            let error = advised(
                naming(refusal.error.clone(), &ops, &outcomes),
                failure.advice,
            );
            blamed.push((ops[0], error));
        }
    }
    // Each blamed operation carries its own error; the plan's is the
    // refusal unattributed.
    Err(Box::new(Blame::Candidate(failure.error, blamed)))
}

/// Why [`prepare_blaming`] refused a plan.
pub(super) enum Blame {
    /// A failure the candidate checks did not raise: its error, as
    /// [`prepare`] reports it.
    Plain(EditError),
    /// A candidate refusal: the error [`prepare`] reports, and each
    /// operation (plan index) a defect is blamed on, with the error that
    /// names it for that defect, in the order the candidate checks met
    /// them. Empty when a defect is the plan's own.
    Candidate(EditError, Vec<(usize, EditError)>),
}

/// What the candidate checks read in the source, read once for a plan and
/// every rerun of it.
#[derive(Default)]
struct SourceChecks {
    /// `validate::validate(source)`, `None` when unreadable.
    findings: std::cell::OnceCell<Option<Vec<Finding>>>,
    /// `validate::revision_id_occurrences(source)`, `None` when unreadable.
    revision_ids: std::cell::OnceCell<Option<HashMap<(String, String), usize>>>,
}

/// One defect a candidate gate refuses, as the attribution reruns compare
/// it: the artifact refused, the finding's code (or the gate's own), its
/// part, and its subject, which tells it from another defect of that code
/// in that part and stays put when a rerun leaves operations out (a
/// source's revision id, a bookmark's name; never an index or a minted
/// id). `error` refuses the candidate for this defect.
#[derive(Clone, Debug)]
struct Refusal {
    artifact: &'static str,
    code: String,
    part: String,
    subject: String,
    error: EditError,
}

impl Refusal {
    /// The same defect: `error` aside, as reruns find it again.
    fn same(&self, other: &Self) -> bool {
        self.artifact == other.artifact
            && self.code == other.code
            && self.part == other.part
            && self.subject == other.subject
    }
}

/// A candidate gate's refusal: every defect it met, the one its error
/// reports first. Never empty.
type Refused = Vec<Refusal>;

/// The refusal of one defect: `CANDIDATE_INVALID` with `message`.
fn refused(
    artifact: &'static str,
    code: &str,
    part: &str,
    subject: String,
    message: String,
) -> Refusal {
    Refusal {
        artifact,
        code: code.to_string(),
        part: part.to_string(),
        subject,
        error: err("CANDIDATE_INVALID", None, message),
    }
}

/// A failed preparation, and the candidate refusal behind it with every
/// operation's outcome, when the candidate checks refused it. `advice`
/// closes the message, after the operations [`attributed`] names.
struct Failure {
    error: EditError,
    refusal: Option<(Refused, Vec<EditOutcome>)>,
    advice: Option<&'static str>,
}

/// The candidate refusals reruns of some of a plan's operations meet, each
/// set of operations prepared once.
struct Reruns<F> {
    rerun: F,
    met: HashMap<Vec<usize>, Option<Refused>>,
}

impl<F: FnMut(&[usize]) -> Option<Refused>> Reruns<F> {
    fn new(rerun: F) -> Self {
        Self {
            rerun,
            met: HashMap::new(),
        }
    }

    /// `failure`'s refusal as what the whole plan of `count` operations
    /// meets: a rerun of them all is the preparation that failed.
    fn knowing(mut self, count: usize, failure: &Failure) -> Self {
        if let Some((refusals, _)) = &failure.refusal {
            self.met
                .insert((0..count).collect(), Some(refusals.clone()));
        }
        self
    }

    /// Whether the plan's operations `ops` alone meet `refusal`'s defect
    /// again, among every defect they meet.
    fn reproduces(&mut self, ops: &[usize], refusal: &Refusal) -> bool {
        let rerun = &mut self.rerun;
        self.met
            .entry(ops.to_vec())
            .or_insert_with(|| rerun(ops))
            .as_ref()
            .is_some_and(|met| met.iter().any(|r| r.same(refusal)))
    }
}

/// `error` with `advice` closing its message.
fn advised(mut error: EditError, advice: Option<&'static str>) -> EditError {
    if let Some(advice) = advice {
        error.message = format!("{}; {advice}", error.message);
    }
    error
}

impl From<EditError> for Box<Failure> {
    fn from(error: EditError) -> Self {
        Box::new(Failure {
            error,
            refusal: None,
            advice: None,
        })
    }
}

/// `failure`'s error, naming the operations that cause the defect its
/// candidate refusal reports first. `reruns` prepares only some of the
/// plan's operations and tells whether they meet that defect again;
/// [`culprits`] finds the operations from it, and [`naming`] names them. A
/// failure the candidate checks did not raise, or one the plan without
/// operations meets too, stays the plan's own (`operation` unset).
fn attributed<F: FnMut(&[usize]) -> Option<Refused>>(
    failure: Failure,
    count: usize,
    together: bool,
    reruns: &mut Reruns<F>,
) -> EditError {
    let Failure {
        error,
        refusal,
        advice,
    } = failure;
    let Some((refusals, outcomes)) = refusal else {
        return advised(error, advice);
    };
    let Some(first) = refusals.first() else {
        return advised(error, advice);
    };
    let Some(ops) = culprits(count, together, |ops| reruns.reproduces(ops, first)) else {
        return advised(error, advice);
    };
    advised(naming(error, &ops, &outcomes), advice)
}

/// `error` naming the operations `ops` (plan indices, the one that brings
/// the defect in first): the first becomes its `operation`, and the
/// message names each by its id and target, from `outcomes`, or by its
/// pointer (`/operations/N`) when `outcomes` has none.
fn naming(mut error: EditError, ops: &[usize], outcomes: &[EditOutcome]) -> EditError {
    let name = |i: usize| match outcomes.get(i) {
        Some(EditOutcome {
            id,
            kind,
            paragraph: Some(paragraph),
            ..
        }) => format!("{id} ({kind} on {paragraph})"),
        Some(EditOutcome { id, kind, .. }) => format!("{id} ({kind})"),
        None => format!("/operations/{i}"),
    };
    let Some(&cause) = ops.first() else {
        return error;
    };
    error.operation = Some(match outcomes.get(cause) {
        Some(outcome) => outcome.id.clone(),
        None => format!("/operations/{cause}"),
    });
    error.message = format!("{}; {} introduces it", error.message, name(cause));
    if let [_, others @ ..] = ops
        && let Some((&last, rest)) = others.split_last()
    {
        let mut message = format!("{}, together with ", error.message);
        for (n, &i) in rest.iter().enumerate() {
            if n > 0 {
                message.push_str(", ");
            }
            message.push_str(&name(i));
        }
        if !rest.is_empty() {
            message.push_str(" and ");
        }
        message.push_str(&name(last));
        error.message = message;
    }
    error
}

/// The operations (plan indices) that cause a refusal the whole plan of
/// `count` operations meets: `reproduces(ops)` tells whether the plan's
/// operations `ops` alone, in plan order, meet it again. The first is the
/// operation that brings it in: the first prefix of the plan that
/// reproduces it ends there, the prefix before it does not. Under
/// `together` (operations independent of each other), the others are the
/// earlier operations that one needs, when it does not reproduce the
/// refusal alone, in plan order, each found the same way. `None` when the
/// plan without operations reproduces it: no operation causes it.
///
/// Prefixes are bisected, so the reruns are logarithmic in `count`, once
/// for the first operation and once for each other one.
fn culprits(
    count: usize,
    together: bool,
    mut reproduces: impl FnMut(&[usize]) -> bool,
) -> Option<Vec<usize>> {
    if count == 0 || reproduces(&[]) {
        return None;
    }
    // Every prefix is a slice of this one list, built once.
    let all: Vec<usize> = (0..count).collect();
    // `lo` operations do not reproduce it; `hi` do: the whole plan does.
    let (mut lo, mut hi) = (0, count);
    while hi - lo > 1 {
        let mid = lo + (hi - lo) / 2;
        if reproduces(&all[..mid]) {
            hi = mid;
        } else {
            lo = mid;
        }
    }
    let first = hi - 1;
    let mut needed = vec![first];
    if !together {
        return Some(needed);
    }
    let with = |pool: &[usize], needed: &[usize]| {
        let mut ops: Vec<usize> = pool.iter().chain(needed).copied().collect();
        ops.sort_unstable();
        ops
    };
    // The candidates: the operations before the last one found. With all of
    // them `needed` reproduces it (the whole prefix through `first` at the
    // start, the prefix the last search ended on after it); with none,
    // `needed` alone does not.
    let mut pool = all[..first].to_vec();
    while !reproduces(&with(&[], &needed)) {
        let (mut lo, mut hi) = (0, pool.len());
        while hi - lo > 1 {
            let mid = lo + (hi - lo) / 2;
            if reproduces(&with(&pool[..mid], &needed)) {
                hi = mid;
            } else {
                lo = mid;
            }
        }
        let Some(&next) = pool.get(hi.wrapping_sub(1)) else {
            break;
        };
        needed.push(next);
        pool.truncate(hi - 1);
    }
    needed[1..].sort_unstable();
    Some(needed)
}

/// [`prepare`] without `skip_failed` and without attribution: the failure
/// keeps the candidate refusal, if one was met, for [`attributed`].
/// `known` holds what the candidate checks read in `source`.
fn prepare_once(
    source: &[u8],
    plan: &EditPlan,
    options: &PrepareOptions,
    known: &SourceChecks,
) -> Result<PreparedEdit, Box<Failure>> {
    #[cfg(test)]
    tests::PREPARATIONS.with(|count| count.set(count.get() + 1));
    check_update_fields(plan)?;
    let lowered = super::visible::lower(source, plan, options.syntax)?;
    let plan = lowered.as_ref().unwrap_or(plan);
    let mut tx = Transaction::start_quoting(source, plan, options.syntax)?;
    tx.resolve()?;
    tracked::check(&tx)?;
    let emit = tx.emits_directly()?;
    // `update_fields` compares an edit beside a range marker (#1156): a
    // refused candidate then names the route that keeps the marker.
    let compared_beside = !emit && plan.update_fields && tx.edits_beside_a_range_marker();
    tx.apply()?;
    check_dom_realized(&tx, options.exact_text)?;
    let result = produce(&mut tx, emit, options.role)?;
    let effect = primary_effect(&tx, &result)?;
    let (primary, primary_bytes) = match effect {
        Effect::Unchanged => (source.to_vec(), PrimaryBytes::Source),
        _ => match options.role {
            PrimaryRole::Clean => (result.clean.clone(), PrimaryBytes::Candidate),
            PrimaryRole::Redline => (result.redline.clone(), PrimaryBytes::Candidate),
        },
    };
    let mut checks = screen_candidates(source, &result, known)
        .and_then(|checks| {
            if compared_beside {
                check_rejected_ranges(source, &result.redline)?;
            }
            Ok(checks)
        })
        .map_err(|refusals| {
            Box::new(Failure {
                error: refusals[0].error.clone(),
                refusal: Some((refusals, tx.outcomes.clone())),
                advice: compared_beside.then_some(
                    "update_fields compares an edit beside a bookmark, comment or permission range, so drop update_fields and refresh the fields with `jubarte convert FILE --update-fields -o OUT.docx` afterwards",
                ),
            })
        })?;
    checks.operation_postconditions = true;
    if options.require_change && effect == Effect::Unchanged {
        return Err(err(
            codes::NO_CHANGES_REQUESTED,
            None,
            "the primary transaction has no document effect; --require-change was given",
        )
        .into());
    }
    Ok(PreparedEdit {
        result,
        source_sha256: source_sha256(source),
        request_sha256: source_sha256(plan.to_json().as_bytes()),
        primary_sha256: source_sha256(&primary),
        primary,
        primary_bytes,
        primary_effect: effect,
        checks,
        correspondence: None,
        skipped: Vec::new(),
    })
}

/// Whether the edit updated a field. Field updates need the renderer, so a
/// build without it updates none.
#[cfg(feature = "render")]
fn fields_updated(report: &super::EditReport) -> bool {
    !report.fields.is_empty()
}

#[cfg(not(feature = "render"))]
fn fields_updated(_report: &super::EditReport) -> bool {
    false
}

/// The primary transaction's document effect. Aggregated text equality
/// alone never proves `unchanged`: revision resolution, comments, thread
/// state, formatting, media, fields and structure are effects too.
fn primary_effect(tx: &Transaction<'_>, result: &EditResult) -> Result<Effect, EditError> {
    let report = &result.report;
    if !report.resolved_revisions.is_empty()
        || report.revisions.total > 0
        || report.paragraphs.from != report.paragraphs.to
        || fields_updated(report)
        || report.comments_added > 0
        || tx.watermark.is_some()
        || tx.settings.is_some()
        || tx.notes_story.is_some()
    {
        return Ok(Effect::Changed);
    }
    for (op, resolved) in &tx.resolved {
        // Only plain text operations can be truly no-effect; a comment,
        // thread, formatting, control, table or structural resolution is an
        // effect even when the aggregated text is equal. A `redact`
        // resolves as text but empties the base too, so the diff misses it.
        if !matches!(resolved, Resolved::Text { .. })
            || !matches!(
                tx.plan.operations[*op].kind,
                OperationKind::Replace { .. }
                    | OperationKind::Insert { .. }
                    | OperationKind::Delete { .. }
                    | OperationKind::Rewrite { .. }
            )
        {
            return Ok(Effect::Changed);
        }
    }
    // A text edit that meant to change the text is an effect even when the
    // engine wrote nothing for it.
    if expected_texts(tx, false)?.into_iter().any(|(p, e)| {
        matches!(e.scope, ExpectedScope::Paragraph) && e.text != tx.projections[p].text
    }) {
        return Ok(Effect::Changed);
    }
    Ok(Effect::Unchanged)
}

/// Reject All of `redline` gives the source back, so it keeps each of the
/// source's bookmarks (by name; Word's own `_GoBack` aside), comment ranges
/// and permission ranges. Compare places them by the words around them and
/// can carry one into an insertion, which Reject All then removes: the
/// TOC entry linking to a heading's `_Toc` bookmark reads "Error! Bookmark
/// not defined." (#1156).
fn check_rejected_ranges(source: &[u8], redline: &[u8]) -> Result<(), Refused> {
    let rejected = crate::document_comparer::reject_revisions(redline).map_err(|e| {
        vec![refused(
            "redline",
            "REJECT_FAILED",
            "",
            String::new(),
            format!("the redline cannot be rejected: {e}"),
        )]
    })?;
    let (before, after) = (range_markers(source), range_markers(&rejected));
    let lost = before
        .bookmarks
        .difference(&after.bookmarks)
        .map(|name| format!("bookmark {name}"))
        .chain((after.comments < before.comments).then(|| "a comment's range".to_string()))
        .chain((after.permissions < before.permissions).then(|| "a permission range".to_string()))
        .collect::<Vec<_>>();
    if lost.is_empty() {
        return Ok(());
    }
    // The ranges lost, together, are the defect: a rerun that loses only
    // some of them meets another.
    let lost = lost.join(", ");
    Err(vec![refused(
        "redline",
        "RANGE_LOST",
        "",
        lost.clone(),
        format!("Reject All of the redline drops {lost} the source holds"),
    )])
}

/// The range markers of a package's stories: bookmark names, and how many
/// comment and permission ranges open.
#[derive(Default)]
struct RangeMarkers {
    bookmarks: std::collections::BTreeSet<String>,
    comments: usize,
    permissions: usize,
}

fn range_markers(docx: &[u8]) -> RangeMarkers {
    let mut markers = RangeMarkers::default();
    let Ok(opened) = crate::inspect::Opened::open(docx) else {
        return markers;
    };
    let mut parts = vec![opened.main.clone()];
    parts.extend(opened.story_parts().into_iter().map(|(_, _, part)| part));
    for part in parts {
        let Ok((dom, _, root)) = crate::inspect::parse_part(&opened.pkg, &part) else {
            continue;
        };
        for start in dom.descendants(root, Some(&W::bookmark_start())) {
            if let Some(name) = dom.attribute(start, &W::name("name"))
                && name != "_GoBack"
            {
                markers.bookmarks.insert(name.to_string());
            }
        }
        markers.comments += dom.descendants(root, Some(&W::comment_range_start())).len();
        markers.permissions += dom.descendants(root, Some(&W::perm_start())).len();
    }
    markers
}

/// Findings on the input and what each serialized artifact adds. The
/// baseline discloses without blocking; an introduced blocking finding
/// fails preparation.
#[cfg(test)]
fn check_candidates(source: &[u8], result: &EditResult) -> Result<CandidateChecks, EditError> {
    screen_candidates(source, result, &SourceChecks::default())
        .map_err(|refusals| refusals[0].error.clone())
}

/// The candidate gates, with every defect they meet (the test-only
/// `check_candidates` keeps the first), in
/// the order they meet them: the clean copy's, then the redline's; in each,
/// the duplicate revision ids, then the introduced blocking findings. The
/// first is the one the refusal reports, as when the gates stopped there;
/// the others let a rerun tell whether it meets that defect again.
/// `known` holds what the gates read in `source`.
fn screen_candidates(
    source: &[u8],
    result: &EditResult,
    known: &SourceChecks,
) -> Result<CandidateChecks, Refused> {
    // A source the validator cannot read gives no trustworthy baseline:
    // an empty one would call every candidate finding introduced, so with
    // no baseline nothing is claimed introduced (the candidate's own
    // validation below still gates what the edit produced).
    let baseline = known
        .findings
        .get_or_init(|| validate::validate(source).ok())
        .as_ref();
    let mut refusals = Vec::new();
    let mut introduced = Vec::new();
    let mut unresolved = Vec::new();
    for (artifact, bytes) in [("clean copy", &result.clean), ("redline", &result.redline)] {
        // An unreadable artifact ends the checks; the defects met before
        // it still come first.
        let findings = match validate::validate(bytes) {
            Ok(findings) => findings,
            Err(e) => {
                refusals.push(refused(
                    artifact,
                    "UNREADABLE",
                    "",
                    String::new(),
                    format!("the serialized candidate cannot be validated: {e}"),
                ));
                return Err(refusals);
            }
        };
        // Counted only when a candidate repeats an id at all: its own
        // `DUPLICATE_REVISION_ID` reads the same parts, records and keys.
        if findings.iter().any(|f| f.code == "DUPLICATE_REVISION_ID")
            && let Some(source_ids) = known
                .revision_ids
                .get_or_init(|| validate::revision_id_occurrences(source).ok())
        {
            match check_revision_ids(source_ids, artifact, bytes) {
                Ok(duplicates) => refusals.extend(duplicates),
                Err(unreadable) => {
                    refusals.extend(unreadable);
                    return Err(refusals);
                }
            }
        }
        for finding in &findings {
            if !unresolved
                .iter()
                .any(|f| finding_key(f) == finding_key(finding))
            {
                unresolved.push(finding.clone());
            }
        }
        let Some(baseline) = baseline else {
            continue;
        };
        // A block placed before a finding shifts its sibling indices. The
        // finding is still the one the source had when a stable anchor
        // (paragraph id, revision id, or that element's own text) finds it.
        // Only findings of a code the candidate seems to introduce are
        // relocated, so an edit that introduces nothing reads no XML here
        // (#2127 review).
        let suspects: HashSet<String> = introduced_findings(baseline, &findings)
            .into_iter()
            .map(|finding| finding.code)
            .collect();
        let baseline = if suspects.is_empty() {
            baseline.to_vec()
        } else {
            relocate_findings(source, bytes, baseline, &suspects)
        };
        for finding in introduced_findings(&baseline, &findings) {
            if blocking(&finding) {
                refusals.push(refused(
                    artifact,
                    &finding.code,
                    &finding.part,
                    finding.unnumbered(),
                    format!(
                        "the edit introduces {} in {}#{}: {}",
                        finding.code, finding.part, finding.location, finding.message
                    ),
                ));
                continue;
            }
            if !introduced
                .iter()
                .any(|f| finding_key(f) == finding_key(&finding))
            {
                introduced.push(finding);
            }
        }
    }
    if !refusals.is_empty() {
        return Err(refusals);
    }
    Ok(CandidateChecks {
        baseline: baseline.cloned().unwrap_or_default(),
        introduced,
        unresolved,
        integrity: true,
        operation_postconditions: false,
    })
}

struct ParsedPart {
    dom: crate::xmllinq::Dom,
    root: crate::xmllinq::NodeId,
    /// Same-name children per parent, with each child's text and revision
    /// marks, built once per part: every finding's walk reads them, and a
    /// part with many findings would otherwise rescan its siblings for
    /// each (#2127 review).
    siblings: HashMap<(crate::xmllinq::NodeId, String, String), std::rc::Rc<Siblings>>,
}

struct Siblings {
    nodes: Vec<crate::xmllinq::NodeId>,
    texts: Vec<String>,
    marks: Vec<Vec<(String, String)>>,
    /// How many siblings carry each text.
    text_counts: HashMap<String, usize>,
}

impl ParsedPart {
    fn siblings(
        &mut self,
        parent: crate::xmllinq::NodeId,
        namespace: &str,
        local: &str,
    ) -> std::rc::Rc<Siblings> {
        let key = (parent, namespace.to_string(), local.to_string());
        if let Some(found) = self.siblings.get(&key) {
            return std::rc::Rc::clone(found);
        }
        let nodes = named_children(&self.dom, parent, namespace, local);
        let texts: Vec<String> = nodes.iter().map(|&node| self.dom.value(node)).collect();
        let marks = nodes
            .iter()
            .map(|&node| revision_marks(&self.dom, node))
            .collect();
        let mut text_counts = HashMap::new();
        for text in &texts {
            *text_counts.entry(text.clone()).or_insert(0) += 1;
        }
        let built = std::rc::Rc::new(Siblings {
            nodes,
            texts,
            marks,
            text_counts,
        });
        self.siblings.insert(key, std::rc::Rc::clone(&built));
        built
    }
}

/// Baseline findings of a `suspects` code with locations rewritten to where
/// the same element sits in `candidate`. An element the candidate no longer
/// has, or that cannot be told from a sibling, keeps the location the
/// source reported. Both packages are read as Transitional, as validation
/// reads a Strict one, so the locations name the same elements (#2127
/// review).
fn relocate_findings(
    source: &[u8],
    candidate: &[u8],
    baseline: &[Finding],
    suspects: &HashSet<String>,
) -> Vec<Finding> {
    let (Ok(source), Ok(candidate)) = (
        crate::strict_translation::strict_to_transitional_docx(source),
        crate::strict_translation::strict_to_transitional_docx(candidate),
    ) else {
        return baseline.to_vec();
    };
    let Ok(source_pkg) = crate::opc::PartFs::open(&source) else {
        return baseline.to_vec();
    };
    let Ok(candidate_pkg) = crate::opc::PartFs::open(&candidate) else {
        return baseline.to_vec();
    };
    let mut source_parts = HashMap::new();
    let mut candidate_parts = HashMap::new();
    baseline
        .iter()
        .map(|finding| {
            let mut finding = finding.clone();
            if !suspects.contains(&finding.code) {
                return finding;
            }
            if let Some(location) = moved_location(
                &source_pkg,
                &candidate_pkg,
                &mut source_parts,
                &mut candidate_parts,
                &finding.part,
                &finding.location,
            ) {
                finding.r#ref.clone_from(&location);
                finding.location = location;
            }
            finding
        })
        .collect()
}

fn ensure_part(
    pkg: &crate::opc::PartFs,
    cache: &mut HashMap<String, ParsedPart>,
    part: &str,
) -> Option<()> {
    if cache.contains_key(part) {
        return Some(());
    }
    let (dom, _, root) = crate::inspect::parse_part(pkg, part).ok()?;
    cache.insert(
        part.to_string(),
        ParsedPart {
            dom,
            root,
            siblings: HashMap::new(),
        },
    );
    Some(())
}

fn moved_location(
    source_pkg: &crate::opc::PartFs,
    candidate_pkg: &crate::opc::PartFs,
    source_parts: &mut HashMap<String, ParsedPart>,
    candidate_parts: &mut HashMap<String, ParsedPart>,
    part: &str,
    location: &str,
) -> Option<String> {
    if !location.contains('[') {
        return None;
    }
    ensure_part(source_pkg, source_parts, part)?;
    ensure_part(candidate_pkg, candidate_parts, part)?;
    let source = source_parts.get_mut(part)?;
    let candidate = candidate_parts.get_mut(part)?;
    let mut source_node = source.root;
    let mut candidate_node = candidate.root;
    let mut steps = Vec::new();
    for step in location.split('/') {
        let (namespace, local, index) = parse_step(step)?;
        let source_siblings = source.siblings(source_node, &namespace, &local);
        let candidate_siblings = candidate.siblings(candidate_node, &namespace, &local);
        let source_child = *source_siblings.nodes.get(index)?;
        let new_index = match_child(
            &source.dom,
            index,
            &source_siblings,
            &candidate.dom,
            &candidate_siblings,
        )?;
        let candidate_child = candidate_siblings.nodes[new_index];
        let name = step.split_once('[').map_or(step, |(name, _)| name);
        steps.push(format!("{name}[{new_index}]"));
        source_node = source_child;
        candidate_node = candidate_child;
    }
    let relocated = steps.join("/");
    (relocated != location).then_some(relocated)
}

fn parse_step(step: &str) -> Option<(String, String, usize)> {
    let (name, rest) = step.strip_suffix(']')?.split_once('[')?;
    let index = rest.parse().ok()?;
    let (namespace, local) = match name.split_once(':') {
        Some((prefix, local)) => (
            crate::xmllinq::serialize::well_known_namespace(prefix)?.to_string(),
            local.to_string(),
        ),
        None => (String::new(), name.to_string()),
    };
    Some((namespace, local, index))
}

fn named_children(
    dom: &crate::xmllinq::Dom,
    parent: crate::xmllinq::NodeId,
    namespace: &str,
    local: &str,
) -> Vec<crate::xmllinq::NodeId> {
    dom.elements(parent, None)
        .into_iter()
        .filter(|&element| {
            dom.name(element).is_some_and(|name| {
                name.local_name() == local && name.namespace_name() == namespace
            })
        })
        .collect()
}

/// The index among `candidate` of the sibling that is `source.nodes[index]`,
/// told apart by paragraph id, its own revision, its revision marks, or its
/// text (an empty text too, #2127 review), each only when that anchor is
/// unique on both sides; else by position when no sibling came or went.
fn match_child(
    source_dom: &crate::xmllinq::Dom,
    index: usize,
    source: &Siblings,
    candidate_dom: &crate::xmllinq::Dom,
    candidate: &Siblings,
) -> Option<usize> {
    let node = source.nodes[index];
    let para_id = crate::namespaces::W14::para_id();
    if let Some(id) = source_dom.attribute(node, &para_id)
        && let Some(found) = sole_index(&candidate.nodes, |sibling| {
            candidate_dom.attribute(sibling, &para_id) == Some(id)
        })
        && sole_index(&source.nodes, |sibling| {
            source_dom.attribute(sibling, &para_id) == Some(id)
        })
        .is_some()
    {
        return Some(found);
    }
    if let Some(revision) = own_revision(source_dom, node)
        && let Some(found) = sole_index(&candidate.nodes, |sibling| {
            own_revision(candidate_dom, sibling).as_ref() == Some(&revision)
        })
    {
        return Some(found);
    }
    let marks = &source.marks[index];
    if !marks.is_empty()
        && source.marks.iter().filter(|other| *other == marks).count() == 1
        && let Some(found) = unique_position(&candidate.marks, marks)
    {
        return Some(found);
    }
    let text = &source.texts[index];
    if source.text_counts.get(text) == Some(&1) && candidate.text_counts.get(text) == Some(&1) {
        return candidate.texts.iter().position(|other| other == text);
    }
    (source.nodes.len() == candidate.nodes.len()).then_some(index)
}

fn unique_position<T: PartialEq>(items: &[T], wanted: &T) -> Option<usize> {
    let mut found = None;
    for (at, item) in items.iter().enumerate() {
        if item != wanted {
            continue;
        }
        if found.is_some() {
            return None;
        }
        found = Some(at);
    }
    found
}

fn sole_index(
    nodes: &[crate::xmllinq::NodeId],
    mut pred: impl FnMut(crate::xmllinq::NodeId) -> bool,
) -> Option<usize> {
    let mut found = None;
    for (at, &node) in nodes.iter().enumerate() {
        if !pred(node) {
            continue;
        }
        if found.is_some() {
            return None;
        }
        found = Some(at);
    }
    found
}

fn own_revision(
    dom: &crate::xmllinq::Dom,
    node: crate::xmllinq::NodeId,
) -> Option<(String, String)> {
    let id = dom.attribute(node, &W::id())?;
    dom.attribute(node, &W::author())?;
    Some((dom.name(node)?.local_name().to_string(), id.to_string()))
}

fn revision_marks(
    dom: &crate::xmllinq::Dom,
    node: crate::xmllinq::NodeId,
) -> Vec<(String, String)> {
    let mut marks: Vec<(String, String)> = std::iter::once(node)
        .chain(dom.descendants(node, None))
        .filter_map(|element| own_revision(dom, element))
        .collect();
    marks.sort();
    marks.dedup();
    marks
}

/// A candidate may not give a revision id a same-kind duplicate the source
/// did not have: per part, record type and id, an id that occurs more than
/// once may occur no more often than in the source. Inherited duplicates
/// pass, healing one hides no collision under another, and kinds may share
/// an id. `jubarte validate` still calls the duplicate non-fatal: Word
/// opens it; publication refuses it. `artifact` names the candidate; every
/// duplicate it introduces is one defect, in key order. An id the source
/// holds is the defect's subject; one the edit minted is not, since a rerun
/// of fewer operations mints others.
fn check_revision_ids(
    source: &HashMap<(String, String), usize>,
    artifact: &'static str,
    candidate: &[u8],
) -> Result<Vec<Refusal>, Refused> {
    let occurrences = validate::revision_id_occurrences(candidate).map_err(|e| {
        vec![refused(
            artifact,
            "UNREADABLE",
            "",
            String::new(),
            format!("the serialized candidate cannot be read: {e}"),
        )]
    })?;
    let mut introduced: Vec<_> = occurrences
        .iter()
        .filter(|&(key, &count)| count > 1 && count > source.get(key).copied().unwrap_or(0))
        .collect();
    introduced.sort();
    Ok(introduced
        .into_iter()
        .map(|(entry, &count)| {
            let (part, key) = entry;
            let mut fields = key.splitn(3, ':');
            let (kind, mark, id) = (
                fields.next().unwrap_or_default(),
                fields.next() == Some("true"),
                fields.next().unwrap_or_default(),
            );
            let mark = if mark { " on a mark" } else { "" };
            let had = source.get(entry).copied().unwrap_or(0);
            let subject = if had > 0 {
                format!("w:{kind}{mark} w:id {id}")
            } else {
                format!("w:{kind}{mark}")
            };
            refused(
                artifact,
                "DUPLICATE_REVISION_ID",
                part,
                subject,
                format!(
                    "the edit introduces a duplicate revision id in the {artifact}: w:{kind}{mark} w:id {id} occurs {count} times in {part} (the source has {had})"
                ),
            )
        })
        .collect())
}

/// What one paragraph reads as once the plan applies, and whether symbol
/// placeholders are out of the comparison.
struct ExpectedText {
    /// The operation that defined it (`op-N`), for error reports.
    op: String,
    /// The expected exact logical text.
    text: String,
    /// Compare without U+FFFC placeholders: a `rewrite` keeps the
    /// paragraph's symbols wherever they sit.
    ignore_symbols: bool,
    /// What `text` covers.
    scope: ExpectedScope,
}

/// The part of a paragraph an [`ExpectedText`] covers.
#[derive(Clone, Copy)]
enum ExpectedScope {
    /// The paragraph's projected text.
    Paragraph,
    /// The content of a content control: the value indexes
    /// `Transaction::controls`. A `fill_control` replaces its control's
    /// projected text, which the span composition cannot predict, so the
    /// fill's own requested value is the postcondition instead.
    Control(usize),
}

/// The exact text each paragraph must read as in the candidate: resolution
/// composed over the base projection, or, under `exact`, the `rewrite`'s
/// requested text itself — so a requested change the algorithm normalizes
/// away is detected instead of passing as a no-op.
fn expected_texts(
    tx: &Transaction<'_>,
    exact: bool,
) -> Result<Vec<(usize, ExpectedText)>, EditError> {
    /// `para` → (first op id, `(start, end, replacement, op, restore,
    /// attach_before)` spans).
    type Spans = (String, Vec<(usize, usize, String, usize, bool, bool)>);
    let mut edits: BTreeMap<usize, Spans> = BTreeMap::new();
    let mut merges = Vec::new();
    let mut filled: HashSet<usize> = HashSet::new();
    // `(para, control, requested text, op id)` — a fill's own postcondition.
    let mut fills = Vec::new();
    for (op, resolved) in &tx.resolved {
        match resolved {
            Resolved::Text {
                para,
                start,
                end,
                replacement,
                attach_before,
                ..
            } => {
                let entry = edits
                    .entry(*para)
                    .or_insert_with(|| (tx.outcomes[*op].id.clone(), Vec::new()));
                entry.1.push((
                    *start,
                    *end,
                    replacement.clone(),
                    *op,
                    false,
                    *attach_before,
                ));
            }
            // A restore's copy is text inserted where the deletion is.
            Resolved::Restore { para, at, text, .. } => {
                let entry = edits
                    .entry(*para)
                    .or_insert_with(|| (tx.outcomes[*op].id.clone(), Vec::new()));
                entry.1.push((*at, *at, text.clone(), *op, true, false));
            }
            Resolved::MergeParagraphs {
                para,
                next,
                separator,
                ..
            } => {
                merges.push((*para, *next, separator.clone(), tx.outcomes[*op].id.clone()));
            }
            Resolved::FillControl {
                control,
                paragraphs,
                value,
                ..
            } => {
                let text = match value {
                    controls::FillValue::Text(text) | controls::FillValue::Date { text, .. } => {
                        text.clone()
                    }
                    controls::FillValue::Checked { glyph, .. } => glyph.to_string(),
                };
                for para in paragraphs {
                    fills.push((*para, *control, text.clone(), tx.outcomes[*op].id.clone()));
                }
                filled.extend(paragraphs.iter().copied());
            }
            _ => {}
        }
    }
    let mut expected = BTreeMap::new();
    for (para, (op, mut spans)) in edits {
        let rewrite = if exact { rewrite_text(tx, para) } else { None };
        let rewrite_op = rewrite.as_ref().map(|&(op, _)| op);
        let base = &tx.projections[para].text;
        // A restore's copy follows its deletion, ahead of the visible text
        // that starts where the deletion is; other spans keep their order,
        // but in an empty paragraph the start edge's text comes first, as
        // `sort_edits` applies it (#1303).
        let empty = base.is_empty();
        // Match the writers' source-range order: an insertion at a removal's
        // start precedes that removal, independent of plan order (#1922).
        spans.sort_by_key(|&(start, end, _, op, restore, after)| {
            (start, !restore, end, empty && after, op)
        });
        let mut text = String::with_capacity(base.len());
        // The rewrite's own spans alone, composed on the base: a rewrite
        // beside co-resident edits must still realize its request, or the
        // failure is the rewrite's, not the composed text's.
        let mut own = String::with_capacity(base.len());
        let mut at = 0;
        let mut own_at = 0;
        let mut previous_op = None;
        for (start, end, replacement, span_op, ..) in spans {
            let conflict = || {
                let previous = previous_op.unwrap_or(span_op);
                tx.conflict(
                    span_op,
                    &format!(
                        "{} and {} have text ranges that cannot be composed in p{para}",
                        tx.outcomes[previous].id, tx.outcomes[span_op].id
                    ),
                )
            };
            // Check every bound, including UTF-8 boundaries. Even an invalid
            // resolved span must refuse rather than reach slice_error_fail.
            let gap = base.get(at..start).ok_or_else(conflict)?;
            base.get(start..end).ok_or_else(conflict)?;
            text.push_str(gap);
            text.push_str(&replacement);
            at = end;
            if Some(span_op) == rewrite_op {
                own.push_str(base.get(own_at..start).ok_or_else(conflict)?);
                own.push_str(&replacement);
                own_at = end;
            }
            previous_op = Some(span_op);
        }
        // Both cursors came from a checked span (or remain at zero).
        text.push_str(base.get(at..).ok_or_else(|| {
            tx.conflict(
                previous_op.unwrap_or(0),
                "text range ends outside the paragraph",
            )
        })?);
        own.push_str(base.get(own_at..).ok_or_else(|| {
            tx.conflict(
                rewrite_op.unwrap_or(0),
                "rewrite range ends outside the paragraph",
            )
        })?);
        if let Some((rewrite_op, requested)) = rewrite {
            let entry = if own == requested {
                // The rewrite's own edits realize its request: the
                // postcondition is the full composition — the request with
                // every co-resident edit applied — never the bare request
                // discarding a neighbour's effect.
                ExpectedText {
                    op,
                    text,
                    ignore_symbols: true,
                    scope: ExpectedScope::Paragraph,
                }
            } else {
                ExpectedText {
                    op: tx.outcomes[rewrite_op].id.clone(),
                    text: requested,
                    ignore_symbols: true,
                    scope: ExpectedScope::Paragraph,
                }
            };
            expected.insert(para, entry);
            continue;
        }
        expected.insert(
            para,
            ExpectedText {
                op,
                text,
                ignore_symbols: false,
                scope: ExpectedScope::Paragraph,
            },
        );
    }
    // A rewrite that resolved to no edits still has a postcondition.
    if exact {
        for (i, op) in tx.plan.operations.iter().enumerate() {
            let OperationKind::Rewrite {
                paragraph, text, ..
            } = &op.kind
            else {
                continue;
            };
            if comment_text_of(paragraph).is_some() {
                continue;
            }
            let Some(para) = outcome_paragraph(tx, i) else {
                continue;
            };
            expected.entry(para).or_insert_with(|| ExpectedText {
                op: tx.outcomes[i].id.clone(),
                text: text.clone(),
                ignore_symbols: true,
                scope: ExpectedScope::Paragraph,
            });
        }
    }
    for (para, next, separator, op) in merges {
        let survivor = expected
            .get(&next)
            .map(|e| e.text.clone())
            .unwrap_or_else(|| tx.projections[next].text.clone());
        let first = expected
            .get(&para)
            .map(|e| e.text.clone())
            .unwrap_or_else(|| tx.projections[para].text.clone());
        expected.insert(
            next,
            ExpectedText {
                op,
                text: format!("{first}{separator}{survivor}"),
                ignore_symbols: false,
                scope: ExpectedScope::Paragraph,
            },
        );
        expected.remove(&para);
    }
    for para in filled {
        expected.remove(&para);
    }
    let mut out: Vec<(usize, ExpectedText)> = expected.into_iter().collect();
    for (para, control, text, op) in fills {
        out.push((
            para,
            ExpectedText {
                op,
                text,
                ignore_symbols: false,
                scope: ExpectedScope::Control(control),
            },
        ));
    }
    out.sort_by_key(|(para, _)| *para);
    Ok(out)
}

/// The first `rewrite` op on `para` and the text it asks for, or `None`
/// when no rewrite op resolved onto it (`cN.text` rewrites excluded: their
/// target is the covered span, not the paragraph).
fn rewrite_text(tx: &Transaction<'_>, para: usize) -> Option<(usize, String)> {
    tx.plan.operations.iter().enumerate().find_map(|(i, op)| {
        let OperationKind::Rewrite {
            paragraph, text, ..
        } = &op.kind
        else {
            return None;
        };
        if comment_text_of(paragraph).is_some() {
            return None;
        }
        (outcome_paragraph(tx, i) == Some(para)).then(|| (i, text.clone()))
    })
}

/// The paragraph index an outcome names (`p3`, `header1.p1`, …) inside the
/// transaction's story list.
fn outcome_paragraph(tx: &Transaction<'_>, op: usize) -> Option<usize> {
    let id = tx.outcomes[op].paragraph.as_deref()?;
    let (story, index) = crate::ids::paragraph_place(id)?;
    let story = tx.stories.iter().position(|s| s.id == story)?;
    tx.paragraph_story
        .iter()
        .position(|&(s, i)| s == story && i == index)
}

/// `para`'s node still belongs to its story in the mutated DOM.
fn attached(tx: &Transaction<'_>, para: usize) -> bool {
    let node = tx.paragraph_nodes[para];
    let (story, _) = tx.paragraph_story[para];
    let root = tx.stories[story].root;
    node == root || tx.opened.dom.ancestors(node, None).contains(&root)
}

/// Each expected paragraph reads exactly so after apply, in the DOM the
/// candidate is serialized from. A realized difference is
/// `POSTCONDITION_FAILED` with the plan's outcomes, never a silent miss.
pub(super) fn check_dom_realized(tx: &Transaction<'_>, exact: bool) -> Result<(), EditError> {
    for (para, expected) in expected_texts(tx, exact)? {
        if !attached(tx, para) {
            // Merged-away or deleted paragraphs carry no text postcondition;
            // merge survivors are checked on their own entry.
            continue;
        }
        let realized = match expected.scope {
            ExpectedScope::Paragraph => {
                project_paragraph(&tx.opened.dom, tx.paragraph_nodes[para]).text
            }
            ExpectedScope::Control(control) => control_text(&tx.opened.dom, tx.controls[control]),
        };
        let (want, got) = if expected.ignore_symbols {
            (
                expected.text.replace('\u{FFFC}', ""),
                realized.replace('\u{FFFC}', ""),
            )
        } else {
            (expected.text.clone(), realized)
        };
        if want != got {
            let mut e = err(
                codes::POSTCONDITION_FAILED,
                Some(&expected.op),
                format!(
                    "the requested text was not realized in paragraph {}",
                    crate::ids::paragraph_id(
                        &tx.stories[tx.paragraph_story[para].0].id,
                        tx.paragraph_story[para].1,
                    ),
                ),
            );
            e.outcomes = tx.outcomes.clone();
            return Err(e);
        }
    }
    Ok(())
}

/// The projected text of a content control (`w:sdtContent` content).
fn control_text(dom: &crate::xmllinq::Dom, sdt: crate::xmllinq::NodeId) -> String {
    dom.element(sdt, &crate::namespaces::W::sdt_content())
        .map(|content| project_paragraph(dom, content).text)
        .unwrap_or_default()
}

/// The serialized paragraphs of every story the plan wrote read exactly
/// as the DOM they were written from — the body always, plus each header,
/// footer and notes story an operation touched: a character the writer
/// dropped or restated is `POSTCONDITION_FAILED`.
pub(super) fn check_serialized_stories(
    tx: &Transaction<'_>,
    clean: &[u8],
) -> Result<(), EditError> {
    let post = body_paragraph_nodes(&tx.opened.dom, tx.opened.body);
    let reopened = crate::inspect::paragraphs(clean)
        .map_err(|e| err("POSTCONDITION_FAILED", None, e.to_string()))?;
    if reopened.len() != post.len() {
        return Err(err(
            codes::POSTCONDITION_FAILED,
            None,
            format!(
                "the serialized candidate holds {} body paragraphs, the prepared document {}",
                reopened.len(),
                post.len(),
            ),
        ));
    }
    for (node, paragraph) in post.iter().zip(&reopened) {
        let expected = project_paragraph(&tx.opened.dom, *node).text;
        if expected != paragraph.text {
            return Err(err(
                codes::POSTCONDITION_FAILED,
                None,
                format!(
                    "the serialized candidate's paragraph {} reads differently than prepared",
                    paragraph.id,
                ),
            ));
        }
    }
    let written: Vec<usize> = tx
        .written_stories
        .iter()
        .copied()
        .filter(|&story| story != 0)
        .collect();
    if written.is_empty() {
        return Ok(());
    }
    let reopened = crate::inspect::stories(clean)
        .map_err(|e| err("POSTCONDITION_FAILED", None, e.to_string()))?;
    for story in written {
        let StoryPart { part, root, .. } = &tx.stories[story];
        let post = crate::inspect::story_paragraph_nodes(&tx.opened.dom, *root);
        let Some(reopened) = reopened.iter().find(|s| &s.part == part) else {
            return Err(err(
                codes::POSTCONDITION_FAILED,
                None,
                format!("the serialized candidate lost the {part} story"),
            ));
        };
        if reopened.paragraphs.len() != post.len() {
            return Err(err(
                codes::POSTCONDITION_FAILED,
                None,
                format!(
                    "the serialized candidate holds {} {part} paragraphs, the prepared document {}",
                    reopened.paragraphs.len(),
                    post.len(),
                ),
            ));
        }
        for (node, paragraph) in post.iter().zip(&reopened.paragraphs) {
            let expected = project_paragraph(&tx.opened.dom, *node).text;
            if expected != paragraph.text {
                return Err(err(
                    codes::POSTCONDITION_FAILED,
                    None,
                    format!(
                        "the serialized candidate's paragraph {} reads differently than prepared",
                        paragraph.id,
                    ),
                ));
            }
        }
    }
    Ok(())
}

/// Compare the published redline's accepted text with the final clean copy.
fn check_redline_stories(
    tx: &Transaction<'_>,
    clean: &[u8],
    redline: &[u8],
) -> Result<(), EditError> {
    fn compare(
        part: &str,
        clean: &[crate::inspect::Paragraph],
        redline: &[crate::inspect::Paragraph],
    ) -> Result<(), EditError> {
        if clean.len() != redline.len() {
            return Err(err(
                codes::POSTCONDITION_FAILED,
                None,
                format!(
                    "the accepted redline's {part} paragraph {} differs: {} paragraphs, the clean candidate {}",
                    clean.len().min(redline.len()),
                    redline.len(),
                    clean.len(),
                ),
            ));
        }
        for (expected, actual) in clean.iter().zip(redline) {
            if expected.text != actual.text {
                return Err(err(
                    codes::POSTCONDITION_FAILED,
                    None,
                    format!(
                        "the accepted redline's {part} paragraph {} reads differently than the clean candidate",
                        expected.id,
                    ),
                ));
            }
        }
        Ok(())
    }

    // Under keep, even the clean copy retains the source's revisions.
    // Settle both copies for comparison without changing the published bytes.
    let accept = |bytes: &[u8]| {
        crate::document_comparer::accept_revisions(bytes)
            .map_err(|e| err(codes::POSTCONDITION_FAILED, None, e.to_string()))
    };
    let clean = accept(clean)?;
    let redline = accept(redline)?;
    let paragraphs = |bytes: &[u8]| {
        crate::inspect::paragraphs(bytes)
            .map_err(|e| err(codes::POSTCONDITION_FAILED, None, e.to_string()))
    };
    compare(
        &tx.opened.main,
        &paragraphs(&clean)?,
        &paragraphs(&redline)?,
    )?;
    if tx.written_stories.iter().all(|&story| story == 0) {
        return Ok(());
    }
    let stories = |bytes: &[u8]| {
        crate::inspect::stories(bytes)
            .map_err(|e| err(codes::POSTCONDITION_FAILED, None, e.to_string()))
    };
    let clean = stories(&clean)?;
    let redline = stories(&redline)?;
    for &story in &tx.written_stories {
        if story == 0 {
            continue;
        }
        let part = &tx.stories[story].part;
        let expected = clean.iter().find(|s| &s.part == part);
        let actual = redline.iter().find(|s| &s.part == part);
        let (Some(expected), Some(actual)) = (expected, actual) else {
            return Err(err(
                codes::POSTCONDITION_FAILED,
                None,
                format!("the accepted candidates lost the {part} story at paragraph 0"),
            ));
        };
        compare(part, &expected.paragraphs, &actual.paragraphs)?;
    }
    Ok(())
}

/// Serialize and check the prepared DOM before field updates, then check
/// the published redline against the final clean candidate.
fn produce(
    tx: &mut Transaction<'_>,
    emit: bool,
    role: PrimaryRole,
) -> Result<EditResult, EditError> {
    let plan = tx.plan;
    let (clean, marked) = tx.finish()?;
    check_serialized_stories(tx, &clean)?;
    // A reworded or deleted comment makes the comparer's base a copy; the
    // emitter writes those threads itself, as under keep.
    if plan.existing_revisions == ExistingRevisions::Keep
        || (emit
            && (tx.edits_only_comments()
                || matches!(tx.commented_base()?, std::borrow::Cow::Borrowed(_))))
    {
        let result = tracked::result(tx, clean)?;
        tx.check_redactions(&[&result.clean, &result.redline])?;
        if role == PrimaryRole::Redline {
            check_redline_stories(tx, &result.clean, &result.redline)?;
        }
        return Ok(result);
    }
    // Only the field refresh below rewrites the candidates.
    #[cfg(feature = "render")]
    let (mut clean, mut marked) = (clean, marked);
    #[cfg(feature = "render")]
    let mut fields = Vec::new();
    #[cfg(feature = "render")]
    if plan.update_fields {
        let refresh = |bytes: &[u8]| {
            crate::fields::update_fields(bytes)
                .map_err(|e| err("FIELDS_FAILED", None, e.to_string()))
        };
        let updated = refresh(&clean)?;
        clean = updated.docx;
        fields = updated.fields;
        if let Some(bytes) = &marked {
            marked = Some(refresh(bytes)?.docx);
        }
    }
    // Accept All gives back `clean`: the comparer leaves what the plan did
    // not touch as the source has it (#1868).
    let settings = WmlComparerSettings {
        author_for_revisions: plan.author.clone(),
        date_time_for_revisions: tx.date.clone(),
        preserve_untouched: true,
        ..WmlComparerSettings::default()
    };
    let revised = marked.as_deref().unwrap_or(&clean);
    let base = tx.commented_base()?;
    let mut redline =
        crate::document_comparer::compare_documents_for_edit(&base, revised, &settings)
            .map_err(|e| err("COMPARE_FAILED", None, e.to_string()))?;
    if tx
        .resolved
        .iter()
        .any(|(_, r)| matches!(r, Resolved::PageSetup { .. }))
    {
        redline = sections::record_mid_changes(&redline, &base, &plan.author, &tx.date)?;
    }
    redline = super::ranges::restore(&redline, &base, revised, &plan.author)?;
    if marked.is_some() {
        let (rewritten, fallbacks) = whole::rewrite(
            &redline,
            (&base, revised),
            &tx.whole_marks,
            &plan.author,
            &tx.date,
        )?;
        redline = rewritten;
        for (op, reason) in fallbacks {
            tx.outcomes[op].message = Some(format!("shown as a word-level diff: {reason}"));
        }
    }
    if let Some(&(_, op)) = tx
        .deletion_comments
        .iter()
        .find(|&&(id, _)| comment_holds_kept_text(&redline, id))
    {
        // Under keep the deletion is written as given, and its comment
        // wraps the words named, its reply the struck paragraph.
        let keep = match tx.syntax {
            super::PlanSyntax::Flags | super::PlanSyntax::Typed => "--existing-revisions keep",
            super::PlanSyntax::V1 | super::PlanSyntax::V2 => r#""existing_revisions": "keep""#,
        };
        let message = match plan.operations[op].kind {
            OperationKind::Delete { .. } => format!(
                "the comparer deleted the same words elsewhere instead, so the comment would sit on text that stays; pass {keep} to write the deletion as given, or delete without a comment"
            ),
            OperationKind::ReplyComment { .. } => format!(
                "the comparer deleted an identical paragraph instead, so the reply would sit on text that stays; pass {keep} to write the deletion as given"
            ),
            _ => "the comparer deleted an identical paragraph instead, so the comment would sit on text that stays; delete without a comment, or comment on a neighbouring paragraph".to_string(),
        };
        let mut error = tx.conflict(op, &message);
        error.code = "UNSUPPORTED_STRUCTURE".into();
        error.outcomes[op].code = Some(error.code.clone());
        return Err(error);
    }
    // After the reply check: a deletion with no new comment can still land
    // on a later copy of the same paragraph. Refuse that before publishing.
    if let Some((op, message)) = identical_paragraph_kept(tx, &redline)? {
        let mut error = tx.conflict(op, &message);
        error.code = "UNSUPPORTED_STRUCTURE".into();
        error.outcomes[op].code = Some(error.code.clone());
        return Err(error);
    }
    // A deleted text's comment wraps its deletion, as a strike's does.
    let lifted: Vec<u32> = tx
        .deletion_comments
        .iter()
        .filter(|&&(_, op)| matches!(plan.operations[op].kind, OperationKind::Delete { .. }))
        .map(|&(id, _)| id)
        .collect();
    redline = lift_deletion_comments(&redline, &lifted)?;
    redline = enclose_deleted_paragraph_comment_references(&redline, &plan.author)?;
    // Settings are not revisions: the redline takes them as they are.
    if let Some(request) = &tx.settings {
        redline = crate::settings::apply_settings_to_docx(&redline, request)
            .map_err(|m| err("PACKAGE_WRITE", None, m))?;
    }
    redline = super::replacement_order::typing_order(&redline)?;
    tx.check_redactions(&[&clean, &redline])?;
    let mut report = tx.report(true);
    report.paragraphs.to = crate::inspect::paragraphs(&clean)
        .map(|p| p.len())
        .unwrap_or(report.paragraphs.from);
    report.revisions = revision_counts(&redline);
    #[cfg(feature = "render")]
    {
        report.fields = fields;
    }
    report.clean_removed_comments = crate::comment_settlement::threads_the_clean_copy_drops(
        &clean,
        &redline,
        &report.removed_comments,
        &tx.deleted_paragraph_ids(),
    )?;
    if role == PrimaryRole::Redline {
        check_redline_stories(tx, &clean, &redline)?;
    }
    Ok(EditResult {
        clean,
        redline,
        report,
    })
}

/// A whole-paragraph deletion whose named paragraph still shows the same
/// non-empty text. The comparer struck another copy. An empty paragraph
/// is skipped: nothing distinguishes it from its twin.
fn identical_paragraph_kept(
    tx: &Transaction<'_>,
    redline: &[u8],
) -> Result<Option<(usize, String)>, EditError> {
    let deletions: Vec<(usize, usize)> = tx
        .resolved
        .iter()
        .filter_map(|(op, resolved)| match resolved {
            Resolved::DeleteParagraph { para } => Some((*op, *para)),
            _ => None,
        })
        .collect();
    if deletions.is_empty() {
        return Ok(None);
    }
    let opened = crate::inspect::Opened::open(redline)
        .map_err(|error| err("PACKAGE_WRITE", None, error.to_string()))?;
    let mut texts: HashMap<usize, Vec<String>> = HashMap::new();
    for (op, para) in deletions {
        let source = match tx.projections.get(para) {
            Some(projection) if !projection.text.is_empty() => projection.text.as_str(),
            _ => continue,
        };
        let Some(&(story, local)) = tx.paragraph_story.get(para) else {
            continue;
        };
        if let std::collections::hash_map::Entry::Vacant(slot) = texts.entry(story) {
            slot.insert(visible_paragraphs(&opened, tx, story)?);
        }
        let still_there = texts
            .get(&story)
            .and_then(|paragraphs| paragraphs.get(local))
            .is_some_and(|text| text == source);
        if !still_there {
            continue;
        }
        let keep = match tx.syntax {
            super::PlanSyntax::Flags | super::PlanSyntax::Typed => "--existing-revisions keep",
            super::PlanSyntax::V1 | super::PlanSyntax::V2 => r#""existing_revisions": "keep""#,
        };
        return Ok(Some((
            op,
            format!(
                "the comparer deleted an identical paragraph instead of the one named; pass {keep} to write the deletion as given"
            ),
        )));
    }
    Ok(None)
}

/// Visible text of each paragraph in `story`, in the order a plan addresses.
fn visible_paragraphs(
    opened: &crate::inspect::Opened,
    tx: &Transaction<'_>,
    story: usize,
) -> Result<Vec<String>, EditError> {
    if story == 0 {
        return Ok(body_paragraph_nodes(&opened.dom, opened.body)
            .into_iter()
            .map(|paragraph| project_paragraph(&opened.dom, paragraph).text)
            .collect());
    }
    let Some(part) = tx.stories.get(story).map(|story| story.part.clone()) else {
        return Ok(Vec::new());
    };
    if opened.pkg.part_bytes(&part).is_none() {
        return Ok(Vec::new());
    }
    let (dom, _, root) = crate::inspect::parse_part(&opened.pkg, &part)
        .map_err(|error| err("PACKAGE_WRITE", None, error.to_string()))?;
    Ok(crate::inspect::story_paragraph_nodes(&dom, root)
        .into_iter()
        .map(|paragraph| project_paragraph(&dom, paragraph).text)
        .collect())
}

/// An [`EditError`] as `super::err` builds it.
fn err(code: &str, operation: Option<&str>, message: impl Into<String>) -> EditError {
    super::err(code, operation, message)
}

/// Prepare a PlanV2 through the existing source/working-baseline adapter and
/// the same serialized candidate checks as v1. Both inline and saved v2
/// requests use this entry point; tracking never selects a different primary.
///
/// # Errors
/// Returns the adapter's source, scope, baseline and effect errors, candidate
/// validation failures, or `NO_CHANGES_REQUESTED` when required.
pub fn prepare_v2(
    source: &[u8],
    plan: &super::v2::PlanV2,
    options: &PrepareOptions,
) -> Result<PreparedEdit, EditError> {
    let known = SourceChecks::default();
    prepare_v2_once(source, plan, options, &known).map_err(|failure| {
        // A v2 plan's operations apply in order, each on what the ones
        // before it left: only prefixes are plans of their own, so only
        // the operation that brings the refusal in is named.
        let mut reruns = Reruns::new(|ops: &[usize]| {
            let part = super::v2::PlanV2 {
                operations: ops.iter().map(|&i| plan.operations[i].clone()).collect(),
                ..plan.clone()
            };
            prepare_v2_once(source, &part, options, &known)
                .err()
                .and_then(|f| f.refusal)
                .map(|(refusals, _)| refusals)
        })
        .knowing(plan.operations.len(), &failure);
        attributed(*failure, plan.operations.len(), false, &mut reruns)
    })
}

/// [`prepare_v2`] without attribution, as [`prepare_once`] is to
/// [`prepare`].
fn prepare_v2_once(
    source: &[u8],
    plan: &super::v2::PlanV2,
    options: &PrepareOptions,
    known: &SourceChecks,
) -> Result<PreparedEdit, Box<Failure>> {
    // A v2 plan's refusals quote its JSON, unless a typed command built it.
    let syntax = if options.syntax == super::PlanSyntax::Typed {
        super::PlanSyntax::Typed
    } else {
        super::PlanSyntax::V2
    };
    let applied = super::v2_apply::apply_quoting(source, plan, syntax)?;
    let result = EditResult {
        clean: applied.clean,
        redline: applied.redline,
        report: applied.report,
    };
    let primary = result.redline.clone();
    let unchanged = primary == source;
    let effect = if unchanged {
        Effect::Unchanged
    } else {
        Effect::Changed
    };
    let mut checks = screen_candidates(source, &result, known).map_err(|refusals| {
        Box::new(Failure {
            error: refusals[0].error.clone(),
            // A v2 operation is named by its pointer, `/operations/N`.
            refusal: Some((refusals, Vec::new())),
            advice: None,
        })
    })?;
    checks.operation_postconditions = true;
    if options.require_change && unchanged {
        return Err(err(
            codes::NO_CHANGES_REQUESTED,
            None,
            "the primary transaction has no document effect; --require-change was given",
        )
        .into());
    }
    Ok(PreparedEdit {
        source_sha256: source_sha256(source),
        request_sha256: source_sha256(
            serde_json::to_string(plan)
                .expect("plan serializes")
                .as_bytes(),
        ),
        primary_sha256: source_sha256(&primary),
        primary,
        primary_bytes: if unchanged {
            PrimaryBytes::Source
        } else {
            PrimaryBytes::Candidate
        },
        primary_effect: effect,
        result,
        checks,
        correspondence: Some(applied.correspondence),
        skipped: Vec::new(),
    })
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
pub(crate) mod tests {
    use super::super::{
        EditReport, EditResult, ExistingRevisions, ParagraphDelta, deeper_boundary_fixture,
    };
    use super::*;

    fn result_of(clean: Vec<u8>) -> EditResult {
        EditResult {
            clean: clean.clone(),
            redline: clean,
            report: EditReport {
                schema_version: 1,
                ok: true,
                source_sha256: String::new(),
                base_sha256: String::new(),
                guarded: false,
                author: "T".into(),
                date: "2026-10-17T12:00:00Z".into(),
                existing_revisions: ExistingRevisions::Refuse,
                resolved_revisions: Default::default(),
                removed_comments: Vec::new(),
                clean_removed_comments: Vec::new(),
                kept_comments: Vec::new(),
                paragraph_ids: Vec::new(),
                split_revisions: Vec::new(),
                paragraphs: ParagraphDelta::default(),
                operations: Vec::new(),
                comments_added: 0,
                revisions: Default::default(),
                #[cfg(feature = "render")]
                fields: Vec::new(),
                skipped: 0,
                notes: Vec::new(),
                output_sha256: None,
            },
        }
    }

    /// A source the full validator cannot read must not become an empty
    /// baseline that calls every candidate finding introduced: with no
    /// trustworthy baseline nothing is claimed introduced, while the
    /// candidate's own validation still runs (fail-safe disclosure, not
    /// a fabricated verdict).
    #[test]
    fn unvalidatable_source_never_claims_findings_introduced() {
        let orphan = deeper_boundary_fixture::docx(
            r#"<w:p><w:r><w:t>x</w:t></w:r><w:r><w:commentReference w:id="0"/></w:r></w:p>"#,
        );
        let findings = crate::validate::validate(&orphan).unwrap();
        assert!(
            findings.iter().any(|f| f.code == "COMMENT_ANCHOR_ORPHAN"),
            "{findings:?}"
        );
        let checks = check_candidates(b"not a zip at all", &result_of(orphan)).unwrap();
        assert!(checks.introduced.is_empty(), "{:?}", checks.introduced);
        assert!(checks.baseline.is_empty());
    }

    /// A paragraph of revisions: each `(kind, id)` around one word.
    fn revisions(records: &[(&str, u32)]) -> Vec<u8> {
        let body: String = records
            .iter()
            .map(|&(kind, id)| match kind {
                "customXmlInsRangeStart" => format!(
                    r#"<w:customXmlInsRangeStart w:id="{id}" w:author="A" w:date="2026-10-17T09:00:00Z"/><w:r><w:t>w</w:t></w:r><w:customXmlInsRangeEnd w:id="{id}"/>"#
                ),
                "rPrChange" => format!(
                    r#"<w:r><w:rPr><w:b/><w:rPrChange w:id="{id}" w:author="A" w:date="2026-10-17T09:00:00Z"><w:rPr/></w:rPrChange></w:rPr><w:t>w</w:t></w:r>"#
                ),
                _ => format!(
                    r#"<w:{kind} w:id="{id}" w:author="A" w:date="2026-10-17T09:00:00Z"><w:r><w:{t}>w</w:{t}></w:r></w:{kind}>"#,
                    t = if kind == "del" { "delText" } else { "t" }
                ),
            })
            .collect();
        deeper_boundary_fixture::docx(&format!("<w:p>{body}</w:p>"))
    }

    fn gate(
        source: &[(&str, u32)],
        candidate: &[(&str, u32)],
    ) -> Result<CandidateChecks, EditError> {
        check_candidates(&revisions(source), &result_of(revisions(candidate)))
    }

    /// `result` refuses `record` (`w:ins w:id 5`), repeated in `part` of
    /// the clean copy.
    fn refused_as_duplicate(result: Result<CandidateChecks, EditError>, record: &str, part: &str) {
        let error = result.unwrap_err();
        assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
        assert!(
            error
                .message
                .contains("introduces a duplicate revision id in the clean copy: ")
                && error.message.contains(&format!(": {record} occurs "))
                && error.message.contains(&format!(" times in {part} (")),
            "{error:?}"
        );
    }

    #[test]
    fn a_candidate_may_not_introduce_a_duplicate_revision_id() {
        refused_as_duplicate(
            gate(&[("ins", 5), ("ins", 6)], &[("ins", 5), ("ins", 5)]),
            "w:ins w:id 5",
            "word/document.xml",
        );
        refused_as_duplicate(
            gate(&[("rPrChange", 5)], &[("rPrChange", 5), ("rPrChange", 5)]),
            "w:rPrChange w:id 5",
            "word/document.xml",
        );
    }

    #[test]
    fn a_candidate_may_not_add_an_occurrence_to_an_inherited_duplicate() {
        refused_as_duplicate(
            gate(
                &[("ins", 5), ("ins", 5)],
                &[("ins", 5), ("ins", 5), ("ins", 5)],
            ),
            "w:ins w:id 5",
            "word/document.xml",
        );
    }

    #[test]
    fn healing_one_duplicate_revision_id_hides_no_new_one() {
        refused_as_duplicate(
            gate(
                &[("ins", 5), ("ins", 5)],
                &[("ins", 5), ("ins", 7), ("ins", 7)],
            ),
            "w:ins w:id 7",
            "word/document.xml",
        );
    }

    #[test]
    fn a_custom_xml_range_start_may_not_take_a_duplicate_id() {
        // A range start and its end share an id; two starts may not.
        assert!(
            gate(
                &[("customXmlInsRangeStart", 5)],
                &[("customXmlInsRangeStart", 5)]
            )
            .is_ok()
        );
        // Their ends repeat too, and come first by name (#1666).
        refused_as_duplicate(
            gate(
                &[("customXmlInsRangeStart", 5), ("customXmlInsRangeStart", 6)],
                &[("customXmlInsRangeStart", 5), ("customXmlInsRangeStart", 5)],
            ),
            "w:customXmlInsRangeEnd w:id 5",
            "word/document.xml",
        );
    }

    /// `bytes` with its main part's WordprocessingML namespace made Strict's.
    fn strict(bytes: &[u8]) -> Vec<u8> {
        let mut pkg = crate::opc::PartFs::open(bytes).unwrap();
        let xml = pkg.part_string("word/document.xml").unwrap().replace(
            "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
            "http://purl.oclc.org/ooxml/wordprocessingml/main",
        );
        pkg.set_part("word/document.xml", xml.into_bytes());
        pkg.to_zip().unwrap()
    }

    #[test]
    fn a_strict_sources_duplicates_are_inherited() {
        // The edit writes a Strict source's revisions in Transitional.
        let source = strict(&revisions(&[("ins", 5), ("ins", 5)]));
        let candidate = revisions(&[("ins", 5), ("ins", 5)]);
        let result = check_candidates(&source, &result_of(candidate));
        assert!(result.is_ok(), "{result:?}");
    }

    /// `bytes` with its main part at `name`, wherever its relationship
    /// and content type point.
    fn main_named(bytes: &[u8], name: &str) -> Vec<u8> {
        const OFFICE_DOCUMENT: &str =
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
        let mut pkg = crate::opc::PartFs::open(bytes).unwrap();
        let xml = pkg.part_bytes("word/document.xml").unwrap().to_vec();
        pkg.remove_part("word/document.xml");
        pkg.set_part(name, xml);
        pkg.add_content_type_override(
            &format!("/{name}"),
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
        );
        pkg.remove_package_relationships_by_type(OFFICE_DOCUMENT);
        pkg.add_package_relationship(OFFICE_DOCUMENT, name);
        pkg.to_zip().unwrap()
    }

    #[test]
    fn a_main_part_without_an_xml_extension_is_gated_too() {
        let source = main_named(&revisions(&[("ins", 5), ("ins", 6)]), "word/main.dat");
        let candidate = main_named(&revisions(&[("ins", 5), ("ins", 5)]), "word/main.dat");
        refused_as_duplicate(
            check_candidates(&source, &result_of(candidate)),
            "w:ins w:id 5",
            "word/main.dat",
        );
    }

    #[test]
    fn a_strict_main_part_without_an_xml_extension_is_read_as_transitional() {
        // #1666: its inherited duplicates stay inherited.
        let source = main_named(
            &strict(&revisions(&[("ins", 5), ("ins", 5)])),
            "word/main.dat",
        );
        let candidate = main_named(&revisions(&[("ins", 5), ("ins", 5)]), "word/main.dat");
        let result = check_candidates(&source, &result_of(candidate));
        assert!(result.is_ok(), "{result:?}");
    }

    fn prepared(source: &[u8], operation: &str) -> Result<PreparedEdit, EditError> {
        let json = format!(
            r#"{{"schema_version":1,"author":"Bob","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{operation}]}}"#
        );
        let plan = EditPlan::from_json(&json).unwrap();
        prepare(source, &plan, &PrepareOptions::default())
    }

    #[test]
    fn a_paragraph_marks_revision_id_survives_paragraph_edits() {
        // `w:pPr/w:rPr/w:ins`: a new paragraph made like it, one split off
        // it, and a replacement inside it publish.
        let source = deeper_boundary_fixture::docx(
            r#"<w:p><w:pPr><w:rPr><w:ins w:id="7" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr></w:pPr><w:r><w:t xml:space="preserve">abcd efgh</w:t></w:r></w:p><w:p><w:r><w:t>next</w:t></w:r></w:p>"#,
        );
        for operation in [
            r#"{"kind":"insert_paragraph","paragraph":{"index":1},"runs":[{"text":"b"}],"like":{"index":0}}"#,
            r#"{"kind":"insert","paragraph":"p0","after":"abcd","text":"\nnew para"}"#,
            r#"{"kind":"replace","paragraph":"p0","find":"cd","replacement":"X"}"#,
        ] {
            let result = prepared(&source, operation);
            assert!(result.is_ok(), "{operation}: {:?}", result.err());
        }
    }

    /// `culprits` against a refusal that comes back whenever the
    /// operations it is given include every set of one of `causes`,
    /// counting the reruns.
    fn culprits_of(
        count: usize,
        together: bool,
        causes: &[&[usize]],
    ) -> (Option<Vec<usize>>, usize) {
        let mut reruns = 0;
        let found = culprits(count, together, |ops| {
            reruns += 1;
            causes
                .iter()
                .any(|cause| cause.iter().all(|i| ops.contains(i)))
        });
        (found, reruns)
    }

    #[test]
    fn culprits_finds_one_operation_in_logarithmic_reruns() {
        for at in 0..64 {
            let (found, reruns) = culprits_of(64, true, &[&[at]]);
            assert_eq!(found, Some(vec![at]));
            // The empty plan, six halvings, and the operation alone.
            assert!(reruns <= 8, "{at}: {reruns}");
        }
        assert_eq!(culprits_of(1, true, &[&[0]]).0, Some(vec![0]));
    }

    #[test]
    fn culprits_names_the_operations_a_refusal_needs() {
        // The later one brings it in; the earlier ones it needs follow in
        // plan order.
        assert_eq!(culprits_of(10, true, &[&[2, 7]]).0, Some(vec![7, 2]));
        assert_eq!(culprits_of(10, true, &[&[5, 1, 8]]).0, Some(vec![8, 1, 5]));
        assert_eq!(culprits_of(2, true, &[&[0, 1]]).0, Some(vec![1, 0]));
        // Independent causes: the first one complete in plan order is
        // named, alone.
        assert_eq!(culprits_of(10, true, &[&[6], &[3]]).0, Some(vec![3]));
        // Without `together` (v2), only the operation that brings it in.
        assert_eq!(culprits_of(10, false, &[&[2, 7]]).0, Some(vec![7]));
    }

    #[test]
    fn culprits_seeks_each_further_operation_before_the_last_found() {
        // The operations before the last one found, with those found,
        // reproduce the refusal: the next search covers only them.
        let (found, reruns) = culprits_of(64, true, &[&[0, 1, 2, 3, 63]]);
        assert_eq!(found, Some(vec![63, 0, 1, 2, 3]));
        assert!(reruns <= 21, "{reruns}");
        let (found, reruns) = culprits_of(64, true, &[&[1, 3, 63]]);
        assert_eq!(found, Some(vec![63, 1, 3]));
        assert!(reruns <= 18, "{reruns}");
    }

    #[test]
    fn a_refusal_without_operations_names_none() {
        // The plan without operations meets it: settling, not an
        // operation, causes it.
        assert_eq!(culprits_of(5, true, &[&[]]).0, None);
        assert_eq!(culprits_of(0, true, &[]).0, None);
    }

    /// A failure the candidate checks did not raise keeps its error as it
    /// is, without a rerun; a refusal no operation causes stays the plan's.
    #[test]
    fn attribution_leaves_other_failures_alone() {
        let error = err("OVERLAPPING_EDITS", Some("op-2"), "two edits");
        let mut never = Reruns::new(|_: &[usize]| -> Option<Refused> {
            panic!("no rerun for a failure the candidate checks did not raise")
        });
        let kept = attributed(*Box::<Failure>::from(error.clone()), 3, true, &mut never);
        assert_eq!(kept, error);
        let refusal = refused(
            "redline",
            "BOOKMARK_CROSSES_REVISION",
            "word/document.xml",
            "Clause".into(),
            "the edit introduces it".into(),
        );
        let failure = Box::new(Failure {
            error: refusal.error.clone(),
            refusal: Some((vec![refusal.clone()], Vec::new())),
            advice: Some("drop update_fields"),
        });
        let mut always = Reruns::new(|_: &[usize]| Some(vec![refusal.clone()]));
        let kept = attributed(*failure, 3, true, &mut always);
        assert_eq!(kept.operation, None);
        assert_eq!(kept.message, "the edit introduces it; drop update_fields");
    }

    /// The operations are named before the advice that closes the message.
    #[test]
    fn named_operations_come_before_the_advice() {
        let refusal = refused(
            "redline",
            "RANGE_LOST",
            "",
            "bookmark B".into(),
            "Reject All drops bookmark B".into(),
        );
        let outcome = |id: &str, kind: &str, paragraph: Option<&str>| EditOutcome {
            id: id.into(),
            kind: kind.into(),
            status: String::new(),
            paragraph: paragraph.map(str::to_string),
            matches: 0,
            context: None,
            comment_id: None,
            anchor_given: None,
            anchor_read_as: None,
            near: None,
            candidates: Vec::new(),
            code: None,
            message: None,
        };
        let failure = Box::new(Failure {
            error: refusal.error.clone(),
            refusal: Some((
                vec![refusal.clone()],
                vec![
                    outcome("op-1", "replace", Some("p0")),
                    outcome("op-2", "insert", Some("p4")),
                    outcome("op-3", "page_setup", None),
                ],
            )),
            advice: Some("drop update_fields"),
        });
        let mut reruns = Reruns::new(|ops: &[usize]| {
            (ops.contains(&0) && ops.contains(&2)).then(|| vec![refusal.clone()])
        });
        let named = attributed(*failure, 3, true, &mut reruns);
        assert_eq!(named.operation.as_deref(), Some("op-3"));
        assert_eq!(
            named.message,
            "Reject All drops bookmark B; op-3 (page_setup) introduces it, together with op-1 (replace on p0); drop update_fields"
        );
    }

    /// A rerun reproduces a defect when it meets that one among others,
    /// never another defect of its code and part; each set of operations
    /// is prepared once.
    #[test]
    fn a_rerun_reproduces_the_defect_not_its_code() {
        let defect = |subject: &str| {
            refused(
                "redline",
                "DUPLICATE_REVISION_ID",
                "word/document.xml",
                subject.into(),
                format!("{subject} repeats"),
            )
        };
        let mut runs = 0;
        let mut reruns = Reruns::new(|ops: &[usize]| {
            runs += 1;
            let mut met = Vec::new();
            if ops.contains(&0) {
                met.push(defect("w:ins w:id 9"));
            }
            if ops.contains(&1) {
                met.push(defect("w:ins w:id 7"));
            }
            (!met.is_empty()).then_some(met)
        });
        let seven = defect("w:ins w:id 7");
        assert!(!reruns.reproduces(&[0], &seven));
        assert!(reruns.reproduces(&[1], &seven));
        assert!(reruns.reproduces(&[0, 1], &seven));
        assert!(!reruns.reproduces(&[0], &seven));
        drop(reruns);
        assert_eq!(runs, 3);
    }

    #[test]
    fn a_run_marks_revision_id_split_in_two_is_refused() {
        // A run's `w:rPr` holds no `w:ins` (OpenXmlValidator:
        // `Sch_InvalidElementContentExpectingComplex`). Splitting such a run
        // copies the mark's id: the candidate is refused, not published.
        let source = deeper_boundary_fixture::docx(
            r#"<w:p><w:r><w:rPr><w:ins w:id="7" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr><w:t>abcd</w:t></w:r></w:p>"#,
        );
        let error = prepared(
            &source,
            r#"{"kind":"replace","paragraph":"p0","find":"bc","replacement":"X"}"#,
        )
        .err()
        .unwrap();
        assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
        assert!(
            error.message.contains("w:ins on a mark w:id 7"),
            "{error:?}"
        );
    }

    /// The KI-8 shape: the clean copy resolves the edit's insertion away
    /// and only the redline repeats an id. The refusal names the redline.
    #[test]
    fn a_duplicate_only_the_redline_carries_is_refused_by_its_artifact() {
        let source = revisions(&[("ins", 5), ("ins", 6)]);
        let mut result = result_of(revisions(&[("ins", 5), ("ins", 6)]));
        result.redline = revisions(&[("ins", 5), ("ins", 5)]);
        let error = check_candidates(&source, &result).unwrap_err();
        assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
        assert_eq!(
            error.message,
            "the edit introduces a duplicate revision id in the redline: w:ins w:id 5 occurs 2 times in word/document.xml (the source has 1)"
        );
    }

    #[test]
    fn an_unreadable_source_skips_the_duplicate_gate() {
        let candidate = revisions(&[("ins", 5), ("ins", 5)]);
        let result = check_candidates(b"not a zip at all", &result_of(candidate));
        assert!(result.is_ok(), "{result:?}");
    }

    #[test]
    fn inherited_duplicates_and_ids_shared_across_kinds_publish() {
        let inherited = [("ins", 5), ("ins", 5)];
        assert!(gate(&inherited, &inherited).is_ok());
        assert!(gate(&inherited, &[("ins", 5)]).is_ok());
        let shared = [("ins", 5), ("del", 5), ("rPrChange", 5)];
        assert!(gate(&[("ins", 5)], &shared).is_ok());
    }

    #[test]
    fn verification_reports_only_completed_candidate_checks() {
        let skipped = CandidateChecks::default().verification();
        assert_eq!(skipped.integrity, "skipped");
        assert_eq!(skipped.operation_postconditions, "skipped");
        let source = deeper_boundary_fixture::docx("<w:p><w:r><w:t>old</w:t></w:r></w:p>");
        let plan = EditPlan::from_json(
            r#"{"schema_version":1,"author":"T","date":"2026-10-17T12:00:00Z","operations":[]}"#,
        )
        .unwrap();
        let checked = prepare(&source, &plan, &PrepareOptions::default())
            .unwrap()
            .checks
            .verification();
        assert_eq!(checked.integrity, "passed");
        assert_eq!(checked.operation_postconditions, "passed");
    }

    #[test]
    fn redline_only_defects_name_the_story_and_paragraph() {
        use deeper_boundary_fixture::{Part, R_NS, W_NS, docx_with_sect, para};

        let kinds = ["header", "footer", "footnotes", "endnotes"];
        let names = [
            "word/header1.xml",
            "word/footer1.xml",
            "word/footnotes.xml",
            "word/endnotes.xml",
        ];
        let roots = ["hdr", "ftr", "footnotes", "endnotes"];
        let xmls: Vec<_> = roots
            .iter()
            .map(|root| {
                let text = para("old");
                let content = match *root {
                    "footnotes" => format!(r#"<w:footnote w:id="1">{text}</w:footnote>"#),
                    "endnotes" => format!(r#"<w:endnote w:id="1">{text}</w:endnote>"#),
                    _ => text,
                };
                format!(r#"<w:{root} xmlns:w="{W_NS}">{content}</w:{root}>"#)
            })
            .collect();
        let rels: Vec<_> = kinds.iter().map(|k| format!("{R_NS}/{k}")).collect();
        let types: Vec<_> = kinds
            .iter()
            .map(|k| {
                format!("application/vnd.openxmlformats-officedocument.wordprocessingml.{k}+xml")
            })
            .collect();
        let parts: Vec<_> = (0..kinds.len())
            .map(|i| Part {
                name: names[i],
                content_type: &types[i],
                rel_type: &rels[i],
                xml: &xmls[i],
            })
            .collect();
        let source = docx_with_sect(
            &(para("old")
                + r#"<w:p><w:r><w:footnoteReference w:id="1"/><w:endnoteReference w:id="1"/></w:r></w:p>"#),
            &parts,
            r#"<w:headerReference w:type="default" r:id="rIdX0"/><w:footerReference w:type="default" r:id="rIdX1"/>"#,
        );
        let mut operations = vec![
            serde_json::json!({"kind":"replace", "paragraph":"p0", "find":"old", "replacement":"new"}),
        ];
        for story in crate::inspect::stories(&source).unwrap() {
            operations.push(serde_json::json!({"kind":"replace", "paragraph":story.paragraphs[0].id, "find":"old", "replacement":"new"}));
        }
        let plan = EditPlan::from_json(&serde_json::json!({
            "schema_version":1, "author":"T", "date":"2026-10-17T12:00:00Z", "operations":operations,
        }).to_string()).unwrap();
        let mut tx = Transaction::start(&source, &plan).unwrap();
        tx.resolve().unwrap();
        tx.apply().unwrap();
        let (clean, _) = tx.finish().unwrap();
        assert!(check_redline_stories(&tx, &clean, &clean).is_ok());
        let mut failures = Vec::new();
        for part in std::iter::once("word/document.xml").chain(names) {
            for defect in ["text", "count", "missing"] {
                if part == "word/document.xml" && defect == "missing" {
                    continue;
                }
                let mut pkg = crate::opc::PartFs::open(&clean).unwrap();
                let xml = pkg.part_string(part).unwrap();
                if defect == "missing" {
                    pkg.remove_related_part(
                        "word/document.xml",
                        &format!("rIdX{}", names.iter().position(|p| *p == part).unwrap()),
                    );
                } else {
                    let broken = if defect == "text" {
                        xml.replace("new", "WRONG")
                    } else {
                        xml.replace("</w:p>", "</w:p><w:p/>")
                    };
                    pkg.set_part(part, broken.into_bytes());
                }
                let redline = pkg.to_zip().unwrap();
                match check_redline_stories(&tx, &clean, &redline) {
                    Err(e)
                        if e.code == codes::POSTCONDITION_FAILED
                            && e.message.contains(part)
                            && e.message.contains("paragraph") => {}
                    outcome => failures.push(format!("{part} {defect}: {outcome:?}")),
                }
            }
        }
        assert!(failures.is_empty(), "{}", failures.join("\n"));
    }

    thread_local! {
        /// How many times [`prepare_once`] ran on this test thread.
        pub(crate) static PREPARATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
    }

    /// `--skip-failed` attributes every defect of a refusal in one pass,
    /// each rerun once: a plan whose operations are each refused alone
    /// takes at most `2n + 1` preparations (the plan, its prefixes, each
    /// operation alone), not a bisection per retry (#1689 review).
    #[test]
    fn skip_failed_attributes_every_defect_of_a_refusal_at_once() {
        let n = 16;
        let body: String = (0..n)
            .map(|i| {
                format!(
                    r#"<w:p><w:r><w:rPr><w:ins w:id="{}" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr><w:t>abcd</w:t></w:r></w:p>"#,
                    100 + i
                )
            })
            .collect();
        let source = deeper_boundary_fixture::docx(&body);
        let operations: Vec<String> = (0..n)
            .map(|i| {
                format!(r#"{{"kind":"replace","paragraph":"p{i}","find":"bc","replacement":"X"}}"#)
            })
            .collect();
        let plan = EditPlan::from_json(&format!(
            r#"{{"schema_version":1,"author":"Bob","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{}]}}"#,
            operations.join(",")
        ))
        .unwrap();
        PREPARATIONS.with(|count| count.set(0));
        let options = PrepareOptions {
            skip_failed: true,
            ..PrepareOptions::default()
        };
        let error = prepare(&source, &plan, &options).unwrap_err();
        assert_eq!(error.code, "CANDIDATE_INVALID", "{error:?}");
        let failed = error
            .outcomes
            .iter()
            .filter(|o| o.status == "failed")
            .count();
        assert_eq!(failed, n, "{error:?}");
        // The plan, then each operation alone.
        let preparations = PREPARATIONS.with(std::cell::Cell::get);
        assert!(
            preparations <= n + 1,
            "{preparations} preparations for {n} operations"
        );
    }

    /// One refused operation among many is still found by bisection, in
    /// logarithmic reruns, and `--skip-failed` publishes the others.
    #[test]
    fn skip_failed_bisects_a_lone_defect() {
        let n = 16;
        let culprit = 11;
        let body: String = (0..n)
            .map(|i| {
                let mark = if i == culprit {
                    r#"<w:rPr><w:ins w:id="100" w:author="Ann" w:date="2026-09-01T00:00:00Z"/></w:rPr>"#
                } else {
                    ""
                };
                format!(r#"<w:p><w:r>{mark}<w:t>abcd</w:t></w:r></w:p>"#)
            })
            .collect();
        let source = deeper_boundary_fixture::docx(&body);
        let operations: Vec<String> = (0..n)
            .map(|i| {
                format!(r#"{{"kind":"replace","paragraph":"p{i}","find":"bc","replacement":"X"}}"#)
            })
            .collect();
        let plan = EditPlan::from_json(&format!(
            r#"{{"schema_version":1,"author":"Bob","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{}]}}"#,
            operations.join(",")
        ))
        .unwrap();
        PREPARATIONS.with(|count| count.set(0));
        let options = PrepareOptions {
            skip_failed: true,
            ..PrepareOptions::default()
        };
        let prepared = prepare(&source, &plan, &options).unwrap();
        let skipped: Vec<usize> = prepared.skipped.iter().map(|s| s.index).collect();
        assert_eq!(skipped, [culprit]);
        assert!(
            prepared.skipped[0]
                .message
                .contains("op-12 (replace on p11) introduces it"),
            "{:?}",
            prepared.skipped
        );
        // The plan, the plan without operations, four prefixes, the
        // culprit alone and the survivors.
        let preparations = PREPARATIONS.with(std::cell::Cell::get);
        assert!(preparations <= 8, "{preparations} preparations");
    }

    thread_local! {
        static SERIALIZED_CANDIDATE: std::cell::RefCell<Option<Vec<u8>>> = const { std::cell::RefCell::new(None) };
    }

    // A deterministic serializer fake: only the current test thread sees it.
    pub(crate) fn serialized_candidate(bytes: Vec<u8>) -> Vec<u8> {
        SERIALIZED_CANDIDATE.with(|candidate| candidate.borrow().clone().unwrap_or(bytes))
    }

    #[test]
    fn review_11_prepare_v2_checks_serialized_text_like_v1() {
        struct Reset;
        impl Drop for Reset {
            fn drop(&mut self) {
                SERIALIZED_CANDIDATE.with(|c| *c.borrow_mut() = None);
            }
        }
        let source = deeper_boundary_fixture::docx("<w:p><w:r><w:t>old</w:t></w:r></w:p>");
        let legacy = EditPlan::from_json(r#"{"schema_version":1,"author":"T","date":"2026-10-17T12:00:00Z","existing_revisions":"keep","operations":[{"kind":"replace","paragraph":"p0","find":"old","replacement":"new"}]}"#).unwrap();
        let view = super::super::for_edit::for_edit(
            &source,
            &super::super::for_edit::ForEditRequest {
                all: true,
                ..Default::default()
            },
        )
        .unwrap();
        let target = &view.paragraphs[0].target;
        let value = serde_json::json!({
            "schema_version": 2, "source": {"sha256": source_sha256(&source)},
            "author": "T", "date": "2026-10-17T12:00:00Z",
            "policy": {"track": "off", "existing_revisions": "keep", "text_view":"final"},
            "resolve_revisions":{"accept":[],"reject":[]}, "scope": {"kind":"all"},
            "operations": [{"kind":"replace", "target": {"short_id":target.short_id,"long_id":target.long_id}, "find":"old", "text":"new"}]
        });
        let plan = super::super::v2::PlanV2::from_json(&value.to_string()).unwrap();
        // The requested DOM reads "new", while the fake serializer emits "wrong".
        SERIALIZED_CANDIDATE.with(|c| {
            *c.borrow_mut() = Some(deeper_boundary_fixture::docx(
                "<w:p><w:r><w:t>wrong</w:t></w:r></w:p>",
            ));
        });
        let _reset = Reset;
        let v1 = prepare(&source, &legacy, &PrepareOptions::default());
        assert!(v1.is_err(), "v1 must detect the serializer disagreement");
        let expected = v1.unwrap_err();
        assert_eq!(expected.code, "POSTCONDITION_FAILED");
        for track in [super::super::v2::Track::Off, super::super::v2::Track::On] {
            let mut plan = plan.clone();
            plan.policy.track = track;
            let actual = prepare_v2(&source, &plan, &PrepareOptions::default());
            assert!(
                actual.is_err(),
                "prepare_v2 must refuse the same serialized-text disagreement as prepare"
            );
            assert_eq!(actual.unwrap_err().code, expected.code);
        }
    }
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod blame_branch_tests {
    use super::*;

    fn refusal(artifact: &'static str, code: &str, part: &str, subject: &str) -> Refusal {
        Refusal {
            artifact,
            code: code.into(),
            part: part.into(),
            subject: subject.into(),
            error: err("CANDIDATE_INVALID", None, "bad"),
        }
    }

    #[test]
    fn the_same_defect_matches_on_every_field_but_the_error() {
        let base = refusal("redline", "X", "word/document.xml", "r1");
        assert!(base.same(&refusal("redline", "X", "word/document.xml", "r1")));
        for other in [
            refusal("clean", "X", "word/document.xml", "r1"),
            refusal("redline", "Y", "word/document.xml", "r1"),
            refusal("redline", "X", "word/header1.xml", "r1"),
            refusal("redline", "X", "word/document.xml", "r2"),
        ] {
            assert!(!base.same(&other), "{other:?}");
        }
    }

    fn outcome(id: &str, paragraph: Option<&str>) -> EditOutcome {
        EditOutcome {
            id: id.into(),
            kind: "replace".into(),
            status: "ok".into(),
            paragraph: paragraph.map(Into::into),
            matches: 1,
            context: None,
            comment_id: None,
            anchor_given: None,
            anchor_read_as: None,
            near: None,
            candidates: Vec::new(),
            code: None,
            message: None,
        }
    }

    #[test]
    fn naming_lists_every_operation_that_brings_the_defect() {
        let outcomes = [
            outcome("e1", Some("p0")),
            outcome("e2", None),
            outcome("e3", None),
        ];
        let error = || err("CANDIDATE_INVALID", None, "broken");
        assert_eq!(naming(error(), &[], &outcomes).message, "broken");
        let one = naming(error(), &[0], &outcomes);
        assert_eq!(one.operation.as_deref(), Some("e1"));
        assert_eq!(one.message, "broken; e1 (replace on p0) introduces it");
        let two = naming(error(), &[1, 0], &outcomes);
        assert_eq!(
            two.message,
            "broken; e2 (replace) introduces it, together with e1 (replace on p0)"
        );
        let many = naming(error(), &[0, 1, 2, 7], &outcomes);
        assert_eq!(
            many.message,
            "broken; e1 (replace on p0) introduces it, together with e2 (replace), e3 (replace) and /operations/7"
        );
        let unknown = naming(error(), &[9], &outcomes);
        assert_eq!(unknown.operation.as_deref(), Some("/operations/9"));
    }
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod marker_branch_tests {
    use super::super::deeper_boundary_fixture::docx;
    use super::*;

    #[test]
    fn range_markers_skip_go_back_and_nameless_bookmarks() {
        let source = docx(
            r#"<w:p><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:bookmarkStart w:id="1"/><w:bookmarkEnd w:id="1"/><w:bookmarkStart w:id="2" w:name="Kept"/><w:bookmarkEnd w:id="2"/><w:permStart w:id="3" w:edGrp="everyone"/><w:r><w:t>Text</w:t></w:r><w:permEnd w:id="3"/></w:p>"#,
        );
        let markers = range_markers(&source);
        assert_eq!(
            markers.bookmarks.into_iter().collect::<Vec<_>>(),
            vec!["Kept"]
        );
        assert_eq!(markers.permissions, 1);
        assert_eq!(markers.comments, 0);
        let none = range_markers(b"not a package");
        assert!(none.bookmarks.is_empty() && none.permissions == 0);
    }

    #[test]
    fn a_location_without_a_step_moves_nowhere() {
        let source = docx("<w:p><w:r><w:t>Text</w:t></w:r></w:p>");
        let pkg = crate::opc::PartFs::open(&source).unwrap();
        let (mut a, mut b) = (HashMap::new(), HashMap::new());
        assert_eq!(
            moved_location(
                &pkg,
                &pkg,
                &mut a,
                &mut b,
                "word/document.xml",
                "/w:document"
            ),
            None
        );
        assert!(a.is_empty() && b.is_empty());
    }
}