shellfirm 0.4.0

`shellfirm` will intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification, kinda like a captcha for your terminal.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
//! `shellfirm connect <tool>` — install/remove AI-agent integrations.
//!
//! Safety rules for touching other tools' config files:
//! - Files are parsed tolerantly (JSONC: comments, trailing commas, BOM). If a
//!   file can't be parsed, nothing is written and the command fails.
//! - Unexpected shapes (root not an object, `hooks` not an object, ...) are
//!   reported as errors, never "fixed" by overwriting.
//! - Every modified existing file gets a timestamped backup first, and is
//!   replaced atomically (temp file + rename, original permissions kept,
//!   symlinks followed).
//! - Key order is preserved; uninstall removes only shellfirm's own entries.

use std::fmt::Write as _;
use std::fs;
use std::io::Write as _;
use std::path::{Path, PathBuf};

use clap::{Arg, ArgAction, ArgMatches, Command};
use serde_json::{json, Map, Value};
use shellfirm::error::Result;

/// Hook arguments written by this version.
const CLAUDE_HOOK_ARGS: &str = "check --hook claude-code";
const CURSOR_HOOK_ARGS: &str = "check --hook cursor";
const BINARY: &str = "shellfirm";
const MCP_ARGS: &[&str] = &["mcp"];
/// `timeout` (seconds) written on hook entries. shellfirm answers in well
/// under a second; the optional LLM review is capped at 10 s.
const HOOK_TIMEOUT_SECS: u64 = 30;

type OpResult<T> = std::result::Result<T, String>;

// ---------------------------------------------------------------------------
// Providers and plan
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Component {
    Hooks,
    Mcp,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Scope {
    User,
    Project,
}

/// What to do inside one config file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OpKind {
    /// `hooks.PreToolUse[]` in Claude Code settings.json.
    ClaudeHooks,
    /// `mcpServers.shellfirm` with `"type": "stdio"` (Claude Code `~/.claude.json` / `.mcp.json`).
    ClaudeMcp,
    /// Remove the `mcpServers.shellfirm` entry older versions wrote into
    /// `~/.claude/settings.json` (Claude Code never read it there).
    LegacyClaudeSettingsMcp,
    /// Cursor `hooks.json` `hooks.beforeShellExecution[]`.
    CursorHooks,
    /// Plain `mcpServers.shellfirm` (Cursor, Windsurf, Cline).
    StandardMcp,
    /// Zed `context_servers.shellfirm`.
    ZedMcp,
}

impl OpKind {
    const fn component(self) -> Component {
        match self {
            Self::ClaudeHooks | Self::CursorHooks => Component::Hooks,
            _ => Component::Mcp,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Provider {
    ClaudeCode,
    Cursor,
    Windsurf,
    Zed,
    Cline,
}

const SUPPORTED_PROVIDERS: &str = "claude-code, cursor, windsurf, zed, cline";

impl Provider {
    fn from_name(name: &str) -> Option<Self> {
        match name {
            "claude-code" => Some(Self::ClaudeCode),
            "cursor" => Some(Self::Cursor),
            "windsurf" => Some(Self::Windsurf),
            "zed" => Some(Self::Zed),
            "cline" => Some(Self::Cline),
            _ => None,
        }
    }

    #[allow(dead_code)]
    const fn cli_name(self) -> &'static str {
        match self {
            Self::ClaudeCode => "claude-code",
            Self::Cursor => "cursor",
            Self::Windsurf => "windsurf",
            Self::Zed => "zed",
            Self::Cline => "cline",
        }
    }

    const fn display_name(self) -> &'static str {
        match self {
            Self::ClaudeCode => "Claude Code",
            Self::Cursor => "Cursor",
            Self::Windsurf => "Windsurf",
            Self::Zed => "Zed",
            Self::Cline => "Cline",
        }
    }

    const fn supports_hooks(self) -> bool {
        matches!(self, Self::ClaudeCode | Self::Cursor)
    }

    const fn supports_scope(self) -> bool {
        matches!(self, Self::ClaudeCode | Self::Cursor)
    }
}

/// Paths the plan is resolved against (injectable for tests).
#[derive(Debug, Clone)]
struct ConnectEnv {
    home: PathBuf,
    cwd: PathBuf,
    /// Where backups of project-scope files go (outside the repository).
    backup_dir: Option<PathBuf>,
    /// shellfirm's configuration directory, recorded in user-scope hooks.
    config_dir: Option<PathBuf>,
}

#[derive(Debug, Clone, Copy)]
#[allow(clippy::struct_excessive_bools)]
struct Options {
    dry_run: bool,
    uninstall: bool,
    hooks: bool,
    mcp: bool,
    scope: Scope,
    /// Claude Code: write `"onFailure": "block"` (needs Claude Code >= 2.1.295).
    fail_closed: bool,
}

fn cline_settings_path(home: &Path) -> Option<PathBuf> {
    let rel = "Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json";
    if cfg!(target_os = "macos") {
        Some(home.join("Library/Application Support").join(rel))
    } else if cfg!(target_os = "linux") {
        Some(home.join(".config").join(rel))
    } else {
        None
    }
}

/// Ordered list of `(file, operations)` for a provider.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn plan(
    provider: Provider,
    opts: &Options,
    env: &ConnectEnv,
) -> OpResult<Vec<(PathBuf, Vec<OpKind>)>> {
    let mut steps: Vec<(PathBuf, OpKind)> = Vec::new();
    let base = match opts.scope {
        Scope::User => env.home.clone(),
        Scope::Project => env.cwd.clone(),
    };
    match provider {
        Provider::ClaudeCode => {
            let settings = base.join(".claude").join("settings.json");
            if opts.hooks {
                steps.push((settings.clone(), OpKind::ClaudeHooks));
            }
            if opts.mcp {
                match opts.scope {
                    Scope::User => {
                        steps.push((env.home.join(".claude.json"), OpKind::ClaudeMcp));
                        steps.push((settings, OpKind::LegacyClaudeSettingsMcp));
                    }
                    Scope::Project => steps.push((base.join(".mcp.json"), OpKind::ClaudeMcp)),
                }
            }
        }
        Provider::Cursor => {
            let dir = base.join(".cursor");
            if opts.hooks {
                steps.push((dir.join("hooks.json"), OpKind::CursorHooks));
            }
            if opts.mcp {
                steps.push((dir.join("mcp.json"), OpKind::StandardMcp));
            }
        }
        Provider::Windsurf => steps.push((
            env.home
                .join(".codeium")
                .join("windsurf")
                .join("mcp_config.json"),
            OpKind::StandardMcp,
        )),
        Provider::Zed => steps.push((
            env.home.join(".config").join("zed").join("settings.json"),
            OpKind::ZedMcp,
        )),
        Provider::Cline => {
            let path = cline_settings_path(&env.home).ok_or_else(|| {
                "Cline's settings location is unknown on this platform".to_string()
            })?;
            steps.push((path, OpKind::StandardMcp));
        }
    }

    let mut grouped: Vec<(PathBuf, Vec<OpKind>)> = Vec::new();
    for (path, op) in steps {
        if let Some((_, ops)) = grouped.iter_mut().find(|(p, _)| *p == path) {
            ops.push(op);
        } else {
            grouped.push((path, vec![op]));
        }
    }
    Ok(grouped)
}

// ---------------------------------------------------------------------------
// Hook command recognition
// ---------------------------------------------------------------------------

/// What the hook entries written by this run look like.
#[derive(Debug, Clone, Default)]
struct InstallSpec {
    /// Configuration directory recorded in the hook command (user scope).
    config_dir: Option<String>,
    /// Write `"onFailure": "block"` on Claude Code hooks.
    fail_closed: bool,
}

impl InstallSpec {
    fn hook_command(&self, args: &str) -> String {
        let mut cmd = format!("{BINARY} {args}");
        if let Some(dir) = &self.config_dir {
            let _ = write!(cmd, " --config-dir {}", shell_quote(dir));
        }
        cmd
    }

    fn mcp_args(&self) -> Vec<String> {
        let mut args: Vec<String> = MCP_ARGS.iter().map(ToString::to_string).collect();
        if let Some(dir) = &self.config_dir {
            args.push("--config-dir".into());
            args.push(dir.clone());
        }
        args
    }
}

/// Quote `s` for a POSIX shell when needed.
fn shell_quote(s: &str) -> String {
    let plain = !s.is_empty()
        && s.chars()
            .all(|c| c.is_ascii_alphanumeric() || "_./:@%+=,-".contains(c));
    if plain {
        s.to_string()
    } else {
        format!("'{}'", s.replace('\'', r"'\''"))
    }
}

/// Split a hook command line like a POSIX shell would (quotes and
/// backslashes only). `None` for unbalanced quotes.
fn shell_split(cmd: &str) -> Option<Vec<String>> {
    let mut out = Vec::new();
    let mut cur = String::new();
    let mut in_word = false;
    let mut chars = cmd.chars();
    while let Some(c) = chars.next() {
        match c {
            '\'' => {
                in_word = true;
                loop {
                    match chars.next()? {
                        '\'' => break,
                        ch => cur.push(ch),
                    }
                }
            }
            '"' => {
                in_word = true;
                loop {
                    match chars.next()? {
                        '"' => break,
                        '\\' => cur.push(chars.next()?),
                        ch => cur.push(ch),
                    }
                }
            }
            '\\' => {
                in_word = true;
                cur.push(chars.next()?);
            }
            c if c.is_whitespace() => {
                if in_word {
                    out.push(std::mem::take(&mut cur));
                    in_word = false;
                }
            }
            c => {
                in_word = true;
                cur.push(c);
            }
        }
    }
    if in_word {
        out.push(cur);
    }
    Some(out)
}

/// A recognised shellfirm hook command.
#[derive(Debug, Clone, PartialEq, Eq)]
struct HookCmd {
    /// `check --stdin --format json --exit-code` (installed by <= 0.3.10).
    legacy: bool,
    /// `--config-dir` value, if any.
    config_dir: Option<String>,
}

/// Recognise a hook command written by any shellfirm version (or edited by
/// the user with extra shellfirm flags such as `--log debug`) for `target`
/// (`claude-code` or `cursor`). Anything else (another program, shell
/// operators, unknown flags) is not ours.
fn parse_shellfirm_hook(cmd: &str, target: &str) -> Option<HookCmd> {
    let tokens = shell_split(cmd)?;
    let (bin, rest) = tokens.split_first()?;
    let name = Path::new(bin).file_name().and_then(|n| n.to_str())?;
    if name != BINARY && name != "shellfirm.exe" {
        return None;
    }
    let mut it = rest.iter().map(String::as_str);
    let mut seen_check = false;
    let (mut hook, mut config_dir, mut format) = (None, None, None);
    let (mut stdin, mut exit_code) = (false, false);
    let value = |flag: &str, tok: &str, it: &mut dyn Iterator<Item = &str>| -> Option<String> {
        tok.strip_prefix(flag)
            .and_then(|r| r.strip_prefix('='))
            .map(ToString::to_string)
            .or_else(|| {
                (tok == flag)
                    .then(|| it.next().map(ToString::to_string))
                    .flatten()
            })
    };
    while let Some(tok) = it.next() {
        if tok == "check" && !seen_check {
            seen_check = true;
        } else if tok == "--log" || tok.starts_with("--log=") {
            value("--log", tok, &mut it)?;
        } else if !seen_check {
            return None;
        } else if tok == "--hook" || tok.starts_with("--hook=") {
            hook = Some(value("--hook", tok, &mut it)?);
        } else if tok == "--config-dir" || tok.starts_with("--config-dir=") {
            config_dir = Some(value("--config-dir", tok, &mut it)?);
        } else if tok == "--format" || tok.starts_with("--format=") {
            format = Some(value("--format", tok, &mut it)?);
        } else if tok == "--stdin" {
            stdin = true;
        } else if tok == "--exit-code" {
            exit_code = true;
        } else if tok == "--no-context" {
        } else {
            return None;
        }
    }
    if !seen_check {
        return None;
    }
    if hook.as_deref() == Some(target) && format.is_none() && !exit_code {
        return Some(HookCmd {
            legacy: false,
            config_dir,
        });
    }
    // `--format claude-hook|cursor-hook` (optionally with `--stdin` /
    // `--exit-code`, which current versions reject) and `--hook X
    // --exit-code` are shellfirm hooks that must be rewritten.
    let format_target = match format.as_deref() {
        Some("claude-hook") => Some("claude-code"),
        Some("cursor-hook") => Some("cursor"),
        _ => None,
    };
    let hook_format_form = hook.as_deref().map_or_else(
        || format_target == Some(target),
        |h| h == target && (format.is_none() || format_target == Some(target)),
    );
    let legacy = hook_format_form
        || (target == "claude-code"
            && hook.is_none()
            && stdin
            && exit_code
            && format.as_deref() == Some("json"));
    legacy.then_some(HookCmd {
        legacy: true,
        config_dir,
    })
}

fn is_claude_hook(cmd: &str) -> bool {
    parse_shellfirm_hook(cmd, "claude-code").is_some()
}

