mise 2026.9.14

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

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Component, Path, PathBuf};

use eyre::Result;
use globset::{Glob, GlobSet, GlobSetBuilder};
use walkdir::WalkDir;

use super::select::{self, Selection};
use super::shadow::{CaptureRoot, MAX_BYTES, MAX_FILE_BYTES, MAX_FILES};
use super::store::{Coverage, CoverageEntry, PathReason};
use crate::config::Config;
use crate::dirs;
use crate::file::{self, display_path};
use crate::system::files::{FileMode, FilePolicy};

/// Credential names excluded from capture by default.
const CREDENTIAL_NAMES: &[&str] = &["github_tokens.toml", "hosts.yml", "age.txt"];
const CREDENTIAL_GLOBS: &[&str] = &[
    ".netrc",
    "*.age",
    "*.key",
    "*.pem",
    "*.gpg",
    "*.kdbx",
    "id_*",
    "*token*",
    "*secret*",
    "credentials*",
    "oauth*",
];

pub(crate) type Policy = FilePolicy;

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub(crate) struct TrackedEntry {
    /// Absolute, `~` expanded, lexically normalized.
    pub path: PathBuf,
    /// The explicit tracking declaration's mode.
    pub mode: String,
    pub policy: Policy,
    /// The shared stream of a tracked file with variants.
    pub variant: Option<String>,
    pub declared_in: Option<PathBuf>,
    /// The entry's own `exclude` globs, relative to its path, with the
    /// rules of a deployment entry's list (see
    /// [`crate::system::files::is_excluded`]).
    ///
    /// `None` means the declaration said nothing, so whatever the saved
    /// manifest carries stands. `Some` means it spoke — including
    /// `Some([])`, which clears the list another machine published.
    /// Flattening the two into one empty vec left no way to say "capture
    /// all of it after all".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exclude: Option<Vec<String>>,
    /// The entry's own `include` globs, relative to its path and matched
    /// like `exclude`.
    ///
    /// `None` means no list was declared and the whole tree is captured.
    /// `Some` means one was, and only what it names is — including
    /// `Some([])`, which selects nothing.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub include: Option<Vec<String>>,
}

impl TrackedEntry {
    /// The compiled `exclude` patterns; an invalid one was already
    /// reported when the declaration was read.
    pub(crate) fn exclude_patterns(&self) -> Vec<glob::Pattern> {
        self.exclude
            .iter()
            .flatten()
            .filter_map(|pattern| glob::Pattern::new(pattern).ok())
            .collect()
    }

    /// Whether the entry's own `exclude` list drops `path`: a path below
    /// the entry whose entry-relative form matches, as for a deployment
    /// entry. The entry path itself is never excluded by its own list.
    pub(crate) fn is_excluded(&self, path: &Path) -> bool {
        excluded_by_entry(&self.path, self.exclude.as_deref().unwrap_or(&[]), path)
    }

    /// The compiled `include` patterns.
    ///
    /// A pattern that cannot be read is dropped, which makes the list
    /// select less: on a capture that means a file is not saved, and on
    /// a replay it means the file reads as unselected, which never
    /// deletes. Both directions are the safe one, and such a pattern is
    /// refused where it is written, so this is the last line rather than
    /// the only one.
    pub(crate) fn include_patterns(&self) -> Option<Vec<glob::Pattern>> {
        Some(
            self.include
                .as_ref()?
                .iter()
                .filter_map(|pattern| glob::Pattern::new(pattern).ok())
                .collect(),
        )
    }

    /// An `include` pattern that names something inside `directory`, if
    /// one does. Used to explain why a pattern reaching into a nested
    /// repository selects nothing, rather than leaving the user to
    /// wonder whether they wrote it wrong.
    /// Whether nothing in the entry's `include` list could select
    /// anything inside `directory`, so the walk can skip it whole.
    ///
    /// **Every way of not knowing answers "walk it".** With the limits
    /// counting what is captured, a directory walked for nothing costs
    /// time; one skipped by mistake costs a file the user asked to keep.
    /// So an entry with no list never prunes, and a directory this entry
    /// cannot even relate to itself is walked rather than skipped.
    pub(crate) fn include_prunes(&self, directory: &Path) -> bool {
        let Some(patterns) = self.include.as_ref() else {
            return false;
        };
        let Ok(rel) = directory.strip_prefix(&self.path) else {
            return false;
        };
        let components = path_components(rel);
        !patterns
            .iter()
            .any(|pattern| reaches_into(pattern, &components))
    }

    pub(crate) fn include_reaching_into(&self, directory: &Path) -> Option<&str> {
        let rel = directory.strip_prefix(&self.path).ok()?;
        let components = path_components(rel);
        self.include
            .iter()
            .flatten()
            .find(|pattern| reaches_into(pattern, &components))
            .map(String::as_str)
    }

    /// Whether the entry's `include` list selects `path`.
    ///
    /// **Rule 1: without an `include` list the whole tracked tree is
    /// considered. Rule 2: with one, only matching paths are. Rule 3: an
    /// explicit `exclude` still wins over `include`.** Rules 1 and 2 live
    /// here; rule 3 is the order the two lists are applied in, at every
    /// call site.
    ///
    /// **A list selects paths *inside* the entry, and the entry itself is
    /// not one of them**: its patterns are read relative to the entry, so
    /// none of them can name it. An entry that is itself a file is
    /// therefore left out by any list it declares — the same answer a
    /// declared-but-empty list gives, which is what makes "declared and
    /// empty" a choice rather than a special case. A directory entry's
    /// own path is not a file and is never captured either way.
    pub(crate) fn is_included(&self, path: &Path) -> bool {
        let Some(patterns) = self.include_patterns() else {
            return true;
        };
        match path.strip_prefix(&self.path) {
            Ok(rel) if !rel.as_os_str().is_empty() => {
                crate::system::files::is_excluded(&pattern_relative(rel), &patterns)
            }
            _ => false,
        }
    }

    /// Whether an `include` pattern names `path`, as opposed to `path`
    /// being the entry itself — which no pattern selected.
    ///
    /// Rule 4 asks this rather than `is_included`, so that declaring a
    /// list on an entry that *is* a credential-named file does not lift
    /// the guard for it. Overriding the guard means a pattern that names
    /// the file, which is something only a directory entry can have.
    fn selected_by_pattern(&self, path: &Path) -> bool {
        // the same matcher, not a second copy of it: the two questions
        // differ only in what they answer when no list was declared
        self.include.is_some() && self.is_included(path)
    }

    /// Why a capture leaves `path` out under this entry, if it does,
    /// with rule 4 applied.
    ///
    /// The one place rule 4 is decided. The walk, `mise dot save <path>`,
    /// `mise dot track`'s preflight and its dry run all ask here, so none
    /// of them can promise something the others will not do.
    pub(crate) fn capture_exclusion(&self, path: &Path) -> Option<&'static str> {
        let reason = capture_exclusion(path, &self.policy)?;
        // **An `include` list is a selection, and selection decides what
        // is captured.** A list the user wrote is the user choosing these
        // paths, so the builtin credential filtering that applies when no
        // list is given does not overrule it — a literal and a glob carry
        // the same authority, because "how specific was the pattern" is
        // not a question the user was answering.
        //
        // What the file is encrypted with is a separate question, decided
        // by `encrypt`. So a credential-like file selected for plaintext
        // capture is captured, and said out loud everywhere selection is
        // shown.
        if reason == CREDENTIAL_REASON && self.selected_by_pattern(path) {
            return None;
        }
        Some(reason)
    }

    pub(crate) fn tree_path(&self, path: &Path) -> Result<String> {
        super::sync::layout::Roots::current()
            .branch_path(path, self.variant.as_deref())
            .ok_or_else(|| {
                eyre::eyre!(
                    "cannot represent tracked path {} portably",
                    display_path(path)
                )
            })
    }

    pub(crate) fn display(&self) -> String {
        display_path(&self.path)
    }

    pub(crate) fn new(path: PathBuf, mode: &str, policy: Policy) -> Self {
        Self {
            path,
            mode: mode.to_string(),
            policy,
            variant: None,
            declared_in: None,
            exclude: None,
            include: None,
        }
    }
}

/// The resolved tracked set for one capture.
#[derive(Clone, Debug, Default)]
pub(crate) struct TrackedSet {
    pub entries: Vec<TrackedEntry>,
    pub manifest: super::manifest::Manifest,
    /// Current explicit local declarations, before repository reconciliation.
    pub declarations: Option<super::manifest::Manifest>,
    /// Explicitly disabled tracking declarations also remove Git enrollment.
    pub disabled: Vec<PathBuf>,
    /// Deployment inputs to validate, not paths enrolled for observation.
    pub required_sources: Vec<PathBuf>,
    /// `[history] exclude` globs as written (with `~`).
    pub exclude: Vec<String>,
    /// Declarations that could not be honoured, so they are never mistaken
    /// for protection.
    pub invalid: Vec<PathReason>,
}

/// What a walk of the tracked set found.
#[derive(Debug, Default)]
pub(crate) struct Walk {
    pub manifest: super::manifest::Manifest,
    /// The explicit entries as walked.
    pub entries: Vec<TrackedEntry>,
    pub roots: Vec<CaptureRoot>,
    /// Every captured file with the entry that owns it and its policy.
    pub files: BTreeMap<PathBuf, (usize, Policy)>,
    pub omitted: Vec<PathReason>,
    /// Credential-named files an `include` list selected for plaintext
    /// capture, so every report can say so.
    pub plaintext: Vec<PathReason>,
    /// For each entry with an `include` list, how many of its files the
    /// walk looked at, so a report can say how much the list selects.
    ///
    /// **This counts what was searched, which is not the whole tree.** A
    /// directory no pattern could reach into is skipped unopened — the
    /// point of the list — so what is inside it is never counted. An
    /// entry whose walk skipped anything is in [`Self::skipped`], and a
    /// report says "2 files" there rather than claiming "2 of 4".
    pub considered: BTreeMap<usize, u64>,
    /// Entries whose walk skipped a directory the `include` list could
    /// not reach into, so their `considered` count is a floor and not a
    /// total.
    pub skipped: BTreeSet<usize>,
    /// Repositories found inside a tracked directory, skipped whole.
    pub nested: Vec<PathReason>,
    pub incomplete: Vec<PathReason>,
    pub warnings: Vec<String>,
    /// Notices that claim selected contents were saved.
    pub capture_warnings: Vec<String>,
}

impl TrackedSet {
    /// The effective tracked set for the loaded configuration.
    pub(crate) async fn effective() -> Result<Self> {
        let config = Config::get().await?;
        let declared = Self::from_config(&config)?;
        if !super::shadow::HistoryRepo::path_in(&dirs::STATE).is_dir() {
            return Ok(declared);
        }
        match super::shadow::HistoryRepo::open_or_init_in(&dirs::STATE)? {
            Some(repo) => super::enrollment::resolve(&dirs::STATE, &repo, &declared, &[], &[]),
            None => Ok(declared),
        }
    }

    /// Explicit tracking from the system and global configuration layers.
    pub(crate) fn from_config(config: &Config) -> Result<Self> {
        let mut set = Self {
            exclude: super::config::exclude_globs()?,
            ..Default::default()
        };
        let requests = crate::system::files::composed_files_from_config(config)?
            .into_iter()
            .filter(|request| crate::system::files::declaration_is_global(config, request));
        set.add_requests(requests);
        for invalid in crate::system::files::invalid_declarations() {
            if !crate::system::files::tracking_config_is_global(config, &invalid.config) {
                continue;
            }
            set.invalid.push(PathReason {
                path: invalid.target,
                reason: format!("{} ({})", invalid.reason, display_path(&invalid.config)),
            });
        }
        set.manifest.exclude = set.exclude.clone();
        if set.manifest.enrollment.iter().any(|entry| entry.encrypt) {
            set.manifest.recipients = super::config::file_recipients()?;
        }
        set.declarations = Some(set.manifest.clone());
        Ok(set)
    }

    /// Add already composed, global declarations. Shared by live discovery
    /// and the read-only incoming-configuration preflight.
    pub(crate) fn add_requests(
        &mut self,
        requests: impl IntoIterator<Item = crate::system::files::FileRequest>,
    ) {
        let set = self;
        let environments = select::active_environments();
        for request in requests {
            if !request.enabled && request.mode == FileMode::Track {
                set.disabled.push(normalize_target(&request.target));
            }
            if request.enabled
                && matches!(
                    request.mode,
                    FileMode::Symlink | FileMode::SymlinkEach | FileMode::Copy | FileMode::Template
                )
            {
                let source = normalize_target(&request.source);
                if !set.required_sources.contains(&source) {
                    set.required_sources.push(source);
                }
            }
            if !request.enabled || request.mode != FileMode::Track {
                continue;
            }
            if let Err(error) = ensure_portable_ancestors(&request.target) {
                set.invalid.push(PathReason {
                    path: display_path(&request.target),
                    reason: error.to_string(),
                });
                continue;
            }
            let target = normalize_target(&request.target);
            let portable =
                if let Ok(relative) = target.strip_prefix(normalize(&global_config_dir())) {
                    Some(
                        format!("config/{}", relative.to_string_lossy().replace('\\', "/"))
                            .trim_end_matches('/')
                            .to_owned(),
                    )
                } else if let Ok(relative) = target.strip_prefix(normalize(&dirs::HOME)) {
                    Some(format!(
                        "home/{}",
                        relative.to_string_lossy().replace('\\', "/")
                    ))
                } else {
                    None
                };
            let Some(path) = portable.filter(|path| super::sync::layout::is_safe_branch_path(path))
            else {
                set.invalid.push(PathReason { path: display_path(&target), reason: "tracking requires a portable path under home or the mise configuration directory".into() });
                continue;
            };
            if let Err(error) = select::validate(&request.variants) {
                set.invalid.push(PathReason {
                    path: display_path(&target),
                    reason: error.to_string(),
                });
                continue;
            }
            set.manifest.enrollment.retain(|entry| entry.path != path);
            set.manifest.enrollment.push(super::manifest::Enrollment {
                path,
                autosave: request.policy.autosave,
                encrypt: request.policy.encrypt,
                variants: request.variants.clone(),
                exclude: declared_exclude(&request),
                include: request.include.as_ref().map(|patterns| {
                    patterns
                        .iter()
                        .map(|pattern| pattern.as_str().to_owned())
                        .collect()
                }),
            });
            set.manifest.enrollment.sort_by(|a, b| a.path.cmp(&b.path));
            let declared_in = Some(request.origin.config.clone());
            match request.mode {
                FileMode::Track => {
                    let mut entry = TrackedEntry::new(
                        normalize_target(&request.target),
                        "track",
                        request.policy,
                    );
                    entry.declared_in = declared_in;
                    entry.exclude = declared_exclude(&request);
                    entry.include = request.include.as_ref().map(|patterns| {
                        patterns
                            .iter()
                            .map(|pattern| pattern.as_str().to_owned())
                            .collect()
                    });
                    match select::select(&request.variants, &environments) {
                        Selection::Single => {}
                        Selection::Variant(variant) => {
                            entry.variant = Some(variant.name());
                        }
                        Selection::NoMatch => continue,
                        Selection::Ambiguous(variants) => {
                            let names: Vec<String> =
                                variants.iter().map(|variant| variant.name()).collect();
                            set.invalid.push(PathReason {
                                path: display_path(&request.target),
                                reason: format!(
                                    "ambiguous variant: {} match this machine equally",
                                    names.join(" and ")
                                ),
                            });
                            continue;
                        }
                    }
                    set.push(entry);
                }
                _ => unreachable!("only explicit tracking requests are enrolled"),
            }
        }
    }

    /// Adds explicit enrollment, rejecting conflicting encryption policies.
    pub(crate) fn push(&mut self, entry: TrackedEntry) {
        if let Some(existing) = self
            .entries
            .iter_mut()
            .find(|existing| existing.path == entry.path)
        {
            if existing.policy.encrypt != entry.policy.encrypt {
                self.invalid.push(PathReason {
                    path: display_path(&entry.path),
                    reason: "overlapping declarations disagree about encryption".into(),
                });
            }
            return;
        }
        self.entries.push(entry);
    }

    /// The most specific entry covering `path`.
    pub(crate) fn entry_for(&self, path: &Path) -> Option<&TrackedEntry> {
        owning_entry(&self.entries, path)
    }

    pub(crate) fn entry_index_for(&self, path: &Path) -> Option<usize> {
        owning_entry_index(&self.entries, path)
    }

    /// Refuse to act while an `[history] exclude` rule cannot be used.
    ///
    /// **Reading works; acting does not. Nothing acts on a rule set it
    /// could not fully build.** `ExcludeSet` keeps the rules it could
    /// compile and remembers the ones it could not, so every reader
    /// carries on with a list that is missing exactly the paths a rule
    /// was written to leave out. Reading that way is fine and is the
    /// point: `mise dot paths`, `mise dot status`, a dry-run preview and
    /// the watch set the watcher builds at startup all keep working and
    /// report the rule, because the command the diagnostic sends the user
    /// to must not fail for the reason it is diagnosing.
    ///
    /// Acting that way is not. A capture would store — and publish — the
    /// files the rule excluded; worse, synchronization would read the
    /// incomplete list as "these paths are selected here", take their
    /// absence from an incoming snapshot for a deletion, and remove the
    /// live files. So every operation that writes or deletes asks this
    /// first: capture, sync, pull, rollback. The watcher takes the
    /// refusal on its own save, which leaves it running and reporting
    /// rather than storing.
    pub(crate) fn refuse_unusable_exclusions(&self) -> Result<()> {
        match unusable_exclusions(&self.exclude_set()?) {
            Some(report) => eyre::bail!(
                "{report}, so nothing is saved, applied, or published; fix or remove the pattern, then try again"
            ),
            None => Ok(()),
        }
    }

    /// Whether a capture of this set would include `path`: under an entry,
    /// not excluded, not inside mise's own directories or a `.git`.
    pub(crate) fn would_capture(&self, path: &Path) -> Result<bool> {
        if !self.would_retain(path)? {
            return Ok(false);
        }
        if let Ok(meta) = std::fs::symlink_metadata(path)
            && !meta.is_dir()
            && classify_file(&meta).is_err()
        {
            return Ok(false);
        }
        Ok(true)
    }

    /// Enrollment and exclusion policy, independent of current readability or
    /// capture limits. An omitted saved file may be retained, never an excluded one.
    pub(crate) fn would_retain(&self, path: &Path) -> Result<bool> {
        let Some(owner) = self.entry_for(path) else {
            return Ok(false);
        };
        if owner.capture_exclusion(path).is_some() {
            return Ok(false);
        }
        if hard_exclusions().iter().any(|dir| path.starts_with(dir)) {
            return Ok(false);
        }
        if path
            .components()
            .any(|component| component.as_os_str() == ".git")
        {
            return Ok(false);
        }
        Ok(!self.dropped(
            &self.exclude_set()?,
            owner,
            path,
            Asked::Exactly,
            kind_of(path),
        ))
    }

    /// Whether the selection lists drop `path`: the global globs read
    /// against its owning entry's path, then that entry's own `exclude`,
    /// which a global `!glob` does not re-include, and last its
    /// `include` list — so **rule 3 holds, an explicit `exclude` wins
    /// over `include`**.
    ///
    /// The one composition. `would_retain` adds the filesystem checks a
    /// capture also makes; the watcher asks this alone, because it is
    /// deciding what to watch rather than what a walk found; and
    /// synchronization asks it to tell a path selection stopped
    /// covering from one that was deleted. All of them pick the owner
    /// with [`owning_entry`], so they cannot disagree.
    ///
    /// **Every caller says which question it is asking.** There is no
    /// default, deliberately: this predicate answered the watcher's
    /// permissive question for everyone, and synchronization — which is
    /// asking retention's strict question — inherited it and deleted a
    /// file on one machine because another machine stopped selecting it.
    /// A default is what let that happen, so the next reader has to
    /// choose rather than be handed the watcher's answer.
    pub(crate) fn excluded_by_lists(
        &self,
        exclude: &ExcludeSet,
        path: &Path,
        asked: Asked,
    ) -> bool {
        match self.entry_for(path) {
            Some(owner) => self.dropped(exclude, owner, path, asked, kind_of(path)),
            None => true,
        }
    }

