onevcs 0.48.0

Version control and remote-host abstraction for agent workflows: host-neutral change requests, sessions, and a rules system.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
//! Publication: verifying a branch and landing it under its repository's policy.
//!
//! One path, two endings. `local-direct` builds the branch-to-base squash in a
//! **detached scratch worktree** and pushes that exact tree, so the registered
//! publication checkout is never worked in and only ever fast-forwarded. Every
//! `change-*` policy pushes the branch, opens (or adopts) a change request, and
//! then differs only in what it asks the host to do with it.
//!
//! Everything automated goes through the FIFO merge queue keyed by the publication
//! checkout's git common directory, which is the one thing every worktree and alias
//! of an identity shares.

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::error::{Error, Result};
use crate::event::{EventKind, Phase};
use crate::gate_run::{self, Gate, Moment};
use url::Url;

use std::collections::BTreeSet;

use crate::boundary::TermScope;
use crate::host::{
    ChangeRequest, ChangeSpec, Check, CheckState, Hosting, MergeOutcome, Mergeability, RemoteHost,
    Sha,
};
use crate::releases::TargetName;
use crate::rules::{MergePolicy, Policy};
use crate::session::{Lifecycle, Provenance, SessionToken};
use crate::standings::ChecksVerdict;
use crate::store::Resolution;
use crate::stream::{self, Stream};
use crate::workspace::{object, Ref};
use crate::{
    gh, git, guidance, home, ids, lock, merge_path, policy, provenance, queue, store, vcs,
    workspace,
};

/// How many times a base that moved under a publication is re-merged before the
/// attempt is abandoned. Bounded, because a base advancing on every retry is a
/// conflict that resolving again will not settle.
pub const SYNC_ATTEMPTS: usize = 3;

/// What one publication was asked for beyond the session it publishes.
///
/// The options `onevcs publish` takes, and nothing else: everything else about a
/// publication comes from the session and from the repository's rules.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PublishRequest {
    /// A per-run policy. It may narrow the repository's but never widen it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub policy: Option<MergePolicy>,
    /// An explicit title, which replaces the subject synthesized from the branch.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<Subject>,
    /// The body the change request is opened with, verbatim.
    ///
    /// Absent opens it with no body at all. Nothing here composes one: every
    /// change request this crate opened used to carry the same three lines — the
    /// branch's own subject echoed back under `## What`, and `Published by
    /// onevcs.` under `## Why` — which told a reviewer nothing the title had not.
    /// Unvalidated, because a body is prose and a host places no shape on it; a
    /// title is a [`Subject`] for the opposite reason.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    /// Open the change request as a **draft**, and why it is not ready.
    ///
    /// Absent is a publication giving no reason, which takes the draft lifecycle: its
    /// change request opens as a draft while the required checks run and is lifted or
    /// kept for review on their verdict — or opens ready, where the rules file says
    /// `drafts: {disabled: true}`. Present is one of two things, and the reason
    /// says which: a change whose work is as far along as it can go while something
    /// outside this repository has not happened yet, or a change the session that
    /// opened it is still making. The reason travels with it, because a draft
    /// nobody can read the reason for is a change request nobody knows how to
    /// finish.
    ///
    /// A draft is unmergeable in that state, and this crate keeps it so: nothing
    /// merges it, arms the host's own merge on it, or advances a base from it while
    /// the draft stands, under any `drafts:` setting. A later publication of the same
    /// branch carrying **no** `DraftReason` adopts it as awaiting its checks and lifts
    /// it only on their verdict, or keeps it for review; `onevcs change ready` lifts it
    /// at once, for a session that wants its draft lifted without landing anything.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub draft: Option<DraftReason>,
    /// Which private repositories the public boundary check derives its terms from,
    /// should the destination be public. Unset is every registered private one.
    #[serde(default, skip_serializing_if = "TermScope::is_registry")]
    pub term_scope: TermScope,
    /// Push into the draft [`adopt`](PublishRequest::adopt) names and stop there:
    /// no host call at all — no lookup, no description write, no check watch, no
    /// lift — ending [`PublishOutcome::ChangeReviewDraftUnchecked`]. Its checks' verdict
    /// is read later, through [`change_standings`](crate::change_standings) or
    /// [`review_standings`](crate::review_standings).
    ///
    /// Only for a change request held for review — a `change-open` identity whose
    /// approvals are required — and only with `adopt`; refused otherwise, with nothing
    /// pushed. Defaulted and omitted when false, so a request written before it reads
    /// and writes as it did.
    // llmlint: ignore[invalid_states_unrepresentable] the landings amendment in
    // `docs/contract.md` fixes this field and `adopt` verbatim as `bool` and
    // `Option<Url>` on a struct consumers build with literals — a sum type would change
    // the agreed surface. Every combination they can spell that is not a deferred
    // publication is refused by name in `deferred`, before anything is pushed.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub defer_checks: bool,
    /// The change request a [`defer_checks`](PublishRequest::defer_checks) publication
    /// pushes into, which is never looked up on the host: its head branch is read from
    /// this host's own newest record of it — a standings read that saw its head, or the
    /// `change-opened` a publication wrote — and must be the session's branch.
    // llmlint: ignore[invalid_states_unrepresentable] fixed verbatim by the landings
    // amendment beside `defer_checks`; `deferred` refuses `adopt` without it by name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub adopt: Option<Url>,
}

/// Why a change request is a draft, in the shape a machine reads it.
///
/// A draft is a change request that must not land yet, and there are two reasons one
/// is held. A change that has gone as far as it can and stopped short of the one step
/// that would make something temporary permanent — a dependency pinned to a branch
/// rather than to a release — is [`AwaitingRelease`](DraftReason::AwaitingRelease),
/// and what it is waiting for is the whole of the reason: which repository, which of
/// that repository's release targets, and the reference the change is pinned to in
/// the meantime. A change whose work is *still being made* — evidence gathered, a
/// description started — is [`Held`](DraftReason::Held) by the session that opened
/// it, and the one line a person reads is the whole of that reason. The `kind` tag
/// is what a consumer routes on; `because` is the same fact as a sentence, for
/// whoever reads it rather than routes on it.
///
/// It is recorded on the session's own event stream — the publication record — and
/// nowhere else. **Nothing is written into the change request's body**, under a
/// marker heading or anywhere: a body is prose a reviewer reads and a drafting
/// caller may rewrite, and deciding a control action from it would turn an editorial
/// act into one. The cost of that is carried openly: somebody looking at the draft
/// change request without access to this host sees that it **is** a draft and not
/// why.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum DraftReason {
    /// A change pinned to a dependency's branch until that dependency releases —
    /// what a fast-adoption publication carries.
    AwaitingRelease {
        /// The repository identity whose release is awaited, as the registry keys one.
        ///
        /// A `String` rather than a newtype because an identity key is one everywhere
        /// this crate spells one — `Identity::origin`, `Recoverable::identity`, the
        /// registry document's own map key — so a narrower type here would disagree
        /// with every type it is compared against. What must not be representable is
        /// a value that renders as something *other* than itself in a refusal or an
        /// event payload, and `checked` refuses exactly that at every boundary one
        /// arrives at.
        // llmlint: ignore[invalid_states_unrepresentable] the doc above: the key's own type, checked.
        awaiting: String,
        /// Which release target of it is awaited.
        target: TargetName,
        /// The reference this change is pinned to until that release arrives — the
        /// branch of the awaited repository the pin names.
        ///
        /// A `String` for the reason `awaiting` above is, and one more of its own:
        /// this is the *awaited* repository's branch name rather than one this
        /// repository's git could be asked about, so the parser that would decide it
        /// is not this host's. `checked` refuses the values that would render as
        /// something else.
        // llmlint: ignore[invalid_states_unrepresentable] the doc above: another host's name, checked.
        reference: String,
        /// One line a person reads, saying why the change is not ready.
        ///
        /// Prose, so there is nothing for a type to narrow. The one shape that matters
        /// is where it lands — a single rendered line — and `checked` is what holds it
        /// to that.
        // llmlint: ignore[invalid_states_unrepresentable] the doc above: prose, checked where it lands.
        because: String,
    },
    /// A change request the session that opened it is holding open as a draft while
    /// its work is still being made: evidence gathered, description started. Lifted
    /// by `onevcs change ready`, or taken by a later publication of the same session
    /// carrying no reason into the draft lifecycle — which keeps it a draft when green
    /// on a `change-open` identity whose approvals are required.
    Held {
        /// One line a person reads, saying why the session is holding it. Prose,
        /// held to the one line it is printed on by `checked`, as the line above is.
        // llmlint: ignore[invalid_states_unrepresentable] the doc above: prose, checked where it lands.
        because: String,
    },
}

impl DraftReason {
    /// The rule a publication holds a reason to, or the refusal that this is not one.
    ///
    /// Every field here is printed: into a refusal, into an event payload a consumer
    /// reads back, and — through `--draft` — at a host. So the check is the one that
    /// decides whether a value renders as itself: nothing empty, and nothing carrying
    /// a control character, which is what turns one line of a record into two.
    ///
    /// Public for the reason [`MergePolicy::narrow`] and [`FailureKind::of`] are: it
    /// belongs to publication rather than to any one implementation of it, so a
    /// supplied [`Vcs`](crate::Vcs) applies *this* rule at its own boundary rather
    /// than a restatement of it that could accept what the real one refuses. It is a
    /// method rather than a conversion because the contract fixes the fields as
    /// public and settable, so there is no constructor for a check to live in.
    ///
    /// # Errors
    ///
    /// [`Error::Invalid`] naming the field that would not render as itself.
    pub fn checked(&self) -> Result<()> {
        let printed: Vec<(&str, &String)> = match self {
            DraftReason::AwaitingRelease {
                awaiting,
                reference,
                because,
                ..
            } => vec![
                ("the repository whose release is awaited", awaiting),
                ("the reference the change is pinned to", reference),
                ("the reason the change is not ready", because),
            ],
            DraftReason::Held { because } => {
                vec![("the reason the session is holding the change", because)]
            }
        };
        for (what, value) in printed {
            if value.is_empty() {
                return Err(crate::error::invalid(format!(
                    "a draft change request must say {what}, and this one names none: a draft \
                     whose reason cannot be read is a change request nobody knows how to finish"
                )));
            }
            if value.chars().any(char::is_control) {
                return Err(crate::error::invalid(format!(
                    "{what} is {value:?}, which carries a control character: every field of a \
                     draft's reason is printed on one line, in a refusal and in the publication \
                     record, and a value that renders as something other than itself is not one"
                )));
            }
        }
        Ok(())
    }

    /// The one line a person reads, whichever kind of reason this is.
    pub(crate) fn because(&self) -> &str {
        match self {
            DraftReason::AwaitingRelease { because, .. } | DraftReason::Held { because } => because,
        }
    }

    /// The word the `kind` tag travels as: `awaiting-release` or `held`.
    ///
    /// Read off the serialized form rather than restated, so a rendering cannot
    /// spell a kind the record does not.
    pub(crate) fn kind(&self) -> String {
        self.fields()["kind"]
            .as_str()
            .expect("a tagged enum serializes its kind as a string")
            .to_owned()
    }

    /// The reason as the fields an event payload and a refusal both name it by:
    /// the `kind` tag and every field of that kind, which is exactly the serialized
    /// form.
    fn fields(&self) -> serde_json::Value {
        serde_json::to_value(self).expect("a draft reason serializes: strings and a name")
    }

    /// The reason as a refusal spells what was asked for — "a draft awaiting … " or
    /// "a draft held by the session that opened it" — so the two refusals a draft
    /// can meet name the same thing the same way whichever kind of reason it was.
    pub(crate) fn asked_for(&self) -> String {
        match self {
            DraftReason::AwaitingRelease {
                awaiting, target, ..
            } => format!("a draft awaiting {awaiting} {target}"),
            DraftReason::Held { because } => {
                format!("a draft held by the session that opened it ({because})")
            }
        }
    }
}

/// A title that can be the subject of the commit a publication lands.
///
/// The check is in the conversion, for the reason every other validated name in
/// this crate has one there: a publication commits the session's work and merges
/// its base before it composes a message, so a title rejected where the message is
/// composed is rejected *after* those. A [`PublishRequest`] carrying an unusable
/// title is therefore unrepresentable rather than representable-and-refused-later
/// — and a caller embedding this crate meets the refusal where it built the
/// request, not after a commit it cannot undo.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Subject(String);

/// The title itself, quoted — never the wrapper. A refusal that names one writes
/// it with `{:?}`, and a derived `Debug` would spell it `Subject("feat: …")` at
/// the operator rather than the title they typed.
impl std::fmt::Debug for Subject {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&self.0, f)
    }
}

impl TryFrom<String> for Subject {
    type Error = String;

    fn try_from(value: String) -> std::result::Result<Self, String> {
        provenance::checked_subject(&value).map(Subject)
    }
}

/// So a title arriving on a command line is checked by the same conversion a title
/// arriving through the library is.
impl std::str::FromStr for Subject {
    type Err = String;

    fn from_str(value: &str) -> std::result::Result<Self, String> {
        Subject::try_from(value.to_owned())
    }
}

impl From<Subject> for String {
    fn from(subject: Subject) -> Self {
        subject.0
    }
}

impl std::ops::Deref for Subject {
    type Target = str;

    fn deref(&self) -> &str {
        &self.0
    }
}

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

/// What one publication did.
///
/// The value `onevcs publish` renders and a caller embedding this crate branches
/// on. Every ending is a case of [`PublishOutcome`] rather than a sentence to
/// match on, because the sentence is what a consumer read wrongly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Publication {
    /// The session that was published.
    pub session: SessionToken,
    /// The branch that carried the change.
    // llmlint: ignore[invalid_states_unrepresentable] this is the session's own branch,
    // and the contract declares that field verbatim as `pub branch: String` on `Session`;
    // spelling it as a validated ref here would disagree with the type the contract fixes
    // and add a public item it does not name. The validated `workspace::Ref` is what the
    // publication path carries internally, and every value written here is one — it is
    // `record.branch`, which git's own parser accepted before the session was opened.
    pub branch: String,
    /// The policy it was published under, after the repository's rules and any
    /// per-run narrowing have both had their say.
    pub policy: MergePolicy,
    /// What happened.
    pub outcome: PublishOutcome,
}

/// How a publication ended.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PublishOutcome {
    /// The change reached its base, at this commit.
    Merged(Sha),
    /// A change request is open, which the policy asked for.
    ChangeOpen(Url),
    /// A change request is open **as a draft**, carrying the reason it is not ready.
    ///
    /// Its own case rather than a shade of [`ChangeOpen`](PublishOutcome::ChangeOpen),
    /// deliberately: the two differ in the one thing a caller acts on — whether this
    /// change can land — and folding them together would make every exhaustive match
    /// on this enum go on compiling while meaning something else.
    ChangeDraft(Url),
    /// A change request whose required checks came back green, kept **as a draft**
    /// for its own user's review: what the draft lifecycle ends in under `change-open`
    /// with approvals required.
    ///
    /// Its own case rather than a shade of [`ChangeDraft`](PublishOutcome::ChangeDraft)
    /// or [`ChangeOpen`](PublishOutcome::ChangeOpen), for the reason `ChangeDraft` is
    /// one: a consumer acts on the difference. This work is done — green, and waiting
    /// for the person who dispatched it — while a `ChangeDraft` is held back by a
    /// reason, and a `ChangeOpen` is already asking the team for review.
    ChangeReviewDraft(Url),
    /// The branch was pushed into a draft held for review **without its checks being
    /// read**: what a [`defer_checks`](PublishRequest::defer_checks) publication ends
    /// in, having asked the host nothing.
    ///
    /// Its own case rather than a [`ChangeReviewDraft`](PublishOutcome::ChangeReviewDraft),
    /// because that case says the checks came back green and this one says nothing
    /// about them: their verdict is read later, through
    /// [`change_standings`](crate::change_standings).
    ChangeReviewDraftUnchecked(Url),
    /// The host queued the merge and will land it once its checks pass.
    Queued(Url),
    /// The branch had nothing the base did not already carry.
    NothingToPublish,
    /// The change did not land, and this is why.
    ///
    /// A case rather than an `Err`, in exactly the places the CLI reports a
    /// non-zero exit rather than a refusal — so the two surfaces cannot disagree
    /// about which failures are which.
    Failed {
        /// Which failure it is, which is what a caller branches on.
        kind: FailureKind,
        /// The failure as it reads, which is what the CLI writes to stderr.
        reason: String,
        /// What became of the branch. The work is the only record of itself, so
        /// whether it outlived the session is the first thing a caller asks.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        retained: Option<Retention>,
        /// The host's own merge, left armed on the change request, where the wait for
        /// its checks elapsed with none of them red: set only with
        /// [`FailureKind::ChecksUnsettled`], and then the change lands on the host's
        /// clock once its required checks pass, with nobody publishing it again.
        // llmlint: ignore[invalid_states_unrepresentable] `docs/contract.md` fixes this
        // field verbatim as an optional `armed` on `Failed`, beside `retained`, and
        // onepipeline compiles against that shape — splitting the case would change the
        // agreed surface. The one place that sets it, the elapsed wait's arming, sets
        // it with `ChecksUnsettled`; every other path passes it through unchanged.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        armed: Option<ArmedMerge>,
    },
}

/// A change request whose merge a publication left armed when its wait for the checks
/// elapsed: the host merges it once every required check passes, and refuses to while
/// any is red.
///
/// What a caller routing a `checks-unsettled` reads to tell work that will land by
/// itself from work nothing is carrying any further. A later publication of the same
/// session at the same head watches the same change request again rather than opening
/// a second one.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArmedMerge {
    /// The change request armed to merge.
    pub change: Url,
    /// The head commit it was armed at.
    // llmlint: ignore[invalid_states_unrepresentable] the contract fixes this field as
    // `pub head: String`; every value written here is the commit the publication pushed,
    // which git's own parser produced.
    pub head: String,
}

impl PublishOutcome {
    /// How the publication is reported to a human.
    #[must_use]
    pub fn describe(&self) -> String {
        match self {
            PublishOutcome::Merged(sha) => format!("merged at {}", sha.0),
            PublishOutcome::ChangeOpen(url) => format!("change request open at {url}"),
            PublishOutcome::ChangeDraft(url) => {
                format!(
                    "change request open as a draft at {url}, which cannot land while it is one"
                )
            }
            PublishOutcome::ChangeReviewDraft(url) => format!(
                "change request's checks are green at {url}, and it is kept as a draft for its \
                 user's review"
            ),
            PublishOutcome::ChangeReviewDraftUnchecked(url) => format!(
                "pushed into the draft at {url}, kept for its user's review; its checks were \
                 not read"
            ),
            PublishOutcome::Queued(url) => format!("merge queued for {url}"),
            PublishOutcome::NothingToPublish => {
                "nothing to publish: the base already carries this branch's content".to_owned()
            }
            PublishOutcome::Failed { reason, .. } => reason.clone(),
        }
    }
}

/// Which failure ended a publication, and therefore which exit code says so.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FailureKind {
    /// A publication was refused by something that judged it, where no narrower
    /// kind says which — the repository's own `commit-msg` hook turning down the
    /// subject, or a host that took a merge and then reported it unperformed. What
    /// the *host's checks* reported is [`ChecksFailed`](FailureKind::ChecksFailed)
    /// or [`ChecksUnsettled`](FailureKind::ChecksUnsettled), what its `pre-push`
    /// hook said is [`PushRejected`](FailureKind::PushRejected), and a push that
    /// landed with the merge path unreadable behind it is
    /// [`PushedUnverified`](FailureKind::PushedUnverified).
    ///
    /// Named for the tier this crate used to run itself and kept under that name:
    /// the contract fixes this vocabulary across the three libraries that route on
    /// it, so a variant is not renamed because the tier behind it went away.
    // llmlint: ignore[names_match_behavior] the approved contract fixes the spelling.
    Gate,
    /// Input was rejected at a trust boundary.
    Invalid,
    /// The base moved under the publication and the bounded resolve-and-requeue
    /// did not converge.
    SyncConflict,
    /// The request was well-formed and the seam behind it has no implementation.
    NotImplemented,
    /// A required check the host reports concluded red. The reason names it.
    ChecksFailed,
    /// The bound on watching the host elapsed with the change still outstanding.
    /// The reason names what was: the required checks that had not settled, that
    /// the host declared none, or that it never performed the merge it took.
    // The vocabulary is fixed across the three libraries that route on it, and it
    // gives the bound one kind whichever of its endings this was; `reason` says which.
    // llmlint: ignore[names_match_behavior] one fixed kind for the bound's three endings.
    ChecksUnsettled,
    /// The publishing push was refused by the merge path. The reason carries git's
    /// own per-ref refusal.
    PushRejected,
    /// The publishing push **reached the remote**, and the merge path could not
    /// then be read. The reason names both: where the push landed, and what stopped
    /// the read.
    ///
    /// Widening the vocabulary rather than adding a clause to the three above is
    /// the decision recorded on [`Error::PushedUnverified`], and the reasoning is
    /// there: a router branches on the kind, so only a kind can stop work that is
    /// on the remote reading as work that never landed.
    PushedUnverified,
    /// The publishing push was refused by the merge path because this host is
    /// missing a tool or a credential a hook needs, which the hook said on the line
    /// [`HOST_PREREQUISITE_MARKER`] begins. The reason carries what it said is
    /// missing and how to install it, and the same evidence pointers
    /// [`PushRejected`](FailureKind::PushRejected) does.
    ///
    /// A kind rather than a clause on `PushRejected`, for the reason
    /// [`PushedUnverified`](FailureKind::PushedUnverified) is one: a router branches
    /// on the kind, and this is a refusal no change to the work can clear.
    HostPrerequisite,
    /// The caller cancelled the publication while it waited — on the host's checks,
    /// on a merge the host was armed to perform, or on the identity's merge queue —
    /// through [`publish_with_cancellation`](crate::publish_with_cancellation). The
    /// reason names what it was waiting on.
    ///
    /// Nothing is undone: the branch stays on its remote, any change request stays
    /// open, and the session is left so that publishing it again continues the same
    /// branch. A kind of its own, with an exit code of its own, because it is neither a
    /// verdict on the work nor a merge path that could not be read, and a router that
    /// read it as either would retry or abandon work nobody judged.
    Cancelled,
}

