croft-software 0.1.758

VSCode-style TUI written in Rust
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
use anyhow::{Context, Result};
use std::collections::BTreeSet;
use std::ffi::{OsStr, OsString};
use std::hash::Hasher;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, ExitStatus, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::{self, JoinHandle};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RemoteTarget {
    pub alias: String,
    pub host_name: Option<String>,
    pub user: Option<String>,
}

impl RemoteTarget {
    pub fn detail(&self) -> String {
        match (&self.user, &self.host_name) {
            (Some(user), Some(host)) => format!("{user}@{host}"),
            (Some(user), None) => format!("{user}@{}", self.alias),
            (None, Some(host)) if host != &self.alias => host.clone(),
            _ => String::new(),
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SshConfigState {
    entries: Vec<SshConfigEntryState>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct SshConfigEntryState {
    path: PathBuf,
    modified: Option<std::time::SystemTime>,
    len: Option<u64>,
}

pub fn ssh_config_state() -> SshConfigState {
    SshConfigState {
        entries: ssh_config_paths()
            .into_iter()
            .map(|path| {
                let meta = std::fs::metadata(&path).ok();
                SshConfigEntryState {
                    path,
                    modified: meta.as_ref().and_then(|m| m.modified().ok()),
                    len: meta.map(|m| m.len()),
                }
            })
            .collect(),
    }
}

#[derive(Default)]
struct HostBlock {
    aliases: Vec<String>,
    host_name: Option<String>,
    user: Option<String>,
}

pub fn discover_ssh_targets() -> Vec<RemoteTarget> {
    ssh_config_paths()
        .into_iter()
        .filter_map(|path| std::fs::read_to_string(path).ok())
        .flat_map(|content| parse_ssh_config(&content))
        .fold(
            (BTreeSet::new(), Vec::new()),
            |(mut seen, mut targets), target| {
                if seen.insert(target.alias.clone()) {
                    targets.push(target);
                }
                (seen, targets)
            },
        )
        .1
}

fn ssh_config_paths() -> Vec<PathBuf> {
    let mut paths = Vec::new();
    if let Some(home) = std::env::var_os("HOME") {
        paths.push(PathBuf::from(home).join(".ssh/config"));
    }
    paths
}

pub fn primary_ssh_config_path() -> Option<PathBuf> {
    ssh_config_paths().into_iter().next()
}

pub fn parse_ssh_config(input: &str) -> Vec<RemoteTarget> {
    let mut targets = Vec::new();
    let mut block = HostBlock::default();

    for raw in input.lines() {
        let line = strip_comment(raw).trim();
        if line.is_empty() {
            continue;
        }
        let mut parts = line.split_whitespace();
        let Some(key) = parts.next() else {
            continue;
        };
        let key = key.to_ascii_lowercase();
        if key == "host" {
            flush_block(&mut targets, &mut block);
            block.aliases = parts
                .filter(|alias| is_explicit_host_alias(alias))
                .map(ToString::to_string)
                .collect();
            continue;
        }
        if block.aliases.is_empty() {
            continue;
        }
        match key.as_str() {
            "hostname" => block.host_name = parts.next().map(ToString::to_string),
            "user" => block.user = parts.next().map(ToString::to_string),
            _ => {}
        }
    }
    flush_block(&mut targets, &mut block);
    targets
}

fn flush_block(targets: &mut Vec<RemoteTarget>, block: &mut HostBlock) {
    for alias in block.aliases.drain(..) {
        targets.push(RemoteTarget {
            alias,
            host_name: block.host_name.clone(),
            user: block.user.clone(),
        });
    }
    block.host_name = None;
    block.user = None;
}

fn strip_comment(line: &str) -> &str {
    line.split_once('#').map(|(head, _)| head).unwrap_or(line)
}

fn is_explicit_host_alias(alias: &str) -> bool {
    !alias.starts_with('!') && !alias.contains('*') && !alias.contains('?')
}

pub fn launch_croft(host: &str, path: Option<&str>, solo: bool) -> Result<RemoteOutcome> {
    launch_croft_with(host, path, None, solo)
}

/// Run any required remote-install work inside the current process,
/// streaming all subprocess stdout/stderr lines through `log_tx` so the
/// in-TUI connect dialog can render them in its log panel. Returns the
/// adopted master untouched so the caller can hand it to
/// `launch_only` once it has surrendered the alt-screen.
pub fn install_only_streaming(
    adopted: AdoptedMaster,
    log_tx: std::sync::mpsc::Sender<String>,
    can_launch_tx: std::sync::mpsc::Sender<()>,
    confirm_fallback: &mut dyn FnMut(&str) -> bool,
) -> Result<AdoptedMaster> {
    let host_label = adopted.host.clone();
    // Mirror every line into ~/.cache/croft/install.log so the install
    // remains diagnosable after the connect dialog is gone.
    let log_tx = spawn_log_tee(install_log_path(), log_tx);
    let _ = log_tx.send(format!(
        "Install session for {host_label} (croft {})",
        env!("CARGO_PKG_VERSION")
    ));
    let _ = log_tx.send(format!("Adopting control socket for {host_label}"));
    let ssh = SshControl::adopt(adopted.clone());
    let result =
        install_only_streaming_over(&ssh, &host_label, &log_tx, &can_launch_tx, confirm_fallback);
    // Never drop `ssh`: its Drop kills the shared control master, and by now
    // the user may already be attached through it (the launch signal fires
    // before any fallible work).
    std::mem::forget(ssh);
    result.map(|_| adopted)
}

/// The body of `install_only_streaming`, split out so the SSH control
/// master can be leaked on every exit path rather than dropped (its `Drop`
/// kills the shared master the user may already be attached through).
///
/// Probes the remote first: an already-installed croft releases
/// `can_launch_tx` within one SSH roundtrip and the (re)install continues
/// behind the user's session, so `confirm_fallback` is bypassed in favour
/// of an automatic decline — there is nobody left to ask, and an
/// unrequested on-box compile would load a live session.
fn install_only_streaming_over(
    ssh: &SshControl,
    host_label: &str,
    log_tx: &std::sync::mpsc::Sender<String>,
    can_launch_tx: &std::sync::mpsc::Sender<()>,
    confirm_fallback: &mut dyn FnMut(&str) -> bool,
) -> Result<()> {
    // If a croft is already on the remote, the user gets dropped into it
    // immediately and the (re)install proceeds in the background. The
    // running croft re-execs into the new binary once the stamp advances.
    // This probe runs BEFORE any local work so the launch signal fires
    // within one SSH roundtrip of connecting.
    let _ = log_tx.send(format!("Checking installed croft on {host_label}"));
    let present = remote_croft_present(ssh).unwrap_or(false);
    if present {
        let _ = can_launch_tx.send(());
    }
    let _ = log_tx.send("Hashing local source tree".to_string());
    let local_stamp = local_source_stamp()?;
    let _ = log_tx.send(format!(
        "Local source stamp: {local_stamp} (source: {})",
        env!("CARGO_MANIFEST_DIR")
    ));
    if let Some(warning) = source_snapshot_warning() {
        let _ = log_tx.send(warning);
    }
    if !remote_install_needed(ssh, &local_stamp)? {
        let _ = log_tx.send(format!("Croft on {host_label} is already up to date."));
        if !present {
            let _ = can_launch_tx.send(());
        }
        return Ok(());
    }
    // Mark the remote as updating before the local cross-compile so the
    // running croft shows "Updating" for the whole build+ship, not just
    // the final remote activation.
    mark_remote_updating(ssh);
    // The user may already be inside the remote croft over the interactive
    // master. Route every bulk byte of this install through a bulk lane so
    // the transfer never queues ahead of their keystrokes in the shared
    // TCP stream (SSH multiplexing is head-of-line blocking).
    let bulk = crate::remote_bulk::establish(&ssh.host, &ssh.socket_path, |msg| {
        let _ = log_tx.send(msg);
    });
    let _ = log_tx.send(format!("Installing/updating Croft on {host_label}"));
    // `present` means the user was already dropped into the running remote
    // croft: there is nobody left to ask, and an unasked-for compile on the
    // box would load their live session. Decline the slow path outright and
    // log how to get the update fast; a fresh install (dialog still up)
    // forwards the question to the caller's prompt.
    let mut gate = |reason: &str| {
        if present {
            let _ = log_tx.send(format!(
                "Update NOT installed: cross-build unavailable ({reason}). Run `croft setup-cross` on this machine, then reconnect — updates then ship a prebuilt binary in seconds."
            ));
            false
        } else {
            confirm_fallback(reason)
        }
    };
    if let Err(e) = install_remote_croft_streaming(ssh, &bulk.lane, &local_stamp, log_tx, &mut gate)
    {
        clear_remote_updating(ssh);
        return Err(e);
    }
    let _ = log_tx.send("Install complete.".to_string());
    Ok(())
}

/// Tee every install log line into a persistent file so a backgrounded
/// install stays diagnosable after its dialog is gone (the line saying WHY
/// the fast cross-build was skipped used to vanish with the connect
/// dialog). Lines flow through to `downstream` unchanged; file IO is
/// best-effort and never blocks the install.
pub(crate) fn spawn_log_tee(
    path: PathBuf,
    downstream: std::sync::mpsc::Sender<String>,
) -> std::sync::mpsc::Sender<String> {
    let (tx, rx) = std::sync::mpsc::channel::<String>();
    std::thread::spawn(move || {
        if let Some(parent) = path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        let mut file = std::fs::File::create(&path).ok();
        while let Ok(line) = rx.recv() {
            if let Some(f) = file.as_mut() {
                use std::io::Write as _;
                let secs = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_secs())
                    .unwrap_or(0);
                let _ = writeln!(f, "[{secs}] {line}");
            }
            // The dialog may be gone (user already inside the remote
            // croft); keep writing the file regardless.
            let _ = downstream.send(line);
        }
    });
    tx
}

fn install_log_path() -> PathBuf {
    let home = std::env::var_os("HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("."));
    home.join(".cache").join("croft").join("install.log")
}

pub const DROP_TO_LOCAL_EXIT_CODE: i32 = 88;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RemoteOutcome {
    ReturnToLocal,
    Exited,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RemoteStatusClass {
    ReturnToLocal,
    Exited,
    NotInstalled,
    Failed,
}

fn classify_remote_status(code: Option<i32>) -> RemoteStatusClass {
    match code {
        Some(DROP_TO_LOCAL_EXIT_CODE) => RemoteStatusClass::ReturnToLocal,
        Some(0) => RemoteStatusClass::Exited,
        Some(127) => RemoteStatusClass::NotInstalled,
        _ => RemoteStatusClass::Failed,
    }
}

fn outcome_or_bail(status: ExitStatus) -> Result<RemoteOutcome> {
    match classify_remote_status(status.code()) {
        RemoteStatusClass::ReturnToLocal => Ok(RemoteOutcome::ReturnToLocal),
        RemoteStatusClass::Exited => Ok(RemoteOutcome::Exited),
        _ => anyhow::bail!("ssh exited with {status}"),
    }
}

/// True when the remote host has `dtach`, which croft launches its session
/// under for persistence across an SSH transport drop. Best-effort over the
/// existing control master; any error (host unreachable, no dtach) reports
/// `false` so croft simply runs without persistence.
fn remote_has_session_supervisor(ssh: &SshControl) -> bool {
    ssh.command()
        .arg(&ssh.host)
        .arg("command -v dtach >/dev/null 2>&1")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

/// Re-establish the SSH control master after a transport drop (laptop sleep,
/// network change), printing progress. Each attempt is bounded by the master's
/// `ConnectTimeout`, then backs off; the user presses Ctrl+C to stop and fall
/// back to the local shell. Returns the fresh control once the host answers.
fn reconnect_master(host: &str) -> Option<SshControl> {
    const MAX_ATTEMPTS: u32 = 30;
    for attempt in 1..=MAX_ATTEMPTS {
        println!(
            "Connection lost. Reconnecting to {host}\u{2026} (attempt {attempt}; Ctrl+C to stop)"
        );
        match SshControl::start(host) {
            Ok(ssh) => {
                println!("Reconnected to {host}; resuming session.");
                return Some(ssh);
            }
            Err(_) => std::thread::sleep(std::time::Duration::from_secs(2)),
        }
    }
    eprintln!("Could not reconnect to {host}; returning to the local shell.");
    None
}

/// Drive the remote croft session to completion, auto-reconnecting when the
/// SSH transport dies on a host that supports session persistence. Owns the
/// control connection so a reconnect can swap in a fresh master and reattach
/// the dtach session (which kept croft alive remotely) with its state intact.
fn run_croft_session(
    mut ssh: SshControl,
    host: &str,
    path: Option<&str>,
    persistent: bool,
    solo: bool,
) -> Result<RemoteOutcome> {
    let mut bootstrapped = false;
    // The relay rendezvous is keyed on the launch identity — the very same
    // `hash(launch arg)` the dtach socket uses — NOT the remote croft's
    // `workspace_root`. The workspace diverges from the launch identity (a
    // no-path launch opens the login dir, but the user may then open a
    // subfolder, and that opened root is also preserved across the F9 re-exec),
    // so a workspace-keyed relay desynced the moment the opened folder differed
    // from where the pump's `pwd` lands. The key is deterministic, so carrying
    // it in env is freeze-safe: dtach freezes it at first launch, but every
    // reconnect recomputes the identical value, so the running croft and a fresh
    // pump always agree on one log. (The original bug was the id being *random*,
    // not its being in env.)
    let relay_id = relay_session_id(path.unwrap_or(""));
    // Per-client-process nonce for the session-host roster (#229). The relay
    // key above cannot serve as a client identity on its own: it is a pure
    // hash of the launch arg, so two terminals opening the SAME path derive
    // the same key and the host would treat the second as the first
    // reconnecting and evict it. Minting this ONCE, outside the loop, gives
    // the pair the two properties the host needs — constant across every
    // reattach this loop performs, distinct in any other process.
    //
    // Unlike the relay key this one is deliberately NOT deterministic, and
    // that is safe for the opposite reason: nothing recomputes it. Only the
    // remote client sends it, and dtach freezing the first launch's value is
    // exactly the desired behavior — that frozen value IS this client's
    // identity for the life of the session.
    let client_nonce = client_process_nonce();
    loop {
        let pump = match DropPump::start(&ssh, &relay_id) {
            Ok(p) => Some(p),
            Err(e) => {
                eprintln!("Drag-drop relay disabled: {e}");
                None
            }
        };
        let env = vec![
            (String::from("CROFT_RELAY_KEY"), relay_id.clone()),
            (String::from("CROFT_CLIENT_NONCE"), client_nonce.clone()),
        ];
        let status = run_remote_croft(&ssh, path, &env, solo)?;
        match classify_remote_status(status.code()) {
            RemoteStatusClass::ReturnToLocal => return Ok(RemoteOutcome::ReturnToLocal),
            RemoteStatusClass::Exited => return Ok(RemoteOutcome::Exited),
            RemoteStatusClass::NotInstalled if !bootstrapped => {
                println!("Croft is not installed on {host}; bootstrapping from local source");
                if let Some(warning) = source_snapshot_warning() {
                    eprintln!("{warning}");
                }
                install_remote_croft(&ssh, &local_source_stamp()?)?;
                bootstrapped = true;
            }
            RemoteStatusClass::Failed if persistent && is_transport_failure(status.code()) => {
                drop(pump);
                match reconnect_master(host) {
                    Some(new_ssh) => ssh = new_ssh,
                    None => return Ok(RemoteOutcome::Exited),
                }
            }
            _ => return outcome_or_bail(status),
        }
    }
}

/// Counterpart to `install_only_streaming`: skips the install check and
/// runs the actual remote croft. Must be called only after the terminal
/// has been returned to cooked mode and the alt-screen surrendered, since
/// the spawned ssh shares stdin/stdout/stderr with the user's terminal.
pub fn launch_only(adopted: AdoptedMaster, path: Option<&str>) -> Result<RemoteOutcome> {
    let ssh = SshControl::adopt(adopted);
    let host = ssh.host.clone();
    let persistent = remote_has_session_supervisor(&ssh);
    run_croft_session(ssh, &host, path, persistent, false)
}

fn run_command_streaming(
    mut cmd: Command,
    log_tx: &std::sync::mpsc::Sender<String>,
) -> Result<std::process::ExitStatus> {
    use std::io::{BufRead, BufReader};
    // stdin must be closed, not inherited: this runs on the background install
    // thread while the attached remote session owns the terminal, and rsync and
    // the bulk lane's own ssh would otherwise read the user's keystrokes for
    // the whole multi-minute transfer.
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let mut child = cmd.spawn().context("spawning streaming subprocess")?;
    let stdout = child.stdout.take().expect("stdout pipe");
    let stderr = child.stderr.take().expect("stderr pipe");
    let tx1 = log_tx.clone();
    let h1 = std::thread::spawn(move || {
        let r = BufReader::new(stdout);
        for line in r.lines().map_while(|l| l.ok()) {
            let _ = tx1.send(line);
        }
    });
    let tx2 = log_tx.clone();
    let h2 = std::thread::spawn(move || {
        let r = BufReader::new(stderr);
        for line in r.lines().map_while(|l| l.ok()) {
            let _ = tx2.send(line);
        }
    });
    let status = child.wait().context("waiting for subprocess")?;
    let _ = h1.join();
    let _ = h2.join();
    Ok(status)
}

/// Ship croft to the remote, preferring the fast path: cross-build a
/// static binary locally and rsync it over.
///
/// When that path is unavailable, `confirm_fallback` is asked — with the
/// reason — whether to compile on the remote box instead. Declining is an
/// error, not a silent skip, so the caller can point the user at
/// `croft setup-cross`. The slow path never engages on its own.
fn install_remote_croft_streaming(
    ssh: &SshControl,
    lane: &crate::remote_bulk::BulkLane,
    source_stamp: &str,
    log_tx: &std::sync::mpsc::Sender<String>,
    confirm_fallback: &mut dyn FnMut(&str) -> bool,
) -> Result<()> {
    let reason = match try_local_cross_install_streaming(ssh, lane, source_stamp, log_tx) {
        Ok(None) => return Ok(()),
        Ok(Some(reason)) => reason,
        Err(e) => format!("local cross-build failed: {e:#}"),
    };
    // Never slide into the slow on-box compile silently: the caller decides
    // (dialog prompt, tty prompt, or an automatic decline for background
    // updates). The fix — `croft setup-cross` — is one command away and
    // turns every future install into a seconds-long prebuilt-binary ship.
    if !confirm_fallback(&reason) {
        anyhow::bail!(
            "cross-build unavailable ({reason}); remote compile declined — run `croft setup-cross`, then reconnect for the fast install"
        );
    }
    let _ = log_tx.send(format!("Falling back to remote cargo install ({reason})"));
    let _ = log_tx.send("Syncing source tree to remote".to_string());
    sync_local_source_to_remote_streaming(ssh, lane, log_tx)?;
    let _ = log_tx
        .send("Running cargo install on remote (first time can take several minutes)".to_string());
    // The compile session is long-lived; keep it (and its streamed output)
    // off the interactive master when a dedicated lane exists.
    let mut cmd = lane.ssh_command(&ssh.socket_path);
    cmd.arg(&ssh.host).arg(remote_install_command(source_stamp));
    let status = run_command_streaming(cmd, log_tx).context("installing croft on remote")?;
    if !status.success() {
        anyhow::bail!("remote croft install failed with {status}");
    }
    Ok(())
}

/// `Ok(None)` = binary shipped via the fast path; `Ok(Some(reason))` = the
/// fast path is unavailable (the reason feeds the fallback confirmation);
/// `Err` = the fast path was attempted and broke.
fn try_local_cross_install_streaming(
    ssh: &SshControl,
    lane: &crate::remote_bulk::BulkLane,
    source_stamp: &str,
    log_tx: &std::sync::mpsc::Sender<String>,
) -> Result<Option<String>> {
    if let Some(reason) = cross_compile_unavailable_reason() {
        let _ = log_tx.send(format!("Local cross-build skipped: {reason}"));
        return Ok(Some(reason));
    }
    let Some(triple) = remote_target_triple(ssh)? else {
        let reason = String::from("could not detect the remote architecture");
        let _ = log_tx.send(format!("Local cross-build skipped: {reason}"));
        return Ok(Some(reason));
    };
    if !rust_target_installed(triple) {
        let reason = format!(
            "rustup target `{triple}` missing (run `rustup target add {triple}` once to enable the fast path)"
        );
        let _ = log_tx.send(format!("Local cross-build skipped: {reason}"));
        return Ok(Some(reason));
    }

    let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    // This build runs *concurrently* with the live remote session, whose
    // keystrokes are relayed by this same local machine. A default `-j N`
    // rustc build pins every core and starves the ssh relay + terminal
    // render, dropping the user's input. Run it niced and capped to half
    // the cores so the interactive session always preempts the compile -
    // the build is background, input latency is not. `nice` exists on both
    // macOS and Linux, so this stays identical on both launch targets.
    let jobs = std::thread::available_parallelism()
        .map(|n| (n.get() / 2).max(1))
        .unwrap_or(1)
        .to_string();
    sync_workspace_lock(&source, |msg| {
        let _ = log_tx.send(msg);
    });
    // Belt and braces with the startup raise: the zig linker dies with
    // ProcessFdQuotaExceeded under macOS's default 256-fd soft limit, and
    // the resulting silent fallback compiles on the remote box instead.
    let fd_limit = raise_fd_limit();
    let _ = log_tx.send(format!(
        "Cross-compiling croft locally for {triple} (niced, {jobs} jobs, fd limit {fd_limit})"
    ));
    let zigbuild = cargo_zigbuild_command(&source, triple, &jobs);
    let status = run_command_streaming(zigbuild, log_tx).context("running cargo zigbuild")?;
    if !status.success() {
        anyhow::bail!("cargo zigbuild exited with {status}");
    }

    let binary = source
        .join("target")
        .join(triple)
        .join("remote-fast")
        .join("croft");
    if !binary.exists() {
        anyhow::bail!(
            "cargo zigbuild reported success but {} is missing",
            binary.display()
        );
    }

    let mut mkdir = ssh.background_command();
    mkdir
        .arg(&ssh.host)
        .arg("mkdir -p \"$HOME/.cargo/bin\" \"$HOME/.cache/croft\"");
    let mkdir_status =
        run_command_streaming(mkdir, log_tx).context("creating remote install dirs")?;
    if !mkdir_status.success() {
        anyhow::bail!("remote mkdir exited with {mkdir_status}");
    }

    let dest = format!("{}:.cargo/bin/croft.new", ssh.host);
    let _ = log_tx.send(format!(
        "Rsyncing binary to {dest} (bulk lane, {} KB/s cap)",
        lane.bwlimit_kbps()
    ));
    let rsync = ship_binary_rsync_command(lane, &ssh.socket_path, &binary, &dest);
    let rsync_status =
        run_command_streaming(rsync, log_tx).context("rsyncing croft binary to remote")?;
    if !rsync_status.success() {
        anyhow::bail!("rsync exited with {rsync_status}");
    }

    let mut act = ssh.background_command();
    act.arg(&ssh.host).arg(activate_command(source_stamp));
    let act_status =
        run_command_streaming(act, log_tx).context("activating remote croft binary")?;
    if !act_status.success() {
        anyhow::bail!("remote activation exited with {act_status}");
    }
    let _ = log_tx.send("Installed croft on remote via local cross-build.".to_string());
    Ok(None)
}

/// Rsync the local source tree into `~/.cache/croft/source` on the remote,
/// streaming rsync's progress through `log_tx`. Only needed for the slow
/// compile-on-remote fallback; the fast path ships a built binary instead.
/// Runs over `lane` so the bulk transfer never queues ahead of the user's
/// keystrokes on the shared interactive master.
fn sync_local_source_to_remote_streaming(
    ssh: &SshControl,
    lane: &crate::remote_bulk::BulkLane,
    log_tx: &std::sync::mpsc::Sender<String>,
) -> Result<()> {
    let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let mut mkdir = ssh.background_command();
    mkdir.arg(&ssh.host).arg(remote_source_dir_prep(&source));
    let mkdir_status =
        run_command_streaming(mkdir, log_tx).context("creating remote source dir")?;
    if !mkdir_status.success() {
        anyhow::bail!("remote mkdir exited with {mkdir_status}");
    }
    let mut source_arg: std::ffi::OsString = source.clone().into_os_string();
    source_arg.push("/");
    let dest = format!("{}:.cache/croft/source/", ssh.host);
    let rsync = source_sync_rsync_command(lane, &ssh.socket_path, &source_arg, &dest);
    let status = run_command_streaming(rsync, log_tx).context("running rsync to remote")?;
    if !status.success() {
        anyhow::bail!("rsync exited with {status}");
    }
    Ok(())
}

/// Same as `launch_croft` but reuses an already-established SSH ControlMaster
/// socket so the in-TUI auth flow (PTY-driven password dialog) doesn't need
/// to prompt the user a second time when the post-quit path hands off the
/// connection.
pub fn launch_croft_with(
    host: &str,
    path: Option<&str>,
    adopted: Option<AdoptedMaster>,
    solo: bool,
) -> Result<RemoteOutcome> {
    println!("Connecting to {host}");
    let ssh = match adopted {
        Some(a) => SshControl::adopt(a),
        None => SshControl::start(host)?,
    };
    if remote_croft_present(&ssh).unwrap_or(false) {
        // A croft is already installed: attach to it right away. Any update
        // cross-builds and ships on a background thread, and the running
        // remote croft offers the F9 reload once the new binary lands. The
        // user must never wait behind a build for a session that already
        // exists.
        spawn_background_install(&ssh);
    } else {
        let local_stamp = local_source_stamp()?;
        println!("Installing Croft on {host}");
        if let Some(warning) = source_snapshot_warning() {
            eprintln!("{warning}");
        }
        install_remote_croft(&ssh, &local_stamp)?;
    }
    let persistent = remote_has_session_supervisor(&ssh);
    run_croft_session(ssh, host, path, persistent, solo)
}

/// Run the update check + (re)install on a detached thread so the caller can
/// hand the terminal to the already-installed remote croft immediately. Every
/// line of progress goes to `~/.cache/croft/install.log` via the streaming
/// installer's tee; nothing here may touch stdout/stderr, which belong to the
/// remote session once the caller attaches.
fn spawn_background_install(ssh: &SshControl) {
    let adopted = AdoptedMaster {
        host: ssh.host.clone(),
        socket_dir: ssh.socket_dir.clone(),
        socket_path: ssh.socket_path.clone(),
    };
    std::thread::spawn(move || {
        // Both receivers are dropped on purpose: every send in the streaming
        // installer is best-effort, and the log tee keeps writing the file
        // even with a disconnected downstream.
        let (log_tx, _) = std::sync::mpsc::channel();
        let (can_launch_tx, _) = std::sync::mpsc::channel();
        // Unreachable: this thread only runs when a croft is already on the
        // remote, and the installer auto-declines the slow fallback in that
        // case (see the `present` gate in `install_only_streaming_over`).
        let _ = install_only_streaming(adopted, log_tx, can_launch_tx, &mut |_| false);
    });
}

pub struct SshControl {
    host: String,
    socket_dir: PathBuf,
    socket_path: PathBuf,
}

/// Hand-off bundle for an already-established SSH ControlMaster, used by
/// the in-TUI password dialog to avoid re-prompting the user when the
/// post-quit flow takes over the connection.
#[derive(Clone)]
pub struct AdoptedMaster {
    pub host: String,
    pub socket_dir: PathBuf,
    pub socket_path: PathBuf,
}

impl SshControl {
    pub fn adopt(a: AdoptedMaster) -> Self {
        Self {
            host: a.host,
            socket_dir: a.socket_dir,
            socket_path: a.socket_path,
        }
    }

    fn start(host: &str) -> Result<Self> {
        let socket_dir = ssh_control_dir()?;
        std::fs::create_dir_all(&socket_dir)
            .with_context(|| format!("creating {}", socket_dir.display()))?;
        let socket_path = socket_dir.join("ctl");
        let status = Command::new("ssh")
            .arg("-M")
            .arg("-S")
            .arg(&socket_path)
            .arg("-f")
            .arg("-N")
            .arg("-T")
            // Bound each connect attempt so a reconnect against a host that is
            // still asleep/offline fails fast instead of hanging, and let the
            // master tear itself down promptly once the link goes away.
            .arg("-o")
            .arg("ConnectTimeout=10")
            .arg("-o")
            .arg("ServerAliveInterval=10")
            .arg("-o")
            .arg("ServerAliveCountMax=3")
            .arg(host)
            .status()
            .context("starting SSH control connection")?;
        if !status.success() {
            anyhow::bail!("ssh control connection exited with {status}");
        }
        Ok(Self {
            host: host.to_string(),
            socket_dir,
            socket_path,
        })
    }

    fn command(&self) -> Command {
        ssh_socket_command(&self.socket_path, false)
    }

    /// A command for the background install path, which runs while the user is
    /// already attached. See [`ssh_socket_command`] for why `-n` is
    /// load-bearing here.
    fn background_command(&self) -> Command {
        ssh_socket_command(&self.socket_path, true)
    }
}

/// Build an ssh invocation over an existing control socket.
///
/// `background` marks a command that runs *while the remote session owns the
/// terminal*. Such a command must not touch any of the three standard streams:
/// ssh forwards the caller's stdin to the remote unless `-n` is given, so
/// without it two processes read() the same tty and every keystroke reaches
/// exactly one of them, and inherited stderr writes ssh diagnostics straight
/// onto the alt screen. The interactive session is the one caller that must
/// keep the terminal, so it passes `false`.
fn ssh_socket_command(socket_path: &Path, background: bool) -> Command {
    let mut command = Command::new("ssh");
    command
        .arg("-S")
        .arg(socket_path)
        .arg("-o")
        .arg("ControlMaster=no")
        .arg("-o")
        .arg("ConnectTimeout=10")
        .arg("-o")
        .arg("ServerAliveInterval=10")
        .arg("-o")
        .arg("ServerAliveCountMax=3");
    if background {
        command
            .arg("-n")
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null());
    }
    command
}

impl Drop for SshControl {
    fn drop(&mut self) {
        let _ = Command::new("ssh")
            .arg("-S")
            .arg(&self.socket_path)
            .arg("-O")
            .arg("exit")
            .arg(&self.host)
            .status();
        let _ = std::fs::remove_dir_all(&self.socket_dir);
    }
}

/// Drag-drop relay between local Finder and a remote-launched croft.
///
/// Local croft holds an SSH ControlMaster to the remote host. We piggy-
/// back on it to expose a tiny request/response protocol so the remote
/// croft can pull files from the user's local machine without needing
/// macOS Remote Login or reverse port forwarding.
///
/// Wire format on the remote box:
///
///   `~/.cache/croft/relay-<id>/requests.log`
///       Append-only log. Remote croft writes one line per drop:
///         `pull\t<request-id>\t<absolute-local-path>\n`
///   `~/.cache/croft/relay-<id>/inbox/<request-id>/<basename>`
///       Where the file lands once scp succeeds.
///   `~/.cache/croft/relay-<id>/inbox/<request-id>/.ok`
///       Sentinel local croft writes after a successful copy.
///   `~/.cache/croft/relay-<id>/inbox/<request-id>/.err`
///       Sentinel local croft writes with the failure message.
struct DropPump {
    stop: Arc<AtomicBool>,
    tail: Option<Child>,
    handle: Option<JoinHandle<()>>,
}

impl DropPump {
    fn start(ssh: &SshControl, relay_id: &str) -> Result<Self> {
        // `relay_id` is the deterministic `hash(launch arg)` shared with the
        // remote croft via `CROFT_RELAY_KEY` (see `run_croft_session` and
        // `App::relay_dir`). Resolve $HOME on the remote and capture the literal
        // absolute relay paths so the tail and the .ok/.err sentinels all agree
        // on one location. The remote croft creates this dir lazily too, so
        // `tail -F` tolerates it not existing yet.
        let id = relay_id;
        let resolve = format!(
            "set -e; \
             RELAY=\"$HOME/.cache/croft/relay-{id}\"; \
             INBOX=\"$RELAY/inbox\"; \
             LOG=\"$RELAY/requests.log\"; \
             mkdir -p \"$INBOX\"; \
             : > \"$LOG\"; \
             printf '%s\\n%s\\n' \"$INBOX\" \"$LOG\""
        );
        let output = ssh
            .command()
            .arg(&ssh.host)
            .arg(&resolve)
            .stderr(Stdio::inherit())
            .output()
            .context("preparing remote drop relay")?;
        if !output.status.success() {
            anyhow::bail!("remote relay setup failed with {}", output.status);
        }
        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut lines = stdout.lines();
        let inbox_dir = lines
            .next()
            .ok_or_else(|| anyhow::anyhow!("remote relay setup returned no inbox path"))?
            .trim()
            .to_string();
        let requests_log = lines
            .next()
            .ok_or_else(|| anyhow::anyhow!("remote relay setup returned no log path"))?
            .trim()
            .to_string();
        if inbox_dir.is_empty() || requests_log.is_empty() {
            anyhow::bail!("remote relay setup returned blank paths");
        }
        let mut tail = Command::new("ssh")
            .arg("-S")
            .arg(&ssh.socket_path)
            .arg("-o")
            .arg("ControlMaster=no")
            .arg(&ssh.host)
            .arg(format!(
                "exec tail -F -n 0 {} 2>/dev/null",
                shell_quote(&requests_log)
            ))
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .stdin(Stdio::null())
            .spawn()
            .context("spawning remote requests tail")?;
        let stdout = tail
            .stdout
            .take()
            .context("capturing remote requests tail stdout")?;
        let stop = Arc::new(AtomicBool::new(false));
        let pump_ssh_host = ssh.host.clone();
        let pump_socket = ssh.socket_path.clone();
        let pump_inbox = inbox_dir.clone();
        let pump_stop = stop.clone();
        let handle = thread::spawn(move || {
            run_pump(pump_ssh_host, pump_socket, pump_inbox, stdout, pump_stop);
        });
        Ok(Self {
            stop,
            tail: Some(tail),
            handle: Some(handle),
        })
    }
}

impl Drop for DropPump {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::SeqCst);
        if let Some(mut tail) = self.tail.take() {
            let _ = tail.kill();
            let _ = tail.wait();
        }
        if let Some(handle) = self.handle.take() {
            let _ = handle.join();
        }
    }
}

fn run_pump(
    host: String,
    socket: PathBuf,
    inbox_dir: String,
    stdout: std::process::ChildStdout,
    stop: Arc<AtomicBool>,
) {
    let reader = BufReader::new(stdout);
    for line in reader.lines() {
        if stop.load(Ordering::SeqCst) {
            break;
        }
        let Ok(line) = line else { break };
        match parse_relay_request(&line) {
            Some(RelayRequest::Pull { id, src }) => {
                handle_pull_request(&host, &socket, &inbox_dir, &id, &src);
            }
            Some(RelayRequest::Clipboard { id }) => {
                handle_clipboard_request(&host, &socket, &inbox_dir, &id);
            }
            Some(RelayRequest::Open { id, url }) => {
                handle_open_request(&host, &socket, &inbox_dir, &id, &url);
            }
            Some(RelayRequest::Forward { id, port, open }) => {
                handle_forward_request(&host, &socket, &inbox_dir, &id, &port, open);
            }
            Some(RelayRequest::Unforward { local, remote }) => {
                handle_unforward_request(&host, &socket, &local, &remote);
            }
            None => {}
        }
    }
}

fn handle_clipboard_request(host: &str, socket: &Path, inbox_dir: &str, request_id: &str) {
    let dest_dir = format!("{inbox_dir}/{request_id}");
    let mkdir = format!("mkdir -p {}", shell_quote(&dest_dir));
    let mkdir_ok = Command::new("ssh")
        .arg("-S")
        .arg(socket)
        .arg("-o")
        .arg("ControlMaster=no")
        .arg(host)
        .arg(&mkdir)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false);
    if !mkdir_ok {
        write_relay_err(host, socket, inbox_dir, request_id, "remote mkdir failed");
        return;
    }
    let payload = match crate::clipboard::read_string() {
        Some(s) => s,
        None => {
            write_relay_err(
                host,
                socket,
                inbox_dir,
                request_id,
                "local clipboard unavailable",
            );
            return;
        }
    };
    let remote_cmd = format!(
        "cat > {dst}",
        dst = shell_quote(&format!("{dest_dir}/clipboard.txt")),
    );
    let mut ssh_recv = match Command::new("ssh")
        .arg("-S")
        .arg(socket)
        .arg("-o")
        .arg("ControlMaster=no")
        .arg(host)
        .arg(&remote_cmd)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
    {
        Ok(c) => c,
        Err(e) => {
            write_relay_err(
                host,
                socket,
                inbox_dir,
                request_id,
                &format!("clipboard relay spawn failed: {e}"),
            );
            return;
        }
    };
    if let Some(mut stdin) = ssh_recv.stdin.take() {
        use std::io::Write as _;
        let _ = stdin.write_all(payload.as_bytes());
    }
    let status = ssh_recv.wait();
    match status {
        Ok(s) if s.success() => {
            let touch = format!("touch {}/.ok", shell_quote(&dest_dir));
            let _ = Command::new("ssh")
                .arg("-S")
                .arg(socket)
                .arg("-o")
                .arg("ControlMaster=no")
                .arg(host)
                .arg(&touch)
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status();
        }
        Ok(s) => write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            &format!("clipboard relay ssh exited {s}"),
        ),
        Err(e) => write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            &format!("clipboard relay wait failed: {e}"),
        ),
    }
}

