rch 2.1.25

Remote Compilation Helper - PreToolUse hook CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
//! Durable source ownership and identity-bound collection. Never replays a command.
use super::super::artifact_patterns::direct_compiler::{
    NativeOutputContract, native_output_contract,
};
use super::super::cargo_output_contract::{
    CargoCallerPaths, CargoOutputCapture, CargoOutputContract,
};
use super::*;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};

#[cfg(unix)]
#[path = "publication_copy.rs"]
mod publication_copy;
#[cfg(unix)]
use publication_copy::prepare_output_temporary;

#[path = "recovery_completion.rs"]
mod recovery_completion;
#[path = "recovery_owner.rs"]
pub(in crate::hook) mod recovery_owner;
#[path = "result_recovery.rs"]
mod result_recovery;

#[derive(Clone, Serialize, Deserialize)]
pub(crate) struct RecoveryRecipe {
    version: u32,
    wrapper_id: String,
    build_id: u64,
    worker: WorkerConfig,
    identity: String,
    completion: String,
    source_roots: Vec<String>,
    pair: Option<(String, String)>,
    retire_root: Option<String>,
    /// An owned source pair retires by moving to this exact sibling. Persist
    /// both names before acquisition so recovery never invents new authority.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    retained_root: Option<String>,
    /// True acknowledges a durable retained tree. False records preparation
    /// that never materialized source; it is not a retained-source receipt.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    retained_materialized: Option<bool>,
    transfer: TransferConfig,
    project_root: PathBuf,
    kind: Option<CompilationKind>,
    expected_triple: String,
    pinned_triple: Option<String>,
    allow_foreign: bool,
    package_archive: bool,
    /// Persisted before execution: a successful captured build cannot publish
    /// until its complete, same-attempt Cargo receipt supplies the output set.
    #[serde(default)]
    cargo_output_capture: Option<CargoOutputCapture>,
    /// Complete same-attempt stdout retained outside disposable staging trees.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    cargo_stdout: Option<RetainedCargoStdout>,
    /// Complete diagnostics retained before retirement can remove the log.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    cargo_stderr: Option<RetainedCargoStderr>,
    /// Persist the caller's output contract before execution so detached
    /// recovery cannot downgrade it to ordinary glob-only Cargo delivery.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    caller_json_policy: Option<PathBuf>,
    /// Persist the first caller-visible paths and bytes before worker release.
    /// Recovery must not project a later build's shared target files instead.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    caller_delivery: Option<RetainedCargoDelivery>,
    phases: Vec<RecoveryPhase>,
    exit_code: Option<i32>,
    returned: Option<i32>,
    /// Daemon accounting may have completed earlier through cancellation.
    /// Preserve its outcome independently of command and artifact delivery.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    daemon_exit_code: Option<i32>,
    #[serde(default)]
    prepared: bool,
    #[serde(default)]
    execution_started: bool,
    #[serde(default)]
    preparation_cancelled: bool,
    #[serde(default)]
    sources_released: bool,
    #[serde(default)]
    tree_retired: bool,
    #[serde(default)]
    pair_released: bool,
    #[serde(default)]
    retired: bool,
}

#[derive(Clone, Serialize, Deserialize)]
struct RetainedCargoStdout {
    path: PathBuf,
    blake3: String,
    bytes: u64,
    remote_root: PathBuf,
    // Ordinary artifact evidence does not observe the physical source root.
    // Old caller-JSON receipts deserialize their existing path as Some(path).
    #[serde(default)]
    remote_project_root: Option<PathBuf>,
    exit_code: i32,
}

#[derive(Clone, Serialize, Deserialize)]
struct RetainedCargoStderr {
    path: PathBuf,
    blake3: String,
    bytes: u64,
    exit_code: i32,
}

impl RetainedCargoStderr {
    fn read(&self) -> anyhow::Result<Vec<u8>> {
        let limit = super::super::cargo_output_contract::MAX_CARGO_OUTPUT_RECEIPT_BYTES;
        anyhow::ensure!(
            self.bytes <= limit as u64,
            "retained Cargo stderr exceeds its bound"
        );
        let metadata = std::fs::symlink_metadata(&self.path)?;
        anyhow::ensure!(
            metadata.is_file() && !metadata.file_type().is_symlink(),
            "retained Cargo stderr is not a regular file"
        );
        let mut file = File::open(&self.path)?.take(limit as u64 + 1);
        let mut bytes = Vec::new();
        file.read_to_end(&mut bytes)?;
        anyhow::ensure!(
            bytes.len() as u64 == self.bytes
                && blake3::hash(&bytes).to_hex().to_string() == self.blake3,
            "retained Cargo stderr failed its exact byte binding"
        );
        Ok(bytes)
    }
}

#[derive(Clone, Serialize, Deserialize)]
struct RetainedCargoDelivery {
    path: PathBuf,
    blake3: String,
    bytes: u64,
    producer_blake3: String,
    output_root: PathBuf,
    files: BTreeMap<PathBuf, String>,
}

impl RetainedCargoDelivery {
    fn read(&self, producer: &RetainedCargoStdout) -> anyhow::Result<Vec<u8>> {
        // Re-read the real producer too. A stored hash string alone cannot
        // replace the immutable same-attempt receipt that authorized delivery.
        producer.read()?;
        anyhow::ensure!(
            self.producer_blake3 == producer.blake3 && producer.exit_code == 0,
            "caller Cargo delivery contradicts its producer receipt"
        );
        let limit = super::super::cargo_output_contract::MAX_CARGO_OUTPUT_RECEIPT_BYTES
            .checked_mul(2)
            .context("caller Cargo JSON size limit overflow")?;
        anyhow::ensure!(
            self.bytes <= limit as u64,
            "retained caller Cargo JSON exceeds its bound"
        );
        let metadata = std::fs::symlink_metadata(&self.path)?;
        anyhow::ensure!(
            metadata.is_file() && !metadata.file_type().is_symlink(),
            "retained caller Cargo JSON is not a regular file"
        );
        let mut file = File::open(&self.path)?.take(limit as u64 + 1);
        let mut bytes = Vec::new();
        file.read_to_end(&mut bytes)?;
        anyhow::ensure!(
            bytes.len() as u64 == self.bytes
                && blake3::hash(&bytes).to_hex().to_string() == self.blake3,
            "retained caller Cargo JSON failed its exact byte binding"
        );
        CargoOutputContract {
            required_files: self.files.keys().cloned().collect(),
        }
        .verify_staged(&self.output_root)?;
        for (relative, expected) in &self.files {
            anyhow::ensure!(
                fingerprint(&self.output_root.join(relative))?.as_ref() == Some(expected),
                "retained caller Cargo output changed: {}",
                relative.display()
            );
        }
        Ok(bytes)
    }
}

fn retain_cargo_bytes(wrapper: &str, label: &str, bytes: &[u8]) -> anyhow::Result<PathBuf> {
    let path =
        default_job_lease_directory().join(format!("{wrapper}.{label}-{}", uuid::Uuid::new_v4()));
    let mut options = OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let mut file = options.open(&path)?;
    file.write_all(bytes)?;
    file.sync_all()?;
    File::open(
        path.parent()
            .context("retained Cargo bytes have no parent")?,
    )?
    .sync_all()?;
    Ok(path)
}

impl RetainedCargoStdout {
    fn read(&self) -> anyhow::Result<Vec<u8>> {
        let limit = super::super::cargo_output_contract::MAX_CARGO_OUTPUT_RECEIPT_BYTES;
        anyhow::ensure!(
            self.bytes <= limit as u64,
            "retained Cargo stdout exceeds its receipt bound"
        );
        let metadata = std::fs::symlink_metadata(&self.path)
            .context("retained Cargo stdout producer metadata is unavailable")?;
        anyhow::ensure!(
            metadata.is_file() && !metadata.file_type().is_symlink(),
            "retained Cargo stdout is not a regular file"
        );
        // The pathname check alone races a symlink/FIFO replacement. On Unix,
        // open the leaf without following it or blocking on a special file.
        #[cfg(unix)]
        let file = {
            use rustix::fs::{Mode, OFlags};
            File::from(rustix::fs::open(
                &self.path,
                OFlags::RDONLY
                    | OFlags::CLOEXEC
                    | OFlags::NOFOLLOW
                    | OFlags::NONBLOCK
                    | OFlags::NOCTTY,
                Mode::empty(),
            )?)
        };
        #[cfg(not(unix))]
        let file = File::open(&self.path)?;
        anyhow::ensure!(
            file.metadata()?.is_file(),
            "retained Cargo stdout is not a regular file"
        );
        let mut file = file.take(limit as u64 + 1);
        let mut stdout = Vec::new();
        file.read_to_end(&mut stdout)?;
        anyhow::ensure!(
            stdout.len() as u64 == self.bytes
                && blake3::hash(&stdout).to_hex().to_string() == self.blake3,
            "retained Cargo stdout bytes failed their exact invocation binding"
        );
        Ok(stdout)
    }
}

#[derive(Clone, Serialize, Deserialize)]
struct RecoveryPhase {
    name: String,
    local: PathBuf,
    remote: String,
    patterns: Vec<String>,
    result_dir: Option<PathBuf>,
    custom_target: bool,
    output_gate: bool,
    #[serde(default)]
    cargo_outputs: Option<CargoOutputContract>,
    /// Exact native-driver files, known before execution and independent of
    /// Cargo's target directory or target-root output gate.
    #[serde(default)]
    native_outputs: Option<NativeOutputContract>,
    baseline: BTreeMap<PathBuf, String>,
    published: BTreeMap<PathBuf, String>,
    #[serde(default)]
    pending: Option<(PathBuf, String)>,
    complete: bool,
    /// A failed delivery is terminal evidence, not a completed publication.
    #[serde(default)]
    missing_result: Option<result_recovery::MissingResult>,
}

pub(crate) struct RecoverySession {
    recipe: RecoveryRecipe,
    writer: DurableLeaseWriter,
}

/// An immutable, complete receipt cannot satisfy this invocation's contract.
/// Keep this separate from local journal I/O, which a collector may retry.
#[derive(Debug, thiserror::Error)]
#[error("invalid Cargo executable evidence: {0:#}")]
pub(super) struct CargoOutputContractRejected(anyhow::Error);

fn fingerprint(path: &Path) -> anyhow::Result<Option<String>> {
    let metadata = match std::fs::symlink_metadata(path) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => return Err(error.into()),
    };
    anyhow::ensure!(
        metadata.is_file() && !metadata.file_type().is_symlink(),
        "output is not a regular file: {}",
        path.display()
    );
    let mut file = File::open(path)?;
    let mut hasher = blake3::Hasher::new();
    let mut bytes = [0u8; 65536];
    loop {
        let count = file.read(&mut bytes)?;
        if count == 0 {
            break;
        }
        hasher.update(&bytes[..count]);
    }
    Ok(Some(hasher.finalize().to_hex().to_string()))
}

/// The caller may choose a symlinked root, but descendants must not redirect
/// either normal publication or a journaled pending output outside that root.
fn validate_output_ancestors(root: &Path, relative: &Path) -> anyhow::Result<()> {
    anyhow::ensure!(
        !relative.as_os_str().is_empty()
            && relative
                .components()
                .all(|part| matches!(part, std::path::Component::Normal(_))),
        "output publication requires a root-relative file path"
    );
    let destination = root.join(relative);
    let mut ancestor = destination.parent();
    while let Some(path) = ancestor {
        if path == root {
            break;
        }
        anyhow::ensure!(
            path.starts_with(root),
            "output ancestor escaped its phase root"
        );
        match std::fs::symlink_metadata(path) {
            Ok(metadata) => {
                anyhow::ensure!(
                    !metadata.file_type().is_symlink(),
                    "output ancestor is a symlink: {}",
                    path.display()
                );
                anyhow::ensure!(
                    metadata.is_dir(),
                    "output ancestor is not a directory: {}",
                    path.display()
                );
            }
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => {
                return Err(error)
                    .with_context(|| format!("inspect output ancestor {}", path.display()));
            }
        }
        ancestor = path.parent();
    }
    Ok(())
}

fn copy_published_output(
    source_path: &Path,
    destination: &Path,
    expected: &str,
) -> anyhow::Result<File> {
    let mut source = File::open(source_path)?;
    let metadata = source.metadata()?;
    anyhow::ensure!(
        metadata.is_file(),
        "published Cargo output is not a regular file"
    );
    let length = metadata.len();
    let bound = length
        .checked_add(1)
        .context("published Cargo output length overflow")?;
    std::fs::create_dir_all(
        destination
            .parent()
            .context("caller Cargo output has no parent")?,
    )?;
    let mut options = OpenOptions::new();
    options.read(true).write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    let mut copy = options.open(destination)?;
    let mut hasher = blake3::Hasher::new();
    let mut buffer = [0u8; 65536];
    let mut count_total = 0u64;
    let mut reader = (&mut source).take(bound);
    loop {
        let count = reader.read(&mut buffer)?;
        if count == 0 {
            break;
        }
        count_total += count as u64;
        hasher.update(&buffer[..count]);
        copy.write_all(&buffer[..count])?;
    }
    anyhow::ensure!(
        count_total == length && hasher.finalize().to_hex().to_string() == expected,
        "published Cargo output changed before stdout delivery: {}",
        source_path.display()
    );
    copy.sync_all()?;
    copy.set_permissions(metadata.permissions())?;
    copy.sync_all()?;
    Ok(copy)
}

/// Keep the original directory and file alive through checkpoint publication.
/// Names are only accepted when they still identify these held descriptors.
struct PreparedOutput {
    file: File,
    directory: File,
    parent: PathBuf,
    temporary: PathBuf,
    expected_hash: String,
    length: u64,
    #[cfg(unix)]
    mode: u32,
}

impl PreparedOutput {
    fn existing(directory: File, temporary: &Path, expected: &str) -> anyhow::Result<Self> {
        #[cfg(unix)]
        let file = {
            use rustix::fs::{Mode, OFlags};
            use std::os::fd::AsFd;
            File::from(rustix::fs::openat(
                directory.as_fd(),
                temporary
                    .file_name()
                    .context("output temporary has no name")?,
                OFlags::RDONLY
                    | OFlags::CLOEXEC
                    | OFlags::NOFOLLOW
                    | OFlags::NONBLOCK
                    | OFlags::NOCTTY,
                Mode::empty(),
            )?)
        };
        #[cfg(not(unix))]
        let file = File::open(temporary)?;
        let metadata = file.metadata()?;
        anyhow::ensure!(
            metadata.is_file(),
            "retained temporary is not a regular file"
        );
        let prepared = Self::hold(file, directory, temporary, expected)?;
        prepared.file.sync_all()?;
        Ok(prepared)
    }

    fn hold(file: File, directory: File, temporary: &Path, expected: &str) -> anyhow::Result<Self> {
        let length = file.metadata()?.len();
        #[cfg(unix)]
        let mode = {
            use std::os::unix::fs::MetadataExt;
            file.metadata()?.mode() & 0o7777
        };
        let prepared = Self {
            file,
            directory,
            parent: temporary
                .parent()
                .context("output temporary has no parent")?
                .to_owned(),
            temporary: temporary.to_owned(),
            expected_hash: expected.to_owned(),
            length,
            #[cfg(unix)]
            mode,
        };
        prepared.verify_name(temporary)?;
        prepared.verify_content()?;
        Ok(prepared)
    }

    fn verify_content(&self) -> anyhow::Result<()> {
        anyhow::ensure!(
            self.file.metadata()?.len() == self.length,
            "prepared output length changed"
        );
        let bound = self
            .length
            .checked_add(1)
            .context("temporary output length overflow")?;
        let mut hasher = blake3::Hasher::new();
        let mut buffer = [0u8; 65536];
        let mut offset = 0u64;
        #[cfg(not(unix))]
        let mut reader = {
            use std::io::{Seek, SeekFrom};
            let mut reader = self.file.try_clone()?;
            reader.seek(SeekFrom::Start(0))?;
            reader
        };
        while offset < bound {
            let size = (bound - offset).min(buffer.len() as u64) as usize;
            #[cfg(unix)]
            let count = {
                use std::os::unix::fs::FileExt;
                // Cloned file handles share their cursor. Positional reads
                // always hash the held bytes from zero without moving it.
                self.file.read_at(&mut buffer[..size], offset)?
            };
            #[cfg(not(unix))]
            let count = reader.read(&mut buffer[..size])?;
            if count == 0 {
                break;
            }
            hasher.update(&buffer[..count]);
            offset += count as u64;
        }
        anyhow::ensure!(
            offset == self.length
                && self.file.metadata()?.len() == self.length
                && hasher.finalize().to_hex().as_str() == self.expected_hash,
            "prepared output bytes changed: {}; publication unverified",
            self.temporary.display()
        );
        Ok(())
    }

    fn verify_name(&self, path: &Path) -> anyhow::Result<()> {
        anyhow::ensure!(
            path.parent() == Some(self.parent.as_path()),
            "output publication parent changed"
        );
        anyhow::ensure!(
            self.file.metadata()?.is_file(),
            "prepared output is not a regular file"
        );
        #[cfg(unix)]
        {
            use rustix::fs::{Mode, OFlags};
            use std::os::fd::AsFd;
            use std::os::unix::fs::MetadataExt;
            let held_directory = self.directory.metadata()?;
            // The caller may choose a symlinked output root; compare its
            // physical referent with the directory held before preparation.
            let named_directory = std::fs::metadata(&self.parent)?;
            anyhow::ensure!(
                held_directory.is_dir()
                    && named_directory.is_dir()
                    && held_directory.dev() == named_directory.dev()
                    && held_directory.ino() == named_directory.ino(),
                "output publication directory changed"
            );
            let named = File::from(rustix::fs::openat(
                self.directory.as_fd(),
                path.file_name().context("output publication has no name")?,
                OFlags::RDONLY
                    | OFlags::CLOEXEC
                    | OFlags::NOFOLLOW
                    | OFlags::NONBLOCK
                    | OFlags::NOCTTY,
                Mode::empty(),
            )?);
            let held = self.file.metadata()?;
            let named = named.metadata()?;
            anyhow::ensure!(
                held.is_file()
                    && named.is_file()
                    && held.dev() == named.dev()
                    && held.ino() == named.ino()
                    && held.nlink() == 1
                    && named.nlink() == 1
                    && held.mode() & 0o7777 == self.mode
                    && named.mode() & 0o7777 == self.mode,
                "prepared output identity, permissions or links changed"
            );
        }
        Ok(())
    }

    fn publish(&self, destination: &Path) -> anyhow::Result<()> {
        self.verify_name(&self.temporary)?;
        self.verify_content()?;
        anyhow::ensure!(
            destination.parent() == Some(self.parent.as_path()),
            "output destination parent changed"
        );
        #[cfg(unix)]
        {
            use std::os::fd::AsFd;
            rustix::fs::renameat(
                self.directory.as_fd(),
                self.temporary
                    .file_name()
                    .context("output temporary has no name")?,
                self.directory.as_fd(),
                destination
                    .file_name()
                    .context("output destination has no name")?,
            )?;
        }
        #[cfg(not(unix))]
        std::fs::rename(&self.temporary, destination)?;
        self.verify_name(destination)?;
        self.directory.sync_all()?;
        self.verify_name(destination)?;
        self.verify_content()
    }
}

/// A failed pending checkpoint can leave this wrapper's unjournaled copy.
/// Reuse only the exact staged bytes and permissions; never truncate a prior
/// temporary file or discard a conflicting/partial copy to force a retry.
#[cfg(not(unix))]
fn prepare_output_temporary(
    source: &Path,
    temporary: &Path,
    expected: &str,
) -> anyhow::Result<PreparedOutput> {
    let parent = temporary
        .parent()
        .context("output temporary has no parent")?;
    std::fs::create_dir_all(parent)?;
    let directory = File::open(parent)?;
    anyhow::ensure!(
        directory.metadata()?.is_dir(),
        "output parent is not a directory"
    );
    match std::fs::symlink_metadata(temporary) {
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            let file = copy_published_output(source, temporary, expected)?;
            PreparedOutput::hold(file, directory, temporary, expected)
        }
        Err(error) => Err(error.into()),
        Ok(metadata) => {
            anyhow::ensure!(
                metadata.is_file() && !metadata.file_type().is_symlink(),
                "retained output temporary is not a regular file: {}",
                temporary.display()
            );
            let source_metadata = std::fs::symlink_metadata(source)?;
            anyhow::ensure!(
                source_metadata.is_file(),
                "staged output is not a regular file"
            );
            #[cfg(unix)]
            {
                use std::os::unix::fs::MetadataExt;
                anyhow::ensure!(
                    metadata.nlink() == 1
                        && metadata.mode() & 0o7777 == source_metadata.mode() & 0o7777,
                    "retained output temporary permissions or links changed: {}",
                    temporary.display()
                );
            }
            #[cfg(not(unix))]
            anyhow::ensure!(
                metadata.permissions().readonly() == source_metadata.permissions().readonly(),
                "retained output temporary permissions changed"
            );
            let prepared = PreparedOutput::existing(directory, temporary, expected)?;
            #[cfg(unix)]
            {
                use std::os::unix::fs::MetadataExt;
                anyhow::ensure!(
                    prepared.mode == source_metadata.mode() & 0o7777,
                    "held retained temporary permissions differ from staged output"
                );
            }
            Ok(prepared)
        }
    }
}

// Unix publication reads the stage through `staged_inventory` instead.
#[cfg(any(not(unix), test))]
fn regular_files(root: &Path) -> anyhow::Result<Vec<PathBuf>> {
    fn visit(root: &Path, directory: &Path, files: &mut Vec<PathBuf>) -> anyhow::Result<()> {
        if !directory.exists() {
            return Ok(());
        }
        for entry in std::fs::read_dir(directory)? {
            let entry = entry?;
            let kind = entry.file_type()?;
            anyhow::ensure!(
                !kind.is_symlink(),
                "symlink in recovery output: {}",
                entry.path().display()
            );
            if kind.is_dir() {
                visit(root, &entry.path(), files)?;
            } else if kind.is_file() {
                files.push(entry.path().strip_prefix(root)?.to_owned());
            } else {
                anyhow::bail!("non-regular recovery output: {}", entry.path().display());
            }
        }
        Ok(())
    }
    let mut files = Vec::new();
    visit(root, root, &mut files)?;
    files.sort();
    Ok(files)
}

/// How long a publication waits for another wrapper's publication into the
/// same local output root. Publications only move already-staged files, so
/// contention lasts seconds; the bound turns a wedged holder into an error.
const OUTPUT_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(600);

/// Different spellings of one output directory must share publication
/// ownership. Resolve existing prefixes without creating a fresh target tree;
/// missing suffixes keep the same identity after another job creates them.
/// Resolve a symlink before applying a following `..`, as the filesystem does.
fn output_lock_root_identity(root: &Path) -> anyhow::Result<PathBuf> {
    use std::path::Component;

    let absolute = if root.is_absolute() {
        root.to_owned()
    } else {
        std::env::current_dir()?.join(root)
    };
    let mut resolved = PathBuf::new();
    for component in absolute.components() {
        match component {
            Component::Prefix(_) => anyhow::bail!(
                "output publication requires a Unix directory path: {}",
                root.display()
            ),
            Component::RootDir => resolved.push(component.as_os_str()),
            Component::CurDir => {}
            Component::ParentDir => {
                resolved.pop();
            }
            Component::Normal(name) => {
                resolved.push(name);
                match std::fs::canonicalize(&resolved) {
                    Ok(canonical) => {
                        anyhow::ensure!(
                            std::fs::metadata(&canonical)?.is_dir(),
                            "output publication root has a non-directory component: {}",
                            resolved.display()
                        );
                        resolved = canonical;
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                        // A missing directory is allowed, but a dangling
                        // symlink is not an ordinary missing suffix: treating
                        // it as one would assign a different lock from its
                        // eventual target when that target appears.
                        match std::fs::symlink_metadata(&resolved) {
                            Err(missing) if missing.kind() == std::io::ErrorKind::NotFound => {}
                            Ok(_) => {
                                return Err(error).with_context(|| {
                                    format!(
                                        "cannot resolve existing output root entry {}",
                                        resolved.display()
                                    )
                                });
                            }
                            Err(error) => return Err(error.into()),
                        }
                    }
                    Err(error) => return Err(error.into()),
                }
            }
        }
    }
    Ok(resolved)
}

/// Exclusive ownership of one local output root for the duration of a single
/// phase publication. It is deliberately NOT held across the remote build:
/// dispatchers share one CARGO_TARGET_DIR (and run concurrent jobs in one
/// project), and a job-long lock failed every overlapping build outright.
async fn lock_output_root(root: &Path) -> anyhow::Result<File> {
    let identity = output_lock_root_identity(root)?;
    let directory = default_job_lease_directory().join("output-locks");
    std::fs::create_dir_all(&directory)?;
    let name = blake3::hash(identity.as_os_str().as_encoded_bytes())
        .to_hex()
        .to_string();
    let file = OpenOptions::new()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .open(directory.join(name))?;
    let deadline = tokio::time::Instant::now() + OUTPUT_LOCK_WAIT;
    loop {
        match file.try_lock() {
            Ok(()) => {
                anyhow::ensure!(
                    output_lock_root_identity(root)? == identity,
                    "output root changed while waiting for publication ownership: {}; journal retained",
                    root.display()
                );
                return Ok(file);
            }
            Err(std::fs::TryLockError::WouldBlock) if tokio::time::Instant::now() < deadline => {
                // The CLI uses a current-thread runtime. Blocking here stalls
                // heartbeats and prevents the caller's recovery deadline or
                // cancellation from being polled for the whole lock budget.
                // No output or journal has been touched while waiting, so a
                // dropped waiter can safely retry this exact publication.
                let next_poll = tokio::time::Instant::now() + std::time::Duration::from_millis(100);
                tokio::time::sleep_until(next_poll.min(deadline)).await;
            }
            Err(error) => anyhow::bail!(
                "output publication into {} is held by another wrapper: {error}",
                root.display()
            ),
        }
    }
}

fn validate_retained_source_intent(
    source_roots: &[String],
    pair: Option<&(String, String)>,
    retire_root: Option<&str>,
    retained_root: Option<&str>,
    identity: &str,
) -> anyhow::Result<()> {
    let Some(retained) = retained_root else {
        return Ok(());
    };
    let root = retire_root.context("retained source intent has no original source root")?;
    anyhow::ensure!(
        identity.len() == 32
            && identity
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)),
        "retained source identity must be 32 lowercase hexadecimal characters"
    );
    anyhow::ensure!(
        retained == format!("{root}.retained-{identity}")
            && source_roots.iter().any(|path| path == root)
            && source_roots.iter().any(|path| path == retained)
            && pair.is_some_and(|(pair_root, token)| pair_root == root && token == identity),
        "retained source intent must bind the exact pair and both sibling authorities"
    );
    super::super::ssh::validate_source_authority_intent(
        &[root.to_owned(), retained.to_owned()],
        identity,
    )
}

