patchloom 0.34.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
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
//! File walking, binary detection, and text-read helpers shared by CLI commands.
//!
//! size-waiver: accepted single-domain bulk (policy #1408). One module owns
//! ignore-aware walks, glob/exclude filtering, binary/UTF-8 guards, and
//! path-missing helpers used by search/replace/tidy; co-located unit tests push
//! the file over 1000 lines. Do not split for LOC alone.
//!
//! # Text I/O honesty (#1894)
//!
//! - **Strict sole-path:** [`load_text_strict`] — binary → `BinaryError`, invalid UTF-8 → `InvalidEncodingError` (CLI, MCP/tx sole path, library `api::*`).
//! - **Soft content skip (walks):** [`try_read_text_file`] / [`read_text_file`] —
//!   binary / invalid UTF-8 → [`SoftTextSkip`] (content not agent-editable).
//! - **Unreadable is not content SoftSkip:** open/read IO is
//!   [`SoftTextSkip::Unreadable`]. Callers decide: directory walks may
//!   continue but must not report pattern `no_matches` when unreadable
//!   paths may have masked the scan; sole paths use Strict IO errors.
//! - Byte rule: [`classify_text_bytes_owned`] / [`classify_text_bytes`]
//!   (NUL probe + UTF-8). Owning loaders pass a `Vec<u8>` so UTF-8
//!   validation does not copy the buffer.

#[cfg(feature = "cli")]
use crate::cli::global::GlobalFlags;
#[cfg(any(feature = "cli", feature = "files"))]
use globset::{Glob, GlobBuilder, GlobSet, GlobSetBuilder};
#[cfg(any(feature = "cli", feature = "files"))]
use ignore::WalkBuilder;
#[cfg(any(feature = "cli", feature = "files"))]
use ignore::WalkState;
use std::path::Path;
#[cfg(any(feature = "cli", feature = "files"))]
use std::path::PathBuf;
#[cfg(any(feature = "cli", feature = "files"))]
use std::sync::Mutex;
#[cfg(any(feature = "cli", feature = "files"))]
use std::sync::atomic::{AtomicUsize, Ordering};

/// Compute a display-friendly relative path by stripping a `base` prefix.
///
/// Returns the relative portion if `path` is under `base`, otherwise returns
/// the original path unchanged. Used by diff headers, search results, and JSON
/// output so users see `src/main.rs` instead of `/home/user/project/src/main.rs`.
#[cfg(any(feature = "cli", feature = "files"))]
pub fn relative_display<'a>(path: &'a Path, base: &Path) -> &'a Path {
    path.strip_prefix(base).unwrap_or(path)
}

/// Check if a string contains common regex metacharacters that suggest
/// the user intended a regex pattern but forgot `--regex` (or used `--literal`).
#[cfg(feature = "cli")]
pub(crate) fn has_regex_metacharacters(s: &str) -> bool {
    s.contains('\\')
        || s.contains('[')
        || s.contains('(')
        || s.contains('{')
        || s.contains('*')
        || s.contains('+')
        || s.contains('?')
        || s.contains('|')
        || s.contains('^')
        || s.contains('$')
}

/// Bytes inspected for a NUL before treating a file as agent-editable text.
pub(crate) const BINARY_PROBE_LEN: usize = 8192;

pub fn is_binary(data: &[u8]) -> bool {
    let check_len = data.len().min(BINARY_PROBE_LEN);
    memchr::memchr(0, &data[..check_len]).is_some()
}

/// Append up to [`BINARY_PROBE_LEN`] bytes, or until EOF.
///
/// `Read::read` may return a short count before EOF.
pub(crate) fn read_binary_probe_into<R: std::io::Read>(
    reader: &mut R,
    buf: &mut Vec<u8>,
) -> std::io::Result<()> {
    use std::io::Read;
    reader.take(BINARY_PROBE_LEN as u64).read_to_end(buf)?;
    Ok(())
}

/// Caps each `read` so a single syscall cannot fill the 8 KiB window.
#[cfg(test)]
pub(crate) struct ShortRead<R> {
    pub inner: R,
    pub max_chunk: usize,
}

#[cfg(test)]
impl<R: std::io::Read> std::io::Read for ShortRead<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = buf.len().min(self.max_chunk);
        self.inner.read(&mut buf[..n])
    }
}

/// Result of classifying on-disk (or in-memory) bytes as agent-editable text.
///
/// Shared by CLI, MCP/tx, and library loaders so they cannot disagree about
/// "is this text?" (#1894). Binary uses the same 8 KiB NUL probe as [`is_binary`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TextBytesKind {
    /// Valid UTF-8 text with no NUL in the binary probe window.
    Text(String),
    /// NUL in the first 8 KiB (or empty probe treated via [`is_binary`]).
    Binary,
    /// Not binary by NUL probe, but not valid UTF-8.
    InvalidUtf8,
}

/// Classify an owned buffer as text, binary, or invalid UTF-8.
///
/// Empty input is **Text** (empty string). This is the single byte-level
/// rule for the text I/O honesty layer (#1894). Callers that already own
/// a `Vec<u8>` should use this so `String::from_utf8` can take the
/// buffer instead of copying it (#2382).
fn classify_utf8_owned(bytes: Vec<u8>) -> TextBytesKind {
    match String::from_utf8(bytes) {
        Ok(s) => TextBytesKind::Text(s),
        Err(_) => TextBytesKind::InvalidUtf8,
    }
}

pub fn classify_text_bytes_owned(bytes: Vec<u8>) -> TextBytesKind {
    if is_binary(&bytes) {
        return TextBytesKind::Binary;
    }
    classify_utf8_owned(bytes)
}

/// Classify a borrowed slice as text, binary, or invalid UTF-8.
///
/// Same rule as [`classify_text_bytes_owned`]. Probes the borrowed slice
/// first so a binary buffer is not copied (#2408). Copies only when the
/// NUL probe says the bytes are not binary. Each entry point runs
/// [`is_binary`] once.
pub fn classify_text_bytes(bytes: &[u8]) -> TextBytesKind {
    if is_binary(bytes) {
        return TextBytesKind::Binary;
    }
    classify_utf8_owned(bytes.to_vec())
}

/// Load a path as UTF-8 text under the **Strict** sole-path policy (#1894).
///
/// Use for explicit single-file mutators and sole-path reads (CLI/MCP/tx/`api`).
///
/// | Kind | Result |
/// |------|--------|
/// | Text | `Ok(String)` |
/// | Binary | `Err(BinaryError)` — `target is a binary file: {display}` (`error_kind: binary`) |
/// | Invalid UTF-8 | `Err(InvalidEncodingError)` — `target is not valid UTF-8 text: {display}` (`error_kind: invalid_encoding`) |
/// | Not a file (directory, FIFO, …) | `Err(InvalidInputError)` — `target is not a file: {display}` |
/// | Dangling symlink / special name | `Err(InvalidInputError)` — `target is not a file: {display}` (not IO NotFound) |
/// | IO NotFound | `Err` with `io::Error` + context `failed to read {display}` (`is_io_not_found`) |
/// | IO other (permission, …) | `Err(InvalidInputError)` — `failed to read {display}: {os error}` |
///
/// Binary / invalid UTF-8 are distinct from argument `invalid_input` so hosts can
/// recover overwrite without treating empty paths the same as content SoftSkip
/// (#1963). Non-NotFound IO is typed so agent JSON gets `error_kind: invalid_input`
/// and a single complete message that includes the OS error even when callers
/// format with `Display` / `to_string()` (which drops anyhow cause chains).
/// NotFound stays an IO error so [`crate::exit::is_io_not_found`] keeps working.
///
/// Directory walks and multi-path soft-skip must use [`read_text_file`] (or
/// `tx::read_and_probe`), not this function.
pub fn load_text_strict(path: &Path, display: &str) -> anyhow::Result<String> {
    use crate::ops::file::{PathEntryKind, classify_path_entry};
    let collapsed = crate::ops::file::windows_collapse_dest_path(path);
    let path = collapsed.as_path();
    crate::ops::file::ensure_not_windows_illegal_dest(path, display)?;
    match classify_path_entry(path) {
        PathEntryKind::RealDirectory => {
            return Err(crate::exit::InvalidInputError {
                msg: format!("target is not a file: {display}"),
            }
            .into());
        }
        PathEntryKind::Special if !path.exists() || !path.is_file() => {
            // Dangling symlink / FIFO / socket / symlink-to-dir: present
            // special name, not a missing path. Must not look like IO
            // NotFound (agents would create over the name). Live file
            // symlinks follow via fs::read (#1230).
            return Err(crate::exit::InvalidInputError {
                msg: format!("target is not a file: {display}"),
            }
            .into());
        }
        // RegularFile, live file symlink (Special + exists + is_file), Missing.
        PathEntryKind::RegularFile | PathEntryKind::Special | PathEntryKind::Missing => {}
    }
    let bytes = match std::fs::read(path) {
        Ok(b) => b,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            // Keep io::Error in the chain for `is_io_not_found`, and put the
            // OS detail in the context string so Display (not only `{:#}`)
            // includes "No such file" for agent JSON (MPI 2026-07-23: explain
            // showed "cannot read X: failed to read X" with no OS detail).
            let msg = format!("failed to read {display}: {e}");
            return Err(anyhow::Error::new(e).context(msg));
        }
        Err(e) => {
            // Full message in InvalidInputError so Display and JSON envelopes
            // keep "Permission denied" without requiring `{:#}` (fixrealloop
            // 2026-07-23: CLI read / MCP read dropped the OS detail).
            return Err(crate::exit::InvalidInputError {
                msg: format!("failed to read {display}: {e}"),
            }
            .into());
        }
    };
    match classify_text_bytes_owned(bytes) {
        TextBytesKind::Text(s) => Ok(s),
        TextBytesKind::Binary => Err(crate::exit::BinaryError {
            msg: format!("target is a binary file: {display}"),
        }
        .into()),
        TextBytesKind::InvalidUtf8 => Err(crate::exit::InvalidEncodingError {
            msg: format!("target is not valid UTF-8 text: {display}"),
        }
        .into()),
    }
}

/// True when `path` is a regular file (or symlink to one) safe to open for
/// text/binary probes. FIFOs, sockets, devices, and directories return false so
/// callers never block forever on open (#2113 family).
fn is_openable_regular_file(path: &Path) -> bool {
    if crate::ops::file::is_windows_illegal_dest_path(path) {
        return false;
    }
    match std::fs::metadata(path) {
        Ok(m) => m.is_file(),
        Err(_) => false,
    }
}

/// Returns whether the file at `path` appears to be binary by reading only its
/// first 8 KiB (streaming, no full allocation for large files).
///
/// Heuristic: true when a NUL byte appears in the probe window (same rule as
/// [`is_binary`]). Returns **false** if the path cannot be opened or read
/// (missing, permission, FIFO/socket/device, …); callers that need a typed open
/// error should open the path themselves.
///
/// Public for embedder preflight (#1884) so hosts do not reimplement the
/// window size or probe semantics. Writers still enforce binary themselves
/// via [`load_text_strict`] or `ops::file::ensure_not_binary_file`.
/// Available with default features and under `features = ["files"]` (always
/// compiled; no `cli` gate).
pub fn is_binary_file(path: &Path) -> bool {
    // Never open special nodes: FIFO/socket open blocks forever.
    if !is_openable_regular_file(path) {
        return false;
    }
    let mut file = match std::fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return false,
    };
    let mut buf = Vec::with_capacity(BINARY_PROBE_LEN);
    if read_binary_probe_into(&mut file, &mut buf).is_err() {
        return false;
    }
    is_binary(&buf)
}

/// Length of a Windows extended/device prefix whose `?` is not a glob.
/// `\\?\C:\file.txt` and `//?/C:/file.txt` must stay literal dests.
#[must_use]
fn windows_extended_prefix_len(path: &str) -> usize {
    let b = path.as_bytes();
    if b.len() >= 4 {
        let p = &b[..4];
        if p == br"\\?\" || p == b"//?/" || p == br"\\.\" || p == b"//./" {
            return 4;
        }
    }
    0
}

/// True when a dest looks like a glob Unix shells expand (`*.txt`, `sub/*.rs`).
/// Windows cmd and PowerShell pass those through, so scan dests must expand
/// them instead of peeling `not_found` / illegal dest.
/// `*` / `?` in a `\\?\` / `//?/` prefix are not glob metacharacters.
/// Always compiled: plan/tx path validation uses this without dest-glob expand.
#[must_use]
pub(crate) fn looks_like_glob_dest(path: &str) -> bool {
    let rest = &path[windows_extended_prefix_len(path)..];
    rest.as_bytes().iter().any(|b| matches!(b, b'*' | b'?'))
}

/// True when the user supplied explicit path roots and none of them exist.
///
/// Empty `paths` means the caller will default to `.` and is never "all
/// missing" here. Used so search/replace/tidy can distinguish path typos
/// (`not_found`) from pattern/whitespace soft success (`no_matches` / clean).
/// Glob dests are ignored: zero matches is pattern `no_matches`, not dest
/// `not_found`.
#[cfg(feature = "cli")]
pub(crate) fn all_explicit_paths_missing(paths: &[String], root: Option<&Path>) -> bool {
    if paths.is_empty() {
        return false;
    }
    let literals: Vec<&String> = paths.iter().filter(|p| !looks_like_glob_dest(p)).collect();
    if literals.is_empty() {
        // Only glob dests: zero matches is pattern `no_matches`, not dest `not_found`.
        return false;
    }
    literals.iter().all(|p| {
        let resolved = match root {
            Some(r) if !std::path::Path::new(p).is_absolute() => r.join(p),
            _ => std::path::PathBuf::from(p),
        };
        !crate::ops::file::path_entry_exists(&resolved)
    })
}

/// Like [`all_explicit_paths_missing`], but prefers `--files-from` entries when
/// set. Does not re-read stdin (`--files-from -`); those lists skip this check.
#[cfg(feature = "cli")]
pub(crate) fn all_scan_targets_missing(
    global: &GlobalFlags,
    paths: &[String],
    root: Option<&Path>,
) -> anyhow::Result<bool> {
    if global.files_from.as_deref() == Some("-") {
        return Ok(false);
    }
    if global.files_from.is_some() {
        let Some(files) = global.read_files_from()? else {
            return Ok(false);
        };
        return Ok(all_explicit_paths_missing(&files, root));
    }
    Ok(all_explicit_paths_missing(paths, root))
}