enum RelayRequest {
    Pull {
        id: String,
        src: String,
    },
    Clipboard {
        id: String,
    },
    Open {
        id: String,
        url: String,
    },
    /// Forward a remote loopback port back to the local machine over the live
    /// SSH master. `open` also launches the local browser once the tunnel is up.
    Forward {
        id: String,
        port: String,
        open: bool,
    },
    /// Tear down a forward previously added by [`RelayRequest::Forward`]. Fire
    /// and forget — no inbox sentinels, since croft just drops the entry (the
    /// parsed request id is validated but unused).
    Unforward {
        local: String,
        remote: String,
    },
}

fn parse_relay_request(line: &str) -> Option<RelayRequest> {
    let line = line.trim();
    let mut parts = line.split('\t');
    let kind = parts.next()?;
    let id = parts.next()?.to_string();
    if id.is_empty() {
        return None;
    }
    match kind {
        "pull" => {
            let src = parts.next()?.to_string();
            if src.is_empty() {
                return None;
            }
            Some(RelayRequest::Pull { id, src })
        }
        "clipboard" => Some(RelayRequest::Clipboard { id }),
        "open" => {
            let url = parts.next()?.to_string();
            if url.is_empty() {
                return None;
            }
            Some(RelayRequest::Open { id, url })
        }
        "forward" => {
            let port = parts.next()?.to_string();
            if port.is_empty() {
                return None;
            }
            let open = parts.next() == Some("1");
            Some(RelayRequest::Forward { id, port, open })
        }
        "unforward" => {
            let _ = id;
            let local = parts.next()?.to_string();
            let remote = parts.next()?.to_string();
            if local.is_empty() || remote.is_empty() {
                return None;
            }
            Some(RelayRequest::Unforward { local, remote })
        }
        _ => None,
    }
}