fn is_cursor_hook(cmd: &str) -> bool {
    parse_shellfirm_hook(cmd, "cursor").is_some()
}

/// Does a Claude Code hook `matcher` select `tool`? Rules from the Claude
/// Code hooks docs: `*`, `""` or omitted match everything; a value made only
/// of letters, digits, `_`, `-`, spaces, `,` and `|` is a list of exact tool
/// names; anything else is an (unanchored) regular expression.
fn matcher_covers(matcher: Option<&str>, tool: &str) -> bool {
    let Some(m) = matcher.map(str::trim) else {
        return true;
    };
    if m.is_empty() || m == "*" {
        return true;
    }
    let exact_list = m
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | ' ' | ',' | '|'));
    if exact_list {
        return m.split(['|', ',']).map(str::trim).any(|t| t == tool);
    }
    regex::Regex::new(m).is_ok_and(|re| re.is_match(tool))
}

// ---------------------------------------------------------------------------
// JSON shape helpers
// ---------------------------------------------------------------------------

fn root_object<'a>(value: &'a mut Value, path: &Path) -> OpResult<&'a mut Map<String, Value>> {
    value.as_object_mut().ok_or_else(|| {
        format!(
            "{} does not contain a JSON object at the top level; refusing to modify it",
            path.display()
        )
    })
}

fn child_object<'a>(
    parent: &'a mut Map<String, Value>,
    key: &str,
    path: &Path,
    create: bool,
) -> OpResult<Option<&'a mut Map<String, Value>>> {
    if !parent.contains_key(key) {
        if !create {
            return Ok(None);
        }
        parent.insert(key.to_string(), json!({}));
    }
    match parent.get_mut(key) {
        Some(Value::Object(m)) => Ok(Some(m)),
        _ => Err(format!(
            "`{key}` in {} is not a JSON object; refusing to modify it",
            path.display()
        )),
    }
}

fn child_array<'a>(
    parent: &'a mut Map<String, Value>,
    key: &str,
    what: &str,
    path: &Path,
    create: bool,
) -> OpResult<Option<&'a mut Vec<Value>>> {
    if !parent.contains_key(key) {
        if !create {
            return Ok(None);
        }
        parent.insert(key.to_string(), json!([]));
    }
    match parent.get_mut(key) {
        Some(Value::Array(a)) => Ok(Some(a)),
        _ => Err(format!(
            "`{what}` in {} is not a JSON array; refusing to modify it",
            path.display()
        )),
    }
}

fn hook_command_of(v: &Value) -> Option<&str> {
    v.get("command").and_then(Value::as_str)
}

/// What is installed in a Claude Code settings file (read-only, tolerant).
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct ClaudeHookState {
    /// Tools (`Bash` + file-writing tools) covered by a shellfirm hook.
    covered: Vec<&'static str>,
    /// Number of shellfirm hook entries (any matcher).
    entries: usize,
    /// Entries using the legacy `--stdin --format json --exit-code` line.
    legacy: usize,
    /// Entries with `"onFailure": "block"`.
    fail_closed: usize,
    /// `--config-dir` values recorded in the entries.
    config_dirs: Vec<String>,
}

const CLAUDE_TOOLS: &[&str] = &[
    "Bash",
    "PowerShell",
    "Monitor",
    "Write",
    "Edit",
    "MultiEdit",
    "NotebookEdit",
    "Read",
];

fn claude_hook_state(value: &Value) -> ClaudeHookState {
    let mut st = ClaudeHookState::default();
    let Some(groups) = value
        .get("hooks")
        .and_then(|h| h.get("PreToolUse"))
        .and_then(Value::as_array)
    else {
        return st;
    };
    for g in groups {
        let matcher = g.get("matcher").and_then(Value::as_str);
        let Some(hs) = g.get("hooks").and_then(Value::as_array) else {
            continue;
        };
        let mut ours = false;
        for h in hs {
            let Some(hc) = hook_command_of(h).and_then(|c| parse_shellfirm_hook(c, "claude-code"))
            else {
                continue;
            };
            ours = true;
            st.entries += 1;
            st.legacy += usize::from(hc.legacy);
            if h.get("onFailure").and_then(Value::as_str) == Some("block") {
                st.fail_closed += 1;
            }
            if let Some(d) = hc.config_dir {
                if !st.config_dirs.contains(&d) {
                    st.config_dirs.push(d);
                }
            }
        }
        if ours {
            for t in CLAUDE_TOOLS {
                if matcher_covers(matcher, t) && !st.covered.contains(t) {
                    st.covered.push(t);
                }
            }
        }
    }
    st
}

/// Hook settings the user chose themselves on shellfirm's Claude Code hooks
/// (a `timeout` other than [`HOOK_TIMEOUT_SECS`], an `onFailure` other than
/// `block`). `connect` keeps them; the result describes them, e.g.
/// `["timeout 120s", "onFailure: allow"]`.
fn kept_hook_settings(value: &Value) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    let Some(groups) = value
        .get("hooks")
        .and_then(|h| h.get("PreToolUse"))
        .and_then(Value::as_array)
    else {
        return out;
    };
    for h in groups
        .iter()
        .filter_map(|g| g.get("hooks").and_then(Value::as_array))
        .flatten()
    {
        if hook_command_of(h).is_none_or(|c| parse_shellfirm_hook(c, "claude-code").is_none()) {
            continue;
        }
        let mut push = |s: String| {
            if !out.contains(&s) {
                out.push(s);
            }
        };
        match h.get("timeout") {
            Some(t) if t.as_u64() != Some(HOOK_TIMEOUT_SECS) => push(format!("timeout {t}s")),
            _ => {}
        }
        match h.get("onFailure") {
            Some(Value::String(v)) if v != "block" => push(format!("onFailure: {v}")),
            Some(v) if !v.is_string() => push(format!("onFailure: {v}")),
            _ => {}
        }
    }
    out
}

/// Read-only: is shellfirm's hook protecting the Bash tool?
#[cfg(test)]
fn claude_hooks_installed(value: &Value) -> bool {
    claude_hook_state(value).covered.contains(&"Bash")
}

/// Cursor hook event holding the shell-command hook.
const CURSOR_SHELL_EVENT: &str = "beforeShellExecution";
/// Cursor hook event holding the file-edit hook.
const CURSOR_TOOL_EVENT: &str = "preToolUse";
/// Matcher of the Cursor file-edit hook (Cursor matchers are regexes; tool
/// names from Cursor's hooks docs).
const CURSOR_FILE_MATCHER: &str = "Write|Delete";

fn cursor_event_entries<'a>(value: &'a Value, event: &str) -> Vec<(HookCmd, &'a Value)> {
    value
        .get("hooks")
        .and_then(|h| h.get(event))
        .and_then(Value::as_array)
        .map(|hs| {
            hs.iter()
                .filter_map(|h| {
                    hook_command_of(h)
                        .and_then(|c| parse_shellfirm_hook(c, "cursor"))
                        .map(|hc| (hc, h))
                })
                .collect()
        })
        .unwrap_or_default()
}

fn cursor_hook_entries(value: &Value) -> Vec<(HookCmd, &Value)> {
    cursor_event_entries(value, CURSOR_SHELL_EVENT)
}

/// shellfirm `preToolUse` entries whose matcher covers Cursor's `Write`.
fn cursor_file_hook_entries(value: &Value) -> Vec<(HookCmd, &Value)> {
    cursor_event_entries(value, CURSOR_TOOL_EVENT)
        .into_iter()
        .filter(|(_, h)| {
            let m = h.get("matcher").and_then(Value::as_str);
            m.is_none_or(|m| {
                m.trim().is_empty()
                    || m == "*"
                    || regex::Regex::new(m).is_ok_and(|re| re.is_match("Write"))
            })
        })
        .collect()
}

#[cfg(test)]
fn cursor_hooks_installed(value: &Value) -> bool {
    !cursor_hook_entries(value).is_empty()
}

fn server_installed(value: &Value, container: &str) -> bool {
    value
        .get(container)
        .and_then(Value::as_object)
        .is_some_and(|m| m.contains_key(BINARY))
}

fn legacy_settings_mcp_present(value: &Value) -> bool {
    value
        .get("mcpServers")
        .and_then(|m| m.get(BINARY))
        .and_then(|s| s.get("command"))
        .and_then(Value::as_str)
        .is_some_and(|c| Path::new(c).file_name().and_then(|n| n.to_str()) == Some(BINARY))
}

/// Bring existing shellfirm hook entries up to date in place: legacy command
/// lines are replaced, a missing `--config-dir` / `timeout` / `onFailure` is
/// added. Returns the number of entries changed.
fn upgrade_hook_entry(
    h: &mut Value,
    hc: &HookCmd,
    args: &str,
    spec: &InstallSpec,
    on_failure: Option<bool>,
) -> bool {
    // A legacy line is rewritten, keeping a custom `--config-dir`.
    let desired = InstallSpec {
        config_dir: hc.config_dir.clone().or_else(|| spec.config_dir.clone()),
        ..spec.clone()
    }
    .hook_command(args);
    let Some(obj) = h.as_object_mut() else {
        return false;
    };
    let mut changed = false;
    let old = obj
        .get("command")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_string();
    if hc.legacy {
        obj.insert("command".into(), json!(desired));
        changed = true;
    } else if hc.config_dir.is_none() {
        if let Some(dir) = &spec.config_dir {
            obj.insert(
                "command".into(),
                json!(format!("{old} --config-dir {}", shell_quote(dir))),
            );
            changed = true;
        }
    }
    if !obj.contains_key("timeout") {
        obj.insert("timeout".into(), json!(HOOK_TIMEOUT_SECS));
        changed = true;
    }
    match on_failure {
        Some(true) if !obj.contains_key("onFailure") => {
            obj.insert("onFailure".into(), json!("block"));
            changed = true;
        }
        // `--no-fail-closed`: downgrade an existing fail-closed entry.
        Some(false) if obj.get("onFailure").and_then(Value::as_str) == Some("block") => {
            obj.shift_remove("onFailure");
            changed = true;
        }
        _ => {}
    }
    changed
}

fn claude_hook_entry(spec: &InstallSpec) -> Value {
    let mut entry = json!({
        "type": "command",
        "command": spec.hook_command(CLAUDE_HOOK_ARGS),
        "timeout": HOOK_TIMEOUT_SECS,
    });
    if spec.fail_closed {
        entry["onFailure"] = json!("block");
    }
    entry
}

/// Remove duplicate shellfirm hooks: within a group only the first one is
/// kept, and a group whose matcher adds no tool beyond the earlier
/// shellfirm groups loses its shellfirm hook (the group itself is dropped
/// when nothing else is left in it). Returns the number removed.
fn dedupe_claude_hooks(groups: &mut Vec<Value>) -> usize {
    let mut covered: Vec<&'static str> = Vec::new();
    let mut removed = 0usize;
    groups.retain_mut(|g| {
        let matcher = g
            .get("matcher")
            .and_then(Value::as_str)
            .map(ToString::to_string);
        let Some(hs) = g.get_mut("hooks").and_then(Value::as_array_mut) else {
            return true;
        };
        let ours = |h: &Value| hook_command_of(h).is_some_and(is_claude_hook);
        if !hs.iter().any(ours) {
            return true;
        }
        let adds: Vec<&'static str> = CLAUDE_TOOLS
            .iter()
            .copied()
            .filter(|t| matcher_covers(matcher.as_deref(), t) && !covered.contains(t))
            .collect();
        let keep_one = !adds.is_empty();
        let n = hs.len();
        let mut kept = false;
        hs.retain(|h| {
            if !ours(h) {
                return true;
            }
            if keep_one && !kept {
                kept = true;
                return true;
            }
            false
        });
        removed += n - hs.len();
        covered.extend(adds);
        !hs.is_empty()
    });
    removed
}