impl RecoverySession {
    /// Save the recovery identity before even a queued remote grant can exist.
    /// Until `starting_execution` is durable, recovery can drain transfers and
    /// cancel this exact preparation without replaying a compiler command.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn begin(
        writer: &DurableLeaseWriter,
        worker: &WorkerConfig,
        source_roots: Vec<String>,
        pair: Option<(String, String)>,
        retire_root: Option<String>,
        retained_root: Option<String>,
        transfer: TransferConfig,
        project_root: PathBuf,
        identity: String,
    ) -> anyhow::Result<()> {
        let lease = writer.snapshot();
        let build_id = lease
            .identity
            .remote_build_id
            .context("source ownership requires admitted build identity")?;
        anyhow::ensure!(
            lease.worker_id.as_deref() == Some(worker.id.as_str()) && lease.recovery.is_none(),
            "source ownership requires a fresh admitted lease; recover the previous attempt first"
        );
        super::super::ssh::validate_source_authority_intent(&source_roots, &identity)?;
        validate_retained_source_intent(
            &source_roots,
            pair.as_ref(),
            retire_root.as_deref(),
            retained_root.as_deref(),
            &identity,
        )?;
        let completion = format!(
            "{}/recovery-{}-{}.done",
            transfer.remote_base.trim_end_matches('/'),
            build_id,
            identity
        );
        let recipe = RecoveryRecipe {
            version: 2,
            wrapper_id: lease.identity.local_wrapper_id,
            build_id,
            worker: worker.clone(),
            identity,
            completion,
            source_roots,
            pair,
            retire_root,
            retained_root,
            retained_materialized: None,
            transfer,
            project_root,
            kind: None,
            expected_triple: String::new(),
            pinned_triple: None,
            allow_foreign: false,
            package_archive: false,
            cargo_output_capture: None,
            cargo_stdout: None,
            cargo_stderr: None,
            caller_json_policy: None,
            caller_delivery: None,
            phases: Vec::new(),
            exit_code: None,
            returned: None,
            daemon_exit_code: None,
            prepared: false,
            execution_started: false,
            preparation_cancelled: false,
            sources_released: false,
            tree_retired: false,
            pair_released: false,
            retired: false,
        };
        writer.set_recovery(serde_json::to_value(recipe)?)
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn prepare(
        writer: &DurableLeaseWriter,
        worker: &WorkerConfig,
        pipeline: &TransferPipeline,
        source_roots: Vec<String>,
        pair: Option<(String, String)>,
        retire_root: Option<String>,
        retained_root: Option<String>,
        transfer: TransferConfig,
        project_root: PathBuf,
        target: Option<&Path>,
        kind: Option<CompilationKind>,
        command: &str,
        result_dirs: &[PathBuf],
        identity: String,
    ) -> anyhow::Result<Self> {
        // Persist the output contract, not runtime-test policy: recovery is
        // collection-only and has no command to reparse after the wrapper dies.
        // The phase patterns below take the command's OWN kind, exactly as the
        // live retrieval does: from the delivery kind they lose the build-only
        // test/bench carve-out, and publication then skipped the very
        // executables the live retrieval fetched (bd-b7lot, seen live).
        let delivery_kind = artifact_delivery_kind(kind, Some(command));
        let lease = writer.snapshot();
        let intent: RecoveryRecipe = serde_json::from_value(
            lease
                .recovery
                .clone()
                .context("output preparation requires the persisted source intent")?,
        )?;
        anyhow::ensure!(
            intent.version == 2
                && intent.identity == identity
                && intent.wrapper_id == lease.identity.local_wrapper_id
                && Some(intent.build_id) == lease.identity.remote_build_id
                && intent.worker.id == worker.id
                && intent.source_roots == source_roots
                && intent.pair == pair
                && intent.retire_root == retire_root
                && intent.retained_root == retained_root
                && intent.retained_materialized.is_none()
                && !intent.tree_retired
                && !intent.execution_started
                && !intent.preparation_cancelled
                && !intent.retired,
            "output preparation cannot replace another source ownership intent"
        );
        let build_id = lease
            .identity
            .remote_build_id
            .context("recovery requires admitted build identity")?;
        let completion = format!(
            "{}/recovery-{}-{}.done",
            transfer.remote_base.trim_end_matches('/'),
            build_id,
            identity
        );
        let mut phases = Vec::new();
        let native_outputs = (!WorkerPlatform::from_worker(worker).is_windows())
            .then(|| native_output_contract(kind, command, true))
            .flatten();
        let project_patterns = match &native_outputs {
            Some(contract) => contract.patterns()?,
            None => get_project_artifact_patterns(kind, Some(command), target.is_some()),
        };
        let native_project_outputs = native_outputs.is_some();
        if !project_patterns.is_empty() {
            phases.push(RecoveryPhase {
                name: "project".into(),
                local: project_root.clone(),
                remote: pipeline.remote_path(),
                patterns: project_patterns,
                result_dir: None,
                custom_target: false,
                output_gate: target.is_none(),
                cargo_outputs: None,
                native_outputs,
                baseline: BTreeMap::new(),
                published: BTreeMap::new(),
                pending: None,
                complete: false,
                missing_result: None,
            });
        }
        if let Some(target) = target.filter(|_| !native_project_outputs) {
            let patterns = get_custom_target_artifact_patterns(kind, Some(command));
            if !patterns.is_empty() {
                phases.push(RecoveryPhase {
                    name: "target".into(),
                    local: target.to_owned(),
                    remote: pipeline.remote_cargo_target_dir(),
                    patterns,
                    result_dir: None,
                    custom_target: true,
                    output_gate: true,
                    cargo_outputs: None,
                    native_outputs: None,
                    baseline: BTreeMap::new(),
                    published: BTreeMap::new(),
                    pending: None,
                    complete: false,
                    missing_result: None,
                });
            }
        }
        for dir in result_dirs {
            phases.push(RecoveryPhase {
                name: format!("result:{}", dir.display()),
                local: project_root.clone(),
                remote: pipeline.remote_path(),
                patterns: Vec::new(),
                result_dir: Some(dir.clone()),
                custom_target: false,
                output_gate: false,
                cargo_outputs: None,
                native_outputs: None,
                baseline: BTreeMap::new(),
                published: BTreeMap::new(),
                pending: None,
                complete: false,
                missing_result: None,
            });
        }
        for phase in &mut phases {
            let patterns: Vec<_> = if let Some(dir) = &phase.result_dir {
                vec![format!("{}/**/*", dir.display())]
            } else {
                phase
                    .patterns
                    .iter()
                    .filter(|pattern| !pattern.starts_with("- "))
                    .map(|pattern| {
                        pattern
                            .trim_start_matches("+ ")
                            .trim_start_matches('/')
                            .to_owned()
                    })
                    .collect()
            };
            for pattern in patterns {
                let pattern = format!(
                    "{}/{}",
                    glob::Pattern::escape(&phase.local.to_string_lossy()),
                    pattern
                );
                for path in glob::glob(&pattern)? {
                    let path = path?;
                    if path.is_file() {
                        let relative = path.strip_prefix(&phase.local)?.to_owned();
                        if let Some(hash) = fingerprint(&path)? {
                            phase.baseline.insert(relative, hash);
                        }
                    }
                }
            }
        }
        let cargo_output_capture = CargoOutputCapture::for_command(kind, command);
        anyhow::ensure!(
            cargo_output_capture.is_none()
                || phases.iter().filter(|phase| phase.output_gate).count() == 1,
            "captured Cargo execution requires exactly one artifact output root"
        );
        let pinned_triple = explicit_target_triple_for_command(command);
        let recipe = RecoveryRecipe {
            version: 2,
            wrapper_id: lease.identity.local_wrapper_id,
            build_id,
            worker: worker.clone(),
            identity,
            completion,
            source_roots,
            pair,
            retire_root,
            retained_root,
            retained_materialized: None,
            transfer,
            project_root,
            kind: delivery_kind,
            expected_triple: pinned_triple
                .clone()
                .unwrap_or_else(default_host_target_triple),
            pinned_triple,
            allow_foreign: foreign_artifact_gate_disabled(),
            package_archive: sync_back_verified_zero_package_archives(Some(0), command),
            cargo_output_capture,
            cargo_stdout: None,
            cargo_stderr: None,
            caller_json_policy: None,
            caller_delivery: None,
            phases,
            exit_code: None,
            returned: None,
            daemon_exit_code: None,
            prepared: true,
            execution_started: false,
            preparation_cancelled: false,
            sources_released: false,
            tree_retired: false,
            pair_released: false,
            retired: false,
        };
        let session = Self {
            recipe,
            writer: writer.clone(),
        };
        session.persist()?;
        Ok(session)
    }

    fn persist(&self) -> anyhow::Result<()> {
        self.writer
            .set_recovery(serde_json::to_value(&self.recipe)?)
    }
    /// A failed journal publication must not install a successful lifecycle
    /// boundary in this session. Same-session retries use these exact flags.
    fn persist_next_recipe(&mut self, next_recipe: RecoveryRecipe) -> anyhow::Result<()> {
        self.writer
            .set_recovery(serde_json::to_value(&next_recipe)?)?;
        self.recipe = next_recipe;
        Ok(())
    }
    /// Output checkpoints touch only one phase. Keep their undo state small,
    /// but restore the last acknowledged session state on publication failure.
    /// Post-rename sync errors still require durable-state reconciliation.
    fn persist_pending_output(
        &mut self,
        index: usize,
        pending: Option<(PathBuf, String)>,
    ) -> anyhow::Result<()> {
        let previous = std::mem::replace(&mut self.recipe.phases[index].pending, pending);
        if let Err(error) = self.persist() {
            self.recipe.phases[index].pending = previous;
            return Err(error);
        }
        Ok(())
    }

    fn persist_published_output(
        &mut self,
        index: usize,
        relative: PathBuf,
        hash: String,
    ) -> anyhow::Result<()> {
        let phase = &mut self.recipe.phases[index];
        let previous_hash = phase.published.insert(relative.clone(), hash);
        let previous_pending = phase.pending.take();
        if let Err(error) = self.persist() {
            let phase = &mut self.recipe.phases[index];
            match previous_hash {
                Some(hash) => {
                    phase.published.insert(relative, hash);
                }
                None => {
                    phase.published.remove(&relative);
                }
            }
            phase.pending = previous_pending;
            return Err(error);
        }
        Ok(())
    }

    fn persist_output_baseline(&mut self, index: usize, files: &[PathBuf]) -> anyhow::Result<()> {
        // A failed fingerprint must not leave half of a new baseline installed.
        // Observe every path before changing any entry, then retain only the
        // entries actually changed as the undo state for a failed publication.
        let phase = &self.recipe.phases[index];
        let mut observations = Vec::new();
        for relative in files {
            if !phase.published.contains_key(relative) {
                validate_output_ancestors(&phase.local, relative)?;
                observations.push((relative.clone(), fingerprint(&phase.local.join(relative))?));
            }
        }
        let phase = &mut self.recipe.phases[index];
        let mut previous = Vec::new();
        for (relative, hash) in observations {
            if phase.baseline.get(&relative) == hash.as_ref() {
                continue;
            }
            let old = match hash {
                Some(hash) => phase.baseline.insert(relative.clone(), hash),
                None => phase.baseline.remove(&relative),
            };
            previous.push((relative, old));
        }
        if let Err(error) = self.persist() {
            let phase = &mut self.recipe.phases[index];
            for (relative, hash) in previous.into_iter().rev() {
                match hash {
                    Some(hash) => {
                        phase.baseline.insert(relative, hash);
                    }
                    None => {
                        phase.baseline.remove(&relative);
                    }
                }
            }
            return Err(error);
        }
        Ok(())
    }
    pub(crate) fn needs_cargo_artifact_evidence(&self) -> bool {
        self.recipe.cargo_output_capture.is_some()
            && self
                .recipe
                .phases
                .iter()
                .any(|phase| phase.output_gate && phase.cargo_outputs.is_none())
    }

    pub(crate) fn request_caller_json(&mut self, remote_project_root: &Path) -> anyhow::Result<()> {
        anyhow::ensure!(
            !self.recipe.execution_started
                && self.recipe.exit_code.is_none()
                && self
                    .recipe
                    .cargo_output_capture
                    .as_ref()
                    .is_some_and(CargoOutputCapture::caller_json_supported)
                && remote_project_root.is_absolute()
                && !remote_project_root
                    .to_string_lossy()
                    .chars()
                    .any(char::is_control),
            "caller Cargo JSON requires a fresh admitted named-binary contract"
        );
        let mut next_recipe = self.recipe.clone();
        next_recipe.caller_json_policy = Some(remote_project_root.to_owned());
        self.persist_next_recipe(next_recipe)
    }

    /// Live collection and detached recovery share this exact entry point.
    pub(crate) fn install_complete_cargo_evidence(
        &mut self,
        evidence: &crate::transfer::CargoArtifactEvidence,
    ) -> anyhow::Result<()> {
        if self.recipe.caller_json_policy.is_some() {
            anyhow::ensure!(
                evidence.remote_project_root.is_some(),
                "caller Cargo JSON has no verified source root"
            );
        }
        // Preserve complete producer bytes for normal machine consumers too.
        // The optional physical source root is observed only by caller JSON;
        // neither retention nor recovery may invent it for ordinary builds.
        self.retain_cargo_stdout(
            &evidence.stdout,
            &evidence.remote_root,
            evidence.remote_project_root.as_deref(),
            0,
        )?;
        self.install_cargo_artifact_evidence(&evidence.stdout, &evidence.remote_root)
    }

    /// Return only the original completed invocation's retained producer bytes.
    /// A missing old receipt allows worker collection; a corrupt present receipt
    /// is an error, never permission to replace it with a new baseline.
    fn retained_cargo_artifact_evidence(
        &self,
    ) -> anyhow::Result<Option<crate::transfer::CargoArtifactEvidence>> {
        let Some(receipt) = self.recipe.cargo_stdout.as_ref() else {
            return Ok(None);
        };
        anyhow::ensure!(
            self.recipe.execution_started
                && self.recipe.exit_code == Some(0)
                && receipt.exit_code == 0
                && self.recipe.cargo_output_capture.is_some(),
            "retained Cargo artifact evidence contradicts the admitted completion"
        );
        Ok(Some(crate::transfer::CargoArtifactEvidence {
            stdout: receipt.read()?,
            remote_root: receipt.remote_root.clone(),
            remote_project_root: receipt.remote_project_root.clone(),
        }))
    }
    pub(crate) fn cargo_artifact_remote_root(&self) -> Option<&str> {
        self.recipe.cargo_output_capture.as_ref()?;
        self.recipe
            .phases
            .iter()
            .find(|phase| phase.output_gate)
            .map(|phase| phase.remote.as_str())
    }
    /// Complete Cargo evidence authorizes the selected binary executables that
    /// this exact invocation emitted. Persist the contract and transfer policy
    /// together, before either live collection or detached recovery begins.
    pub(crate) fn install_cargo_artifact_evidence(
        &mut self,
        stdout: &[u8],
        canonical_remote_root: &Path,
    ) -> anyhow::Result<()> {
        let prepared = (|| -> anyhow::Result<_> {
            anyhow::ensure!(
                self.recipe.execution_started && self.recipe.exit_code == Some(0),
                "Cargo output evidence requires a successful admitted execution"
            );
            let capture = self
                .recipe
                .cargo_output_capture
                .as_ref()
                .context("this execution did not request Cargo output evidence")?;
            let configured_root = self
                .cargo_artifact_remote_root()
                .context("captured Cargo execution has no artifact output root")?;
            // The transport proved this exact configured path resolves to the
            // canonical root. Cargo can report either spelling (or both); no
            // unrelated suffix/prefix inference can authorize an output.
            let mut contract = capture.parse_receipt_with_roots(
                stdout,
                &[Path::new(configured_root), canonical_remote_root],
            )?;
            if let Some(project) = self.recipe.caller_json_policy.as_deref() {
                let receipt = self.recipe.cargo_stdout.as_ref().context("caller Cargo output requires complete retained producer evidence before publication")?;
                anyhow::ensure!(receipt.exit_code == 0 && receipt.blake3 == blake3::hash(stdout).to_hex().to_string(), "caller Cargo output contradicts its retained producer receipt");
                let source_root = self.recipe.project_root.canonicalize()?;
                contract.required_files.extend(capture.validate_caller_metadata(
                    stdout,
                    &[Path::new(configured_root), canonical_remote_root],
                    &[project, receipt.remote_project_root.as_deref()
                        .context("caller Cargo output has no retained verified source root")?],
                    &source_root,
                )?);
            }
            let index = self
                .recipe
                .phases
                .iter()
                .position(|phase| phase.output_gate)
                .context("captured Cargo execution has no artifact output root")?;
            let phase = &self.recipe.phases[index];
            if let Some(existing) = &phase.cargo_outputs {
                anyhow::ensure!(
                    existing.required_files == contract.required_files,
                    "Cargo output evidence contradicts the persisted required output set"
                );
                return Ok(None);
            }
            anyhow::ensure!(
                !phase.complete && phase.pending.is_none() && phase.published.is_empty(),
                "Cargo output evidence must precede every artifact publication"
            );
            let mut exact_patterns = Vec::new();
            for relative in &contract.required_files {
                // A project-root collection must never promote a source file
                // through the priority include below. With a custom Cargo
                // target, the phase root already is the designated output
                // tree; otherwise outputs must remain beneath target/.
                anyhow::ensure!(
                    phase.custom_target
                        || (relative.starts_with("target") && relative.components().count() > 1),
                    "Cargo output is outside the project's target tree: {}",
                    relative.display()
                );
                let name = relative.to_str().context("non-UTF-8 Cargo output path")?;
                let mut literal = String::with_capacity(name.len());
                for character in name.chars() {
                    if matches!(character, '\\' | '*' | '?' | '[' | ']') {
                        literal.push('\\');
                    }
                    literal.push(character);
                }
                exact_patterns.push(format!("+ {literal}"));
            }
            exact_patterns.extend(phase.patterns.iter().cloned());
            Ok(Some((index, contract, exact_patterns)))
        })()
        .map_err(CargoOutputContractRejected)?;
        let Some((index, contract, patterns)) = prepared else {
            return Ok(());
        };
        let mut next_recipe = self.recipe.clone();
        let phase = &mut next_recipe.phases[index];
        phase.patterns = patterns;
        phase.cargo_outputs = Some(contract);
        self.writer
            .set_recovery(serde_json::to_value(&next_recipe)?)?;
        self.recipe = next_recipe;
        Ok(())
    }
    pub(crate) fn cargo_artifact_patterns(&self, name: &str) -> anyhow::Result<Vec<String>> {
        self.recipe
            .phases
            .iter()
            .find(|phase| phase.name == name)
            .map(|phase| phase.patterns.clone())
            .context("missing retrieval phase")
    }

    /// Preserve the exact producer log before any retirement can remove the
    /// supervisor's copy. A failed write stays retained; a recovery collector
    /// can create a fresh uniquely named copy without overwriting that evidence.
    pub(crate) fn retain_cargo_stdout<'a>(
        &mut self,
        stdout: &[u8],
        remote_root: &Path,
        remote_project_root: impl Into<Option<&'a Path>>,
        exit_code: i32,
    ) -> anyhow::Result<()> {
        anyhow::ensure!(
            self.recipe.execution_started
                && self.recipe.exit_code == Some(exit_code)
                && self.recipe.cargo_output_capture.is_some(),
            "Cargo stdout requires this admitted invocation's exact completion"
        );
        let remote_project_root = remote_project_root.into();
        anyhow::ensure!(
            self.recipe.caller_json_policy.is_none() || remote_project_root.is_some(),
            "caller Cargo stdout has no observed source root"
        );
        anyhow::ensure!(
            stdout.len() <= super::super::cargo_output_contract::MAX_CARGO_OUTPUT_RECEIPT_BYTES,
            "Cargo stdout exceeds the complete-receipt limit"
        );
        let digest = blake3::hash(stdout).to_hex().to_string();
        if let Some(receipt) = &self.recipe.cargo_stdout {
            anyhow::ensure!(
                receipt.blake3 == digest
                    && receipt.bytes == stdout.len() as u64
                    && receipt.remote_root == remote_root
                    && receipt.remote_project_root.as_deref() == remote_project_root
                    && receipt.exit_code == exit_code
                    && receipt.read()? == stdout,
                "retained Cargo stdout contradicts the completed invocation"
            );
            return Ok(());
        }
        let path = retain_cargo_bytes(&self.recipe.wrapper_id, "cargo-stdout", stdout)?;
        let mut next_recipe = self.recipe.clone();
        next_recipe.cargo_stdout = Some(RetainedCargoStdout {
            path,
            blake3: digest,
            bytes: stdout.len() as u64,
            remote_root: remote_root.to_owned(),
            remote_project_root: remote_project_root.map(Path::to_path_buf),
            exit_code,
        });
        self.writer
            .set_recovery(serde_json::to_value(&next_recipe)?)?;
        self.recipe = next_recipe;
        Ok(())
    }

    pub(crate) async fn collect_cargo_stderr(
        &mut self,
        pipeline: &TransferPipeline,
        worker: &WorkerConfig,
    ) -> anyhow::Result<()> {
        let Some(project) = self.recipe.caller_json_policy.clone() else {
            return Ok(());
        };
        let exit = self
            .recipe
            .exit_code
            .context("Cargo diagnostics have no exact completion")?;
        if let Some(receipt) = self.recipe.cargo_stderr.as_ref() {
            anyhow::ensure!(
                receipt.exit_code == exit,
                "Cargo stderr contradicts its completion"
            );
            receipt.read()?;
            return Ok(());
        }
        anyhow::ensure!(
            self.recipe.execution_started,
            "Cargo stderr has no admitted invocation"
        );
        let bytes = pipeline
            .clone()
            .with_remote_path_override(
                project
                    .to_str()
                    .context("persisted Cargo source root is not UTF-8")?,
            )
            .read_cargo_stderr_evidence(worker, exit)
            .await?;
        let path = retain_cargo_bytes(&self.recipe.wrapper_id, "cargo-stderr", &bytes)?;
        let receipt = RetainedCargoStderr {
            path,
            blake3: blake3::hash(&bytes).to_hex().to_string(),
            bytes: bytes.len() as u64,
            exit_code: exit,
        };
        receipt.read()?;
        let mut next_recipe = self.recipe.clone();
        next_recipe.cargo_stderr = Some(receipt);
        self.writer
            .set_recovery(serde_json::to_value(&next_recipe)?)?;
        self.recipe = next_recipe;
        Ok(())
    }

    /// A success record may reach compiler stdout only after actual delivery.
    /// Check the exact publication fingerprints again under the same physical
    /// output-root lock, so an ambient old executable cannot satisfy the API.
    pub(crate) async fn caller_cargo_json(&mut self) -> anyhow::Result<Vec<u8>> {
        anyhow::ensure!(
            self.recipe.returned == Some(0) && self.recipe.exit_code == Some(0),
            "caller Cargo JSON requires successful compilation and artifact delivery"
        );
        let receipt = self
            .recipe
            .cargo_stdout
            .as_ref()
            .context("complete retained Cargo stdout is unavailable")?;
        anyhow::ensure!(
            receipt.exit_code == 0,
            "retained Cargo stdout failed its invocation binding"
        );
        if let Some(delivery) = self.recipe.caller_delivery.as_ref() {
            return delivery.read(receipt);
        }
        let remote_project_root = self
            .recipe
            .caller_json_policy
            .as_deref()
            .context("caller Cargo JSON has no persisted source policy")?;
        let stdout = receipt.read()?;
        let phase = self
            .recipe
            .phases
            .iter()
            .find(|phase| phase.output_gate)
            .context("caller Cargo JSON has no output phase")?;
        anyhow::ensure!(
            phase.complete && phase.pending.is_none(),
            "caller Cargo JSON output phase is incomplete"
        );
        let _publication = lock_output_root(&phase.local).await?;
        let local_root = phase.local.canonicalize()?;
        phase
            .cargo_outputs
            .as_ref()
            .context("caller Cargo JSON has no output contract")?
            .verify_staged(&local_root)?;
        let source_root = self.recipe.project_root.canonicalize()?;
        let capture = self
            .recipe
            .cargo_output_capture
            .as_ref()
            .context("caller Cargo JSON has no capture policy")?;
        // Other builds can legitimately publish into the shared target root
        // after this lock is released. Give this invocation independent copies
        // before advertising paths, so later atomic replacements cannot change
        // the bytes an installer is about to consume.
        let caller_parent = if phase.custom_target {
            local_root.clone()
        } else {
            local_root.join("target").canonicalize()?
        };
        anyhow::ensure!(
            caller_parent.starts_with(&local_root),
            "caller Cargo target root escapes its verified output phase"
        );
        let caller_root = caller_parent.join(format!(
            ".rch-delivered-{}-{}",
            self.recipe.wrapper_id,
            uuid::Uuid::new_v4(),
        ));
        std::fs::create_dir(&caller_root)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&caller_root, std::fs::Permissions::from_mode(0o700))?;
        }
        let binding = serde_json::json!({
            "path": receipt.path,
            "blake3": receipt.blake3,
            "wrapper_id": self.recipe.wrapper_id,
            "build_id": self.recipe.build_id,
            "published_output_root": local_root,
            "caller_output_root": caller_root,
            "remote_project_root": receipt.remote_project_root,
            "stderr": self.recipe.cargo_stderr.as_ref()
                .context("caller Cargo delivery has no retained complete diagnostics")?,
        });
        let roots = [Path::new(&phase.remote), receipt.remote_root.as_path()];
        let source_roots = [
            remote_project_root,
            receipt
                .remote_project_root
                .as_deref()
                .context("caller Cargo delivery has no retained verified source root")?,
        ];
        let projected = capture.caller_json(
            &stdout,
            &CargoCallerPaths {
                remote_output_roots: &roots,
                local_output_root: &caller_root,
                remote_project_roots: &source_roots,
                local_project_root: &source_root,
                published: &phase.published,
                receipt_binding: &binding,
            },
        )?;
        CargoOutputContract {
            required_files: projected.files.clone(),
        }
        .verify_staged(&local_root)?;
        for relative in &projected.files {
            let expected = phase
                .published
                .get(relative)
                .context("caller Cargo filename lacks a publication fingerprint")?;
            copy_published_output(
                &local_root.join(relative),
                &caller_root.join(relative),
                expected,
            )?;
        }
        File::open(&caller_root)?.sync_all()?;
        File::open(&caller_parent)?.sync_all()?;
        CargoOutputContract {
            required_files: projected.files.clone(),
        }
        .verify_staged(&caller_root)?;
        let limit = super::super::cargo_output_contract::MAX_CARGO_OUTPUT_RECEIPT_BYTES
            .checked_mul(2)
            .context("caller Cargo JSON size limit overflow")?;
        anyhow::ensure!(
            projected.stdout.len() <= limit,
            "projected caller Cargo JSON exceeds its bound"
        );
        let path =
            retain_cargo_bytes(&self.recipe.wrapper_id, "cargo-delivery", &projected.stdout)?;
        let mut files = BTreeMap::new();
        for relative in &projected.files {
            files.insert(
                relative.clone(),
                phase
                    .published
                    .get(relative)
                    .context("caller Cargo output lacks its publication fingerprint")?
                    .clone(),
            );
        }
        let delivery = RetainedCargoDelivery {
            path,
            blake3: blake3::hash(&projected.stdout).to_hex().to_string(),
            bytes: projected.stdout.len() as u64,
            producer_blake3: receipt.blake3.clone(),
            output_root: caller_root,
            files,
        };
        delivery.read(receipt)?;
        let mut next_recipe = self.recipe.clone();
        next_recipe.caller_delivery = Some(delivery);
        self.writer
            .set_recovery(serde_json::to_value(&next_recipe)?)?;
        self.recipe = next_recipe;
        Ok(projected.stdout)
    }

    async fn prepare_caller_delivery(&mut self) -> anyhow::Result<()> {
        if self.recipe.caller_json_policy.is_some() && self.recipe.returned == Some(0) {
            self.caller_cargo_json().await?;
        }
        Ok(())
    }

    pub(crate) fn failed_cargo_stdout(&self) -> anyhow::Result<Vec<u8>> {
        let receipt = self
            .recipe
            .cargo_stdout
            .as_ref()
            .context("complete failed Cargo stdout is unavailable")?;
        anyhow::ensure!(
            receipt.exit_code != 0 && self.recipe.exit_code == Some(receipt.exit_code),
            "failed Cargo stdout requires the exact nonzero completion"
        );
        let stdout = receipt.read()?;
        // Telemetry follows the real workload's terminal output and is not
        // part of Cargo stdout. Keep every preceding byte, including failures.
        let marker = format!("\n{}\n", rch_telemetry::protocol::PIGGYBACK_MARKER);
        let end = if stdout.starts_with(&marker.as_bytes()[1..]) {
            0
        } else {
            stdout
                .windows(marker.len())
                .position(|window| window == marker.as_bytes())
                .map_or(stdout.len(), |index| index + 1)
        };
        Ok(stdout[..end].to_vec())
    }
    pub(crate) fn has_native_output_contract(&self) -> bool {
        self.recipe
            .phases
            .iter()
            .any(|phase| phase.native_outputs.is_some())
    }
    fn verify_native_outputs(&self, index: usize) -> anyhow::Result<()> {
        if let Some(contract) = &self.recipe.phases[index].native_outputs {
            contract.verify_staged(&self.stage(index))?;
        }
        Ok(())
    }
    fn verify_cargo_outputs(&self, index: usize) -> anyhow::Result<()> {
        let phase = &self.recipe.phases[index];
        if self.recipe.cargo_output_capture.is_none() || !phase.output_gate {
            return Ok(());
        }
        phase
            .cargo_outputs
            .as_ref()
            .context("complete Cargo output evidence is required before publication")?
            .verify_staged(&self.stage(index))
    }
    pub(crate) fn completion_pipeline(&self, pipeline: TransferPipeline) -> TransferPipeline {
        pipeline
            .with_recovery_completion(self.recipe.completion.clone(), self.recipe.identity.clone())
    }
    pub(crate) fn completed(&mut self, exit: i32) -> anyhow::Result<()> {
        anyhow::ensure!(
            self.recipe.execution_started,
            "completion requires an admitted execution attempt"
        );
        anyhow::ensure!(
            self.recipe
                .exit_code
                .is_none_or(|observed| observed == exit),
            "remote completion contradicts the recorded command outcome; ownership retained"
        );
        let mut next_recipe = self.recipe.clone();
        next_recipe.exit_code = Some(exit);
        self.persist_next_recipe(next_recipe)
    }
    pub(crate) fn starting_execution(&mut self) -> anyhow::Result<()> {
        anyhow::ensure!(
            self.recipe.prepared
                && !self.recipe.execution_started
                && !self.recipe.preparation_cancelled
                && !self.recipe.sources_released,
            "execution requires an unconsumed prepared source grant"
        );
        let mut next_recipe = self.recipe.clone();
        next_recipe.execution_started = true;
        self.persist_next_recipe(next_recipe)
    }
    fn stage(&self, index: usize) -> PathBuf {
        self.recipe
            .transfer
            .retrieval_staging_dir
            .clone()
            .unwrap_or_else(|| default_job_lease_directory().join("retrieval"))
            .join(&self.recipe.identity)
            .join(index.to_string())
    }

    /// Apply operator recovery settings under the exclusive journal claim.
    /// Only extend the transfer budget; all output and source authority stays
    /// bound to the original recipe. Never move or delete an old staging tree.
    fn apply_retrieval_config(&mut self, current: &TransferConfig) -> anyhow::Result<()> {
        let mut next = self.recipe.clone();
        if let Some(timeout) = current.max_transfer_time_ms.filter(|value| *value > 0) {
            anyhow::ensure!(
                timeout <= 86_400_000,
                "recovery transfer timeout must not exceed 24 hours"
            );
            // Zero, like None, means adaptive timing in TransferPipeline.
            // Do not turn that sentinel into a hard ceiling or compare an
            // extension against zero instead of the persisted retry floor.
            let explicit = next
                .transfer
                .max_transfer_time_ms
                .filter(|value| *value > 0);
            let floor = explicit.unwrap_or(next.transfer.retry.total_timeout_ms);
            if timeout > floor {
                if explicit.is_some() {
                    next.transfer.max_transfer_time_ms = Some(timeout);
                } else {
                    // Preserve size-derived budgets: an extension of the
                    // base must not introduce a smaller hard ceiling for a
                    // large retained output set.
                    next.transfer.retry.total_timeout_ms = timeout;
                }
            }
        }
        if let Some(root) = &current.retrieval_staging_dir {
            anyhow::ensure!(
                root.is_absolute(),
                "transfer.retrieval_staging_dir must be absolute"
            );
            if next.transfer.retrieval_staging_dir.as_ref() != Some(root) {
                anyhow::ensure!(
                    !next.sources_released
                        && next.returned.is_none()
                        && next.phases.iter().all(|phase| !phase.complete
                            && phase.published.is_empty()
                            && phase.pending.is_none()),
                    "cannot change retrieval staging after publication began; retained staging and ownership are unchanged"
                );
                next.transfer.retrieval_staging_dir = Some(root.clone());
            }
        }
        next.transfer.retrieval_min_free_bytes = next
            .transfer
            .retrieval_min_free_bytes
            .max(current.retrieval_min_free_bytes);
        self.persist_next_recipe(next)
    }
    pub(crate) fn staging_pipeline(
        &self,
        name: &str,
        pipeline: &TransferPipeline,
    ) -> anyhow::Result<TransferPipeline> {
        let index = self
            .recipe
            .phases
            .iter()
            .position(|phase| phase.name == name)
            .context("missing retrieval phase")?;
        let stage = self.stage(index);
        anyhow::ensure!(
            stage.is_absolute(),
            "retrieval staging directory must be absolute"
        );
        std::fs::create_dir_all(&stage)?;
        Ok(pipeline
            .clone()
            .with_retrieval_reference_root(self.recipe.phases[index].local.clone())
            .with_local_root(stage))
    }
    pub(crate) async fn publish(&mut self, name: &str) -> anyhow::Result<()> {
        let index = self
            .recipe
            .phases
            .iter()
            .position(|phase| phase.name == name)
            .context("missing retrieval phase")?;
        anyhow::ensure!(
            self.recipe.phases[index].missing_result.is_none(),
            "a missing required result cannot be published as complete"
        );
        if self.recipe.phases[index].complete {
            return Ok(());
        }
        // Exclusive for this publication only; see `lock_output_root`.
        let _publication = lock_output_root(&self.recipe.phases[index].local).await?;
        // Validate the entire required set before the first journal/output
        // mutation. One executable or support library cannot satisfy another
        // selected Cargo target, and existing destination files are no proof.
        self.verify_cargo_outputs(index)?;
        self.verify_native_outputs(index)?;
        let stage = self.stage(index);
        #[cfg(unix)]
        let mut files = publication_copy::staged_regular_files(&stage)?;
        #[cfg(not(unix))]
        let mut files = regular_files(&stage)?;
        let phase = &self.recipe.phases[index];
        if let Some((relative, _)) = &phase.pending {
            // Check before the completed shortcut, policy fingerprint or
            // pending rename can observe a redirected descendant as ours.
            validate_output_ancestors(&phase.local, relative)?;
        }
        if phase.result_dir.is_some() {
            anyhow::ensure!(
                phase.pending.as_ref().is_none_or(|(relative, _)| {
                    !crate::transfer::is_internal_source_archive(relative)
                }),
                "internal source archive has a pending publication; retained evidence requires inspection"
            );
            files.retain(|relative| !crate::transfer::is_internal_source_archive(relative));
        } else {
            // A resumed transfer can retain files selected by an older
            // collector. Ownership fingerprints alone do not authorize a
            // source file as an artifact, so only staged files the artifact
            // policy admits are ever published. The rest (retained Cargo
            // metadata such as `.rustc_info.json`) stay private to the stage:
            // refusing the whole publication instead left the job forever
            // unrecoverable and its worker-side source claim stranded.
            let policy = TransferPipeline::new(
                phase.local.clone(),
                "recovery-publication".into(),
                self.recipe.identity.clone(),
                self.recipe.transfer.clone(),
            );
            let (admitted, outside) =
                policy.partition_staged_artifact_paths(&files, &phase.patterns)?;
            for path in &outside {
                // Warn: a skip means collector and recipe disagree about the
                // policy (as in bd-3kskq); keep that skew visible.
                warn!(
                    "recovery skips staged file outside the artifact policy: {}",
                    path.display()
                );
            }
            files = admitted;
            if let Some((relative, hash)) = &phase.pending {
                // A journaled write already began renaming this path into
                // place. It must be in policy to be finished; never skip it.
                let completed = !phase.baseline.contains_key(relative)
                    && fingerprint(&phase.local.join(relative))?.as_ref() == Some(hash);
                if !completed {
                    policy.validate_staged_artifact_paths(
                        std::slice::from_ref(relative),
                        &phase.patterns,
                    )?;
                }
            }
        }
        if let Some((relative, hash)) = self.recipe.phases[index].pending.clone() {
            let destination = self.recipe.phases[index].local.join(&relative);
            let parent = destination
                .parent()
                .context("pending output missing parent")?;
            let temporary = parent.join(format!(".rch-return-{}", self.recipe.identity));
            let current = fingerprint(&destination)?;
            if current.as_ref() != Some(&hash) {
                anyhow::ensure!(
                    current.as_ref() == self.recipe.phases[index].baseline.get(&relative),
                    "interrupted output publication cannot prove ownership at {}; refusing overwrite",
                    destination.display()
                );
                let prepared = PreparedOutput::existing(File::open(parent)?, &temporary, &hash)?;
                prepared.publish(&destination)?;
            }
            self.persist_published_output(index, relative, hash)?;
        }
        // Re-baseline, under the publication lock, every output this job has
        // not written yet. The preparation-time snapshot predates the remote
        // build, and in a shared target dir other jobs legitimately replace
        // these files meanwhile. Only this job's own interrupted write (the
        // journaled `pending` entry above) has to match the older baseline;
        // the refreshed one is persisted before any write so a crash mid-loop
        // recovers against exactly what this publication observed.
        self.persist_output_baseline(index, &files)?;
        for relative in files {
            let phase = &self.recipe.phases[index];
            if phase.published.contains_key(&relative) {
                continue;
            }
            validate_output_ancestors(&phase.local, &relative)?;
            let source = stage.join(&relative);
            let destination = phase.local.join(&relative);
            let hash = fingerprint(&source)?.context("staged output disappeared")?;
            let current = fingerprint(&destination)?;
            anyhow::ensure!(
                current.as_ref() == phase.baseline.get(&relative),
                "output ownership changed at {}; refusing to overwrite",
                destination.display()
            );
            let parent = destination.parent().context("output missing parent")?;
            let temporary = parent.join(format!(".rch-return-{}", self.recipe.identity));
            let prepared = prepare_output_temporary(&source, &temporary, &hash)?;
            prepared.directory.sync_all()?;
            self.persist_pending_output(index, Some((relative.clone(), hash.clone())))?;
            anyhow::ensure!(
                fingerprint(&destination)?.as_ref()
                    == self.recipe.phases[index].baseline.get(&relative),
                "output changed while publishing {}",
                destination.display()
            );
            prepared.publish(&destination)?;
            self.persist_published_output(index, relative, hash)?;
        }
        let mut next_recipe = self.recipe.clone();
        next_recipe.phases[index].complete = true;
        self.persist_next_recipe(next_recipe)?;
        // Ordinary successful delivery can release its duplicate staging.
        // A retained-source intent also protects its original delivery inputs,
        // including out-of-policy files kept private in this stage.
        if !self.retain_source_lifecycle_evidence() {
            let _ = std::fs::remove_dir_all(&stage);
        }
        Ok(())
    }
    pub(crate) fn returned(&mut self, exit: i32) -> anyhow::Result<()> {
        anyhow::ensure!(
            exit != 0
                || self
                    .recipe
                    .phases
                    .iter()
                    .all(|phase| { phase.native_outputs.is_none() || phase.complete }),
            "native compilation cannot succeed before every required output is published"
        );
        anyhow::ensure!(
            !result_recovery::has_missing_results(&self.recipe)
                || exit == EXIT_ARTIFACT_TRANSFER_FAILED,
            "missing required outputs must remain a delivery failure"
        );
        anyhow::ensure!(
            exit != 0
                || self.recipe.cargo_output_capture.is_none()
                || self.recipe.phases.iter().all(|phase| {
                    !phase.output_gate || (phase.cargo_outputs.is_some() && phase.complete)
                }),
            "captured Cargo build cannot succeed before all required outputs are published"
        );
        let mut next_recipe = self.recipe.clone();
        next_recipe.returned = Some(exit);
        self.persist_next_recipe(next_recipe)
    }
    pub(crate) fn tree_retired(&mut self) -> anyhow::Result<()> {
        self.persist_tree_retirement(self.recipe.retained_materialized)
    }
    fn persist_tree_retirement(&mut self, materialized: Option<bool>) -> anyhow::Result<()> {
        anyhow::ensure!(
            self.recipe.retained_root.is_none() || materialized.is_some(),
            "retained source retirement has no durable filesystem acknowledgment"
        );
        anyhow::ensure!(
            materialized != Some(false)
                || (!self.recipe.execution_started && self.recipe.preparation_cancelled),
            "an executed source pair cannot retire as unmaterialized preparation"
        );
        anyhow::ensure!(
            materialized.is_none() || self.recipe.retained_root.is_some(),
            "retained source acknowledgment has no original destination"
        );
        let mut next_recipe = self.recipe.clone();
        next_recipe.retained_materialized = materialized;
        next_recipe.tree_retired = true;
        self.persist_next_recipe(next_recipe)
    }
    /// The caller holds the complete active or drained cleanup grant. A lost
    /// acknowledgment leaves the recipe pending; the identity-bound rename
    /// protocol verifies the exact retained sibling on the next attempt.
    pub(crate) async fn retire_source_tree(
        &mut self,
        pipeline: &TransferPipeline,
    ) -> anyhow::Result<()> {
        if self.recipe.tree_retired {
            return Ok(());
        }
        let mut materialized = None;
        if let Some(root) = self.recipe.retire_root.as_deref() {
            if let Some(retained) = self.recipe.retained_root.as_deref() {
                materialized = Some(
                    pipeline
                        .retain_remote_tree(
                            &self.recipe.worker,
                            root,
                            retained,
                            &self.recipe.identity,
                            !self.recipe.execution_started && self.recipe.preparation_cancelled,
                        )
                        .await?,
                );
            } else {
                pipeline.reap_remote_tree(&self.recipe.worker, root).await?;
            }
        }
        self.persist_tree_retirement(materialized)
    }
    pub(crate) fn pair_released(&mut self) -> anyhow::Result<()> {
        let mut next_recipe = self.recipe.clone();
        next_recipe.pair_released = true;
        self.persist_next_recipe(next_recipe)
    }
    pub(crate) fn sources_released(&mut self) -> anyhow::Result<()> {
        let mut next_recipe = self.recipe.clone();
        next_recipe.sources_released = true;
        self.persist_next_recipe(next_recipe)
    }
    pub(crate) fn retired(&mut self) -> anyhow::Result<()> {
        anyhow::ensure!(
            self.recipe.returned.is_some(),
            "cannot retire before output publication completes"
        );
        anyhow::ensure!(
            self.recipe.retire_root.is_none() || self.recipe.tree_retired,
            "remote tree retirement is outstanding"
        );
        anyhow::ensure!(
            self.recipe.pair.is_none() || self.recipe.pair_released,
            "source-pair release is outstanding"
        );
        anyhow::ensure!(
            self.recipe.source_roots.is_empty() || self.recipe.sources_released,
            "remote source release is outstanding"
        );
        let mut next_recipe = self.recipe.clone();
        next_recipe.retired = true;
        self.persist_next_recipe(next_recipe)?;
        // Retained invocations and failed required-output deliveries keep
        // staging for inspection. Ordinary retired jobs can release duplicates.
        if !self.retain_source_lifecycle_evidence()
            && !self.retain_failed_delivery_evidence()
            && let Some(stages) = self.stage(0).parent()
        {
            let _ = std::fs::remove_dir_all(stages);
        }
        Ok(())
    }
    pub(crate) async fn retire_returned(&mut self) -> anyhow::Result<()> {
        if self.recipe.retired {
            return Ok(());
        }
        anyhow::ensure!(
            self.recipe.returned.is_some(),
            "cannot retire outputs before durable return"
        );
        let worker = self.recipe.worker.clone();
        let mut pair = if !self.recipe.pair_released {
            if let Some((root, token)) = self.recipe.pair.clone() {
                if super::super::ssh::clean_overlay_source_pair_was_released(&worker, &root, &token)
                    .await?
                {
                    self.pair_released()?;
                    None
                } else {
                    Some(
                        super::super::ssh::recover_clean_overlay_source_pair(
                            &worker,
                            &root,
                            &token,
                            Duration::from_secs(15),
                        )
                        .await?,
                    )
                }
            } else {
                None
            }
        } else {
            None
        };
        let mut sources = if self.recipe.sources_released || self.recipe.source_roots.is_empty() {
            None
        } else if super::super::ssh::remote_source_authority_was_released(
            &worker,
            &self.recipe.source_roots,
            &self.recipe.identity,
        )
        .await?
        {
            // A release receipt is only a terminal reconciliation fact. It can
            // never reopen paths which a later invocation may now be writing.
            anyhow::ensure!(
                self.recipe.tree_retired,
                "source release preceded durable tree retirement; refusing path access"
            );
            self.sources_released()?;
            None
        } else {
            Some(
                super::super::ssh::recover_remote_source_authority_lock(
                    &worker,
                    &self.recipe.source_roots,
                    pair.as_mut(),
                    &self.recipe.identity,
                    Duration::from_secs(15),
                )
                .await?,
            )
        };
        if self.recipe.tree_retired {
            // Do not reopen released worker paths. Any first caller delivery
            // must already have enough retained local evidence to verify here.
            self.prepare_caller_delivery().await?;
        }
        if !self.recipe.tree_retired {
            let sources = sources
                .as_mut()
                .context("tree retirement requires the active source grant")?;
            sources.ensure_held()?;
            if let Some(pair) = pair.as_mut() {
                pair.ensure_held()?;
            }
            if self.recipe.caller_json_policy.is_some() {
                let pipeline = self.completion_pipeline(
                    TransferPipeline::new(
                        self.recipe.project_root.clone(),
                        "recovery".into(),
                        self.recipe.identity.clone(),
                        self.recipe.transfer.clone(),
                    )
                    .with_source_authority(self.recipe.identity.clone())?,
                );
                self.collect_cargo_stderr(&pipeline, &worker).await?;
            }
            self.prepare_caller_delivery().await?;
            let pipeline = TransferPipeline::new(
                self.recipe.project_root.clone(),
                "recovery".into(),
                self.recipe.identity.clone(),
                self.recipe.transfer.clone(),
            )
            .with_source_authority(self.recipe.identity.clone())?;
            self.retire_source_tree(&pipeline).await?;
        }
        if let Some(sources) = sources.take() {
            sources.release().await?;
            self.sources_released()?;
        }
        if let Some(pair) = pair.take() {
            pair.release().await?;
        }
        self.pair_released()?;
        self.retired()?;
        self.discard_completion_receipts().await;
        Ok(())
    }
    /// Retained custody begins with the admitted intent, before a filesystem
    /// retirement acknowledgment. A pending or cancelled tree does not grant
    /// permission to discard the invocation's delivery/completion evidence.
    fn retain_source_lifecycle_evidence(&self) -> bool {
        self.recipe.retained_root.is_some()
    }

    fn retain_failed_delivery_evidence(&self) -> bool {
        result_recovery::has_missing_results(&self.recipe)
            || (self.recipe.exit_code == Some(0)
                && self
                    .recipe
                    .phases
                    .iter()
                    .any(|phase| phase.native_outputs.is_some() && !phase.complete))
            || (self.recipe.cargo_output_capture.is_some()
                && self.recipe.exit_code == Some(0)
                && self
                    .recipe
                    .phases
                    .iter()
                    .any(|phase| phase.output_gate && !phase.complete))
    }
    async fn discard_completion_receipts(&self) {
        // Retained source custody protects the original completion too. Keep
        // terminal required-output failure evidence under the existing policy.
        if self.retain_source_lifecycle_evidence() || self.retain_failed_delivery_evidence() {
            return;
        }
        let pipeline = self.completion_pipeline(TransferPipeline::new(
            self.recipe.project_root.clone(),
            "recovery".into(),
            self.recipe.identity.clone(),
            self.recipe.transfer.clone(),
        ));
        if let Err(error) = pipeline
            .discard_recovery_completion(&self.recipe.worker)
            .await
        {
            debug!(worker = %self.recipe.worker.id, %error, "completion receipt cleanup failed");
        }
    }
}