/// Validate a URL we're about to hand to the local Mac's `open(1)`. We
/// only accept the schemes the welcome panel ever produces — http, https,
/// mailto — so a hostile remote can't smuggle `file://` or shell metachars
/// through the relay log.
fn url_is_safe_to_open(url: &str) -> bool {
    let lower: String = url
        .chars()
        .take(16)
        .collect::<String>()
        .to_ascii_lowercase();
    let scheme_ok = lower.starts_with("https://")
        || lower.starts_with("http://")
        || lower.starts_with("mailto:");
    if !scheme_ok {
        return false;
    }
    !url.chars()
        .any(|c| c == '\0' || c == '\n' || c == '\r' || c == '\t')
}

fn handle_pull_request(host: &str, socket: &Path, inbox_dir: &str, request_id: &str, src: &str) {
    let src_path = PathBuf::from(src);
    let dest_dir = format!("{inbox_dir}/{request_id}");
    if !src_path.exists() {
        write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            &format!("local file not found: {}", src_path.display()),
        );
        return;
    }
    let Some(parent) = src_path.parent() else {
        write_relay_err(host, socket, inbox_dir, request_id, "source has no parent");
        return;
    };
    let Some(basename) = src_path.file_name() else {
        write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            "source has no basename",
        );
        return;
    };
    // Pipe a tar of the source through ssh into a remote tar -x. This
    // copies file or directory equivalently in one round trip and
    // sidesteps OpenSSH 9.x scp/sftp's realpath check on the
    // destination, which kept failing on freshly-mkdir'd dirs.
    let mut tar = match Command::new("tar")
        .env("COPYFILE_DISABLE", "1")
        .arg("-c")
        .arg("-f")
        .arg("-")
        .arg("-C")
        .arg(parent)
        .arg(basename)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::null())
        .spawn()
    {
        Ok(c) => c,
        Err(e) => {
            write_relay_err(
                host,
                socket,
                inbox_dir,
                request_id,
                &format!("local tar spawn failed: {e}"),
            );
            return;
        }
    };
    let tar_stdout = match tar.stdout.take() {
        Some(s) => s,
        None => {
            let _ = tar.wait();
            write_relay_err(host, socket, inbox_dir, request_id, "tar stdout missing");
            return;
        }
    };
    let remote_cmd = format!(
        "set -e; mkdir -p {dir} && tar -x -f - -C {dir}",
        dir = shell_quote(&dest_dir),
    );
    let ssh_recv = match Command::new("ssh")
        .arg("-S")
        .arg(socket)
        .arg("-o")
        .arg("ControlMaster=no")
        .arg(host)
        .arg(&remote_cmd)
        .stdin(Stdio::from(tar_stdout))
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
    {
        Ok(c) => c,
        Err(e) => {
            let _ = tar.wait();
            write_relay_err(
                host,
                socket,
                inbox_dir,
                request_id,
                &format!("ssh recv spawn failed: {e}"),
            );
            return;
        }
    };
    let ssh_out = ssh_recv.wait_with_output();
    let tar_out = tar.wait_with_output();
    match (tar_out, ssh_out) {
        (Ok(t), Ok(s)) if t.status.success() && s.status.success() => {
            let touch = format!("touch {}/.ok", shell_quote(&dest_dir));
            let _ = Command::new("ssh")
                .arg("-S")
                .arg(socket)
                .arg("-o")
                .arg("ControlMaster=no")
                .arg(host)
                .arg(&touch)
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status();
        }
        (Ok(t), Ok(s)) => {
            let mut detail = format!("tar exited {}, ssh exited {}", t.status, s.status);
            let tar_err = String::from_utf8_lossy(&t.stderr);
            let ssh_err = String::from_utf8_lossy(&s.stderr);
            if !tar_err.trim().is_empty() {
                detail.push_str(" | tar: ");
                detail.push_str(tar_err.trim());
            }
            if !ssh_err.trim().is_empty() {
                detail.push_str(" | remote: ");
                detail.push_str(ssh_err.trim());
            }
            write_relay_err(host, socket, inbox_dir, request_id, &detail);
        }
        (Err(e), _) | (_, Err(e)) => write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            &format!("relay pipe wait failed: {e}"),
        ),
    }
}

/// Run `cmd` on the remote over the existing master socket (no second auth, no
/// nested master). Used for the relay inbox bookkeeping (`mkdir`, sentinel
/// writes). Returns whether it exited zero.
fn ssh_exec(host: &str, socket: &Path, cmd: &str) -> bool {
    Command::new("ssh")
        .arg("-S")
        .arg(socket)
        .arg("-o")
        .arg("ControlMaster=no")
        .arg(host)
        .arg(cmd)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

/// Hand `url` to the local platform opener (`open` on macOS, `xdg-open` on
/// Linux). The caller has already validated the URL.
fn open_local_url(url: &str) -> std::io::Result<std::process::ExitStatus> {
    let program = if cfg!(target_os = "linux") {
        "xdg-open"
    } else {
        "open"
    };
    Command::new(program)
        .arg(url)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
}

fn handle_open_request(host: &str, socket: &Path, inbox_dir: &str, request_id: &str, url: &str) {
    if !url_is_safe_to_open(url) {
        write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            "rejected: URL scheme must be http/https/mailto and contain no control chars",
        );
        return;
    }
    let dest_dir = format!("{inbox_dir}/{request_id}");
    if !ssh_exec(
        host,
        socket,
        &format!("mkdir -p {}", shell_quote(&dest_dir)),
    ) {
        write_relay_err(host, socket, inbox_dir, request_id, "remote mkdir failed");
        return;
    }
    match open_local_url(url) {
        Ok(s) if s.success() => {
            let _ = ssh_exec(
                host,
                socket,
                &format!("touch {}/.ok", shell_quote(&dest_dir)),
            );
        }
        Ok(s) => write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            &format!("local open(1) exited {s}"),
        ),
        Err(e) => write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            &format!("local open(1) spawn failed: {e}"),
        ),
    }
}

/// Mirror the remote port locally when it's free, else take an ephemeral port
/// from the OS. The tiny bind race (port taken between probe and `ssh -L`) just
/// surfaces as a forward failure the user can retry.
fn pick_local_port(remote: u16) -> u16 {
    use std::net::TcpListener;
    if TcpListener::bind(("127.0.0.1", remote)).is_ok() {
        return remote;
    }
    TcpListener::bind(("127.0.0.1", 0))
        .ok()
        .and_then(|l| l.local_addr().ok())
        .map(|a| a.port())
        .unwrap_or(remote)
}

/// Forward remote loopback `port` to the local machine by adding an `-L`
/// channel to the live SSH master (`ssh -O forward` — no second auth), then,
/// when `open`, launch the local browser at the forwarded address. The chosen
/// local port is written as the `.ok` payload so remote croft can label the
/// forward and re-open it; only digits ever reach the `-L` spec, so there's no
/// metacharacter to smuggle.
fn handle_forward_request(
    host: &str,
    socket: &Path,
    inbox_dir: &str,
    request_id: &str,
    port: &str,
    open: bool,
) {
    let Some(remote_port) = port.parse::<u16>().ok().filter(|p| *p >= 1) else {
        write_relay_err(
            host,
            socket,
            inbox_dir,
            request_id,
            "rejected: port must be 1..=65535",
        );
        return;
    };
    let dest_dir = format!("{inbox_dir}/{request_id}");
    if !ssh_exec(
        host,
        socket,
        &format!("mkdir -p {}", shell_quote(&dest_dir)),
    ) {
        write_relay_err(host, socket, inbox_dir, request_id, "remote mkdir failed");
        return;
    }
    let local_port = pick_local_port(remote_port);
    let spec = format!("127.0.0.1:{local_port}:127.0.0.1:{remote_port}");
    let forwarded = Command::new("ssh")
        .arg("-S")
        .arg(socket)
        .arg("-O")
        .arg("forward")
        .arg("-L")
        .arg(&spec)
        .arg(host)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false);
    if !forwarded {
        write_relay_err(host, socket, inbox_dir, request_id, "ssh -O forward failed");
        return;
    }
    if open {
        let _ = open_local_url(&format!("http://127.0.0.1:{local_port}/"));
    }
    let _ = ssh_exec(
        host,
        socket,
        &format!("printf %s {local_port} > {}/.ok", shell_quote(&dest_dir)),
    );
}

/// Tear down a forward added by [`handle_forward_request`] via `ssh -O cancel`
/// against the live master. Best-effort and silent: only digits reach the `-L`
/// spec, and a stale cancel (the forward already gone) is harmless.
fn handle_unforward_request(host: &str, socket: &Path, local: &str, remote: &str) {
    let (Ok(local), Ok(remote)) = (local.parse::<u16>(), remote.parse::<u16>()) else {
        return;
    };
    let spec = format!("127.0.0.1:{local}:127.0.0.1:{remote}");
    let _ = Command::new("ssh")
        .arg("-S")
        .arg(socket)
        .arg("-O")
        .arg("cancel")
        .arg("-L")
        .arg(&spec)
        .arg(host)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
}

fn write_relay_err(host: &str, socket: &Path, inbox_dir: &str, request_id: &str, message: &str) {
    let dest_dir = format!("{inbox_dir}/{request_id}");
    let err_path = format!("{dest_dir}/.err");
    let cmd = format!(
        "mkdir -p {dir} && printf %s {msg} > {err}",
        dir = shell_quote(&dest_dir),
        msg = shell_quote(message),
        err = shell_quote(&err_path),
    );
    let _ = Command::new("ssh")
        .arg("-S")
        .arg(socket)
        .arg("-o")
        .arg("ControlMaster=no")
        .arg(host)
        .arg(&cmd)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
}

/// Stable id for the drop-relay directory, keyed on the launch arg — the exact
/// same `hash(launch arg)` that [`dtach_socket_path`] uses for the session
/// socket (so `relay-<id>` and `sessions/<id>.sock` share one id). The local
/// pump and the local launcher both compute it from the connection's launch
/// arg, and the launcher carries it to the long-lived remote croft as the
/// deterministic `CROFT_RELAY_KEY` env var (see `App::relay_dir`).
///
/// Two earlier schemes were wrong. A `pid-nanos` random id desynced on every
/// reconnect, because `dtach -A` reattaches the *already-running* croft whose
/// relay env froze at first launch while the pump minted a fresh id. Keying on
/// the croft's `workspace_root` desynced whenever the opened folder differed
/// from the launch identity — a no-path launch opens the login dir, the user
/// then opens a subfolder, and the pump (which only knows the launch arg) can't
/// see that divergence. The launch arg is the one identity both sides share and
/// that is invariant across an in-session workspace change and the F9 re-exec.
/// `DefaultHasher` has fixed SipHash keys, so the value is deterministic across
/// croft processes.
pub(crate) fn relay_session_id(launch_arg: &str) -> String {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    hasher.write(launch_arg.as_bytes());
    format!("{:016x}", hasher.finish())
}

/// A value unique to THIS client process, for the session-host client
/// identity (#229). Call once per process and carry the result; calling it
/// twice yields different values by design.
///
/// pid alone is not enough — pids are recycled, and two croft clients on
/// different machines attaching to one remote session can share one. Mixing
/// in the start time and the address of a local allocation distinguishes
/// those without needing a uuid dependency.
pub(crate) fn client_process_nonce() -> String {
    use std::hash::{Hash, Hasher};
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    std::process::id().hash(&mut hasher);
    if let Ok(since_epoch) = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
        since_epoch.as_nanos().hash(&mut hasher);
    }
    // ASLR plus allocator state: distinct between concurrent processes that
    // started in the same nanosecond and share a recycled pid.
    let here = Box::new(0u8);
    (Box::as_ref(&here) as *const u8 as usize).hash(&mut hasher);
    format!("{:016x}", hasher.finish())
}

pub fn ssh_control_dir() -> Result<PathBuf> {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .context("system clock before unix epoch")?
        .as_millis();
    Ok(std::env::temp_dir().join(format!("croft-ssh-{}-{now}", std::process::id())))
}

fn run_remote_croft(
    ssh: &SshControl,
    path: Option<&str>,
    env: &[(String, String)],
    solo: bool,
) -> Result<ExitStatus> {
    let mut command = ssh.command();
    command
        .arg("-tt")
        .arg(&ssh.host)
        .arg(remote_croft_command(path, env, solo));
    command.status().context("starting ssh")
}

fn install_remote_croft(ssh: &SshControl, source_stamp: &str) -> Result<()> {
    // Fast path: cross-compile a static musl binary on the local Mac and
    // rsync it directly into the remote's ~/.cargo/bin. Skips the
    // crates.io index update, the dependency walk, and the release-mode
    // codegen+link of the croft crate on the remote. The legacy
    // source-rsync + `cargo install` fallback still exists for when the
    // tooling isn't present (zig + cargo-zigbuild + the matching rust
    // target), the remote arch can't be detected, or the build fails —
    // but it never engages silently: the user confirms it on the tty
    // first, because quitting to run `croft setup-cross` once is almost
    // always the better deal.
    let reason = match try_local_cross_install(ssh, source_stamp) {
        Ok(None) => return Ok(()),
        Ok(Some(reason)) => reason,
        Err(e) => {
            eprintln!("Local cross-build failed ({e:#})");
            format!("local cross-build failed: {e:#}")
        }
    };
    if !confirm_remote_compile_on_tty(&ssh.host, &reason) {
        anyhow::bail!(
            "cross-build unavailable ({reason}); remote compile declined — run `croft setup-cross`, then `croft remote {}` again for the fast install",
            ssh.host
        );
    }
    sync_local_source_to_remote(ssh)?;
    let status = ssh
        .command()
        .arg(&ssh.host)
        .arg(remote_install_command(source_stamp))
        .status()
        .context("installing croft on remote")?;
    if !status.success() {
        anyhow::bail!("remote croft install failed with {status}");
    }
    Ok(())
}