impl FailureKind {
    /// The exit code the contract fixes for this failure.
    ///
    /// The one statement of it: `onevcs publish` reports this, and a caller
    /// embedding the crate reads the same mapping rather than rediscovering it.
    #[must_use]
    pub fn exit_code(self) -> u8 {
        match self {
            // The contract fixes `1` for every verification failure and the codes are
            // the published surface, so which one it was travels on the kind instead.
            // Widening the set is an amendment, reported rather than taken here.
            // llmlint: ignore-block[cli_output_contract] the approved contract fixes this code.
            FailureKind::Gate
            | FailureKind::ChecksFailed
            | FailureKind::ChecksUnsettled
            | FailureKind::PushRejected
            | FailureKind::PushedUnverified
            | FailureKind::HostPrerequisite => 1,
            // llmlint: ignore-end[cli_output_contract]
            FailureKind::Invalid => 2,
            FailureKind::SyncConflict => 3,
            FailureKind::NotImplemented => 70,
            // `EX_TEMPFAIL`: nothing failed, and the same publication can be asked for
            // again. Distinct from every other kind's, so a caller reading `$?` alone
            // cannot take a cancellation for a verdict.
            FailureKind::Cancelled => 75,
        }
    }

    /// Which failure an error is.
    ///
    /// Public because a supplied implementation of [`Vcs`](crate::Vcs) has to
    /// report the same kind for the same failure — a publication that started and
    /// did not land is an outcome on every backend, and which one it is cannot be
    /// left to a restatement of this match.
    #[must_use]
    pub fn of(error: &Error) -> Self {
        match error {
            Error::GateFailed { .. } => FailureKind::Gate,
            Error::SyncConflict { .. } => FailureKind::SyncConflict,
            Error::NotImplemented { .. } => FailureKind::NotImplemented,
            Error::ChecksFailed { .. } => FailureKind::ChecksFailed,
            Error::ChecksUnsettled { .. } => FailureKind::ChecksUnsettled,
            Error::PushRejected { .. } => FailureKind::PushRejected,
            Error::PushedUnverified { .. } => FailureKind::PushedUnverified,
            Error::HostPrerequisite { .. } => FailureKind::HostPrerequisite,
            Error::Cancelled { .. } => FailureKind::Cancelled,
            _ => FailureKind::Invalid,
        }
    }
}

/// What became of a branch whose publication did not land.
///
/// The session's clone is disposable, so a branch that is not handed back goes
/// with it. Which of these happened decides whether there is anything left to
/// recover, and the CLI says so on stderr for the same reason.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Retention {
    /// The branch was handed back to this checkout, which now carries the work.
    HandedBack(PathBuf),
    /// This checkout refused the branch, so nothing outside the session carries it.
    Refused(PathBuf),
}

/// Whether the caller of a publication has asked it to stop.
///
/// Handed to [`publish_with_cancellation`](crate::publish_with_cancellation) and asked,
/// at least once a second, by every phase of a publication that waits: the watch of the
/// host's checks, a draft's settle and the one after an early lift, the watch of a
/// merge the host was armed to perform, and the wait for the identity's merge queue.
/// A cancelled publication ends as [`FailureKind::Cancelled`] and undoes nothing.
///
/// A git command already running — the publishing push and its `pre-push` hook
/// included — is never interrupted: the answer is asked again once it returns.
pub trait PublicationCancellation {
    /// Whether the publication should stop at the next point it is asked.
    fn is_cancelled(&self) -> bool;
}

/// The cancellation nobody can trigger, which is every publication [`crate::publish`]
/// and the command line run.
pub(crate) struct NeverCancelled;

impl PublicationCancellation for NeverCancelled {
    fn is_cancelled(&self) -> bool {
        false
    }
}

/// The longest a waiting phase sleeps between two askings of its cancellation, so a
/// cancellation is observed within it whatever `ONEVCS_CHECKS_POLL_SECONDS` says.
pub(crate) const CANCELLATION_GRAIN: std::time::Duration = std::time::Duration::from_millis(100);

/// Sleep for `span`, asking `cancellation` at least every [`CANCELLATION_GRAIN`], and
/// answer `cancelled()` the first time it says so.
pub(crate) fn pause_unless_cancelled(
    span: std::time::Duration,
    cancellation: &dyn PublicationCancellation,
    cancelled: impl Fn() -> Error,
) -> Result<()> {
    let until = std::time::Instant::now() + span;
    loop {
        if cancellation.is_cancelled() {
            return Err(cancelled());
        }
        let left = until.saturating_duration_since(std::time::Instant::now());
        if left.is_zero() {
            return Ok(());
        }
        std::thread::sleep(left.min(CANCELLATION_GRAIN));
    }
}

/// The failure a cancelled publication ends with, saying what it was doing and that
/// nothing was undone.
pub(crate) fn cancelled_while(doing: &str) -> Error {
    Error::Cancelled {
        reason: format!(
            "the caller cancelled the publication while {doing}. Nothing was undone: the \
             branch stays where it is, any change request stays open, and publishing the \
             session again continues it"
        ),
    }
}

/// Publish one session's branch, as the git implementation does it.
///
/// The whole of `onevcs publish` behind the interface, which is where it had to
/// move: it starts from the session record, and a record only this implementation
/// writes is what made a session opened through the seam unpublishable.
///
/// The split between `Err` and [`PublishOutcome::Failed`] is the CLI's own, kept
/// exactly: a publication that could not *start* — an unknown token, an unreadable
/// registry, a `--policy` that widens — is a refusal, and one that started and did
/// not land is an outcome carrying what became of the branch.
///
/// `cancellation` is asked by every phase that waits, and a publication it stops ends
/// as [`FailureKind::Cancelled`] with the record left open, exactly as every other
/// failure leaves it — so publishing the session again continues the same branch.
pub fn run_for_session(
    token: &SessionToken,
    request: &PublishRequest,
    hosting: &dyn Hosting,
    cancellation: &dyn PublicationCancellation,
) -> Result<Publication> {
    let record = workspace::load(&token.0)?;
    // Before anything is read, locked or committed: a session opened over a conflicted
    // merge is one whose worker has not concluded it yet, and the preserve below would
    // conclude it for them, markers and all.
    vcs::refuse_unfinished_merge(
        &record.worktree,
        &record.branch,
        &format!(
            "publish it again with `{}`",
            crate::guidance::command(["onevcs", "publish", &record.token])
        ),
    )?;
    let registry = store::load()?;
    let resolution = store::resolve(&registry, &record.identity)?;
    let (file, source) = policy::load(&registry)?;
    let normalized = store::normalize(&resolution.identity.origin);
    let resolved = policy::resolve(&file, &source, &normalized, &resolution.publication);
    let effective = effective_policy(&resolved.policy, request.policy)?;
    // Before anything is preserved or pushed: a deferred publication is refused whole
    // or not at all.
    let adopt = deferred(
        request,
        &record,
        &resolution.key,
        &resolved.policy,
        effective,
    )?;

    let mut stream = Stream::open(&record.token)?;
    stream.label("identity", &record.identity);

    // On the stream this publication is already writing, deliberately: a second
    // stream over the same file resumes the same sequence number, and the two would
    // then emit one `seq` twice — which is exactly the gap a consumer reads as loss.
    if git::is_dirty(&record.worktree)? {
        vcs::preserve_into(&record, &mut stream, Provenance::Complete)?;
    }
    let target = recorded_target(&record, &resolution.publication)?;
    let context = Context {
        resolution,
        policy: resolved.policy.clone(),
        effective,
        repo: record.clone.clone(),
        worktree: record.worktree.clone(),
        branch: record.branch.clone(),
        target,
        // Nothing has rewritten this branch yet; a replay below decides otherwise.
        push: Push::Forward,
        run_root: record.run_root.clone(),
        preserved_into: record.execution_checkout.clone(),
        title: request.title.clone(),
        body: request.body.clone(),
        draft: request.draft.clone(),
        drafts: resolved.drafts.clone(),
        base_merge: resolved.base_merge.value,
        checks_wait: resolved.checks_wait.clone(),
        trailers: Vec::new(),
        provenance: provenance::from_rules(&file),
        hosting,
        cancellation,
        built: Built::InWorkspace,
        term_scope: request.term_scope.clone(),
        adopt,
    };
    let branch = record.branch.to_string();
    // The branch is the only record of the work, so it is handed back to the execution
    // checkout whatever ended the publication without landing it — the alternative is
    // a tree that exists only in a run root about to be reclaimed.
    let failed = |kind: FailureKind, reason: String, armed: Option<ArmedMerge>| {
        let checkout = record.execution_checkout.clone();
        let retained = match git::copy_branch(&record.clone, &checkout, &record.branch) {
            Ok(true) => Retention::HandedBack(checkout),
            _ => Retention::Refused(checkout),
        };
        PublishOutcome::Failed {
            kind,
            reason,
            retained: Some(retained),
            armed,
        }
    };
    let outcome = match run_resolving(&context, &mut stream) {
        // An ending the publication reached as a value rather than a refusal — a wait
        // that elapsed with the merge left armed — is still one that did not land, so
        // the session stays open for the publication that watches it again.
        Ok((
            PublishOutcome::Failed {
                kind,
                reason,
                armed,
                ..
            },
            _,
        )) => failed(kind, reason, armed),
        Ok((outcome, landed)) => {
            // A stacked change the root carried was replayed onto the root, and the
            // record learns that: every later read and publication of this session
            // then resolves the root directly rather than a stack the replayed
            // branch no longer shows — the tip it was cut from is out of its history
            // now, so there is no stack left to record. Recorded rather than
            // inferred, exactly as the stack itself was: this publication is the one
            // that moved it.
            let moved = landed != context.target;
            // A publication is the end of a session's work, so the record closes
            // with it — with one exception. A draft the session itself is holding
            // is a session still being worked in: its worktree is still the
            // dispatch's, and `session close` is what ends it. That matters because
            // `workspace::reclaim` keeps a run root only while an *open* record names
            // it, so a held draft that closed the record would let the next sibling
            // `session open` reap the worktree the worker is committing in. A draft
            // held for a release is the other thing: the work is finished, and the
            // next worker opens a session of its own.
            //
            // Written through `workspace::update`, which re-reads the record under its
            // lock: the copy loaded when this publication began is minutes old by now,
            // and a session opened over the same branch meanwhile has recorded on it
            // which session continued the work.
            let still_working = matches!(outcome, PublishOutcome::ChangeDraft(_))
                && matches!(context.draft, Some(DraftReason::Held { .. }));
            if !still_working || moved {
                workspace::update(&record.token, |stored| {
                    if moved {
                        stored.change_base = Some(landed.base().clone());
                        stored.stack_tip = None;
                    }
                    if !still_working {
                        stored.state = Lifecycle::Closed;
                    }
                })?;
            }
            outcome
        }
        Err(error) => failed(FailureKind::of(&error), error.to_string(), None),
    };
    Ok(Publication {
        session: token.clone(),
        branch,
        policy: effective,
        outcome,
    })
}

/// Everything one publication needs to know about itself.
pub struct Context<'a> {
    /// The identity and checkouts the branch belongs to.
    pub resolution: Resolution,
    /// The resolved policy, before any per-run narrowing.
    pub policy: Policy,
    /// The policy actually being published under.
    pub effective: MergePolicy,
    /// The repository the branch lives in — a session clone, or a recovery clone.
    pub repo: PathBuf,
    /// The tree this publication is built in.
    pub worktree: PathBuf,
    /// The branch carrying the change.
    pub branch: Ref,
    /// Where this publication lands, and what it is compared against on the way.
    pub target: Target,
    /// How its branch reaches the host, for a caller that rewrote the branch before
    /// handing it here. A publication that replays during its own run decides this
    /// for itself.
    pub push: Push,
    /// Where preserved merge-path logs are written.
    pub run_root: PathBuf,
    /// The checkout that keeps this branch once the run root is gone — a session's
    /// execution checkout, or the checkout a branch-keyed verb read the branch out
    /// of.
    ///
    /// Written to for one thing only: the landing record. Everything else this
    /// publication does happens in [`repo`](Context::repo), which is disposable.
    pub preserved_into: PathBuf,
    /// An explicit title, which replaces the synthesized subject. Checked where it
    /// was built, so nothing here can compose a message from one that is not a
    /// subject.
    pub title: Option<Subject>,
    /// The body the change request is opened with, verbatim, or none at all. A
    /// branch-keyed verb has no caller to take one from and publishes without one.
    pub body: Option<String>,
    /// Why this change request is opened as a draft, or none at all — which is both
    /// an ordinary publication and the thing that *lifts* a draft one.
    ///
    /// A branch-keyed verb has no caller to take one from and publishes without one,
    /// as it does with a body: landing a branch somebody else drafted is exactly the
    /// call that says the reason no longer holds.
    pub draft: Option<DraftReason>,
    /// How the draft lifecycle resolved for this identity: whether a reasonless
    /// publication opens a draft while its checks run, and whether an early lift
    /// warns.
    pub drafts: policy::DraftLifecycle,
    /// When a moved base is merged into this branch before it is pushed: always, or
    /// only where it conflicts or the host requires an up-to-date branch.
    pub base_merge: crate::rules::BaseMerge,
    /// How long the watch of the change request's checks lasts, as the identity's
    /// rules resolve it — before the process-wide override, which the watch asks.
    pub checks_wait: policy::Decided<crate::rules::WaitSeconds>,
    /// Trailers the publication commit must carry.
    pub trailers: Vec<String>,
    /// The provenance trailer keys this host reads and writes, which decide which
    /// of the branch's commits describe the change and which record the session.
    pub provenance: provenance::Trailers,
    /// Where the host that lands a change request comes from. The seam, carried on
    /// the context rather than reached for at the call site, so every publication —
    /// a session's and a recovery's — goes through the one a caller supplied.
    pub hosting: &'a dyn Hosting,
    /// Whether the caller has asked this publication to stop, asked by every phase
    /// that waits. A branch-keyed verb has no caller to ask and is never cancelled.
    pub cancellation: &'a dyn PublicationCancellation,
    /// Where this publication was built, which decides how its landing is recorded.
    pub built: Built,
    /// Which private repositories the public boundary check derives its terms from.
    pub term_scope: TermScope,
    /// The draft a deferred publication pushes into and asks the host nothing about,
    /// already held to everything [`deferred`] checks. `None` for every other
    /// publication, a branch-keyed verb's included.
    pub adopt: Option<Url>,
}

/// Where a publication was built.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Built {
    /// In a workspace cut for it, whose [`worktree`](Context::worktree) has the branch
    /// checked out: its landing is committed there and handed to the checkout that
    /// keeps the branch.
    InWorkspace,
    /// Nowhere: it resumes a publication whose verification already passed, and
    /// [`repo`](Context::repo) is the checkout that keeps the branch. Its landing is
    /// recorded there by ref, because that checkout's worktree is somebody else's.
    Resumed,
}

/// How a publication's branch reaches the host.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Push {
    /// Forward or not at all, which is every publication that rewrote nothing.
    Forward,
    /// Replacing one commit and no other, because this publication replayed the
    /// branch's own commits onto the root and what it pushes is therefore no
    /// descendant of what the host has.
    Replacing {
        /// The commit this repository last saw the host's copy of the branch at,
        /// which is the only one this may replace. Read before the fetch rather than
        /// after: a host that moved *since* is the thing this exists to catch, and a
        /// value taken from a later fetch would name that move and allow it.
        // llmlint: ignore[invalid_states_unrepresentable] `git::tip` answered it.
        replaced: String,
    },
}

/// Where a publication lands, and what it is compared against on the way there.
///
/// One or the other and never a mixture: everything that follows from being stacked
/// — what the branch is compared against, what its change request targets, where it
/// lands, and which commit its own work begins after — comes from this one value, so
/// a publication cannot hold a stack and a root that disagree about any of it.
///
/// Stacked is *recorded*, never inferred: a branch that carries the change below it
/// commit for commit reads exactly like a branch that wrote those commits itself, so
/// a stack read off content would rewrite branches nobody stacked.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Target {
    /// Onto one branch, which is also what the change is compared against: the base
    /// a session was opened with, or the root a stacked change moved onto once the
    /// change below it landed.
    Base(Ref),
    /// Onto the change below this one, until the root carries that change.
    Stacked {
        /// The branch below: what this change targets and is compared against.
        below: Ref,
        /// The identity's root base, which this moves onto once the root carries
        /// the change below.
        root: Ref,
        /// The commit the branch was cut from, after which its own work begins.
        // llmlint: ignore[invalid_states_unrepresentable] `git::tip` answered it.
        tip: String,
    },
}

impl Target {
    /// The branch this change is compared against and lands on.
    pub fn base(&self) -> &Ref {
        match self {
            Target::Base(base) => base,
            Target::Stacked { below, .. } => below,
        }
    }
}

/// The stack a session's record wrote down, or `None` for the branch every ordinary
/// session cuts.
///
/// Two things have to be there: the tip the session was cut at, which `session open`
/// records only for a base that is not the identity's root, and a root to land on
/// once the change below has landed.
///
/// The recorded tip is resolved through git before it is trusted — a record is a file
/// under the state root — and one this session's clone does not have is refused rather
/// than read as no stack: answering "no stack" from a value nothing could read is the
/// merge this whole path exists to stop.
///
/// A root nobody can name is different and is no stack: nothing here fails, there is
/// simply nowhere to move the change to, and the publication is the one it always
/// was.
fn recorded_target(record: &workspace::Record, publication: &Path) -> Result<Target> {
    let base = preserved_change_base(&record.base, record.change_base.as_ref());
    let Some(recorded) = record.stack_tip.as_deref() else {
        return Ok(Target::Base(base));
    };
    let Some(tip) = git::tip(&record.clone, recorded) else {
        return Err(Error::Invalid {
            reason: format!(
                "the record for session {token} names {recorded:?} as the commit branch {branch:?} \
                 was cut from, and the clone at {clone} does not have it, so nothing can tell \
                 which of that branch's commits belong to the change below it. The branch is \
                 whole — publish it by name with `{command}`, which reads the stack off the \
                 branch instead",
                token = record.token,
                branch = record.branch,
                clone = record.clone.display(),
                command = guidance::command([
                    "onevcs",
                    "publish-branch",
                    &record.branch,
                    "--repo",
                    &record.publication_checkout.to_string_lossy(),
                ]),
            ),
        });
    };
    let Some(root) = git::default_branch(publication, "origin").ok() else {
        return Ok(Target::Base(base));
    };
    let root = Ref::from_git(root);
    if root == base {
        return Ok(Target::Base(base));
    }
    Ok(Target::Stacked {
        below: base,
        root,
        tip,
    })
}

/// Whether the root base already carries everything the change below this one
/// contributed, while carrying none of the commits that contributed it.
///
/// Three things past the record: the branch was cut from that tip, the root does not
/// hold the tip's commits under their own names, and the root does hold what they
/// changed. What that establishes is exactly its name and no more — it is what a
/// squash-merge of the change below leaves, and a root that came by the same content
/// some other way is indistinguishable from one here, because content equality is all
/// git can be asked. Both are answered the same way deliberately: the commits a
/// replay drops are commits whose content the root already has, so the branch's own
/// work is what is left either way. A change still open whose content has *not*
/// reached the root fails the last test, and one merged as its own commits fails the
/// second.
pub(crate) fn root_is_known_to_carry_the_stack(
    repo: &Path,
    branch: &str,
    root: &Ref,
    tip: &str,
) -> Result<bool> {
    let root = vcs::base_ref(repo, root);
    if !git::ref_exists(repo, &format!("refs/remotes/{root}")) && !git::branch_exists(repo, &root) {
        return Ok(false);
    }
    if !git::is_ancestor(repo, tip, branch)? {
        return Ok(false);
    }
    // Still in the root as the commits it was written as: merging the root brings
    // them in the way it always has, and there is nothing to replay past.
    if git::is_ancestor(repo, tip, &root)? {
        return Ok(false);
    }
    let Some(fork) = git::merge_base(repo, &root, tip)? else {
        return Ok(false);
    };
    git::known_to_carry_changes(repo, &root, &fork, tip)
}

/// Where a publication lands once the root has been consulted: the target it was
/// handed, or the root once the root carries the change below — and, in that case,
/// the tip the branch's own work begins after, which is what a replay onto the root
/// starts from.
///
/// Asked after a fetch, deliberately: the root is what decides it, and a clone's own
/// copy of the root is only as current as its last fetch. Every target that is not a
/// stacked one is handed back as it was, with nothing to replay.
pub(crate) fn landed_target(
    repo: &Path,
    branch: &Ref,
    target: &Target,
) -> Result<(Target, Option<String>)> {
    if let Target::Stacked { root, tip, .. } = target {
        if root_is_known_to_carry_the_stack(repo, branch, root, tip)? {
            return Ok((Target::Base(root.clone()), Some(tip.clone())));
        }
    }
    Ok((target.clone(), None))
}

/// The change request a session's publication opens or adopts: the branch it pushes,
/// into the base it resolves for it, stacked or not.
///
/// One computation for `publish` and for the `change` verbs that address the same
/// change request afterwards, so no session can have the two naming different ones.
/// It is the resolution [`run_for_session`] and [`run`] make between them — the
/// target the record wrote down, then the root once the root carries the change
/// below — made after the same fetch a publication makes, because the root decides
/// the second step and the clone's own copy of it is only as current as its last
/// fetch. A `change show` on a stacked session therefore answers the change request
/// the stack publishes onto, which is the one its next `publish` adopts.
pub(crate) fn session_target(record: &workspace::Record) -> Result<Target> {
    if git::has_remote(&record.clone, "origin") {
        git::fetch(&record.clone, "origin")?;
    }
    standing_target(record)
}