    /// **One predicate, two axes: which question is being asked, and
    /// what is being asked about.** Stated here and implemented here,
    /// because four separate bugs came from these questions being
    /// answered by parallel copies.
    ///
    /// The *kind* axis:
    ///
    /// - For a **file**, the question is "is this file selected".
    /// - For a **directory**, it is "does this tree contain any
    ///   selected path". A directory is covered when something beneath
    ///   it is selected, although no pattern ever selects the directory
    ///   itself: an anchored list like `rules/**` does not name `rules`.
    ///   Asking `is_included` of a directory reported a tracked tree as
    ///   uncaptured, so `mise dot track` warned about a symlink whose
    ///   source it was capturing, and a rollback skipped the empty
    ///   subdirectories a capture still walks.
    /// - A path with **no kind** — removed or renamed — is where the
    ///   two disagree, and the reader decides.
    ///
    /// The *reader* axis, which only a path with no kind is left to:
    ///
    /// - A **capture** asks "should I store this path now", and
    ///   **retention** asks "is this still selected", to decide whether
    ///   a previously saved version is carried forward when the file
    ///   cannot be read now. Both answer from the patterns alone:
    ///   [`Asked::Exactly`]. Answering permissively would keep files
    ///   the user has taken out of their `include` list in history
    ///   indefinitely, for no better reason than that they happened to
    ///   be unreadable at save time.
    /// - **Synchronization** asks "is this path still managed here, so
    ///   that its absence from the incoming snapshot is a deletion to
    ///   replay". That is retention's question in other words, so it is
    ///   [`Asked::Exactly`] too. Asked permissively, a local copy whose
    ///   kind cannot be read — it sits under a directory this machine
    ///   cannot search — looks managed although no pattern selects it,
    ///   and a narrowing made on another machine deletes it here.
    /// - The **watcher** asks "might this event matter", and is
    ///   deliberately permissive: a path that has just vanished counts
    ///   as whatever it could have been, so a deleted tree still wakes
    ///   it. [`Asked::Possibly`].
    fn dropped(
        &self,
        exclude: &ExcludeSet,
        owner: &TrackedEntry,
        path: &Path,
        asked: Asked,
        kind: Kind,
    ) -> bool {
        // **A tracked directory's own root is not something the global
        // list judges.** The walk never tests it: it steps past the root
        // and filters what is inside. A bare pattern that happens to
        // equal the directory's name would otherwise say "excluded"
        // about an entry whose contents a capture takes in full — and a
        // rollback would then stop recording that the directory existed.
        // A file entry is tested, exactly as the walk tests it.
        // A root that cannot be read — it has just been removed — is
        // not judged either. Nothing in the declaration says which kind
        // an entry is (`mode = "track"` covers both), and of the two
        // answers only this one is safe: a removed tracked tree is a
        // change history has to notice, and a pattern equal to its name
        // swallowing that event would leave its files in history after
        // they are gone.
        if inside_nested_repository(owner, path) {
            return true;
        }
        let judged = path != owner.path || kind == Kind::File;
        if judged && exclude.is_match(path, &owner.path) {
            return true;
        }
        if owner.is_excluded(path) {
            return true;
        }
        match kind {
            Kind::File => !owner.is_included(path),
            Kind::Directory => owner.include_prunes(path),
            Kind::Unknown => match asked {
                Asked::Exactly => !owner.is_included(path),
                Asked::Possibly => !owner.is_included(path) && owner.include_prunes(path),
            },
        }
    }

    pub(crate) fn exclude_set(&self) -> Result<ExcludeSet> {
        ExcludeSet::new(&self.exclude)
    }

    /// Walks every entry and decides, file by file, what the capture holds.
    pub(crate) fn walk(&self) -> Result<Walk> {
        self.walk_entries(None)
    }

    /// Walks only the entries at `selected`, while the set keeps all of
    /// them.
    ///
    /// **Which entry owns a path is a question about the whole set;
    /// walking is what costs.** A preview needs every declaration
    /// present, so a target nested under an existing entry — or an
    /// existing entry nested under the target — is attributed the way a
    /// capture would attribute it. It does not need the other entries
    /// walked: `mise dot track --dry-run` on one directory would
    /// otherwise re-walk and re-stat every directory already tracked on
    /// the machine, which is the opposite of cheap.
    pub(crate) fn walk_selected(&self, selected: &[usize]) -> Result<Walk> {
        self.walk_entries(Some(selected))
    }

    fn walk_entries(&self, selected: Option<&[usize]>) -> Result<Walk> {
        let set = self;
        let exclude = set.exclude_set()?;
        let hard = hard_exclusions();
        let home = normalize(&dirs::HOME);
        let mut walk = Walk {
            manifest: set.manifest.clone(),
            ..Default::default()
        };
        walk.manifest.exclude = set.exclude.clone();
        // the rule cannot narrow this walk, so say what the listing now
        // holds that it was written to leave out. Refusing here would
        // break `mise dot paths`, the watch set, and the previews — the
        // very commands that let someone see and fix the rule.
        if let Some(report) = unusable_exclusions(&exclude) {
            walk.warnings.push(format!(
                "{report}; it is ignored here, so this lists paths it would leave out, and nothing is saved, applied, or published until it is fixed"
            ));
        }
        for (index, entry) in set.entries.iter().enumerate() {
            if selected.is_some_and(|selected| !selected.contains(&index)) {
                continue;
            }
            walk_entry(set, index, entry, &exclude, &hard, &mut walk);
        }
        // Protected files are excluded from capture itself, never kept in
        // a hidden local-only history. Explicit encryption permits key files
        // to be tracked without storing their plaintext.
        walk.files.retain(|path, (index, policy)| {
            let Some(owner) = set.entries.get(*index) else {
                return capture_exclusion(path, policy).is_none();
            };
            // rule 4 lives on the entry, so every reader gets the same
            // answer; a file it lets through is announced, because it
            // goes into history in plaintext and to any connected origin
            let Some(reason) = owner.capture_exclusion(path) else {
                if capture_exclusion(path, policy).is_some() {
                    walk.plaintext.push(PathReason {
                        path: display_path(path),
                        reason: "selected by an include list; saved in plaintext".into(),
                    });
                }
                return true;
            };
            walk.omitted.push(PathReason {
                path: display_path(path),
                reason: reason.into(),
            });
            false
        });
        // a credential-named file saved because an entry named it exactly
        // is worth saying out loud on every capture, not only in
        // `mise dot paths`: it goes to any connected origin as plaintext
        for plaintext in &walk.plaintext {
            walk.capture_warnings.push(format!(
                "{}: an include list selects it, so it is saved in plaintext although it looks like a credential store; `encrypt = true` saves it encrypted instead",
                plaintext.path
            ));
        }
        walk.entries = set.entries.clone();
        let config = normalize(&global_config_dir());
        let mut roots: BTreeMap<String, CaptureRoot> = BTreeMap::new();
        for (path, (owner, _)) in &walk.files {
            if path.to_str().is_none() {
                eyre::bail!(
                    "history cannot represent a non-UTF-8 filename; refusing to change its bytes"
                );
            }
            let (label, base, relative) = if let Ok(relative) = path.strip_prefix(&config) {
                ("config", config.clone(), relative.to_path_buf())
            } else if let Ok(relative) = path.strip_prefix(&home) {
                ("home", home.clone(), relative.to_path_buf())
            } else {
                (
                    "fs",
                    PathBuf::from(std::path::MAIN_SEPARATOR.to_string()),
                    path.components()
                        .filter(|c| matches!(c, Component::Normal(_)))
                        .collect(),
                )
            };
            let label = walk.entries[*owner]
                .variant
                .as_ref()
                .map_or_else(|| label.to_string(), |variant| format!("{label}@{variant}"));
            let root = roots.entry(label.clone()).or_insert_with(|| CaptureRoot {
                label,
                path: base,
                files: vec![],
                bytes: 0,
            });
            root.files.push(relative);
            // a symlink's length is its target string and a nested
            // repository's its directory entry: neither is captured content
            root.bytes += std::fs::symlink_metadata(path)
                .ok()
                .filter(|m| m.is_file())
                .map_or(0, |m| m.len());
        }
        walk.roots = roots.into_values().collect();
        Ok(walk)
    }

    /// The rules this set captures under, for the checkpoint record.
    pub(crate) fn coverage(&self, walk: &Walk) -> Coverage {
        let entries: Vec<CoverageEntry> = walk
            .entries
            .iter()
            .map(|entry| CoverageEntry {
                path: entry.display(),
                mode: entry.mode.clone(),
                variant: entry.variant.clone(),
                autosave: entry.policy.autosave,
                encrypt: entry.policy.encrypt,
                state: "live".into(),
                declared_in: entry.declared_in.as_deref().map(display_path),
                exclude: entry.exclude.clone(),
                include: entry.include.clone(),
            })
            .collect();
        let mut omitted = walk.omitted.clone();
        omitted.extend(self.invalid.iter().cloned());
        Coverage {
            entries,
            exclude: self.exclude.clone(),
            // a marker rather than the rules themselves: a checkpoint
            // written before this matcher cannot be read with it
            matcher: Some(MATCHER_VERSION),
            incomplete: walk.incomplete.clone(),
            omitted,
            nested: walk.nested.clone(),
        }
    }
}

/// Walks one entry, recording its files, omissions, and symlinks.
fn walk_entry(
    set: &TrackedSet,
    index: usize,
    entry: &TrackedEntry,
    exclude: &ExcludeSet,
    hard: &[PathBuf],
    walk: &mut Walk,
) {
    let home = normalize(&dirs::HOME);
    let display = entry.display();
    if is_refused_root(&entry.path, &home) {
        walk.omitted.push(PathReason {
            path: display,
            reason: "refused: the home directory or above".into(),
        });
        return;
    }
    if hard.iter().any(|dir| entry.path.starts_with(dir)) {
        walk.omitted.push(PathReason {
            path: display,
            reason: "mise internal directory".into(),
        });
        return;
    }
    let meta = match std::fs::symlink_metadata(&entry.path) {
        Ok(meta) => meta,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return,
        Err(err) => {
            walk.omitted.push(PathReason {
                path: display,
                reason: format!("unreadable: {err}"),
            });
            return;
        }
    };
    if !meta.is_dir() {
        // an exclusion wins over a direct declaration as it does over a
        // directory walk: what the user excluded never enters a snapshot.
        if exclude.is_match(&entry.path, &entry.path) {
            return;
        }
        // an `include` list selects paths inside a tracked directory, so
        // a file entry that declares one selects nothing at all. Said out
        // loud rather than dropped quietly: a file that stops being saved
        // is exactly the kind of thing a user needs told.
        if !entry.is_included(&entry.path) {
            walk.omitted.push(PathReason {
                path: display,
                reason: "its include list selects nothing: a list selects paths inside a tracked directory, and this entry is a file".into(),
            });
            return;
        }
        match classify_file(&meta) {
            Ok(_) => {
                walk.files.insert(entry.path.clone(), (index, entry.policy));
            }
            Err(reason) => walk.omitted.push(PathReason {
                path: display,
                reason,
            }),
        }
        return;
    }
    let walker = WalkDir::new(&entry.path)
        .follow_links(false)
        .sort_by_file_name()
        .into_iter()
        .filter_entry(|candidate| {
            !(candidate.file_type().is_dir()
                && (candidate.file_name() == ".git"
                    || hard.iter().any(|dir| dir == candidate.path())))
        });
    let entry_exclude = entry.exclude_patterns();
    let entry_include = entry.include_patterns();
    let mut files = 0u64;
    let mut bytes = 0u64;
    let mut walker = walker;
    while let Some(candidate) = walker.next() {
        let candidate = match candidate {
            Ok(candidate) => candidate,
            Err(err) => {
                let path = err
                    .path()
                    .map(display_path)
                    .unwrap_or_else(|| display.clone());
                walk.omitted.push(PathReason {
                    path,
                    reason: format!("unreadable: {err}"),
                });
                continue;
            }
        };
        let path = candidate.path();
        if path == entry.path {
            continue;
        }
        // A more specific entry owns this subtree and walks it itself.
        // **Skipped whole, not file by file**: ownership is by the most
        // specific entry, so nothing below a directory another entry
        // owns can belong to this one, and stat'ing all of it to decide
        // that again is the cost this avoids — a tracked directory
        // inside another tracked directory would otherwise be walked
        // twice on every save.
        if set
            .entry_index_for(path)
            .is_some_and(|owner| owner != index)
        {
            if candidate.file_type().is_dir() {
                walker.skip_current_dir();
            }
            continue;
        }
        let file_type = candidate.file_type();
        // **A repository inside a tracked directory is structure, not
        // selection.** Asked of every directory before any exclude,
        // include or re-include rule, because no rule in a selection
        // list may send the walk into another working tree. A directory
        // an exclusion drops is not always pruned — a later `!` rule can
        // re-include something below it — so the walk descends, and with
        // this check further down it descended into repositories, and a
        // re-include then captured their files. `would_retain` refuses
        // those same paths, so capture and retention disagreed about
        // files that a connected origin would have received.
        //
        // A repository found under an excluded directory is reported
        // too: it costs one `.git` probe per directory the walk was
        // about to skip anyway, and saying "this is a repository" about
        // a path the user will look for is better than saying nothing.
        if file_type.is_dir() && path.join(".git").exists() {
            // A repository found inside a tracked directory is skipped
            // whole, and nothing is written for it — not its files, not
            // a commit pointer. A pointer would name objects this
            // history does not have, and there is a supported way to
            // get the files: track the repository itself. An `include`
            // pattern that names paths inside one is called out, so the
            // skip does not read as the list being wrong.
            let reason = match entry.include_reaching_into(path) {
                Some(pattern) => format!(
                    "{NESTED_REPOSITORY_REASON}; the include pattern {pattern:?} selects nothing inside it"
                ),
                None => NESTED_REPOSITORY_REASON.to_string(),
            };
            walk.nested.push(PathReason {
                path: display_path(path),
                reason,
            });
            walker.skip_current_dir();
            continue;
        }
        // an excluded directory is not entered at all: `~/.codex/sessions`
        // can hold tens of thousands of files, and none of them can come
        // back into the capture
        if file_type.is_dir() && exclude.prunes_directory(path, &entry.path) {
            walker.skip_current_dir();
            continue;
        }
        if exclude.is_match(path, &entry.path) {
            continue;
        }
        // the entry's own exclusions: a matching directory is not entered
        if !entry_exclude.is_empty()
            && let Ok(rel) = path.strip_prefix(&entry.path)
            && crate::system::files::is_excluded(&pattern_relative(rel), &entry_exclude)
        {
            if file_type.is_dir() {
                walker.skip_current_dir();
            }
            continue;
        }
        if file_type.is_dir() {
            // **What the list cannot select is not walked.** The mirror
            // of exclude pruning, conservative in the same direction:
            // `include_reaching_into` says a pattern could name something
            // here whenever it cannot rule it out, so a missed skip costs
            // a walk while a wrong one would cost a file. A repository
            // was already decided above, before any of this.
            if entry.include_prunes(path) {
                walk.skipped.insert(index);
                walker.skip_current_dir();
            }
            continue;
        }
        // rule 2: with an `include` list, only matching paths are
        // considered. Applied after the exclude lists, so rule 3 holds: an
        // explicit exclusion wins.
        if let Some(entry_include) = &entry_include {
            *walk.considered.entry(index).or_default() += 1;
            match path.strip_prefix(&entry.path) {
                Ok(rel)
                    if !crate::system::files::is_excluded(
                        &pattern_relative(rel),
                        entry_include,
                    ) =>
                {
                    continue;
                }
                Err(_) => continue,
                Ok(_) => {}
            }
        }
        let meta = match candidate.metadata() {
            Ok(meta) => meta,
            Err(err) => {
                walk.omitted.push(PathReason {
                    path: display_path(path),
                    reason: format!("unreadable: {err}"),
                });
                continue;
            }
        };
        match classify_file(&meta) {
            Ok(size) => {
                // **The limits bound what is captured, so they count what
                // is captured.** A path an `include` list rejected is not
                // in the snapshot and must not spend the budget: a
                // growing `sessions/` directory would otherwise exhaust
                // it and the one file the list names would never be
                // saved — the feature defeated by exactly the files it
                // exists to leave out.
                files += 1;
                bytes += size;
                if files > MAX_FILES || bytes > MAX_BYTES {
                    let reason = format!(
                        "scan stopped after {MAX_FILES} files or {} MiB",
                        MAX_BYTES / (1024 * 1024)
                    );
                    walk.warnings
                        .push(format!("{display}: {reason}; the rest was not captured"));
                    walk.incomplete.push(PathReason {
                        path: display,
                        reason,
                    });
                    return;
                }
                walk.files.insert(path.to_path_buf(), (index, entry.policy));
            }
            Err(reason) => walk.omitted.push(PathReason {
                path: display_path(path),
                reason,
            }),
        }
    }
}

/// Size of a capturable file, or why it is omitted.
fn classify_file(meta: &std::fs::Metadata) -> std::result::Result<u64, String> {
    let file_type = meta.file_type();
    if file_type.is_symlink() {
        return Ok(0);
    }
    if !file_type.is_file() {
        return Err("special file".into());
    }
    let size = meta.len();
    if size > MAX_FILE_BYTES {
        return Err(format!(
            "{} MiB is over the {} MiB limit",
            size / (1024 * 1024),
            MAX_FILE_BYTES / (1024 * 1024)
        ));
    }
    Ok(size)
}

/// Why the credential guard keeps a file out of capture.
pub(crate) const CREDENTIAL_REASON: &str = "credential store; encrypt the file before tracking it";

/// What a capture says about a directory with its own `.git` found
/// inside a tracked one.
///
/// **A repository encountered inside a tracked directory is skipped, and
/// nothing is recorded for it. Tracking a repository's own directory
/// captures its working files, always without `.git`.** The explanation
/// names the remedy because there is one: the reach-in is unsupported,
/// not the goal.
pub(crate) const NESTED_REPOSITORY_REASON: &str =
    "a separate Git repository; track it directly to capture its working files";

/// Why `path` is left out of every capture under `policy`, if it is: a
/// machine-local configuration file, or a credential store that is not
/// enrolled with encryption. The guard is deliberately conservative and
/// matches by name alone (`id_ed25519.pub` is protected like its private
/// half); a narrower rule is the user's to add.
pub(crate) fn capture_exclusion(path: &Path, policy: &Policy) -> Option<&'static str> {
    let name = path.file_name()?.to_str()?;
    if name.ends_with(".local.toml") {
        Some("machine-local configuration")
    } else if !policy.encrypt && is_builtin_credential(path, name) {
        Some(CREDENTIAL_REASON)
    } else {
        None
    }
}

/// Whether the builtin rules protect a file of this name at this path.
pub(crate) fn is_builtin_credential(path: &Path, name: &str) -> bool {
    static NAMES: std::sync::LazyLock<GlobSet> = std::sync::LazyLock::new(credential_names);
    static GLOBS: std::sync::LazyLock<GlobSet> =
        std::sync::LazyLock::new(|| glob_set(CREDENTIAL_GLOBS));
    GLOBS.is_match(name)
        || (path.starts_with(normalize(&global_config_dir())) && NAMES.is_match(name))
}

/// How many omissions a capture report lists one by one before it
/// summarizes them and points at `mise dot paths`.
pub(crate) const OMISSION_LINES: usize = 10;

/// An entry-relative path as its patterns see it.
///
/// **A pattern is written with `/`, and on Windows the path it is matched
/// against arrives with `\`.** `cache/**` would never match
/// `cache\index`, so a `~\.codex` entry's `exclude` list would quietly
/// do nothing there. The separator is settled here, in the one helper the
/// capture walk, a dry run and a replay all match through, so the three
/// cannot disagree about what a list drops.
///
/// On unix a backslash is an ordinary character in a filename and is left
/// alone: a file actually named `cache\index` is one component, not two.
fn pattern_relative(rel: &Path) -> std::borrow::Cow<'_, Path> {
    #[cfg(windows)]
    {
        std::borrow::Cow::Owned(PathBuf::from(rel.to_string_lossy().replace('\\', "/")))
    }
    #[cfg(not(windows))]
    {
        std::borrow::Cow::Borrowed(rel)
    }
}

/// Whether `patterns` (an entry's own `exclude` list, relative to
/// `entry_path`) drop `path`; the entry path itself never is.
pub(crate) fn excluded_by_entry(entry_path: &Path, patterns: &[String], path: &Path) -> bool {
    if patterns.is_empty() {
        return false;
    }
    let patterns: Vec<glob::Pattern> = patterns
        .iter()
        .filter_map(|pattern| glob::Pattern::new(pattern).ok())
        .collect();
    match path.strip_prefix(entry_path) {
        Ok(rel) if !rel.as_os_str().is_empty() => {
            crate::system::files::is_excluded(&pattern_relative(rel), &patterns)
        }
        _ => false,
    }
}

/// Whether `patterns` (an entry's own `include` list, relative to
/// `entry_path`) select `path`; the entry path itself never is, exactly
/// as [`TrackedEntry::is_included`] answers it. The mirror of
/// [`excluded_by_entry`], for a replay reading the list a checkpoint
/// recorded.
pub(crate) fn included_by_entry(entry_path: &Path, patterns: &[String], path: &Path) -> bool {
    let patterns: Vec<glob::Pattern> = patterns
        .iter()
        .filter_map(|pattern| glob::Pattern::new(pattern).ok())
        .collect();
    match path.strip_prefix(entry_path) {
        Ok(rel) if !rel.as_os_str().is_empty() => {
            crate::system::files::is_excluded(&pattern_relative(rel), &patterns)
        }
        _ => false,
    }
}

