ignition-core 1.6.8

Core library for ign: config, profiles, gateway client, actions, error taxonomy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
//! The ssh read half: [`SftpTransport`] reads a Linux gateway host's
//! `data/config/resources/<collection>/...` trees and the declared-names
//! ledger over system `sftp` (22-06).
//!
//! ## The protocol cannot mistake a failure for an absence
//!
//! One sftp batch aborts on its first failing command, and a `-` prefix on a
//! command would hide that failure. So no batch line ever starts with `-`
//! (the typed [`SftpOp`] cannot render one), and a path appears in a batch
//! only after an EARLIER session's listing showed it. Every session lists one
//! tree level of every directory that level needs, at once:
//!
//! 1. `data_dir`, then `config`, then `config/resources`: three sessions that
//!    prove each level is a real directory (not a symlink).
//! 2. The collections (`core`, the mode, `local`), then their modules, then
//!    the scoped types (and for tags the four tag types, then the declared
//!    providers): one session per level.
//! 3. Inside a scoped directory, one session per depth lists every directory
//!    of that depth with `ls -lan` (`ls -la` on a Windows host), so every
//!    file, its size, and any symlink or special file is known BEFORE a byte
//!    is downloaded.
//! 4. Download sessions fetch each listed file with its own `get` (never
//!    `get -R`, whose contents the server would decide) into a private temp
//!    dir, in chunks; the running total is checked before each chunk, the
//!    temp dir is measured while sftp runs and sftp is killed if it outgrows
//!    what the listing allowed, and afterwards the local walk must find
//!    exactly the listed files with exactly the listed sizes.
//!
//! On a Windows OpenSSH host step 4 is one archive instead (quick
//! 261105-dl1): the exact list goes up, one PowerShell call zips exactly
//! those files into a fresh `.ign-ctl-*` directory below the account home,
//! one `get` downloads it and the same session removes it, and the archive is
//! extracted under the hostile-archive rules of [`bundle`] before the same
//! final check. One `get` per file cost about 0.37 s there, about 88 minutes
//! for a production tag tree.
//!
//! On a Windows OpenSSH host steps 1 to 3 are not sftp listings (quick
//! 261104-s3n): that server drops paths of 260 characters or more from a
//! listing, lists a junction as a directory and costs about 0.8 s per folder,
//! so one PowerShell listing of the folders on the path and one PowerShell
//! walk of the requested trees, a fixed number of ssh calls whatever the tree
//! size, list everything a read downloads (see `discover_windows`).
//!
//! Any non-zero sftp exit, any stderr byte or a timeout is `transport_failed`
//! (see [`RemoteShell::require_success`]); a directory that is permission
//! denied, vanishes between sessions, or whose download is missing locally is
//! `transport_failed`, never an absent or empty resource. Only a name missing
//! from its parent's listing reads as absent.
//!
//! ## Untrusted input
//!
//! The host controls every listing line. A POSIX listing line is accepted
//! only when it carries the exact listed-directory prefix (a Windows line is
//! the server's bare longname), a recognised mode column and a numeric size;
//! the output is split on `\n` alone, so a `\r` inside a name stays in it.
//! A name that is empty, holds `/`, or fails
//! [`safe_remote_path`] (control characters, quotes, backslash, glob
//! characters) is refused. A name that passed those checks is the only
//! remote-derived text that ever reaches a later batch line, always inside the
//! double quotes of a typed op. Symlinks and special files are refused (the
//! sftp client's `get -R` would otherwise skip a symlink silently), the three
//! `READ_MAX_*` caps are enforced from the listing before the download and
//! again on the downloaded bytes (entries while the listing is parsed), and
//! the depth of the walk is bounded by
//! [`READ_MAX_DEPTH`]. Entries are counted while a listing is parsed, so an
//! oversized listing stops at the line that passes the cap. File hashes are computed here on the downloaded bytes,
//! never by a remote tool, and the downloads live in a per-read temp dir that
//! is deleted on drop.

use std::collections::{BTreeMap, BTreeSet};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};

mod apply_ops;
mod project_ops;

use super::bundle::{self, BundleFile};
use super::remote::{RemoteShell, SftpOp, safe_remote_path};
use super::winps::{
    self, LONG_PATH_LIMIT, LONG_PATH_PREFIX, windows_absolute_path_problem,
    windows_component_problem,
};
use super::{
    CORE_COLLECTION, DeleteSet, FileBlob, MirrorReport, ObservedFiles, READ_MAX_ENTRIES,
    READ_MAX_FILE_BYTES, READ_MAX_TOTAL_BYTES, Scope, Staged, StagingMode, TreeEntry, TreeListing,
    TreeMirror, TreeReader, Unreadable, absolute_components, copy_to_temp, mirror_mode_problem,
    read_staged_copy, validate_mirror_inputs, validate_mirror_paths, validate_staging_root_for,
};
use crate::client::config_resources::validate_segment;
use crate::deploy::apply::journal::JOURNAL_MAX_BYTES;
use crate::deploy::component_problem;
use crate::deploy::layout::{RelPath, TAG_TYPES};
use crate::deploy::ledger::{
    LEDGER_MAX_BYTES, LedgerRoot, journal_location, journal_rel, ledger_location, ledger_rel,
};
use crate::deploy::plan::unit_key;
use crate::deploy::tree::{OpenError, RenderedTree, open_regular_file, sha256_hex, walk_capped};
use crate::error::CoreError;

/// The collection that overrides the mode collection on the gateway.
const LOCAL_COLLECTION: &str = "local";

/// Path components from `data_dir` to the collections.
const RESOURCES: [&str; 2] = ["config", "resources"];

/// A walk deeper than this many components below `data_dir` is refused. Each
/// level costs one sftp session, so a hostile deep tree cannot drive an
/// unbounded number of them.
pub const READ_MAX_DEPTH: usize = 64;

/// One download session carries at most this many listed bytes. The total cap
/// is checked before each chunk, so a tree near [`READ_MAX_TOTAL_BYTES`] is
/// fetched in several sessions, each verified before the next starts.
const DOWNLOAD_CHUNK_BYTES: u64 = 256 * 1024 * 1024;
/// One download session carries at most this many `get` lines.
const DOWNLOAD_CHUNK_FILES: usize = 10_000;
/// How far past the listed bytes the download dir may grow before the sftp
/// process is killed (a server may legitimately differ by a rounding error,
/// not by this much).
const DOWNLOAD_SLACK_BYTES: u64 = 1024 * 1024;
/// How often the download dir is measured while sftp runs.
const WATCH_INTERVAL: std::time::Duration = std::time::Duration::from_millis(50);

/// What kind of filesystem object a listing line describes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntryKind {
    /// A regular file (`-`).
    File,
    /// A directory (`d`).
    Dir,
    /// A symbolic link (`l`).
    Symlink,
    /// Anything else (device, socket, fifo, unknown).
    Other,
}

/// One entry of a directory listing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListedEntry {
    /// The entry's name: one path component, already checked by
    /// [`safe_remote_path`].
    pub name: String,
    /// What the listing says it is.
    pub kind: EntryKind,
    /// The size column (a directory's size is not meaningful).
    pub size: u64,
}

/// The POSIX data directory of an `ssh` node, ready to be used as a path
/// prefix: absolute, checked by [`safe_remote_path`], free of empty, `.` and
/// `..` components, with the trailing `/` trimmed.
pub fn posix_root(data_dir: &str) -> Result<String, String> {
    if !data_dir.starts_with('/') {
        return Err(format!(
            "{data_dir:?} is not an absolute POSIX path (it must start with `/`)"
        ));
    }
    safe_remote_path(data_dir)?;
    let trimmed = data_dir.trim_end_matches('/');
    if trimmed.is_empty() {
        return Err("the filesystem root cannot be a gateway data directory".to_string());
    }
    if trimmed
        .split('/')
        .skip(1)
        .any(|part| part.is_empty() || part == "." || part == "..")
    {
        return Err(format!(
            "{data_dir:?} must not contain empty, `.` or `..` components"
        ));
    }
    Ok(trimmed.to_string())
}

/// The longest Windows `data_dir` accepted: the sftp-server resolves no path
/// of 260 characters (C7a-8), and `config/resources/<collection>/<module>/<type>`
/// below the data directory must always resolve, because a directory listing
/// that cannot see its own children cannot be told from an empty one.
pub const WINDOWS_DATA_DIR_MAX: usize = 150;

/// The sftp spelling of the Windows `data_dir` of an `ssh` node (C7a-1):
/// `/` + the drive letter, upper-cased, + `:` + the path with forward slashes
/// (`C:\Program Files\Inductive Automation\Ignition\data` becomes
/// `/C:/Program Files/Inductive Automation/Ignition/data`). `C:/...` without
/// the leading slash is relative to the account's home on this server, and a
/// backslash after the drive is not found, so neither is ever produced.
///
/// Refused: UNC and `\\?\` paths, relative and drive-relative paths (`C:foo`),
/// anything not starting with a drive letter and a separator, empty, `.` and
/// `..` components, a `:` after the drive (an alternate data stream), a
/// component ending in a dot or a space (Windows strips them, so two names
/// would mean one directory), a path over [`WINDOWS_DATA_DIR_MAX`], the bare
/// drive root, and whatever [`safe_remote_path`] refuses (quotes, control and
/// glob characters).
pub fn windows_sftp_root(data_dir: &str) -> Result<String, String> {
    let normal = data_dir.replace('\\', "/");
    if normal.starts_with("//") {
        return Err(format!(
            "{data_dir:?} is a UNC or extended-length path; use a drive letter path such as C:\\Program Files\\Inductive Automation\\Ignition\\data"
        ));
    }
    let bytes = normal.as_bytes();
    let drive_form =
        bytes.len() >= 3 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':' && bytes[2] == b'/';
    if !drive_form {
        return Err(format!(
            "{data_dir:?} is not an absolute Windows path (it must start with a drive letter, a colon and a separator, e.g. C:\\Program Files\\...)"
        ));
    }
    let drive = (bytes[0] as char).to_ascii_uppercase();
    let rest = normal[3..].trim_end_matches('/');
    if rest.is_empty() {
        return Err("a drive root cannot be a gateway data directory".to_string());
    }
    for part in rest.split('/') {
        if part.is_empty() || part == "." || part == ".." {
            return Err(format!(
                "{data_dir:?} must not contain empty, `.` or `..` components"
            ));
        }
        if part.contains(':') {
            return Err(format!(
                "{data_dir:?} has a `:` after the drive letter (an alternate data stream name)"
            ));
        }
        if part.ends_with('.') || part.ends_with(' ') {
            return Err(format!(
                "{data_dir:?} has a component ending in a dot or a space, which Windows strips"
            ));
        }
    }
    let root = format!("/{drive}:/{rest}");
    if root.chars().count() > WINDOWS_DATA_DIR_MAX {
        return Err(format!(
            "{data_dir:?} is longer than {WINDOWS_DATA_DIR_MAX} characters; the sftp server cannot resolve paths of 260 characters or more"
        ));
    }
    safe_remote_path(&root)?;
    Ok(root)
}

/// The native Windows spelling (`C:\a b\c`) of an sftp path made by
/// [`windows_sftp_root`] (`/C:/a b/c`), for the PowerShell enumeration.
fn windows_native_path(sftp_path: &str) -> String {
    sftp_path.trim_start_matches('/').replace('/', "\\")
}

/// Parse the stdout of one `@pwd`, `@ls -lan "<dir>"` batch for the single
/// POSIX directory `dir`. See [`parse_batch_listing`].
pub fn parse_ls_listing(dir: &str, stdout: &[u8]) -> Result<Vec<ListedEntry>, String> {
    let mut all = parse_batch_listing(&[dir.to_string()], stdout)?;
    Ok(all.pop().unwrap_or_default())
}

/// Why a listing was refused.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ListingError {
    /// More entries than the budget allows. Raised while the output is being
    /// parsed, so nothing after the offending line is looked at or stored.
    TooMany,
    /// A line, name or mode that cannot be trusted.
    Bad(String),
}

impl std::fmt::Display for ListingError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ListingError::TooMany => write!(f, "more than {READ_MAX_ENTRIES} entries"),
            ListingError::Bad(why) => f.write_str(why),
        }
    }
}

/// [`parse_batch_listing_capped`] with the whole [`READ_MAX_ENTRIES`] budget.
pub fn parse_batch_listing(
    dirs: &[String],
    stdout: &[u8],
) -> Result<Vec<Vec<ListedEntry>>, String> {
    parse_batch_listing_capped(dirs, stdout, READ_MAX_ENTRIES).map_err(|e| e.to_string())
}

/// The host family a long listing comes from. In both families an `@pwd`
/// marker leads every listing, so a line is attributed to its directory by
/// the marker.
///
/// The OpenSSH client prints `\n`, `\r` and `\t` inside a name raw
/// (openssh-portable `utf8.c`), so a POSIX name holding `\n` reaches the
/// parser as two lines. For `ls -la` the client prints the server's bare
/// `longname` unless the server offers `users-groups-by-id@openssh.com` (the
/// macOS `sftp-server` does, Ubuntu's OpenSSH 8.9 does not), and a bare line
/// is byte-identical to the tail of a name that held a `\n`: no parser rule
/// can tell them apart. So a POSIX host is listed with `ls -lan`, which makes
/// every OpenSSH client format every line itself with the listed path in
/// front of the name, and a POSIX line without that prefix is refused (a
/// name fragment cannot hold `/`). This reverses the 23-22 (G1) choice of
/// `ls -la` with a bare fallback; the captured bare 8.9 listing is now refused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListFormat {
    /// A POSIX host (Linux, macOS), listed with `ls -lan`: every entry line,
    /// `.` and `..` included, carries the exact `<dir>/` prefix. Names are
    /// ordinary POSIX names.
    Posix,
    /// A Windows host (Win32-OpenSSH), listed with `ls -la`: the server's
    /// bare names (a line holding the `<dir>/` prefix is read the same way as
    /// a POSIX one). A Win32 name cannot hold `\n` or `\r`; a name also passes
    /// the Windows component gate and no two names of a directory differ only
    /// by case. Reads are checked against a PowerShell walk as well.
    Windows,
}

/// What a listing does with a name that cannot be placed in a later batch line.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NameRule {
    /// The whole listing is refused. Used wherever every child of the
    /// directory is followed or read, so a skipped name would be an
    /// incomplete read.
    Refuse,
    /// The entry is left out. Used for directories where only fixed, safe
    /// names are ever looked up (an account home holds
    /// `NTUSER.DAT{<guid>}.TM.blf`, whose braces are glob characters).
    Skip,
}

/// The line `@pwd` prints before its path.
const PWD_MARKER: &str = "Remote working directory: ";

/// [`parse_batch_listing_as`] for a POSIX host.
pub fn parse_batch_listing_capped(
    dirs: &[String],
    stdout: &[u8],
    max_entries: usize,
) -> Result<Vec<Vec<ListedEntry>>, ListingError> {
    parse_batch_listing_as(
        ListFormat::Posix,
        NameRule::Refuse,
        dirs,
        stdout,
        max_entries,
    )
}

/// Parse the stdout of a batch that listed `dirs` in order, each listing led
/// by an `@pwd` marker, into one entry list per directory (byte-sorted by
/// name).
///
/// The output is split on `\n` alone, so a `\r` inside a name stays in the
/// name. One trailing `\r` is stripped from each line only when EVERY line,
/// the markers included, ends in `\r\n` (the Win32-OpenSSH client writes
/// CRLF, the other OpenSSH clients LF); in a listing whose line ends are
/// mixed every `\r` stays, so the names that hold one are refused.
///
/// A `Remote working directory:` line starts the next directory; the number
/// of markers must equal the number of directories. Every other line is `<mode>
/// <links> <owner> <group> <size> <month> <day> <time-or-year> <name>`. Its
/// name is `<dir>/<name>` (the client formatted the line; the exact prefix of
/// the CURRENT directory is found and ONE component is taken after it). For
/// [`ListFormat::Posix`] that is the only shape and a line without the prefix
/// is refused. For [`ListFormat::Windows`] a line without it is the server's
/// bare longname: the name is everything after the eighth column and the
/// single space that follows it, so a name with spaces survives, holding no
/// `/`. The day and time-or-year columns must read as a date, so a line whose
/// owner or group holds a space is refused rather than read under a wrong
/// name. A line before the first marker, an empty line, a line with an empty
/// or multi-component name, with an unrecognised mode or a non-numeric size,
/// a name that fails [`safe_remote_path`] (a client that did not run in a
/// UTF-8 locale prints `caf\303\251`, whose backslash is refused here), or a
/// name listed twice is an error. `.` and `..` are dropped. For
/// [`ListFormat::Windows`] a name also passes the Windows component gate and
/// no two names differ only by case.
///
/// Entries are counted AS EACH LINE IS PARSED: the line that makes the count
/// pass `max_entries` stops the parse with [`ListingError::TooMany`] before
/// it is stored and before any later line is parsed, so a huge or endless
/// listing is never materialised (the line-end check before the parse stores
/// nothing). The 32 MiB stdout cap of the process runner is a backstop, not
/// this check.
pub fn parse_batch_listing_as(
    format: ListFormat,
    names: NameRule,
    dirs: &[String],
    stdout: &[u8],
    max_entries: usize,
) -> Result<Vec<Vec<ListedEntry>>, ListingError> {
    let bad = |why: String| ListingError::Bad(why);
    let text = std::str::from_utf8(stdout)
        .map_err(|_| bad("the listing holds a name that is not valid UTF-8".to_string()))?;
    let prefixes: Vec<String> = dirs.iter().map(|d| format!("{d}/")).collect();
    let mut out: Vec<Vec<ListedEntry>> = vec![Vec::new(); dirs.len()];
    // `.` and `..` of every directory are lines too, and so is a marker
    let max_lines = max_entries.saturating_add(dirs.len().saturating_mul(3));
    let mut lines_seen = 0usize;
    let mut entries_seen = 0usize;
    let mut current = 0usize;
    let mut markers = 0usize;
    for line in output_lines(text) {
        lines_seen += 1;
        if lines_seen > max_lines {
            return Err(ListingError::TooMany);
        }
        if line.is_empty() {
            return Err(bad("the listing holds an empty line".to_string()));
        }
        if line.starts_with(PWD_MARKER) {
            markers += 1;
            if markers > dirs.len() {
                return Err(bad(
                    "the listing holds more directory markers than directories".to_string(),
                ));
            }
            current = markers - 1;
            continue;
        }
        if markers == 0 {
            return Err(bad(format!(
                "a listing line comes before the first directory marker: {line:?}"
            )));
        }
        let (columns, name) =
            line_columns_and_name(format, line, &prefixes[current]).map_err(bad)?;
        if name.contains('/') {
            return Err(bad(format!(
                "a listing line holds a multi-component name: {line:?}"
            )));
        }
        if name != "." && name != ".." {
            entries_seen += 1;
            if entries_seen > max_entries {
                return Err(ListingError::TooMany);
            }
        }
        if columns.len() < 8 {
            return Err(bad(format!("a listing line has too few columns: {line:?}")));
        }
        let mode = columns[0];
        if mode.len() < 10 || !mode.is_ascii() {
            return Err(bad(format!("a listing line has an unusable mode {mode:?}")));
        }
        let kind = match mode.as_bytes()[0] {
            b'-' => EntryKind::File,
            b'd' => EntryKind::Dir,
            b'l' => EntryKind::Symlink,
            _ => EntryKind::Other,
        };
        // month, day and time follow the size: it is the fourth from last
        let size_text = columns[columns.len() - 4];
        let size: u64 = size_text.parse().map_err(|_| {
            bad(format!(
                "a listing line has a non-numeric size {size_text:?}"
            ))
        })?;
        // A bare line is cut after its eighth column, so an owner or group
        // with a space shifts every column: the date columns then read
        // something else and the line is refused, never misread.
        if !reads_as_date(columns[columns.len() - 2], columns[columns.len() - 1]) {
            return Err(bad(format!(
                "a listing line does not carry a date after its size: {line:?}"
            )));
        }
        if name == "." || name == ".." {
            continue;
        }
        if name.is_empty() {
            return Err(bad("a listing line has an empty name".to_string()));
        }
        let problem = safe_remote_path(name).err().or_else(|| {
            // a Windows host's names also pass the Windows component gate
            (format == ListFormat::Windows)
                .then(|| windows_component_problem(name))
                .flatten()
        });
        if let Some(why) = problem {
            if names == NameRule::Skip {
                continue;
            }
            return Err(bad(format!("the name {name:?} is unsafe: {why}")));
        }
        out[current].push(ListedEntry {
            name: name.to_string(),
            kind,
            size,
        });
    }
    if markers != dirs.len() {
        return Err(bad(format!(
            "the listing holds {markers} directory markers for {} directories",
            dirs.len()
        )));
    }
    for entries in &mut out {
        entries.sort_by(|a, b| a.name.as_bytes().cmp(b.name.as_bytes()));
        if let Some(pair) = entries.windows(2).find(|w| w[0].name == w[1].name) {
            return Err(bad(format!("the name {:?} is listed twice", pair[0].name)));
        }
        if format == ListFormat::Windows {
            // NTFS is case-insensitive: two names that differ only by case
            // cannot be two entries of one directory
            let mut folded: BTreeSet<String> = BTreeSet::new();
            if let Some(dup) = entries
                .iter()
                .find(|e| !folded.insert(e.name.to_lowercase()))
            {
                return Err(bad(format!(
                    "the name {:?} is listed twice, ignoring case",
                    dup.name
                )));
            }
        }
    }
    Ok(out)
}

/// The lines of an sftp client's stdout. The text is split on `\n` alone (a
/// final `\n` ends the last line, it does not start an empty one), so a `\r`
/// inside a name stays in its line; `str::lines` would drop a `\r` before
/// each `\n` and read a name ending in `\r` as another name. One trailing
/// `\r` is stripped from each line only when EVERY line ends in `\r`: the
/// Win32-OpenSSH client ends every line it prints in CRLF (OpenSSH_9.5p2 for
/// Windows, measured on a pipe), the other OpenSSH clients in LF. When the
/// line ends are mixed, every `\r` stays.
fn output_lines(text: &str) -> impl Iterator<Item = &str> {
    let crlf = text.split_terminator('\n').all(|l| l.ends_with('\r'));
    text.split_terminator('\n').map(move |l| {
        if crlf {
            l.strip_suffix('\r').unwrap_or(l)
        } else {
            l
        }
    })
}

/// The columns before the name and the name of one listing line of the
/// directory whose prefix (`<dir>/`) is `prefix`. A line the client formatted
/// holds the exact prefix, preceded by a space, with one component after it.
/// For [`ListFormat::Posix`] (`ls -lan`) that is the only shape: a line
/// without it is refused, since it may be the tail of a name that held a raw
/// `\n`. For [`ListFormat::Windows`] (`ls -la`) a line without it is the
/// server's bare longname and the name is what follows the eighth column.
fn line_columns_and_name<'a>(
    format: ListFormat,
    line: &'a str,
    prefix: &str,
) -> Result<(Vec<&'a str>, &'a str), String> {
    if let Some(at) = line.rfind(prefix) {
        let name = &line[at + prefix.len()..];
        if at > 0 && line.as_bytes()[at - 1] == b' ' && !name.contains('/') {
            return Ok((line[..at].split_whitespace().collect(), name));
        }
    }
    match format {
        ListFormat::Posix => Err(format!(
            "a listing line does not carry the listed directory {prefix:?} and one name after it: {line:?}"
        )),
        ListFormat::Windows => split_columns(line, 8)
            .ok_or_else(|| format!("a listing line has too few columns: {line:?}")),
    }
}

/// True when `day` is 1 to 31 and `tail` is a time (`H:MM`, `HH:MM`) or a
/// four-digit year: the last two columns of an `ls -l` date.
fn reads_as_date(day: &str, tail: &str) -> bool {
    let digits = |t: &str| !t.is_empty() && t.bytes().all(|b| b.is_ascii_digit());
    let day_ok =
        digits(day) && day.len() <= 2 && (1..=31).contains(&day.parse::<u8>().unwrap_or(0));
    let tail_ok = match tail.split_once(':') {
        Some((hour, minute)) => {
            digits(hour) && hour.len() <= 2 && digits(minute) && minute.len() == 2
        }
        None => digits(tail) && tail.len() == 4,
    };
    day_ok && tail_ok
}

/// The first `count` whitespace-separated columns of `line` and the rest of
/// the line after the single space that follows the last of them (so a name
/// that starts or ends with spaces is kept). `None` when the line has fewer
/// columns or no name.
fn split_columns(line: &str, count: usize) -> Option<(Vec<&str>, &str)> {
    let mut columns = Vec::with_capacity(count);
    let mut rest = line.trim_start();
    for _ in 0..count {
        let end = rest.find(char::is_whitespace)?;
        columns.push(&rest[..end]);
        let after = &rest[end..];
        // the separator is one space between the last column and the name,
        // any run of spaces between the earlier columns
        rest = if columns.len() == count {
            after.strip_prefix(' ')?
        } else {
            after.trim_start()
        };
    }
    Some((columns, rest))
}

/// Running totals of one read.
#[derive(Default)]
struct Budget {
    entries: usize,
    /// Leave out names that are unsafe in a batch line instead of refusing
    /// the listing (see [`NameRule::Skip`]).
    lenient: bool,
}

impl Budget {
    /// A budget for directories where only fixed names are looked up.
    fn lenient() -> Budget {
        Budget {
            entries: 0,
            lenient: true,
        }
    }
}

/// Why walking down a fixed path stopped.
enum Descend {
    /// An sftp session failed.
    Failed(CoreError),
    /// A component (display path relative to `data_dir`) is a symlink.
    Symlink(String),
    /// A component (display path relative to `data_dir`) is not a directory.
    NotDir(String),
}

impl From<CoreError> for Descend {
    fn from(err: CoreError) -> Self {
        Descend::Failed(err)
    }
}

/// A file the listing found inside a scoped directory.
#[derive(Debug, Clone)]
struct FoundFile {
    collection: String,
    /// Collection-relative path.
    rel: String,
    size: u64,
}

/// What discovery found.
struct Found {
    /// Scoped directories that exist (chains below `data_dir`).
    roots: Vec<Vec<String>>,
    /// Every file below a root.
    files: Vec<FoundFile>,
    /// Every directory below a root (a root itself is not listed), as chains
    /// below `data_dir`.
    dirs: Vec<Vec<String>>,
}

/// A host entry the Windows walk found at or over [`LONG_PATH_LIMIT`]:
/// listed, never downloaded or read as absent.
#[derive(Debug, Clone)]
struct LongEntry {
    collection: String,
    /// Collection-relative path.
    rel: String,
    /// The host path length (without the `\\?\` prefix).
    length: usize,
    dir: bool,
}

/// Reads a Linux gateway data directory over system `sftp`.
pub struct SftpTransport {
    shell: RemoteShell,
    data_dir: String,
    label: String,
    sessions: AtomicUsize,
    mirror: Option<MirrorSetup>,
    /// A Windows OpenSSH host: listings are the marked format, a read is
    /// listed by PowerShell ([`discover_windows`](Self::discover_windows),
    /// quick 261104-s3n), never by per-folder sftp `ls` (22-13), and its
    /// files arrive as one archive (`bundle_reads`, quick 261105-dl1).
    windows: bool,
    /// The account's home directory as the server spells it, read once with
    /// `pwd`.
    home: std::sync::OnceLock<String>,
    /// The apply half (23-06): the target gateway and mode.
    apply: Option<apply_ops::ApplySetup>,
    /// The work lists `prepare` staged, by the SHA-256 of their bytes, so
    /// `commit` can name the entries a script result points at.
    lists: Mutex<BTreeMap<String, super::batch::WorkList>>,
    /// Numbers the throw-away control directories of this transport.
    ctl_seq: AtomicUsize,
    /// Fetch a read's files as one archive ([`fetch_bundled`](Self::fetch_bundled),
    /// quick 261105-dl1) instead of one `get` per file: true on a Windows
    /// host, where each `get` costs about 0.37 s.
    bundle_reads: bool,
}

fn transport_failed(label: &str, detail: impl std::fmt::Display) -> CoreError {
    CoreError::TransportFailed {
        operation: "read".to_string(),
        node: label.to_string(),
        detail: detail.to_string(),
    }
}

impl SftpTransport {
    /// A reader over `data_dir` (use [`posix_root`] to build it from a
    /// target); `label` (e.g. `nodes.master`) names the node in errors. A
    /// `data_dir` that cannot be placed in a batch line is refused when it
    /// is first used, before any process is spawned.
    pub fn new(shell: RemoteShell, data_dir: String, label: impl Into<String>) -> Self {
        SftpTransport {
            shell,
            data_dir,
            label: label.into(),
            sessions: AtomicUsize::new(0),
            mirror: None,
            windows: false,
            home: std::sync::OnceLock::new(),
            apply: None,
            lists: Mutex::new(BTreeMap::new()),
            ctl_seq: AtomicUsize::new(0),
            bundle_reads: false,
        }
    }

    /// A reader over a Windows OpenSSH host's `data_dir` (use
    /// [`windows_sftp_root`] to build it from a target). Listings use the
    /// marked format (C7a-2), paths compare case-insensitively where it
    /// matters, and a read lists its trees with a PowerShell walk with the
    /// `\\?\` prefix (C7a-8), a fixed number of calls whatever the tree
    /// size. The files the walk listed are fetched as one archive
    /// ([`fetch_bundled`](Self::fetch_bundled), quick 261105-dl1), staged in
    /// a fresh `.ign-ctl-*` directory below the account home and removed
    /// before the read returns: a read needs a readable account home outside
    /// the data directory and free space there for one compressed copy plus
    /// [`bundle::STAGING_FREE_MARGIN`].
    pub fn new_windows(shell: RemoteShell, data_dir: String, label: impl Into<String>) -> Self {
        let mut transport = Self::new(shell, data_dir, label);
        transport.windows = true;
        transport.bundle_reads = true;
        transport
    }

    /// Test only: a Windows reader that fetches with one `get` per file, as
    /// before quick 261105-dl1, for the equivalence test.
    #[cfg(test)]
    fn with_per_file_downloads(mut self) -> Self {
        self.bundle_reads = false;
        self
    }

    /// How many ssh connections this reader has opened so far: sftp
    /// sessions plus the read path's PowerShell calls (each one ssh
    /// handshake), including a Windows read's bundle connections (the list
    /// upload, BUNDLE, the archive download and, after a failure,
    /// BUNDLE_CLEAN).
    pub fn sessions(&self) -> usize {
        self.sessions.load(Ordering::Relaxed)
    }

    fn fail(&self, detail: impl std::fmt::Display) -> CoreError {
        transport_failed(&self.label, detail)
    }

    /// The absolute remote path of `chain` below `data_dir`.
    fn abs(&self, chain: &[String]) -> String {
        let mut path = self.data_dir.clone();
        for part in chain {
            path.push('/');
            path.push_str(part);
        }
        path
    }

    /// One sftp session: list every directory of `dirs` (`ls -lan` on a POSIX
    /// host, `ls -la` on a Windows host).
    ///
    /// Every parsed entry counts against `budget`, which is shared by all
    /// listings of one read, so [`READ_MAX_ENTRIES`] is cumulative across
    /// directories, levels and collections.
    async fn list_level(
        &self,
        dirs: &[String],
        budget: &mut Budget,
    ) -> Result<Vec<Vec<ListedEntry>>, CoreError> {
        self.list_level_as("read", dirs, budget).await
    }

    /// [`list_level`](Self::list_level) reporting failures as operation `op`.
    async fn list_level_as(
        &self,
        op: &str,
        dirs: &[String],
        budget: &mut Budget,
    ) -> Result<Vec<Vec<ListedEntry>>, CoreError> {
        if dirs.is_empty() {
            return Ok(Vec::new());
        }
        // Each listing is led by a `pwd` marker that says whose lines follow
        // (C7a-2). A Windows host is listed with `ls -la` (the server's bare
        // names; a Win32 name holds no line break). A POSIX host is listed
        // with `ls -lan`, so the client formats every line with the listed
        // path in front of the name and a name split by a raw `\n` is refused
        // (see [`ListFormat`]).
        let mut ops: Vec<SftpOp> = Vec::with_capacity(dirs.len() * 2);
        for path in dirs {
            ops.push(SftpOp::Pwd);
            let path = path.clone();
            ops.push(if self.windows {
                SftpOp::LsLongAll { path }
            } else {
                SftpOp::LsLongAllNumeric { path }
            });
        }
        self.sessions.fetch_add(1, Ordering::Relaxed);
        let out = self.shell.sftp_batch(&ops).await?;
        let out = self.shell.require_success(op, out)?;
        let remaining = READ_MAX_ENTRIES.saturating_sub(budget.entries);
        let fail = |detail: String| CoreError::TransportFailed {
            operation: op.to_string(),
            node: self.label.clone(),
            detail,
        };
        let format = if self.windows {
            ListFormat::Windows
        } else {
            ListFormat::Posix
        };
        let names = if budget.lenient {
            NameRule::Skip
        } else {
            NameRule::Refuse
        };
        let listings = parse_batch_listing_as(format, names, dirs, &out.stdout, remaining)
            .map_err(|e| match e {
                ListingError::TooMany => fail(format!(
                    "more than {READ_MAX_ENTRIES} entries under the data directory (at {})",
                    self.shown(&dirs[0])
                )),
                ListingError::Bad(why) => fail(format!("cannot use the sftp listing: {why}")),
            })?;
        budget.entries += listings.iter().map(Vec::len).sum::<usize>();
        Ok(listings)
    }

    /// `abs` relative to `data_dir`, for messages.
    fn shown(&self, abs: &str) -> String {
        abs.strip_prefix(&self.data_dir)
            // only a whole component of data_dir: `/data-evil` is not below `/data`
            .filter(|rest| rest.is_empty() || rest.starts_with('/'))
            .map(|rest| rest.trim_start_matches('/'))
            .filter(|rest| !rest.is_empty())
            .unwrap_or(".")
            .to_string()
    }

    /// Walk the fixed `components` below `data_dir`, one listing session per
    /// level. `Ok(None)` when a component is missing from its parent's
    /// listing; otherwise the absolute path and listing entry of the last
    /// component, whatever its kind (a symlink anywhere is refused).
    async fn descend(
        &self,
        components: &[&str],
        budget: &mut Budget,
    ) -> Result<Option<(String, ListedEntry)>, Descend> {
        self.descend_from(&self.data_dir, components, budget).await
    }

    /// [`descend`](Self::descend) starting at the absolute directory `base`.
    async fn descend_from(
        &self,
        base: &str,
        components: &[&str],
        budget: &mut Budget,
    ) -> Result<Option<(String, ListedEntry)>, Descend> {
        let mut current = base.to_string();
        let mut shown = String::new();
        for (index, component) in components.iter().enumerate() {
            let last = index + 1 == components.len();
            let mut listed = self
                .list_level(std::slice::from_ref(&current), budget)
                .await?;
            let entries = listed.pop().unwrap_or_default();
            let Some(entry) = entries.into_iter().find(|e| e.name == *component) else {
                return Ok(None);
            };
            if !shown.is_empty() {
                shown.push('/');
            }
            shown.push_str(component);
            current = format!("{current}/{component}");
            match entry.kind {
                EntryKind::Symlink => return Err(Descend::Symlink(shown)),
                EntryKind::Dir => {}
                _ if last => {}
                _ => return Err(Descend::NotDir(shown)),
            }
            if last {
                return Ok(Some((current, entry)));
            }
        }
        Ok(None)
    }

    /// The account's home directory as the server spells it (`/C:/Users/<user>`
    /// on Windows OpenSSH, `/home/<user>` on Linux), asked once with `pwd`.
    /// Ledgers and staging live below it (Decision B(i)); the answer is held to
    /// the same rules as any path that reaches a batch line.
    pub async fn account_home(&self) -> Result<String, CoreError> {
        if let Some(home) = self.home.get() {
            return Ok(home.clone());
        }
        self.sessions.fetch_add(1, Ordering::Relaxed);
        let out = self.shell.sftp_batch(&[SftpOp::Pwd]).await?;
        let out = self.shell.require_success("read", out)?;
        let text = std::str::from_utf8(&out.stdout)
            .map_err(|_| self.fail("the account home is not valid UTF-8"))?;
        // the batch is `@pwd` alone, so the output is that one line: any other
        // line is the tail of a home that held a raw `\n`, never ignored
        let mut lines = output_lines(text);
        let (Some(raw), None) = (
            lines.next().and_then(|l| l.strip_prefix(PWD_MARKER)),
            lines.next(),
        ) else {
            return Err(
                self.fail("the server did not report exactly one working directory, on one line")
            );
        };
        let trimmed = raw.trim_end_matches('/');
        let problem = if self.windows {
            windows_absolute_path_problem(trimmed)
        } else if !trimmed.starts_with('/') {
            Some("is not an absolute path".to_string())
        } else {
            safe_remote_path(trimmed).err().or_else(|| {
                trimmed
                    .split('/')
                    .skip(1)
                    .any(|part| part.is_empty() || part == "." || part == "..")
                    .then(|| "has an empty, `.` or `..` component".to_string())
            })
        };
        if let Some(why) = problem {
            return Err(self.fail(format!("the account home {raw:?} cannot be used: {why}")));
        }
        let _ = self.home.set(trimmed.to_string());
        Ok(trimmed.to_string())
    }

    /// The Decision B(i) staging mode: `Separate` at
    /// `<home>/ign-deploy/<gateway>/<mode>/staging/<run_id>`, where `<home>`
    /// is what the server reports for the account ([`account_home`](Self::account_home)).
    /// `gateway`, `mode` and `run_id` are checked by the caller
    /// ([`default_staging_mode`](super::default_staging_mode)).
    pub async fn default_staging(
        &self,
        gateway: &str,
        mode: &str,
        run_id: &str,
    ) -> Result<StagingMode, CoreError> {
        for (what, value) in [("gateway", gateway), ("mode", mode), ("run id", run_id)] {
            self.windows_gate(&format!("the staging {what}"), value)?;
        }
        let home = self.account_home().await?;
        let root = format!(
            "{home}/{}",
            crate::deploy::ledger::staging_rel(gateway, mode, run_id)
        );
        safe_remote_path(&root)
            .map_err(|why| self.fail(format!("the staging root is unsafe: {why}")))?;
        if self.windows
            && let Some(why) = windows_absolute_path_problem(&root)
        {
            return Err(self.fail(format!("the staging root is refused: {why}")));
        }
        Ok(StagingMode::Separate { root })
    }

    /// Windows only (quick 261104-s3n): list what a read of `chains` needs
    /// with a fixed number of PowerShell calls, never one sftp `ls` per
    /// folder. The Windows sftp server costs about 0.8 s per folder listed
    /// plus about 3.5 s per session, so the per-level [`discover`](Self::discover)
    /// took about 100 minutes over a production tag tree of 7,500 folders. It
    /// is also not complete there: it drops every path of 260 characters or
    /// more with exit 0 (C7a-8), and it lists a junction as a plain directory
    /// (C7a-5).
    ///
    /// Step A lists the direct children of every folder on the path to a
    /// requested folder ([`winps::ENUMERATE_CHILDREN`], the data directory
    /// included) in one batched call, and walks the answers top-down from the
    /// data directory exactly as `discover` follows its path: only `config`,
    /// `config/resources` and the components of a requested folder are
    /// followed. A missing data directory, `config` or `config/resources` is
    /// `transport_failed`, never an empty tree; a followed component that is a
    /// reparse point (junction or symlink) or not a directory is refused, and
    /// every listed name must be safe in a batch line (skipped when `budget`
    /// is lenient). Names match exactly, as in `discover`.
    ///
    /// Step B walks every requested folder, found or not, in one batched
    /// [`winps::ENUMERATE`] call with the `\\?\` prefix. A requested folder
    /// step A did not find but the host holds is `transport_failed` naming it
    /// (quick 261104-ava, N2: a requested folder the host holds is never read
    /// as absent); one step A found that the walk does not is "changed while
    /// it was read". Below a found folder every entry is charged once to
    /// `budget` ([`READ_MAX_ENTRIES`], long entries included), a reparse point
    /// is refused, a directory deeper than [`READ_MAX_DEPTH`] is refused, and
    /// two names that are equal, or equal ignoring case, are refused. An
    /// entry whose host path reaches [`LONG_PATH_LIMIT`] (measured as
    /// `full_len - LONG_PATH_PREFIX.len()`; 240 characters, the conservative
    /// limit the reader keeps below the server's) is returned as a
    /// [`LongEntry`] and never put into [`Found`]: `list_tree` lists it, and
    /// `read` reports its files as [`Unreadable`](super::Unreadable) instead
    /// of downloading them. Every shorter entry can reach a `get` line, so
    /// each of its components must pass [`safe_remote_path`] (an unsafe one,
    /// and everything below it, is skipped when `budget` is lenient). A long
    /// entry never reaches a batch line and is held to the Windows component
    /// gate only ([`winps::parse_enumeration`]).
    async fn discover_windows(
        &self,
        chains: &BTreeSet<Vec<String>>,
        budget: &mut Budget,
    ) -> Result<(Found, Vec<LongEntry>), CoreError> {
        // a chain inside another chain is read by the outer chain's walk
        let outer: Vec<&Vec<String>> = chains
            .iter()
            .filter(|c| {
                !chains
                    .iter()
                    .any(|o| o.len() < c.len() && c.starts_with(o.as_slice()))
            })
            .collect();

        // Step A: the folders on the path, `config/resources` always included
        let mut prefixes: BTreeSet<Vec<String>> = (0..=RESOURCES.len())
            .map(|n| RESOURCES[..n].iter().map(|c| (*c).to_string()).collect())
            .collect();
        for chain in &outer {
            for n in 0..chain.len() {
                prefixes.insert(chain[..n].to_vec());
            }
        }
        let prefixes: Vec<Vec<String>> = prefixes.into_iter().collect();
        let natives: Vec<String> = prefixes
            .iter()
            .map(|p| windows_native_path(&self.abs(p)))
            .collect();
        let listed = self.enumerate_children_of(&natives).await?;
        let listing: BTreeMap<&Vec<String>, Option<Vec<winps::EnumEntry>>> =
            prefixes.iter().zip(listed).collect();
        let mut present: BTreeSet<Vec<String>> = BTreeSet::new();
        let mut stack: Vec<Vec<String>> = vec![Vec::new()];
        while let Some(dir) = stack.pop() {
            let shown_dir = if dir.is_empty() {
                ".".to_string()
            } else {
                dir.join("/")
            };
            let entries = match listing.get(&dir) {
                Some(Some(entries)) => entries,
                Some(None) if dir.is_empty() => {
                    return Err(self.fail(
                        "the data directory cannot be read (not found); is data_dir a gateway data directory?",
                    ));
                }
                Some(None) => {
                    return Err(self.fail(format!(
                        "{shown_dir} was listed but the host enumeration does not find it; the tree changed while it was read"
                    )));
                }
                None => {
                    return Err(CoreError::Internal(format!(
                        "the Windows path listing did not ask for {shown_dir}"
                    )));
                }
            };
            // `config` and `config/resources` must exist: a data directory
            // without them is not a gateway data directory, which is a
            // failure and never an empty tree
            let required = RESOURCES.get(dir.len()).copied();
            if let Some(need) = required
                && !entries.iter().any(|e| e.rel == need)
            {
                return Err(self.fail(format!(
                    "{} cannot be read (not found); is data_dir a gateway data directory?",
                    RESOURCES[..=dir.len()].join("/")
                )));
            }
            let mut names: Vec<&str> = Vec::with_capacity(entries.len());
            for entry in entries {
                if let Err(why) = safe_remote_path(&entry.rel) {
                    if budget.lenient {
                        continue;
                    }
                    return Err(self.fail(format!(
                        "cannot use the host listing: the name {:?} is unsafe: {why}",
                        entry.rel
                    )));
                }
                names.push(&entry.rel);
                budget.entries += 1;
                if budget.entries > READ_MAX_ENTRIES {
                    return Err(self.fail(format!(
                        "more than {READ_MAX_ENTRIES} entries under the data directory (at {shown_dir})"
                    )));
                }
                let mut child = dir.clone();
                child.push(entry.rel.clone());
                if required != Some(entry.rel.as_str())
                    && !outer.iter().any(|t| t.starts_with(child.as_slice()))
                {
                    continue;
                }
                let shown = child.join("/");
                if entry.reparse {
                    return Err(self.fail(format!(
                        "{shown} is a junction or symlink (a reparse point); sftp would follow it, so a Windows read refuses it"
                    )));
                }
                if !entry.dir {
                    return Err(self.fail(format!("{shown} is not a directory")));
                }
                if outer.iter().any(|t| **t == child) {
                    present.insert(child);
                } else {
                    stack.push(child);
                }
            }
            self.refuse_twins(&shown_dir, names)?;
        }

        // Step B: one walk of every requested folder, absent ones probed
        let found_roots: Vec<&Vec<String>> = outer
            .iter()
            .copied()
            .filter(|c| present.contains(*c))
            .collect();
        let absent: Vec<&Vec<String>> = outer
            .iter()
            .copied()
            .filter(|c| !present.contains(*c))
            .collect();
        let natives: Vec<String> = found_roots
            .iter()
            .chain(&absent)
            .map(|c| windows_native_path(&self.abs(c)))
            .collect();
        let mut walked = self.enumerate_roots(&natives).await?;
        let probed = walked.split_off(found_roots.len());
        for (chain, entries) in absent.iter().zip(probed) {
            if entries.is_some() {
                return Err(self.fail(format!(
                    "{} exists on the host but the listing did not return it (the folder was made while it was read)",
                    chain.join("/")
                )));
            }
        }
        let mut roots: Vec<Vec<String>> = Vec::new();
        let mut files: Vec<FoundFile> = Vec::new();
        let mut dirs_found: Vec<Vec<String>> = Vec::new();
        let mut long: Vec<LongEntry> = Vec::new();
        for (chain, entries) in found_roots.iter().zip(walked) {
            let shown_root = chain.join("/");
            let Some(entries) = entries else {
                return Err(self.fail(format!(
                    "{shown_root} was listed but the host enumeration does not find it; the tree changed while it was read"
                )));
            };
            roots.push((*chain).clone());
            let (collection, root_rel) = (&chain[2], chain[3..].join("/"));
            let mut skipped: BTreeSet<&str> = BTreeSet::new();
            let mut exact: BTreeSet<&str> = BTreeSet::new();
            let mut folded: BTreeSet<String> = BTreeSet::new();
            for entry in &entries {
                // below a name a lenient read left out
                if !skipped.is_empty()
                    && entry
                        .rel
                        .match_indices('/')
                        .any(|(at, _)| skipped.contains(&entry.rel[..at]))
                {
                    continue;
                }
                let shown = format!("{shown_root}/{}", entry.rel);
                if entry.reparse {
                    return Err(self.fail(format!(
                        "{shown} is a junction or symlink (a reparse point); sftp would follow it, so a Windows read refuses it"
                    )));
                }
                let path_len = entry.full_len.saturating_sub(LONG_PATH_PREFIX.len());
                let is_long = path_len >= LONG_PATH_LIMIT;
                if !is_long
                    && let Some((name, why)) = entry
                        .rel
                        .split('/')
                        .find_map(|c| safe_remote_path(c).err().map(|why| (c, why)))
                {
                    if budget.lenient {
                        skipped.insert(&entry.rel);
                        continue;
                    }
                    return Err(self.fail(format!(
                        "cannot use the host listing: the name {name:?} is unsafe: {why}"
                    )));
                }
                budget.entries += 1;
                if budget.entries > READ_MAX_ENTRIES {
                    return Err(self.fail(format!(
                        "more than {READ_MAX_ENTRIES} entries under the data directory (at {shown_root})"
                    )));
                }
                let name = entry.rel.rsplit('/').next().unwrap_or(&entry.rel);
                if !exact.insert(&entry.rel) {
                    return Err(self.fail(format!(
                        "cannot use the host listing: the name {name:?} is listed twice ({shown})"
                    )));
                }
                if !folded.insert(entry.rel.to_lowercase()) {
                    return Err(self.fail(format!(
                        "cannot use the host listing: the name {name:?} is listed twice, ignoring case ({shown})"
                    )));
                }
                let components = entry.rel.split('/').count();
                if entry.dir && chain.len() + components > READ_MAX_DEPTH {
                    return Err(self.fail(format!(
                        "the tree under config/resources is deeper than {READ_MAX_DEPTH} levels"
                    )));
                }
                if is_long {
                    long.push(LongEntry {
                        collection: collection.clone(),
                        rel: format!("{root_rel}/{}", entry.rel),
                        length: path_len,
                        dir: entry.dir,
                    });
                } else if entry.dir {
                    let mut child = (*chain).clone();
                    child.extend(entry.rel.split('/').map(str::to_string));
                    dirs_found.push(child);
                } else {
                    files.push(FoundFile {
                        collection: collection.clone(),
                        rel: format!("{root_rel}/{}", entry.rel),
                        size: entry.size,
                    });
                }
            }
        }
        // a requested folder inside another one is a root when the outer
        // walk found it as a directory
        let walked_dirs: BTreeSet<&Vec<String>> = dirs_found.iter().collect();
        let nested: Vec<Vec<String>> = chains
            .iter()
            .filter(|c| !outer.contains(c) && walked_dirs.contains(c))
            .cloned()
            .collect();
        roots.extend(nested);
        roots.sort();
        // a root is not a directory "below" itself
        let root_set: BTreeSet<Vec<String>> = roots.iter().cloned().collect();
        dirs_found.retain(|d| !root_set.contains(d));
        dirs_found.sort();
        Ok((
            Found {
                roots,
                files,
                dirs: dirs_found,
            },
            long,
        ))
    }

    /// Two direct children of `shown` whose names are equal, or equal
    /// ignoring case (NTFS cannot hold both), are refused.
    fn refuse_twins(&self, shown: &str, mut names: Vec<&str>) -> Result<(), CoreError> {
        names.sort_unstable();
        if let Some(pair) = names.windows(2).find(|w| w[0] == w[1]) {
            return Err(self.fail(format!(
                "cannot use the host listing: the name {:?} is listed twice (in {shown})",
                pair[0]
            )));
        }
        let mut folded: BTreeSet<String> = BTreeSet::new();
        if let Some(dup) = names.iter().find(|n| !folded.insert(n.to_lowercase())) {
            return Err(self.fail(format!(
                "cannot use the host listing: the name {dup:?} is listed twice, ignoring case (in {shown})"
            )));
        }
        Ok(())
    }

    /// One PowerShell call of the read path, counted in
    /// [`sessions`](Self::sessions): each is one ssh connection.
    async fn read_powershell(
        &self,
        script: &winps::WinScript,
    ) -> Result<serde_json::Value, CoreError> {
        self.sessions.fetch_add(1, Ordering::Relaxed);
        self.shell.exec_powershell(script).await
    }

    /// Windows only: the PowerShell walk of each native root in `roots`, in
    /// order (`None` for a root that does not exist), one call per
    /// [`winps::enumerate_chunks`] chunk. A walk that cannot be run or parsed
    /// is `transport_failed`.
    async fn enumerate_roots(
        &self,
        roots: &[String],
    ) -> Result<Vec<Option<Vec<winps::EnumEntry>>>, CoreError> {
        let chunks = winps::enumerate_chunks(LONG_PATH_PREFIX, roots).map_err(|why| {
            self.fail(format!("cannot check the listing for completeness: {why}"))
        })?;
        let mut enumerated: Vec<Option<Vec<winps::EnumEntry>>> = vec![None; roots.len()];
        for (range, script) in chunks {
            let value = self.read_powershell(&script).await.map_err(|e| match e {
                CoreError::TransportFailed { detail, .. } => self.fail(format!(
                    "cannot check the listing for completeness: {detail}"
                )),
                other => other,
            })?;
            let got = winps::parse_enumeration(&value, range.len()).map_err(|why| {
                self.fail(format!("cannot check the listing for completeness: {why}"))
            })?;
            for (slot, entries) in enumerated[range].iter_mut().zip(got) {
                *slot = entries;
            }
        }
        Ok(enumerated)
    }

    /// Windows only: the direct children of each native directory of `dirs`,
    /// in order ([`winps::ENUMERATE_CHILDREN`], one call per
    /// [`winps::enumerate_children_chunks`] chunk; `None` for a directory that
    /// does not exist). Nothing below a child is read. A listing that cannot
    /// be run or parsed, or that reports a nested entry, is `transport_failed`.
    async fn enumerate_children_of(
        &self,
        dirs: &[String],
    ) -> Result<Vec<Option<Vec<winps::EnumEntry>>>, CoreError> {
        let fail =
            |why: String| self.fail(format!("cannot check the listing for completeness: {why}"));
        let chunks = winps::enumerate_children_chunks(LONG_PATH_PREFIX, dirs).map_err(fail)?;
        let mut listed: Vec<Option<Vec<winps::EnumEntry>>> = vec![None; dirs.len()];
        for (range, script) in chunks {
            let value = self.read_powershell(&script).await.map_err(|e| match e {
                CoreError::TransportFailed { detail, .. } => fail(detail),
                other => other,
            })?;
            let got = winps::parse_enumeration(&value, range.len()).map_err(fail)?;
            for (slot, entries) in listed[range].iter_mut().zip(got) {
                if let Some(nested) = entries.iter().flatten().find(|e| e.rel.contains('/')) {
                    return Err(fail(format!(
                        "the host listed {:?}, which is not a direct child",
                        nested.rel
                    )));
                }
                *slot = entries;
            }
        }
        Ok(listed)
    }

    /// Windows only: the direct children of the native directory `dir`
    /// ([`enumerate_children_of`](Self::enumerate_children_of) of one
    /// directory; `None` when `dir` does not exist).
    async fn enumerate_children(
        &self,
        dir: &str,
    ) -> Result<Option<Vec<winps::EnumEntry>>, CoreError> {
        Ok(self
            .enumerate_children_of(&[dir.to_string()])
            .await?
            .pop()
            .flatten())
    }

    /// Windows only (WR-03, quick-261029-ms3): the directory names sftp
    /// listed directly below `dir` (shown as `shown`) against one PowerShell
    /// listing of its direct children (C6, PR #31 review: never a walk of the
    /// provider subtrees), the names-only form of the completeness rule a
    /// read keeps by listing with [`discover_windows`](Self::discover_windows).
    /// A directory the host holds and the listing omits (the server drops
    /// paths of 260 characters or more, C7a-8) is `transport_failed` naming
    /// it, whatever its length: a names-only listing has no unreadable list,
    /// so an omitted name would otherwise be silently absent. A reparse point
    /// directly below `dir`, and a name the host does not hold, are refused as
    /// a read refuses them. Deeper entries are the read's own cross-check.
    async fn cross_check_names(
        &self,
        dir: &str,
        shown: &str,
        names: &[String],
    ) -> Result<(), CoreError> {
        let Some(entries) = self.enumerate_children(&windows_native_path(dir)).await? else {
            return Err(self.fail(format!(
                "{shown} was listed by sftp but the host enumeration does not find it; the tree changed while it was read"
            )));
        };
        let mut host: BTreeSet<&str> = BTreeSet::new();
        for entry in &entries {
            if entry.reparse {
                return Err(self.fail(format!(
                    "{shown}/{} is a junction or symlink (a reparse point); sftp would follow it, so a Windows read refuses it",
                    entry.rel
                )));
            }
            if entry.dir {
                host.insert(entry.rel.as_str());
            }
        }
        let listed: BTreeSet<&str> = names.iter().map(String::as_str).collect();
        let missing: Vec<String> = host
            .difference(&listed)
            .map(|n| format!("{shown}/{n}"))
            .collect();
        if !missing.is_empty() {
            return Err(self.fail(format!(
                "the host holds entries that the sftp listing did not return (the sftp server drops paths of 260 characters or more): {}",
                named(&missing)
            )));
        }
        let extra: Vec<String> = listed
            .difference(&host)
            .map(|n| format!("{shown}/{n}"))
            .collect();
        if !extra.is_empty() {
            return Err(self.fail(format!(
                "sftp listed entries that the host enumeration does not find; the tree changed while it was read: {}",
                named(&extra)
            )));
        }
        Ok(())
    }

    /// `descend`'s refusal as a read failure.
    fn read_problem(&self, problem: Descend) -> CoreError {
        match problem {
            Descend::Failed(err) => err,
            Descend::Symlink(rel) => self.fail(format!("{rel} is a symlink; symlinks are refused")),
            Descend::NotDir(rel) => self.fail(format!("{rel} is not a directory")),
        }
    }

    /// The directory chains a read of `scope` opens, each starting with
    /// `config/resources/<collection>`.
    /// Whether this is a Windows OpenSSH host (the factory test of
    /// `apply_host_for_node`).
    #[cfg(test)]
    pub(super) fn is_windows_for_tests(&self) -> bool {
        self.windows
    }

    /// The Windows half of the component gate: on a Windows host `name` must
    /// also pass [`windows_component_problem`]. `what` names it in the error.
    fn windows_gate(&self, what: &str, name: &str) -> Result<(), CoreError> {
        match self
            .windows
            .then(|| windows_component_problem(name))
            .flatten()
        {
            Some(why) => Err(self.fail(format!("{what}: {why}"))),
            None => Ok(()),
        }
    }

    fn target_chains(&self, scope: &Scope) -> Result<BTreeSet<Vec<String>>, CoreError> {
        if let Some(why) = component_problem(&scope.mode) {
            return Err(self.fail(format!("the target mode {why}")));
        }
        self.windows_gate("the target mode", &scope.mode)?;
        safe_remote_path(&scope.mode)
            .map_err(|why| self.fail(format!("the target mode is unsafe: {why}")))?;
        let mut dirs: Vec<Vec<String>> = Vec::new();
        for (module, ty) in &scope.types {
            validate_segment("module", module).map_err(|e| self.fail(e))?;
            validate_segment("type", ty).map_err(|e| self.fail(e))?;
            self.windows_gate("the module", module)?;
            self.windows_gate("the type", ty)?;
            dirs.push(vec![module.clone(), ty.clone()]);
        }
        for provider in &scope.tag_providers {
            if let Some(why) = component_problem(provider) {
                return Err(self.fail(format!("tag provider {provider:?} {why}")));
            }
            self.windows_gate("the tag provider", provider)?;
            safe_remote_path(provider)
                .map_err(|why| self.fail(format!("tag provider {provider:?} is unsafe: {why}")))?;
            for tag_type in TAG_TYPES {
                dirs.push(vec![
                    "ignition".to_string(),
                    (*tag_type).to_string(),
                    provider.clone(),
                ]);
            }
        }
        let collections: BTreeSet<&str> = [CORE_COLLECTION, scope.mode.as_str(), LOCAL_COLLECTION]
            .into_iter()
            .collect();
        let mut chains = BTreeSet::new();
        for collection in collections {
            for dir in &dirs {
                let mut chain: Vec<String> = RESOURCES.iter().map(|c| (*c).to_string()).collect();
                chain.push(collection.to_string());
                chain.extend(dir.iter().cloned());
                chains.insert(chain);
            }
        }
        Ok(chains)
    }

    /// List every directory the read needs, level by level, and return the
    /// scoped directories that exist and every file below them.
    async fn discover(
        &self,
        chains: &BTreeSet<Vec<String>>,
        budget: &mut Budget,
    ) -> Result<Found, CoreError> {
        let mut roots = Vec::new();
        let mut files = Vec::new();
        let mut dirs_found: Vec<Vec<String>> = Vec::new();
        let mut frontier: Vec<Vec<String>> = vec![Vec::new()];
        while !frontier.is_empty() {
            let dirs: Vec<String> = frontier.iter().map(|c| self.abs(c)).collect();
            let listings = self.list_level(&dirs, budget).await?;
            let mut next: Vec<Vec<String>> = Vec::new();
            for (chain, entries) in frontier.iter().zip(listings) {
                let inside = chains.iter().any(|t| chain.starts_with(t.as_slice()));
                if chains.contains(chain) {
                    roots.push(chain.clone());
                }
                // `config` and `config/resources` must exist: a data directory
                // without them is not a gateway data directory, which is a
                // failure and never an empty tree.
                let required = RESOURCES.get(chain.len()).copied();
                if let Some(need) = required
                    && !entries.iter().any(|e| e.name == need)
                {
                    return Err(self.fail(format!(
                        "{} cannot be read (not found); is data_dir a gateway data directory?",
                        RESOURCES[..=chain.len()].join("/")
                    )));
                }
                for entry in entries {
                    let mut child = chain.clone();
                    child.push(entry.name.clone());
                    let shown = child.join("/");
                    if inside {
                        match entry.kind {
                            EntryKind::Dir => {
                                dirs_found.push(child.clone());
                                next.push(child);
                            }
                            EntryKind::File => {
                                if let (Some(collection), true) = (chain.get(2), chain.len() >= 3) {
                                    let mut rel = chain[3..].join("/");
                                    if !rel.is_empty() {
                                        rel.push('/');
                                    }
                                    rel.push_str(&entry.name);
                                    files.push(FoundFile {
                                        collection: collection.clone(),
                                        rel,
                                        size: entry.size,
                                    });
                                }
                            }
                            EntryKind::Symlink => {
                                return Err(self
                                    .fail(format!("{shown} is a symlink; symlinks are refused")));
                            }
                            EntryKind::Other => {
                                return Err(self
                                    .fail(format!("{shown} is not a regular file or directory")));
                            }
                        }
                    } else if required == Some(entry.name.as_str())
                        || chains.iter().any(|t| t.starts_with(child.as_slice()))
                    {
                        match entry.kind {
                            EntryKind::Dir => next.push(child),
                            EntryKind::Symlink => {
                                return Err(self
                                    .fail(format!("{shown} is a symlink; symlinks are refused")));
                            }
                            _ => return Err(self.fail(format!("{shown} is not a directory"))),
                        }
                    }
                }
            }
            if next.iter().any(|c| c.len() > READ_MAX_DEPTH) {
                return Err(self.fail(format!(
                    "the tree under config/resources is deeper than {READ_MAX_DEPTH} levels"
                )));
            }
            frontier = next;
        }
        roots.sort();
        // a root is not a directory "below" itself
        dirs_found.retain(|d| !roots.contains(d));
        dirs_found.sort();
        Ok(Found {
            roots,
            files,
            dirs: dirs_found,
        })
    }

    /// Run one download session and kill it if `dir` grows past `limit`
    /// bytes. The listing said how much each `get` may deliver, so a server
    /// that sends more is aborted mid-transfer (the dropped batch future kills
    /// the sftp child) instead of filling the runner's disk; the caller's
    /// temp dir is then removed on drop.
    async fn run_download(
        &self,
        op: &str,
        ops: &[SftpOp],
        dir: &Path,
        limit: u64,
    ) -> Result<(), CoreError> {
        self.sessions.fetch_add(1, Ordering::Relaxed);
        let batch = self.shell.sftp_batch(ops);
        let watch = async {
            loop {
                tokio::time::sleep(WATCH_INTERVAL).await;
                let target = dir.to_path_buf();
                let used = tokio::task::spawn_blocking(move || dir_bytes(&target))
                    .await
                    .unwrap_or(u64::MAX);
                if used > limit {
                    return used;
                }
            }
        };
        tokio::pin!(batch);
        tokio::pin!(watch);
        tokio::select! {
            out = &mut batch => {
                let out = out?;
                self.shell.require_success(op, out)?;
                Ok(())
            }
            used = &mut watch => Err(self.mirror_fail(op, format!(
                "the download wrote {used} bytes, more than the {limit} bytes the listing allowed; it was aborted"
            ))),
        }
    }

    /// Fetch the listed `files` into `tmp` with one `get` per file, in chunks.
    /// Before each chunk the running total (actual bytes already on disk plus
    /// the chunk's listed bytes) must fit [`READ_MAX_TOTAL_BYTES`]; after each
    /// chunk every delivered file must be exactly its listed size.
    async fn fetch_files(&self, files: &[&FoundFile], tmp: &Path) -> Result<(), CoreError> {
        let mut downloaded = 0u64;
        let mut next = 0usize;
        while next < files.len() {
            let start = next;
            let mut ops = Vec::new();
            let mut chunk_listed = 0u64;
            while next < files.len() && ops.len() < DOWNLOAD_CHUNK_FILES {
                let file = files[next];
                if !ops.is_empty() && chunk_listed + file.size > DOWNLOAD_CHUNK_BYTES {
                    break;
                }
                if downloaded + chunk_listed + file.size > READ_MAX_TOTAL_BYTES {
                    return Err(self.fail(format!(
                        "the files under config/resources exceed {READ_MAX_TOTAL_BYTES} bytes in total (at config/resources/{}/{})",
                        file.collection, file.rel
                    )));
                }
                let local = file
                    .rel
                    .split('/')
                    .fold(tmp.join(&file.collection), |p, c| p.join(c));
                if let Some(parent) = local.parent() {
                    std::fs::create_dir_all(parent).map_err(|e| {
                        self.fail(format!("cannot create a download dir ({:?})", e.kind()))
                    })?;
                }
                ops.push(SftpOp::Get {
                    remote: format!(
                        "{}/config/resources/{}/{}",
                        self.data_dir, file.collection, file.rel
                    ),
                    local,
                });
                chunk_listed += file.size;
                next += 1;
            }
            let limit = downloaded + chunk_listed + DOWNLOAD_SLACK_BYTES;
            self.run_download("read", &ops, tmp, limit).await?;
            downloaded += self.check_chunk(&files[start..next], tmp)?;
        }
        Ok(())
    }

    /// Windows only (quick 261105-dl1): fetch the listed `files` into `tmp`
    /// as one archive, a fixed number of connections whatever the count:
    ///
    /// 1. the account home (one `pwd` session unless known); the staging dir
    ///    `<home>/.ign-ctl-<nonce>` is refused when the home is at or below
    ///    the data directory (ASCII case ignored), before anything is written;
    /// 2. one session makes the staging dir and uploads the exact list
    ///    ([`bundle::render_list`]);
    /// 3. one PowerShell call of [`winps::BUNDLE`] zips exactly the listed
    ///    files there after checking the list's SHA-256 (never a re-walk);
    /// 4. a reported archive size over [`bundle::archive_limit`] is refused
    ///    before any byte moves; otherwise one session gets the archive into
    ///    a private dir (killed if it grows past the reported size plus
    ///    `DOWNLOAD_SLACK_BYTES`), removes it and the staging dir;
    /// 5. the archive must be exactly the reported size, and
    ///    [`bundle::extract`] unpacks it under its hostile-archive checks.
    ///
    /// Any failure once step 2 began is followed by one best-effort
    /// [`winps::BUNDLE_CLEAN`] call; when that fails too, its reason is
    /// appended to the original error. An empty `files` costs nothing.
    async fn fetch_bundled(&self, files: &[&FoundFile], tmp: &Path) -> Result<(), CoreError> {
        if files.is_empty() {
            return Ok(());
        }
        let listed: Vec<BundleFile> = files
            .iter()
            .map(|f| BundleFile {
                collection: f.collection.clone(),
                rel: f.rel.clone(),
                size: f.size,
            })
            .collect();
        let home = self.account_home().await?;
        let folded_home = home.to_ascii_lowercase();
        let folded_data = self.data_dir.trim_end_matches('/').to_ascii_lowercase();
        if folded_home == folded_data || folded_home.starts_with(&format!("{folded_data}/")) {
            return Err(self.fail(format!(
                "the account home {home} is inside the data directory; a Windows read stages its archive in the account home, never below the data directory"
            )));
        }
        let ctl = format!("{home}/{}", self.ctl_name());
        if let Some(why) = safe_remote_path(&ctl)
            .err()
            .or_else(|| windows_absolute_path_problem(&ctl))
        {
            return Err(self.fail(format!("the read staging directory is unsafe: {why}")));
        }
        let ctl_native = windows_native_path(&ctl);
        let ran = self.bundle_into(&ctl, &listed, tmp).await;
        let Err(err) = ran else {
            return Ok(());
        };
        let names = [bundle::LIST_FILE, bundle::ARCHIVE_FILE];
        let clean = winps::WinScript::bundle_clean(LONG_PATH_PREFIX, &ctl_native, &names);
        let why = match self.read_powershell(&clean).await {
            Ok(value) if value.get("ok").and_then(serde_json::Value::as_i64) == Some(1) => {
                return Err(err);
            }
            Ok(value) => match value.get("err").and_then(serde_json::Value::as_str) {
                Some("left") => "it holds other entries".to_string(),
                Some("reparse") => "it is a reparse point".to_string(),
                _ => "the cleanup result is malformed".to_string(),
            },
            Err(CoreError::TransportFailed { detail, .. }) => detail,
            Err(other) => other.to_string(),
        };
        Err(match err {
            CoreError::TransportFailed {
                operation,
                node,
                detail,
            } => CoreError::TransportFailed {
                operation,
                node,
                detail: format!(
                    "{detail}; the read staging directory {ctl} could not be removed ({why})"
                ),
            },
            other => other,
        })
    }

    /// Steps 2 to 5 of [`fetch_bundled`](Self::fetch_bundled) in the staging
    /// dir `ctl` (sftp spelling).
    async fn bundle_into(
        &self,
        ctl: &str,
        listed: &[BundleFile],
        tmp: &Path,
    ) -> Result<(), CoreError> {
        let list = bundle::render_list(listed);
        let list_sha = sha256_hex(&list);
        let local = self.download_dir("read")?;
        let local_list = local.path().join(bundle::LIST_FILE);
        std::fs::write(&local_list, &list)
            .map_err(|e| self.fail(format!("cannot stage the read list ({:?})", e.kind())))?;
        let remote_list = format!("{ctl}/{}", bundle::LIST_FILE);
        let remote_zip = format!("{ctl}/{}", bundle::ARCHIVE_FILE);
        self.run_batch(
            "read",
            vec![
                SftpOp::Mkdir {
                    path: ctl.to_string(),
                },
                SftpOp::Put {
                    local: local_list,
                    remote: remote_list.clone(),
                },
            ],
        )
        .await?;

        let limit = bundle::archive_limit(listed);
        let need = limit.saturating_add(bundle::STAGING_FREE_MARGIN);
        let script = winps::WinScript::bundle(
            LONG_PATH_PREFIX,
            &windows_native_path(&format!("{}/config/resources", self.data_dir)),
            &windows_native_path(&remote_list),
            &windows_native_path(&remote_zip),
            &list_sha,
            need,
        );
        let value = self.read_powershell(&script).await.map_err(|e| match e {
            CoreError::TransportFailed { detail, .. } => self.fail(format!(
                "cannot bundle the listed files on the host: {detail}"
            )),
            other => other,
        })?;
        let z = self.bundle_result(&value, listed, need)?;
        if z > limit {
            return Err(self.fail(format!(
                "the read archive is {z} bytes, more than the {limit} bytes the listed files can take; it was not downloaded"
            )));
        }

        let archive_dir = self.download_dir("read")?;
        let archive = archive_dir.path().join(bundle::ARCHIVE_FILE);
        self.run_download(
            "read",
            &[
                SftpOp::Get {
                    remote: remote_zip.clone(),
                    local: archive.clone(),
                },
                SftpOp::Rm { path: remote_zip },
                SftpOp::Rmdir {
                    path: ctl.to_string(),
                },
            ],
            archive_dir.path(),
            z.saturating_add(DOWNLOAD_SLACK_BYTES),
        )
        .await?;
        let arrived = std::fs::symlink_metadata(&archive)
            .map_err(|e| self.fail(format!("the read archive did not arrive ({:?})", e.kind())))?;
        if !arrived.file_type().is_file() || arrived.len() != z {
            return Err(self.fail(format!(
                "the read archive arrived as {} bytes but was reported as {z}",
                arrived.len()
            )));
        }
        let files = listed.to_vec();
        let dest = tmp.to_path_buf();
        tokio::task::spawn_blocking(move || {
            bundle::extract(&archive, &files, &dest, DOWNLOAD_SLACK_BYTES)
        })
        .await
        .map_err(|e| CoreError::Internal(format!("sftp read task failed: {e}")))?
        .map_err(|why| self.fail(why))?;
        drop(archive_dir);
        Ok(())
    }

    /// The archive size of a BUNDLE result for `listed`, or the read's
    /// error for any other answer.
    fn bundle_result(
        &self,
        value: &serde_json::Value,
        listed: &[BundleFile],
        need: u64,
    ) -> Result<u64, CoreError> {
        let malformed = || self.fail("the bundle result is malformed");
        let number = |key: &str| value.get(key).and_then(serde_json::Value::as_u64);
        let file_at = |key: &str| {
            number(key)
                .and_then(|i| usize::try_from(i).ok())
                .and_then(|i| listed.get(i))
        };
        match value.get("ok").and_then(serde_json::Value::as_i64) {
            Some(1) => match (number("n"), number("z")) {
                (Some(n), Some(z)) if n == listed.len() as u64 => Ok(z),
                _ => Err(malformed()),
            },
            Some(0) => Err(match value.get("err").and_then(serde_json::Value::as_str) {
                Some("list") => self.fail(
                    "the read list on the host does not match its SHA-256; it was changed after the upload",
                ),
                Some("space") => match number("free") {
                    Some(free) => self.fail(format!(
                        "the account home's drive has {free} bytes free, fewer than the {need} bytes a read archive of the listed files needs"
                    )),
                    None => malformed(),
                },
                Some("missing") => match file_at("i") {
                    Some(file) => self.fail(format!(
                        "config/resources/{} is not on the host any more; it changed while it was read",
                        file.name()
                    )),
                    None => malformed(),
                },
                Some("changed") => match (file_at("i"), number("n")) {
                    // BUNDLE stops one byte past the listed size, so a
                    // larger `n` is a lower bound, not the file's size
                    (Some(file), Some(n)) if n > file.size => self.fail(format!(
                        "config/resources/{} is more than the {} bytes it was listed as; it changed while it was read",
                        file.name(),
                        file.size
                    )),
                    (Some(file), Some(n)) => self.fail(format!(
                        "config/resources/{} is {n} bytes but was listed as {}; it changed while it was read",
                        file.name(),
                        file.size
                    )),
                    _ => malformed(),
                },
                _ => malformed(),
            }),
            _ => Err(malformed()),
        }
    }

    /// The delivered size of each file of a finished chunk must be exactly
    /// the listed size (and within the per-file cap); returns the bytes.
    fn check_chunk(&self, chunk: &[&FoundFile], tmp: &Path) -> Result<u64, CoreError> {
        let mut bytes = 0u64;
        for file in chunk {
            let local = file
                .rel
                .split('/')
                .fold(tmp.join(&file.collection), |p, c| p.join(c));
            let shown = format!("config/resources/{}/{}", file.collection, file.rel);
            let meta = match std::fs::symlink_metadata(&local) {
                Ok(meta) => meta,
                // reported with the other missing files by the final check
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
                Err(e) => return Err(self.fail(format!("cannot inspect {shown} ({:?})", e.kind()))),
            };
            if !meta.file_type().is_file() {
                return Err(self.fail(format!("{shown} was downloaded as a non-regular file")));
            }
            let len = meta.len();
            if len > READ_MAX_FILE_BYTES {
                return Err(self.fail(format!(
                    "{shown} is {len} bytes, over the {READ_MAX_FILE_BYTES} byte file cap"
                )));
            }
            if len != file.size {
                return Err(self.fail(format!(
                    "{shown} is {len} bytes but was listed as {}; it changed while it was read",
                    file.size
                )));
            }
            bytes += len;
        }
        Ok(bytes)
    }

    /// A fresh private directory for downloads, inside the shell's own temp
    /// dir when it has one. Deleted when dropped.
    fn download_dir(&self, op: &str) -> Result<tempfile::TempDir, CoreError> {
        let mut builder = tempfile::Builder::new();
        builder.prefix("ign-read-");
        let made = match self.shell.temp_dir() {
            Some(parent) => builder.tempdir_in(parent),
            None => builder.tempdir(),
        };
        made.map_err(|e| {
            self.mirror_fail(op, format!("cannot create a download dir ({:?})", e.kind()))
        })
    }
}

/// Bytes held by regular files under `dir`, without following symlinks. A
/// tree with more than [`READ_MAX_ENTRIES`] entries, or one that cannot be
/// measured, counts as unbounded so the caller aborts.
fn dir_bytes(dir: &Path) -> u64 {
    let mut total = 0u64;
    let mut entries = 0usize;
    let mut stack = vec![dir.to_path_buf()];
    while let Some(current) = stack.pop() {
        let Ok(read) = std::fs::read_dir(&current) else {
            continue;
        };
        for entry in read.flatten() {
            entries += 1;
            if entries > READ_MAX_ENTRIES {
                return u64::MAX;
            }
            let Ok(meta) = std::fs::symlink_metadata(entry.path()) else {
                continue;
            };
            if meta.is_dir() {
                stack.push(entry.path());
            } else {
                total = total.saturating_add(meta.len());
            }
        }
    }
    total
}

/// Why reading a downloaded file back was refused.
enum Readback {
    Io(std::io::ErrorKind),
    Changed,
    Capped(u64),
    Grew,
    SizeMismatch { listed: u64, actual: u64 },
}

/// Read one downloaded file: open it descriptor-relative from `root`
/// (a swapped-in symlink is not followed), require the size to equal what
/// the listing said and to be within `cap`, and return its bytes.
fn read_downloaded(root: &Path, rel: &Path, listed: u64, cap: u64) -> Result<Vec<u8>, Readback> {
    let path = root.join(rel);
    let before = std::fs::symlink_metadata(&path).map_err(|e| Readback::Io(e.kind()))?;
    if !before.file_type().is_file() {
        return Err(Readback::Changed);
    }
    let (file, held) = open_regular_file(root, rel, &before).map_err(|e| match e {
        OpenError::Changed => Readback::Changed,
        OpenError::Io(e) => Readback::Io(e.kind()),
    })?;
    let len = held.len();
    if len > cap {
        return Err(Readback::Capped(len));
    }
    if len != listed {
        return Err(Readback::SizeMismatch {
            listed,
            actual: len,
        });
    }
    let mut bytes = Vec::with_capacity(len as usize);
    file.take(cap + 1)
        .read_to_end(&mut bytes)
        .map_err(|e| Readback::Io(e.kind()))?;
    if bytes.len() as u64 > cap {
        return Err(Readback::Grew);
    }
    Ok(bytes)
}

/// Check the downloads of `roots` under `tmp_root` against the listing and
/// return the hashed blobs per collection. Runs on a blocking thread.
fn verify_download(
    label: &str,
    tmp_root: &Path,
    roots: &[Vec<String>],
    listed: &BTreeMap<(String, String), u64>,
) -> Result<BTreeMap<String, BTreeMap<RelPath, FileBlob>>, CoreError> {
    let fail = |detail: String| transport_failed(label, detail);
    let mut actual: BTreeMap<(String, String), PathBuf> = BTreeMap::new();
    for root in roots {
        let below: Vec<&str> = root.iter().skip(2).map(String::as_str).collect();
        let shown = format!("config/resources/{}", below.join("/"));
        let dir = below.iter().fold(tmp_root.to_path_buf(), |p, c| p.join(c));
        match std::fs::symlink_metadata(&dir) {
            Ok(meta) if meta.file_type().is_dir() => {}
            Ok(_) => {
                return Err(fail(format!(
                    "{shown} was downloaded as something other than a directory"
                )));
            }
            // nothing was fetched into it (an empty directory); a listed file
            // that is missing is reported below
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => {
                return Err(fail(format!(
                    "{shown} cannot be inspected after the download ({:?})",
                    e.kind()
                )));
            }
        }
        let mut files: BTreeMap<String, PathBuf> = BTreeMap::new();
        let mut dirs: BTreeSet<String> = BTreeSet::new();
        walk_capped(&dir, &shown, &mut files, &mut dirs, READ_MAX_ENTRIES).map_err(fail)?;
        let collection = below.first().copied().unwrap_or_default();
        let strip = format!("config/resources/{collection}/");
        for (full, path) in files {
            let rel = full.strip_prefix(&strip).unwrap_or(&full).to_string();
            actual.insert((collection.to_string(), rel), path);
        }
    }
    if let Some(key) = listed.keys().find(|k| !actual.contains_key(*k)) {
        return Err(fail(format!(
            "config/resources/{}/{} was listed but not downloaded; the tree changed while it was read",
            key.0, key.1
        )));
    }
    if let Some(key) = actual.keys().find(|k| !listed.contains_key(*k)) {
        return Err(fail(format!(
            "config/resources/{}/{} appeared in the download but was not listed; the tree changed while it was read",
            key.0, key.1
        )));
    }
    let mut out: BTreeMap<String, BTreeMap<RelPath, FileBlob>> = BTreeMap::new();
    let mut total = 0u64;
    for ((collection, rel), path) in actual {
        let shown = format!("config/resources/{collection}/{rel}");
        let key = RelPath::new(&rel)
            .map_err(|why| fail(format!("{shown} has an unusable name ({why})")))?;
        let size = listed.get(&(collection.clone(), rel.clone())).copied();
        let Some(size) = size else { continue };
        let Ok(under) = path.strip_prefix(tmp_root) else {
            return Err(fail(format!("{shown} is outside the download dir")));
        };
        let bytes = read_downloaded(tmp_root, under, size, READ_MAX_FILE_BYTES).map_err(|e| {
            fail(match e {
                Readback::Io(kind) => format!("cannot read {shown} ({kind:?})"),
                Readback::Changed => format!("{shown} changed while it was read"),
                Readback::Capped(len) => format!(
                    "{shown} is {len} bytes, over the {READ_MAX_FILE_BYTES} byte file cap"
                ),
                Readback::Grew => format!(
                    "{shown} grew past the {READ_MAX_FILE_BYTES} byte file cap while it was read"
                ),
                Readback::SizeMismatch { listed, actual } => format!(
                    "{shown} is {actual} bytes but was listed as {listed}; it changed while it was read"
                ),
            })
        })?;
        total += bytes.len() as u64;
        if total > READ_MAX_TOTAL_BYTES {
            return Err(fail(format!(
                "the files under config/resources exceed {READ_MAX_TOTAL_BYTES} bytes in total (at {shown})"
            )));
        }
        let sha256 = sha256_hex(&bytes);
        out.entry(collection)
            .or_default()
            .insert(key, FileBlob { bytes, sha256 });
    }
    Ok(out)
}

#[async_trait::async_trait]
impl TreeReader for SftpTransport {
    async fn list_types(&self, collection: &str) -> Result<Vec<(String, String)>, CoreError> {
        if let Some(why) = component_problem(collection) {
            return Err(self.fail(format!("the collection name {why}")));
        }
        self.windows_gate("the collection name", collection)?;
        safe_remote_path(collection)
            .map_err(|why| self.fail(format!("the collection name is unsafe: {why}")))?;
        let mut budget = Budget::default();
        let found = self
            .descend(&[RESOURCES[0], RESOURCES[1], collection], &mut budget)
            .await
            .map_err(|p| self.read_problem(p))?;
        let Some((collection_dir, entry)) = found else {
            return Ok(Vec::new());
        };
        if entry.kind != EntryKind::Dir {
            return Err(self.fail(format!("config/resources/{collection} is not a directory")));
        }
        // every parsed entry already counted against the budget
        let child_dirs = |entries: Vec<ListedEntry>, rel: &str| -> Result<Vec<String>, CoreError> {
            let mut names = Vec::new();
            for entry in entries {
                match entry.kind {
                    EntryKind::Symlink => {
                        return Err(self.fail(format!(
                            "{rel}/{} is a symlink; symlinks are refused",
                            entry.name
                        )));
                    }
                    EntryKind::Dir => names.push(entry.name),
                    _ => {}
                }
            }
            Ok(names)
        };
        let coll_rel = format!("config/resources/{collection}");
        let top = self
            .list_level(std::slice::from_ref(&collection_dir), &mut budget)
            .await?
            .pop()
            .unwrap_or_default();
        let modules = child_dirs(top, &coll_rel)?;
        let module_dirs: Vec<String> = modules
            .iter()
            .map(|m| format!("{collection_dir}/{m}"))
            .collect();
        let listings = self.list_level(&module_dirs, &mut budget).await?;
        let mut pairs = Vec::new();
        for (module, entries) in modules.iter().zip(listings) {
            for ty in child_dirs(entries, &format!("{coll_rel}/{module}"))? {
                pairs.push((module.clone(), ty));
            }
        }
        pairs.sort_by(|a, b| {
            (a.0.as_bytes(), a.1.as_bytes()).cmp(&(b.0.as_bytes(), b.1.as_bytes()))
        });
        Ok(pairs)
    }

    async fn list_tag_providers(&self, collection: &str) -> Result<Vec<String>, CoreError> {
        if let Some(why) = component_problem(collection) {
            return Err(self.fail(format!("the collection name {why}")));
        }
        self.windows_gate("the collection name", collection)?;
        safe_remote_path(collection)
            .map_err(|why| self.fail(format!("the collection name is unsafe: {why}")))?;
        let mut budget = Budget::default();
        let found = self
            .descend(
                &[
                    RESOURCES[0],
                    RESOURCES[1],
                    collection,
                    "ignition",
                    "tag-provider",
                ],
                &mut budget,
            )
            .await
            .map_err(|p| self.read_problem(p))?;
        let Some((provider_dir, entry)) = found else {
            return Ok(Vec::new());
        };
        let rel = format!("config/resources/{collection}/ignition/tag-provider");
        if entry.kind != EntryKind::Dir {
            return Err(self.fail(format!("{rel} is not a directory")));
        }
        let entries = self
            .list_level(std::slice::from_ref(&provider_dir), &mut budget)
            .await?
            .pop()
            .unwrap_or_default();
        // every parsed entry already counted against the budget
        let mut names = Vec::new();
        for entry in entries {
            match entry.kind {
                EntryKind::Symlink => {
                    return Err(self.fail(format!(
                        "{rel}/{} is a symlink; symlinks are refused",
                        entry.name
                    )));
                }
                EntryKind::Dir => names.push(entry.name),
                _ => {}
            }
        }
        names.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes()));
        if self.windows {
            // the listing is not proof of completeness on this host (WR-03)
            self.cross_check_names(&provider_dir, &rel, &names).await?;
        }
        Ok(names)
    }

    async fn list_tree(
        &self,
        collection: &str,
        dirs: &[(String, String)],
    ) -> Result<Vec<TreeListing>, CoreError> {
        if let Some(why) = component_problem(collection) {
            return Err(self.fail(format!("the collection name {why}")));
        }
        self.windows_gate("the collection name", collection)?;
        safe_remote_path(collection)
            .map_err(|why| self.fail(format!("the collection name is unsafe: {why}")))?;
        let mut chains: BTreeSet<Vec<String>> = BTreeSet::new();
        for (module, ty) in dirs {
            validate_segment("module", module).map_err(|e| self.fail(e))?;
            validate_segment("type", ty).map_err(|e| self.fail(e))?;
            self.windows_gate("the module", module)?;
            self.windows_gate("the type", ty)?;
            let mut chain: Vec<String> = RESOURCES.iter().map(|c| (*c).to_string()).collect();
            chain.extend([collection.to_string(), module.clone(), ty.clone()]);
            chains.insert(chain);
        }
        if chains.is_empty() {
            return Ok(Vec::new());
        }
        let mut budget = Budget::default();
        // a Windows host is listed by a fixed number of PowerShell calls
        // (quick 261104-s3n): an over-limit entry is listed, never dropped,
        // and a requested folder the host holds is never read as absent (N2)
        let (found, long) = if self.windows {
            self.discover_windows(&chains, &mut budget).await?
        } else {
            (self.discover(&chains, &mut budget).await?, Vec::new())
        };
        let mut listings = Vec::new();
        for (module, ty) in dirs {
            let mut chain: Vec<String> = RESOURCES.iter().map(|c| (*c).to_string()).collect();
            chain.extend([collection.to_string(), module.clone(), ty.clone()]);
            if !found.roots.contains(&chain) {
                continue;
            }
            let under = format!("{module}/{ty}/");
            let mut seen: BTreeMap<String, bool> = BTreeMap::new();
            for file in found.files.iter().filter(|f| f.collection == collection) {
                if file.rel.starts_with(&under) {
                    seen.insert(file.rel.clone(), false);
                }
            }
            for dir in found
                .dirs
                .iter()
                .filter(|d| d.len() > chain.len() && d.starts_with(chain.as_slice()))
            {
                seen.insert(dir[RESOURCES.len() + 1..].join("/"), true);
            }
            for entry in long.iter().filter(|e| e.collection == collection) {
                if entry.rel.starts_with(&under) {
                    seen.insert(entry.rel.clone(), entry.dir);
                }
            }
            let mut entries = Vec::with_capacity(seen.len());
            for (rel, dir) in seen {
                let path = RelPath::new(&rel).map_err(|why| {
                    self.fail(format!(
                        "config/resources/{collection}/{rel} has an unusable name ({why})"
                    ))
                })?;
                entries.push(TreeEntry { path, dir });
            }
            entries.sort_by(|a, b| a.path.as_str().as_bytes().cmp(b.path.as_str().as_bytes()));
            listings.push(TreeListing {
                module: module.clone(),
                ty: ty.clone(),
                entries,
            });
        }
        Ok(listings)
    }

    async fn read(&self, scope: &Scope) -> Result<ObservedFiles, CoreError> {
        let chains = self.target_chains(scope)?;
        let mut budget = Budget::default();
        // a Windows host is listed by a fixed number of PowerShell calls
        // before any byte is downloaded (quick 261104-s3n); a requested root
        // the host holds is never read as absent (M5, N2)
        let (found, long) = if self.windows {
            self.discover_windows(&chains, &mut budget).await?
        } else {
            (self.discover(&chains, &mut budget).await?, Vec::new())
        };
        let mut unreadable: Vec<Unreadable> = Vec::new();
        let mut local_keys: BTreeMap<RelPath, ()> = BTreeMap::new();
        if self.windows {
            // an over-limit file is never in `found`, so it is never fetched
            // and `verify_download` never expects it
            for entry in long.into_iter().filter(|e| !e.dir) {
                let shown = format!("config/resources/{}/{}", entry.collection, entry.rel);
                let path = RelPath::new(&entry.rel)
                    .map_err(|why| self.fail(format!("{shown} has an unusable name ({why})")))?;
                let collection = if entry.collection == LOCAL_COLLECTION {
                    // names only: its unit is still a `local` unit
                    local_keys.insert(path, ());
                    continue;
                } else if entry.collection == CORE_COLLECTION {
                    "core"
                } else if entry.collection == scope.mode {
                    "mode"
                } else {
                    continue;
                };
                unreadable.push(Unreadable {
                    collection: collection.to_string(),
                    path,
                    length: entry.length,
                });
            }
            unreadable.sort_by(|a, b| {
                (a.collection.as_bytes(), a.path.as_str().as_bytes())
                    .cmp(&(b.collection.as_bytes(), b.path.as_str().as_bytes()))
            });
            unreadable.dedup_by(|a, b| a.collection == b.collection && a.path == b.path);
        }

        // Caps from the listing, before a byte is downloaded (local files are
        // listed only for their names and are not read).
        let mut total = 0u64;
        let mut listed: BTreeMap<(String, String), u64> = BTreeMap::new();
        for file in &found.files {
            let shown = format!("config/resources/{}/{}", file.collection, file.rel);
            if file.collection == LOCAL_COLLECTION {
                let key = RelPath::new(&file.rel)
                    .map_err(|why| self.fail(format!("{shown} has an unusable name ({why})")))?;
                local_keys.insert(key, ());
                continue;
            }
            if file.size > READ_MAX_FILE_BYTES {
                return Err(self.fail(format!(
                    "{shown} is {} bytes, over the {READ_MAX_FILE_BYTES} byte file cap",
                    file.size
                )));
            }
            total += file.size;
            if total > READ_MAX_TOTAL_BYTES {
                return Err(self.fail(format!(
                    "the files under config/resources exceed {READ_MAX_TOTAL_BYTES} bytes in total (at {shown})"
                )));
            }
            listed.insert((file.collection.clone(), file.rel.clone()), file.size);
        }

        let download_roots: Vec<Vec<String>> = found
            .roots
            .iter()
            .filter(|root| root.get(2).map(String::as_str) != Some(LOCAL_COLLECTION))
            .cloned()
            .collect();
        let mut by_collection: BTreeMap<String, BTreeMap<RelPath, FileBlob>> = BTreeMap::new();
        if !download_roots.is_empty() {
            // One `get` per listed file, never `get -R`: the server decides
            // what a recursive get delivers, so a recursive get would bypass
            // every per-file check made on the listing.
            // On a Windows host the listed files arrive as one archive of
            // exactly those files (quick 261105-dl1): one `get` per file
            // costs about 0.37 s there.
            let tmp = self.download_dir("read")?;
            let fetch: Vec<&FoundFile> = found
                .files
                .iter()
                .filter(|f| f.collection != LOCAL_COLLECTION)
                .collect();
            if self.bundle_reads {
                self.fetch_bundled(&fetch, tmp.path()).await?;
            } else {
                self.fetch_files(&fetch, tmp.path()).await?;
            }

            let label = self.label.clone();
            by_collection = tokio::task::spawn_blocking(move || {
                verify_download(&label, tmp.path(), &download_roots, &listed)
            })
            .await
            .map_err(|e| CoreError::Internal(format!("sftp read task failed: {e}")))??;
        }

        let local_units: BTreeSet<String> = local_keys
            .keys()
            .map(|k| unit_key(k, &local_keys))
            .collect();
        Ok(ObservedFiles {
            core: by_collection
                .get(CORE_COLLECTION)
                .cloned()
                .unwrap_or_default(),
            mode: by_collection.get(&scope.mode).cloned().unwrap_or_default(),
            local_units: local_units.into_iter().collect(),
            unreadable,
        })
    }

    async fn read_ledger(&self, gateway: &str, mode: &str) -> Result<Option<Vec<u8>>, CoreError> {
        // Decision B(i): an ssh node keeps its ledger in the service account's
        // home, outside every collection and outside the data directory.
        let location = ledger_location(LedgerRoot::AccountHome, gateway, mode);
        self.read_home_file(
            &ledger_rel(gateway, mode),
            LEDGER_MAX_BYTES,
            "the ledger",
            [("target gateway", gateway), ("target mode", mode)],
            &|problem| CoreError::DeployLedgerInvalid {
                location: location.clone(),
                problem,
            },
        )
        .await
    }

    async fn read_journal(&self, gateway: &str, mode: &str) -> Result<Option<Vec<u8>>, CoreError> {
        // Under the same root as the ledger (D-04). A refusal is
        // `deploy_run_invalid` naming the location; the run id is not known
        // before the journal is parsed.
        let location = journal_location(LedgerRoot::AccountHome, gateway, mode);
        self.read_home_file(
            &journal_rel(gateway, mode),
            JOURNAL_MAX_BYTES,
            "the journal",
            [("target gateway", gateway), ("target mode", mode)],
            &|problem| CoreError::DeployRunInvalid {
                run_id: "unknown".to_string(),
                problem: format!("{location}: {problem}"),
            },
        )
        .await
    }
}

impl SftpTransport {
    /// The bytes of the regular file `rel` below the service account's home,
    /// or `None` when any level of its path is absent. Names that are not one
    /// safe component, a symlink or a non-directory on the way, a
    /// non-regular file, a file over `cap` (checked from the listing before
    /// any byte is fetched) and every read error are refused through `bad`,
    /// which builds the refusal from a value-free problem. The ledger, the
    /// journal and the release record (25-08) all read through here, so they
    /// fail closed the same way. `names` are the two path components the
    /// caller fills in (`kind` is how a refusal names each).
    async fn read_home_file(
        &self,
        rel: &str,
        cap: usize,
        what: &str,
        names: [(&str, &str); 2],
        bad: &(dyn Fn(String) -> CoreError + Sync),
    ) -> Result<Option<Vec<u8>>, CoreError> {
        for (kind, value) in names {
            if let Some(why) = component_problem(value) {
                return Err(bad(format!("the {kind} {why}")));
            }
            if let Some(why) = self
                .windows
                .then(|| windows_component_problem(value))
                .flatten()
            {
                return Err(bad(format!("the {kind}: {why}")));
            }
        }
        let parts: Vec<&str> = rel.split('/').collect();
        // An unreadable host is not an absent file: the home must answer
        // `pwd` and list.
        let home = self.account_home().await?;
        // Only the fixed names of the path are looked up in the home, so
        // names in it that cannot ride a batch line (a Windows profile holds
        // `NTUSER.DAT{<guid>}.TM.blf`) are skipped, not refused.
        let mut budget = Budget::lenient();
        let found = self
            .descend_from(&home, &parts, &mut budget)
            .await
            .map_err(|problem| match problem {
                Descend::Failed(err) => err,
                Descend::Symlink(_) => {
                    bad("a path component is a symlink; symlinks are refused".to_string())
                }
                Descend::NotDir(_) => bad("a path component is not a directory".to_string()),
            })?;
        let Some((remote, entry)) = found else {
            return Ok(None);
        };
        if entry.kind != EntryKind::File {
            return Err(bad(format!("{what} is not a regular file")));
        }
        if entry.size > cap as u64 {
            return Err(bad(format!(
                "the file is {} bytes, over the {cap} byte cap",
                entry.size
            )));
        }
        let tmp = self.download_dir("read")?;
        self.run_download(
            "read",
            &[SftpOp::Get {
                remote,
                local: tmp.path().join("ledger"),
            }],
            tmp.path(),
            entry.size + DOWNLOAD_SLACK_BYTES,
        )
        .await?;
        let size = entry.size;
        let root = tmp.path().to_path_buf();
        let read = tokio::task::spawn_blocking(move || {
            let result = read_downloaded(&root, Path::new("ledger"), size, cap as u64);
            drop(tmp);
            result
        })
        .await
        .map_err(|e| CoreError::Internal(format!("sftp read task failed: {e}")))?;
        match read {
            Ok(bytes) => Ok(Some(bytes)),
            Err(Readback::Io(kind)) => Err(bad(format!("cannot be read ({kind:?})"))),
            Err(Readback::Changed) => Err(bad(
                "changed while it was read; symlinks are refused".to_string()
            )),
            Err(Readback::Capped(len)) => Err(bad(format!(
                "the file is {len} bytes, over the {cap} byte cap"
            ))),
            Err(Readback::Grew) => Err(bad(format!(
                "the file grew past the {cap} byte cap while it was read"
            ))),
            Err(Readback::SizeMismatch { listed, actual }) => Err(bad(format!(
                "the file is {actual} bytes but was listed as {listed}; it changed while it was read"
            ))),
        }
    }
}

// ---------------------------------------------------------------------------
// The write half (22-07)
// ---------------------------------------------------------------------------
//
// `TreeMirror::commit` (D-01) runs the uploaded script's `mirror` op in one
// `ssh` call on either family (`sftp/apply_ops.rs`: POSIX `sh` since 23-06,
// Windows PowerShell since 23-20); there is no rename per file. `stage` still
// uploads the staged copies with sftp `put` (nothing under the scan lock), and
// the script, which checks the SHA-256 of every staged copy on the host before
// the first move, does every check, move, delete and emptied-directory removal.

/// What the write half needs: the mode collection and the staging mode (the
/// staging root normalised to an absolute POSIX path).
#[derive(Debug, Clone)]
struct MirrorSetup {
    mode: String,
    staging: StagingMode,
}

/// What a survey listing says about one path.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Seen {
    Missing,
    Dir,
    File(u64),
}

/// At most this many mismatches are named in an error.
const MISMATCH_NAMED: usize = 8;

/// `base` plus `chain`, joined with `/`.
fn join_abs(base: &str, chain: &[String]) -> String {
    let mut path = base.to_string();
    for part in chain {
        path.push('/');
        path.push_str(part);
    }
    path
}

/// The first [`MISMATCH_NAMED`] entries of `items`, joined for an error.
fn named(items: &[String]) -> String {
    let mut text = items
        .iter()
        .take(MISMATCH_NAMED)
        .cloned()
        .collect::<Vec<_>>()
        .join(", ");
    if items.len() > MISMATCH_NAMED {
        text.push_str(&format!(" and {} more", items.len() - MISMATCH_NAMED));
    }
    text
}

impl SftpTransport {
    /// Bind the write half to the `mode` collection with `staging`. A
    /// `Separate` root is normalised to an absolute POSIX path here and must
    /// lie outside `config/resources`.
    pub fn with_mirror(mut self, mode: &str, staging: StagingMode) -> Result<Self, String> {
        if let Some(why) = mirror_mode_problem(mode) {
            return Err(why);
        }
        safe_remote_path(mode).map_err(|why| format!("the mode is unsafe: {why}"))?;
        if self.windows
            && let Some(why) = windows_component_problem(mode)
        {
            return Err(format!("the mode is refused: {why}"));
        }
        let resources = self.resources_display();
        validate_staging_root_for(&staging, &resources, self.windows)?;
        let staging = match staging {
            StagingMode::Separate { root } => {
                let parts = absolute_components(&root)
                    .map_err(|why| format!("the staging root {root:?} {why}"))?;
                if !root.starts_with('/') {
                    return Err(format!(
                        "the staging root {root:?} must be an absolute POSIX path on the host"
                    ));
                }
                let normal = format!("/{}", parts.join("/"));
                safe_remote_path(&normal)
                    .map_err(|why| format!("the staging root is unsafe: {why}"))?;
                if self.windows
                    && let Some(why) = windows_absolute_path_problem(&normal)
                {
                    return Err(format!("the staging root is refused: {why}"));
                }
                StagingMode::Separate { root: normal }
            }
            StagingMode::InPlace => StagingMode::InPlace,
        };
        self.mirror = Some(MirrorSetup {
            mode: mode.to_string(),
            staging,
        });
        Ok(self)
    }

    /// `<data_dir>/config/resources`, for the staging-root containment check.
    fn resources_display(&self) -> String {
        format!("{}/config/resources", self.data_dir)
    }

    fn mirror_fail(&self, op: &str, detail: impl std::fmt::Display) -> CoreError {
        CoreError::TransportFailed {
            operation: op.to_string(),
            node: self.label.clone(),
            detail: detail.to_string(),
        }
    }

    /// On a Windows host every component of a staged or deleted path must
    /// also pass the Windows component gate; checked before any I/O.
    fn windows_paths<'a>(
        &self,
        op: &str,
        paths: impl Iterator<Item = &'a RelPath>,
    ) -> Result<(), CoreError> {
        if !self.windows {
            return Ok(());
        }
        for path in paths {
            for component in path.components() {
                if let Some(why) = windows_component_problem(component) {
                    return Err(
                        self.mirror_fail(op, format!("{path} cannot be a Windows path: {why}"))
                    );
                }
            }
        }
        Ok(())
    }

    fn mirror_setup(&self, op: &str) -> Result<&MirrorSetup, CoreError> {
        self.mirror.as_ref().ok_or_else(|| {
            self.mirror_fail(
                op,
                "this transport has no write half; build it with mirror_for_node",
            )
        })
    }

    /// Which node and mode a [`Staged`] belongs to.
    fn mirror_owner(&self, setup: &MirrorSetup) -> String {
        format!("sftp:{}:{}:{}", self.label, self.data_dir, setup.mode)
    }

    /// Run one mutating batch; an empty batch spawns nothing. Nothing is
    /// retried here: a held target is the script's business (D-01), and the
    /// uploads and directory makes of this batch have no such case.
    async fn run_batch(&self, op: &str, ops: Vec<SftpOp>) -> Result<(), CoreError> {
        if ops.is_empty() {
            return Ok(());
        }
        self.sessions.fetch_add(1, Ordering::Relaxed);
        let out = self.shell.sftp_batch(&ops).await?;
        self.shell.require_success(op, out)?;
        Ok(())
    }

    /// Where a survey of the staging `root` starts, and the components of
    /// `root` below that point.
    ///
    /// A root below the account home is surveyed from the home, which the
    /// server itself named (`pwd`) and which every read below the home
    /// already descends from. Nothing at or above the home is listed or
    /// created, so a home spelled with an 8.3 short name (the Windows runner's
    /// `C:\Users\RUNNER~1`, which the listing of `C:\Users` never shows) or one
    /// whose ancestors the account cannot list does not fail the survey. A root
    /// outside the home starts at `/<first component>` as before.
    ///
    /// The home is always asked first ([`account_home`](Self::account_home),
    /// one `@pwd` session unless cached), so the survey never depends on
    /// whether an earlier call on this instance happened to cache it (a
    /// transport fresh from `mirror_for_node` has not, WR-01). An unreadable
    /// home is `transport_failed`, before anything is listed.
    ///
    /// The home's components are compared by the host's case rule:
    /// ASCII-case-insensitively on a Windows host (as
    /// [`validate_staging_root_for`](super::validate_staging_root_for) does),
    /// exactly on a POSIX one. The base keeps the server's spelling of the
    /// home. An 8.3 short name and its long name are not resolved: that needs
    /// the host, so a root spelled with the other form than the home's (for
    /// a home `/C:/Users/RUNNER~1`, the root `/C:/Users/runneradmin/...`, or
    /// the reverse) still falls back to the `/<first component>` survey
    /// (WR-02). That fallback lists the drive root, and when the root names
    /// the short spelling it fails at `mkdir` exactly as G2 did.
    async fn survey_root(&self, op: &str, root: &str) -> Result<(String, Vec<String>), CoreError> {
        let parts = absolute_components(root).map_err(|why| self.mirror_fail(op, why))?;
        if parts.is_empty() {
            return Err(self.mirror_fail(op, "must name a directory below the filesystem root"));
        }
        let home = self.account_home().await?;
        let same = |a: &String, b: &String| {
            if self.windows {
                a.eq_ignore_ascii_case(b)
            } else {
                a == b
            }
        };
        if let Ok(home_parts) = absolute_components(&home)
            && !home_parts.is_empty()
            && parts.len() > home_parts.len()
            && parts.iter().zip(&home_parts).all(|(a, b)| same(a, b))
        {
            return Ok((home, parts[home_parts.len()..].to_vec()));
        }
        Ok((format!("/{}", parts[0]), parts[1..].to_vec()))
    }

    /// Read-only: what every path in `targets` (chains below the existing
    /// directory `base`) is on the host, one listing session per depth.
    ///
    /// A directory is listed only when it is known to exist and has a needed
    /// child; a child of a missing directory is missing without a listing. A
    /// symlink or special file anywhere, or a file where a directory is
    /// needed, is refused. `base` itself is listed first, so a missing or
    /// unreadable `base` is `transport_failed`.
    async fn survey(
        &self,
        op: &str,
        base: &str,
        targets: &[Vec<String>],
        budget: &mut Budget,
    ) -> Result<BTreeMap<Vec<String>, Seen>, CoreError> {
        let mut needed: BTreeSet<Vec<String>> = BTreeSet::new();
        for target in targets {
            for len in 1..=target.len() {
                needed.insert(target[..len].to_vec());
            }
        }
        let mut has_child: BTreeSet<Vec<String>> = BTreeSet::new();
        for chain in &needed {
            if chain.len() > 1 {
                has_child.insert(chain[..chain.len() - 1].to_vec());
            }
        }
        let max_depth = needed.iter().map(Vec::len).max().unwrap_or(0);
        if max_depth > READ_MAX_DEPTH {
            return Err(self.mirror_fail(op, "a path is deeper than the walk bound"));
        }
        let mut seen: BTreeMap<Vec<String>, Seen> = BTreeMap::new();
        for depth in 0..max_depth {
            let children: Vec<&Vec<String>> =
                needed.iter().filter(|c| c.len() == depth + 1).collect();
            let mut parents: BTreeSet<Vec<String>> = BTreeSet::new();
            for child in &children {
                let parent = child[..depth].to_vec();
                let parent_state = if depth == 0 {
                    Seen::Dir
                } else {
                    seen.get(&parent).copied().unwrap_or(Seen::Missing)
                };
                if parent_state == Seen::Dir {
                    parents.insert(parent);
                }
            }
            let parents: Vec<Vec<String>> = parents.into_iter().collect();
            let dirs: Vec<String> = parents.iter().map(|p| join_abs(base, p)).collect();
            let listings = self.list_level_as(op, &dirs, budget).await?;
            for child in children {
                let parent = child[..depth].to_vec();
                let name = &child[depth];
                let state = match parents.iter().position(|p| *p == parent) {
                    None => Seen::Missing,
                    Some(index) => match listings[index].iter().find(|e| e.name == *name) {
                        None => Seen::Missing,
                        Some(entry) => {
                            let shown = self.shown(&join_abs(base, child));
                            match entry.kind {
                                EntryKind::Symlink => {
                                    return Err(self.mirror_fail(
                                        op,
                                        format!("{shown} is a symlink; symlinks are refused"),
                                    ));
                                }
                                EntryKind::Other => {
                                    return Err(self.mirror_fail(
                                        op,
                                        format!("{shown} is not a regular file or directory"),
                                    ));
                                }
                                EntryKind::File if has_child.contains(child) => {
                                    return Err(
                                        self.mirror_fail(op, format!("{shown} is not a directory"))
                                    );
                                }
                                EntryKind::File => Seen::File(entry.size),
                                EntryKind::Dir => Seen::Dir,
                            }
                        }
                    },
                };
                seen.insert(child.clone(), state);
            }
        }
        Ok(seen)
    }

    /// Create every directory in `missing` (chains below `base`), one `mkdir`
    /// session per depth, shallowest first. Only chains a survey showed
    /// missing are passed, so `mkdir` is never issued for an existing level.
    async fn make_dirs(
        &self,
        op: &str,
        base: &str,
        missing: &BTreeSet<Vec<String>>,
    ) -> Result<(), CoreError> {
        let max_depth = missing.iter().map(Vec::len).max().unwrap_or(0);
        for depth in 1..=max_depth {
            let ops: Vec<SftpOp> = missing
                .iter()
                .filter(|chain| chain.len() == depth)
                .map(|chain| SftpOp::Mkdir {
                    path: join_abs(base, chain),
                })
                .collect();
            self.run_batch(op, ops).await?;
        }
        Ok(())
    }

    /// The directories above `files` that a survey showed missing.
    fn dirs_to_make(
        seen: &BTreeMap<Vec<String>, Seen>,
        files: &[Vec<String>],
    ) -> BTreeSet<Vec<String>> {
        let mut missing = BTreeSet::new();
        for file in files {
            for len in 1..file.len() {
                let chain = file[..len].to_vec();
                if seen.get(&chain) == Some(&Seen::Missing) {
                    missing.insert(chain);
                }
            }
        }
        missing
    }

    /// Sessions of at most [`DOWNLOAD_CHUNK_FILES`] lines and, for `bytes`
    /// per op, at most [`DOWNLOAD_CHUNK_BYTES`] bytes.
    async fn run_chunked(&self, op: &str, ops: Vec<(SftpOp, u64)>) -> Result<(), CoreError> {
        let mut batch: Vec<SftpOp> = Vec::new();
        let mut bytes = 0u64;
        for (line, size) in ops {
            if !batch.is_empty()
                && (batch.len() >= DOWNLOAD_CHUNK_FILES || bytes + size > DOWNLOAD_CHUNK_BYTES)
            {
                self.run_batch(op, std::mem::take(&mut batch)).await?;
                bytes = 0;
            }
            bytes += size;
            batch.push(line);
        }
        self.run_batch(op, batch).await
    }

    async fn stage_inner(&self, tree: &RenderedTree) -> Result<Staged, CoreError> {
        const OP: &str = "stage";
        let setup = self.mirror_setup(OP)?;
        self.windows_paths(OP, tree.files.keys())?;
        validate_mirror_inputs(
            tree,
            &DeleteSet::default(),
            &setup.staging,
            &self.resources_display(),
        )
        .map_err(|why| self.mirror_fail(OP, why))?;
        let owned = tree.clone();
        let (local, files) = tokio::task::spawn_blocking(move || copy_to_temp(&owned))
            .await
            .map_err(|e| CoreError::Internal(format!("sftp stage task failed: {e}")))?
            .map_err(|why| self.mirror_fail(OP, why))?;

        let remote_root = match &setup.staging {
            StagingMode::InPlace => None,
            StagingMode::Separate { root } => {
                // the account home (asked, then listed first, so an unreadable
                // home is `transport_failed`), or `/<first>` for a root
                // outside it; either is assumed to exist and its listing
                // proves it
                let (base, rest) = self.survey_root(OP, root).await?;
                let mut targets: Vec<Vec<String>> = Vec::new();
                let mut file_chains: Vec<Vec<String>> = Vec::new();
                for (path, _) in &files {
                    let mut chain = rest.clone();
                    chain.extend(path.components().map(str::to_string));
                    file_chains.push(chain.clone());
                    targets.push(chain);
                }
                targets.push(rest.clone());
                // the staging root is below the account home, whose listing
                // may hold names a batch line cannot carry; only the fixed
                // names of the root are looked up
                let mut budget = Budget::lenient();
                let seen = self.survey(OP, &base, &targets, &mut budget).await?;
                for chain in &file_chains {
                    if matches!(seen.get(chain), Some(Seen::Dir)) {
                        return Err(self.mirror_fail(
                            OP,
                            format!(
                                "{} exists in the staging root and is a directory",
                                self.shown(&join_abs(&base, chain))
                            ),
                        ));
                    }
                }
                if matches!(seen.get(&rest), Some(Seen::File(_))) {
                    return Err(self.mirror_fail(
                        OP,
                        format!("the staging root {root:?} is a file, not a directory"),
                    ));
                }
                let mut missing = Self::dirs_to_make(&seen, &file_chains);
                // the root itself and its missing ancestors
                for len in 1..=rest.len() {
                    let chain = rest[..len].to_vec();
                    if seen.get(&chain) == Some(&Seen::Missing) {
                        missing.insert(chain);
                    }
                }
                self.make_dirs(OP, &base, &missing).await?;
                let puts: Vec<(SftpOp, u64)> = tree
                    .files
                    .iter()
                    .map(|(path, bytes)| {
                        (
                            SftpOp::Put {
                                local: local.path().join(path.as_str()),
                                remote: format!("{root}/{path}"),
                            },
                            bytes.len() as u64,
                        )
                    })
                    .collect();
                self.run_chunked(OP, puts).await?;
                Some(root.clone())
            }
        };
        Ok(Staged {
            files,
            local,
            remote_root,
            owner: self.mirror_owner(setup),
        })
    }

    /// Everything a mirror commit checks before any I/O on the host: the
    /// owner, the staging mode, every path (Windows gate, mirror rules, batch
    /// line) and the local staged copies, which are re-read and re-hashed.
    /// Returns the setup and each staged copy's size.
    async fn mirror_precheck(
        &self,
        local: &Path,
        files: &[(RelPath, String)],
        remote_root: Option<&str>,
        owner: &str,
        delete_set: &DeleteSet,
    ) -> Result<(&MirrorSetup, Vec<u64>), CoreError> {
        const OP: &str = "commit";
        let setup = self.mirror_setup(OP)?;
        if owner != self.mirror_owner(setup) {
            return Err(self.mirror_fail(
                OP,
                "these files were staged by a different mirror (another node or mode)",
            ));
        }
        let expected_root = match &setup.staging {
            StagingMode::Separate { root } => Some(root.as_str()),
            StagingMode::InPlace => None,
        };
        if remote_root != expected_root {
            return Err(self.mirror_fail(OP, "these files were staged in a different staging mode"));
        }
        let staged_paths: Vec<&RelPath> = files.iter().map(|(path, _)| path).collect();
        self.windows_paths(
            OP,
            staged_paths.iter().copied().chain(delete_set.files.iter()),
        )?;
        validate_mirror_paths(&staged_paths, delete_set)
            .and_then(|()| {
                validate_staging_root_for(&setup.staging, &self.resources_display(), self.windows)
            })
            .map_err(|why| self.mirror_fail(OP, why))?;
        for path in staged_paths.iter().copied().chain(delete_set.files.iter()) {
            safe_remote_path(path.as_str()).map_err(|why| {
                self.mirror_fail(
                    OP,
                    format!("{path} cannot be placed in an sftp batch: {why}"),
                )
            })?;
        }

        // ---- expected state: everything is checked before the first write.
        // The local copies first, through `open_regular_file`.
        let sizes: Vec<u64> = {
            let (base, copies) = (local.to_path_buf(), files.to_vec());
            tokio::task::spawn_blocking(move || -> Result<Vec<u64>, String> {
                copies
                    .iter()
                    .map(|(path, sha)| read_staged_copy(&base, path, sha).map(|b| b.len() as u64))
                    .collect()
            })
            .await
            .map_err(|e| CoreError::Internal(format!("sftp commit task failed: {e}")))?
            .map_err(|why| self.mirror_fail(OP, why))?
        };
        Ok((setup, sizes))
    }
}

#[async_trait::async_trait]
impl TreeMirror for SftpTransport {
    async fn stage(&self, tree: &RenderedTree) -> Result<Staged, CoreError> {
        self.stage_inner(tree).await
    }

    async fn commit(
        &self,
        staged: &Staged,
        delete_set: &DeleteSet,
    ) -> Result<MirrorReport, CoreError> {
        // D-01: a commit is one script call on either family, never a rename
        // per file.
        self.mirror_commit_script(
            staged.local.path(),
            &staged.files,
            staged.remote_root.as_deref(),
            &staged.owner,
            delete_set,
        )
        .await
    }
}

/// The production-shape generator the integration tests share (quick
/// 261104-s3n), for the offline connection-count tests below.
#[cfg(test)]
#[path = "../../../tests/common/prod_shape.rs"]
mod prod_shape;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::deploy::apply::journal::JOURNAL_FILE;
    use crate::deploy::ledger::LEDGER_FILE;
    use crate::deploy::transport::batch::RENAME_RETRIES;
    use crate::deploy::transport::process::{ProcessOutput, ProcessRunner};
    use std::collections::HashSet;
    use std::ffi::{OsStr, OsString};
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    use super::prod_shape;

    const DATA: &str = "/gw/data";
    const RES: &str = "/gw/data/config/resources";
    /// The service account's home as the fake server reports it (`pwd`).
    const HOME: &str = "/home/svc";

    // ---- parse_ls_listing / posix_root ------------------------------------

    fn line(mode: &str, size: u64, path: &str) -> String {
        format!("{mode}    ? svc staff {size:>10} Oct 14 10:21 {path}")
    }

    #[test]
    fn parse_ls_listing_cases() {
        let dir = "/d/a b,c";
        let out = [
            line("drwxr-xr-x", 96, "/d/a b,c/."),
            line("drwxr-xr-x", 96, "/d/a b,c/.."),
            line("-rw-r--r--", 12, "/d/a b,c/zeta file"),
            line("drwxr-xr-x", 64, "/d/a b,c/alpha"),
            line("lrwxr-xr-x", 5, "/d/a b,c/lnk"),
            line("crw-r--r--", 0, "/d/a b,c/dev"),
            line("-rw-r--r--", 7, "/d/a b,c/.hidden"),
        ]
        .join("\n");
        let out = format!("{PWD_MARKER}/home/u\n{out}\n");
        let got = parse_ls_listing(dir, out.as_bytes()).expect("listing");
        let names: Vec<(&str, EntryKind, u64)> = got
            .iter()
            .map(|e| (e.name.as_str(), e.kind, e.size))
            .collect();
        assert_eq!(
            names,
            [
                (".hidden", EntryKind::File, 7),
                ("alpha", EntryKind::Dir, 64),
                ("dev", EntryKind::Other, 0),
                ("lnk", EntryKind::Symlink, 5),
                ("zeta file", EntryKind::File, 12),
            ],
            "byte-sorted, dot entries dropped, kinds from the mode column"
        );

        let bad = [
            // no prefix of a listed directory
            line("-rw-r--r--", 1, "/other/x"),
            // a name holding a slash is two components
            line("-rw-r--r--", 1, "/d/a b,c/x/y"),
            // empty name
            line("-rw-r--r--", 1, "/d/a b,c/"),
            // unsafe names
            line("-rw-r--r--", 1, "/d/a b,c/odd[1]"),
            line("-rw-r--r--", 1, "/d/a b,c/odd\"q"),
            line("-rw-r--r--", 1, "/d/a b,c/back\\slash"),
            // too few columns, bad mode, bad size
            "/d/a b,c/x".to_string(),
            line("-rw", 1, "/d/a b,c/x"),
            "-rw-r--r--    ? svc staff NaN Oct 14 10:21 /d/a b,c/x".to_string(),
            // an empty line
            "\n".to_string(),
        ];
        for text in bad {
            // each case sits under its marker, so it is refused for its own
            // reason and not for the missing marker
            let text = format!("{PWD_MARKER}/home/u\n{text}");
            assert!(
                parse_ls_listing(dir, text.as_bytes()).is_err(),
                "{text:?} must be refused"
            );
        }
        // the same name twice
        let twice = format!(
            "{PWD_MARKER}/home/u\n{}\n{}\n",
            line("-rw-r--r--", 1, "/d/a b,c/x"),
            line("-rw-r--r--", 2, "/d/a b,c/x")
        );
        assert!(parse_ls_listing(dir, twice.as_bytes()).is_err());
        // not UTF-8
        assert!(parse_ls_listing(dir, b"\xff\xfe").is_err());
        // an empty directory is a marker and no entry line (or only the dots)
        let marker = format!("{PWD_MARKER}/home/u\n");
        assert!(
            parse_ls_listing(dir, marker.as_bytes())
                .expect("empty")
                .is_empty()
        );
        let dots = format!(
            "{marker}{}\n{}\n",
            line("drwxr-xr-x", 1, "/d/a b,c/."),
            line("drwxr-xr-x", 1, "/d/a b,c/..")
        );
        assert!(
            parse_ls_listing(dir, dots.as_bytes())
                .expect("dots")
                .is_empty()
        );
        // a bare `.` or `..` carries no directory: not a POSIX `ls -lan` line
        for bare in [".", ".."] {
            let text = format!("{marker}{}\n", line("drwxr-xr-x", 1, bare));
            assert!(
                parse_ls_listing(dir, text.as_bytes()).is_err(),
                "a bare {bare:?} must be refused"
            );
        }
        // an output with no marker at all is not a listing (the batch always
        // leads with `@pwd`), as on Windows
        assert!(parse_ls_listing(dir, b"").is_err());
    }

    #[test]
    fn parse_batch_listing_attributes_lines_in_order() {
        let dirs = vec!["/d/a".to_string(), "/d/a/b".to_string(), "/d/c".to_string()];
        let marker = format!("{PWD_MARKER}/home/u");
        let out = [
            marker.clone(),
            line("drwxr-xr-x", 1, "/d/a/."),
            line("drwxr-xr-x", 1, "/d/a/b"),
            line("-rw-r--r--", 3, "/d/a/f"),
            marker.clone(),
            line("drwxr-xr-x", 1, "/d/a/b/."),
            line("-rw-r--r--", 4, "/d/a/b/x"),
            marker.clone(),
            line("-rw-r--r--", 5, "/d/c/y"),
        ]
        .join("\n");
        let got = parse_batch_listing(&dirs, out.as_bytes()).expect("batch");
        let names = |i: usize| -> Vec<&str> { got[i].iter().map(|e| e.name.as_str()).collect() };
        assert_eq!(names(0), ["b", "f"]);
        assert_eq!(names(1), ["x"]);
        assert_eq!(names(2), ["y"]);
        // a line under the marker of another directory is not valid
        let backwards = [
            marker.clone(),
            line("-rw-r--r--", 5, "/d/c/y"),
            marker.clone(),
            line("-rw-r--r--", 3, "/d/a/f"),
            marker,
        ]
        .join("\n");
        assert!(parse_batch_listing(&dirs, backwards.as_bytes()).is_err());
    }

    // ---- G1: the two shapes of a POSIX long listing -----------------------
    //
    // For `ls -la` the sftp CLIENT prints a line the server's way (a bare
    // name: OpenSSH 8.9 on Ubuntu 22.04) unless it formats the line itself,
    // which it does when the server offers `users-groups-by-id@openssh.com`
    // (the macOS `sftp-server`); then the listed path is in front of the
    // name. Both captures are verbatim, from `## G1: POSIX sftp listing
    // shapes` in 23-LIVE-CAPTURES.md. A bare line is byte-identical to the
    // tail of a name that held a raw `\n`, so a POSIX host is now listed with
    // `ls -lan` (the client formats every line) and the bare capture is
    // refused; the client-formatted one is still read.

    /// `pwd`, `ls -la` of the account home, `pwd`, `ls -la` of the production
    /// collection, through ign-lnx-836's own `sftp-server` (2026-10-19).
    const OPENSSH_8_9: &str = "\
Remote working directory: /home/svc-ignition-deploy\n\
drwxr-x---    4 svc-ignition-deploy svc-ignition-deploy     4096 Oct 18 23:43 .\n\
drwxr-xr-x    4 root     root         4096 Oct 18 22:52 ..\n\
-rw-r--r--    1 svc-ignition-deploy svc-ignition-deploy      220 Jan  6  2022 .bash_logout\n\
-rw-r--r--    1 svc-ignition-deploy svc-ignition-deploy     3771 Jan  6  2022 .bashrc\n\
drwx------    2 svc-ignition-deploy svc-ignition-deploy     4096 Oct 18 22:52 .cache\n\
-rw-r--r--    1 svc-ignition-deploy svc-ignition-deploy      807 Jan  6  2022 .profile\n\
drwx------    2 svc-ignition-deploy svc-ignition-deploy     4096 Oct 18 22:52 .ssh\n\
Remote working directory: /home/svc-ignition-deploy\n\
drwxrwxr-x    2 root     root         4096 Oct 18 22:59 .\n\
drwxr-xr-x    7 root     root         4096 Oct 18 22:50 ..\n\
-rw-rw-r--    1 root     root          140 Oct 18 22:59 config-mode.json\n\
";

    /// `pwd`, `ls -la` of a scratch directory, `pwd`, `ls -la` of its
    /// sub directory, through the macOS `/usr/libexec/sftp-server`.
    const MACOS_CLIENT_FORMATTED: &str = "\
Remote working directory: /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac\n\
drwxr-xr-x    ? pmannion wheel         128 Oct 18 18:27 /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch/.\n\
drwxr-xr-x    ? pmannion wheel          96 Oct 18 18:27 /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch/..\n\
-rw-r--r--    ? pmannion wheel           6 Oct 18 18:27 /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch/file.txt\n\
drwxr-xr-x    ? pmannion wheel          96 Oct 18 18:27 /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch/sub\n\
Remote working directory: /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac\n\
drwxr-xr-x    ? pmannion wheel          96 Oct 18 18:27 /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch/sub/.\n\
drwxr-xr-x    ? pmannion wheel         128 Oct 18 18:27 /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch/sub/..\n\
-rw-r--r--    ? pmannion wheel           1 Oct 18 18:27 /private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch/sub/inner.json\n\
";

    const MAC_DIR: &str = "/private/tmp/claude-501/-Users-pmannion-whiskeyhouse-ignition-cli/7dbe57f3-a4c4-4f47-88c4-a2249a4d2b80/scratchpad/g1mac/scratch";

    fn shown(entries: &[ListedEntry]) -> Vec<(&str, EntryKind, u64)> {
        entries
            .iter()
            .map(|e| (e.name.as_str(), e.kind, e.size))
            .collect()
    }

    #[test]
    fn posix_listing_refuses_captured_bare_openssh_8_9_output() {
        // the bare `ls -la` shape carries no directory on any line, so a line
        // cannot be told from the tail of a name split by a raw `\n`
        let dirs = vec![
            "/home/svc-ignition-deploy".to_string(),
            "/usr/local/bin/ignition/data/config/resources/production".to_string(),
        ];
        for rule in [NameRule::Refuse, NameRule::Skip] {
            let got =
                parse_batch_listing_as(ListFormat::Posix, rule, &dirs, OPENSSH_8_9.as_bytes(), 100);
            assert!(
                matches!(&got, Err(ListingError::Bad(why)) if why.contains("does not carry the listed directory")),
                "{rule:?}: {got:?}"
            );
        }
    }

    /// `pwd`, `ls -lan` of the same two directories through the same
    /// ign-lnx-836 OpenSSH 8.9 `sftp-server` (D3 of the raw-newline fix,
    /// 2026-10-19, `lnxvm_listing_capture`, macOS OpenSSH_10.3p1 client): with
    /// `-n` the client formats every line itself, numeric owner and group and
    /// the listed path in front of the name, though the server offers no
    /// users-groups extension.
    const OPENSSH_8_9_LAN: &str = "\
Remote working directory: /home/svc-ignition-deploy\n\
drwxr-x---    ? 1001     1001         4096 Oct 19 00:59 /home/svc-ignition-deploy/.\n\
drwxr-xr-x    ? 0        0            4096 Oct 18 16:52 /home/svc-ignition-deploy/..\n\
-rw-r--r--    ? 1001     1001          220 Jan  6  2022 /home/svc-ignition-deploy/.bash_logout\n\
-rw-r--r--    ? 1001     1001         3771 Jan  6  2022 /home/svc-ignition-deploy/.bashrc\n\
drwx------    ? 1001     1001         4096 Oct 18 16:52 /home/svc-ignition-deploy/.cache\n\
-rw-r--r--    ? 1001     1001          807 Jan  6  2022 /home/svc-ignition-deploy/.profile\n\
drwx------    ? 1001     1001         4096 Oct 18 16:52 /home/svc-ignition-deploy/.ssh\n\
Remote working directory: /home/svc-ignition-deploy\n\
drwxrwxr-x    ? 0        0            4096 Oct 18 20:40 /usr/local/bin/ignition/data/config/resources/production/.\n\
drwxr-xr-x    ? 0        0            4096 Oct 18 16:50 /usr/local/bin/ignition/data/config/resources/production/..\n\
-rw-rw-r--    ? 0        0             140 Oct 18 16:59 /usr/local/bin/ignition/data/config/resources/production/config-mode.json\n\
";

    #[test]
    fn posix_listing_parses_captured_ls_lan_output_from_openssh_8_9() {
        let dirs = vec![
            "/home/svc-ignition-deploy".to_string(),
            "/usr/local/bin/ignition/data/config/resources/production".to_string(),
        ];
        let got =
            parse_batch_listing(&dirs, OPENSSH_8_9_LAN.as_bytes()).expect("the 8.9 -lan capture");
        assert_eq!(
            shown(&got[0]),
            [
                (".bash_logout", EntryKind::File, 220),
                (".bashrc", EntryKind::File, 3771),
                (".cache", EntryKind::Dir, 4096),
                (".profile", EntryKind::File, 807),
                (".ssh", EntryKind::Dir, 4096),
            ],
            "the entries of the bare capture, dots dropped, a year column read as a date"
        );
        assert_eq!(shown(&got[1]), [("config-mode.json", EntryKind::File, 140)]);
    }

    #[test]
    fn posix_listing_parses_captured_client_formatted_output() {
        let dirs = vec![MAC_DIR.to_string(), format!("{MAC_DIR}/sub")];
        let got = parse_batch_listing(&dirs, MACOS_CLIENT_FORMATTED.as_bytes())
            .expect("the macOS capture");
        assert_eq!(
            shown(&got[0]),
            [
                ("file.txt", EntryKind::File, 6),
                ("sub", EntryKind::Dir, 96)
            ]
        );
        assert_eq!(shown(&got[1]), [("inner.json", EntryKind::File, 1)]);
    }

    #[test]
    fn posix_listing_refuses_a_bare_line_and_windows_takes_either_shape() {
        let dirs = vec!["/d/bare".to_string(), "/d/prefixed".to_string()];
        let text = format!(
            "{PWD_MARKER}/home/u\n{}\n{}\n{PWD_MARKER}/home/u\n{}\n{}\n",
            line("drwxr-xr-x", 1, "."),
            line("-rw-r--r--", 3, "plain name"),
            line("drwxr-xr-x", 1, "/d/prefixed/."),
            line("-rw-r--r--", 4, "/d/prefixed/other name"),
        );
        // POSIX (`ls -lan`): every entry line carries its directory
        for rule in [NameRule::Refuse, NameRule::Skip] {
            let got = parse_batch_listing_as(ListFormat::Posix, rule, &dirs, text.as_bytes(), 100);
            assert!(
                matches!(got, Err(ListingError::Bad(_))),
                "{rule:?}: {got:?}"
            );
        }
        // the control: the same entries, every line prefixed
        let prefixed = format!(
            "{PWD_MARKER}/home/u\n{}\n{}\n{PWD_MARKER}/home/u\n{}\n{}\n",
            line("drwxr-xr-x", 1, "/d/bare/."),
            line("-rw-r--r--", 3, "/d/bare/plain name"),
            line("drwxr-xr-x", 1, "/d/prefixed/."),
            line("-rw-r--r--", 4, "/d/prefixed/other name"),
        );
        let got = parse_batch_listing(&dirs, prefixed.as_bytes()).expect("prefixed");
        assert_eq!(shown(&got[0]), [("plain name", EntryKind::File, 3)]);
        assert_eq!(shown(&got[1]), [("other name", EntryKind::File, 4)]);
        // Windows (`ls -la`, bare server names) still reads either shape
        let got = parse_batch_listing_as(
            ListFormat::Windows,
            NameRule::Refuse,
            &dirs,
            text.as_bytes(),
            100,
        )
        .expect("mixed shapes on Windows");
        assert_eq!(shown(&got[0]), [("plain name", EntryKind::File, 3)]);
        assert_eq!(shown(&got[1]), [("other name", EntryKind::File, 4)]);
    }

    #[test]
    fn posix_listing_refuses_a_prefix_of_another_directory() {
        let dirs = vec!["/d/one".to_string(), "/d/two".to_string()];
        let marker = format!("{PWD_MARKER}/home/u\n");
        let one = line("-rw-r--r--", 1, "/d/one/x");
        let two = line("-rw-r--r--", 1, "/d/two/y");
        // the control: each line under its own marker
        let ok = format!("{marker}{one}\n{marker}{two}\n");
        assert!(parse_batch_listing(&dirs, ok.as_bytes()).is_ok());
        for (why, text) in [
            (
                "directory one's prefix under marker two",
                format!("{marker}{one}\n{marker}{one}\n"),
            ),
            (
                "directory two's prefix under marker one",
                format!("{marker}{two}\n{marker}{two}\n"),
            ),
            (
                "a name with a slash that is not the current prefix",
                format!(
                    "{marker}{one}\n{marker}{}\n",
                    line("-rw-r--r--", 1, "/elsewhere/y")
                ),
            ),
            (
                "a nested path under the parent's marker",
                format!(
                    "{marker}{}\n{marker}{two}\n",
                    line("-rw-r--r--", 1, "/d/one/sub/z")
                ),
            ),
            ("fewer markers than directories", format!("{marker}{one}\n")),
            (
                "an extra marker line",
                format!("{marker}{one}\n{marker}{two}\n{marker}"),
            ),
            (
                "a line before the first marker",
                format!("{one}\n{marker}{two}\n"),
            ),
            ("no marker at all", format!("{one}\n{two}\n")),
        ] {
            assert!(
                parse_batch_listing(&dirs, text.as_bytes()).is_err(),
                "{why}: must be refused"
            );
        }
    }

    #[test]
    fn bare_listing_with_a_spaced_owner_is_refused_not_misread() {
        // a bare line (the server's longname) is read only for a Windows host
        let dirs = vec!["/d".to_string()];
        let one = |text: &str| format!("{PWD_MARKER}/home/u\n{text}\n");
        let bare = |text: &str| {
            parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Refuse,
                &dirs,
                one(text).as_bytes(),
                100,
            )
        };
        // the control: a plain owner and group
        let control = "drwxr-x--- 4 svc users 4096 Oct 18 23:43 name";
        assert!(bare(control).is_ok());
        // the same bare line from a POSIX host is refused, not read
        assert!(parse_batch_listing(&dirs, one(control).as_bytes()).is_err());
        for text in [
            // an owner with a space: the day column would be a month
            "drwxr-x--- 4 svc domain users 4096 Oct 18 23:43 name",
            // a group with a space and a number in it
            "drwxr-x--- 4 svc domain 123 4096 Oct 18 23:43 name",
            // a day that is not a day, a time that is not a time
            "drwxr-x--- 4 svc users 4096 Oct 99 23:43 name",
            "drwxr-x--- 4 svc users 4096 Oct 18 noon name",
            "drwxr-x--- 4 svc users 4096 Oct 18 23:4 name",
            "drwxr-x--- 4 svc users 4096 Oct 18 202 name",
        ] {
            assert!(bare(text).is_err(), "{text:?} must be refused");
        }
        // both date forms of `ls` are read: a time, and a year, bare (Windows)
        // and with the directory in front of the name (POSIX)
        for text in [
            "-rw-r--r-- 1 svc users 4096 Oct  8 23:43 name",
            "-rw-r--r-- 1 svc users 4096 Jan  6  2022 name",
        ] {
            assert!(bare(text).is_ok(), "{text:?} must be read");
            let prefixed = text.replace(" name", " /d/name");
            assert!(
                parse_batch_listing(&dirs, one(&prefixed).as_bytes()).is_ok(),
                "{prefixed:?} must be read"
            );
        }
    }

    #[test]
    fn posix_listing_keeps_windows_only_rules_off() {
        let one = vec!["/d".to_string()];
        let posix = |name: &str| {
            format!("{PWD_MARKER}/home/u\n-rw-r--r--    ? 501 20   8 Oct 15 02:35 /d/{name}\n")
        };
        for name in ["a:b", "CON", "trail.", "trail ", "aux.txt"] {
            let got = parse_batch_listing(&one, posix(name).as_bytes())
                .unwrap_or_else(|e| panic!("{name:?} is an ordinary POSIX name: {e}"));
            assert_eq!(got[0].len(), 1, "{name:?}");
        }
        let both = format!(
            "{PWD_MARKER}/home/u\n-rw-r--r--    ? 501 20   8 Oct 15 02:35 /d/A\n-rw-r--r--    ? 501 20   8 Oct 15 02:35 /d/a\n"
        );
        assert_eq!(
            parse_batch_listing(&one, both.as_bytes()).expect("A and a")[0].len(),
            2,
            "POSIX names are case-sensitive"
        );
        // the same names under the Windows family are still refused
        for name in ["a:b", "CON", "trail."] {
            let text =
                format!("{PWD_MARKER}/C:/h\n-rw-******    1 -        -   8 Oct 15 02:35 {name}\n");
            assert!(
                parse_batch_listing_as(
                    ListFormat::Windows,
                    NameRule::Refuse,
                    &one,
                    text.as_bytes(),
                    100
                )
                .is_err(),
                "{name:?} must be refused on Windows"
            );
        }
        assert!(
            parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Refuse,
                &one,
                both.as_bytes(),
                100
            )
            .is_err(),
            "A and a are one name on Windows"
        );
    }

    // ---- a raw newline or carriage return inside a listed name --------------
    //
    // Every OpenSSH sftp client prints `\n`, `\r` and `\t` inside a name raw
    // (openssh-portable utf8.c, 8.9 through 10.3): a name holding `\n`
    // reaches the parser as two lines, and a name ending in `\r` as a line
    // ending in CRLF. The captures are from the OpenSSH_10.3p1 client over a
    // directory holding `plain` and `x\n<FORGED>`, and over one holding only
    // `y\r`, in three shapes: the server's bare longname, the line the client
    // formats, and `ls -lan`. The listed directory is normalised to `/d/nl`
    // and `/d/cr`, the `pwd` path to `/home/u`. A listing that cannot be read
    // exactly is refused (an unsafe but whole name is left out under Skip),
    // never read as `fake`, `x` or `y`.

    /// The text after the raw `\n` of the split name: a well-formed line.
    const FORGED: &str = "-rw-r--r--    1 root     root            5 Oct 18 22:52 fake";

    const NL_BARE: &str = "\
Remote working directory: /home/u\n\
drwxr-xr-x    4 pmannion wheel         128 Oct 19 04:26 .\n\
drwxr-xr-x    8 pmannion wheel         256 Oct 19 04:26 ..\n\
-rw-r--r--    1 pmannion wheel           2 Oct 19 04:26 plain\n\
-rw-r--r--    1 pmannion wheel           2 Oct 19 04:26 x\n\
-rw-r--r--    1 root     root            5 Oct 18 22:52 fake\n\
";

    const NL_CLIENT: &str = "\
Remote working directory: /home/u\n\
drwxr-xr-x    ? pmannion wheel         128 Oct 19 04:26 /d/nl/.\n\
drwxr-xr-x    ? pmannion wheel         288 Oct 19 04:26 /d/nl/..\n\
-rw-r--r--    ? pmannion wheel           2 Oct 19 04:26 /d/nl/plain\n\
-rw-r--r--    ? pmannion wheel           2 Oct 19 04:26 /d/nl/x\n\
-rw-r--r--    1 root     root            5 Oct 18 22:52 fake\n\
";

    const NL_LAN: &str = "\
Remote working directory: /home/u\n\
drwxr-xr-x    ? 501      0             128 Oct 19 04:26 /d/nl/.\n\
drwxr-xr-x    ? 501      0             320 Oct 19 04:26 /d/nl/..\n\
-rw-r--r--    ? 501      0               2 Oct 19 04:26 /d/nl/plain\n\
-rw-r--r--    ? 501      0               2 Oct 19 04:26 /d/nl/x\n\
-rw-r--r--    1 root     root            5 Oct 18 22:52 fake\n\
";

    const CR_BARE: &str = "\
Remote working directory: /home/u\n\
drwxr-xr-x    3 pmannion wheel          96 Oct 19 04:26 .\n\
drwxr-xr-x   13 pmannion wheel         416 Oct 19 04:26 ..\n\
-rw-r--r--    1 pmannion wheel           2 Oct 19 04:26 y\r\n\
";

    const CR_CLIENT: &str = "\
Remote working directory: /home/u\n\
drwxr-xr-x    ? pmannion wheel          96 Oct 19 04:26 /d/cr/.\n\
drwxr-xr-x    ? pmannion wheel         448 Oct 19 04:26 /d/cr/..\n\
-rw-r--r--    ? pmannion wheel           2 Oct 19 04:26 /d/cr/y\r\n\
";

    const CR_LAN: &str = "\
Remote working directory: /home/u\n\
drwxr-xr-x    ? 501      0              96 Oct 19 04:26 /d/cr/.\n\
drwxr-xr-x    ? 501      0             480 Oct 19 04:26 /d/cr/..\n\
-rw-r--r--    ? 501      0               2 Oct 19 04:26 /d/cr/y\r\n\
";

    /// One line the way the OpenSSH client formats `ls -lan`: numeric owner
    /// and group, the listed directory in front of the name.
    fn lan(mode: &str, size: u64, path: &str) -> String {
        format!("{mode}    ? 501      0      {size:>10} Oct 19 04:26 {path}")
    }

    fn names_of(entries: &[ListedEntry]) -> Vec<&str> {
        entries.iter().map(|e| e.name.as_str()).collect()
    }

    #[test]
    fn posix_listing_refuses_a_name_split_by_a_raw_newline() {
        let dirs = vec!["/d/nl".to_string()];
        // every case is tried, so a failure names each one that was misread
        let mut misread = Vec::new();
        for (shape, text) in [
            ("bare", NL_BARE),
            ("client-formatted", NL_CLIENT),
            ("ls -lan", NL_LAN),
        ] {
            assert!(
                text.ends_with(&format!("/x\n{FORGED}\n"))
                    || text.ends_with(&format!(" x\n{FORGED}\n"))
            );
            for rule in [NameRule::Refuse, NameRule::Skip] {
                let got =
                    parse_batch_listing_as(ListFormat::Posix, rule, &dirs, text.as_bytes(), 100);
                if !matches!(got, Err(ListingError::Bad(_))) {
                    misread.push(format!("{shape}, {rule:?}: {got:?}"));
                }
            }
        }
        assert!(
            misread.is_empty(),
            "the directory holds `plain` and `x\\n{FORGED}`, so every listing must be refused; these were not:\n{}",
            misread.join("\n")
        );
    }

    #[test]
    fn posix_listing_refuses_a_name_ending_in_a_carriage_return() {
        let dirs = vec!["/d/cr".to_string()];
        let mut misread = Vec::new();
        for (shape, text) in [
            ("bare", CR_BARE),
            ("client-formatted", CR_CLIENT),
            ("ls -lan", CR_LAN),
        ] {
            let got = parse_batch_listing_as(
                ListFormat::Posix,
                NameRule::Refuse,
                &dirs,
                text.as_bytes(),
                100,
            );
            if !matches!(got, Err(ListingError::Bad(_))) {
                misread.push(format!("{shape}, Refuse (must be refused): {got:?}"));
            }
        }
        // under Skip the unsafe name is left out, never read as `y`
        for (shape, text) in [("client-formatted", CR_CLIENT), ("ls -lan", CR_LAN)] {
            let got = parse_batch_listing_as(
                ListFormat::Posix,
                NameRule::Skip,
                &dirs,
                text.as_bytes(),
                100,
            );
            if got != Ok(vec![Vec::new()]) {
                misread.push(format!("{shape}, Skip (must leave `y\\r` out): {got:?}"));
            }
        }
        assert!(
            misread.is_empty(),
            "the only entry is `y\\r`, an unsafe name; these listings misread it:\n{}",
            misread.join("\n")
        );
    }

    #[test]
    fn posix_listing_refuses_names_next_to_a_raw_newline() {
        let dirs = vec!["/d/nl".to_string()];
        let head = format!(
            "{PWD_MARKER}/home/u\n{}\n{}\n",
            lan("drwxr-xr-x", 128, "/d/nl/."),
            lan("drwxr-xr-x", 320, "/d/nl/..")
        );
        let x = lan("-rw-r--r--", 2, "/d/nl/x");
        let z = lan("-rw-r--r--", 2, "/d/nl/z");
        let empty = lan("-rw-r--r--", 2, "/d/nl/");
        let mut misread = Vec::new();
        for (why, body) in [
            // `x\n` before another entry: an empty line between them
            ("`x\\n` before another entry", format!("{x}\n\n{z}\n")),
            // `x\n` as the last entry: the output ends in an empty line
            ("`x\\n` as the last entry", format!("{x}\n\n")),
            // `\nfake`: an empty name after the prefix, then a bare fragment
            ("`\\nfake`", format!("{empty}\nfake\n")),
            // `\n<FORGED>`: an empty name, then a well-formed line
            ("`\\n<forged line>`", format!("{empty}\n{FORGED}\n")),
            // `x\r\n<FORGED>`: a CRLF inside the name, then a well-formed line
            ("`x\\r\\n<forged line>`", format!("{x}\r\n{FORGED}\n")),
        ] {
            let text = format!("{head}{body}");
            for rule in [NameRule::Refuse, NameRule::Skip] {
                let got =
                    parse_batch_listing_as(ListFormat::Posix, rule, &dirs, text.as_bytes(), 100);
                if !matches!(got, Err(ListingError::Bad(_))) {
                    misread.push(format!("{why}, {rule:?}: {got:?}"));
                }
            }
        }
        assert!(
            misread.is_empty(),
            "every listing must be refused; these were not:\n{}",
            misread.join("\n")
        );
    }

    #[test]
    fn posix_listing_strips_a_carriage_return_only_when_every_line_ends_in_crlf() {
        let dirs = vec!["/d/nl".to_string()];
        let read = |rule: NameRule, text: &str| {
            parse_batch_listing_as(ListFormat::Posix, rule, &dirs, text.as_bytes(), 100)
        };
        let lines = [
            format!("{PWD_MARKER}/home/u"),
            lan("drwxr-xr-x", 128, "/d/nl/."),
            lan("drwxr-xr-x", 320, "/d/nl/.."),
            lan("-rw-r--r--", 2, "/d/nl/plain"),
            lan("-rw-r--r--", 2, "/d/nl/x"),
        ];
        // the controls: every line ends in LF, or every line (the client's
        // marker included) ends in CRLF and one CR is stripped from each
        let lf = format!("{}\n", lines.join("\n"));
        let crlf = format!("{}\r\n", lines.join("\r\n"));
        for (ends, text) in [("LF", &lf), ("CRLF", &crlf)] {
            let got = read(NameRule::Refuse, text)
                .unwrap_or_else(|e| panic!("a {ends}-uniform listing: {e}"));
            assert_eq!(names_of(&got[0]), ["plain", "x"], "{ends}");
        }
        // a CRLF-uniform listing keeps a name's own CR: refused, or left out
        let cr_name = format!("{crlf}{}\r\n", lan("-rw-r--r--", 2, "/d/nl/y\r"));
        let got = read(NameRule::Refuse, &cr_name);
        assert!(matches!(got, Err(ListingError::Bad(_))), "{got:?}");
        let got = read(NameRule::Skip, &cr_name).expect("skip");
        assert_eq!(names_of(&got[0]), ["plain", "x"]);
        let mut misread = Vec::new();
        // mixed: the marker ends in LF and every entry line in CRLF, so no CR
        // is stripped and every name keeps one
        let mixed = format!("{}\n{}\r\n", lines[0], lines[1..].join("\r\n"));
        let got = read(NameRule::Refuse, &mixed);
        if !matches!(got, Err(ListingError::Bad(_))) {
            misread.push(format!(
                "LF marker, CRLF entries, Refuse (must be refused): {got:?}"
            ));
        }
        let got = read(NameRule::Skip, &mixed);
        if got != Ok(vec![Vec::new()]) {
            misread.push(format!(
                "LF marker, CRLF entries, Skip (every name keeps its CR and is left out): {got:?}"
            ));
        }
        // a CRLF client listing a name split by a raw newline: the line before
        // the split ends in LF alone
        let split = format!(
            "{}\r\n{}\n{FORGED}\r\n",
            lines[..4].join("\r\n"),
            lan("-rw-r--r--", 2, "/d/nl/x")
        );
        for rule in [NameRule::Refuse, NameRule::Skip] {
            let got = read(rule, &split);
            if !matches!(got, Err(ListingError::Bad(_))) {
                misread.push(format!(
                    "CRLF listing of `x\\n<forged line>`, {rule:?} (must be refused): {got:?}"
                ));
            }
        }
        assert!(
            misread.is_empty(),
            "a listing whose line ends are not uniform was read:\n{}",
            misread.join("\n")
        );
    }

    #[test]
    fn windows_listing_strips_a_carriage_return_only_when_every_line_ends_in_crlf() {
        let dirs = vec!["/C:/d".to_string()];
        let read = |text: &str| {
            parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Refuse,
                &dirs,
                text.as_bytes(),
                100,
            )
        };
        let lines = [
            format!("{PWD_MARKER}/C:/Users/svc"),
            "-rw-******    1 -        -               8 Oct 15 02:35 a.json".to_string(),
            "drwx******    1 -        -               0 Oct 15 02:35 sub".to_string(),
        ];
        // the controls: LF-uniform and CRLF-uniform output
        for (ends, text) in [
            ("LF", format!("{}\n", lines.join("\n"))),
            ("CRLF", format!("{}\r\n", lines.join("\r\n"))),
        ] {
            let got = read(&text).unwrap_or_else(|e| panic!("a {ends}-uniform listing: {e}"));
            assert_eq!(names_of(&got[0]), ["a.json", "sub"], "{ends}");
        }
        // mixed: the marker ends in LF and the entry lines in CRLF
        let mixed = format!("{}\n{}\r\n", lines[0], lines[1..].join("\r\n"));
        let got = read(&mixed);
        assert!(
            matches!(got, Err(ListingError::Bad(_))),
            "a marker in LF and entries in CRLF must be refused, got {got:?}"
        );
    }

    #[tokio::test]
    async fn a_posix_host_is_listed_with_ls_lan_and_a_windows_host_with_ls_la() {
        // Windows keeps bare `ls -la` (a Win32 name cannot hold `\n` or `\r`)
        let wfake = windows_fake();
        windows_transport(&wfake)
            .list_level(&[WDATA.to_string()], &mut Budget::default())
            .await
            .expect("windows listing");
        assert_eq!(
            wfake.session_lines(),
            [vec!["@pwd".to_string(), format!("@ls -la \"{WDATA}\"")]]
        );
        // a POSIX host is listed with `ls -lan`: the client then formats every
        // line itself, with the listed directory in front of the name
        let fake = FakeSftp::new();
        transport(&fake)
            .list_level(&[DATA.to_string()], &mut Budget::default())
            .await
            .expect("posix listing");
        assert_eq!(
            fake.session_lines(),
            [vec!["@pwd".to_string(), format!("@ls -lan \"{DATA}\"")]]
        );
    }

    #[test]
    fn posix_root_cases() {
        assert_eq!(
            posix_root("/usr/local/bin/ignition/data"),
            Ok("/usr/local/bin/ignition/data".to_string())
        );
        assert_eq!(
            posix_root("/opt/ign data/"),
            Ok("/opt/ign data".to_string())
        );
        for bad in [
            "",
            "relative/dir",
            "C:\\Program Files\\Inductive",
            "/",
            "///",
            "/a//b",
            "/a/./b",
            "/a/../b",
            "/a/b*",
            "/a/[b]",
            "/a/{b}",
            "/a/b?",
            "/a/\"b",
            "/a/b\\c",
            "/a/b\nc",
        ] {
            assert!(posix_root(bad).is_err(), "{bad:?} must be refused");
        }
    }

    // ---- a scripted sftp server --------------------------------------------

    #[derive(Clone)]
    enum Node {
        Dir,
        File(Vec<u8>),
        Symlink,
        Special,
    }

    #[derive(Default)]
    struct Knobs {
        timeout: bool,
        stderr_noise: bool,
        fail_get: bool,
        /// Do not materialise a file whose path ends with this.
        skip_download: Option<String>,
        /// Write this extra file into every downloaded directory.
        extra_download: Option<String>,
        /// Report a different size for the path.
        lie_size: Option<(String, u64)>,
        /// Remove this path from the tree once the first session ended.
        vanish_after_first_session: Option<String>,
        /// A `get` of a path ending with `.0` writes a sparse file of `.1`
        /// bytes regardless of the listing.
        overshoot: Option<(String, u64)>,
        /// What `pwd` answers (default [`HOME`]).
        home: Option<String>,
        /// Behave like Win32-OpenSSH: long listings print bare names (no
        /// `.`/`..`, `******` permission bits) and `ssh` runs PowerShell.
        windows: bool,
        /// Behave like OpenSSH 8.9's `sftp-server` (no users-groups
        /// extension): for `ls -la` the client prints the server's bare
        /// names, `.` and `..` included, with no directory in front of them
        /// (G1). It changes nothing for `ls -lan`, whose lines the client
        /// always formats itself.
        bare_posix: bool,
        /// Paths the listing omits though they exist (the 260 character
        /// silent omission, C7a-8).
        hidden: Vec<String>,
        /// Paths the enumeration reports as reparse points.
        reparse: Vec<String>,
        /// The enumeration fails with this exit code and CLIXML.
        enum_fail: Option<i32>,
        /// Extra entries the enumeration reports: (path, dir, size).
        enum_extra: Vec<(String, bool, u64)>,
        /// Report this absolute path length for every enumerated entry.
        enum_len: Option<usize>,
        /// The decoded text of every PowerShell script the fake ran.
        enum_scripts: Vec<String>,
        /// The relative paths (`/`-separated) each enumeration returned, all
        /// roots of one call together, in call order.
        enum_rels: Vec<Vec<String>>,
        /// Report this native path length (without the `\\?\` prefix) for
        /// the enumerated entry at the fake path `.0`; `enum_len` still
        /// applies to every other entry.
        enum_len_for: Vec<(String, usize)>,
        /// A `rename` whose target ends with `.0` fails with `.1` this many
        /// more times: `Failure` or `Permission denied` per `.2`.
        rename_fails: Vec<(String, usize, &'static str)>,
        /// This many more `put`s leave a partial file at their target and then
        /// fail with `Permission denied`.
        put_fails: usize,
        /// Every `rm` fails with `Permission denied` and leaves the file.
        rm_denied: bool,
        /// Every `ssh` argv seen.
        ssh_calls: usize,
        /// Make this directory (and its parents) once the first `ssh` call
        /// ended: a folder made while a Windows read runs (quick 261104-s3n).
        appear_after_first_exec: Option<String>,
        /// Remove this path and everything below it once the first `ssh`
        /// call ended.
        vanish_after_first_exec: Option<String>,
        /// The PowerShell walk reports this size for the path ending with
        /// `.0`, whatever `get` delivers.
        walk_size_lie: Option<(String, u64)>,
        /// An 8.3 alias: `(short, long)`. Every path the client names under
        /// `short` is the same directory as under `long`, and a listing shows
        /// only the stored long names, as Win32-OpenSSH does (G2).
        alias: Option<(String, String)>,
        /// The bundle protocol (quick 261105-dl1). The free bytes BUNDLE
        /// reports on the archive's drive (default: plenty).
        bundle_free: Option<u64>,
        /// Store these bytes as the archive instead of the one BUNDLE builds,
        /// and report their length.
        bundle_archive: Option<Vec<u8>>,
        /// Answer this result after BUNDLE's normal effects.
        bundle_reply: Option<serde_json::Value>,
        /// BUNDLE exits with this code and CLIXML on stderr, leaving the list
        /// and a 3-byte partial archive, as a killed script would.
        bundle_fail: Option<i32>,
        /// BUNDLE_CLEAN exits 1 and leaves everything.
        clean_fail: bool,
        /// The bytes of every list BUNDLE read, in call order.
        bundle_lists: Vec<Vec<u8>>,
        /// How many times BUNDLE_CLEAN ran.
        clean_calls: usize,
    }

    struct FakeSftp {
        fs: Mutex<std::collections::BTreeMap<String, Node>>,
        listed: Mutex<HashSet<String>>,
        sessions: Mutex<Vec<Vec<String>>>,
        knobs: Mutex<Knobs>,
        /// Local files the server wrote.
        downloads: Mutex<Vec<PathBuf>>,
    }

    impl FakeSftp {
        fn new() -> Arc<FakeSftp> {
            let fake = FakeSftp {
                fs: Mutex::new(std::collections::BTreeMap::new()),
                listed: Mutex::new(HashSet::from([DATA.to_string(), HOME.to_string()])),
                sessions: Mutex::new(Vec::new()),
                knobs: Mutex::new(Knobs::default()),
                downloads: Mutex::new(Vec::new()),
            };
            fake.mkdirs(DATA);
            fake.mkdirs(HOME);
            Arc::new(fake)
        }

        fn mkdirs(&self, path: &str) {
            let mut fs = self.fs.lock().expect("fs");
            let mut cur = String::new();
            for part in path.split('/').skip(1) {
                cur.push('/');
                cur.push_str(part);
                fs.entry(cur.clone()).or_insert(Node::Dir);
            }
        }

        fn file(&self, path: &str, bytes: &str) {
            self.mkdirs(path.rsplit_once('/').expect("parent").0);
            self.fs
                .lock()
                .expect("fs")
                .insert(path.to_string(), Node::File(bytes.as_bytes().to_vec()));
        }

        fn put(&self, path: &str, node: Node) {
            self.mkdirs(path.rsplit_once('/').expect("parent").0);
            self.fs.lock().expect("fs").insert(path.to_string(), node);
        }

        fn knobs(&self) -> std::sync::MutexGuard<'_, Knobs> {
            self.knobs.lock().expect("knobs")
        }

        fn session_lines(&self) -> Vec<Vec<String>> {
            self.sessions.lock().expect("sessions").clone()
        }

        /// The stored path a client-typed `path` names (the 8.3 alias knob).
        fn resolve(&self, path: &str) -> String {
            if let Some((short, long)) = &self.knobs().alias
                && let Some(rest) = path.strip_prefix(short.as_str())
                && (rest.is_empty() || rest.starts_with('/'))
            {
                return format!("{long}{rest}");
            }
            path.to_string()
        }

        /// Apply one mutating batch line; `Err((class, stderr text))`.
        fn mutate(&self, l: &str, typed: &[String]) -> Result<(), (&'static str, String)> {
            let resolved: Vec<String> = typed.iter().map(|a| self.resolve(a)).collect();
            let args = &resolved[..];
            let parent_is_dir = |fs: &std::collections::BTreeMap<String, Node>, path: &str| {
                path.rsplit_once('/')
                    .is_some_and(|(parent, _)| matches!(fs.get(parent), Some(Node::Dir)))
            };
            let mut fs = self.fs.lock().expect("fs");
            if l.starts_with("mkdir ") {
                let path = &args[0];
                let shown = &typed[0];
                if !parent_is_dir(&fs, path) {
                    return Err((
                        "not_found",
                        format!("remote mkdir \"{shown}\": No such file or directory"),
                    ));
                }
                if fs.contains_key(path) {
                    return Err(("failure", format!("remote mkdir \"{shown}\": Failure")));
                }
                fs.insert(path.clone(), Node::Dir);
            } else if l.starts_with("put ") {
                let (local, path) = (&args[0], &args[1]);
                if !parent_is_dir(&fs, path) || matches!(fs.get(path), Some(Node::Dir)) {
                    return Err((
                        "not_found",
                        format!("dest open \"{path}\": No such file or directory"),
                    ));
                }
                {
                    let mut knobs = self.knobs();
                    if knobs.put_fails > 0 {
                        knobs.put_fails -= 1;
                        fs.insert(path.clone(), Node::File(b"par".to_vec()));
                        return Err(("denied", format!("dest open \"{path}\": Permission denied")));
                    }
                }
                let bytes = std::fs::read(local).expect("local put source");
                fs.insert(path.clone(), Node::File(bytes));
            } else if l.starts_with("rename ") {
                let (from, to) = (&args[0], &args[1]);
                assert_ne!(
                    from.to_ascii_lowercase(),
                    to.to_ascii_lowercase(),
                    "a rename onto itself reached the server: {l}"
                );
                {
                    let mut knobs = self.knobs();
                    if let Some(entry) = knobs
                        .rename_fails
                        .iter_mut()
                        .find(|(suffix, left, _)| *left > 0 && to.ends_with(suffix.as_str()))
                    {
                        entry.1 -= 1;
                        return Err((
                            "failure",
                            format!("remote rename \"{from}\" to \"{to}\": {}", entry.2),
                        ));
                    }
                }
                let Some(node) = fs.get(from).cloned() else {
                    return Err((
                        "not_found",
                        format!("remote rename \"{from}\" to \"{to}\": No such file or directory"),
                    ));
                };
                if !parent_is_dir(&fs, to) {
                    return Err((
                        "not_found",
                        format!("remote rename \"{from}\" to \"{to}\": No such file or directory"),
                    ));
                }
                if matches!(fs.get(to), Some(Node::Dir)) {
                    return Err((
                        "failure",
                        format!("remote rename \"{from}\" to \"{to}\": Failure"),
                    ));
                }
                fs.remove(from);
                fs.insert(to.clone(), node);
            } else if l.starts_with("rm ") {
                let path = &args[0];
                if self.knobs().rm_denied {
                    return Err(("denied", format!("remote delete {path}: Permission denied")));
                }
                if !matches!(fs.get(path), Some(Node::File(_))) {
                    return Err((
                        "not_found",
                        format!("remote delete {path}: No such file or directory"),
                    ));
                }
                fs.remove(path);
            } else if l.starts_with("rmdir ") {
                let path = &args[0];
                let prefix = format!("{path}/");
                if fs.keys().any(|k| k.starts_with(&prefix)) {
                    return Err(("failure", format!("remote rmdir \"{path}\": Failure")));
                }
                fs.remove(path);
            } else {
                panic!("unexpected mutating line {l}");
            }
            Ok(())
        }

        /// `ssh <opts> -- user@host powershell -EncodedCommand <b64>`: decode
        /// the script, take the roots from its parameter blob and answer the
        /// ENUMERATE script from the tree (or fail as a PowerShell would).
        fn exec(&self, args: &[OsString]) -> ProcessOutput {
            use base64::Engine as _;
            self.knobs().ssh_calls += 1;
            // the bundle protocol (quick 261105-dl1) is told apart by its
            // parameter keys; every other script is an enumeration
            if let Some(params) = exec_params(args) {
                if params.get("l").is_some() && params.get("z").is_some() {
                    return self.bundle_host(&params);
                }
                if params.get("d").is_some() && params.get("f").is_some() {
                    return self.clean_host(&params);
                }
            }
            if let Some(code) = self.knobs().enum_fail {
                return ProcessOutput {
                    stdout: Vec::new(),
                    stderr: b"#< CLIXML\r\n<Objs><S S=\"Error\">Access to the path is denied._x000D__x000A_</S></Objs>"
                        .to_vec(),
                    code,
                    timed_out: false,
                };
            }
            let last = args
                .last()
                .expect("remote command")
                .to_string_lossy()
                .into_owned();
            let b64 = last
                .strip_prefix("powershell -NoProfile -NonInteractive -EncodedCommand ")
                .expect("an encoded powershell command");
            let bytes = base64::engine::general_purpose::STANDARD
                .decode(b64)
                .expect("base64");
            let units: Vec<u16> = bytes
                .chunks(2)
                .map(|c| u16::from_le_bytes([c[0], c[1]]))
                .collect();
            let script = String::from_utf16(&units).expect("utf-16");
            self.knobs().enum_scripts.push(script.clone());
            // a script without the walk's stack lists direct children only,
            // as ENUMERATE_CHILDREN does on a host (pinned for real by the
            // winps pwsh tests)
            let recursive = script.contains("Collections.Stack");
            let mut call_rels: Vec<String> = Vec::new();
            let blob = script
                .split("FromBase64String('")
                .nth(1)
                .and_then(|r| r.split('\'').next())
                .expect("parameter blob");
            let params: serde_json::Value = serde_json::from_slice(
                &base64::engine::general_purpose::STANDARD
                    .decode(blob)
                    .expect("blob"),
            )
            .expect("params json");
            let prefix = params["x"].as_str().expect("prefix").to_string();
            assert_eq!(prefix, "\\\\?\\", "the long path prefix");
            let knobs_len = self.knobs().enum_len;
            let len_for = self.knobs().enum_len_for.clone();
            let reparse: Vec<String> = self.knobs().reparse.clone();
            let extra = self.knobs().enum_extra.clone();
            let size_lie = self.knobs().walk_size_lie.clone();
            let fs = self.fs.lock().expect("fs");
            let mut res = Vec::new();
            // the walk is the Windows read's listing: what it reports may be
            // named in a later `get` line
            let mut reported: Vec<String> = Vec::new();
            for (i, root) in params["r"].as_array().expect("roots").iter().enumerate() {
                let native = root.as_str().expect("root");
                assert!(
                    native.contains('\\') && !native.starts_with('/'),
                    "{native}"
                );
                let fake_root = format!("/{}", native.replace('\\', "/"));
                if !matches!(fs.get(&fake_root), Some(Node::Dir)) {
                    res.push(serde_json::json!({ "i": i, "x": 0, "e": [] }));
                    continue;
                }
                let below = format!("{fake_root}/");
                let mut entries = Vec::new();
                reported.push(fake_root.clone());
                for (path, node) in fs
                    .range(below.clone()..)
                    .take_while(|(path, _)| path.starts_with(&below))
                {
                    let Some(rel) = path.strip_prefix(&below) else {
                        continue;
                    };
                    if !recursive && rel.contains('/') {
                        continue;
                    }
                    call_rels.push(rel.to_string());
                    reported.push(path.clone());
                    let (dir, mut size) = match node {
                        Node::Dir => (1, 0),
                        Node::File(b) => (0, b.len() as u64),
                        _ => (0, 0),
                    };
                    if let Some((suffix, lie)) = &size_lie
                        && path.ends_with(suffix.as_str())
                    {
                        size = *lie;
                    }
                    // a Windows symlink is a reparse point
                    let flagged = i32::from(
                        reparse.iter().any(|r| r == path) || matches!(node, Node::Symlink),
                    );
                    let full = len_for
                        .iter()
                        .find(|(p, _)| p == path)
                        .map(|(_, n)| n + prefix.len())
                        .or(knobs_len)
                        .unwrap_or(native.len() + 1 + rel.len() + prefix.len());
                    entries.push(serde_json::json!([
                        rel.replace('/', "\\"),
                        dir,
                        size,
                        flagged,
                        full
                    ]));
                }
                for (path, dir, size) in &extra {
                    if let Some(rel) = path.strip_prefix(&below)
                        && (recursive || !rel.contains('/'))
                    {
                        call_rels.push(rel.to_string());
                        reported.push(path.clone());
                        entries.push(serde_json::json!([rel, i32::from(*dir), size, 0, 100]));
                    }
                }
                res.push(serde_json::json!({ "i": i, "x": 1, "e": entries }));
            }
            drop(fs);
            self.listed.lock().expect("listed").extend(reported);
            self.knobs().enum_rels.push(call_rels);
            let appear = self.knobs().appear_after_first_exec.take();
            if let Some(path) = appear {
                self.mkdirs(&path);
            }
            let vanish = self.knobs().vanish_after_first_exec.take();
            if let Some(path) = vanish {
                self.fs
                    .lock()
                    .expect("fs")
                    .retain(|k, _| k != &path && !k.starts_with(&format!("{path}/")));
            }
            let json = serde_json::json!({ "ok": 1, "res": res });
            ProcessOutput {
                stdout: format!("IGN-RESULT:{json}\r\n").into_bytes(),
                stderr: Vec::new(),
                code: 0,
                timed_out: false,
            }
        }

        /// The host side of BUNDLE (quick 261105-dl1, P4): read the uploaded
        /// list, check its SHA-256, the free space and every listed source,
        /// zip exactly the listed files into the archive path (CreateNew) and
        /// remove the list. Every refusal removes the list too.
        fn bundle_host(&self, params: &serde_json::Value) -> ProcessOutput {
            let text = |key: &str| {
                params[key]
                    .as_str()
                    .unwrap_or_else(|| panic!("bundle parameter {key}"))
                    .to_string()
            };
            assert_eq!(text("x"), "\\\\?\\", "the long path prefix");
            let base = fake_path(&text("b"));
            let list_path = fake_path(&text("l"));
            let zip_path = fake_path(&text("z"));
            let need = params["f"].as_u64().expect("free bytes needed");
            if let Some(code) = self.knobs().bundle_fail {
                // killed mid-build: the list and a partial archive stay
                self.put(&zip_path, Node::File(b"PK\x03".to_vec()));
                return clixml_failure(code);
            }
            let list = match self.fs.lock().expect("fs").get(&list_path) {
                Some(Node::File(bytes)) => bytes.clone(),
                _ => return clixml_failure(1),
            };
            self.knobs().bundle_lists.push(list.clone());
            let refuse = |value: serde_json::Value| {
                self.fs.lock().expect("fs").remove(&list_path);
                ign_line(&value)
            };
            if sha256_hex(&list) != text("h") {
                return refuse(serde_json::json!({ "ok": 0, "err": "list" }));
            }
            let free = self.knobs().bundle_free.unwrap_or(u64::MAX);
            if free < need {
                return refuse(serde_json::json!({ "ok": 0, "err": "space", "free": free }));
            }
            let mut entries: Vec<(String, Vec<u8>)> = Vec::new();
            let text_list = String::from_utf8(list).expect("the list is UTF-8");
            for (i, line) in text_list.split('\n').filter(|l| !l.is_empty()).enumerate() {
                let (size, rel) = line.split_once('\t').expect("size TAB name");
                let size: usize = size.parse().expect("size");
                let src = format!("{base}/{rel}");
                let node = self.fs.lock().expect("fs").get(&src).cloned();
                match node {
                    Some(Node::File(bytes)) if bytes.len() == size => {
                        entries.push((rel.to_string(), bytes));
                    }
                    Some(Node::File(bytes)) => {
                        // the real script stops one byte past the listed size
                        let n = bytes.len().min(size + 1);
                        return refuse(
                            serde_json::json!({ "ok": 0, "err": "changed", "i": i, "n": n }),
                        );
                    }
                    _ => return refuse(serde_json::json!({ "ok": 0, "err": "missing", "i": i })),
                }
            }
            if self.fs.lock().expect("fs").contains_key(&zip_path) {
                // CreateNew: an existing archive path is never written
                self.fs.lock().expect("fs").remove(&list_path);
                return clixml_failure(1);
            }
            let archive = self
                .knobs()
                .bundle_archive
                .clone()
                .unwrap_or_else(|| zip_at_level(&entries, Some(1)));
            let z = archive.len();
            {
                let mut fs = self.fs.lock().expect("fs");
                fs.insert(zip_path.clone(), Node::File(archive));
                fs.remove(&list_path);
            }
            self.listed.lock().expect("listed").insert(zip_path);
            let reply = self.knobs().bundle_reply.clone();
            ign_line(
                &reply
                    .unwrap_or_else(|| serde_json::json!({ "ok": 1, "n": entries.len(), "z": z })),
            )
        }

        /// The host side of BUNDLE_CLEAN (P5): remove each named regular file
        /// of the staging dir, then the dir when it is empty.
        fn clean_host(&self, params: &serde_json::Value) -> ProcessOutput {
            assert_eq!(
                params["x"].as_str(),
                Some("\\\\?\\"),
                "the long path prefix"
            );
            self.knobs().clean_calls += 1;
            if self.knobs().clean_fail {
                return clixml_failure(1);
            }
            let dir = fake_path(params["d"].as_str().expect("dir"));
            let names: Vec<String> = params["f"]
                .as_array()
                .expect("names")
                .iter()
                .map(|n| n.as_str().expect("name").to_string())
                .collect();
            let reparse = self.knobs().reparse.contains(&dir);
            let mut fs = self.fs.lock().expect("fs");
            match fs.get(&dir) {
                None => return ign_line(&serde_json::json!({ "ok": 1 })),
                Some(Node::Dir) if !reparse => {}
                Some(_) => return ign_line(&serde_json::json!({ "ok": 0, "err": "reparse" })),
            }
            for name in names {
                let path = format!("{dir}/{name}");
                if matches!(fs.get(&path), Some(Node::File(_))) {
                    fs.remove(&path);
                }
            }
            let below = format!("{dir}/");
            if fs.keys().any(|k| k.starts_with(&below)) {
                return ign_line(&serde_json::json!({ "ok": 0, "err": "left" }));
            }
            fs.remove(&dir);
            ign_line(&serde_json::json!({ "ok": 1 }))
        }

        fn children(&self, dir: &str) -> Vec<(String, Node)> {
            let prefix = format!("{dir}/");
            // every path below `dir` sorts after `{dir}/` and shares it, so a
            // range scan returns what a full scan would, fast at prod scale
            self.fs
                .lock()
                .expect("fs")
                .range(prefix.clone()..)
                .take_while(|(path, _)| path.starts_with(&prefix))
                .filter_map(|(path, node)| {
                    let rest = path.strip_prefix(&prefix)?;
                    (!rest.contains('/')).then(|| (rest.to_string(), node.clone()))
                })
                .collect()
        }
    }

    fn quoted(text: &str) -> Vec<String> {
        // the arguments of an sftp line: each is wrapped in double quotes
        text.split('"')
            .enumerate()
            .filter(|(i, _)| i % 2 == 1)
            .map(|(_, s)| s.to_string())
            .collect()
    }

    /// The decoded parameter blob of an `ssh ... powershell -EncodedCommand`
    /// argv, or `None` when it carries none.
    fn exec_params(args: &[OsString]) -> Option<serde_json::Value> {
        use base64::Engine as _;
        let last = args.last()?.to_string_lossy().into_owned();
        let b64 = last.strip_prefix("powershell -NoProfile -NonInteractive -EncodedCommand ")?;
        let bytes = base64::engine::general_purpose::STANDARD.decode(b64).ok()?;
        let units: Vec<u16> = bytes
            .chunks(2)
            .map(|c| u16::from_le_bytes([c[0], c.get(1).copied().unwrap_or(0)]))
            .collect();
        let script = String::from_utf16(&units).ok()?;
        let blob = script
            .split("FromBase64String('")
            .nth(1)?
            .split('\'')
            .next()?;
        serde_json::from_slice(
            &base64::engine::general_purpose::STANDARD
                .decode(blob)
                .ok()?,
        )
        .ok()
    }

    /// The fake path of a native Windows path (`C:\a\b` is `/C:/a/b`).
    fn fake_path(native: &str) -> String {
        assert!(
            native.contains('\\') && !native.starts_with('/'),
            "{native}"
        );
        format!("/{}", native.replace('\\', "/"))
    }

    /// A PowerShell that failed: exit `code`, an error record on stderr.
    fn clixml_failure(code: i32) -> ProcessOutput {
        ProcessOutput {
            stdout: Vec::new(),
            stderr:
                b"#< CLIXML\r\n<Objs><S S=\"Error\">The script failed._x000D__x000A_</S></Objs>"
                    .to_vec(),
            code,
            timed_out: false,
        }
    }

    /// A PowerShell that answered `value` on the result line.
    fn ign_line(value: &serde_json::Value) -> ProcessOutput {
        ProcessOutput {
            stdout: format!("IGN-RESULT:{value}\r\n").into_bytes(),
            stderr: Vec::new(),
            code: 0,
            timed_out: false,
        }
    }

    // ---- zip crafting for the bundle tests (quick 261105-dl1) --------------

    /// A Deflate archive of `entries`, shaped like the one Windows
    /// PowerShell 5.1 builds with `System.IO.Compression.ZipArchive` on .NET
    /// Framework: every central record is "made by" MS-DOS (host 0) with
    /// external attributes 0, so the zip crate reports no unix mode for it.
    fn zip_of(entries: &[(String, Vec<u8>)]) -> Vec<u8> {
        zip_at_level(entries, None)
    }

    /// [`zip_of`] at Deflate `level` (`None`: the crate's default, 6). The
    /// fake's BUNDLE uses level 1, not the host's `Optimal`: the offline
    /// reads need a valid Deflate archive, not its ratio, and level 1 moves
    /// the 48 MB tree several times faster in the debug profile
    /// (`prod_scale_pwsh_bundle` measures the real script's archive).
    fn zip_at_level(entries: &[(String, Vec<u8>)], level: Option<i64>) -> Vec<u8> {
        use std::io::Write as _;
        let mut writer = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
        let options = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Deflated)
            .compression_level(level);
        for (name, bytes) in entries {
            writer.start_file(name.as_str(), options).expect("entry");
            writer.write_all(bytes).expect("write");
        }
        let mut zip = writer.finish().expect("finish").into_inner();
        for record in zip_records(&zip) {
            zip[record.central + 5] = 0;
            zip[record.central + 38..record.central + 42].copy_from_slice(&[0; 4]);
        }
        zip
    }

    /// One central-directory record of a crafted archive.
    struct ZipRecord {
        /// Offset of the central record.
        central: usize,
        /// Offset of its local header.
        local: usize,
        name: Vec<u8>,
    }

    fn le16(zip: &[u8], at: usize) -> usize {
        usize::from(u16::from_le_bytes([zip[at], zip[at + 1]]))
    }

    fn le32(zip: &[u8], at: usize) -> usize {
        u32::from_le_bytes([zip[at], zip[at + 1], zip[at + 2], zip[at + 3]]) as usize
    }

    fn set32(zip: &mut [u8], at: usize, value: u32) {
        zip[at..at + 4].copy_from_slice(&value.to_le_bytes());
    }

    /// The end-of-central-directory record's offset.
    fn zip_eocd(zip: &[u8]) -> usize {
        (0..=zip.len() - 22)
            .rev()
            .find(|&i| zip[i..i + 4] == [0x50, 0x4b, 0x05, 0x06])
            .expect("an end of central directory record")
    }

    /// Every central-directory record, walked from the end record.
    fn zip_records(zip: &[u8]) -> Vec<ZipRecord> {
        let end = zip_eocd(zip);
        let count = le16(zip, end + 10);
        let mut at = le32(zip, end + 16);
        let mut out = Vec::new();
        for _ in 0..count {
            assert_eq!(zip[at..at + 4], [0x50, 0x4b, 0x01, 0x02], "central record");
            let name_len = le16(zip, at + 28);
            let next = at + 46 + name_len + le16(zip, at + 30) + le16(zip, at + 32);
            out.push(ZipRecord {
                central: at,
                local: le32(zip, at + 42),
                name: zip[at + 46..at + 46 + name_len].to_vec(),
            });
            at = next;
        }
        out
    }

    fn zip_record(zip: &[u8], name: &[u8]) -> ZipRecord {
        zip_records(zip)
            .into_iter()
            .find(|r| r.name == name)
            .unwrap_or_else(|| panic!("no entry {}", String::from_utf8_lossy(name)))
    }

    /// Rename `from` to `to` (the same length) in its local header and its
    /// central record.
    fn zip_rename(zip: &mut [u8], from: &[u8], to: &[u8]) {
        assert_eq!(from.len(), to.len(), "a same-length rename");
        let record = zip_record(zip, from);
        zip[record.central + 46..record.central + 46 + to.len()].copy_from_slice(to);
        zip[record.local + 30..record.local + 30 + to.len()].copy_from_slice(to);
    }

    /// Mark `name` as made by Unix with the full `mode` (type bits too).
    fn zip_set_mode(zip: &mut [u8], name: &[u8], mode: u32) {
        let record = zip_record(zip, name);
        zip[record.central + 5] = 3;
        set32(zip, record.central + 38, mode << 16);
    }

    /// Declare `size` uncompressed bytes for `name`, in both headers.
    fn zip_set_size(zip: &mut [u8], name: &[u8], size: u32) {
        let record = zip_record(zip, name);
        set32(zip, record.central + 24, size);
        set32(zip, record.local + 22, size);
    }

    /// Set general-purpose flag bit 0 (encrypted) for `name`, in both headers.
    fn zip_set_encrypted(zip: &mut [u8], name: &[u8]) {
        let record = zip_record(zip, name);
        zip[record.central + 8] |= 1;
        zip[record.local + 6] |= 1;
    }

    /// Set the compression method of `name`, in both headers.
    fn zip_set_method(zip: &mut [u8], name: &[u8], method: u16) {
        let record = zip_record(zip, name);
        zip[record.central + 10..record.central + 12].copy_from_slice(&method.to_le_bytes());
        zip[record.local + 8..record.local + 10].copy_from_slice(&method.to_le_bytes());
    }

    /// `junk` in front of the archive. With `fix_offsets` every offset is
    /// moved with it, so the archive is self-consistent and its first entry
    /// starts after the junk; without, the offsets are stale.
    fn zip_prepend(zip: &[u8], junk: &[u8], fix_offsets: bool) -> Vec<u8> {
        let records = zip_records(zip);
        let end = zip_eocd(zip);
        let mut out = junk.to_vec();
        out.extend_from_slice(zip);
        if fix_offsets {
            let n = junk.len();
            for record in records {
                let at = record.central + n + 42;
                let local = le32(&out, at);
                set32(&mut out, at, (local + n) as u32);
            }
            let at = end + n + 16;
            let start = le32(&out, at);
            set32(&mut out, at, (start + n) as u32);
        }
        out
    }

    #[async_trait::async_trait]
    impl ProcessRunner for FakeSftp {
        async fn run(
            &self,
            program: &OsStr,
            _args: &[OsString],
            stdin: Option<Vec<u8>>,
            _timeout: Duration,
        ) -> ProcessOutput {
            if program == "ssh" {
                return self.exec(_args);
            }
            assert_eq!(program, "sftp");
            let batch = String::from_utf8(stdin.expect("batch on stdin")).expect("utf8");
            let lines: Vec<String> = batch.lines().map(str::to_string).collect();
            self.sessions.lock().expect("sessions").push(lines.clone());
            if self.knobs().timeout {
                return ProcessOutput {
                    stdout: Vec::new(),
                    stderr: Vec::new(),
                    code: -1,
                    timed_out: true,
                };
            }
            let mut stdout = String::new();
            let mut stderr = String::new();
            let mut code = 0;
            let mut newly_listed: Vec<String> = Vec::new();
            for l in &lines {
                assert!(!l.starts_with('-'), "an error-suppressing line: {l}");
                if l == "@pwd" {
                    let home = self
                        .knobs()
                        .home
                        .clone()
                        .unwrap_or_else(|| HOME.to_string());
                    stdout.push_str(&format!("Remote working directory: {home}\n"));
                    continue;
                }
                let args = quoted(l);
                let remote = args.first().expect("a path").clone();
                let mutating = ["mkdir ", "put ", "rename ", "rm ", "rmdir "]
                    .iter()
                    .any(|c| l.starts_with(c));
                if !mutating {
                    assert!(
                        self.listed.lock().expect("listed").contains(&remote),
                        "{remote} is referenced before an earlier session listed it"
                    );
                } else {
                    stdout.push_str(&format!("sftp> {l}\n"));
                    if let Err((class, text)) = self.mutate(l, &args) {
                        stderr.push_str(&format!("{text}\n"));
                        let _ = class;
                        code = 1;
                        break;
                    }
                    // a path the client just created is known to it
                    if let Some(made) = args.last() {
                        newly_listed.push(made.clone());
                    }
                    continue;
                }
                let numeric = l.starts_with("@ls -lan \"");
                if numeric || l.starts_with("@ls -la \"") {
                    let stored = self.resolve(&remote);
                    let node = self.fs.lock().expect("fs").get(&stored).cloned();
                    if !matches!(node, Some(Node::Dir)) {
                        stderr.push_str(&format!("Can't ls: \"{remote}\" not found\n"));
                        code = 1;
                        break;
                    }
                    let windows = self.knobs().windows;
                    // `-n` makes the client format every line itself, the
                    // listed path in front of the name and the owner and
                    // group numeric, whatever the server. Without it the
                    // client prints the server's bare longname when the
                    // server lacks the users-groups extension: Win32-OpenSSH,
                    // and OpenSSH 8.9 (`bare_posix`, G1).
                    let bare = !numeric && (windows || self.knobs().bare_posix);
                    let row = |mode: &str, size: u64, name: &str| -> String {
                        let mode = if windows {
                            format!("{}******", &mode[..4])
                        } else {
                            mode.to_string()
                        };
                        if bare && windows {
                            format!("{mode}    1 -        - {size:>15} Oct 15 02:27 {name}")
                        } else if bare {
                            line(&mode, size, name)
                        } else if numeric {
                            format!(
                                "{mode}    ? 501      20     {size:>10} Oct 15 02:27 {remote}/{name}"
                            )
                        } else {
                            line(&mode, size, &format!("{remote}/{name}"))
                        }
                    };
                    if !windows {
                        stdout.push_str(&format!("{}\n", row("drwxr-xr-x", 96, ".")));
                        stdout.push_str(&format!("{}\n", row("drwxr-xr-x", 96, "..")));
                    }
                    for (name, node) in self.children(&stored) {
                        let child = format!("{remote}/{name}");
                        if self.knobs().hidden.contains(&child) {
                            continue;
                        }
                        let (mode, mut size) = match &node {
                            Node::Dir => ("drwxr-xr-x", 64),
                            Node::File(b) => ("-rw-r--r--", b.len() as u64),
                            Node::Symlink => ("lrwxr-xr-x", 5),
                            Node::Special => ("crw-r--r--", 0),
                        };
                        if let Some((path, lie)) = &self.knobs().lie_size
                            && child.ends_with(path.as_str())
                        {
                            size = *lie;
                        }
                        stdout.push_str(&format!("{}\n", row(mode, size, &name)));
                        newly_listed.push(child);
                    }
                } else if l.starts_with("get \"") {
                    if self.knobs().fail_get {
                        stderr.push_str(&format!("remote open(\"{remote}\"): No such file\n"));
                        code = 1;
                        break;
                    }
                    let node = self
                        .fs
                        .lock()
                        .expect("fs")
                        .get(&self.resolve(&remote))
                        .cloned();
                    let Some(Node::File(bytes)) = node else {
                        stderr.push_str(&format!("File \"{remote}\" not found.\n"));
                        code = 1;
                        break;
                    };
                    let skip = self.knobs().skip_download.clone();
                    if skip.as_deref().is_some_and(|s| remote.ends_with(s)) {
                        continue;
                    }
                    let dst = PathBuf::from(&args[1]);
                    std::fs::write(&dst, bytes).expect("write");
                    self.downloads.lock().expect("downloads").push(dst.clone());
                    let extra = self.knobs().extra_download.clone();
                    if let Some(extra) = extra {
                        std::fs::write(dst.parent().expect("parent").join(extra), b"surprise")
                            .expect("extra");
                    }
                    let overshoot = self.knobs().overshoot.clone();
                    if let Some((path, len)) = overshoot
                        && remote.ends_with(&path)
                    {
                        // a server that sends far more than it listed, then stalls
                        let file = std::fs::File::create(&dst).expect("create");
                        file.set_len(len).expect("sparse");
                        drop(file);
                        tokio::time::sleep(Duration::from_secs(30)).await;
                    }
                } else {
                    panic!("unexpected batch line {l}");
                }
            }
            self.listed.lock().expect("listed").extend(newly_listed);
            if self.knobs().stderr_noise {
                stderr.push_str("warning: something odd\n");
            }
            let vanish = self.knobs().vanish_after_first_session.take();
            if let Some(path) = vanish {
                self.fs
                    .lock()
                    .expect("fs")
                    .retain(|k, _| k != &path && !k.starts_with(&format!("{path}/")));
            }
            ProcessOutput {
                stdout: stdout.into_bytes(),
                stderr: stderr.into_bytes(),
                code,
                timed_out: false,
            }
        }
    }

    fn transport(fake: &Arc<FakeSftp>) -> SftpTransport {
        transport_at(fake, DATA)
    }

    fn transport_at(fake: &Arc<FakeSftp>, data_dir: &str) -> SftpTransport {
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        SftpTransport::new(shell, data_dir.to_string(), "nodes.master")
    }

    fn scope() -> Scope {
        Scope {
            mode: "production".to_string(),
            types: vec![("ignition".to_string(), "database-connection".to_string())],
            tag_providers: Vec::new(),
        }
    }

    fn standard_tree(fake: &FakeSftp) {
        fake.file(
            &format!("{RES}/core/ignition/database-connection/CoreDb/config.json"),
            "{\"core\":1}",
        );
        fake.file(
            &format!("{RES}/production/ignition/database-connection/MES db,1/config.json"),
            "{\"mes\":1}",
        );
        fake.file(
            &format!("{RES}/production/ignition/database-connection/MES db,1/resource.json"),
            "{\"scope\":\"A\"}",
        );
        fake.file(
            &format!("{RES}/production/ignition/database-connection/MES db,1/.dot"),
            "d",
        );
        fake.file(
            &format!("{RES}/local/ignition/database-connection/Override/config.json"),
            "{}",
        );
        // outside the scope: never inspected
        fake.put(
            &format!("{RES}/production/ignition/other-type"),
            Node::Symlink,
        );
    }

    fn rel(text: &str) -> RelPath {
        RelPath::new(text).expect("rel path")
    }

    fn detail(err: CoreError) -> String {
        match err {
            CoreError::TransportFailed {
                operation,
                node,
                detail,
            } => {
                // the transport's own failures say `read`; a batch the shell
                // refuses to render says `sftp`
                assert!(operation == "read" || operation == "sftp", "{operation}");
                assert!(node.starts_with("nodes."), "{node}");
                detail
            }
            other => panic!("expected transport_failed, got {other:?}"),
        }
    }

    // ---- read ----------------------------------------------------------------

    #[tokio::test]
    async fn read_collects_core_mode_and_local_units() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let t = transport(&fake);
        let observed = t.read(&scope()).await.expect("read");

        let core: Vec<&str> = observed.core.keys().map(RelPath::as_str).collect();
        assert_eq!(core, ["ignition/database-connection/CoreDb/config.json"]);
        let mode: Vec<&str> = observed.mode.keys().map(RelPath::as_str).collect();
        assert_eq!(
            mode,
            [
                "ignition/database-connection/MES db,1/.dot",
                "ignition/database-connection/MES db,1/config.json",
                "ignition/database-connection/MES db,1/resource.json",
            ]
        );
        let blob = &observed.mode[&rel("ignition/database-connection/MES db,1/config.json")];
        assert_eq!(blob.bytes, b"{\"mes\":1}");
        assert_eq!(blob.sha256, sha256_hex(b"{\"mes\":1}"));
        assert_eq!(
            observed.local_units,
            ["ignition/database-connection/Override"]
        );
        // data_dir, config, resources, collections, modules, types, the type
        // contents, the unit contents, then one download
        assert_eq!(t.sessions(), 8);
    }

    #[tokio::test]
    async fn read_over_a_bare_name_posix_server() {
        // a server without the users-groups extension (OpenSSH 8.9): `ls -lan`
        // makes the client format every line itself, so its bare `ls -la`
        // names never reach the parser
        let fake = FakeSftp::new();
        fake.knobs().bare_posix = true;
        standard_tree(&fake);
        let observed = transport(&fake).read(&scope()).await.expect("read");
        let mode: Vec<&str> = observed.mode.keys().map(RelPath::as_str).collect();
        assert_eq!(
            mode,
            [
                "ignition/database-connection/MES db,1/.dot",
                "ignition/database-connection/MES db,1/config.json",
                "ignition/database-connection/MES db,1/resource.json",
            ]
        );
        let lines: Vec<String> = fake.session_lines().into_iter().flatten().collect();
        let listings = lines.iter().filter(|l| l.starts_with("@ls -lan ")).count();
        let markers = lines.iter().filter(|l| *l == "@pwd").count();
        assert!(listings > 0 && markers == listings, "{lines:?}");
        assert!(
            !lines.iter().any(|l| l.starts_with("@ls -la ")),
            "a POSIX host is never listed with `ls -la`: {lines:?}"
        );
    }

    #[tokio::test]
    async fn prepare_over_a_bare_name_posix_server() {
        // the staging root below the account home is surveyed by listings,
        // the way `prepare` surveys it before it uploads
        let fake = FakeSftp::new();
        fake.knobs().bare_posix = true;
        fake.mkdirs("/home/svc/ign-deploy/gw/production");
        fake.listed
            .lock()
            .expect("listed")
            .extend(["/home".to_string(), "/home/svc".to_string()]);
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        let m = SftpTransport::new(shell, DATA.to_string(), "nodes.master")
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: "/home/svc/ign-deploy/gw/production/staging/run-1".to_string(),
                },
            )
            .expect("mirror");
        m.stage(&win_tree_of(&[(
            &format!("{DC}/Keep/config.json"),
            "{\"keep\":2}",
        )]))
        .await
        .expect("stage over bare names");
        let fs = fake.fs.lock().expect("fs");
        assert!(
            fs.keys().any(
                |k| k.ends_with("/staging/run-1/ignition/database-connection/Keep/config.json")
            ),
            "{:?}",
            fs.keys().collect::<Vec<_>>()
        );
    }

    // ---- G2: the survey of a staging root starts at the account home ----------

    const RUN: &str = "20261019T030000Z-aaaaaa";

    /// A local directory holding `a.json`, and its `put_tree` entry list.
    fn one_local_file() -> (tempfile::TempDir, Vec<(String, u64)>) {
        let local = tempfile::tempdir().expect("local dir");
        std::fs::write(local.path().join("a.json"), b"{}").expect("local file");
        (local, vec![("a.json".to_string(), 2)])
    }

    fn apply_transport(base: SftpTransport) -> SftpTransport {
        base.with_apply("gw", "production").expect("apply half")
    }

    fn session_lines_flat(fake: &FakeSftp) -> Vec<String> {
        fake.session_lines().into_iter().flatten().collect()
    }

    /// The directories a session listed (`@ls -lan "<dir>"` on a POSIX host,
    /// `@ls -la "<dir>"` on a Windows host) or made.
    fn listed_dirs(lines: &[String]) -> Vec<String> {
        lines
            .iter()
            .filter(|l| l.starts_with("@ls -lan \"") || l.starts_with("@ls -la \""))
            .map(|l| quoted(l).remove(0))
            .collect()
    }

    fn made_dirs(lines: &[String]) -> Vec<String> {
        lines
            .iter()
            .filter(|l| l.starts_with("mkdir \""))
            .map(|l| quoted(l).remove(0))
            .collect()
    }

    #[tokio::test]
    async fn prepare_below_a_short_name_home_makes_nothing_at_or_above_it() {
        // the windows-latest shape: the account home is given as
        // `C:\Users\RUNNER~1`, which the listing of `C:\Users` never shows
        let short = "/C:/Users/RUNNER~1";
        let fake = windows_fake();
        fake.knobs().alias = Some((short.to_string(), WHOME.to_string()));
        fake.listed
            .lock()
            .expect("listed")
            .insert(short.to_string());
        let t = apply_transport(windows_transport(&fake).with_home(short));
        let home = t.account_home().await.expect("the cached home");
        assert_eq!(home, short);
        let root = t.staging_abs(&home, RUN).expect("staging root");
        let (local, entries) = one_local_file();
        let made = t
            .put_tree("prepare", &root, local.path(), &entries, true)
            .await
            .expect("prepare below a short-name home");
        assert!(made, "the staging root is new");
        let lines = session_lines_flat(&fake);
        for dir in made_dirs(&lines) {
            assert!(
                dir.starts_with(&format!("{short}/")),
                "mkdir {dir} is at or above the home"
            );
        }
        for dir in listed_dirs(&lines) {
            assert!(
                dir == short || dir.starts_with(&format!("{short}/")),
                "listed {dir}, which is above the home"
            );
        }
        let fs = fake.fs.lock().expect("fs");
        assert!(
            fs.contains_key(&format!(
                "{WHOME}/ign-deploy/gw/production/staging/{RUN}/a.json"
            )),
            "{:?}",
            fs.keys().collect::<Vec<_>>()
        );
    }

    #[tokio::test]
    async fn prepare_lists_nothing_above_the_home_posix() {
        let fake = FakeSftp::new();
        let t = apply_transport(transport(&fake));
        // the home is asked once, as `apply_roots` does
        let home = t.account_home().await.expect("home");
        assert_eq!(home, HOME);
        let root = t.staging_abs(&home, RUN).expect("staging root");
        let (local, entries) = one_local_file();
        t.put_tree("prepare", &root, local.path(), &entries, true)
            .await
            .expect("prepare");
        let lines = session_lines_flat(&fake);
        let dirs = listed_dirs(&lines);
        assert_eq!(dirs.first().map(String::as_str), Some(HOME), "{dirs:?}");
        assert!(
            dirs.iter().all(|d| d.starts_with(HOME)),
            "a directory above the home was listed: {dirs:?}"
        );
        assert!(made_dirs(&lines).iter().all(|d| d.starts_with(HOME)));
    }

    #[tokio::test]
    async fn a_staging_root_outside_the_home_keeps_the_root_survey() {
        let fake = FakeSftp::new();
        fake.listed
            .lock()
            .expect("listed")
            .insert("/gw".to_string());
        let t = apply_transport(transport(&fake).with_home(HOME));
        let root = "/gw/stage/gw/production/staging/run-1";
        let (local, entries) = one_local_file();
        t.put_tree("prepare", root, local.path(), &entries, true)
            .await
            .expect("a root outside the home");
        let lines = session_lines_flat(&fake);
        assert_eq!(
            listed_dirs(&lines).first().map(String::as_str),
            Some("/gw"),
            "{lines:?}"
        );
        assert!(
            !listed_dirs(&lines).iter().any(|d| d == HOME),
            "the home plays no part"
        );
        assert!(
            fake.fs
                .lock()
                .expect("fs")
                .contains_key("/gw/stage/gw/production/staging/run-1/a.json")
        );
    }

    #[tokio::test]
    async fn a_symlink_below_the_home_is_still_refused() {
        let fake = FakeSftp::new();
        fake.put(&format!("{HOME}/ign-deploy"), Node::Symlink);
        let t = apply_transport(transport(&fake).with_home(HOME));
        let root = t.staging_abs(HOME, RUN).expect("staging root");
        let (local, entries) = one_local_file();
        let err = t
            .put_tree("prepare", &root, local.path(), &entries, true)
            .await
            .expect_err("a symlink at the first component below the home");
        let text = err.to_string();
        assert!(
            text.contains("is a symlink; symlinks are refused"),
            "{text}"
        );
        assert!(made_dirs(&session_lines_flat(&fake)).is_empty());
    }

    #[tokio::test]
    async fn mirror_stage_below_the_home_surveys_from_the_home() {
        let fake = FakeSftp::new();
        fake.mkdirs("/home/svc/ign-deploy/gw/production");
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        let m = SftpTransport::new(shell, DATA.to_string(), "nodes.master")
            .with_home(HOME)
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: "/home/svc/ign-deploy/gw/production/staging/run-1".to_string(),
                },
            )
            .expect("mirror");
        m.stage(&win_tree_of(&[(
            &format!("{DC}/Keep/config.json"),
            "{\"keep\":2}",
        )]))
        .await
        .expect("stage");
        let lines = session_lines_flat(&fake);
        let dirs = listed_dirs(&lines);
        assert_eq!(dirs.first().map(String::as_str), Some(HOME), "{dirs:?}");
        assert!(
            dirs.iter().all(|d| d.starts_with(HOME)),
            "a directory above the home was listed: {dirs:?}"
        );
    }

    // ---- WR-01: a fresh transport (as `mirror_for_node` builds) asks the home --

    /// `dir` is `home` or below it, on a component boundary.
    fn at_or_below(dir: &str, home: &str) -> bool {
        dir == home || dir.starts_with(&format!("{home}/"))
    }

    #[tokio::test]
    async fn mirror_stage_without_a_cached_home_surveys_from_the_short_name_home() {
        // `mirror_for_node` builds a fresh transport: nothing has asked the
        // home, and the server's `pwd` answers with the 8.3 alias, which the
        // listing of `C:\Users` never shows (G2 on the mirror path)
        let short = "/C:/Users/RUNNER~1";
        let fake = windows_fake();
        fake.knobs().alias = Some((short.to_string(), WHOME.to_string()));
        fake.knobs().home = Some(short.to_string());
        fake.listed
            .lock()
            .expect("listed")
            .insert(short.to_string());
        let m = windows_transport(&fake)
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: format!("{short}/ign-deploy/gw/production/staging/run-1"),
                },
            )
            .expect("mirror");
        m.stage(&win_tree_of(&[(
            &format!("{DC}/Keep/config.json"),
            "{\"keep\":2}",
        )]))
        .await
        .expect("stage below a short-name home");
        let lines = session_lines_flat(&fake);
        assert!(
            lines.iter().any(|l| l == "@pwd"),
            "the home is asked: {lines:?}"
        );
        for dir in made_dirs(&lines) {
            assert!(
                dir.starts_with(&format!("{short}/")),
                "mkdir {dir} is at or above the home"
            );
        }
        for dir in listed_dirs(&lines) {
            assert!(
                at_or_below(&dir, short),
                "listed {dir}, which is above the home"
            );
        }
        assert!(fake.fs.lock().expect("fs").contains_key(&format!(
            "{WHOME}/ign-deploy/gw/production/staging/run-1/{DC}/Keep/config.json"
        )));
    }

    #[tokio::test]
    async fn mirror_stage_without_a_cached_home_lists_nothing_above_the_home_posix() {
        let fake = FakeSftp::new();
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        let m = SftpTransport::new(shell, DATA.to_string(), "nodes.master")
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: "/home/svc/ign-deploy/gw/production/staging/run-1".to_string(),
                },
            )
            .expect("mirror");
        m.stage(&win_tree_of(&[(
            &format!("{DC}/Keep/config.json"),
            "{\"keep\":2}",
        )]))
        .await
        .expect("stage");
        let lines = session_lines_flat(&fake);
        let dirs = listed_dirs(&lines);
        assert_eq!(dirs.first().map(String::as_str), Some(HOME), "{dirs:?}");
        assert!(
            dirs.iter().all(|d| at_or_below(d, HOME)),
            "a directory above the home was listed: {dirs:?}"
        );
        assert!(
            made_dirs(&lines)
                .iter()
                .all(|d| d.starts_with(&format!("{HOME}/"))),
            "{lines:?}"
        );
    }

    #[tokio::test]
    async fn put_tree_without_a_cached_home_surveys_from_the_short_name_home() {
        let short = "/C:/Users/RUNNER~1";
        let fake = windows_fake();
        fake.knobs().alias = Some((short.to_string(), WHOME.to_string()));
        fake.knobs().home = Some(short.to_string());
        fake.listed
            .lock()
            .expect("listed")
            .insert(short.to_string());
        let t = apply_transport(windows_transport(&fake));
        let root = t.staging_abs(short, RUN).expect("staging root");
        let (local, entries) = one_local_file();
        let made = t
            .put_tree("prepare", &root, local.path(), &entries, true)
            .await
            .expect("put_tree below a short-name home, nothing cached");
        assert!(made, "the staging root is new");
        let lines = session_lines_flat(&fake);
        for dir in listed_dirs(&lines) {
            assert!(
                at_or_below(&dir, short),
                "listed {dir}, which is above the home"
            );
        }
        for dir in made_dirs(&lines) {
            assert!(
                dir.starts_with(&format!("{short}/")),
                "mkdir {dir} is at or above the home"
            );
        }
    }

    #[tokio::test]
    async fn mirror_stage_with_an_unreadable_home_is_transport_failed() {
        // the home the server reports does not exist: its listing fails
        let fake = FakeSftp::new();
        fake.knobs().home = Some("/home/nope".to_string());
        fake.listed
            .lock()
            .expect("listed")
            .insert("/home/nope".to_string());
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        let m = SftpTransport::new(shell, DATA.to_string(), "nodes.master")
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: "/home/nope/ign-deploy/gw/production/staging/run-1".to_string(),
                },
            )
            .expect("mirror");
        let err = m
            .stage(&win_tree_of(&[(
                &format!("{DC}/Keep/config.json"),
                "{\"keep\":2}",
            )]))
            .await
            .expect_err("an unreadable home");
        assert!(matches!(err, CoreError::TransportFailed { .. }), "{err:?}");
        let lines = session_lines_flat(&fake);
        assert!(made_dirs(&lines).is_empty(), "{lines:?}");
        assert!(
            !listed_dirs(&lines).iter().any(|d| d == "/home"),
            "the home's parent is never listed: {lines:?}"
        );
    }

    // ---- WR-02: the home match follows the host's case rule -------------------

    #[tokio::test]
    async fn mirror_stage_below_a_windows_home_spelled_in_another_case() {
        // `/c:/users/SVC` is the home `/C:/Users/svc` on a Windows host: the
        // survey starts at the home, in the server's spelling
        let fake = windows_fake();
        // the fake's paths are case-sensitive; NTFS is not. The uploads name
        // the root as the operator spelled it, so fold that spelling onto the
        // stored one, as the host would
        fake.knobs().alias = Some(("/c:/users/SVC".to_string(), WHOME.to_string()));
        let m = windows_transport(&fake)
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: "/c:/users/SVC/ign-deploy/gw/production/staging/run-1".to_string(),
                },
            )
            .expect("mirror");
        m.stage(&win_tree_of(&[(
            &format!("{DC}/Keep/config.json"),
            "{\"keep\":2}",
        )]))
        .await
        .expect("stage below a home spelled in another case");
        let lines = session_lines_flat(&fake);
        let dirs = listed_dirs(&lines);
        assert_eq!(dirs.first().map(String::as_str), Some(WHOME), "{dirs:?}");
        for dir in &dirs {
            assert!(
                at_or_below(dir, WHOME),
                "listed {dir}, which is above the home"
            );
        }
        for dir in made_dirs(&lines) {
            assert!(
                dir.starts_with(&format!("{WHOME}/")),
                "mkdir {dir} is not below the home's own spelling"
            );
        }
        assert!(fake.fs.lock().expect("fs").contains_key(&format!(
            "{WHOME}/ign-deploy/gw/production/staging/run-1/{DC}/Keep/config.json"
        )));
    }

    #[tokio::test]
    async fn survey_root_home_match_follows_the_host_case_rule() {
        let fake = windows_fake();
        let t = windows_transport(&fake);
        let (base, rest) = t
            .survey_root("stage", "/c:/USERS/Svc/ign-deploy/run-1")
            .await
            .expect("windows root");
        assert_eq!(base, WHOME, "the base keeps the server's spelling");
        assert_eq!(rest, ["ign-deploy", "run-1"]);

        // a POSIX host is case-sensitive: `/home/SVC` is not the home
        let fake = FakeSftp::new();
        let t = transport(&fake);
        let (base, rest) = t
            .survey_root("stage", "/home/SVC/ign-deploy/run-1")
            .await
            .expect("posix root");
        assert_eq!(base, "/home");
        assert_eq!(rest, ["SVC", "ign-deploy", "run-1"]);
    }

    #[tokio::test]
    async fn survey_root_another_8dot3_spelling_of_the_home_falls_back() {
        // an 8.3 alias and its long name cannot be told apart offline: a root
        // in the other spelling than the home's is surveyed from the drive
        // root, as a root outside the home is (pinned, WR-02)
        let short = "/C:/Users/RUNNER~1";
        let fake = windows_fake();
        fake.knobs().home = Some(short.to_string());
        let t = windows_transport(&fake);
        let (base, rest) = t
            .survey_root("stage", "/C:/Users/svc/ign-deploy/run-1")
            .await
            .expect("long spelling, short home");
        assert_eq!(base, "/C:");
        assert_eq!(rest, ["Users", "svc", "ign-deploy", "run-1"]);

        let fake = windows_fake();
        let t = windows_transport(&fake);
        let (base, rest) = t
            .survey_root("stage", &format!("{short}/ign-deploy/run-1"))
            .await
            .expect("short spelling, long home");
        assert_eq!(base, "/C:");
        assert_eq!(rest, ["Users", "RUNNER~1", "ign-deploy", "run-1"]);
    }

    #[tokio::test]
    async fn batches_never_suppress_errors() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        transport(&fake).read(&scope()).await.expect("read");
        let lines: Vec<String> = fake.session_lines().into_iter().flatten().collect();
        assert!(!lines.is_empty());
        for l in &lines {
            assert!(
                l == "@pwd" || l.starts_with("@ls -lan \"") || l.starts_with("get \""),
                "only markers, listings and single-file gets, never `get -R`: {l}"
            );
            assert!(!l.starts_with('-'), "{l}");
        }
        // the fake itself asserts that no batch line was sent before an earlier
        // session listed its path (batches_only_reference_listed_paths)
    }

    #[tokio::test]
    async fn batches_only_reference_listed_paths() {
        // The fake panics when a batch names a path no earlier session listed.
        // A scope with tag providers exercises the longest descent.
        let fake = FakeSftp::new();
        standard_tree(&fake);
        fake.file(
            &format!("{RES}/production/ignition/tag-definition/default/Folder/tags.json"),
            "{}",
        );
        fake.file(
            &format!("{RES}/core/ignition/tag-definition/default/tags.json"),
            "{}",
        );
        let mut s = scope();
        s.tag_providers = vec!["default".to_string(), "absent".to_string()];
        let observed = transport(&fake).read(&s).await.expect("read");
        assert!(
            observed
                .mode
                .contains_key(&rel("ignition/tag-definition/default/Folder/tags.json"))
        );
        assert!(
            observed
                .core
                .contains_key(&rel("ignition/tag-definition/default/tags.json"))
        );
        // the absent provider and the absent tag types are simply not read
        let everything: String = fake.session_lines().into_iter().flatten().collect();
        assert!(!everything.contains("absent"), "{everything}");
    }

    #[tokio::test]
    async fn absent_collection_and_type_read_as_empty() {
        let fake = FakeSftp::new();
        fake.mkdirs(RES);
        let observed = transport(&fake).read(&scope()).await.expect("read");
        assert!(observed.core.is_empty() && observed.mode.is_empty());
        assert!(observed.local_units.is_empty());

        // a mode collection that exists without the scoped type
        fake.mkdirs(&format!("{RES}/production/ignition"));
        let observed = transport(&fake).read(&scope()).await.expect("read");
        assert!(observed.mode.is_empty());
    }

    #[tokio::test]
    async fn fake_timeout_is_transport_failed() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        fake.knobs().timeout = true;
        let why = detail(transport(&fake).read(&scope()).await.expect_err("timeout"));
        assert!(why.contains("timed out"), "{why}");
        assert_eq!(fake.session_lines().len(), 1, "nothing ran after a timeout");
    }

    #[tokio::test]
    async fn fake_stderr_is_transport_failed() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        fake.knobs().stderr_noise = true;
        let why = detail(transport(&fake).read(&scope()).await.expect_err("stderr"));
        assert!(why.contains("unexpected stderr output"), "{why}");
    }

    #[tokio::test]
    async fn missing_data_dir_is_transport_failed_not_empty() {
        let fake = FakeSftp::new();
        let t = transport_at(&fake, "/gw/nope");
        // the fake panics on an unlisted path, so give it the path as listed
        fake.listed
            .lock()
            .expect("listed")
            .insert("/gw/nope".to_string());
        let why = detail(t.read(&scope()).await.expect_err("missing data dir"));
        assert!(why.contains("exit code 1"), "{why}");
    }

    #[tokio::test]
    async fn vanished_dir_is_transport_failed() {
        // the type directory is listed, then it disappears before the next
        // session touches it
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let target = format!("{RES}/production/ignition/database-connection");
        // sessions: 1 data, 2 config, 3 resources, 4 collections, 5 modules,
        // 6 types. The type dir vanishes after session 6, before session 7
        // lists its contents.
        // script the vanish: remove the dir once the type level was listed
        struct Vanisher {
            inner: Arc<FakeSftp>,
            target: String,
            after: usize,
        }
        #[async_trait::async_trait]
        impl ProcessRunner for Vanisher {
            async fn run(
                &self,
                program: &OsStr,
                args: &[OsString],
                stdin: Option<Vec<u8>>,
                timeout: Duration,
            ) -> ProcessOutput {
                let out = self.inner.run(program, args, stdin, timeout).await;
                if self.inner.session_lines().len() == self.after {
                    let t = &self.target;
                    self.inner
                        .fs
                        .lock()
                        .expect("fs")
                        .retain(|k, _| k != t && !k.starts_with(&format!("{t}/")));
                }
                out
            }
        }
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused"),
            Arc::new(Vanisher {
                inner: fake.clone(),
                target,
                after: 6,
            }),
            Duration::from_secs(30),
        );
        let t = SftpTransport::new(shell, DATA.to_string(), "nodes.master");
        let why = detail(t.read(&scope()).await.expect_err("vanished"));
        assert!(why.contains("exit code 1"), "{why}");
        assert!(why.contains("not found"), "{why}");
    }

    #[tokio::test]
    async fn symlinks_and_special_files_are_refused_not_skipped() {
        for node in [Node::Symlink, Node::Special] {
            let fake = FakeSftp::new();
            standard_tree(&fake);
            fake.put(
                &format!("{RES}/production/ignition/database-connection/MES db,1/lnk"),
                node.clone(),
            );
            let why = detail(transport(&fake).read(&scope()).await.expect_err("refused"));
            assert!(
                why.contains(
                    "config/resources/production/ignition/database-connection/MES db,1/lnk"
                ),
                "{why}"
            );
            if matches!(node, Node::Symlink) {
                assert!(why.contains("is a symlink; symlinks are refused"), "{why}");
            } else {
                assert!(why.contains("is not a regular file or directory"), "{why}");
            }
            // refused from the listing, before any download
            // (a listing session is `@pwd` markers and `@ls -lan` lines)
            let last = fake.session_lines().pop().expect("session");
            assert!(
                last.iter()
                    .all(|l| l == "@pwd" || l.starts_with("@ls -lan \"")),
                "{last:?}"
            );
        }
    }

    #[tokio::test]
    async fn symlinked_scope_path_components_are_refused() {
        // a symlink where config, resources, a collection, a module or a type
        // should be a directory
        for path in [
            "config",
            "config/resources",
            "config/resources/production",
            "config/resources/production/ignition",
            "config/resources/production/ignition/database-connection",
        ] {
            let fake = FakeSftp::new();
            standard_tree(&fake);
            let full = format!("{DATA}/{path}");
            fake.fs
                .lock()
                .expect("fs")
                .retain(|k, _| k != &full && !k.starts_with(&format!("{full}/")));
            fake.put(&full, Node::Symlink);
            let why = detail(transport(&fake).read(&scope()).await.expect_err(path));
            assert!(why.contains(path), "{path}: {why}");
            assert!(why.contains("symlink"), "{path}: {why}");
        }
        // a file where a directory belongs
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let full = format!("{RES}/production/ignition");
        fake.fs
            .lock()
            .expect("fs")
            .retain(|k, _| k != &full && !k.starts_with(&format!("{full}/")));
        fake.file(&full, "x");
        let why = detail(transport(&fake).read(&scope()).await.expect_err("file"));
        assert!(why.contains("is not a directory"), "{why}");
    }

    #[tokio::test]
    async fn unsafe_names_are_refused() {
        for name in ["odd[1]", "a\\b", "q\"uote", "g*lob"] {
            let fake = FakeSftp::new();
            standard_tree(&fake);
            fake.file(
                &format!("{RES}/production/ignition/database-connection/{name}/config.json"),
                "{}",
            );
            let why = detail(transport(&fake).read(&scope()).await.expect_err(name));
            assert!(why.contains("unsafe"), "{name}: {why}");
        }
    }

    #[tokio::test]
    async fn caps_are_enforced_from_the_listing_before_any_download() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        fake.knobs().lie_size = Some(("MES db,1/config.json".to_string(), READ_MAX_FILE_BYTES + 1));
        let why = detail(transport(&fake).read(&scope()).await.expect_err("file cap"));
        assert!(why.contains("byte file cap"), "{why}");
        let last = fake.session_lines().pop().expect("session");
        assert!(
            !last[0].starts_with("get"),
            "no download happened: {last:?}"
        );

        // the total cap: 17 files that each fit the file cap (64 MiB), together
        // over 1 GiB
        let fake = FakeSftp::new();
        for n in 0..17 {
            fake.file(
                &format!("{RES}/production/ignition/database-connection/D{n:02}/config.json"),
                "{}",
            );
        }
        fake.knobs().lie_size = Some(("config.json".to_string(), READ_MAX_FILE_BYTES));
        let why = detail(
            transport(&fake)
                .read(&scope())
                .await
                .expect_err("total cap"),
        );
        assert!(why.contains("in total"), "{why}");
        let last = fake.session_lines().pop().expect("session");
        assert!(
            !last[0].starts_with("get"),
            "no download happened: {last:?}"
        );
    }

    #[tokio::test]
    async fn depth_is_bounded() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let mut deep = format!("{RES}/production/ignition/database-connection/MES db,1");
        for _ in 0..READ_MAX_DEPTH {
            deep.push_str("/d");
        }
        fake.file(&format!("{deep}/f"), "x");
        let why = detail(transport(&fake).read(&scope()).await.expect_err("deep"));
        assert!(why.contains("deeper than"), "{why}");
    }

    #[tokio::test]
    async fn download_that_disagrees_with_the_listing_is_transport_failed() {
        // a listed type directory is missing locally after the download
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let t = transport(&fake);
        // a file that listed but did not come down
        fake.knobs().skip_download = Some("MES db,1/resource.json".to_string());
        let why = detail(t.read(&scope()).await.expect_err("missing file"));
        assert!(why.contains("was listed but not downloaded"), "{why}");
        fake.knobs().skip_download = None;

        // a file that came down but was never listed
        fake.knobs().extra_download = Some("surprise.json".to_string());
        let why = detail(t.read(&scope()).await.expect_err("extra file"));
        assert!(why.contains("appeared in the download"), "{why}");
        fake.knobs().extra_download = None;

        // a file whose size differs from its listing
        fake.knobs().lie_size = Some(("MES db,1/config.json".to_string(), 3));
        let why = detail(t.read(&scope()).await.expect_err("size"));
        assert!(why.contains("was listed as 3"), "{why}");
    }

    #[tokio::test]
    async fn downloaded_files_missing_locally_are_transport_failed() {
        // the sftp client exits 0 but wrote nothing for the listed files
        struct Silent(Arc<FakeSftp>);
        #[async_trait::async_trait]
        impl ProcessRunner for Silent {
            async fn run(
                &self,
                program: &OsStr,
                args: &[OsString],
                stdin: Option<Vec<u8>>,
                timeout: Duration,
            ) -> ProcessOutput {
                let batch =
                    String::from_utf8_lossy(stdin.as_deref().unwrap_or_default()).to_string();
                if batch.contains("get \"") {
                    return ProcessOutput {
                        stdout: Vec::new(),
                        stderr: Vec::new(),
                        code: 0,
                        timed_out: false,
                    };
                }
                self.0.run(program, args, stdin, timeout).await
            }
        }
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused"),
            Arc::new(Silent(fake.clone())),
            Duration::from_secs(30),
        );
        let t = SftpTransport::new(shell, DATA.to_string(), "nodes.master");
        let why = detail(t.read(&scope()).await.expect_err("missing files"));
        assert!(why.contains("was listed but not downloaded"), "{why}");
    }

    #[tokio::test]
    async fn get_failure_is_transport_failed() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        fake.knobs().fail_get = true;
        let why = detail(transport(&fake).read(&scope()).await.expect_err("get"));
        assert!(why.contains("exit code 1"), "{why}");
    }

    #[tokio::test]
    async fn hostile_scope_values_are_refused_before_any_session() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let t = transport(&fake);
        let mut bad_type = scope();
        bad_type.types = vec![("ignition".to_string(), "x\"; rm".to_string())];
        let mut bad_module = scope();
        bad_module.types = vec![("../etc".to_string(), "t".to_string())];
        let mut bad_provider = scope();
        bad_provider.tag_providers = vec!["p[1]".to_string()];
        let mut bad_mode = scope();
        bad_mode.mode = "prod/../x".to_string();
        for s in [bad_type, bad_module, bad_provider, bad_mode] {
            detail(t.read(&s).await.expect_err("refused"));
        }
        assert!(fake.session_lines().is_empty(), "no process was spawned");
    }

    #[tokio::test]
    async fn glob_data_dir_is_refused_before_any_session() {
        let fake = FakeSftp::new();
        let t = transport_at(&fake, "/gw/da[ta]");
        detail(t.read(&scope()).await.expect_err("glob"));
        detail(t.list_types("core").await.expect_err("glob"));
        assert!(fake.session_lines().is_empty(), "no process was spawned");
        // the ledger lives in the account home (Decision B(i)), not below
        // the data directory, so it does not depend on the data directory
        assert_eq!(
            t.read_ledger("gw", "production")
                .await
                .expect("home ledger"),
            None
        );
    }

    // ---- list_types ----------------------------------------------------------

    #[tokio::test]
    async fn list_types_lists_module_type_pairs() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        fake.file(
            &format!("{RES}/core/ignition/alarm-journal/a/config.json"),
            "{}",
        );
        fake.file(&format!("{RES}/core/ignition/loose.json"), "{}");
        fake.file(&format!("{RES}/core/zeta/thing/x"), "{}");
        let t = transport(&fake);
        assert_eq!(
            t.list_types("core").await.expect("types"),
            [
                ("ignition".to_string(), "alarm-journal".to_string()),
                ("ignition".to_string(), "database-connection".to_string()),
                ("zeta".to_string(), "thing".to_string()),
            ]
        );
        assert!(t.list_types("nothing").await.expect("absent").is_empty());
    }

    // ---- list_tree ---------------------------------------------------------------

    /// quick 261104-7cy, C3: every file and folder below a type folder,
    /// byte-sorted, the folder itself not an entry; an absent folder is left
    /// out; nothing is downloaded; a symlink below is refused.
    #[tokio::test]
    async fn list_tree_lists_files_and_folders_of_a_type_folder() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        let ty = format!("{RES}/production/ignition/tag-definition");
        fake.file(&format!("{ty}/default/Deploy/tags.json"), "{}");
        fake.mkdirs(&format!("{ty}/default/Empty"));
        fake.file(&format!("{ty}/loose.json"), "{}");
        let t = transport(&fake);
        let dirs = [
            ("ignition".to_string(), "tag-type-definition".to_string()),
            ("ignition".to_string(), "tag-definition".to_string()),
        ];
        let listed = t.list_tree("production", &dirs).await.expect("listed");
        assert_eq!(listed.len(), 1, "the absent folder is left out: {listed:?}");
        assert_eq!(
            (listed[0].module.as_str(), listed[0].ty.as_str()),
            ("ignition", "tag-definition")
        );
        let got: Vec<(&str, bool)> = listed[0]
            .entries
            .iter()
            .map(|e| (e.path.as_str(), e.dir))
            .collect();
        assert_eq!(
            got,
            [
                ("ignition/tag-definition/default", true),
                ("ignition/tag-definition/default/Deploy", true),
                ("ignition/tag-definition/default/Deploy/tags.json", false),
                ("ignition/tag-definition/default/Empty", true),
                ("ignition/tag-definition/loose.json", false),
            ]
        );
        assert!(fake.downloads.lock().expect("downloads").is_empty());
        assert!(
            t.list_tree("nothing", &dirs)
                .await
                .expect("absent")
                .is_empty()
        );
        assert!(
            t.list_tree("a/b", &dirs).await.is_err(),
            "unsafe collection"
        );

        fake.put(&format!("{ty}/default/link"), Node::Symlink);
        let err = t
            .list_tree("production", &dirs)
            .await
            .expect_err("a symlink below is refused");
        assert!(err.to_string().contains("symlink"), "{err}");
    }

    // ---- list_tag_providers ----------------------------------------------------

    #[tokio::test]
    async fn list_tag_providers_lists_provider_directory_names() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        fake.file(
            &format!("{RES}/core/ignition/tag-provider/zeta/config.json"),
            "{}",
        );
        fake.file(
            &format!("{RES}/core/ignition/tag-provider/MQTT Engine/config.json"),
            "{}",
        );
        fake.file(
            &format!("{RES}/core/ignition/tag-provider/loose.json"),
            "{}",
        );
        fake.mkdirs(&format!("{RES}/core/ignition/tag-provider/Empty"));
        let t = transport(&fake);
        assert_eq!(
            t.list_tag_providers("core").await.expect("providers"),
            ["Empty", "MQTT Engine", "zeta"]
        );
        // a collection without the directory, and no collection at all
        assert!(
            t.list_tag_providers("production")
                .await
                .expect("absent")
                .is_empty()
        );
        assert!(
            t.list_tag_providers("nothing")
                .await
                .expect("absent")
                .is_empty()
        );
        // names only: nothing was downloaded
        assert!(fake.downloads.lock().expect("downloads").is_empty());

        fake.put(
            &format!("{RES}/core/ignition/tag-provider/link"),
            Node::Symlink,
        );
        let err = t.list_tag_providers("core").await.expect_err("symlink");
        assert!(err.to_string().contains("symlink"), "{err}");
        assert!(
            t.list_tag_providers("a/b").await.is_err(),
            "unsafe collection"
        );
    }

    // ---- read_ledger -----------------------------------------------------------

    /// Decision B(i): an ssh node's ledger is below the account home.
    fn ledger_path() -> String {
        format!("{HOME}/ign-deploy/gw/production/{LEDGER_FILE}")
    }

    #[tokio::test]
    async fn absent_ledger_at_any_level_is_none() {
        let fake = FakeSftp::new();
        let t = transport(&fake);
        assert_eq!(t.read_ledger("gw", "production").await.expect("none"), None);
        fake.mkdirs(&format!("{HOME}/ign-deploy"));
        assert_eq!(t.read_ledger("gw", "production").await.expect("none"), None);
        fake.mkdirs(&format!("{HOME}/ign-deploy/gw/production"));
        assert_eq!(t.read_ledger("gw", "production").await.expect("none"), None);
    }

    #[tokio::test]
    async fn ledger_present_round_trips() {
        let fake = FakeSftp::new();
        fake.file(&ledger_path(), "{\"schema_version\":1}");
        let t = transport(&fake);
        assert_eq!(
            t.read_ledger("gw", "production").await.expect("ledger"),
            Some(b"{\"schema_version\":1}".to_vec())
        );
    }

    #[tokio::test]
    async fn ledger_anomalies_are_deploy_ledger_invalid() {
        let invalid = |err: CoreError| match err {
            CoreError::DeployLedgerInvalid { problem, .. } => problem,
            other => panic!("expected deploy_ledger_invalid, got {other:?}"),
        };
        // the file is a symlink
        let fake = FakeSftp::new();
        fake.put(&ledger_path(), Node::Symlink);
        let p = invalid(
            transport(&fake)
                .read_ledger("gw", "production")
                .await
                .expect_err("link"),
        );
        assert!(p.contains("symlink"), "{p}");
        // a directory in the middle is a symlink
        let fake = FakeSftp::new();
        fake.put(&format!("{HOME}/ign-deploy/gw"), Node::Symlink);
        let p = invalid(
            transport(&fake)
                .read_ledger("gw", "production")
                .await
                .expect_err("link"),
        );
        assert!(p.contains("symlink"), "{p}");
        // a component that is a file
        let fake = FakeSftp::new();
        fake.file(&format!("{HOME}/ign-deploy/gw"), "x");
        let p = invalid(
            transport(&fake)
                .read_ledger("gw", "production")
                .await
                .expect_err("file"),
        );
        assert!(p.contains("not a directory"), "{p}");
        // the ledger is a directory
        let fake = FakeSftp::new();
        fake.mkdirs(&ledger_path());
        let p = invalid(
            transport(&fake)
                .read_ledger("gw", "production")
                .await
                .expect_err("dir"),
        );
        assert!(p.contains("not a regular file"), "{p}");
        // over the cap, from the listing, before a download
        let fake = FakeSftp::new();
        fake.file(&ledger_path(), "{}");
        fake.knobs().lie_size = Some((LEDGER_FILE.to_string(), LEDGER_MAX_BYTES as u64 + 1));
        let p = invalid(
            transport(&fake)
                .read_ledger("gw", "production")
                .await
                .expect_err("cap"),
        );
        assert!(p.contains("byte cap"), "{p}");
        assert!(!fake.session_lines().pop().expect("last")[0].starts_with("get"));
        // a hostile gateway name
        let fake = FakeSftp::new();
        let p = invalid(
            transport(&fake)
                .read_ledger("../x", "production")
                .await
                .expect_err("name"),
        );
        assert!(p.contains("gateway"), "{p}");
    }

    #[tokio::test]
    async fn unreadable_host_is_not_an_absent_ledger() {
        // the home the server reports does not exist
        let fake = FakeSftp::new();
        let t = transport(&fake);
        fake.knobs().home = Some("/home/nope".to_string());
        fake.listed
            .lock()
            .expect("listed")
            .insert("/home/nope".to_string());
        let err = t.read_ledger("gw", "production").await.expect_err("host");
        assert!(matches!(err, CoreError::TransportFailed { .. }), "{err:?}");

        let fake = FakeSftp::new();
        fake.file(&ledger_path(), "{}");
        fake.knobs().fail_get = false;
        fake.knobs().stderr_noise = true;
        let err = transport(&fake)
            .read_ledger("gw", "production")
            .await
            .expect_err("stderr");
        assert!(matches!(err, CoreError::TransportFailed { .. }), "{err:?}");
    }

    // ---- read_release_record (25-08) -------------------------------------------

    /// The release record is below the account home, under `projects/<name>`.
    fn record_path() -> String {
        format!("{HOME}/ign-deploy/gw/projects/CapApp/release.json")
    }

    #[tokio::test]
    async fn sftp_release_record_posix() {
        use crate::deploy::ledger::RELEASE_RECORD_MAX_BYTES;
        use crate::deploy::transport::projects::ProjectHost;
        let invalid = |err: CoreError| match err {
            CoreError::DeployRunInvalid { run_id, problem } => {
                assert_eq!(run_id, "unknown");
                problem
            }
            other => panic!("expected deploy_run_invalid, got {other:?}"),
        };
        // absent at every level is None
        let fake = FakeSftp::new();
        let t = transport(&fake);
        assert_eq!(
            t.read_release_record("gw", "CapApp").await.expect("none"),
            None
        );
        fake.mkdirs(&format!("{HOME}/ign-deploy"));
        assert_eq!(
            t.read_release_record("gw", "CapApp").await.expect("none"),
            None
        );
        fake.mkdirs(&format!("{HOME}/ign-deploy/gw/projects/CapApp"));
        assert_eq!(
            t.read_release_record("gw", "CapApp").await.expect("none"),
            None
        );

        // a regular file returns its bytes, read below the account home
        let fake = FakeSftp::new();
        fake.file(&record_path(), "{\"schema_version\":1}");
        assert_eq!(
            transport(&fake)
                .read_release_record("gw", "CapApp")
                .await
                .expect("record"),
            Some(b"{\"schema_version\":1}".to_vec())
        );
        // and not from below the data directory
        let fake = FakeSftp::new();
        fake.file(
            &format!("{DATA}/ign-deploy/gw/projects/CapApp/release.json"),
            "{}",
        );
        assert_eq!(
            transport(&fake)
                .read_release_record("gw", "CapApp")
                .await
                .expect("none"),
            None
        );

        // the file is a symlink
        let fake = FakeSftp::new();
        fake.put(&record_path(), Node::Symlink);
        let p = invalid(
            transport(&fake)
                .read_release_record("gw", "CapApp")
                .await
                .expect_err("link"),
        );
        assert!(p.contains("symlink"), "{p}");
        assert!(
            p.starts_with("~/ign-deploy/gw/projects/CapApp/release.json"),
            "{p}"
        );
        // a component on the way is a symlink
        let fake = FakeSftp::new();
        fake.put(&format!("{HOME}/ign-deploy/gw/projects"), Node::Symlink);
        let p = invalid(
            transport(&fake)
                .read_release_record("gw", "CapApp")
                .await
                .expect_err("link"),
        );
        assert!(p.contains("symlink"), "{p}");
        // the record is a directory
        let fake = FakeSftp::new();
        fake.mkdirs(&record_path());
        let p = invalid(
            transport(&fake)
                .read_release_record("gw", "CapApp")
                .await
                .expect_err("dir"),
        );
        assert!(p.contains("not a regular file"), "{p}");
        // over the cap, from the listing, before any download
        let fake = FakeSftp::new();
        fake.file(&record_path(), "{}");
        fake.knobs().lie_size = Some((
            "release.json".to_string(),
            RELEASE_RECORD_MAX_BYTES as u64 + 1,
        ));
        let p = invalid(
            transport(&fake)
                .read_release_record("gw", "CapApp")
                .await
                .expect_err("cap"),
        );
        assert!(p.contains("byte cap"), "{p}");
        assert!(!fake.session_lines().pop().expect("last")[0].starts_with("get"));
        // hostile names are refused before any session
        let fake = FakeSftp::new();
        let t = transport(&fake);
        for (gateway, project, kind) in [
            ("../x", "CapApp", "gateway name"),
            ("gw", "../x", "project name"),
            ("gw", "a/b", "project name"),
            ("gw", "", "project name"),
        ] {
            let p = invalid(
                t.read_release_record(gateway, project)
                    .await
                    .expect_err(project),
            );
            assert!(p.contains(kind), "{p}");
        }
        assert!(fake.session_lines().is_empty());
    }

    #[tokio::test]
    async fn sftp_release_record_unreadable_host_is_not_an_absent_record() {
        use crate::deploy::transport::projects::ProjectHost;
        let fake = FakeSftp::new();
        let t = transport(&fake);
        fake.knobs().home = Some("/home/nope".to_string());
        fake.listed
            .lock()
            .expect("listed")
            .insert("/home/nope".to_string());
        let err = t
            .read_release_record("gw", "CapApp")
            .await
            .expect_err("host");
        assert!(matches!(err, CoreError::TransportFailed { .. }), "{err:?}");
    }

    // ---- the release record write (25-13) -----------------------------------------

    /// Runs sftp against the scripted server and answers an `ssh` call as a
    /// POSIX host would answer the fixed `mv -f -- <tmp> <final>`: it records
    /// the command, then moves the file in the scripted tree over the target
    /// (or fails with `Permission denied` when told to).
    struct MvRunner {
        fake: Arc<FakeSftp>,
        execs: Mutex<Vec<String>>,
        fail: bool,
    }

    #[async_trait::async_trait]
    impl ProcessRunner for MvRunner {
        async fn run(
            &self,
            program: &OsStr,
            args: &[OsString],
            stdin: Option<Vec<u8>>,
            timeout: Duration,
        ) -> ProcessOutput {
            if program != "ssh" {
                return self.fake.run(program, args, stdin, timeout).await;
            }
            let command = args.last().expect("command").to_string_lossy().into_owned();
            self.execs.lock().expect("execs").push(command.clone());
            if self.fail {
                return ProcessOutput {
                    stdout: Vec::new(),
                    stderr: b"mv: cannot move: Permission denied\n".to_vec(),
                    code: 1,
                    timed_out: false,
                };
            }
            let inner = command
                .strip_prefix("'mv' '-f' '--' '")
                .and_then(|rest| rest.strip_suffix('\''))
                .expect("a fixed mv command");
            let (from, to) = inner.split_once("' '").expect("two paths");
            let mut fs = self.fake.fs.lock().expect("fs");
            let node = fs.remove(from).expect("the temporary file exists");
            fs.insert(to.to_string(), node);
            ProcessOutput {
                stdout: Vec::new(),
                stderr: Vec::new(),
                code: 0,
                timed_out: false,
            }
        }
    }

    fn mv_transport(fake: &Arc<FakeSftp>, fail: bool) -> (Arc<MvRunner>, SftpTransport) {
        let runner = Arc::new(MvRunner {
            fake: fake.clone(),
            execs: Mutex::new(Vec::new()),
            fail,
        });
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            runner.clone(),
            Duration::from_secs(30),
        );
        (
            runner,
            SftpTransport::new(shell, DATA.to_string(), "nodes.master"),
        )
    }

    fn record_for(gateway: &str, project: &str, release: &str) -> Vec<u8> {
        crate::deploy::projects::ReleaseRecord {
            schema_version: 1,
            gateway: gateway.to_string(),
            project: project.to_string(),
            release: release.to_string(),
            artifact_sha256: "ab".repeat(32),
            run_id: "20261020T101500Z-ab12".to_string(),
            deployed_at: "2026-10-20T10:15:00Z".to_string(),
        }
        .to_bytes()
    }

    const TEMP_NAME: &str = "release.json.tmp-20261020T101500Z-ab12";

    /// The mutating sftp lines of every session so far, as `(command, path)`.
    fn writes(fake: &FakeSftp) -> Vec<(String, String)> {
        fake.session_lines()
            .into_iter()
            .flatten()
            .filter(|l| {
                ["mkdir ", "put ", "rename ", "rm ", "rmdir "]
                    .iter()
                    .any(|c| l.starts_with(c))
            })
            .map(|l| {
                let q = quoted(&l);
                let command = l.split(' ').next().expect("command").to_string();
                (command, q.last().expect("a path").clone())
            })
            .collect()
    }

    fn tree_keys(fake: &FakeSftp) -> Vec<String> {
        fake.fs.lock().expect("fs").keys().cloned().collect()
    }

    /// Edge DPRJ-01 concurrency, T-25-40, T-25-43: missing levels are made in
    /// order, the bytes go to a temporary sibling, one fixed `mv -f --` puts
    /// them over the record, and nothing outside the account home's
    /// `ign-deploy` tree changes.
    #[tokio::test]
    async fn sftp_record_write_posix() {
        use crate::deploy::transport::projects::{ProjectHost, ReleaseRecordWriter};
        let fake = FakeSftp::new();
        let (runner, t) = mv_transport(&fake, false);
        let before = tree_keys(&fake);
        let first = record_for("gw", "CapApp", "v1.0.0");
        t.write_release_record("gw", "CapApp", &first)
            .await
            .expect("first write");

        let base = format!("{HOME}/ign-deploy");
        let dir = format!("{base}/gw/projects/CapApp");
        assert_eq!(
            writes(&fake),
            [
                ("mkdir".to_string(), base.clone()),
                ("mkdir".to_string(), format!("{base}/gw")),
                ("mkdir".to_string(), format!("{base}/gw/projects")),
                ("mkdir".to_string(), dir.clone()),
                ("put".to_string(), format!("{dir}/{TEMP_NAME}")),
            ],
            "levels in order, then one put to the temporary name, nothing else"
        );
        // one exec, exactly the fixed argv, both paths below the account home
        let execs = runner.execs.lock().expect("execs").clone();
        assert_eq!(
            execs,
            [format!(
                "'mv' '-f' '--' '{dir}/{TEMP_NAME}' '{dir}/release.json'"
            )]
        );
        // the record is in place, the temporary file is gone
        assert_eq!(
            t.read_release_record("gw", "CapApp").await.expect("read"),
            Some(first.clone())
        );
        let after = tree_keys(&fake);
        assert!(!after.iter().any(|k| k.contains(".tmp-")), "{after:?}");
        // nothing outside `<home>/ign-deploy` was created, and nothing at all
        // below the data directory
        for key in after.iter().filter(|k| !before.contains(k)) {
            assert!(key.starts_with(&base), "{key}");
        }
        assert!(!after.iter().any(|k| k.starts_with(&format!("{DATA}/"))));

        // a second write replaces it: no directory is made again, the old
        // bytes are never seen torn (the replace is the mv)
        let before_second = fake.session_lines().len();
        let second = record_for("gw", "CapApp", "v1.1.0");
        t.write_release_record("gw", "CapApp", &second)
            .await
            .expect("second write");
        let later: Vec<(String, String)> = writes(&fake).into_iter().skip(5).collect();
        assert_eq!(later, [("put".to_string(), format!("{dir}/{TEMP_NAME}"))]);
        assert!(fake.session_lines().len() > before_second);
        assert_eq!(
            t.read_release_record("gw", "CapApp").await.expect("read"),
            Some(second)
        );

        // an existing project directory under a new gateway level: only the
        // missing levels are made
        let fake = FakeSftp::new();
        fake.mkdirs(&format!("{HOME}/ign-deploy/gw"));
        let (_, t) = mv_transport(&fake, false);
        t.write_release_record("gw", "CapApp", &first)
            .await
            .expect("partial tree");
        let made: Vec<String> = writes(&fake)
            .into_iter()
            .filter(|(c, _)| c == "mkdir")
            .map(|(_, p)| p)
            .collect();
        assert_eq!(made, [format!("{base}/gw/projects"), dir.clone()]);
    }

    /// T-25-41: a link or a non-directory on the way, a record that is not a
    /// regular file and a temporary file already in the way are refused
    /// before anything is made or written; the names and the bytes are held
    /// to the read's rules first.
    #[tokio::test]
    async fn sftp_record_write_refusals() {
        use crate::deploy::transport::projects::ReleaseRecordWriter;
        let bytes = record_for("gw", "CapApp", "v1.0.0");
        let base = format!("{HOME}/ign-deploy");
        let record = format!("{base}/gw/projects/CapApp/release.json");
        type Setup<'a> = Box<dyn Fn(&FakeSftp) + 'a>;
        let cases: Vec<(&str, Setup<'_>, &str)> = vec![
            (
                "a link at ign-deploy",
                Box::new(|f| f.put(&base, Node::Symlink)),
                "symlink",
            ),
            (
                "a link at projects",
                Box::new(|f| f.put(&format!("{base}/gw/projects"), Node::Symlink)),
                "symlink",
            ),
            (
                "a file at projects",
                Box::new(|f| f.file(&format!("{base}/gw/projects"), "x")),
                "not a directory",
            ),
            (
                "a link at the project",
                Box::new(|f| f.put(&format!("{base}/gw/projects/CapApp"), Node::Symlink)),
                "symlink",
            ),
            (
                "a directory at the record",
                Box::new(|f| f.mkdirs(&record)),
                "a directory",
            ),
            (
                "a link at the record",
                Box::new(|f| f.put(&record, Node::Symlink)),
                "symlink",
            ),
            (
                "a special file at the record",
                Box::new(|f| f.put(&record, Node::Special)),
                "not a regular file",
            ),
            (
                "a temporary file in the way",
                Box::new(|f| f.file(&format!("{base}/gw/projects/CapApp/{TEMP_NAME}"), "x")),
                "temporary name",
            ),
            (
                "a link of the temporary name",
                Box::new(|f| {
                    f.put(
                        &format!("{base}/gw/projects/CapApp/{TEMP_NAME}"),
                        Node::Symlink,
                    )
                }),
                "temporary name",
            ),
        ];
        for (name, setup, needle) in cases {
            let fake = FakeSftp::new();
            fake.mkdirs(&format!("{base}/gw/projects/CapApp"));
            setup(&fake);
            let (runner, t) = mv_transport(&fake, false);
            let before = tree_keys(&fake);
            let err = t
                .write_release_record("gw", "CapApp", &bytes)
                .await
                .expect_err(name);
            let CoreError::TransportFailed { detail, .. } = &err else {
                panic!("{name}: expected transport_failed, got {err:?}");
            };
            assert!(
                detail.starts_with("~/ign-deploy/gw/projects/CapApp/release.json"),
                "{name}: {detail}"
            );
            assert!(detail.contains(needle), "{name}: {detail}");
            assert!(
                writes(&fake).is_empty(),
                "{name}: wrote {:?}",
                writes(&fake)
            );
            assert!(runner.execs.lock().expect("execs").is_empty(), "{name}");
            assert_eq!(tree_keys(&fake), before, "{name}");
        }

        // hostile names, a record for another project and an empty record are
        // refused before any session
        let fake = FakeSftp::new();
        let (runner, t) = mv_transport(&fake, false);
        for (gateway, project, payload) in [
            ("../x", "CapApp", bytes.clone()),
            ("gw", "../x", bytes.clone()),
            ("gw", "a/b", bytes.clone()),
            ("gw", "", bytes.clone()),
            ("gw", "CapApp", record_for("gw", "Other", "v1.0.0")),
            ("gw", "CapApp", record_for("other", "CapApp", "v1.0.0")),
            ("gw", "CapApp", Vec::new()),
            ("gw", "CapApp", b"{}".to_vec()),
        ] {
            let err = t
                .write_release_record(gateway, project, &payload)
                .await
                .expect_err(project);
            assert!(
                matches!(err, CoreError::DeployRunInvalid { .. }),
                "{gateway}/{project}: {err:?}"
            );
        }
        assert!(fake.session_lines().is_empty());
        assert!(runner.execs.lock().expect("execs").is_empty());
    }

    /// T-25-40: when the replace fails, the temporary file is removed and the
    /// previous record is still there, byte for byte.
    #[tokio::test]
    async fn sftp_record_write_failure_cleans_temp() {
        use crate::deploy::transport::projects::{ProjectHost, ReleaseRecordWriter};
        let fake = FakeSftp::new();
        let previous = record_for("gw", "CapApp", "v1.0.0");
        fake.file(
            &record_path(),
            std::str::from_utf8(&previous).expect("utf-8"),
        );
        let (runner, t) = mv_transport(&fake, true);
        let err = t
            .write_release_record("gw", "CapApp", &record_for("gw", "CapApp", "v1.1.0"))
            .await
            .expect_err("the mv fails");
        let CoreError::TransportFailed { detail, .. } = &err else {
            panic!("expected transport_failed, got {err:?}");
        };
        assert!(
            detail.starts_with("~/ign-deploy/gw/projects/CapApp/release.json"),
            "{detail}"
        );
        assert!(detail.contains("Permission denied"), "{detail}");
        assert!(!detail.contains("may still be there"), "{detail}");
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
        // put, then rm of the same temporary path
        let dir = format!("{HOME}/ign-deploy/gw/projects/CapApp");
        assert_eq!(
            writes(&fake),
            [
                ("put".to_string(), format!("{dir}/{TEMP_NAME}")),
                ("rm".to_string(), format!("{dir}/{TEMP_NAME}")),
            ]
        );
        assert!(!tree_keys(&fake).iter().any(|k| k.contains(".tmp-")));
        let (_, reader) = mv_transport(&fake, false);
        assert_eq!(
            reader
                .read_release_record("gw", "CapApp")
                .await
                .expect("read"),
            Some(previous)
        );
    }

    /// WR-04: a `put` that fails leaves a partial temporary file; it is
    /// removed, and the previous record is untouched.
    #[tokio::test]
    async fn sftp_record_put_failure_removes_the_partial_temp() {
        use crate::deploy::transport::projects::{ProjectHost, ReleaseRecordWriter};
        let fake = FakeSftp::new();
        let previous = record_for("gw", "CapApp", "v1.0.0");
        fake.file(
            &record_path(),
            std::str::from_utf8(&previous).expect("utf-8"),
        );
        fake.knobs().put_fails = 1;
        let (runner, t) = mv_transport(&fake, false);
        let err = t
            .write_release_record("gw", "CapApp", &record_for("gw", "CapApp", "v1.1.0"))
            .await
            .expect_err("the put fails");
        let CoreError::TransportFailed { detail, .. } = &err else {
            panic!("expected transport_failed, got {err:?}");
        };
        assert!(detail.contains("Permission denied"), "{detail}");
        assert!(!detail.contains("may still be there"), "{detail}");
        assert!(runner.execs.lock().expect("execs").is_empty(), "no mv");
        let dir = format!("{HOME}/ign-deploy/gw/projects/CapApp");
        assert_eq!(
            writes(&fake),
            [
                ("put".to_string(), format!("{dir}/{TEMP_NAME}")),
                ("rm".to_string(), format!("{dir}/{TEMP_NAME}")),
            ]
        );
        assert!(
            !tree_keys(&fake).iter().any(|k| k.contains(".tmp-")),
            "the partial temporary file is gone"
        );
        let (_, reader) = mv_transport(&fake, false);
        assert_eq!(
            reader
                .read_release_record("gw", "CapApp")
                .await
                .expect("read"),
            Some(previous)
        );
    }

    /// WR-04: when the removal of the temporary file after a failed `put`
    /// fails too, the error says the file may still be there.
    #[tokio::test]
    async fn sftp_record_put_failure_with_failed_cleanup_is_reported() {
        use crate::deploy::transport::projects::ReleaseRecordWriter;
        let fake = FakeSftp::new();
        fake.knobs().put_fails = 1;
        fake.knobs().rm_denied = true;
        let (_, t) = mv_transport(&fake, false);
        let err = t
            .write_release_record("gw", "CapApp", &record_for("gw", "CapApp", "v1.0.0"))
            .await
            .expect_err("the put fails");
        let CoreError::TransportFailed { detail, .. } = &err else {
            panic!("expected transport_failed, got {err:?}");
        };
        assert!(
            detail.contains(&format!(
                "the temporary file {TEMP_NAME} may still be there"
            )),
            "{detail}"
        );
        assert!(
            tree_keys(&fake).iter().any(|k| k.contains(".tmp-")),
            "the leftover is real"
        );
    }

    /// Answers an `ssh ... powershell -EncodedCommand` call as a Windows host
    /// would answer the fixed atomic record-replace script: it records the
    /// command, then moves the file in the scripted tree over the target (or
    /// fails with an error CLIXML when told to).
    /// What the record-replace script throws when error 32 outlasts the
    /// retries; the failure answer is the PowerShell 5.1 record of it.
    const BLOCKED_THROW: &str = "the record replace failed with Win32 error 32: The process cannot access the file because it is being used by another process.";

    struct WinMoveRunner {
        fake: Arc<FakeSftp>,
        execs: Mutex<Vec<String>>,
        fail: bool,
    }

    #[async_trait::async_trait]
    impl ProcessRunner for WinMoveRunner {
        async fn run(
            &self,
            program: &OsStr,
            args: &[OsString],
            stdin: Option<Vec<u8>>,
            timeout: Duration,
        ) -> ProcessOutput {
            if program != "ssh" {
                return self.fake.run(program, args, stdin, timeout).await;
            }
            let command = args.last().expect("command").to_string_lossy().into_owned();
            self.execs.lock().expect("execs").push(command.clone());
            if self.fail {
                return ProcessOutput {
                    stdout: Vec::new(),
                    stderr: winps::ps51_error_clixml(BLOCKED_THROW),
                    code: 1,
                    timed_out: false,
                };
            }
            let (_, params) = stub_params(&command);
            let sftp_spelling =
                |key: &str| format!("/{}", params[key].as_str().expect(key).replace('\\', "/"));
            let (from, to) = (sftp_spelling("src"), sftp_spelling("dst"));
            let mut fs = self.fake.fs.lock().expect("fs");
            let node = fs.remove(&from).expect("the temporary file exists");
            fs.insert(to, node);
            ProcessOutput {
                stdout: b"IGN-RESULT:{\"ok\":1}\r\n".to_vec(),
                stderr: Vec::new(),
                code: 0,
                timed_out: false,
            }
        }
    }

    fn win_move_transport(fake: &Arc<FakeSftp>, fail: bool) -> (Arc<WinMoveRunner>, SftpTransport) {
        let runner = Arc::new(WinMoveRunner {
            fake: fake.clone(),
            execs: Mutex::new(Vec::new()),
            fail,
        });
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            runner.clone(),
            Duration::from_secs(30),
        );
        (
            runner,
            SftpTransport::new_windows(shell, WDATA.to_string(), "nodes.master"),
        )
    }

    /// Edge DPRJ-01 concurrency, T-25-40, T-25-42, T-25-43 on a Windows host:
    /// the same write order as the POSIX one, the replace is one fixed
    /// atomic `MoveFileEx` script whose paths are native and travel, with the
    /// retry count, as parameters, a failed move removes the temporary file and leaves the
    /// previous record, and the Windows name rules run before any session.
    #[tokio::test]
    async fn sftp_record_write_windows() {
        use crate::deploy::transport::projects::{ProjectHost, ReleaseRecordWriter};
        let fake = windows_fake();
        let (runner, t) = win_move_transport(&fake, false);
        let before = tree_keys(&fake);
        let first = record_for("gw", "CapApp", "v1.0.0");
        t.write_release_record("gw", "CapApp", &first)
            .await
            .expect("first write");

        let base = format!("{WHOME}/ign-deploy");
        let dir = format!("{base}/gw/projects/CapApp");
        assert_eq!(
            writes(&fake),
            [
                ("mkdir".to_string(), base.clone()),
                ("mkdir".to_string(), format!("{base}/gw")),
                ("mkdir".to_string(), format!("{base}/gw/projects")),
                ("mkdir".to_string(), dir.clone()),
                ("put".to_string(), format!("{dir}/{TEMP_NAME}")),
            ],
            "levels in order, then one put to the temporary name, nothing else"
        );
        // one exec: the fixed script text, native paths in the parameters only
        let execs = runner.execs.lock().expect("execs").clone();
        assert_eq!(execs.len(), 1, "{execs:?}");
        let (text, params) = stub_params(&execs[0]);
        assert!(
            text.ends_with(winps::RECORD_REPLACE),
            "the script is the fixed text: {text}"
        );
        assert!(!text.contains("ign-deploy"), "no path in the script text");
        assert_eq!(
            params,
            serde_json::json!({
                "src": format!("C:\\Users\\svc\\ign-deploy\\gw\\projects\\CapApp\\{TEMP_NAME}"),
                "dst": "C:\\Users\\svc\\ign-deploy\\gw\\projects\\CapApp\\release.json",
                "retries": RENAME_RETRIES,
            })
        );
        assert_eq!(
            t.read_release_record("gw", "CapApp").await.expect("read"),
            Some(first.clone())
        );
        let after = tree_keys(&fake);
        assert!(!after.iter().any(|k| k.contains(".tmp-")), "{after:?}");
        for key in after.iter().filter(|k| !before.contains(k)) {
            assert!(key.starts_with(&base), "{key}");
        }
        assert!(!after.iter().any(|k| k.starts_with(&format!("{WDATA}/"))));

        // an overwrite makes no directory and replaces the bytes
        let second = record_for("gw", "CapApp", "v1.1.0");
        t.write_release_record("gw", "CapApp", &second)
            .await
            .expect("second write");
        let later: Vec<(String, String)> = writes(&fake).into_iter().skip(5).collect();
        assert_eq!(later, [("put".to_string(), format!("{dir}/{TEMP_NAME}"))]);
        assert_eq!(runner.execs.lock().expect("execs").len(), 2);
        assert_eq!(
            t.read_release_record("gw", "CapApp").await.expect("read"),
            Some(second)
        );

        // a failed move: the temporary file is put, then removed; the previous
        // record is intact
        let fake = windows_fake();
        let previous = record_for("gw", "CapApp", "v1.0.0");
        fake.file(
            &format!("{WHOME}/ign-deploy/gw/projects/CapApp/release.json"),
            std::str::from_utf8(&previous).expect("utf-8"),
        );
        let (runner, t) = win_move_transport(&fake, true);
        let err = t
            .write_release_record("gw", "CapApp", &record_for("gw", "CapApp", "v1.1.0"))
            .await
            .expect_err("the move fails");
        let CoreError::TransportFailed { detail, .. } = &err else {
            panic!("expected transport_failed, got {err:?}");
        };
        assert!(
            detail.starts_with("~/ign-deploy/gw/projects/CapApp/release.json"),
            "{detail}"
        );
        assert!(!detail.contains("may still be there"), "{detail}");
        let squash =
            |text: &str| -> String { text.chars().filter(|c| !c.is_whitespace()).collect() };
        assert!(
            squash(detail).contains(&squash(BLOCKED_THROW)),
            "the cause reaches the detail whole: {detail}"
        );
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
        assert_eq!(
            writes(&fake),
            [
                ("put".to_string(), format!("{dir}/{TEMP_NAME}")),
                ("rm".to_string(), format!("{dir}/{TEMP_NAME}")),
            ]
        );
        assert!(!tree_keys(&fake).iter().any(|k| k.contains(".tmp-")));
        let (_, reader) = win_move_transport(&fake, false);
        assert_eq!(
            reader
                .read_release_record("gw", "CapApp")
                .await
                .expect("read"),
            Some(previous)
        );

        // a link on the way refuses before anything is written
        let fake = windows_fake();
        fake.put(&format!("{WHOME}/ign-deploy"), Node::Symlink);
        let (runner, t) = win_move_transport(&fake, false);
        let err = t
            .write_release_record("gw", "CapApp", &first)
            .await
            .expect_err("link");
        let CoreError::TransportFailed { detail, .. } = &err else {
            panic!("expected transport_failed, got {err:?}");
        };
        assert!(detail.contains("symlink"), "{detail}");
        assert!(writes(&fake).is_empty());
        assert!(runner.execs.lock().expect("execs").is_empty());

        // the Windows name rules (device names, trailing dot, stream colon)
        // refuse before any session or exec
        let fake = windows_fake();
        let (runner, t) = win_move_transport(&fake, false);
        for (gateway, project) in [
            ("gw", "NUL"),
            ("gw", "CapApp."),
            ("gw", "a:b"),
            ("COM1", "p"),
        ] {
            let err = t
                .write_release_record(gateway, project, &record_for(gateway, project, "v1"))
                .await
                .expect_err(project);
            assert!(
                matches!(err, CoreError::DeployRunInvalid { .. }),
                "{gateway}/{project}: {err:?}"
            );
        }
        assert!(fake.session_lines().is_empty());
        assert!(runner.execs.lock().expect("execs").is_empty());
    }

    // ---- read_journal (23-18) --------------------------------------------------

    /// The journal is below the account home, beside the ledger (D-04).
    fn journal_path() -> String {
        format!("{HOME}/ign-deploy/gw/production/{JOURNAL_FILE}")
    }

    #[tokio::test]
    async fn read_journal_sftp() {
        let invalid = |err: CoreError| match err {
            CoreError::DeployRunInvalid { problem, .. } => problem,
            other => panic!("expected deploy_run_invalid, got {other:?}"),
        };
        let read = |fake: &Arc<FakeSftp>| {
            let t = transport(fake);
            async move { t.read_journal("gw", "production").await }
        };

        // absent at every level is None
        let fake = FakeSftp::new();
        assert_eq!(read(&fake).await.expect("none"), None);
        fake.mkdirs(&format!("{HOME}/ign-deploy"));
        assert_eq!(read(&fake).await.expect("none"), None);
        fake.mkdirs(&format!("{HOME}/ign-deploy/gw/production"));
        assert_eq!(read(&fake).await.expect("none"), None);

        // present: the bytes, unvalidated; a ledger beside it is not read
        fake.file(&journal_path(), "{\"schema_version\":1}");
        fake.file(&ledger_path(), "other");
        assert_eq!(
            read(&fake).await.expect("journal"),
            Some(b"{\"schema_version\":1}".to_vec())
        );

        // the journal is a symlink
        let fake = FakeSftp::new();
        fake.put(&journal_path(), Node::Symlink);
        let p = invalid(read(&fake).await.expect_err("link"));
        assert!(p.contains("symlink"), "{p}");
        assert!(p.contains("~/ign-deploy/gw/production/journal.json"), "{p}");
        // a directory on the way is a symlink
        let fake = FakeSftp::new();
        fake.put(&format!("{HOME}/ign-deploy/gw"), Node::Symlink);
        let p = invalid(read(&fake).await.expect_err("link"));
        assert!(p.contains("symlink"), "{p}");
        // a component that is a file
        let fake = FakeSftp::new();
        fake.file(&format!("{HOME}/ign-deploy/gw"), "x");
        let p = invalid(read(&fake).await.expect_err("file"));
        assert!(p.contains("not a directory"), "{p}");
        // the journal is a directory
        let fake = FakeSftp::new();
        fake.mkdirs(&journal_path());
        let p = invalid(read(&fake).await.expect_err("dir"));
        assert!(p.contains("not a regular file"), "{p}");
        // over the cap, from the listing, before a download
        let fake = FakeSftp::new();
        fake.file(&journal_path(), "{}");
        fake.knobs().lie_size = Some((JOURNAL_FILE.to_string(), JOURNAL_MAX_BYTES as u64 + 1));
        let p = invalid(read(&fake).await.expect_err("cap"));
        assert!(p.contains("byte cap"), "{p}");
        assert!(!fake.session_lines().pop().expect("last")[0].starts_with("get"));
        // a hostile gateway name
        let fake = FakeSftp::new();
        let err = transport(&fake)
            .read_journal("../x", "production")
            .await
            .expect_err("name");
        assert!(invalid(err).contains("gateway"));
        // an unreadable host is not an absent journal
        let fake = FakeSftp::new();
        fake.knobs().home = Some("/home/nope".to_string());
        fake.listed
            .lock()
            .expect("listed")
            .insert("/home/nope".to_string());
        let err = read(&fake).await.expect_err("host");
        assert!(matches!(err, CoreError::TransportFailed { .. }), "{err:?}");
        // a failed download is not an absent journal
        let fake = FakeSftp::new();
        fake.file(&journal_path(), "{}");
        fake.knobs().stderr_noise = true;
        let err = read(&fake).await.expect_err("stderr");
        assert!(matches!(err, CoreError::TransportFailed { .. }), "{err:?}");
    }

    // ---- resource caps (post-review) -------------------------------------------

    /// Answers every `@ls -lan` with a fixed directory chain and `filler`
    /// extra regular files per directory, and counts the sessions it served.
    struct FloodRunner {
        filler: usize,
        sessions: Mutex<usize>,
    }

    #[async_trait::async_trait]
    impl ProcessRunner for FloodRunner {
        async fn run(
            &self,
            _program: &OsStr,
            _args: &[OsString],
            stdin: Option<Vec<u8>>,
            _timeout: Duration,
        ) -> ProcessOutput {
            *self.sessions.lock().expect("sessions") += 1;
            let batch = String::from_utf8(stdin.expect("stdin")).expect("utf8");
            let mut out = String::new();
            for l in batch.lines() {
                if l == "@pwd" {
                    out.push_str(&format!("Remote working directory: {DATA}\n"));
                    continue;
                }
                let dir = quoted(l).remove(0);
                out.push_str(&format!("{}\n", line("drwxr-xr-x", 1, &format!("{dir}/."))));
                let child = match dir.as_str() {
                    DATA => Some("config"),
                    d if d == format!("{DATA}/config") => Some("resources"),
                    d if d == RES => Some("core"),
                    _ => None,
                };
                if let Some(child) = child {
                    out.push_str(&format!(
                        "{}\n",
                        line("drwxr-xr-x", 1, &format!("{dir}/{child}"))
                    ));
                }
                for n in 0..self.filler {
                    out.push_str(&format!(
                        "{}\n",
                        line("-rw-r--r--", 1, &format!("{dir}/f{n}"))
                    ));
                }
            }
            ProcessOutput {
                stdout: out.into_bytes(),
                stderr: Vec::new(),
                code: 0,
                timed_out: false,
            }
        }
    }

    fn flood_transport(filler: usize) -> (Arc<FloodRunner>, SftpTransport) {
        let runner = Arc::new(FloodRunner {
            filler,
            sessions: Mutex::new(0),
        });
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused"),
            runner.clone(),
            Duration::from_secs(30),
        );
        (
            runner,
            SftpTransport::new(shell, DATA.to_string(), "nodes.master"),
        )
    }

    #[test]
    fn entry_cap_stops_the_parse_at_the_offending_line() {
        let dir = "/d".to_string();
        let ok = |n: usize| -> String {
            std::iter::once(format!("{PWD_MARKER}/home/u"))
                .chain((0..n).map(|i| line("-rw-r--r--", 1, &format!("/d/f{i}"))))
                .collect::<Vec<_>>()
                .join("\n")
        };
        // exactly at the cap is fine; one more is not
        assert_eq!(
            parse_batch_listing_capped(std::slice::from_ref(&dir), ok(5).as_bytes(), 5)
                .expect("at the cap")[0]
                .len(),
            5
        );
        assert_eq!(
            parse_batch_listing_capped(std::slice::from_ref(&dir), ok(6).as_bytes(), 5),
            Err(ListingError::TooMany)
        );
        // the line that passes the cap ends the parse: a hostile line after it
        // is never looked at (a TooMany, not a Bad)
        let hostile = format!("{}\n{}", ok(6), "garbage that would be a parse error");
        assert_eq!(
            parse_batch_listing_capped(std::slice::from_ref(&dir), hostile.as_bytes(), 5),
            Err(ListingError::TooMany)
        );
        // `.` and `..` do not count as entries
        let dots = format!(
            "{}\n{}\n{}",
            ok(0),
            line("drwxr-xr-x", 1, "/d/."),
            line("drwxr-xr-x", 1, "/d/..")
        ) + &ok(5)["Remote working directory: /home/u".len()..];
        assert!(parse_batch_listing_capped(std::slice::from_ref(&dir), dots.as_bytes(), 5).is_ok());
    }

    #[tokio::test]
    async fn entry_cap_rejects_one_huge_listing_in_its_first_session() {
        let (runner, t) = flood_transport(READ_MAX_ENTRIES + 1);
        let why = detail(t.read(&scope()).await.expect_err("flood"));
        assert!(
            why.contains(&format!("more than {READ_MAX_ENTRIES} entries")),
            "{why}"
        );
        assert_eq!(*runner.sessions.lock().expect("sessions"), 1);
    }

    #[tokio::test]
    async fn entry_cap_is_cumulative_across_levels() {
        // each directory is far under the cap; three levels together are not
        let (runner, t) = flood_transport(READ_MAX_ENTRIES / 3 + 100);
        let why = detail(t.read(&scope()).await.expect_err("cumulative"));
        assert!(
            why.contains(&format!("more than {READ_MAX_ENTRIES} entries")),
            "{why}"
        );
        assert_eq!(*runner.sessions.lock().expect("sessions"), 3, "{why}");

        // list_types and the ledger walk keep their own cumulative budget
        let (_, t) = flood_transport(READ_MAX_ENTRIES + 1);
        assert!(t.list_types("core").await.is_err());
        let (_, t) = flood_transport(READ_MAX_ENTRIES + 1);
        assert!(t.read_ledger("gw", "production").await.is_err());
    }

    #[test]
    fn file_cap_is_rechecked_through_the_handle() {
        // an on-disk file over the cap is refused even if the listing agrees
        let dir = tempfile::tempdir().expect("dir");
        let file = std::fs::File::create(dir.path().join("big")).expect("create");
        file.set_len(READ_MAX_FILE_BYTES + 1).expect("sparse");
        drop(file);
        assert!(matches!(
            read_downloaded(
                dir.path(),
                Path::new("big"),
                READ_MAX_FILE_BYTES + 1,
                READ_MAX_FILE_BYTES
            ),
            Err(Readback::Capped(_))
        ));
        // a file that does not match its listing is refused
        std::fs::write(dir.path().join("small"), b"12345").expect("write");
        assert!(matches!(
            read_downloaded(dir.path(), Path::new("small"), 3, READ_MAX_FILE_BYTES),
            Err(Readback::SizeMismatch {
                listed: 3,
                actual: 5
            })
        ));
        assert_eq!(
            read_downloaded(dir.path(), Path::new("small"), 5, READ_MAX_FILE_BYTES)
                .ok()
                .as_deref(),
            Some(&b"12345"[..])
        );
    }

    #[tokio::test]
    async fn oversize_download_is_aborted_and_cleaned_up() {
        // the listing says 10 bytes; the server delivers far more
        let fake = FakeSftp::new();
        fake.file(
            &format!("{RES}/production/ignition/database-connection/A/config.json"),
            "0123456789",
        );
        fake.knobs().overshoot = Some(("A/config.json".to_string(), 8 * 1024 * 1024));
        let t = transport(&fake);
        let started = std::time::Instant::now();
        let why = detail(t.read(&scope()).await.expect_err("overshoot"));
        assert!(why.contains("it was aborted"), "{why}");
        assert!(
            started.elapsed() < Duration::from_secs(10),
            "the stalled transfer was killed, not waited for"
        );
        let written = fake.downloads.lock().expect("downloads").clone();
        assert!(!written.is_empty());
        for path in written {
            assert!(!path.exists(), "{} was cleaned up", path.display());
        }
    }

    #[tokio::test]
    async fn total_cap_is_checked_before_each_chunk() {
        // 8 files listed at 64 MiB: 512 MiB, under the total cap, so they are
        // fetched in chunks of 256 MiB (4 files). The first chunk delivers the
        // real (tiny) bytes, fails its size check, and the second never starts.
        let fake = FakeSftp::new();
        for n in 0..8 {
            fake.file(
                &format!("{RES}/production/ignition/database-connection/D{n}/config.json"),
                "{}",
            );
        }
        fake.knobs().lie_size = Some(("config.json".to_string(), READ_MAX_FILE_BYTES));
        let why = detail(transport(&fake).read(&scope()).await.expect_err("chunk"));
        assert!(why.contains("was listed as"), "{why}");
        let get_sessions = fake
            .session_lines()
            .iter()
            .filter(|lines| lines[0].starts_with("get \""))
            .count();
        assert_eq!(get_sessions, 1, "the second chunk never started");
        let first = fake
            .session_lines()
            .into_iter()
            .find(|lines| lines[0].starts_with("get \""))
            .expect("get session");
        assert_eq!(first.len(), 4, "a chunk is bounded by listed bytes");
    }

    #[tokio::test]
    async fn recursive_get_is_never_issued() {
        let fake = FakeSftp::new();
        standard_tree(&fake);
        transport(&fake).read(&scope()).await.expect("read");
        transport(&fake)
            .read_ledger("gw", "production")
            .await
            .expect("ledger");
        let all: Vec<String> = fake.session_lines().into_iter().flatten().collect();
        assert!(all.iter().any(|l| l.starts_with("get \"")));
        assert!(all.iter().all(|l| !l.contains("get -R")), "{all:?}");
    }

    #[tokio::test]
    async fn data_dir_without_config_resources_is_transport_failed() {
        for missing in ["config", "config/resources"] {
            let fake = FakeSftp::new();
            fake.mkdirs(RES);
            let full = format!("{DATA}/{missing}");
            fake.fs
                .lock()
                .expect("fs")
                .retain(|k, _| k != &full && !k.starts_with(&format!("{full}/")));
            let why = detail(transport(&fake).read(&scope()).await.expect_err(missing));
            assert!(why.contains(missing), "{missing}: {why}");
            assert!(why.contains("gateway data directory"), "{why}");
        }
        // also with an empty scope
        let fake = FakeSftp::new();
        let empty = Scope {
            mode: "production".to_string(),
            types: Vec::new(),
            tag_providers: Vec::new(),
        };
        detail(
            transport(&fake)
                .read(&empty)
                .await
                .expect_err("bare data dir"),
        );
    }

    // ---- Windows OpenSSH (22-13), from the C7a captures -------------------------

    const WDATA: &str = "/C:/gw/data";
    const WRES: &str = "/C:/gw/data/config/resources";
    const WHOME: &str = "/C:/Users/svc";

    #[test]
    fn windows_sftp_root_vectors() {
        for (input, want) in [
            // the captured data dir (C7a-1)
            (
                "C:\\Program Files\\Inductive Automation\\Ignition\\data",
                "/C:/Program Files/Inductive Automation/Ignition/data",
            ),
            // forward slashes, a lowercase drive letter, a trailing separator
            (
                "c:/Program Files/Inductive Automation/Ignition/data/",
                "/C:/Program Files/Inductive Automation/Ignition/data",
            ),
            ("d:\\ign data, 1\\gw\\", "/D:/ign data, 1/gw"),
            ("C:\\a\\b\\\\", "/C:/a/b"),
        ] {
            assert_eq!(windows_sftp_root(input).as_deref(), Ok(want), "{input}");
        }
        for bad in [
            // UNC and extended-length paths
            "\\\\server\\share\\data",
            "//server/share/data",
            "\\\\?\\C:\\data",
            // relative, drive-relative, rooted without a drive, POSIX
            "data",
            "relative\\data",
            "C:data",
            "\\data",
            "/usr/local/ignition/data",
            "/C:/data",
            "",
            // the drive root
            "C:\\",
            "C:",
            // components
            "C:\\a\\..\\b",
            "C:\\a\\.\\b",
            "C:\\a\\\\b",
            "C:\\a.\\b",
            "C:\\a \\b",
            "C:\\a\\b:stream",
            // quotes, control characters, glob characters
            "C:\\a\"b",
            "C:\\a\nb",
            "C:\\a*b",
            "C:\\a[1]",
            "C:\\a{b}",
            // too long for the sftp server to resolve what is below it
            &format!("C:\\{}", "d".repeat(WINDOWS_DATA_DIR_MAX)),
        ] {
            assert!(windows_sftp_root(bad).is_err(), "{bad:?} must be refused");
        }
        assert_eq!(
            windows_native_path("/C:/Program Files/Ignition/data/config"),
            "C:\\Program Files\\Ignition\\data\\config"
        );
    }

    /// Verbatim from `## C7a` (C7a-2): `ls -la` of the real collection root
    /// and of the spaces-and-comma directory, in a UTF-8 client locale, each
    /// led by the `pwd` marker the transport puts before a listing.
    #[test]
    fn windows_listing_parses_captured_output() {
        const ROOT: &str = "Remote working directory: /C:/Users/svc-ignition-deploy\n\
drwx******    1 -        -          262144 Oct 15 02:10 .resources\n\
drwx******    1 -        -            4096 Oct 15 02:05 core\n\
drwx******    1 -        -               0 Oct 15 02:07 external\n\
drwx******    1 -        -               0 Oct 15 02:05 local\n\
-rw-******    1 -        -           26893 Oct 15 02:04 migration-log-202610150204.md\n\
drwx******    1 -        -               0 Oct 15 02:08 production\n";
        let dirs = vec!["/C:/Program Files/x/resources".to_string()];
        let got = parse_batch_listing_as(
            ListFormat::Windows,
            NameRule::Refuse,
            &dirs,
            ROOT.as_bytes(),
            100,
        )
        .expect("root listing")
        .remove(0);
        let shown: Vec<(&str, EntryKind, u64)> = got
            .iter()
            .map(|e| (e.name.as_str(), e.kind, e.size))
            .collect();
        assert_eq!(
            shown,
            [
                (".resources", EntryKind::Dir, 262144),
                ("core", EntryKind::Dir, 4096),
                ("external", EntryKind::Dir, 0),
                ("local", EntryKind::Dir, 0),
                ("migration-log-202610150204.md", EntryKind::File, 26893),
                ("production", EntryKind::Dir, 0),
            ]
        );

        // spaces, a comma, a non-ASCII name printed raw by a UTF-8 client, an
        // empty directory first and a listing after it
        const SPACES: &str = "Remote working directory: /C:/Users/u\n\
Remote working directory: /C:/Users/u\n\
-rw-******    1 -        -               8 Oct 15 02:35 café.txt\n\
-rw-******    1 -        -               6 Oct 15 02:35 f one.txt\n\
-rw-******    1 -        -               6 Oct 15 02:35 f,two.txt\n\
-rw-******    1 -        -               1 Oct 15 02:35 a  double  space\n\
drwx******    1 -        -               0 Oct 15 02:35 sub dir\n";
        let dirs = vec!["/C:/e".to_string(), "/C:/d".to_string()];
        let got = parse_batch_listing_as(
            ListFormat::Windows,
            NameRule::Refuse,
            &dirs,
            SPACES.as_bytes(),
            100,
        )
        .expect("spaces");
        assert!(
            got[0].is_empty(),
            "an empty directory prints nothing after its marker"
        );
        let names: Vec<&str> = got[1].iter().map(|e| e.name.as_str()).collect();
        assert_eq!(
            names,
            [
                "a  double  space",
                "café.txt",
                "f one.txt",
                "f,two.txt",
                "sub dir"
            ]
        );

        let one = vec!["/C:/d".to_string()];
        let parse = |text: &str| {
            parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Refuse,
                &one,
                text.as_bytes(),
                100,
            )
        };
        // a client without a UTF-8 locale prints the escaped name: refused,
        // never read as the real one
        assert!(
            parse("Remote working directory: /C:/h\n-rw-******    1 -        -   8 Oct 15 02:35 caf\\303\\251.txt\n")
                .is_err()
        );
        // lines before the first marker, too many markers, too few markers
        assert!(parse("-rw-******    1 -        -   8 Oct 15 02:35 f\n").is_err());
        assert!(
            parse("Remote working directory: /C:/h\nRemote working directory: /C:/h\n").is_err()
        );
        assert!(
            parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Refuse,
                &["/C:/a".to_string(), "/C:/b".to_string()],
                b"Remote working directory: /C:/h\n",
                100
            )
            .is_err()
        );
        // too few columns, a bad size, a bad mode, a duplicate
        for bad in [
            "-rw-******    1 -        - 8 Oct 15 02:35\n",
            "-rw-******    1 -        -  NaN Oct 15 02:35 f\n",
            "-rw    1 -        -   8 Oct 15 02:35 f\n",
            "-rw-******    1 -        -   8 Oct 15 02:35 f\n-rw-******    1 -        -   9 Oct 15 02:35 f\n",
            "-rw-******    1 -        -   8 Oct 15 02:35 a/b\n",
        ] {
            assert!(
                parse(&format!("Remote working directory: /C:/h\n{bad}")).is_err(),
                "{bad:?}"
            );
        }
        // the single-file shape (C7a-2: link count `?`, uid 0) is not a
        // directory listing line the transport ever asks for; with its full
        // path as the name it is refused as a multi-component name
        assert!(
            parse("Remote working directory: /C:/h\n-rw-------    ? 0        0               6 Oct 14 20:35 ign-c7a/d/f one.txt\n")
                .is_err()
        );
        // unsafe names: refused, or skipped where only fixed names are looked up
        let home = "Remote working directory: /C:/Users/u\n\
drwx******    1 -        -               0 Oct 15 01:55 AppData\n\
-rw-******    1 -        -           65536 Oct 15 01:54 NTUSER.DAT{6b3cbf8b-c6cb-11f1-8338-00155d013703}.TM.blf\n\
drwx******    1 -        -               0 Oct 15 01:54 ign-deploy\n";
        assert!(
            parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Refuse,
                &one,
                home.as_bytes(),
                100
            )
            .is_err()
        );
        let skipped = parse_batch_listing_as(
            ListFormat::Windows,
            NameRule::Skip,
            &one,
            home.as_bytes(),
            100,
        )
        .expect("skip")
        .remove(0);
        let names: Vec<&str> = skipped.iter().map(|e| e.name.as_str()).collect();
        assert_eq!(names, ["AppData", "ign-deploy"]);
    }

    fn windows_fake() -> Arc<FakeSftp> {
        let fake = FakeSftp::new();
        fake.knobs().windows = true;
        fake.knobs().home = Some(WHOME.to_string());
        fake.mkdirs(WDATA);
        fake.mkdirs(WHOME);
        fake.listed.lock().expect("listed").extend([
            WDATA.to_string(),
            WHOME.to_string(),
            "/C:".to_string(),
        ]);
        fake
    }

    fn windows_transport(fake: &Arc<FakeSftp>) -> SftpTransport {
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        SftpTransport::new_windows(shell, WDATA.to_string(), "nodes.master")
    }

    fn windows_tree(fake: &FakeSftp) {
        fake.file(
            &format!("{WRES}/core/ignition/database-connection/CoreDb/config.json"),
            "{\"core\":1}",
        );
        fake.file(
            &format!("{WRES}/production/ignition/database-connection/MES db,1/config.json"),
            "{\"mes\":1}",
        );
        fake.file(
            &format!("{WRES}/production/ignition/database-connection/MES db,1/resource.json"),
            "{\"scope\":\"A\"}",
        );
        fake.file(
            &format!("{WRES}/production/ignition/database-connection/MES db,1/.dot"),
            "d",
        );
        fake.file(
            &format!("{WRES}/local/ignition/database-connection/Override/config.json"),
            "{}",
        );
        fake.put(
            &format!("{WRES}/production/ignition/other-type"),
            Node::Symlink,
        );
    }

    #[tokio::test]
    async fn windows_read_collects_the_same_tree_and_checks_it_against_a_walk() {
        let fake = windows_fake();
        windows_tree(&fake);
        let got = windows_transport(&fake).read(&scope()).await.expect("read");
        assert_eq!(got.core.len(), 1);
        assert_eq!(
            got.mode.len(),
            3,
            "config.json, resource.json and the dot file"
        );
        assert_eq!(got.local_units.len(), 1);
        assert!(
            got.mode
                .contains_key(&rel("ignition/database-connection/MES db,1/.dot"))
        );
        // quick 261104-s3n: no per-folder sftp listing, the walk is the
        // listing
        assert_eq!(ls_lines(&fake), 0, "{:?}", fake.session_lines());
        assert!(fake.knobs().ssh_calls >= 1, "no walk, no completeness");
        // sizes and bytes survive
        assert_eq!(
            got.mode[&rel("ignition/database-connection/MES db,1/config.json")].bytes,
            b"{\"mes\":1}"
        );
    }

    /// WR-03 (quick-261029-ms3): the names-only provider listing is held to
    /// the same completeness rule as a read. A provider directory the sftp
    /// server drops from the listing (C7a-8) is `transport_failed` naming it,
    /// never a provider that is silently missing from a generated manifest;
    /// a reparse point among them is refused as a read refuses it.
    #[tokio::test]
    async fn windows_list_tag_providers_fails_closed_when_the_listing_omits_a_provider() {
        let fake = windows_fake();
        windows_tree(&fake);
        let providers = format!("{WRES}/core/ignition/tag-provider");
        for p in ["default", "MQTT Engine"] {
            fake.file(&format!("{providers}/{p}/config.json"), "{}");
        }
        let t = windows_transport(&fake);
        let calls = fake.knobs().ssh_calls;
        assert_eq!(
            t.list_tag_providers("core").await.expect("complete"),
            ["MQTT Engine", "default"]
        );
        assert!(fake.knobs().ssh_calls > calls, "no walk, no completeness");
        // a collection without the directory needs no walk and lists nothing
        assert!(
            t.list_tag_providers("production")
                .await
                .expect("absent")
                .is_empty()
        );

        fake.knobs().hidden.push(format!("{providers}/MQTT Engine"));
        let err = t.list_tag_providers("core").await.expect_err("omitted");
        assert_eq!(err.code(), "transport_failed");
        let why = detail(err);
        assert!(
            why.contains("did not return") && why.contains("tag-provider/MQTT Engine"),
            "{why}"
        );
        fake.knobs().hidden.clear();

        fake.knobs().reparse.push(format!("{providers}/default"));
        let why = detail(t.list_tag_providers("core").await.expect_err("reparse"));
        assert!(
            why.contains("reparse point") && why.contains("tag-provider/default"),
            "{why}"
        );
    }

    /// C6 (PR #31 review): the names-only cross-check of `list_tag_providers`
    /// lists the direct children of `tag-provider` and nothing below them. A
    /// provider such as Cirrus Link's auto-generated `MQTT Engine` holds a
    /// huge, deep tree; walking it is the slowness and the over-long path this
    /// release removes. The read's own walk (`ENUMERATE`) stays recursive.
    #[tokio::test]
    async fn windows_list_tag_providers_cross_check_lists_direct_children_only() {
        let fake = windows_fake();
        windows_tree(&fake);
        let providers = format!("{WRES}/core/ignition/tag-provider");
        fake.file(&format!("{providers}/default/config.json"), "{}");
        let deep = format!(
            "{providers}/MQTT Engine/Edge Nodes/wh_site1/Processing01/Control/TankState/tags.json"
        );
        fake.file(&deep, "{}");
        // over the limit and dropped by the sftp server: a walk would see it
        fake.knobs().enum_len_for.push((deep.clone(), 262));
        fake.knobs().hidden.push(deep);
        let t = windows_transport(&fake);
        assert_eq!(
            t.list_tag_providers("core").await.expect("complete"),
            ["MQTT Engine", "default"]
        );
        let scripts = fake.knobs().enum_scripts.clone();
        assert_eq!(scripts.len(), 1, "one names-only enumeration");
        assert!(
            !scripts[0].contains("Collections.Stack") && !scripts[0].contains(".Push("),
            "the names-only script must not recurse: {}",
            scripts[0]
        );
        let rels = fake.knobs().enum_rels.clone();
        assert_eq!(rels.len(), 1);
        let mut got = rels[0].clone();
        got.sort();
        assert_eq!(
            got,
            ["MQTT Engine", "default"],
            "no entry below a provider directory is enumerated"
        );

        // a read still walks the subtrees it reads; its names-only listings
        // (quick 261104-s3n) cover only the folders on the path, so no entry
        // below a requested root comes from a non-recursive script
        fake.knobs().enum_scripts.clear();
        windows_transport(&fake).read(&scope()).await.expect("read");
        let scripts = fake.knobs().enum_scripts.clone();
        assert!(
            scripts.iter().any(|s| s.contains("Collections.Stack")),
            "the read keeps the recursive walk"
        );
        let requested: Vec<String> = ["core", "production", "local"]
            .iter()
            .map(|c| windows_native_path(&format!("{WRES}/{c}/ignition/database-connection")))
            .collect();
        for script in scripts.iter().filter(|s| !s.contains("Collections.Stack")) {
            for root in script_roots(script) {
                assert!(
                    !requested
                        .iter()
                        .any(|r| root == *r || root.starts_with(&format!("{r}\\"))),
                    "a names-only listing of {root} is at or below a requested root"
                );
            }
        }
    }

    /// The roots (`$p.r`) of a decoded PowerShell script the fake ran.
    fn script_roots(script: &str) -> Vec<String> {
        use base64::Engine as _;
        let blob = script
            .split("FromBase64String('")
            .nth(1)
            .and_then(|r| r.split('\'').next())
            .expect("parameter blob");
        let params: serde_json::Value = serde_json::from_slice(
            &base64::engine::general_purpose::STANDARD
                .decode(blob)
                .expect("blob"),
        )
        .expect("params json");
        params["r"]
            .as_array()
            .expect("roots")
            .iter()
            .map(|r| r.as_str().expect("root").to_string())
            .collect()
    }

    /// quick 261104-7cy, C3: on a Windows host the full listing of a type
    /// folder is the PowerShell walk (quick 261104-s3n). An entry the sftp
    /// server would omit is listed, a reparse point below is refused, and an
    /// entry at or over `LONG_PATH_LIMIT` that the server drops is listed.
    #[tokio::test]
    async fn windows_list_tree_is_checked_against_a_walk() {
        let fake = windows_fake();
        windows_tree(&fake);
        let ty = format!("{WRES}/production/ignition/tag-definition");
        fake.file(&format!("{ty}/default/Deploy/tags.json"), "{}");
        fake.mkdirs(&format!("{ty}/default/Empty"));
        let t = windows_transport(&fake);
        let dirs = [("ignition".to_string(), "tag-definition".to_string())];
        let calls = fake.knobs().ssh_calls;
        let listed = t.list_tree("production", &dirs).await.expect("complete");
        assert!(fake.knobs().ssh_calls > calls, "no walk, no completeness");
        let got: Vec<(&str, bool)> = listed[0]
            .entries
            .iter()
            .map(|e| (e.path.as_str(), e.dir))
            .collect();
        assert_eq!(
            got,
            [
                ("ignition/tag-definition/default", true),
                ("ignition/tag-definition/default/Deploy", true),
                ("ignition/tag-definition/default/Deploy/tags.json", false),
                ("ignition/tag-definition/default/Empty", true),
            ]
        );

        // over the limit and dropped by the server: listed, not dropped
        let long = format!("{ty}/Elsewhere/tags.json");
        fake.file(&long, "{}");
        fake.knobs().enum_len_for.push((long.clone(), 262));
        fake.knobs().hidden.push(long);
        let listed = t.list_tree("production", &dirs).await.expect("long listed");
        assert!(
            listed[0].entries.iter().any(|e| e.path.as_str()
                == "ignition/tag-definition/Elsewhere/tags.json"
                && !e.dir),
            "{listed:?}"
        );
        fake.knobs().hidden.clear();
        fake.knobs().enum_len_for.clear();

        // a short entry the sftp server would drop is listed: sftp is not
        // asked
        fake.knobs().hidden.push(format!("{ty}/default/Empty"));
        let listed = t.list_tree("production", &dirs).await.expect("listed");
        assert!(
            listed[0]
                .entries
                .iter()
                .any(|e| e.path.as_str() == "ignition/tag-definition/default/Empty" && e.dir),
            "{listed:?}"
        );
        fake.knobs().hidden.clear();

        fake.knobs().reparse.push(format!("{ty}/default/Deploy"));
        let why = detail(t.list_tree("production", &dirs).await.expect_err("reparse"));
        assert!(
            why.contains("reparse point") && why.contains("default/Deploy"),
            "{why}"
        );
    }

    /// quick 261104-ava, N2: a requested type folder the Windows path listing
    /// did not return but the host holds is `transport_failed` naming it. A
    /// requested folder the host does not hold either is no error, and the
    /// probe shares the found roots' one walk call. Since quick 261104-s3n
    /// sftp is not asked, so a folder the sftp server would drop is listed.
    #[tokio::test]
    async fn windows_list_tree_refuses_a_type_folder_made_while_it_is_read() {
        let fake = windows_fake();
        windows_tree(&fake);
        let ty = format!("{WRES}/production/ignition/tag-definition");
        fake.file(&format!("{ty}/default/Deploy/tags.json"), "{}");
        let t = windows_transport(&fake);
        let dirs = [
            ("ignition".to_string(), "tag-definition".to_string()),
            ("ignition".to_string(), "tag-type-definition".to_string()),
        ];

        // (a) the absent type folder is left out: one call lists the path,
        // one walks the found root and probes the missing folder
        let calls = fake.knobs().enum_scripts.len();
        let listed = t.list_tree("production", &dirs).await.expect("complete");
        let types: Vec<&str> = listed.iter().map(|l| l.ty.as_str()).collect();
        assert_eq!(types, ["tag-definition"]);
        assert_eq!(
            fake.knobs().enum_scripts.len(),
            calls + 2,
            "one path listing and one walk for the found root and the missing folder"
        );

        // (b) the type folder itself is one the sftp server would drop: it is
        // listed, sftp is not asked
        fake.knobs().hidden.push(ty.clone());
        let listed = t.list_tree("production", &dirs).await.expect("listed");
        assert_eq!(listed[0].ty, "tag-definition");
        fake.knobs().hidden.clear();

        // (c) its `ignition` parent instead
        fake.knobs()
            .hidden
            .push(format!("{WRES}/production/ignition"));
        let listed = t.list_tree("production", &dirs).await.expect("listed");
        assert_eq!(listed[0].ty, "tag-definition");
        fake.knobs().hidden.clear();

        // (d) N2: the missing folder is made after the path listing ran
        fake.knobs().appear_after_first_exec =
            Some(format!("{WRES}/production/ignition/tag-type-definition"));
        let err = t
            .list_tree("production", &dirs)
            .await
            .expect_err("made while it was read");
        assert_eq!(err.code(), "transport_failed");
        let why = detail(err);
        assert!(
            why.contains("did not return")
                && why.contains("production/ignition/tag-type-definition"),
            "{why}"
        );
    }

    /// quick 261104-dnb, M5: `read` probes its requested roots the Windows
    /// path listing did not return, as `list_tree` does. A root the host
    /// holds is `transport_failed` naming it, never read as empty; a root the
    /// host does not hold either is no error. Since quick 261104-s3n sftp is
    /// not asked, so a root the sftp server would drop is read.
    #[tokio::test]
    async fn windows_read_refuses_a_requested_root_made_while_it_is_read() {
        let fake = windows_fake();
        windows_tree(&fake);
        let t = windows_transport(&fake);
        // `tag-type-definition` exists in no collection on the host
        let scope = Scope {
            mode: "production".to_string(),
            types: vec![
                ("ignition".to_string(), "database-connection".to_string()),
                ("ignition".to_string(), "tag-type-definition".to_string()),
            ],
            tag_providers: Vec::new(),
        };

        // (a) nothing hidden: the root the host lacks too is no error
        let got = t.read(&scope).await.expect("complete");
        assert_eq!(got.mode.len(), 3, "the mode files are read");

        // (b) the mode root is one the sftp server would drop: it is read
        fake.knobs()
            .hidden
            .push(format!("{WRES}/production/ignition/database-connection"));
        let got = t.read(&scope).await.expect("mode root read");
        assert_eq!(got.mode.len(), 3);
        fake.knobs().hidden.clear();

        // (c) the `local` collection's `ignition` folder instead
        fake.knobs().hidden.push(format!("{WRES}/local/ignition"));
        let got = t.read(&scope).await.expect("local root read");
        assert_eq!(got.local_units.len(), 1);
        fake.knobs().hidden.clear();

        // (d) N2: a requested root is made after the path listing ran
        fake.knobs().appear_after_first_exec =
            Some(format!("{WRES}/local/ignition/tag-type-definition"));
        let err = t.read(&scope).await.expect_err("made while it was read");
        assert_eq!(err.code(), "transport_failed");
        let why = detail(err);
        assert!(
            why.contains("did not return") && why.contains("local/ignition/tag-type-definition"),
            "{why}"
        );
    }

    /// quick 261104-ava, N3, re-expressed for quick 261104-s3n: every entry
    /// the Windows walk reports, long or not, and every name the path listing
    /// returns is charged once to `READ_MAX_ENTRIES`; at the cap the listing
    /// is Ok and one more is the exact detail.
    #[tokio::test]
    async fn windows_discover_charges_every_walked_entry_once() {
        let fake = windows_fake();
        windows_tree(&fake);
        let ty = format!("{WRES}/production/ignition/tag-definition");
        for name in ["a.json", "b.json"] {
            let path = format!("{ty}/{name}");
            fake.file(&path, "{}");
            fake.knobs().enum_len_for.push((path.clone(), 262));
            fake.knobs().hidden.push(path);
        }
        let folder = format!("{ty}/Empty");
        fake.mkdirs(&folder);
        fake.knobs().enum_len_for.push((folder.clone(), 262));
        fake.knobs().hidden.push(folder);
        let listed = format!("{ty}/c.json");
        fake.file(&listed, "{}");
        fake.knobs().enum_len_for.push((listed, 262));
        fake.file(&format!("{ty}/short.json"), "{}");
        let t = windows_transport(&fake);
        let mut chain: Vec<String> = RESOURCES.iter().map(|c| (*c).to_string()).collect();
        chain.extend(["production", "ignition", "tag-definition"].map(String::from));
        let chains: BTreeSet<Vec<String>> = [chain].into_iter().collect();
        let mut budget = Budget::default();
        let (found, long) = t
            .discover_windows(&chains, &mut budget)
            .await
            .expect("discover");
        assert_eq!(long.len(), 4, "{long:?}");
        assert_eq!(found.files.len(), 1, "short.json");
        // data: config; config: resources; resources: core, local,
        // production; production: ignition; ignition: database-connection,
        // other-type, tag-definition; then the five walked entries
        let charged = budget.entries;
        assert_eq!(charged, 1 + 1 + 3 + 1 + 3 + 5, "each entry once");

        // (a) at the cap
        let mut budget = Budget {
            entries: READ_MAX_ENTRIES - charged,
            lenient: false,
        };
        t.discover_windows(&chains, &mut budget)
            .await
            .expect("at the cap");
        assert_eq!(budget.entries, READ_MAX_ENTRIES);

        // (b) one more is past the cap, with the exact detail
        let mut budget = Budget {
            entries: READ_MAX_ENTRIES - charged + 1,
            lenient: false,
        };
        let err = t
            .discover_windows(&chains, &mut budget)
            .await
            .err()
            .expect("past the cap");
        assert_eq!(err.code(), "transport_failed");
        let why = detail(err);
        assert_eq!(
            why,
            format!(
                "more than {READ_MAX_ENTRIES} entries under the data directory (at config/resources/production/ignition/tag-definition)"
            )
        );
    }

    /// quick 261104-s3n: the folders on a Windows read's path are held to
    /// the rules `discover` held them to, now from one PowerShell listing: a
    /// reparse point or a non-directory on the path is refused naming it,
    /// and a missing data directory, `config` or `config/resources` is a
    /// failure, never an empty tree. A reparse point on the path is new
    /// strictness: the sftp listing showed a junction there as a directory.
    #[tokio::test]
    async fn windows_read_refuses_a_bad_folder_on_its_path() {
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs()
            .reparse
            .push(format!("{WRES}/production/ignition"));
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("reparse on the path"),
        );
        assert!(
            why.contains("reparse point") && why.contains("config/resources/production/ignition"),
            "{why}"
        );

        let fake = windows_fake();
        fake.file(&format!("{WRES}/core/ignition"), "not a folder");
        fake.mkdirs(&format!("{WRES}/production"));
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("file on the path"),
        );
        assert_eq!(why, "config/resources/core/ignition is not a directory");

        let fake = windows_fake();
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("no config"),
        );
        assert_eq!(
            why,
            "config cannot be read (not found); is data_dir a gateway data directory?"
        );
        fake.mkdirs(&format!("{WDATA}/config"));
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("no resources"),
        );
        assert_eq!(
            why,
            "config/resources cannot be read (not found); is data_dir a gateway data directory?"
        );

        let fake = windows_fake();
        windows_tree(&fake);
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        let t = SftpTransport::new_windows(shell, "/C:/gw/missing".to_string(), "nodes.master");
        let err = t.read(&scope()).await.expect_err("no data dir");
        assert_eq!(err.code(), "transport_failed");
        assert!(detail(err).contains("the data directory cannot be read"));
        let err = t
            .list_tree(
                "core",
                &[("ignition".to_string(), "database-connection".to_string())],
            )
            .await
            .expect_err("no data dir");
        assert!(detail(err).contains("the data directory cannot be read"));
    }

    /// quick 261104-s3n: a requested folder the path listing found but the
    /// walk does not is "changed while it was read", never an empty tree.
    #[tokio::test]
    async fn windows_read_fails_when_a_listed_root_vanishes_before_the_walk() {
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().vanish_after_first_exec =
            Some(format!("{WRES}/production/ignition/database-connection"));
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("vanished"),
        );
        assert!(
            why.contains("config/resources/production/ignition/database-connection")
                && why.contains("changed while it was read"),
            "{why}"
        );
    }

    /// quick 261104-s3n: a walked name that can reach a `get` line (below
    /// `LONG_PATH_LIMIT`) must be safe in a batch line; the same name at or
    /// over the limit is never downloaded and is listed as unreadable. Two
    /// names equal ignoring case are refused.
    #[tokio::test]
    async fn windows_read_holds_walked_names_to_the_batch_line_gate() {
        let odd = format!("{WRES}/production/ignition/database-connection/MES db,1/odd[1].json");
        let fake = windows_fake();
        windows_tree(&fake);
        fake.file(&odd, "{}");
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("unsafe name"),
        );
        assert!(
            why.contains("\"odd[1].json\" is unsafe") && why.contains("glob"),
            "{why}"
        );

        fake.knobs().enum_len_for.push((odd.clone(), 262));
        let got = windows_transport(&fake)
            .read(&scope())
            .await
            .expect("long, so listed");
        assert_eq!(
            got.unreadable,
            vec![Unreadable {
                collection: "mode".to_string(),
                path: rel("ignition/database-connection/MES db,1/odd[1].json"),
                length: 262,
            }]
        );
        never_fetched(&fake, "odd[1]");

        let fake = windows_fake();
        windows_tree(&fake);
        fake.file(
            &format!("{WRES}/production/ignition/database-connection/MES db,1/Config.json"),
            "{}",
        );
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("case twins"),
        );
        assert!(why.contains("listed twice, ignoring case"), "{why}");
    }

    /// quick 261104-s3n: a walked tree deeper than `READ_MAX_DEPTH` is the
    /// existing depth refusal.
    #[tokio::test]
    async fn windows_read_refuses_a_walked_tree_deeper_than_the_cap() {
        let fake = windows_fake();
        windows_tree(&fake);
        let mut deep = format!("{WRES}/production/ignition/database-connection/Deep");
        for _ in 0..READ_MAX_DEPTH {
            deep.push_str("/d");
        }
        fake.mkdirs(&deep);
        let why = detail(
            windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("too deep"),
        );
        assert_eq!(
            why,
            format!("the tree under config/resources is deeper than {READ_MAX_DEPTH} levels")
        );
    }

    /// quick 261104-s3n: a lenient budget leaves out an unsafe walked name
    /// and everything below it, and an unsafe name on the path, instead of
    /// refusing the listing.
    #[tokio::test]
    async fn windows_discover_lenient_skips_an_unsafe_name_and_its_subtree() {
        let fake = windows_fake();
        windows_tree(&fake);
        let mes = format!("{WRES}/production/ignition/database-connection/MES db,1");
        fake.file(&format!("{mes}/odd[1].json"), "{}");
        fake.file(&format!("{mes}/bad{{x}}/inner.json"), "{}");
        fake.mkdirs(&format!("{WDATA}/we[ird]"));
        let t = windows_transport(&fake);
        let mut chain: Vec<String> = RESOURCES.iter().map(|c| (*c).to_string()).collect();
        chain.extend(["production", "ignition", "database-connection"].map(String::from));
        let chains: BTreeSet<Vec<String>> = [chain].into_iter().collect();
        let (found, long) = t
            .discover_windows(&chains, &mut Budget::lenient())
            .await
            .expect("lenient");
        let mut files: Vec<&str> = found.files.iter().map(|f| f.rel.as_str()).collect();
        files.sort();
        assert_eq!(
            files,
            [
                "ignition/database-connection/MES db,1/.dot",
                "ignition/database-connection/MES db,1/config.json",
                "ignition/database-connection/MES db,1/resource.json",
            ]
        );
        assert!(
            found
                .dirs
                .iter()
                .all(|d| !d.iter().any(|c| c.contains('{'))),
            "{:?}",
            found.dirs
        );
        assert!(long.is_empty());
        // the default budget refuses the same tree
        let why = detail(
            t.discover_windows(&chains, &mut Budget::default())
                .await
                .err()
                .expect("strict"),
        );
        assert!(why.contains("is unsafe"), "{why}");
    }

    fn windows_read_failure(fake: &Arc<FakeSftp>) -> String {
        let err = tokio::runtime::Builder::new_current_thread()
            .enable_time()
            .build()
            .expect("runtime")
            .block_on(windows_transport(fake).read(&scope()))
            .expect_err("the read must fail closed");
        detail(err)
    }

    /// C7a-8: the sftp server drops entries from a listing with exit 0. A
    /// Windows read never asks it to list (quick 261104-s3n): the walk sees
    /// the entry and its bytes are read.
    #[tokio::test]
    async fn windows_read_reads_an_entry_the_sftp_listing_would_omit() {
        let fake = windows_fake();
        windows_tree(&fake);
        let path = format!("{WRES}/production/{MES_CONFIG}");
        fake.knobs().hidden.push(path);
        let got = windows_transport(&fake).read(&scope()).await.expect("read");
        assert_eq!(got.mode[&rel(MES_CONFIG)].bytes, b"{\"mes\":1}");
        assert!(got.unreadable.is_empty());
    }

    #[test]
    fn windows_read_refuses_reparse_points() {
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().reparse.push(format!(
            "{WRES}/production/ignition/database-connection/MES db,1"
        ));
        let why = windows_read_failure(&fake);
        assert!(
            why.contains("reparse point") && why.contains("MES db,1"),
            "{why}"
        );
    }

    /// No `get` line and no uploaded read list (quick 261105-dl1) names
    /// `needle`.
    fn never_fetched(fake: &FakeSftp, needle: &str) {
        assert!(
            gets(fake).iter().all(|l| !l.contains(needle)),
            "{:?}",
            gets(fake)
        );
        for list in &fake.knobs().bundle_lists {
            let list = String::from_utf8_lossy(list);
            assert!(
                !list.contains(needle),
                "the read list names {needle}: {list}"
            );
        }
    }

    /// The `get` lines of every session.
    fn gets(fake: &FakeSftp) -> Vec<String> {
        session_lines_flat(fake)
            .into_iter()
            .filter(|l| l.starts_with("get \""))
            .collect()
    }

    const MES_CONFIG: &str = "ignition/database-connection/MES db,1/config.json";

    #[tokio::test]
    async fn windows_read_unreadable_over_limit_hidden_mode_file() {
        // C7a-8: the server drops it from the listing; only the enumeration
        // sees it. Unreadable, never absent, never fetched.
        let fake = windows_fake();
        windows_tree(&fake);
        let long = format!("{WRES}/production/{MES_CONFIG}");
        fake.knobs().hidden.push(long.clone());
        fake.knobs().enum_len_for.push((long, 262));
        let got = windows_transport(&fake).read(&scope()).await.expect("read");
        assert_eq!(
            got.unreadable,
            vec![Unreadable {
                collection: "mode".to_string(),
                path: rel(MES_CONFIG),
                length: 262,
            }]
        );
        assert!(!got.mode.contains_key(&rel(MES_CONFIG)));
        assert_eq!(got.mode.len(), 2, "resource.json and the dot file");
        assert_eq!(got.core.len(), 1);
        never_fetched(&fake, "MES db,1/config.json");
    }

    #[tokio::test]
    async fn windows_read_unreadable_over_limit_listed_file_is_not_fetched() {
        // 240 to 259 characters: sftp lists it, the reader still keeps its
        // margin and never downloads it
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs()
            .enum_len_for
            .push((format!("{WRES}/production/{MES_CONFIG}"), 245));
        let got = windows_transport(&fake).read(&scope()).await.expect("read");
        assert_eq!(got.unreadable.len(), 1, "{:?}", got.unreadable);
        assert_eq!(got.unreadable[0].collection, "mode");
        assert_eq!(got.unreadable[0].path, rel(MES_CONFIG));
        assert_eq!(got.unreadable[0].length, 245);
        assert!(!got.mode.contains_key(&rel(MES_CONFIG)));
        never_fetched(&fake, "MES db,1/config.json");
    }

    #[tokio::test]
    async fn windows_read_unreadable_core_and_local_entries() {
        let fake = windows_fake();
        windows_tree(&fake);
        let core = "ignition/database-connection/CoreDb/config.json";
        fake.knobs()
            .enum_len_for
            .push((format!("{WRES}/core/{core}"), 250));
        let local = format!("{WRES}/local/ignition/database-connection/Override/config.json");
        fake.knobs().hidden.push(local.clone());
        fake.knobs().enum_len_for.push((local, 250));
        let got = windows_transport(&fake).read(&scope()).await.expect("read");
        // a core file is unreadable as `core`
        assert_eq!(
            got.unreadable,
            vec![Unreadable {
                collection: "core".to_string(),
                path: rel(core),
                length: 250,
            }]
        );
        assert!(got.core.is_empty(), "{:?}", got.core.keys());
        // a local file is only ever a name: its unit is still listed
        assert_eq!(
            got.local_units,
            vec!["ignition/database-connection/Override".to_string()]
        );
    }

    #[tokio::test]
    async fn windows_read_unreadable_when_every_entry_reaches_the_limit() {
        // every file and directory at LONG_PATH_LIMIT: the read is Ok, every
        // file is unreadable, nothing is read as absent
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().enum_len = Some(LONG_PATH_LIMIT + LONG_PATH_PREFIX.len());
        let got = windows_transport(&fake).read(&scope()).await.expect("read");
        assert!(got.core.is_empty() && got.mode.is_empty(), "{got:?}");
        let seen: Vec<(&str, &str, usize)> = got
            .unreadable
            .iter()
            .map(|u| (u.collection.as_str(), u.path.as_str(), u.length))
            .collect();
        assert_eq!(
            seen,
            [
                (
                    "core",
                    "ignition/database-connection/CoreDb/config.json",
                    LONG_PATH_LIMIT
                ),
                (
                    "mode",
                    "ignition/database-connection/MES db,1/.dot",
                    LONG_PATH_LIMIT
                ),
                ("mode", MES_CONFIG, LONG_PATH_LIMIT),
                (
                    "mode",
                    "ignition/database-connection/MES db,1/resource.json",
                    LONG_PATH_LIMIT
                ),
            ]
        );
        assert_eq!(got.local_units.len(), 1);
        assert!(gets(&fake).is_empty(), "{:?}", gets(&fake));
        // nothing to fetch: no bundle connection and nothing staged
        assert!(fake.knobs().bundle_lists.is_empty());
        assert!(writes(&fake).is_empty(), "{:?}", writes(&fake));
        assert!(staged_left(&fake).is_empty());
    }

    #[test]
    fn windows_read_fails_closed_on_walk_failure_and_disagreement() {
        // the walk itself fails (access denied): never "complete"
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().enum_fail = Some(1);
        let why = windows_read_failure(&fake);
        assert!(
            why.contains("cannot check the listing for completeness"),
            "{why}"
        );
        assert!(why.contains("Access to the path is denied"), "{why}");
        assert!(!why.contains("<S S="), "{why}");

        // the walk reports a file the host cannot bundle: BUNDLE answers
        // `missing` naming it (quick 261105-dl1)
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().enum_extra.push((
            format!("{WRES}/production/ignition/database-connection/MES db,1/extra.json"),
            false,
            3,
        ));
        let why = windows_read_failure(&fake);
        assert!(
            why.contains("MES db,1/extra.json is not on the host any more"),
            "{why}"
        );
        assert!(staged_left(&fake).is_empty(), "{:?}", staged_left(&fake));

        // the walk and the bundled bytes disagree on a size: BUNDLE answers
        // `changed`
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().walk_size_lie = Some(("MES db,1/config.json".to_string(), 99));
        let why = windows_read_failure(&fake);
        assert!(
            why.contains("MES db,1/config.json") && why.contains("was listed as 99"),
            "{why}"
        );
        assert!(staged_left(&fake).is_empty(), "{:?}", staged_left(&fake));

        // an entry outside every scoped root is not part of the check
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().enum_extra.push((
            format!("{WRES}/production/ignition/other-type/ignored.json"),
            false,
            1,
        ));
        let got = tokio::runtime::Builder::new_current_thread()
            .enable_time()
            .build()
            .expect("runtime")
            .block_on(windows_transport(&fake).read(&scope()));
        assert!(got.is_ok(), "{:?}", got.err());
    }

    // ---- production scale (quick 261104-s3n) -------------------------------
    //
    // The generator in `tests/common/prod_shape.rs` builds the recorded shape
    // of a production pair's core tag folders. On a Windows host a read of it
    // must open a fixed number of ssh connections, the same as for a tiny
    // tree: one sftp `ls` per folder cost about 100 minutes there.

    /// The sftp spelling of the typical Windows data directory.
    fn typical_wdata() -> String {
        format!(
            "/{}",
            prod_shape::TYPICAL_WINDOWS_DATA_DIR.replace('\\', "/")
        )
    }

    /// [`windows_fake`] with its data directory at `data`.
    fn windows_fake_at(data: &str) -> Arc<FakeSftp> {
        let fake = windows_fake();
        fake.mkdirs(data);
        fake.listed.lock().expect("listed").extend([
            data.to_string(),
            "/C:".to_string(),
            WHOME.to_string(),
        ]);
        fake
    }

    fn windows_transport_at(fake: &Arc<FakeSftp>, data: &str) -> SftpTransport {
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            fake.clone(),
            Duration::from_secs(30),
        );
        SftpTransport::new_windows(shell, data.to_string(), "nodes.master")
    }

    /// Put `entries` into `config/resources/<collection>/ignition/<ty>` below
    /// `data`. An entry whose native path reaches 260 characters is hidden
    /// from sftp listings, as the real server drops it (C7a-8).
    fn seed_shape(
        fake: &FakeSftp,
        data: &str,
        collection: &str,
        ty: &str,
        entries: &[prod_shape::Entry],
    ) {
        let type_dir = format!("{data}/config/resources/{collection}/ignition/{ty}");
        fake.mkdirs(&type_dir);
        for entry in entries {
            let path = format!("{type_dir}/{}", entry.rel);
            if entry.dir {
                fake.mkdirs(&path);
            } else {
                fake.put(
                    &path,
                    Node::File(prod_shape::content(&entry.rel, entry.size)),
                );
            }
            if windows_native_path(&path).chars().count() >= 260 {
                fake.knobs().hidden.push(path);
            }
        }
    }

    /// Every ssh connection the fake saw: sftp sessions and PowerShell calls.
    fn connections(fake: &FakeSftp) -> usize {
        fake.session_lines().len() + fake.knobs().ssh_calls
    }

    /// The sftp sessions that downloaded (carry a `get` line).
    fn download_sessions(fake: &FakeSftp) -> usize {
        fake.session_lines()
            .iter()
            .filter(|s| s.iter().any(|l| l.starts_with("get \"")))
            .count()
    }

    fn ls_lines(fake: &FakeSftp) -> usize {
        session_lines_flat(fake)
            .iter()
            .filter(|l| l.starts_with("@ls"))
            .count()
    }

    const PROD_TYPES: [&str; 2] = ["tag-definition", "tag-type-definition"];

    /// A Windows fake at the typical data directory holding `tag_definition`
    /// and the production `tag-type-definition` tree in `core`.
    fn prod_shape_fake(tag_definition: &[prod_shape::Entry]) -> (Arc<FakeSftp>, String) {
        prod_shape_fake_with(
            tag_definition,
            &prod_shape::tag_type_definition(prod_shape::Bytes::Small),
        )
    }

    /// [`prod_shape_fake`] with both trees given.
    fn prod_shape_fake_with(
        tag_definition: &[prod_shape::Entry],
        tag_type_definition: &[prod_shape::Entry],
    ) -> (Arc<FakeSftp>, String) {
        let data = typical_wdata();
        let fake = windows_fake_at(&data);
        fake.mkdirs(&format!("{data}/config/resources/production"));
        seed_shape(&fake, &data, "core", PROD_TYPES[0], tag_definition);
        seed_shape(&fake, &data, "core", PROD_TYPES[1], tag_type_definition);
        (fake, data)
    }

    #[tokio::test]
    async fn windows_prod_shape_list_tree_uses_constant_connections() {
        let mut counts = Vec::new();
        for tree in [
            prod_shape::tag_definition(prod_shape::Bytes::Small),
            prod_shape::tiny_tag_definition(),
        ] {
            let (fake, data) = prod_shape_fake(&tree);
            let t = windows_transport_at(&fake, &data);
            let dirs = PROD_TYPES.map(|ty| ("ignition".to_string(), ty.to_string()));
            let listed = t.list_tree("core", &dirs).await.expect("list_tree");

            // identity first: every generated entry, long ones included
            let types: Vec<&str> = listed.iter().map(|l| l.ty.as_str()).collect();
            assert_eq!(types, PROD_TYPES);
            let trees = [
                tree.clone(),
                prod_shape::tag_type_definition(prod_shape::Bytes::Small),
            ];
            for (listing, (ty, want)) in listed.iter().zip(PROD_TYPES.iter().zip(&trees)) {
                let got: Vec<(String, bool)> = listing
                    .entries
                    .iter()
                    .map(|e| (e.path.as_str().to_string(), e.dir))
                    .collect();
                let mut want: Vec<(String, bool)> = want
                    .iter()
                    .map(|e| (format!("ignition/{ty}/{}", e.rel), e.dir))
                    .collect();
                want.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes()));
                assert_eq!(got.len(), want.len(), "{ty}");
                assert_eq!(got, want, "{ty}");
            }

            let n = connections(&fake);
            eprintln!(
                "list_tree of {} entries: {n} connections ({} sftp sessions, {} PowerShell calls, {} `ls` lines), sessions() = {}",
                tree.len(),
                fake.session_lines().len(),
                fake.knobs().ssh_calls,
                ls_lines(&fake),
                t.sessions()
            );
            assert!(n <= 3, "list_tree opened {n} connections");
            assert_eq!(ls_lines(&fake), 0, "no per-folder sftp listing");
            assert_eq!(t.sessions(), n, "sessions() counts every connection");
            counts.push(n);
        }
        assert_eq!(counts[0], counts[1], "O(1) in tree size");
    }

    #[tokio::test]
    async fn windows_prod_shape_read_uses_constant_connections() {
        let mut counts = Vec::new();
        for tree in [
            prod_shape::tag_definition(prod_shape::Bytes::Small),
            prod_shape::tiny_tag_definition(),
        ] {
            let (fake, data) = prod_shape_fake(&tree);
            let t = windows_transport_at(&fake, &data);
            let scope = Scope {
                mode: "production".to_string(),
                types: Vec::new(),
                tag_providers: vec![prod_shape::PROVIDER.to_string()],
            };
            let got = t.read(&scope).await.expect("read");

            // identity first: every short file with its bytes, every long
            // file unreadable with its length
            let trees = [
                tree.clone(),
                prod_shape::tag_type_definition(prod_shape::Bytes::Small),
            ];
            let mut want_core: BTreeMap<String, Vec<u8>> = BTreeMap::new();
            let mut want_unreadable: Vec<(String, usize)> = Vec::new();
            for (ty, entries) in PROD_TYPES.iter().zip(&trees) {
                for entry in entries.iter().filter(|e| !e.dir) {
                    let key = format!("ignition/{ty}/{}", entry.rel);
                    let length = prod_shape::windows_full_len("core", ty, &entry.rel);
                    if length < LONG_PATH_LIMIT {
                        want_core.insert(key, prod_shape::content(&entry.rel, entry.size));
                    } else {
                        want_unreadable.push((key, length));
                    }
                }
            }
            let got_core: BTreeMap<String, Vec<u8>> = got
                .core
                .iter()
                .map(|(k, v)| (k.as_str().to_string(), v.bytes.clone()))
                .collect();
            assert_eq!(got_core.len(), want_core.len());
            assert!(got_core == want_core, "the core bytes differ");
            want_unreadable.sort();
            let mut got_unreadable: Vec<(String, usize)> = got
                .unreadable
                .iter()
                .map(|u| {
                    assert_eq!(u.collection, "core");
                    (u.path.as_str().to_string(), u.length)
                })
                .collect();
            got_unreadable.sort();
            assert_eq!(got_unreadable, want_unreadable);
            assert!(got.mode.is_empty() && got.local_units.is_empty());

            // the listing alone: the enumeration scripts (params `r`) and any
            // `ls` session; the download bound belongs to
            // windows_bundled_prod_shape_read_uses_one_transfer (quick
            // 261105-dl1)
            let all = connections(&fake);
            let scripts = fake
                .knobs()
                .enum_scripts
                .iter()
                .filter(|s| s.contains("FromBase64String"))
                .count();
            let ls_sessions = fake
                .session_lines()
                .iter()
                .filter(|s| s.iter().any(|l| l.starts_with("@ls")))
                .count();
            let listing = scripts + ls_sessions;
            eprintln!(
                "read of {} entries: {listing} listing connections ({ls_sessions} sftp `ls` sessions, {scripts} enumeration calls, {} `ls` lines), {all} connections in all for {} files, sessions() = {}",
                tree.len(),
                ls_lines(&fake),
                want_core.len(),
                t.sessions()
            );
            assert!(listing <= 6, "read opened {listing} listing connections");
            assert_eq!(ls_lines(&fake), 0, "no per-folder sftp listing");
            assert_eq!(t.sessions(), all, "sessions() counts every connection");
            counts.push(listing);
        }
        assert_eq!(counts[0], counts[1], "O(1) in tree size");
    }

    // ---- bundled Windows downloads (quick 261105-dl1) ------------------------
    //
    // A Windows read fetches its files as one archive: the list goes up, one
    // PowerShell call (BUNDLE) zips exactly the listed files into a fresh
    // `.ign-ctl-*` dir below the account home, one sftp `get` downloads it,
    // the same session removes it, and the archive is extracted locally under
    // the hostile-archive rules. One `get` per file cost about 0.37 s each on
    // a Windows OpenSSH server, about 88 minutes at production shape.

    /// The `get` lines of every session.
    fn get_lines(fake: &FakeSftp) -> Vec<String> {
        gets(fake)
    }

    /// Fake paths on the host that are, or are below, a `.ign-ctl-*` dir.
    fn staged_left(fake: &FakeSftp) -> Vec<String> {
        tree_keys(fake)
            .into_iter()
            .filter(|k| k.split('/').any(|c| c.starts_with(".ign-ctl-")))
            .collect()
    }

    /// Every mutating line names a path below `{WHOME}/.ign-ctl-`.
    fn assert_writes_below_home(fake: &FakeSftp) {
        let below = format!("{WHOME}/.ign-ctl-");
        for (command, path) in writes(fake) {
            assert!(
                path.starts_with(&below),
                "`{command}` of {path} is outside the read's staging dir"
            );
        }
    }

    /// The ObservedFiles a Windows read of the generated `core` trees
    /// (`(type, entries)`) must return, built from the generator alone.
    fn reference_observed(trees: &[(&str, &[prod_shape::Entry])]) -> ObservedFiles {
        let mut core: BTreeMap<RelPath, FileBlob> = BTreeMap::new();
        let mut unreadable: Vec<Unreadable> = Vec::new();
        for (ty, entries) in trees {
            for entry in entries.iter().filter(|e| !e.dir) {
                let key = rel(&format!("ignition/{ty}/{}", entry.rel));
                let length = prod_shape::windows_full_len("core", ty, &entry.rel);
                if length < LONG_PATH_LIMIT {
                    let bytes = prod_shape::content(&entry.rel, entry.size);
                    let sha256 = sha256_hex(&bytes);
                    core.insert(key, FileBlob { bytes, sha256 });
                } else {
                    unreadable.push(Unreadable {
                        collection: "core".to_string(),
                        path: key,
                        length,
                    });
                }
            }
        }
        unreadable.sort_by(|a, b| a.path.as_str().as_bytes().cmp(b.path.as_str().as_bytes()));
        ObservedFiles {
            core,
            mode: BTreeMap::new(),
            local_units: Vec::new(),
            unreadable,
        }
    }

    /// Whole-struct equality without printing 48 MB: on a difference, name
    /// the first key that differs.
    fn assert_observed_eq(got: &ObservedFiles, want: &ObservedFiles, what: &str) {
        if got == want {
            return;
        }
        let first = |a: &BTreeMap<RelPath, FileBlob>, b: &BTreeMap<RelPath, FileBlob>| {
            a.keys()
                .chain(b.keys())
                .find(|k| a.get(*k) != b.get(*k))
                .map(|k| k.as_str().to_string())
        };
        panic!(
            "{what}: the ObservedFiles differ: core {} vs {} entries, first differing core key {:?}, mode {:?}, local_units equal {}, unreadable equal {}",
            got.core.len(),
            want.core.len(),
            first(&got.core, &want.core),
            first(&got.mode, &want.mode),
            got.local_units == want.local_units,
            got.unreadable == want.unreadable
        );
    }

    fn prod_scope() -> Scope {
        Scope {
            mode: "production".to_string(),
            types: Vec::new(),
            tag_providers: vec![prod_shape::PROVIDER.to_string()],
        }
    }

    /// The entry of `windows_tree` every hostile archive below changes.
    const VICTIM: &str = "core/ignition/database-connection/CoreDb/config.json";

    /// The archive entries an honest host builds for a read of
    /// [`windows_tree`] under [`scope`].
    fn windows_tree_entries() -> Vec<(String, Vec<u8>)> {
        [
            (VICTIM, "{\"core\":1}"),
            ("production/ignition/database-connection/MES db,1/.dot", "d"),
            (
                "production/ignition/database-connection/MES db,1/config.json",
                "{\"mes\":1}",
            ),
            (
                "production/ignition/database-connection/MES db,1/resource.json",
                "{\"scope\":\"A\"}",
            ),
        ]
        .map(|(name, body)| (name.to_string(), body.as_bytes().to_vec()))
        .to_vec()
    }

    /// [`windows_tree_entries`] with [`VICTIM`] named `name` instead.
    fn entries_with_victim_named(name: &str) -> Vec<(String, Vec<u8>)> {
        windows_tree_entries()
            .into_iter()
            .map(|(n, b)| {
                if n == VICTIM {
                    (name.to_string(), b)
                } else {
                    (n, b)
                }
            })
            .collect()
    }

    /// A Windows fake holding [`windows_tree`] whose BUNDLE answers with
    /// `archive`.
    fn hostile_fake(archive: Vec<u8>) -> Arc<FakeSftp> {
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().bundle_archive = Some(archive);
        fake
    }

    /// A Windows read that must fail closed with `transport_failed`: its
    /// detail, after checking that the host holds no staging dir and that no
    /// archive the fake delivered is left on this machine.
    async fn bundled_read_failure(fake: &Arc<FakeSftp>) -> String {
        let err = windows_transport(fake)
            .read(&scope())
            .await
            .expect_err("the read must fail closed");
        let why = detail(err);
        assert!(
            staged_left(fake).is_empty(),
            "{why}: the host still holds {:?}",
            staged_left(fake)
        );
        for local in fake.downloads.lock().expect("downloads").iter() {
            assert!(!local.exists(), "{why}: {} was left", local.display());
        }
        why
    }

    /// BRIEF test 2: a Windows read of the production-shape tree moves its
    /// files with one `get` and a fixed number of ssh connections in total,
    /// the same as for the tiny tree. One `get` per file was about 14,270.
    #[tokio::test]
    async fn windows_bundled_prod_shape_read_uses_one_transfer() {
        let whole = std::time::Instant::now();
        let mut counts = Vec::new();
        for (tag_definition, tag_type) in [
            (
                prod_shape::tag_definition(prod_shape::Bytes::Prod),
                prod_shape::tag_type_definition(prod_shape::Bytes::Prod),
            ),
            (
                prod_shape::tiny_tag_definition(),
                prod_shape::tag_type_definition(prod_shape::Bytes::Small),
            ),
        ] {
            let started = std::time::Instant::now();
            let (fake, data) = prod_shape_fake_with(&tag_definition, &tag_type);
            let t = windows_transport_at(&fake, &data);
            let got = t.read(&prod_scope()).await.expect("read");

            // identity first
            let want =
                reference_observed(&[(PROD_TYPES[0], &tag_definition), (PROD_TYPES[1], &tag_type)]);
            assert_observed_eq(&got, &want, "bundled prod-shape read");

            let gets = get_lines(&fake).len();
            let n = connections(&fake);
            eprintln!(
                "read of {} + {} entries ({} files, {} bytes): {gets} `get` lines, {n} connections ({} sftp sessions, {} PowerShell calls), sessions() = {}, {:.1} s",
                tag_definition.len(),
                tag_type.len(),
                want.core.len(),
                want.core.values().map(|b| b.bytes.len()).sum::<usize>(),
                fake.session_lines().len(),
                fake.knobs().ssh_calls,
                t.sessions(),
                started.elapsed().as_secs_f64()
            );
            assert_eq!(gets, 1, "one `get` for the whole read");
            assert!(n <= 8, "the read opened {n} connections");
            assert_eq!(t.sessions(), n, "sessions() counts every connection");
            assert!(staged_left(&fake).is_empty(), "{:?}", staged_left(&fake));
            assert_writes_below_home(&fake);
            counts.push(n);
        }
        assert_eq!(counts[0], counts[1], "O(1) in tree size");
        assert!(
            whole.elapsed() <= PROD_FAKE_CEILING,
            "{:?}",
            whole.elapsed()
        );
    }

    /// T-dl1-01: entry names the host controls. Each is refused by its name
    /// rule, before membership is checked (the name is never "not listed").
    #[tokio::test]
    async fn windows_bundled_read_refuses_unsafe_entry_names() {
        for (name, want) in [
            (format!("/{VICTIM}"), "is an absolute path"),
            (
                "core/../database-connection/CoreDb/config.json".to_string(),
                "has an empty, `.` or `..` component",
            ),
            (
                "core/./ignition/database-connection/CoreDb/config.json".to_string(),
                "has an empty, `.` or `..` component",
            ),
            (
                "core//ignition/database-connection/CoreDb/config.json".to_string(),
                "has an empty, `.` or `..` component",
            ),
            (format!("C:/{VICTIM}"), "names a drive or a stream"),
            (format!("{VICTIM}:evil"), "names a drive or a stream"),
            (VICTIM.replace('/', "\\"), "holds a backslash"),
            (format!("{VICTIM}/"), "is a directory entry"),
        ] {
            let fake = hostile_fake(zip_of(&entries_with_victim_named(&name)));
            let why = bundled_read_failure(&fake).await;
            assert!(why.contains(want), "{name:?}: {why}");
            assert!(!why.contains("which was not listed"), "{name:?}: {why}");
        }
        // a name that is not UTF-8
        let placeholder = VICTIM.replace("config", "confiX");
        let mut zip = zip_of(&entries_with_victim_named(&placeholder));
        let mut raw = placeholder.clone().into_bytes();
        let at = raw.iter().position(|b| *b == b'X').expect("X");
        raw[at] = 0xff;
        zip_rename(&mut zip, placeholder.as_bytes(), &raw);
        let fake = hostile_fake(zip);
        let why = bundled_read_failure(&fake).await;
        assert!(why.contains("is not UTF-8"), "{why}");
    }

    /// T-dl1-02: entry types, flags, membership and archive framing.
    #[tokio::test]
    async fn windows_bundled_read_refuses_unlisted_or_irregular_entries() {
        let honest = zip_of(&windows_tree_entries());
        let mut cases: Vec<(&str, Vec<u8>, &str)> = Vec::new();
        for (what, mode) in [
            ("symlink", 0o120_777),
            ("char device", 0o020_644),
            ("block device", 0o060_644),
            ("socket", 0o140_644),
            ("fifo", 0o010_644),
            ("directory bits", 0o040_755),
            ("unix-made, no type bits", 0o644),
        ] {
            let mut zip = honest.clone();
            zip_set_mode(&mut zip, VICTIM.as_bytes(), mode);
            cases.push((what, zip, "is not a regular file"));
        }
        let mut encrypted = honest.clone();
        zip_set_encrypted(&mut encrypted, VICTIM.as_bytes());
        cases.push(("encrypted", encrypted, "is encrypted"));
        let mut lzma = honest.clone();
        zip_set_method(&mut lzma, VICTIM.as_bytes(), 14);
        cases.push(("method 14", lzma, "unsupported compression method"));
        let mut unlisted = windows_tree_entries();
        unlisted.push((
            "core/ignition/database-connection/CoreDb/extra.json".to_string(),
            b"{}".to_vec(),
        ));
        cases.push(("unlisted", zip_of(&unlisted), "which was not listed"));
        let twin = VICTIM.replace("config", "confiX");
        let mut duplicate = windows_tree_entries();
        duplicate.push((twin.clone(), b"{\"core\":2}".to_vec()));
        let mut duplicate = zip_of(&duplicate);
        zip_rename(&mut duplicate, twin.as_bytes(), VICTIM.as_bytes());
        cases.push(("duplicate", duplicate, "twice"));
        let missing: Vec<(String, Vec<u8>)> = windows_tree_entries()
            .into_iter()
            .filter(|(n, _)| n != VICTIM)
            .collect();
        cases.push((
            "missing",
            zip_of(&missing),
            "was listed but is not in the read archive",
        ));
        cases.push((
            "garbage",
            b"this is not a zip archive at all".to_vec(),
            "is not a readable zip",
        ));
        cases.push((
            "truncated",
            honest[..honest.len() / 2].to_vec(),
            "is not a readable zip",
        ));
        cases.push((
            "prepended, stale offsets",
            zip_prepend(&honest, b"JUNKJUNK", false),
            "has data before its first entry",
        ));
        cases.push((
            "prepended, moved offsets",
            zip_prepend(&honest, b"JUNKJUNK", true),
            "has data before its first entry",
        ));
        for (what, zip, want) in cases {
            let fake = hostile_fake(zip);
            let why = bundled_read_failure(&fake).await;
            assert!(why.contains(want), "{what}: {why}");
        }
        // control: the shape PowerShell 7 builds on a Unix host (made by
        // Unix, S_IFREG | 0644) and the .NET Framework shape both extract
        for mode in [None, Some(0o100_644)] {
            let mut zip = honest.clone();
            if let Some(mode) = mode {
                zip_set_mode(&mut zip, VICTIM.as_bytes(), mode);
            }
            let fake = hostile_fake(zip);
            let got = windows_transport(&fake).read(&scope()).await.expect("read");
            assert_eq!(
                got.core[&rel("ignition/database-connection/CoreDb/config.json")].bytes,
                b"{\"core\":1}"
            );
        }
    }

    /// T-dl1-03: declared sizes, inflation, the archive bound and delivery.
    #[tokio::test]
    async fn windows_bundled_read_bounds_sizes() {
        // a declared size other than the listed one
        let mut zip = zip_of(&windows_tree_entries());
        zip_set_size(&mut zip, VICTIM.as_bytes(), 11);
        let why = bundled_read_failure(&hostile_fake(zip)).await;
        assert!(
            why.contains("but was listed as") && why.contains("it changed while it was read"),
            "{why}"
        );

        // a bomb: declared as the listed size, inflates to 4 MiB
        let bomb: Vec<(String, Vec<u8>)> = windows_tree_entries()
            .into_iter()
            .map(|(n, b)| {
                if n == VICTIM {
                    (n, vec![b'a'; 4 << 20])
                } else {
                    (n, b)
                }
            })
            .collect();
        let mut zip = zip_of(&bomb);
        eprintln!("the bomb archive is {} bytes", zip.len());
        zip_set_size(&mut zip, VICTIM.as_bytes(), 10);
        let why = bundled_read_failure(&hostile_fake(zip)).await;
        assert!(why.contains("inflates past its listed"), "{why}");

        // a reported archive size over the bound is never downloaded
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().bundle_reply =
            Some(serde_json::json!({ "ok": 1, "n": 4, "z": 1_000_000_000u64 }));
        let why = bundled_read_failure(&fake).await;
        assert!(
            why.contains("more than the") && why.contains("it was not downloaded"),
            "{why}"
        );
        assert!(get_lines(&fake).is_empty(), "{:?}", get_lines(&fake));

        // an archive that arrives at another size than reported
        let honest = zip_of(&windows_tree_entries());
        let fake = hostile_fake(honest.clone());
        fake.knobs().bundle_reply =
            Some(serde_json::json!({ "ok": 1, "n": 4, "z": honest.len() + 1 }));
        let why = bundled_read_failure(&fake).await;
        assert!(why.contains("arrived as"), "{why}");

        // an archive that grows past what was reported while it downloads
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().overshoot = Some(("/read.zip".to_string(), 64 << 20));
        let why = bundled_read_failure(&fake).await;
        assert!(why.contains("it was aborted"), "{why}");
    }

    /// BUNDLE answers the host can give, and a cleanup that fails after.
    #[tokio::test]
    async fn windows_bundled_read_refuses_bad_host_answers() {
        // the script failed: the list and the partial archive are removed
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().bundle_fail = Some(1);
        let why = bundled_read_failure(&fake).await;
        assert!(
            why.contains("cannot bundle the listed files on the host"),
            "{why}"
        );
        assert_eq!(fake.knobs().clean_calls, 1);

        for (reply, want) in [
            (
                serde_json::json!({ "ok": 0, "err": "list" }),
                "does not match its SHA-256",
            ),
            (
                serde_json::json!({ "ok": 0, "err": "missing", "i": 0 }),
                "is not on the host any more; it changed while it was read",
            ),
            (
                // BUNDLE stops one byte past the listed size (quick 261105-cr1)
                serde_json::json!({ "ok": 0, "err": "changed", "i": 0, "n": 1u64 << 40 }),
                "bytes it was listed as; it changed while it was read",
            ),
            (
                serde_json::json!({ "ok": 1 }),
                "the bundle result is malformed",
            ),
            (
                serde_json::json!({ "ok": "1", "n": 4, "z": 10 }),
                "the bundle result is malformed",
            ),
            (
                serde_json::json!({ "ok": 0, "err": "what" }),
                "the bundle result is malformed",
            ),
            (
                serde_json::json!({ "ok": 1, "n": 99, "z": 10 }),
                "the bundle result is malformed",
            ),
        ] {
            let fake = windows_fake();
            windows_tree(&fake);
            fake.knobs().bundle_reply = Some(reply.clone());
            let why = bundled_read_failure(&fake).await;
            assert!(why.contains(want), "{reply}: {why}");
        }

        // too little free space on the archive's drive
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().bundle_free = Some(0);
        let why = bundled_read_failure(&fake).await;
        assert!(why.contains("bytes free"), "{why}");

        // a listed file whose size changed before BUNDLE copied it
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().walk_size_lie = Some(("CoreDb/config.json".to_string(), 99));
        let why = bundled_read_failure(&fake).await;
        assert!(why.contains("but was listed as 99"), "{why}");

        // a cleanup that fails after another failure is named after it
        let fake = windows_fake();
        windows_tree(&fake);
        fake.knobs().bundle_fail = Some(1);
        fake.knobs().clean_fail = true;
        let err = windows_transport(&fake)
            .read(&scope())
            .await
            .expect_err("the read must fail closed");
        let why = detail(err);
        assert!(
            why.contains("cannot bundle the listed files on the host")
                && why.contains("; the read staging directory")
                && why.contains("could not be removed"),
            "{why}"
        );
    }

    /// P1: a Windows read stages its archive below the account home, never
    /// at or below the data directory.
    #[tokio::test]
    async fn windows_bundled_read_stages_only_below_the_home() {
        for home in [
            format!("{WDATA}/svc"),
            "/C:/GW/Data/svc".to_string(),
            WDATA.to_string(),
        ] {
            let fake = windows_fake();
            windows_tree(&fake);
            fake.knobs().home = Some(home.clone());
            let err = windows_transport(&fake)
                .read(&scope())
                .await
                .expect_err("a home inside the data directory is refused");
            let why = detail(err);
            assert!(
                why.contains("is inside the data directory"),
                "{home}: {why}"
            );
            assert!(writes(&fake).is_empty(), "{home}: {:?}", writes(&fake));
        }
    }

    /// BRIEF test 4 baseline: a Windows read of the production-shape tree
    /// with production bytes equals the reference built from the generator
    /// (bytes, hashes, no mode or local entries, the long files unreadable).
    /// Passes on the per-file path and on the bundled path.
    #[tokio::test]
    async fn windows_prod_shape_read_matches_the_reference() {
        let started = std::time::Instant::now();
        let tag_definition = prod_shape::tag_definition(prod_shape::Bytes::Prod);
        let tag_type = prod_shape::tag_type_definition(prod_shape::Bytes::Prod);
        let (fake, data) = prod_shape_fake_with(&tag_definition, &tag_type);
        let got = windows_transport_at(&fake, &data)
            .read(&prod_scope())
            .await
            .expect("read");
        let want =
            reference_observed(&[(PROD_TYPES[0], &tag_definition), (PROD_TYPES[1], &tag_type)]);
        assert!(!want.unreadable.is_empty(), "the shape has long files");
        assert_observed_eq(&got, &want, "prod-shape read");
        eprintln!(
            "windows_prod_shape_read_matches_the_reference: {:.1} s",
            started.elapsed().as_secs_f64()
        );
        assert!(
            started.elapsed() <= PROD_FAKE_CEILING,
            "{:?}",
            started.elapsed()
        );
    }

    /// Wall-time ceiling, debug profile, of each offline test that moves the
    /// 48 MB production-shape tree through the fake (quick 261105-dl1).
    /// Measured 2026-11-05 on an Apple M1 Max (10 cores) shared with other
    /// work (load average 110 to 210): standalone,
    /// `windows_bundled_prod_shape_read_uses_one_transfer` 113 s (both
    /// trees), `windows_prod_shape_read_matches_the_reference` 43 s, the two
    /// production passes of
    /// `windows_bundled_and_per_file_prod_reads_are_identical` 147 s; inside
    /// the parallel transport suite 66 s to 414 s (the per-file read of the
    /// same tree took 16.5 s standalone at a lower load). The ceiling is a
    /// hang detector, about
    /// twice the slowest loaded run, not a benchmark: the transfer bound is
    /// the `get` and connection counts, and the timed rows of
    /// `tests/deploy_prod_scale.rs` hold the real script's speed.
    const PROD_FAKE_CEILING: Duration = Duration::from_secs(900);

    /// BRIEF test 4, on every run: the per-file path and the bundled path
    /// return identical ObservedFiles for the tiny production-shape tree and
    /// for a tree with mode, local and unreadable entries. The same for the
    /// 48 MB production-shape tree is
    /// `windows_bundled_and_per_file_prod_reads_are_identical`, opt-in.
    #[tokio::test]
    async fn windows_bundled_and_per_file_reads_are_identical() {
        let tag_definition = prod_shape::tiny_tag_definition();
        let tag_type = prod_shape::tag_type_definition(prod_shape::Bytes::Prod);
        let want =
            reference_observed(&[(PROD_TYPES[0], &tag_definition), (PROD_TYPES[1], &tag_type)]);
        let mut tiny = Vec::new();
        for per_file in [true, false] {
            let (fake, data) = prod_shape_fake_with(&tag_definition, &tag_type);
            let mut t = windows_transport_at(&fake, &data);
            if per_file {
                t = t.with_per_file_downloads();
            }
            let got = t.read(&prod_scope()).await.expect("read");
            assert_observed_eq(&got, &want, "tiny prod-shape read against the reference");
            tiny.push(got);
        }
        assert!(tiny[0] == tiny[1], "per-file and bundled tiny reads differ");

        // mode, local and unreadable entries
        let mut small = Vec::new();
        for per_file in [true, false] {
            let fake = windows_fake();
            windows_tree(&fake);
            fake.knobs()
                .enum_len_for
                .push((format!("{WRES}/production/{MES_CONFIG}"), 262));
            let mut t = windows_transport(&fake);
            if per_file {
                t = t.with_per_file_downloads();
            }
            let got = t.read(&scope()).await.expect("read");
            eprintln!(
                "small read, {}: {} connections, {} download sessions",
                if per_file { "per file" } else { "bundled" },
                connections(&fake),
                download_sessions(&fake)
            );
            assert_eq!(got.mode.len(), 2);
            assert_eq!(got.unreadable.len(), 1);
            assert_eq!(got.local_units.len(), 1);
            small.push(got);
        }
        assert_eq!(small[0], small[1]);
    }

    /// BRIEF test 4 at production scale, opt-in (quick 261105-dl1, amendment
    /// 6): the per-file pass moves the 48 MB tree a second time and grew the
    /// workspace run by more than a minute. The bundled pass alone runs on
    /// every run in `windows_bundled_prod_shape_read_uses_one_transfer` and
    /// `windows_prod_shape_read_matches_the_reference`. Run it with
    /// `cargo test -p ignition-core --lib windows_bundled_and_per_file_prod_reads_are_identical -- --ignored`.
    #[tokio::test]
    #[ignore = "opt-in: moves the 48 MB production-shape tree twice; run with --ignored"]
    async fn windows_bundled_and_per_file_prod_reads_are_identical() {
        let started = std::time::Instant::now();
        let tag_definition = prod_shape::tag_definition(prod_shape::Bytes::Prod);
        let tag_type = prod_shape::tag_type_definition(prod_shape::Bytes::Prod);
        let want =
            reference_observed(&[(PROD_TYPES[0], &tag_definition), (PROD_TYPES[1], &tag_type)]);
        let mut prod = Vec::new();
        for per_file in [true, false] {
            let (fake, data) = prod_shape_fake_with(&tag_definition, &tag_type);
            let mut t = windows_transport_at(&fake, &data);
            if per_file {
                t = t.with_per_file_downloads();
            }
            let got = t.read(&prod_scope()).await.expect("read");
            eprintln!(
                "prod-shape read, {}: {} connections, {} download sessions, {} `get` lines",
                if per_file { "per file" } else { "bundled" },
                connections(&fake),
                download_sessions(&fake),
                get_lines(&fake).len()
            );
            assert_observed_eq(&got, &want, "prod-shape read against the reference");
            prod.push(got);
        }
        assert!(prod[0] == prod[1], "per-file and bundled prod reads differ");
        let elapsed = started.elapsed();
        eprintln!(
            "windows_bundled_and_per_file_prod_reads_are_identical: {:.1} s",
            elapsed.as_secs_f64()
        );
        assert!(elapsed <= PROD_FAKE_CEILING, "{elapsed:?}");
    }

    /// A name the listing can represent but that is not ASCII is read with
    /// identical bytes.
    #[tokio::test]
    async fn windows_read_reads_a_non_ascii_name_identically() {
        let fake = windows_fake();
        windows_tree(&fake);
        fake.file(
            &format!("{WRES}/core/ignition/database-connection/Caf\u{e9} \u{c5}/config.json"),
            "{\"caf\\u00e9\":1}",
        );
        let got = windows_transport(&fake).read(&scope()).await.expect("read");
        assert_eq!(
            got.core[&rel("ignition/database-connection/Caf\u{e9} \u{c5}/config.json")].bytes,
            b"{\"caf\\u00e9\":1}"
        );
        assert_eq!(got.core.len(), 2);
    }

    #[tokio::test]
    async fn windows_ledger_is_read_from_the_account_home_and_skips_unsafe_siblings() {
        let fake = windows_fake();
        // a real Windows profile holds files whose names carry braces
        fake.file(
            &format!("{WHOME}/NTUSER.DAT{{6b3cbf8b-c6cb-11f1-8338-00155d013703}}.TM.blf"),
            "x",
        );
        fake.file(
            &format!("{WHOME}/ign-deploy/gw/production/{LEDGER_FILE}"),
            "{\"schema_version\":1}",
        );
        // and none below the data directory
        let t = windows_transport(&fake);
        assert_eq!(
            t.read_ledger("gw", "production").await.expect("ledger"),
            Some(b"{\"schema_version\":1}".to_vec())
        );
        assert_eq!(t.account_home().await.expect("home"), WHOME);
        // the ledger is NOT read from below the data directory
        let fake = windows_fake();
        fake.file(
            &format!("{WDATA}/ign-deploy/gw/production/{LEDGER_FILE}"),
            "{\"schema_version\":1}",
        );
        assert_eq!(
            windows_transport(&fake)
                .read_ledger("gw", "production")
                .await
                .expect("none"),
            None
        );
    }

    #[test]
    fn account_home_answers_are_validated() {
        // a Windows host reports /X:/...; a POSIX path is not one
        for (answer, ok) in [
            ("/C:/Users/svc", true),
            ("/C:/Users/svc/", true),
            ("/home/svc", false),
            ("relative", false),
            ("/C:/Users/../x", false),
            ("/C:/Users/s*c", false),
            ("/", false),
        ] {
            let fake = windows_fake();
            fake.knobs().home = Some(answer.to_string());
            let got = block_on(windows_transport(&fake).account_home());
            assert_eq!(got.is_ok(), ok, "windows {answer}: {got:?}");
        }
        for (answer, ok) in [
            ("/home/svc", true),
            ("/home/svc/", true),
            ("relative", false),
            ("/home/../x", false),
            ("/home/s*c", false),
            ("/", false),
            // a raw `\n` in the home: the line after it is not ignored
            ("/home/s\nvc", false),
            ("/home/svc\nother", false),
        ] {
            let fake = FakeSftp::new();
            fake.knobs().home = Some(answer.to_string());
            let got = block_on(transport(&fake).account_home());
            assert_eq!(got.is_ok(), ok, "posix {answer}: {got:?}");
        }
    }

    #[tokio::test]
    async fn default_staging_follows_decision_b() {
        let fake = windows_fake();
        let staging = windows_transport(&fake)
            .default_staging("gw", "production", "run-1")
            .await
            .expect("staging");
        assert_eq!(
            staging,
            StagingMode::Separate {
                root: "/C:/Users/svc/ign-deploy/gw/production/staging/run-1".to_string()
            }
        );
        // and it is a root the mirror accepts: outside config/resources
        assert!(
            windows_transport(&fake)
                .with_mirror("production", staging)
                .is_ok()
        );
    }

    // ---- the Windows write half over the scripted server --------------------------

    fn win_tree_of(files: &[(&str, &str)]) -> RenderedTree {
        RenderedTree {
            files: files
                .iter()
                .map(|(p, b)| (rel(p), b.as_bytes().to_vec()))
                .collect(),
        }
    }

    const STAGING: &str = "/C:/Users/svc/ign-deploy/gw/production/staging/run-1";
    const DC: &str = "ignition/database-connection";

    fn mirror(fake: &Arc<FakeSftp>) -> SftpTransport {
        windows_transport(fake)
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: STAGING.to_string(),
                },
            )
            .expect("mirror")
    }

    fn fixture_collection(fake: &FakeSftp) {
        fake.file(
            &format!("{WRES}/production/config-mode.json"),
            "{\"mode\":1}",
        );
        fake.file(
            &format!("{WRES}/production/{DC}/Old/config.json"),
            "{\"old\":1}",
        );
        fake.file(
            &format!("{WRES}/production/{DC}/Keep/config.json"),
            "{\"keep\":1}",
        );
    }

    fn renames(fake: &FakeSftp) -> Vec<(String, String)> {
        fake.session_lines()
            .into_iter()
            .flatten()
            .filter(|l| l.starts_with("rename "))
            .map(|l| {
                let q = quoted(&l);
                (q[0].clone(), q[1].clone())
            })
            .collect()
    }

    /// Runs sftp against the scripted server and answers every `ssh` with one
    /// canned script result, recording the remote commands it was given.
    struct OneExec {
        fake: Arc<FakeSftp>,
        execs: Mutex<Vec<String>>,
        reply: String,
    }

    #[async_trait::async_trait]
    impl ProcessRunner for OneExec {
        async fn run(
            &self,
            program: &OsStr,
            args: &[OsString],
            stdin: Option<Vec<u8>>,
            timeout: Duration,
        ) -> ProcessOutput {
            if program == "ssh" {
                self.execs
                    .lock()
                    .expect("execs")
                    .push(args.last().expect("command").to_string_lossy().into_owned());
                return ProcessOutput {
                    stdout: format!("IGN-RESULT:{}\n", self.reply).into_bytes(),
                    stderr: Vec::new(),
                    code: 0,
                    timed_out: false,
                };
            }
            self.fake.run(program, args, stdin, timeout).await
        }
    }

    /// D-01: on a Linux node the whole commit is ONE `ssh` call that runs the
    /// uploaded script's `mirror` op; no per-file sftp `rename` is issued, and
    /// the result's indices are mapped back to the paths of the work list.
    #[tokio::test]
    async fn linux_mirror_commit_is_one_exec() {
        for separate in [true, false] {
            let fake = FakeSftp::new();
            fake.mkdirs("/home/svc");
            fake.listed
                .lock()
                .expect("listed")
                .extend(["/home".to_string(), "/home/svc".to_string()]);
            let runner = Arc::new(OneExec {
                fake: fake.clone(),
                execs: Mutex::new(Vec::new()),
                reply: r#"{"ok":1,"moved":[0,1],"deleted":[0],"dirs":[[0,3]],"fail":null}"#
                    .to_string(),
            });
            let shell = RemoteShell::local_sftp_server(
                Path::new("/unused/sftp-server"),
                runner.clone(),
                Duration::from_secs(30),
            );
            let staging = if separate {
                StagingMode::Separate {
                    root: "/home/svc/ign-deploy/gw/production/staging/run-1".to_string(),
                }
            } else {
                StagingMode::InPlace
            };
            let m = SftpTransport::new(shell, DATA.to_string(), "nodes.master")
                .with_mirror("production", staging)
                .expect("mirror");
            let tree = win_tree_of(&[
                (&format!("{DC}/Keep/config.json"), "{\"keep\":2}"),
                (&format!("{DC}/New/config.json"), "{\"new\":1}"),
            ]);
            let staged = m.stage(&tree).await.expect("stage");
            let before = fake.session_lines().len();
            let report = m
                .commit(
                    &staged,
                    &DeleteSet {
                        files: vec![rel(&format!("{DC}/Old/config.json"))],
                    },
                )
                .await
                .expect("commit");

            let execs = runner.execs.lock().expect("execs").clone();
            assert_eq!(
                execs.len(),
                1,
                "separate={separate}: one exec call: {execs:?}"
            );
            assert!(execs[0].contains("'mirror'"), "{}", execs[0]);
            assert!(execs[0].contains("commit.sh"), "{}", execs[0]);
            assert!(
                !execs[0].contains("Old/config.json"),
                "paths never reach the command line: {}",
                execs[0]
            );
            let lines: Vec<String> = fake.session_lines().into_iter().flatten().collect();
            assert!(
                !lines.iter().any(|l| l.starts_with("rename ")),
                "separate={separate}: no per-file rename: {lines:?}"
            );
            assert!(fake.session_lines().len() >= before);
            assert_eq!(
                report.written,
                [
                    format!("{DC}/Keep/config.json"),
                    format!("{DC}/New/config.json")
                ]
            );
            assert_eq!(report.deleted, [format!("{DC}/Old/config.json")]);
            assert_eq!(report.removed_dirs, [format!("{DC}/Old")]);
            // the script removes its own control directory when it exits (the
            // canned reply here never ran it; the integration tests check the
            // real thing), so only what was uploaded is asserted: the script
            // and the work list, below the account home
            let fs = fake.fs.lock().expect("fs");
            let ctl: Vec<&String> = fs.keys().filter(|k| k.contains("/.ign-ctl-")).collect();
            assert!(
                ctl.iter().any(|k| k.ends_with("/commit.sh"))
                    && ctl.iter().any(|k| k.ends_with("/work.tsv")),
                "{ctl:?}"
            );
        }
    }

    /// The decoded parameters of the one `powershell -EncodedCommand` an exec
    /// carried: the base64 JSON blob the fixed stub reads as `$p`.
    fn stub_params(exec: &str) -> (String, serde_json::Value) {
        use base64::Engine as _;
        let b64 = exec
            .strip_prefix("powershell -NoProfile -NonInteractive -EncodedCommand ")
            .or_else(|| {
                exec.split("powershell -NoProfile -NonInteractive -EncodedCommand ")
                    .nth(1)
            })
            .unwrap_or_else(|| panic!("not a powershell command: {exec}"))
            .trim();
        let bytes = base64::engine::general_purpose::STANDARD
            .decode(b64)
            .expect("outer base64");
        let units: Vec<u16> = bytes
            .chunks(2)
            .map(|c| u16::from_le_bytes([c[0], c[1]]))
            .collect();
        let text = String::from_utf16(&units).expect("utf-16");
        let blob = text
            .split("FromBase64String('")
            .nth(1)
            .and_then(|rest| rest.split('\'').next())
            .expect("parameter blob");
        let json = base64::engine::general_purpose::STANDARD
            .decode(blob)
            .expect("blob base64");
        (text, serde_json::from_slice(&json).expect("json"))
    }

    /// The text of the work list the transport uploaded below the account home.
    fn uploaded_work_list(fake: &FakeSftp) -> String {
        let fs = fake.fs.lock().expect("fs");
        let (_, node) = fs
            .iter()
            .find(|(k, _)| k.contains("/.ign-ctl-") && k.ends_with("/work.tsv"))
            .expect("a work list was uploaded");
        match node {
            Node::File(bytes) => String::from_utf8(bytes.clone()).expect("utf-8"),
            _ => panic!("work.tsv is not a file"),
        }
    }

    /// A Windows mirror over `OneExec` answering every `ssh` with `reply`.
    fn windows_mirror_with_reply(
        separate: bool,
        reply: &str,
    ) -> (Arc<FakeSftp>, Arc<OneExec>, SftpTransport) {
        let fake = windows_fake();
        fixture_collection(&fake);
        let runner = Arc::new(OneExec {
            fake: fake.clone(),
            execs: Mutex::new(Vec::new()),
            reply: reply.to_string(),
        });
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            runner.clone(),
            Duration::from_secs(30),
        );
        let staging = if separate {
            StagingMode::Separate {
                root: STAGING.to_string(),
            }
        } else {
            StagingMode::InPlace
        };
        let m = SftpTransport::new_windows(shell, WDATA.to_string(), "nodes.master")
            .with_mirror("production", staging)
            .expect("mirror");
        (fake, runner, m)
    }

    /// D-01 on Windows: the whole commit is ONE `ssh` call that runs the
    /// uploaded PowerShell script's `mirror` op; no per-file sftp `rename` is
    /// issued, the command line carries no path, the work list holds the
    /// native staged-files directory and the result's indices are mapped back
    /// to the paths of the work list.
    #[tokio::test]
    async fn windows_mirror_commit_is_one_exec() {
        for separate in [true, false] {
            let (fake, runner, m) = windows_mirror_with_reply(
                separate,
                r#"{"ok":1,"moved":[0,1],"deleted":[0],"dirs":[[0,3]],"fail":null}"#,
            );
            let tree = win_tree_of(&[
                (&format!("{DC}/Keep/config.json"), "{\"keep\":2}"),
                (&format!("{DC}/New One/config.json"), "{\"new\":1}"),
            ]);
            let staged = m.stage(&tree).await.expect("stage");
            let report = m
                .commit(
                    &staged,
                    &DeleteSet {
                        files: vec![rel(&format!("{DC}/Old/config.json"))],
                    },
                )
                .await
                .expect("commit");

            let execs = runner.execs.lock().expect("execs").clone();
            assert_eq!(
                execs.len(),
                1,
                "separate={separate}: one exec call: {execs:?}"
            );
            let (text, params) = stub_params(&execs[0]);
            assert_eq!(params["o"], "mirror");
            assert_eq!(params["c"], "C:\\gw\\data\\config\\resources\\production");
            assert!(
                params["t"]
                    .as_str()
                    .expect("t")
                    .starts_with("C:\\Users\\svc\\.ign-ctl-"),
                "{params}"
            );
            assert!(
                !text.contains("Old") && !text.contains("config.json"),
                "paths never reach the command line"
            );
            assert_eq!(
                params["ss"],
                crate::deploy::transport::batch::script_ps1_sha256()
            );
            let work = uploaded_work_list(&fake);
            let source = work
                .lines()
                .find_map(|l| l.strip_prefix("S\t"))
                .expect("an S record");
            if separate {
                assert_eq!(
                    source,
                    "C:\\Users\\svc\\ign-deploy\\gw\\production\\staging\\run-1"
                );
            } else {
                assert!(
                    source.starts_with("C:\\Users\\svc\\.ign-ctl-") && source.ends_with("\\files"),
                    "{source}"
                );
            }
            assert!(
                work.contains(&format!("\nX\t0\t{DC}/Old/config.json\n")),
                "{work}"
            );
            assert!(
                work.contains("\nT\t3\n"),
                "the retry bound travels in the list"
            );
            let lines: Vec<String> = fake.session_lines().into_iter().flatten().collect();
            assert!(
                !lines.iter().any(|l| l.starts_with("rename ")),
                "separate={separate}: no per-file rename: {lines:?}"
            );
            assert_eq!(
                report.written,
                [
                    format!("{DC}/Keep/config.json"),
                    format!("{DC}/New One/config.json")
                ]
            );
            assert_eq!(report.deleted, [format!("{DC}/Old/config.json")]);
            assert_eq!(report.removed_dirs, [format!("{DC}/Old")]);
            // the script (not this process) touches the collection: the canned
            // reply never ran it, so the fixture is as it was
            let fs = fake.fs.lock().expect("fs");
            assert!(matches!(
                fs.get(&format!("{WRES}/production/{DC}/Keep/config.json")),
                Some(Node::File(b)) if b == b"{\"keep\":1}"
            ));
            let ctl: Vec<&String> = fs.keys().filter(|k| k.contains("/.ign-ctl-")).collect();
            assert!(
                ctl.iter().any(|k| k.ends_with("/commit.ps1"))
                    && ctl.iter().any(|k| k.ends_with("/work.tsv")),
                "{ctl:?}"
            );
        }
    }

    /// debug deploy-tag-redundancy-sync: the script's report may name the
    /// emptied type folder of a tag type a redundant pair cannot sync (two
    /// levels: `ignition/tag-definition`), and only of those types: two
    /// levels below any other type, or one level below a tag type, is a
    /// malformed report, so the `<module>/<type>` floor and the module
    /// directory still hold.
    #[tokio::test]
    async fn mirror_report_names_the_type_folder_only_for_the_pair_tag_types() {
        const TAGS: &str = "ignition/tag-definition/default/Deploy/tags.json";
        let old = format!("{DC}/Old/config.json");
        for (delete, levels, removed) in [
            (TAGS, 2, Some("ignition/tag-definition")),
            (TAGS, 1, None),
            (old.as_str(), 2, None),
        ] {
            let reply = format!(
                r#"{{"ok":1,"moved":[0],"deleted":[0],"dirs":[[0,{levels}]],"fail":null}}"#
            );
            let (fake, _runner, m) = windows_mirror_with_reply(true, &reply);
            fake.file(&format!("{WRES}/production/{TAGS}"), "[]");
            let tree = win_tree_of(&[(&format!("{DC}/Keep/config.json"), "{\"keep\":2}")]);
            let staged = m.stage(&tree).await.expect("stage");
            let outcome = m
                .commit(
                    &staged,
                    &DeleteSet {
                        files: vec![rel(delete)],
                    },
                )
                .await;
            match removed {
                Some(dir) => {
                    let report = outcome.expect("accepted");
                    assert_eq!(report.removed_dirs, [dir.to_string()], "{delete} {levels}");
                }
                None => {
                    let err = outcome.expect_err("malformed");
                    assert!(
                        err.to_string().contains("malformed (dirs)"),
                        "{delete} {levels}: {err}"
                    );
                }
            }
        }
    }

    /// Was `windows_commit_replaces_by_rename_and_never_renames_onto_itself`.
    /// A Windows commit no longer renames over sftp (D-01): the replaced file,
    /// the new one, the delete and the untouched `config-mode.json` are the
    /// script's business, which the live host proves; here the scenario is
    /// the report the script answers with and the two rules that were
    /// asserted of the renames: nothing is renamed onto itself (the script
    /// refuses it, and no sftp rename exists to try) and the staged copies
    /// move only from the staging root into the collection.
    #[tokio::test]
    async fn windows_commit_replaces_through_the_script_and_never_renames_over_sftp() {
        let (fake, runner, m) = windows_mirror_with_reply(
            true,
            r#"{"ok":1,"moved":[0,1],"deleted":[0],"dirs":[],"fail":null}"#,
        );
        let tree = win_tree_of(&[
            (&format!("{DC}/Keep/config.json"), "{\"keep\":2}"),
            (&format!("{DC}/New One/config.json"), "{\"new\":1}"),
        ]);
        let staged = m.stage(&tree).await.expect("stage");
        let report = m
            .commit(
                &staged,
                &DeleteSet {
                    files: vec![rel(&format!("{DC}/Old/config.json"))],
                },
            )
            .await
            .expect("commit");
        assert_eq!(report.written.len(), 2);
        assert_eq!(report.deleted, [format!("{DC}/Old/config.json")]);
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
        assert_eq!(renames(&fake).len(), 0, "no sftp rename on a Windows host");
        // the staged copies are on the host before the call, in the staging
        // root, and nowhere inside the collection
        let fs = fake.fs.lock().expect("fs");
        for p in ["Keep/config.json", "New One/config.json"] {
            assert!(
                matches!(fs.get(&format!("{STAGING}/{DC}/{p}")), Some(Node::File(_))),
                "{p} is staged"
            );
        }
        assert!(
            fs.keys()
                .filter(|k| k.starts_with(&format!("{WRES}/production/")))
                .all(|k| !k.rsplit('/').next().expect("name").starts_with('.')),
            "nothing dot-named in the collection"
        );
        // the script itself refuses a move onto itself and keeps the config
        // file of the gateway out of every list (the work list gate)
        assert!(
            crate::deploy::transport::batch::SCRIPT_PS1.contains("a file cannot move onto itself")
        );
    }

    /// Was `windows_commit_retries_a_held_rename_a_bounded_number_of_times`
    /// (a held rename retried by the transport, three segments). The retry is
    /// the script's now: the work list's `T` record carries the bound, a move
    /// that still fails comes back as the script's own result and is reported
    /// as `transport_failed` naming the entry by its path, saying nothing was
    /// rolled back, and the transport itself never repeats the call or issues
    /// an sftp `rename`.
    #[tokio::test]
    async fn windows_commit_holds_retry_in_the_script_and_reports_a_failed_move() {
        // segment 1 (held twice, then success): the script retried; the bound
        // is in the list, the call is made once
        let (fake, runner, m) = windows_mirror_with_reply(
            true,
            r#"{"ok":1,"moved":[0],"deleted":[],"dirs":[],"fail":null}"#,
        );
        let staged = m
            .stage(&win_tree_of(&[(
                &format!("{DC}/Keep/config.json"),
                "{\"keep\":2}",
            )]))
            .await
            .expect("stage");
        m.commit(&staged, &DeleteSet::default())
            .await
            .expect("commit");
        assert!(uploaded_work_list(&fake).contains(&format!("\nT\t{RENAME_RETRIES}\n")));
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
        assert_eq!(renames(&fake).len(), 0);

        // segment 2 (held for good) and 3 (permission denied): the script's
        // failure comes back as transport_failed naming the path; not
        // repeated by the transport
        for fail in [
            "write 0: cannot move the file into place",
            "write 0: cannot reset the permissions of the file",
        ] {
            let (fake, runner, m) = windows_mirror_with_reply(
                true,
                &format!(r#"{{"ok":1,"moved":[],"deleted":[],"dirs":[],"fail":"{fail}"}}"#),
            );
            let staged = m
                .stage(&win_tree_of(&[(
                    &format!("{DC}/Keep/config.json"),
                    "{\"keep\":2}",
                )]))
                .await
                .expect("stage");
            let err = m
                .commit(&staged, &DeleteSet::default())
                .await
                .expect_err("held for good");
            let CoreError::TransportFailed {
                operation, detail, ..
            } = err
            else {
                panic!("not transport_failed")
            };
            assert_eq!(operation, "commit");
            assert!(
                detail.contains(&format!("{DC}/Keep/config.json")),
                "{detail}"
            );
            assert!(detail.contains("nothing was rolled back"), "{detail}");
            assert_eq!(runner.execs.lock().expect("execs").len(), 1, "{fail}");
            assert_eq!(renames(&fake).len(), 0, "no sftp rename on a Windows host");
            // the old content is still there
            let fs = fake.fs.lock().expect("fs");
            assert!(matches!(
                fs.get(&format!("{WRES}/production/{DC}/Keep/config.json")),
                Some(Node::File(b)) if b == b"{\"keep\":1}"
            ));
        }
    }

    /// 23-20: every `ApplyHost` call of a Windows node runs the PowerShell
    /// script (`probe`, `fingerprint`, `finalize`, `discard_interrupted`) or
    /// reads over sftp outside the lock (`read_record`, `fetch_snapshot`); none
    /// is refused as unsupported any more.
    #[tokio::test]
    async fn windows_apply_calls_run_the_script_or_read_over_sftp() {
        use crate::deploy::apply::host::ApplyHost;
        use crate::deploy::apply::run_store::RunId;
        let run = RunId::parse("20261017T120000Z-aaaaaa").expect("run id");
        let host = |reply: &str| {
            let fake = windows_fake();
            let runner = Arc::new(OneExec {
                fake: fake.clone(),
                execs: Mutex::new(Vec::new()),
                reply: reply.to_string(),
            });
            let shell = RemoteShell::local_sftp_server(
                Path::new("/unused/sftp-server"),
                runner.clone(),
                Duration::from_secs(30),
            );
            let t = SftpTransport::new_windows(shell, WDATA.to_string(), "nodes.master")
                .with_apply("gw", "production")
                .expect("apply half");
            (fake, runner, t)
        };

        // probe: the script's problem words come back as sentences
        let (_, runner, t) = host(r#"{"ok":1,"problems":["coll_unwritable","deploy_unwritable"]}"#);
        let report = t.probe().await.expect("probe");
        assert!(!report.writable);
        assert_eq!(report.problems.len(), 2, "{:?}", report.problems);
        let execs = runner.execs.lock().expect("execs").clone();
        assert_eq!(execs.len(), 1);
        let (_, params) = stub_params(&execs[0]);
        assert_eq!(params["o"], "probe");
        assert_eq!(params["d"], "C:\\Users\\svc\\ign-deploy\\gw\\production");
        assert_eq!(params["c"], "C:\\gw\\data\\config\\resources\\production");
        let (_, runner, t) = host(r#"{"ok":1,"problems":[]}"#);
        assert!(t.probe().await.expect("probe").writable);
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);

        // fingerprint: hashes by path; a held file is a refusal naming the path
        let hash = "ab".repeat(32);
        let (fake, runner, t) = host(&format!(r#"{{"ok":1,"hashes":["{hash}","-"]}}"#));
        let got = t
            .fingerprint(&[format!("{DC}/Keep/config.json"), "ledger.json".to_string()])
            .await
            .expect("fingerprint");
        assert_eq!(
            got.get(&format!("{DC}/Keep/config.json")),
            Some(&Some(hash))
        );
        assert_eq!(got.get("ledger.json"), Some(&None));
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
        assert!(uploaded_work_list(&fake).contains("\nO\tfingerprint\n"));
        let (_, _, t) = host(r#"{"ok":0,"err":"held","detail":"x","bad":["P0"]}"#);
        let err = t
            .fingerprint(&[format!("{DC}/Keep/config.json")])
            .await
            .expect_err("held");
        assert!(
            err.to_string().contains(&format!("{DC}/Keep/config.json"))
                && err.to_string().contains("holds it open"),
            "{err}"
        );

        // discard_interrupted: no journal on the host, the script drops the run
        let (fake, runner, t) = host(r#"{"ok":1}"#);
        t.discard_interrupted(&run).await.expect("discard");
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
        assert!(uploaded_work_list(&fake).contains("\nO\tdiscard\n"));

        // read_record and fetch_snapshot read over sftp and start no process
        let (fake, runner, t) = host(r#"{"ok":1}"#);
        assert!(t.read_record(&run).await.expect("read_record").is_none());
        let dest = tempfile::tempdir().expect("dest");
        let err = t
            .fetch_snapshot(&run, dest.path())
            .await
            .expect_err("no snapshot");
        assert!(
            err.to_string().contains("has no snapshot on this host"),
            "{err}"
        );
        assert!(!err.to_string().contains("not supported"), "{err}");
        assert!(t.sessions() > 0, "they read over sftp");
        assert_eq!(runner.execs.lock().expect("execs").len(), 0, "no ssh exec");
        let _ = fake;
        // the script the Windows family runs is the PowerShell one
        let (name, text, sha) = t.script_file_for_tests();
        assert_eq!(name, "commit.ps1");
        assert_eq!(text, crate::deploy::transport::batch::SCRIPT_PS1);
        assert_eq!(sha, crate::deploy::transport::batch::script_ps1_sha256());
    }

    /// The `write_host` preflight reaches the Windows host through
    /// `probe_paths`: a touched path whose copy below the account home the sftp
    /// server could not serve again is a refusal that names the path, decided
    /// in Rust from the account home alone, and writes nothing.
    #[tokio::test]
    async fn windows_probe_paths_refuses_a_path_the_sftp_server_could_not_reach() {
        use crate::deploy::apply::host::ApplyHost;
        let fake = windows_fake();
        let t = windows_transport(&fake)
            .with_apply("gw", "production")
            .expect("apply half");
        let long = format!(
            "ignition/database-connection/{}/config.json",
            "n".repeat(260)
        );
        let report = t
            .probe_paths(&[
                "ignition/database-connection/ok/config.json".to_string(),
                long.clone(),
            ])
            .await
            .expect("the gate answers");
        assert!(!report.writable);
        assert_eq!(report.problems.len(), 1, "{:?}", report.problems);
        assert!(
            report.problems[0].starts_with(&long),
            "{:?}",
            report.problems
        );
        // nothing was written: the only sftp work is the `pwd` for the home
        assert!(
            fake.sessions
                .lock()
                .expect("sessions")
                .iter()
                .flatten()
                .all(|c| ["put", "mkdir", "rename", "rm"]
                    .iter()
                    .all(|w| !c.trim_start_matches('@').starts_with(w))),
            "the gate writes nothing"
        );
        // a path that fits goes on to the script's own probe
        let runner = Arc::new(OneExec {
            fake: fake.clone(),
            execs: Mutex::new(Vec::new()),
            reply: r#"{"ok":1,"problems":[]}"#.to_string(),
        });
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            runner.clone(),
            Duration::from_secs(30),
        );
        let t = SftpTransport::new_windows(shell, WDATA.to_string(), "nodes.master")
            .with_apply("gw", "production")
            .expect("apply half");
        let report = t
            .probe_paths(&["ignition/database-connection/ok/config.json".to_string()])
            .await
            .expect("the script probe answers");
        assert!(report.writable, "{report:?}");
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
    }

    /// Was the POSIX segment of `windows_commit_retries_a_held_rename_a_bounded_number_of_times`
    /// (now `windows_commit_holds_retry_in_the_script_and_reports_a_failed_move`)
    /// ("a POSIX host does not retry at all", one sftp `rename` that failed).
    /// A Linux commit no longer renames over sftp at all (D-01): the script
    /// moves, retrying a held target as many times as the work list's `T`
    /// record says, and a move that still fails comes back as the script's
    /// own result, which the transport reports as `transport_failed` naming
    /// the entry by its path and saying nothing was rolled back.
    #[tokio::test]
    async fn linux_commit_never_renames_over_sftp_and_reports_a_failed_move() {
        let fake = FakeSftp::new();
        fake.mkdirs("/home/svc/ign-deploy/gw/production/staging");
        fake.listed
            .lock()
            .expect("listed")
            .extend(["/home".to_string(), "/home/svc".to_string()]);
        let runner = Arc::new(OneExec {
            fake: fake.clone(),
            execs: Mutex::new(Vec::new()),
            reply: r#"{"ok":1,"moved":[],"deleted":[],"dirs":[],"fail":"write 0: cannot move the file into place"}"#
                .to_string(),
        });
        let shell = RemoteShell::local_sftp_server(
            Path::new("/unused/sftp-server"),
            runner.clone(),
            Duration::from_secs(30),
        );
        let m = SftpTransport::new(shell, DATA.to_string(), "nodes.master")
            .with_mirror(
                "production",
                StagingMode::Separate {
                    root: "/home/svc/ign-deploy/gw/production/staging/run-1".to_string(),
                },
            )
            .expect("mirror");
        let staged = m
            .stage(&win_tree_of(&[(
                &format!("{DC}/Keep/config.json"),
                "{\"keep\":2}",
            )]))
            .await
            .expect("stage");
        let err = m
            .commit(&staged, &DeleteSet::default())
            .await
            .expect_err("held for good");
        let CoreError::TransportFailed {
            operation, detail, ..
        } = err
        else {
            panic!("not transport_failed")
        };
        assert_eq!(operation, "commit");
        assert!(
            detail.contains(&format!("{DC}/Keep/config.json")),
            "{detail}"
        );
        assert!(detail.contains("nothing was rolled back"), "{detail}");
        assert_eq!(renames(&fake).len(), 0, "no sftp rename on a POSIX host");
        assert_eq!(runner.execs.lock().expect("execs").len(), 1);
    }

    #[tokio::test]
    async fn windows_staging_inside_the_collection_is_refused_whatever_its_case() {
        let fake = windows_fake();
        for root in [
            "/c:/GW/Data/config/resources/staging/run-1",
            "/C:/gw/data/config/resources",
            "/C:/gw",
        ] {
            let err = windows_transport(&fake)
                .with_mirror(
                    "production",
                    StagingMode::Separate {
                        root: root.to_string(),
                    },
                )
                .err()
                .unwrap_or_else(|| panic!("{root} refused"));
            assert!(err.contains("staging root"), "{root}: {err}");
        }
    }

    // ---- post-review: path gate parity and component boundaries ----------------

    fn block_on<T>(future: impl std::future::Future<Output = T>) -> T {
        tokio::runtime::Builder::new_current_thread()
            .enable_time()
            .build()
            .expect("runtime")
            .block_on(future)
    }

    #[test]
    fn windows_listing_names_pass_the_windows_gate() {
        let one = vec!["/C:/d".to_string()];
        let listing = |name: &str| {
            format!(
                "Remote working directory: /C:/h\n-rw-******    1 -        -   8 Oct 15 02:35 {name}\n"
            )
        };
        for bad in [
            "a:b",
            "name:stream",
            "trail.",
            "trail ",
            "NUL",
            "aux.txt",
            "a<b",
            "a|b",
        ] {
            let text = listing(bad);
            assert!(
                parse_batch_listing_as(
                    ListFormat::Windows,
                    NameRule::Refuse,
                    &one,
                    text.as_bytes(),
                    100
                )
                .is_err(),
                "{bad:?} must be refused"
            );
            let kept = parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Skip,
                &one,
                text.as_bytes(),
                100,
            )
            .expect("skip")
            .remove(0);
            assert!(kept.is_empty(), "{bad:?} must be skipped, not kept");
        }
        // one directory cannot hold two names that differ only by case
        let twice = "Remote working directory: /C:/h\n\
-rw-******    1 -        -   8 Oct 15 02:35 Config.json\n\
-rw-******    1 -        -   9 Oct 15 02:35 config.JSON\n";
        assert!(
            parse_batch_listing_as(
                ListFormat::Windows,
                NameRule::Refuse,
                &one,
                twice.as_bytes(),
                100
            )
            .is_err()
        );
        // the POSIX format keeps its rules: a colon is an ordinary name there
        let posix =
            format!("{PWD_MARKER}/home/u\n-rw-r--r--    ? svc staff 8 Oct 14 10:21 /d/a:b\n");
        assert!(parse_batch_listing(&["/d".to_string()], posix.as_bytes()).is_ok());
    }

    #[test]
    fn windows_home_and_staging_roots_pass_the_windows_path_gate() {
        for answer in [
            "/C:/Users/svc/D:/x",
            "/Users/svc",
            "/C:/Users/svc.",
            "/C:/Users/svc/NUL",
            "/C:/Users/svc/a:stream",
            "/C:",
        ] {
            let fake = windows_fake();
            fake.knobs().home = Some(answer.to_string());
            let got = block_on(windows_transport(&fake).account_home());
            assert!(got.is_err(), "{answer}: {got:?}");
        }
        // the POSIX transport is untouched by the Windows gate
        let fake = FakeSftp::new();
        fake.knobs().home = Some("/home/svc:1".to_string());
        fake.listed
            .lock()
            .expect("listed")
            .insert("/home/svc:1".to_string());
        assert!(block_on(transport(&fake).account_home()).is_ok());

        let fake = windows_fake();
        for root in [
            "/C:/Users/svc/D:/x",
            "/C:/Users/svc/x:stream",
            "/C:/Users/svc/NUL",
            "/C:/Users/svc/staging.",
            "/C:/Users/../x",
            "/Users/svc/staging",
        ] {
            let got = windows_transport(&fake).with_mirror(
                "production",
                StagingMode::Separate {
                    root: root.to_string(),
                },
            );
            assert!(got.is_err(), "{root} must be refused");
        }
        // a sibling is not the thing it is next to, whatever its case
        for root in [
            "/C:/gw/data/config/resources2/staging",
            "/c:/GW/DATA/config/Resources-evil/staging",
            "/C:/gw/data2/staging",
            "/C:/gw/data-staging/x",
        ] {
            let got = windows_transport(&fake).with_mirror(
                "production",
                StagingMode::Separate {
                    root: root.to_string(),
                },
            );
            assert!(
                got.is_ok(),
                "{root} is a sibling, not inside: {:?}",
                got.err()
            );
        }
        // and the real thing is inside, whatever its case and separators
        for root in [
            "/c:/GW/Data/CONFIG/resources/x/y",
            "/C:/gw/data/config/resources/",
        ] {
            let got = windows_transport(&fake).with_mirror(
                "production",
                StagingMode::Separate {
                    root: root.to_string(),
                },
            );
            assert!(got.is_err(), "{root} is inside config/resources");
        }
    }

    #[test]
    fn windows_components_pass_the_windows_gate_before_any_session() {
        let fake = windows_fake();
        let t = windows_transport(&fake);
        for (mode, provider, module, ty) in [
            ("prod:x", None, "ignition", "database-connection"),
            ("production.", None, "ignition", "database-connection"),
            ("production", Some("p:q"), "ignition", "database-connection"),
            ("production", Some("NUL"), "ignition", "database-connection"),
            ("production", None, "ignition:x", "database-connection"),
            ("production", None, "ignition", "con"),
            ("production", None, "ignition", "type "),
        ] {
            let scope = Scope {
                mode: mode.to_string(),
                types: vec![(module.to_string(), ty.to_string())],
                tag_providers: provider.map(|p| vec![p.to_string()]).unwrap_or_default(),
            };
            assert!(
                block_on(t.read(&scope)).is_err(),
                "{mode}/{provider:?}/{module}/{ty}"
            );
        }
        assert!(block_on(t.list_types("core:x")).is_err());
        assert!(
            fake.session_lines().is_empty(),
            "refused before any process was spawned"
        );
        // the ledger: gateway and mode
        for (gateway, mode) in [("gw:x", "production"), ("gw", "NUL"), ("gw.", "production")] {
            let err = block_on(t.read_ledger(gateway, mode)).expect_err(gateway);
            assert!(
                matches!(err, CoreError::DeployLedgerInvalid { .. }),
                "{err:?}"
            );
        }
        assert!(fake.session_lines().is_empty());
        // the default staging: gateway, mode and run id
        for (gateway, mode, run) in [
            ("gw:x", "production", "r"),
            ("gw", "NUL", "r"),
            ("gw", "production", "r:1"),
            ("gw", "production", "run."),
            ("gw", "production", "CON"),
        ] {
            assert!(
                block_on(t.default_staging(gateway, mode, run)).is_err(),
                "{gateway}/{mode}/{run}"
            );
        }
        assert!(fake.session_lines().is_empty());
        // a mirror for a mode the host cannot name
        assert!(
            windows_transport(&fake)
                .with_mirror("production.", StagingMode::InPlace)
                .is_err()
        );
    }

    #[tokio::test]
    async fn windows_staged_and_deleted_paths_pass_the_windows_gate_and_write_nothing() {
        let fake = windows_fake();
        fixture_collection(&fake);
        let m = mirror(&fake);
        for bad in [
            format!("{DC}/NUL/config.json"),
            format!("{DC}/a:stream/config.json"),
            format!("{DC}/trail./config.json"),
            format!("{DC}/x/aux.json"),
        ] {
            let staged = m.stage(&win_tree_of(&[(&bad, "{}")])).await;
            assert!(staged.is_err(), "{bad} must not be staged");
        }
        let staged = m
            .stage(&win_tree_of(&[(
                &format!("{DC}/Keep/config.json"),
                "{\"k\":3}",
            )]))
            .await
            .expect("a clean path stages");
        let err = m
            .commit(
                &staged,
                &DeleteSet {
                    files: vec![rel(&format!("{DC}/NUL/config.json"))],
                },
            )
            .await
            .expect_err("a delete-set path the host cannot name");
        assert!(matches!(err, CoreError::TransportFailed { .. }));
        let fs = fake.fs.lock().expect("fs");
        assert!(matches!(
            fs.get(&format!("{WRES}/production/{DC}/Keep/config.json")),
            Some(Node::File(b)) if b == b"{\"keep\":1}"
        ));
    }

    /// Each requested root is walked on its own (quick 261104-s3n: the walk
    /// is the listing): a sibling type's entries are read under the sibling,
    /// and a host entry whose name differs only in case or by a suffix is a
    /// different entry, never matched to a listed one.
    #[test]
    fn windows_read_does_not_let_a_sibling_satisfy_another() {
        let two_types = || Scope {
            mode: "production".to_string(),
            types: vec![
                ("ignition".to_string(), "database-connection".to_string()),
                ("ignition".to_string(), "database-connection2".to_string()),
            ],
            tag_providers: Vec::new(),
        };
        let fake = windows_fake();
        windows_tree(&fake);
        fake.file(
            &format!("{WRES}/production/ignition/database-connection2/Other/config.json"),
            "{\"o\":1}",
        );
        let ok = block_on(windows_transport(&fake).read(&two_types())).expect("siblings read");
        assert_eq!(
            ok.mode.len(),
            4,
            "3 files of the first type, 1 of its sibling"
        );

        // an entry the host holds in the SIBLING type that sftp would drop is
        // read under the sibling, by the sibling's own walk
        let fake = windows_fake();
        windows_tree(&fake);
        fake.file(
            &format!("{WRES}/production/ignition/database-connection2/Other/config.json"),
            "{\"o\":1}",
        );
        fake.knobs().hidden.push(format!(
            "{WRES}/production/ignition/database-connection2/Other/config.json"
        ));
        let ok = block_on(windows_transport(&fake).read(&two_types())).expect("sibling read");
        assert_eq!(
            ok.mode[&rel("ignition/database-connection2/Other/config.json")].bytes,
            b"{\"o\":1}"
        );

        // a host entry whose name differs only in case or by a suffix is a
        // different entry: it is reported, not matched
        for sibling in [
            "mes db,1/config.json",
            "MES db,10/config.json",
            "MES db,1/config.json2",
        ] {
            let fake = windows_fake();
            windows_tree(&fake);
            fake.knobs().enum_extra.push((
                format!("{WRES}/production/ignition/database-connection/{sibling}"),
                false,
                9,
            ));
            let why = windows_read_failure(&fake);
            assert!(why.contains(sibling), "{sibling}: {why}");
        }
    }

    #[test]
    fn shown_paths_respect_the_data_dir_component_boundary() {
        let fake = windows_fake();
        let t = windows_transport(&fake);
        assert_eq!(t.shown("/C:/gw/data/config"), "config");
        assert_eq!(t.shown("/C:/gw/data"), ".");
        assert_eq!(
            t.shown("/C:/gw/data-evil/config"),
            ".",
            "a sibling is not below data_dir"
        );
        assert_eq!(t.shown("/C:/gw/data2"), ".");
    }
}