/// The same resolution as [`session_target`], from what the session's clone already
/// holds, without asking the remote.
///
/// What a *read* of the session judges its branch against: the base its publication
/// would resolve from the clone's own view, which is the branch below until the
/// clone has seen the root carry it. A stacked session whose branch below has
/// landed and gone loses that name from the clone at the next fetch — its
/// publication's own fetch prunes — and a read that kept comparing against the
/// recorded base would then fail on a branch nothing has any more, for a session
/// whose work is exactly where its publication put it.
pub(crate) fn standing_target(record: &workspace::Record) -> Result<Target> {
    let recorded = recorded_target(record, &record.publication_checkout)?;
    let (target, _replay) = landed_target(&record.clone, &record.branch, &recorded)?;
    Ok(target)
}

impl<'a> Context<'a> {
    /// The same publication, landing where `target` says instead.
    fn onto(&self, target: Target) -> Context<'a> {
        Context {
            resolution: self.resolution.clone(),
            policy: self.policy.clone(),
            effective: self.effective,
            repo: self.repo.clone(),
            worktree: self.worktree.clone(),
            branch: self.branch.clone(),
            target,
            push: self.push.clone(),
            run_root: self.run_root.clone(),
            preserved_into: self.preserved_into.clone(),
            title: self.title.clone(),
            body: self.body.clone(),
            draft: self.draft.clone(),
            drafts: self.drafts.clone(),
            base_merge: self.base_merge,
            checks_wait: self.checks_wait.clone(),
            trailers: self.trailers.clone(),
            provenance: self.provenance.clone(),
            hosting: self.hosting,
            cancellation: self.cancellation,
            built: self.built,
            term_scope: self.term_scope.clone(),
            adopt: self.adopt.clone(),
        }
    }
}

/// The draft a deferred publication pushes into, or `None` for a publication that
/// watches its checks as it always has — refusing, before anything is preserved or
/// pushed, every deferred request this build will not carry out.
///
/// A deferred publication asks the host nothing, so everything it needs is decided
/// here from what this host holds: the resolved policy must hold its change request
/// for review, the change request must be one of the session's repository, and this
/// host's newest record of its head branch must name the session's branch. Where this
/// host holds no record of it at all, the refusal is its own kind,
/// [`Error::UnrecordedChangeHead`], so a caller can tell it from a mismatch and fall back
/// to a publication that reads the host.
fn deferred(
    request: &PublishRequest,
    record: &workspace::Record,
    identity: &str,
    resolved: &Policy,
    effective: MergePolicy,
) -> Result<Option<Url>> {
    let token = &record.token;
    let (defer, adopt) = match (request.defer_checks, &request.adopt) {
        (false, None) => return Ok(None),
        (true, Some(adopt)) => (true, adopt),
        (true, None) => {
            return Err(crate::error::invalid(format!(
                "--defer-checks publishes into a draft that is already open, and names no host \
                 call to find it with: name it with --adopt <URL>. Nothing was pushed; `{}` \
                 publishes and watches the checks instead",
                guidance::command(["onevcs", "publish", token])
            )))
        }
        (false, Some(adopt)) => {
            return Err(crate::error::invalid(format!(
                "--adopt {adopt} is the draft a --defer-checks publication pushes into, and this \
                 publication does not defer its checks: add --defer-checks, or drop --adopt. \
                 Nothing was pushed"
            )))
        }
    };
    debug_assert!(defer);
    if request.draft.is_some() {
        return Err(crate::error::invalid(
            "--defer-checks pushes into a draft held for its user's review, and a draft reason \
             asks for a different draft: drop one of them. Nothing was pushed",
        ));
    }
    if policy::GreenDraft::of(effective, resolved.approvals) != policy::GreenDraft::KeepForReview {
        return Err(crate::error::invalid(format!(
            "--defer-checks is for a change request held for review — a change-open identity \
             whose approvals are required — and {identity} publishes {} here, so its checks are \
             what decides it. Nothing was pushed; `{}` publishes and watches them",
            policy::spell(effective),
            guidance::command(["onevcs", "publish", token])
        )));
    }
    let named = crate::review::ChangeUrl::parse(adopt.as_str())?;
    if named.identity != identity {
        return Err(crate::error::invalid(format!(
            "{adopt} is a change request of {}, and session {token} publishes to {identity}. \
             Nothing was pushed",
            named.identity
        )));
    }
    match crate::standings::recorded_head(adopt)? {
        Some(head) if head == *record.branch => Ok(Some(adopt.clone())),
        Some(head) => Err(crate::error::invalid(format!(
            "{adopt} carries {head:?} by this host's newest record of it, and session {token} \
             is on {:?}, so it is not this session's change request. Nothing was pushed",
            record.branch
        ))),
        None => Err(Error::UnrecordedChangeHead {
            change: adopt.to_string(),
            reason: format!(
                "this host holds no record of which branch {adopt} carries — no standings read \
                 that saw its head and no change request this host opened — and a \
                 --defer-checks publication asks the host nothing, so it cannot tell whether it \
                 is session {token}'s. Nothing was pushed: read it with `onevcs change \
                 standings`, or publish with `{}`, which reads the host",
                guidance::command(["onevcs", "publish", token])
            ),
        }),
    }
}

/// Verify and publish a branch.
pub fn run(context: &Context<'_>, stream: &mut Stream) -> Result<PublishOutcome> {
    run_resolving(context, stream).map(|(outcome, _)| outcome)
}

/// [`run`], also answering where the branch landed: the target it was handed, or
/// the root a stacked change was replayed onto because the root already carried the
/// change below.
///
/// The second is what a session's own record has to learn, because a replay changes
/// the branch's shape: the tip it was cut from is no longer in its history, so the
/// record's stack cannot be recognised on the branch again — and a later read or
/// publication of the same session that still resolved the recorded stack would
/// compare the branch against a branch below it that has landed and gone.
fn run_resolving(context: &Context<'_>, stream: &mut Stream) -> Result<(PublishOutcome, Target)> {
    // Input, rejected at its boundary: before the fetch, before the sync, and before
    // anything reaches a remote. A draft is a state of a *change request*, so a
    // publication that opens none cannot be in it, and a reason nobody can read is
    // not a reason.
    if let Some(reason) = &context.draft {
        reason.checked()?;
        if context.effective == MergePolicy::LocalDirect {
            return Err(crate::error::invalid(format!(
                "{branch:?} was asked to publish as {asked}, and this identity publishes with \
                 local-direct, which squashes the branch onto {base:?} and opens no change \
                 request at all — so there is nothing to draft and {lands}. Publish it under a \
                 change-* policy, or publish it without a draft",
                branch = context.branch,
                asked = reason.asked_for(),
                base = context.target.base(),
                lands = match reason {
                    DraftReason::AwaitingRelease { .. } => {
                        "the work would land with the very pin the draft exists to hold back"
                    }
                    DraftReason::Held { .. } => {
                        "the work would land while the session is still making it"
                    }
                },
            )));
        }
    }
    // Before anything is compared, because what it may establish is that this branch
    // already landed: a change request a `checks-unsettled` close stopped watching
    // merges on the host's clock, and a publication that met it again republished the
    // work the base already carried.
    take_up_a_late_merge(context);
    // A publication attempt starts here: every gate it runs is numbered as one.
    stream.begin_attempt();
    // Where this repository last saw the host's copy of the branch, read before the
    // fetch that is about to update it: a publication that replays its own commits
    // pushes over that copy, and what it may replace is what it had already seen —
    // never whatever arrived in between, which is the thing worth refusing over.
    let last_seen = git::tip(&context.repo, &format!("origin/{}", context.branch));
    if git::has_remote(&context.repo, "origin") {
        // Outside every exclusive section, deliberately.
        git::fetch(&context.repo, "origin")?;
        stream.emit(
            EventKind::Fetch,
            object(json!({"remote": "origin", "checkout": context.repo.display().to_string()})),
        );
    }
    // Asked after the fetch and before anything else: a change whose stack the root
    // already carries is a change onto the root base, and everything below — what it is compared
    // against, what its change request targets — follows from that rather than from
    // the branch it was opened against.
    let (target, replay) = landed_target(&context.repo, &context.branch, &context.target)?;
    let landed;
    let mut context = context;
    if replay.is_some() {
        landed = context.onto(target.clone());
        context = &landed;
    }
    let remote_base = format!("origin/{}", context.target.base());
    let compared = if git::ref_exists(&context.repo, &format!("refs/remotes/{remote_base}")) {
        remote_base.clone()
    } else {
        context.target.base().to_string()
    };

    sync(context, stream, &compared, replay.as_deref())?;

    if nothing_to_publish(context, &compared)? {
        return Ok((PublishOutcome::NothingToPublish, target));
    }

    let push = match (&replay, &last_seen) {
        (Some(_), Some(replaced)) => Push::Replacing {
            replaced: replaced.clone(),
        },
        // Otherwise it is whatever the caller knows: a branch-keyed verb replays
        // before it hands a publication here, and its branch is rewritten just the
        // same.
        _ => context.push.clone(),
    };
    // The trailers are the publication *commit*'s, so only the local path takes them
    // here — and it re-describes the branch after the queue anyway. Asking now is
    // still what refuses a branch with no usable subject before anything is pushed.
    let (subject, _) = describe(context, &compared)?;
    // The last thing before the first write to the remote: everything this
    // publication would put there — its commits against every parent, their
    // messages, the branch, the title and the body — is held to the public boundary.
    hold_to_the_boundary(context, &compared, Some(&subject))?;
    let environment = merge_path::comparison_env("origin", context.target.base());

    let outcome = match context.effective {
        MergePolicy::LocalDirect => publish_locally(context, stream, &compared, &environment)?,
        _ => publish_as_change(context, stream, &subject, &environment, push)?,
    };
    Ok((outcome, target))
}

/// Hold what this publication would write to its destination to the public
/// boundary: refused before any remote mutation where it carries a private term, and
/// passed without a term being read where the destination is not public.
fn hold_to_the_boundary(context: &Context<'_>, compared: &str, title: Option<&str>) -> Result<()> {
    let tip = context.branch.to_string();
    let base = git::tip(&context.repo, compared).map(|_| compared);
    let outgoing = crate::boundary::screen::Outgoing {
        repo: &context.repo,
        base,
        tip: &tip,
        branch: Some(&tip),
        title,
        body: context.body.as_deref(),
    };
    // A publication that defers its checks asks the host nothing, its visibility
    // included: the answer this host last recorded stands for it.
    if context.adopt.is_some() {
        return crate::boundary::evidence::guard_publication_unasked(
            &context.resolution.key,
            &outgoing,
            &context.term_scope,
        );
    }
    crate::boundary::evidence::guard_publication(
        context.hosting,
        &context.resolution.key,
        &outgoing,
        &context.term_scope,
    )
}

/// The subject a publication of one branch would carry, or the refusal that none
/// of its commits can supply one.
///
/// Public to this crate because it is also a *precondition*: a branch-keyed verb
/// asks it before it writes anything to the branch, so a refusal an explicit
/// `--title` answers is met before the work rather than after a commit the operator
/// then has to reason about.
pub(crate) fn subject_for(
    repo: &Path,
    compared: &str,
    branch: &str,
    title: Option<&str>,
    trailers: &provenance::Trailers,
) -> Result<String> {
    let subject = match provenance::publication_subject(repo, compared, branch, title, trailers)? {
        Ok(subject) => subject,
        Err(reason) => {
            return Err(Error::Invalid {
                reason: format!(
                    "cannot publish {branch:?}: {reason}. A subject that names no change would \
                     make the base branch a worse record than this refusal does."
                ),
            })
        }
    };
    hold_to_repository_policy(repo, branch, &subject)?;
    Ok(subject)
}

/// Put the composed subject to the repository's own `commit-msg` hook, and refuse
/// the publication when it turns the subject down.
///
/// This crate states no subject policy of its own and never will: a squash-merge
/// subject decides whether a release cuts, and *which* subjects release is a fact
/// about the repository rather than about publishing. So the question is asked of
/// the repository, in the one place it can be asked at all — the subject a merge
/// lands under comes from a change request's title, which no local hook ever sees.
///
/// A repository with no `commit-msg` hook expresses no policy and acquires none by
/// being published through here: [`git::MessagePolicy::Unstated`] passes silently.
pub(crate) fn hold_to_repository_policy(repo: &Path, branch: &str, subject: &str) -> Result<()> {
    let git::MessagePolicy::Rejected { status, output } = git::message_policy(repo, subject)?
    else {
        return Ok(());
    };
    let said = guidance::quoted_output(output.trim());
    let said = if said.is_empty() {
        "<no output>".to_owned()
    } else {
        said
    };
    Err(Error::GateFailed {
        reason: format!(
            "the repository's own commit-msg hook rejected the subject publishing {branch:?} \
             would land, {subject:?} (exit {status}). Reword the commit the subject comes from, \
             or publish with an explicit title that satisfies it. The hook said:\n{said}"
        ),
    })
}

/// Whether the base already carries everything this branch has, once the base has
/// been merged into it.
///
/// Two shapes, and only one of them is "no commits": a branch squash-merged under
/// somebody else's change request keeps every commit it had and adds nothing to the
/// tree, so the tree is what decides. Opening a change request for one produces an
/// empty diff, which every path-filtered required check skips rather than runs and
/// the host then blocks forever.
///
/// Asked here, on the one path both policies go through and before anything is
/// pushed, so every caller of a publication gets it.
fn nothing_to_publish(context: &Context<'_>, compared: &str) -> Result<bool> {
    if git::log_messages(&context.repo, compared, &context.branch)?.is_empty() {
        return Ok(true);
    }
    Ok(!git::trees_differ(
        &context.repo,
        compared,
        &context.branch,
    )?)
}

/// The subject one publication commit carries, and what it must carry forward.
fn describe(context: &Context<'_>, compared: &str) -> Result<(String, Vec<String>)> {
    let subject = subject_for(
        &context.repo,
        compared,
        &context.branch,
        context.title.as_deref(),
        &context.provenance,
    )?;
    let mut trailers = context.trailers.clone();
    trailers.extend(provenance::attestation_trailers(
        &context.repo,
        compared,
        &context.branch,
        &context.provenance,
    )?);
    // What this commit lands, named on the base itself. A squash leaves the base
    // carrying the branch's content under no name it kept, so whether the work
    // reached the base could only ever be *inferred* from content afterwards — and
    // that inference stops being true the moment anything edits those paths. This is
    // the record that answers it instead, and it is written here because this path is
    // the one landing that opens no change request: everything the host lands carries
    // the change request's own number.
    if let Some(tip) = git::tip(&context.repo, &context.branch) {
        trailers.push(format!("{} {tip}", context.provenance.landed()));
    }
    Ok((subject, trailers))
}

/// What bringing a branch level with what it lands on did.
pub(crate) enum Reconciled {
    /// The branch carries what it lands on now.
    Settled,
    /// It conflicted, doing this — which is what a refusal about it has to name,
    /// since the resolution an operator is sent to is the shape that was attempted
    /// — and *what* conflicted, which is what makes the refusal actionable rather
    /// than merely true.
    Conflicted(Reconciliation, git::Conflict),
}

/// Which shape bringing a branch level with what it lands on takes.
pub(crate) enum Reconciliation {
    /// The base is merged into the branch.
    Merge,
    /// Only the branch's own commits are replayed onto the base, because the base
    /// already carries its history up to this commit.
    Replay {
        /// The commit where the branch's own work begins, as `git` spelled it.
        // llmlint: ignore[invalid_states_unrepresentable] `git::tip` answered it.
        from: String,
    },
}

/// Bring a branch level with the base it is published onto, once.
///
/// Two shapes, and the record decides which rather than this: ordinarily the base is
/// merged into the branch, and a branch whose recorded stack has landed replays only
/// its own commits onto the base instead — `git rebase --onto <base> <stack tip>
/// <branch>`, which is what an operator ends up running by hand. Merging there would
/// replay the change below against its own squashed equivalent, which conflicts in
/// every file both touched and conflicts again on every bounded retry.
///
/// Shared by `publish` and by the branch-keyed verbs, so the two cannot come to
/// disagree about what a sync does.
pub(crate) fn reconcile(
    worktree: &Path,
    compared: &str,
    branch: &str,
    replay_from: Option<&str>,
) -> Result<Reconciled> {
    let (shape, integrated) = match replay_from {
        Some(tip) => (
            Reconciliation::Replay {
                from: tip.to_owned(),
            },
            git::rebase_onto(worktree, compared, tip, branch)?,
        ),
        None => (
            Reconciliation::Merge,
            git::merge_into_branch(worktree, compared, &merge_message(compared, branch))?,
        ),
    };
    Ok(match integrated {
        git::Integrated::Settled => Reconciled::Settled,
        git::Integrated::Conflicted(conflict) => Reconciled::Conflicted(shape, conflict),
    })
}

/// The subject of the commit a sync's merge writes, so a session's opening merge
/// and a publication's read the same in `git log`.
pub(crate) fn merge_message(compared: &str, branch: &str) -> String {
    format!("Merge {compared} into {branch}")
}

/// Report the conflict that stopped a publication: which paths, and the hunks.
///
/// The paths go in the payload because they are a short list an operator acts on
/// directly — "what conflicts" is the question a refusal that only said *that*
/// something conflicted left unanswered. The hunks go beside it as an artifact,
/// because a diff is evidence rather than a field, and the envelope's own rule is
/// that large evidence is referenced by id.
///
/// Shared with the branch-keyed verbs, which conflict at the same place for the
/// same reasons: a second emitter would be a second answer to what conflicts.
///
/// A hunk artifact that could not be stored is a warning on stderr and no artifact,
/// never a failure: the conflict is the answer, and reporting a filesystem problem
/// in place of it would lose the diagnosis this exists to keep.
pub(crate) fn report_conflict(
    stream: &mut Stream,
    branch: &Ref,
    base: &Ref,
    conflict: &git::Conflict,
    attempts: Option<usize>,
) {
    let artifacts = match stream::store_artifact("diff", conflict.hunks()) {
        Ok(artifact) => vec![artifact],
        Err(error) => {
            eprintln!(
                "onevcs: warning: the conflict on {branch:?} is recorded without its \
                 hunks: {error}"
            );
            Vec::new()
        }
    };
    let mut payload = object(json!({
        "branch": branch,
        "base": base,
        "paths": conflict.paths(),
    }));
    if let Some(attempts) = attempts {
        payload.insert("attempts".to_owned(), json!(attempts));
    }
    stream.emit_with(EventKind::SyncConflict, payload, artifacts);
}

/// Sync the branch with the current base, bounded, before anything is published.
///
/// `replay_from` is the recorded stack tip when the change below this one has
/// landed, and `None` — every publication that is not a stacked one — is the merge
/// this has always been.
fn sync(
    context: &Context<'_>,
    stream: &mut Stream,
    compared: &str,
    replay_from: Option<&str>,
) -> Result<()> {
    if !git::ref_exists(&context.repo, &format!("refs/remotes/{compared}"))
        && !git::branch_exists(&context.repo, compared)
    {
        return Ok(());
    }
    // A stacked branch whose change below landed is replayed whatever the identity
    // says about merging its base: what moves it is the root now carrying the change
    // below, and merging instead would replay that change against itself.
    if replay_from.is_none() {
        let decision = crate::base_merge::decide(
            context.base_merge,
            &crate::base_merge::Merging {
                repo: &context.worktree,
                integrated: compared,
                base: context.target.base(),
                branch: &context.branch,
                identity: &context.resolution.key,
                hosting: context.hosting,
            },
        )?;
        crate::base_merge::record(stream, &context.branch, context.target.base(), &decision);
        if !decision.merges() {
            return Ok(());
        }
    }
    let mut last: Option<(Reconciliation, git::Conflict)> = None;
    for attempt in 1..=SYNC_ATTEMPTS {
        match reconcile(&context.worktree, compared, &context.branch, replay_from)? {
            Reconciled::Settled => return Ok(()),
            Reconciled::Conflicted(shape, found) => last = Some((shape, found)),
        }
        if attempt < SYNC_ATTEMPTS && git::has_remote(&context.repo, "origin") {
            git::fetch(&context.repo, "origin")?;
        }
    }
    // Every path out of the loop above either returned or wrote this, and the bound
    // is at least one attempt — so the refusal below is always about a conflict this
    // run actually met, rather than about a value standing in for one.
    let Some((attempted, conflict)) = last else {
        return Ok(());
    };
    report_conflict(
        stream,
        &context.branch,
        context.target.base(),
        &conflict,
        Some(SYNC_ATTEMPTS),
    );
    let conflicting = guidance::listed(conflict.paths());
    let land = guidance::command([
        "onevcs",
        "publish-branch",
        &context.branch,
        "--repo",
        &context.resolution.publication.to_string_lossy(),
    ]);
    // The branch is retained rather than lost, and the refusal says what would
    // change the answer: another attempt resolves nothing a bounded retry has
    // already tried, and an operator told only that the two conflict is an operator
    // reaching for raw `git` to land the work. Which resolution it names follows
    // what was attempted — telling a branch whose stack parent has landed to merge
    // the base is telling it to reproduce the conflict it is refusing.
    Err(Error::SyncConflict {
        reason: match attempted {
            Reconciliation::Replay { from } => {
                format!(
                    "{compared} conflicts with {branch:?} in {conflicting} after {SYNC_ATTEMPTS} \
                 bounded attempts; the branch is retained. {compared} already carries what \
                 {branch:?} was stacked on, so only its own commits are replayed onto it. Resolve \
                 the conflict on it — replay it with `{}` — and then land it with `{land}`",
                    guidance::command([
                        "git",
                        "rebase",
                        "--onto",
                        compared,
                        &from,
                        &context.branch
                    ]),
                    branch = context.branch,
                )
            }
            Reconciliation::Merge => format!(
                "{compared} conflicts with {branch:?} in {conflicting} after {SYNC_ATTEMPTS} \
                 bounded attempts; the branch is retained. Resolve the conflict on it — merge \
                 {compared} into {branch} — and then land it with `{land}`",
                branch = context.branch,
            ),
        },
    })
}