#[allow(clippy::too_many_lines)]
fn install_claude_hooks(
    value: &mut Value,
    path: &Path,
    spec: &InstallSpec,
) -> OpResult<Vec<String>> {
    let mut actions = Vec::new();
    let root = root_object(value, path)?;
    let before = claude_hook_state(&Value::Object(root.clone()));
    // New entries use the configuration directory the existing hooks
    // already record (e.g. a team directory), so all hooks read the same
    // settings.
    let spec = &InstallSpec {
        config_dir: before
            .config_dirs
            .first()
            .cloned()
            .or_else(|| spec.config_dir.clone()),
        ..spec.clone()
    };
    let desired = spec.hook_command(CLAUDE_HOOK_ARGS);
    if before.entries > 0 {
        let hooks = child_object(root, "hooks", path, false)?.expect("present");
        let groups =
            child_array(hooks, "PreToolUse", "hooks.PreToolUse", path, false)?.expect("present");
        let duplicates = dedupe_claude_hooks(groups);
        if duplicates > 0 {
            actions.push(format!(
                "remove {duplicates} duplicate shellfirm hook(s) (shellfirm would run twice per tool call)"
            ));
        }
        let mut upgraded = 0usize;
        let mut legacy = 0usize;
        let mut downgraded = 0usize;
        for g in groups.iter_mut() {
            let Some(hs) = g.get_mut("hooks").and_then(Value::as_array_mut) else {
                continue;
            };
            for h in hs.iter_mut() {
                let Some(hc) =
                    hook_command_of(h).and_then(|c| parse_shellfirm_hook(c, "claude-code"))
                else {
                    continue;
                };
                legacy += usize::from(hc.legacy);
                let had_block = h.get("onFailure").and_then(Value::as_str) == Some("block");
                if upgrade_hook_entry(h, &hc, CLAUDE_HOOK_ARGS, spec, Some(spec.fail_closed)) {
                    upgraded += 1;
                    downgraded += usize::from(had_block && h.get("onFailure").is_none());
                }
            }
        }
        if upgraded > 0 {
            // Values the user set themselves are kept (see
            // `kept_hook_settings`): only claim what was actually written.
            let kept = kept_hook_settings(&Value::Object(root.clone()));
            let where_unset = if kept.is_empty() {
                ""
            } else {
                " where not set"
            };
            actions.push(format!(
                "upgrade {upgraded} existing shellfirm hook(s) in place{} (timeout {HOOK_TIMEOUT_SECS}s{}{}{})",
                if legacy > 0 {
                    format!(", replacing {legacy} legacy command line(s) with `{desired}`")
                } else {
                    String::new()
                },
                where_unset,
                if spec.fail_closed {
                    format!(", onFailure: block{where_unset}")
                } else if downgraded > 0 {
                    format!(", onFailure removed from {downgraded} (--no-fail-closed)")
                } else {
                    String::new()
                },
                if spec.config_dir.is_some() { ", pinned --config-dir" } else { "" },
            ));
        }
    }

    let state = claude_hook_state(&Value::Object(root.clone()));
    let hooks = child_object(root, "hooks", path, true)?.expect("created");
    let groups =
        child_array(hooks, "PreToolUse", "hooks.PreToolUse", path, true)?.expect("created");
    let missing_shell: Vec<&str> = shellfirm::agent::SHELL_TOOLS
        .iter()
        .copied()
        .filter(|t| !state.covered.contains(t))
        .collect();
    if !missing_shell.is_empty() {
        let matcher = missing_shell.join("|");
        groups.push(json!({ "matcher": matcher, "hooks": [claude_hook_entry(spec)] }));
        actions.push(format!(
            "add hooks.PreToolUse entry (matcher \"{matcher}\") running `{desired}`"
        ));
    }
    let missing: Vec<&str> = shellfirm::agent::FILE_WRITE_TOOLS
        .iter()
        .copied()
        .filter(|t| !state.covered.contains(t))
        .collect();
    if !missing.is_empty() {
        let matcher = missing.join("|");
        groups.push(json!({ "matcher": matcher, "hooks": [claude_hook_entry(spec)] }));
        actions.push(format!(
            "add hooks.PreToolUse entry (matcher \"{matcher}\") so edits to shellfirm, policy, \
             agent-settings and shell startup files need approval"
        ));
    }
    let missing_read: Vec<&str> = shellfirm::agent::FILE_READ_TOOLS
        .iter()
        .copied()
        .filter(|t| !state.covered.contains(t))
        .collect();
    if !missing_read.is_empty() {
        let matcher = missing_read.join("|");
        groups.push(json!({ "matcher": matcher, "hooks": [claude_hook_entry(spec)] }));
        actions.push(format!(
            "add hooks.PreToolUse entry (matcher \"{matcher}\") so reading a private key or a \
             credentials file needs approval"
        ));
    }
    if before.config_dirs.len() > 1 && (!missing.is_empty() || !missing_shell.is_empty()) {
        actions.push(format!(
            "note: the existing shellfirm hooks use different --config-dir values ({}); the new \
             entries use {}, so different tools may read different settings",
            before.config_dirs.join(", "),
            before.config_dirs[0]
        ));
    }
    Ok(actions)
}

/// Upgrade (and dedupe) the shellfirm entries of one Cursor hook event.
/// Returns `(entries kept, upgraded, failClosed removed, duplicates removed)`.
fn upgrade_cursor_event(list: &mut Vec<Value>, spec: &InstallSpec) -> (usize, usize, usize, usize) {
    let (mut kept, mut upgraded, mut unfailed) = (0usize, 0usize, 0usize);
    let n = list.len();
    let mut seen = false;
    list.retain(|h| {
        if !hook_command_of(h).is_some_and(is_cursor_hook) {
            return true;
        }
        let keep = !seen;
        seen = true;
        keep
    });
    let duplicates = n - list.len();
    for h in list.iter_mut() {
        let Some(hc) = hook_command_of(h).and_then(|c| parse_shellfirm_hook(c, "cursor")) else {
            continue;
        };
        kept += 1;
        let mut changed = upgrade_hook_entry(h, &hc, CURSOR_HOOK_ARGS, spec, None);
        // shellfirm prints nothing for safe commands; with `failClosed`
        // Cursor treats "no output" as a failure and would block them all.
        if let Some(obj) = h.as_object_mut() {
            if obj.get("failClosed").and_then(Value::as_bool) == Some(true) {
                obj.shift_remove("failClosed");
                unfailed += 1;
                changed = true;
            }
        }
        upgraded += usize::from(changed);
    }
    (kept, upgraded, unfailed, duplicates)
}

fn install_cursor_hooks(
    value: &mut Value,
    path: &Path,
    spec: &InstallSpec,
) -> OpResult<Vec<String>> {
    let mut actions = Vec::new();
    let existing_dir = cursor_hook_entries(value)
        .into_iter()
        .chain(cursor_event_entries(value, CURSOR_TOOL_EVENT))
        .find_map(|(hc, _)| hc.config_dir);
    let spec = &InstallSpec {
        config_dir: existing_dir.or_else(|| spec.config_dir.clone()),
        ..spec.clone()
    };
    let desired = spec.hook_command(CURSOR_HOOK_ARGS);
    let has_file_hook = !cursor_file_hook_entries(value).is_empty();
    let root = root_object(value, path)?;
    let had_hooks_key = root.contains_key("hooks");
    let hooks = child_object(root, "hooks", path, true)?.expect("created");
    let mut kept_shell = 0usize;
    for event in [CURSOR_SHELL_EVENT, CURSOR_TOOL_EVENT] {
        let Some(list) = child_array(hooks, event, &format!("hooks.{event}"), path, false)? else {
            continue;
        };
        let (kept, upgraded, unfailed, duplicates) = upgrade_cursor_event(list, spec);
        if event == CURSOR_SHELL_EVENT {
            kept_shell = kept;
        }
        if duplicates > 0 {
            actions.push(format!(
                "remove {duplicates} duplicate shellfirm hook(s) from hooks.{event}"
            ));
        }
        if unfailed > 0 {
            actions.push(format!(
                "remove \"failClosed\": true from {unfailed} shellfirm hook(s) in hooks.{event} \
                 (shellfirm prints nothing for safe commands, which Cursor would treat as a \
                 failure and block)"
            ));
        }
        if upgraded > unfailed {
            actions.push(format!(
                "upgrade {} existing shellfirm hook(s) in hooks.{event} in place (timeout {HOOK_TIMEOUT_SECS}s{})",
                upgraded - unfailed,
                if spec.config_dir.is_some() { ", pinned --config-dir" } else { "" }
            ));
        }
    }
    if kept_shell == 0 {
        let list = child_array(
            hooks,
            CURSOR_SHELL_EVENT,
            "hooks.beforeShellExecution",
            path,
            true,
        )?
        .expect("created");
        list.push(json!({ "command": desired, "timeout": HOOK_TIMEOUT_SECS }));
        actions.push(format!(
            "add hooks.beforeShellExecution entry running `{desired}`"
        ));
    }
    if !has_file_hook {
        let list = child_array(hooks, CURSOR_TOOL_EVENT, "hooks.preToolUse", path, true)?
            .expect("created");
        list.push(json!({
            "command": desired,
            "matcher": CURSOR_FILE_MATCHER,
            "timeout": HOOK_TIMEOUT_SECS,
        }));
        actions.push(format!(
            "add hooks.preToolUse entry (matcher \"{CURSOR_FILE_MATCHER}\") so agent edits to \
             shellfirm, policy, agent-settings and shell startup files are blocked"
        ));
    }
    if hooks.is_empty() && !had_hooks_key {
        root.shift_remove("hooks");
    }
    if !actions.is_empty() && !root.contains_key("version") {
        root.insert("version".into(), json!(1));
    }
    Ok(actions)
}

/// Apply an install operation. Returns human-readable action descriptions
/// (empty = nothing to do).
fn install(
    op: OpKind,
    value: &mut Value,
    path: &Path,
    spec: &InstallSpec,
) -> OpResult<Vec<String>> {
    match op {
        OpKind::ClaudeHooks => return install_claude_hooks(value, path, spec),
        OpKind::CursorHooks => return install_cursor_hooks(value, path, spec),
        OpKind::LegacyClaudeSettingsMcp => return uninstall(op, value, path),
        OpKind::ClaudeMcp | OpKind::StandardMcp | OpKind::ZedMcp => {}
    }
    let container = if op == OpKind::ZedMcp {
        "context_servers"
    } else {
        "mcpServers"
    };
    if server_installed(value, container) {
        return Ok(Vec::new());
    }
    let root = root_object(value, path)?;
    let args = spec.mcp_args();
    let entry = match op {
        OpKind::ClaudeMcp => {
            json!({ "type": "stdio", "command": BINARY, "args": args, "env": {} })
        }
        OpKind::ZedMcp => json!({ "command": BINARY, "args": args, "env": {} }),
        _ => json!({ "command": BINARY, "args": args }),
    };
    let servers = child_object(root, container, path, true)?.expect("created");
    servers.insert(BINARY.into(), entry.clone());
    Ok(vec![format!("add {container}.{BINARY} = {entry}")])
}

/// Apply an uninstall operation, removing only shellfirm's own entries.
fn uninstall(op: OpKind, value: &mut Value, path: &Path) -> OpResult<Vec<String>> {
    let Some(root) = value.as_object_mut() else {
        // Nothing of ours can be in a non-object file.
        return Ok(Vec::new());
    };
    let mut actions = Vec::new();
    match op {
        OpKind::ClaudeHooks => {
            let Some(hooks) = child_object(root, "hooks", path, false)? else {
                return Ok(actions);
            };
            let Some(groups) = child_array(hooks, "PreToolUse", "hooks.PreToolUse", path, false)?
            else {
                return Ok(actions);
            };
            let before = groups.len();
            let mut removed = 0usize;
            groups.retain_mut(|group| {
                let Some(inner) = group.get_mut("hooks").and_then(Value::as_array_mut) else {
                    return true;
                };
                let n = inner.len();
                inner.retain(|h| !hook_command_of(h).is_some_and(is_claude_hook));
                removed += n - inner.len();
                // Drop the group only when it became empty because of us.
                !(n > 0 && inner.is_empty())
            });
            if removed > 0 {
                actions.push(format!(
                    "remove {removed} shellfirm hook(s) from hooks.PreToolUse"
                ));
                if groups.is_empty() && before > 0 {
                    hooks.shift_remove("PreToolUse");
                    if hooks.is_empty() {
                        root.shift_remove("hooks");
                    }
                }
            }
        }
        OpKind::CursorHooks => {
            let Some(hooks) = child_object(root, "hooks", path, false)? else {
                return Ok(actions);
            };
            for event in [CURSOR_SHELL_EVENT, CURSOR_TOOL_EVENT] {
                let Some(list) = child_array(hooks, event, &format!("hooks.{event}"), path, false)?
                else {
                    continue;
                };
                let n = list.len();
                list.retain(|h| !hook_command_of(h).is_some_and(is_cursor_hook));
                if list.len() < n {
                    actions.push(format!("remove shellfirm hook from hooks.{event}"));
                    if list.is_empty() {
                        hooks.shift_remove(event);
                    }
                }
            }
            if hooks.is_empty() && !actions.is_empty() {
                root.shift_remove("hooks");
            }
        }
        OpKind::ClaudeMcp
        | OpKind::StandardMcp
        | OpKind::ZedMcp
        | OpKind::LegacyClaudeSettingsMcp => {
            if op == OpKind::LegacyClaudeSettingsMcp
                && !legacy_settings_mcp_present(&Value::Object(root.clone()))
            {
                return Ok(actions);
            }
            let container = if op == OpKind::ZedMcp {
                "context_servers"
            } else {
                "mcpServers"
            };
            let Some(servers) = child_object(root, container, path, false)? else {
                return Ok(actions);
            };
            if servers.shift_remove(BINARY).is_some() {
                actions.push(if op == OpKind::LegacyClaudeSettingsMcp {
                    format!(
                        "remove stale {container}.{BINARY} (Claude Code does not load MCP servers from this file)"
                    )
                } else {
                    format!("remove {container}.{BINARY}")
                });
                if servers.is_empty() {
                    root.shift_remove(container);
                }
            }
        }
    }
    Ok(actions)
}