/// `--files-from` list entries that do not exist under `cwd` (agent JSON).
///
/// Returns `None` when files-from is unset, is stdin (`-`, single-read), or
/// every listed path exists. Used so soft-skips are visible under `--json`
/// even when `--quiet` suppresses stderr (#1756).
#[cfg(feature = "cli")]
pub(crate) fn files_from_missing_entries(
    global: &crate::cli::global::GlobalFlags,
    cwd: &Path,
) -> anyhow::Result<Option<Vec<String>>> {
    if global.files_from.as_deref() == Some("-") {
        return Ok(None);
    }
    let Some(files) = global.read_files_from()? else {
        return Ok(None);
    };
    Ok(missing_paths_under(cwd, &files))
}

/// Explicit CLI path args that do not exist under `cwd` (agent JSON `skipped`).
///
/// Mirrors [`files_from_missing_entries`] for positional paths so partial
/// multi-path replace/search is not `ok: true` with only stderr warnings.
/// Returns `None` when `paths` is empty (directory walk) or every path exists.
#[cfg(feature = "cli")]
#[must_use]
pub(crate) fn explicit_paths_missing_entries(cwd: &Path, paths: &[String]) -> Option<Vec<String>> {
    if paths.is_empty() {
        return None;
    }
    missing_paths_under(cwd, paths)
}

/// Prefer `--files-from` missing entries when set; otherwise explicit path args.
#[cfg(feature = "cli")]
pub(crate) fn scan_missing_entries(
    global: &crate::cli::global::GlobalFlags,
    cwd: &Path,
    paths: &[String],
) -> anyhow::Result<Option<Vec<String>>> {
    if global.files_from.is_some() {
        files_from_missing_entries(global, cwd)
    } else {
        Ok(explicit_paths_missing_entries(cwd, paths))
    }
}

#[cfg(feature = "cli")]
fn missing_paths_under(cwd: &Path, paths: &[String]) -> Option<Vec<String>> {
    let mut missing = Vec::new();
    for f in paths {
        if looks_like_glob_dest(f) {
            continue;
        }
        if !crate::ops::file::path_entry_exists(&cwd.join(f)) {
            missing.push(f.clone());
        }
    }
    if missing.is_empty() {
        None
    } else {
        Some(missing)
    }
}

/// Reject an empty `--files-from` list as `invalid_input` (exit 1).
///
/// Soft `no_matches` (search) or clean success (tidy check) would mislead agents
/// into widening the pattern or treating the workspace as clean when zero paths
/// were scanned. Same contract as replace (#1796), applied to all scan commands.
#[cfg(feature = "cli")]
pub(crate) fn ensure_files_from_nonempty(
    global: &GlobalFlags,
    file_paths: &[PathBuf],
) -> anyhow::Result<()> {
    if global.files_from.is_some() && file_paths.is_empty() {
        return Err(crate::exit::InvalidInputError {
            msg: "empty --files-from path list (no files to scan)".into(),
        }
        .into());
    }
    Ok(())
}

/// Collect file paths from either `--files-from`, or by walking `paths` with
/// `ignore::WalkBuilder` (respects `.gitignore`).  When `root` is `Some`,
/// paths are joined with it before walking.  Tidy commands set
/// `include_hidden = true` so dotfiles are checked.
///
/// Pass `files_from_preload` when the caller already read `--files-from`
/// (including stdin `-`). Stdin can only be consumed once; re-reading yields
/// an empty list and breaks sole-binary / refused honesty.
#[cfg(feature = "cli")]
pub(crate) fn collect_file_paths_opts(
    paths: &[String],
    global: &GlobalFlags,
    include_hidden: bool,
    root: Option<&Path>,
) -> anyhow::Result<Vec<PathBuf>> {
    collect_file_paths_opts_with_list(paths, global, include_hidden, root, None, None)
}

/// Like [`collect_file_paths_opts`], with optional walk-time [`max_depth`].
///
/// `max_depth` is passed to [`ignore::WalkBuilder::max_depth`] so deep trees
/// are not entered (MCP `list_files` #2078). Semantics match component count
/// under each walk root: `Some(1)` is files directly under each root.
#[cfg(feature = "cli")]
pub(crate) fn collect_file_paths_opts_depth(
    paths: &[String],
    global: &GlobalFlags,
    include_hidden: bool,
    root: Option<&Path>,
    max_depth: Option<usize>,
) -> anyhow::Result<Vec<PathBuf>> {
    collect_file_paths_opts_with_list(paths, global, include_hidden, root, None, max_depth)
}

/// Like [`collect_file_paths_opts`], but accepts a pre-read `--files-from` list.
#[cfg(feature = "cli")]
pub(crate) fn collect_file_paths_opts_with_list(
    paths: &[String],
    global: &GlobalFlags,
    include_hidden: bool,
    root: Option<&Path>,
    files_from_preload: Option<&[String]>,
    max_depth: Option<usize>,
) -> anyhow::Result<Vec<PathBuf>> {
    let files_owned;
    let files_from: Option<&[String]> = if let Some(pre) = files_from_preload {
        Some(pre)
    } else if global.files_from.is_some() {
        files_owned = global.read_files_from()?;
        files_owned.as_deref()
    } else {
        None
    };
    if let Some(files) = files_from {
        // --files-from entries must honor --contain (paths may escape even when
        // CLI positional paths were empty or in-workspace).
        if let Some(r) = root {
            global.check_paths_contained(r, files)?;
        }
        return Ok(files
            .iter()
            .map(|f| {
                let raw = match root {
                    Some(r) => r.join(f),
                    None => PathBuf::from(f),
                };
                crate::ops::file::windows_collapse_dest_path(&raw)
            })
            .collect());
    }
    let defaults;
    let effective: &[String] = if paths.is_empty() {
        defaults = [".".to_string()];
        &defaults
    } else {
        paths
    };
    let resolve = |p: &str| -> PathBuf {
        let raw = match root {
            Some(r) => r.join(p),
            None => PathBuf::from(p),
        };
        crate::ops::file::windows_collapse_dest_path(&raw)
    };
    // Unix shells expand `*.txt` before exec. Windows cmd/PowerShell do not.
    // Split glob dests from literal walk roots so `search KEEP *.txt` is
    // `--glob *.txt` over `.`, not dest `not_found`.
    let mut walk_specs: Vec<String> = Vec::new();
    let mut dest_globs: Vec<String> = Vec::new();
    for p in effective {
        let resolved = resolve(p);
        // On Windows, `exists("C:\\ws\\*.txt")` is true when any .txt
        // exists (wildcard FindFirstFile). That skipped dest-glob expand
        // and walked the tree. `*` / `?` cannot be a real Win32 name.
        let as_glob = looks_like_glob_dest(p)
            && (cfg!(windows) || !crate::ops::file::path_entry_exists(&resolved));
        if as_glob {
            dest_globs.push(p.clone());
        } else {
            walk_specs.push(p.clone());
        }
    }
    let dest_glob_only = walk_specs.is_empty() && !dest_globs.is_empty();
    let dest_walk_depth = dest_glob_walk_max_depth(&dest_globs);
    let literal_specs = walk_specs.clone();
    // Mix + no `**`: walk literals unbounded; walk `.` separately with dest
    // depth so `search KEEP src *.txt` does not recurse the whole cwd.
    let mix_capped_dot = !dest_glob_only
        && dest_walk_depth.is_some()
        && !walk_specs.iter().any(|s| s == "." || s == "./");
    if walk_specs.is_empty() {
        walk_specs.push(".".to_string());
    } else if !dest_globs.is_empty()
        && !walk_specs.iter().any(|s| s == "." || s == "./")
        && !mix_capped_dot
    {
        // Mix + `**`: walk cwd unbounded so dest `**/*.txt` can match.
        walk_specs.push(".".to_string());
    }
    let dest_glob_matcher = build_dest_glob_matcher(&dest_globs)?;
    let effective: &[String] = &walk_specs;
    // Explicit walk roots under --contain (defense-in-depth for callers that
    // skip an early check_paths_contained on the same list).
    if let Some(r) = root {
        global.check_paths_contained(r, effective)?;
    }
    // Warn about nonexistent user-supplied paths so typos are visible
    // instead of silently producing an empty result set (exit 3 / soft success).
    // Under --json/--jsonl, callers list soft-misses in `skipped[]`; skip the
    // human stderr line so agents that merge streams do not treat it as a hard
    // failure (#1797).
    // Callers that want a hard not_found should also check
    // [`all_explicit_paths_missing`].
    for p in effective {
        let resolved = resolve(p);
        if !crate::ops::file::path_entry_exists(&resolved) && !global.json && !global.jsonl {
            eprintln!(
                "patchloom: {}: No such file or directory",
                resolved.display()
            );
        }
    }

    let first = resolve(&effective[0]);
    let mut builder = WalkBuilder::new(&first);
    apply_platform_ignore_case(&mut builder);
    for p in &effective[1..] {
        builder.add(resolve(p));
    }
    if include_hidden {
        builder.hidden(false);
    }
    // Walk-time prune: ignore crate depth is per root (depth 1 = root +
    // immediate children). Matches list_files max_depth component count (#2078).
    // Dest-glob-only without `**` also caps here (`*.txt` is cwd files).
    let walk_depth = if dest_glob_only {
        match (max_depth, dest_walk_depth) {
            (Some(a), Some(b)) => Some(a.min(b)),
            (Some(a), None) => Some(a),
            (None, Some(b)) => Some(b),
            (None, None) => None,
        }
    } else {
        max_depth
    };
    if let Some(depth) = walk_depth {
        builder.max_depth(Some(depth));
    }
    // Never enter .git / .patchloom; prune exclude prefixes (vendor/**) at walk
    // time so we do not descend into trees that apply_exclude_globs would drop.
    let exclude_set = build_glob_matcher(&global.exclude)?;
    attach_walk_entry_filter(
        &mut builder,
        exclude_set.clone(),
        root.map(Path::to_path_buf),
    );
    // Support advanced layered ignores (e.g. .agentignore) for parity with library
    // `collect_file_paths_with_ignores` and `api::SearchOptions` (#821).
    for name in &global.ignore_file {
        builder.add_custom_ignore_filename(name);
    }
    let collected: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());

    // Flush-on-drop wrapper so entries remaining in a thread-local batch
    // are merged into the shared vec when the per-thread worker is dropped.
    struct FlushOnDrop<'a> {
        batch: Vec<PathBuf>,
        target: &'a Mutex<Vec<PathBuf>>,
    }
    impl Drop for FlushOnDrop<'_> {
        fn drop(&mut self) {
            if !self.batch.is_empty() {
                self.target
                    .lock()
                    .expect("file list mutex")
                    .append(&mut self.batch);
            }
        }
    }

    builder.build_parallel().run(|| {
        let mut state = FlushOnDrop {
            batch: Vec::with_capacity(256),
            target: &collected,
        };
        Box::new(move |result| {
            let Ok(entry) = result else {
                return WalkState::Continue;
            };
            // Skip VCS / internal dirs even when include_hidden=true (tidy).
            // .git: objects are binary; walking them emits invalid-UTF-8 noise
            // (fixrealloop 2026-07-15). .patchloom: backup storage (#1349).
            if should_skip_walk_dirname(entry.file_name()) {
                return WalkState::Skip;
            }
            if entry.file_type().is_some_and(|ft| ft.is_file()) {
                state.batch.push(entry.into_path());
                if state.batch.len() >= 256 {
                    state
                        .target
                        .lock()
                        .expect("file list mutex")
                        .append(&mut state.batch);
                }
            }
            WalkState::Continue
        })
    });
    let mut paths = collected.into_inner().expect("all walkers done");

    if mix_capped_dot {
        let dest_dot_depth = match (max_depth, dest_walk_depth) {
            (Some(a), Some(b)) => Some(a.min(b)),
            (_, Some(b)) => Some(b),
            (Some(a), None) => Some(a),
            (None, None) => None,
        };
        let mut dest_builder = WalkBuilder::new(resolve("."));
        apply_platform_ignore_case(&mut dest_builder);
        if include_hidden {
            dest_builder.hidden(false);
        }
        if let Some(depth) = dest_dot_depth {
            dest_builder.max_depth(Some(depth));
        }
        attach_walk_entry_filter(
            &mut dest_builder,
            exclude_set.clone(),
            root.map(Path::to_path_buf),
        );
        for name in &global.ignore_file {
            dest_builder.add_custom_ignore_filename(name);
        }
        paths.extend(collect_files_from_walk_builder(dest_builder));
    }

    if let Some(ref matcher) = dest_glob_matcher {
        // Root-relative only. Filename fallback would make `*.txt` match
        // `sub/a.txt`, but Unix shells and `dir *.txt` stay in cwd.
        let dest_roots: Vec<PathBuf> = match root {
            Some(r) => vec![normalize_glob_root(r.to_path_buf())],
            None => vec![PathBuf::from(".")],
        };
        if dest_glob_only {
            paths.retain(|p| matches_dest_glob(p, matcher, &dest_roots));
        } else {
            let literal_roots: Vec<PathBuf> = literal_specs.iter().map(|s| resolve(s)).collect();
            paths.retain(|p| {
                literal_roots.iter().any(|r| p == r || p.starts_with(r))
                    || matches_dest_glob(p, matcher, &dest_roots)
            });
        }
    }

    // Explicit file path args must not be dropped by exclude. Config like
    // exclude.globs = ["vendor/**"] is for walks; a targeted
    // `replace … vendor/pkg/x.js` must still hit that file. Directory roots
    // keep exclude for their contents. --files-from already skips exclude.
    let explicit_files: Vec<PathBuf> = effective
        .iter()
        .map(|p| resolve(p))
        .filter(|p| p.is_file())
        .collect();

    // File-level excludes (*.rs) and defense-in-depth after walk-time prune.
    apply_exclude_globs(&mut paths, &global.exclude, root)?;

    for f in explicit_files {
        if !paths.iter().any(|p| p == &f) {
            paths.push(f);
        }
    }
    Ok(paths)
}

/// Strip leading `./` / `.\` so `./*.txt` and `.\*.txt` match cwd files.
/// Unix shells expand those before exec; Windows cmd/PowerShell pass them
/// through.
#[cfg(any(feature = "cli", feature = "files"))]
fn strip_leading_dot_slash(pattern: &str) -> &str {
    let mut p = pattern;
    loop {
        if let Some(rest) = p.strip_prefix("./") {
            p = rest;
            continue;
        }
        if let Some(rest) = p.strip_prefix(".\\") {
            p = rest;
            continue;
        }
        break;
    }
    p
}