/// Land the branch as one squashed commit on the base, built detached.
fn publish_locally(
    context: &Context<'_>,
    stream: &mut Stream,
    compared: &str,
    environment: &[(String, String)],
) -> Result<PublishOutcome> {
    let publication = &context.resolution.publication;
    require_publication_checkout_ready(publication, context.target.base())?;
    let synced = git::tip(&context.repo, compared);

    let identity = lock::git_identity(&git::common_dir(publication)?);
    let turn = queue::turn(&identity, context.cancellation)?;
    stream.emit(
        EventKind::LockWait,
        object(json!({
            "identity": identity,
            "elapsed": turn.waited.as_secs_f64(),
            "queue_position": turn.position,
        })),
    );
    stream.emit(
        EventKind::LockAcquired,
        object(json!({"identity": identity})),
    );
    stream.emit(
        EventKind::MergeQueued,
        object(json!({"identity": identity, "queue_position": turn.position})),
    );

    let outcome = (|| -> Result<PublishOutcome> {
        // The base can have advanced while this turn was queued — the writer ahead
        // of it in the queue is the usual reason. The tree that lands is therefore
        // re-synced here rather than silently reconciled: what was brought level with
        // the base is no longer what would be published, and the merge path is about
        // to rule on whatever this pushes.
        if git::has_remote(&context.repo, "origin") {
            git::fetch(&context.repo, "origin")?;
        }
        if git::tip(&context.repo, compared) != synced {
            sync(context, stream, compared, None)?;
        }
        let (subject, trailers) = describe(context, compared)?;
        let message = compose_squash(&subject, &closing_lines(context, compared)?, &trailers);

        let scratch_parent = context.run_root.join(format!("publish-{}", ids::unique()));
        home::ensure_dir(&scratch_parent)?;
        let scratch = scratch_parent.join("worktree");
        git::worktree_add_detached(&context.repo, &scratch, compared)?;
        let landed = (|| -> Result<PublishOutcome> {
            let Some(sha) = git::merge_squash(&scratch, &context.branch, &message)? else {
                return Ok(PublishOutcome::NothingToPublish);
            };
            let hooked = gate_run::runs_pre_push(&scratch);
            let started = Moment::now();
            let pushed = git::push(
                &scratch,
                &format!("HEAD:refs/heads/{}", context.target.base()),
                "origin",
                environment,
            )?;
            let ended = Moment::now();
            // The squash goes onto the base rather than onto the branch it landed,
            // so this push is the work being integrated whatever branch the payload
            // names.
            let kept = record_push(
                stream,
                &context.branch,
                &pushed,
                Some(&context.run_root),
                Phase::Integrate,
                None,
            )?;
            record_pre_push(stream, hooked, started, ended, &pushed);
            if !pushed.accepted() {
                // The tree this push was built in goes when this closure returns,
                // so the refusal must not send anybody to a path inside it.
                return Err(rejected(
                    &publishing(&context.branch),
                    &pushed,
                    &kept,
                    Some(&scratch_parent),
                ));
            }
            fast_forward_publication(publication, context.target.base())?;
            // The base received the squash when the push that put it there returned.
            let mut completed =
                object(json!({"identity": identity, "sha": sha, "base": context.target.base()}));
            gate_run::landed(&mut completed, Some(ended), &sha);
            stream.emit(EventKind::MergeCompleted, completed);
            record_baselines(context, &sha, stream);
            Ok(PublishOutcome::Merged(Sha(sha)))
        })();
        git::worktree_remove(&context.repo, &scratch)?;
        let _ = std::fs::remove_dir_all(&scratch_parent);
        landed
    })();

    drop(turn);
    outcome
}

/// How a publication names the push it was refused, so the one refusal builder is
/// handed the same phrase from both of its call sites here.
fn publishing(branch: &Ref) -> String {
    format!("the publishing push of {branch:?}")
}

/// A push the merge path refused: what git said it was, everywhere the merge path's
/// own account of it can be read, and the end of that account.
///
/// [`Error::PushRejected`] rather than [`Error::GateFailed`]: the same exit code, and a
/// kind a caller can route on.
///
/// **The pointers are the point.** What the hook or the remote wrote is a run of the
/// repository's whole verification and does not belong inline, so `record_push`
/// stored it a moment earlier — as an artifact, and where the caller had a run root,
/// as a file that outlives the tree the publication was built in. This used to name
/// neither, and a refusal that reports git's three generic lines while the diagnosis
/// sits in two places nothing points at is a refusal an operator has to go searching
/// behind: one landing here cost an hour that way, to find four redundant comment
/// lines. So the failure names both, and each only where it exists.
///
/// It quotes an excerpt rather than the whole, because the whole is what the
/// pointers above are for; [`excerpt`] is where the bound and the end it is taken
/// from are reasoned about.
///
/// **A refusal that captured nothing is a different sentence.** There is no excerpt
/// to quote and no per-ref summary to name, so the two clauses above collapse into
/// prose that describes nothing — `rejected by the merge path: .` is what one
/// seventeen-minute publishing push was reported as, and its caller had to run the
/// whole gate again to learn anything. What is said instead is that the merge path
/// wrote nothing, and what the run itself reported: the exit status or the signal
/// that terminated the push, and any read of its pipes that stopped short. A
/// refusal that *did* capture output is reported exactly as it always was.
///
/// This is also what an *unclassified* rejection is reported as, deliberately: it
/// names the push and hands over git's own per-ref summary without deciding what
/// produced it. The fallback below it is for a failure that never reached a ref at
/// all — no credential, no remote — where git's last line is the whole answer.
///
/// `what` is the push, named as its own caller names it: the branch a publication
/// was landing, or the base a merge train advanced.
///
/// `removed` is the tree the push was built in where its caller removes that tree
/// before returning, and is what [`outliving`] scrubs the message of: every path
/// this refusal names has to be openable by the person reading it.
pub(crate) fn rejected(
    what: &str,
    pushed: &git::Pushed,
    kept: &Kept,
    removed: Option<&Path>,
) -> Error {
    let summary = pushed
        .refusal()
        .unwrap_or_else(|| pushed.output().lines().next_back().unwrap_or("").trim());
    // Said rather than left to be inferred. A refusal that names nowhere reads as one
    // that simply did not bother to; "there is nothing to go and read" and "there is,
    // and this failure has lost the address of it" are the same sentence to an
    // operator and different next moves. `record_push` has already said on stderr why
    // each store refused the bytes.
    let where_it_is = if kept.anywhere() {
        kept.evidence()
    } else {
        " What it wrote could not be stored anywhere, so the excerpt below is all that \
         survives of it."
            .to_owned()
    };
    let wrote = pushed.output().trim();
    let missing = host_prerequisite(wrote);
    let said = if wrote.is_empty() {
        String::new()
    } else {
        format!(
            " It said:\n{}",
            guidance::quoted_output(&excerpt(
                wrote,
                "the whole of it is where this refusal says"
            ))
        )
    };
    // The one case the sentence above cannot describe: nothing was captured, so
    // `summary` is empty and `said` is empty, and what an operator is handed reads
    // `rejected by the merge path: .` — which is how a seventeen-minute push was
    // reported, and why the whole gate had to be run again to learn anything about
    // it. The run's own account of itself is what is left, so it is said outright.
    if wrote.is_empty() && missing.is_none() {
        return Error::PushRejected {
            reason: outliving(
                &format!(
                    "{what} was rejected by the merge path, and the merge path wrote nothing: \
                     {diagnosis}.{where_it_is}",
                    diagnosis = diagnosis(pushed),
                ),
                removed,
            ),
        };
    }
    match missing {
        Some(missing) => Error::HostPrerequisite {
            reason: outliving(
                &format!(
                    "{what} was refused by the merge path because this host is missing a \
                     prerequisite: {missing}.{where_it_is}{said}",
                    missing = missing.trim_end_matches('.'),
                ),
                removed,
            ),
        },
        None => Error::PushRejected {
            reason: outliving(
                &format!("{what} was rejected by the merge path: {summary}.{where_it_is}{said}"),
                removed,
            ),
        },
    }
}

/// What the push's own run says about itself: how it ended, and any read of its
/// pipes that stopped short.
///
/// Both, and neither inferred from the other — a push terminated by a signal and a
/// push whose output this process could not read are different failures with
/// different next moves, and a push can be both. A refusal reaching here with no
/// diagnostics at all is one no `git push` produced, and says so rather than
/// claiming an ending nobody reported.
fn diagnosis(pushed: &git::Pushed) -> String {
    let Some(diagnostics) = pushed.diagnostics() else {
        return "nothing was recorded about how the push itself ended".to_owned();
    };
    let mut said = format!("the push {}", diagnostics.ended);
    if !diagnostics.read_failures.is_empty() {
        said.push_str(&format!(
            ", and its output could not be read whole ({})",
            diagnostics.read_failures.join("; "),
        ));
    }
    said
}

/// How a merge-path hook begins the one line saying a refusal is this host's rather
/// than the work's: a tool or a credential it needs is missing, and what follows the
/// marker says which and how to install it.
///
/// The contract's spelling, stated once. A hook prints it only for a failure that does
/// not depend on the tree being pushed, because the reader on the other side settles
/// such a refusal without sending anybody to change the work.
pub const HOST_PREREQUISITE_MARKER: &str = "onevcs: host-prerequisite:";

/// What a merge path's host-prerequisite line says is missing, where its output
/// carries one.
///
/// Read line by line, and the line has to *begin* with the marker — after the
/// `remote:` git puts before what a server-side hook printed — so a hook quoting the
/// marker in prose, or a test log mentioning it, is not read as one. A marker naming
/// nothing is not the line the contract describes, and the refusal is classified as
/// though it were absent.
fn host_prerequisite(output: &str) -> Option<&str> {
    output.lines().find_map(|line| {
        let line = line.trim();
        let line = line.strip_prefix("remote:").map_or(line, str::trim_start);
        line.strip_prefix(HOST_PREREQUISITE_MARKER)
            .map(str::trim)
            .filter(|missing| !missing.is_empty())
    })
}

/// The same refusal with every path into a tree that will not outlive this command
/// replaced by the fact that it did not.
///
/// A merge path keeps its own account where it runs — `.logs/nx.log` under the tree
/// the publishing push was built from is one real spelling — and says so in the
/// output quoted above. That tree is a scratch worktree removed before the command
/// returns, so the refusal named a path an operator opened and found nothing at,
/// while the same output sat under the publication's own retained gate logs, which
/// the sentence beside it already names. A path that is not there is worse than no
/// path: it is the one thing in a failure a reader trusts enough to go and open.
///
/// What is replaced is the whole path and not its prefix, because half of one still
/// reads as somewhere to look. Nothing else is touched — the preserved log and the
/// checkouts this names are under neither this tree nor any other that is going.
fn outliving(reason: &str, removed: Option<&Path>) -> String {
    let Some(removed) = removed else {
        return reason.to_owned();
    };
    let doomed = removed.display().to_string();
    if doomed.is_empty() {
        return reason.to_owned();
    }
    let mut left = reason;
    let mut answer = String::with_capacity(reason.len());
    while let Some(at) = left.find(&doomed) {
        answer.push_str(&left[..at]);
        let rest = &left[at..];
        // To the end of the path and no further: a path ends where the prose around
        // it resumes, and the sentence-final stop of the line it sits in is prose.
        let end = rest
            .find(|c: char| c.is_whitespace() || ENDS_A_PATH.contains(&c))
            .unwrap_or(rest.len());
        let end = rest[..end].trim_end_matches('.').len().max(doomed.len());
        answer.push_str(GONE);
        left = &rest[end..];
    }
    answer.push_str(left);
    answer
}

/// What a path is quoted or punctuated with once it is over, in the prose a merge
/// path writes around one.
const ENDS_A_PATH: [char; 6] = [')', '"', '\'', '`', ',', ';'];

/// What stands where such a path stood: not a path, and the reason it is not.
const GONE: &str = "(gone with the publication worktree this ran in)";

/// Why git turned a leased push down, when the lease is what it turned down.
///
/// The two are told apart because the operator's next move differs: one branch has
/// somebody else's work on it to reconcile with, and the other has nothing on the
/// host at all.
enum Declined {
    /// The branch is on the host at a commit this run never saw.
    Moved(git::ObjectId),
    /// The host has no branch of that name any more.
    Gone,
}

/// Whether git turned this push down *because the lease was stale*, decided from
/// what git and the remote report and never from the prose beside it.
///
/// Two structural facts have to agree, because neither alone is enough. git has to
/// have declined this very ref: its `--porcelain` line for the branch carries the
/// `!` flag, which rules out a push that failed before any ref was negotiated. And
/// the lease has to be genuinely stale: `--force-with-lease=<branch>:<seen>` is
/// declined exactly when the remote's value for the branch is not `<seen>`, so the
/// remote's own answer to "where is this branch" settles whether that is what
/// happened. A remote that will not say answers `None`, leaving the rejection
/// unclassified — assuming a stale lease from an answer nobody gave is the same
/// mistake as reading it out of a message a locale could translate.
fn declined_the_lease(
    context: &Context<'_>,
    pushed: &git::Pushed,
    replaced: &str,
    environment: &[(String, String)],
) -> Result<Option<Declined>> {
    if !pushed.refused_branch(&context.branch) {
        return Ok(None);
    }
    Ok(
        match git::remote_tip(&context.worktree, "origin", &context.branch, environment)? {
            git::RemoteTip::At(tip) if tip.as_str() == replaced => None,
            git::RemoteTip::At(tip) => Some(Declined::Moved(tip)),
            // The host no longer has the branch at all, which is not where this run
            // last saw it either — the lease named a commit, and git declines it
            // against a ref that is gone just as it does against one that moved.
            git::RemoteTip::Absent => Some(Declined::Gone),
            git::RemoteTip::Unknown => None,
        },
    )
}

/// Push the branch, open or adopt its change request, and ask the host to land it.
fn publish_as_change(
    context: &Context<'_>,
    stream: &mut Stream,
    subject: &str,
    environment: &[(String, String)],
    push: Push,
) -> Result<PublishOutcome> {
    // Input, rejected at its boundary rather than half way through the watch below,
    // where the branch would already be on the remote and the refusal would read as a
    // merge path nobody could verify.
    refuse_an_unhosted_identity(&context.resolution.key)?;
    if let Some(reason) = &context.draft {
        if let Some(refusal) = refuse_a_draft_over_a_reviewed_change(context, reason) {
            return Err(refusal);
        }
    }
    // Every change policy watches its checks now, `change-open` included, so every one
    // is held to the knobs that bound the watch — before anything is pushed.
    context.checks_wait.effective()?;
    gh::checks_poll()?;
    crate::host::check_source_names_a_source()?;
    if !context.drafts.disabled {
        gh::draft_grace()?;
    }

    // A branch this publication replayed is not a descendant of the one the host has
    // for it — the change below's commits are gone from it — so the push replaces one
    // commit and no other, and git refuses it if the host is anywhere else. Every
    // other publication pushes as it always has: forward or not at all.
    let replacing = match &push {
        Push::Replacing { replaced } => Some(replaced.as_str()),
        Push::Forward => None,
    };
    if context.cancellation.is_cancelled() {
        return Err(cancelled_while(&format!(
            "it prepared {:?} and before anything was pushed",
            context.branch
        )));
    }
    let hooked = gate_run::runs_pre_push(&context.worktree);
    let started = Moment::now();
    let pushed = git::push_replacing(
        &context.worktree,
        &context.branch,
        "origin",
        replacing,
        environment,
    )?;
    let ended = Moment::now();
    // The session's own branch, on its way to being proposed: the work being made.
    let kept = record_push(
        stream,
        &context.branch,
        &pushed,
        Some(&context.run_root),
        Phase::Development,
        git::tip(&context.worktree, &format!("refs/heads/{}", context.branch)).as_deref(),
    )?;
    record_pre_push(stream, hooked, started, ended, &pushed);
    if !pushed.accepted() {
        // Which refusal this is depends on what git declined, and that is decided
        // from what git and the host *report* rather than from the sentence either
        // wrote. A declined lease means somebody else's commit is on this branch
        // while the work here is a replay that would have written over it. Every
        // other rejection of the same push is what it has always been — a
        // credential, a hook, or the host's own policy — and so is every rejection
        // this cannot tell apart: reading one of those as a branch that moved would
        // send an operator to reconcile histories that never diverged.
        let declined = match replacing {
            Some(replaced) => declined_the_lease(context, &pushed, replaced, environment)?,
            None => None,
        };
        let (Some(replaced), Some(declined)) = (replacing, declined) else {
            // The session's own worktree, which outlives this command: what it
            // wrote about itself stays readable.
            return Err(rejected(&publishing(&context.branch), &pushed, &kept, None));
        };
        let branch = &context.branch;
        let base = context.target.base();
        let command = guidance::command([
            "onevcs",
            "publish-branch",
            branch,
            "--repo",
            &context.resolution.publication.to_string_lossy(),
        ]);
        return Err(Error::SyncConflict {
            // What the host has instead is what the operator has to do something
            // about, so the refusal says which of the two it is rather than one
            // sentence that is only true of the commoner one.
            reason: match declined {
                Declined::Moved(tip) => format!(
                    "{branch:?} moved on the host since this run last had it at {replaced} — it \
                     is at {tip} now — and this publication replayed it onto {base:?}, so pushing \
                     would replace whatever was pushed there in between. Nothing was overwritten \
                     and the branch is retained. Reconcile the two — fetch {branch}, and replay \
                     or merge what the host has into it — and then land it with `{command}`",
                    tip = tip.as_str(),
                ),
                Declined::Gone => format!(
                    "{branch:?} is gone from the host, which this run last had at {replaced}, and \
                     this publication replayed it onto {base:?} — so the push would put a branch \
                     back that somebody deleted, out of a history nobody there has seen. Nothing \
                     was pushed and the branch is retained. Fetch {branch} so this run sees the \
                     host as it stands — every fetch here prunes — and then land it with \
                     `{command}`"
                ),
            },
        });
    }

    // Everything past here reads the *host*, and the push above has already reached
    // the remote: from this point a failure is a merge path this build could not
    // read rather than a publication that never landed. The commit is taken from the
    // tree that was pushed rather than asked of the remote, because asking the remote
    // is one more read that can fail for the very reason being reported.
    let pushed_at = git::tip(&context.worktree, &context.branch);
    // The push — and the `pre-push` hook it ran — is never interrupted, so a caller
    // that cancelled while it ran is answered here, once it has returned and before
    // anything on the host is opened or watched.
    if context.cancellation.is_cancelled() {
        return Err(cancelled_while(&format!(
            "its publishing push ran; {:?} is on origin{}",
            context.branch,
            pushed_at
                .as_deref()
                .map_or_else(String::new, |sha| format!(" at {sha}")),
        )));
    }
    // The commit is what the watch below decides *which* of the host's checks are
    // about this publication, so a tree that will not say what it just pushed is a
    // merge path this build cannot read rather than one it reads about some other
    // commit. It reaches the operator as exactly that, through `unverified`.
    if let Some(adopt) = &context.adopt {
        return Ok(land_deferred(context, stream, adopt));
    }
    match pushed_at.as_deref() {
        Some(sha) => land_as_change(context, stream, subject, &Sha(sha.to_owned())),
        None => Err(crate::error::invalid(format!(
            "{:?} was pushed and this worktree will not say which commit it is at, so which of \
             the host's checks are about what was just pushed cannot be decided",
            context.branch
        ))),
    }
    .map_err(|unread| unverified(context, pushed_at.as_deref(), unread))
}

/// End a deferred publication once its push is accepted: the change request it adopts
/// is recorded as this publication's, and nothing at all is asked of the host.
///
/// The `change-opened` it records is the one a watched publication records, less the
/// author — which only the host could say — so `status`, `landing_status` and a later
/// deferred publication read this one's change request exactly as they read a watched
/// one's.
fn land_deferred(context: &Context<'_>, stream: &mut Stream, adopt: &Url) -> PublishOutcome {
    let number = adopt
        .path_segments()
        .and_then(|mut segments| segments.nth(3))
        .unwrap_or_default()
        .to_owned();
    stream.emit(
        EventKind::ChangeOpened,
        object(json!({
            "url": adopt.to_string(),
            "host": "github",
            "id": number,
            "base": context.target.base().to_string(),
        })),
    );
    crate::standings::record_head(adopt, &context.branch);
    PublishOutcome::ChangeReviewDraftUnchecked(adopt.clone())
}

/// Refuse an identity that has no host at all, before anything is pushed.
///
/// Half of [`change_host`]'s question, and only that half: a **hosted** identity this
/// build has no implementation for is left until after the push, because there the
/// branch reaching the origin is not what is missing, and `edges.rs` holds that.
fn refuse_an_unhosted_identity(identity: &str) -> Result<()> {
    match change_host(identity) {
        Ok(_) | Err(Error::NotImplemented { .. }) => Ok(()),
        Err(no_host) => Err(no_host),
    }
}