/// `true` when a file holds nothing but (at most) a Cursor `version` key, so
/// it can be deleted instead of being left behind as a `{}` stub.
fn is_empty_stub(value: &Value, ops: &[OpKind]) -> bool {
    value.as_object().is_some_and(|m| {
        m.is_empty()
            || (ops.contains(&OpKind::CursorHooks) && m.len() == 1 && m.contains_key("version"))
    })
}

// ---------------------------------------------------------------------------
// Status (used by `shellfirm status`)
// ---------------------------------------------------------------------------

/// The state of one AI-agent integration, for `shellfirm status`.
// Consumed by `status` (wired up by the status/config workstream).
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, PartialEq, Eq, serde_derive::Serialize)]
pub struct IntegrationStatus {
    /// `claude-code`, `cursor`, `windsurf`, `zed` or `cline`.
    pub tool: &'static str,
    /// `user` or `project`.
    pub scope: &'static str,
    /// File holding the command hook (tools with hooks only).
    pub hook_file: Option<String>,
    /// A shellfirm hook intercepts the agent's shell commands (Claude Code:
    /// its matcher covers `Bash`).
    pub hook_installed: bool,
    /// At least one shellfirm hook entry exists, whatever its matcher. With
    /// `hook_installed == false` this means "hook present but Bash not
    /// covered" (e.g. an `Edit|Write`-only entry).
    pub hook_present: bool,
    /// Claude Code shell tools (`Bash`, `PowerShell`, `Monitor`) that no
    /// shellfirm hook covers while a hook is present (re-run `connect`).
    pub shell_tools_missing: Vec<&'static str>,
    /// Claude Code: Write/Edit/MultiEdit/NotebookEdit and Read are covered too.
    /// Cursor: a `preToolUse` hook covers `Write`.
    pub file_tools_covered: bool,
    /// At least one hook uses the legacy (<= 0.3.10) command line.
    pub legacy_hook: bool,
    /// Every shellfirm hook blocks the tool call when shellfirm cannot run
    /// (Claude Code `onFailure: block`; Cursor hooks are never fail-closed).
    pub fail_closed: bool,
    /// Configuration directory recorded in the hook command.
    pub config_dir: Option<String>,
    /// File holding the MCP server registration.
    pub mcp_file: Option<String>,
    /// shellfirm is registered as an MCP server.
    pub mcp_registered: bool,
    /// A file could not be read or parsed.
    pub error: Option<String>,
}

#[allow(dead_code)]
fn read_value(path: &Path) -> std::result::Result<Option<Value>, String> {
    if !path.exists() {
        return Ok(None);
    }
    let content = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
    parse_jsonc(&content)
        .map(|(v, _)| Some(v))
        .map_err(|e| format!("{}: {e}", path.display()))
}

/// Report every AI-agent integration shellfirm knows about, for the user
/// (`home`) and, when it differs, the project (`cwd`) scope.
///
/// Call it from `shellfirm status` as
/// `connect_cmd::integration_status(&home, &cwd)` (both from `dirs::home_dir()`
/// / `std::env::current_dir()`); rows with neither a hook (`hook_present`)
/// nor an MCP registration can be hidden. A row with `hook_present` but not
/// `hook_installed` should be shown as "hook present but Bash not covered",
/// and a non-empty `shell_tools_missing` as "PowerShell/Monitor not covered".
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn integration_status(home: &Path, cwd: &Path) -> Vec<IntegrationStatus> {
    let env = ConnectEnv {
        home: home.to_path_buf(),
        cwd: cwd.to_path_buf(),
        backup_dir: None,
        config_dir: None,
    };
    let mut out = Vec::new();
    let mut scopes = vec![Scope::User];
    if cwd != home {
        scopes.push(Scope::Project);
    }
    for provider in [
        Provider::ClaudeCode,
        Provider::Cursor,
        Provider::Windsurf,
        Provider::Zed,
        Provider::Cline,
    ] {
        for &scope in &scopes {
            if scope == Scope::Project && !provider.supports_scope() {
                continue;
            }
            let opts = Options {
                dry_run: true,
                uninstall: false,
                hooks: provider.supports_hooks(),
                mcp: true,
                scope,
                fail_closed: true,
            };
            let Ok(files) = plan(provider, &opts, &env) else {
                continue;
            };
            let mut st = IntegrationStatus {
                tool: provider.cli_name(),
                scope: if scope == Scope::User {
                    "user"
                } else {
                    "project"
                },
                hook_file: None,
                hook_installed: false,
                hook_present: false,
                shell_tools_missing: Vec::new(),
                file_tools_covered: false,
                legacy_hook: false,
                fail_closed: false,
                config_dir: None,
                mcp_file: None,
                mcp_registered: false,
                error: None,
            };
            for (path, ops) in files {
                let value = match read_value(&path) {
                    Ok(v) => v.unwrap_or_else(|| json!({})),
                    Err(e) => {
                        st.error = Some(e);
                        continue;
                    }
                };
                for op in ops {
                    match op {
                        OpKind::ClaudeHooks => {
                            let s = claude_hook_state(&value);
                            st.hook_file = Some(path.display().to_string());
                            st.hook_installed = s.covered.contains(&"Bash");
                            st.hook_present = s.entries > 0;
                            if s.entries > 0 {
                                st.shell_tools_missing = shellfirm::agent::SHELL_TOOLS
                                    .iter()
                                    .copied()
                                    .filter(|t| !s.covered.contains(t))
                                    .collect();
                            }
                            st.file_tools_covered = shellfirm::agent::FILE_WRITE_TOOLS
                                .iter()
                                .chain(shellfirm::agent::FILE_READ_TOOLS)
                                .all(|t| s.covered.contains(t));
                            st.legacy_hook = s.legacy > 0;
                            st.fail_closed = s.entries > 0 && s.fail_closed == s.entries;
                            st.config_dir = s.config_dirs.first().cloned();
                        }
                        OpKind::CursorHooks => {
                            let entries = cursor_hook_entries(&value);
                            st.hook_file = Some(path.display().to_string());
                            let files = cursor_file_hook_entries(&value);
                            st.hook_installed = !entries.is_empty();
                            st.hook_present = !entries.is_empty()
                                || !cursor_event_entries(&value, CURSOR_TOOL_EVENT).is_empty();
                            st.file_tools_covered = !files.is_empty();
                            st.legacy_hook =
                                entries.iter().chain(files.iter()).any(|(hc, _)| hc.legacy);
                            st.config_dir =
                                entries.iter().find_map(|(hc, _)| hc.config_dir.clone());
                        }
                        OpKind::ClaudeMcp | OpKind::StandardMcp | OpKind::ZedMcp => {
                            let container = if op == OpKind::ZedMcp {
                                "context_servers"
                            } else {
                                "mcpServers"
                            };
                            st.mcp_file = Some(path.display().to_string());
                            st.mcp_registered = server_installed(&value, container);
                        }
                        OpKind::LegacyClaudeSettingsMcp => {}
                    }
                }
            }
            out.push(st);
        }
    }
    out
}

// ---------------------------------------------------------------------------
// JSONC parsing and safe file IO
// ---------------------------------------------------------------------------

/// Strip `//` and `/* */` comments and trailing commas (outside strings).
/// Returns the cleaned text and whether anything was removed.
fn strip_jsonc(input: &str) -> (String, bool) {
    let input = input.strip_prefix('\u{feff}').unwrap_or(input);
    let chars: Vec<char> = input.chars().collect();
    let mut out = String::with_capacity(input.len());
    let mut changed = false;
    let mut i = 0;
    let mut in_str = false;
    while i < chars.len() {
        let c = chars[i];
        if in_str {
            out.push(c);
            if c == '\\' && i + 1 < chars.len() {
                out.push(chars[i + 1]);
                i += 2;
                continue;
            }
            if c == '"' {
                in_str = false;
            }
            i += 1;
            continue;
        }
        match c {
            '"' => {
                in_str = true;
                out.push(c);
                i += 1;
            }
            '/' if chars.get(i + 1) == Some(&'/') => {
                changed = true;
                while i < chars.len() && chars[i] != '\n' {
                    i += 1;
                }
            }
            '/' if chars.get(i + 1) == Some(&'*') => {
                changed = true;
                i += 2;
                while i < chars.len() && !(chars[i] == '*' && chars.get(i + 1) == Some(&'/')) {
                    if chars[i] == '\n' {
                        out.push('\n');
                    }
                    i += 1;
                }
                i += 2;
                out.push(' ');
            }
            _ => {
                out.push(c);
                i += 1;
            }
        }
    }

    // Second pass: trailing commas.
    let chars: Vec<char> = out.chars().collect();
    let mut result = String::with_capacity(out.len());
    let mut in_str = false;
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        if in_str {
            result.push(c);
            if c == '\\' && i + 1 < chars.len() {
                result.push(chars[i + 1]);
                i += 2;
                continue;
            }
            if c == '"' {
                in_str = false;
            }
        } else if c == '"' {
            in_str = true;
            result.push(c);
        } else if c == ',' {
            let next = chars[i + 1..].iter().find(|ch| !ch.is_whitespace());
            if matches!(next, Some('}' | ']')) {
                changed = true;
            } else {
                result.push(c);
            }
        } else {
            result.push(c);
        }
        i += 1;
    }
    (result, changed)
}

/// Parse JSON or JSONC. Returns the value and whether comments/trailing
/// commas were present (they cannot be preserved on rewrite).
fn parse_jsonc(content: &str) -> std::result::Result<(Value, bool), String> {
    if content.trim().is_empty() {
        return Ok((json!({}), false));
    }
    match serde_json::from_str(content.strip_prefix('\u{feff}').unwrap_or(content)) {
        Ok(v) => Ok((v, false)),
        Err(strict_err) => {
            let (clean, changed) = strip_jsonc(content);
            if !changed {
                return Err(strict_err.to_string());
            }
            serde_json::from_str(&clean)
                .map(|v| (v, true))
                .map_err(|e| e.to_string())
        }
    }
}

struct LoadedFile {
    /// Path as shown to the user.
    path: PathBuf,
    /// Where to write (symlink target if `path` is a symlink).
    write_path: PathBuf,
    exists: bool,
    value: Value,
    lossy_format: bool,
}

fn load_file(path: &Path) -> OpResult<LoadedFile> {
    if !path.exists() {
        if fs::symlink_metadata(path).is_ok() {
            return Err(format!(
                "{} is a dangling symlink; refusing to modify it",
                path.display()
            ));
        }
        return Ok(LoadedFile {
            path: path.to_path_buf(),
            write_path: path.to_path_buf(),
            exists: false,
            value: json!({}),
            lossy_format: false,
        });
    }
    let write_path = fs::canonicalize(path).map_err(|e| format!("{}: {e}", path.display()))?;
    let bytes = fs::read(path).map_err(|e| format!("could not read {}: {e}", path.display()))?;
    let content = String::from_utf8(bytes).map_err(|_| {
        format!(
            "{} is not valid UTF-8; refusing to modify it",
            path.display()
        )
    })?;
    let (value, lossy_format) = parse_jsonc(&content).map_err(|e| {
        format!(
            "could not parse {} as JSON/JSONC ({e}); nothing was changed. Fix the file or edit it by hand.",
            path.display()
        )
    })?;
    Ok(LoadedFile {
        path: path.to_path_buf(),
        write_path,
        exists: true,
        value,
        lossy_format,
    })
}

/// Where to back up `path`. With `backup_dir` (project scope) the backup
/// goes there, named after the full path, so nothing lands in the repository.
fn backup_path_for(path: &Path, backup_dir: Option<&Path>) -> PathBuf {
    let stamp: String = shellfirm::audit::now_timestamp()
        .chars()
        .filter(char::is_ascii_alphanumeric)
        .collect();
    let (dir, name) = backup_dir.map_or_else(
        || {
            (
                path.parent().map(Path::to_path_buf).unwrap_or_default(),
                path.file_name()
                    .map_or_else(|| "config".into(), |n| n.to_string_lossy().into_owned()),
            )
        },
        |d| {
            let shown = path.display().to_string();
            // Windows: the canonical path is `\\?\C:\…` (`\\?\UNC\…` on a
            // share); that prefix is not part of the name, and `?`, `*`,
            // `<`, `>`, `"`, `|` cannot appear in a file name there.
            #[cfg(windows)]
            let shown = shown
                .strip_prefix(r"\\?\UNC\")
                .map(str::to_string)
                .or_else(|| shown.strip_prefix(r"\\?\").map(str::to_string))
                .unwrap_or(shown);
            let flat: String = shown
                .trim_start_matches(['/', '\\'])
                .chars()
                .map(|c| {
                    if matches!(c, '/' | '\\' | ':')
                        || (cfg!(windows) && matches!(c, '?' | '*' | '<' | '>' | '"' | '|'))
                    {
                        '%'
                    } else {
                        c
                    }
                })
                .collect();
            (d.to_path_buf(), flat)
        },
    );
    let mut candidate = dir.join(format!("{name}.shellfirm-bak-{stamp}"));
    let mut n = 1;
    while candidate.exists() {
        candidate = dir.join(format!("{name}.shellfirm-bak-{stamp}-{n}"));
        n += 1;
    }
    candidate
}

fn create_private_dir(dir: &Path) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::DirBuilderExt;
        fs::DirBuilder::new()
            .recursive(true)
            .mode(0o700)
            .create(dir)
    }
    #[cfg(not(unix))]
    {
        fs::create_dir_all(dir)
    }
}

/// What to do with a file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SaveMode {
    Write,
    /// The file would only be an empty stub after uninstall: delete it.
    Remove,
}