/// Whether the display path `path` is `root` itself or lies below it.
///
/// **Two display paths are compared through one normalized form, never
/// byte for byte.** Within a host both spellings occur: one path may
/// have been written by `display_path`, with the host's separator, while
/// the other was rebuilt from a tree path with `/`. They name the same
/// file, and every caller that compares them — coverage, replay, status
/// — goes through here so there is one place this can be wrong.
///
/// **The convention is the reading host's, and that is the only one
/// these strings are ever in.** A checkpoint records portable `home/…`
/// tree paths; every display string is rebuilt from those locally, so
/// none of them carries another host's separator. That is what makes it
/// safe — and necessary — to read a backslash as an ordinary character
/// in a file name on unix, where it is one: treating `~/x\y` as a file
/// inside `~/x` would let one entry appear to own another's paths, and a
/// replay would then judge a live file by the wrong root and the wrong
/// exclusions.
pub(crate) fn display_under(path: &str, root: &str) -> bool {
    let path = display_separators(&file::replace_path(path).to_string_lossy());
    let root = display_separators(&file::replace_path(root).to_string_lossy());
    path == root
        || path
            .strip_prefix(&root)
            .is_some_and(|rest| rest.starts_with('/'))
}

/// A display path in the one form comparisons use: `/`-separated.
fn display_separators(path: &str) -> String {
    // on Windows both characters separate; on unix a backslash is part of
    // a file's name, and rewriting it would invent a directory boundary
    // that does not exist
    if cfg!(windows) {
        path.replace('\\', "/")
    } else {
        path.to_string()
    }
}

/// A tree this large is worth a second look before it is tracked: more
/// files than this, or more bytes, and `mise dot track` warns.
pub(crate) const LARGE_TREE_FILES: usize = 5_000;
pub(crate) const LARGE_TREE_BYTES: u64 = 256 * 1024 * 1024;

impl Walk {
    /// How many files the walk captures.
    pub(crate) fn file_count(&self) -> usize {
        self.roots.iter().map(|root| root.files.len()).sum()
    }

    /// The bytes of the captured files.
    pub(crate) fn bytes(&self) -> u64 {
        self.roots.iter().map(|root| root.bytes).sum()
    }

    /// `22,972 files, 1.2 GiB`.
    pub(crate) fn summary(&self) -> String {
        count_and_size(self.file_count(), self.bytes())
    }

    /// Say what this walk had to report: a truncated tree, an exclusion
    /// it could not apply.
    ///
    /// **Every reader says it, not only the one that captures.** A walk
    /// that only lists still walked under the same rules, and a rule it
    /// could not use changes what the listing holds — so a command that
    /// showed the listing silently would be the one place the problem is
    /// invisible.
    pub(crate) fn report_warnings(&self) {
        for warning in self.warnings.iter().chain(&self.capture_warnings) {
            super::notices::say(&format!("history: {warning}"));
        }
    }
}

/// What tracking one entry of a preview set captures: the files that entry
/// owns (a more specific entry owns its own subtree), and what a save
/// would leave out under it.
#[derive(Debug, Default)]
pub(crate) struct EntryPreview {
    pub files: usize,
    pub bytes: u64,
    pub omitted: Vec<PathReason>,
    pub nested: Vec<PathReason>,
    /// Credential-named files this entry captures in the clear, so a dry
    /// run promises what the first save will really do.
    pub plaintext: Vec<PathReason>,
    pub incomplete: Vec<PathReason>,
}

impl EntryPreview {
    pub(crate) fn summary(&self) -> String {
        count_and_size(self.files, self.bytes)
    }

    pub(crate) fn is_large(&self) -> bool {
        self.files > LARGE_TREE_FILES || self.bytes > LARGE_TREE_BYTES
    }
}

impl Walk {
    /// The preview of the entry at `index` of `set`, which this walk was
    /// taken from: nested targets in one command partition instead of the
    /// outer one counting the inner one's files too.
    pub(crate) fn preview_of(&self, set: &TrackedSet, index: usize) -> EntryPreview {
        let mut preview = EntryPreview::default();
        for (path, (owner, _)) in &self.files {
            if *owner != index {
                continue;
            }
            preview.files += 1;
            preview.bytes += std::fs::symlink_metadata(path)
                .ok()
                .filter(|m| m.is_file())
                .map_or(0, |m| m.len());
        }
        let owned = |reported: &PathReason| {
            set.entry_index_for(&file::replace_path(Path::new(&reported.path))) == Some(index)
        };
        preview.omitted = self.omitted.iter().filter(|r| owned(r)).cloned().collect();
        preview.nested = self.nested.iter().filter(|r| owned(r)).cloned().collect();
        preview.plaintext = self
            .plaintext
            .iter()
            .filter(|r| owned(r))
            .cloned()
            .collect();
        let display = set.entries[index].display();
        preview.incomplete = self
            .incomplete
            .iter()
            .filter(|r| r.path == display)
            .cloned()
            .collect();
        preview
    }
}

/// `1 file, 12 B` or `22,972 files, 1.2 GiB`.
pub(crate) fn count_and_size(files: usize, bytes: u64) -> String {
    format!(
        "{} {}, {}",
        with_separators(files),
        if files == 1 { "file" } else { "files" },
        bytesize::ByteSize::b(bytes).display().iec()
    )
}

pub(crate) fn with_separators(n: usize) -> String {
    let digits = n.to_string();
    let mut out = String::with_capacity(digits.len() + digits.len() / 3);
    for (i, ch) in digits.chars().enumerate() {
        if i > 0 && (digits.len() - i).is_multiple_of(3) {
            out.push(',');
        }
        out.push(ch);
    }
    out
}

/// The set tracking `path` alone would capture, under the `[history]`
/// exclusions: what `mise dot paths --preview` lists and what `mise dot
/// track` sizes up before it writes a declaration.
pub(crate) fn preview_set(path: &Path, policy: Policy) -> Result<TrackedSet> {
    Ok(preview_set_with(
        path,
        policy,
        super::config::exclude_globs()?,
    ))
}

/// [`preview_set`] with the `[history] exclude` globs already read, so a
/// command previewing several paths reads the configuration once.
pub(crate) fn preview_set_with(path: &Path, policy: Policy, exclude: Vec<String>) -> TrackedSet {
    let mut set = TrackedSet {
        exclude,
        ..Default::default()
    };
    set.push(TrackedEntry::new(normalize_target(path), "track", policy));
    set
}

/// The lines a capture reports about what it left out: every omission and
/// nested repository with its reason when there are few, otherwise one
/// summary.
pub(crate) fn omission_report(omitted: &[PathReason], nested: &[PathReason]) -> Vec<String> {
    if omitted.is_empty() && nested.is_empty() {
        vec![]
    } else if omitted.len() + nested.len() <= OMISSION_LINES {
        omitted
            .iter()
            .map(|omitted| format!("omitted: {} ({})", omitted.path, omitted.reason))
            .chain(
                nested
                    .iter()
                    .map(|nested| format!("nested: {} ({})", nested.path, nested.reason)),
            )
            .collect()
    } else {
        vec![omission_summary(omitted, nested)]
    }
}

/// One line naming how many files a capture leaves out and why.
pub(crate) fn omission_summary(omitted: &[PathReason], nested: &[PathReason]) -> String {
    let mut parts = vec![];
    if !omitted.is_empty() {
        let credentials = omitted
            .iter()
            .filter(|omitted| omitted.reason == CREDENTIAL_REASON)
            .count();
        let detail = match credentials {
            0 => String::new(),
            n if n == omitted.len() => " (credential store)".into(),
            n => format!(" ({n} credential store)"),
        };
        parts.push(format!(
            "{} files omitted from capture{detail}",
            omitted.len()
        ));
    }
    if !nested.is_empty() {
        parts.push(format!(
            "{} nested {} skipped",
            nested.len(),
            if nested.len() == 1 {
                "repository"
            } else {
                "repositories"
            }
        ));
    }
    format!("{}; `mise dot paths` lists them", parts.join("; "))
}

/// Credential stores mise itself knows by name; they mean something only
/// under the global configuration directory.
fn credential_names() -> GlobSet {
    glob_set(CREDENTIAL_NAMES)
}

fn glob_set(patterns: &[&str]) -> GlobSet {
    let mut builder = GlobSetBuilder::new();
    for pattern in patterns {
        if let Ok(glob) = Glob::new(pattern) {
            builder.add(glob);
        }
    }
    builder.build().expect("static credential globs")
}

/// The `[history] exclude` globs, applied in order with the last match
/// deciding: a `!glob` after a broader glob re-includes what it matches.
#[derive(Debug, Default)]
pub(crate) struct ExcludeSet {
    list: PatternList,
}

impl ExcludeSet {
    pub(crate) fn new(globs: &[String]) -> Result<Self> {
        Ok(Self {
            list: PatternList::new(globs)?,
        })
    }

    /// The `[history] exclude` rules this matcher cannot use.
    ///
    /// **An exclusion that does not work must not be read as no
    /// exclusion.** A capture asks here and refuses rather than store
    /// the files the broken rule named, because those are the files the
    /// user wrote it to keep out, and a capture can be published to a
    /// connected origin.
    pub(crate) fn unusable(&self) -> &[(String, String)] {
        &self.list.unusable
    }

    /// Whether `path`, tracked under `root`, is excluded.
    ///
    /// **A path is excluded when the last rule that matches it, or any of
    /// its ancestors down to the tracked root, is an exclusion.** So an
    /// excluded directory takes its contents with it — the reading a
    /// tracked entry's own `exclude` list already has — and a later
    /// `!glob` naming something inside it still re-includes that, because
    /// it is the later rule.
    ///
    /// Because a descendant is excluded by its own ancestor's match,
    /// pruning an excluded directory out of the walk can only ever be a
    /// speed-up: every file under it would have been excluded one by one
    /// anyway, and the walk and the callers that never walk — `mise dot
    /// save <path>`, the watcher — cannot disagree.
    ///
    /// Ancestors stop at the tracked root. A pattern is about what the
    /// user tracks, not about where their home directory happens to live.
    pub(crate) fn is_match(&self, path: &Path, root: &Path) -> bool {
        self.decide(path, root, false)
    }

    /// Whether the walk can skip `dir` whole: the rules exclude the
    /// directory itself, or a rule of the form `<P>/**` names everything
    /// under it. The second case is the spelling the docs show, and
    /// without it `~/.codex/sessions` was still enumerated file by file.
    ///
    /// This belongs here and not in [`Self::is_match`] because pruning is
    /// a pure optimization: the matcher is ancestor-aware, so every file
    /// under an excluded directory is excluded whether or not the walk
    /// visits it. A missed prune is only slower, and a prune that fires
    /// cannot change which files are captured.
    pub(crate) fn prunes_directory(&self, dir: &Path, root: &Path) -> bool {
        self.decide(dir, root, true) && !self.may_reinclude_below(dir)
    }

    fn decide(&self, path: &Path, root: &Path, as_directory: bool) -> bool {
        if self.list.rules.is_empty() {
            return false;
        }
        // Everything is compared as `/`-separated text. A pattern is
        // written that way whatever platform it is read on, a path is not,
        // and normalizing once here is the only place the two forms can
        // fail to line up.
        // **A path outside the tracked root has no path relative to
        // it.** Falling back to the absolute path here let a relative
        // rule — compiled as `**/sessions/**` — match
        // `/somewhere/else/sessions/file`, so a capture would have
        // considered paths no entry covers, the watcher would have
        // followed them, and a replay would have skipped restoring files
        // the checkpoint never held. An absolute rule still applies:
        // those are matched against the path itself, which is a question
        // that does not need a root.
        let relative_path = if path == root {
            path.file_name().map(PathBuf::from)
        } else {
            path.strip_prefix(root).map(Path::to_path_buf).ok()
        };
        let mut candidates = vec![];
        for ancestor in path.ancestors() {
            candidates.push(separators(ancestor));
            if ancestor == root {
                break;
            }
        }
        let relative: Vec<String> = relative_path
            .iter()
            .flat_map(|relative| relative.ancestors().collect::<Vec<_>>())
            .filter(|ancestor| !ancestor.as_os_str().is_empty())
            .map(separators)
            .collect();
        let components: Vec<&str> = relative
            .first()
            .map(|relative| {
                relative
                    .split('/')
                    .filter(|component| !component.is_empty())
                    .collect()
            })
            .unwrap_or_default();
        self.list
            .rules
            .iter()
            .rfind(|rule| {
                if as_directory {
                    rule.covers_directory(&candidates, &relative, &components)
                } else {
                    rule.matches_any(&candidates, &relative, &components)
                }
            })
            .is_some_and(|rule| !rule.negated)
    }

    /// Whether any `!pattern` could re-include something below `dir`.
    ///
    /// **A matching directory is pruned from the walk only when no
    /// negated pattern could re-include anything beneath it. When one
    /// could, the directory is walked and its files are filtered
    /// individually.** Pruning is an optimization; last-match-wins is the
    /// semantics, and the semantics win. A list with no negations — which
    /// is almost every list — keeps the whole saving.
    ///
    /// Deliberately conservative, and more so than it may look: only an
    /// absolute negation has a directory of its own to compare against.
    /// A name pattern (`!important.log`) or a relative one
    /// (`!cache/keep.conf`) matches at any depth by design, so there is
    /// no subtree it is confined to and no directory that can be proved
    /// safe to skip — one such rule anywhere in `[history] exclude`
    /// turns pruning off for every tracked entry. That costs a walk of
    /// directories whose files are then filtered one by one; the
    /// alternative costs files the user asked to keep.
    pub(crate) fn may_reinclude_below(&self, dir: &Path) -> bool {
        // the comparison is between a pattern prefix and a path, so it
        // gets the same normalization the matcher gives them: `/` on
        // every platform, compared component by component
        let dir = separators(dir);
        self.list
            .rules
            .iter()
            .filter(|rule| rule.negated)
            .any(|rule| {
                if rule.anchor != Anchor::Absolute || rule.reinclude_roots.is_empty() {
                    return true;
                }
                rule.reinclude_roots
                    .iter()
                    .any(|root| under_or_above(root, &dir))
            })
    }
}

/// The `exclude` list a declaration wrote, or `None` when it wrote none.
///
/// **Saying nothing and saying nothing-is-excluded are different
/// answers.** The composed request flattens both to an empty pattern
/// list, so the explicitness the configuration layer already records is
/// what tells them apart. Without it a machine could never clear a list
/// another machine published: writing `exclude = []` would read as "I
/// have no opinion" and the saved list would stand for ever.
fn declared_exclude(request: &crate::system::files::FileRequest) -> Option<Vec<String>> {
    request.policy.explicit.exclude.then(|| {
        request
            .exclude
            .iter()
            .map(|pattern| pattern.as_str().to_owned())
            .collect()
    })
}

/// Which question a reader of the selection lists is asking.
///
/// Every caller of [`TrackedSet::excluded_by_lists`] names one. There is
/// no default: inheriting the watcher's permissive answer by omission is
/// how synchronization came to delete a file another machine had merely
/// stopped selecting.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Asked {
    /// Is this path itself selected? What a capture stores and what
    /// retention keeps.
    Exactly,
    /// Could this path matter? What the watcher wakes for.
    Possibly,
}

/// How a `[history]` pattern list is matched.
///
/// **A pattern with no path separator matches any single path
/// component, so `cache` matches a file named `cache` and everything
/// inside a directory named `cache`. A pattern that is absolute after `~` and environment expansion matches
/// the file's absolute path, both as written and normalized exactly as
/// tracked paths are, so it still matches through a symlinked ancestor.
/// Any other pattern is relative: it matches at any depth, as if written
/// with a leading `**/`, and is never resolved against the working
/// directory. A leading `./` is ignored. On Windows both `/` and `\`
/// count as separators.**
///
/// Within the list the last matching pattern decides and a leading `!`
/// negates, so `!glob` re-includes what an earlier glob excluded.
///
/// Each case answers a way the previous matcher failed. An absolute
/// pattern has to be normalized the way the walk normalizes what it
/// captures, or a pattern naming the live `~/…` location silently misses
/// whenever an ancestor is a symlink. `\` counts as a separator on
/// Windows because that is what `display_path` writes, so a path copied
/// out of mise's own output would otherwise be read back as a file-name
/// glob. A relative pattern is neither resolved nor left to fail:
/// resolving it would bind `**/*.log` to whichever directory the command
/// ran in, and matching it as written against an absolute path would
/// make `keys/**` match nothing at all while the config looked correct.
/// A leading `./` is ignored, so `./keys/**` means what `keys/**` means.
///
/// This is the matcher for the global `[history] exclude` list. A
/// tracked entry's own `exclude` list is a separate thing — patterns
/// there are relative to the entry and use
/// [`crate::system::files::is_excluded`] — but both read a
/// separator-free pattern as naming any path component, so the two lists
/// agree about what `cache` means.
///
/// The builtin credential heuristic deliberately does not read patterns
/// this way: [`is_builtin_credential`] tests the file name alone. The
/// two questions are different. Exclusion asks whether a *path* should
/// be saved, which a directory can answer for everything below it.
/// The guard asks whether a *file* is a credential store, which is a
/// property of its own name — a directory called `oauth` or
/// `token-cache` says nothing about the files inside it.
#[derive(Debug, Default)]
pub(crate) struct PatternList {
    rules: Vec<PatternRule>,
    /// Rules this matcher cannot use, as `(pattern, reason)`, kept
    /// rather than warned about and forgotten. See
    /// [`ExcludeSet::unusable`].
    unusable: Vec<(String, String)>,
}

/// What a pattern is matched against.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Anchor {
    /// No path separator: any single component of the root-relative path.
    Name,
    /// Absolute after `~` expansion: the path itself or an ancestor.
    Absolute,
    /// Anything else: the root-relative path, at any depth below the
    /// tracked root. Never the absolute path — a `sessions` directory
    /// somewhere above the tracked root is not what `sessions/**` means.
    Relative,
}

#[derive(Debug)]
struct PatternRule {
    matchers: Vec<globset::GlobMatcher>,
    /// For a glob of the form `<P>/**`, a matcher for `<P>` itself. The
    /// glob matches what is under the directory, never the directory
    /// path, so this is what lets the walk recognise the directory and
    /// skip it whole.
    directory_matchers: Vec<globset::GlobMatcher>,
    /// For a negated path rule, the directories its matches must live
    /// under: each glob's literal leading prefix, cut at the last
    /// separator, `/`-separated like everything else the matcher
    /// compares. Empty means the rule could match anywhere.
    reinclude_roots: Vec<String>,
    negated: bool,
    anchor: Anchor,
}

impl PatternRule {
    /// Compiles one already-expanded pattern. `negated` is given, never
    /// read out of the text: the pattern is opaque from here on.
    fn compile(body: &str, negated: bool) -> Result<Self> {
        let anchor = anchor_of(body);
        let globs = if anchor == Anchor::Name {
            vec![body.to_string()]
        } else {
            anchored_globs(body)
        };
        let compile = |glob: &str| -> Result<globset::GlobMatcher> {
            Ok(build_glob(glob, anchor)?.compile_matcher())
        };
        let mut matchers = vec![];
        let mut directory_matchers = vec![];
        for glob in &globs {
            matchers.push(compile(glob)?);
            if let Some(directory) = glob.strip_suffix("/**")
                && !directory.is_empty()
            {
                directory_matchers.push(compile(directory)?);
            }
        }
        let reinclude_roots = if negated && anchor == Anchor::Absolute {
            globs.iter().filter_map(|glob| literal_root(glob)).collect()
        } else {
            vec![]
        };
        Ok(Self {
            matchers,
            directory_matchers,
            reinclude_roots,
            negated,
            anchor,
        })
    }

    /// Whether the rule matches any of `candidates` — the path and its
    /// ancestors down to the tracked root — or, for a name pattern, any
    /// component of the path relative to that root.
    /// `candidates` are the path and its ancestors down to the tracked
    /// root, `relative` the path relative to that root, and `components`
    /// its components — all already written with `/`, because that is
    /// what the patterns were compiled to.
    fn matches_any(&self, candidates: &[String], relative: &[String], components: &[&str]) -> bool {
        match self.anchor {
            Anchor::Absolute => candidates.iter().any(|c| self.is_glob_match(c)),
            Anchor::Relative => relative.iter().any(|r| self.is_glob_match(r)),
            Anchor::Name => components.iter().any(|c| self.is_glob_match(c)),
        }
    }

    fn is_glob_match(&self, candidate: &str) -> bool {
        self.matchers
            .iter()
            .any(|matcher| matcher.is_match(Path::new(candidate)))
    }

    /// Whether the rule covers `dir` as a whole: it matches the directory
    /// itself, or it is a `<P>/**` naming everything under it.
    fn covers_directory(
        &self,
        candidates: &[String],
        relative: &[String],
        components: &[&str],
    ) -> bool {
        if self.matches_any(candidates, relative, components) {
            return true;
        }
        let against: &[String] = match self.anchor {
            Anchor::Absolute => candidates,
            Anchor::Relative => relative,
            // a name glob has no `/`, so it has no `/**` form either
            Anchor::Name => return false,
        };
        against.iter().any(|candidate| {
            self.directory_matchers
                .iter()
                .any(|matcher| matcher.is_match(Path::new(candidate)))
        })
    }
}