/// A push that reached the remote and a merge path that could not then be read,
/// reported as the one thing it is rather than as a publication that failed.
///
/// This covers the answers nobody got. What passes through is every failure the
/// contract already fixes a kind for — [`Error::GateFailed`] included, which it fixes
/// for a host that took a merge and then reported it unperformed: that shares this
/// defect's shape, and re-pointing a meaning is an amendment somebody approves. A
/// conflict the host confirmed and a caller's cancellation pass through too: both are
/// answers, and neither is a merge path that could not be read.
fn unverified(context: &Context<'_>, pushed_at: Option<&str>, unread: Error) -> Error {
    match unread {
        verdict @ (Error::ChecksFailed { .. }
        | Error::ChecksUnsettled { .. }
        | Error::GateFailed { .. }
        | Error::NotImplemented { .. }
        | Error::SyncConflict { .. }
        | Error::Cancelled { .. }) => verdict,
        unread => Error::PushedUnverified {
            reason: format!(
                "{branch:?} is on origin{at} and the merge path could not be read: {unread}. The \
                 work reached the remote, so re-publishing it would repeat what already landed \
                 there — read the change request on the host, and land the branch with \
                 `{command}` once the host answers",
                branch = context.branch,
                at = pushed_at.map_or_else(String::new, |sha| format!(" at {sha}")),
                command = guidance::command([
                    "onevcs",
                    "publish-branch",
                    &context.branch,
                    "--repo",
                    &context.resolution.publication.to_string_lossy(),
                ]),
            ),
        },
    }
}

/// Open or adopt the change request for a branch **already on the remote**, and ask
/// the host to land it.
///
/// Separate from the push above so that what every failure in here has in common is a
/// property of the function rather than a comment somebody has to keep true.
///
/// **The draft lifecycle** is here. A publication carrying no [`DraftReason`] opens its
/// change request as a draft while the pushed commit's required checks run, records
/// `change-drafted` with `kind: "awaiting-checks"`, and once they settle lifts or keeps
/// it by the policy's row of [`policy::GreenDraft`]: every change policy watches, and
/// the lift comes **before** the merge is armed or asked for, because a host will do
/// neither to a draft. Adopting a draft is the same — it is not lifted on the spot but
/// watched into a lift or a keep. Adopting a change the host holds ready never
/// re-drafts it: the lift is one-way. `drafts: {disabled: true}` opens ready and lifts
/// an adopted draft at once, as before the lifecycle.
fn land_as_change(
    context: &Context<'_>,
    stream: &mut Stream,
    subject: &str,
    pushed: &Sha,
) -> Result<PublishOutcome> {
    let slug = change_host(&context.resolution.key)?;
    let host = context.hosting.for_repo(&slug)?;
    // Who the host believes is calling travels with the change: a change request
    // opened by an identity nobody expected is the thing an operator reads this to
    // find out.
    let author = host.authenticated_user()?;
    let lifecycle = !context.drafts.disabled;

    let existing = host.find_changes(&context.branch, context.target.base())?;
    // Whether the host already held this change request: with the lifecycle off,
    // that decides whether there can be a draft to lift, since a change this
    // publication just opened without a reason is one nobody drafted.
    let adopted = !existing.is_empty();
    let change = match existing.into_iter().next() {
        Some(change) => change,
        None => host.open_change(ChangeSpec {
            head: context.branch.to_string(),
            base: context.target.base().to_string(),
            title: subject.to_owned(),
            // Exactly what the caller passed, or nothing. The caller is the layer
            // that knows what the change is for; this one only knows the branch,
            // and everything it could say from that the title already says.
            body: context.body.clone(),
            // The reason travels to the host so that the host can open it as a
            // draft, and no further: what the host renders of a draft is the state,
            // never the reason. The reason is recorded below, on the stream.
            draft: context.draft.clone(),
            // No reason, and the lifecycle on: a draft while the checks run.
            draft_awaiting_checks: lifecycle && context.draft.is_none(),
        })?,
    };
    record_boundary(context, &change, pushed);
    land_opened_change(
        context,
        stream,
        host.as_ref(),
        &author,
        &change,
        adopted,
        pushed,
    )
}

/// Resume a publication whose verification already passed, at its hosted checks.
///
/// What `publish-branch` hands a change request it found exactly at the boundary it
/// recorded (`verified.rs`): nothing is fetched, synced, or pushed, and the change is
/// taken from there by the same code a first publication runs once it has pushed and
/// opened one — the same outcomes, the same events, and a red check ending it as one
/// does there. It is an adopted change, because it is one: the host already held it.
pub(crate) fn resume(
    context: &Context<'_>,
    change: &ChangeRequest,
    pushed: &Sha,
    stream: &mut Stream,
) -> Result<PublishOutcome> {
    context.checks_wait.effective()?;
    gh::checks_poll()?;
    crate::host::check_source_names_a_source()?;
    if !context.drafts.disabled {
        gh::draft_grace()?;
    }
    // A resumed publication is an attempt of its own, whose one gate is the watch.
    stream.begin_attempt();
    // Held again, under this resumption's own scope: what it may land under — a title,
    // a body — is new, and the commits are screened once more for the same reason.
    let compared = format!("origin/{}", context.target.base());
    hold_to_the_boundary(context, &compared, context.title.as_deref())?;
    (|| -> Result<PublishOutcome> {
        let slug = change_host(&context.resolution.key)?;
        let host = context.hosting.for_repo(&slug)?;
        let author = host.authenticated_user()?;
        land_opened_change(
            context,
            stream,
            host.as_ref(),
            &author,
            change,
            true,
            pushed,
        )
    })()
    .map_err(|unread| unverified(context, Some(&pushed.0), unread))
}

/// Record the boundary a publication's verification passed at, once its change
/// request is open: the push that verified the branch has been accepted by the time
/// anything here runs. Best effort, as every record of something that already
/// happened is.
fn record_boundary(context: &Context<'_>, change: &ChangeRequest, pushed: &Sha) {
    let base = context.target.base();
    let Some(base_commit) = git::tip(&context.repo, &vcs::base_ref(&context.repo, base)) else {
        return;
    };
    let inputs = git::configured_hooks(&context.worktree)
        .and_then(|hooks| crate::verified::inputs(hooks.as_deref(), base, &context.provenance));
    let inputs = match inputs {
        Ok(inputs) => inputs,
        Err(failure) => {
            eprintln!(
                "onevcs: warning: what verified {branch:?} could not be read back, so publishing \
                 it again verifies it again: {failure}",
                branch = context.branch,
            );
            return;
        }
    };
    crate::verified::Boundary {
        identity: context.resolution.key.clone(),
        branch: context.branch.to_string(),
        tip: pushed.clone(),
        base: base.to_string(),
        base_commit: Sha(base_commit),
        inputs,
        change: change.id.clone(),
        change_url: change.url.clone(),
    }
    .record();
}

/// Everything a change publication does once its change request is open on a pushed,
/// verified branch: watch its checks, lift or keep its draft, and land it under the
/// policy — one path for a first publication and for a resumed one.
fn land_opened_change(
    context: &Context<'_>,
    stream: &mut Stream,
    host: &dyn RemoteHost,
    author: &str,
    change: &ChangeRequest,
    adopted: bool,
    pushed: &Sha,
) -> Result<PublishOutcome> {
    let lifecycle = !context.drafts.disabled;
    stream.emit(
        EventKind::ChangeOpened,
        object(json!({
            "url": change.url.to_string(),
            "host": "github",
            "id": change.id.0,
            "base": change.base,
            "author": author,
        })),
    );

    if let Some(reason) = &context.draft {
        return hold_as_draft(context, host, change, reason, stream);
    }
    let drafted = if lifecycle {
        await_as_draft(host, change, stream)?
    } else {
        if adopted {
            lift_any_draft(host, change, stream)?;
        }
        false
    };

    let mut watcher = Watcher::new(
        host,
        change,
        pushed,
        context.drafts.warn_on_early_lift,
        context.cancellation,
        context.checks_wait.effective()?.value.get(),
    )?;
    let green = policy::GreenDraft::of(context.effective, context.policy.approvals);
    if context.effective == MergePolicy::ChangeOpen {
        // No merge is asked for, so the identity's merge queue is not taken: what
        // this waits for is the verdict a person reviews against, not a turn.
        if settle(&mut watcher, stream, drafted)? {
            if green == policy::GreenDraft::KeepForReview {
                stream.emit(
                    EventKind::DraftKeptForReview,
                    object(json!({
                        "url": change.url.to_string(),
                        "id": change.id.0,
                        "base": change.base,
                    })),
                );
                return Ok(PublishOutcome::ChangeReviewDraft(change.url.clone()));
            }
            lift_draft(host, change, stream)?;
        }
        return Ok(PublishOutcome::ChangeOpen(change.url.clone()));
    }

    // Everything automated is serialized against the identity, so two sessions of
    // one repository cannot ask the host to land two changes at once.
    let identity = lock::git_identity(&git::common_dir(&context.resolution.publication)?);
    let turn = queue::turn(&identity, context.cancellation)?;
    stream.emit(
        EventKind::LockWait,
        object(json!({
            "identity": identity,
            "elapsed": turn.waited.as_secs_f64(),
            "queue_position": turn.position,
        })),
    );
    stream.emit(
        EventKind::LockAcquired,
        object(json!({"identity": identity})),
    );

    let outcome = (|| -> Result<PublishOutcome> {
        let watched = (|| -> Result<WatchEnded> {
            // What is watched, and until when, follows the **merge policy** and nothing
            // else — least of all anything a repository *calls* its verification. A
            // change-direct publication asks for the merge itself, so it waits for the
            // checks the host says block one first; a draft waits for its checks under
            // either policy, because it is lifted on their verdict and a host will
            // neither merge a draft nor arm its own merge on one.
            if (drafted || context.effective == MergePolicy::ChangeDirect)
                && settle(&mut watcher, stream, drafted)?
            {
                lift_draft(host, change, stream)?;
            }
            stream.emit(
                EventKind::MergeQueued,
                object(json!({
                    "identity": identity,
                    "queue_position": turn.position,
                    "url": change.url.to_string(),
                })),
            );
            Ok(if context.effective == MergePolicy::ChangeAuto {
                // Arming happens inside the watch, and after its first reading of the
                // checks: a host that will not say what its checks are is refused with
                // nothing armed against it, and one loop rather than a read followed by
                // a watch reports each transition exactly once.
                WatchEnded::Landed(watch_the_merge(&mut watcher, stream)?)
            } else {
                match merge_or_find_merged(host, change, context.effective)? {
                    MergeOutcome::Merged(sha) => WatchEnded::Landed(sha),
                    MergeOutcome::Queued => {
                        WatchEnded::Ended(PublishOutcome::Queued(change.url.clone()))
                    }
                    MergeOutcome::Open => {
                        WatchEnded::Ended(PublishOutcome::ChangeOpen(change.url.clone()))
                    }
                }
            })
        })();
        let sha = match watched {
            Ok(WatchEnded::Landed(sha)) => sha,
            Ok(WatchEnded::Ended(outcome)) => return Ok(outcome),
            // The wait elapsed with no required check red: the change is left armed to
            // merge itself where the host allows it, rather than stranded as a draft
            // nothing will lift.
            Err(Error::ChecksUnsettled { reason }) if watcher.elapsed => {
                let elapsed = Elapsed {
                    reason,
                    already_armed: watcher.armed,
                    skipped: &watcher.skipped_at_elapse,
                };
                match arm_on_elapse(host, change, pushed, stream, elapsed)? {
                    WatchEnded::Landed(sha) => sha,
                    WatchEnded::Ended(outcome) => return Ok(outcome),
                }
            }
            Err(other) => return Err(other),
        };
        // When the base received it: the host's own merge time, and nothing where the
        // host cannot say. Never the moment this watch saw the merge, which is only
        // when somebody happened to ask, and never a time read from anywhere else.
        let landed_at = merge_time(host, change);
        let mut merged = object(json!({"url": change.url.to_string(), "sha": sha.0}));
        gate_run::landed(&mut merged, landed_at, &sha.0);
        stream.emit(EventKind::ChangeMerged, merged);
        let mut completed = object(json!({"identity": identity, "sha": sha.0}));
        gate_run::landed(&mut completed, landed_at, &sha.0);
        stream.emit(EventKind::MergeCompleted, completed);
        crate::verified::forget(&context.resolution.key, &context.branch);
        record_landing(context, &sha);
        record_baselines(context, &sha.0, stream);
        fast_forward_publication(&context.resolution.publication, context.target.base())?;
        Ok(PublishOutcome::Merged(sha))
    })();
    drop(turn);
    outcome
}

/// Ask the host to merge the change, and where it refuses, whether it already has.
///
/// A change an earlier publication left armed is merged by the host on its own clock,
/// and that can happen between this publication's checks settling and its asking for
/// the merge — which the host then refuses, since there is nothing left to merge. So a
/// refusal is answered by the host's own record of the merge where it has one, and is
/// the refusal it was otherwise.
fn merge_or_find_merged(
    host: &dyn RemoteHost,
    change: &ChangeRequest,
    policy: MergePolicy,
) -> Result<MergeOutcome> {
    match host.merge(change, policy) {
        Ok(outcome) => Ok(outcome),
        Err(refused) => match host.merged_at(change) {
            Ok(Some(sha)) => Ok(MergeOutcome::Merged(sha)),
            _ => Err(refused),
        },
    }
}

/// What a watch whose bound elapsed leaves for [`arm_on_elapse`] to decide on.
struct Elapsed<'w> {
    /// The `checks-unsettled` reason the bound's ending carries.
    reason: String,
    /// Whether the watch already armed the host's merge — `change-auto`'s merge watch.
    already_armed: bool,
    /// The required checks that had concluded skipped when the bound elapsed.
    skipped: &'w [String],
}

/// Where a `change-auto` or `change-direct` watch ended: the host merged the change at
/// a commit, or the publication ends here without a landing.
enum WatchEnded {
    /// The base received the change, at this commit.
    Landed(Sha),
    /// The publication ends with this outcome, and nothing landed.
    Ended(PublishOutcome),
}

/// What a `change-auto` or `change-direct` publication does when its wait for the
/// checks elapses with none of the required ones red: leave the change armed to merge
/// itself once they pass, where the host allows that.
///
/// Whether the repository allows auto-merge is asked **before anything is changed**,
/// so a host that does not allow it — or will not say — is left exactly as the
/// elapsed wait left it, a draft still a draft, and the refusal names why. One that
/// does has the draft awaiting its checks lifted (only that draft: a held or
/// release-awaiting one never reaches a watch) and then its merge armed, in that
/// order, since a host arms nothing on a draft. A merge `watch_the_merge` already
/// armed is not asked for again.
///
/// The publication ends as the `checks-unsettled` it always did, carrying the
/// [`ArmedMerge`] and saying the merge is armed, and `merge-armed` is recorded. A host
/// that merges at the moment it is armed — its checks passed in between — lands it
/// here, and the publication ends as any landing does.
///
/// A change any of whose required checks had concluded **skipped** when the wait
/// elapsed is never lifted or armed: the host's auto-merge reads a skipped required
/// check as satisfied, and a repository whose workflow does not run again on the lift
/// would then merge with that check never run. It is left as it was, and the refusal
/// names each skipped check.
fn arm_on_elapse(
    host: &dyn RemoteHost,
    change: &ChangeRequest,
    pushed: &Sha,
    stream: &mut Stream,
    elapsed: Elapsed<'_>,
) -> Result<WatchEnded> {
    let Elapsed {
        reason,
        already_armed,
        skipped,
    } = elapsed;
    if !skipped.is_empty() {
        return Err(Error::ChecksUnsettled {
            reason: format!(
                "{reason}. Required check {skipped} concluded skipped, which the host's \
                 auto-merge would read as passed, so {url} is {left} — this publication lifted \
                 nothing and armed no merge for it",
                skipped = guidance::listed(skipped),
                url = change.url,
                left = if already_armed {
                    "left with the merge its watch had already armed"
                } else {
                    "left as it was"
                },
            ),
        });
    }
    if !already_armed {
        match host.allows_auto_merge() {
            Ok(true) => {}
            Ok(false) => {
                return Err(Error::ChecksUnsettled {
                    reason: format!(
                        "{reason}. The repository does not allow auto-merge, so {url} is left as \
                         it was — nothing lifted it or armed its merge; allow auto-merge on the \
                         repository, or publish the session again once its checks have passed",
                        url = change.url,
                    ),
                })
            }
            Err(unread) => {
                return Err(Error::ChecksUnsettled {
                    reason: format!(
                        "{reason}. Whether the repository allows auto-merge could not be read \
                         ({unread}), so {url} is left as it was — nothing lifted it or armed its \
                         merge; publish the session again once its checks have passed",
                        url = change.url,
                    ),
                })
            }
        }
        lift_any_draft(host, change, stream)?;
        if let MergeOutcome::Merged(sha) = host.merge(change, MergePolicy::ChangeAuto)? {
            return Ok(WatchEnded::Landed(sha));
        }
    }
    stream.emit(
        EventKind::MergeArmed,
        object(json!({"url": change.url.to_string(), "head": pushed.0})),
    );
    Ok(WatchEnded::Ended(PublishOutcome::Failed {
        kind: FailureKind::ChecksUnsettled,
        reason: format!(
            "{}. Its merge is armed: the host lands {url} once its required checks pass, and \
             publishing the session again watches it",
            Error::ChecksUnsettled { reason },
            url = change.url,
        ),
        retained: None,
        armed: Some(ArmedMerge {
            change: change.url.clone(),
            head: pushed.0.clone(),
        }),
    }))
}

/// Whether the change a reasonless publication holds is a draft awaiting its checks,
/// recording it as one where it is.
///
/// Asked of the host rather than assumed from having asked for one, both for a change
/// this publication just opened — a host written before `draft_awaiting_checks`
/// opens it ready, and a ready change is watched as one — and for one it adopted,
/// whatever drafted it: a held draft, a release-awaiting one, or one an earlier red run
/// left awaiting its checks is watched into a lift or a keep rather than lifted on the
/// spot. A change the host holds **ready** is never asked to be a draft again, and
/// records nothing: lifting is one-way.
///
/// A host that was never taught to say whether it drafted anything has drafted
/// nothing this can lift, so it answers "not a draft" exactly as [`lift_any_draft`]
/// reads it; a host that could not say is a refusal.
fn await_as_draft(
    host: &dyn RemoteHost,
    change: &ChangeRequest,
    stream: &mut Stream,
) -> Result<bool> {
    match host.is_draft(change) {
        Ok(true) => {}
        Ok(false) | Err(Error::NotImplemented { .. }) => return Ok(false),
        Err(unreadable) => return Err(unreadable),
    }
    stream.emit(
        EventKind::ChangeDrafted,
        object(json!({
            "url": change.url.to_string(),
            "id": change.id.0,
            "base": change.base,
            "kind": AWAITING_CHECKS,
        })),
    );
    Ok(true)
}

/// The `kind` a `change-drafted` payload carries for a draft the lifecycle opened
/// while the checks run — the one kind that is no [`DraftReason`], because nobody
/// asked for it.
const AWAITING_CHECKS: &str = "awaiting-checks";

/// Hold a change request open as a draft, and record why.
///
/// The publication stops here under **every** change policy: a draft is unmergeable
/// in that state, so nothing below this asks the host to merge it, arms the host's
/// own merge on it, takes the identity's merge queue, or fast-forwards a base from
/// it. Which policy the identity publishes under decides what happens once the draft
/// is lifted, not what happens while it stands.
fn hold_as_draft(
    context: &Context<'_>,
    host: &dyn RemoteHost,
    change: &ChangeRequest,
    reason: &DraftReason,
    stream: &mut Stream,
) -> Result<PublishOutcome> {
    // Asked of the host rather than assumed from having said `--draft`, and the two
    // cases it separates are both real: a host whose `open_change` was written before
    // this field silently ignores it, and a change request already open for review is
    // one this crate will not put back into a draft — the seam has no method for it,
    // and inventing one would be a public item nobody approved. Either way the answer
    // is the same refusal, because either way the change on the host can land while
    // its caller believes it cannot.
    if !host.is_draft(change)? {
        return Err(already_open_for_review(context, change, reason));
    }
    // The publication record, and the only place the reason is written: see
    // [`DraftReason`].
    let mut payload = reason.fields();
    let fields = payload.as_object_mut().expect("the reason is an object");
    fields.insert("url".to_owned(), json!(change.url.to_string()));
    fields.insert("id".to_owned(), json!(change.id.0));
    fields.insert("base".to_owned(), json!(change.base));
    stream.emit(EventKind::ChangeDrafted, object(payload));
    Ok(PublishOutcome::ChangeDraft(change.url.clone()))
}

/// The refusal for a draft asked over a change request the host has open for review.
///
/// One sentence, two places: `hold_as_draft` reaches it once the change is adopted,
/// and the pre-push check below reaches it before anything is on the remote. A
/// refusal that read differently depending on which noticed first would be two rules
/// about one state.
fn already_open_for_review(
    context: &Context<'_>,
    change: &ChangeRequest,
    reason: &DraftReason,
) -> Error {
    crate::error::invalid(format!(
        "{url} is open for review on the host, and this publication asked for {asked}. A change \
         that is open can land, so reporting it as a draft would say the work is held back when \
         nothing is holding it. Lift nothing and publish {branch:?} without a draft, or close \
         that change request and publish again",
        url = change.url,
        asked = reason.asked_for(),
        branch = context.branch,
    ))
}

/// Refuse a draft over a change request the host already has open for review, before
/// anything reaches the remote.
///
/// The same question [`hold_as_draft`] asks after the change is adopted, asked at the
/// boundary as well — because everything past the publishing push is reported as a
/// merge path this build could not read, and this is not one of those. The merge path
/// ruled on the push perfectly well; what stopped the publication is a state the host
/// *answered*, and an operator told their push is unverified would go looking for a
/// failure that never happened.
///
/// **Only a definite answer refuses.** A host this build has no implementation for,
/// one that cannot be reached, and one that will not say whether a change is a draft
/// are each left exactly where they are met today — after the push, by the paths that
/// already handle them — so this can only ever move a refusal *earlier*, never invent
/// one. `hold_as_draft` stays the authoritative check for the same reason it existed
/// before this: a host may take `--draft` and open an ordinary change anyway.
fn refuse_a_draft_over_a_reviewed_change(
    context: &Context<'_>,
    reason: &DraftReason,
) -> Option<Error> {
    let host = change_host(&context.resolution.key)
        .ok()
        .and_then(|slug| context.hosting.for_repo(&slug).ok())?;
    let open = host
        .find_changes(&context.branch, context.target.base())
        .ok()?
        .into_iter()
        .next()?;
    match host.is_draft(&open) {
        Ok(false) => Some(already_open_for_review(context, &open, reason)),
        _ => None,
    }
}