/// Write `value` to `file` atomically (or remove the file), after backing up
/// the existing file. Returns the backup path (if a backup was made).
fn save_file(
    file: &LoadedFile,
    value: &Value,
    backup_dir: Option<&Path>,
    mode: SaveMode,
) -> OpResult<Option<PathBuf>> {
    let target = &file.write_path;
    let mut backup = None;
    let mut permissions = None;
    if file.exists {
        let meta = fs::metadata(target).map_err(|e| format!("{}: {e}", file.path.display()))?;
        if meta.permissions().readonly() {
            return Err(format!(
                "{} is read-only; make it writable or edit it by hand",
                file.path.display()
            ));
        }
        permissions = Some(meta.permissions());
        if let Some(d) = backup_dir {
            create_private_dir(d)
                .map_err(|e| format!("could not create backup directory {}: {e}", d.display()))?;
        }
        let bak = backup_path_for(target, backup_dir);
        fs::copy(target, &bak)
            .map_err(|e| format!("could not back up {}: {e}", file.path.display()))?;
        backup = Some(bak);
    }

    if mode == SaveMode::Remove {
        if file.exists {
            fs::remove_file(target)
                .map_err(|e| format!("could not remove {}: {e}", file.path.display()))?;
        }
        return Ok(backup);
    }

    let parent = target
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .map_or_else(|| PathBuf::from("."), Path::to_path_buf);
    fs::create_dir_all(&parent)
        .map_err(|e| format!("could not create {}: {e}", parent.display()))?;

    let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())? + "\n";
    let name = target
        .file_name()
        .map_or_else(|| "config".into(), |n| n.to_string_lossy().into_owned());
    let tmp = parent.join(format!(".{name}.shellfirm-tmp-{}", std::process::id()));
    let write = || -> std::io::Result<()> {
        let mut f = fs::File::create(&tmp)?;
        f.write_all(text.as_bytes())?;
        f.sync_all()?;
        if let Some(ref p) = permissions {
            fs::set_permissions(&tmp, p.clone())?;
        }
        fs::rename(&tmp, target)
    };
    if let Err(e) = write() {
        let _ = fs::remove_file(&tmp);
        return Err(format!("could not write {}: {e}", file.path.display()));
    }
    Ok(backup)
}

// ---------------------------------------------------------------------------
// CLI
// ---------------------------------------------------------------------------

fn dry_run_arg() -> Arg {
    Arg::new("dry-run")
        .long("dry-run")
        .help("Show what would change without writing anything")
        .action(ArgAction::SetTrue)
}

fn uninstall_arg() -> Arg {
    Arg::new("uninstall")
        .long("uninstall")
        .help("Remove shellfirm's entries (only those; other config is kept)")
        .action(ArgAction::SetTrue)
}

fn hooks_tool_subcommand(name: &'static str, about: &'static str, long: &'static str) -> Command {
    let cmd = Command::new(name);
    let cmd = if name == "claude-code" {
        cmd.arg(
            Arg::new("no-fail-closed")
                .long("no-fail-closed")
                .help("Don't add \"onFailure\": \"block\" to the hook (for Claude Code older than 2.1.295)")
                .action(ArgAction::SetTrue),
        )
    } else {
        cmd
    };
    cmd
        .about(about)
        .long_about(long)
        .arg(
            Arg::new("hooks-only")
                .long("hooks-only")
                .help("Only the command-interception hook (no MCP)")
                .action(ArgAction::SetTrue)
                .conflicts_with("mcp-only"),
        )
        .arg(
            Arg::new("mcp-only")
                .long("mcp-only")
                .help("Only the MCP server registration (no hook)")
                .action(ArgAction::SetTrue)
                .conflicts_with("hooks-only"),
        )
        .arg(
            Arg::new("scope")
                .long("scope")
                .help("user: your home directory (default); project: the current directory (for team rollout)")
                .value_parser(["user", "project"])
                .default_value("user"),
        )
        .arg(dry_run_arg())
        .arg(uninstall_arg())
}

fn mcp_only_subcommand(name: &'static str, about: &'static str) -> Command {
    Command::new(name)
        .about(about)
        .arg(dry_run_arg())
        .arg(uninstall_arg())
}

pub fn command() -> Command {
    Command::new("connect")
        .about("Connect AI tool integrations (hooks + MCP)")
        .arg_required_else_help(true)
        .subcommand(hooks_tool_subcommand(
            "claude-code",
            "Connect Claude Code (PreToolUse hook + MCP server)",
            "Connect Claude Code.\n\n\
            user scope:    hook in ~/.claude/settings.json, MCP server in ~/.claude.json\n\
            project scope: hook in ./.claude/settings.json, MCP server in ./.mcp.json\n\n\
            The hook runs `shellfirm check --hook claude-code` before every tool call that runs \
            a shell command (matcher Bash|PowerShell|Monitor): risky commands get an `ask` \
            prompt, dangerous ones are denied, everything else is left to Claude Code's normal \
            permission rules. A second PreToolUse entry (matcher \
            Write|Edit|MultiEdit|NotebookEdit) asks before the agent edits shellfirm, \
            project-policy, agent-settings, hook or shell startup files, and a third (matcher \
            Read) asks before it reads a private key or a credentials file.\n\n\
            Hooks are written with \"timeout\": 30 and \"onFailure\": \"block\" (Claude Code \
            >= 2.1.295 then blocks the tool call when shellfirm is missing, crashes or times \
            out; use --no-fail-closed for older versions, which also removes \
            \"onFailure\" from existing shellfirm hooks). User-scope hooks record the \
            configuration directory (--config-dir) so a redirected HOME/XDG_CONFIG_HOME cannot \
            swap in other settings. Existing shellfirm hooks (also legacy ones and ones with \
            extra flags such as --log debug) are upgraded in place, duplicate shellfirm hooks are \
            removed, and new entries reuse the --config-dir already recorded. Project-scope backups are \
            written to ~/.cache/shellfirm/backups, not into the repository.",
        ))
        .subcommand(hooks_tool_subcommand(
            "cursor",
            "Connect Cursor (beforeShellExecution hook + MCP server)",
            "Connect Cursor.\n\n\
            user scope:    ~/.cursor/hooks.json and ~/.cursor/mcp.json\n\
            project scope: ./.cursor/hooks.json and ./.cursor/mcp.json\n\n\
            The beforeShellExecution hook answers deny/ask for risky commands and prints \
            nothing for safe ones (Cursor documents no neutral answer, and an explicit allow \
            could skip Cursor's own approval). A preToolUse hook (matcher Write|Delete) denies \
            agent edits of shellfirm, policy, agent-settings, hook and shell startup files \
            (Cursor does not enforce `ask` for preToolUse, so the user makes such changes by \
            hand). The hooks are not marked failClosed (Cursor counts \"no output\" as a \
            failure and would then block every safe command; an existing failClosed on a \
            shellfirm hook is removed): if shellfirm is missing or times out, Cursor lets the \
            action through. shellfirm itself never exits without a decision for risky input \
            (crashes become deny).",
        ))
        .subcommand(mcp_only_subcommand(
            "windsurf",
            "Register shellfirm's MCP tools with Windsurf (advisory; no interception)",
        ))
        .subcommand(mcp_only_subcommand(
            "zed",
            "Register shellfirm's MCP tools with Zed (advisory; no interception)",
        ))
        .subcommand(mcp_only_subcommand(
            "cline",
            "Register shellfirm's MCP tools with Cline (advisory; no interception)",
        ))
}

fn flag(m: &ArgMatches, name: &str) -> bool {
    m.try_get_one::<bool>(name)
        .ok()
        .flatten()
        .copied()
        .unwrap_or(false)
}

#[allow(clippy::unnecessary_wraps)]
pub fn run(matches: &ArgMatches) -> Result<shellfirm::CmdExit> {
    let Some((name, sub)) = matches.subcommand() else {
        return Ok(shellfirm::CmdExit {
            code: 2,
            message: Some(format!("Specify an AI tool: {SUPPORTED_PROVIDERS}")),
        });
    };
    let Some(provider) = Provider::from_name(name) else {
        return Ok(shellfirm::CmdExit {
            code: 2,
            message: Some(format!(
                "Unknown AI tool: {name}. Supported: {SUPPORTED_PROVIDERS}"
            )),
        });
    };
    let Some(home) = dirs::home_dir() else {
        return Ok(shellfirm::CmdExit {
            code: 1,
            message: Some("Could not determine your home directory".into()),
        });
    };
    let hooks_only = flag(sub, "hooks-only");
    let mcp_only = flag(sub, "mcp-only");
    let scope = match sub
        .try_get_one::<String>("scope")
        .ok()
        .flatten()
        .map(String::as_str)
    {
        Some("project") => Scope::Project,
        _ => Scope::User,
    };
    let opts = Options {
        dry_run: flag(sub, "dry-run"),
        uninstall: flag(sub, "uninstall"),
        hooks: !mcp_only && provider.supports_hooks(),
        mcp: !hooks_only,
        scope,
        fail_closed: !flag(sub, "no-fail-closed"),
    };
    let env = ConnectEnv {
        home,
        cwd: std::env::current_dir().unwrap_or_default(),
        backup_dir: (scope == Scope::Project)
            .then(|| dirs::cache_dir().map(|d| d.join("shellfirm").join("backups")))
            .flatten(),
        config_dir: shellfirm::Config::new(None).ok().map(|c| c.root_folder),
    };

    let mut out = String::new();
    let res = execute(provider, &opts, &env, &mut out);
    {
        let mut stdout = std::io::stdout().lock();
        if let Err(e) = stdout
            .write_all(out.as_bytes())
            .and_then(|()| stdout.flush())
        {
            return Ok(shellfirm::CmdExit {
                code: 74,
                message: Some(format!("shellfirm: error: writing output: {e}")),
            });
        }
    }
    Ok(match res {
        Ok(()) => shellfirm::CmdExit {
            code: 0,
            message: None,
        },
        Err(e) => shellfirm::CmdExit {
            code: 1,
            message: Some(format!("Error: {e}")),
        },
    })
}