/// A relative path as the components a pattern is matched against.
fn path_components(rel: &Path) -> Vec<String> {
    rel.components()
        .map(|component| component.as_os_str().to_string_lossy().into_owned())
        .collect()
}

/// Whether an `include` pattern could select something inside the
/// directory at `components` (relative to the tracked entry).
///
/// **Asked with the same glob semantics selection uses, not with a
/// literal prefix.** `Spoons/*` reaches into `Spoons`, and so do
/// `**/Sky.spoon` and a bare `Sky.spoon`, because a pattern without a
/// separator matches a name at any depth. A prefix comparison sees only
/// the first of those, and the user is then told their pattern selects
/// nothing without being told which pattern.
fn reaches_into(pattern: &str, components: &[String]) -> bool {
    // **Pruning is an optimization and must never change what is
    // selected, so it reads a pattern exactly as the matcher reads it.**
    // On Windows a backslash separates; on unix it is a character in a
    // name or a glob escape, and rewriting it here would invent a
    // directory boundary the matcher does not see — and then skip a
    // directory whose files the list still selects. Same rule as
    // `pattern_relative` and `display_separators`, not a third one.
    let pattern = display_separators(pattern);
    // a name matches a component at any depth, so it can name a file
    // inside any directory
    if !pattern.contains('/') {
        return true;
    }
    let parts: Vec<&str> = pattern.split('/').filter(|part| !part.is_empty()).collect();
    let mut parts = parts.as_slice();
    let mut rest = components;
    loop {
        match (parts.first(), rest.first()) {
            // `**` descends as far as it likes
            (Some(&"**"), _) => return true,
            // One side ran out with everything so far matching, and all
            // three ways that happens reach in: the pattern names
            // something inside the directory, or the directory itself,
            // or an ancestor of it — and a pattern matching a directory
            // takes everything under it.
            (Some(_), None) | (None, _) => return true,
            (Some(part), Some(component)) => {
                // a component pattern that cannot be read is not a
                // mismatch: this answer decides whether a directory is
                // walked at all, so "cannot tell" descends
                let matches = glob::Pattern::new(part)
                    .map(|glob| glob.matches(component))
                    .unwrap_or(true);
                if !matches {
                    return false;
                }
                parts = &parts[1..];
                rest = &rest[1..];
            }
        }
    }
}

/// The one way a pattern body becomes a glob.
///
/// **What validation asks and what compilation does are the same
/// question, so they go through the same builder.** A path glob is
/// gitignore-like: `*` stops at a separator, `**` crosses them. A name
/// glob matches one component, so there is no separator in it to stop
/// at. Building a candidate any other way would let `mise dot exclude`
/// accept a pattern that [`PatternRule::compile`] then drops with a
/// warning — the exclusion the user asked for silently doing nothing.
fn build_glob(glob: &str, anchor: Anchor) -> std::result::Result<Glob, globset::Error> {
    if anchor == Anchor::Name {
        Glob::new(glob)
    } else {
        globset::GlobBuilder::new(glob)
            .literal_separator(true)
            .build()
    }
}

/// The anchor a pattern body compiles under, which decides how its globs
/// are built.
fn anchor_of(body: &str) -> Anchor {
    if !is_path_anchored(body) {
        Anchor::Name
    } else if file::replace_path(Path::new(body)).is_absolute() {
        Anchor::Absolute
    } else {
        Anchor::Relative
    }
}

/// Why this matcher cannot use a pattern, if it cannot.
///
/// Shared with `mise dot exclude`, which refuses such a pattern rather
/// than writing it, so the message a user sees when they type one is the
/// message the loader would have warned about later.
pub(crate) fn unusable_pattern(body: &str) -> Option<String> {
    if body.contains('$') {
        return Some(
            "environment variables are not supported in exclusion patterns; write `~/…` or an absolute path".into(),
        );
    }
    // Every form the pattern is compiled from is checked, not just the
    // last one. An anchored pattern becomes two globs — as written and
    // normalized — and they are not equally valid: a directory literally
    // named `link[x` makes the written form an unclosed character class
    // while its normalized form, through a symlink, has no bracket at
    // all. Checking one of them would accept a pattern that
    // `PatternRule::compile` then drops with a warning, which is the
    // opposite of what this exists to prevent.
    let anchor = anchor_of(body);
    let probes = if anchor == Anchor::Name {
        vec![body.to_string()]
    } else {
        anchored_globs(body)
    };
    probes
        .iter()
        .find_map(|probe| build_glob(probe, anchor).err().map(|err| err.to_string()))
}

/// Whether a pattern names a path rather than a file name. `~` alone is
/// a path, so it is expanded before the question is asked.
fn is_path_anchored(body: &str) -> bool {
    let body = &file::replace_path(Path::new(body))
        .to_string_lossy()
        .into_owned();
    body.contains('/') || (cfg!(windows) && body.contains('\\'))
}

/// The glob texts a path-anchored pattern is compiled from: the path as
/// written, and — when the two differ — its normalized form.
///
/// Both are needed. Normalizing is what lets a pattern naming the live
/// `~/…` location match a file the walk reached through a symlinked
/// ancestor. Keeping the form as written is what lets the same pattern
/// match a path that was never normalized, which is every path a caller
/// hands to [`TrackedSet::would_retain`] and every entry pushed with a
/// raw path — a `/tmp` that is really `/private/tmp`, a Windows 8.3
/// short name. Matching either is right for a list of globs where the
/// two spellings name the same file.
fn anchored_globs(body: &str) -> Vec<String> {
    // `~` is the one expansion a pattern gets, and it decides whether the
    // pattern is absolute, so it happens before anything else is asked
    // about it
    let expanded = file::replace_path(Path::new(body));
    let expanded = expanded.as_path();
    if !expanded.is_absolute() {
        // a leading `./` says nothing a relative pattern does not already
        // say, and keeping it would produce `**/./keys/**`, which matches
        // nothing at all
        let text = separators(
            &expanded
                .components()
                .filter(|component| !matches!(component, std::path::Component::CurDir))
                .collect::<PathBuf>(),
        );
        return vec![if text.starts_with("**/") {
            text
        } else {
            format!("**/{text}")
        }];
    }
    let written = separators(expanded);
    let normalized = separators(&normalize_target(expanded));
    if normalized == written {
        vec![written]
    } else {
        vec![written, normalized]
    }
}

/// The directory a glob's matches must live under: its literal leading
/// part, cut at the last separator. `None` when the glob starts with a
/// metacharacter, and so could match anywhere.
fn literal_root(glob: &str) -> Option<String> {
    let literal = match glob.find(['*', '?', '[', '{']) {
        Some(index) => &glob[..index],
        None => glob,
    };
    let root = literal.rsplit_once('/').map(|(root, _)| root)?;
    (!root.is_empty()).then(|| root.to_string())
}

/// Whether two `/`-separated paths are the same or one contains the
/// other, compared by whole components so `/a/bc` is not below `/a/b`.
fn under_or_above(one: &str, other: &str) -> bool {
    let (shorter, longer) = if one.len() <= other.len() {
        (one, other)
    } else {
        (other, one)
    };
    longer == shorter
        || longer
            .strip_prefix(shorter)
            .is_some_and(|rest| rest.starts_with('/'))
}

/// A path as the patterns are written: `/`-separated on every platform.
fn separators(path: &Path) -> String {
    let text = path.to_string_lossy().into_owned();
    if cfg!(windows) {
        text.replace('\\', "/")
    } else {
        text
    }
}

impl PatternList {
    pub(crate) fn new(patterns: &[String]) -> Result<Self> {
        let mut rules = vec![];
        let mut unusable = vec![];
        for pattern in patterns {
            let (body, negated) = match pattern.strip_prefix('!') {
                Some(rest) => (rest, true),
                None => (pattern.as_str(), false),
            };
            // **A rule this matcher cannot use is recorded, not dropped
            // quietly.** Building the matcher still never fails: a list
            // lives in configuration that was written against an older
            // mise, and a matcher that refuses to build takes the
            // watcher down with it. What must not happen is the capture
            // going ahead without the rule, which is the one case where
            // dropping it broadens the snapshot to exactly the files
            // the rule existed to leave out. So the rule is kept here
            // and [`ExcludeSet::unusable`] hands it to the capture,
            // which refuses. `mise dot exclude` refuses such a pattern
            // at the point the user writes it, so nothing new gets in.
            if let Some(reason) = unusable_pattern(body) {
                unusable.push((pattern.clone(), reason));
                continue;
            }
            match PatternRule::compile(body, negated) {
                Ok(rule) => rules.push(rule),
                Err(err) => unusable.push((pattern.clone(), err.to_string())),
            }
        }
        Ok(Self { rules, unusable })
    }
}

/// What a path is, as far as the caller of the selection predicate can
/// tell.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Kind {
    /// A file, a symlink, or anything else that is not a directory.
    File,
    Directory,
    /// Nothing left to ask: the path has been removed or renamed.
    Unknown,
}

/// The kind to judge `path` as.
///
/// **Nothing in a declaration says which kind an entry is**, and no
/// field is a safe proxy for one: `mode = "track"` covers a file and a
/// directory alike, and an `include` list on an entry that turns out to
/// be a file is reported and kept, not removed from the set, so reading
/// the list as "therefore a directory" gives that entry the wrong
/// answer. The filesystem is asked instead, and a path that is not
/// there has no kind — which is the case [`TrackedSet::dropped`] hands
/// to the reader, because that is the one case where a file's answer
/// and a directory's answer differ and neither is available.
fn kind_of(path: &Path) -> Kind {
    match std::fs::symlink_metadata(path) {
        Ok(meta) if meta.is_dir() => Kind::Directory,
        Ok(_) => Kind::File,
        Err(_) => Kind::Unknown,
    }
}

/// Whether `path` lies inside a repository nested below `owner`'s root.
///
/// **Structure, not selection.** A working tree inside a tracked
/// directory is never captured — its files belong to that repository,
/// and history has none of its objects — so no pattern, in any list, can
/// bring one back. Every reader asks this before it asks what the lists
/// say: the walk when it decides whether to descend, `would_retain`
/// when it decides whether a saved version is still covered, and the
/// watcher, which would otherwise wake for every write inside a
/// checked-out repository it can never save.
fn inside_nested_repository(owner: &TrackedEntry, path: &Path) -> bool {
    path.ancestors()
        .take_while(|ancestor| ancestor.starts_with(&owner.path) && *ancestor != owner.path)
        .any(|ancestor| ancestor.join(".git").exists())
}

/// Which `[history] exclude` rules cannot be used, each named with the
/// file that declares it, or `None` when every rule compiles.
///
/// One wording for both readers: the walk reports it and carries on, and
/// [`TrackedSet::refuse_unusable_exclusions`] turns the same sentence into
/// the refusal a checkpoint gets, so the diagnostic a user is asked to act
/// on says the same thing wherever they meet it.
fn unusable_exclusions(exclude: &ExcludeSet) -> Option<String> {
    let unusable = exclude.unusable();
    if unusable.is_empty() {
        return None;
    }
    let sources: Vec<String> = unusable
        .iter()
        .map(|(pattern, reason)| {
            let files = super::config::exclusion_sources(pattern);
            match files.is_empty() {
                true => format!("{pattern:?}: {reason}"),
                false => format!(
                    "{pattern:?} in {}: {reason}",
                    files
                        .iter()
                        .map(display_path)
                        .collect::<Vec<_>>()
                        .join(", ")
                ),
            }
        })
        .collect();
    Some(format!(
        "[history] exclude cannot be applied: {}",
        sources.join("; ")
    ))
}

/// The matcher a checkpoint's `exclude` list was read with. Bumped only
/// when a change could make a pattern match *less* than it used to, so a
/// replay of an older checkpoint does not conclude a path was absent
/// when the older matcher would have called it excluded.
pub(crate) const MATCHER_VERSION: u32 = 1;

/// Directories mise owns that are never captured.
pub(crate) fn hard_exclusions() -> Vec<PathBuf> {
    let mut dirs: Vec<PathBuf> = [
        *dirs::STATE,
        *dirs::CACHE,
        *dirs::DATA,
        *dirs::INSTALLS,
        *dirs::DOWNLOADS,
        *dirs::PLUGINS,
    ]
    .into_iter()
    .map(normalize)
    .collect();
    dirs.push(normalize(&super::store::store_dir_in(&dirs::STATE)));
    dirs.push(normalize(&global_config_dir()).join(".mise-history"));
    dirs.extend(
        crate::agecrypt::identity_paths()
            .iter()
            .map(|path| normalize(path)),
    );
    dirs.sort();
    dirs.dedup();
    dirs
}

/// The global config directory (where `--adopt` checks out).
pub(crate) fn global_config_dir() -> PathBuf {
    crate::env::MISE_GLOBAL_CONFIG_FILE
        .as_deref()
        .map(|path| {
            path.parent()
                .filter(|parent| !parent.as_os_str().is_empty())
                .unwrap_or_else(|| Path::new("."))
                .to_path_buf()
        })
        .unwrap_or_else(|| dirs::CONFIG.to_path_buf())
}

/// Canonical when the path exists, lexically normalized otherwise.
pub(crate) fn normalize(path: &Path) -> PathBuf {
    let expanded = file::replace_path(path);
    dunce::canonicalize(&expanded).unwrap_or_else(|_| lexical(&expanded))
}

/// Resolve existing ancestors consistently even when the leaf is missing.
/// A symlink leaf is tracked as a link, never as its destination.
pub(crate) fn normalize_target(path: &Path) -> PathBuf {
    let expanded = file::replace_path(path);
    if !file::is_symlink_or_junction(&expanded)
        && let Ok(resolved) = dunce::canonicalize(&expanded)
    {
        return lexical(&resolved);
    }
    let mut tail = Vec::new();
    let mut ancestor = expanded.as_path();
    if let (Some(parent), Some(name)) = (ancestor.parent(), ancestor.file_name()) {
        tail.push(name.to_os_string());
        ancestor = parent;
    }
    loop {
        let candidate = if ancestor.as_os_str().is_empty() {
            Path::new(".")
        } else {
            ancestor
        };
        if let Ok(mut resolved) = dunce::canonicalize(candidate) {
            for component in tail.iter().rev() {
                resolved.push(component);
            }
            return lexical(&resolved);
        }
        let (Some(parent), Some(name)) = (ancestor.parent(), ancestor.file_name()) else {
            return lexical(&expanded);
        };
        tail.push(name.to_os_string());
        ancestor = parent;
    }
}

/// The home directory or above: never walked, never watched.
pub(crate) fn is_refused_root(path: &Path, home: &Path) -> bool {
    path == home || home.starts_with(path) || path.parent().is_none()
}

fn lexical(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => {
                out.pop();
            }
            other => out.push(other),
        }
    }
    out
}

/// Turns a snapshot-tree path (`home/.zshrc`, `fs/etc/hosts`) into the
/// display form (`~/.zshrc`, `/etc/hosts`).
pub(crate) fn tree_path_to_display(tree_path: &str) -> String {
    let (stem, rest) = tree_path.split_once('/').unwrap_or((tree_path, ""));
    let root = stem.split('@').next().unwrap_or(stem);
    if root == "config" {
        display_path(global_config_dir().join(rest))
    } else if root == "home" {
        format!("~/{rest}")
    } else if let Some(rest) = tree_path.strip_prefix("fs/") {
        format!("/{rest}")
    } else {
        tree_path.to_string()
    }
}

/// Root aliases are portable through the home/config mapping. Aliases below
/// those roots are not: canonicalizing them would silently change the enrolled
/// destination on another machine. The leaf itself may still be a symlink.
/// The entry that owns `path`: the most specific one it lies under.
///
/// One rule, one place. Capture, `mise dot save <path>`, the watcher and
/// a replay all have to agree about which entry owns a file, because
/// each reads that entry's own lists and uses its path as the root the
/// global patterns are measured against. Ranking by byte length instead
/// of component count picks a different entry whenever a shallower path
/// simply has a longer name, and the two readings then disagree about
/// what a checkpoint covers.
pub(crate) fn owning_entry<'a>(
    entries: &'a [TrackedEntry],
    path: &Path,
) -> Option<&'a TrackedEntry> {
    owning_entry_index(entries, path).map(|index| &entries[index])
}

/// Where [`owning_entry`]'s answer sits in `entries`.
///
/// **The search hands back the index; nothing looks the entry up again.**
/// A second search — by identity or by value — can come back empty, and
/// an empty answer here does not read as "something went wrong", it reads
/// as "no entry covers this path". That turns a covered path into an
/// uncovered one, and every selection and coverage decision downstream
/// then goes the other way.
pub(crate) fn owning_entry_index(entries: &[TrackedEntry], path: &Path) -> Option<usize> {
    entries
        .iter()
        .enumerate()
        .filter(|(_, entry)| path.starts_with(&entry.path))
        .max_by_key(|(_, entry)| entry.path.components().count())
        .map(|(index, _)| index)
}

/// The most specific of `items` that `path` lies under, for the display
/// paths a checkpoint or a manifest records. The same rule as
/// [`owning_entry`], counting components rather than bytes.
pub(crate) fn owning_display<'a, T: 'a>(
    items: impl IntoIterator<Item = &'a T>,
    path: &str,
    key: impl Fn(&T) -> &str,
) -> Option<&'a T> {
    items
        .into_iter()
        .filter(|item| display_under(path, key(item)))
        // **Normalized once, then used for everything after.** A
        // recorded display path keeps the separator of the host that
        // wrote it, so `~\.config\mise` counted as components on a unix
        // reader is one component, not three — and the most specific
        // entry would lose to a shallower one. The comparison above
        // already reads both spellings as the same path; the ranking has
        // to read them the same way too.
        .max_by_key(|item| display_depth(key(item)))
}

/// How deep a display path is, whichever host's separator it carries.
fn display_depth(path: &str) -> usize {
    display_separators(path)
        .split('/')
        .filter(|component| !component.is_empty() && *component != ".")
        .count()
}

/// The permission key under which the enrollment this machine selects
/// governs a path: the stream of the closest enrolled path at or above it,
/// else, for a directory with enrolled paths inside it, the variant-less
/// containing key; else none. Every reader of directory modes goes through
/// this, so what a pull applies, what a checkpoint records, and what the
/// next pull assumes are one definition.
pub(crate) fn governing_key(
    roots: &super::sync::layout::Roots,
    entries: &[TrackedEntry],
    path: &Path,
) -> Option<String> {
    let owner = owning_entry(entries, path);
    match owner {
        Some(entry) => roots.branch_path(path, entry.variant.as_deref()),
        None if entries
            .iter()
            .any(|entry| entry.path.starts_with(path) && entry.path != path) =>
        {
            roots.branch_path(path, None)
        }
        None => None,
    }
}

/// The mode a manifest wants for a directory: its record under the
/// governing key, absent meaning the default; nothing when no enrollment
/// this machine selects covers the directory.
pub(crate) fn mode_from(
    roots: &super::sync::layout::Roots,
    entries: &[TrackedEntry],
    permissions: &BTreeMap<String, u32>,
    path: &Path,
) -> Option<u32> {
    let key = governing_key(roots, entries, path)?;
    Some(permissions.get(&key).copied().unwrap_or(0o755))
}

pub(crate) fn ensure_portable_ancestors(path: &Path) -> Result<()> {
    eyre::ensure!(
        path.to_str().is_some(),
        "tracking does not support non-UTF-8 filenames"
    );
    let roots = super::sync::layout::Roots::current();
    let bases = [
        dirs::HOME.to_path_buf(),
        global_config_dir(),
        roots.home,
        roots.config_dir,
    ];
    let relative = |base: &Path| {
        let mut components = path.components();
        for expected in base.components() {
            let actual = components.next()?;
            if actual != expected
                && !(cfg!(windows)
                    && actual
                        .as_os_str()
                        .to_string_lossy()
                        .eq_ignore_ascii_case(&expected.as_os_str().to_string_lossy()))
            {
                return None;
            }
        }
        Some(components.collect::<PathBuf>())
    };
    let Some((base, rest)) = bases
        .iter()
        .filter_map(|base| relative(base).map(|rest| (base, rest)))
        .max_by_key(|(base, _)| base.components().count())
    else {
        eyre::bail!(
            "tracking requires a portable path under home or the mise configuration directory"
        );
    };
    let mut ancestor = base.clone();
    for component in rest
        .components()
        .take(rest.components().count().saturating_sub(1))
    {
        ancestor.push(component);
        if file::is_symlink_or_junction(&ancestor) {
            eyre::bail!(
                "cannot track {} through symlinked parent {}; explicitly track the link itself and its real target instead",
                display_path(path),
                display_path(&ancestor)
            );
        }
    }
    Ok(())
}