/// Lift any draft on a change request, at once.
///
/// What `onevcs change ready` asks for, and what a publication carrying no
/// [`DraftReason`] does with a draft it adopts when the rules file says `drafts:
/// {disabled: true}` — the behaviour before the draft lifecycle. With the lifecycle
/// on, an adopted draft is watched into a lift or a keep instead (`await_as_draft`).
///
/// **Idempotent, because the host decides.** A change request that is not a draft is
/// asked for nothing, so a second publication after a lift makes no call and reports
/// exactly what the first one did.
///
/// Asked only of a change request the host already held. One this publication opened
/// moments ago, without a reason, is one nobody drafted — so a publication that
/// opens its own change request asks the host nothing extra, and every implementation
/// written against the earlier surface goes on publishing exactly as it did.
///
/// A host that cannot say is a host that was never taught to draft one, and it is
/// passed over rather than refused — `is_draft` is defaulted to
/// [`Error::NotImplemented`], so this is the answer every implementation written
/// against the earlier surface gives, and refusing it would break publications that
/// have nothing to do with drafts. The cost is bounded and safe in the one direction
/// that matters: a host that drafts a change and cannot be asked about it leaves the
/// draft standing, and a draft that stands is a change that does not land.
pub(crate) fn lift_any_draft(
    host: &dyn RemoteHost,
    change: &ChangeRequest,
    stream: &mut Stream,
) -> Result<()> {
    match host.is_draft(change) {
        Ok(true) => {}
        Ok(false) => return Ok(()),
        Err(Error::NotImplemented { .. }) => return Ok(()),
        Err(unreadable) => return Err(unreadable),
    }
    lift_draft(host, change, stream)
}

/// Ask the host to take a change it holds as a draft out of it, and record the lift.
///
/// The one place a lift is performed and recorded, whichever path decided on it: a
/// publication lifting on green checks or early, `onevcs change ready`, and a
/// publication under `drafts: {disabled: true}` lifting an adopted draft.
fn lift_draft(host: &dyn RemoteHost, change: &ChangeRequest, stream: &mut Stream) -> Result<()> {
    host.ready_for_review(change)?;
    stream.emit(
        EventKind::DraftLifted,
        object(json!({"url": change.url.to_string(), "id": change.id.0})),
    );
    Ok(())
}

/// Record the commit the host merged this change at, on the branch itself.
///
/// The most certain answer to "did this branch land": a landing that was *recorded*
/// rather than inferred from what the base happens to carry. The record is one
/// provenance trailer, `<prefix>Landed-Commit: <sha>`, under the same configured
/// prefix every other trailer this crate reads and writes uses — so a host that
/// spells its provenance differently reads its own landings and not somebody
/// else's. It goes on an otherwise empty commit, because the branch's content is
/// exactly what merged and a record that changed it would leave the branch
/// disagreeing with the base it has just reached.
///
/// Best effort, deliberately. The change has already merged by the time this runs,
/// and reporting the publication as failed because its own footnote could not be
/// written would be a worse lie than the missing line — the same rule the event
/// stream is written under. What went wrong is said on stderr, where the operator
/// running the command sees it.
fn record_landing(context: &Context<'_>, sha: &Sha) {
    if let Err(error) = write_landing(context, sha) {
        eprintln!(
            "onevcs: warning: {branch:?} merged at {merged}, and the landing was not recorded on \
             the branch: {error}",
            branch = context.branch,
            merged = sha.0,
        );
    }
}

/// Record what each of this identity's **automated** release targets had at the
/// moment this change landed.
///
/// A baseline is what makes "has this change been released" answerable later: a
/// probe answering a strictly greater version afterwards is the release that
/// carries this work. It is captured here, at the landing, because a reading taken
/// any later cannot tell a release that carries this change from one that predates
/// it.
///
/// Nothing is probed for a human-step target, because there is nothing to probe —
/// what a landing starts for one of those is a wait a person ends. And nothing at
/// all happens for an identity with no release targets, which is every identity on
/// a host with no release-targets file.
///
/// Best effort, exactly as [`record_landing`] above is and for the same reason: the
/// change has already merged, and reporting the publication as failed because its
/// own baseline could not be captured would be a worse lie than the missing record.
fn record_baselines(context: &Context<'_>, sha: &str, stream: &mut Stream) {
    match crate::store::load() {
        Ok(registry) => {
            crate::release::record_baselines(&registry, &context.resolution.key, sha, stream)
        }
        Err(failure) => eprintln!(
            "onevcs: warning: the release baselines for {identity} at landing {sha} were not \
             captured: {failure}",
            identity = context.resolution.key,
        ),
    }
}

fn write_landing(context: &Context<'_>, sha: &Sha) -> Result<()> {
    let message = format!(
        "chore: record the landing of {branch}\n\n{key} {merged}",
        branch = context.branch,
        key = context.provenance.landed(),
        merged = sha.0,
    );
    if context.built == Built::Resumed {
        // Nothing was built for a resumed publication, so the branch's own checkout is
        // the repository it works in, and the record goes onto the branch there.
        git::commit_empty_on_branch(&context.repo, &context.branch, &message)?;
        return Ok(());
    }
    git::commit_empty(&context.worktree, &message)?;
    // The repository this publication worked in goes with its run root, so the
    // record has to reach the checkout that keeps the branch. Fast-forward only,
    // like every other hand-back here: a checkout holding work this run does not
    // have keeps it.
    if !git::copy_branch(&context.repo, &context.preserved_into, &context.branch)? {
        return Err(crate::error::invalid(format!(
            "the checkout {} would not take the branch, so nothing outside this run carries the \
             record",
            context.preserved_into.display()
        )));
    }
    Ok(())
}

/// How much of a verifier's log travels on the failure that names it.
///
/// Well under the envelope's own 4096-byte payload limit, because this is a
/// pointer to the evidence and not a second copy of it: the whole log is the
/// artifact the event beside it already carries, fetched with `onevcs artifact
/// cat`. One bound for both verifiers this crate reports on — a host's required
/// check and the repository's own merge path — because the reason is the same for
/// each: how long a verification's log runs is the verifier's business, and neither
/// failure is the place to carry the whole of one.
const LOG_EXCERPT: usize = 2048;

/// The end of a verifier's log, bounded at a line boundary.
///
/// The *end*, because a verification prints its diagnosis last and its setup first —
/// an excerpt taken from the top of a twenty-thousand-line log is the part nobody
/// needed. That is not a guess about CI: a merge path that refused a publication
/// here put its one finding in the last twelve lines of seventy-six thousand bytes,
/// and the bounded head that travelled on the `push` event was the toolchain warming
/// up. What was cut is marked, so a reader can tell an excerpt from a short log, and
/// `whole` says where the rest of it is — which the failure has already named.
fn excerpt(log: &str, whole: &str) -> String {
    let trimmed = log.trim_end();
    if trimmed.len() <= LOG_EXCERPT {
        return trimmed.to_owned();
    }
    let mut cut = trimmed.len() - LOG_EXCERPT;
    while cut < trimmed.len() && !trimmed.is_char_boundary(cut) {
        cut += 1;
    }
    let tail = &trimmed[cut..];
    let tail = tail.find('\n').map_or(tail, |at| &tail[at + 1..]);
    format!("[…earlier output omitted; {whole}…]\n{tail}")
}

/// What a host's answer about a change request says about **one commit**.
///
/// The publication path's whole defence against a verdict that is about the wrong
/// thing, and it is a type rather than a filter so the distinction cannot be
/// dropped by accident: "the host has said nothing about this commit yet" and "the
/// host says nothing blocks it" are the same empty list and opposite answers. A
/// caller reaches the checks only by having said which of the two it is holding.
///
/// A publication watches the commit it just pushed. The host attaches that commit's
/// checks seconds to minutes later, and until it does, the change request still
/// carries the *previous* head's — a red one of which used to end the publication
/// with a verdict that predated the check it claimed to have read.
enum Reported {
    /// What this answer holds about the commit: the checks the host attached to it,
    /// and the checks the host attached to no commit at all. Empty is an answer —
    /// the one a repository that declares no check gives — and it is only ever
    /// reached when the host reported no checks whatsoever.
    About(Vec<Check>),
    /// Every check the host reported names some *other* commit, so it has said
    /// nothing about this one. What a head pushed moments ago looks like.
    NotYet,
}

impl Reported {
    /// Read one host answer as what it says about `commit`.
    fn of(answered: Vec<Check>, commit: &Sha) -> Self {
        let reported = !answered.is_empty();
        let about: Vec<Check> = answered
            .into_iter()
            .filter(|check| is_about(check, commit))
            .collect();
        if about.is_empty() && reported {
            return Self::NotYet;
        }
        Self::About(about)
    }

    /// The checks that are about the commit, which is none of them until the host
    /// has said anything about it.
    fn checks(&self) -> &[Check] {
        match self {
            Self::About(checks) => checks,
            Self::NotYet => &[],
        }
    }
}

/// Whether one check is the host's answer *about* `commit`.
///
/// Two cases, and the second is why this is not equality: a check the host attached
/// to `commit` is about it, and so is a check the host named no commit for at all.
/// A host that will not say which head its checks belong to is a host whose checks
/// are read exactly as they were before any of them carried one — declining to
/// answer is not a reason to stall a publication until its bound. Only a check
/// naming some *other* commit is set aside.
///
/// Private, and it belongs here rather than on [`Check`]: it is this path's reading
/// of the host's answer, not a property of the answer, and the approved surface
/// declares the field and no method over it.
fn is_about(check: &Check, commit: &Sha) -> bool {
    check.head.as_ref().is_none_or(|head| head == commit)
}

/// One reading of the host, narrowed to the pushed commit, with whether the answer
/// came from the host's complete rollup.
struct Reading {
    reported: Reported,
    /// Whether the checks were read from the source that sees every check — which is
    /// what makes "none of them is required" the host's own answer that nothing is.
    complete: bool,
}

impl Reading {
    /// Each required check's standing in this reading, by what the host declared it
    /// requires — so a declared check with no run yet stands with no state.
    fn standing(&self, declared: &Declared) -> Vec<Standing<'_>> {
        let about: Vec<&Check> = self.reported.checks().iter().collect();
        standings(&about, declared)
    }
}

/// Which checks a repository requires before a merge, as far as the host said.
///
/// Three answers, never two: "none is required" is the one a draft may act on at once,
/// and it is only ever the host's **complete** answer. An incomplete or unreadable one
/// is [`Declared::Unknown`], which waits.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Declared {
    /// The host answered, completely, that nothing is required.
    Nothing,
    /// The checks the host names as required, from every source that answered.
    Names(BTreeSet<String>),
    /// The host did not say — a source it could not read, or no answer at all — with
    /// why. What is required is then read off the checks' own `required` as they
    /// arrive, and a settlement read that way has to say so and why: by the manager's
    /// ruling on the amendment, such a green must never read as an ordinary complete
    /// answer.
    Unknown(String),
}

/// What the host says it requires before a merge into this change's base.
///
/// Asked once, before the draft's checks are watched, through the same seam the audit
/// reads. A host that was never taught to answer, or would not, has not said nothing
/// is required.
fn declared_required(host: &dyn RemoteHost, change: &ChangeRequest) -> Declared {
    match host.required_checks_on(&change.base) {
        Ok(answer) if answer.checks.is_empty() && answer.complete() => Declared::Nothing,
        Ok(answer) if !answer.checks.is_empty() => Declared::Names(answer.checks),
        Ok(answer) => Declared::Unknown(format!(
            "the host could not read {} for which checks a merge into {} requires",
            answer
                .unconsulted
                .keys()
                .map(|source| source.describe())
                .collect::<Vec<_>>()
                .join(" or "),
            change.base
        )),
        Err(refused) => Declared::Unknown(format!(
            "the host would not say which checks a merge into {} requires: {refused}",
            change.base
        )),
    }
}

impl Declared {
    /// This answer, sharpened by one reading: where the host would not say what it
    /// requires, a **complete** rollup reporting checks of which none is required is
    /// that same answer given another way — `gh pr checks --required`'s "no required
    /// checks reported" — and is read as it.
    fn given(&self, reading: &Reading) -> Declared {
        match &reading.reported {
            Reported::About(checks) => self.sharpened(checks, reading.complete),
            Reported::NotYet => self.clone(),
        }
    }

    /// [`given`](Declared::given), for checks already narrowed to the commit they are
    /// about: where the host would not say what it requires, a complete answer
    /// reporting checks of which none is required is its answer that nothing is.
    fn sharpened(&self, checks: &[Check], complete: bool) -> Declared {
        match self {
            Declared::Unknown(_)
                if complete && !checks.is_empty() && checks.iter().all(|check| !check.required) =>
            {
                Declared::Nothing
            }
            _ => self.clone(),
        }
    }

    /// Why what is required is read off the host's own per-check marking, where it
    /// is — under [`Declared::Unknown`], and nowhere the host gave an answer — which is
    /// what a settlement then has to say it was read from.
    fn unread(&self) -> Option<&str> {
        match self {
            Declared::Unknown(because) => Some(because),
            Declared::Nothing | Declared::Names(_) => None,
        }
    }
}

/// The verdict one reading's standing gives, by the rules every phase of the watch
/// reads a ready change's checks by — the one classifier a publication's watch and a
/// standings read both decide through, so the two cannot come to disagree.
///
/// A required check that concluded red is `Failed`, naming every one that did; a set
/// the host answered, completely, is empty is `NoneRequired`; a set every member of
/// which passed or was skipped is `Passed`, or `PassedWithSkipped` naming the skipped
/// ones, because a skipped check never reads as passed; and anything else — a declared
/// check with no run yet, one still running, one that ended with no verdict — is
/// `Pending`, naming each.
fn verdict_of(standing: &[Standing<'_>], declared: &Declared) -> ChecksVerdict {
    let named = |wanted: fn(Option<CheckState>) -> bool| -> Vec<String> {
        standing
            .iter()
            .filter(|standing| wanted(standing.state))
            .map(|standing| standing.name.clone())
            .collect()
    };
    let failed = named(|state| state == Some(CheckState::Failed));
    if !failed.is_empty() {
        return ChecksVerdict::Failed { failed };
    }
    if *declared == Declared::Nothing && standing.is_empty() {
        return ChecksVerdict::NoneRequired;
    }
    if lets_through(standing, declared) {
        let skipped = skipped_in(standing);
        return if skipped.is_empty() {
            ChecksVerdict::Passed
        } else {
            ChecksVerdict::PassedWithSkipped { skipped }
        };
    }
    ChecksVerdict::Pending {
        waiting: named(|state| {
            matches!(
                state,
                None | Some(CheckState::Pending | CheckState::NoVerdict)
            )
        }),
    }
}

/// The verdict of `checks` — every check the host reports on one commit — against
/// what the host declares a merge into the base requires, read the way the watch
/// reads one reading of them. `complete` is whether the checks came from the host's
/// whole rollup, which is what lets a rollup marking nothing required stand for a
/// declaration that could not be read.
pub(crate) fn checks_verdict(
    checks: &[Check],
    declared: &Declared,
    complete: bool,
) -> ChecksVerdict {
    let declared = declared.sharpened(checks, complete);
    let about: Vec<&Check> = checks.iter().collect();
    verdict_of(&standings(&about, &declared), &declared)
}

/// Whether `standing` lets the change through: every required check passed or was
/// skipped, over a set that is not empty unless the host answered, completely, that
/// nothing is required. A declared check with no run yet stands with no state, so it
/// holds the change; and where the declaration could not be read, an empty set is
/// the host having marked nothing required *yet* rather than its answer that nothing is.
fn lets_through(standing: &[Standing<'_>], declared: &Declared) -> bool {
    (*declared == Declared::Nothing || !standing.is_empty())
        && standing.iter().all(|standing| {
            matches!(
                standing.state,
                Some(CheckState::Passed | CheckState::Skipped)
            )
        })
}

/// Where one required check stands in a reading: its name, the state its entries
/// add up to — `None` where the host reported none — and the entry that decided it.
struct Standing<'c> {
    name: String,
    state: Option<CheckState>,
    check: Option<&'c Check>,
}

/// Each required check's standing in `checks`.
///
/// The names come from the host's declaration where it made one, so a required
/// check it has not reported yet is *absent* rather than overlooked; otherwise from
/// the checks' own `required`. A name reported more than once — a run re-attached
/// after a lift, a run that ended with no verdict and was re-run — stands at the most
/// decisive of its entries: a red one fails it, a running one holds it, and a passed
/// one outranks a run that ended with no verdict or was skipped, which is how a later
/// green run of a check that was cancelled or skipped proceeds.
fn standings<'c>(checks: &[&'c Check], declared: &Declared) -> Vec<Standing<'c>> {
    let names: BTreeSet<String> = match declared {
        Declared::Names(names) => names.clone(),
        Declared::Nothing => BTreeSet::new(),
        Declared::Unknown(_) => checks
            .iter()
            .filter(|check| check.required)
            .map(|check| check.name.clone())
            .collect(),
    };
    let rank = |state: CheckState| match state {
        CheckState::Failed => 0,
        CheckState::Pending => 1,
        CheckState::Passed => 2,
        CheckState::NoVerdict => 3,
        CheckState::Skipped => 4,
    };
    names
        .into_iter()
        .map(|name| {
            let decided = checks
                .iter()
                .copied()
                .filter(|check| check.name == name)
                .min_by_key(|check| rank(check.state()));
            Standing {
                state: decided.map(Check::state),
                check: decided,
                name,
            }
        })
        .collect()
}

/// The standings' red one, where a required check concluded red.
fn red<'c>(standings: &[Standing<'c>]) -> Option<&'c Check> {
    standings
        .iter()
        .find(|standing| standing.state == Some(CheckState::Failed))
        .and_then(|standing| standing.check)
}

/// A publication watching one change request at one commit, across every phase of
/// its watch — the draft's, the one after an early lift, and the ready change's up to
/// the merge — so that each check transition is reported once and one bound covers
/// the whole of it.
///
/// Three ways out of any phase, and each one says which it was: the phase's own
/// ending, a **required** check concluding red ([`Error::ChecksFailed`], naming it and
/// quoting its log), or the bound elapsing ([`Error::ChecksUnsettled`], naming what was
/// still pending). A caller routing a failure has to be able to tell "CI said no" from
/// "nobody answered in an hour".
///
/// `pushed` is what the checks are read *about*, and it is the commit this run put on
/// the remote rather than the head the host reports for the change request — which is
/// the same value only once the host has noticed the push. [`Reported`] is where that
/// is decided, and everything past it — the stream, the red-check verdict, and the
/// bound's own sentence — sees only checks about `pushed`.
/// Where one check stood when it was last reported: its status, its conclusion, and
/// when the run started.
type Transition = (String, Option<String>, Option<String>);

struct Watcher<'a> {
    host: &'a dyn RemoteHost,
    change: &'a ChangeRequest,
    pushed: &'a Sha,
    /// Asked between readings, at least every [`CANCELLATION_GRAIN`], so a caller's
    /// cancellation ends any phase of the watch whatever the poll interval is.
    cancellation: &'a dyn PublicationCancellation,
    bound: std::time::Duration,
    poll: std::time::Duration,
    started: std::time::Instant,
    /// When the watch began on the pushed head, which is when its gate run started.
    began: Moment,
    /// Whether the watch's `gate-run` has been recorded, which it is once per watch:
    /// when the required checks settle, or when the watch ends on a red check or its
    /// bound — whichever comes first.
    gate_recorded: bool,
    /// What each check was last reported as, so only a transition is reported. Keyed
    /// on the status, the conclusion *and* the run's start, because a run the host
    /// re-attaches after a lift can complete exactly as the skipped one had, and it is
    /// still a run of its own.
    reported: Vec<(String, Transition)>,
    /// What each settled check's log was stored as, so the refusal that names a red
    /// check can quote it without fetching it from the host a second time.
    logs: Vec<(String, crate::event::ArtifactId)>,
    /// Whether `checks-settled` has been recorded, which it is once per watch.
    settled: bool,
    /// Whether an early lift prints its warning line: the resolved
    /// `drafts.warn_on_early_lift`.
    warn_on_early_lift: bool,
    /// What the host declares a merge into the base requires — asked once per watch,
    /// by [`Watcher::declared`].
    declaration: Option<Declared>,
    /// Whether the watch ended because its bound elapsed, which is the one
    /// `checks-unsettled` a publication answers by arming the host's merge.
    elapsed: bool,
    /// Whether the host's own merge has been armed on the change by this watch.
    armed: bool,
    /// The required checks that had concluded skipped when the bound elapsed. The
    /// host's auto-merge reads a skipped required check as satisfied, so a watch that
    /// elapsed over any is never armed.
    skipped_at_elapse: Vec<String>,
}

impl<'a> Watcher<'a> {
    fn new(
        host: &'a dyn RemoteHost,
        change: &'a ChangeRequest,
        pushed: &'a Sha,
        warn_on_early_lift: bool,
        cancellation: &'a dyn PublicationCancellation,
        bound_seconds: f64,
    ) -> Result<Self> {
        Ok(Self {
            warn_on_early_lift,
            host,
            change,
            pushed,
            cancellation,
            bound: std::time::Duration::from_secs_f64(bound_seconds),
            poll: std::time::Duration::from_secs_f64(gh::checks_poll()?),
            started: std::time::Instant::now(),
            began: Moment::now(),
            gate_recorded: false,
            reported: Vec::new(),
            logs: Vec::new(),
            settled: false,
            declaration: None,
            elapsed: false,
            armed: false,
            skipped_at_elapse: Vec::new(),
        })
    }