fn load_recipe(writer: &DurableLeaseWriter) -> anyhow::Result<RecoveryRecipe> {
    let lease = writer.snapshot();
    let mut recipe: RecoveryRecipe = serde_json::from_value(
        lease
            .recovery
            .context("job has no durable source/retrieval recipe")?,
    )?;
    // Leases written before bd-4d1hs may carry a trailing-slash root that the
    // source-authority lock plan refuses; recover them under the canonical
    // spelling instead of failing forever.
    for root in &mut recipe.source_roots {
        *root = super::super::ssh::canonical_source_authority_root(root);
    }
    anyhow::ensure!(
        recipe.version == 2
            && recipe.wrapper_id == lease.identity.local_wrapper_id
            && Some(recipe.build_id) == lease.identity.remote_build_id
            && lease.worker_id.as_deref() == Some(recipe.worker.id.as_str()),
        "recovery recipe identity mismatch or unsupported source ownership version"
    );
    validate_retained_source_intent(
        &recipe.source_roots,
        recipe.pair.as_ref(),
        recipe.retire_root.as_deref(),
        recipe.retained_root.as_deref(),
        &recipe.identity,
    )?;
    anyhow::ensure!(
        recipe
            .transfer
            .retrieval_staging_dir
            .as_ref()
            .is_none_or(|path| path.is_absolute()),
        "persisted retrieval staging directory must be absolute"
    );
    anyhow::ensure!(
        recipe.retained_materialized.is_none() || recipe.retained_root.is_some(),
        "retained source acknowledgment has no original destination"
    );
    if recipe.retained_root.is_some() {
        anyhow::ensure!(
            recipe.tree_retired == recipe.retained_materialized.is_some()
                && (!recipe.retired || recipe.tree_retired)
                && (recipe.retained_materialized != Some(false)
                    || (!recipe.execution_started && recipe.preparation_cancelled)),
            "retained source acknowledgment contradicts the execution or retirement boundary"
        );
    }
    anyhow::ensure!(
        recipe.cargo_output_capture.is_none()
            || (recipe.prepared
                && recipe
                    .phases
                    .iter()
                    .filter(|phase| phase.output_gate)
                    .count()
                    == 1),
        "captured Cargo recovery has an invalid output root contract"
    );
    anyhow::ensure!(
        recipe.phases.iter().all(|phase| {
            phase.cargo_outputs.is_none()
                || (recipe.cargo_output_capture.is_some() && phase.output_gate)
        }),
        "Cargo output evidence is attached to an unrelated recovery phase"
    );
    if let Some(project) = recipe.caller_json_policy.as_ref() {
        anyhow::ensure!(
            recipe.prepared
                && project.is_absolute()
                && !project.to_string_lossy().chars().any(char::is_control)
                && !WorkerPlatform::from_worker(&recipe.worker).is_windows()
                && recipe
                    .cargo_output_capture
                    .as_ref()
                    .is_some_and(CargoOutputCapture::caller_json_supported),
            "caller Cargo JSON recovery has an invalid admitted source/output policy"
        );
    } else {
        anyhow::ensure!(
            recipe.cargo_stderr.is_none() && recipe.caller_delivery.is_none(),
            "retained caller Cargo output has no original opt-in policy"
        );
    }
    if let Some(producer) = recipe.cargo_stdout.as_ref() {
        anyhow::ensure!(
            recipe.execution_started
                && recipe.exit_code == Some(producer.exit_code)
                && recipe.cargo_output_capture.is_some()
                && (recipe.caller_json_policy.is_some() || producer.exit_code == 0)
                && (recipe.caller_json_policy.is_none() || producer.remote_project_root.is_some())
                && producer.bytes
                    <= super::super::cargo_output_contract::MAX_CARGO_OUTPUT_RECEIPT_BYTES as u64,
            "retained Cargo producer contradicts the original completion"
        );
    }
    if let Some(diagnostics) = recipe.cargo_stderr.as_ref() {
        anyhow::ensure!(
            recipe.execution_started && recipe.exit_code == Some(diagnostics.exit_code),
            "retained Cargo diagnostics contradict the original completion"
        );
    }
    if recipe.caller_delivery.is_some() {
        anyhow::ensure!(
            recipe.exit_code == Some(0)
                && recipe.returned == Some(0)
                && recipe
                    .cargo_stdout
                    .as_ref()
                    .is_some_and(|producer| producer.exit_code == 0)
                && recipe
                    .phases
                    .iter()
                    .filter(|phase| phase.output_gate)
                    .all(|phase| phase.complete && phase.pending.is_none()),
            "retained caller Cargo delivery precedes successful publication"
        );
    }
    result_recovery::validate_missing_results(&recipe)?;
    for phase in &recipe.phases {
        if let Some(contract) = &phase.native_outputs {
            anyhow::ensure!(
                recipe.prepared
                    && phase.name == "project"
                    && phase.result_dir.is_none()
                    && !phase.custom_target
                    && phase.local == recipe.project_root
                    && !WorkerPlatform::from_worker(&recipe.worker).is_windows()
                    && contract.patterns()? == phase.patterns,
                "native output contract is attached to an unrelated recovery phase"
            );
        }
    }
    Ok(recipe)
}

/// Cancel only preparation. The remote activity fence drains surviving sync
/// processes and denies late arrivals before any root becomes writable again.
/// Once execution might have started, only its exact completion can retire it.
pub(crate) async fn cancel_preparation(writer: &DurableLeaseWriter) -> anyhow::Result<bool> {
    let recipe = load_recipe(writer)?;
    if recipe.retired {
        return Ok(true);
    }
    if recipe.execution_started {
        return Ok(false);
    }
    let mut session = RecoverySession {
        recipe,
        writer: writer.clone(),
    };
    let mut next_recipe = session.recipe.clone();
    next_recipe.preparation_cancelled = true;
    session.persist_next_recipe(next_recipe)?;
    cancel_source_grant(session).await
}

/// Cancel the job's source grant (and pair), reap its remote tree, and retire
/// the recipe with a build-error exit. Callers must have proven that nothing
/// of the job can still run remotely: execution never started, or the probe in
/// `remote_execution_lost` found it gone.
async fn cancel_source_grant(mut session: RecoverySession) -> anyhow::Result<bool> {
    let worker = session.recipe.worker.clone();
    let owned = super::super::ssh::cancel_remote_source_authority_intent(
        &worker,
        &session.recipe.source_roots,
        &session.recipe.identity,
    )
    .await?;
    let mut pair = if let Some((root, token)) = session.recipe.pair.clone() {
        super::super::ssh::cancel_clean_overlay_source_pair_intent(
            &worker,
            &root,
            &token,
            Duration::from_secs(15),
        )
        .await?
    } else {
        None
    };
    anyhow::ensure!(
        owned || pair.is_none(),
        "cancelled intent without a source grant cannot acquire a source pair"
    );
    if owned {
        if let Some(pair) = pair.as_mut() {
            pair.ensure_held()?;
        }
        // The full durable grant still excludes every overlapping owner.
        // Cleanup activity survives its SSH client and is drained again
        // before final cancellation can make these paths writable.
        let pipeline = TransferPipeline::new(
            session.recipe.project_root.clone(),
            "recovery".into(),
            session.recipe.identity.clone(),
            session.recipe.transfer.clone(),
        )
        .with_source_authority_cleanup(session.recipe.identity.clone())?;
        session.retire_source_tree(&pipeline).await?;
        if let Some(pair) = pair.take() {
            pair.release().await?;
        }
        super::super::ssh::finish_cancel_remote_source_authority_intent(
            &worker,
            &session.recipe.source_roots,
            &session.recipe.identity,
        )
        .await?;
    }
    // Without a full grant, cancellation only fences delayed acquisition and
    // abandons pair metadata. The paths may belong to another parent-root job.
    if !owned && session.recipe.retained_root.is_some() && !session.recipe.tree_retired {
        anyhow::ensure!(
            !session.recipe.execution_started && session.recipe.preparation_cancelled,
            "an executed source pair cannot be retired without its full source grant"
        );
        // No activity was admitted for this identity. Do not inspect paths
        // now owned by someone else or manufacture a retained tree receipt.
        session.persist_tree_retirement(Some(false))?;
    } else {
        session.tree_retired()?;
    }
    session.pair_released()?;
    session.sources_released()?;
    session.returned(EXIT_BUILD_ERROR)?;
    session.retired()?;
    Ok(true)
}