/// Raise this process's soft RLIMIT_NOFILE as far as the hard limit
/// allows (capped at 1M). macOS launchd hands GUI/login processes a soft
/// limit of 256; croft's cross-link opens ~250 rlibs at once, so the zig
/// linker spawned under that default dies with ProcessFdQuotaExceeded and
/// the installer silently falls back to compiling on the remote box —
/// the 2026-06-12 "horrendous latency on various" root cause. Children
/// inherit the raised limit. Returns the resulting soft limit.
pub(crate) fn raise_fd_limit() -> u64 {
    let mut lim = libc::rlimit {
        rlim_cur: 0,
        rlim_max: 0,
    };
    if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut lim) } != 0 {
        return 0;
    }
    let ceiling: libc::rlim_t = 1 << 20;
    let target = if lim.rlim_max == libc::RLIM_INFINITY {
        ceiling
    } else {
        lim.rlim_max.min(ceiling)
    };
    // macOS rejects soft limits above kern.maxfilesperproc even when the
    // hard limit reads as unlimited, so step down through sane sizes
    // until one sticks. Never lower an already-high limit.
    for candidate in [target, 65536, 10240] {
        if candidate <= lim.rlim_cur {
            break;
        }
        let raised = libc::rlimit {
            rlim_cur: candidate,
            rlim_max: lim.rlim_max,
        };
        if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &raised) } == 0 {
            return candidate;
        }
    }
    lim.rlim_cur
}

/// Reason the local cross-build fast path can't run, or `None` if it can. Each
/// tool is located via [`cross_tool_program`] (PATH plus standard toolchain
/// dirs) and then probed, so a GUI launch with a stripped launchd PATH still
/// finds `~/.cargo/bin` and Homebrew. Returning a reason (instead of a bare
/// bool) lets the caller log *why* it fell back to the slow from-scratch build.
fn cross_compile_unavailable_reason() -> Option<String> {
    // `cargo zigbuild --version` is rejected by cargo-zigbuild >=0.22 (the
    // `zigbuild` subcommand has no `--version`, exit 2); probe each binary
    // directly with the arg it accepts.
    for (tool, version_arg, install_hint) in [
        ("cargo", "--version", "install Rust via https://rustup.rs"),
        ("cargo-zigbuild", "--version", "run `croft setup-cross`"),
        ("zig", "version", "run `croft setup-cross`"),
    ] {
        if cross_tool_program(tool).is_none() {
            return Some(format!(
                "`{tool}` not found on PATH or standard toolchain locations ({install_hint})"
            ));
        }
        if !probe_cross_tool(tool, version_arg) {
            return Some(format!("`{tool} {version_arg}` failed ({install_hint})"));
        }
    }
    None
}

fn probe_cross_tool(tool: &str, version_arg: &str) -> bool {
    cross_tool_command(tool)
        .arg(version_arg)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Directories searched for the cross-build toolchain. A Croft.app / Ghostty
/// launch on macOS inherits launchd's stripped PATH, so `~/.cargo/bin` and
/// Homebrew are absent even though the same tools resolve in an interactive
/// shell. PATH entries come first (an explicit override wins), then the
/// standard rustup/Homebrew locations.
fn cross_tool_search_dirs_from(path: Option<&OsStr>, home: Option<&OsStr>) -> Vec<PathBuf> {
    let mut dirs = Vec::new();
    if let Some(path) = path {
        for dir in std::env::split_paths(path) {
            push_unique_path(&mut dirs, dir);
        }
    }
    if let Some(home) = home {
        let home = PathBuf::from(home);
        push_unique_path(&mut dirs, home.join(".cargo").join("bin"));
        push_unique_path(&mut dirs, home.join(".local").join("bin"));
    }
    push_unique_path(&mut dirs, PathBuf::from("/opt/homebrew/bin"));
    push_unique_path(&mut dirs, PathBuf::from("/usr/local/bin"));
    dirs
}

fn push_unique_path(paths: &mut Vec<PathBuf>, path: PathBuf) {
    if !paths.iter().any(|existing| existing == &path) {
        paths.push(path);
    }
}

fn cross_tool_search_dirs() -> Vec<PathBuf> {
    let path = std::env::var_os("PATH");
    let home = std::env::var_os("HOME");
    cross_tool_search_dirs_from(path.as_deref(), home.as_deref())
}

/// A PATH that includes the standard toolchain dirs, handed to every spawned
/// cross-build tool so child processes (e.g. `cargo` invoking `cargo-zigbuild`
/// invoking `zig`) resolve each other even under a stripped launchd PATH.
fn cross_tool_path() -> OsString {
    let path = std::env::var_os("PATH");
    let home = std::env::var_os("HOME");
    cross_tool_path_from(path.as_deref(), home.as_deref())
}

fn cross_tool_path_from(path: Option<&OsStr>, home: Option<&OsStr>) -> OsString {
    std::env::join_paths(cross_tool_search_dirs_from(path, home))
        .unwrap_or_else(|_| path.map(OsString::from).unwrap_or_default())
}

fn cross_tool_program(tool: &str) -> Option<PathBuf> {
    find_executable_in_dirs(tool, &cross_tool_search_dirs())
}

fn find_executable_in_dirs(tool: &str, dirs: &[PathBuf]) -> Option<PathBuf> {
    dirs.iter()
        .map(|dir| dir.join(tool))
        .find(|candidate| is_executable_file(candidate))
}

fn is_executable_file(path: &Path) -> bool {
    if !path.is_file() {
        return false;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::metadata(path)
            .map(|m| m.permissions().mode() & 0o111 != 0)
            .unwrap_or(false)
    }
    #[cfg(not(unix))]
    {
        true
    }
}

/// A `Command` for a cross-build tool, resolved to its absolute path and run
/// with the augmented PATH so its own child processes resolve too.
fn cross_tool_command(tool: &str) -> Command {
    let program = cross_tool_program(tool).unwrap_or_else(|| PathBuf::from(tool));
    let mut command = Command::new(program);
    command.env("PATH", cross_tool_path());
    command
}

/// The checkout, when this binary still has one. A croft installed from a
/// release (or built in a Nix sandbox) has a `CARGO_MANIFEST_DIR` that does
/// not exist at runtime, and handing a missing directory to `current_dir`
/// fails the spawn with ENOENT before the tool ever runs - which would turn
/// "cargo is not installed" into the diagnosis on a machine where cargo works
/// fine. No checkout also means no cross-build, so the fast path is
/// unavailable either way and the existing reason reporting covers it.
fn checkout_dir() -> Option<PathBuf> {
    let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    dir.is_dir().then_some(dir)
}

/// A cross-build tool run FROM THE CHECKOUT, for the calls whose ANSWER
/// depends on which toolchain is resolved. `rust-toolchain.toml` lives there
/// and rustup reads the channel from its working directory, so a target query
/// run anywhere else answers for the default toolchain while `cargo zigbuild`
/// (which sets `current_dir(source)`) answers for the pinned one. A target
/// present on only one of them makes the guard report success immediately
/// before the build dies with E0463 - four days of silent fallback to a
/// remote `cargo install` after the 1.95.0 to 1.97.1 bump.
///
/// Deliberately NOT used for the mere "is this binary present" probes: those
/// have the same answer in any directory, and running them in the checkout
/// would make an availability check trigger a multi-hundred-MB rustup
/// auto-install of the pinned channel.
fn cross_tool_command_in_checkout(tool: &str) -> Command {
    let mut command = cross_tool_command(tool);
    if let Some(dir) = checkout_dir() {
        command.current_dir(dir);
    }
    command
}

/// The niced, job-capped `cargo zigbuild` invocation, with `cargo` resolved to
/// an absolute path and the augmented PATH exported so it finds cargo-zigbuild
/// and zig.
fn cargo_zigbuild_command(source: &Path, triple: &str, jobs: &str) -> Command {
    let cargo = cross_tool_program("cargo").unwrap_or_else(|| PathBuf::from("cargo"));
    let mut zigbuild = Command::new("nice");
    zigbuild
        .args(["-n", "19"])
        .arg(cargo)
        .args([
            "zigbuild",
            "--profile",
            "remote-fast",
            "--locked",
            "--jobs",
            jobs,
            "--bin",
            "croft",
            "--target",
            triple,
        ])
        .env("PATH", cross_tool_path())
        .current_dir(source);
    zigbuild
}

/// Re-pin the croft workspace member in `Cargo.lock` to whatever `Cargo.toml`
/// now declares, immediately before a `--locked` cross-build.
///
/// Every behavioural change bumps the patch version in `Cargo.toml`, but
/// `cargo install --path .` never rewrites the on-disk lockfile - so the lock
/// drifts exactly one patch behind. `cargo zigbuild --locked` then refuses to
/// build, and the installer silently falls back to a minutes-long
/// from-scratch `cargo install` *on the remote host* (the thing the fast path
/// exists to avoid). `cargo update -p croft-software --offline` rewrites only croft's
/// own version line - it touches no dependency, needs no network, and so keeps
/// `--locked`'s real guarantee (a reproducible dependency graph) fully intact.
///
/// Best-effort: if the sync itself fails we log and still attempt the locked
/// build, preserving the old fall-back behaviour rather than blocking install.
fn sync_workspace_lock(source: &Path, log: impl Fn(String)) {
    match cross_tool_command("cargo")
        .args(["update", "-p", "croft-software", "--offline"])
        .current_dir(source)
        .output()
    {
        Ok(out) if out.status.success() => {
            log(
                "Synced Cargo.lock to the bumped croft version before the locked cross-build"
                    .to_string(),
            );
        }
        Ok(out) => log(format!(
            "Cargo.lock sync skipped (cargo update exited {}): {}",
            out.status,
            String::from_utf8_lossy(&out.stderr).trim()
        )),
        Err(e) => log(format!("Cargo.lock sync skipped ({e})")),
    }
}

/// Build the rsync that ships the cross-built binary, routed and paced by
/// the bulk lane so it never queues ahead of the live session's keystrokes.
fn ship_binary_rsync_command(
    lane: &crate::remote_bulk::BulkLane,
    interactive_socket: &Path,
    binary: &Path,
    dest: &str,
) -> Command {
    let mut rsync = Command::new("rsync");
    rsync.args(["-az", "--checksum"]);
    rsync.args(lane.rsync_throttle_args());
    rsync.arg("-e").arg(lane.rsync_ssh_arg(interactive_socket));
    rsync.arg(binary).arg(dest);
    rsync
}

/// Prepare the remote source dir: clear every stale top-level entry, keeping
/// only `target/` (the incremental build cache the install depends on) and
/// the [`SOURCE_STAMP_INPUTS`] actually present in the local checkout.
///
/// Neither transfer can be trusted to clean up after a previous croft: the
/// allow-list's `--exclude=*` PROTECTS every unlisted path from rsync's
/// `--delete` (which is how the old deny-list's shipped `target.noindex`
/// trees, 23 to 27 GB per host, would have outlived the allow-list fix), and
/// the tar fallback never deletes at all, so an input since removed from the
/// checkout or a stale `.cargo/config.toml` - which would silently
/// reconfigure every later remote build - persists forever. Inputs the local
/// checkout still contains are deliberately KEPT: rsync deletes staleness
/// inside them itself, wiping them would re-ship the whole tree every sync,
/// and two croft processes updating the same host concurrently must never
/// yank the source out from under the other's running `cargo install`
/// (identical files are a no-op to `rsync -a`, so a concurrent sync of the
/// same checkout touches nothing).
fn remote_source_dir_prep(source: &Path) -> String {
    let keeps: String = std::iter::once("target")
        .chain(
            SOURCE_STAMP_INPUTS
                .iter()
                .copied()
                .filter(|name| source.join(name).exists()),
        )
        .map(|name| format!("! -name '{name}' "))
        .collect();
    // The `/.` dereferences a symlinked source dir (a relocated cache on a
    // small-root VPS): find does not follow a symlink given as its start
    // point, and would otherwise clean nothing while exiting 0.
    format!(
        "mkdir -p \"$HOME/.cache/croft/source\" && \
         find \"$HOME/.cache/croft/source/.\" -mindepth 1 -maxdepth 1 \
         {keeps}-exec rm -rf {{}} +"
    )
}

/// rsync filter rules that ship exactly [`SOURCE_STAMP_INPUTS`] and nothing
/// else.
///
/// An ALLOW-list, deliberately. The deny-list this replaces excluded `target`,
/// which never matched this repo's real build directory `target.noindex`, so
/// every fallback install pushed the whole 176 GB artifact tree to the remote
/// box at the bulk lane's couple of MB/s. A deny-list has to be updated every
/// time the tree grows a new directory; an allow-list derived from the very
/// list the stamp hashes cannot drift, and the two can never disagree about
/// what "the source" is.
fn source_sync_filter_args() -> Vec<String> {
    let mut args: Vec<String> = Vec::new();
    // First match wins in rsync, so the every-level denials go first: a root
    // allow-list alone would re-admit `assets/.DS_Store` and any nested
    // `target/`, which the stamp walk skips and would therefore ship without
    // ever being a reason to reinstall.
    for name in SOURCE_SKIP_NAMES {
        args.push(format!("--exclude={name}"));
    }
    for name in SOURCE_STAMP_INPUTS {
        // `/name` admits the entry itself; `/name/***` admits a directory's
        // whole subtree without needing a rule per level.
        args.push(format!("--include=/{name}"));
        args.push(format!("--include=/{name}/***"));
    }
    args.push(String::from("--exclude=*"));
    args
}

/// The build inputs that actually exist under `source`, as relative paths.
/// The tar fallback has no filter language worth the name, so it names the
/// members instead - the same allow-list, expressed the way tar accepts it.
fn source_sync_tar_members(source: &Path) -> Vec<&'static str> {
    SOURCE_STAMP_INPUTS
        .iter()
        .copied()
        .filter(|name| source.join(name).exists())
        .collect()
}

fn source_sync_rsync_command(
    lane: &crate::remote_bulk::BulkLane,
    interactive_socket: &Path,
    source_arg: &std::ffi::OsStr,
    dest: &str,
) -> Command {
    let mut rsync = Command::new("rsync");
    rsync.args(["-a", "-z", "--delete"]);
    rsync.args(source_sync_filter_args());
    rsync.args(lane.rsync_throttle_args());
    rsync.arg("-e").arg(lane.rsync_ssh_arg(interactive_socket));
    rsync.arg(source_arg).arg(dest);
    rsync
}

fn remote_target_triple(ssh: &SshControl) -> Result<Option<&'static str>> {
    let output = ssh
        .command()
        .arg(&ssh.host)
        .arg("uname -m")
        .output()
        .context("probing remote architecture")?;
    if !output.status.success() {
        return Ok(None);
    }
    let arch = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(arch_to_musl_triple(&arch))
}

pub fn arch_to_musl_triple(arch: &str) -> Option<&'static str> {
    match arch {
        "x86_64" | "amd64" => Some("x86_64-unknown-linux-musl"),
        "aarch64" | "arm64" => Some("aarch64-unknown-linux-musl"),
        _ => None,
    }
}

fn rust_target_installed(triple: &str) -> bool {
    let Ok(output) = cross_tool_command_in_checkout("rustup")
        .args(["target", "list", "--installed"])
        .output()
    else {
        return false;
    };
    if !output.status.success() {
        return false;
    }
    String::from_utf8_lossy(&output.stdout)
        .lines()
        .any(|line| line.trim() == triple)
}

/// `Ok(None)` = binary shipped via the fast path; `Ok(Some(reason))` = the
/// fast path is unavailable (the reason feeds the fallback confirmation);
/// `Err` = the fast path was attempted and broke.
fn try_local_cross_install(ssh: &SshControl, source_stamp: &str) -> Result<Option<String>> {
    if let Some(reason) = cross_compile_unavailable_reason() {
        eprintln!("Local cross-build skipped: {reason}");
        return Ok(Some(reason));
    }
    let Some(triple) = remote_target_triple(ssh)? else {
        let reason = String::from("could not detect the remote architecture");
        eprintln!("Local cross-build skipped: {reason}");
        return Ok(Some(reason));
    };
    if !rust_target_installed(triple) {
        let reason = format!(
            "rustup target `{triple}` not installed (run `rustup target add {triple}` once to enable the fast path)"
        );
        eprintln!("Local cross-build skipped: {reason}");
        return Ok(Some(reason));
    }

    let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    sync_workspace_lock(&source, |msg| println!("{msg}"));
    raise_fd_limit();
    let jobs = std::thread::available_parallelism()
        .map(|n| (n.get() / 2).max(1))
        .unwrap_or(1)
        .to_string();
    println!("Cross-compiling croft locally for {triple} (niced, {jobs} jobs)");
    let status = cargo_zigbuild_command(&source, triple, &jobs)
        .status()
        .context("running cargo zigbuild")?;
    if !status.success() {
        anyhow::bail!("cargo zigbuild exited with {status}");
    }

    let binary = source
        .join("target")
        .join(triple)
        .join("remote-fast")
        .join("croft");
    if !binary.exists() {
        anyhow::bail!(
            "cargo zigbuild reported success but {} is missing",
            binary.display()
        );
    }

    let mkdir = ssh
        .command()
        .arg(&ssh.host)
        .arg("mkdir -p \"$HOME/.cargo/bin\" \"$HOME/.cache/croft\"")
        .status()
        .context("creating remote install dirs")?;
    if !mkdir.success() {
        anyhow::bail!("remote mkdir exited with {mkdir}");
    }

    let ssh_e = format!(
        "ssh -S {} -o ControlMaster=no",
        shell_quote_for_e_arg(&ssh.socket_path),
    );
    let dest = format!("{}:.cargo/bin/croft.new", ssh.host);
    let rsync_status = Command::new("rsync")
        .args(["-az", "--checksum", "-e"])
        .arg(&ssh_e)
        .arg(&binary)
        .arg(&dest)
        .status()
        .context("rsyncing croft binary to remote")?;
    if !rsync_status.success() {
        anyhow::bail!("rsync exited with {rsync_status}");
    }

    // Atomic-swap the freshly-shipped binary into place. `mv` on the
    // same filesystem is the standard way to avoid the race where a
    // mid-flight process opens a half-written executable.
    let activate_status = ssh
        .command()
        .arg(&ssh.host)
        .arg(activate_command(source_stamp))
        .status()
        .context("activating remote croft binary")?;
    if !activate_status.success() {
        anyhow::bail!("remote activation exited with {activate_status}");
    }
    println!("Installed croft on remote via local cross-build.");
    Ok(None)
}