    /// What the host declares it requires before a merge into this change's base:
    /// asked of the host the first time a phase of the watch needs it and remembered
    /// for the rest of the watch, so one watch reads every phase against one answer.
    fn declared(&mut self) -> Declared {
        self.declaration
            .get_or_insert_with(|| declared_required(self.host, self.change))
            .clone()
    }

    /// Ask the host once, narrow its answer to the pushed commit, and report every
    /// check that moved.
    fn read(&mut self, stream: &mut Stream) -> Result<Reading> {
        // What was consulted travels with the checks and is deliberately not acted
        // on here: a credential that can see only GitHub Actions still gates a merge
        // on what it *can* see, and a credential that could see nothing was a
        // refusal rather than an empty answer.
        let answered = self.host.change_checks(self.change)?;
        let complete = answered.complete();
        // Narrowed to the commit this publication pushed before anything is reported
        // or acted on, so a check the host attached to a head this run replaced
        // reaches neither the stream nor the verdict.
        let reported = Reported::of(answered.checks, self.pushed);
        for check in reported.checks() {
            let now = (
                check.status.clone(),
                check.conclusion.clone(),
                check.started_at.clone(),
            );
            let previous = self
                .reported
                .iter()
                .find(|(name, _)| name == &check.name)
                .map(|(_, was)| was.clone());
            if previous.as_ref() == Some(&now) {
                continue;
            }
            let mut artifacts = Vec::new();
            if check.settled() {
                // The log records what the check printed; `conclusion`, already read,
                // is what decides whether it blocks. So a host that will not hand
                // one over is reported the way a stream that cannot be written is —
                // on stderr, without failing the command over it.
                match self.host.check_log(self.change, check) {
                    Ok(id) => {
                        self.logs.retain(|(name, _)| name != &check.name);
                        self.logs.push((check.name.clone(), id.clone()));
                        artifacts.push(crate::event::ArtifactRef {
                            id: id.0,
                            kind: "log".to_owned(),
                            bytes: 0,
                        });
                    }
                    Err(error) => eprintln!(
                        "onevcs: warning: check {:?} on {} is recorded without its log: {error}",
                        check.name, self.change.url
                    ),
                }
            }
            stream.emit_with(
                EventKind::ChangeCheck,
                object(json!({
                    "name": check.name,
                    "required": check.required,
                    "status": check.status,
                    "from_status": previous.map(|(status, _, _)| status),
                    "conclusion": check.conclusion,
                    "state": check.state(),
                })),
                artifacts,
            );
            self.reported.retain(|(name, _)| name != &check.name);
            self.reported.push((check.name.clone(), now));
        }
        Ok(Reading { reported, complete })
    }