/// Recollect the admitted command's outputs; no execution path is reachable.
pub(crate) async fn recover_job(
    writer: &DurableLeaseWriter,
) -> anyhow::Result<(i32, DurableLeaseWriter)> {
    let owned = recovery_owner::claim(writer).await?;
    let exit = Box::pin(recover_owned_job(&owned)).await?;
    owned
        .recovery_ownership
        .as_ref()
        .context("recovery has no journal owner")?
        .verify()?;
    Ok((exit, owned))
}

async fn recover_owned_job(writer: &DurableLeaseWriter) -> anyhow::Result<i32> {
    // The recipe is reloaded after claim. Persist extensions before collection
    // so a subsequent retry keeps them even without the invocation config.
    if !writer.snapshot().terminal_acknowledged {
        let config = crate::config::load_config()?;
        let mut session = RecoverySession {
            recipe: load_recipe(writer)?,
            writer: writer.clone(),
        };
        if session.recipe.returned.is_none() {
            session.apply_retrieval_config(&config.transfer)?;
        }
    }
    // Resolve lazily so an already acknowledged journal stays a read-only
    // operation, but pin the chosen endpoint across the entire handoff.
    let mut socket = None;
    Box::pin(recover_owned_job_with_daemon(
        writer,
        async |command: &str| {
            if socket.is_none() {
                let config = crate::config::load_config()?;
                socket = Some(shellexpand::tilde(&config.general.socket_path).into_owned());
            }
            recovery_completion::request(socket.as_deref().expect("resolved socket"), command).await
        },
    ))
    .await
}

/// Recover only the original opt-in invocation, then return its durable stdout.
/// An ordinary job cannot acquire a Cargo output contract after execution.
pub(crate) async fn recover_job_cargo_json(
    writer: &DurableLeaseWriter,
) -> anyhow::Result<(i32, Option<Vec<u8>>, Vec<u8>)> {
    let owned = recovery_owner::claim(writer).await?;
    let writer = &owned;
    {
        let recipe = load_recipe(writer)?;
        anyhow::ensure!(
            recipe.execution_started
                && recipe.caller_json_policy.is_some()
                && recipe
                    .cargo_output_capture
                    .as_ref()
                    .is_some_and(CargoOutputCapture::caller_json_supported),
            "Cargo JSON recovery requires the original admitted opt-in named-binary invocation"
        );
    }
    let exit = Box::pin(recover_owned_job(writer)).await?;
    // Keep the same namespace and lock through acknowledgement and readback.
    let recipe = load_recipe(writer)?;
    let lease = writer.snapshot();
    anyhow::ensure!(
        lease.terminal_acknowledged
            && lease.exit_code == Some(exit)
            && recipe.returned == Some(exit)
            && recipe.retired
            && recipe.caller_json_policy.is_some(),
        "Cargo JSON recovery lacks its exact acknowledged delivery result"
    );
    let session = RecoverySession {
        recipe,
        writer: writer.clone(),
    };
    let stdout = if exit == 0 {
        anyhow::ensure!(
            session.recipe.exit_code == Some(0),
            "successful Cargo recovery contradicts its compiler completion"
        );
        let producer = session
            .recipe
            .cargo_stdout
            .as_ref()
            .context("successful Cargo recovery has no retained producer stdout")?;
        let delivery = session
            .recipe
            .caller_delivery
            .as_ref()
            .context("successful Cargo recovery has no retained caller delivery")?;
        Some(delivery.read(producer)?)
    } else if session
        .recipe
        .cargo_stdout
        .as_ref()
        .is_some_and(|receipt| receipt.exit_code != 0)
    {
        Some(session.failed_cargo_stdout()?)
    } else {
        // Compiler success with failed delivery must not advertise success.
        None
    };
    let diagnostics = session
        .recipe
        .cargo_stderr
        .as_ref()
        .context("Cargo recovery has no retained complete compiler stderr")?;
    anyhow::ensure!(
        session.recipe.exit_code == Some(diagnostics.exit_code),
        "recovered Cargo stderr contradicts its compiler completion"
    );
    let stderr = diagnostics.read()?;
    writer
        .recovery_ownership
        .as_ref()
        .context("recovery has no journal owner")?
        .verify()?;
    Ok((exit, stdout, stderr))
}

#[cfg(test)]
async fn recover_job_with_daemon(
    writer: &DurableLeaseWriter,
    daemon: impl AsyncFnMut(&str) -> anyhow::Result<String>,
) -> anyhow::Result<i32> {
    let owned = recovery_owner::claim(writer).await?;
    let exit = Box::pin(recover_owned_job_with_daemon(&owned, daemon)).await?;
    owned
        .recovery_ownership
        .as_ref()
        .context("recovery has no journal owner")?
        .verify()?;
    Ok(exit)
}

async fn recover_owned_job_with_daemon(
    writer: &DurableLeaseWriter,
    mut daemon: impl AsyncFnMut(&str) -> anyhow::Result<String>,
) -> anyhow::Result<i32> {
    // Hold one journal owner across the ENTIRE detached recovery, including
    // worker probes, source release and final daemon reservation reconciliation.
    // claim reloads the latest disk state after acquisition and never mistakes
    // an unverified wrapper for a dead one.
    writer
        .recovery_ownership
        .as_ref()
        .context("recovery has no journal owner")?
        .verify()?;
    let recipe = load_recipe(writer)?;
    let mut session = RecoverySession {
        recipe,
        writer: writer.clone(),
    };
    // Validate present producer custody before successful terminal
    // shortcut. These branches may only use local original evidence: a
    // released worker path is never reopened and an absent legacy receipt
    // remains absent. Retain the same verified bytes for unfinished recovery.
    // A recorded failed delivery must still reconcile/release its ownership;
    // it advertises no successful artifact or SDK evidence to borrow.
    let retained_cargo_evidence = if session.recipe.exit_code == Some(0)
        && session.recipe.returned.is_none_or(|exit| exit == 0)
    {
        session.retained_cargo_artifact_evidence()?
    } else {
        None
    };
    let lease = writer.snapshot();
    if lease.terminal_acknowledged {
        let exit = session
            .recipe
            .returned
            .context("terminal recovery lacks a delivery result")?;
        anyhow::ensure!(
            session.recipe.retired && lease.exit_code == Some(exit),
            "terminal recovery contradicts its retained delivery/retirement evidence"
        );
        return Ok(exit);
    }
    if !session.recipe.execution_started {
        cancel_preparation(writer).await?;
        writer.record_exit(EXIT_BUILD_ERROR)?;
        recovery_completion::finish(writer, &mut daemon).await?;
        return Ok(EXIT_BUILD_ERROR);
    }
    if let Some(exit) = session.recipe.returned {
        session.retire_returned().await?;
        writer.record_exit(exit)?;
        recovery_completion::finish(writer, &mut daemon).await?;
        return Ok(exit);
    }
    let worker = session.recipe.worker.clone();
    let base = TransferPipeline::new(
        session.recipe.project_root.clone(),
        "recovery".into(),
        session.recipe.identity.clone(),
        session.recipe.transfer.clone(),
    );
    let base = session.completion_pipeline(base);
    let Some(mut exit) = base.read_recovery_completion(&worker).await? else {
        // A supervisor that died without its receipt (worker reboot, OOM,
        // kill) never writes one, and waiting for it kept the lease's source
        // claim fencing the worker forever. When the probe proves the
        // execution is gone, retire it as a failed build: nothing is replayed.
        if !session.recipe.source_roots.is_empty()
            && super::super::ssh::remote_execution_lost(
                &worker,
                &session.recipe.identity,
                &session.recipe.completion,
            )
            .await?
        {
            tracing::warn!(
                identity = %session.recipe.identity,
                worker = %worker.id,
                "remote execution ended without a completion receipt; retiring it as failed"
            );
            cancel_source_grant(session).await?;
            writer.record_exit(EXIT_BUILD_ERROR)?;
            recovery_completion::finish(writer, &mut daemon).await?;
            return Ok(EXIT_BUILD_ERROR);
        }
        anyhow::bail!(
            "same-id remote execution has no durable completion yet; command was not replayed"
        );
    };
    let mut pair = if let Some((root, token)) = &session.recipe.pair {
        Some(
            super::super::ssh::recover_clean_overlay_source_pair(
                &worker,
                root,
                token,
                Duration::from_secs(15),
            )
            .await?,
        )
    } else {
        None
    };
    let mut sources = super::super::ssh::recover_remote_source_authority_lock(
        &worker,
        &session.recipe.source_roots,
        pair.as_mut(),
        &session.recipe.identity,
        Duration::from_secs(15),
    )
    .await?;
    let base = base.with_source_authority(session.recipe.identity.clone())?;
    session.completed(exit)?;
    let command_exit = exit;
    session.collect_cargo_stderr(&base, &worker).await?;
    // A wrapper can disappear after Cargo finishes but before it records the
    // output set locally. Re-read the complete same-attempt receipt; never
    // rerun Cargo and never silently downgrade to the old glob-only policy.
    let mut rejected_cargo_evidence = false;
    if command_exit == 0 && session.needs_cargo_artifact_evidence() {
        let root = session
            .cargo_artifact_remote_root()
            .context("captured Cargo recovery has no output root")?
            .to_owned();
        // Transport failure remains retryable. A complete but invalid Cargo
        // receipt cannot improve by repeating collection, so retire that
        // attempt as a delivery failure while retaining its receipt/stage.
        let evidence = if let Some(retained) = retained_cargo_evidence {
            Ok(retained)
        } else if let Some(project) = session.recipe.caller_json_policy.as_ref() {
            // The generic recovery pipeline is not the executed source root.
            // Restore only the exact opt-in path persisted before execution.
            base.clone()
                .with_remote_path_override(
                    project
                        .to_str()
                        .context("persisted Cargo source root is not UTF-8")?,
                )
                .read_cargo_stdout_evidence(&worker, &root, 0)
                .await
        } else {
            base.read_cargo_artifact_evidence(&worker, &root).await
        };
        match evidence {
            Ok(evidence) => {
                if let Err(error) = session.install_complete_cargo_evidence(&evidence) {
                    if !error.is::<CargoOutputContractRejected>() {
                        return Err(error);
                    }
                    eprintln!(
                        "[RCH] recovered Cargo output evidence is invalid: {error:#}; \
                         no build artifacts published (exit {EXIT_ARTIFACT_TRANSFER_FAILED})"
                    );
                    rejected_cargo_evidence = true;
                }
            }
            Err(error) if error.is::<crate::transfer::CargoArtifactEvidenceRejected>() => {
                eprintln!(
                    "[RCH] recovered Cargo output evidence was rejected: {error:#}; \
                     no build artifacts published (exit {EXIT_ARTIFACT_TRANSFER_FAILED})"
                );
                rejected_cargo_evidence = true;
            }
            Err(error) if base.remote_path_absent(&worker, &root).await? => {
                eprintln!(
                    "[RCH] recovered Cargo output root is gone ({root}): {error:#}; \
                     no build artifacts published (exit {EXIT_ARTIFACT_TRANSFER_FAILED})"
                );
                rejected_cargo_evidence = true;
            }
            Err(error) => return Err(error),
        }
        if rejected_cargo_evidence {
            exit = EXIT_ARTIFACT_TRANSFER_FAILED;
        }
    } else if let Some(retained) = retained_cargo_evidence {
        session.install_complete_cargo_evidence(&retained)?;
    }
    if command_exit != 0
        && let Some(project) = session.recipe.caller_json_policy.clone()
    {
        if session.recipe.cargo_stdout.is_none() {
            // Failed Cargo need not create a target tree. Use the executed
            // source root for physical-path proof, and preserve the complete
            // same-attempt nonzero stdout before retiring its supervisor log.
            let source = project
                .to_str()
                .context("persisted Cargo source root is not UTF-8")?;
            let evidence = base
                .clone()
                .with_remote_path_override(source)
                .read_cargo_stdout_evidence(&worker, source, command_exit)
                .await?;
            session.retain_cargo_stdout(
                &evidence.stdout,
                &evidence.remote_root,
                evidence
                    .remote_project_root
                    .as_deref()
                    .context("recovered Cargo stdout has no verified source root")?,
                command_exit,
            )?;
        } else {
            session.failed_cargo_stdout()?;
        }
    }
    for index in 0..session.recipe.phases.len() {
        let phase = session.recipe.phases[index].clone();
        if phase.complete
            || ((command_exit != 0 || rejected_cargo_evidence) && phase.result_dir.is_none())
        {
            continue;
        }
        sources.ensure_held()?;
        if let Some(pair) = pair.as_mut() {
            pair.ensure_held()?;
        }
        let pipeline = base
            .clone()
            .with_remote_path_override(phase.remote.clone())
            .with_retrieval_reference_root(phase.local.clone())
            .with_local_root(session.stage(index));
        std::fs::create_dir_all(session.stage(index))?;
        if phase.result_dir.is_some() {
            // The completion receipt and recovered source grant above fence
            // the producer. Absence is now a failed output contract, not an
            // endlessly retryable transfer. Still collect other result dirs.
            if !result_recovery::collect_result(&mut session, index, &pipeline, &worker).await? {
                exit = EXIT_ARTIFACT_TRANSFER_FAILED;
                continue;
            }
        } else {
            let retrieved = match pipeline.retrieve_artifacts(&worker, &phase.patterns).await {
                Ok(retrieved) => retrieved,
                // Outputs whose remote tree is gone (e.g. a GC'd target pool)
                // can never come back: failing here stranded the job and its
                // worker-side source claim on every retry. Like a rejected
                // output set, it is a terminal build failure.
                Err(error) if pipeline.remote_path_absent(&worker, &phase.remote).await? => {
                    eprintln!(
                        "[RCH] recovered outputs are gone from the worker ({}): {error:#}; \
                         treating the recovered build as failed (exit {EXIT_ARTIFACT_TRANSFER_FAILED})",
                        phase.remote
                    );
                    exit = EXIT_ARTIFACT_TRANSFER_FAILED;
                    break;
                }
                Err(error) => return Err(error),
            };
            if let Err(error) = session.verify_native_outputs(index) {
                eprintln!(
                    "[RCH] recovered native outputs are incomplete: {error:#}; \
                     no build artifacts published (exit {EXIT_ARTIFACT_TRANSFER_FAILED})"
                );
                exit = EXIT_ARTIFACT_TRANSFER_FAILED;
                break;
            }
            if phase.output_gate {
                // A rejected output set is a terminal build failure, exactly
                // as on the live path, not a recovery error: the remote outputs
                // never change, so an error would strand source ownership on
                // every retry. Publish nothing further, then retire normally.
                let rejection = if let Err(error) = session.verify_cargo_outputs(index) {
                    Some(format!("recovered Cargo outputs are incomplete: {error:#}"))
                } else if sync_back_verified_zero_build_outputs(
                    &retrieved.manifest_regular_files,
                    retrieved.matched_regular_files,
                    session.recipe.kind,
                    phase.custom_target,
                ) || (session.recipe.package_archive
                    && retrieved.matched_regular_files == Some(0))
                {
                    Some("recovered transfer matched zero expected outputs".to_owned())
                } else if !session.recipe.allow_foreign
                    && kind_has_enumerable_output_contract(session.recipe.kind)
                {
                    let foreign = foreign_target_artifacts(
                        &session.stage(index),
                        &retrieved.manifest_regular_files,
                        phase.custom_target,
                        &session.recipe.expected_triple,
                        session.recipe.pinned_triple.as_deref(),
                    );
                    (!foreign.is_empty()).then(|| {
                        format!(
                            "recovered outputs target a foreign platform: {}",
                            describe_findings(&foreign)
                        )
                    })
                } else {
                    None
                };
                if let Some(rejection) = rejection {
                    eprintln!(
                        "[RCH] {rejection}; treating the recovered build as failed \
                         (exit {EXIT_ARTIFACT_TRANSFER_FAILED})"
                    );
                    exit = EXIT_ARTIFACT_TRANSFER_FAILED;
                    break;
                }
            }
        }
        session.publish(&phase.name).await?;
    }
    // Publication is the durable terminal boundary. Retirement can be retried
    // independently and must never cause a second output write.
    session.returned(exit)?;
    session.prepare_caller_delivery().await?;
    session.retire_source_tree(&base).await?;
    if let Some(pair) = pair.take() {
        pair.release().await?;
    }
    session.pair_released()?;
    sources.release().await?;
    session.sources_released()?;
    session.retired()?;
    session.discard_completion_receipts().await;
    writer.record_exit(exit)?;
    recovery_completion::finish(writer, &mut daemon).await?;
    Ok(exit)
}

/// Exercise the same durable publication boundary with real Cargo records from
/// the selector's owned fixture, without running Cargo a second time here.
#[cfg(test)]
pub(crate) async fn assert_cargo_fixture_publication(
    kind: CompilationKind,
    command: &str,
    stdout: &[u8],
    remote_root: &Path,
    custom_target: bool,
) {
    tests::assert_cargo_fixture_publication(kind, command, stdout, remote_root, custom_target)
        .await;
}

#[cfg(test)]
mod tests {
    use super::*;
    use rch_common::test_guard;

    fn admitted_preparation_fixture() -> (tempfile::TempDir, DurableLeaseWriter, WorkerConfig) {
        // Retain construction failures too; calling keep only after setup
        // succeeds lets a panic delete evidence through TempDir's destructor.
        let directory = tempfile::Builder::new()
            .disable_cleanup(true)
            .tempdir()
            .unwrap();
        let worker = WorkerConfig {
            id: WorkerId::new("source-recovery"),
            host: "unreachable.invalid".into(),
            user: "worker".into(),
            identity_file: "/unused/test-key".into(),
            total_slots: 1,
            priority: 100,
            tags: Vec::new(),
            tools: Vec::new(),
        };
        let writer = DurableLeaseWriter {
            path: directory.path().join("lease.json"),
            recovery_ownership: None,
            lease: Arc::new(Mutex::new(DurableJobLease::new(
                JobIdentity::new_local(),
                std::process::id(),
                None,
                None,
                0,
                false,
                false,
                "test-command-fingerprint".into(),
            ))),
        };
        writer.admit(41, &worker.id).unwrap();
        (directory, writer, worker)
    }

    fn preparation_fixture() -> (tempfile::TempDir, DurableLeaseWriter, WorkerConfig) {
        preparation_fixture_with_identity("abc123")
    }

    fn preparation_fixture_with_identity(
        identity: &str,
    ) -> (tempfile::TempDir, DurableLeaseWriter, WorkerConfig) {
        let (directory, writer, worker) = admitted_preparation_fixture();
        RecoverySession::begin(
            &writer,
            &worker,
            vec!["/data/projects/source-recovery".into()],
            None,
            None,
            None,
            TransferConfig::default(),
            directory.path().to_owned(),
            identity.into(),
        )
        .unwrap();
        (directory, writer, worker)
    }

    fn retained_preparation_fixture() -> (PathBuf, DurableLeaseWriter, WorkerConfig) {
        let (directory, writer, worker) = admitted_preparation_fixture();
        let directory = directory.keep();
        let identity = "0123456789abcdef0123456789abcdef";
        let root = "/data/tmp/rch/source-content-pair-recovery";
        let retained = format!("{root}.retained-{identity}");
        RecoverySession::begin(
            &writer,
            &worker,
            vec![root.into(), retained.clone()],
            Some((root.into(), identity.into())),
            Some(root.into()),
            Some(retained),
            TransferConfig::default(),
            directory.clone(),
            identity.into(),
        )
        .unwrap();
        (directory, writer, worker)
    }

    #[cfg(unix)]
    fn go_fixture_command(go: &str, args: &[&str], environment: &[&str]) -> String {
        shell_words::join(
            [
                "env",
                "GOENV=off",
                "GOTOOLCHAIN=local",
                "GOWORK=off",
                "GOFLAGS=",
                "GOOS=",
                "GOARCH=",
                "CGO_ENABLED=0",
                "GOMAXPROCS=1",
            ]
            .into_iter()
            .chain(environment.iter().copied())
            .chain(std::iter::once(go))
            .chain(args.iter().copied()),
        )
    }