/// Run the whole connect/uninstall flow, writing user-facing output to `out`.
#[allow(clippy::too_many_lines, clippy::trivially_copy_pass_by_ref)]
fn execute(provider: Provider, opts: &Options, env: &ConnectEnv, out: &mut String) -> OpResult<()> {
    let verb = if opts.uninstall {
        "removing"
    } else {
        "connecting"
    };
    let _ = writeln!(
        out,
        "\nshellfirm {} {verb} {} integration{}\n",
        shellfirm::config::glyph("—", "-"),
        provider.display_name(),
        if opts.dry_run { " (dry run)" } else { "" }
    );

    if !opts.hooks && !opts.mcp {
        return Err(format!(
            "{} has no command hook; run `shellfirm connect {}` without --hooks-only",
            provider.display_name(),
            match provider {
                Provider::Windsurf => "windsurf",
                Provider::Zed => "zed",
                Provider::Cline => "cline",
                _ => "<tool>",
            }
        ));
    }
    if opts.scope == Scope::Project && !provider.supports_scope() {
        return Err(format!(
            "{} does not support --scope project",
            provider.display_name()
        ));
    }

    // 1. Load and parse every file first; abort before writing anything.
    let files = plan(provider, opts, env)?;
    let mut loaded = Vec::new();
    for (path, ops) in files {
        loaded.push((load_file(&path)?, ops));
    }

    // 2. Compute changes in memory.
    let spec = InstallSpec {
        // A project-scope file is shared with the team: no personal paths.
        config_dir: if opts.scope == Scope::User {
            env.config_dir.as_ref().map(|d| d.display().to_string())
        } else {
            None
        },
        fail_closed: opts.fail_closed,
    };
    let mut changes: Vec<(&LoadedFile, Value, Vec<String>, SaveMode)> = Vec::new();
    let mut already: Vec<Component> = Vec::new();
    for (file, ops) in &loaded {
        let mut value = file.value.clone();
        let mut actions = Vec::new();
        for op in ops {
            let acts = if opts.uninstall {
                uninstall(*op, &mut value, &file.path)?
            } else {
                install(*op, &mut value, &file.path, &spec)?
            };
            if acts.is_empty() && !opts.uninstall && *op != OpKind::LegacyClaudeSettingsMcp {
                already.push(op.component());
            }
            actions.extend(acts);
        }
        if !actions.is_empty() {
            // Don't leave `{}` stubs behind (a symlinked file is kept).
            // Asked of the file itself: its canonical path also differs
            // when a parent directory is a link (macOS `/var` →
            // `/private/var`) and always on Windows (`\\?\` prefix).
            let is_symlink =
                fs::symlink_metadata(&file.path).is_ok_and(|m| m.file_type().is_symlink());
            let mode = if opts.uninstall && is_empty_stub(&value, ops) && !is_symlink {
                actions.push("delete the file (only shellfirm's entries were in it)".into());
                SaveMode::Remove
            } else {
                SaveMode::Write
            };
            changes.push((file, value, actions, mode));
        }
    }

    for c in &already {
        let _ = writeln!(
            out,
            "  = {} already installed",
            if *c == Component::Hooks {
                "Hook"
            } else {
                "MCP server"
            }
        );
    }
    // Settings the user chose on existing hooks are kept: say so, with the
    // values shellfirm would have written.
    if !opts.uninstall {
        for (file, _) in &loaded {
            let value = changes
                .iter()
                .find(|c| std::ptr::eq(c.0, file))
                .map_or(&file.value, |c| &c.1);
            let kept = kept_hook_settings(value);
            if !kept.is_empty() {
                let _ = writeln!(
                    out,
                    "  ! kept your own hook settings in {} ({}); shellfirm writes timeout {HOOK_TIMEOUT_SECS}s{} \
                     on new hooks. Edit the file to change them.",
                    file.path.display(),
                    kept.join(", "),
                    if opts.fail_closed {
                        " and onFailure: block (fail closed)"
                    } else {
                        ""
                    }
                );
            }
        }
    }

    if changes.is_empty() {
        let _ = writeln!(
            out,
            "\nNothing to change: {}",
            if opts.uninstall {
                format!(
                    "no shellfirm configuration found for {}.",
                    provider.display_name()
                )
            } else {
                format!(
                    "shellfirm is already connected to {}.",
                    provider.display_name()
                )
            }
        );
        return Ok(());
    }

    // 3. Show the plan / write.
    let mut written: Vec<String> = Vec::new();
    let backup_dir = env.backup_dir.as_deref();
    for (file, value, actions, mode) in &changes {
        if opts.dry_run {
            let _ = writeln!(
                out,
                "  Would {} {}:",
                if file.exists { "update" } else { "create" },
                file.path.display()
            );
        } else {
            let _ = writeln!(out, "  {}:", file.path.display());
        }
        for a in actions {
            let _ = writeln!(out, "    - {a}");
        }
        if file.lossy_format {
            let _ = writeln!(
                out,
                "    ! this file contains comments or trailing commas, which will not be kept (a backup is made first)"
            );
        }
        if opts.dry_run {
            if file.exists {
                let _ = writeln!(
                    out,
                    "    (a timestamped backup would be written first{})",
                    backup_dir.map_or_else(String::new, |d| format!(" to {}", d.display()))
                );
            }
            continue;
        }
        match save_file(file, value, backup_dir, *mode) {
            Ok(backup) => {
                written.push(file.path.display().to_string());
                if *mode == SaveMode::Remove && opts.scope == Scope::Project {
                    // `.claude` / `.cursor` that shellfirm emptied (fails if not empty).
                    if let Some(parent) = file.path.parent().filter(|p| *p != env.cwd) {
                        let _ = fs::remove_dir(parent);
                    }
                }
                let _ = writeln!(
                    out,
                    "    ✓ {}{}",
                    if *mode == SaveMode::Remove {
                        "removed"
                    } else {
                        "written"
                    },
                    backup.map_or_else(String::new, |b| format!(" (backup: {})", b.display()))
                );
            }
            Err(e) => {
                if written.is_empty() {
                    return Err(e);
                }
                return Err(format!(
                    "{e}. Already updated before the failure: {}",
                    written.join(", ")
                ));
            }
        }
    }

    out.push('\n');
    if opts.dry_run {
        out.push_str("Dry run: no changes were made. Run without --dry-run to apply.\n");
        return Ok(());
    }
    if opts.uninstall {
        let _ = writeln!(out, "shellfirm removed from {}.", provider.display_name());
        return Ok(());
    }
    let note = match provider {
        Provider::ClaudeCode if opts.hooks => {
            "Claude Code Bash/PowerShell/Monitor commands are now checked by shellfirm before they run \
             (risky -> ask, dangerous -> deny; everything else follows your normal \
             Claude Code permissions). Restart running Claude Code sessions."
        }
        Provider::Cursor if opts.hooks => {
            "Cursor shell commands are now checked by shellfirm (beforeShellExecution hook), \
             and agent edits of protected files are blocked (preToolUse hook). \
             Restart Cursor to pick up the change."
        }
        _ => {
            "MCP tools registered. Note: commands are NOT automatically intercepted — the \
             agent has to choose to call shellfirm's tools."
        }
    };
    let _ = writeln!(out, "{note}");
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn opts(uninstall: bool, hooks: bool, mcp: bool) -> Options {
        Options {
            dry_run: false,
            uninstall,
            hooks,
            mcp,
            scope: Scope::User,
            fail_closed: true,
        }
    }

    struct Sandbox {
        tree: tree_fs::Tree,
        env: ConnectEnv,
    }

    fn sandbox() -> Sandbox {
        let tree = tree_fs::TreeBuilder::default().create().unwrap();
        let home = tree.root.join("home");
        let cwd = tree.root.join("project");
        fs::create_dir_all(&home).unwrap();
        fs::create_dir_all(&cwd).unwrap();
        Sandbox {
            env: ConnectEnv {
                home,
                cwd,
                backup_dir: None,
                config_dir: None,
            },
            tree,
        }
    }

    fn write(path: &Path, content: &str) {
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, content).unwrap();
    }

    fn read_json(path: &Path) -> Value {
        serde_json::from_str(&fs::read_to_string(path).unwrap()).unwrap()
    }

    fn backups(dir: &Path) -> Vec<PathBuf> {
        fs::read_dir(dir)
            .unwrap()
            .filter_map(|e| e.ok().map(|e| e.path()))
            .filter(|p| p.to_string_lossy().contains(".shellfirm-bak-"))
            .collect()
    }

    fn exec(p: Provider, o: &Options, env: &ConnectEnv) -> (OpResult<()>, String) {
        let mut out = String::new();
        let r = execute(p, o, env, &mut out);
        (r, out)
    }

    // -- JSONC --

    #[test]
    fn jsonc_comments_and_trailing_commas_parse() {
        let src = "\u{feff}{\n // c\n \"model\":\"opus\", /* x */\n \"url\": \"http://a//b\",\n \"permissions\":{\"allow\":[\"Bash(ls)\",],},\n}";
        let (v, lossy) = parse_jsonc(src).unwrap();
        assert!(lossy);
        assert_eq!(v["model"], "opus");
        assert_eq!(v["url"], "http://a//b");
        assert_eq!(v["permissions"]["allow"][0], "Bash(ls)");
    }

    #[test]
    fn strict_json_is_not_lossy() {
        let (v, lossy) = parse_jsonc(r#"{"a": "// not a comment, ]"}"#).unwrap();
        assert!(!lossy);
        assert_eq!(v["a"], "// not a comment, ]");
        assert!(parse_jsonc("").unwrap().0.is_object());
    }

    #[test]
    fn garbage_does_not_parse() {
        assert!(parse_jsonc("{not json").is_err());
        assert!(parse_jsonc("{\"a\": // c\n").is_err());
    }

    // -- Claude Code end-to-end on temp dirs --

    #[test]
    fn claude_fresh_install_writes_hook_and_mcp_to_correct_files() {
        let sb = sandbox();
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env);
        r.unwrap();
        let settings = read_json(&sb.env.home.join(".claude/settings.json"));
        let cmd = &settings["hooks"]["PreToolUse"][0]["hooks"][0]["command"];
        assert_eq!(cmd, "shellfirm check --hook claude-code");
        assert_eq!(
            settings["hooks"]["PreToolUse"][0]["matcher"],
            "Bash|PowerShell|Monitor"
        );
        let entry = &settings["hooks"]["PreToolUse"][0]["hooks"][0];
        assert_eq!(entry["onFailure"], "block");
        assert_eq!(entry["timeout"], HOOK_TIMEOUT_SECS);
        assert_eq!(
            settings["hooks"]["PreToolUse"][1]["matcher"],
            "Write|Edit|MultiEdit|NotebookEdit"
        );
        assert_eq!(
            claude_hook_state(&settings).covered,
            [
                "Bash",
                "PowerShell",
                "Monitor",
                "Write",
                "Edit",
                "MultiEdit",
                "NotebookEdit",
                "Read"
            ]
        );
        assert!(settings.get("mcpServers").is_none());
        let claude_json = read_json(&sb.env.home.join(".claude.json"));
        assert_eq!(claude_json["mcpServers"]["shellfirm"]["type"], "stdio");
        assert_eq!(claude_json["mcpServers"]["shellfirm"]["args"][0], "mcp");
        assert!(out.contains("✓ written"));

        // idempotent
        let before = fs::read(sb.env.home.join(".claude/settings.json")).unwrap();
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env);
        r.unwrap();
        assert!(out.contains("already connected"), "{out}");
        assert_eq!(
            before,
            fs::read(sb.env.home.join(".claude/settings.json")).unwrap()
        );
    }

    #[test]
    fn claude_install_preserves_jsonc_content_and_key_order_and_backs_up() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        let original = "{\n // c\n \"model\":\"opus\",\n \"permissions\":{\"deny\":[\"Bash(rm:*)\"],},\n \"env\": {\"Z\": \"1\", \"A\": \"2\"}\n}";
        write(&path, original);
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
        r.unwrap();
        assert!(out.contains("comments or trailing commas"));
        let v = read_json(&path);
        assert_eq!(v["model"], "opus");
        assert_eq!(v["permissions"]["deny"][0], "Bash(rm:*)");
        let keys: Vec<&String> = v.as_object().unwrap().keys().collect();
        assert_eq!(keys, ["model", "permissions", "env", "hooks"]);
        let env_keys: Vec<&String> = v["env"].as_object().unwrap().keys().collect();
        assert_eq!(env_keys, ["Z", "A"]);
        let baks = backups(path.parent().unwrap());
        assert_eq!(baks.len(), 1);
        assert_eq!(fs::read_to_string(&baks[0]).unwrap(), original);
    }

    #[test]
    fn unparseable_file_aborts_without_writing() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        write(&path, "{ \"model\": \"opus\", oops");
        let (r, _) = exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env);
        let err = r.unwrap_err();
        assert!(err.contains("could not parse"), "{err}");
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            "{ \"model\": \"opus\", oops"
        );
        assert!(
            !sb.env.home.join(".claude.json").exists(),
            "nothing else written"
        );
        assert!(backups(path.parent().unwrap()).is_empty());
    }

    #[test]
    fn unexpected_shapes_are_errors_not_panics() {
        for content in [
            "[1,2,3]",
            r#"{"hooks":"oops"}"#,
            r#"{"hooks":{"PreToolUse":{"matcher":"Bash"}}}"#,
            "\"str\"",
        ] {
            let sb = sandbox();
            let path = sb.env.home.join(".claude/settings.json");
            write(&path, content);
            let (r, _) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
            assert!(r.is_err(), "expected error for {content}");
            assert_eq!(fs::read_to_string(&path).unwrap(), content);
        }
        let sb = sandbox();
        write(&sb.env.home.join(".claude.json"), r#"{"mcpServers": []}"#);
        let (r, _) = exec(Provider::ClaudeCode, &opts(false, false, true), &sb.env);
        assert!(r.unwrap_err().contains("mcpServers"));
    }

    #[cfg(unix)]
    #[test]
    fn read_only_file_is_a_clear_error() {
        use std::os::unix::fs::PermissionsExt;
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        write(&path, "{}");
        fs::set_permissions(&path, fs::Permissions::from_mode(0o444)).unwrap();
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
        assert!(r.unwrap_err().contains("read-only"));
        assert!(!out.contains('✓'));
        fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn symlink_target_is_written_and_mode_kept() {
        use std::os::unix::fs::PermissionsExt;
        let sb = sandbox();
        let real = sb.env.home.join("dotfiles/settings.json");
        write(&real, "{\"model\":\"x\"}");
        fs::set_permissions(&real, fs::Permissions::from_mode(0o600)).unwrap();
        fs::create_dir_all(sb.env.home.join(".claude")).unwrap();
        let link = sb.env.home.join(".claude/settings.json");
        std::os::unix::fs::symlink(&real, &link).unwrap();
        exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env)
            .0
            .unwrap();
        assert!(fs::symlink_metadata(&link)
            .unwrap()
            .file_type()
            .is_symlink());
        assert_eq!(read_json(&real)["model"], "x");
        assert!(claude_hooks_installed(&read_json(&real)));
        let mode = fs::metadata(&real).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);
    }

    #[test]
    fn uninstall_removes_only_exact_shellfirm_hooks() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        write(
            &path,
            r#"{"hooks":{"PreToolUse":[
                {"matcher":"Bash","hooks":[{"type":"command","command":"/opt/myaudit.sh"},
                                           {"type":"command","command":"shellfirm check --stdin --format json --exit-code"}]},
                {"matcher":"Edit","hooks":[{"type":"command","command":"~/tools/shellfirm-lint-notes.sh"}]},
                {"matcher":"Bash","hooks":[{"type":"command","command":"/usr/local/bin/shellfirm check --hook claude-code"}]}
            ]},"mcpServers":{"shellfirm":{"command":"shellfirm","args":["mcp"]},"other":{"command":"o"}}}"#,
        );
        let (r, out) = exec(Provider::ClaudeCode, &opts(true, true, true), &sb.env);
        r.unwrap();
        assert!(out.contains("remove 2 shellfirm hook(s)"), "{out}");
        let v = read_json(&path);
        let groups = v["hooks"]["PreToolUse"].as_array().unwrap();
        assert_eq!(groups.len(), 2);
        assert_eq!(groups[0]["hooks"][0]["command"], "/opt/myaudit.sh");
        assert_eq!(groups[0]["hooks"].as_array().unwrap().len(), 1);
        assert_eq!(
            groups[1]["hooks"][0]["command"],
            "~/tools/shellfirm-lint-notes.sh"
        );
        // stale legacy MCP entry removed, other server kept
        assert!(v["mcpServers"].get("shellfirm").is_none());
        assert_eq!(v["mcpServers"]["other"]["command"], "o");
    }

    #[test]
    fn uninstall_cleans_up_containers_it_emptied() {
        let sb = sandbox();
        exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env)
            .0
            .unwrap();
        exec(Provider::ClaudeCode, &opts(true, true, true), &sb.env)
            .0
            .unwrap();
        // files that only held shellfirm's entries are removed, not left as `{}`
        assert!(!sb.env.home.join(".claude/settings.json").exists());
        assert!(!sb.env.home.join(".claude.json").exists());
        let (r, out) = exec(Provider::ClaudeCode, &opts(true, true, true), &sb.env);
        r.unwrap();
        assert!(out.contains("no shellfirm configuration found"));
    }

    #[test]
    fn uninstall_hooks_only_keeps_mcp() {
        let sb = sandbox();
        exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env)
            .0
            .unwrap();
        exec(Provider::ClaudeCode, &opts(true, true, false), &sb.env)
            .0
            .unwrap();
        assert!(!sb.env.home.join(".claude/settings.json").exists());
        assert!(server_installed(
            &read_json(&sb.env.home.join(".claude.json")),
            "mcpServers"
        ));
    }

    #[test]
    fn existing_custom_mcp_entry_is_kept() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude.json");
        write(
            &path,
            r#"{"numStartups": 3, "mcpServers": {"shellfirm": {"command": "/opt/sf", "args": ["mcp", "--x"]}}}"#,
        );
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, false, true), &sb.env);
        r.unwrap();
        assert!(out.contains("already"));
        assert_eq!(
            read_json(&path)["mcpServers"]["shellfirm"]["command"],
            "/opt/sf"
        );
    }

    #[test]
    fn legacy_settings_mcp_entry_is_migrated() {
        let sb = sandbox();
        let settings = sb.env.home.join(".claude/settings.json");
        write(
            &settings,
            r#"{"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"shellfirm check --stdin --format json --exit-code"}]}]},"mcpServers":{"shellfirm":{"command":"shellfirm","args":["mcp"]}}}"#,
        );
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env);
        r.unwrap();
        assert!(out.contains("replacing 1 legacy command line"), "{out}");
        let v = read_json(&settings);
        assert!(v.get("mcpServers").is_none());
        assert!(claude_hooks_installed(&v), "legacy hook upgraded in place");
        let groups = v["hooks"]["PreToolUse"].as_array().unwrap();
        assert_eq!(
            groups[0]["hooks"].as_array().unwrap().len(),
            1,
            "no duplicate"
        );
        assert_eq!(
            groups[0]["hooks"][0]["command"],
            "shellfirm check --hook claude-code"
        );
        assert_eq!(claude_hook_state(&v).legacy, 0);
        assert!(server_installed(
            &read_json(&sb.env.home.join(".claude.json")),
            "mcpServers"
        ));
    }

    #[test]
    fn project_scope_uses_project_files() {
        let sb = sandbox();
        let o = Options {
            scope: Scope::Project,
            ..opts(false, true, true)
        };
        exec(Provider::ClaudeCode, &o, &sb.env).0.unwrap();
        assert!(claude_hooks_installed(&read_json(
            &sb.env.cwd.join(".claude/settings.json")
        )));
        assert!(server_installed(
            &read_json(&sb.env.cwd.join(".mcp.json")),
            "mcpServers"
        ));
        assert!(!sb.env.home.join(".claude.json").exists());
    }

    #[test]
    fn dry_run_writes_nothing_and_has_no_checkmarks() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        write(&path, r#"{"model":"opus"}"#);
        let o = Options {
            dry_run: true,
            ..opts(false, true, true)
        };
        let (r, out) = exec(Provider::ClaudeCode, &o, &sb.env);
        r.unwrap();
        assert!(!out.contains('✓'), "{out}");
        assert!(out.contains("Would update"));
        assert!(out.contains("Would create"));
        assert!(out.trim_end().ends_with("Run without --dry-run to apply."));
        assert_eq!(fs::read_to_string(&path).unwrap(), r#"{"model":"opus"}"#);
        assert!(!sb.env.home.join(".claude.json").exists());
        assert!(backups(path.parent().unwrap()).is_empty());

        let o = Options {
            dry_run: true,
            ..opts(true, true, true)
        };
        exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env)
            .0
            .unwrap();
        let before = fs::read(&path).unwrap();
        exec(Provider::ClaudeCode, &o, &sb.env).0.unwrap();
        assert_eq!(before, fs::read(&path).unwrap());
    }

    // -- Cursor --

    #[test]
    fn cursor_user_scope_hooks_and_mcp() {
        let sb = sandbox();
        let (r, out) = exec(Provider::Cursor, &opts(false, true, true), &sb.env);
        r.unwrap();
        let hooks = read_json(&sb.env.home.join(".cursor/hooks.json"));
        assert_eq!(hooks["version"], 1);
        assert_eq!(
            hooks["hooks"]["beforeShellExecution"][0]["command"],
            "shellfirm check --hook cursor"
        );
        assert!(server_installed(
            &read_json(&sb.env.home.join(".cursor/mcp.json")),
            "mcpServers"
        ));
        assert!(!sb.env.cwd.join(".cursor").exists(), "project untouched");
        assert!(out.contains("beforeShellExecution"));

        exec(Provider::Cursor, &opts(true, true, true), &sb.env)
            .0
            .unwrap();
        assert!(!sb.env.home.join(".cursor/hooks.json").exists());
        assert!(!sb.env.home.join(".cursor/mcp.json").exists());
    }

    // -- MCP-only tools --

    #[test]
    fn zed_jsonc_settings_are_kept_and_message_is_honest() {
        let sb = sandbox();
        let path = sb.env.home.join(".config/zed/settings.json");
        write(
            &path,
            "// Zed settings\n{\n  \"theme\": \"One Dark\",\n  \"vim_mode\": true,\n  \"context_servers\": {\"github\": {\"command\": \"gh-mcp\"}},\n}\n",
        );
        let (r, out) = exec(Provider::Zed, &opts(false, false, true), &sb.env);
        r.unwrap();
        let v = read_json(&path);
        assert_eq!(v["theme"], "One Dark");
        assert_eq!(v["vim_mode"], true);
        assert_eq!(v["context_servers"]["github"]["command"], "gh-mcp");
        assert_eq!(v["context_servers"]["shellfirm"]["command"], "shellfirm");
        assert!(out.contains("NOT automatically intercepted"), "{out}");
        assert!(!out.contains("protected"));
    }

    #[test]
    fn mcp_only_providers_install_and_uninstall() {
        for p in [Provider::Windsurf, Provider::Cline, Provider::Zed] {
            let sb = sandbox();
            if p == Provider::Cline && cline_settings_path(&sb.env.home).is_none() {
                continue;
            }
            exec(p, &opts(false, false, true), &sb.env).0.unwrap();
            let file = plan(p, &opts(false, false, true), &sb.env).unwrap()[0]
                .0
                .clone();
            let container = if p == Provider::Zed {
                "context_servers"
            } else {
                "mcpServers"
            };
            assert!(server_installed(&read_json(&file), container), "{p:?}");
            exec(p, &opts(true, false, true), &sb.env).0.unwrap();
            assert!(!file.exists(), "{p:?}");
        }
    }

    #[test]
    fn hooks_only_on_mcp_only_tool_is_error() {
        let sb = sandbox();
        let (r, _) = exec(Provider::Zed, &opts(false, false, false), &sb.env);
        assert!(r.unwrap_err().contains("no command hook"));
    }

    #[test]
    fn hook_command_recognition_accepts_shellfirm_flags_only() {
        assert!(is_claude_hook(
            "shellfirm check --hook claude-code --log debug"
        ));
        assert!(is_claude_hook(
            "shellfirm --log=debug check --hook=claude-code"
        ));
        assert!(is_claude_hook(
            "shellfirm check --hook claude-code --config-dir '/Users/me/Library/Application Support/shellfirm'"
        ));
        assert_eq!(
            parse_shellfirm_hook(
                "shellfirm check --hook claude-code --config-dir '/a b/c'",
                "claude-code"
            ),
            Some(HookCmd {
                legacy: false,
                config_dir: Some("/a b/c".into())
            })
        );
        assert!(!is_claude_hook("shellfirm check --hook cursor"));
        assert!(!is_claude_hook(
            "shellfirm check --hook claude-code; rm -rf ~"
        ));
        // rejected by current versions (usage error): recognised so it is rewritten
        assert!(
            parse_shellfirm_hook(
                "shellfirm check --hook claude-code --exit-code",
                "claude-code"
            )
            .unwrap()
            .legacy
        );
        assert!(!is_claude_hook("shellfirm check --hook 'claude-code"));
        assert!(!is_claude_hook("shellfirm config ignore x"));
        assert!(!is_cursor_hook(
            "shellfirm check --stdin --format json --exit-code"
        ));
        let mut legacy = String::from("shellfirm ");
        legacy.push_str(LEGACY_CLAUDE_HOOK_ARGS);
        assert!(parse_shellfirm_hook(&legacy, "claude-code").unwrap().legacy);
    }

    #[test]
    fn matcher_semantics_follow_claude_code_docs() {
        assert!(matcher_covers(None, "Bash"));
        assert!(matcher_covers(Some("*"), "Bash"));
        assert!(matcher_covers(Some(""), "Write"));
        assert!(matcher_covers(Some("Bash"), "Bash"));
        assert!(!matcher_covers(Some("Bash"), "Write"));
        assert!(matcher_covers(Some("Edit|Write"), "Write"));
        assert!(!matcher_covers(Some("Edit|Write"), "Bash"));
        assert!(
            !matcher_covers(Some("Edit"), "NotebookEdit"),
            "exact, not regex"
        );
        assert!(
            matcher_covers(Some("Edit.*"), "NotebookEdit"),
            "regex, unanchored"
        );
        assert!(matcher_covers(Some("^Ba"), "Bash"));
    }

    #[test]
    fn only_bash_covering_hooks_count_as_installed() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        write(
            &path,
            r#"{"hooks":{"PreToolUse":[{"matcher":"Edit|Write","hooks":[{"type":"command","command":"shellfirm check --hook claude-code --log debug"}]}]}}"#,
        );
        let v = read_json(&path);
        assert!(!claude_hooks_installed(&v));
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
        r.unwrap();
        assert!(!out.contains("already installed"), "{out}");
        let v = read_json(&path);
        let groups = v["hooks"]["PreToolUse"].as_array().unwrap();
        assert_eq!(groups.len(), 4, "{v}");
        assert_eq!(groups[1]["matcher"], "Bash|PowerShell|Monitor");
        assert_eq!(groups[2]["matcher"], "MultiEdit|NotebookEdit");
        assert_eq!(groups[3]["matcher"], "Read");
        // user flags are kept, fail-closed options added
        assert_eq!(
            groups[0]["hooks"][0]["command"],
            "shellfirm check --hook claude-code --log debug"
        );
        assert_eq!(groups[0]["hooks"][0]["onFailure"], "block");
        // idempotent
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
        r.unwrap();
        assert!(out.contains("Hook already installed"), "{out}");
        // uninstall removes the `--log debug` hook too
        exec(Provider::ClaudeCode, &opts(true, true, false), &sb.env)
            .0
            .unwrap();
        assert!(!path.exists());
    }

    #[test]
    fn user_scope_pins_config_dir_and_upgrades_existing_hooks() {
        let mut sb = sandbox();
        sb.env.config_dir = Some(PathBuf::from(
            "/Users/me/Library/Application Support/shellfirm",
        ));
        let path = sb.env.home.join(".claude/settings.json");
        write(
            &path,
            r#"{"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"shellfirm check --hook claude-code"}]}]}}"#,
        );
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env);
        r.unwrap();
        assert!(out.contains("pinned --config-dir"), "{out}");
        let v = read_json(&path);
        let expected = "shellfirm check --hook claude-code --config-dir '/Users/me/Library/Application Support/shellfirm'";
        assert_eq!(v["hooks"]["PreToolUse"][0]["hooks"][0]["command"], expected);
        assert_eq!(v["hooks"]["PreToolUse"][1]["hooks"][0]["command"], expected);
        let mcp = read_json(&sb.env.home.join(".claude.json"));
        assert_eq!(mcp["mcpServers"]["shellfirm"]["args"][1], "--config-dir");
        // the recorded dir parses back
        assert_eq!(
            claude_hook_state(&v).config_dirs,
            ["/Users/me/Library/Application Support/shellfirm"]
        );
    }

    #[test]
    fn no_fail_closed_omits_on_failure() {
        let sb = sandbox();
        let o = Options {
            fail_closed: false,
            ..opts(false, true, false)
        };
        exec(Provider::ClaudeCode, &o, &sb.env).0.unwrap();
        let v = read_json(&sb.env.home.join(".claude/settings.json"));
        assert!(v["hooks"]["PreToolUse"][0]["hooks"][0]
            .get("onFailure")
            .is_none());
        assert_eq!(
            v["hooks"]["PreToolUse"][0]["hooks"][0]["timeout"],
            HOOK_TIMEOUT_SECS
        );
    }

    #[test]
    fn project_scope_backups_go_outside_the_repo_and_no_stubs_remain() {
        let mut sb = sandbox();
        sb.env.backup_dir = Some(sb.tree.root.join("cache/backups"));
        let o = Options {
            scope: Scope::Project,
            ..opts(false, true, true)
        };
        let settings = sb.env.cwd.join(".claude/settings.json");
        write(&settings, r#"{"permissions":{"allow":["Bash(ls)"]}}"#);
        exec(Provider::ClaudeCode, &o, &sb.env).0.unwrap();
        let hook = &read_json(&settings)["hooks"]["PreToolUse"][0]["hooks"][0]["command"];
        assert_eq!(
            hook, "shellfirm check --hook claude-code",
            "no personal path"
        );
        let un = Options {
            uninstall: true,
            ..o
        };
        exec(Provider::ClaudeCode, &un, &sb.env).0.unwrap();
        assert_eq!(
            read_json(&settings),
            json!({"permissions":{"allow":["Bash(ls)"]}})
        );
        assert!(!sb.env.cwd.join(".mcp.json").exists(), "no {{}} stub");
        let in_repo: Vec<PathBuf> = backups(&sb.env.cwd)
            .into_iter()
            .chain(backups(&sb.env.cwd.join(".claude")))
            .collect();
        assert!(in_repo.is_empty(), "{in_repo:?}");
        let bdir = sb.env.backup_dir.clone().unwrap();
        assert!(backups(&bdir).len() >= 2, "{:?}", backups(&bdir));

        // created-from-scratch files and dirs disappear entirely
        let mut sb = sandbox();
        sb.env.backup_dir = Some(sb.tree.root.join("cache/backups"));
        exec(Provider::Cursor, &o, &sb.env).0.unwrap();
        assert!(sb.env.cwd.join(".cursor/hooks.json").exists());
        exec(
            Provider::Cursor,
            &Options {
                uninstall: true,
                ..o
            },
            &sb.env,
        )
        .0
        .unwrap();
        assert!(!sb.env.cwd.join(".cursor").exists());
    }

    #[test]
    fn integration_status_reports_hooks_and_mcp() {
        let sb = sandbox();
        exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env)
            .0
            .unwrap();
        let st = integration_status(&sb.env.home, &sb.env.cwd);
        let cc = st
            .iter()
            .find(|s| s.tool == "claude-code" && s.scope == "user")
            .unwrap();
        assert!(cc.hook_installed && cc.file_tools_covered && cc.fail_closed);
        assert!(cc.mcp_registered && !cc.legacy_hook);
        let cur = st
            .iter()
            .find(|s| s.tool == "cursor" && s.scope == "user")
            .unwrap();
        assert!(!cur.hook_installed && !cur.mcp_registered);
        assert!(st
            .iter()
            .any(|s| s.tool == "claude-code" && s.scope == "project"));
        let json = serde_json::to_value(cc).unwrap();
        assert_eq!(json["tool"], "claude-code");
    }

    #[test]
    fn hook_command_recognition_is_exact() {
        assert!(is_claude_hook("shellfirm check --hook claude-code"));
        assert!(is_claude_hook(
            "/opt/homebrew/bin/shellfirm  check --stdin --format json --exit-code"
        ));
        assert!(!is_claude_hook("~/tools/shellfirm-lint-notes.sh"));
        assert!(!is_claude_hook(
            "shellfirm check --hook claude-code && evil"
        ));
        assert!(!is_claude_hook("myshellfirm check --hook claude-code"));
        assert!(is_cursor_hook("shellfirm check --hook cursor"));
    }

    #[test]
    fn upgrade_adds_powershell_and_monitor_and_dedupes() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        write(
            &path,
            r#"{"hooks":{"PreToolUse":[
                {"matcher":"Bash","hooks":[{"type":"command","command":"shellfirm check --hook claude-code"}]},
                {"matcher":"Bash","hooks":[{"type":"command","command":"shellfirm check --hook claude-code --log debug"}]},
                {"matcher":"Write|Edit|MultiEdit|NotebookEdit","hooks":[
                    {"type":"command","command":"shellfirm check --hook claude-code"},
                    {"type":"command","command":"shellfirm check --hook claude-code"}]}
            ]}}"#,
        );
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
        r.unwrap();
        assert!(
            out.contains("remove 2 duplicate shellfirm hook(s)"),
            "{out}"
        );
        let v = read_json(&path);
        let groups = v["hooks"]["PreToolUse"].as_array().unwrap();
        let matchers: Vec<&str> = groups
            .iter()
            .map(|g| g["matcher"].as_str().unwrap())
            .collect();
        assert_eq!(
            matchers,
            [
                "Bash",
                "Write|Edit|MultiEdit|NotebookEdit",
                "PowerShell|Monitor",
                "Read"
            ],
            "{v}"
        );
        assert_eq!(claude_hook_state(&v).entries, 4);
        let st = integration_status(&sb.env.home, &sb.env.cwd);
        let cc = st
            .iter()
            .find(|s| s.tool == "claude-code" && s.scope == "user")
            .unwrap();
        assert!(cc.shell_tools_missing.is_empty() && cc.hook_present);
    }

    #[test]
    fn user_hook_settings_are_kept_and_reported() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(
            &path,
            r#"{"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"shellfirm check --hook claude-code","timeout":120,"onFailure":"allow"}]}]}}"#,
        )
        .unwrap();
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
        r.unwrap();
        assert!(
            out.contains("kept your own hook settings") && out.contains("onFailure: allow"),
            "{out}"
        );
        assert!(out.contains("timeout 120s"), "{out}");
        assert!(!out.contains("(timeout 30s, onFailure: block"), "{out}");
        let v = read_json(&path);
        assert_eq!(
            v["hooks"]["PreToolUse"][0]["hooks"][0]["onFailure"],
            "allow"
        );
        assert_eq!(v["hooks"]["PreToolUse"][0]["hooks"][0]["timeout"], 120);
        // The rerun still says so.
        let (r, out) = exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env);
        r.unwrap();
        assert!(out.contains("kept your own hook settings"), "{out}");
    }

    #[test]
    fn no_fail_closed_downgrades_existing_hooks() {
        let sb = sandbox();
        exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env)
            .0
            .unwrap();
        let o = Options {
            fail_closed: false,
            ..opts(false, true, false)
        };
        let (r, out) = exec(Provider::ClaudeCode, &o, &sb.env);
        r.unwrap();
        assert!(out.contains("onFailure removed"), "{out}");
        let v = read_json(&sb.env.home.join(".claude/settings.json"));
        assert!(v["hooks"]["PreToolUse"]
            .as_array()
            .unwrap()
            .iter()
            .all(|g| g["hooks"][0].get("onFailure").is_none()));
        // and back
        exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env)
            .0
            .unwrap();
        let v = read_json(&sb.env.home.join(".claude/settings.json"));
        assert_eq!(
            v["hooks"]["PreToolUse"][0]["hooks"][0]["onFailure"],
            "block"
        );
    }

    #[test]
    fn new_entries_reuse_recorded_custom_config_dir() {
        let mut sb = sandbox();
        sb.env.config_dir = Some(PathBuf::from("/home/me/.config/shellfirm"));
        let path = sb.env.home.join(".claude/settings.json");
        write(
            &path,
            r#"{"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"shellfirm check --stdin --format json --exit-code --config-dir /etc/team-shellfirm"}]}]}}"#,
        );
        exec(Provider::ClaudeCode, &opts(false, true, false), &sb.env)
            .0
            .unwrap();
        let v = read_json(&path);
        for g in v["hooks"]["PreToolUse"].as_array().unwrap() {
            assert_eq!(
                g["hooks"][0]["command"],
                "shellfirm check --hook claude-code --config-dir /etc/team-shellfirm",
                "{v}"
            );
        }
    }

    #[test]
    fn hook_format_spellings_are_recognised_as_legacy() {
        let h = parse_shellfirm_hook(
            "shellfirm check --stdin --format cursor-hook --exit-code",
            "cursor",
        )
        .unwrap();
        assert!(h.legacy);
        assert!(
            parse_shellfirm_hook("shellfirm check --format claude-hook", "claude-code")
                .unwrap()
                .legacy
        );
        assert!(
            parse_shellfirm_hook("shellfirm check --hook cursor --exit-code", "cursor")
                .unwrap()
                .legacy
        );
        assert!(
            parse_shellfirm_hook("shellfirm check --format cursor-hook", "claude-code").is_none()
        );
        assert!(
            !parse_shellfirm_hook("shellfirm check --hook cursor", "cursor")
                .unwrap()
                .legacy
        );
    }

    #[test]
    fn cursor_upgrade_removes_fail_closed_and_adds_file_hook() {
        let sb = sandbox();
        let path = sb.env.home.join(".cursor/hooks.json");
        write(
            &path,
            r#"{"version":1,"hooks":{"beforeShellExecution":[
                {"command":"shellfirm check --stdin --format cursor-hook --exit-code","failClosed":true},
                {"command":"shellfirm check --hook cursor"},
                {"command":"./mine.sh","failClosed":true}]}}"#,
        );
        let (r, out) = exec(Provider::Cursor, &opts(false, true, false), &sb.env);
        r.unwrap();
        assert!(out.contains("failClosed"), "{out}");
        let v = read_json(&path);
        let shell = v["hooks"]["beforeShellExecution"].as_array().unwrap();
        assert_eq!(shell.len(), 2, "{v}");
        assert_eq!(shell[0]["command"], "shellfirm check --hook cursor");
        assert!(shell[0].get("failClosed").is_none());
        assert_eq!(shell[1]["failClosed"], true, "foreign hook untouched");
        assert_eq!(v["hooks"]["preToolUse"][0]["matcher"], CURSOR_FILE_MATCHER);
        let st = integration_status(&sb.env.home, &sb.env.cwd);
        let cur = st
            .iter()
            .find(|s| s.tool == "cursor" && s.scope == "user")
            .unwrap();
        assert!(cur.hook_installed && cur.file_tools_covered && !cur.legacy_hook);
        // idempotent, then uninstall removes both events
        let (_, out) = exec(Provider::Cursor, &opts(false, true, false), &sb.env);
        assert!(out.contains("Hook already installed"), "{out}");
        exec(Provider::Cursor, &opts(true, true, false), &sb.env)
            .0
            .unwrap();
        let v = read_json(&path);
        assert!(v["hooks"].get("preToolUse").is_none(), "{v}");
        assert_eq!(
            v["hooks"]["beforeShellExecution"][0]["command"],
            "./mine.sh"
        );
    }

    #[test]
    fn status_flags_hook_without_bash() {
        let sb = sandbox();
        write(
            &sb.env.home.join(".claude/settings.json"),
            r#"{"hooks":{"PreToolUse":[{"matcher":"Edit|Write","hooks":[{"type":"command","command":"shellfirm check --hook claude-code"}]}]}}"#,
        );
        let st = integration_status(&sb.env.home, &sb.env.cwd);
        let cc = st
            .iter()
            .find(|s| s.tool == "claude-code" && s.scope == "user")
            .unwrap();
        assert!(cc.hook_present && !cc.hook_installed);
        assert_eq!(cc.shell_tools_missing, ["Bash", "PowerShell", "Monitor"]);
    }

    #[test]
    fn cli_definition_is_valid() {
        command().debug_assert();
    }
}