    /// The refusal for a required check that concluded red, recording the watch's
    /// gate run as `failed`.
    fn failed(&mut self, stream: &mut Stream, standing: &[Standing<'_>], check: &Check) -> Error {
        self.record_gate(stream, gate_run::Ruling::Failed, standing);
        checks_failed(check, &self.logs)
    }

    /// Record, once, the watch's gate run: from when the watch began on the pushed
    /// head to now, with every required check as the host last reported it.
    fn record_gate(
        &mut self,
        stream: &mut Stream,
        verdict: gate_run::Ruling,
        standing: &[Standing<'_>],
    ) {
        if std::mem::replace(&mut self.gate_recorded, true) {
            return;
        }
        stream.emit_gate_run(&gate_run::Run {
            gate: Gate::RequiredChecks,
            started: self.began,
            ended: Moment::now(),
            verdict,
            checks: standing
                .iter()
                .map(|standing| gate_run::listed(&standing.name, standing.check))
                .collect(),
        });
    }

    /// Whether the bound on the whole watch has elapsed.
    fn out_of_time(&self) -> bool {
        self.started.elapsed() >= self.bound
    }

    /// Wait out one interval between readings, or end the watch as
    /// [`Error::Cancelled`] the moment the caller cancels it.
    fn pause(&self) -> Result<()> {
        pause_unless_cancelled(self.poll, self.cancellation, || {
            cancelled_while(&format!(
                "it watched {url} at {commit}",
                url = self.change.url,
                commit = self.pushed.0,
            ))
        })
    }

    /// The refusal for a change request the host confirms conflicts with its base,
    /// quoting what the host reported.
    fn conflicting(&self, reported: &str) -> Error {
        Error::SyncConflict {
            reason: format!(
                "{url} conflicts with its base {base:?}: the host reports it {reported}, so the \
                 merge it was armed to perform can never happen. The branch stays on its remote \
                 at {commit} and the change request stays open — merge {base} into the branch, \
                 resolve the conflict, and publish the session again",
                url = self.change.url,
                base = self.change.base,
                commit = self.pushed.0,
            ),
        }
    }

    /// The bound elapsed, naming what the host had not settled, and recording the
    /// watch's gate run as one that reached no verdict.
    ///
    /// `declared` is what `standing` was read against, so the sentence says which of
    /// the host's answers the standing came from.
    fn unsettled(
        &mut self,
        stream: &mut Stream,
        reading: &Reading,
        standing: &[Standing<'_>],
        declared: &Declared,
        awaited: &str,
    ) -> Error {
        self.elapsed = true;
        self.skipped_at_elapse = skipped_in(standing);
        self.record_gate(stream, gate_run::Ruling::NoVerdict, standing);
        unsettled(
            self.change,
            self.pushed,
            &reading.reported,
            Requirement { standing, declared },
            self.bound,
            awaited,
        )
    }

    /// Record, once, that the required checks stopped blocking — `passed`, or
    /// `passed-with-skipped` naming the required checks that concluded skipped.
    ///
    /// `unread` is set where the draft's requirement was read from the host's own
    /// per-check `required` marking because its declaration could not be read — and
    /// then the record says so, in the payload's `requirement` and on stderr, so the
    /// settlement never reads as the host's ordinary complete answer. Omitted
    /// otherwise, so every other settlement's payload is the five fields it always was.
    ///
    /// The watch's gate run ends here too, with the same verdict, listing `standing` —
    /// every required check as this reading left it.
    fn record_settled(
        &mut self,
        stream: &mut Stream,
        standing: &[Standing<'_>],
        unread: Option<&str>,
    ) {
        if std::mem::replace(&mut self.settled, true) {
            return;
        }
        let skipped = skipped_in(standing);
        let ruled = gate_run::passed(!skipped.is_empty());
        let verdict = if skipped.is_empty() {
            "passed"
        } else {
            "passed-with-skipped"
        };
        let mut payload = object(json!({
            "url": self.change.url.to_string(),
            "id": self.change.id.0,
            "head": self.pushed.0,
            "verdict": verdict,
            "skipped": skipped,
        }));
        if let Some(because) = unread {
            eprintln!(
                "onevcs: warning: the required checks on {url} were read from the host's own \
                 per-check marking, because the declaration could not be read: {because}. The \
                 host's merge path still rules on any merge",
                url = self.change.url,
            );
            payload.insert(
                "requirement".to_owned(),
                json!({"read_from": "host-marking", "because": because}),
            );
        }
        stream.emit(EventKind::ChecksSettled, payload);
        self.record_gate(stream, ruled, standing);
    }
}

/// Wait until the change's required checks stop blocking, and answer whether it is
/// still a draft then.
///
/// A change that is not a draft is watched as it always was — [`settle_ready`]. A
/// draft is watched by the draft's rule, [`settle_draft`]: a skipped required check
/// on it has **not run**, and a draft none of whose required checks has run by the
/// grace window is lifted so that they can, after which it is a ready change whose
/// checks must run again. `true` means the checks came back green on the draft, which
/// is still standing for the caller to lift or keep.
fn settle(watcher: &mut Watcher<'_>, stream: &mut Stream, drafted: bool) -> Result<bool> {
    if drafted {
        settle_draft(watcher, stream)
    } else {
        settle_ready(watcher, stream).map(|()| false)
    }
}

/// Wait until every check the host requires has settled without blocking.
///
/// What a `change-direct` publication does before it asks the host to merge, and what
/// every ready change does before its policy's ending: this run performs the merge,
/// or reports the change as open, so an answer the host's own checks have already
/// refused is one to report rather than act past.
///
/// What is required is what the host **declares** — its rulesets and its branch
/// protection — so a required check that has not started yet holds the watch rather
/// than going unnoticed; only where that declaration could not be read is it read off
/// the checks' own marking, and then a settlement says so. A skipped required check
/// satisfies this, as it satisfies the host's own merge path — and the watch then
/// records `checks-settled` as `passed-with-skipped`, naming it, so the record says
/// what the host accepted rather than calling it green. A host whose complete answer
/// is that **nothing is required** is not a stall here: it has answered, and its
/// answer is that nothing blocks. A host that could not say, and has marked nothing
/// required, has not answered that, so it is waited on.
fn settle_ready(watcher: &mut Watcher<'_>, stream: &mut Stream) -> Result<()> {
    let declared = watcher.declared();
    loop {
        let reading = watcher.read(stream)?;
        let declared = declared.given(&reading);
        if let Reported::About(checks) = &reading.reported {
            let about: Vec<&Check> = checks.iter().collect();
            let standing = standings(&about, &declared);
            // The verdict a standings read gives the same checks, through the same
            // function: pending waits, red fails, and every other verdict settles.
            match verdict_of(&standing, &declared) {
                ChecksVerdict::Pending { .. } => {}
                ChecksVerdict::Failed { .. } => {
                    if let Some(failed) = red(&standing) {
                        return Err(watcher.failed(stream, &standing, failed));
                    }
                }
                _ => {
                    watcher.record_settled(stream, &standing, declared.unread());
                    return Ok(());
                }
            }
        }
        // Otherwise the host has posted nothing about the commit this publication
        // pushed, or something it requires is still running or has not started. The
        // first is a clock and not an answer — the checks it *is* reporting belong to
        // a head this run replaced — so the watch keeps waiting rather than merging on
        // an emptiness that is really somebody else's verdict having been filtered out.
        if watcher.out_of_time() {
            return Err(watcher.unsettled(
                stream,
                &reading,
                &reading.standing(&declared),
                &declared,
                "settled its required checks on",
            ));
        }
        watcher.pause()?;
    }
}

/// The required checks that concluded skipped, by name.
fn skipped_in(standing: &[Standing<'_>]) -> Vec<String> {
    standing
        .iter()
        .filter(|standing| standing.state == Some(CheckState::Skipped))
        .map(|standing| standing.name.clone())
        .collect()
}

/// Watch a draft's required checks until they are green on it, lifting it early where
/// none of them has run by the grace window. `true` is green on the draft; `false` is
/// a draft lifted early whose checks then ran green as a ready change's.
///
/// **A skipped required check has not run** while the change is a draft: a workflow
/// that gates a job on the change not being one reports `skipped` without having
/// verified anything. So green on a draft is every required check `passed`, and a
/// draft where the only thing stopping that is checks that have not run — absent, or
/// skipped — is the draft-skipping repository: once the grace window elapses it is
/// lifted, `draft-lifted` and `draft-lifted-early` are recorded, one warning line is
/// printed unless `drafts.warn_on_early_lift` is off, and the watch goes on as
/// [`settle_after_early_lift`].
///
/// A host that says, completely, that nothing is required is green at once: no grace
/// window and no warning.
fn settle_draft(watcher: &mut Watcher<'_>, stream: &mut Stream) -> Result<bool> {
    let grace = std::time::Duration::from_secs_f64(gh::draft_grace()?);
    let declared = watcher.declared();
    loop {
        let reading = watcher.read(stream)?;
        let declared = declared.given(&reading);
        if declared == Declared::Nothing {
            watcher.record_settled(stream, &[], None);
            return Ok(true);
        }
        // Whether what is required is being read off the host's marking because the
        // declaration could not be read, which a settlement then has to say.
        let marked = declared.unread();
        let about: Vec<&Check> = reading.reported.checks().iter().collect();
        let standing = standings(&about, &declared);
        if let Some(failed) = red(&standing) {
            return Err(watcher.failed(stream, &standing, failed));
        }
        if !standing.is_empty()
            && standing
                .iter()
                .all(|standing| standing.state == Some(CheckState::Passed))
        {
            watcher.record_settled(stream, &standing, marked);
            return Ok(true);
        }
        let not_run: Vec<String> = standing
            .iter()
            .filter(|standing| matches!(standing.state, None | Some(CheckState::Skipped)))
            .map(|standing| standing.name.clone())
            .collect();
        let running = standing.iter().any(|standing| {
            matches!(
                standing.state,
                Some(CheckState::Pending | CheckState::NoVerdict)
            )
        });
        // Nothing is known to be required and nothing reported is marked required: a
        // host that will not say what a merge needs. That is never read as "none", so
        // it is not green at once — it waits the grace window. Then, where checks ran
        // on the draft and none of them was skipped, the host's own marking is the
        // answer, exactly as a ready change's watch reads it; where nothing ran, or
        // something was skipped, it is the draft-skipping repository, and only a lift
        // can show what runs.
        let unseen = matches!(declared, Declared::Unknown(_)) && standing.is_empty();
        let grace_elapsed = watcher.started.elapsed() >= grace;
        let about = reading.reported.checks();
        if unseen
            && grace_elapsed
            && !about.is_empty()
            && about
                .iter()
                .all(|check| check.state() != CheckState::Skipped)
        {
            watcher.record_settled(stream, &standing, marked);
            return Ok(true);
        }
        if grace_elapsed && !running && (!not_run.is_empty() || unseen) {
            let snapshot = reading.reported.checks().to_vec();
            lift_early(watcher, stream, &not_run, grace)?;
            return settle_after_early_lift(watcher, stream, &declared, &snapshot, grace)
                .map(|()| false);
        }
        if watcher.out_of_time() {
            return Err(watcher.unsettled(
                stream,
                &reading,
                &standing,
                &declared,
                "settled its required checks on",
            ));
        }
        watcher.pause()?;
    }
}

/// Lift a draft whose required checks had not run on it, and say so.
fn lift_early(
    watcher: &mut Watcher<'_>,
    stream: &mut Stream,
    awaited: &[String],
    grace: std::time::Duration,
) -> Result<()> {
    let change = watcher.change;
    lift_draft(watcher.host, change, stream)?;
    let warned = watcher.warn_on_early_lift;
    if warned {
        eprintln!(
            "onevcs: warning: {url} was lifted out of its draft before its required checks ran \
             on it ({awaited} had not run within {seconds}s), which is what a workflow that \
             skips drafts looks like. It is watched as a ready change from here; set \
             `drafts: {{warn_on_early_lift: false}}` in the rules file to silence this",
            url = change.url,
            awaited = if awaited.is_empty() {
                "the host named none".to_owned()
            } else {
                guidance::listed(awaited)
            },
            seconds = grace.as_secs_f64(),
        );
    }
    stream.emit(
        EventKind::DraftLiftedEarly,
        object(json!({
            "url": change.url.to_string(),
            "id": change.id.0,
            "base": change.base,
            "awaited": awaited,
            "grace_seconds": grace.as_secs_f64(),
            "warned": warned,
        })),
    );
    Ok(())
}

/// Whether `check` is a run the host attached after the lift, rather than the run of
/// that check it reported on the draft — `snapshot`.
///
/// Decided by the run's own identity as the host reports it, never by what it
/// concluded or where it stands: a re-run a workflow starts on `ready_for_review` can
/// conclude `skipped` exactly as the draft's run did, and it is still a run. So a check
/// the draft never reported is new, and one it did report is new only where the host
/// reports a start for it that no draft-era run of that check had — running or settled
/// alike. One reporting no start, or the draft's own start, is the draft's — which is
/// the side a wrong answer must fall on, since a draft-era skip read as a post-lift one
/// is a merge nothing verified. A re-run queued without a start yet therefore counts
/// once the host says it has started.
fn ran_after_the_lift(check: &Check, snapshot: &[Check]) -> bool {
    let earlier: Vec<&Check> = snapshot
        .iter()
        .filter(|seen| seen.name == check.name)
        .collect();
    if earlier.is_empty() {
        return true;
    }
    check.started_at.as_ref().is_some_and(|started| {
        earlier
            .iter()
            .all(|seen| seen.started_at.as_ref() != Some(started))
    })
}

/// Watch a change lifted early until a run the host attached **after** the lift
/// settles its required checks.
///
/// What was reported while it was a draft is `snapshot`, and a draft-era `skipped` in
/// it is not a verdict: nothing ran. So the watch needs a run the host attached since
/// — told from the draft's by [`ran_after_the_lift`] — and where none of the required
/// checks has one within another grace window, it ends [`Error::ChecksUnsettled`]
/// saying they did not re-run — never merging, and never lifting anything, on skips
/// from the draft. Once one has, the ordinary rule applies, under which a skipped
/// required check from a post-lift run satisfies the watch and is recorded as
/// `passed-with-skipped`.
fn settle_after_early_lift(
    watcher: &mut Watcher<'_>,
    stream: &mut Stream,
    declared: &Declared,
    snapshot: &[Check],
    grace: std::time::Duration,
) -> Result<()> {
    let lifted = std::time::Instant::now();
    let from_the_draft = |check: &Check| !ran_after_the_lift(check, snapshot);
    loop {
        let reading = watcher.read(stream)?;
        let checks = reading.reported.checks();
        // A draft-era skip is not a verdict, so it is as though it were not there.
        let counted: Vec<&Check> = checks
            .iter()
            .filter(|check| !(from_the_draft(check) && check.state() == CheckState::Skipped))
            .collect();
        let standing = standings(&counted, declared);
        if let Some(failed) = red(&standing) {
            return Err(watcher.failed(stream, &standing, failed));
        }
        let rerun = checks.iter().any(|check| {
            !from_the_draft(check)
                && (standing.is_empty() || standing.iter().any(|s| s.name == check.name))
        });
        if rerun
            && standing.iter().all(|standing| {
                matches!(
                    standing.state,
                    Some(CheckState::Passed | CheckState::Skipped)
                )
            })
        {
            watcher.record_settled(stream, &standing, declared.unread());
            return Ok(());
        }
        if !rerun && lifted.elapsed() >= grace {
            watcher.record_gate(stream, gate_run::Ruling::NoVerdict, &standing);
            return Err(Error::ChecksUnsettled {
                reason: format!(
                    "the required checks on {url} did not re-run after its draft was lifted: no \
                     run the host attached to {commit} after the lift appeared within {seconds}s, \
                     and the runs from while it was a draft were skipped, which is not a verdict. \
                     The likely cause is a workflow that does not trigger on `ready_for_review` \
                     — add it to the workflow's pull_request types, or run the checks by hand",
                    url = watcher.change.url,
                    commit = watcher.pushed.0,
                    seconds = grace.as_secs_f64(),
                ),
            });
        }
        if watcher.out_of_time() {
            return Err(watcher.unsettled(
                stream,
                &reading,
                &standing,
                declared,
                "settled its required checks on",
            ));
        }
        watcher.pause()?;
    }
}

/// Arm the host's own merge and watch until it performs it.
///
/// Arming happens after the first reading of the checks: a host that will not say
/// what its checks are is refused with nothing armed against it. Only required checks
/// may end it: a non-blocking check never holds or fails a merge.
///
/// A change request the host confirms **conflicts** with its base ends it too, as
/// [`Error::SyncConflict`], at the first reading that says so: the base moved under the
/// armed merge and the host can never perform it, so waiting out the bound would only
/// report later what is known now. Its branch and its change request are left as they
/// are for the session to resolve. A mergeability the host has not computed yet is
/// never read as a conflict.
fn watch_the_merge(watcher: &mut Watcher<'_>, stream: &mut Stream) -> Result<Sha> {
    let declared = watcher.declared();
    loop {
        let reading = watcher.read(stream)?;
        let declared = declared.given(&reading);
        if let Reported::About(checks) = &reading.reported {
            let about: Vec<&Check> = checks.iter().collect();
            let standing = standings(&about, &declared);
            if let Some(failed) = red(&standing) {
                return Err(watcher.failed(stream, &standing, failed));
            }
            if lets_through(&standing, &declared) {
                watcher.record_settled(stream, &standing, declared.unread());
            }
        }
        if !watcher.armed {
            watcher
                .host
                .merge(watcher.change, MergePolicy::ChangeAuto)?;
            watcher.armed = true;
        }
        if let Some(sha) = watcher.host.merged_at(watcher.change)? {
            return Ok(sha);
        }
        // Asked after the merge, so a change the host landed is never reported as a
        // conflict it had before it landed. A host that has not computed it yet is
        // waited on like any other "not yet", and one that was never taught to answer
        // has said nothing, which is no conflict.
        match watcher.host.mergeability(watcher.change) {
            Ok(Mergeability::Conflicting { reported }) => {
                return Err(watcher.conflicting(&reported))
            }
            // Every other state, any added later included, is not a confirmed conflict.
            Ok(_) | Err(Error::NotImplemented { .. }) => {}
            Err(unreadable) => return Err(unreadable),
        }
        if watcher.out_of_time() {
            return Err(watcher.unsettled(
                stream,
                &reading,
                &reading.standing(&declared),
                &declared,
                "merged",
            ));
        }
        watcher.pause()?;
    }
}

/// A required check that concluded red, named, with everywhere its evidence is and
/// a bounded excerpt of what it printed.
///
/// The excerpt is on the failure itself rather than only in the artifact beside it,
/// because a caller routing this failure — or a person reading stderr — otherwise
/// learns only that a check called `gate` failed and has to go and fetch the log by
/// hand to find out why. A log this crate could not read back is simply not quoted:
/// an excerpt naming the reason there is no excerpt would read as the check's own
/// output.
///
/// The other two are *pointers*, and they are here because the excerpt is bounded
/// and this failure is usually read by whatever gets dispatched next: where the
/// check is on the host, so a person can open it, and the artifact this watch
/// already stored the whole log as, with the command that prints it. A retry handed
/// the word `checks-failed` and nothing else has to rediscover both.
fn checks_failed(check: &Check, logs: &[(String, crate::event::ArtifactId)]) -> Error {
    let stored = logs.iter().find(|(name, _)| name == &check.name);
    let said = stored
        .and_then(|(_, id)| stream::read_artifact(&id.0).ok())
        .map(|log| excerpt(&log, "the whole log is the check's artifact"))
        .filter(|log| !log.trim().is_empty())
        .map(|log| format!(" It said:\n{}", guidance::quoted_output(&log)))
        .unwrap_or_default();
    let at = check
        .url
        .as_ref()
        .map(|url| format!(" It ran at {url}."))
        .unwrap_or_default();
    let evidence = stored
        .map(|(_, id)| {
            format!(
                " Its whole log is artifact {id} — `{command}`.",
                id = id.0,
                command = guidance::command(["onevcs", "artifact", "cat", &id.0]),
            )
        })
        .unwrap_or_default();
    Error::ChecksFailed {
        reason: format!(
            "required check {:?} concluded {}.{at}{evidence}{said}",
            check.name,
            check
                .conclusion
                .as_deref()
                .unwrap_or("without a conclusion"),
        ),
    }
}

/// What a watch read the required checks against when its bound elapsed: each
/// required check's standing, and the declaration that standing was read by.
struct Requirement<'s, 'c> {
    standing: &'s [Standing<'c>],
    declared: &'s Declared,
}

/// The bound elapsed, naming what the host had not settled.
///
/// It names them because the alternative is what the bound used to do: stop after
/// an hour with a sentence that reads the same whether one check is still running,
/// the repository declares none, or the host has simply never landed the change.
/// Those are three different next moves.
///
/// Every name comes from the watch's own standing, never from the reported checks
/// alone: a check the host declares required and has not started is the one most
/// worth naming, and the reported checks are exactly where it is missing. So a
/// sentence saying every required check settled is one the standing says, and a
/// sentence saying the host declared none is one only its complete answer says.
fn unsettled(
    change: &ChangeRequest,
    pushed: &Sha,
    reported: &Reported,
    requirement: Requirement<'_, '_>,
    bound: std::time::Duration,
    awaited: &str,
) -> Error {
    if let Reported::NotYet = reported {
        // A fourth ending, and the one that must not read as any of the three below:
        // the host answered, and everything it said was about a head this run
        // replaced. "Nothing blocks the merge" is what that used to look like.
        return Error::ChecksUnsettled {
            reason: format!(
                "the host had not {awaited} {url} within {seconds}s; it has reported no check at \
                 all on {commit}, the commit this publication pushed — every check it does report \
                 is attached to some other commit",
                url = change.url,
                seconds = bound.as_secs_f64(),
                commit = pushed.0,
            ),
        };
    }
    let Requirement { standing, declared } = requirement;
    let pending: Vec<String> = standing
        .iter()
        .filter_map(|standing| match standing.state {
            Some(CheckState::Pending) => Some(format!("{:?}", standing.name)),
            None => Some(format!("{:?} (no run yet)", standing.name)),
            _ => None,
        })
        .collect();
    let no_verdict: Vec<String> = standing
        .iter()
        .filter(|standing| standing.state == Some(CheckState::NoVerdict))
        .map(|standing| {
            format!(
                "{:?}: {}",
                standing.name,
                standing
                    .check
                    .and_then(|check| check.conclusion.as_deref())
                    .unwrap_or("unknown")
            )
        })
        .collect();
    let skipped: Vec<&str> = standing
        .iter()
        .filter(|standing| standing.state == Some(CheckState::Skipped))
        .map(|standing| standing.name.as_str())
        .collect();
    let named = if !pending.is_empty() {
        let verdicts = if no_verdict.is_empty() {
            String::new()
        } else {
            format!("; completed with no verdict: {}", no_verdict.join(", "))
        };
        format!("still unsettled: {}{verdicts}", pending.join(", "))
    } else if !no_verdict.is_empty() {
        format!("completed with no verdict: {}", no_verdict.join(", "))
    } else if !skipped.is_empty() {
        format!(
            "skipped on the draft, so not run on it: {}",
            guidance::listed(&skipped)
        )
    } else if standing.is_empty() {
        match declared {
            Declared::Nothing => "the host declared no required check on it at all".to_owned(),
            _ => "it has marked no check required on it".to_owned(),
        }
    } else {
        "every required check it declared had settled".to_owned()
    };
    // Where the host's declaration could not be read, what is required was read off
    // its marking, and a reader deciding what to do next has to know the standing
    // above may be missing a required check the host simply has not reported yet.
    let read_from = declared
        .unread()
        .map(|because| {
            format!(
                ". Which checks it requires could not be read, so they were read from the \
                 checks it marked required: {because}"
            )
        })
        .unwrap_or_default();
    // llmlint: ignore[names_match_behavior] one fixed kind for the bound's three endings.
    Error::ChecksUnsettled {
        reason: format!(
            "the host had not {awaited} {url} within {seconds}s; {named}{read_from}",
            url = change.url,
            seconds = bound.as_secs_f64(),
        ),
    }
}

/// Record the push that executed the repository's `pre-push` hook as one gate run,
/// where it did execute one.
///
/// The run is the push itself — it starts when the push starts and ends when it
/// returns — so whatever git and the remote spend around the hook is in it too:
/// that is how long the gate held the publication. Accepted is `passed`, and a
/// refusal is `failed` whoever refused it, because the push is what was refused.
/// A push with no hook to run records nothing: there was no local gate.
pub(crate) fn record_pre_push(
    stream: &mut Stream,
    hooked: bool,
    started: Moment,
    ended: Moment,
    pushed: &git::Pushed,
) {
    if !hooked {
        return;
    }
    stream.emit_gate_run(&gate_run::Run {
        gate: Gate::PrePush,
        started,
        ended,
        verdict: if pushed.accepted() {
            gate_run::Ruling::Passed
        } else {
            gate_run::Ruling::Failed
        },
        checks: Vec::new(),
    });
}

/// When the host says a change request merged, where it says so in a form this
/// crate can read.
fn merge_time(host: &dyn RemoteHost, change: &ChangeRequest) -> Option<Moment> {
    host.merge_time(change)
        .ok()
        .flatten()
        .and_then(|spelled| Moment::reported(&spelled))
}

/// Record one publishing push, and what it wrote.
///
/// **Unconditionally**, which is the point of this function. The publishing push is
/// where the repository's own `pre-push` hook rules on the change, so what git and
/// that hook wrote is the verdict as well as the only account of why a push was
/// refused — and it lives in a pipe until the process ends. Preserving it only where
/// the rules named `gate: {kind: pre-push}` meant no repository on a host whose
/// rules named commands: what a policy calls its verification cannot decide whether
/// a failure is diagnosable.
///
/// Storing it is best effort: the push has already happened, so a state root that
/// would not take the bytes says so on stderr rather than turning a push git
/// accepted into a publication that failed.
///
/// Every publishing push in this crate goes through here — the merge train's push of
/// its advanced base included, because that push is where the hook rules on a train.
/// One producer, so a second one cannot come to emit a thinner `push` event than the
/// contract says a push verdict is.
///
/// `preserve_under` is the run root whose per-branch directory keeps a copy that
/// outlives the tree the push was built in, and is `None` for a caller whose scratch
/// workspace is removed when it returns — there the stored artifact is what persists,
/// and a copy under a directory about to go would only look like evidence.
///
/// `phase` is which branch this push updated, which is the one thing about a push
/// that its own payload cannot say: `branch` here names the work being published,
/// and a `local-direct` squash and a merge train both push that work onto the *base*
/// under it. So the caller that made the push says whether it was the session's own
/// branch — the work being made, [`Phase::Development`] — or another, which is that
/// work being integrated.
pub(crate) fn record_push(
    stream: &mut Stream,
    branch: &Ref,
    pushed: &git::Pushed,
    preserve_under: Option<&Path>,
    phase: Phase,
    head: Option<&str>,
) -> Result<Kept> {
    let output = pushed.output();
    let stored = stream::store_artifact("log", output);
    let kept = preserve_under.map(|root| merge_path::preserve_log(root, branch, output));
    for error in [
        stored.as_ref().err(),
        kept.as_ref().and_then(|kept| kept.as_ref().err()),
    ]
    .into_iter()
    .flatten()
    {
        eprintln!(
            "onevcs: warning: the push of {branch:?} is recorded without what it wrote: {error}"
        );
    }
    let artifact = stored.ok();
    let mut payload = object(json!({
        "branch": branch,
        "remote": "origin",
        "accepted": pushed.accepted(),
        "output": output,
    }));
    if let Some(Ok(preserved)) = &kept {
        payload.insert(
            "preserved_log".to_owned(),
            json!(preserved.display().to_string()),
        );
    }
    // The commit a push of the branch's own name put there, which is the head a change
    // request opened from it carries — and what a retirement later asks the branch's
    // content to be contained in. Only the push of the branch itself names one: a push
    // of the base under it lands a commit no change request is opened from.
    if let Some(head) = head {
        payload.insert("head".to_owned(), json!(head));
    }
    let evidence = Kept {
        artifact: artifact
            .as_ref()
            .map(|stored| crate::event::ArtifactId(stored.id.clone())),
        preserved: match kept {
            Some(Ok(preserved)) => Some(preserved),
            Some(Err(_)) | None => None,
        },
    };
    stream.emit_push(phase, payload, artifact.into_iter().collect());
    Ok(evidence)
}

/// Where a recorded push's output can be read once the push is over.
///
/// Handed back rather than only written, because the refusal that reports a push
/// the merge path turned down is read by whoever has to diagnose it — and until it
/// named these, an operator was told three lines of git's generic message while the
/// whole verification sat in two places nothing pointed at. An hour went on
/// rediscovering four redundant comment lines that way.
///
/// Either may be absent and both may be: the artifact is missing where the state
/// root would not take the bytes, and the preserved copy where the caller had no
/// run root to outlive its tree or the write failed. A refusal that has neither
/// must say there is nothing to read rather than name a place that holds nothing.
pub(crate) struct Kept {
    /// The artifact the whole output was stored as, fetched with `onevcs artifact
    /// cat`.
    artifact: Option<crate::event::ArtifactId>,
    /// The file under the run root that outlives the tree the push was built in.
    preserved: Option<PathBuf>,
}

impl Kept {
    /// Where the whole of what the push wrote can be read, as a refusal says it.
    ///
    /// Both when both are there, because they answer different questions: the
    /// artifact survives the run root being reaped and is fetched through this
    /// crate's own CLI, and the preserved file is a path an operator can open
    /// directly, beside every other attempt on the same branch.
    fn evidence(&self) -> String {
        let artifact = self.artifact.as_ref().map(|id| {
            format!(
                "artifact {id} — `{command}`",
                id = id.0,
                command = guidance::command(["onevcs", "artifact", "cat", &id.0]),
            )
        });
        let preserved = self
            .preserved
            .as_ref()
            .map(|path| format!("preserved at {}", path.display()));
        match (artifact, preserved) {
            (Some(artifact), Some(preserved)) => {
                format!(" Its whole output is {artifact}, and {preserved}.")
            }
            (Some(one), None) | (None, Some(one)) => format!(" Its whole output is {one}."),
            // Both stores refused the bytes, and each said why on stderr as it
            // happened. Naming neither is the point: a path that holds nothing sends
            // an operator to look at it.
            (None, None) => String::new(),
        }
    }

    /// Whether the output was kept anywhere at all.
    fn anywhere(&self) -> bool {
        self.artifact.is_some() || self.preserved.is_some()
    }
}

/// The publication checkout must be clean and on its root branch before anything
/// is published onto it; a safety clone never makes an arbitrary active branch the
/// fast-forward target.
fn require_publication_checkout_ready(publication: &Path, base: &str) -> Result<()> {
    let current = git::current_branch(publication)?;
    if current != base {
        return Err(Error::Invalid {
            reason: format!(
                "the publication checkout {} has {current:?} checked out, not the base {base:?}; \
                 it is never worked in and only ever fast-forwarded",
                publication.display()
            ),
        });
    }
    if git::is_dirty(publication)? {
        return Err(Error::Invalid {
            reason: format!(
                "the publication checkout {} is dirty; it is never worked in",
                publication.display()
            ),
        });
    }
    Ok(())
}

/// Advance the publication checkout, and only ever forwards.
pub fn fast_forward_publication(publication: &Path, base: &str) -> Result<()> {
    if !git::has_remote(publication, "origin") {
        return Ok(());
    }
    git::fetch(publication, "origin")?;
    if git::current_branch(publication)? != base {
        return Ok(());
    }
    git::merge_ff_only(publication, &format!("origin/{base}"))
}

/// The host slug a change request is opened against, or the reason there is none.
///
/// Two different failures, deliberately not collapsed: a local identity has no
/// host at all and is asking for the wrong policy, while a hosted identity on a
/// host this build does not speak for is asking for an implementation that has not
/// arrived. The second is the seam `Error::NotImplemented` exists for.
pub(crate) fn change_host(identity: &str) -> Result<String> {
    if let Some(slug) = gh::slug(identity) {
        return Ok(slug);
    }
    let hosted = identity.split('/').count() == 3;
    if hosted {
        return Err(Error::NotImplemented {
            operation: "RemoteHost for a host other than github.com",
        });
    }
    Err(Error::Invalid {
        reason: format!(
            "identity {identity:?} is not a hosted repository, so it cannot publish a change \
             request; a local identity publishes with local-direct"
        ),
    })
}

/// One publication commit message: the subject, then whatever it must carry
/// forward.
pub fn compose_message(subject: &str, trailers: &[String]) -> String {
    if trailers.is_empty() {
        subject.to_owned()
    } else {
        format!("{subject}\n\n{}", trailers.join("\n"))
    }
}

/// The issue-closing lines the commits a squash lands carry, each once.
///
/// Read from the branch's own commits past what it lands on, which after the sync is
/// exactly the set the squash folds into one; see [`closing`](crate::closing) for why
/// these lines, and only these, outlive the squash.
fn closing_lines(context: &Context<'_>, compared: &str) -> Result<Vec<String>> {
    let commits = git::log_messages(&context.repo, compared, &context.branch)?;
    Ok(crate::closing::lines(
        commits.iter().map(|commit| commit.message.as_str()),
    ))
}

/// A squash's message: the subject, the issue-closing lines its commits carried, then
/// the trailers — so a branch with no closing lines lands exactly as
/// [`compose_message`] composes it.
fn compose_squash(subject: &str, closing: &[String], trailers: &[String]) -> String {
    if closing.is_empty() {
        return compose_message(subject, trailers);
    }
    compose_message(&format!("{subject}\n\n{}", closing.join("\n")), trailers)
}

/// The policy a run publishes under, once the rules and any `--policy` have both
/// had their say.
pub fn effective_policy(resolved: &Policy, requested: Option<MergePolicy>) -> Result<MergePolicy> {
    match requested {
        Some(requested) => policy::narrow(resolved, requested),
        None => Ok(resolved.publication),
    }
}

/// The exit code an error reports, as the contract fixes them.
pub fn exit_code(error: &Error) -> u8 {
    FailureKind::of(error).exit_code()
}

/// A session's own record, once publication has decided what it publishes onto.
pub fn preserved_change_base(record_base: &Ref, recorded: Option<&Ref>) -> Ref {
    recorded.unwrap_or(record_base).clone()
}

/// A change request this host opened, stopped watching, and may meet again.
///
/// Everything the question below is asked from, read out of the records this host
/// already holds rather than derived a second way: the event stream's own
/// `change-opened`, and the branch as some copy of it here stands. One value rather
/// than seven arguments, for the reason [`Advance`](crate::status) is one.
pub(crate) struct Watched<'a> {
    /// The identity the work belongs to.
    pub identity: &'a str,
    /// The branch that carries it.
    pub branch: &'a str,
    /// Where a human reads the change request, as `change-opened` recorded it.
    pub url: &'a Url,
    /// The host's own identifier for it, as `change-opened` recorded it.
    pub id: &'a str,
    /// The branch it targets, as `change-opened` recorded it.
    pub base: &'a str,
    /// The commit this host last saw the branch at, which is the head the change
    /// request was opened from.
    pub head: &'a str,
    /// The stream that recorded it being opened, which is where a landing found
    /// after that publication ended is recorded.
    pub stream: Option<&'a str>,
}

/// Ask the host **once** whether a change request this host stopped watching has
/// merged since, and record the landing where it has.
///
/// A `change-auto` publication watches the host for a fixed span and closes
/// [`ChecksUnsettled`](FailureKind::ChecksUnsettled) when that span runs out. The
/// merge often happens seconds later, and nothing afterwards asks: the landing tiers
/// read stored records and the base's history, a `status` deliberately never fetches,
/// and the host is asked only which change requests are *open* — where a merged one
/// is simply absent. So a change request merged five seconds after its last required
/// check went on reading `closed without landing`, and the next attempt republished
/// work the base already carried.
///
/// The question is asked of exactly one state and asked once per read: a change
/// request this host recorded, which it asked the host to land, and for which no
/// landing is recorded — which is what a `checks-unsettled` close leaves behind.
/// [`RemoteHost::merged_at`] answers it, and its three answers are three outcomes: a
/// merge commit is recorded as the landing, `None` and every refusal leave the record
/// exactly as it stands. A change request the host reports open or closed-unmerged is
/// the `None`.
///
/// What a recorded landing then gets is what a witnessed one gets, through the one
/// function that gives it: the publication checkout fast-forwarded, each automated
/// target's baseline captured at the landing commit, and the merge recorded on the
/// stream that opened the change. The caller recomputes from that record, which is
/// what puts the landing into the same read rather than into the next one.
pub(crate) fn reconcile_late_merge(
    registry: &crate::registry::Registry,
    watched: &Watched<'_>,
    hosting: &dyn Hosting,
) -> Option<String> {
    let host = hosting
        .for_repo(&change_host(watched.identity).ok()?)
        .ok()?;
    let change = ChangeRequest {
        id: crate::host::ChangeId(watched.id.to_owned()),
        url: watched.url.clone(),
        head_sha: Sha(watched.head.to_owned()),
        base: watched.base.to_owned(),
    };
    let merged = host.merged_at(&change).ok()??;
    record_late_merge(
        registry,
        watched,
        &merged,
        merge_time(host.as_ref(), &change),
    )
}

/// When the host says a change request this host stopped watching merged — the
/// landing time a late merge is recorded with, which is the host's and never the
/// moment a later read asked. For a caller that already holds the host.
pub(crate) fn late_merge_time(host: &dyn RemoteHost, change: &ChangeRequest) -> Option<Moment> {
    merge_time(host, change)
}

/// Record a merge the host reported for a change request this host stopped watching,
/// the way [`reconcile_late_merge`] records the one it finds — for a caller that asked
/// the host itself, because it has to tell a refusal from "not merged". Answers the
/// landing where it was recorded, and `None` where it could not be, for the reason
/// [`warn_unreconciled`] gives.
///
/// `landed_at` is when the host says it merged, and `None` where the host could not
/// say — never the time of this read, nor one read from anywhere but the host.
pub(crate) fn record_late_merge(
    registry: &crate::registry::Registry,
    watched: &Watched<'_>,
    merged: &Sha,
    landed_at: Option<Moment>,
) -> Option<String> {
    let located = match crate::release::for_repository(registry, watched.identity) {
        Ok(located) => located,
        // Best effort throughout, as every part of recording a landing after the
        // fact is: the change has merged whatever this host can write about it, and
        // refusing the read that found it would be the worse answer.
        Err(failure) => return warn_unreconciled(watched, &merged.0, &failure),
    };
    let mut stream = match Stream::releases(watched.identity) {
        Ok(stream) => stream,
        Err(failure) => return warn_unreconciled(watched, &merged.0, &failure),
    };
    let _ = crate::release::reconcile_landing(
        registry,
        &located,
        watched.branch,
        watched.stream.map(crate::release::LandingStream::Opened),
        crate::release::Landing {
            commit: &merged.0,
            at: landed_at,
        },
        watched.url,
        &mut stream,
    );
    Some(merged.0.clone())
}

/// Say on stderr that a merge the host reported could not be recorded here, and
/// answer as though it had not been found.
///
/// Answering `None` is the point: the caller goes on to decide the landing from the
/// records as they stand, which is what they still say. Reporting the merge without
/// having recorded it would leave a read answering `landed` that the next read,
/// reading the same records, answers `closed` — one landing, two answers.
fn warn_unreconciled(
    watched: &Watched<'_>,
    commit: &str,
    failure: &dyn std::fmt::Display,
) -> Option<String> {
    eprintln!(
        "onevcs: warning: the host reports {url} merged at {commit}, and this host could not \
         record that landing for {branch} ({failure}); `onevcs status {branch}` still answers \
         from what is recorded",
        url = watched.url,
        branch = watched.branch,
    );
    None
}

/// Take up a merge the host performed after a publication of this branch stopped
/// watching for it, before this one decides what there is to publish.
///
/// The third of the three reads that meet such a change request, and it asks through
/// the *same* path the other two ask through — `status`'s landing read — so a
/// publication and a status cannot come to two answers about one change request.
/// What that read does with a merge the host reports is recorded on
/// [`reconcile_late_merge`]: the landing is written, the publication checkout is
/// fast-forwarded, and each automated target's baseline is captured at the merge
/// commit. The fetch below then brings the merge into this publication's own
/// comparison, which is what answers `nothing to publish` instead of opening a
/// second change request for work already on the base.
///
/// Best effort, and deliberately silent about its own failures: everything it could
/// establish is established again by the comparison this publication is about to
/// make, and a publication refused because a question about the host could not be
/// asked would be a worse answer than one that simply asks nobody.
fn take_up_a_late_merge(context: &Context<'_>) {
    let Ok(registry) = store::load() else {
        return;
    };
    let _ = crate::status::landing_of_within(
        &registry,
        &context.branch,
        Some(&context.resolution.key),
        Some(context.hosting),
    );
}