/// Ask on the controlling terminal before the slow on-box compile. The
/// answer defaults to "no": quitting to run `croft setup-cross` once turns
/// every future install into a seconds-long prebuilt-binary ship, so the
/// slow path must be an explicit choice, never a silent fallback.
/// Non-interactive stdin (scripts, CI) declines for the same reason.
fn confirm_remote_compile_on_tty(host: &str, reason: &str) -> bool {
    use std::io::{IsTerminal as _, Write as _};
    eprintln!("Fast install unavailable: {reason}");
    eprintln!(
        "Croft can compile itself on {host} instead; the first build can take several minutes and loads the box."
    );
    if !std::io::stdin().is_terminal() {
        eprintln!(
            "stdin is not a terminal; declining the remote compile. Run `croft setup-cross`, then retry."
        );
        return false;
    }
    eprint!(
        "Compile on {host} anyway? [y/N] (N quits; run `croft setup-cross` once and reconnect — much faster) "
    );
    let _ = std::io::stderr().flush();
    let mut line = String::new();
    if std::io::stdin().read_line(&mut line).is_err() {
        return false;
    }
    matches!(line.trim().to_ascii_lowercase().as_str(), "y" | "yes")
}

pub fn remote_croft_command(path: Option<&str>, env: &[(String, String)], solo: bool) -> String {
    remote_croft_command_for_terminal(
        path,
        std::env::var("TERM_PROGRAM").ok().as_deref(),
        std::env::var("TERM").ok().as_deref(),
        crate::iterm2_inline::detect_osk_auto(),
        env,
        solo,
    )
}

fn remote_croft_command_for_terminal(
    path: Option<&str>,
    term_program: Option<&str>,
    term: Option<&str>,
    osk: bool,
    env: &[(String, String)],
    solo: bool,
) -> String {
    use crate::iterm2_inline::InlineImageProtocol;
    let mut prefix = String::from("export CROFT_REMOTE_AUTOUPDATE=1; ");
    // SSH from Termux does not forward TERMUX_VERSION, so the remote croft
    // can't detect the touch environment itself; carry the local on-screen
    // keyboard detection across the hop like the TERM_PROGRAM hint below.
    if osk {
        prefix.push_str("export CROFT_FORCE_OSK=1; ");
    }
    // The remote croft renders into *this* terminal over the SSH PTY, so it has
    // to use whichever inline-image protocol the local terminal speaks. SSH
    // forwards `TERM` but not `TERM_PROGRAM` (absent an `AcceptEnv` opt-in), so
    // export the hint the remote detector needs explicitly rather than relying
    // on the environment surviving the hop.
    match crate::iterm2_inline::inline_image_protocol_for(term_program, term) {
        InlineImageProtocol::ITerm2 => {
            // iTerm2/WezTerm implement OSC 1337 inline images + `SetColors`;
            // force them on and carry `TERM_PROGRAM` so the remote paints the
            // session background too.
            let tp = term_program.unwrap_or("iTerm.app");
            prefix.push_str("export CROFT_FORCE_INLINE_IMAGES=1 TERM_PROGRAM=");
            prefix.push_str(&shell_quote(tp));
            prefix.push_str("; ");
        }
        InlineImageProtocol::Kitty => {
            // Ghostty/Kitty render via the Kitty graphics protocol and paint the
            // background with explicit truecolor (no `SetColors`), so export
            // `TERM_PROGRAM=ghostty` only; do NOT force the OSC-1337 path.
            prefix.push_str("export TERM_PROGRAM=ghostty; ");
        }
        // Sixel is never returned by `inline_image_protocol_for` (it has no env
        // signal); the remote croft discovers it by running its own DA1 probe
        // against this same terminal over the SSH PTY, so no hint is forwarded.
        InlineImageProtocol::Sixel => {}
        InlineImageProtocol::None => {}
    }
    for (k, v) in env {
        prefix.push_str("export ");
        prefix.push_str(k);
        prefix.push('=');
        prefix.push_str(&shell_quote(v));
        prefix.push_str("; ");
    }
    prefix.push_str("export PATH=\"$HOME/.cargo/bin:$PATH\"; command -v croft >/dev/null 2>&1 || { echo 'croft not found on remote PATH' >&2; exit 127; }; ");
    let croft_invocation = match path.filter(|p| !p.is_empty()) {
        Some(p) => format!("croft {}", shell_quote(p)),
        None => String::from("croft"),
    };
    // A solo guest never attaches the shared PTY: it runs its own croft
    // (independent viewport) wired to the workspace's collab relay for
    // shared-file edits (docs/MULTIPLAYER.md, Phase D). The probe keeps a
    // freshly shipped local croft from driving an unknown subcommand at a
    // remote binary the background updater hasn't replaced yet, exactly
    // like the session-host probe below; such a host falls back to a plain
    // (non-collab) croft rather than failing the connect. No persistence
    // supervisor: a solo viewport is per-guest scratch state, and wrapping
    // it in the mux would just share its PTY again.
    if solo {
        let collab_socket = collab_socket_path(path);
        return format!(
            "{prefix}if croft collab-relay --probe >/dev/null 2>&1; then mkdir -p \"$(dirname \"{collab_socket}\")\"; croft collab-relay --ensure --socket \"{collab_socket}\"; export CROFT_COLLAB_SOCKET=\"{collab_socket}\"; export CROFT_COLLAB_ROLE=guest; exec {croft_invocation}; else exec {croft_invocation}; fi"
        );
    }
    // Run croft under a persistence supervisor so the session survives an SSH
    // transport drop (laptop sleep, network change). Preferred: croft's own
    // session host (`croft session-host`, the multiplayer mux; see
    // docs/MULTIPLAYER.md) — attach-or-create semantics, byte-transparent
    // broadcast to every attached client, server-side write control, and it
    // propagates the inner exit code (dtach never did, so drop-to-local's 88
    // only works on this branch). The `--probe` guard keeps a freshly shipped
    // local croft from sending an unknown subcommand at a remote binary the
    // background updater hasn't replaced yet. Fallback: dtach, exactly as
    // before (`-A` attach-or-create, `-E`/`-z` keep its hands off croft's
    // Ctrl chords, `-r winch` repaints on reattach). Both supervisors are
    // byte-transparent so OSC-1337 / Kitty graphics pass through untouched —
    // tmux corrupts the Kitty protocol, which is why neither branch uses it.
    // Hosts with neither exec croft directly, no persistence.
    // `CROFT_SESSION_PERSISTENT=1` is exported only on the persistent
    // branches so the remote croft can tell whether its session survives a
    // transport drop and surface that on its status line.
    let socket = dtach_socket_path(path);
    let mux_socket = mux_socket_path(path);
    let workspace_flag = match path.filter(|p| !p.is_empty()) {
        Some(p) => format!(" --workspace {}", shell_quote(p)),
        None => String::new(),
    };
    format!(
        "{prefix}if croft session-host --probe >/dev/null 2>&1; then mkdir -p \"$(dirname \"{mux_socket}\")\"; export CROFT_SESSION_PERSISTENT=1; exec croft session-host --socket \"{mux_socket}\"{workspace_flag} -- {croft_invocation}; elif command -v dtach >/dev/null 2>&1; then mkdir -p \"$(dirname \"{socket}\")\"; export CROFT_SESSION_PERSISTENT=1; exec dtach -A \"{socket}\" -E -z -r winch {croft_invocation}; else exec {croft_invocation}; fi"
    )
}

/// Remote path of the dtach control socket for a workspace. Keyed on a stable
/// hash of the workspace path so a reconnect reattaches the same session.
/// `DefaultHasher` uses fixed SipHash keys, so the name is deterministic across
/// croft processes (a randomized hasher would orphan the session on relaunch).
fn dtach_socket_path(path: Option<&str>) -> String {
    use std::hash::Hasher;
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    hasher.write(path.unwrap_or("").as_bytes());
    format!("$HOME/.cache/croft/sessions/{:016x}.sock", hasher.finish())
}

/// Remote socket for a mux (session-host) session: same keying as the dtach
/// socket (the raw launch arg, per the relay-key post-mortem above
/// `relay_session_id`) but a distinct name, so a mux client never speaks
/// croft's frame protocol at a live dtach server left over from an older
/// binary.
fn mux_socket_path(path: Option<&str>) -> String {
    use std::hash::Hasher;
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    hasher.write(path.unwrap_or("").as_bytes());
    format!(
        "$HOME/.cache/croft/sessions/{:016x}.mux.sock",
        hasher.finish()
    )
}

/// Remote socket carrying Phase D collab ops between independent-viewport
/// participants (never PTY bytes): same keying as the dtach and mux sockets,
/// its own endpoint (see docs/MULTIPLAYER.md).
fn collab_socket_path(path: Option<&str>) -> String {
    use std::hash::Hasher;
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    hasher.write(path.unwrap_or("").as_bytes());
    format!(
        "$HOME/.cache/croft/sessions/{:016x}.collab.sock",
        hasher.finish()
    )
}

/// True for ssh's own connection-failure exit code (255), as opposed to any
/// status the remote croft itself returned. Only this warrants an auto-
/// reconnect; a real remote crash (e.g. 101) must surface to the user.
fn is_transport_failure(code: Option<i32>) -> bool {
    code == Some(255)
}

fn remote_install_needed(ssh: &SshControl, local_stamp: &str) -> Result<bool> {
    let output = ssh
        .command()
        .arg(&ssh.host)
        .arg(remote_install_check_command())
        .output()
        .context("checking remote croft install")?;
    if !output.status.success() {
        return Ok(true);
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim() != local_stamp)
}

fn remote_install_check_command() -> &'static str {
    r#"if [ -f "$HOME/.cargo/env" ]; then
  . "$HOME/.cargo/env"
fi
export PATH="$HOME/.cargo/bin:$PATH"
command -v croft >/dev/null 2>&1 && cat "$HOME/.cache/croft/install-stamp" 2>/dev/null"#
}

fn sync_local_source_to_remote(ssh: &SshControl) -> Result<()> {
    match sync_via_rsync(ssh) {
        Ok(()) => Ok(()),
        Err(e) => {
            // Fallback path so first-time installs on minimal remotes
            // (alpine images, locked-down VMs) that ship without rsync
            // still succeed. The trade-off is a from-scratch dependency
            // rebuild on those hosts, same as the legacy behaviour.
            eprintln!("rsync sync failed ({e}); falling back to tar pipe");
            sync_via_tar(ssh)
        }
    }
}

/// Mirror the local source tree onto the remote with rsync, preserving
/// mtimes and skipping unchanged files. Critically, this does NOT delete
/// `target/` on the remote, so `cargo install` does an incremental rebuild
/// (typically rebuilding only the 1-3 crates that actually changed)
/// instead of starting from scratch every time.
fn sync_via_rsync(ssh: &SshControl) -> Result<()> {
    let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let mkdir_status = ssh
        .command()
        .arg(&ssh.host)
        .arg(remote_source_dir_prep(&source))
        .status()
        .context("creating remote source dir")?;
    if !mkdir_status.success() {
        anyhow::bail!("remote mkdir exited with {mkdir_status}");
    }
    let ssh_e = format!(
        "ssh -S {} -o ControlMaster=no",
        shell_quote_for_e_arg(&ssh.socket_path),
    );
    let mut source_arg: std::ffi::OsString = source.clone().into_os_string();
    source_arg.push("/");
    let dest = format!("{}:.cache/croft/source/", ssh.host);
    let status = Command::new("rsync")
        .args(["-a", "-z", "--delete"])
        .args(source_sync_filter_args())
        .arg("-e")
        .arg(&ssh_e)
        .arg(&source_arg)
        .arg(&dest)
        .status()
        .context("running rsync to remote")?;
    if !status.success() {
        anyhow::bail!("rsync exited with {status}");
    }
    Ok(())
}

/// Last-resort tar pipe sync used when rsync isn't available on either
/// side. This path also keeps the remote source dir in place rather than
/// blowing it away — extracting over an existing tree overwrites changed
/// files, so `target/` still survives between installs even when the user
/// is forced down this branch.
fn sync_via_tar(ssh: &SshControl) -> Result<()> {
    let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let mut tar = Command::new("tar")
        .env("COPYFILE_DISABLE", "1")
        .args(["-czf", "-"])
        .args(
            SOURCE_SKIP_NAMES
                .iter()
                .map(|name| format!("--exclude={name}")),
        )
        .arg("-C")
        .arg(&source)
        .args(source_sync_tar_members(&source))
        .stdout(Stdio::piped())
        .spawn()
        .with_context(|| format!("packing {}", source.display()))?;

    let tar_stdout = tar.stdout.take().context("opening tar stdout")?;
    let mut remote = ssh
        .command()
        .arg(&ssh.host)
        .arg(format!(
            "{} && tar -xzf - -C \"$HOME/.cache/croft/source\"",
            remote_source_dir_prep(&source)
        ))
        .stdin(Stdio::from(tar_stdout))
        .spawn()
        .context("copying croft source to remote")?;

    let ssh_status = remote.wait().context("waiting for remote source copy")?;
    let tar_status = tar.wait().context("waiting for local source archive")?;
    if !tar_status.success() {
        anyhow::bail!("local source archive failed with {tar_status}");
    }
    if !ssh_status.success() {
        anyhow::bail!("remote source copy failed with {ssh_status}");
    }
    Ok(())
}

/// Quote a path for embedding inside rsync's `-e "ssh -S <path> "`
/// argument. rsync executes the remote-shell string through `/bin/sh -c`,
/// so any spaces or shell metacharacters in the control-socket path would
/// break the invocation. The control socket lives under
/// `std::env::temp_dir()` which is normally space-free, but the socket
/// path is process-id + millisecond-stamped so quote defensively rather
/// than trust the format.
pub(crate) fn shell_quote_for_e_arg(p: &std::path::Path) -> String {
    let s = p.to_string_lossy();
    if s.chars()
        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '/' | '.' | '_' | '-'))
    {
        s.into_owned()
    } else {
        format!("'{}'", s.replace('\'', r"'\''"))
    }
}

fn remote_install_command(source_stamp: &str) -> String {
    format!(
        r#"set -e
if [ -f "$HOME/.cargo/env" ]; then
  . "$HOME/.cargo/env"
fi
export PATH="$HOME/.cargo/bin:$PATH"

# Package installs need root. Run them directly when already root, otherwise
# via sudo if available. CROFT_SUDO is empty in both the root and the
# no-sudo case so the install commands stay identical.
if [ "$(id -u)" = "0" ]; then
  CROFT_SUDO=""
elif command -v sudo >/dev/null 2>&1; then
  CROFT_SUDO="sudo"
else
  CROFT_SUDO=""
fi

# Install system packages with whatever package manager the box ships.
croft_pkg_install() {{
  if command -v apt-get >/dev/null 2>&1; then
    export DEBIAN_FRONTEND=noninteractive
    $CROFT_SUDO apt-get update
    $CROFT_SUDO apt-get install -y "$@"
  elif command -v dnf >/dev/null 2>&1; then
    $CROFT_SUDO dnf install -y "$@"
  elif command -v yum >/dev/null 2>&1; then
    $CROFT_SUDO yum install -y "$@"
  elif command -v apk >/dev/null 2>&1; then
    $CROFT_SUDO apk add "$@"
  elif command -v pacman >/dev/null 2>&1; then
    $CROFT_SUDO pacman -Sy --noconfirm "$@"
  elif command -v zypper >/dev/null 2>&1; then
    $CROFT_SUDO zypper install -y "$@"
  else
    return 1
  fi
}}

# croft compiles native crates from source, so the final link step needs a C
# compiler/linker (`cc`) and pkg-config. Stock cloud images (Ubuntu Server,
# minimal Fedora, Alpine, ...) ship without them, which is why a bare
# `cargo install` dies with `linker `cc` not found`. The user opted into a
# from-source install, so install the toolchain rather than fail. This runs
# unconditionally, even when cargo already exists, because cargo can be
# present on a box that still lacks cc. The per-manager package names differ:
# Debian bundles the compiler in build-essential; others name it separately.
croft_ensure_build_toolchain() {{
  if command -v cc >/dev/null 2>&1 && command -v pkg-config >/dev/null 2>&1; then
    return 0
  fi
  if command -v apt-get >/dev/null 2>&1; then
    export DEBIAN_FRONTEND=noninteractive
    $CROFT_SUDO apt-get update
    $CROFT_SUDO apt-get install -y build-essential pkg-config
  elif command -v dnf >/dev/null 2>&1; then
    $CROFT_SUDO dnf install -y gcc make pkgconf-pkg-config
  elif command -v yum >/dev/null 2>&1; then
    $CROFT_SUDO yum install -y gcc make pkgconfig
  elif command -v apk >/dev/null 2>&1; then
    $CROFT_SUDO apk add build-base pkgconf
  elif command -v pacman >/dev/null 2>&1; then
    $CROFT_SUDO pacman -Sy --noconfirm base-devel pkgconf
  elif command -v zypper >/dev/null 2>&1; then
    $CROFT_SUDO zypper install -y gcc make pkg-config
  else
    echo 'croft: no supported package manager found to install a C toolchain (need cc + pkg-config)' >&2
    return 1
  fi
}}

if ! command -v cargo >/dev/null 2>&1; then
  if ! command -v curl >/dev/null 2>&1; then
    if ! croft_pkg_install curl ca-certificates; then
      echo 'cargo and curl are missing and no supported package manager was found to bootstrap them' >&2
      exit 127
    fi
  fi
  curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal
  . "$HOME/.cargo/env"
fi
export PATH="$HOME/.cargo/bin:$PATH"
# Ensure the C toolchain before compiling, regardless of whether cargo was
# already installed: a box can have rustup/cargo but no cc.
croft_ensure_build_toolchain
# dtach lets a remote croft session survive an SSH transport drop (laptop
# sleep, network change): croft launches itself under it for reconnect. Best
# effort and never fatal; a box without dtach just runs without persistence.
command -v dtach >/dev/null 2>&1 || croft_pkg_install dtach || true
# Cap the host build to ~half its cores and run it niced: this fallback
# compiles on the shared remote (often while other workloads run and,
# with launch-now, while a live croft session is using the box), so the
# default `cargo install -j <all cores>` would pin the machine. (nproc+1)/2
# stays >=1 even on a single core; nice is best-effort if present.
CROFT_JOBS=$(( ( $(nproc 2>/dev/null || echo 2) + 1 ) / 2 ))
# A live croft session on this box outranks the build entirely: the user
# is typing into a shell that shares these cores, RAM, and disk. Half the
# cores still wrecks a small VPS, so drop to one compile job and put all
# codegen IO in the idle class; the update simply takes longer.
if pgrep -x croft >/dev/null 2>&1; then
  CROFT_JOBS=1
fi
CROFT_NICE=""
if command -v nice >/dev/null 2>&1; then CROFT_NICE="nice -n 19"; fi
CROFT_IONICE=""
if command -v ionice >/dev/null 2>&1; then CROFT_IONICE="ionice -c3"; fi
# eval, because this script runs under the remote user's login shell and
# zsh does not word-split unquoted parameters: bare `$CROFT_NICE ...` would
# try to run a command literally named "nice -n 19". eval re-parses the
# assembled line, which splits correctly under both sh/bash and zsh.
eval "$CROFT_NICE $CROFT_IONICE"' cargo install --path "$HOME/.cache/croft/source" --jobs "$CROFT_JOBS" --force --locked'
mkdir -p "$HOME/.cache/croft"
printf %s {stamp} > "$HOME/.cache/croft/install-stamp"
rm -f "$HOME/.cache/croft/updating"
"#,
        stamp = shell_quote(source_stamp)
    )
}