/// Map a globset compile failure to [`crate::exit::InvalidInputError`].
///
/// Untyped `?` remaps to `operation_failed` / missing `error_kind` on CLI
/// `--json` (tx search/replace glob, `--glob`, dest-glob). Same class as
/// `for_each` (#2182).
#[cfg(any(feature = "cli", feature = "files"))]
fn invalid_glob_error(pattern: &str, err: impl std::fmt::Display) -> anyhow::Error {
    anyhow::Error::new(crate::exit::InvalidInputError {
        msg: format!("error parsing glob '{pattern}': {err}"),
    })
}

/// Compile a user `--glob` / exclude / `for_each` pattern.
///
/// Windows file names are case-insensitive. `*.txt` must match `Hit.TXT`
/// the same way `dir *.txt` does. Linux stays case-sensitive.
#[cfg(any(feature = "cli", feature = "files"))]
pub(crate) fn compile_user_glob(pattern: &str) -> anyhow::Result<Glob> {
    let stripped = strip_leading_dot_slash(pattern);
    // Win32 `\` is a separator. globset otherwise treats it as a literal
    // (or escape), so `*.txt` matches `sub\a.txt` and `sub\*.txt` misses
    // a `/`-normalized relative path.
    GlobBuilder::new(stripped)
        .case_insensitive(cfg!(windows))
        .build()
        .map_err(|e| invalid_glob_error(pattern, e))
}

/// Walk depth for dest-glob retain. `None` is unbounded (`**` or no dest-glob).
/// `*.txt` / `./*.txt` is cwd files (depth 1). `sub/*.txt` is one directory
/// (depth 2). `--glob *.txt` is not dest-glob and stays recursive.
#[cfg(feature = "cli")]
fn dest_glob_walk_max_depth(globs: &[String]) -> Option<usize> {
    if globs.is_empty() {
        return None;
    }
    let mut cap = 0usize;
    for pattern in globs {
        let stripped = strip_leading_dot_slash(pattern);
        let normalized = if cfg!(windows) && stripped.contains('\\') {
            stripped.replace('\\', "/")
        } else {
            stripped.to_string()
        };
        if normalized.contains("**") {
            return None;
        }
        cap = cap.max(normalized.matches('/').count() + 1);
    }
    Some(cap)
}

/// Dest-glob with no `**` (`*.txt`, `sub/*.txt`). Not recursive.
#[cfg(feature = "cli")]
#[must_use]
pub(crate) fn dest_glob_is_non_recursive(path: &str) -> bool {
    looks_like_glob_dest(path) && dest_glob_walk_max_depth(&[path.to_string()]).is_some()
}

/// Last `/` or `\` component of a dest glob (`*.txt`, `sub/*.txt` → `*.txt`).
#[cfg(feature = "cli")]
fn dest_glob_leaf(dest: &str) -> &str {
    dest.rsplit(['/', '\\']).next().unwrap_or(dest)
}

/// Dest-glob that walks cwd files only (`*.txt`, `./*.txt`). `sub/*.txt` is not.
#[cfg(feature = "cli")]
fn dest_glob_is_cwd_only(path: &str) -> bool {
    looks_like_glob_dest(path) && dest_glob_walk_max_depth(&[path.to_string()]) == Some(1)
}

/// Dest-glob scope rule for agent JSON `error` (not stderr-only).
/// `*.txt` is cwd-only; `sub/*.txt` is that directory only. Remedy uses dest leaf.
#[cfg(feature = "cli")]
#[must_use]
pub(crate) fn dest_glob_cwd_only_rule(paths: &[String]) -> Option<String> {
    let dest = paths.iter().find(|p| dest_glob_is_non_recursive(p))?;
    let leaf = dest_glob_leaf(dest);
    let scope = if dest_glob_is_cwd_only(dest) {
        "the current directory only"
    } else {
        "that directory only"
    };
    Some(format!(
        "dest `{dest}` matches files in {scope}; use `**/{leaf}` or `--glob '{leaf}'` for nested files"
    ))
}

/// Append [`dest_glob_cwd_only_rule`] so JSON `error` names dest-glob scope.
#[cfg(feature = "cli")]
#[must_use]
pub(crate) fn with_dest_glob_cwd_only_rule(msg: &str, paths: &[String]) -> String {
    match dest_glob_cwd_only_rule(paths) {
        Some(rule) => format!("{msg}. {rule}"),
        None => msg.to_string(),
    }
}

/// Skip the `-i` tip when dest-glob expanded zero files or dest-glob
/// dropped nested-only hits (cwd-only dest such as `*.txt`).
/// Dest `sub/*.txt` with files found is a content miss: keep the `-i` tip.
#[cfg(feature = "cli")]
#[must_use]
pub(crate) fn dest_glob_skip_case_tip(paths: &[String], dest_glob_files_empty: bool) -> bool {
    if !paths.iter().any(|p| looks_like_glob_dest(p)) {
        return false;
    }
    dest_glob_files_empty || paths.iter().any(|p| dest_glob_is_cwd_only(p))
}

/// Dest-glob compile: `/` separators and `*` does not cross directories.
/// Unix shells and `dir *.txt` stay in one directory; `**` is recursive.
#[cfg(feature = "cli")]
fn compile_dest_glob(pattern: &str) -> anyhow::Result<Glob> {
    let stripped = strip_leading_dot_slash(pattern);
    let normalized = if cfg!(windows) && stripped.contains('\\') {
        stripped.replace('\\', "/")
    } else {
        stripped.to_string()
    };
    GlobBuilder::new(&normalized)
        .case_insensitive(cfg!(windows))
        .literal_separator(true)
        .build()
        .map_err(|e| invalid_glob_error(pattern, e))
}

/// Git on Windows defaults `core.ignorecase=true`. Honor that so
/// `.gitignore` `*.log` also drops `app.LOG` (R121 live red).
#[cfg(any(feature = "cli", feature = "files"))]
pub(crate) fn apply_platform_ignore_case(builder: &mut WalkBuilder) {
    builder.ignore_case_insensitive(cfg!(windows));
}

#[cfg(feature = "cli")]
fn build_dest_glob_matcher(globs: &[String]) -> anyhow::Result<Option<GlobSet>> {
    if globs.is_empty() {
        return Ok(None);
    }
    let mut builder = GlobSetBuilder::new();
    for pattern in globs {
        builder.add(compile_dest_glob(pattern)?);
    }
    builder
        .build()
        .map(Some)
        .map_err(|e| invalid_glob_error(globs.first().map_or("", String::as_str), e))
}

/// Build a compiled glob matcher from globs, or `None` if no globs given.
/// Available for library use when "files" feature is enabled.
#[cfg(any(feature = "cli", feature = "files"))]
pub fn build_glob_matcher(globs: &[String]) -> anyhow::Result<Option<GlobSet>> {
    if globs.is_empty() {
        return Ok(None);
    }
    let mut builder = GlobSetBuilder::new();
    for pattern in globs {
        builder.add(compile_user_glob(pattern)?);
    }
    builder
        .build()
        .map(Some)
        .map_err(|e| invalid_glob_error(globs.first().map_or("", String::as_str), e))
}

/// Build from GlobalFlags (cli only).
#[cfg(feature = "cli")]
pub(crate) fn build_glob_matcher_from_global(
    global: &GlobalFlags,
) -> anyhow::Result<Option<GlobSet>> {
    build_glob_matcher(&global.glob)
}

/// Collect roots used for matching globs against walked files.
/// Lib version.
#[cfg(any(feature = "cli", feature = "files"))]
pub fn collect_glob_roots(paths: &[PathBuf], root: Option<&Path>) -> Vec<PathBuf> {
    let mut roots = Vec::new();
    for path in paths {
        let resolved = match root {
            Some(r) => r.join(path),
            None => path.clone(),
        };
        let glob_root = if resolved.is_file() {
            resolved
                .parent()
                .map(Path::to_path_buf)
                .unwrap_or_else(|| resolved.clone())
        } else {
            resolved.clone()
        };
        let glob_root = normalize_glob_root(glob_root);
        if !roots.contains(&glob_root) {
            roots.push(glob_root);
        }
    }

    roots
}

/// Collect roots from GlobalFlags (cli only, for --files-from etc).
#[cfg(feature = "cli")]
pub(crate) fn collect_glob_roots_from_global(
    paths: &[String],
    global: &GlobalFlags,
    root: Option<&Path>,
) -> anyhow::Result<Vec<PathBuf>> {
    if global.files_from.is_some() {
        return Ok(root.map(|r| vec![r.to_path_buf()]).unwrap_or_default());
    }

    let defaults;
    let effective: &[String] = if paths.is_empty() {
        defaults = [".".to_string()];
        &defaults
    } else {
        paths
    };

    let paths_buf: Vec<PathBuf> = effective
        .iter()
        .map(|p| match root {
            Some(r) => r.join(p),
            None => PathBuf::from(p),
        })
        .collect();

    Ok(collect_glob_roots(&paths_buf, root))
}

#[cfg(any(feature = "cli", feature = "files"))]
fn normalize_glob_root(path: PathBuf) -> PathBuf {
    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::CurDir => {}
            _ => normalized.push(component.as_os_str()),
        }
    }
    if normalized.as_os_str().is_empty() {
        PathBuf::from(".")
    } else {
        normalized
    }
}

#[cfg(any(feature = "cli", feature = "files"))]
fn glob_matches_path(path: &Path, matcher: &GlobSet) -> bool {
    matcher.is_match(path) || path.file_name().is_some_and(|name| matcher.is_match(name))
}

/// Dest-glob retain: cwd-relative path with `/` separators.
/// Do not match the absolute walk path: on Windows globset treats `\` as
/// a normal character, so `*.txt` would match `C:\ws\sub\a.txt`.
/// `*.txt` is cwd files, like a Unix shell or `dir *.txt`. `--glob`
/// still uses [`matches_glob_with_roots`] (recursive filename match).
#[cfg(feature = "cli")]
fn matches_dest_glob(path: &Path, matcher: &GlobSet, roots: &[PathBuf]) -> bool {
    roots.iter().any(|root| {
        let Ok(relative) = path.strip_prefix(root) else {
            return false;
        };
        if relative.as_os_str().is_empty() {
            return false;
        }
        let rel = relative.to_string_lossy().replace('\\', "/");
        matcher.is_match(rel.as_str())
    })
}

/// Check whether `path` matches any of the globs, either directly or relative
/// to one of the provided roots (always true if no globs).
#[cfg(any(feature = "cli", feature = "files"))]
pub fn matches_glob_with_roots(path: &Path, matcher: Option<&GlobSet>, roots: &[PathBuf]) -> bool {
    match matcher {
        None => true,
        Some(m) => {
            matches_glob(path, Some(m))
                || roots.iter().any(|root| {
                    path.strip_prefix(root).ok().is_some_and(|relative| {
                        !relative.as_os_str().is_empty() && matches_glob(relative, Some(m))
                    })
                })
        }
    }
}

/// Check whether `path` matches any of the globs (always true if no globs).
#[cfg(any(feature = "cli", feature = "files"))]
pub fn matches_glob(path: &Path, matcher: Option<&GlobSet>) -> bool {
    match matcher {
        None => true,
        Some(m) => glob_matches_path(path, m),
    }
}

/// Why a soft walk skipped a path (#1894).
///
/// **Content skips** ([`Binary`], [`InvalidUtf8`]) mean "not agent-editable
/// text." **[`Unreadable`]** is operational (permission/IO), not a content
/// SoftSkip: sole paths should hard-fail via [`load_text_strict`]; multi-path
/// walks may continue but must not report pattern `no_matches` when any
/// unreadable path may have masked the scan.
/// **[`NotRegularFile`]** is FIFO/socket/device/directory (or other non-regular
/// entry): open would hang or is not text; multi-path `refused[]` reason is
/// `not_regular_file` (not `unreadable`) so agents do not retry chmod
/// (fixrealloop 2026-08-03).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SoftTextSkip {
    /// NUL in the binary probe window.
    Binary,
    /// Valid NUL probe but not UTF-8.
    InvalidUtf8,
    /// Open/read/metadata failed (missing, permission, …).
    Unreadable,
    /// Path exists but is not a regular file (FIFO, socket, device, directory).
    /// Appended after [`Unreadable`] (0.25 honesty; fixrealloop multi-path refuse).
    NotRegularFile,
}

impl SoftTextSkip {
    /// Stable agent-facing reason for `refused[]` / logs.
    pub fn as_reason(self) -> &'static str {
        match self {
            SoftTextSkip::Binary => "binary",
            SoftTextSkip::InvalidUtf8 => "invalid_utf8",
            SoftTextSkip::Unreadable => "unreadable",
            SoftTextSkip::NotRegularFile => "not_regular_file",
        }
    }

    /// Content SoftSkip (binary / invalid UTF-8), not operational unreadable.
    pub fn is_content_skip(self) -> bool {
        matches!(self, SoftTextSkip::Binary | SoftTextSkip::InvalidUtf8)
    }
}

fn soft_text_from_kind(kind: TextBytesKind) -> Result<String, SoftTextSkip> {
    match kind {
        TextBytesKind::Text(s) => Ok(s),
        TextBytesKind::Binary => Err(SoftTextSkip::Binary),
        TextBytesKind::InvalidUtf8 => Err(SoftTextSkip::InvalidUtf8),
    }
}