/// Turns a display or absolute path into its snapshot-tree path.
pub(crate) fn display_to_tree_path(path: &str) -> String {
    // the link itself, never its destination: a tracked symlink is captured
    // as a link and addressed as one
    let expanded = normalize_target(Path::new(path));
    let config = normalize(&global_config_dir());
    if let Ok(relative) = expanded.strip_prefix(config) {
        return format!("config/{}", relative.to_string_lossy().replace('\\', "/"))
            .trim_end_matches('/')
            .to_owned();
    }
    let home = normalize(&dirs::HOME);
    match expanded.strip_prefix(&home) {
        Ok(rel) if !rel.as_os_str().is_empty() => {
            format!("home/{}", rel.to_string_lossy().replace('\\', "/"))
        }
        Ok(_) => "home".to_string(),
        Err(_) => {
            let rel: Vec<String> = expanded
                .components()
                .filter_map(|component| match component {
                    Component::Normal(part) => Some(part.to_string_lossy().to_string()),
                    _ => None,
                })
                .collect();
            format!("fs/{}", rel.join("/"))
        }
    }
}

#[cfg(test)]
mod tests {
    /// Tracked root for the matcher tests: ancestors stop here.
    const ROOT: &str = "/nonexistent-mise-test";
    use super::*;

    #[cfg(unix)]
    #[test]
    fn enrollment_rejects_alias_parents_but_allows_links_and_real_targets() -> Result<()> {
        let roots = super::super::sync::layout::Roots::current();
        let temp = tempfile::tempdir_in(&roots.home)?;
        let real = temp.path().join("real");
        let alias = temp.path().join("alias");
        std::fs::create_dir(&real)?;
        std::fs::write(real.join("config"), "native")?;
        std::os::unix::fs::symlink("real", &alias)?;
        assert!(ensure_portable_ancestors(&alias).is_ok());
        assert!(ensure_portable_ancestors(&real.join("config")).is_ok());
        assert!(ensure_portable_ancestors(&alias.join("config")).is_err());
        assert!(ensure_portable_ancestors(&alias.join("missing/child")).is_err());
        let manifest = super::super::manifest::Manifest {
            enrollment: vec![super::super::manifest::Enrollment {
                path: roots.branch_path(&alias.join("config"), None).unwrap(),
                autosave: true,
                encrypt: false,
                variants: vec![],
                exclude: None,
                include: None,
            }],
            ..Default::default()
        };
        assert!(manifest.tracking().is_err());
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn missing_descendants_keep_their_resolved_identity() {
        let temp = tempfile::tempdir().unwrap();
        let real = temp.path().join("real");
        std::fs::create_dir_all(real.join("nested")).unwrap();
        let alias = temp.path().join("alias");
        std::os::unix::fs::symlink(&real, &alias).unwrap();
        let path = alias.join("nested/file");
        std::fs::write(&path, "contents").unwrap();
        let before = normalize_target(&path);
        std::fs::remove_file(&path).unwrap();
        std::fs::remove_dir(real.join("nested")).unwrap();
        assert_eq!(normalize_target(&path), before);
        assert_eq!(
            normalize_target(&alias),
            normalize(temp.path()).join("alias")
        );
    }

    /// A preview walks the target and nothing else, while the set still
    /// holds every declaration so ownership is decided the way a capture
    /// decides it.
    #[test]
    fn a_selected_walk_visits_only_what_was_asked_for() {
        let tmp = tempfile::tempdir().unwrap();
        let other = tmp.path().join("other");
        let target = tmp.path().join("target");
        std::fs::create_dir_all(other.join("deep")).unwrap();
        std::fs::create_dir_all(&target).unwrap();
        std::fs::write(other.join("deep/one.toml"), "other").unwrap();
        std::fs::write(target.join("two.toml"), "target").unwrap();

        let mut set = TrackedSet::default();
        set.push(entry(&other));
        set.push(entry(&target));
        let index = set.entry_index_for(&target).unwrap();

        let all = set.walk().unwrap();
        assert!(all.files.contains_key(&other.join("deep/one.toml")));
        assert!(all.files.contains_key(&target.join("two.toml")));

        let selected = set.walk_selected(&[index]).unwrap();
        assert!(
            !selected.files.contains_key(&other.join("deep/one.toml")),
            "an entry nobody asked about was walked"
        );
        assert!(selected.files.contains_key(&target.join("two.toml")));
        // and the answer about the target is the same one a full walk
        // gives, because ownership was decided from the whole set
        assert_eq!(
            selected.preview_of(&set, index).files,
            all.preview_of(&set, index).files
        );
    }

    /// A tracked directory inside another tracked directory is walked
    /// by its own entry, once — not descended into twice and then
    /// discarded file by file.
    #[test]
    fn an_entry_inside_another_is_not_walked_twice() {
        let tmp = tempfile::tempdir().unwrap();
        let outer = tmp.path().join("config");
        let inner = outer.join("nvim");
        std::fs::create_dir_all(inner.join("lua")).unwrap();
        std::fs::write(outer.join("outer.toml"), "outer").unwrap();
        for i in 0..20 {
            std::fs::write(inner.join(format!("lua/{i}.lua")), "inner").unwrap();
        }

        let mut set = TrackedSet::default();
        set.push(entry(&outer));
        set.push(entry(&inner));
        let outer_index = set.entry_index_for(&outer).unwrap();
        let inner_index = set.entry_index_for(&inner).unwrap();

        // the files land under the entry that owns them, exactly as
        // before: what changed is only how the other entry got there
        let walk = set.walk().unwrap();
        assert_eq!(walk.files[&outer.join("outer.toml")].0, outer_index);
        assert_eq!(walk.files[&inner.join("lua/3.lua")].0, inner_index);
        assert_eq!(walk.files.len(), 21);

        // and walking the outer entry alone reaches only its own file
        let selected = set.walk_selected(&[outer_index]).unwrap();
        assert_eq!(
            selected.files.keys().collect::<Vec<_>>(),
            vec![&outer.join("outer.toml")]
        );
    }

    /// A pattern is written with `/` on every platform; the path it is
    /// matched against is not. Capture, dry run and replay all match
    /// through `excluded_by_entry`, so this is where the two meet.
    #[test]
    fn an_entry_list_matches_a_path_with_the_host_separator() {
        let root = PathBuf::from(if cfg!(windows) {
            "C:\\Users\\me\\.codex"
        } else {
            "/home/me/.codex"
        });
        let patterns = ["cache/**".to_string()];
        assert!(excluded_by_entry(
            &root,
            &patterns,
            &root.join("cache").join("index"),
        ));
        assert!(!excluded_by_entry(
            &root,
            &patterns,
            &root.join("config.toml"),
        ));
        // on unix a backslash is a character in a filename, not a
        // separator, so one file named `cache\index` is not a file
        // `index` inside `cache`
        #[cfg(unix)]
        assert!(!excluded_by_entry(
            &root,
            &patterns,
            &root.join("cache\\index"),
        ));
    }

    /// What the CLI accepts is what the matcher compiles. The two asked
    /// the same question through different builders once, which is how a
    /// pattern gets accepted at the prompt and then dropped with a
    /// warning at load — the exclusion silently doing nothing.
    #[test]
    fn every_pattern_the_cli_accepts_compiles() {
        for body in [
            "cache",
            "*.log",
            "!*.log",
            "sessions/**",
            "~/.codex/sessions/**",
            "./rules/*.md",
            "a[bc]d",
            "**/node_modules",
            "{a,b}/**",
        ] {
            let negated = body.starts_with('!');
            let rule = body.strip_prefix('!').unwrap_or(body);
            assert_eq!(
                unusable_pattern(rule).is_none(),
                PatternRule::compile(rule, negated).is_ok(),
                "the CLI and the matcher disagree about {body:?}"
            );
        }
        // and one the matcher cannot compile is refused rather than
        // accepted and dropped
        assert!(unusable_pattern("rules/[unclosed/**").is_some());
        assert!(PatternRule::compile("rules/[unclosed/**", false).is_err());
    }

    #[test]
    fn replay_versions_only_the_patterns_that_can_affect_a_path() {
        use crate::system::history::replay::{PathState, classify_coverage};
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("plain");
        let other = tmp.path().join("filtered");
        std::fs::create_dir(&root).unwrap();
        std::fs::create_dir(&other).unwrap();
        let mut set = TrackedSet::default();
        set.push(entry(&root));
        let mut filtered = entry(&other);
        filtered.exclude = Some(vec!["private/**".into()]);
        set.push(filtered);
        let mut coverage = set.coverage(&set.walk().unwrap());
        coverage.matcher = Some(MATCHER_VERSION + 1);
        let display = display_path(root.join("new.txt"));
        assert!(matches!(
            classify_coverage(&coverage, &display),
            PathState::Absent
        ));
        let index = coverage
            .entries
            .iter()
            .position(|entry| entry.path == display_path(&root))
            .unwrap();
        coverage.entries[index].exclude = Some(vec![]);
        assert!(matches!(
            classify_coverage(&coverage, &display),
            PathState::Absent
        ));
        coverage.entries[index].exclude = Some(vec!["private/**".into()]);
        assert!(matches!(
            classify_coverage(&coverage, &display),
            PathState::Unevaluable(_)
        ));
    }

    #[cfg(unix)]
    #[test]
    fn replay_skips_exclusions_made_unusable_by_a_changed_symlink() {
        use crate::system::history::replay::{PathState, classify_coverage};
        let tmp = tempfile::tempdir().unwrap();
        let temp_root = tmp.path().canonicalize().unwrap();
        let root = temp_root.join("real");
        let bad = temp_root.join("invalid[");
        std::fs::create_dir(&root).unwrap();
        std::fs::create_dir(&bad).unwrap();
        let local = root.join("private.txt");
        std::fs::write(&local, "keep locally").unwrap();
        let link = temp_root.join("link");
        std::os::unix::fs::symlink(&root, &link).unwrap();
        let mut set = TrackedSet {
            exclude: vec![format!("{}/**", link.display())],
            ..Default::default()
        };
        set.push(entry(&root));
        assert!(set.exclude_set().unwrap().unusable().is_empty());
        let walk = set.walk().unwrap();
        assert!(!walk.files.contains_key(&local));
        let coverage = set.coverage(&walk);
        assert!(matches!(
            classify_coverage(&coverage, &display_path(&local)),
            PathState::Uncovered
        ));

        std::fs::remove_file(&link).unwrap();
        std::os::unix::fs::symlink(&bad, &link).unwrap();
        assert!(!set.exclude_set().unwrap().unusable().is_empty());
        let state = classify_coverage(&coverage, &display_path(&local));
        assert!(matches!(state, PathState::Unevaluable(_)));
        assert!(state.skip_reason("0123456789").is_some());
    }

    /// A pattern is compiled from more than one glob when it is
    /// anchored, and the refusal has to cover all of them: a form that
    /// only `PatternRule::compile` rejects is dropped with a warning long
    /// after `mise dot exclude` accepted the pattern.
    #[cfg(unix)]
    #[test]
    fn a_pattern_is_refused_when_any_form_it_compiles_to_is_unusable() {
        let tmp = tempfile::tempdir().unwrap();
        std::fs::create_dir(tmp.path().join("real")).unwrap();
        // a directory whose real name opens a character class that the
        // path it resolves to does not
        let link = tmp.path().join("link[x");
        std::os::unix::fs::symlink(tmp.path().join("real"), &link).unwrap();
        let body = format!("{}/**", link.display());

        let forms = anchored_globs(&body);
        assert_eq!(forms.len(), 2, "expected two forms, got {forms:?}");
        assert!(
            Glob::new(&forms[1]).is_ok(),
            "the normalized form is the usable one: {forms:?}"
        );
        assert!(
            unusable_pattern(&body).is_some(),
            "the written form is unusable, so the pattern is refused: {forms:?}"
        );
    }

    /// A recorded display path carries the separator of the host that
    /// wrote it, and the most specific entry has to win on either host —
    /// the comparison and the ranking must read the same path the same
    /// way.
    #[test]
    fn the_owner_of_a_recorded_path_is_the_same_on_either_host() {
        struct Recorded(&'static str);
        for (entries, path, expected) in [
            (
                vec![Recorded("~/.config"), Recorded("~/.config/mise")],
                "~/.config/mise/config.toml",
                "~/.config/mise",
            ),
            // the same set spelled the way `display_path` writes it on
            // Windows, where both characters separate and the two
            // spellings mix within one host
            #[cfg(windows)]
            (
                vec![Recorded("~\\.config"), Recorded("~\\.config\\mise")],
                "~\\.config\\mise\\config.toml",
                "~\\.config\\mise",
            ),
            #[cfg(windows)]
            (
                vec![Recorded("~/.config"), Recorded("~\\.config\\mise")],
                "~/.config/mise/config.toml",
                "~\\.config\\mise",
            ),
            // on unix a backslash is part of a name, so this entry is one
            // directory called `.config\mise` and owns nothing under
            // `~/.config`
            #[cfg(unix)]
            (
                vec![Recorded("~/.config"), Recorded("~/.config\\mise")],
                "~/.config/mise/config.toml",
                "~/.config",
            ),
        ] {
            let owner = owning_display(&entries, path, |entry| entry.0);
            assert_eq!(
                owner.map(|entry| entry.0),
                Some(expected),
                "{path} under {:?}",
                entries.iter().map(|entry| entry.0).collect::<Vec<_>>()
            );
        }
    }

    /// The walk steps past a tracked directory's own root and filters
    /// what is inside, so a bare global pattern equal to that
    /// directory's name must not make retention or the watcher call the
    /// entry excluded — and removing the entry must still be noticed,
    /// when there is no longer anything to say what kind it was.
    #[test]
    fn a_global_pattern_matching_an_entrys_own_name_does_not_exclude_it() {
        let tmp = tempfile::tempdir().unwrap();
        let directory = tmp.path().join("cache");
        std::fs::create_dir_all(&directory).unwrap();
        std::fs::write(directory.join("kept.toml"), "keep").unwrap();
        let file = tmp.path().join("notes.md");
        std::fs::write(&file, "keep").unwrap();

        let mut set = TrackedSet {
            exclude: vec!["cache".to_string(), "notes.md".to_string()],
            ..Default::default()
        };
        set.push(entry(&directory));
        set.push(entry(&file));
        let exclude = set.exclude_set().unwrap();

        // the directory entry: walked, so its root is not judged
        assert!(!set.excluded_by_lists(&exclude, &directory, Asked::Possibly));
        assert!(set.would_retain(&directory).unwrap());
        assert!(
            set.walk()
                .unwrap()
                .files
                .contains_key(&directory.join("kept.toml"))
        );
        // and a file entry is judged, exactly as the walk judges it
        assert!(set.excluded_by_lists(&exclude, &file, Asked::Possibly));
        assert!(!set.would_retain(&file).unwrap());
        assert!(!set.walk().unwrap().files.contains_key(&file));

        // removing a tracked entry is a change history has to notice,
        // and once it is gone there is nothing to ask what kind it was
        std::fs::remove_dir_all(&directory).unwrap();
        std::fs::remove_file(&file).unwrap();
        assert!(
            !set.excluded_by_lists(&exclude, &directory, Asked::Possibly),
            "the removal of a tracked directory was ignored"
        );
        assert!(
            !set.excluded_by_lists(&exclude, &file, Asked::Possibly),
            "the removal of a tracked file was ignored"
        );
    }

    /// A list selects paths inside a tracked directory. An entry that is
    /// itself a file has nothing inside it, so any list it declares
    /// leaves it out — one answer for the empty list and the non-empty
    /// one alike, and the same answer from the capture walk, the
    /// watcher's filter and a replay reading the recorded list.
    #[test]
    fn an_include_list_on_a_file_entry_selects_nothing() {
        let tmp = tempfile::tempdir().unwrap();
        let file = tmp.path().join("config.toml");
        std::fs::write(&file, "keep").unwrap();

        for include in [None, Some(vec![]), Some(vec!["config.toml".to_string()])] {
            let declared = include.is_some();
            let mut tracked = entry(&file);
            tracked.include = include.clone();
            let mut set = TrackedSet::default();
            set.push(tracked);

            let walk = set.walk().unwrap();
            assert_eq!(
                walk.files.contains_key(&file),
                !declared,
                "capture with include = {include:?}"
            );
            assert_eq!(
                set.would_retain(&file).unwrap(),
                !declared,
                "would_retain with include = {include:?}"
            );
            // the entry is a file, so the watcher asks the same question
            // the capture does and gets the same answer
            assert_eq!(
                !set.excluded_by_lists(&set.exclude_set().unwrap(), &file, Asked::Possibly),
                !declared,
                "watcher with include = {include:?}"
            );
            assert!(
                !included_by_entry(&file, include.as_deref().unwrap_or_default(), &file),
                "replay with include = {include:?}"
            );
            // and the drop is reported rather than silent
            assert_eq!(
                walk.omitted
                    .iter()
                    .any(|omitted| omitted.reason.contains("selects nothing")),
                declared,
                "reported with include = {include:?}"
            );
        }
    }

    /// **A directory is covered when something beneath it is, although
    /// no pattern ever selects a directory.** Asking a directory "are
    /// you selected" made every reader call a tracked tree uncaptured:
    /// `mise dot track` warned that a symlink's source was not in
    /// history while the capture was saving its files, and a rollback
    /// called the subdirectories a capture still walks uncovered.
    #[test]
    fn a_directory_is_covered_by_what_its_include_list_selects_below_it() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("rules/deep")).unwrap();
        std::fs::create_dir_all(root.join("sessions")).unwrap();
        std::fs::write(root.join("rules/one.md"), "keep").unwrap();
        std::fs::write(root.join("sessions/a.jsonl"), "drop").unwrap();
        let mut tracked = entry(&root);
        tracked.include = Some(vec!["rules/**".to_string()]);
        let mut set = TrackedSet::default();
        set.push(tracked);

        // the tree the list selects into, and every directory on the way
        for directory in [root.clone(), root.join("rules"), root.join("rules/deep")] {
            assert!(
                set.would_capture(&directory).unwrap(),
                "would_capture {}",
                directory.display()
            );
            assert!(
                set.would_retain(&directory).unwrap(),
                "would_retain {}",
                directory.display()
            );
        }
        // and one no pattern can reach into is not
        assert!(!set.would_capture(&root.join("sessions")).unwrap());

        // files still answer as files: strict, from the patterns, with
        // no stat of their own
        assert!(set.would_retain(&root.join("rules/one.md")).unwrap());
        assert!(!set.would_retain(&root.join("sessions/a.jsonl")).unwrap());

        // an unreadable file the list does not select is still not
        // retained: unreadable is not unknown
        let unselected = root.join("sessions/a.jsonl");
        assert!(!set.would_retain(&unselected).unwrap());

        // a selected directory that has just vanished still wakes the
        // watcher, because a deleted tree is a change history must see
        let exclude = set.exclude_set().unwrap();
        std::fs::remove_dir_all(root.join("rules")).unwrap();
        assert!(!set.excluded_by_lists(&exclude, &root.join("rules"), Asked::Possibly));
    }

    /// The credential guard is lifted by a pattern that selected the
    /// file, never by the presence of a list. An entry that is itself a
    /// credential-named file has nothing inside it for a pattern to
    /// name, so no list it declares can lift its guard.
    #[test]
    fn a_list_on_a_credential_file_entry_does_not_lift_its_guard() {
        let tmp = tempfile::tempdir().unwrap();
        let key = tmp.path().join("id_rsa");
        std::fs::write(&key, "key").unwrap();
        for include in [
            None,
            Some(vec![]),
            Some(vec!["id_rsa".to_string()]),
            Some(vec!["**".to_string()]),
        ] {
            let mut tracked = entry(&key);
            tracked.include = include.clone();
            assert_eq!(
                tracked.capture_exclusion(&key),
                Some(CREDENTIAL_REASON),
                "include = {include:?} lifted the guard on the entry itself"
            );
        }
        // inside a directory entry a pattern can name it, and then the
        // selection decides
        let dir = tmp.path().join("ssh");
        std::fs::create_dir(&dir).unwrap();
        let inside = dir.join("id_rsa");
        std::fs::write(&inside, "key").unwrap();
        let mut tracked = entry(&dir);
        assert_eq!(tracked.capture_exclusion(&inside), Some(CREDENTIAL_REASON));
        tracked.include = Some(vec!["id_rsa".to_string()]);
        assert_eq!(tracked.capture_exclusion(&inside), None);
    }

    /// A repository inside a tracked directory is skipped whatever the
    /// include list says, and the explanation names the pattern that
    /// reached into it — found with the same glob semantics selection
    /// uses, not a literal prefix.
    #[test]
    fn a_nested_repository_is_skipped_and_the_pattern_that_reached_in_is_named() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("hammerspoon");
        let plugin = root.join("Spoons/Sky.spoon");
        std::fs::create_dir_all(plugin.join(".git")).unwrap();
        std::fs::write(root.join("init.lua"), "keep").unwrap();
        std::fs::write(plugin.join("init.lua"), "theirs").unwrap();