/// Final swap step shared by the cross-build and rsync fast paths: chmod
/// the staged binary, atomically `mv` it over the live one, write the new
/// install-stamp, then clear the `updating` marker. The stamp is written
/// only after the binary is in place, so a remote-launched croft that
/// watches the stamp never re-execs into a half-shipped binary.
fn activate_command(source_stamp: &str) -> String {
    format!(
        "chmod 755 \"$HOME/.cargo/bin/croft.new\" && mv \"$HOME/.cargo/bin/croft.new\" \"$HOME/.cargo/bin/croft\" && printf %s {stamp} > \"$HOME/.cache/croft/install-stamp\" && rm -f \"$HOME/.cache/croft/updating\"",
        stamp = shell_quote(source_stamp)
    )
}

/// Drop the `updating` marker so a remote-launched croft shows its
/// "Updating" indicator for the whole build+ship, not just the final
/// remote-side activation. Written before the (long) local cross-compile.
fn mark_remote_updating(ssh: &SshControl) {
    let _ = ssh
        .background_command()
        .arg(&ssh.host)
        .arg("mkdir -p \"$HOME/.cache/croft\" && : > \"$HOME/.cache/croft/updating\"")
        .status();
}

/// Clear the marker after a failed install so the indicator resolves to a
/// brief "update failed" rather than hanging on "Updating" forever.
fn clear_remote_updating(ssh: &SshControl) {
    let _ = ssh
        .background_command()
        .arg(&ssh.host)
        .arg("rm -f \"$HOME/.cache/croft/updating\"")
        .status();
}

/// True when a croft binary is already resolvable on the remote PATH,
/// regardless of version. Drives the launch-now path: an existing binary
/// can be launched immediately while a newer one installs underneath it.
fn remote_croft_present(ssh: &SshControl) -> Result<bool> {
    let output = ssh
        .command()
        .arg(&ssh.host)
        .arg(
            r#"if [ -f "$HOME/.cargo/env" ]; then . "$HOME/.cargo/env"; fi
export PATH="$HOME/.cargo/bin:$PATH"
command -v croft >/dev/null 2>&1 && echo yes"#,
        )
        .output()
        .context("probing remote croft presence")?;
    Ok(String::from_utf8_lossy(&output.stdout).trim() == "yes")
}

fn local_source_stamp() -> Result<String> {
    source_stamp_for(&PathBuf::from(env!("CARGO_MANIFEST_DIR")))
}

/// A croft installed with `cargo install` from a registry or git snapshot
/// bakes that frozen checkout as its `CARGO_MANIFEST_DIR`, so the stamp it
/// hashes can never change: "already up to date" then reports on a dead tree
/// while the user's working repo drifts, and installs silently ship nothing.
/// (2026-08-22: a session-host fix believed deployed was never shipped
/// because of exactly this.) Every install path surfaces this warning so the
/// no-op is loud instead of silent.
fn source_snapshot_warning() -> Option<String> {
    let dir = env!("CARGO_MANIFEST_DIR");
    source_dir_is_snapshot(dir).then(|| {
        format!(
            "WARNING: this croft was installed from an immutable snapshot ({dir}); remote installs can never ship local changes. Reinstall with `cargo install --path <your repo>` to make installs track your tree."
        )
    })
}

/// The two places `cargo install` unpacks immutable sources: registry
/// tarballs and git checkouts. Both are subtrees of `$CARGO_HOME`, and a
/// custom `CARGO_HOME` need not contain `.cargo` anywhere in its path
/// (`CARGO_HOME=/custom/cargo-home` unpacks to
/// `/custom/cargo-home/registry/src/...`), so only the two layout-defining
/// components are matched, with no assumption about the prefix.
fn source_dir_is_snapshot(dir: &str) -> bool {
    dir.contains("/registry/src/") || dir.contains("/git/checkouts/")
}

/// Names of the root entries that shape the shipped binary: the crate source,
/// the embedded asset tree, and the build/toolchain pins. The stamp hashes
/// exactly these, so build artifacts (`target`, `target.noindex`), `.git`,
/// docs, and editor scratch dirs can never stall the hash or force a reship —
/// the old deny-list stamp read a 98 GB `target.noindex` on every connect and
/// changed after every local build.
/// Names skipped at EVERY level of the crate source, by the stamp walk and by
/// the sync filters alike. Sharing the list is what makes "the stamp and the
/// sync agree on what the source is" a fact rather than a hope.
const SOURCE_SKIP_NAMES: &[&str] = &[".git", "target", ".DS_Store"];

const SOURCE_STAMP_INPUTS: &[&str] = &[
    "Cargo.toml",
    "Cargo.lock",
    "build.rs",
    "rust-toolchain.toml",
    "src",
    "assets",
];

fn source_stamp_for(root: &PathBuf) -> Result<String> {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    for name in SOURCE_STAMP_INPUTS {
        let path = root.join(name);
        let Ok(meta) = std::fs::metadata(&path) else {
            continue;
        };
        hasher.write(name.as_bytes());
        if meta.is_dir() {
            hash_source_dir(root, &path, &mut hasher)?;
        } else {
            hasher.write_u64(meta.len());
            hasher.write(
                &std::fs::read(&path).with_context(|| format!("reading {}", path.display()))?,
            );
        }
    }
    Ok(format!("{:016x}", hasher.finish()))
}

fn hash_source_dir(root: &PathBuf, dir: &PathBuf, hasher: &mut impl Hasher) -> Result<()> {
    let mut entries = std::fs::read_dir(dir)
        .with_context(|| format!("reading {}", dir.display()))?
        .collect::<std::result::Result<Vec<_>, _>>()
        .with_context(|| format!("reading entries in {}", dir.display()))?;
    entries.sort_by_key(|entry| entry.path());
    for entry in entries {
        let path = entry.path();
        let name = entry.file_name();
        let name = name.to_string_lossy();
        if SOURCE_SKIP_NAMES.contains(&name.as_ref()) {
            continue;
        }
        let meta = entry
            .metadata()
            .with_context(|| format!("reading metadata for {}", path.display()))?;
        let rel = path.strip_prefix(root).unwrap_or(&path);
        hasher.write(rel.to_string_lossy().as_bytes());
        if meta.is_dir() {
            hasher.write_u8(b'/');
            hash_source_dir(root, &path, hasher)?;
        } else if meta.is_file() {
            hasher.write_u64(meta.len());
            hasher.write(
                &std::fs::read(&path).with_context(|| format!("reading {}", path.display()))?,
            );
        }
    }
    Ok(())
}

fn shell_quote(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\"'\"'"))
}