/// Soft-path text load with typed skip reason (#1894).
///
/// Empty files return `Ok("")`. For sole-path mutators use [`load_text_strict`].
///
/// For files larger than 8 KiB, only the first 8 KiB are read initially
/// for the binary check. If the file is binary, no further I/O occurs.
///
/// Never opens FIFOs, sockets, or devices (would block forever); those
/// return [`SoftTextSkip::NotRegularFile`] when the path exists as a non-regular
/// entry, else [`SoftTextSkip::Unreadable`] when missing / metadata fails.
pub fn try_read_text_file(path: &Path) -> Result<String, SoftTextSkip> {
    use std::io::Read;

    let collapsed = crate::ops::file::windows_collapse_dest_path(path);
    let path = collapsed.as_path();

    // `\\.\CON` / `\\.\NUL` / `\\.\pipe\...` are not files. Open hangs
    // (console) or is a device. Same refuse as dest writes (#2322).
    if crate::ops::file::is_windows_illegal_dest_path(path) {
        return Err(SoftTextSkip::NotRegularFile);
    }

    // Metadata-first: `File::open` on a FIFO blocks until a writer connects.
    if !is_openable_regular_file(path) {
        // Same kind as dest-symlink refuse: classify_path_entry, not raw
        // FileType::is_file. Windows dangling file symlinks report is_file
        // true; they must skip as NotRegularFile, not Unreadable.
        use crate::ops::file::{PathEntryKind, classify_path_entry};
        return match classify_path_entry(path) {
            PathEntryKind::Missing | PathEntryKind::RegularFile => Err(SoftTextSkip::Unreadable),
            PathEntryKind::Special | PathEntryKind::RealDirectory => {
                Err(SoftTextSkip::NotRegularFile)
            }
        };
    }

    let mut file = match std::fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return Err(SoftTextSkip::Unreadable),
    };

    let file_len = match file.metadata() {
        Ok(m) => m.len() as usize,
        Err(_) => return Err(SoftTextSkip::Unreadable),
    };
    if file_len == 0 {
        return Ok(String::new());
    }

    // Probe 8 KiB first so a large binary does not get a body-sized Vec.
    // After a text probe, reserve the known length and keep one body Vec.
    if file_len > BINARY_PROBE_LEN {
        let mut bytes = Vec::with_capacity(BINARY_PROBE_LEN);
        if read_binary_probe_into(&mut file, &mut bytes).is_err() {
            return Err(SoftTextSkip::Unreadable);
        }
        if is_binary(&bytes) {
            return Err(SoftTextSkip::Binary);
        }
        bytes.reserve(file_len.saturating_sub(bytes.len()));
        if file.read_to_end(&mut bytes).is_err() {
            return Err(SoftTextSkip::Unreadable);
        }
        return soft_text_from_kind(classify_text_bytes_owned(bytes));
    }

    // Small file: read all at once (single syscall).
    let mut bytes = Vec::with_capacity(file_len);
    if file.read_to_end(&mut bytes).is_err() {
        return Err(SoftTextSkip::Unreadable);
    }

    soft_text_from_kind(classify_text_bytes_owned(bytes))
}

/// Soft-skip text load for walks; collapses all skip reasons to `None`.
///
/// Prefer [`try_read_text_file`] when unreadable must not be confused with
/// content SoftSkip (e.g. AST directory rename). Empty files return `Some("")`.
pub fn read_text_file(path: &Path) -> Option<String> {
    try_read_text_file(path).ok()
}

/// Internal version with optional diagnostic logging for CLI commands.
///
/// Re-opens on unreadable only to print the OS error (tests assert permission
/// strings); content SoftSkip logs stay reason-only.
#[cfg(feature = "cli")]
pub(crate) fn read_text_file_logged(path: &Path, cmd: &str, quiet: bool) -> Option<String> {
    match try_read_text_file(path) {
        Ok(s) => Some(s),
        Err(SoftTextSkip::Binary) => None,
        Err(SoftTextSkip::InvalidUtf8) => {
            if !quiet {
                eprintln!("{cmd}: skipping {} (invalid UTF-8)", path.display());
            }
            None
        }
        Err(SoftTextSkip::NotRegularFile) => {
            if !quiet {
                eprintln!("{cmd}: skipping {}: not a regular file", path.display());
            }
            None
        }
        Err(SoftTextSkip::Unreadable) => {
            if !quiet {
                // Regular-file path only: re-open for OS permission detail.
                // Specials use NotRegularFile and never reach here.
                let detail = std::fs::File::open(path)
                    .err()
                    .map(|e| e.to_string())
                    .or_else(|| std::fs::metadata(path).err().map(|e| e.to_string()))
                    .unwrap_or_else(|| "unreadable".into());
                eprintln!("{cmd}: skipping {}: {detail}", path.display());
            }
            None
        }
    }
}

/// Simple file collection for library use (sequential for simplicity; full parallel in par_process_files).
#[cfg(any(feature = "cli", feature = "files"))]
pub fn collect_file_paths(root: &Path, include_hidden: bool) -> anyhow::Result<Vec<PathBuf>> {
    collect_file_paths_with_ignores(root, &[], &[], include_hidden)
}

/// Whether `path` is excluded by the same rules as [`apply_exclude_globs`].
///
/// Checks the full path, the filename fallback, and (when `root` is set) the
/// path relative to that root so `vendor/**` matches `<root>/vendor/lib.rs`.
#[cfg(any(feature = "cli", feature = "files"))]
fn exclude_path_matches(path: &Path, root: Option<&Path>, matcher: &GlobSet) -> bool {
    if glob_matches_path(path, matcher) {
        return true;
    }
    if let Some(r) = root
        && let Ok(rel) = path.strip_prefix(r)
        && !rel.as_os_str().is_empty()
        && matcher.is_match(rel)
    {
        return true;
    }
    false
}

/// Drop paths that match exclude globs (post-filter).
#[cfg(any(feature = "cli", feature = "files"))]
fn apply_exclude_globset(paths: &mut Vec<PathBuf>, matcher: &GlobSet, root: Option<&Path>) {
    paths.retain(|p| !exclude_path_matches(p, root, matcher));
}

/// Apply exclude glob patterns to a list of paths (post-filter).
/// Shared to avoid duplication between collect_file_paths_with_ignores and
/// the advanced logic in collect_file_paths_opts.
///
/// When `root` is provided, each path is also tested as a relative path
/// (stripped from the root prefix) so that patterns like `vendor/**` match
/// files at `<root>/vendor/lib.rs` even though the full path is absolute.
#[cfg(any(feature = "cli", feature = "files"))]
fn apply_exclude_globs(
    paths: &mut Vec<PathBuf>,
    patterns: &[String],
    root: Option<&Path>,
) -> anyhow::Result<()> {
    if let Some(ex) = build_glob_matcher(patterns)? {
        apply_exclude_globset(paths, &ex, root);
    }
    Ok(())
}

/// True when exclude globs would drop every descendant of `dir`.
///
/// Requires a synthetic child *and* grandchild to match so a glob that only
/// hits one path shape does not prune the rest. Same matcher as
/// [`apply_exclude_globs`]; no extra exclude syntax.
#[cfg(any(feature = "cli", feature = "files"))]
fn should_prune_excluded_directory(dir: &Path, root: Option<&Path>, matcher: &GlobSet) -> bool {
    const SENTINEL: &str = "__walk_prune__";
    let child = dir.join(SENTINEL);
    let nested = child.join(SENTINEL);
    exclude_path_matches(&child, root, matcher) && exclude_path_matches(&nested, root, matcher)
}

/// Skip `.git` / `.patchloom` and directories fully covered by exclude globs.
#[cfg(any(feature = "cli", feature = "files"))]
fn attach_walk_entry_filter(
    builder: &mut WalkBuilder,
    exclude: Option<GlobSet>,
    root: Option<PathBuf>,
) {
    builder.filter_entry(move |e| {
        if should_skip_walk_dirname(e.file_name()) {
            return false;
        }
        if let Some(ref matcher) = exclude
            && e.file_type().is_some_and(|ft| ft.is_dir())
            && should_prune_excluded_directory(e.path(), root.as_deref(), matcher)
        {
            return false;
        }
        true
    });
}

/// Directory basenames we never descend into when walking the tree.
///
/// Applied even with `include_hidden=true` (tidy needs dotfiles but not VCS
/// object stores or Patchloom backup sessions). Shared by CLI/library collectors
/// and plan/tx `ast.rename` directory walks.
#[cfg(any(feature = "cli", feature = "files"))]
pub(crate) fn should_skip_walk_dirname(name: &std::ffi::OsStr) -> bool {
    name == ".git" || name == ".patchloom"
}

/// Parallel file listing from a configured [`WalkBuilder`].
///
/// Same engine as CLI [`collect_file_paths_opts`] (`build_parallel` + batched
/// merge). Callers still apply exclude globs / explicit-file keepers.
#[cfg(any(feature = "cli", feature = "files"))]
fn collect_files_from_walk_builder(builder: WalkBuilder) -> Vec<PathBuf> {
    let collected: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());

    // Flush-on-drop so a thread-local batch merges when the worker is dropped.
    struct FlushOnDrop<'a> {
        batch: Vec<PathBuf>,
        target: &'a Mutex<Vec<PathBuf>>,
    }
    impl Drop for FlushOnDrop<'_> {
        fn drop(&mut self) {
            if !self.batch.is_empty() {
                self.target
                    .lock()
                    .expect("file list mutex")
                    .append(&mut self.batch);
            }
        }
    }

    builder.build_parallel().run(|| {
        let mut state = FlushOnDrop {
            batch: Vec::with_capacity(256),
            target: &collected,
        };
        Box::new(move |result| {
            let Ok(entry) = result else {
                return WalkState::Continue;
            };
            if should_skip_walk_dirname(entry.file_name()) {
                return WalkState::Skip;
            }
            if entry.file_type().is_some_and(|ft| ft.is_file()) {
                state.batch.push(entry.into_path());
                if state.batch.len() >= 256 {
                    state
                        .target
                        .lock()
                        .expect("file list mutex")
                        .append(&mut state.batch);
                }
            }
            WalkState::Continue
        })
    });
    collected.into_inner().expect("all walkers done")
}

/// Collect files while respecting .gitignore + custom ignore files (e.g. .agentignore)
/// + additional exclude globs.
///
/// This is the reusable primitive for library consumers
/// who want the same precedence as `api::search_directory` (#813).
#[cfg(any(feature = "cli", feature = "files"))]
pub fn collect_file_paths_with_ignores(
    root: &Path,
    custom_ignore_filenames: &[String],
    exclude_patterns: &[String],
    include_hidden: bool,
) -> anyhow::Result<Vec<PathBuf>> {
    let mut builder = WalkBuilder::new(root);
    apply_platform_ignore_case(&mut builder);
    if include_hidden {
        builder.hidden(false);
    }
    // Do not enter .git / .patchloom; prune exclude prefixes before descending.
    let exclude_set = build_glob_matcher(exclude_patterns)?;
    attach_walk_entry_filter(&mut builder, exclude_set.clone(), Some(root.to_path_buf()));
    for name in custom_ignore_filenames {
        builder.add_custom_ignore_filename(name);
    }
    let mut paths = collect_files_from_walk_builder(builder);
    apply_exclude_globs(&mut paths, exclude_patterns, Some(root))?;
    Ok(paths)
}