    #[cfg(unix)]
    async fn run_go_fixture(command: &mut tokio::process::Command) -> std::process::Output {
        command.stdin(Stdio::null()).kill_on_drop(true);
        tokio::time::timeout(Duration::from_secs(120), command.output())
            .await
            .expect("Go output fixture exceeded its bound")
            .expect("Go and rsync are required for the Go delivery regression")
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn go_build_explicit_output_survives_recovery_and_missing_delivery() {
        use super::super::artifact_patterns::direct_compiler::{
            go_build_execution_command, validate_go_build_output,
        };
        use tokio::process::Command;

        let _guard = test_guard!();
        for (relative, inline_output) in [
            ("app", false),
            ("products/app [dev]*?", false),
            ("target/app", true),
        ] {
            let identity = uuid::Uuid::new_v4().to_string();
            let (_owner, writer, worker) = preparation_fixture_with_identity(&identity);
            let base = writer.path.parent().unwrap();
            let local = base.join("checkout");
            let remote = base.join("worker");
            let missing = base.join("missing-worker");
            let unrelated_target = base.join("unrelated-cargo-target");
            for root in [&local, &remote, &missing, &unrelated_target] {
                std::fs::create_dir_all(root.join("products")).unwrap();
                std::fs::create_dir_all(root.join("target")).unwrap();
            }
            for root in [&local, &remote] {
                std::fs::write(
                    root.join("go.mod"),
                    b"module fixture.invalid/output\n\ngo 1.20\n",
                )
                .unwrap();
            }
            let (source, expected_stdout): (&[u8], &[u8]) = if relative == "app" {
                // The previous output can also be a build input. Moving it
                // away before compilation would break this real embed case.
                (
                    b"package main\nimport (\"fmt\"; _ \"embed\")\nvar label = \"unset\"\n//go:embed app\nvar previous string\nfunc main() { fmt.Println(label + \":\" + previous) }\n",
                    b"go-delivered-37:old remote output\n",
                )
            } else {
                (
                    b"package main\nimport \"fmt\"\nvar label = \"unset\"\nfunc main() { fmt.Println(label) }\n",
                    b"go-delivered-37\n",
                )
            };
            std::fs::write(remote.join("main.go"), source).unwrap();
            std::fs::write(local.join("main.go"), b"local source sentinel").unwrap();
            std::fs::write(local.join(relative), b"old local output").unwrap();
            std::fs::write(remote.join(relative), b"old remote output").unwrap();
            std::fs::write(local.join("foreign.o"), b"other job's object").unwrap();
            std::fs::write(unrelated_target.join("sentinel"), b"unrelated Cargo target").unwrap();
            let output_option = format!("-o={relative}");
            let mut argv = vec![
                "build",
                "-p=1",
                "-ldflags",
                "-s -w -X main.label=go-delivered-37",
            ];
            if inline_output {
                argv.push(&output_option);
            } else {
                argv.extend(["-o", relative]);
            }
            argv.push("main.go");
            let command = go_fixture_command("go", &argv, &[]);
            assert_eq!(
                rch_common::classify_command(&command).kind,
                Some(CompilationKind::GoBuild)
            );
            let environment = validate_go_build_output(&command, &local).await.unwrap();
            assert_eq!(
                std::fs::read(local.join(relative)).unwrap(),
                b"old local output"
            );
            let pipeline = TransferPipeline::new(
                local.clone(),
                "go-fixture".into(),
                identity.clone(),
                TransferConfig::default(),
            )
            .with_remote_path_override(remote.to_str().unwrap().to_owned());
            let mut session = RecoverySession::prepare(
                &writer,
                &worker,
                &pipeline,
                vec!["/data/projects/source-recovery".into()],
                None,
                None,
                None,
                TransferConfig::default(),
                local.clone(),
                Some(&unrelated_target),
                Some(CompilationKind::GoBuild),
                &command,
                &[],
                identity,
            )
            .unwrap();
            assert!(session.has_native_output_contract());
            assert_eq!(session.recipe.phases.len(), 1);
            assert_eq!(session.recipe.phases[0].name, "project");
            assert!(session.returned(0).is_err());
            session.starting_execution().unwrap();
            let guarded = go_build_execution_command(&command, &environment).unwrap();
            let mut compiler = Command::new("sh");
            compiler.args(["-c", &guarded]).current_dir(&remote);
            let output = run_go_fixture(&mut compiler).await;
            assert!(output.status.success(), "{output:?}");
            session.completed(0).unwrap();
            let mut session = reload_publication(&session);
            assert_eq!(
                session.recipe.phases[0]
                    .native_outputs
                    .as_ref()
                    .unwrap()
                    .required_files,
                std::collections::BTreeSet::from([PathBuf::from(relative)])
            );
            for root in [&remote, &missing] {
                std::fs::write(root.join("foreign.o"), b"unrelated remote object").unwrap();
                std::fs::write(root.join("source.go"), b"unselected remote source").unwrap();
                std::fs::write(root.join("products/app d-decoy"), b"wildcard decoy").unwrap();
            }
            for complete in [false, true] {
                let stage = session.stage(0);
                let staged = session.staging_pipeline("project", &pipeline).unwrap();
                let patterns = session.cargo_artifact_patterns("project").unwrap();
                let mut rsync = staged.local_artifact_retrieval_for_test(
                    &worker,
                    if complete { &remote } else { &missing },
                    &patterns,
                );
                let output = run_go_fixture(&mut rsync).await;
                assert!(output.status.success(), "{output:?}");
                assert!(!stage.join("foreign.o").exists());
                assert!(!stage.join("source.go").exists());
                assert!(!stage.join("products/app d-decoy").exists());
                let journal_before = std::fs::read(&writer.path).unwrap();
                if !complete {
                    assert!(session.publish("project").await.is_err());
                    assert!(session.returned(0).is_err());
                    assert!(session.retain_failed_delivery_evidence());
                    assert_eq!(std::fs::read(&writer.path).unwrap(), journal_before);
                    assert_eq!(
                        std::fs::read(local.join(relative)).unwrap(),
                        b"old local output"
                    );
                    session = reload_publication(&session);
                    assert!(session.recipe.phases[0].published.is_empty());
                    assert!(session.recipe.phases[0].pending.is_none());
                } else {
                    session.publish("project").await.unwrap();
                    session.returned(0).unwrap();
                    session = reload_publication(&session);
                    assert!(session.recipe.phases[0].complete);
                    session.publish("project").await.unwrap();
                    assert_eq!(
                        std::fs::read(local.join(relative)).unwrap(),
                        std::fs::read(remote.join(relative)).unwrap()
                    );
                    let mut executable = Command::new(local.join(relative));
                    let output = run_go_fixture(&mut executable).await;
                    assert!(output.status.success(), "{output:?}");
                    assert_eq!(output.stdout, expected_stdout);
                }
            }
            assert_eq!(
                std::fs::read(local.join("main.go")).unwrap(),
                b"local source sentinel"
            );
            assert_eq!(
                std::fs::read(local.join("foreign.o")).unwrap(),
                b"other job's object"
            );
            assert_eq!(
                std::fs::read(unrelated_target.join("sentinel")).unwrap(),
                b"unrelated Cargo target"
            );
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn go_output_guards_preserve_old_files_on_invalid_or_empty_builds() {
        use super::super::artifact_patterns::direct_compiler::{
            go_build_execution_command, validate_go_build_output,
        };
        use std::os::unix::fs::{PermissionsExt, symlink};
        use tokio::process::Command;

        let _guard = test_guard!();
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path();
        std::fs::write(
            root.join("go.mod"),
            b"module fixture.invalid/guard\n\ngo 1.20\n",
        )
        .unwrap();
        std::fs::write(root.join("main.go"), b"package main\nfunc main() {}\n").unwrap();
        std::fs::write(
            root.join("broken.go"),
            b"package main\nthis is invalid Go\n",
        )
        .unwrap();
        std::fs::write(root.join("app"), b"old output must survive").unwrap();
        std::fs::create_dir(root.join("directory-output")).unwrap();
        std::fs::create_dir(root.join("outside")).unwrap();
        std::fs::write(root.join("outside/app"), b"outside sentinel").unwrap();
        symlink(root.join("outside/app"), root.join("linked-output")).unwrap();
        symlink(root.join("outside"), root.join("linked-parent")).unwrap();
        std::fs::write(root.join("file-parent"), b"parent sentinel").unwrap();
        let foreign_os = if std::env::consts::OS == "linux" {
            "GOOS=darwin"
        } else {
            "GOOS=linux"
        };
        let foreign_arch = if std::env::consts::ARCH == "x86_64" {
            "GOARCH=arm64"
        } else {
            "GOARCH=amd64"
        };
        let baseline_command =
            go_fixture_command("go", &["build", "-p=1", "-o", "app", "main.go"], &[]);
        let baseline = validate_go_build_output(&baseline_command, root)
            .await
            .unwrap();
        for (output_path, environment, reason) in [
            ("app", vec!["GOFLAGS=-n"], "empty GOFLAGS"),
            ("app", vec![foreign_os], "native GOOS/GOARCH"),
            ("app", vec![foreign_arch], "native GOOS/GOARCH"),
            ("directory-output", vec![], "regular file"),
            ("linked-output", vec![], "regular file"),
            ("linked-parent/app", vec![], "real directories"),
            ("file-parent/app", vec![], "real directories"),
        ] {
            let command = go_fixture_command(
                "go",
                &["build", "-p=1", "-o", output_path, "main.go"],
                &environment,
            );
            let error = validate_go_build_output(&command, root).await.unwrap_err();
            assert!(format!("{error:#}").contains(reason), "{error:#}");
            let guarded = go_build_execution_command(&command, &baseline).unwrap();
            let mut process = Command::new("sh");
            process.args(["-c", &guarded]).current_dir(root);
            let output = run_go_fixture(&mut process).await;
            assert_eq!(output.status.code(), Some(113), "{output:?}");
            let remote_reason = if environment.is_empty() {
                reason
            } else {
                "settings differ"
            };
            assert!(
                String::from_utf8_lossy(&output.stderr).contains(remote_reason),
                "{output:?}"
            );
            assert_eq!(
                std::fs::read(root.join("app")).unwrap(),
                b"old output must survive"
            );
            assert_eq!(
                std::fs::read(root.join("outside/app")).unwrap(),
                b"outside sentinel"
            );
        }
        // An ambient build setting that is absent on the worker is a mismatch,
        // even when both endpoints still report the same GOOS/GOARCH.
        for changed in ["CGO_ENABLED=1", "GOEXPERIMENT=none", "GOAMD64=v2"] {
            let command =
                go_fixture_command("go", &["build", "-p=1", "-o", "app", "main.go"], &[changed]);
            validate_go_build_output(&command, root).await.unwrap();
            let guarded = go_build_execution_command(&command, &baseline).unwrap();
            let mut process = Command::new("sh");
            process.args(["-c", &guarded]).current_dir(root);
            let output = run_go_fixture(&mut process).await;
            assert_eq!(output.status.code(), Some(113), "{changed}: {output:?}");
            assert!(String::from_utf8_lossy(&output.stderr).contains("settings differ"));
            assert_eq!(
                std::fs::read(root.join("app")).unwrap(),
                b"old output must survive"
            );
        }
        // Matching CGO_ENABLED=1 remains supported; the guard does not force
        // otherwise ordinary Go builds into a pure-Go-only policy.
        let cgo_command = go_fixture_command(
            "go",
            &["build", "-p=1", "-o", "cgo-enabled-app", "main.go"],
            &["CGO_ENABLED=1"],
        );
        let cgo_environment = validate_go_build_output(&cgo_command, root).await.unwrap();
        let guarded = go_build_execution_command(&cgo_command, &cgo_environment).unwrap();
        let mut process = Command::new("sh");
        process.args(["-c", &guarded]).current_dir(root);
        let output = run_go_fixture(&mut process).await;
        assert!(output.status.success(), "{output:?}");
        assert!(root.join("cgo-enabled-app").is_file());

        // Both are real Go failures, including the explicit -o/no-packages
        // case. Neither may replace a previous successful build's output.
        for package in ["broken.go", "./missing/..."] {
            let command = go_fixture_command("go", &["build", "-p=1", "-o", "app", package], &[]);
            let environment = validate_go_build_output(&command, root).await.unwrap();
            let guarded = go_build_execution_command(&command, &environment).unwrap();
            let mut process = Command::new("sh");
            process.args(["-c", &guarded]).current_dir(root);
            let output = run_go_fixture(&mut process).await;
            assert_eq!(output.status.code(), Some(1), "{output:?}");
            assert_eq!(
                std::fs::read(root.join("app")).unwrap(),
                b"old output must survive"
            );
        }

        // Package loading must see exactly the caller's embed inputs. An
        // adjacent empty staging directory or a newly created output parent
        // changes `go:embed *` and makes an otherwise valid package fail.
        for (fixture, output_path) in [
            ("wildcard-root-output", "app"),
            ("wildcard-new-parent", "new-products/app"),
        ] {
            let project = root.join(fixture);
            std::fs::create_dir(&project).unwrap();
            std::fs::write(
                project.join("go.mod"),
                b"module fixture.invalid/embed\n\ngo 1.20\n",
            )
            .unwrap();
            std::fs::write(project.join("app"), b"embedded prior output").unwrap();
            std::fs::write(
                project.join("main.go"),
                b"package main\nimport (\"embed\"; \"fmt\")\n//go:embed *\nvar inputs embed.FS\nfunc main() { previous, err := inputs.ReadFile(\"app\"); if err != nil { panic(err) }; fmt.Println(string(previous)) }\n",
            )
            .unwrap();
            assert!(!project.join("new-products").exists());
            let command = go_fixture_command("go", &["build", "-p=1", "-o", output_path, "."], &[]);
            let environment = validate_go_build_output(&command, &project).await.unwrap();
            let guarded = go_build_execution_command(&command, &environment).unwrap();
            let mut compiler = Command::new("sh");
            compiler.args(["-c", &guarded]).current_dir(&project);
            let output = run_go_fixture(&mut compiler).await;
            assert!(output.status.success(), "{fixture}: {output:?}");
            let mut executable = Command::new(project.join(output_path));
            let output = run_go_fixture(&mut executable).await;
            assert!(output.status.success(), "{fixture}: {output:?}");
            assert_eq!(output.stdout, b"embedded prior output\n");
            if output_path != "app" {
                assert_eq!(
                    std::fs::read(project.join("app")).unwrap(),
                    b"embedded prior output"
                );
            }
        }

        // Failure injection: a Go shim reports success but creates no file.
        // Its env probe forwards to the actual compiler; an
        // old destination must still not satisfy the fresh-output contract.
        let mut real_env = Command::new("sh");
        real_env
            .args(["-c", &go_fixture_command("go", &["env", "GOROOT"], &[])])
            .current_dir(root);
        let goroot = run_go_fixture(&mut real_env).await;
        assert!(goroot.status.success(), "{goroot:?}");
        let go = PathBuf::from(String::from_utf8(goroot.stdout).unwrap().trim()).join("bin/go");
        let go = shell_escape::escape(go.to_string_lossy());
        let shim = root.join("shim/go");
        std::fs::create_dir(shim.parent().unwrap()).unwrap();
        std::fs::write(
            &shim,
            format!("#!/bin/sh\nif [ \"$1\" = env ]; then exec {go} \"$@\"; fi\nexit 0\n"),
        )
        .unwrap();
        std::fs::set_permissions(&shim, std::fs::Permissions::from_mode(0o755)).unwrap();
        let command = go_fixture_command(
            shim.to_str().unwrap(),
            &["build", "-o", "app", "main.go"],
            &[],
        );
        let environment = validate_go_build_output(&command, root).await.unwrap();
        let guarded = go_build_execution_command(&command, &environment).unwrap();
        let mut process = Command::new("sh");
        process.args(["-c", &guarded]).current_dir(root);
        let output = run_go_fixture(&mut process).await;
        assert_eq!(output.status.code(), Some(113), "{output:?}");
        assert!(
            String::from_utf8_lossy(&output.stderr).contains("without its required output file")
        );
        assert_eq!(
            std::fs::read(root.join("app")).unwrap(),
            b"old output must survive"
        );
        assert!(!std::fs::read_dir(root).unwrap().any(|entry| {
            entry
                .unwrap()
                .file_name()
                .to_string_lossy()
                .starts_with(".rch-go-output.")
        }));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn real_native_outputs_are_complete_before_publication_and_survive_recovery() {
        use tokio::process::Command;
        async fn run(command: &mut Command) -> std::process::Output {
            command.stdin(Stdio::null()).kill_on_drop(true);
            let output = tokio::time::timeout(Duration::from_secs(20), command.output())
                .await
                .expect("native output fixture exceeded its bound")
                .expect("GCC and rsync are required for the native delivery regression");
            assert!(output.status.success(), "{output:?}");
            output
        }

        let _guard = test_guard!();
        for (argv, expected, missing_index) in [
            (vec!["main.c", "lib/helper.c"], vec!["a.out"], 0),
            (
                vec!["-c", "main.c", "lib/helper.c"],
                vec!["helper.o", "main.o"],
                1,
            ),
            (
                vec!["-S", "main.c", "lib/helper.c"],
                vec!["helper.s", "main.s"],
                1,
            ),
            (
                vec![
                    "main.c",
                    "lib/helper.c",
                    "-o",
                    "products/app[dev]*?",
                    "-MMD",
                    "-MF",
                    "products/app.d",
                ],
                vec!["products/app.d", "products/app[dev]*?"],
                1,
            ),
            (
                vec![
                    "main.c",
                    "lib/helper.c",
                    "-o",
                    "target/app[dev]*?",
                    "-MMD",
                    "-MF",
                    "target/app.d",
                ],
                vec!["target/app.d", "target/app[dev]*?"],
                0,
            ),
        ] {
            let identity = uuid::Uuid::new_v4().to_string();
            let (_owner, writer, worker) = preparation_fixture_with_identity(&identity);
            let base = writer.path.parent().unwrap();
            let local = base.join("checkout");
            let remote = base.join("worker");
            let partial = base.join("partial-worker");
            let unrelated_target = base.join("unrelated-cargo-target");
            for path in [&local, &remote, &partial, &unrelated_target] {
                std::fs::create_dir_all(path.join("products")).unwrap();
                std::fs::create_dir_all(path.join("target")).unwrap();
            }
            std::fs::create_dir(remote.join("lib")).unwrap();
            std::fs::write(remote.join("main.c"),
                b"#include <stdio.h>\nint helper(void); int main(void) { printf(\"native-%d\\n\", helper()); return 0; }\n").unwrap();
            std::fs::write(
                remote.join("lib/helper.c"),
                b"int helper(void) { return 37; }\n",
            )
            .unwrap();
            std::fs::write(local.join("main.c"), b"local source sentinel").unwrap();
            std::fs::write(local.join("foreign.o"), b"other job's object").unwrap();
            std::fs::write(unrelated_target.join("sentinel"), b"unrelated Cargo target").unwrap();
            for relative in &expected {
                std::fs::write(local.join(relative), b"old local output").unwrap();
            }
            let command = shell_words::join(std::iter::once("gcc").chain(argv.iter().copied()));
            let pipeline = TransferPipeline::new(
                local.clone(),
                "native-fixture".into(),
                identity.clone(),
                TransferConfig::default(),
            )
            .with_remote_path_override(remote.to_str().unwrap().to_owned());
            let mut session = RecoverySession::prepare(
                &writer,
                &worker,
                &pipeline,
                vec!["/data/projects/source-recovery".into()],
                None,
                None,
                None,
                TransferConfig::default(),
                local.clone(),
                Some(&unrelated_target),
                Some(CompilationKind::Gcc),
                &command,
                &[],
                identity,
            )
            .unwrap();
            assert!(session.has_native_output_contract());
            assert_eq!(
                session.recipe.phases.len(),
                1,
                "ambient Cargo target must not displace native outputs"
            );
            assert_eq!(session.recipe.phases[0].name, "project");
            assert!(
                !session.recipe.phases[0].output_gate,
                "native completeness is independent of Cargo gating"
            );
            assert!(
                session.returned(0).is_err(),
                "pre-execution contract already forbids empty success"
            );
            session.starting_execution().unwrap();
            let mut compiler = Command::new("gcc");
            compiler
                .args(&argv)
                .current_dir(&remote)
                .env_remove("DEPENDENCIES_OUTPUT")
                .env_remove("SUNPRO_DEPENDENCIES");
            run(&mut compiler).await;
            session.completed(0).unwrap();
            let mut session = reload_publication(&session);
            assert_eq!(
                session.recipe.phases[0]
                    .native_outputs
                    .as_ref()
                    .unwrap()
                    .required_files,
                expected
                    .iter()
                    .map(PathBuf::from)
                    .collect::<std::collections::BTreeSet<_>>()
            );

            // A successful local rsync from this incomplete worker view must
            // not turn one object/depfile into proof of the entire contract.
            let missing = &expected[missing_index];
            for relative in expected.iter().filter(|relative| *relative != missing) {
                std::fs::copy(remote.join(relative), partial.join(relative)).unwrap();
            }
            for root in [&remote, &partial] {
                std::fs::write(root.join("foreign.o"), b"unrelated remote object").unwrap();
                std::fs::write(root.join("source.c"), b"unselected remote source").unwrap();
            }
            for complete in [false, true] {
                let stage = session.stage(0);
                let staged = session.staging_pipeline("project", &pipeline).unwrap();
                let patterns = session.cargo_artifact_patterns("project").unwrap();
                let mut rsync = staged.local_artifact_retrieval_for_test(
                    &worker,
                    if complete { &remote } else { &partial },
                    &patterns,
                );
                run(&mut rsync).await;
                assert!(!stage.join("foreign.o").exists());
                assert!(!stage.join("source.c").exists());
                let journal_before = std::fs::read(&writer.path).unwrap();
                if !complete {
                    assert!(session.publish("project").await.is_err());
                    assert_eq!(
                        std::fs::read(&writer.path).unwrap(),
                        journal_before,
                        "no durable publication frontier crossed"
                    );
                    assert!(session.returned(0).is_err());
                    assert!(session.retain_failed_delivery_evidence());
                    for relative in &expected {
                        assert_eq!(
                            std::fs::read(local.join(relative)).unwrap(),
                            b"old local output"
                        );
                    }
                    session = reload_publication(&session);
                    assert!(session.recipe.phases[0].published.is_empty());
                    assert!(session.recipe.phases[0].pending.is_none());
                } else {
                    session.publish("project").await.unwrap();
                    session.returned(0).unwrap();
                    session = reload_publication(&session);
                    assert!(session.recipe.phases[0].complete);
                    assert_eq!(session.recipe.phases[0].published.len(), expected.len());
                    session.publish("project").await.unwrap();
                    for relative in &expected {
                        assert_eq!(
                            std::fs::read(local.join(relative)).unwrap(),
                            std::fs::read(remote.join(relative)).unwrap()
                        );
                    }
                    if argv[0] != "-c" && argv[0] != "-S" {
                        let mut executable = Command::new(local.join(expected.last().unwrap()));
                        assert_eq!(run(&mut executable).await.stdout, b"native-37\n");
                    }
                }
            }
            assert_eq!(
                std::fs::read(local.join("main.c")).unwrap(),
                b"local source sentinel"
            );
            assert_eq!(
                std::fs::read(local.join("foreign.o")).unwrap(),
                b"other job's object"
            );
            assert_eq!(
                std::fs::read(unrelated_target.join("sentinel")).unwrap(),
                b"unrelated Cargo target"
            );
        }
    }

    fn publication_fixture() -> (tempfile::TempDir, tempfile::TempDir, RecoverySession) {
        let (directory, writer, _worker) = preparation_fixture();
        let stages = default_job_lease_directory().join("retrieval");
        std::fs::create_dir_all(&stages).unwrap();
        let stage_owner = tempfile::Builder::new()
            .prefix("publication-regression-")
            .disable_cleanup(true)
            .tempdir_in(stages)
            .unwrap();
        let mut recipe = load_recipe(&writer).unwrap();
        recipe.identity = stage_owner
            .path()
            .file_name()
            .unwrap()
            .to_str()
            .unwrap()
            .to_owned();
        recipe.project_root = directory.path().join("checkout");
        std::fs::create_dir(&recipe.project_root).unwrap();
        recipe.phases.push(RecoveryPhase {
            name: "project".into(),
            local: recipe.project_root.clone(),
            remote: "/unused/remote".into(),
            patterns: default_c_cpp_artifact_patterns(),
            result_dir: None,
            custom_target: false,
            output_gate: false,
            cargo_outputs: None,
            native_outputs: None,
            baseline: BTreeMap::new(),
            published: BTreeMap::new(),
            pending: None,
            complete: false,
            missing_result: None,
        });
        let session = RecoverySession { recipe, writer };
        std::fs::create_dir(session.stage(0)).unwrap();
        (directory, stage_owner, session)
    }

    #[test]
    fn clean_overlay_recovery_config_persists_extensions_and_retains_old_stage() {
        let (_directory, _stage_owner, mut session) = publication_fixture();
        let old = session.stage(0);
        std::fs::write(old.join("worker-output"), b"retained").unwrap();
        session.recipe.transfer.max_transfer_time_ms = Some(138_000);
        let new = tempfile::tempdir().unwrap().keep();
        let config = TransferConfig {
            max_transfer_time_ms: Some(900_000),
            retrieval_staging_dir: Some(new.clone()),
            retrieval_min_free_bytes: 64 * 1024 * 1024,
            ..Default::default()
        };
        let original_identity = session.recipe.identity.clone();
        let original_phases = serde_json::to_value(&session.recipe.phases).unwrap();
        session.apply_retrieval_config(&config).unwrap();
        let mut reloaded = reload_publication(&session);
        assert_eq!(reloaded.recipe.transfer.max_transfer_time_ms, Some(900_000));
        assert_eq!(reloaded.stage(0), new.join(&original_identity).join("0"));
        assert_eq!(
            serde_json::to_value(&reloaded.recipe.phases).unwrap(),
            original_phases
        );
        assert!(!reloaded.writer.snapshot().terminal_acknowledged);
        assert_eq!(
            std::fs::read(old.join("worker-output")).unwrap(),
            b"retained"
        );
        reloaded
            .apply_retrieval_config(&TransferConfig {
                max_transfer_time_ms: Some(1),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(
            load_recipe(&reloaded.writer)
                .unwrap()
                .transfer
                .max_transfer_time_ms,
            Some(900_000)
        );
        assert_eq!(
            load_recipe(&reloaded.writer)
                .unwrap()
                .transfer
                .retrieval_min_free_bytes,
            64 * 1024 * 1024
        );
        reloaded.recipe.phases[0].pending = Some((PathBuf::from("worker-output"), "hash".into()));
        assert!(
            reloaded
                .apply_retrieval_config(&TransferConfig {
                    retrieval_staging_dir: Some(old),
                    ..Default::default()
                })
                .is_err()
        );
        assert_eq!(reloaded.stage(0), new.join(original_identity).join("0"));
        let journal = std::fs::read(&reloaded.writer.path).unwrap();
        for invalid in [
            TransferConfig {
                retrieval_staging_dir: Some(PathBuf::from("relative")),
                ..Default::default()
            },
            TransferConfig {
                max_transfer_time_ms: Some(u64::MAX),
                ..Default::default()
            },
        ] {
            assert!(reloaded.apply_retrieval_config(&invalid).is_err());
            assert_eq!(std::fs::read(&reloaded.writer.path).unwrap(), journal);
        }
    }

    #[test]
    fn clean_overlay_recovery_extension_preserves_adaptive_budget() {
        for sentinel in [None, Some(0)] {
            for (initial_floor, requested, expected_floor) in [
                (138_000, 900_000, 900_000),
                (1_200_000, 900_000, 1_200_000),
                (900_000, 900_000, 900_000),
                (900_000, 0, 900_000),
            ] {
                let (_directory, _stage_owner, mut session) = publication_fixture();
                session.recipe.transfer.max_transfer_time_ms = sentinel;
                session.recipe.transfer.retry.total_timeout_ms = initial_floor;
                session.persist().unwrap();
                let mut expected = serde_json::to_value(&session.recipe).unwrap();
                expected["transfer"]["retry"]["total_timeout_ms"] = expected_floor.into();
                let stage = session.stage(0);
                std::fs::write(stage.join("worker-output"), b"retained output").unwrap();
                session
                    .apply_retrieval_config(&TransferConfig {
                        max_transfer_time_ms: Some(requested),
                        ..Default::default()
                    })
                    .unwrap();
                let mut reloaded = reload_publication(&session);
                assert_eq!(reloaded.recipe.transfer.max_transfer_time_ms, sentinel);
                assert_eq!(
                    reloaded.recipe.transfer.retry.total_timeout_ms,
                    expected_floor
                );
                // Compare the whole recipe, including worker/source authority,
                // output policy, publication checkpoints and original exit.
                assert_eq!(serde_json::to_value(&reloaded.recipe).unwrap(), expected);
                assert_eq!(reloaded.stage(0), stage);
                assert_eq!(
                    std::fs::read(stage.join("worker-output")).unwrap(),
                    b"retained output"
                );
                assert!(!reloaded.writer.snapshot().terminal_acknowledged);
                assert!(reloaded.recipe.returned.is_none());
                assert!(!reloaded.recipe.phases[0].complete);
                // A retry without an invocation override keeps the extension.
                reloaded
                    .apply_retrieval_config(&TransferConfig::default())
                    .unwrap();
                let reloaded = reload_publication(&reloaded);
                assert_eq!(serde_json::to_value(&reloaded.recipe).unwrap(), expected);
            }
        }
    }

    #[tokio::test]
    async fn clean_overlay_recovery_publication_skips_old_archive_without_acknowledging() {
        let _guard = test_guard!();
        let (_directory, _stage_owner, mut session) = publication_fixture();
        let phases = session.recipe.phases.clone();
        let project_root = session.recipe.project_root.clone();
        let (_retained_directory, retained_writer, _worker) = retained_preparation_fixture();
        session.recipe = load_recipe(&retained_writer).unwrap();
        session.recipe.phases = phases;
        session.recipe.project_root = project_root;
        session.writer = retained_writer;
        // Reproduce a persisted recipe from before the reserved exclusion.
        session.recipe.phases[0].patterns = vec!["*".into(), "build/**".into()];
        let stage = session.stage(0);
        std::fs::create_dir_all(&stage).unwrap();
        std::fs::write(
            stage.join(".rch-clean-overlay-base.tar"),
            b"immutable input",
        )
        .unwrap();
        std::fs::write(stage.join("a.out"), b"worker executable").unwrap();
        session.publish("project").await.unwrap();
        assert_eq!(
            std::fs::read(session.recipe.project_root.join("a.out")).unwrap(),
            b"worker executable"
        );
        assert!(
            !session
                .recipe
                .project_root
                .join(".rch-clean-overlay-base.tar")
                .exists()
        );
        assert!(!session.writer.snapshot().terminal_acknowledged);
        assert!(!session.recipe.sources_released);
        assert!(session.recipe.phases[0].complete);
        assert_eq!(
            std::fs::read(stage.join(".rch-clean-overlay-base.tar")).unwrap(),
            b"immutable input"
        );
    }

    fn reload_publication(session: &RecoverySession) -> RecoverySession {
        let writer = DurableLeaseWriter {
            path: session.writer.path.clone(),
            recovery_ownership: None,
            lease: Arc::new(Mutex::new(
                serde_json::from_slice(&std::fs::read(&session.writer.path).unwrap()).unwrap(),
            )),
        };
        RecoverySession {
            recipe: load_recipe(&writer).unwrap(),
            writer,
        }
    }

    fn captured_publication_fixture(
        kind: CompilationKind,
        command: &str,
        remote_root: &Path,
        custom_target: bool,
    ) -> (tempfile::TempDir, tempfile::TempDir, RecoverySession) {
        let (directory, stage_owner, mut session) = publication_fixture();
        let kind = Some(kind);
        session.recipe.kind = artifact_delivery_kind(kind, Some(command));
        session.recipe.prepared = true;
        session.recipe.execution_started = true;
        session.recipe.exit_code = Some(0);
        session.recipe.cargo_output_capture = CargoOutputCapture::for_command(kind, command);
        assert!(session.recipe.cargo_output_capture.is_some());
        let phase = &mut session.recipe.phases[0];
        phase.name = if custom_target { "target" } else { "project" }.into();
        phase.remote = remote_root.to_str().unwrap().to_owned();
        phase.custom_target = custom_target;
        phase.output_gate = true;
        phase.patterns = if custom_target {
            get_custom_target_artifact_patterns(kind, Some(command))
        } else {
            get_project_artifact_patterns(kind, Some(command), false)
        };
        session.persist().unwrap();
        (directory, stage_owner, session)
    }

    pub(super) async fn assert_cargo_fixture_publication(
        kind: CompilationKind,
        command: &str,
        stdout: &[u8],
        remote_root: &Path,
        custom_target: bool,
    ) {
        let (_directory, _stage_owner, session) =
            captured_publication_fixture(kind, command, remote_root, custom_target);
        // A detached collector must remember that evidence is still owed,
        // even if the wrapper stopped immediately after remote completion.
        let mut session = reload_publication(&session);
        assert!(session.needs_cargo_artifact_evidence());
        session
            .install_complete_cargo_evidence(&crate::transfer::CargoArtifactEvidence {
                stdout: stdout.to_vec(),
                remote_root: remote_root.to_path_buf(),
                remote_project_root: None,
            })
            .unwrap();
        let mut session = reload_publication(&session);
        let retained = session.retained_cargo_artifact_evidence().unwrap().unwrap();
        assert_eq!(retained.stdout, stdout);
        assert_eq!(retained.remote_root, remote_root);
        assert!(retained.remote_project_root.is_none());
        assert!(!session.needs_cargo_artifact_evidence());
        let required = session.recipe.phases[0]
            .cargo_outputs
            .as_ref()
            .unwrap()
            .required_files
            .clone();
        assert_eq!(
            required.len(),
            2,
            "fixture must select two named executables"
        );
        let missing = required
            .iter()
            .nth(1)
            .expect("fixture must contain a second executable");
        let local = session.recipe.phases[0].local.clone();
        let phase_name = session.recipe.phases[0].name.clone();
        let stage = session.stage(0);
        for relative in &required {
            let destination = local.join(relative);
            std::fs::create_dir_all(destination.parent().unwrap()).unwrap();
            std::fs::write(&destination, b"previous local executable").unwrap();
            if relative != missing {
                let staged = stage.join(relative);
                std::fs::create_dir_all(staged.parent().unwrap()).unwrap();
                std::fs::copy(remote_root.join(relative), staged).unwrap();
            }
        }
        // A genuine non-empty support-file transfer used to hide a missing
        // requested executable from the old zero-output gate.
        let support = missing.parent().unwrap().join("deps/unrelated-runtime.so");
        std::fs::create_dir_all(stage.join(&support).parent().unwrap()).unwrap();
        std::fs::write(
            stage.join(&support),
            b"runtime support is not a requested binary",
        )
        .unwrap();
        let journal = std::fs::read(&session.writer.path).unwrap();
        assert!(session.publish(&phase_name).await.is_err());
        for relative in &required {
            assert_eq!(
                std::fs::read(local.join(relative)).unwrap(),
                b"previous local executable",
                "a missing executable must not partially replace {}",
                relative.display()
            );
        }
        assert!(!local.join(&support).exists());
        assert!(stage.join(&support).is_file());
        assert_eq!(std::fs::read(&session.writer.path).unwrap(), journal);
        assert!(session.recipe.phases[0].published.is_empty());
        assert!(!session.recipe.phases[0].complete);
        assert!(session.returned(0).is_err());

        // Supply the missing output on the same attempt, then recover solely
        // from the disk journal and staged bytes. No command is replayed.
        let mut resumed = reload_publication(&session);
        std::fs::create_dir_all(stage.join(missing).parent().unwrap()).unwrap();
        std::fs::copy(remote_root.join(missing), stage.join(missing)).unwrap();
        resumed.publish(&phase_name).await.unwrap();
        for relative in &required {
            assert_eq!(
                std::fs::read(local.join(relative)).unwrap(),
                std::fs::read(remote_root.join(relative)).unwrap()
            );
        }
        resumed.returned(0).unwrap();
        let finished = reload_publication(&resumed);
        assert!(finished.recipe.phases[0].complete);
        assert_eq!(finished.recipe.returned, Some(0));
        assert_eq!(
            finished.recipe.phases[0]
                .cargo_outputs
                .as_ref()
                .unwrap()
                .required_files,
            required
        );
        assert!(!stage.exists());
    }

    fn cargo_bin_receipt(root: &Path, custom_target: bool) -> Vec<u8> {
        let mut receipt = Vec::new();
        for name in ["app", "helper"] {
            let path = root
                .join(if custom_target { "lean" } else { "target/lean" })
                .join(name);
            let record = serde_json::json!({
                "reason": "compiler-artifact",
                "target": {"name": name, "kind": ["bin"]},
                "filenames": [path], "executable": path, "fresh": true
            });
            receipt.extend(serde_json::to_vec(&record).unwrap());
            receipt.push(b'\n');
        }
        receipt.extend_from_slice(b"{\"reason\":\"build-finished\",\"success\":true}\n");
        receipt
    }

    // These are retained journal/byte-binding regressions, not SSH, compiler,
    // native SDK or admission proof. They run only after the real quality gates
    // admit test execution; all fixture owners remain retained.
    #[test]
    fn ordinary_cargo_retains_complete_producer_across_contract_recovery() {
        let remote_root = Path::new("/retained-cargo-output");
        let (directory, stage_owner, mut session) = captured_publication_fixture(
            CompilationKind::CargoBuild,
            "cargo build --bin app --bin helper --profile lean",
            remote_root,
            false,
        );
        let _directory = directory.keep();
        let _stage = stage_owner.keep();
        let stdout = cargo_bin_receipt(remote_root, false);
        assert!(session.recipe.caller_json_policy.is_none());
        assert!(
            session
                .retained_cargo_artifact_evidence()
                .unwrap()
                .is_none()
        );
        // Model interruption after producer custody, before contract install.
        session
            .retain_cargo_stdout(&stdout, remote_root, None, 0)
            .unwrap();
        let mut recovered = reload_publication(&session);
        assert!(recovered.needs_cargo_artifact_evidence());
        let retained = recovered
            .retained_cargo_artifact_evidence()
            .unwrap()
            .unwrap();
        assert_eq!(retained.stdout, stdout);
        assert_eq!(retained.remote_root, remote_root);
        assert!(retained.remote_project_root.is_none());
        recovered
            .install_complete_cargo_evidence(&retained)
            .unwrap();
        let completed = reload_publication(&recovered);
        assert!(!completed.needs_cargo_artifact_evidence());
        assert_eq!(
            completed
                .retained_cargo_artifact_evidence()
                .unwrap()
                .unwrap()
                .stdout,
            stdout
        );
        assert!(completed.recipe.caller_json_policy.is_none());
        assert!(completed.recipe.cargo_stderr.is_none());
        assert!(completed.recipe.caller_delivery.is_none());
    }

    #[test]
    fn retained_cargo_corruption_and_missing_bytes_do_not_allow_rebaselining() {
        let remote_root = Path::new("/retained-cargo-output");
        let (directory, stage_owner, mut session) = captured_publication_fixture(
            CompilationKind::CargoBuild,
            "cargo build --bin app --bin helper --profile lean",
            remote_root,
            false,
        );
        let _directory = directory.keep();
        let _stage = stage_owner.keep();
        let stdout = cargo_bin_receipt(remote_root, false);
        session
            .install_complete_cargo_evidence(&crate::transfer::CargoArtifactEvidence {
                stdout: stdout.clone(),
                remote_root: remote_root.to_path_buf(),
                remote_project_root: None,
            })
            .unwrap();
        let path = session.recipe.cargo_stdout.as_ref().unwrap().path.clone();
        // A wrong present file exercises the immutable byte pin without
        // truncating the original retained producer evidence.
        let corrupt_path = _directory.join("wrong-present-producer");
        let corrupt_bytes = b"changed same-attempt producer bytes";
        OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&corrupt_path)
            .unwrap()
            .write_all(corrupt_bytes)
            .unwrap();
        session.recipe.cargo_stdout.as_mut().unwrap().path = corrupt_path.clone();
        session.persist().unwrap();
        let mut corrupted = reload_publication(&session);
        assert!(!corrupted.needs_cargo_artifact_evidence());
        assert!(corrupted.retained_cargo_artifact_evidence().is_err());
        assert!(
            corrupted
                .retain_cargo_stdout(&stdout, remote_root, None, 0)
                .is_err()
        );
        assert_eq!(
            corrupted.recipe.cargo_stdout.as_ref().unwrap().path,
            corrupt_path
        );
        assert_eq!(std::fs::read(&path).unwrap(), stdout);
        assert_eq!(std::fs::read(&corrupt_path).unwrap(), corrupt_bytes);
        // Preserve both original and wrong files while making the recorded
        // path absent. This is a missing-path probe, not a deletion probe.
        corrupted.recipe.cargo_stdout.as_mut().unwrap().path = _directory.join("absent-producer");
        corrupted.persist().unwrap();
        let mut missing = reload_publication(&corrupted);
        assert!(missing.retained_cargo_artifact_evidence().is_err());
        assert!(
            missing
                .retain_cargo_stdout(&stdout, remote_root, None, 0)
                .is_err()
        );
        missing.recipe.exit_code = Some(7);
        missing.persist().unwrap();
        assert!(load_recipe(&missing.writer).is_err());
    }

    #[test]
    fn legacy_caller_receipt_preserves_its_observed_source_root() {
        let receipt: RetainedCargoStdout = serde_json::from_value(serde_json::json!({
            "path": "/retained/producer",
            "blake3": blake3::hash(b"").to_hex().to_string(),
            "bytes": 0,
            "remote_root": "/worker/target",
            "remote_project_root": "/worker/source",
            "exit_code": 0,
        }))
        .unwrap();
        assert_eq!(
            receipt.remote_project_root.as_deref(),
            Some(Path::new("/worker/source"))
        );
    }

    #[test]
    fn retained_cargo_root_preserves_the_worker_transport_spelling() {
        // This stores declared receipt data; it does not prove a Windows
        // compiler/transport executed. The coordinator must not reinterpret a
        // worker's drive spelling with its own Unix Path::is_absolute rules.
        let remote_root = Path::new("C:/rch/retained-cargo-output");
        let (directory, stage_owner, mut session) = captured_publication_fixture(
            CompilationKind::CargoBuild,
            "cargo build --bin app --bin helper --profile lean",
            remote_root,
            false,
        );
        let _directory = directory.keep();
        let _stage = stage_owner.keep();
        let stdout = cargo_bin_receipt(remote_root, false);
        session
            .retain_cargo_stdout(&stdout, remote_root, None, 0)
            .unwrap();
        let recovered = reload_publication(&session);
        let retained = recovered
            .retained_cargo_artifact_evidence()
            .unwrap()
            .unwrap();
        assert_eq!(retained.remote_root, remote_root);
        assert_eq!(retained.stdout, stdout);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn terminal_cargo_recovery_checks_original_local_producer_before_shortcuts() {
        // Real held journal ownership and local files exercise the production
        // recovery consumer. Job fields below are unit inputs, not evidence
        // that a worker/compiler/SDK ran. No worker or daemon may be contacted.
        // Successful returned-but-unacknowledged recovery needs real daemon
        // reconciliation and remains in that service lane. These seven cases
        // are exactly the local terminal-readback/early-refusal coverage.
        for (acknowledged, receipt_case) in [
            (true, "valid"),
            (true, "wrong-present"),
            (true, "missing"),
            (true, "legacy-absent"),
            (true, "failed-delivery-unbound"),
            (false, "wrong-present"),
            (false, "missing"),
        ] {
            // A successful returned-but-unacknowledged negative needs an
            // absent real wrapper. Let an owned process exit naturally;
            // never fake owner absence or signal a process to force it.
            let mut wrapper = std::process::Command::new("/usr/bin/true").spawn().unwrap();
            let wrapper_pid = wrapper.id();
            assert!(wrapper.wait().unwrap().success());
            let remote_root = Path::new("/retained-cargo-output");
            let (directory, stage_owner, mut session) = captured_publication_fixture(
                CompilationKind::CargoBuild,
                "cargo build --bin app --bin helper --profile lean",
                remote_root,
                false,
            );
            let directory = directory.keep();
            let _stage = stage_owner.keep();
            let stdout = cargo_bin_receipt(remote_root, false);
            session
                .retain_cargo_stdout(&stdout, remote_root, None, 0)
                .unwrap();
            let original = session.recipe.cargo_stdout.as_ref().unwrap().path.clone();
            let delivered_exit = if receipt_case == "failed-delivery-unbound" {
                EXIT_ARTIFACT_TRANSFER_FAILED
            } else {
                0
            };
            session.recipe.returned = Some(delivered_exit);
            session.recipe.phases[0].complete = true;
            session.recipe.tree_retired = true;
            session.recipe.pair_released = true;
            session.recipe.sources_released = true;
            session.recipe.retired = true;
            match receipt_case {
                "wrong-present" | "failed-delivery-unbound" => {
                    let path = directory.join("wrong-present-producer");
                    OpenOptions::new()
                        .write(true)
                        .create_new(true)
                        .open(&path)
                        .unwrap()
                        .write_all(b"unbound producer bytes")
                        .unwrap();
                    session.recipe.cargo_stdout.as_mut().unwrap().path = path;
                }
                "missing" => {
                    session.recipe.cargo_stdout.as_mut().unwrap().path =
                        directory.join("absent-producer")
                }
                "legacy-absent" => session.recipe.cargo_stdout = None,
                "valid" => {}
                _ => unreachable!(),
            }
            {
                let mut lease = session.writer.lease.lock().unwrap();
                lease.wrapper_pid = wrapper_pid;
                lease.process_start_ticks = None;
                lease.boot_id = None;
                lease.process_birth = None;
            }
            session.persist().unwrap();
            session.writer.record_exit(delivered_exit).unwrap();
            if acknowledged {
                session.writer.acknowledge_terminal().unwrap();
            }
            let journal = std::fs::read(&session.writer.path).unwrap();
            let result = recover_job_with_daemon(&session.writer, async |_request: &str| {
                panic!("terminal producer check unexpectedly contacted daemon")
            })
            .await;
            if matches!(receipt_case, "wrong-present" | "missing") {
                let error = result.expect_err("invalid producer must not borrow terminal success");
                let description = format!("{error:#}");
                if receipt_case == "wrong-present" {
                    assert!(
                        description.contains(
                            "retained Cargo stdout bytes failed their exact invocation binding"
                        ),
                        "{receipt_case} did not reach the producer binding check: {description}"
                    );
                } else {
                    assert!(
                        description
                            .contains("retained Cargo stdout producer metadata is unavailable")
                            && error.chain().any(|cause| {
                                cause.downcast_ref::<std::io::Error>().is_some_and(|error| {
                                    error.kind() == std::io::ErrorKind::NotFound
                                })
                            }),
                        "{receipt_case} did not reach the producer read: {description}"
                    );
                }
            } else {
                assert_eq!(result.unwrap(), delivered_exit);
            }
            assert_eq!(std::fs::read(&original).unwrap(), stdout);
            assert_eq!(std::fs::read(&session.writer.path).unwrap(), journal);
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn cargo_no_run_delivers_every_real_named_test_and_bench_after_recovery() {
        use std::process::Stdio;
        use tokio::process::Command;

        for (kind, subcommand, selector, target_kind, custom_target) in [
            (CompilationKind::CargoTest, "test", "test", "test", false),
            (CompilationKind::CargoTest, "test", "test", "test", true),
            (
                CompilationKind::CargoBench,
                "bench",
                "bench",
                "bench",
                false,
            ),
            (CompilationKind::CargoBench, "bench", "bench", "bench", true),
        ] {
            let fixture = tempfile::tempdir().unwrap();
            let source = fixture.path().join("source project");
            for directory in ["src", "tests", "benches"] {
                std::fs::create_dir_all(source.join(directory)).unwrap();
            }
            std::fs::write(source.join("Cargo.toml"), concat!(
                "[package]\nname = \"rch_named_no_run_fixture\"\nversion = \"0.0.0\"\nedition = \"2024\"\n",
                "[workspace]\n",
                "[[test]]\nname = \"alpha\"\npath = \"tests/alpha.rs\"\n",
                "[[test]]\nname = \"beta\"\npath = \"tests/beta.rs\"\n",
                "[[bench]]\nname = \"alpha\"\npath = \"benches/alpha.rs\"\nharness = false\n",
                "[[bench]]\nname = \"beta\"\npath = \"benches/beta.rs\"\nharness = false\n",
                "[profile.contract-fixture]\ninherits = \"dev\"\ndebug = 0\n",
            )).unwrap();
            std::fs::write(source.join("src/lib.rs"), "pub fn answer() -> u8 { 42 }\n").unwrap();
            for name in ["alpha", "beta"] {
                std::fs::write(
                    source.join("tests").join(format!("{name}.rs")),
                    format!("#[test] fn {name}_must_not_run() {{ panic!(\"no-run executed a test\"); }}\n"),
                ).unwrap();
                std::fs::write(
                    source.join("benches").join(format!("{name}.rs")),
                    "fn main() { panic!(\"no-run executed a benchmark\"); }\n",
                )
                .unwrap();
            }
            let remote_target = if custom_target {
                fixture.path().join("worker target")
            } else {
                source.join("target")
            };
            let remote_root = if custom_target {
                &remote_target
            } else {
                &source
            };
            let command = format!(
                "cargo {subcommand} --no-run --{selector} alpha --{selector} beta --profile contract-fixture --offline --jobs=1"
            );
            let capture = CargoOutputCapture::for_command(Some(kind), &command).unwrap();
            let instrumented = capture.execution_command(&command);
            let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
            let mut compile = Command::new(cargo);
            compile
                .current_dir(&source)
                .args(instrumented.split_ascii_whitespace().skip(1))
                // The live command pins build.build-dir into the worker output
                // tree. Exercise the real new layout, including executable
                // files beneath normally excluded build-cache directories.
                .arg("--config")
                .arg(format!(
                    "build.build-dir={}",
                    serde_json::to_string(remote_target.to_str().unwrap()).unwrap()
                ))
                .env("CARGO_HOME", fixture.path().join("cargo-home"))
                .env("CARGO_TARGET_DIR", &remote_target)
                .env_remove("RUSTC_WRAPPER")
                .env_remove("RUSTC_WORKSPACE_WRAPPER")
                .env_remove("RUSTFLAGS")
                .env_remove("CARGO_ENCODED_RUSTFLAGS")
                .env_remove("CARGO_BUILD_TARGET")
                .env_remove("CARGO_BUILD_TARGET_DIR")
                .env_remove("CARGO_BUILD_BUILD_DIR")
                .env_remove("CARGO_MAKEFLAGS")
                .env_remove("MAKEFLAGS")
                .stdin(Stdio::null())
                .kill_on_drop(true);
            let mut completed_stdout = Vec::new();
            for warm in [false, true] {
                let output = tokio::time::timeout(Duration::from_secs(90), compile.output())
                    .await
                    .expect("owned no-run Cargo fixture timed out")
                    .expect("Cargo is required for the named no-run delivery regression");
                assert!(
                    output.status.success(),
                    "{}",
                    String::from_utf8_lossy(&output.stderr)
                );
                let records: Vec<serde_json::Value> = std::str::from_utf8(&output.stdout)
                    .unwrap()
                    .lines()
                    .filter_map(|line| serde_json::from_str(line).ok())
                    .filter(|record: &serde_json::Value| {
                        record["reason"] == "compiler-artifact"
                            && record["target"]["kind"] == serde_json::json!([target_kind])
                    })
                    .collect();
                assert_eq!(records.len(), 2, "both requested targets must be emitted");
                assert!(records.iter().all(|record| record["fresh"] == warm));
                let contract = capture.parse_receipt(&output.stdout, remote_root).unwrap();
                assert_eq!(contract.required_files.len(), 2);
                assert!(
                    contract
                        .required_files
                        .iter()
                        .all(|path| { path.components().any(|part| part.as_os_str() == "build") }),
                    "fixture must exercise the managed build directory: {contract:?}"
                );
                completed_stdout = output.stdout;
            }

            let (_local_owner, _stage_owner, mut session) =
                captured_publication_fixture(kind, &command, remote_root, custom_target);
            session
                .install_cargo_artifact_evidence(&completed_stdout, remote_root)
                .unwrap();
            let mut session = reload_publication(&session);
            let phase = &session.recipe.phases[0];
            let required = phase.cargo_outputs.as_ref().unwrap().required_files.clone();
            let patterns = phase.patterns.clone();
            let name = phase.name.clone();
            let local = phase.local.clone();
            let stage = session.stage(0);
            let missing = required.iter().next_back().unwrap();
            for relative in &required {
                std::fs::create_dir_all(local.join(relative).parent().unwrap()).unwrap();
                std::fs::write(local.join(relative), b"previous local executable").unwrap();
            }
            let source_sentinel = local.join("source.rs");
            std::fs::write(&source_sentinel, b"local source remains owned locally").unwrap();
            for complete_transfer in [false, true] {
                let mut transfer = Command::new("rsync");
                transfer.args(["-a", "--checksum", "--safe-links", "--prune-empty-dirs"]);
                if !complete_transfer {
                    // Simulate a successful but incomplete artifact transfer.
                    // The other executable makes the old nonempty gate pass.
                    transfer.arg(format!("--exclude=/{}", missing.display()));
                }
                for rule in crate::transfer::priority_rsync_includes(&patterns) {
                    transfer.arg(format!("--include={rule}"));
                }
                for pattern in &patterns {
                    if let Some(excluded) = pattern.strip_prefix("- ") {
                        transfer.arg(format!("--exclude={excluded}"));
                    }
                }
                transfer.arg("--include=*/");
                for pattern in &patterns {
                    if !pattern.starts_with("- ") {
                        transfer.arg(format!(
                            "--include=/{}",
                            pattern.strip_prefix("+ ").unwrap_or(pattern)
                        ));
                    }
                }
                transfer
                    .arg("--exclude=*")
                    .arg(format!("{}/", remote_root.display()))
                    .arg(format!("{}/", stage.display()))
                    .stdin(Stdio::null())
                    .kill_on_drop(true);
                let copied = tokio::time::timeout(Duration::from_secs(15), transfer.output())
                    .await
                    .expect("owned no-run rsync fixture timed out")
                    .expect("rsync is required for the named no-run delivery regression");
                assert!(copied.status.success(), "{copied:?}");
                if !complete_transfer {
                    assert!(
                        required
                            .iter()
                            .any(|relative| stage.join(relative).is_file())
                    );
                    assert!(!stage.join(missing).exists());
                    let journal = std::fs::read(&session.writer.path).unwrap();
                    assert!(session.publish(&name).await.is_err());
                    assert_eq!(std::fs::read(&session.writer.path).unwrap(), journal);
                    assert!(session.returned(0).is_err());
                    for relative in &required {
                        assert_eq!(
                            std::fs::read(local.join(relative)).unwrap(),
                            b"previous local executable"
                        );
                    }
                    session = reload_publication(&session);
                } else {
                    session.publish(&name).await.unwrap();
                    session.returned(0).unwrap();
                    for relative in &required {
                        assert_eq!(
                            std::fs::read(local.join(relative)).unwrap(),
                            std::fs::read(remote_root.join(relative)).unwrap()
                        );
                    }
                    let restored = reload_publication(&session);
                    assert_eq!(restored.recipe.returned, Some(0));
                    assert!(restored.recipe.phases[0].complete);
                }
                assert_eq!(
                    std::fs::read(&source_sentinel).unwrap(),
                    b"local source remains owned locally"
                );
            }
        }
    }

    #[tokio::test]
    async fn cargo_missing_named_bin_preserves_all_outputs_until_disk_reloaded_retry() {
        for custom_target in [false, true] {
            let remote = tempfile::tempdir().unwrap();
            let outputs = remote
                .path()
                .join(if custom_target { "lean" } else { "target/lean" });
            std::fs::create_dir_all(&outputs).unwrap();
            std::fs::write(outputs.join("app"), b"new app executable").unwrap();
            std::fs::write(outputs.join("helper"), b"new helper executable").unwrap();
            assert_cargo_fixture_publication(
                CompilationKind::CargoBuild,
                "cargo build --bin app --bin helper --profile lean",
                &cargo_bin_receipt(remote.path(), custom_target),
                remote.path(),
                custom_target,
            )
            .await;
        }
    }

    #[tokio::test]
    async fn cargo_awaiting_receipt_cannot_publish_or_report_success_and_retains_failure_stage() {
        let remote = tempfile::tempdir().unwrap();
        let (_directory, _stage_owner, session) = captured_publication_fixture(
            CompilationKind::CargoBuild,
            "cargo build --bin app --bin helper --profile lean",
            remote.path(),
            false,
        );
        let mut session = reload_publication(&session);
        let stage = session.stage(0);
        std::fs::create_dir_all(stage.join("target/lean")).unwrap();
        std::fs::write(stage.join("target/lean/app"), b"new app").unwrap();
        let before = std::fs::read(&session.writer.path).unwrap();
        assert!(session.publish("project").await.is_err());
        assert!(session.returned(0).is_err());
        assert!(
            !session.recipe.phases[0]
                .local
                .join("target/lean/app")
                .exists()
        );
        assert_eq!(std::fs::read(&session.writer.path).unwrap(), before);
        session.returned(EXIT_ARTIFACT_TRANSFER_FAILED).unwrap();
        session.sources_released().unwrap();
        session.retired().unwrap();
        assert!(session.retain_failed_delivery_evidence());
        assert_eq!(
            std::fs::read(stage.join("target/lean/app")).unwrap(),
            b"new app"
        );
        let retired = reload_publication(&session);
        assert!(retired.recipe.retired);
        assert!(!retired.recipe.phases[0].complete);
        assert_eq!(retired.recipe.returned, Some(EXIT_ARTIFACT_TRANSFER_FAILED));
    }

    #[test]
    fn cargo_invalid_evidence_cannot_expand_policy_or_replace_sources() {
        let remote = tempfile::tempdir().unwrap();
        let (_directory, _stage_owner, mut session) = captured_publication_fixture(
            CompilationKind::CargoBuild,
            "cargo build --bin app --bin helper --profile lean",
            remote.path(),
            false,
        );
        let before = std::fs::read(&session.writer.path).unwrap();
        let patterns = session.recipe.phases[0].patterns.clone();
        let receipt = cargo_bin_receipt(remote.path(), false);
        let finished_start = receipt[..receipt.len() - 1]
            .iter()
            .rposition(|byte| *byte == b'\n')
            .unwrap()
            + 1;
        let missing_finished = &receipt[..finished_start];
        let error = session
            .install_cargo_artifact_evidence(missing_finished, remote.path())
            .unwrap_err();
        assert!(error.is::<CargoOutputContractRejected>());
        let forged = String::from_utf8(cargo_bin_receipt(remote.path(), false))
            .unwrap()
            .replace("target/lean/app", "Cargo.toml");
        assert!(
            session
                .install_cargo_artifact_evidence(forged.as_bytes(), remote.path())
                .is_err()
        );
        assert_eq!(session.recipe.phases[0].patterns, patterns);
        assert!(session.recipe.phases[0].cargo_outputs.is_none());
        assert_eq!(std::fs::read(&session.writer.path).unwrap(), before);
    }

    #[test]
    fn cargo_evidence_journal_failure_remains_retryable_and_cannot_authorize_publication() {
        let remote = tempfile::tempdir().unwrap();
        let (directory, _stage_owner, mut session) = captured_publication_fixture(
            CompilationKind::CargoBuild,
            "cargo build --bin app --bin helper --profile lean",
            remote.path(),
            false,
        );
        let journal = session.writer.path.clone();
        let before = std::fs::read(&journal).unwrap();
        // Atomic publication cannot replace a directory with the lease file.
        // This is a real filesystem failure, not an invalid Cargo receipt.
        session.writer.path = directory.path().to_owned();
        let error = session
            .install_cargo_artifact_evidence(
                &cargo_bin_receipt(remote.path(), false),
                remote.path(),
            )
            .unwrap_err();
        assert!(!error.is::<CargoOutputContractRejected>());
        assert!(session.needs_cargo_artifact_evidence());
        assert_eq!(std::fs::read(journal).unwrap(), before);
    }

    #[tokio::test]
    async fn recovery_publication_refuses_retained_or_pending_source_before_any_write() {
        for pending in [false, true] {
            let (_directory, _stage_owner, mut session) = publication_fixture();
            let local = session.recipe.project_root.clone();
            let stage = session.stage(0);
            let original = b"int main(void) { return 0; }\n";
            let remote = b"changed source from an earlier collector\n";
            std::fs::write(local.join("main.c"), original).unwrap();
            session.recipe.phases[0].baseline.insert(
                PathBuf::from("main.c"),
                fingerprint(&local.join("main.c")).unwrap().unwrap(),
            );
            std::fs::create_dir(stage.join("build")).unwrap();
            std::fs::write(stage.join("build/app"), b"valid artifact").unwrap();
            let retained = if pending {
                let temporary = local.join(format!(".rch-return-{}", session.recipe.identity));
                std::fs::write(&temporary, remote).unwrap();
                session.recipe.phases[0].pending = Some((
                    PathBuf::from("main.c"),
                    fingerprint(&temporary).unwrap().unwrap(),
                ));
                temporary
            } else {
                let retained = stage.join("main.c");
                std::fs::write(&retained, remote).unwrap();
                retained
            };
            session.persist().unwrap();
            let journal = std::fs::read(&session.writer.path).unwrap();

            let published = session.publish("project").await;
            // Source is never overwritten.
            assert_eq!(std::fs::read(local.join("main.c")).unwrap(), original);
            if pending {
                // The refused publication keeps its journaled copy for a retry.
                assert_eq!(std::fs::read(&retained).unwrap(), remote);
                // An interrupted write of an out-of-policy path cannot be
                // finished or skipped: refuse before any write.
                assert!(published.is_err());
                assert!(!local.join("build/app").exists());
                assert_eq!(std::fs::read(&session.writer.path).unwrap(), journal);
                assert!(session.recipe.phases[0].published.is_empty());
            } else {
                // A retained out-of-policy file is skipped, not fatal: the
                // job's real outputs still publish, so recovery can retire
                // and release the worker-side source claim.
                published.unwrap();
                assert_eq!(
                    std::fs::read(local.join("build/app")).unwrap(),
                    b"valid artifact"
                );
                assert!(
                    !session.recipe.phases[0]
                        .published
                        .contains_key(&PathBuf::from("main.c"))
                );
                // Completion drops the stage, retained copy included; it was
                // never published and nothing reads it again.
                assert!(!stage.exists());
            }
        }
    }

    #[tokio::test]
    async fn publication_overwrites_outputs_another_job_replaced_after_preparation() {
        // Dispatchers share one CARGO_TARGET_DIR: between this job's
        // preparation snapshot and its publication, another job may replace
        // the same output. That is not an ownership conflict; publishing must
        // re-baseline under the publication lock and write, not refuse.
        let (_directory, _stage_owner, mut session) = publication_fixture();
        let local = session.recipe.project_root.clone();
        let stage = session.stage(0);
        std::fs::create_dir(local.join("build")).unwrap();
        std::fs::write(local.join("build/app"), b"snapshot at preparation").unwrap();
        session.recipe.phases[0].baseline.insert(
            PathBuf::from("build/app"),
            fingerprint(&local.join("build/app")).unwrap().unwrap(),
        );
        std::fs::write(local.join("build/app"), b"written by a concurrent job").unwrap();
        std::fs::create_dir(stage.join("build")).unwrap();
        std::fs::write(stage.join("build/app"), b"this job's output").unwrap();
        session.persist().unwrap();

        session.publish("project").await.unwrap();
        assert_eq!(
            std::fs::read(local.join("build/app")).unwrap(),
            b"this job's output"
        );
        assert!(session.recipe.phases[0].complete);
        assert!(
            !stage.exists(),
            "a completed publication must not keep its duplicate stage"
        );
    }

    /// The output root may sit behind system symlinks (macOS `/tmp`, `/var`,
    /// a symlinked `/data`): publication must accept that root, while a
    /// symlink BELOW it, which could redirect a write, is still refused.
    #[cfg(unix)]
    #[tokio::test]
    async fn publication_accepts_symlinked_root_but_refuses_symlinks_inside_it() {
        let (directory, _stage_owner, mut session) = publication_fixture();
        let real = directory.path().join("real-target");
        std::fs::create_dir(&real).unwrap();
        let link = directory.path().join("linked-target");
        std::os::unix::fs::symlink(&real, &link).unwrap();
        session.recipe.phases[0].local = link;
        let stage = session.stage(0);
        std::fs::create_dir(stage.join("build")).unwrap();
        std::fs::write(stage.join("build/app"), b"output").unwrap();
        session.persist().unwrap();
        session.publish("project").await.unwrap();
        assert_eq!(std::fs::read(real.join("build/app")).unwrap(), b"output");

        let (directory, _stage_owner, mut session) = publication_fixture();
        let local = session.recipe.project_root.clone();
        let elsewhere = directory.path().join("elsewhere");
        std::fs::create_dir(&elsewhere).unwrap();
        std::os::unix::fs::symlink(&elsewhere, local.join("build")).unwrap();
        let stage = session.stage(0);
        std::fs::create_dir(stage.join("build")).unwrap();
        std::fs::write(stage.join("build/app"), b"output").unwrap();
        session.persist().unwrap();
        assert!(session.publish("project").await.is_err());
        assert!(!elsewhere.join("app").exists());
    }

    #[tokio::test]
    async fn publication_lock_is_released_between_publications() {
        let (_directory, _stage_owner, session) = publication_fixture();
        let root = session.recipe.project_root.clone();
        let first = lock_output_root(&root).await.unwrap();
        drop(first);
        // A second wrapper can publish into the same root once the first is done.
        let _second = lock_output_root(&root).await.unwrap();
    }

    /// Keep a real kernel lock held independently of the Tokio reactor. The
    /// watchdog makes a regression to a blocking wait fail in seconds instead
    /// of hanging the test for the production ten-minute publication budget.
    fn hold_publication_lock(
        lock: File,
    ) -> (std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>) {
        let (release, released) = std::sync::mpsc::channel();
        let holder = std::thread::spawn(move || {
            let _ = released.recv_timeout(Duration::from_secs(2));
            drop(lock);
        });
        (release, holder)
    }

    #[cfg(unix)]
    #[test]
    fn publication_identity_resolves_symlinks_before_parent_components_without_creating_outputs() {
        let directory = tempfile::tempdir().unwrap();
        let real = directory.path().join("real");
        std::fs::create_dir_all(real.join("nested")).unwrap();
        let alias = directory.path().join("alias");
        std::os::unix::fs::symlink(real.join("nested"), &alias).unwrap();
        let expected = std::fs::canonicalize(&real).unwrap().join("target");
        for path in [
            real.join("target"),
            alias.join("../target"),
            alias.join("missing/../../target"),
        ] {
            assert_eq!(output_lock_root_identity(&path).unwrap(), expected);
        }
        assert!(!real.join("target").exists());
        assert!(!real.join("nested/missing").exists());

        let dangling = directory.path().join("dangling");
        std::os::unix::fs::symlink(directory.path().join("absent"), &dangling).unwrap();
        assert!(output_lock_root_identity(&dangling.join("target")).is_err());
        std::fs::write(real.join("file"), b"not a directory").unwrap();
        assert!(output_lock_root_identity(&real.join("file/../target")).is_err());
    }

    #[cfg(unix)]
    #[tokio::test(flavor = "current_thread")]
    async fn symlinked_and_canonical_output_roots_share_publication_ownership() {
        let (directory, _stage_owner, session) = publication_fixture();
        let root = session.recipe.project_root.clone();
        let alias = directory.path().join("output-alias");
        std::os::unix::fs::symlink(&root, &alias).unwrap();
        let lock = lock_output_root(&root).await.unwrap();
        let (release, holder) = hold_publication_lock(lock);
        let alias_lock =
            tokio::time::timeout(Duration::from_millis(25), lock_output_root(&alias)).await;
        let _ = release.send(());
        holder.join().unwrap();
        assert!(
            alias_lock.is_err(),
            "path alias bypassed the existing publication owner"
        );
        let _after_release = lock_output_root(&alias).await.unwrap();
    }

    #[cfg(unix)]
    #[tokio::test(flavor = "current_thread")]
    async fn missing_output_root_keeps_its_lock_when_another_job_creates_the_directory() {
        let directory = tempfile::tempdir().unwrap();
        let real = directory.path().join("real");
        std::fs::create_dir(&real).unwrap();
        let alias = directory.path().join("alias");
        std::os::unix::fs::symlink(&real, &alias).unwrap();
        let root = real.join("new-target/profile");
        let aliased_root = alias.join("new-target/profile");
        let lock = lock_output_root(&aliased_root).await.unwrap();
        assert!(
            !root.exists(),
            "lock discovery created the caller's output tree"
        );
        let (release, holder) = hold_publication_lock(lock);
        std::fs::create_dir_all(&root).unwrap();
        let created_lock =
            tokio::time::timeout(Duration::from_millis(25), lock_output_root(&root)).await;
        let _ = release.send(());
        holder.join().unwrap();
        assert!(
            created_lock.is_err(),
            "creating the output tree changed its ownership identity"
        );
        let _after_release = lock_output_root(&root).await.unwrap();
    }

    #[cfg(unix)]
    #[tokio::test(flavor = "current_thread")]
    async fn retargeted_output_alias_refuses_publication_and_retains_the_journal() {
        let (directory, _stage_owner, mut session) = publication_fixture();
        let original = session.recipe.project_root.clone();
        let replacement = directory.path().join("other-output");
        std::fs::create_dir(&replacement).unwrap();
        let alias = directory.path().join("output-alias");
        std::os::unix::fs::symlink(&original, &alias).unwrap();
        session.recipe.phases[0].local = alias.clone();
        let stage = session.stage(0);
        std::fs::create_dir(stage.join("build")).unwrap();
        std::fs::write(stage.join("build/app"), b"completed remote output").unwrap();
        session.persist().unwrap();
        let before = std::fs::read(&session.writer.path).unwrap();
        let lock = lock_output_root(&original).await.unwrap();
        let (release, holder) = hold_publication_lock(lock);
        let mut publication = Box::pin(session.publish("project"));
        // Borrow the future so the first deadline leaves its original lock
        // identity pending, then retarget the alias before ownership arrives.
        assert!(
            tokio::time::timeout(Duration::from_millis(25), &mut publication)
                .await
                .is_err()
        );
        std::fs::rename(&alias, directory.path().join("previous-output-alias")).unwrap();
        std::os::unix::fs::symlink(&replacement, &alias).unwrap();
        let _ = release.send(());
        holder.join().unwrap();
        let error = tokio::time::timeout(Duration::from_secs(1), publication)
            .await
            .expect("publication did not resume after its original owner released the lock")
            .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("output root changed while waiting")
        );
        assert_eq!(std::fs::read(&session.writer.path).unwrap(), before);
        assert!(!original.join("build/app").exists());
        assert!(!replacement.join("build/app").exists());
        assert_eq!(
            std::fs::read(stage.join("build/app")).unwrap(),
            b"completed remote output"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn publication_deadline_keeps_staged_outputs_and_journal_for_same_job_retry() {
        let (_directory, _stage_owner, mut session) = publication_fixture();
        let local = session.recipe.project_root.clone();
        let stage = session.stage(0);
        std::fs::create_dir(local.join("build")).unwrap();
        std::fs::write(local.join("build/app"), b"previous output").unwrap();
        std::fs::create_dir(stage.join("build")).unwrap();
        std::fs::write(stage.join("build/app"), b"completed remote output").unwrap();
        session.persist().unwrap();
        let before = std::fs::read(&session.writer.path).unwrap();
        let lock = lock_output_root(&local).await.unwrap();
        let (release, holder) = hold_publication_lock(lock);

        // This is the same cancellation boundary as jobs recover's deadline:
        // dropping the wait must not begin publication or retire ownership.
        let publication =
            tokio::time::timeout(Duration::from_millis(25), session.publish("project")).await;
        let _ = release.send(());
        holder.join().unwrap();
        assert!(
            publication.is_err(),
            "contended publication blocked its caller's deadline"
        );
        assert_eq!(std::fs::read(&session.writer.path).unwrap(), before);
        assert_eq!(
            std::fs::read(local.join("build/app")).unwrap(),
            b"previous output"
        );
        assert_eq!(
            std::fs::read(stage.join("build/app")).unwrap(),
            b"completed remote output"
        );
        assert!(!session.recipe.phases[0].complete);
        assert!(session.recipe.phases[0].pending.is_none());
        assert!(session.recipe.phases[0].published.is_empty());
        assert!(!session.recipe.sources_released);

        // Resume from the retained disk journal, not the cancelled future's
        // in-memory session. Only the already-staged bytes are published.
        let writer = DurableLeaseWriter {
            path: session.writer.path.clone(),
            recovery_ownership: None,
            lease: Arc::new(Mutex::new(serde_json::from_slice(&before).unwrap())),
        };
        let mut resumed = RecoverySession {
            recipe: load_recipe(&writer).unwrap(),
            writer,
        };
        resumed.publish("project").await.unwrap();
        assert_eq!(
            std::fs::read(local.join("build/app")).unwrap(),
            b"completed remote output"
        );
        assert!(resumed.recipe.phases[0].complete);
        assert!(!stage.exists());
        assert_eq!(resumed.recipe.wrapper_id, session.recipe.wrapper_id);
        assert_eq!(resumed.recipe.build_id, session.recipe.build_id);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn same_root_publishers_wait_without_stalling_runtime_progress() {
        let (_first_dir, _first_stage_owner, mut first) = publication_fixture();
        let (_second_dir, _second_stage_owner, mut second) = publication_fixture();
        let local = first.recipe.project_root.clone();
        second.recipe.phases[0].local = local.clone();
        std::fs::create_dir(local.join("build")).unwrap();
        std::fs::write(local.join("build/app"), b"before both jobs").unwrap();
        for (session, name, bytes) in [
            (&mut first, "first.o", b"first job".as_slice()),
            (&mut second, "second.o", b"second job".as_slice()),
        ] {
            let stage = session.stage(0);
            std::fs::create_dir(stage.join("build")).unwrap();
            std::fs::write(stage.join("build/app"), bytes).unwrap();
            std::fs::write(stage.join("build").join(name), bytes).unwrap();
            session.persist().unwrap();
        }
        let lock = lock_output_root(&local).await.unwrap();
        let (release, holder) = hold_publication_lock(lock);
        let publications = tokio::time::timeout(Duration::from_secs(1), async {
            tokio::join!(first.publish("project"), second.publish("project"), async {
                // A heartbeat/recovery timer must still run while both jobs
                // wait. Neither may touch the shared output before ownership.
                tokio::time::sleep(Duration::from_millis(25)).await;
                assert_eq!(
                    std::fs::read(local.join("build/app")).unwrap(),
                    b"before both jobs"
                );
                release.send(()).unwrap();
            })
        })
        .await;
        drop(release);
        holder.join().unwrap();
        let (first_result, second_result, ()) = publications
            .expect("contended publication prevented the timer from releasing its owner");
        first_result.unwrap();
        second_result.unwrap();
        assert!(first.recipe.phases[0].complete);
        assert!(second.recipe.phases[0].complete);
        assert_eq!(
            std::fs::read(local.join("build/first.o")).unwrap(),
            b"first job"
        );
        assert_eq!(
            std::fs::read(local.join("build/second.o")).unwrap(),
            b"second job"
        );
        let final_output = std::fs::read(local.join("build/app")).unwrap();
        assert!(matches!(
            final_output.as_slice(),
            b"first job" | b"second job"
        ));
        assert!(!first.stage(0).exists());
        assert!(!second.stage(0).exists());
    }

    #[tokio::test]
    async fn recovery_publication_finishes_journaled_new_outputs_without_rewriting_them() {
        let (_directory, _stage_owner, mut session) = publication_fixture();
        let local = session.recipe.project_root.clone();
        let stage = session.stage(0);
        for (path, bytes) in [("a.out", b"first output"), ("b.out", b"other output")] {
            std::fs::write(local.join(path), bytes).unwrap();
            std::fs::write(stage.join(path), b"stale retained staging bytes").unwrap();
        }
        let first = fingerprint(&local.join("a.out")).unwrap().unwrap();
        session.recipe.phases[0].pending = Some((PathBuf::from("a.out"), first.clone()));
        session.recipe.phases[0].published.insert(
            PathBuf::from("b.out"),
            fingerprint(&local.join("b.out")).unwrap().unwrap(),
        );
        std::fs::create_dir(stage.join("build")).unwrap();
        std::fs::write(stage.join("build/next.o"), b"next output").unwrap();
        session.persist().unwrap();

        session.publish("project").await.unwrap();
        assert_eq!(std::fs::read(local.join("a.out")).unwrap(), b"first output");
        assert_eq!(std::fs::read(local.join("b.out")).unwrap(), b"other output");
        assert_eq!(
            std::fs::read(local.join("build/next.o")).unwrap(),
            b"next output"
        );
        assert_eq!(session.recipe.phases[0].published.len(), 3);
        assert_eq!(
            session.recipe.phases[0].published[Path::new("a.out")],
            first
        );
        assert!(session.recipe.phases[0].pending.is_none());
        assert!(session.recipe.phases[0].complete);
    }

    #[tokio::test]
    async fn recovery_publication_retains_declared_result_directory_contract() {
        let (_directory, _stage_owner, mut session) = publication_fixture();
        let local = session.recipe.project_root.clone();
        let stage = session.stage(0);
        std::fs::create_dir(local.join("reports")).unwrap();
        std::fs::create_dir(stage.join("reports")).unwrap();
        std::fs::write(local.join("reports/result.json"), b"old report").unwrap();
        std::fs::write(stage.join("reports/result.json"), b"new report").unwrap();
        session.recipe.phases[0].patterns.clear();
        session.recipe.phases[0].result_dir = Some(PathBuf::from("reports"));
        session.recipe.phases[0].baseline.insert(
            PathBuf::from("reports/result.json"),
            fingerprint(&local.join("reports/result.json"))
                .unwrap()
                .unwrap(),
        );
        session.persist().unwrap();

        session.publish("project").await.unwrap();
        assert_eq!(
            std::fs::read(local.join("reports/result.json")).unwrap(),
            b"new report"
        );
        assert!(session.recipe.phases[0].complete);
    }

    #[test]
    fn retained_source_intent_binds_siblings_pair_and_authority_before_journaling() {
        let _guard = test_guard!();
        for case in [
            "different-sibling",
            "missing-original-authority",
            "missing-retained-authority",
            "missing-pair",
            "different-pair-root",
            "different-pair-token",
            "missing-original-root",
            "noncanonical-identity",
        ] {
            let (directory, writer, worker) = admitted_preparation_fixture();
            let directory = directory.keep();
            let root = "/data/tmp/rch/source-content-pair-recovery".to_owned();
            let mut identity = "0123456789abcdef0123456789abcdef".to_owned();
            let mut retained = format!("{root}.retained-{identity}");
            let mut roots = vec![root.clone(), retained.clone()];
            let mut pair = Some((root.clone(), identity.clone()));
            let mut retire_root = Some(root.clone());
            match case {
                "different-sibling" => {
                    retained = format!("{root}.retained-1123456789abcdef0123456789abcdef");
                    roots[1] = retained.clone();
                }
                "missing-original-authority" => roots = vec![retained.clone()],
                "missing-retained-authority" => roots = vec![root.clone()],
                "missing-pair" => pair = None,
                "different-pair-root" => {
                    pair = Some((format!("{root}-other"), identity.clone()));
                }
                "different-pair-token" => {
                    pair = Some((root.clone(), "1123456789abcdef0123456789abcdef".into()));
                }
                "missing-original-root" => retire_root = None,
                "noncanonical-identity" => {
                    identity = "ABCDEF0123456789ABCDEF0123456789".into();
                    retained = format!("{root}.retained-{identity}");
                    roots[1] = retained.clone();
                    pair = Some((root.clone(), identity.clone()));
                }
                _ => unreachable!(),
            }
            let before = std::fs::read(&writer.path).unwrap();
            let result = RecoverySession::begin(
                &writer,
                &worker,
                roots,
                pair,
                retire_root,
                Some(retained),
                TransferConfig::default(),
                directory,
                identity,
            );
            assert!(result.is_err(), "admitted invalid retained intent: {case}");
            assert_eq!(std::fs::read(&writer.path).unwrap(), before, "{case}");
            assert!(writer.snapshot().recovery.is_none(), "{case}");
            assert_eq!(writer.snapshot().identity.remote_build_id, Some(41));
        }
    }

    #[test]
    fn retained_source_prepare_cannot_drop_or_change_the_admitted_destination() {
        let _guard = test_guard!();
        let (directory, writer, worker) = retained_preparation_fixture();
        let intent = load_recipe(&writer).unwrap();
        let pipeline = TransferPipeline::new(
            directory.clone(),
            "retained-recovery".into(),
            intent.identity.clone(),
            intent.transfer.clone(),
        )
        .with_remote_path_override(format!(
            "{}/projects/app",
            intent.retire_root.as_deref().unwrap()
        ));
        let before = std::fs::read(&writer.path).unwrap();
        for changed in [
            None,
            Some(format!(
                "{}-other",
                intent.retained_root.as_deref().unwrap()
            )),
        ] {
            let result = RecoverySession::prepare(
                &writer,
                &worker,
                &pipeline,
                intent.source_roots.clone(),
                intent.pair.clone(),
                intent.retire_root.clone(),
                changed,
                intent.transfer.clone(),
                directory.clone(),
                None,
                Some(CompilationKind::CargoBuild),
                "cargo build",
                &[],
                intent.identity.clone(),
            );
            assert!(
                result.is_err(),
                "preparation replaced retained-source authority"
            );
            assert_eq!(std::fs::read(&writer.path).unwrap(), before);
        }
        let session = RecoverySession::prepare(
            &writer,
            &worker,
            &pipeline,
            intent.source_roots.clone(),
            intent.pair.clone(),
            intent.retire_root.clone(),
            intent.retained_root.clone(),
            intent.transfer.clone(),
            directory,
            None,
            Some(CompilationKind::CargoBuild),
            "cargo build",
            &[],
            intent.identity.clone(),
        )
        .unwrap();
        let persisted = reload_publication(&session).recipe;
        assert!(persisted.prepared);
        assert_eq!(persisted.retained_root, intent.retained_root);
        assert_eq!(persisted.retire_root, intent.retire_root);
        assert_eq!(persisted.pair, intent.pair);
        assert_eq!(persisted.source_roots, intent.source_roots);
        assert_eq!(persisted.retained_materialized, None);
        assert!(!persisted.tree_retired);
        assert!(!persisted.execution_started);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn pending_publication_refuses_redirected_descendants_before_any_shortcut() {
        use std::os::unix::fs::symlink;
        let _guard = test_guard!();
        for already_published in [false, true] {
            let (directory, _stages, mut session) = publication_fixture();
            let local = session.recipe.phases[0].local.clone();
            let outside = directory.path().join("outside-phase");
            std::fs::create_dir(&outside).unwrap();
            symlink(&outside, local.join("build")).unwrap();
            let relative = PathBuf::from("build/app");
            let destination = outside.join("app");
            let temporary = outside.join(format!(".rch-return-{}", session.recipe.identity));
            let original = if already_published {
                &b"pending bytes"[..]
            } else {
                &b"prior outside bytes"[..]
            };
            std::fs::write(&destination, original).unwrap();
            std::fs::write(&temporary, b"pending bytes").unwrap();
            let hash = fingerprint(&temporary).unwrap().unwrap();
            let phase = &mut session.recipe.phases[0];
            if !already_published {
                phase.baseline.insert(
                    relative.clone(),
                    fingerprint(&destination).unwrap().unwrap(),
                );
            }
            phase.pending = Some((relative.clone(), hash));
            session.persist().unwrap();
            let recipe_before = serde_json::to_value(&session.recipe).unwrap();
            let lease_before = session.writer.snapshot();
            let journal_before = std::fs::read(&session.writer.path).unwrap();
            assert!(session.publish("project").await.is_err());
            assert_eq!(
                serde_json::to_value(&session.recipe).unwrap(),
                recipe_before
            );
            assert_eq!(session.writer.snapshot(), lease_before);
            assert_eq!(std::fs::read(&session.writer.path).unwrap(), journal_before);
            assert_eq!(std::fs::read(&destination).unwrap(), original);
            assert_eq!(std::fs::read(&temporary).unwrap(), b"pending bytes");
            assert!(!session.recipe.phases[0].complete);
            assert!(session.recipe.phases[0].pending.is_some());
            assert!(session.recipe.phases[0].published.is_empty());
            let alias = directory.path().join("caller-root-alias");
            symlink(&local, &alias).unwrap();
            std::fs::create_dir(local.join("real-directory")).unwrap();
            validate_output_ancestors(&alias, Path::new("real-directory/app")).unwrap();
            for escaped in [Path::new("../outside-phase/app"), destination.as_path()] {
                assert!(validate_output_ancestors(&local, escaped).is_err());
            }
        }
    }

    #[cfg(unix)]
    #[test]
    fn prepared_output_refuses_same_inode_rewrites_and_growth_without_moving_its_cursor() {
        use std::io::Seek;
        use std::os::unix::fs::{FileExt, MetadataExt};
        let _guard = test_guard!();
        for reuse in [false, true] {
            for mutation in ["byte", "growth"] {
                let directory = tempfile::Builder::new()
                    .disable_cleanup(true)
                    .tempdir()
                    .unwrap();
                let source = directory.path().join("source");
                let temporary = directory.path().join("prepared-copy");
                let destination = directory.path().join("destination");
                let original = b"staged executable bytes";
                std::fs::write(&source, original).unwrap();
                std::fs::write(&destination, b"prior destination bytes").unwrap();
                let hash = fingerprint(&source).unwrap().unwrap();
                let first = prepare_output_temporary(&source, &temporary, &hash).unwrap();
                let prepared = if reuse {
                    prepare_output_temporary(&source, &temporary, &hash).unwrap()
                } else {
                    first
                };
                let metadata = prepared.file.metadata().unwrap();
                let mut cursor = prepared.file.try_clone().unwrap();
                let original_position = cursor.stream_position().unwrap();
                prepared.verify_content().unwrap();
                assert_eq!(cursor.stream_position().unwrap(), original_position);
                let rewrite = OpenOptions::new().write(true).open(&temporary).unwrap();
                let offset = if mutation == "byte" {
                    0
                } else {
                    metadata.len()
                };
                assert_eq!(rewrite.write_at(b"X", offset).unwrap(), 1);
                rewrite.sync_all().unwrap();
                assert_eq!(rewrite.metadata().unwrap().ino(), metadata.ino());
                assert_eq!(rewrite.metadata().unwrap().nlink(), metadata.nlink());
                assert_eq!(rewrite.metadata().unwrap().mode(), metadata.mode());
                assert!(prepared.publish(&destination).is_err());
                assert_eq!(
                    std::fs::read(&destination).unwrap(),
                    b"prior destination bytes"
                );
                assert_eq!(std::fs::read(&source).unwrap(), original);
                let changed = std::fs::read(&temporary).unwrap();
                if mutation == "byte" {
                    assert_eq!(changed.len(), original.len());
                    assert_eq!(changed[0], b'X');
                    assert_eq!(&changed[1..], &original[1..]);
                } else {
                    assert_eq!(&changed[..original.len()], original);
                    assert_eq!(changed[original.len()], b'X');
                }
            }
        }
    }

    #[cfg(unix)]
    #[test]
    fn output_temporary_reuses_complete_copies_and_preserves_conflicting_evidence() {
        use std::os::unix::fs::{MetadataExt, PermissionsExt, symlink};
        let _guard = test_guard!();
        let directory = tempfile::Builder::new()
            .disable_cleanup(true)
            .tempdir()
            .unwrap();
        let source = directory.path().join("source");
        let temporary = directory.path().join("complete-copy");
        let bytes = b"staged executable bytes";
        std::fs::write(&source, bytes).unwrap();
        std::fs::set_permissions(&source, std::fs::Permissions::from_mode(0o755)).unwrap();
        let hash = fingerprint(&source).unwrap().unwrap();
        prepare_output_temporary(&source, &temporary, &hash).unwrap();
        let inode = std::fs::metadata(&temporary).unwrap().ino();
        assert_eq!(std::fs::read(&temporary).unwrap(), bytes);
        assert_eq!(
            std::fs::metadata(&temporary).unwrap().mode() & 0o7777,
            0o755
        );
        let prepared = prepare_output_temporary(&source, &temporary, &hash).unwrap();
        assert_eq!(std::fs::metadata(&temporary).unwrap().ino(), inode);
        assert_eq!(std::fs::read(&temporary).unwrap(), bytes);
        for (name, original) in [("conflict", &b"other bytes"[..]), ("partial", &b""[..])] {
            let conflict = directory.path().join(name);
            std::fs::write(&conflict, original).unwrap();
            std::fs::set_permissions(&conflict, std::fs::Permissions::from_mode(0o755)).unwrap();
            assert!(prepare_output_temporary(&source, &conflict, &hash).is_err());
            assert_eq!(std::fs::read(&conflict).unwrap(), original);
        }
        let link = directory.path().join("additional-copy-link");
        std::fs::hard_link(&temporary, &link).unwrap();
        let destination = directory.path().join("prior-destination");
        std::fs::write(&destination, b"prior destination bytes").unwrap();
        // Mutation AFTER preparation must invalidate actual publication too.
        // Preserve both original names and all bytes; no gate move or mock.
        assert!(prepared.publish(&destination).is_err());
        assert_eq!(
            std::fs::read(&destination).unwrap(),
            b"prior destination bytes"
        );
        assert!(prepare_output_temporary(&source, &temporary, &hash).is_err());
        assert_eq!(std::fs::read(&temporary).unwrap(), bytes);
        assert_eq!(std::fs::read(&link).unwrap(), bytes);
        let redirect = directory.path().join("redirect-copy");
        symlink(&source, &redirect).unwrap();
        assert!(prepare_output_temporary(&source, &redirect, &hash).is_err());
        assert!(
            std::fs::symlink_metadata(&redirect)
                .unwrap()
                .file_type()
                .is_symlink()
        );
        let wrong_mode = directory.path().join("wrong-mode-copy");
        std::fs::write(&wrong_mode, bytes).unwrap();
        std::fs::set_permissions(&wrong_mode, std::fs::Permissions::from_mode(0o644)).unwrap();
        assert!(prepare_output_temporary(&source, &wrong_mode, &hash).is_err());
        assert_eq!(std::fs::read(&wrong_mode).unwrap(), bytes);
        assert_eq!(
            std::fs::metadata(&wrong_mode).unwrap().mode() & 0o7777,
            0o644
        );
        let publish_copy = directory.path().join("publish-copy");
        let published = directory.path().join("published-copy");
        let prepared = prepare_output_temporary(&source, &publish_copy, &hash).unwrap();
        let held_inode = prepared.file.metadata().unwrap().ino();
        prepared.publish(&published).unwrap();
        assert_eq!(std::fs::read(&published).unwrap(), bytes);
        assert_eq!(std::fs::metadata(&published).unwrap().ino(), held_inode);
        assert_eq!(
            std::fs::metadata(&published).unwrap().mode() & 0o7777,
            0o755
        );
        assert!(!publish_copy.exists());
        assert_eq!(std::fs::read(&source).unwrap(), bytes);
    }

    #[cfg(any(target_os = "linux", target_os = "macos"))]
    #[tokio::test]
    async fn output_checkpoints_restore_the_durable_epoch_on_publication_failure() {
        let _guard = test_guard!();
        for checkpoint in ["pending", "published-new", "published-existing", "baseline"] {
            for refuse_publication in [false, true] {
                let (directory, stages, mut session) = publication_fixture();
                let directory = directory.keep();
                let _stages = stages.keep();
                let local = session.recipe.phases[0].local.clone();
                let relative = PathBuf::from("build/app");
                let added = PathBuf::from("build/added");
                let absent = PathBuf::from("build/absent");
                let skip = PathBuf::from("build/published");
                std::fs::create_dir(local.join("build")).unwrap();
                std::fs::write(local.join(&relative), b"current app").unwrap();
                std::fs::write(local.join(&added), b"current addition").unwrap();
                std::fs::write(local.join(&skip), b"another job's later output").unwrap();
                let hash = fingerprint(&local.join(&relative)).unwrap().unwrap();
                let added_hash = fingerprint(&local.join(&added)).unwrap().unwrap();
                let phase = &mut session.recipe.phases[0];
                phase
                    .baseline
                    .insert(relative.clone(), "old baseline".into());
                phase
                    .baseline
                    .insert(absent.clone(), "absent baseline".into());
                phase
                    .baseline
                    .insert(skip.clone(), "published baseline".into());
                phase
                    .published
                    .insert(skip.clone(), "already published".into());
                phase.pending = Some((relative.clone(), "previous pending".into()));
                if checkpoint == "published-existing" {
                    phase
                        .published
                        .insert(relative.clone(), "previous published".into());
                }
                session.persist().unwrap();
                // Claim the actual journal after a real local owner exits.
                // The inputs above model checkpoints, not a compiler result.
                let mut owner = std::process::Command::new("sh")
                    .args(["-c", "exit 0"])
                    .spawn()
                    .unwrap();
                let owner_pid = owner.id();
                assert!(owner.wait().unwrap().success());
                {
                    let mut lease = session.writer.lease.lock().unwrap();
                    lease.wrapper_pid = owner_pid;
                    lease.process_start_ticks = None;
                    lease.boot_id = None;
                    lease.process_birth = None;
                }
                session.writer.persist().unwrap();
                session.writer = recovery_owner::claim(&session.writer).await.unwrap();
                let original_recipe = serde_json::to_value(&session.recipe).unwrap();
                let original_lease = session.writer.snapshot();
                let original_bytes = std::fs::read(&session.writer.path).unwrap();
                let gate = session.writer.path.with_extension("recovery.lock");
                let gate_alias = directory.join("additional-output-checkpoint-link.lock");
                if refuse_publication {
                    std::fs::hard_link(&gate, &gate_alias).unwrap();
                }
                let result = match checkpoint {
                    "pending" => {
                        session.persist_pending_output(0, Some((added.clone(), added_hash.clone())))
                    }
                    "published-new" | "published-existing" => {
                        session.persist_published_output(0, relative.clone(), hash.clone())
                    }
                    "baseline" => session.persist_output_baseline(
                        0,
                        &[
                            relative.clone(),
                            added.clone(),
                            absent.clone(),
                            skip.clone(),
                        ],
                    ),
                    _ => unreachable!(),
                };
                if refuse_publication {
                    assert!(result.is_err(), "{checkpoint}: refused gate was accepted");
                    assert_eq!(
                        serde_json::to_value(&session.recipe).unwrap(),
                        original_recipe
                    );
                    assert_eq!(session.writer.snapshot(), original_lease);
                    assert_eq!(std::fs::read(&session.writer.path).unwrap(), original_bytes);
                    assert!(gate_alias.is_file() && gate.is_file());
                } else {
                    result.unwrap();
                    assert_eq!(
                        serde_json::to_value(load_recipe(&session.writer).unwrap()).unwrap(),
                        serde_json::to_value(&session.recipe).unwrap()
                    );
                    let phase = &session.recipe.phases[0];
                    match checkpoint {
                        "pending" => {
                            assert_eq!(phase.pending, Some((added.clone(), added_hash.clone())))
                        }
                        "published-new" | "published-existing" => {
                            assert_eq!(phase.published.get(&relative), Some(&hash));
                            assert!(phase.pending.is_none());
                        }
                        "baseline" => {
                            assert_eq!(phase.baseline.get(&relative), Some(&hash));
                            assert_eq!(phase.baseline.get(&added), Some(&added_hash));
                            assert!(!phase.baseline.contains_key(&absent));
                            assert_eq!(phase.baseline.get(&skip).unwrap(), "published baseline");
                        }
                        _ => unreachable!(),
                    }
                }
                assert_eq!(
                    std::fs::read(local.join(&relative)).unwrap(),
                    b"current app"
                );
                assert_eq!(
                    std::fs::read(local.join(&added)).unwrap(),
                    b"current addition"
                );
                assert_eq!(
                    std::fs::read(local.join(&skip)).unwrap(),
                    b"another job's later output"
                );
            }
        }
    }

    #[test]
    fn baseline_inspection_failure_preserves_all_prior_entries() {
        let _guard = test_guard!();
        let (_directory, _stages, mut session) = publication_fixture();
        let local = session.recipe.phases[0].local.clone();
        let first = PathBuf::from("build/first");
        let invalid = PathBuf::from("build/directory");
        std::fs::create_dir_all(local.join(&invalid)).unwrap();
        std::fs::write(local.join(&first), b"later output").unwrap();
        session.recipe.phases[0]
            .baseline
            .insert(first.clone(), "prior baseline".into());
        session.persist().unwrap();
        let original_recipe = serde_json::to_value(&session.recipe).unwrap();
        let original_lease = session.writer.snapshot();
        let original_bytes = std::fs::read(&session.writer.path).unwrap();
        assert!(
            session
                .persist_output_baseline(0, &[first.clone(), invalid.clone()])
                .is_err()
        );
        assert_eq!(
            serde_json::to_value(&session.recipe).unwrap(),
            original_recipe
        );
        assert_eq!(session.writer.snapshot(), original_lease);
        assert_eq!(std::fs::read(&session.writer.path).unwrap(), original_bytes);
        assert_eq!(std::fs::read(local.join(&first)).unwrap(), b"later output");
        assert!(local.join(&invalid).is_dir());
    }

    #[cfg(any(target_os = "linux", target_os = "macos"))]
    #[tokio::test]
    async fn lifecycle_publication_keeps_session_and_journal_in_one_epoch_on_failure() {
        let _guard = test_guard!();
        for transition in [
            "execution",
            "completed",
            "returned",
            "retained-tree",
            "absent-tree",
            "pair",
            "sources",
            "retired",
        ] {
            for refuse_publication in [false, true] {
                let (directory, writer, _worker) = retained_preparation_fixture();
                // Use a genuinely exited local owner, not a fabricated owner
                // inspection result or a substitute publication function.
                let mut owner = std::process::Command::new("sh")
                    .args(["-c", "exit 0"])
                    .spawn()
                    .unwrap();
                let owner_pid = owner.id();
                assert!(owner.wait().unwrap().success());
                {
                    let mut lease = writer.lease.lock().unwrap();
                    lease.wrapper_pid = owner_pid;
                    lease.process_start_ticks = None;
                    lease.boot_id = None;
                    lease.process_birth = None;
                }
                writer.persist().unwrap();
                let writer = recovery_owner::claim(&writer).await.unwrap();
                let mut session = RecoverySession {
                    recipe: load_recipe(&writer).unwrap(),
                    writer: writer.clone(),
                };
                // These remote lifecycle flags are unit inputs only. No
                // compiler, SDK, remote source rename or slot release ran.
                session.recipe.prepared = true;
                session.recipe.execution_started =
                    transition != "execution" && transition != "absent-tree";
                if !matches!(transition, "execution" | "completed" | "absent-tree") {
                    session.recipe.exit_code = Some(7);
                }
                if matches!(transition, "pair" | "sources" | "retired") {
                    session.recipe.returned = Some(7);
                    session.recipe.tree_retired = true;
                    session.recipe.retained_materialized = Some(true);
                }
                session.recipe.pair_released = matches!(transition, "sources" | "retired");
                session.recipe.sources_released = transition == "retired";
                session.recipe.preparation_cancelled = transition == "absent-tree";
                session.persist().unwrap();
                let original_recipe = serde_json::to_value(&session.recipe).unwrap();
                let original_lease = writer.snapshot();
                let original_bytes = std::fs::read(&writer.path).unwrap();
                let gate = writer.path.with_extension("recovery.lock");
                let gate_alias = directory.join("additional-recovery-link.lock");
                if refuse_publication {
                    // Preserve the original path and bytes. A second actual
                    // link invalidates the existing publisher's single-link
                    // ownership fence, without moving or replacing the gate.
                    std::fs::hard_link(&gate, &gate_alias).unwrap();
                }
                let result = match transition {
                    "execution" => session.starting_execution(),
                    "completed" => session.completed(7),
                    "returned" => session.returned(7),
                    "retained-tree" => session.persist_tree_retirement(Some(true)),
                    "absent-tree" => session.persist_tree_retirement(Some(false)),
                    "pair" => session.pair_released(),
                    "sources" => session.sources_released(),
                    "retired" => session.retired(),
                    _ => unreachable!(),
                };
                if refuse_publication {
                    assert!(result.is_err(), "{transition}: refused gate was accepted");
                    assert_eq!(
                        serde_json::to_value(&session.recipe).unwrap(),
                        original_recipe,
                        "{transition}"
                    );
                    assert_eq!(writer.snapshot(), original_lease, "{transition}");
                    assert_eq!(
                        std::fs::read(&writer.path).unwrap(),
                        original_bytes,
                        "{transition}"
                    );
                    assert!(gate_alias.is_file() && gate.is_file());
                    if transition == "retired" {
                        // A failed write must not permit the actual retry's
                        // retired shortcut to return success. All remote
                        // release flags above are already true, so this
                        // reaches local publication without worker contact.
                        assert!(session.retire_returned().await.is_err());
                        assert_eq!(std::fs::read(&writer.path).unwrap(), original_bytes);
                    }
                } else {
                    assert!(result.is_ok(), "{transition}: {result:?}");
                    let recovered = load_recipe(&writer).unwrap();
                    assert_eq!(
                        serde_json::to_value(recovered).unwrap(),
                        serde_json::to_value(&session.recipe).unwrap()
                    );
                    assert_ne!(
                        serde_json::to_value(&session.recipe).unwrap(),
                        original_recipe
                    );
                    match transition {
                        "execution" => assert!(session.recipe.execution_started),
                        "completed" => assert_eq!(session.recipe.exit_code, Some(7)),
                        "returned" => assert_eq!(session.recipe.returned, Some(7)),
                        "retained-tree" | "absent-tree" => {
                            assert!(session.recipe.tree_retired);
                            assert_eq!(
                                session.recipe.retained_materialized,
                                Some(transition == "retained-tree")
                            );
                        }
                        "pair" => assert!(session.recipe.pair_released),
                        "sources" => assert!(session.recipe.sources_released),
                        "retired" => {
                            assert!(session.recipe.retired);
                            session.retire_returned().await.unwrap();
                        }
                        _ => unreachable!(),
                    }
                }
            }
        }
    }

    #[test]
    fn retained_source_tree_retirement_requires_an_acknowledged_filesystem_outcome() {
        let _guard = test_guard!();
        let (_directory, writer, _worker) = retained_preparation_fixture();
        let mut session = RecoverySession {
            recipe: load_recipe(&writer).unwrap(),
            writer: writer.clone(),
        };
        let before = std::fs::read(&writer.path).unwrap();
        assert!(session.tree_retired().is_err());
        assert!(!session.recipe.tree_retired);
        assert_eq!(std::fs::read(&writer.path).unwrap(), before);
        session.recipe.retained_materialized = Some(false);
        assert!(session.tree_retired().is_err());
        assert!(!session.recipe.tree_retired);
        assert_eq!(std::fs::read(&writer.path).unwrap(), before);
        assert!(writer.ensure_released_for_retry().is_err());
    }

    #[tokio::test]
    async fn retained_source_delivery_preserves_original_stages_through_retirement() {
        let _guard = test_guard!();
        for command_exit in [0, 7] {
            let (directory, writer, worker) = admitted_preparation_fixture();
            let directory = directory.keep();
            let identity = uuid::Uuid::new_v4().simple().to_string();
            let remote = format!("/data/tmp/rch/retained-lifecycle-{identity}");
            let retained = format!("{remote}.retained-{identity}");
            RecoverySession::begin(
                &writer,
                &worker,
                vec![remote.clone(), retained.clone()],
                Some((remote.clone(), identity.clone())),
                Some(remote),
                Some(retained),
                TransferConfig::default(),
                directory.clone(),
                identity,
            )
            .unwrap();
            let mut session = RecoverySession {
                recipe: load_recipe(&writer).unwrap(),
                writer: writer.clone(),
            };
            let local = directory.join("caller-outputs");
            std::fs::create_dir(&local).unwrap();
            session.recipe.project_root = local.clone();
            session.recipe.phases.push(RecoveryPhase {
                name: "project".into(),
                local: local.clone(),
                remote: "/unused/remote-output".into(),
                patterns: default_c_cpp_artifact_patterns(),
                result_dir: None,
                custom_target: false,
                output_gate: false,
                cargo_outputs: None,
                native_outputs: None,
                baseline: BTreeMap::new(),
                published: BTreeMap::new(),
                pending: None,
                complete: false,
                missing_result: None,
            });
            let stage = session.stage(0);
            std::fs::create_dir_all(stage.parent().unwrap().parent().unwrap()).unwrap();
            std::fs::create_dir(stage.parent().unwrap()).unwrap();
            std::fs::create_dir(&stage).unwrap();
            std::fs::create_dir(stage.join("build")).unwrap();
            for (path, bytes) in [
                (local.join("source.c"), b"original caller source".as_slice()),
                (
                    stage.join("source.c"),
                    b"private collector input".as_slice(),
                ),
                (
                    stage.join("build/app"),
                    b"retained delivery bytes".as_slice(),
                ),
            ] {
                OpenOptions::new()
                    .write(true)
                    .create_new(true)
                    .open(path)
                    .unwrap()
                    .write_all(bytes)
                    .unwrap();
            }
            // The actual local publisher consumes real owned files. Compiler,
            // source-retirement and release flags below are unit inputs, not
            // evidence that a worker, SDK, source seal or slot release ran.
            session.recipe.prepared = true;
            session.starting_execution().unwrap();
            session.completed(command_exit).unwrap();
            session.publish("project").await.unwrap();
            session.returned(command_exit).unwrap();
            let before_retirement = std::fs::read(&writer.path).unwrap();
            assert!(session.tree_retired().is_err());
            assert!(session.retired().is_err());
            assert_eq!(std::fs::read(&writer.path).unwrap(), before_retirement);
            assert!(!session.recipe.pair_released && !session.recipe.sources_released);

            session.recipe.retained_materialized = Some(true);
            session.tree_retired().unwrap();
            assert!(session.retired().is_err());
            session.pair_released().unwrap();
            assert!(session.retired().is_err());
            session.sources_released().unwrap();
            session.retired().unwrap();
            let recovered = reload_publication(&session);
            assert!(recovered.recipe.retired);
            assert!(recovered.recipe.phases[0].complete);
            assert_eq!(recovered.recipe.returned, Some(command_exit));
            assert!(recovered.retain_source_lifecycle_evidence());
            for (path, bytes) in [
                (local.join("source.c"), b"original caller source".as_slice()),
                (
                    stage.join("source.c"),
                    b"private collector input".as_slice(),
                ),
                (
                    stage.join("build/app"),
                    b"retained delivery bytes".as_slice(),
                ),
                (
                    local.join("build/app"),
                    b"retained delivery bytes".as_slice(),
                ),
            ] {
                assert_eq!(std::fs::read(path).unwrap(), bytes);
            }
        }
    }

    #[test]
    fn retained_source_recovery_rejects_contradictory_durable_acknowledgments() {
        let _guard = test_guard!();
        for case in [
            "tree-without-acknowledgment",
            "acknowledgment-before-tree",
            "acknowledgment-without-destination",
            "absent-after-execution",
            "absent-without-cancellation",
            "terminal-without-tree",
            "changed-destination",
            "changed-pair-token",
            "missing-retained-authority",
        ] {
            let (_directory, writer, _worker) = retained_preparation_fixture();
            let mut recipe = load_recipe(&writer).unwrap();
            match case {
                "tree-without-acknowledgment" => recipe.tree_retired = true,
                "acknowledgment-before-tree" => recipe.retained_materialized = Some(true),
                "acknowledgment-without-destination" => {
                    recipe.retained_root = None;
                    recipe.retained_materialized = Some(true);
                    recipe.tree_retired = true;
                }
                "absent-after-execution" => {
                    recipe.prepared = true;
                    recipe.execution_started = true;
                    recipe.preparation_cancelled = true;
                    recipe.retained_materialized = Some(false);
                    recipe.tree_retired = true;
                }
                "absent-without-cancellation" => {
                    recipe.retained_materialized = Some(false);
                    recipe.tree_retired = true;
                }
                "terminal-without-tree" => recipe.retired = true,
                "changed-destination" => {
                    recipe.retained_root = Some(format!(
                        "{}-other",
                        recipe.retained_root.as_deref().unwrap()
                    ));
                }
                "changed-pair-token" => {
                    recipe.pair.as_mut().unwrap().1 = "1123456789abcdef0123456789abcdef".into();
                }
                "missing-retained-authority" => {
                    recipe.source_roots = vec![recipe.retire_root.clone().unwrap()];
                }
                _ => unreachable!(),
            }
            writer
                .set_recovery(serde_json::to_value(recipe).unwrap())
                .unwrap();
            let before = std::fs::read(&writer.path).unwrap();
            let disk_writer = DurableLeaseWriter {
                path: writer.path.clone(),
                recovery_ownership: None,
                lease: Arc::new(Mutex::new(serde_json::from_slice(&before).unwrap())),
            };
            assert!(
                load_recipe(&disk_writer).is_err(),
                "accepted contradictory state: {case}"
            );
            assert_eq!(std::fs::read(&writer.path).unwrap(), before, "{case}");
        }
    }

    #[test]
    fn retained_source_recovery_distinguishes_retained_bytes_from_cancelled_absence() {
        let _guard = test_guard!();
        for materialized in [true, false] {
            let (_directory, writer, _worker) = retained_preparation_fixture();
            let mut session = RecoverySession {
                recipe: load_recipe(&writer).unwrap(),
                writer: writer.clone(),
            };
            if materialized {
                session.recipe.prepared = true;
                session.starting_execution().unwrap();
                session.completed(0).unwrap();
            } else {
                session.recipe.preparation_cancelled = true;
                session.persist().unwrap();
            }
            // This fixture supplies the filesystem acknowledgment to exercise
            // journal validation. The rename protocol has separate OS tests.
            session.recipe.retained_materialized = Some(materialized);
            session.tree_retired().unwrap();
            let persisted = reload_publication(&session).recipe;
            assert!(persisted.tree_retired);
            assert_eq!(persisted.retained_materialized, Some(materialized));
            assert_eq!(persisted.execution_started, materialized);
            assert_eq!(persisted.preparation_cancelled, !materialized);
            assert_eq!(persisted.retained_root, session.recipe.retained_root);
            assert_eq!(persisted.pair, session.recipe.pair);
            assert!(!persisted.sources_released);
            assert!(!persisted.pair_released);
            assert!(!persisted.retired);
            assert!(writer.ensure_released_for_retry().is_err());
        }
    }

    #[test]
    fn source_intent_rejects_invalid_paths_and_identity_before_journaling() {
        let invalid_roots = [
            "relative",
            "/data/projects/source-recovery/",
            "/data/projects//source-recovery",
            "/data/projects/./source-recovery",
            "/data/projects/../source-recovery",
            "/data/projects/source\nrecovery",
            "/data/projects/source\rrecovery",
            "/data/projects/source\0recovery",
        ];
        for (roots, identity) in invalid_roots
            .into_iter()
            .map(|root| (vec![root.to_owned()], "abc123"))
            .chain([
                (Vec::new(), "abc123"),
                (vec!["/data/projects/source-recovery".into()], ""),
                (vec!["/data/projects/source-recovery".into()], "bad/token"),
            ])
        {
            let (directory, writer, worker) = admitted_preparation_fixture();
            let before = std::fs::read(&writer.path).unwrap();
            let result = RecoverySession::begin(
                &writer,
                &worker,
                roots.clone(),
                None,
                None,
                None,
                TransferConfig::default(),
                directory.path().to_owned(),
                identity.into(),
            );
            assert!(
                result.is_err(),
                "admitted invalid intent {roots:?}/{identity:?}"
            );
            assert_eq!(std::fs::read(&writer.path).unwrap(), before);
            assert!(writer.snapshot().recovery.is_none());
            assert_eq!(writer.snapshot().identity.remote_build_id, Some(41));
        }
    }

    #[test]
    fn source_intent_is_durable_before_remote_acquisition_and_blocks_re_admission() {
        let (_directory, writer, worker) = preparation_fixture();
        let disk: DurableJobLease =
            serde_json::from_slice(&std::fs::read(&writer.path).unwrap()).unwrap();
        let recipe: RecoveryRecipe = serde_json::from_value(disk.recovery.unwrap()).unwrap();
        assert_eq!(recipe.identity, "abc123");
        assert_eq!(recipe.source_roots, ["/data/projects/source-recovery"]);
        assert!(!recipe.prepared);
        assert!(!recipe.execution_started);
        assert!(recipe.phases.is_empty());
        assert!(writer.ensure_released_for_retry().is_err());
        assert!(writer.admit(42, &worker.id).is_err());
        assert_eq!(writer.snapshot().identity.remote_build_id, Some(41));
        assert!(writer.acknowledge_terminal().is_err());
    }

    #[test]
    fn source_execution_requires_preparation_and_persists_before_launch() {
        let (_directory, writer, _worker) = preparation_fixture();
        let mut session = RecoverySession {
            recipe: load_recipe(&writer).unwrap(),
            writer: writer.clone(),
        };
        assert!(session.starting_execution().is_err());
        session.recipe.prepared = true;
        session.starting_execution().unwrap();
        let disk: DurableJobLease =
            serde_json::from_slice(&std::fs::read(&writer.path).unwrap()).unwrap();
        let recipe: RecoveryRecipe = serde_json::from_value(disk.recovery.unwrap()).unwrap();
        assert!(recipe.execution_started);
        assert!(
            session.starting_execution().is_err(),
            "a prepared execution is consumed once"
        );
    }

    #[test]
    fn delayed_heartbeat_publication_cannot_roll_back_execution_admission() {
        let (_directory, writer, _worker) = preparation_fixture();
        let mut recipe = load_recipe(&writer).unwrap();
        recipe.prepared = true;
        let mut session = RecoverySession {
            recipe,
            writer: writer.clone(),
        };
        let (captured_tx, captured_rx) = std::sync::mpsc::channel();
        let (release_tx, release_rx) = std::sync::mpsc::channel();
        let heartbeat_writer = writer.clone();
        let older = std::thread::spawn(move || {
            heartbeat_writer.persist_snapshot(|path, bytes| {
                captured_tx.send(()).unwrap();
                release_rx.recv().unwrap();
                atomic_write(path, bytes)
            })
        });
        captured_rx.recv_timeout(Duration::from_secs(5)).unwrap();
        let (starting_tx, starting_rx) = std::sync::mpsc::channel();
        let (finished_tx, finished_rx) = std::sync::mpsc::channel();
        let newer = std::thread::spawn(move || {
            starting_tx.send(()).unwrap();
            let result = session.starting_execution();
            finished_tx.send(()).unwrap();
            result
        });
        starting_rx.recv_timeout(Duration::from_secs(5)).unwrap();
        let overtook = finished_rx.recv_timeout(Duration::from_millis(200)).is_ok();
        // Release both threads before asserting so a failed ordering assertion
        // never strands a blocked publisher in the test process.
        release_tx.send(()).unwrap();
        older.join().unwrap().unwrap();
        newer.join().unwrap().unwrap();
        assert!(
            !overtook,
            "new execution admission overtook an older pending disk publication"
        );
        let disk: DurableJobLease =
            serde_json::from_slice(&std::fs::read(&writer.path).unwrap()).unwrap();
        let persisted: RecoveryRecipe = serde_json::from_value(disk.recovery.unwrap()).unwrap();
        assert!(
            persisted.execution_started,
            "a delayed heartbeat replaced the durable execution boundary with Preparing"
        );
    }

    #[tokio::test]
    async fn started_source_intent_cannot_be_cancelled_as_preparation() {
        let (_directory, writer, _worker) = preparation_fixture();
        let mut recipe = load_recipe(&writer).unwrap();
        recipe.prepared = true;
        recipe.execution_started = true;
        writer
            .set_recovery(serde_json::to_value(&recipe).unwrap())
            .unwrap();
        let before = std::fs::read(&writer.path).unwrap();
        assert!(!cancel_preparation(&writer).await.unwrap());
        assert_eq!(
            std::fs::read(&writer.path).unwrap(),
            before,
            "a started attempt cannot contact the worker's cancellation path or rewrite its journal"
        );
    }

    #[test]
    fn returned_outputs_do_not_authorize_terminal_ack_until_sources_release() {
        let (_directory, writer, worker) = preparation_fixture();
        let mut session = RecoverySession {
            recipe: load_recipe(&writer).unwrap(),
            writer: writer.clone(),
        };
        session.returned(0).unwrap();
        session.tree_retired().unwrap();
        session.pair_released().unwrap();
        assert!(session.retired().is_err());
        assert!(writer.acknowledge_terminal().is_err());
        session.sources_released().unwrap();
        session.retired().unwrap();
        writer.record_exit(0).unwrap();
        writer.acknowledge_terminal().unwrap();
        writer.ensure_released_for_retry().unwrap();
        writer.admit(42, &worker.id).unwrap();
        let next = writer.snapshot();
        assert!(next.recovery.is_none());
        assert!(!next.terminal_acknowledged);
        assert_eq!(next.exit_code, None);
        assert_eq!(next.identity.remote_build_id, Some(42));
    }

    #[test]
    fn unsupported_or_mismatched_source_intent_never_enters_recovery() {
        let (_directory, writer, _worker) = preparation_fixture();
        let original = load_recipe(&writer).unwrap();
        for changed in [
            {
                let mut recipe = original.clone();
                recipe.version = 1;
                recipe
            },
            {
                let mut recipe = original.clone();
                recipe.build_id += 1;
                recipe
            },
            {
                let mut recipe = original.clone();
                recipe.worker.id = WorkerId::new("other");
                recipe
            },
        ] {
            writer
                .set_recovery(serde_json::to_value(changed).unwrap())
                .unwrap();
            assert!(load_recipe(&writer).is_err());
        }
    }
}