#[cfg(test)]
fn ssh_control_socket_path_for_test(dir: &Path) -> PathBuf {
    dir.join("ctl")
}

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

    /// The source sync must ship the BUILD INPUTS and nothing else.
    ///
    /// It used to deny-list `target`, which never matched this repo's actual
    /// build directory, `target.noindex`. Every fallback install therefore
    /// rsynced 176 GB of build artifacts to the user's box, throttled to a
    /// couple of MB/s, to deliver a source tree whose only job was to be
    /// compiled there. Three boxes were 23-27 GB deep in it before anyone
    /// noticed. The stamp learned this lesson already (SOURCE_STAMP_INPUTS);
    /// the rsync did not, so now they share one list and cannot drift apart.
    #[test]
    fn the_source_sync_ships_only_build_inputs() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("checkout");
        let dst = tmp.path().join("staged");
        for dir in [
            "src",
            "assets",
            "target.noindex/debug",
            "target/debug",
            ".git",
        ] {
            std::fs::create_dir_all(src.join(dir)).unwrap();
        }
        std::fs::write(src.join("src/main.rs"), "fn main() {}").unwrap();
        std::fs::write(src.join("assets/logo.png"), "png").unwrap();
        std::fs::write(src.join("Cargo.toml"), "[package]").unwrap();
        std::fs::write(src.join("Cargo.lock"), "lock").unwrap();
        std::fs::write(src.join("build.rs"), "fn main() {}").unwrap();
        std::fs::write(src.join("rust-toolchain.toml"), "[toolchain]").unwrap();
        // The three things that must never cross the wire.
        std::fs::write(src.join("target.noindex/debug/croft"), "HUGE").unwrap();
        std::fs::write(src.join("target/debug/croft"), "HUGE").unwrap();
        std::fs::write(src.join(".git/config"), "gitdir").unwrap();
        // Nested junk: a root-anchored allow-list alone re-admits these,
        // because `/assets/***` beats `--exclude=*`. The stamp walk skips them
        // at every level, so the sync has to as well or the two disagree.
        std::fs::write(src.join("assets/.DS_Store"), "finder").unwrap();
        std::fs::create_dir_all(src.join("src/vendor/target")).unwrap();
        std::fs::write(src.join("src/vendor/target/blob"), "HUGE").unwrap();

        let mut rsync = Command::new("rsync");
        rsync.arg("-a").args(source_sync_filter_args());
        rsync
            .arg(format!("{}/", src.display()))
            .arg(format!("{}/", dst.display()));
        let Ok(status) = rsync.status() else {
            return; // no rsync on this box; the remote path is unusable anyway
        };
        assert!(status.success(), "rsync exited with {status}");

        for shipped in [
            "src/main.rs",
            "assets/logo.png",
            "Cargo.toml",
            "Cargo.lock",
            "build.rs",
            "rust-toolchain.toml",
        ] {
            assert!(dst.join(shipped).exists(), "{shipped} must be shipped");
        }
        for withheld in [
            "target.noindex",
            "target",
            ".git",
            "assets/.DS_Store",
            "src/vendor/target",
        ] {
            assert!(
                !dst.join(withheld).exists(),
                "{withheld} must never be shipped: this is the 176 GB bug"
            );
        }
    }

    /// Stage a remote source dir holding both legitimate content and every
    /// kind of junk a previous croft could have left behind.
    fn staged_remote_source(home: &std::path::Path) -> std::path::PathBuf {
        let source = home.join(".cache/croft/source");
        std::fs::create_dir_all(source.join("target/remote-fast")).unwrap();
        std::fs::create_dir_all(source.join("target.noindex/debug")).unwrap();
        std::fs::create_dir_all(source.join(".cargo")).unwrap();
        std::fs::write(source.join(".cargo/config.toml"), "[build]\njobs = 1\n").unwrap();
        std::fs::write(source.join("build.rs"), "fn main() {}").unwrap();
        std::fs::create_dir_all(source.join("src")).unwrap();
        std::fs::write(source.join("src/main.rs"), "fn main() {}").unwrap();
        std::fs::write(source.join("Cargo.toml"), "[package]\n").unwrap();
        source
    }

    fn assert_prep_outcome(source: &std::path::Path) {
        assert!(
            source.join("target/remote-fast").is_dir(),
            "removing the remote's own target/ would force a from-scratch rebuild every install"
        );
        assert!(
            source.join("src/main.rs").is_file() && source.join("Cargo.toml").is_file(),
            "inputs present in the local checkout must survive: deleting them re-ships \
             everything and yanks the tree out from under a concurrent cargo install"
        );
        assert!(
            !source.join("target.noindex").exists(),
            "the artifact tree a previous croft shipped must be reclaimed"
        );
        assert!(
            !source.join(".cargo").exists(),
            "a stale dotfile like .cargo/config.toml would silently reconfigure every later remote build"
        );
        assert!(
            !source.join("build.rs").exists(),
            "an input no longer in the local checkout must not survive on the remote (tar never deletes)"
        );
    }

    /// rsync will not clean up what a previous croft already shipped: the
    /// allow-list's `--exclude=*` PROTECTS every unlisted path from
    /// `--delete`, and the tar fallback never deletes anything. So the prep
    /// must clear the remote source dir of everything except `target/` (the
    /// incremental build cache) and the inputs the local checkout actually
    /// contains - shipped artifact trees, a stale `.cargo/config.toml` that
    /// would silently reconfigure every later remote build, and inputs since
    /// removed from the checkout all go. Current inputs stay: deleting them
    /// would re-ship the whole tree every sync and race a concurrent
    /// `cargo install` reading it. The test runs the real command against a
    /// staged HOME.
    #[test]
    fn the_remote_dir_prep_evicts_stale_entries_but_keeps_cache_and_inputs() {
        let checkout = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(checkout.path().join("src")).unwrap();
        std::fs::write(checkout.path().join("Cargo.toml"), "[package]\n").unwrap();
        // No build.rs in this checkout: the staged remote copy is stale.
        let prep = remote_source_dir_prep(checkout.path());

        let tmp = tempfile::tempdir().unwrap();
        let source = staged_remote_source(tmp.path());
        let status = std::process::Command::new("sh")
            .arg("-c")
            .arg(&prep)
            .env("HOME", tmp.path())
            .status()
            .unwrap();
        assert!(status.success(), "the prep command must run under plain sh");
        assert_prep_outcome(&source);

        let empty = tempfile::tempdir().unwrap();
        let status = std::process::Command::new("sh")
            .arg("-c")
            .arg(&prep)
            .env("HOME", empty.path())
            .status()
            .unwrap();
        assert!(status.success(), "a first install has no source dir yet");
    }

    /// A relocated cache is ordinary on a small-root VPS: the source dir is a
    /// symlink onto a bigger volume. `find` does not follow a symlinked start
    /// point on its own, silently cleaning nothing with a success exit code,
    /// so the prep must dereference it explicitly.
    #[test]
    fn the_remote_dir_prep_follows_a_symlinked_source_dir() {
        let checkout = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(checkout.path().join("src")).unwrap();
        std::fs::write(checkout.path().join("Cargo.toml"), "[package]\n").unwrap();
        let prep = remote_source_dir_prep(checkout.path());

        let tmp = tempfile::tempdir().unwrap();
        let real = tempfile::tempdir().unwrap();
        let source = staged_remote_source(real.path());
        std::fs::create_dir_all(tmp.path().join(".cache/croft")).unwrap();
        std::os::unix::fs::symlink(
            real.path().join(".cache/croft/source"),
            tmp.path().join(".cache/croft/source"),
        )
        .unwrap();

        let status = std::process::Command::new("sh")
            .arg("-c")
            .arg(&prep)
            .env("HOME", tmp.path())
            .status()
            .unwrap();
        assert!(status.success(), "the prep command must run under plain sh");
        assert_prep_outcome(&source);
    }

    /// The tar fallback (no rsync on either side) shipped `.` under the same
    /// broken `--exclude=target`, so it packed the whole artifact tree into a
    /// gzip pipe - worse than the rsync path, and reached on exactly the same
    /// first-install flow. It names its members from the allow-list now.
    #[test]
    fn the_tar_fallback_packs_only_build_inputs() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("checkout");
        std::fs::create_dir_all(src.join("src")).unwrap();
        std::fs::create_dir_all(src.join("target.noindex")).unwrap();
        std::fs::write(src.join("src/main.rs"), "fn main() {}").unwrap();
        std::fs::write(src.join("Cargo.toml"), "[package]").unwrap();
        std::fs::write(src.join("target.noindex/blob"), "HUGE").unwrap();

        let members = source_sync_tar_members(&src);
        assert!(members.contains(&"src"), "src is a build input");
        assert!(
            members.contains(&"Cargo.toml"),
            "Cargo.toml is a build input"
        );
        assert!(
            !members.iter().any(|m| m.starts_with("target")),
            "the artifact tree must never be packed"
        );
        assert!(
            !members.contains(&"build.rs"),
            "a member that does not exist must not be named, or tar fails"
        );
    }

    /// A rustup target belongs to ONE toolchain. Bumping `rust-toolchain.toml`
    /// therefore orphans every cross target, and `croft <host>` degrades from
    /// "ship a 34 MB prebuilt binary" to "compile the whole crate graph on the
    /// user's VPS" with nothing but a line in `~/.cache/croft/install.log` to
    /// say so. That is exactly what the 1.95.0 -> 1.97.1 bump did, unnoticed
    /// for four days. This test is the thing that notices.
    #[test]
    fn the_pinned_toolchain_has_every_cross_target() {
        // Only machines that have actually set up the cross fast path are held
        // to it. A fresh clone, a CI lint runner, Termux, a Nix shell with no
        // zig: none of them ship remote installs, and failing their `cargo
        // test` over a target they will never use is noise, not a signal. The
        // skip is announced, because a guard that vanishes silently is how the
        // original guard managed to be useless.
        if let Some(reason) = cross_compile_unavailable_reason() {
            eprintln!("SKIP the_pinned_toolchain_has_every_cross_target: {reason}");
            return;
        }
        let Ok(out) = cross_tool_command_in_checkout("rustup")
            .args(["target", "list", "--installed"])
            .output()
        else {
            eprintln!("SKIP the_pinned_toolchain_has_every_cross_target: rustup would not run");
            return;
        };
        if !out.status.success() {
            eprintln!("SKIP the_pinned_toolchain_has_every_cross_target: rustup errored");
            return;
        }
        let text = String::from_utf8_lossy(&out.stdout);
        let installed: Vec<&str> = text.lines().map(str::trim).collect();
        let missing: Vec<&str> = crate::cli::CROSS_TARGETS
            .iter()
            .copied()
            .filter(|t| !installed.contains(t))
            .collect();
        assert!(
            missing.is_empty(),
            "the pinned toolchain is missing {missing:?}, so every remote \
             update will compile croft on the remote box instead of shipping \
             a prebuilt binary. Fix from inside this checkout, so the \
             rust-toolchain.toml override applies:\n    rustup target add {}",
            missing.join(" ")
        );
    }

    /// The target query and the build must ask the SAME toolchain.
    /// `cargo zigbuild` runs in the checkout, where `rust-toolchain.toml` pins
    /// the channel; a rustup query with no working directory answers for the
    /// DEFAULT channel instead. When those differ the guard reports "target
    /// installed" and the build dies with E0463, which is why a missing target
    /// went unreported for four days.
    ///
    /// The plain probes must NOT inherit that directory: their answer does not
    /// depend on the toolchain, and running `cargo --version` inside the
    /// checkout makes an availability check auto-install the pinned channel.
    #[test]
    fn only_toolchain_sensitive_calls_run_in_the_checkout() {
        let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        assert_eq!(
            cross_tool_command_in_checkout("rustup").get_current_dir(),
            Some(source.as_path()),
            "a target query must resolve the pinned toolchain"
        );
        assert_eq!(
            cross_tool_command("cargo").get_current_dir(),
            None,
            "a presence probe must not trigger a toolchain auto-install"
        );
    }

    #[test]
    fn source_stamp_tracks_build_inputs_only() {
        // The stamp decides whether a remote reinstall is needed, so it must
        // cover exactly the inputs that shape the shipped binary. Build
        // artifacts (`target.noindex` reached 98 GB) and docs must not feed
        // it: hashing them made every connect stall for a minute and reship
        // croft even when the source was untouched.
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().to_path_buf();
        std::fs::create_dir_all(root.join("src")).unwrap();
        std::fs::create_dir_all(root.join("target.noindex")).unwrap();
        std::fs::create_dir_all(root.join("docs")).unwrap();
        std::fs::write(root.join("Cargo.toml"), "[package]").unwrap();
        std::fs::write(root.join("src/main.rs"), "fn main() {}").unwrap();
        std::fs::write(root.join("target.noindex/dep.rlib"), "artifact-v1").unwrap();
        std::fs::write(root.join("docs/NOTES.md"), "notes-v1").unwrap();
        let base = source_stamp_for(&root).unwrap();

        std::fs::write(root.join("target.noindex/dep.rlib"), "artifact-v2").unwrap();
        std::fs::write(root.join("docs/NOTES.md"), "notes-v2").unwrap();
        assert_eq!(
            source_stamp_for(&root).unwrap(),
            base,
            "build artifacts and docs must not change the source stamp"
        );

        std::fs::write(root.join("src/main.rs"), "fn main() { changed(); }").unwrap();
        assert_ne!(
            source_stamp_for(&root).unwrap(),
            base,
            "a source edit must change the stamp"
        );
    }

    #[test]
    fn drop_to_local_exit_code_maps_to_return_to_local() {
        assert_eq!(
            classify_remote_status(Some(DROP_TO_LOCAL_EXIT_CODE)),
            RemoteStatusClass::ReturnToLocal
        );
        assert_eq!(classify_remote_status(Some(0)), RemoteStatusClass::Exited);
        assert_eq!(
            classify_remote_status(Some(127)),
            RemoteStatusClass::NotInstalled
        );
        assert_eq!(classify_remote_status(Some(1)), RemoteStatusClass::Failed);
        assert_eq!(classify_remote_status(None), RemoteStatusClass::Failed);
    }

    #[test]
    fn parse_ssh_config_extracts_explicit_hosts() {
        let targets = parse_ssh_config(
            r#"
Host *
  User nobody

Host genesis-cloud github.com
  HostName 10.0.0.2
  User ubuntu

Host !blocked *.internal
  User root
"#,
        );
        assert_eq!(
            targets,
            vec![
                RemoteTarget {
                    alias: String::from("genesis-cloud"),
                    host_name: Some(String::from("10.0.0.2")),
                    user: Some(String::from("ubuntu")),
                },
                RemoteTarget {
                    alias: String::from("github.com"),
                    host_name: Some(String::from("10.0.0.2")),
                    user: Some(String::from("ubuntu")),
                },
            ]
        );
    }

    #[test]
    fn remote_croft_command_quotes_paths() {
        // A workspace path with a single quote must be shell-quoted in the
        // croft invocation (which now appears in both the dtach and the
        // direct-exec branch).
        let command = remote_croft_command_for_terminal(
            Some("/tmp/it's here"),
            None,
            None,
            false,
            &[],
            false,
        );
        assert!(command.contains("croft '/tmp/it'\"'\"'s here'"));
        assert!(command.starts_with("export CROFT_REMOTE_AUTOUPDATE=1;"));
    }

    #[test]
    fn remote_croft_command_forwards_supported_terminal_program() {
        let command =
            remote_croft_command_for_terminal(None, Some("iTerm.app"), None, false, &[], false);
        assert!(command.contains("export CROFT_FORCE_INLINE_IMAGES=1 TERM_PROGRAM='iTerm.app';"));
    }

    #[test]
    fn remote_croft_command_exports_kitty_hint_for_ghostty() {
        // Ghostty: TERM_PROGRAM=ghostty locally, but SSH does not forward it, so
        // croft must export the hint itself. It must NOT force the OSC-1337 path
        // (Ghostty parses but ignores it); the remote resolves the Kitty
        // protocol from the exported TERM_PROGRAM.
        let command =
            remote_croft_command_for_terminal(None, Some("ghostty"), None, false, &[], false);
        assert!(command.contains("export TERM_PROGRAM=ghostty;"));
        assert!(!command.contains("CROFT_FORCE_INLINE_IMAGES"));
    }

    #[test]
    fn remote_croft_command_exports_kitty_hint_from_term_only() {
        // A bare `kitty` terminal sets no TERM_PROGRAM; detection keys off TERM.
        let command =
            remote_croft_command_for_terminal(None, None, Some("xterm-kitty"), false, &[], false);
        assert!(command.contains("export TERM_PROGRAM=ghostty;"));
    }

    #[test]
    fn remote_launch_carries_only_a_deterministic_relay_key() {
        // The relay rendezvous reaches the long-lived remote croft as the
        // deterministic `CROFT_RELAY_KEY` env var, NOT the old per-connection
        // `CROFT_DROP_RELAY_*` paths. Because the key is `hash(launch arg)` it is
        // identical on every reconnect, so dtach freezing it at first launch can
        // never desync the running croft from a fresh pump.
        let id = relay_session_id("");
        let env = vec![(String::from("CROFT_RELAY_KEY"), id.clone())];
        let command = remote_croft_command_for_terminal(None, None, None, false, &env, false);
        assert!(
            command.contains(&format!("export CROFT_RELAY_KEY='{id}'")),
            "launch must export the deterministic relay key, got: {command}",
        );
        assert!(
            !command.contains("CROFT_DROP_RELAY"),
            "launch must not use the old per-connection relay env, got: {command}",
        );
    }

    #[test]
    fn relay_session_id_matches_dtach_socket_and_is_deterministic() {
        // The relay id is keyed on the launch arg, exactly like the dtach socket,
        // so `relay-<id>` and `sessions/<id>.sock` share one id and the two sides
        // always agree across reconnects. It must be deterministic (same arg ->
        // same id) and arg-sensitive; the random per-connection id it replaced
        // silently broke every drop after the first reconnect.
        assert_eq!(relay_session_id(""), relay_session_id(""));
        assert_ne!(relay_session_id("/srv/app"), relay_session_id("/srv/other"));
        // Same hash the dtach socket path embeds for the same launch arg.
        assert!(dtach_socket_path(None).contains(&relay_session_id("")));
        assert!(dtach_socket_path(Some("/srv/app")).contains(&relay_session_id("/srv/app")));
    }

    #[test]
    fn cross_tool_search_dirs_cover_gui_launch_tool_locations() {
        // launchd hands a Croft.app / Ghostty launch a stripped PATH; the
        // toolchain lives in dirs that PATH omits, so the search must add them.
        let launchd_path = OsString::from("/usr/bin:/bin:/usr/sbin:/sbin");
        let home = PathBuf::from("/Users/example");
        let dirs =
            cross_tool_search_dirs_from(Some(launchd_path.as_os_str()), Some(home.as_os_str()));

        assert_eq!(dirs.first(), Some(&PathBuf::from("/usr/bin")));
        assert!(
            dirs.contains(&home.join(".cargo").join("bin")),
            "cargo, rustup, and cargo-zigbuild installed by rustup live here, but launchd omits it"
        );
        assert!(
            dirs.contains(&PathBuf::from("/opt/homebrew/bin")),
            "Apple Silicon Homebrew's zig lives here, but launchd omits it"
        );
        assert!(dirs.contains(&PathBuf::from("/usr/local/bin")));
    }

    #[test]
    fn cross_tool_lookup_finds_home_cargo_bin_outside_inherited_path() {
        use std::os::unix::fs::PermissionsExt;

        let tmp = tempfile::tempdir().unwrap();
        let bin_dir = tmp.path().join(".cargo").join("bin");
        std::fs::create_dir_all(&bin_dir).unwrap();
        let tool = bin_dir.join("cargo-zigbuild");
        std::fs::write(&tool, "#!/bin/sh\nexit 0\n").unwrap();
        std::fs::set_permissions(&tool, std::fs::Permissions::from_mode(0o755)).unwrap();

        let launchd_path = OsString::from("/usr/bin:/bin:/usr/sbin:/sbin");
        let dirs = cross_tool_search_dirs_from(
            Some(launchd_path.as_os_str()),
            Some(tmp.path().as_os_str()),
        );

        assert_eq!(
            find_executable_in_dirs("cargo-zigbuild", &dirs),
            Some(tool),
            "the remote fast path must work when Croft.app starts with launchd's stripped PATH"
        );
    }

    #[test]
    fn remote_croft_command_forwards_osk_flag_when_local_osk_is_armed() {
        // SSH from Termux does not forward TERMUX_VERSION, so a remote croft
        // would never auto-arm the on-screen keyboard; the launcher carries
        // the local detection across the hop explicitly.
        let command = remote_croft_command_for_terminal(None, None, None, true, &[], false);
        assert!(command.contains("export CROFT_FORCE_OSK=1;"));
        let command = remote_croft_command_for_terminal(None, None, None, false, &[], false);
        assert!(!command.contains("CROFT_FORCE_OSK"));
    }

    #[test]
    fn remote_command_prefers_session_host_and_falls_back_to_dtach() {
        let command =
            remote_croft_command_for_terminal(Some("/srv/app"), None, None, false, &[], false);
        // Preferred: croft's own session host (multiplayer mux), gated on a
        // probe so an old remote binary is never sent an unknown subcommand.
        assert!(command.contains("if croft session-host --probe"));
        assert!(command.contains("exec croft session-host --socket"));
        assert!(command.contains(".mux.sock"));
        assert!(command.contains("--workspace '/srv/app' -- croft '/srv/app'"));
        // Fallback: dtach with the exact flags croft needs (attach-or-create,
        // no detach/suspend key theft, WINCH redraw on reattach).
        assert!(command.contains("elif command -v dtach"));
        assert!(command.contains("dtach -A"));
        assert!(command.contains("-E"));
        assert!(command.contains("-z"));
        assert!(command.contains("-r winch"));
        // Both persistent branches flag the session for the status line.
        assert!(command.contains("export CROFT_SESSION_PERSISTENT=1;"));
        // Hosts with neither supervisor still launch croft directly.
        assert!(command.contains("else exec croft"));
    }

    #[test]
    fn remote_command_omits_workspace_flag_without_a_path() {
        let command = remote_croft_command_for_terminal(None, None, None, false, &[], false);
        assert!(command.contains("exec croft session-host --socket"));
        assert!(!command.contains("--workspace"));
        assert!(command.contains(" -- croft;"));
    }

    #[test]
    fn dtach_socket_is_stable_per_workspace_and_differs_across_paths() {
        let a1 = dtach_socket_path(Some("/srv/app"));
        let a2 = dtach_socket_path(Some("/srv/app"));
        let b = dtach_socket_path(Some("/srv/other"));
        assert_eq!(a1, a2, "same workspace must map to the same dtach session");
        assert_ne!(a1, b, "different workspaces must not share a session");
        assert!(a1.contains("/.cache/croft/sessions/"));
        // The mux socket shares the keying but never the name, so a mux
        // client can never connect to a live legacy dtach server.
        let m = mux_socket_path(Some("/srv/app"));
        assert_ne!(m, a1);
        assert!(m.ends_with(".mux.sock"));
        assert!(m.contains(&relay_session_id("/srv/app")));
        // The collab socket (Phase D op relay) shares the keying too but is
        // its own endpoint: it never carries PTY bytes.
        let c = collab_socket_path(Some("/srv/app"));
        assert_ne!(c, a1);
        assert_ne!(c, m);
        assert!(c.ends_with(".collab.sock"));
        assert!(c.contains(&relay_session_id("/srv/app")));
    }

    #[test]
    fn remote_command_solo_skips_the_mux_and_wires_the_collab_relay() {
        let command = remote_croft_command_for_terminal(
            Some("/srv/app"),
            Some("iTerm.app"),
            Some("xterm-256color"),
            false,
            &[],
            true,
        );
        // A solo guest never attaches the shared PTY.
        assert!(!command.contains("session-host --socket"));
        assert!(!command.contains("dtach -A"));
        // It probes for collab support (old remote binaries fall back to a
        // plain croft), ensures the relay, and exports the channel env.
        assert!(command.contains("if croft collab-relay --probe"));
        assert!(command.contains("collab-relay --ensure --socket"));
        assert!(command.contains(&collab_socket_path(Some("/srv/app"))));
        assert!(command.contains("export CROFT_COLLAB_SOCKET="));
        assert!(command.contains("export CROFT_COLLAB_ROLE=guest;"));
        assert!(command.contains("exec croft '/srv/app'"));
        // Non-solo stays on the shared-session path, untouched.
        let shared = remote_croft_command_for_terminal(
            Some("/srv/app"),
            Some("iTerm.app"),
            Some("xterm-256color"),
            false,
            &[],
            false,
        );
        assert!(shared.contains("if croft session-host --probe"));
        assert!(!shared.contains("CROFT_COLLAB_ROLE"));
    }

    #[test]
    fn transport_failure_is_exit_255_only() {
        // 255 is ssh's own connection-died code; everything else is a real
        // remote exit and must not trigger an auto-reconnect.
        assert!(is_transport_failure(Some(255)));
        assert!(!is_transport_failure(Some(0)));
        assert!(!is_transport_failure(Some(1)));
        assert!(!is_transport_failure(Some(101)));
        assert!(!is_transport_failure(None));
    }

    #[test]
    fn parse_relay_request_pulls_have_id_and_path() {
        match super::parse_relay_request("pull\tabc-123\t/Users/v/foo.txt") {
            Some(super::RelayRequest::Pull { id, src }) => {
                assert_eq!(id, "abc-123");
                assert_eq!(src, "/Users/v/foo.txt");
            }
            other => panic!("expected Pull, got {:?}", other.is_some()),
        }
    }

    #[test]
    fn activate_command_writes_stamp_then_clears_updating_marker() {
        let command = super::activate_command("deadbeef");
        assert!(command.contains("mv \"$HOME/.cargo/bin/croft.new\" \"$HOME/.cargo/bin/croft\""));
        let stamp_at = command
            .find("printf %s 'deadbeef' > \"$HOME/.cache/croft/install-stamp\"")
            .expect("stamp write present");
        let clear_at = command
            .find("rm -f \"$HOME/.cache/croft/updating\"")
            .expect("marker clear present");
        assert!(
            stamp_at < clear_at,
            "stamp must be written before marker clear"
        );
    }

    #[test]
    fn parse_relay_request_open_extracts_id_and_url() {
        match super::parse_relay_request("open\topen-1\thttps://example.com/x") {
            Some(super::RelayRequest::Open { id, url }) => {
                assert_eq!(id, "open-1");
                assert_eq!(url, "https://example.com/x");
            }
            _ => panic!("expected Open variant"),
        }
    }

    #[test]
    fn url_is_safe_to_open_accepts_http_https_mailto() {
        assert!(super::url_is_safe_to_open("https://example.com/path"));
        assert!(super::url_is_safe_to_open("http://example.com"));
        assert!(super::url_is_safe_to_open("HTTPS://Example.COM"));
        assert!(super::url_is_safe_to_open("mailto:foo@example.com"));
    }

    #[test]
    fn url_is_safe_to_open_rejects_other_schemes_and_control_chars() {
        assert!(!super::url_is_safe_to_open("file:///etc/passwd"));
        assert!(!super::url_is_safe_to_open("javascript:alert(1)"));
        assert!(!super::url_is_safe_to_open("ssh://attacker"));
        assert!(!super::url_is_safe_to_open("https://example.com\nrm -rf /"));
        assert!(!super::url_is_safe_to_open("https://example.com\r\nx"));
        assert!(!super::url_is_safe_to_open("plaintext"));
    }

    #[test]
    fn parse_relay_request_clipboard_requires_id_only() {
        match super::parse_relay_request("clipboard\tclip-7") {
            Some(super::RelayRequest::Clipboard { id }) => assert_eq!(id, "clip-7"),
            _ => panic!("expected Clipboard variant"),
        }
    }

    #[test]
    fn parse_relay_request_rejects_malformed_lines() {
        assert!(super::parse_relay_request("ping\tabc\t/x").is_none());
        assert!(super::parse_relay_request("pull\t\t/x").is_none());
        assert!(super::parse_relay_request("pull\tabc\t").is_none());
        assert!(super::parse_relay_request("clipboard\t").is_none());
    }

    #[test]
    fn parse_relay_request_forward_extracts_port_and_open_flag() {
        match super::parse_relay_request("forward\tfwd-1\t3000\t1") {
            Some(super::RelayRequest::Forward { id, port, open }) => {
                assert_eq!(id, "fwd-1");
                assert_eq!(port, "3000");
                assert!(open);
            }
            _ => panic!("expected Forward variant"),
        }
    }

    #[test]
    fn parse_relay_request_forward_defaults_open_to_false() {
        match super::parse_relay_request("forward\tfwd-2\t8080") {
            Some(super::RelayRequest::Forward { open, .. }) => assert!(!open),
            _ => panic!("expected Forward variant"),
        }
        assert!(super::parse_relay_request("forward\tfwd-3\t").is_none());
    }

    #[test]
    fn pick_local_port_mirrors_a_free_port() {
        // Port 0 can never be mirrored (it's the "any" sentinel), but a high
        // port is almost certainly free in the test environment.
        let p = super::pick_local_port(54321);
        assert!(p == 54321 || p >= 1024, "got {p}");
    }

    #[test]
    fn remote_install_command_installs_from_staged_source() {
        let command = remote_install_command("abc123");
        assert!(command.contains("cargo install --path \"$HOME/.cache/croft/source\""));
        assert!(command.contains("rustup.rs"));
        assert!(command.contains("printf %s 'abc123' > \"$HOME/.cache/croft/install-stamp\""));
    }

    // The 2026-06-12 "horrendous typing latency on various" report: the
    // fast cross-build silently fell back to `cargo install` ON the remote
    // box, and even niced, a rustc compile on a small VPS wrecks the live
    // session sharing it. When a croft session is running on the box, the
    // compile must yield everything: one job and idle-class IO.
    #[test]
    fn remote_install_compile_yields_to_a_live_croft_session() {
        let command = remote_install_command("abc123");
        assert!(
            command.contains("pgrep -x croft"),
            "must detect a live croft session on the box"
        );
        assert!(
            command.contains("CROFT_JOBS=1"),
            "a live session drops the compile to a single job"
        );
        assert!(
            command.contains("ionice") && command.contains("-c3"),
            "codegen writes must run in the idle IO class so the session's PTY never waits on them"
        );
        let gate = command.find("pgrep -x croft").unwrap();
        let install = command.find("cargo install --path").unwrap();
        assert!(gate < install, "the session check must precede the compile");
    }

    // A backgrounded install's log lines died with the connect dialog,
    // which is why the various fallback went undiagnosed. Every line must
    // also land in a persistent local file.
    #[test]
    fn install_log_tee_writes_lines_to_file_and_forwards_them_unchanged() {
        let dir = std::env::temp_dir().join(format!("croft-log-tee-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("install.log");
        let (down_tx, down_rx) = std::sync::mpsc::channel::<String>();
        let tee = spawn_log_tee(path.clone(), down_tx);
        tee.send(String::from("Cross-compiling croft locally"))
            .unwrap();
        tee.send(String::from("Local cross-build skipped (zig missing)"))
            .unwrap();
        let timeout = std::time::Duration::from_secs(2);
        assert_eq!(
            down_rx.recv_timeout(timeout).unwrap(),
            "Cross-compiling croft locally",
            "downstream consumers must still receive every line"
        );
        assert_eq!(
            down_rx.recv_timeout(timeout).unwrap(),
            "Local cross-build skipped (zig missing)"
        );
        drop(tee);
        let deadline = std::time::Instant::now() + timeout;
        let mut contents = String::new();
        while std::time::Instant::now() < deadline {
            contents = std::fs::read_to_string(&path).unwrap_or_default();
            if contents.contains("zig missing") {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(50));
        }
        assert!(contents.contains("Cross-compiling croft locally"));
        assert!(
            contents.contains("Local cross-build skipped (zig missing)"),
            "the skip REASON is the whole point of the persistent log"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn remote_install_command_ensures_c_toolchain_unconditionally() {
        // Regression: cargo can be present on a box that still lacks `cc`, so a
        // bare `cargo install` dies with `linker `cc` not found`. The toolchain
        // ensure must run on its own, not only when cargo+curl are both absent,
        // and it must precede the compile.
        let command = remote_install_command("abc123");
        assert!(command.contains("croft_ensure_build_toolchain"));
        assert!(command.contains("build-essential"));
        let ensure_call = command
            .rfind("\ncroft_ensure_build_toolchain")
            .expect("toolchain ensure must be invoked");
        let cargo_install = command
            .find("cargo install --path")
            .expect("cargo install must be present");
        assert!(
            ensure_call < cargo_install,
            "the C toolchain must be ensured before `cargo install` runs"
        );
    }

    #[test]
    fn arch_to_musl_triple_maps_every_arch_uname_reports_for_supported_targets() {
        assert_eq!(
            arch_to_musl_triple("x86_64"),
            Some("x86_64-unknown-linux-musl")
        );
        assert_eq!(
            arch_to_musl_triple("amd64"),
            Some("x86_64-unknown-linux-musl"),
            "BSD-style `uname -m` reports amd64 for the same machine class linux reports as x86_64; the fast-install path must accept both"
        );
        assert_eq!(
            arch_to_musl_triple("aarch64"),
            Some("aarch64-unknown-linux-musl")
        );
        assert_eq!(
            arch_to_musl_triple("arm64"),
            Some("aarch64-unknown-linux-musl"),
            "Apple Silicon Linux VMs report arm64; same triple as aarch64"
        );
    }

    #[test]
    fn arch_to_musl_triple_returns_none_for_unsupported_archs_so_caller_falls_back() {
        for arch in ["i686", "armv7l", "ppc64le", "riscv64", "mips", ""] {
            assert_eq!(
                arch_to_musl_triple(arch),
                None,
                "{arch} has no statically-known musl target in croft's bundled toolchain; caller must fall back to remote cargo install"
            );
        }
    }

    #[test]
    fn remote_install_check_uses_cargo_path_before_probe() {
        let command = remote_install_check_command();
        let path_pos = command
            .find("export PATH=\"$HOME/.cargo/bin:$PATH\"")
            .unwrap();
        let probe_pos = command.find("command -v croft").unwrap();
        assert!(command.contains(". \"$HOME/.cargo/env\""));
        assert!(path_pos < probe_pos);
        assert!(command.contains("cat \"$HOME/.cache/croft/install-stamp\""));
    }

    #[test]
    fn shell_quote_for_e_arg_passes_alnum_paths_through() {
        assert_eq!(
            shell_quote_for_e_arg(Path::new("/tmp/croft-ssh-1234/ctl")),
            "/tmp/croft-ssh-1234/ctl",
        );
    }

    #[test]
    fn shell_quote_for_e_arg_quotes_paths_with_spaces() {
        let q = shell_quote_for_e_arg(Path::new("/Users/v a/croft/ctl"));
        assert_eq!(q, "'/Users/v a/croft/ctl'");
    }

    #[test]
    fn shell_quote_for_e_arg_escapes_single_quotes_inside_path() {
        let q = shell_quote_for_e_arg(Path::new("/tmp/it's/ctl"));
        // /tmp/it's → /tmp/it'\''s after the standard sh single-quote
        // escaping trick (close, escaped quote, reopen).
        assert_eq!(q, "'/tmp/it'\\''s/ctl'");
    }

    #[test]
    fn ssh_control_socket_path_is_short_and_inside_temp_dir() {
        let dir = std::env::temp_dir().join("croft-ssh-test");
        let socket = ssh_control_socket_path_for_test(&dir);
        assert!(socket.starts_with(&dir));
        assert_eq!(socket.file_name().and_then(|s| s.to_str()), Some("ctl"));
    }

    fn args_of(cmd: &Command) -> Vec<String> {
        cmd.get_args()
            .map(|a| a.to_string_lossy().into_owned())
            .collect()
    }

    // The other half of the launch-now bug class. Bytes are not the only
    // shared resource: ssh forwards the caller's stdin to the remote command
    // unless `-n` is given, and on this path that stdin is the terminal the
    // attached remote croft is reading. Two processes then read() the same
    // tty and each keystroke reaches exactly one of them, so typing into the
    // remote session silently vanishes for as long as the background command
    // runs. Verified against a real host: `ssh h 'sleep 1' < probe` drains
    // all 16 bytes even though the remote command never reads stdin, while
    // `ssh -n h 'sleep 1' < probe` leaves every byte for the next reader.
    #[test]
    fn background_ssh_never_reads_the_attached_sessions_stdin() {
        let socket = PathBuf::from("/tmp/croft-ctl/ctl");
        let background = args_of(&ssh_socket_command(&socket, true));
        assert!(
            background.iter().any(|a| a == "-n"),
            "a background command sharing the session's tty steals its keystrokes"
        );
        let interactive = args_of(&ssh_socket_command(&socket, false));
        assert!(
            !interactive.iter().any(|a| a == "-n"),
            "the interactive session is the one process that must keep the tty"
        );
    }

    // The launch-now bug class: a background install shipping bytes through
    // the SAME multiplexed TCP connection as the live remote session queues
    // megabytes ahead of every keystroke (SSH head-of-line blocking) and
    // makes the running croft unusable. Every bulk rsync must honor the
    // bulk lane's routing and pacing.
    #[test]
    fn binary_ship_never_rides_the_interactive_master_when_lane_is_dedicated() {
        let lane = crate::remote_bulk::BulkLane::new(
            crate::remote_bulk::LaneMode::Dedicated {
                socket_path: PathBuf::from("/tmp/bulk/ctl"),
            },
            512,
        );
        let cmd = ship_binary_rsync_command(
            &lane,
            Path::new("/tmp/interactive/ctl"),
            Path::new("/tmp/target/croft"),
            "host:.cargo/bin/croft.new",
        );
        let args = args_of(&cmd);
        let e = args
            .iter()
            .position(|a| a == "-e")
            .map(|i| args[i + 1].clone())
            .expect("rsync must use an explicit -e remote shell");
        assert!(e.contains("/tmp/bulk/ctl"));
        assert!(!e.contains("/tmp/interactive/ctl"));
        assert!(
            args.iter().any(|a| a == "--bwlimit=512"),
            "unpaced bulk saturates the uplink queue and lags the session even on its own connection"
        );
    }

    #[test]
    fn binary_ship_throttles_on_the_shared_mux_when_no_lane_is_available() {
        let lane = crate::remote_bulk::BulkLane::new(crate::remote_bulk::LaneMode::SharedMux, 300);
        let cmd = ship_binary_rsync_command(
            &lane,
            Path::new("/tmp/interactive/ctl"),
            Path::new("/tmp/target/croft"),
            "host:.cargo/bin/croft.new",
        );
        let args = args_of(&cmd);
        let e = args
            .iter()
            .position(|a| a == "-e")
            .map(|i| args[i + 1].clone())
            .expect("rsync must use an explicit -e remote shell");
        assert!(e.contains("/tmp/interactive/ctl"));
        assert!(args.iter().any(|a| a == "--bwlimit=300"));
        assert!(args.iter().any(|a| a == "--checksum"));
    }

    /// The lane wiring, plus the allow-list that replaced the deny-list.
    ///
    /// The previous version of this test asserted `--exclude=target` under the
    /// comment "shipping target/ would be gigabytes through the lane". It
    /// passed on every run while 176 GB of `target.noindex` went over the wire,
    /// because it checked the RULE and never the OUTCOME. The outcome is now
    /// pinned by `the_source_sync_ships_only_build_inputs`, which runs rsync
    /// for real; this one only guarantees the allow-list reaches the command.
    #[test]
    fn source_sync_honors_the_bulk_lane_and_allow_lists_the_build_inputs() {
        let lane = crate::remote_bulk::BulkLane::new(
            crate::remote_bulk::LaneMode::Dedicated {
                socket_path: PathBuf::from("/tmp/bulk/ctl"),
            },
            700,
        );
        let cmd = source_sync_rsync_command(
            &lane,
            Path::new("/tmp/interactive/ctl"),
            std::ffi::OsStr::new("/Users/v/croft/"),
            "host:.cache/croft/source/",
        );
        let args = args_of(&cmd);
        let e = args
            .iter()
            .position(|a| a == "-e")
            .map(|i| args[i + 1].clone())
            .expect("rsync must use an explicit -e remote shell");
        assert!(e.contains("/tmp/bulk/ctl"));
        assert!(!e.contains("/tmp/interactive/ctl"));
        assert!(args.iter().any(|a| a == "--bwlimit=700"));
        for name in SOURCE_STAMP_INPUTS {
            assert!(
                args.iter().any(|a| a == &format!("--include=/{name}")),
                "{name} is a build input and must be shipped"
            );
        }
        assert!(
            args.iter().any(|a| a == "--exclude=*"),
            "the allow-list is only an allow-list if everything else is denied"
        );
        for name in SOURCE_SKIP_NAMES {
            assert!(
                args.iter().any(|a| a == &format!("--exclude={name}")),
                "{name} is skipped by the stamp walk, so it must not be shipped"
            );
        }
        assert!(args.iter().any(|a| a == "--delete"));
    }

    // The 2026-06-12 root cause on various: croft inherited macOS's default
    // 256-fd soft limit, the zig linker needed ~250 rlibs open at once and
    // died with ProcessFdQuotaExceeded, and the installer silently fell
    // back to compiling on the remote box under the live session.
    #[test]
    fn raise_fd_limit_lifts_the_soft_limit_beyond_the_linkers_needs() {
        let soft = raise_fd_limit();
        assert!(
            soft >= 4096,
            "croft's cross-link opens ~250 rlibs plus zig's own files; a {soft}-fd soft limit reproduces ProcessFdQuotaExceeded"
        );
    }

    // Guards the exact bug class `sync_workspace_lock` exists to fix: a version
    // bump in Cargo.toml that leaves Cargo.lock one patch behind makes the
    // `--locked` remote cross-build fail and silently host-compile. A drifted
    // lock should never reach a commit, so fail the suite if the two disagree.
    #[test]
    fn cargo_lock_croft_version_matches_cargo_toml_so_locked_cross_build_never_falls_back() {
        let root = Path::new(env!("CARGO_MANIFEST_DIR"));
        let toml = std::fs::read_to_string(root.join("Cargo.toml")).unwrap();
        let toml_version = toml
            .lines()
            .find_map(|l| {
                l.strip_prefix("version = \"")
                    .and_then(|r| r.strip_suffix('"'))
            })
            .expect("Cargo.toml has a package version");

        let lock = std::fs::read_to_string(root.join("Cargo.lock")).unwrap();
        let lock_version = lock
            .split("name = \"croft-software\"")
            .nth(1)
            .and_then(|after| {
                after.lines().find_map(|l| {
                    l.trim()
                        .strip_prefix("version = \"")
                        .and_then(|r| r.strip_suffix('"'))
                })
            })
            .expect("Cargo.lock has a croft entry");

        assert_eq!(
            lock_version, toml_version,
            "Cargo.lock croft version {lock_version} drifted from Cargo.toml {toml_version}; run `cargo update -p croft-software`"
        );
    }

    // 2026-08-22: a croft installed from `git#de1a7ab7` hashed its frozen
    // ~/.cargo checkout, said "already up to date", and silently shipped
    // nothing while the fix sat in the working repo. The snapshot warning
    // exists so that no-op announces itself.
    #[test]
    fn snapshot_dirs_are_detected_and_real_repos_are_not() {
        assert!(source_dir_is_snapshot(
            "/Users/u/.cargo/registry/src/index.crates.io-6f17d22bba15001f/croft-software-0.1.757"
        ));
        assert!(source_dir_is_snapshot(
            "/home/u/.cargo/git/checkouts/croft-9a1f/de1a7ab"
        ));
        assert!(source_dir_is_snapshot(
            "/custom/cargo-home/.cargo/git/checkouts/croft-9a1f/de1a7ab"
        ));
        // CARGO_HOME=/custom/cargo-home has no `.cargo` component at all
        assert!(source_dir_is_snapshot(
            "/custom/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/croft-software-0.1.757"
        ));
        assert!(source_dir_is_snapshot(
            "/custom/cargo-home/git/checkouts/croft-9a1f/de1a7ab"
        ));
        assert!(!source_dir_is_snapshot("/Users/u/Documents/croft"));
        assert!(!source_dir_is_snapshot("/home/u/work/croft"));
        assert!(
            source_snapshot_warning().is_none(),
            "the test build itself must come from a working tree, not a snapshot"
        );
    }
}