/// Process file paths using adaptive parallelism via `std::thread::scope`.
///
/// Workers claim the next path from a shared atomic cursor instead of a
/// static file-count slice, so a thread that finishes small files picks up
/// remaining large ones. Each result is stored at its input index so output
/// order matches walk order. The calling thread claims work immediately
/// rather than only joining.
///
/// Thread creation cost is ~0.05ms per thread (vs ~2ms for rayon's
/// global thread pool init), so overhead is near-zero even for small
/// workloads. For large workloads, all cores run concurrently.
#[cfg(any(feature = "cli", feature = "files"))]
pub fn par_process_files<T, F>(
    paths: &[PathBuf],
    glob_matcher: Option<&GlobSet>,
    glob_roots: &[PathBuf],
    f: F,
) -> Vec<T>
where
    T: Send,
    F: Fn(&Path) -> Option<T> + Sync,
{
    fn process_slice<T, F>(
        paths: &[PathBuf],
        glob_matcher: Option<&GlobSet>,
        glob_roots: &[PathBuf],
        f: &F,
    ) -> Vec<T>
    where
        T: Send,
        F: Fn(&Path) -> Option<T> + Sync,
    {
        paths
            .iter()
            .filter(|p| matches_glob_with_roots(p, glob_matcher, glob_roots))
            .filter_map(|p| f(p.as_path()))
            .collect()
    }

    fn apply_at<T, F>(
        paths: &[PathBuf],
        i: usize,
        glob_matcher: Option<&GlobSet>,
        glob_roots: &[PathBuf],
        f: &F,
        out: &mut Vec<(usize, T)>,
    ) where
        T: Send,
        F: Fn(&Path) -> Option<T> + Sync,
    {
        let p = &paths[i];
        if matches_glob_with_roots(p, glob_matcher, glob_roots)
            && let Some(v) = f(p.as_path())
        {
            out.push((i, v));
        }
    }

    fn claim_one<T, F>(
        paths: &[PathBuf],
        glob_matcher: Option<&GlobSet>,
        glob_roots: &[PathBuf],
        f: &F,
        next: &AtomicUsize,
        out: &mut Vec<(usize, T)>,
    ) -> bool
    where
        T: Send,
        F: Fn(&Path) -> Option<T> + Sync,
    {
        let i = next.fetch_add(1, Ordering::Relaxed);
        if i >= paths.len() {
            return false;
        }
        apply_at(paths, i, glob_matcher, glob_roots, f, out);
        true
    }

    fn claim_work<T, F>(
        paths: &[PathBuf],
        glob_matcher: Option<&GlobSet>,
        glob_roots: &[PathBuf],
        f: &F,
        next: &AtomicUsize,
        out: &mut Vec<(usize, T)>,
    ) where
        T: Send,
        F: Fn(&Path) -> Option<T> + Sync,
    {
        while claim_one(paths, glob_matcher, glob_roots, f, next, out) {}
    }

    let num_splits = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1)
        .min(paths.len());

    if num_splits <= 1 {
        return process_slice(paths, glob_matcher, glob_roots, &f);
    }

    let next = AtomicUsize::new(0);
    // Reserve one index so this thread is never join-only. Do not run `f`
    // until workers exist, or a large first file keeps every other core idle.
    let reserved = next.fetch_add(1, Ordering::Relaxed);

    std::thread::scope(|s| {
        let handles: Vec<_> = (1..num_splits)
            .map(|_| {
                s.spawn(|| {
                    let mut out = Vec::new();
                    claim_work(paths, glob_matcher, glob_roots, &f, &next, &mut out);
                    out
                })
            })
            .collect();

        let mut mine = Vec::new();
        apply_at(paths, reserved, glob_matcher, glob_roots, &f, &mut mine);
        claim_work(paths, glob_matcher, glob_roots, &f, &next, &mut mine);

        // Merge by original index. Indices are unique (`fetch_add`).
        // Sort the hit list, not a `paths.len()` slot vec (#2409).
        let mut merged = mine;
        // This `expect` is not a recovery path: release builds set
        // `panic = "abort"` (`Cargo.toml`), so a panicking worker aborts the
        // process and never returns a `join` error here. It documents the
        // invariant and stays reachable under the unwinding dev profile used
        // by `cargo test`. See #184 for why the signature is not `Result`, and
        // #2379 for the decision to keep `abort`.
        for handle in handles {
            merged.extend(handle.join().expect("worker thread panicked"));
        }
        merged.sort_unstable_by_key(|(i, _)| *i);
        merged.into_iter().map(|(_, v)| v).collect()
    })
}

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

    // ── has_regex_metacharacters ──────────────────────────────────────

    #[test]
    #[cfg(feature = "cli")]
    fn plain_text_has_no_regex_meta() {
        assert!(!has_regex_metacharacters("hello world"));
        assert!(!has_regex_metacharacters("foo-bar_baz"));
    }

    #[test]
    #[cfg(feature = "cli")]
    fn regex_patterns_detected() {
        assert!(has_regex_metacharacters("fn\\s+main"));
        assert!(has_regex_metacharacters("v1\\.0"));
        assert!(has_regex_metacharacters("[a-z]+"));
        assert!(has_regex_metacharacters("(group)"));
        assert!(has_regex_metacharacters("a|b"));
        assert!(has_regex_metacharacters("^start"));
        assert!(has_regex_metacharacters("end$"));
    }

    // ── ensure_files_from_nonempty (#1796) ─────────────────────────────

    #[test]
    #[cfg(feature = "cli")]
    fn ensure_files_from_nonempty_rejects_empty_list() {
        let global = GlobalFlags {
            files_from: Some("list.txt".into()),
            ..GlobalFlags::default()
        };
        let err = ensure_files_from_nonempty(&global, &[]).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("empty --files-from"),
            "expected empty-list message: {msg}"
        );
        assert_eq!(
            crate::exit::classify_typed_error(&err).map(|(kind, _)| kind),
            Some("invalid_input")
        );
    }

    #[test]
    #[cfg(feature = "cli")]
    fn ensure_files_from_nonempty_ok_when_paths_present() {
        let global = GlobalFlags {
            files_from: Some("list.txt".into()),
            ..GlobalFlags::default()
        };
        ensure_files_from_nonempty(&global, &[PathBuf::from("a.txt")]).unwrap();
    }

    #[test]
    #[cfg(feature = "cli")]
    fn ensure_files_from_nonempty_skips_when_unset() {
        // Directory walk with zero matching files is not invalid_input.
        let global = GlobalFlags::default();
        ensure_files_from_nonempty(&global, &[]).unwrap();
    }

    // ── is_binary ─────────────────────────────────────────────────────

    #[test]
    fn text_is_not_binary() {
        assert!(!is_binary(b"hello world\n"));
    }

    #[test]
    fn empty_is_not_binary() {
        assert!(!is_binary(b""));
    }

    #[test]
    fn nul_byte_makes_binary() {
        assert!(is_binary(b"hello\x00world"));
    }

    #[test]
    fn nul_at_8k_boundary_is_binary() {
        let mut data = vec![b'a'; 8191];
        data.push(0);
        assert!(is_binary(&data));
    }

    #[test]
    fn nul_past_8k_is_not_binary() {
        let mut data = vec![b'a'; 8192];
        data.push(0);
        assert!(!is_binary(&data));
    }

    // ── is_binary_file ────────────────────────────────────────────────

    #[test]
    fn is_binary_file_detects_nul_in_real_file() {
        let dir = tempfile::TempDir::new().unwrap();
        let p = dir.path().join("bin.dat");
        std::fs::write(&p, b"hello\x00world").unwrap();
        assert!(is_binary_file(&p));
    }

    #[test]
    fn is_binary_file_returns_false_for_text_and_nonexistent() {
        let dir = tempfile::TempDir::new().unwrap();
        let p = dir.path().join("text.txt");
        std::fs::write(&p, b"hello world\n").unwrap();
        assert!(!is_binary_file(&p));
        assert!(!is_binary_file(&dir.path().join("nope.bin"))); // open fails -> false
    }

    #[test]
    fn short_reads_still_classify_nul_in_probe_window_as_binary() {
        let mut data = vec![b'a'; 10_000];
        data[6000] = 0;
        let mut reader = ShortRead {
            inner: std::io::Cursor::new(data),
            max_chunk: 100,
        };
        let mut buf = Vec::new();
        read_binary_probe_into(&mut reader, &mut buf).unwrap();
        assert_eq!(buf.len(), BINARY_PROBE_LEN);
        assert!(is_binary(&buf));
    }

    #[test]
    fn short_read_probe_stops_at_eof_before_window() {
        let data = vec![b'a'; 200];
        let mut reader = ShortRead {
            inner: std::io::Cursor::new(data),
            max_chunk: 50,
        };
        let mut buf = Vec::new();
        read_binary_probe_into(&mut reader, &mut buf).unwrap();
        assert_eq!(buf.len(), 200);
        assert!(!is_binary(&buf));
    }

    #[test]
    fn short_read_probe_leaves_remainder_on_same_vec() {
        let mut data = vec![b'x'; 10_000];
        data[6000] = b'y';
        let mut reader = ShortRead {
            inner: std::io::Cursor::new(data.clone()),
            max_chunk: 100,
        };
        let mut buf = Vec::with_capacity(10_000);
        read_binary_probe_into(&mut reader, &mut buf).unwrap();
        assert_eq!(buf.len(), BINARY_PROBE_LEN);
        std::io::Read::read_to_end(&mut reader, &mut buf).unwrap();
        assert_eq!(buf, data);
    }

    /// Public binary probe must not open FIFOs (blocks forever).
    #[cfg(unix)]
    #[test]
    fn is_binary_file_fifo_no_hang() {
        use std::time::Instant;
        let dir = tempfile::TempDir::new().unwrap();
        let fifo = dir.path().join("p.fifo");
        std::process::Command::new("mkfifo")
            .arg(&fifo)
            .status()
            .expect("mkfifo");
        let start = Instant::now();
        assert!(!is_binary_file(&fifo));
        assert!(
            start.elapsed().as_secs() < 2,
            "is_binary_file on FIFO took {:?}",
            start.elapsed()
        );
    }

    // ── Text I/O honesty (#1894) ──────────────────────────────────────

    #[test]
    fn classify_text_bytes_empty_is_text() {
        assert_eq!(classify_text_bytes(b""), TextBytesKind::Text(String::new()));
    }

    #[test]
    fn classify_text_bytes_utf8_text() {
        assert_eq!(
            classify_text_bytes(b"hello\n"),
            TextBytesKind::Text("hello\n".into())
        );
    }

    #[test]
    fn classify_text_bytes_binary_nul() {
        assert_eq!(
            classify_text_bytes(b"hello\x00world"),
            TextBytesKind::Binary
        );
    }

    #[test]
    fn classify_text_bytes_invalid_utf8() {
        assert_eq!(
            classify_text_bytes(b"hello \xff world"),
            TextBytesKind::InvalidUtf8
        );
    }

    #[test]
    fn classify_text_bytes_owned_matches_borrowed() {
        let cases: &[&[u8]] = &[b"", b"hello\n", b"hello\x00world", b"hello \xff world"];
        for bytes in cases {
            assert_eq!(
                classify_text_bytes(bytes),
                classify_text_bytes_owned(bytes.to_vec()),
                "borrowed vs owned disagree on {bytes:?}"
            );
        }
    }

    #[test]
    fn classify_text_bytes_owned_text_keeps_payload() {
        let s = "a".repeat(10_000);
        match classify_text_bytes_owned(s.clone().into_bytes()) {
            TextBytesKind::Text(got) => assert_eq!(got, s),
            other => panic!("expected Text, got {other:?}"),
        }
    }

    #[test]
    fn load_text_strict_ok_for_text() {
        let dir = tempfile::TempDir::new().unwrap();
        let p = dir.path().join("t.txt");
        std::fs::write(&p, "line\n").unwrap();
        assert_eq!(load_text_strict(&p, "t.txt").unwrap(), "line\n");
    }

    #[cfg(windows)]
    #[test]
    fn load_text_strict_refuses_dot_con_device() {
        let err = load_text_strict(std::path::Path::new(r"\\.\CON"), r"\\.\CON").unwrap_err();
        assert!(crate::exit::is_invalid_input(&err), "{err:#}");
        assert!(err.to_string().contains("not a file name"), "msg: {err}");
    }

    #[cfg(windows)]
    #[test]
    fn try_read_text_file_skips_dot_con_device() {
        assert_eq!(
            try_read_text_file(std::path::Path::new(r"\\.\CON")),
            Err(SoftTextSkip::NotRegularFile)
        );
    }

    #[test]
    fn load_text_strict_rejects_binary() {
        let dir = tempfile::TempDir::new().unwrap();
        let p = dir.path().join("b.bin");
        std::fs::write(&p, b"hello\x00world").unwrap();
        let err = load_text_strict(&p, "b.bin").unwrap_err();
        assert!(crate::exit::is_binary(&err), "{err:#}");
        assert!(!crate::exit::is_invalid_input(&err), "{err:#}");
        assert!(err.to_string().contains("binary file"), "msg: {err}");
        assert_eq!(
            crate::fallback::edit_error_kind(&err),
            Some(crate::fallback::EditErrorKind::Binary)
        );
        assert_eq!(crate::fallback::error_kind_str(&err), Some("binary"));
        assert_eq!(std::fs::read(&p).unwrap(), b"hello\x00world");
    }

    #[test]
    fn load_text_strict_rejects_invalid_utf8() {
        let dir = tempfile::TempDir::new().unwrap();
        let p = dir.path().join("bad.txt");
        std::fs::write(&p, b"hello \xff world").unwrap();
        let err = load_text_strict(&p, "bad.txt").unwrap_err();
        assert!(crate::exit::is_invalid_encoding(&err), "{err:#}");
        assert!(!crate::exit::is_invalid_input(&err), "{err:#}");
        assert!(err.to_string().contains("UTF-8"), "msg: {err}");
        assert_eq!(
            crate::fallback::edit_error_kind(&err),
            Some(crate::fallback::EditErrorKind::InvalidEncoding)
        );
        assert_eq!(
            crate::fallback::error_kind_str(&err),
            Some("invalid_encoding")
        );
    }

    #[test]
    fn load_text_strict_rejects_directory() {
        let dir = tempfile::TempDir::new().unwrap();
        let err = load_text_strict(dir.path(), "dir").unwrap_err();
        assert!(crate::exit::is_invalid_input(&err), "{err:#}");
        assert!(err.to_string().contains("not a file"), "msg: {err}");
    }

    #[test]
    #[cfg(unix)]
    fn load_text_strict_unreadable_is_invalid_input_with_os_detail() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::TempDir::new().unwrap();
        let p = dir.path().join("locked.txt");
        std::fs::write(&p, "secret\n").unwrap();
        std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o000)).unwrap();
        // Root (common in Docker) can still read mode-000 files.
        if std::fs::read_to_string(&p).is_ok() {
            std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o644)).unwrap();
            return;
        }
        let err = load_text_strict(&p, "locked.txt").unwrap_err();
        let _ = std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o644));
        assert!(crate::exit::is_invalid_input(&err), "{err:#}");
        // Display (not only {:#}) must include OS detail for agent JSON paths
        // that use e.to_string().
        let msg = err.to_string();
        assert!(msg.contains("failed to read locked.txt"), "msg: {msg}");
        assert!(
            msg.contains("Permission denied")
                || msg.contains("PermissionDenied")
                || msg.contains("os error"),
            "OS detail missing from Display: {msg}"
        );
        assert_eq!(
            msg.matches("failed to read").count(),
            1,
            "must not double-wrap: {msg}"
        );
    }

    /// Create a file symlink; return false when the OS refuses (Windows
    /// without Developer Mode / admin).
    fn try_symlink_file(target: &std::path::Path, link: &std::path::Path) -> bool {
        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(target, link).is_ok()
        }
        #[cfg(windows)]
        {
            match std::os::windows::fs::symlink_file(target, link) {
                Ok(()) => true,
                Err(e) => {
                    eprintln!("skip file symlink test: {e}");
                    false
                }
            }
        }
        #[cfg(not(any(unix, windows)))]
        {
            let _ = (target, link);
            false
        }
    }

    #[test]
    fn load_text_strict_dangling_symlink_is_invalid_input() {
        let dir = tempfile::TempDir::new().unwrap();
        let link = dir.path().join("dangling.txt");
        if !try_symlink_file(&dir.path().join("missing-target"), &link) {
            return;
        }
        let err = load_text_strict(&link, "dangling.txt").unwrap_err();
        assert!(
            crate::exit::is_invalid_input(&err),
            "dangling must be InvalidInputError, got: {err:#}"
        );
        assert!(
            !crate::exit::is_io_not_found(&err),
            "dangling must not look missing: {err:#}"
        );
        assert!(err.to_string().contains("not a file"), "msg: {err}");
    }

    #[test]
    fn load_text_strict_live_symlink_to_text_ok() {
        let dir = tempfile::TempDir::new().unwrap();
        let target = dir.path().join("real.txt");
        std::fs::write(&target, "hello via link\n").unwrap();
        let link = dir.path().join("live.txt");
        if !try_symlink_file(&target, &link) {
            return;
        }
        assert_eq!(
            load_text_strict(&link, "live.txt").unwrap(),
            "hello via link\n"
        );
    }

    #[cfg(windows)]
    #[test]
    fn load_text_strict_collapses_trailing_separators() {
        let dir = tempfile::TempDir::new().unwrap();
        let file = dir.path().join("keep.txt");
        std::fs::write(&file, "KEEP\n").unwrap();
        let slashed = std::path::PathBuf::from(format!("{}\\", file.display()));
        assert_eq!(load_text_strict(&slashed, r"keep.txt\\").unwrap(), "KEEP\n");
        assert_eq!(try_read_text_file(&slashed).unwrap(), "KEEP\n");
    }

    #[test]
    fn load_text_strict_missing_is_io_not_found() {
        let dir = tempfile::TempDir::new().unwrap();
        let p = dir.path().join("nope.txt");
        let err = load_text_strict(&p, "nope.txt").unwrap_err();
        assert!(
            crate::exit::is_io_not_found(&err),
            "expected is_io_not_found, got: {err:#}"
        );
        assert!(
            !crate::exit::is_invalid_input(&err),
            "NotFound must not be invalid_input: {err:#}"
        );
        // Display must include OS detail (not only `{:#}`).
        let msg = err.to_string();
        assert!(msg.contains("failed to read nope.txt"), "msg: {msg}");
        assert!(
            msg.contains("No such file") || msg.contains("os error 2") || msg.contains("not found"),
            "OS detail missing from Display: {msg}"
        );
        assert_eq!(
            msg.matches("failed to read").count(),
            1,
            "must not double-wrap: {msg}"
        );
        // Agent JSON / {:#} consumers must not double the OS detail either.
        let agent = crate::exit::agent_error_message(&err);
        let os_hits = agent
            .matches("No such file")
            .count()
            .max(agent.matches("os error 2").count())
            .max(agent.matches("not found").count());
        assert_eq!(
            os_hits, 1,
            "agent_error_message must keep OS detail once: {agent}"
        );
    }

    #[test]
    #[cfg(feature = "cli")]
    fn all_explicit_paths_missing_detects_typos() {
        let dir = tempfile::TempDir::new().unwrap();
        let missing = vec!["nope.txt".to_string()];
        assert!(all_explicit_paths_missing(&missing, Some(dir.path())));
        assert!(!all_explicit_paths_missing(&[], Some(dir.path())));
        std::fs::write(dir.path().join("exists.txt"), b"x\n").unwrap();
        let mixed = vec!["exists.txt".to_string(), "nope.txt".to_string()];
        assert!(!all_explicit_paths_missing(&mixed, Some(dir.path())));
    }

    #[test]
    fn looks_like_glob_dest_star_and_question() {
        assert!(looks_like_glob_dest("*.txt"));
        assert!(looks_like_glob_dest("sub/*.rs"));
        assert!(looks_like_glob_dest(r"sub\*.rs"));
        assert!(looks_like_glob_dest("file?.txt"));
        assert!(looks_like_glob_dest(r"\\?\C:\temp\*.txt"));
        assert!(looks_like_glob_dest("//?/C:/temp/*.txt"));
        assert!(!looks_like_glob_dest("keep.txt"));
        assert!(!looks_like_glob_dest("sub/keep.txt"));
        assert!(!looks_like_glob_dest(r"\\?\C:\temp\keep.txt"));
        assert!(!looks_like_glob_dest("//?/C:/temp/keep.txt"));
        assert!(!looks_like_glob_dest(r"\\.\C:\temp\keep.txt"));
        assert!(!looks_like_glob_dest("//./C:/temp/keep.txt"));
    }

    #[cfg(feature = "cli")]
    fn dest_glob_set(pattern: &str) -> GlobSet {
        let mut builder = GlobSetBuilder::new();
        builder.add(compile_dest_glob(pattern).expect("valid dest glob"));
        builder.build().expect("globset")
    }

    #[cfg(feature = "cli")]
    fn dest_glob_root() -> PathBuf {
        if cfg!(windows) {
            PathBuf::from(r"C:\pl-dest-glob")
        } else {
            PathBuf::from("/tmp/pl-dest-glob")
        }
    }

    #[test]
    #[cfg(feature = "cli")]
    fn dest_glob_matcher_star_star_matches_nested_star_does_not() {
        let root = dest_glob_root();
        let nested = root.join("sub").join("a.txt");
        let roots = [root];
        let rec = dest_glob_set("**/*.txt");
        let star = dest_glob_set("*.txt");
        assert!(
            matches_dest_glob(&nested, &rec, &roots),
            "**/*.txt must match sub/a.txt"
        );
        assert!(
            !matches_dest_glob(&nested, &star, &roots),
            "*.txt must not match sub/a.txt"
        );
    }

    #[test]
    #[cfg(feature = "cli")]
    fn compile_dest_glob_and_matches_dest_glob_edges() {
        let root = dest_glob_root();
        let cwd = root.join("keep.txt");
        let nested = root.join("sub").join("a.txt");
        let roots = [root.clone()];

        let star = dest_glob_set("*.txt");
        assert!(matches_dest_glob(&cwd, &star, &roots));
        assert!(!matches_dest_glob(&nested, &star, &roots));

        let rec = dest_glob_set("**/*.txt");
        assert!(matches_dest_glob(&cwd, &rec, &roots));
        assert!(matches_dest_glob(&nested, &rec, &roots));

        let q = dest_glob_set("file?.txt");
        assert!(matches_dest_glob(&root.join("fileA.txt"), &q, &roots));
        assert!(!matches_dest_glob(&root.join("fileAB.txt"), &q, &roots));

        assert!(compile_dest_glob("*[").is_err());
        let dest_err = compile_dest_glob("*[").unwrap_err();
        assert!(
            crate::exit::is_invalid_input(&dest_err),
            "dest-glob compile must be InvalidInputError, got: {dest_err:#}"
        );

        #[cfg(windows)]
        {
            let win_slash = dest_glob_set(r"sub\*.txt");
            assert!(matches_dest_glob(&nested, &win_slash, &roots));
            assert!(!matches_dest_glob(&cwd, &win_slash, &roots));
            assert!(matches_dest_glob(&root.join("Hit.TXT"), &star, &roots));
        }
    }

    #[test]
    #[cfg(feature = "cli")]
    fn dest_glob_cwd_only_rule_and_skip_case_tip() {
        let cwd = vec!["*.txt".to_string()];
        let rec = vec!["**/*.txt".to_string()];
        let literal = vec!["keep.txt".to_string()];
        let rule = dest_glob_cwd_only_rule(&cwd).expect("cwd dest-glob rule");
        assert!(
            rule.contains("dest `*.txt` matches files in the current directory only"),
            "rule must name dest-subject cwd-only: {rule}"
        );
        assert!(
            rule.contains("--glob"),
            "rule must name --glob as nested remedy: {rule}"
        );
        assert!(dest_glob_cwd_only_rule(&rec).is_none());
        assert!(dest_glob_cwd_only_rule(&literal).is_none());
        let with_rule = with_dest_glob_cwd_only_rule("no matches for 'KEEP' in *.txt", &cwd);
        assert!(
            with_rule.contains("dest `*.txt` matches files in the current directory only")
                && with_rule.contains("--glob"),
            "appended rule must name dest-subject cwd-only and --glob nested: {with_rule}"
        );
        assert!(dest_glob_skip_case_tip(&cwd, true));
        assert!(dest_glob_skip_case_tip(&cwd, false));
        assert!(dest_glob_skip_case_tip(&rec, true));
        assert!(!dest_glob_skip_case_tip(&rec, false));
        assert!(!dest_glob_skip_case_tip(&literal, true));
        assert!(!dest_glob_skip_case_tip(&literal, false));

        let sub = vec!["sub/*.txt".to_string()];
        let sub_rule = dest_glob_cwd_only_rule(&sub).expect("subdir dest-glob rule");
        assert!(
            sub_rule.contains("dest `sub/*.txt`")
                && sub_rule.contains("that directory only")
                && !sub_rule.contains("current directory only")
                && sub_rule.contains("--glob")
                && sub_rule.contains("**/*.txt"),
            "subdir dest-glob rule must name dest-subject that-directory-only and **/*.txt: {sub_rule}"
        );
        let with_sub = with_dest_glob_cwd_only_rule("no matches for 'KEEP' in sub/*.txt", &sub);
        assert!(
            with_sub.contains("dest `sub/*.txt`")
                && with_sub.contains("that directory only")
                && !with_sub.contains("current directory only")
                && with_sub.contains("--glob")
                && with_sub.contains("**/*.txt"),
            "appended subdir rule must name dest-subject that-directory-only: {with_sub}"
        );
        let rs = dest_glob_cwd_only_rule(&["src/*.rs".to_string()]).expect("src dest-glob rule");
        assert!(
            rs.contains("**/*.rs") && rs.contains("--glob '*.rs'") && !rs.contains("*.txt"),
            "src/*.rs remedy must name **/*.rs / --glob '*.rs', not *.txt: {rs}"
        );
        assert!(!dest_glob_skip_case_tip(&["sub/*.txt".to_string()], false));
        assert!(dest_glob_skip_case_tip(&["sub/*.txt".to_string()], true));
    }

    #[test]
    #[cfg(feature = "cli")]
    fn dest_glob_walk_max_depth_cwd_vs_recursive() {
        assert_eq!(dest_glob_walk_max_depth(&["*.txt".into()]), Some(1));
        assert_eq!(dest_glob_walk_max_depth(&["./*.txt".into()]), Some(1));
        assert_eq!(dest_glob_walk_max_depth(&[r".\*.txt".into()]), Some(1));
        assert_eq!(dest_glob_walk_max_depth(&["sub/*.txt".into()]), Some(2));
        assert_eq!(dest_glob_walk_max_depth(&["a/b/*.txt".into()]), Some(3));
        assert_eq!(
            dest_glob_walk_max_depth(&["*.txt".into(), "sub/*.txt".into()]),
            Some(2)
        );
        assert_eq!(dest_glob_walk_max_depth(&["**/*.txt".into()]), None);
        assert_eq!(
            dest_glob_walk_max_depth(&["*.txt".into(), "**/*.md".into()]),
            None
        );
        assert_eq!(dest_glob_walk_max_depth(&[]), None);
        #[cfg(windows)]
        {
            assert_eq!(dest_glob_walk_max_depth(&[r"sub\*.txt".into()]), Some(2));
            assert_eq!(dest_glob_walk_max_depth(&[r"**\*.txt".into()]), None);
        }
    }

    #[test]
    #[cfg(feature = "cli")]
    fn dest_glob_mix_literal_root_stays_recursive() {
        use std::fs;
        let dir = tempfile::TempDir::new().unwrap();
        let root = dir.path();
        fs::create_dir_all(root.join("src/deep")).unwrap();
        fs::create_dir_all(root.join("other")).unwrap();
        fs::write(root.join("keep.txt"), "k\n").unwrap();
        fs::write(root.join("src/deep/in_src.txt"), "s\n").unwrap();
        fs::write(root.join("other/nested.txt"), "o\n").unwrap();
        let global = GlobalFlags::test_with_cwd(root);
        let paths =
            collect_file_paths_opts(&["src".into(), "*.txt".into()], &global, false, Some(root))
                .unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| {
                p.strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect();
        assert!(
            rels.iter().any(|r| r == "keep.txt"),
            "cwd dest *.txt: {rels:?}"
        );
        assert!(
            rels.iter().any(|r| r.ends_with("in_src.txt")),
            "literal src must stay recursive: {rels:?}"
        );
        assert!(
            !rels.iter().any(|r| r.contains("other")),
            "dest *.txt must not pick other/: {rels:?}"
        );
    }

    #[test]
    #[cfg(feature = "cli")]
    fn all_explicit_paths_missing_ignores_glob_dests() {
        let dir = tempfile::TempDir::new().unwrap();
        assert!(
            !all_explicit_paths_missing(&["*.txt".into()], Some(dir.path())),
            "glob dests are not dest not_found"
        );
        std::fs::write(dir.path().join("keep.txt"), "KEEP\n").unwrap();
        assert!(!all_explicit_paths_missing(
            &["keep.txt".into(), "*.md".into()],
            Some(dir.path())
        ));
    }

    #[cfg(all(windows, feature = "cli"))]
    #[test]
    fn all_explicit_paths_missing_collapses_trailing_separators() {
        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join("keep.txt"), "KEEP\n").unwrap();
        let slashed = vec![r"keep.txt\\".to_string()];
        assert!(
            !all_explicit_paths_missing(&slashed, Some(dir.path())),
            "Win32 keep.txt\\ must not peel not_found"
        );
    }

    // ── matches_glob ──────────────────────────────────────────────────
    // These require the glob/walker APIs which are behind "cli" or "files" feature.

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn no_matcher_matches_everything() {
        assert!(matches_glob(Path::new("any/file.rs"), None));
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn glob_matches_extension() {
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("*.rs").unwrap());
        let matcher = builder.build().unwrap();
        assert!(matches_glob(Path::new("src/main.rs"), Some(&matcher)));
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn glob_rejects_non_matching() {
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("*.rs").unwrap());
        let matcher = builder.build().unwrap();
        assert!(!matches_glob(Path::new("src/main.py"), Some(&matcher)));
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn glob_matches_nested_relative_pattern_with_root() {
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("sub/*.txt").unwrap());
        let matcher = builder.build().unwrap();
        let roots = vec![PathBuf::from("/tmp/project")];

        assert!(matches_glob_with_roots(
            Path::new("/tmp/project/sub/file.txt"),
            Some(&matcher),
            &roots,
        ));
        assert!(!matches_glob_with_roots(
            Path::new("/tmp/project/other.txt"),
            Some(&matcher),
            &roots,
        ));
    }

    #[test]
    #[cfg(feature = "cli")]
    fn collect_glob_roots_normalizes_current_directory_segments() {
        let global = GlobalFlags::test_default();
        let roots =
            collect_glob_roots_from_global(&[], &global, Some(Path::new("/tmp/project"))).unwrap();

        assert_eq!(roots, vec![PathBuf::from("/tmp/project")]);
    }

    // ── par_process_files ─────────────────────────────────────────────
    // These require the glob/walker APIs which are behind "cli" or "files" feature.

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_single_file() {
        let paths = vec![PathBuf::from("a.txt")];
        let results = par_process_files(&paths, None, &[], |p| {
            Some(p.to_string_lossy().into_owned())
        });
        assert_eq!(results, vec!["a.txt"]);
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_filters_with_glob() {
        let paths = vec![
            PathBuf::from("a.rs"),
            PathBuf::from("b.py"),
            PathBuf::from("c.rs"),
        ];
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("*.rs").unwrap());
        let matcher = builder.build().unwrap();
        let results = par_process_files(&paths, Some(&matcher), &[], |p| {
            Some(p.to_string_lossy().into_owned())
        });
        assert_eq!(results, vec!["a.rs".to_string(), "c.rs".to_string()]);
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_filters_with_relative_glob_root() {
        let paths = vec![
            PathBuf::from("/tmp/project/sub/a.txt"),
            PathBuf::from("/tmp/project/other.txt"),
        ];
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("sub/*.txt").unwrap());
        let matcher = builder.build().unwrap();
        let roots = vec![PathBuf::from("/tmp/project")];
        let results = par_process_files(&paths, Some(&matcher), &roots, |p| {
            Some(p.to_string_lossy().into_owned())
        });
        assert_eq!(results, vec!["/tmp/project/sub/a.txt".to_string()]);
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_empty_paths() {
        let paths: Vec<PathBuf> = vec![];
        let results: Vec<String> = par_process_files(&paths, None, &[], |p| {
            Some(p.to_string_lossy().into_owned())
        });
        assert!(results.is_empty());
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_closure_can_filter() {
        let paths = vec![PathBuf::from("a.txt"), PathBuf::from("b.txt")];
        let results = par_process_files(&paths, None, &[], |p| {
            if p.to_string_lossy().contains('a') {
                Some(1)
            } else {
                None
            }
        });
        assert_eq!(results, vec![1]);
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_preserves_walk_order_with_skips() {
        let paths: Vec<PathBuf> = (0..48).map(|i| PathBuf::from(format!("{i}.txt"))).collect();
        let results = par_process_files(&paths, None, &[], |p| {
            let n = p
                .file_stem()
                .and_then(|s| s.to_str())
                .and_then(|s| s.parse::<usize>().ok())?;
            if n % 3 == 0 { None } else { Some(n) }
        });
        let expected: Vec<usize> = (0..48).filter(|n| n % 3 != 0).collect();
        assert_eq!(results, expected);
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_preserves_walk_order_skip_heavy() {
        let paths: Vec<PathBuf> = (0..4000)
            .map(|i| PathBuf::from(format!("{i}.txt")))
            .collect();
        let results = par_process_files(&paths, None, &[], |p| {
            let n = p
                .file_stem()
                .and_then(|s| s.to_str())
                .and_then(|s| s.parse::<usize>().ok())?;
            if n % 17 == 0 { None } else { Some(n) }
        });
        let expected: Vec<usize> = (0..4000).filter(|n| n % 17 != 0).collect();
        assert_eq!(results, expected);
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_calling_thread_participates() {
        let n = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1)
            .max(8);
        let paths: Vec<PathBuf> = (0..n).map(|i| PathBuf::from(format!("p{i}"))).collect();
        let caller = std::thread::current().id();
        let seen = Mutex::new(Vec::new());
        let results = par_process_files(&paths, None, &[], |p| {
            seen.lock().unwrap().push(std::thread::current().id());
            Some(p.to_string_lossy().into_owned())
        });
        assert_eq!(results.len(), n);
        assert!(
            seen.lock().unwrap().contains(&caller),
            "calling thread must claim work, not only join"
        );
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn par_process_claims_work_dynamically() {
        let parallelism = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);
        if parallelism <= 1 {
            return;
        }
        let n = parallelism * 8;
        let paths: Vec<PathBuf> = (0..n).map(|i| PathBuf::from(format!("f{i}"))).collect();
        let claimed = Mutex::new(Vec::new());
        let results = par_process_files(&paths, None, &[], |p| {
            let idx = p
                .file_name()
                .and_then(|s| s.to_str())
                .and_then(|s| s.strip_prefix('f'))
                .and_then(|s| s.parse::<usize>().ok())?;
            claimed
                .lock()
                .unwrap()
                .push((std::thread::current().id(), idx));
            if idx % 2 == 0 {
                std::thread::sleep(std::time::Duration::from_millis(2));
            }
            Some(idx)
        });
        assert_eq!(results, (0..n).collect::<Vec<_>>());
        let mut by_thread: std::collections::HashMap<std::thread::ThreadId, Vec<usize>> =
            std::collections::HashMap::new();
        for (tid, idx) in claimed.into_inner().unwrap() {
            by_thread.entry(tid).or_default().push(idx);
        }
        assert!(
            by_thread.len() >= 2,
            "expected at least two threads to claim work, got {by_thread:?}"
        );
        let multi: Vec<&Vec<usize>> = by_thread.values().filter(|idxs| idxs.len() >= 2).collect();
        if multi.is_empty() {
            return;
        }
        let any_noncontiguous = multi.iter().any(|idxs| {
            let mut sorted = (*idxs).clone();
            sorted.sort_unstable();
            sorted.windows(2).any(|w| w[1] > w[0] + 1)
        });
        assert!(
            any_noncontiguous,
            "static file-count slices assign each thread a contiguous range; \
             dynamic claims should interleave under uneven work: {by_thread:?}"
        );
    }

    // ── read_text_file ────────────────────────────────────────────────

    #[test]
    fn read_text_file_returns_content_for_utf8_file() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("hello.txt");
        std::fs::write(&file, "hello world\n").unwrap();
        let result = read_text_file(&file);
        assert_eq!(result.unwrap(), "hello world\n");
    }

    #[test]
    fn read_text_file_returns_none_for_binary() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("binary.bin");
        std::fs::write(&file, b"hello\x00world").unwrap();
        assert!(read_text_file(&file).is_none());
        assert_eq!(try_read_text_file(&file).unwrap_err(), SoftTextSkip::Binary);
    }

    #[test]
    fn try_read_text_file_reasons_utf8_and_missing() {
        let dir = tempfile::tempdir().unwrap();
        let bad = dir.path().join("bad.txt");
        std::fs::write(&bad, b"hello \xff world").unwrap();
        assert_eq!(
            try_read_text_file(&bad).unwrap_err(),
            SoftTextSkip::InvalidUtf8
        );
        assert_eq!(
            try_read_text_file(dir.path().join("missing.txt").as_path()).unwrap_err(),
            SoftTextSkip::Unreadable
        );
        assert!(SoftTextSkip::Binary.is_content_skip());
        assert!(!SoftTextSkip::Unreadable.is_content_skip());
        assert_eq!(SoftTextSkip::InvalidUtf8.as_reason(), "invalid_utf8");
    }

    /// Dangling symlink is a present special entry, not missing/unreadable.
    #[cfg(unix)]
    #[test]
    fn try_read_text_file_dangling_symlink_is_not_regular() {
        let dir = tempfile::tempdir().unwrap();
        let link = dir.path().join("dangling.txt");
        std::os::unix::fs::symlink(dir.path().join("missing-target"), &link).unwrap();
        assert_eq!(
            try_read_text_file(&link).unwrap_err(),
            SoftTextSkip::NotRegularFile
        );
    }

    /// Windows dangling file symlink reports `FileType::is_file()`; soft
    /// read must still skip as NotRegularFile, not Unreadable.
    #[cfg(windows)]
    #[test]
    fn try_read_text_file_dangling_file_symlink_is_not_regular() {
        let dir = tempfile::tempdir().unwrap();
        let link = dir.path().join("dangling.txt");
        if let Err(e) = std::os::windows::fs::symlink_file(dir.path().join("missing-target"), &link)
        {
            // File symlinks need Developer Mode or admin.
            eprintln!("skip dangling file symlink test: {e}");
            return;
        }
        assert_eq!(
            try_read_text_file(&link).unwrap_err(),
            SoftTextSkip::NotRegularFile
        );
    }

    /// Soft text load must not open FIFOs (blocks forever).
    #[cfg(unix)]
    #[test]
    fn try_read_text_file_fifo_no_hang() {
        use std::time::Instant;
        let dir = tempfile::tempdir().unwrap();
        let fifo = dir.path().join("p.fifo");
        std::process::Command::new("mkfifo")
            .arg(&fifo)
            .status()
            .expect("mkfifo");
        let start = Instant::now();
        assert_eq!(
            try_read_text_file(&fifo).unwrap_err(),
            SoftTextSkip::NotRegularFile
        );
        assert_eq!(SoftTextSkip::NotRegularFile.as_reason(), "not_regular_file");
        assert!(
            start.elapsed().as_secs() < 2,
            "try_read_text_file on FIFO took {:?}",
            start.elapsed()
        );
    }

    /// CLI soft-read diagnostic must not re-open FIFOs for OS error text
    /// (hang residual after soft refuse; fixloop 2026-08-02).
    #[cfg(all(unix, feature = "cli"))]
    #[test]
    fn read_text_file_logged_fifo_no_hang() {
        use std::time::Instant;
        let dir = tempfile::tempdir().unwrap();
        let fifo = dir.path().join("p.fifo");
        std::process::Command::new("mkfifo")
            .arg(&fifo)
            .status()
            .expect("mkfifo");
        let start = Instant::now();
        assert!(
            read_text_file_logged(&fifo, "replace", false).is_none(),
            "FIFO must soft-skip"
        );
        assert!(
            start.elapsed().as_secs() < 2,
            "read_text_file_logged on FIFO took {:?}",
            start.elapsed()
        );
    }

    #[test]
    fn read_text_file_returns_empty_string_for_empty_file() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("empty.txt");
        std::fs::write(&file, b"").unwrap();
        let result = read_text_file(&file);
        assert_eq!(result, Some(String::new()));
    }

    #[test]
    fn read_text_file_returns_none_for_invalid_utf8() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("bad.txt");
        std::fs::write(&file, b"hello \xff world\n").unwrap();
        assert!(read_text_file(&file).is_none());
    }

    #[test]
    fn read_text_file_returns_none_for_missing_file() {
        assert!(read_text_file(Path::new("/tmp/patchloom_nonexistent_xyz.txt")).is_none());
    }

    #[test]
    fn read_text_file_large_file_two_phase_read() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("large.txt");
        // Create a text file larger than the 8 KiB binary-check probe.
        let content = "a".repeat(10_000) + "\n";
        std::fs::write(&file, &content).unwrap();
        let result = read_text_file(&file);
        assert_eq!(result.unwrap(), content);
    }

    #[test]
    fn read_text_file_large_binary_rejected_via_header_probe() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("large.bin");
        // Create a binary file larger than the 8 KiB probe with a NUL
        // in the header. The two-phase read should detect the NUL in the
        // first 8 KiB and return None without reading the rest.
        let mut data = vec![b'a'; 10_000];
        data[4096] = 0; // NUL in the first 8 KiB
        std::fs::write(&file, &data).unwrap();
        assert!(read_text_file(&file).is_none());
    }

    #[test]
    fn read_text_file_large_file_invalid_utf8_past_header() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("bad_tail.txt");
        // First 8 KiB is valid ASCII; byte 9000 is invalid UTF-8.
        let mut data = vec![b'a'; 10_000];
        data[9000] = 0xff;
        std::fs::write(&file, &data).unwrap();
        // The two-phase read should detect invalid UTF-8 in the second
        // phase (read_to_end) and return None.
        assert!(read_text_file(&file).is_none());
    }

    #[test]
    fn read_text_file_binary_past_8k_still_read_as_text() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("mostly_text.txt");
        // 8 KiB of text, then a NUL byte and newline. The binary check only
        // inspects the first 8 KiB, so the file is still treated as text.
        let mut data = vec![b'a'; 8192];
        data.push(0);
        data.push(b'\n');
        std::fs::write(&file, &data).unwrap();
        let result = read_text_file(&file).expect("NUL past 8KiB should still read as text");
        assert_eq!(result.len(), 8194);
    }

    // ── collect_file_paths_opts with advanced ignores (for #821) ────────

    #[test]
    #[cfg(feature = "cli")]
    fn collect_file_paths_opts_respects_ignore_file_and_exclude() {
        use crate::cli::global::GlobalFlags;
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        // Create tree
        fs::create_dir_all(root.join("src")).unwrap();
        fs::create_dir_all(root.join("target")).unwrap();
        fs::write(root.join("src/lib.rs"), "pub fn foo() {}\n").unwrap();
        fs::write(root.join("src/main.rs"), "fn main() {}\n").unwrap();
        fs::write(root.join("target/debug"), "binary").unwrap(); // will be excluded by pattern
        fs::write(root.join("README.md"), "# hi\n").unwrap();
        fs::write(root.join("Cargo.toml"), "[package]\n").unwrap(); // should survive .agentignore + exclude
        fs::write(root.join(".agentignore"), "target/\n*.md\n").unwrap();

        let mut global = GlobalFlags::test_default();
        global.cwd = Some(root.to_string_lossy().into_owned());
        global.ignore_file = vec![".agentignore".to_string()];
        global.exclude = vec!["*.rs".to_string()]; // further exclude rs on top

        let paths =
            collect_file_paths_opts(&[".".to_string()], &global, false, Some(root)).unwrap();

        // .agentignore skips target/ and *.md; then exclude *.rs skips the rs files.
        // Only nothing should remain? Wait, adjust: actually with exclude *.rs and ignore md/target, expect empty or adjust expectation.
        // Simpler assertion: the ignore_file was honored (no target, no md), and additional exclude removed rs.
        let rels: Vec<_> = paths
            .iter()
            .map(|p| p.strip_prefix(root).unwrap().to_string_lossy().to_string())
            .collect();
        assert!(
            rels.contains(&"Cargo.toml".to_string()),
            "surviving file missing: {:?}",
            rels
        );
        assert!(
            !rels
                .iter()
                .any(|r| r.starts_with("target") || r.ends_with(".md") || r.ends_with(".rs")),
            "advanced ignores not applied: {:?}",
            rels
        );
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn collect_file_paths_skips_patchloom_directory() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::write(root.join("real.txt"), "hello\n").unwrap();
        fs::create_dir_all(root.join(".patchloom/backups/12345")).unwrap();
        fs::write(root.join(".patchloom/backups/12345/manifest.json"), "{}\n").unwrap();

        let paths = collect_file_paths(root, false).unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| p.strip_prefix(root).unwrap().to_string_lossy().to_string())
            .collect();
        assert!(
            rels.contains(&"real.txt".to_string()),
            "real.txt should be collected: {rels:?}"
        );
        assert!(
            !rels.iter().any(|r| r.contains(".patchloom")),
            ".patchloom files should be excluded: {rels:?}"
        );
    }

    /// #2078: WalkBuilder max_depth must not enter pruned dirs (only top-level files).
    #[test]
    #[cfg(feature = "cli")]
    fn collect_file_paths_opts_depth_prunes_nested() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::write(root.join("top.txt"), "t\n").unwrap();
        fs::create_dir_all(root.join("a/b/c")).unwrap();
        fs::write(root.join("a/mid.txt"), "m\n").unwrap();
        fs::write(root.join("a/b/c/deep.txt"), "d\n").unwrap();

        let global = GlobalFlags::test_with_cwd(root);
        let shallow =
            collect_file_paths_opts_depth(&[".".into()], &global, false, Some(root), Some(1))
                .unwrap();
        let shallow_rels: Vec<_> = shallow
            .iter()
            .map(|p| {
                p.strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect();
        assert!(
            shallow_rels.iter().any(|r| r == "top.txt"),
            "depth 1 includes top: {shallow_rels:?}"
        );
        assert!(
            !shallow_rels
                .iter()
                .any(|r| r.contains("mid") || r.contains("deep")),
            "depth 1 must not enter a/: {shallow_rels:?}"
        );

        let mid = collect_file_paths_opts_depth(&[".".into()], &global, false, Some(root), Some(2))
            .unwrap();
        let mid_rels: Vec<_> = mid
            .iter()
            .map(|p| {
                p.strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect();
        assert!(
            mid_rels.iter().any(|r| r.ends_with("mid.txt")),
            "depth 2 includes a/mid: {mid_rels:?}"
        );
        assert!(
            !mid_rels.iter().any(|r| r.contains("deep")),
            "depth 2 must not reach a/b/c: {mid_rels:?}"
        );
    }

    /// #2078: max_depth is per walk root when multiple roots are collected.
    #[test]
    #[cfg(feature = "cli")]
    fn collect_file_paths_opts_depth_multi_root() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir_all(root.join("left/deep")).unwrap();
        fs::create_dir_all(root.join("right/deep")).unwrap();
        fs::write(root.join("left/l.txt"), "l\n").unwrap();
        fs::write(root.join("left/deep/x.txt"), "x\n").unwrap();
        fs::write(root.join("right/r.txt"), "r\n").unwrap();
        fs::write(root.join("right/deep/y.txt"), "y\n").unwrap();
        let global = GlobalFlags::test_with_cwd(root);
        let paths = collect_file_paths_opts_depth(
            &["left".into(), "right".into()],
            &global,
            false,
            Some(root),
            Some(1),
        )
        .unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| {
                p.strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect();
        assert!(
            rels.iter().any(|r| r.ends_with("l.txt")) && rels.iter().any(|r| r.ends_with("r.txt")),
            "top of each root: {rels:?}"
        );
        assert!(
            !rels.iter().any(|r| r.contains("deep")),
            "deep under each root pruned: {rels:?}"
        );
    }

    #[test]
    #[cfg(feature = "cli")]
    fn collect_file_paths_opts_skips_patchloom_directory() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::write(root.join("real.txt"), "hello\n").unwrap();
        fs::create_dir_all(root.join(".patchloom/backups/12345")).unwrap();
        fs::write(root.join(".patchloom/backups/12345/manifest.json"), "{}\n").unwrap();

        let global = GlobalFlags::test_with_cwd(root);
        let paths = collect_file_paths_opts(&[".".to_string()], &global, true, Some(root)).unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| p.strip_prefix(root).unwrap().to_string_lossy().to_string())
            .collect();
        assert!(
            rels.contains(&"real.txt".to_string()),
            "real.txt should be collected: {rels:?}"
        );
        assert!(
            !rels.iter().any(|r| r.contains(".patchloom")),
            ".patchloom files should be excluded even with include_hidden=true: {rels:?}"
        );
    }

    /// tidy uses include_hidden=true so .env is checked; .git must still be skipped
    /// (otherwise binary objects cause invalid-UTF-8 skip noise on stderr).
    #[test]
    #[cfg(feature = "cli")]
    fn collect_file_paths_opts_skips_git_directory_when_hidden() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::write(root.join("real.txt"), "hello\n").unwrap();
        fs::create_dir_all(root.join(".git/objects/ab")).unwrap();
        // Invalid UTF-8 "object" that would trip the text reader if walked.
        fs::write(root.join(".git/objects/ab/cdef"), [0xffu8, 0xfe, 0x00]).unwrap();
        fs::write(root.join(".env"), "SECRET=1\n").unwrap();

        let global = GlobalFlags::test_with_cwd(root);
        let paths = collect_file_paths_opts(&[".".to_string()], &global, true, Some(root)).unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| p.strip_prefix(root).unwrap().to_string_lossy().to_string())
            .collect();
        assert!(
            rels.iter()
                .any(|r| r == "real.txt" || r.ends_with("real.txt")),
            "real.txt should be collected: {rels:?}"
        );
        assert!(
            rels.iter().any(|r| r == ".env" || r.ends_with(".env")),
            ".env should be collected with include_hidden: {rels:?}"
        );
        assert!(
            !rels.iter().any(|r| r.contains(".git")),
            ".git must not be walked with include_hidden=true: {rels:?}"
        );
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn exclude_glob_matches_files_in_subdirs() {
        // Regression: exclude globs like "*.rs" must match files in
        // subdirectories via filename fallback, same as include globs.
        let mut paths = vec![
            PathBuf::from("src/main.rs"),
            PathBuf::from("src/lib.rs"),
            PathBuf::from("README.md"),
        ];
        apply_exclude_globs(&mut paths, &["*.rs".into()], None).unwrap();
        assert_eq!(
            paths,
            vec![PathBuf::from("README.md")],
            "*.rs should exclude files in subdirs"
        );
    }

    #[test]
    #[cfg(any(feature = "cli", feature = "files"))]
    fn exclude_glob_matches_directory_pattern_with_root() {
        // Regression: patterns like "vendor/**" must match absolute paths
        // when the root prefix is stripped for comparison.
        let root = PathBuf::from("/project");
        let mut paths = vec![
            PathBuf::from("/project/src/main.rs"),
            PathBuf::from("/project/vendor/lib.rs"),
            PathBuf::from("/project/vendor/sub/dep.rs"),
        ];
        apply_exclude_globs(&mut paths, &["vendor/**".into()], Some(&root)).unwrap();
        assert_eq!(
            paths,
            vec![PathBuf::from("/project/src/main.rs")],
            "vendor/** with root should exclude vendor files"
        );
    }
}