        for (patterns, named) in [
            (vec!["Spoons/*"], true),
            (vec!["**/init.lua"], true),
            (vec!["init.lua"], true),
            (vec!["Spoons/Sky.spoon/**"], true),
        ] {
            let mut tracked = entry(&root);
            tracked.include = Some(patterns.iter().map(|p| (*p).to_string()).collect());
            let mut set = TrackedSet::default();
            set.push(tracked);
            let walk = set.walk().unwrap();
            assert!(
                !walk.files.contains_key(&plugin.join("init.lua")),
                "{patterns:?} captured a file inside a nested repository"
            );
            let reported = walk
                .nested
                .iter()
                .find(|nested| nested.path.ends_with("Sky.spoon"))
                .unwrap_or_else(|| panic!("{patterns:?}: the repository was not reported"));
            assert_eq!(
                reported.reason.contains("selects nothing inside it"),
                named,
                "{patterns:?}: {}",
                reported.reason
            );
        }
    }

    /// The promise of the feature, end to end: one named file beside a
    /// directory of noise is captured, the noise is not walked, and the
    /// scan limits — which count what is captured — are nowhere near.
    #[test]
    fn a_narrow_include_list_does_not_walk_what_it_leaves_out() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("rules")).unwrap();
        std::fs::create_dir_all(root.join("sessions/deep")).unwrap();
        std::fs::write(root.join("rules/one.md"), "keep").unwrap();
        for i in 0..40 {
            std::fs::write(root.join(format!("sessions/{i}.jsonl")), "noise").unwrap();
            std::fs::write(root.join(format!("sessions/deep/{i}.jsonl")), "noise").unwrap();
        }

        let mut tracked = entry(&root);
        tracked.include = Some(vec!["rules/**".to_string()]);
        let mut set = TrackedSet::default();
        set.push(tracked);
        let index = set.entry_index_for(&root).unwrap();
        let walk = set.walk().unwrap();

        assert_eq!(
            walk.files.keys().collect::<Vec<_>>(),
            vec![&root.join("rules/one.md")]
        );
        // the 80 files under `sessions` were never examined, let alone
        // counted: `considered` only counts what the walk reached
        assert!(
            walk.considered.get(&index).copied().unwrap_or(0) <= 2,
            "the walk descended into what the list leaves out: {:?}",
            walk.considered
        );
        assert!(walk.incomplete.is_empty(), "{:?}", walk.incomplete);
    }

    /// The watcher must wake on the tracked directory itself when the
    /// list selects something inside it, while a capture still declines
    /// to store the directory as a file — and retention, which asks the
    /// patterns alone, answers about the files.
    #[test]
    fn a_directory_whose_children_are_selected_is_watched() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("rules")).unwrap();
        std::fs::write(root.join("rules/one.md"), "keep").unwrap();
        std::fs::write(root.join("notes.md"), "noise").unwrap();

        let mut tracked = entry(&root);
        tracked.include = Some(vec!["rules/**".to_string()]);
        let mut set = TrackedSet::default();
        set.push(tracked);
        let exclude = set.exclude_set().unwrap();
        let exclude_anchored = set.exclude_set().unwrap();

        // the entry directory and the directory the list reaches into:
        // an event on either has to be looked at
        for directory in [root.clone(), root.join("rules")] {
            assert!(
                !set.excluded_by_lists(&exclude, &directory, Asked::Possibly),
                "the watcher ignored {}",
                directory.display()
            );
        }
        // a directory nothing could select is still dropped
        std::fs::create_dir(root.join("sessions")).unwrap();
        assert!(set.excluded_by_lists(&exclude, &root.join("sessions"), Asked::Possibly));

        // **and the files inside one the patterns can reach are still
        // judged one by one.** A list of names reaches into every
        // directory, which must not turn every file under the entry into
        // something the watcher wakes for — that is the noise the list
        // was written to stop.
        let mut by_name = entry(&root);
        by_name.include = Some(vec!["config.toml".to_string()]);
        let mut named = TrackedSet::default();
        named.push(by_name);
        let exclude = named.exclude_set().unwrap();
        std::fs::create_dir_all(root.join("sessions")).unwrap();
        std::fs::write(root.join("sessions/one.jsonl"), "noise").unwrap();
        std::fs::write(root.join("config.toml"), "keep").unwrap();
        assert!(
            named.excluded_by_lists(&exclude, &root.join("sessions/one.jsonl"), Asked::Possibly),
            "the watcher woke for a transcript the list does not select"
        );
        assert!(!named.excluded_by_lists(&exclude, &root.join("config.toml"), Asked::Possibly));
        // a deletion leaves nothing to stat, and a selected file that
        // was deleted is still a change worth capturing
        std::fs::remove_file(root.join("config.toml")).unwrap();
        assert!(!named.excluded_by_lists(&exclude, &root.join("config.toml"), Asked::Possibly));
        // the directory a name pattern could match in is still watched
        assert!(!named.excluded_by_lists(&exclude, &root.join("sessions"), Asked::Possibly));

        // **a directory that is gone counts as what it could have
        // been.** An anchored list never selects the directory itself,
        // so reading a vanished path as a file would let a removed tree
        // pass unnoticed and leave history claiming files that no longer
        // exist.
        std::fs::remove_dir_all(root.join("rules")).unwrap();
        assert!(
            !set.excluded_by_lists(&exclude_anchored, &root.join("rules"), Asked::Possibly),
            "a removed directory of selected files was ignored"
        );
        std::fs::remove_dir_all(root.join("sessions")).unwrap();
        assert!(
            set.excluded_by_lists(&exclude_anchored, &root.join("sessions"), Asked::Possibly),
            "a removed directory nothing could select woke the watcher"
        );

        // and the files are decided exactly, by both
        assert!(set.would_retain(&root.join("rules/one.md")).unwrap());
        assert!(!set.would_retain(&root.join("notes.md")).unwrap());
        assert!(!set.excluded_by_lists(&exclude, &root.join("rules/one.md"), Asked::Possibly));
        assert!(set.excluded_by_lists(&exclude, &root.join("notes.md"), Asked::Possibly));
    }

    /// What a pattern selects decides what may be skipped, so the two
    /// have to say the same thing about a directory.
    #[test]
    fn a_pattern_that_selects_below_a_directory_reaches_into_it() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("rules/deep")).unwrap();
        std::fs::write(root.join("rules/one.md"), "keep").unwrap();
        std::fs::write(root.join("rules/deep/two.md"), "keep").unwrap();

        // every one of these matches the directory `rules/deep` or an
        // ancestor of it, and **a pattern matching a directory takes
        // everything under it** — so all three select the file, and none
        // of them may prune the directory
        for (pattern, selects_deep) in [
            ("rules", true),
            ("rules/**", true),
            ("rules/*", true),
            ("sessions/**", false),
        ] {
            let mut tracked = entry(&root);
            tracked.include = Some(vec![pattern.to_string()]);
            let deep = root.join("rules/deep/two.md");
            assert_eq!(
                tracked.is_included(&deep),
                selects_deep,
                "{pattern} selecting {}",
                deep.display()
            );
            // and the walk must not skip a directory whose contents the
            // same pattern selects
            assert_eq!(
                tracked.include_prunes(&root.join("rules/deep")),
                !selects_deep,
                "{pattern} pruning rules/deep"
            );
        }
    }

    /// **Retention asks the patterns, not the filesystem.** A file the
    /// include list no longer selects is not carried forward because it
    /// happened to be unreadable when the save ran; one the list still
    /// selects is.
    #[test]
    fn retention_keeps_what_the_list_still_selects() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("rules")).unwrap();

        let mut tracked = entry(&root);
        tracked.include = Some(vec!["rules/**".to_string()]);
        let mut set = TrackedSet::default();
        set.push(tracked);

        // neither file exists — this is the save that cannot read them
        let selected = root.join("rules/one.md");
        let dropped = root.join("sessions/one.jsonl");
        assert!(
            set.would_retain(&selected).unwrap(),
            "a selected file was not carried forward"
        );
        assert!(
            !set.would_retain(&dropped).unwrap(),
            "a file the list no longer selects was kept because it could not be read"
        );
        // and the watcher, which asks a different question, wakes for
        // both because either might have just been deleted
        let exclude = set.exclude_set().unwrap();
        assert!(!set.excluded_by_lists(&exclude, &selected, Asked::Possibly));
    }

    /// **Unselected is not absent.** A file that appears at a tracked
    /// entry's own path is not something a checkpoint with an include
    /// list ever held, so a rollback must not remove it.
    #[test]
    fn a_file_at_the_entry_path_is_not_read_as_absent() {
        use crate::system::history::replay::{PathState, classify_coverage};
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("rules")).unwrap();
        std::fs::write(root.join("rules/one.md"), "keep").unwrap();

        let mut tracked = entry(&root);
        tracked.include = Some(vec!["rules/**".to_string()]);
        let mut set = TrackedSet::default();
        set.push(tracked);
        let coverage = set.coverage(&set.walk().unwrap());

        for (path, expected_absent) in [
            // the entry's own path: no pattern selects it, so a file
            // that appears there was never covered
            (root.clone(), false),
            // unselected, so never covered either
            (root.join("notes.md"), false),
            // selected and not in the tree: this one the checkpoint
            // positively says was absent
            (root.join("rules/gone.md"), true),
        ] {
            let state = classify_coverage(&coverage, &display_path(&path));
            assert_eq!(
                matches!(state, PathState::Absent),
                expected_absent,
                "{}",
                path.display()
            );
        }
    }

    /// **Pruning must never change what is selected.** On unix a
    /// backslash is a character in a name, so a pattern carrying one
    /// selects a directory whose name carries one — and the walk has to
    /// go in there rather than reading the backslash as a separator and
    /// skipping it.
    #[cfg(unix)]
    #[test]
    fn a_pattern_with_a_backslash_selects_what_it_names_on_unix() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        let odd = root.join("we\\ird");
        std::fs::create_dir_all(&odd).unwrap();
        std::fs::write(odd.join("kept.md"), "keep").unwrap();

        let mut tracked = entry(&root);
        tracked.include = Some(vec!["we\\ird/**".to_string()]);
        let mut set = TrackedSet::default();
        set.push(tracked);

        // the matcher selects it, so the walk must reach it
        assert!(set.entries[0].is_included(&odd.join("kept.md")));
        assert!(
            !set.entries[0].include_prunes(&odd),
            "a directory the list selects was skipped unopened"
        );
        assert!(set.walk().unwrap().files.contains_key(&odd.join("kept.md")));
    }

    /// A path whose kind cannot be read is where the strict and the
    /// permissive questions disagree, and synchronization asks the
    /// strict one: a file another machine stopped selecting is not a
    /// deletion to replay here, however unreadable the local copy is.
    #[test]
    fn a_path_with_no_kind_answers_the_question_it_was_asked() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sample");
        std::fs::create_dir_all(root.join("nested")).unwrap();
        let mut tracked = entry(&root);
        // a name-only list: it can match at any depth, so no directory
        // can be pruned and the permissive answer is always "might
        // matter"
        tracked.include = Some(vec!["keep".into()]);
        let mut set = TrackedSet::default();
        set.push(tracked);
        let exclude = set.exclude_set().unwrap();

        // nothing is there to stat, so the reader decides
        let narrowed = root.join("nested/leave");
        assert!(
            set.excluded_by_lists(&exclude, &narrowed, Asked::Exactly),
            "a path the include list does not select counted as managed, so a narrowing elsewhere would delete it here"
        );
        assert!(
            !set.excluded_by_lists(&exclude, &narrowed, Asked::Possibly),
            "the watcher stopped waking for a path that has just vanished"
        );

        // what the list does name is selected under either question
        let selected = root.join("nested/keep");
        assert!(!set.excluded_by_lists(&exclude, &selected, Asked::Exactly));
        assert!(!set.excluded_by_lists(&exclude, &selected, Asked::Possibly));
    }

    fn entry(path: &Path) -> TrackedEntry {
        TrackedEntry::new(
            path.to_path_buf(),
            "track",
            Policy::for_mode(FileMode::Track),
        )
    }

    #[test]
    fn the_most_specific_entry_owns_a_file() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("root");
        let child = root.join("child");
        let mut set = TrackedSet::default();
        set.push(entry(&root));
        set.push(entry(&child));
        assert_eq!(
            set.entry_for(&child.join("file")).map(|e| &e.path),
            Some(&child)
        );
        assert_eq!(
            set.entry_for(&root.join("other")).map(|e| &e.path),
            Some(&root)
        );
        assert!(set.entry_for(&tmp.path().join("elsewhere")).is_none());
        // the index and the entry are one answer, so a covered path is
        // never read as uncovered because the entry could not be located
        // a second time
        for path in [child.join("file"), root.join("other")] {
            let index = set
                .entry_index_for(&path)
                .expect("a covered path has an index");
            assert_eq!(
                Some(&set.entries[index].path),
                set.entry_for(&path).map(|entry| &entry.path)
            );
        }
        assert!(set.entry_index_for(&tmp.path().join("elsewhere")).is_none());
        // Repeating an enrollment does not create another owner.
        let mut set = TrackedSet::default();
        set.push(entry(&root));
        set.push(entry(&root));
        assert_eq!(set.entries.len(), 1);
        let mut encrypted = entry(&root);
        encrypted.policy.encrypt = true;
        set.push(encrypted);
        assert_eq!(set.invalid.len(), 1);
    }

    #[test]
    fn existing_leaf_uses_the_filesystem_canonical_spelling() {
        let temp = tempfile::tempdir().unwrap();
        let actual = temp.path().join("MixedCase");
        std::fs::write(&actual, "contents").unwrap();
        let alternative = temp.path().join("mixedcase");
        // This exercises case folding only on filesystems which provide it.
        if alternative.exists() {
            assert_eq!(normalize_target(&alternative), normalize_target(&actual));
        }
    }

    #[cfg(windows)]
    #[test]
    fn junction_leaf_keeps_its_enrolled_location() {
        let temp = tempfile::tempdir().unwrap();
        let root = dunce::canonicalize(temp.path()).unwrap();
        let target = root.join("target");
        let link = root.join("junction");
        std::fs::create_dir(&target).unwrap();
        junction::create(&target, &link).unwrap();
        assert_eq!(normalize_target(&link), link);
        assert_ne!(normalize_target(&link), target);
    }

    #[test]
    fn deployment_requests_do_not_enroll_files_or_sources() {
        use crate::system::files::FileRequest;
        use crate::system::resources::ResourceOrigin;

        let tmp = tempfile::tempdir().unwrap();
        let mut set = TrackedSet::default();
        for mode in [
            FileMode::Copy,
            FileMode::Template,
            FileMode::Content,
            FileMode::Symlink,
            FileMode::SymlinkEach,
        ] {
            set.add_requests([FileRequest {
                target_raw: tmp.path().join("output").display().to_string(),
                target: tmp.path().join("output"),
                source: tmp.path().join("source"),
                content: None,
                mode,
                exclude: vec![],
                include: None,
                manifest: None,
                permissions: None,
                base: tmp.path().to_path_buf(),
                origin: ResourceOrigin {
                    config: tmp.path().join("config.toml"),
                    config_root: tmp.path().to_path_buf(),
                    environment: vec![],
                    source: None,
                },
                policy: Policy::for_mode(mode),
                variants: vec![],
                enabled: true,
                remove_empty: false,
                dot_prefix: false,
                relative: false,
            }]);
        }
        assert!(set.entries.is_empty());
        assert_eq!(
            set.required_sources,
            vec![normalize_target(&tmp.path().join("source"))]
        );
        assert!(set.walk().unwrap().files.is_empty());
    }

    #[test]
    fn enrolled_directories_include_new_descendants_but_not_credentials() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("templates");
        std::fs::create_dir_all(&root).unwrap();
        let mut set = TrackedSet::default();
        set.push(entry(&root));
        assert!(set.walk().unwrap().files.is_empty());
        let child = root.join("nested");
        std::fs::create_dir_all(&child).unwrap();
        let included = child.join("gitconfig.tera");
        std::fs::write(&included, "template").unwrap();
        std::fs::write(child.join("config.local.toml"), "private").unwrap();
        std::fs::write(child.join("credentials.json"), "private").unwrap();
        let walk = set.walk().unwrap();
        assert_eq!(walk.files.len(), 1);
        assert!(walk.files.contains_key(&included));
        assert_eq!(walk.omitted.len(), 2);
        assert!(set.would_capture(&included).unwrap());
        assert!(!set.would_capture(&child.join("config.local.toml")).unwrap());
        assert!(!set.would_capture(&child.join("credentials.json")).unwrap());
    }

    #[test]
    fn the_credential_guard_matches_by_name_alone() {
        let policy = Policy::for_mode(FileMode::Track);
        let dir = Path::new("/nonexistent-mise-test/.ssh");
        // conservative by name: a public half or a recipient list is
        // protected like the private half; un-protecting is the user's call
        for name in [
            "id_ed25519",
            "id_ed25519.pub",
            "secrets.fish",
            "client_secret.pub",
            "oauth_token.pub",
            "credentials.pub",
        ] {
            assert_eq!(
                capture_exclusion(&dir.join(name), &policy),
                Some(CREDENTIAL_REASON),
                "{name}"
            );
        }
        assert_eq!(
            capture_exclusion(&dir.join("recipients.txt"), &policy),
            None
        );
        assert_eq!(
            capture_exclusion(&dir.join("config.local.toml"), &policy),
            Some("machine-local configuration")
        );
        let mut encrypted = policy;
        encrypted.encrypt = true;
        assert_eq!(capture_exclusion(&dir.join("id_ed25519"), &encrypted), None);
    }

    /// A pattern is matched against the path only when it holds a
    /// separator, and `\` is one only where the platform writes it.
    #[test]
    fn a_pattern_is_anchored_only_when_it_holds_a_separator() {
        assert!(is_path_anchored("~/.config/app/**"));
        assert!(is_path_anchored("keys/*.pem"));
        assert!(!is_path_anchored("*.pem"));
        assert!(!is_path_anchored("cache"));
        assert_eq!(is_path_anchored("~\\.config\\app"), cfg!(windows));
    }

    /// A path glob is gitignore-like: `*` stops at a separator and `**`
    /// crosses them, so `keys/*.pem` is one directory deep.
    #[test]
    fn a_star_in_a_path_pattern_stops_at_a_separator() {
        let root = Path::new(ROOT);
        let set = ExcludeSet::new(&["keys/*.pem".to_string()]).unwrap();
        assert!(set.is_match(&root.join("app/keys/a.pem"), root));
        assert!(!set.is_match(&root.join("app/keys/sub/a.pem"), root));
        let set = ExcludeSet::new(&["keys/**/*.pem".to_string()]).unwrap();
        assert!(set.is_match(&root.join("app/keys/a.pem"), root));
        assert!(set.is_match(&root.join("app/keys/sub/a.pem"), root));
    }

    /// A pattern with no separator matches any single path component, so
    /// it takes a matching directory's contents with it — the same
    /// reading a tracked entry's own `exclude` list already has.
    #[test]
    fn a_name_glob_excludes_any_path_component() {
        let set = ExcludeSet::new(&["cache".to_string()]).unwrap();
        assert!(set.is_match(
            Path::new("/nonexistent-mise-test/.codex/cache"),
            Path::new(ROOT)
        ));
        assert!(set.is_match(
            Path::new("/nonexistent-mise-test/.codex/cache/index"),
            Path::new(ROOT)
        ));
        assert!(set.is_match(
            Path::new("/nonexistent-mise-test/cache/deep/index"),
            Path::new(ROOT)
        ));
        assert!(!set.is_match(
            Path::new("/nonexistent-mise-test/.codex/config.toml"),
            Path::new(ROOT)
        ));
        assert!(!set.is_match(
            Path::new("/nonexistent-mise-test/.codex/cached"),
            Path::new(ROOT)
        ));
        let set = ExcludeSet::new(&["*.log".to_string()]).unwrap();
        assert!(set.is_match(
            Path::new("/nonexistent-mise-test/a/b/run.log"),
            Path::new(ROOT)
        ));
        // the last matching pattern decides, and `!` re-includes
        let set = ExcludeSet::new(&["cache".to_string(), "!cache".to_string()]).unwrap();
        assert!(!set.is_match(
            Path::new("/nonexistent-mise-test/.codex/cache"),
            Path::new(ROOT)
        ));
        // the guard asks a different question and keeps reading names only
        assert!(!is_builtin_credential(
            Path::new("/nonexistent-mise-test/oauth/notes.txt"),
            "notes.txt"
        ));
    }

    /// An excluded directory is not enumerated. `~/.codex/sessions` can
    /// hold tens of thousands of files and none of them can re-enter the
    /// capture, so reading the directory at all is wasted work. An
    /// unreadable directory makes the difference visible: descending into
    /// it records an `unreadable` omission, skipping it records nothing.
    #[cfg(unix)]
    #[test]
    fn an_excluded_directory_is_not_descended_into() {
        use std::os::unix::fs::PermissionsExt;
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("sessions")).unwrap();
        std::fs::write(root.join("config.toml"), "keep").unwrap();
        std::fs::write(root.join("sessions/one.jsonl"), "drop").unwrap();
        std::fs::set_permissions(
            root.join("sessions"),
            std::fs::Permissions::from_mode(0o000),
        )
        .unwrap();
        let restore = || {
            std::fs::set_permissions(
                root.join("sessions"),
                std::fs::Permissions::from_mode(0o700),
            )
            .unwrap()
        };
        if std::fs::read_dir(root.join("sessions")).is_ok() {
            // running as root, where the permission says nothing
            restore();
            return;
        }
        // every spelling of "exclude this directory" prunes it: a bare
        // name, the gitignore form the docs show, and an absolute path
        for pattern in [
            "sessions".to_string(),
            "sessions/**".to_string(),
            format!("{}/sessions/**", root.display()),
        ] {
            let mut set = TrackedSet {
                exclude: vec![pattern.clone()],
                ..Default::default()
            };
            set.push(entry(&root));
            let walk = set.walk().unwrap();
            assert_eq!(walk.files.len(), 1, "{pattern}");
            assert!(
                walk.files.contains_key(&root.join("config.toml")),
                "{pattern}"
            );
            assert!(walk.omitted.is_empty(), "{pattern}: {:?}", walk.omitted);
        }
        // but a negation that could re-include something inside makes the
        // walk descend after all, and the unreadable directory shows it
        let mut set = TrackedSet {
            exclude: vec![
                "sessions/**".to_string(),
                "!sessions/keep.jsonl".to_string(),
            ],
            ..Default::default()
        };
        set.push(entry(&root));
        let walk = set.walk().unwrap();
        assert!(
            walk.omitted
                .iter()
                .any(|omitted| omitted.reason.starts_with("unreadable")),
            "{:?}",
            walk.omitted
        );
        restore();
    }

    /// A relative pattern matches at any depth and means the same thing
    /// wherever the command runs: never bound to `$PWD`, and never left
    /// matching nothing because the path it is compared with is
    /// absolute.
    #[test]
    fn a_relative_pattern_matches_at_any_depth_and_ignores_the_working_directory() {
        let cwd = std::env::current_dir().unwrap();
        let log = ExcludeSet::new(&["**/*.log".to_string()]).unwrap();
        assert!(log.is_match(
            Path::new("/nonexistent-mise-test/a/b/c.log"),
            Path::new(ROOT)
        ));
        // the same rule under a different tracked root: a relative
        // pattern matches at any depth *inside the entry*, which is the
        // only place it is ever asked about
        assert!(log.is_match(&cwd.join("a/b/c.log"), &cwd));
        assert!(!log.is_match(
            Path::new("/nonexistent-mise-test/a/b/c.txt"),
            Path::new(ROOT)
        ));

        // `sessions/**` used to match nothing at all, and briefly matched
        // only under whichever directory the command ran in; a leading
        // `./` says nothing extra and must not break the pattern
        for pattern in ["sessions/**", "./sessions/**"] {
            let sessions = ExcludeSet::new(&[pattern.to_string()]).unwrap();
            for root in [Path::new("/nonexistent-mise-test/.codex"), cwd.as_path()] {
                assert!(
                    sessions.is_match(&root.join("sessions/one.jsonl"), root),
                    "{pattern} under {}",
                    root.display()
                );
            }
            // and a path outside the tracked entry is not this entry's
            // business, whatever the pattern would say about its name
            assert!(!sessions.is_match(
                Path::new("/somewhere/else/sessions/one.jsonl"),
                Path::new("/nonexistent-mise-test/.codex")
            ));
            assert!(!sessions.is_match(
                Path::new("/nonexistent-mise-test/.codex/config.toml"),
                Path::new("/nonexistent-mise-test/.codex")
            ));
        }
        // `~` is expanded, so a pattern written with it is absolute and
        // anchored, not a relative one matching at any depth
        let home_pattern = ExcludeSet::new(&["~/.mise-test-tilde/**".to_string()]).unwrap();
        let home = crate::dirs::HOME.to_path_buf();
        assert!(home_pattern.is_match(&home.join(".mise-test-tilde/x"), &home));
        assert!(!home_pattern.is_match(
            Path::new("/nonexistent-mise-test/a/.mise-test-tilde/x"),
            Path::new("/nonexistent-mise-test")
        ));

        // a relative pattern is relative to the tracked root: a
        // `sessions` directory in the path above the root must not make
        // `sessions/**` swallow the whole tracked tree
        let above = ExcludeSet::new(&["sessions/**".to_string()]).unwrap();
        let nested_root = Path::new("/nonexistent-mise-test/sessions/.codex");
        assert!(!above.is_match(&nested_root.join("config.toml"), nested_root));
        assert!(above.is_match(&nested_root.join("sessions/one.jsonl"), nested_root));

        let keys = ExcludeSet::new(&["./keys/**".to_string()]).unwrap();
        assert!(keys.is_match(
            Path::new("/nonexistent-mise-test/.config/app/keys/id"),
            Path::new(ROOT)
        ));
    }

    /// An absolute pattern naming the live `~/…` location has to match a
    /// file the walk reached through a symlinked ancestor, which it only
    /// does when the pattern is normalized the way tracked paths are.
    #[cfg(unix)]
    #[test]
    fn an_absolute_pattern_follows_a_symlinked_ancestor() {
        let tmp = tempfile::tempdir().unwrap();
        let real = tmp.path().join("real");
        std::fs::create_dir_all(real.join("app")).unwrap();
        std::fs::write(real.join("app/store.kdb"), "vault").unwrap();
        let link = tmp.path().join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();
        // what the walk captures: the normalized path under the real directory
        let walked = normalize_target(&link.join("app/store.kdb"));
        assert!(walked.starts_with(normalize_target(&real)));
        // what the user writes: the location they know, through the link
        let pattern = link.join("app/**").to_string_lossy().into_owned();
        let set = ExcludeSet::new(std::slice::from_ref(&pattern)).unwrap();
        assert!(set.is_match(&walked, Path::new(ROOT)));
    }

    /// A pattern must also match the path exactly as it was written,
    /// not only its normalized form. A tracked entry pushed with an
    /// un-normalized path — a temp directory reached through a symlink,
    /// a Windows 8.3 short name — is walked as written, so normalizing
    /// only the pattern would silently stop the exclusion matching.
    #[cfg(unix)]
    #[test]
    fn an_absolute_pattern_matches_the_path_as_written_too() {
        let tmp = tempfile::tempdir().unwrap();
        let real = tmp.path().join("real");
        std::fs::create_dir_all(real.join("codex")).unwrap();
        std::fs::write(real.join("codex/one.jsonl"), "drop").unwrap();
        let link = tmp.path().join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();
        let root = link.join("codex");
        let pattern = format!("{}/**", root.display());
        let set = ExcludeSet::new(std::slice::from_ref(&pattern)).unwrap();
        // as written
        assert!(set.is_match(&root.join("one.jsonl"), Path::new(ROOT)));
        // and normalized, the way a configured entry is walked
        assert!(set.is_match(&normalize_target(&root).join("one.jsonl"), Path::new(ROOT)));
    }

    /// Pruning a matching directory must not swallow a later `!pattern`
    /// that re-includes something inside it: last-match-wins is the
    /// semantics, and the pruning is only an optimization.
    #[test]
    fn a_negation_below_an_excluded_directory_still_re_includes() {
        // the matcher alone, with no walk: if this part ever fails the
        // cause is the matching, and if only the walk below fails the
        // cause is pruning skipping the directory before the negation is
        // consulted
        let root = Path::new(ROOT);
        let rules = ["cache".to_string(), "!cache/keep.conf".to_string()];
        let set = ExcludeSet::new(&rules).unwrap();
        for (file, excluded) in [("cache/index", true), ("cache/keep.conf", false)] {
            let path = root.join(file);
            assert_eq!(
                set.is_match(&path, root),
                excluded,
                "matcher: {file} under {} should be excluded={excluded}",
                root.display()
            );
        }
        assert!(
            set.may_reinclude_below(&root.join("cache")),
            "a negation naming something inside the directory must keep it walked"
        );

        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        let cache = root.join("cache");
        std::fs::create_dir_all(&cache).unwrap();
        std::fs::write(root.join("config.toml"), "keep").unwrap();
        std::fs::write(cache.join("index"), "drop").unwrap();
        std::fs::write(cache.join("keep.conf"), "keep").unwrap();
        for negation in [
            format!("!{}/cache/keep.conf", root.display()),
            "!cache/keep.conf".to_string(),
        ] {
            let mut set = TrackedSet {
                exclude: vec!["cache".to_string(), negation.clone()],
                ..Default::default()
            };
            set.push(entry(&root));
            let walk = set.walk().unwrap();
            let mut names: Vec<String> = walk
                .files
                .keys()
                .map(|path| path.file_name().unwrap().to_string_lossy().into_owned())
                .collect();
            names.sort();
            assert_eq!(names, ["config.toml", "keep.conf"], "{negation}");
        }
        // the walk and the callers that never walk must agree about every
        // file, which is what pruning could otherwise have broken
        let mut set = TrackedSet {
            exclude: vec![
                "cache".to_string(),
                format!("!{}/cache/keep.conf", root.display()),
            ],
            ..Default::default()
        };
        set.push(entry(&root));
        let walk = set.walk().unwrap();
        for file in ["config.toml", "cache/index", "cache/keep.conf"] {
            let path = root.join(file);
            assert_eq!(
                walk.files.contains_key(&path),
                set.would_retain(&path).unwrap(),
                "{file}"
            );
        }

        // with nothing to re-include, the directory is still pruned
        let exclude = ExcludeSet::new(&["cache".to_string()]).unwrap();
        assert!(
            !exclude.may_reinclude_below(&cache),
            "no negation at all, so {} must be prunable",
            cache.display()
        );
        // a negation somewhere else entirely does not disable pruning.
        // The path is built from the temp directory rather than written
        // as `/somewhere/else`, which is not absolute on Windows and
        // would be read as a relative pattern matching at any depth.
        let elsewhere = tmp.path().join("elsewhere/keep.conf");
        let exclude =
            ExcludeSet::new(&["cache".to_string(), format!("!{}", elsewhere.display())]).unwrap();
        assert!(
            !exclude.may_reinclude_below(&cache),
            "a negation at {} cannot re-include anything below {}",
            elsewhere.display(),
            cache.display()
        );
    }

    /// The invariant that keeps this family of bugs from recurring:
    /// the capture walk, `would_retain`, the watcher's own filter and a
    /// replay's reading of the recorded coverage must agree about every
    /// path. All four pick the owning entry the same way — most specific
    /// wins — and evaluate exclusion with the same ancestor-aware,
    /// last-match-wins matcher over one shared composition.
    #[test]
    fn capture_would_retain_watcher_and_replay_agree_about_every_path() {
        use crate::system::history::replay::{PathState, classify_coverage};
        let tmp = tempfile::tempdir().unwrap();
        let outer = tmp.path().join("a");
        let inner = outer.join("b");
        std::fs::create_dir_all(inner.join("cache")).unwrap();
        std::fs::write(outer.join("outer.toml"), "keep").unwrap();
        // owned by the inner entry, and the global `b` must not reach it
        // through the outer entry's root
        std::fs::write(inner.join("inner.toml"), "keep").unwrap();
        std::fs::write(inner.join("cache/index"), "drop").unwrap();
        std::fs::write(inner.join("cache/keep.conf"), "keep").unwrap();
        // a working tree of its own, under a directory the global list
        // excludes and a later `!` rule reaches back into. The `!` rule
        // keeps `cache` from being pruned, so the walk descends — and
        // nothing in a selection list may carry it into another
        // repository.
        let repository = inner.join("cache/repo");
        std::fs::create_dir_all(repository.join(".git")).unwrap();
        std::fs::write(repository.join("secret"), "not ours").unwrap();

        let mut set = TrackedSet {
            exclude: vec![
                "b".to_string(),
                "cache".to_string(),
                format!("!{}/cache/keep.conf", inner.display()),
                format!("!{}/cache/repo/secret", inner.display()),
            ],
            ..Default::default()
        };
        set.push(entry(&outer));
        set.push(entry(&inner));
        let walk = set.walk().unwrap();
        let coverage = set.coverage(&walk);

        // every path is built with `join`, so on Windows the live paths
        // carry `\` while the patterns carry `/`: one test pins the
        // separator handling on both platforms
        for (file, expected) in [
            (outer.join("outer.toml"), true),
            (inner.join("inner.toml"), true),
            (inner.join("cache/index"), false),
            (inner.join("cache/keep.conf"), true),
        ] {
            let display = display_path(&file);
            assert_eq!(
                walk.files.contains_key(&file),
                expected,
                "capture {display}"
            );
            assert_eq!(
                set.would_retain(&file).unwrap(),
                expected,
                "would_retain {display}"
            );
            assert_eq!(
                !set.excluded_by_lists(&set.exclude_set().unwrap(), &file, Asked::Possibly),
                expected,
                "watcher {display}"
            );
            let covered = !matches!(classify_coverage(&coverage, &display), PathState::Uncovered);
            assert_eq!(covered, expected, "replay {display}");
        }

        // The repository is reported as one, not silently skipped, and
        // every reader refuses what is inside it — the walk because it
        // never descended, `would_retain` and the watcher because no
        // list may reach into another working tree, and a replay with
        // the reason rather than by calling it uncovered.
        assert_eq!(
            walk.nested
                .iter()
                .map(|nested| nested.path.as_str())
                .collect::<Vec<_>>(),
            vec![display_path(&repository).as_str()],
        );
        let describe = |state: &PathState| match state {
            PathState::Absent => "absent".to_string(),
            PathState::Uncovered => "uncovered".to_string(),
            PathState::Omitted(reason) => format!("omitted: {reason}"),
            PathState::Unevaluable(reason) => format!("unevaluable: {reason}"),
        };
        let secret = repository.join("secret");
        assert!(!walk.files.contains_key(&secret), "capture");
        assert!(!set.would_retain(&secret).unwrap(), "would_retain");
        assert!(
            set.excluded_by_lists(&set.exclude_set().unwrap(), &secret, Asked::Possibly),
            "watcher"
        );
        assert!(
            matches!(
                classify_coverage(&coverage, &display_path(&secret)),
                PathState::Omitted(reason) if reason.contains("repository")
            ),
            "replay: {}",
            describe(&classify_coverage(&coverage, &display_path(&secret)))
        );

        // `Absent` only when the record positively says so. Each other
        // input gets the answer that fits it — a repository the record
        // says was skipped reads as `Omitted` carrying the record's own
        // explanation, a record this mise cannot interpret reads as
        // `Unevaluable` — and none of them deletes.
        let display = display_path(outer.join("outer.toml"));
        for (name, broken, omitted_as) in [
            (
                "written before this matcher",
                {
                    let mut c = coverage.clone();
                    c.matcher = None;
                    c
                },
                None,
            ),
            (
                "written by a newer matcher",
                {
                    let mut c = coverage.clone();
                    c.matcher = Some(super::MATCHER_VERSION + 1);
                    c
                },
                None,
            ),
            (
                "a repository recorded as skipped",
                {
                    let mut c = coverage.clone();
                    c.nested.push(crate::system::history::store::PathReason {
                        path: display_path(&outer),
                        reason: NESTED_REPOSITORY_REASON.into(),
                    });
                    c
                },
                Some(NESTED_REPOSITORY_REASON),
            ),
            // On Windows the record may spell the path with either
            // separator and they mean the same directory. On unix a
            // backslash is part of a name, so a path spelled that way is
            // a different path — and one the record says nothing about,
            // which is why this case is Windows-only rather than
            // expecting the same answer everywhere.
            #[cfg(windows)]
            (
                "a repository recorded with the host's separators",
                {
                    let mut c = coverage.clone();
                    c.nested.push(crate::system::history::store::PathReason {
                        path: display_path(&outer).replace('/', "\\"),
                        reason: NESTED_REPOSITORY_REASON.into(),
                    });
                    c
                },
                Some(NESTED_REPOSITORY_REASON),
            ),
        ] {
            let state = classify_coverage(&broken, &display);
            let got = describe(&state);
            match omitted_as {
                // the record explains itself, so a rollback tells the
                // user what was skipped rather than that the checkpoint
                // cannot be interpreted
                Some(reason) => assert!(
                    matches!(&state, PathState::Omitted(found) if found == reason),
                    "{name}: expected the record's own explanation, got {got}"
                ),
                None => assert!(
                    matches!(&state, PathState::Unevaluable(_)),
                    "{name}: expected an uninterpretable record, got {got}"
                ),
            }
            // asserted, not assumed: `skip_reason` is the single place a
            // rollback decides to delete, and every state here refuses
            assert!(
                state.skip_reason("0123456789").is_some(),
                "{name}: {got} must never delete a live file"
            );
        }

        // the third non-deleting answer, from a path no entry covers
        let outside = display_path(tmp.path().join("outside.toml"));
        let state = classify_coverage(&coverage, &outside);
        assert!(
            matches!(state, PathState::Uncovered),
            "a path under no entry is uncovered, got {}",
            describe(&state)
        );
        assert!(
            state.skip_reason("0123456789").is_some(),
            "an uncovered path must never delete a live file"
        );
        // Unusable rules make deletion coverage uncertain, even when the
        // record claims the current matcher version.
        let mut unusable = coverage.clone();
        unusable
            .exclude
            .push("$MISE_TEST_UNSUPPORTED/**".to_string());
        let state = classify_coverage(&unusable, &display);
        assert!(matches!(state, PathState::Unevaluable(_)));
        assert!(state.skip_reason("0123456789").is_some());

        // a record this mise can read answers normally
        let mut plain = coverage.clone();
        plain.exclude.clear();
        assert!(!matches!(
            classify_coverage(&plain, &display),
            PathState::Unevaluable(_)
        ));
    }

    /// The four rules of a tracked entry's `include` list, each pinned
    /// on the same tree.
    #[test]
    fn an_include_list_selects_what_a_tracked_directory_saves() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("rules/deep")).unwrap();
        std::fs::create_dir_all(root.join("sessions")).unwrap();
        std::fs::write(root.join("config.toml"), "keep").unwrap();
        std::fs::write(root.join("notes.md"), "noise").unwrap();
        std::fs::write(root.join("rules/one.md"), "keep").unwrap();
        std::fs::write(root.join("rules/deep/two.md"), "keep").unwrap();
        std::fs::write(root.join("sessions/one.jsonl"), "noise").unwrap();

        let captured = |include: Option<&[&str]>, exclude: &[&str]| -> Vec<String> {
            let mut entry = entry(&root);
            entry.include = include.map(|p| p.iter().map(|p| (*p).to_string()).collect());
            entry.exclude = Some(exclude.iter().map(|p| (*p).to_string()).collect());
            let mut set = TrackedSet::default();
            set.push(entry);
            let walk = set.walk().unwrap();
            let mut names: Vec<String> = walk
                .files
                .keys()
                .map(|path| {
                    path.strip_prefix(&root)
                        .unwrap()
                        .to_string_lossy()
                        .replace('\\', "/")
                })
                .collect();
            names.sort();
            // the walk and every other reader have to agree
            for path in walk.files.keys() {
                assert!(set.would_retain(path).unwrap(), "{}", path.display());
            }
            names
        };

        // rule 1: no list means the whole tree
        assert_eq!(
            captured(None, &[]),
            [
                "config.toml",
                "notes.md",
                "rules/deep/two.md",
                "rules/one.md",
                "sessions/one.jsonl"
            ]
        );
        // rule 2: with a list, only what it names — and a directory
        // pattern takes everything under it
        assert_eq!(
            captured(Some(&["config.toml", "rules/**"]), &[]),
            ["config.toml", "rules/deep/two.md", "rules/one.md"]
        );
        // a new sibling appears without being named, and stays out
        std::fs::write(root.join("telemetry.json"), "noise").unwrap();
        assert_eq!(
            captured(Some(&["config.toml", "rules/**"]), &[]),
            ["config.toml", "rules/deep/two.md", "rules/one.md"]
        );
        // a declared but empty list selects nothing, and is never read as
        // no list at all — that would capture the whole tree
        assert!(captured(Some(&[]), &[]).is_empty());
        // and a list that names nothing present is the same answer: the
        // list decides, so what it does not select is not captured
        assert!(captured(Some(&["nothing-here"]), &[]).is_empty());
        // rule 3: an explicit exclude wins over an include
        assert_eq!(
            captured(Some(&["config.toml", "rules/**"]), &["rules/deep"]),
            ["config.toml", "rules/one.md"]
        );
    }

    /// An `include` list is a selection, and selection decides what is
    /// captured: a literal and a glob carry the same authority, and
    /// either one selecting a credential-named file captures it — in
    /// plaintext, said out loud. Without a list, the builtin filtering
    /// applies as it always has, so an existing declaration is
    /// unaffected by this feature.
    #[test]
    fn an_include_list_decides_what_is_captured_and_says_when_it_is_plaintext() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("fish");
        std::fs::create_dir_all(root.join("functions")).unwrap();
        std::fs::write(root.join("functions/hello.fish"), "function hello; end").unwrap();
        std::fs::write(root.join("functions/secrets.fish"), "set -x TOKEN x").unwrap();

        let walk_with = |include: Option<&[&str]>, encrypt: bool| {
            let mut entry = entry(&root);
            entry.include = include.map(|p| p.iter().map(|p| (*p).to_string()).collect());
            entry.policy.encrypt = encrypt;
            let mut set = TrackedSet::default();
            set.push(entry);
            set.walk().unwrap()
        };
        let holds = |walk: &Walk, name: &str| walk.files.keys().any(|p| p.ends_with(name));

        // no list: the builtin filtering applies, exactly as before
        let walk = walk_with(None, false);
        assert!(holds(&walk, "hello.fish"));
        assert!(!holds(&walk, "secrets.fish"));
        assert!(walk.plaintext.is_empty());
        assert!(
            walk.omitted
                .iter()
                .any(|o| o.path.ends_with("secrets.fish"))
        );

        // a list selects, and a glob is as authoritative as a literal
        for include in [
            &["functions/secrets.fish"][..],
            &["functions/*.fish"][..],
            &["**"][..],
        ] {
            let walk = walk_with(Some(include), false);
            assert!(holds(&walk, "secrets.fish"), "{include:?}");
            assert!(walk.omitted.is_empty(), "{include:?}");
            assert_eq!(walk.plaintext.len(), 1, "{include:?}");
            assert_eq!(walk.capture_warnings.len(), 1, "{include:?}");
            assert!(
                walk.warnings.is_empty(),
                "capture notices must wait for a commit"
            );
            assert!(
                walk.plaintext[0].path.ends_with("secrets.fish"),
                "{include:?}"
            );
            // and every reader agrees with the walk
            let mut set = TrackedSet::default();
            let mut owner = entry(&root);
            owner.include = Some(include.iter().map(|p| (*p).to_string()).collect());
            set.push(owner);
            assert!(
                set.would_retain(&root.join("functions/secrets.fish"))
                    .unwrap(),
                "{include:?}"
            );
        }

        // encryption is a separate question: the file is captured either
        // way, and nothing is stored in the clear
        let walk = walk_with(Some(&["functions/secrets.fish"]), true);
        assert!(holds(&walk, "secrets.fish"));
        assert!(walk.plaintext.is_empty());
    }

    /// A declared-but-empty list selects nothing, an entry that is
    /// itself a file included — and declaring a list on such an entry
    /// never lifts the credential guard for it, because no pattern named
    /// the file. Overriding the guard is something only a pattern does.
    #[test]
    fn an_empty_include_selects_nothing_even_for_a_single_file_entry() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path().join("app");
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("credentials"), "token").unwrap();
        let file = dir.join("credentials");

        let walk_with = |include: Option<&[&str]>| {
            let mut e = entry(&file);
            e.include = include.map(|p| p.iter().map(|p| (*p).to_string()).collect());
            let mut set = TrackedSet::default();
            set.push(e);
            (set.walk().unwrap(), set)
        };
        // no list: the guard applies, as it always has
        let (walk, set) = walk_with(None);
        assert!(walk.files.is_empty());
        assert!(!set.would_retain(&file).unwrap());
        // an empty list selects nothing at all
        let (walk, set) = walk_with(Some(&[]));
        assert!(walk.files.is_empty());
        assert!(walk.plaintext.is_empty());
        assert!(!set.would_retain(&file).unwrap());
        // and a list on a file entry does not name the file, so the
        // guard still stands: this is not the direct-entry override
        let (walk, set) = walk_with(Some(&["credentials"]));
        assert!(walk.files.is_empty(), "{:?}", walk.files);
        assert!(walk.plaintext.is_empty());
        assert!(!set.would_retain(&file).unwrap());
        // the supported spelling is a pattern on the directory entry
        let mut owner = entry(&dir);
        owner.include = Some(vec!["credentials".to_string()]);
        let mut set = TrackedSet::default();
        set.push(owner);
        let walk = set.walk().unwrap();
        assert!(walk.files.contains_key(&file));
        assert_eq!(walk.plaintext.len(), 1);
        assert!(set.would_retain(&file).unwrap());
    }

    /// A repository inside a tracked directory is skipped whatever the
    /// entry's `include` list says, and a pattern reaching into it is
    /// told that it selects nothing — there is a supported way to
    /// capture those files, and it is not this.
    #[test]
    fn an_include_list_cannot_reach_into_a_nested_repository() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("hammerspoon");
        let plugin = root.join("Spoons/Sky.spoon");
        std::fs::create_dir_all(plugin.join(".git")).unwrap();
        std::fs::write(plugin.join("init.lua"), "return {}").unwrap();
        std::fs::write(root.join("init.lua"), "top").unwrap();

        let walk_with = |include: &[&str]| {
            let mut entry = entry(&root);
            entry.include = Some(include.iter().map(|p| (*p).to_string()).collect());
            let mut set = TrackedSet::default();
            set.push(entry);
            set.walk().unwrap()
        };
        // a pattern that names paths inside it selects nothing, and the
        // skip says which pattern that was
        let walk = walk_with(&["Spoons/Sky.spoon/**"]);
        assert!(walk.files.is_empty(), "{:?}", walk.files);
        assert_eq!(walk.nested.len(), 1);
        assert!(walk.nested[0].reason.contains("selects nothing inside it"));
        assert!(walk.nested[0].reason.contains("Spoons/Sky.spoon/**"));
        // a bare name matches a component at any depth, so `init.lua`
        // does reach into the repository, and is named as such while the
        // entry's own `init.lua` is still captured
        let walk = walk_with(&["init.lua"]);
        assert!(walk.files.contains_key(&root.join("init.lua")));
        assert!(!walk.files.contains_key(&plugin.join("init.lua")));
        assert_eq!(walk.nested.len(), 1);
        assert!(walk.nested[0].reason.contains("\"init.lua\""));
        // a list that cannot select anything under `Spoons` does not
        // walk it at all, so there is no repository to report and
        // nothing to explain: what the list leaves out stays out, rather
        // than being visited and discarded
        let walk = walk_with(&["other/**"]);
        assert!(walk.nested.is_empty(), "{:?}", walk.nested);
        assert!(walk.files.is_empty(), "{:?}", walk.files);
        // an entry with no list reports it as it always did
        let mut plain = entry(&root);
        plain.include = None;
        let mut set = TrackedSet::default();
        set.push(plain);
        let walk = set.walk().unwrap();
        assert_eq!(walk.nested.len(), 1);
        assert_eq!(walk.nested[0].reason, NESTED_REPOSITORY_REASON);
    }

    #[test]
    fn display_under_accepts_either_separator() {
        assert!(display_under("~/.ssh", "~/.ssh"));
        assert!(display_under("~/.ssh/id_test", "~/.ssh"));
        assert!(!display_under("~/.sshd/x", "~/.ssh"));
        assert!(!display_under("~/.ssh", "~/.ssh/id_test"));

        // **On Windows both spellings mix, and both separate.** A path
        // recorded by `display_path` carries `\` while one rebuilt from a
        // tree path carries `/`, and they name the same file.
        #[cfg(windows)]
        {
            assert!(display_under("~\\.ssh\\id_test", "~\\.ssh"));
            assert!(display_under("~\\.ssh\\id_test", "~/.ssh"));
            assert!(display_under("~/.ssh/id_test", "~\\.ssh"));
            assert!(display_under("~\\.ssh", "~/.ssh"));
            assert!(!display_under("~\\.sshd\\x", "~/.ssh"));
        }

        // **On unix a backslash is part of a name.** `~/.ssh\id_test` is
        // one file called `.ssh\id_test`, not a file inside `~/.ssh` —
        // reading it as a separator would let one entry appear to own
        // another's paths, and a replay would judge a live file by the
        // wrong root and the wrong exclusions.
        #[cfg(unix)]
        {
            assert!(!display_under("~/.ssh\\id_test", "~/.ssh"));
            assert!(display_under("~/.ssh\\id_test", "~/.ssh\\id_test"));
            assert!(!display_under("~\\.ssh\\id_test", "~/.ssh"));
        }
    }

    #[test]
    fn display_under_matches_home_and_native_paths() {
        let root = crate::dirs::HOME.join(".nested");
        let child = root.join("plugin");
        assert!(display_under("~/.nested/plugin", &root.to_string_lossy()));
        assert!(display_under(&child.to_string_lossy(), "~/.nested"));
        assert!(!display_under(
            "~/.nested-other/plugin",
            &root.to_string_lossy()
        ));
    }

    #[test]
    fn omission_reports_list_few_and_summarize_many() {
        let omitted = |n: usize| -> Vec<PathReason> {
            (0..n)
                .map(|i| PathReason {
                    path: format!("~/.config/app/secret{i}"),
                    reason: CREDENTIAL_REASON.into(),
                })
                .collect()
        };
        assert!(omission_report(&[], &[]).is_empty());
        let few = omission_report(&omitted(2), &[]);
        assert_eq!(few.len(), 2);
        assert_eq!(
            few[0],
            format!("omitted: ~/.config/app/secret0 ({CREDENTIAL_REASON})")
        );
        let many = omission_report(&omitted(OMISSION_LINES + 1), &[]);
        assert_eq!(
            many,
            vec![format!(
                "{} files omitted from capture (credential store); `mise dot paths` lists them",
                OMISSION_LINES + 1
            )]
        );
        let mut mixed = omitted(1);
        mixed.push(PathReason {
            path: "~/.config/app/config.local.toml".into(),
            reason: "machine-local configuration".into(),
        });
        assert_eq!(
            omission_summary(&mixed, &[]),
            "2 files omitted from capture (1 credential store); `mise dot paths` lists them"
        );
        let nested = vec![PathReason {
            path: "~/.hammerspoon/Spoons/Sky.spoon".into(),
            reason: NESTED_REPOSITORY_REASON.into(),
        }];
        assert_eq!(
            omission_report(&[], &nested),
            vec![format!(
                "nested: ~/.hammerspoon/Spoons/Sky.spoon ({NESTED_REPOSITORY_REASON})"
            )]
        );
        assert_eq!(
            omission_summary(&omitted(1), &nested),
            "1 files omitted from capture (credential store); 1 nested repository skipped; `mise dot paths` lists them"
        );
    }

    #[test]
    fn a_nested_repository_is_skipped_and_tracking_it_captures_its_files() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("plugins");
        let plugin = root.join("nested");
        std::fs::create_dir_all(plugin.join(".git")).unwrap();
        std::fs::write(plugin.join(".git/HEAD"), "ref: refs/heads/main").unwrap();
        std::fs::write(plugin.join("init.lua"), "return {}").unwrap();
        std::fs::write(root.join("init.lua"), "top").unwrap();
        let mut set = TrackedSet::default();
        set.push(entry(&root));
        let walk = set.walk().unwrap();
        assert!(walk.files.contains_key(&root.join("init.lua")));
        // nothing is written for it: not its files, and not a pointer
        assert!(!walk.files.contains_key(&plugin));
        assert!(!walk.files.contains_key(&plugin.join("init.lua")));
        assert_eq!(walk.nested.len(), 1);
        assert_eq!(walk.nested[0].path, display_path(&plugin));
        assert_eq!(walk.nested[0].reason, NESTED_REPOSITORY_REASON);
        // the explanation names the remedy, because there is one
        assert!(walk.nested[0].reason.contains("track it directly"));
        assert!(walk.omitted.is_empty());
        assert_eq!(set.coverage(&walk).nested, walk.nested);
        assert!(!set.would_capture(&plugin).unwrap());
        assert!(!set.would_capture(&plugin.join("init.lua")).unwrap());
        // and that remedy works: tracking the repository itself captures
        // its working files, always without `.git`
        set.push(entry(&plugin));
        let walk = set.walk().unwrap();
        assert!(walk.files.contains_key(&plugin.join("init.lua")));
        assert!(!walk.files.contains_key(&plugin));
        assert!(!walk.files.contains_key(&plugin.join(".git/HEAD")));
        assert!(
            !walk
                .files
                .keys()
                .any(|path| path.components().any(|c| c.as_os_str() == ".git")),
            "`.git` is never captured"
        );
        assert!(walk.nested.is_empty());
        assert!(set.would_capture(&plugin.join("init.lua")).unwrap());
        assert!(!set.would_capture(&plugin.join(".git/HEAD")).unwrap());
    }

    #[test]
    fn nested_targets_partition_a_preview() {
        let tmp = tempfile::tempdir().unwrap();
        let outer = tmp.path().join("codex");
        let inner = outer.join("sessions");
        std::fs::create_dir_all(&inner).unwrap();
        std::fs::write(outer.join("config.toml"), "outer").unwrap();
        std::fs::write(inner.join("one.jsonl"), "inner-1").unwrap();
        std::fs::write(inner.join("two.jsonl"), "inner-2").unwrap();
        std::fs::write(inner.join("auth-token"), "x").unwrap();
        let mut set = TrackedSet::default();
        set.push(entry(&outer));
        set.push(entry(&inner));
        let walk = set.walk().unwrap();
        let outer_preview = walk.preview_of(&set, 0);
        let inner_preview = walk.preview_of(&set, 1);
        assert_eq!(outer_preview.files, 1);
        assert_eq!(outer_preview.bytes, 5);
        assert!(outer_preview.omitted.is_empty());
        assert_eq!(inner_preview.files, 2);
        assert_eq!(inner_preview.bytes, 14);
        assert_eq!(inner_preview.omitted.len(), 1);
        assert_eq!(outer_preview.summary(), "1 file, 5 B");
    }

    #[test]
    fn walk_summaries_count_files_and_bytes() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("tree");
        std::fs::create_dir_all(root.join("sub")).unwrap();
        std::fs::write(root.join("a"), "12345").unwrap();
        std::fs::write(root.join("sub/b"), "123").unwrap();
        std::fs::write(root.join("sub/secret.key"), "x").unwrap();
        let set = preview_set(&root, Policy::for_mode(FileMode::Track)).unwrap();
        assert_eq!(set.entries.len(), 1);
        assert_eq!(set.entries[0].path, normalize_target(&root));
        #[cfg(unix)]
        std::os::unix::fs::symlink("a", root.join("link")).unwrap();
        let walk = set.walk().unwrap();
        // a symlink counts as a file but adds no bytes
        assert_eq!(walk.file_count(), if cfg!(unix) { 3 } else { 2 });
        assert_eq!(walk.bytes(), 8);
        assert_eq!(
            walk.summary(),
            if cfg!(unix) {
                "3 files, 8 B"
            } else {
                "2 files, 8 B"
            }
        );
        assert_eq!(walk.omitted.len(), 1);
        assert!(!walk.preview_of(&set, 0).is_large());
        assert_eq!(count_and_size(1, 0), "1 file, 0 B");
        assert_eq!(with_separators(0), "0");
        assert_eq!(with_separators(999), "999");
        assert_eq!(with_separators(1000), "1,000");
        assert_eq!(with_separators(22972), "22,972");
        assert_eq!(with_separators(1234567), "1,234,567");
    }

    #[test]
    fn an_entry_excludes_relative_to_its_own_path() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("codex");
        std::fs::create_dir_all(root.join("sessions/deep")).unwrap();
        std::fs::create_dir_all(root.join("config/sessions")).unwrap();
        std::fs::create_dir_all(root.join("cache")).unwrap();
        std::fs::write(root.join("config.toml"), "keep").unwrap();
        std::fs::write(root.join("notes.md"), "drop").unwrap();
        std::fs::write(root.join("sessions/one.jsonl"), "drop").unwrap();
        std::fs::write(root.join("sessions/deep/two.jsonl"), "drop").unwrap();
        std::fs::write(root.join("config/sessions/keep.toml"), "keep").unwrap();
        std::fs::write(root.join("cache/index"), "drop").unwrap();
        // a component pattern matches anywhere, an anchored one only at
        // the entry root, and a matching directory takes its subtree
        let mut entry = entry(&root);
        entry.exclude = Some(vec!["*.md".into(), "sessions/**".into(), "cache".into()]);
        let mut set = TrackedSet::default();
        set.push(entry);
        let walk = set.walk().unwrap();
        assert_eq!(walk.files.len(), 2);
        assert!(walk.files.contains_key(&root.join("config.toml")));
        assert!(
            walk.files
                .contains_key(&root.join("config/sessions/keep.toml"))
        );
        assert!(walk.omitted.is_empty());
        assert!(set.would_retain(&root.join("config.toml")).unwrap());
        assert!(!set.would_retain(&root.join("notes.md")).unwrap());
        assert!(
            !set.would_retain(&root.join("sessions/deep/two.jsonl"))
                .unwrap()
        );
        assert!(!set.would_retain(&root.join("cache/index")).unwrap());
        assert!(
            set.would_retain(&root.join("config/sessions/keep.toml"))
                .unwrap()
        );
        // the entry path itself is never dropped by its own list
        let mut entry =
            super::TrackedEntry::new(root.clone(), "track", Policy::for_mode(FileMode::Track));
        entry.exclude = Some(vec!["codex".into()]);
        assert!(!entry.is_excluded(&root));
        assert!(!entry.is_excluded(tmp.path()));
        // a global `!glob` re-include does not override an entry's list
        let mut entry =
            super::TrackedEntry::new(root.clone(), "track", Policy::for_mode(FileMode::Track));
        entry.exclude = Some(vec!["cache".into()]);
        let mut set = TrackedSet {
            exclude: vec![
                format!("{}/**", root.display()),
                format!("!{}/cache/**", root.display()),
            ],
            ..Default::default()
        };
        set.push(entry);
        let walk = set.walk().unwrap();
        assert!(walk.files.is_empty());
        assert!(!set.would_retain(&root.join("cache/index")).unwrap());
        assert_eq!(
            set.coverage(&walk).entries[0].exclude,
            Some(vec!["cache".to_string()])
        );
    }

    #[test]
    fn entry_excludes_travel_through_the_enrollment_manifest() {
        use crate::system::files::FileRequest;
        use crate::system::resources::ResourceOrigin;
        let home = normalize(&dirs::HOME);
        let target = home.join(".mise-test-entry-exclude");
        let mut set = TrackedSet::default();
        set.add_requests([FileRequest {
            target_raw: "~/.mise-test-entry-exclude".into(),
            target: target.clone(),
            source: PathBuf::new(),
            content: None,
            mode: FileMode::Track,
            exclude: vec![glob::Pattern::new("sessions").unwrap()],
            include: None,
            manifest: None,
            permissions: None,
            base: home.clone(),
            origin: ResourceOrigin {
                config: home.join(".config/mise/config.toml"),
                config_root: home.join(".config/mise"),
                environment: vec![],
                source: None,
            },
            // the declaration wrote `exclude`, which is what makes the
            // list its own answer rather than silence
            policy: Policy {
                explicit: crate::system::files::ExplicitFields {
                    exclude: true,
                    ..Default::default()
                },
                ..Policy::for_mode(FileMode::Track)
            },
            variants: vec![],
            enabled: true,
            remove_empty: false,
            dot_prefix: false,
            relative: false,
        }]);
        assert_eq!(set.manifest.enrollment.len(), 1);
        assert_eq!(
            set.manifest.enrollment[0].exclude,
            Some(vec!["sessions".to_string()])
        );
        let rebuilt = set.manifest.tracking().unwrap();
        assert_eq!(
            rebuilt.entries[0].exclude,
            Some(vec!["sessions".to_string()])
        );
        assert!(rebuilt.entries[0].is_excluded(&target.join("sessions/one")));
    }

    #[test]
    fn tree_paths_round_trip() {
        assert_eq!(tree_path_to_display("home/.zshrc"), "~/.zshrc");
        // a path that exists is canonicalized first (`/etc` is a link on
        // macOS), so the round trip uses one that does not
        assert_eq!(
            tree_path_to_display("fs/nonexistent-mise-test/hosts"),
            "/nonexistent-mise-test/hosts"
        );
        assert_eq!(
            display_to_tree_path("/nonexistent-mise-test/hosts"),
            "fs/nonexistent-mise-test/hosts"
        );
    }

    #[cfg(unix)]
    #[test]
    fn tracking_a_symlink_does_not_enroll_its_target() {
        let tmp = tempfile::tempdir().unwrap();
        let target = tmp.path().join("target");
        std::fs::write(&target, "not enrolled").unwrap();
        let link = tmp.path().join("link");
        std::os::unix::fs::symlink(&target, &link).unwrap();
        let mut set = TrackedSet::default();
        set.push(entry(&link));
        let walk = set.walk().unwrap();
        assert!(walk.files.contains_key(&link));
        assert!(!walk.files.contains_key(&target));
    }
}