#[cfg(all(test, feature = "cli"))]
mod explicit_exclude_tests {
    use super::*;
    use crate::cli::global::GlobalFlags;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn explicit_file_arg_not_dropped_by_exclude_glob() {
        let dir = TempDir::new().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("vendor")).unwrap();
        let file = root.join("vendor/x.js");
        fs::write(&file, "foo\n").unwrap();
        let global = GlobalFlags {
            exclude: vec!["vendor/**".into()],
            ..GlobalFlags::test_default()
        };
        let paths = collect_file_paths_opts_with_list(
            &["vendor/x.js".into()],
            &global,
            false,
            Some(root),
            None,
            None,
        )
        .unwrap();
        assert!(
            paths.iter().any(|p| p.ends_with("x.js")),
            "explicit file must survive exclude: {paths:?}"
        );
    }

    #[test]
    fn directory_root_still_honors_exclude() {
        let dir = TempDir::new().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("vendor")).unwrap();
        fs::write(root.join("vendor/x.js"), "foo\n").unwrap();
        fs::write(root.join("app.js"), "foo\n").unwrap();
        let global = GlobalFlags {
            exclude: vec!["vendor/**".into()],
            ..GlobalFlags::test_default()
        };
        let paths = collect_file_paths_opts_with_list(
            &[".".into()],
            &global,
            false,
            Some(root),
            None,
            None,
        )
        .unwrap();
        assert!(
            paths.iter().any(|p| p.ends_with("app.js")),
            "app.js should remain: {paths:?}"
        );
        assert!(
            !paths.iter().any(|p| p.to_string_lossy().contains("vendor")),
            "vendor walk contents still excluded: {paths:?}"
        );
    }

    #[test]
    fn excluded_directory_tree_is_omitted() {
        let dir = TempDir::new().unwrap();
        let root = dir.path();
        fs::create_dir_all(root.join("vendor/pkg/deep")).unwrap();
        fs::write(root.join("vendor/pkg/deep/lib.js"), "v\n").unwrap();
        fs::write(root.join("vendor/top.js"), "v\n").unwrap();
        fs::write(root.join("app.js"), "a\n").unwrap();
        let global = GlobalFlags {
            exclude: vec!["vendor/**".into()],
            ..GlobalFlags::test_default()
        };
        let paths = collect_file_paths_opts_with_list(
            &[".".into()],
            &global,
            false,
            Some(root),
            None,
            None,
        )
        .unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| {
                p.strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect();
        assert!(rels.iter().any(|r| r == "app.js"), "kept app.js: {rels:?}");
        assert!(
            !rels.iter().any(|r| r.contains("vendor")),
            "vendor/** must omit the tree: {rels:?}"
        );
    }

    #[test]
    fn extension_exclude_still_collects_other_files_under_dir() {
        let dir = TempDir::new().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("src")).unwrap();
        fs::write(root.join("src/lib.rs"), "fn x() {}\n").unwrap();
        fs::write(root.join("src/keep.md"), "ok\n").unwrap();
        let global = GlobalFlags {
            exclude: vec!["*.rs".into()],
            ..GlobalFlags::test_default()
        };
        let paths = collect_file_paths_opts_with_list(
            &[".".into()],
            &global,
            false,
            Some(root),
            None,
            None,
        )
        .unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| {
                p.strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect();
        assert!(
            rels.iter().any(|r| r == "src/keep.md"),
            "*.rs must not prune src/: {rels:?}"
        );
        assert!(
            !rels.iter().any(|r| r.ends_with(".rs")),
            "*.rs still excludes rust files: {rels:?}"
        );
    }

    #[test]
    fn vendor_star_exclude_omits_nested_like_double_star() {
        // globset `*` matches `/` (literal_separator is off). `vendor/*` is
        // the same exclude language as `vendor/**`.
        let dir = TempDir::new().unwrap();
        let root = dir.path();
        fs::create_dir_all(root.join("vendor/pkg")).unwrap();
        fs::write(root.join("vendor/top.js"), "t\n").unwrap();
        fs::write(root.join("vendor/pkg/deep.js"), "d\n").unwrap();
        fs::write(root.join("app.js"), "a\n").unwrap();
        let global = GlobalFlags {
            exclude: vec!["vendor/*".into()],
            ..GlobalFlags::test_default()
        };
        let paths = collect_file_paths_opts_with_list(
            &[".".into()],
            &global,
            false,
            Some(root),
            None,
            None,
        )
        .unwrap();
        let rels: Vec<_> = paths
            .iter()
            .map(|p| {
                p.strip_prefix(root)
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect();
        assert!(rels.iter().any(|r| r == "app.js"), "kept app.js: {rels:?}");
        assert!(
            !rels.iter().any(|r| r.contains("vendor")),
            "vendor/* omits the tree: {rels:?}"
        );
    }
}

#[cfg(all(test, any(feature = "cli", feature = "files")))]
mod gitignore_case_tests {
    use super::*;
    use std::fs;

    #[test]
    fn gitignore_star_log_skips_uppercase_ext_only_on_windows() {
        let dir = tempfile::TempDir::new().unwrap();
        fs::create_dir(dir.path().join(".git")).unwrap();
        fs::write(dir.path().join(".gitignore"), "*.log\n").unwrap();
        fs::write(dir.path().join("app.LOG"), "hit\n").unwrap();
        fs::write(dir.path().join("keep.txt"), "hit\n").unwrap();
        let paths = collect_file_paths_with_ignores(dir.path(), &[], &[], false).unwrap();
        let names: Vec<String> = paths
            .iter()
            .filter_map(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
            .collect();
        assert!(names.iter().any(|n| n == "keep.txt"), "{names:?}");
        #[cfg(windows)]
        assert!(
            !names.iter().any(|n| n.eq_ignore_ascii_case("app.LOG")),
            "gitignore *.log must drop app.LOG on Windows: {names:?}"
        );
        #[cfg(not(windows))]
        assert!(
            names.iter().any(|n| n == "app.LOG"),
            "gitignore stays case-sensitive off Windows: {names:?}"
        );
    }
}

#[cfg(all(test, any(feature = "cli", feature = "files")))]
mod user_glob_case_tests {
    use super::*;

    #[test]
    fn unclosed_class_is_invalid_input() {
        let err = compile_user_glob("*[").unwrap_err();
        assert!(
            crate::exit::is_invalid_input(&err),
            "user glob compile must be InvalidInputError, got: {err:#}"
        );
        let msg = err.to_string();
        assert!(
            msg.contains("error parsing glob") && msg.contains("*["),
            "{msg}"
        );
        let built = build_glob_matcher(&["*[".into()]).unwrap_err();
        assert!(
            crate::exit::is_invalid_input(&built),
            "build_glob_matcher must peel InvalidInputError, got: {built:#}"
        );
    }

    #[test]
    fn star_txt_matches_uppercase_ext_only_on_windows() {
        let set = build_glob_matcher(&["*.txt".into()]).unwrap().unwrap();
        #[cfg(windows)]
        assert!(
            set.is_match("Hit.TXT"),
            "*.txt must match Hit.TXT on Windows"
        );
        #[cfg(not(windows))]
        assert!(
            !set.is_match("Hit.TXT"),
            "*.txt stays case-sensitive off Windows"
        );
        assert!(set.is_match("hit.txt"));
    }
}

#[cfg(all(test, any(feature = "cli", feature = "files")))]
mod walk_prune_tests {
    use super::*;
    use std::path::Path;

    fn matcher(pat: &str) -> globset::GlobSet {
        build_glob_matcher(&[pat.into()]).unwrap().unwrap()
    }

    #[test]
    fn prune_vendor_double_star() {
        let m = matcher("vendor/**");
        let root = Path::new("/project");
        assert!(should_prune_excluded_directory(
            &root.join("vendor"),
            Some(root),
            &m
        ));
        assert!(!should_prune_excluded_directory(
            &root.join("src"),
            Some(root),
            &m
        ));
    }

    #[test]
    fn prune_vendor_star_same_as_double_star() {
        // globset `*` matches `/`; `vendor/*` covers every descendant.
        let m = matcher("vendor/*");
        let root = Path::new("/project");
        assert!(should_prune_excluded_directory(
            &root.join("vendor"),
            Some(root),
            &m
        ));
    }

    #[test]
    fn do_not_prune_extension_glob() {
        let m = matcher("*.rs");
        let root = Path::new("/project");
        assert!(!should_prune_excluded_directory(
            &root.join("src"),
            Some(root),
            &m
        ));
    }

    #[test]
    fn prune_nested_node_modules_prefix() {
        let m = matcher("**/node_modules/**");
        let root = Path::new("/project");
        assert!(should_prune_excluded_directory(
            &root.join("src/node_modules"),
            Some(root),
            &m
        ));
    }

    #[test]
    fn prune_target_double_star() {
        let m = matcher("target/**");
        let root = Path::new("/project");
        assert!(should_prune_excluded_directory(
            &root.join("target"),
            Some(root),
            &m
        ));
    }
}