shellfirm 0.4.2

`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
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
//! `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;

mod agents;

/// 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;

pub(super) 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,
    /// Codex CLI `hooks.json` `hooks.PreToolUse[]`.
    CodexHooks,
    /// Gemini CLI `settings.json` `hooks.BeforeTool[]`.
    GeminiHooks,
    /// Qwen Code `settings.json` `hooks.PreToolUse[]`.
    QwenHooks,
    /// GitHub Copilot CLI hook file `hooks.preToolUse[]`.
    CopilotHooks,
    /// Windsurf `hooks.json` `hooks.pre_run_command[]` (and file hooks).
    WindsurfHooks,
    /// Cline `PreToolUse` hook script (a file shellfirm owns).
    ClineScript,
    /// `OpenCode` plugin (a file shellfirm owns).
    OpenCodePlugin,
}

impl OpKind {
    const fn component(self) -> Component {
        match self {
            Self::ClaudeMcp | Self::LegacyClaudeSettingsMcp | Self::StandardMcp | Self::ZedMcp => {
                Component::Mcp
            }
            _ => Component::Hooks,
        }
    }

    /// A file shellfirm owns entirely (not a JSON file it edits).
    const fn is_owned_file(self) -> bool {
        matches!(self, Self::ClineScript | Self::OpenCodePlugin)
    }
}

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

const SUPPORTED_PROVIDERS: &str =
    "claude-code, cursor, codex, gemini, qwen, copilot, windsurf, cline, opencode, zed";

/// Every provider, in `status` order.
const ALL_PROVIDERS: [Provider; 10] = [
    Provider::ClaudeCode,
    Provider::Cursor,
    Provider::Codex,
    Provider::Gemini,
    Provider::Qwen,
    Provider::Copilot,
    Provider::Windsurf,
    Provider::Cline,
    Provider::OpenCode,
    Provider::Zed,
];

impl Provider {
    fn from_name(name: &str) -> Option<Self> {
        ALL_PROVIDERS.into_iter().find(|p| p.cli_name() == name)
    }

    const fn cli_name(self) -> &'static str {
        match self {
            Self::ClaudeCode => "claude-code",
            Self::Cursor => "cursor",
            Self::Codex => "codex",
            Self::Gemini => "gemini",
            Self::Qwen => "qwen",
            Self::Copilot => "copilot",
            Self::Windsurf => "windsurf",
            Self::Cline => "cline",
            Self::OpenCode => "opencode",
            Self::Zed => "zed",
        }
    }

    const fn display_name(self) -> &'static str {
        match self {
            Self::ClaudeCode => "Claude Code",
            Self::Cursor => "Cursor",
            Self::Codex => "Codex CLI",
            Self::Gemini => "Gemini CLI",
            Self::Qwen => "Qwen Code",
            Self::Copilot => "GitHub Copilot CLI",
            Self::Windsurf => "Windsurf",
            Self::Cline => "Cline",
            Self::OpenCode => "OpenCode",
            Self::Zed => "Zed",
        }
    }

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

    /// shellfirm registers its MCP server with this tool.
    const fn supports_mcp(self) -> bool {
        matches!(
            self,
            Self::ClaudeCode | Self::Cursor | Self::Windsurf | Self::Cline | Self::Zed
        )
    }

    /// `--scope project` is available (for the hook; the MCP registration of
    /// Windsurf and Cline is user-level only).
    const fn supports_scope(self) -> bool {
        self.supports_hooks()
    }

    /// The MCP registration follows `--scope` (otherwise it is user-level).
    const fn mcp_follows_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>,
    /// Agent home directories moved by environment variables.
    dirs: AgentDirs,
}

/// Where agents keep their user-level configuration when an environment
/// variable moves it (`None`: the default under `home`).
#[derive(Debug, Clone, Default)]
struct AgentDirs {
    /// `CODEX_HOME` (default `~/.codex`).
    codex_home: Option<PathBuf>,
    /// `COPILOT_HOME` (default `~/.copilot`).
    copilot_home: Option<PathBuf>,
    /// `XDG_CONFIG_HOME` (default `~/.config`), for `OpenCode`.
    xdg_config_home: Option<PathBuf>,
    /// `CLAUDE_CONFIG_DIR` (default `~/.claude`; `.claude.json` moves into
    /// it too).
    claude_config_dir: Option<PathBuf>,
    /// Claude Code's managed settings file (read for `disableAllHooks`).
    claude_managed: Option<PathBuf>,
    /// The running shellfirm binary: the Cline script and the `OpenCode`
    /// plugin fall back to it when `shellfirm` is not on the agent's PATH.
    current_exe: Option<PathBuf>,
}

impl AgentDirs {
    fn from_env() -> Self {
        let var = |k: &str| {
            std::env::var_os(k)
                .filter(|v| !v.is_empty())
                .map(PathBuf::from)
                .filter(|p| p.is_absolute())
        };
        Self {
            codex_home: var("CODEX_HOME"),
            copilot_home: var("COPILOT_HOME"),
            xdg_config_home: var("XDG_CONFIG_HOME"),
            claude_config_dir: claude_config_dir_from(
                std::env::var_os("CLAUDE_CONFIG_DIR"),
                std::env::current_dir().ok().as_deref(),
            ),
            claude_managed: Some(claude_managed_settings()),
            current_exe: std::env::current_exe().ok(),
        }
    }

    /// Claude Code's user directory: `$CLAUDE_CONFIG_DIR`, else `~/.claude`.
    fn claude_dir(&self, home: &Path) -> PathBuf {
        self.claude_config_dir
            .clone()
            .unwrap_or_else(|| home.join(".claude"))
    }

    /// Claude Code's `.claude.json` (user MCP servers): inside
    /// `$CLAUDE_CONFIG_DIR` when it is set, else `~/.claude.json`.
    fn claude_json(&self, home: &Path) -> PathBuf {
        self.claude_config_dir
            .as_ref()
            .map_or_else(|| home.join(".claude.json"), |d| d.join(".claude.json"))
    }
}

/// `$CLAUDE_CONFIG_DIR` as Claude Code uses it: a relative value (`rel`,
/// a literal `~/cc`) is relative to the current directory. Empty: unset.
fn claude_config_dir_from(
    value: Option<std::ffi::OsString>,
    cwd: Option<&Path>,
) -> Option<PathBuf> {
    let p = PathBuf::from(value.filter(|v| !v.is_empty())?);
    if p.is_absolute() {
        Some(p)
    } else {
        cwd.map(|c| c.join(p))
    }
}

/// Claude Code's user directory for `home`: `$CLAUDE_CONFIG_DIR` (a
/// relative one resolved against the current directory) or `~/.claude`.
#[must_use]
pub(super) fn claude_user_dir(home: &Path) -> PathBuf {
    AgentDirs::from_env().claude_dir(home)
}

/// Where Claude Code reads managed (organisation) settings on this platform.
#[must_use]
pub(super) fn claude_managed_settings() -> PathBuf {
    if cfg!(target_os = "macos") {
        PathBuf::from("/Library/Application Support/ClaudeCode/managed-settings.json")
    } else if cfg!(windows) {
        PathBuf::from(r"C:\Program Files\ClaudeCode\managed-settings.json")
    } else {
        PathBuf::from("/etc/claude-code/managed-settings.json")
    }
}

/// `path` (under `root`) reaches outside `root` through a symlink: the
/// file itself or a directory between `root` and it is a link that points
/// out of `root`, or a dangling link. Returns the offending link and where
/// it leads. A project-scope file is shared with the repository; a link
/// planted in it must not make shellfirm write another file (an agent's
/// user settings, say).
#[must_use]
pub(super) fn symlink_out_of(path: &Path, root: &Path) -> Option<(PathBuf, String)> {
    let rel = path.strip_prefix(root).ok()?;
    let root_canon = fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
    let mut cur = root.to_path_buf();
    for comp in rel.components() {
        cur.push(comp);
        if !fs::symlink_metadata(&cur).is_ok_and(|m| m.file_type().is_symlink()) {
            continue;
        }
        match fs::canonicalize(&cur) {
            Ok(target) if target.starts_with(&root_canon) => {}
            Ok(target) => return Some((cur, target.display().to_string())),
            Err(_) => return Some((cur, "a missing target".into())),
        }
    }
    None
}

/// The error for [`symlink_out_of`]; `scope` is `project` or `local`.
pub(super) fn symlink_refusal(path: &Path, link: &Path, target: &str, scope: &str) -> String {
    format!(
        "{} is a symlink to {target}, outside this project; shellfirm does not write through \
         it at {scope} scope (it would change {}). Remove the link, or edit the file by hand.",
        link.display(),
        if link == path {
            "that file".to_string()
        } else {
            format!("a file other than {}", path.display())
        }
    )
}

#[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, clippy::too_many_lines)]
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 = match opts.scope {
                Scope::User => env.dirs.claude_dir(&env.home).join("settings.json"),
                Scope::Project => 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.dirs.claude_json(&env.home), 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::Codex if opts.hooks => {
            let dir = match opts.scope {
                Scope::User => env
                    .dirs
                    .codex_home
                    .clone()
                    .unwrap_or_else(|| env.home.join(".codex")),
                Scope::Project => base.join(".codex"),
            };
            steps.push((dir.join("hooks.json"), OpKind::CodexHooks));
        }
        Provider::Gemini if opts.hooks => {
            steps.push((
                base.join(".gemini").join("settings.json"),
                OpKind::GeminiHooks,
            ));
        }
        Provider::Qwen if opts.hooks => {
            steps.push((base.join(".qwen").join("settings.json"), OpKind::QwenHooks));
        }
        Provider::Copilot if opts.hooks => {
            let dir = match opts.scope {
                Scope::User => env
                    .dirs
                    .copilot_home
                    .clone()
                    .unwrap_or_else(|| env.home.join(".copilot"))
                    .join("hooks"),
                Scope::Project => base.join(".github").join("hooks"),
            };
            steps.push((dir.join("shellfirm.json"), OpKind::CopilotHooks));
        }
        Provider::OpenCode if opts.hooks => {
            let dir = match opts.scope {
                Scope::User => env
                    .dirs
                    .xdg_config_home
                    .clone()
                    .unwrap_or_else(|| env.home.join(".config"))
                    .join("opencode"),
                Scope::Project => base.join(".opencode"),
            };
            steps.push((
                dir.join("plugins").join("shellfirm.js"),
                OpKind::OpenCodePlugin,
            ));
        }
        Provider::Codex
        | Provider::Gemini
        | Provider::Qwen
        | Provider::Copilot
        | Provider::OpenCode => {}
        Provider::Windsurf => {
            if opts.hooks {
                let path = match opts.scope {
                    Scope::User => env
                        .home
                        .join(".codeium")
                        .join("windsurf")
                        .join("hooks.json"),
                    Scope::Project => base.join(".windsurf").join("hooks.json"),
                };
                steps.push((path, OpKind::WindsurfHooks));
            }
            if opts.mcp && opts.scope == Scope::User {
                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 => {
            if opts.hooks {
                if cfg!(windows) {
                    return Err("shellfirm's Cline hook is a POSIX shell script; it is not \
                                available on Windows (use --mcp-only)"
                        .into());
                }
                let dir = match opts.scope {
                    Scope::User => env.home.join("Documents").join("Cline").join("Hooks"),
                    Scope::Project => base.join(".clinerules").join("hooks"),
                };
                steps.push((dir.join("PreToolUse"), OpKind::ClineScript));
            }
            if opts.mcp && opts.scope == Scope::User {
                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
// ---------------------------------------------------------------------------

pub(super) 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()
        )
    })
}

pub(super) 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()
        )),
    }
}

pub(super) 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";
/// Cursor hook event that runs before the agent reads a file (its payload
/// carries `file_path`); a read of a credential file is blocked.
const CURSOR_READ_EVENT: &str = "beforeReadFile";
/// Every Cursor event shellfirm writes an entry in.
const CURSOR_EVENTS: [&str; 3] = [CURSOR_SHELL_EVENT, CURSOR_TOOL_EVENT, CURSOR_READ_EVENT];

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_EVENTS
        .iter()
        .flat_map(|e| cursor_event_entries(value, e))
        .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 has_read_hook = !cursor_event_entries(value, CURSOR_READ_EVENT).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_EVENTS {
        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 !has_read_hook {
        let list = child_array(hooks, CURSOR_READ_EVENT, "hooks.beforeReadFile", path, true)?
            .expect("created");
        list.push(json!({ "command": desired, "timeout": HOOK_TIMEOUT_SECS }));
        actions.push(
            "add hooks.beforeReadFile entry so agent reads of private keys and credential \
             files are blocked"
                .into(),
        );
    }
    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>> {
    if let Some(r) = agents::install(op, value, path, spec) {
        return r;
    }
    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 => {}
        _ => return Err(format!("internal error: {op:?} is not a JSON operation")),
    }
    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>> {
    if let Some(r) = agents::uninstall(op, value, path) {
        return r;
    }
    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_EVENTS {
                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::CodexHooks
        | OpKind::GeminiHooks
        | OpKind::QwenHooks
        | OpKind::CopilotHooks
        | OpKind::WindsurfHooks
        | OpKind::ClineScript
        | OpKind::OpenCodePlugin => {}
        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)
}

/// A planned change to a file shellfirm owns (Cline script, `OpenCode` plugin).
struct OwnedChange {
    path: PathBuf,
    exists: bool,
    /// New content; `None` removes the file.
    content: Option<String>,
    executable: bool,
    actions: Vec<String>,
}

/// What to do with a file shellfirm owns. `None`: nothing to change. A file
/// at `path` that shellfirm did not write is never replaced or removed.
#[allow(clippy::trivially_copy_pass_by_ref)]
fn plan_owned(
    op: OpKind,
    path: &Path,
    opts: &Options,
    spec: &InstallSpec,
    fallback: Option<&str>,
) -> OpResult<Option<OwnedChange>> {
    let current = match read_regular(path) {
        Ok(b) => Some(String::from_utf8_lossy(&b).into_owned()),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            if fs::symlink_metadata(path).is_ok() {
                return Err(format!(
                    "{} is a dangling symlink; refusing to modify it",
                    path.display()
                ));
            }
            None
        }
        Err(e) => return Err(format!("could not read {}: {e}", path.display())),
    };
    let ours = current.as_deref().is_some_and(|c| agents::is_ours(op, c));
    let what = if op == OpKind::ClineScript {
        "Cline PreToolUse hook script"
    } else {
        "OpenCode plugin"
    };
    if opts.uninstall {
        return Ok(ours.then(|| OwnedChange {
            path: path.to_path_buf(),
            exists: true,
            content: None,
            executable: false,
            actions: vec![format!("remove the shellfirm {what}")],
        }));
    }
    if current.is_some() && !ours {
        return Err(format!(
            "{} exists and was not written by shellfirm; nothing was changed. Move it away (or \
             call `shellfirm check --hook {}` from it) and run this again.",
            path.display(),
            if op == OpKind::ClineScript {
                "cline"
            } else {
                "opencode"
            }
        ));
    }
    // An existing shellfirm file keeps the configuration directory it records.
    let spec = InstallSpec {
        config_dir: current
            .as_deref()
            .and_then(|c| agents::recorded_config_dir(op, c))
            .or_else(|| spec.config_dir.clone()),
        ..spec.clone()
    };
    let desired = agents::owned_content(op, &spec, fallback);
    if current.as_deref() == Some(desired.as_str()) {
        return Ok(None);
    }
    Ok(Some(OwnedChange {
        path: path.to_path_buf(),
        exists: current.is_some(),
        executable: op == OpKind::ClineScript,
        actions: vec![if current.is_some() {
            format!("rewrite the shellfirm {what} (it is out of date)")
        } else {
            format!(
                "add the {what} running `{}`",
                spec.hook_command(&format!(
                    "check --hook {}",
                    if op == OpKind::ClineScript {
                        "cline"
                    } else {
                        "opencode"
                    }
                ))
            )
        }],
        content: Some(desired),
    }))
}

/// Write (or remove) a file shellfirm owns, backing up the old one first.
fn save_owned(c: &OwnedChange, backup_dir: Option<&Path>) -> OpResult<Option<PathBuf>> {
    let target = if c.exists {
        fs::canonicalize(&c.path).map_err(|e| format!("{}: {e}", c.path.display()))?
    } else {
        c.path.clone()
    };
    let backup = if c.exists {
        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}", c.path.display()))?;
        prune_backups(&bak);
        Some(bak)
    } else {
        None
    };
    let Some(content) = &c.content else {
        fs::remove_file(&target)
            .map_err(|e| format!("could not remove {}: {e}", c.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 name = target
        .file_name()
        .map_or_else(|| "hook".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(content.as_bytes())?;
        f.sync_all()?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = if c.executable { 0o755 } else { 0o644 };
            fs::set_permissions(&tmp, fs::Permissions::from_mode(mode))?;
        }
        fs::rename(&tmp, &target)
    };
    if let Err(e) = write() {
        let _ = fs::remove_file(&tmp);
        return Err(format!("could not write {}: {e}", c.path.display()));
    }
    Ok(backup)
}

/// `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) || ops.contains(&OpKind::CopilotHooks))
                && 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`, `codex`, `gemini`, `qwen`, `copilot`,
    /// `windsurf`, `cline`, `opencode` or `zed`.
    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,
    /// Things to know about an installed hook (the agent must trust it
    /// first, a file at its path that shellfirm did not write, ...).
    pub notes: Vec<String>,
    /// Why the hook does not run although it is installed (turned off in
    /// the agent's settings), each with how to turn it back on.
    pub hook_off: Vec<String>,
    /// A file could not be read or parsed.
    pub error: Option<String>,
}

/// Read `path` only when it is a regular file (see
/// [`shellfirm::config::read_regular_file`]).
pub(super) fn read_regular(path: &Path) -> std::io::Result<Vec<u8>> {
    shellfirm::config::read_regular_file(path)
}

/// [`read_regular`] as UTF-8 text.
pub(super) fn read_regular_string(path: &Path) -> std::io::Result<String> {
    shellfirm::config::read_regular_string(path)
}

pub(super) fn read_value(path: &Path) -> std::result::Result<Option<Value>, String> {
    // A folder on the path that cannot be read is an error, not "missing".
    match path.try_exists() {
        Ok(false) => return Ok(None),
        Err(e) => return Err(format!("can't read {}: {e}", path.display())),
        Ok(true) => {}
    }
    let content = read_regular_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> {
    integration_status_in(&ConnectEnv {
        home: home.to_path_buf(),
        cwd: cwd.to_path_buf(),
        backup_dir: None,
        config_dir: None,
        dirs: AgentDirs::from_env(),
    })
}

#[allow(clippy::too_many_lines)]
fn integration_status_in(env: &ConnectEnv) -> Vec<IntegrationStatus> {
    let (home, cwd) = (env.home.as_path(), env.cwd.as_path());
    let mut out = Vec::new();
    let mut scopes = vec![Scope::User];
    if cwd != home {
        scopes.push(Scope::Project);
    }
    for provider in ALL_PROVIDERS {
        for &scope in &scopes {
            if scope == Scope::Project && !provider.supports_scope() {
                continue;
            }
            let opts = Options {
                dry_run: true,
                uninstall: false,
                // shellfirm's Cline hook is a POSIX shell script.
                hooks: provider.supports_hooks() && !(cfg!(windows) && provider == Provider::Cline),
                mcp: provider.supports_mcp(),
                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,
                notes: Vec::new(),
                hook_off: Vec::new(),
                error: None,
            };
            for (path, ops) in files {
                if scope == Scope::Project {
                    if let Some((link, target)) = symlink_out_of(&path, cwd) {
                        st.hook_file = Some(path.display().to_string());
                        st.error = Some(symlink_refusal(&path, &link, &target, "project"));
                        continue;
                    }
                }
                if let Some(&op) = ops.iter().find(|o| o.is_owned_file()) {
                    st.hook_file = Some(path.display().to_string());
                    match read_regular_string(&path) {
                        Ok(content) if agents::is_ours(op, &content) => {
                            st.hook_present = true;
                            st.hook_installed = true;
                            st.file_tools_covered = true;
                            // Both answer "block" themselves when shellfirm
                            // cannot run.
                            st.fail_closed = true;
                            st.config_dir = agents::recorded_config_dir(op, &content);
                            if op == OpKind::ClineScript {
                                st.notes.push(
                                    "in the VS Code extension, turn on \"Enable Hooks\" in \
                                     Cline's settings"
                                        .into(),
                                );
                            }
                        }
                        Ok(_) => st.notes.push(format!(
                            "{} exists but was not written by shellfirm",
                            path.display()
                        )),
                        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
                        Err(e) => st.error = Some(format!("{}: {e}", path.display())),
                    }
                    continue;
                }
                let value = match read_value(&path) {
                    Ok(v) => v.unwrap_or_else(|| json!({})),
                    Err(e) => {
                        st.error = Some(e);
                        continue;
                    }
                };
                for op in ops {
                    if agents::status(op, &value, &mut st) {
                        st.hook_file = Some(path.display().to_string());
                        continue;
                    }
                    match op {
                        OpKind::ClaudeHooks => {
                            st.hook_file = Some(path.display().to_string());
                            fill_claude_hook_state(&mut st, &value);
                        }
                        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()
                                || !cursor_event_entries(&value, CURSOR_READ_EVENT).is_empty();
                            let reads = cursor_event_entries(&value, CURSOR_READ_EVENT);
                            st.file_tools_covered = !files.is_empty() && !reads.is_empty();
                            st.legacy_hook = entries
                                .iter()
                                .chain(files.iter())
                                .chain(reads.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);
                        }
                        _ => {}
                    }
                }
            }
            if st.hook_present {
                match provider {
                    Provider::ClaudeCode => st.hook_off.extend(
                        claude_hooks_off(env)
                            .into_iter()
                            .chain(claude_managed_only(env)),
                    ),
                    Provider::Codex => st.hook_off.extend(codex_hooks_off(env)),
                    _ => {}
                }
            }
            out.push(st);
        }
        if provider == Provider::ClaudeCode {
            out.extend(claude_managed_row(env));
        }
    }
    out
}

/// What the shellfirm entries in Claude Code `hooks` cover.
fn fill_claude_hook_state(st: &mut IntegrationStatus, value: &Value) {
    let s = claude_hook_state(value);
    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();
}

/// A shellfirm hook in Claude Code's managed settings (put there by an
/// administrator; `connect` never writes that file). `None` when the file
/// is missing or holds no shellfirm hook.
fn claude_managed_row(env: &ConnectEnv) -> Option<IntegrationStatus> {
    let path = env.dirs.claude_managed.as_ref()?;
    let mut st = IntegrationStatus {
        tool: Provider::ClaudeCode.cli_name(),
        scope: "managed",
        hook_file: Some(path.display().to_string()),
        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,
        notes: Vec::new(),
        hook_off: Vec::new(),
        error: None,
    };
    match read_value(path) {
        Ok(None) => return None,
        Ok(Some(value)) => {
            fill_claude_hook_state(&mut st, &value);
            if !st.hook_present {
                return None;
            }
            // Only managed settings can turn managed hooks off.
            if value.get("disableAllHooks").and_then(Value::as_bool) == Some(true) {
                st.hook_off.push(format!(
                    "disableAllHooks is true in {}: Claude Code runs no hook (remove it or set \
                     it to false)",
                    path.display()
                ));
            }
        }
        Err(e) => st.error = Some(e),
    }
    Some(st)
}

/// The managed settings file holds a shellfirm hook that covers `Bash`.
fn claude_managed_hook(env: &ConnectEnv) -> bool {
    claude_managed_row(env).is_some_and(|r| r.hook_installed && r.hook_off.is_empty())
}

/// Claude Code's `disableAllHooks`: the value from the highest-precedence
/// settings file that sets it (managed, local, project, user). `Some` with
/// the file when it turns every hook off.
fn claude_hooks_off(env: &ConnectEnv) -> Option<String> {
    let dir = env.cwd.join(".claude");
    let project = (env.cwd != env.home)
        .then(|| [dir.join("settings.local.json"), dir.join("settings.json")])
        .into_iter()
        .flatten();
    let files = env
        .dirs
        .claude_managed
        .iter()
        .cloned()
        .chain(project)
        .chain([env.dirs.claude_dir(&env.home).join("settings.json")]);
    files.into_iter().find_map(|f| {
        let v = read_value(&f).ok().flatten()?;
        let off = v.get("disableAllHooks")?.as_bool()?;
        Some(off.then(|| {
            format!(
                "disableAllHooks is true in {}: Claude Code runs no hook (remove it or set it \
                 to false)",
                f.display()
            )
        }))
    })?
}

/// Claude Code's managed setting that loads only managed hooks.
pub(super) const MANAGED_HOOKS_ONLY: &str = "allowManagedHooksOnly";

/// Claude Code's managed `allowManagedHooksOnly`: user, project and plugin
/// hooks (where `shellfirm connect` puts its hook) are not loaded.
fn claude_managed_only(env: &ConnectEnv) -> Option<String> {
    let f = env.dirs.claude_managed.as_ref()?;
    let on = read_value(f).ok()??.get(MANAGED_HOOKS_ONLY)?.as_bool()?;
    on.then(|| {
        let advice = if claude_managed_hook(env) {
            "the shellfirm hook in that file runs instead"
        } else {
            "put the shellfirm hook in that file, or remove the setting"
        };
        format!(
            "{MANAGED_HOOKS_ONLY} is true in {}: Claude Code runs only hooks from managed \
             settings ({advice})",
            f.display()
        )
    })
}

/// Codex's hooks feature flag (`[features] hooks`, formerly `codex_hooks`,
/// on by default): the project `.codex/config.toml` overrides the user one.
fn codex_hooks_off(env: &ConnectEnv) -> Option<String> {
    let user = env
        .dirs
        .codex_home
        .clone()
        .unwrap_or_else(|| env.home.join(".codex"))
        .join("config.toml");
    let mut files = Vec::new();
    if env.cwd != env.home {
        files.push(env.cwd.join(".codex").join("config.toml"));
    }
    files.push(user);
    files.into_iter().find_map(|f| {
        let doc = read_regular_string(&f)
            .ok()?
            .parse::<toml_edit::DocumentMut>()
            .ok()?;
        let features = doc.get("features")?;
        let (key, on) = ["hooks", "codex_hooks"]
            .into_iter()
            .find_map(|k| features.get(k)?.as_bool().map(|b| (k, b)))?;
        Some((!on).then(|| {
            format!(
                "hooks are turned off in {} ([features] {key} = false): Codex runs no hook \
                 (set it to true)",
                f.display()
            )
        }))
    })?
}

// ---------------------------------------------------------------------------
// 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())
        }
    }
}

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

/// Read a config file that is about to be modified: `None` when it does not
/// exist, else the path to write to (symlink target) and its text.
pub(super) fn read_text(path: &Path) -> OpResult<Option<(PathBuf, String)>> {
    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(None);
    }
    let write_path = fs::canonicalize(path).map_err(|e| format!("{}: {e}", path.display()))?;
    let bytes =
        read_regular(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()
        )
    })?;
    Ok(Some((write_path, content)))
}

pub(super) fn load_file(path: &Path) -> OpResult<LoadedFile> {
    let Some((write_path, content)) = read_text(path)? else {
        return Ok(LoadedFile {
            path: path.to_path_buf(),
            write_path: path.to_path_buf(),
            exists: false,
            value: json!({}),
            lossy_format: false,
        });
    };
    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
}

/// Remove the directories between `file` and `root` that are left empty
/// (`.github/hooks` and `.github` after a project-scope uninstall). A
/// directory that still holds anything stays.
pub(super) fn remove_empty_parents(file: &Path, root: &Path) {
    for dir in file.ancestors().skip(1) {
        if dir == root || !dir.starts_with(root) || fs::remove_dir(dir).is_err() {
            break;
        }
    }
}

/// shellfirm keeps this many backups of each file; older ones are deleted.
pub(super) const BACKUPS_KEPT: usize = 3;

/// `rest` is what [`backup_path_for`] puts after `.shellfirm-bak-`: a
/// `YYYYMMDDTHHMMSSZ` stamp, then an optional `-<n>`.
fn is_backup_suffix(rest: &str) -> bool {
    let (stamp, n) = rest.split_once('-').unwrap_or((rest, "1"));
    let b = stamp.as_bytes();
    b.len() == 16
        && b[8] == b'T'
        && b[15] == b'Z'
        && b[..8].iter().chain(&b[9..15]).all(u8::is_ascii_digit)
        && !n.is_empty()
        && n.bytes().all(|c| c.is_ascii_digit())
}

/// Delete all but the newest [`BACKUPS_KEPT`] shellfirm backups of the
/// file `bak` (just written) is a backup of.
fn prune_backups(bak: &Path) {
    let (Some(dir), Some(name)) = (bak.parent(), bak.file_name().and_then(|n| n.to_str())) else {
        return;
    };
    let Some(idx) = name.find(".shellfirm-bak-") else {
        return;
    };
    let prefix = &name[..idx + ".shellfirm-bak-".len()];
    let Ok(entries) = fs::read_dir(dir) else {
        return;
    };
    // Named `<prefix><UTC timestamp>[-<n>]`: the name sorts by age.
    let mut found: Vec<(String, PathBuf)> = entries
        .filter_map(std::result::Result::ok)
        .filter_map(|e| {
            let n = e.file_name().to_str()?.to_string();
            // Only names shellfirm writes: not a user's own
            // `settings.json.shellfirm-bak-mine`.
            is_backup_suffix(n.strip_prefix(prefix)?).then(|| (n.clone(), e.path()))
        })
        .collect();
    found.sort();
    let excess = found.len().saturating_sub(BACKUPS_KEPT);
    for (_, p) in found.into_iter().take(excess) {
        let _ = fs::remove_file(p);
    }
}

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)]
pub(super) 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).
pub(super) fn save_file(
    file: &LoadedFile,
    value: &Value,
    backup_dir: Option<&Path>,
    mode: SaveMode,
) -> OpResult<Option<PathBuf>> {
    let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())? + "\n";
    save_text(file, &text, backup_dir, mode)
}

/// [`save_file`] for content that is already serialized (any format).
pub(super) fn save_text(
    file: &LoadedFile,
    text: &str,
    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()))?;
        prune_backups(&bak);
        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 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())
}

/// A tool with a hook and no MCP registration.
fn hooks_only_subcommand(name: &'static str, about: &'static str, long: &'static str) -> Command {
    Command::new(name)
        .about(about)
        .long_about(long)
        .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())
}

#[allow(clippy::too_many_lines)]
pub fn command() -> Command {
    Command::new("connect")
        .about("Connect AI agents (blocking hooks, MCP server)")
        .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(hooks_only_subcommand(
            "codex",
            "Connect OpenAI Codex CLI (PreToolUse hook)",
            "Connect OpenAI Codex CLI (0.124.0 or later).\n\n\
            user scope:    ~/.codex/hooks.json ($CODEX_HOME/hooks.json when set)\n\
            project scope: ./.codex/hooks.json (Codex loads it in trusted projects)\n\n\
            A PreToolUse hook (matcher Bash|apply_patch) runs `shellfirm check --hook codex` \
            before every shell command and apply_patch edit. Codex hooks cannot ask, so \
            risky commands are denied with the reason and a safer alternative, like dangerous \
            ones; safe commands get no answer (Codex's own approvals apply). Codex runs a new \
            or changed hook only after you trust it: the TUI lists it when it starts (or use \
            /hooks), and `codex exec` skips it until then. If shellfirm is missing or times \
            out, Codex runs the command.",
        ))
        .subcommand(hooks_only_subcommand(
            "gemini",
            "Connect Google Gemini CLI (BeforeTool hook)",
            "Connect Google Gemini CLI.\n\n\
            user scope:    ~/.gemini/settings.json\n\
            project scope: ./.gemini/settings.json (Gemini asks you to trust project hooks)\n\n\
            A BeforeTool hook named \"shellfirm\" (matcher \
            ^(run_shell_command|write_file|replace|read_file|read_many_files)$, timeout 30000 ms) runs \
            `shellfirm check --hook gemini`: risky commands and edits ask (Gemini CLI 0.36.0 \
            or later; older versions ignore the ask), dangerous ones are denied. Turn it off \
            with /hooks disable shellfirm.",
        ))
        .subcommand(hooks_only_subcommand(
            "qwen",
            "Connect Qwen Code (PreToolUse hook)",
            "Connect Qwen Code.\n\n\
            user scope:    ~/.qwen/settings.json\n\
            project scope: ./.qwen/settings.json (Qwen Code asks you to approve project hooks)\n\n\
            A PreToolUse hook (matcher \
            run_shell_command|write_file|edit|read_file|read_many_files) runs \
            `shellfirm check --hook qwen`: risky commands and edits ask, dangerous ones are \
            denied (headless runs deny what would be asked).",
        ))
        .subcommand(hooks_only_subcommand(
            "copilot",
            "Connect GitHub Copilot CLI (preToolUse hook)",
            "Connect GitHub Copilot CLI (1.0.4 or later).\n\n\
            user scope:    ~/.copilot/hooks/shellfirm.json ($COPILOT_HOME/hooks when set)\n\
            project scope: ./.github/hooks/shellfirm.json (also used by Copilot cloud agent)\n\n\
            A preToolUse hook runs `shellfirm check --hook copilot` before every tool call: \
            risky shell commands and edits ask (autopilot and the cloud agent deny them), \
            dangerous ones are denied. Copilot denies the tool call when the hook fails \
            (shellfirm missing or crashing), except on a timeout.",
        ))
        .subcommand(hooks_tool_subcommand(
            "windsurf",
            "Connect Windsurf (Cascade hooks + MCP server)",
            "Connect Windsurf.\n\n\
            user scope:    hooks in ~/.codeium/windsurf/hooks.json, MCP server in \
            ~/.codeium/windsurf/mcp_config.json\n\
            project scope: hooks in ./.windsurf/hooks.json (the MCP registration is \
            user-level only)\n\n\
            pre_run_command, pre_write_code and pre_read_code hooks run `shellfirm check \
            --hook windsurf`, which blocks with exit code 2 and the reason on stderr. Cascade \
            hooks cannot ask, so risky commands are blocked like dangerous ones. If shellfirm \
            is missing, Cascade runs the action.",
        ))
        .subcommand(hooks_tool_subcommand(
            "cline",
            "Connect Cline (PreToolUse hook script + MCP server)",
            "Connect Cline (3.36 or later; macOS and Linux).\n\n\
            user scope:    hook script ~/Documents/Cline/Hooks/PreToolUse, MCP server in \
            Cline's cline_mcp_settings.json\n\
            project scope: hook script ./.clinerules/hooks/PreToolUse (the MCP registration is \
            user-level only)\n\n\
            The script runs `shellfirm check --hook cline` before every tool call and \
            answers {\"cancel\": true} (which stops the task) for dangerous and risky \
            commands and edits: Cline hooks cannot ask. It also cancels when shellfirm cannot \
            run. In the VS Code extension, turn on \"Enable Hooks\" in Cline's settings. An \
            existing PreToolUse script that shellfirm did not write is never replaced.",
        ))
        .subcommand(hooks_only_subcommand(
            "opencode",
            "Connect OpenCode (tool.execute.before plugin)",
            "Connect OpenCode.\n\n\
            user scope:    ~/.config/opencode/plugins/shellfirm.js ($XDG_CONFIG_HOME when set)\n\
            project scope: ./.opencode/plugins/shellfirm.js\n\n\
            The plugin sends bash, write, edit, read and apply_patch calls to `shellfirm check \
            --hook opencode` and throws (which blocks the call) unless shellfirm allows it: \
            dangerous and risky ones are blocked, because the plugin cannot ask. It also \
            blocks when shellfirm cannot run.",
        ))
        .subcommand(mcp_only_subcommand(
            "zed",
            "Register shellfirm's MCP tools with Zed (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 && provider.supports_mcp(),
        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),
        dirs: AgentDirs::from_env(),
    };

    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}")),
        },
    })
}

/// An installed hook that the agent's settings turn off (this file or
/// another scope): say so, with how to turn it on.
fn hook_off_warnings(provider: Provider, scope: Scope, env: &ConnectEnv, out: &mut String) {
    let scope = if scope == Scope::User {
        "user"
    } else {
        "project"
    };
    for row in integration_status_in(env)
        .iter()
        .filter(|r| r.tool == provider.cli_name() && r.scope == scope)
    {
        for off in &row.hook_off {
            let _ = writeln!(out, "! The hook does not run yet: {off}.");
        }
    }
}

/// 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(if provider.supports_hooks() {
            format!(
                "shellfirm does not register an MCP server with {}; run `shellfirm connect {}` \
                 without --mcp-only",
                provider.display_name(),
                provider.cli_name()
            )
        } else {
            format!(
                "{} has no command hook; run `shellfirm connect {}` without --hooks-only",
                provider.display_name(),
                provider.cli_name()
            )
        });
    }
    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)?;
    if opts.scope == Scope::Project {
        for (path, _) in &files {
            if let Some((link, target)) = symlink_out_of(path, &env.cwd) {
                return Err(symlink_refusal(path, &link, &target, "project"));
            }
        }
    }
    let mut loaded = Vec::new();
    let mut owned_files = Vec::new();
    for (path, ops) in files {
        if let Some(&op) = ops.iter().find(|o| o.is_owned_file()) {
            owned_files.push((path, op));
        } else {
            loaded.push((load_file(&path)?, ops));
        }
    }
    if opts.scope == Scope::Project && opts.mcp && !provider.mcp_follows_scope() {
        let _ = writeln!(
            out,
            "  ! the {} MCP registration is user-level only; run `shellfirm connect {}` \
             (user scope) for it",
            provider.display_name(),
            provider.cli_name()
        );
    }

    // 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();
    let fallback = (opts.scope == Scope::User)
        .then_some(env.dirs.current_exe.as_ref())
        .flatten()
        .map(|p| p.display().to_string());
    let mut owned: Vec<OwnedChange> = Vec::new();
    for (path, op) in &owned_files {
        match plan_owned(*op, path, opts, &spec, fallback.as_deref())? {
            Some(c) => owned.push(c),
            None if !opts.uninstall => already.push(Component::Hooks),
            None => {}
        }
    }
    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() && owned.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()
                )
            }
        );
        if !opts.uninstall && opts.hooks {
            hook_off_warnings(provider, opts.scope, env, out);
        }
        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 {
        // The change list is shown once the file is written (or for a dry
        // run): a failed write shows only the error.
        let mut block = String::new();
        if opts.dry_run {
            let _ = writeln!(
                block,
                "  Would {} {}:",
                if file.exists { "update" } else { "create" },
                file.path.display()
            );
        } else {
            let _ = writeln!(block, "  {}:", file.path.display());
        }
        if file.lossy_format {
            let _ = writeln!(
                block,
                "    ! this file contains comments or trailing commas, which will not be kept (a backup is made first)"
            );
        }
        for a in actions {
            let _ = writeln!(block, "    - {a}");
        }
        if opts.dry_run {
            if file.exists {
                let _ = writeln!(
                    block,
                    "    (a timestamped backup would be written first{})",
                    backup_dir.map_or_else(String::new, |d| format!(" to {}", d.display()))
                );
            }
            out.push_str(&block);
            continue;
        }
        match save_file(file, value, backup_dir, *mode) {
            Ok(backup) => {
                out.push_str(&block);
                written.push(file.path.display().to_string());
                if *mode == SaveMode::Remove && opts.scope == Scope::Project {
                    // `.claude` / `.cursor` that shellfirm emptied.
                    remove_empty_parents(&file.path, &env.cwd);
                }
                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(", ")
                ));
            }
        }
    }

    for c in &owned {
        let verb = match (&c.content, c.exists) {
            (None, _) => "remove",
            (Some(_), true) => "update",
            (Some(_), false) => "create",
        };
        let mut block = String::new();
        if opts.dry_run {
            let _ = writeln!(block, "  Would {verb} {}:", c.path.display());
        } else {
            let _ = writeln!(block, "  {}:", c.path.display());
        }
        for a in &c.actions {
            let _ = writeln!(block, "    - {a}");
        }
        if opts.dry_run {
            if c.exists {
                let _ = writeln!(
                    block,
                    "    (a timestamped backup would be written first{})",
                    backup_dir.map_or_else(String::new, |d| format!(" to {}", d.display()))
                );
            }
            out.push_str(&block);
            continue;
        }
        match save_owned(c, backup_dir) {
            Ok(backup) => {
                out.push_str(&block);
                written.push(c.path.display().to_string());
                if c.content.is_none() && opts.scope == Scope::Project {
                    // The `hooks` / `plugins` directories shellfirm emptied.
                    remove_empty_parents(&c.path, &env.cwd);
                }
                let _ = writeln!(
                    out,
                    "    ✓ {}{}",
                    if c.content.is_none() {
                        "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(());
    }
    if opts.hooks {
        hook_off_warnings(provider, opts.scope, env, out);
    }
    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), \
             agent edits of protected files are blocked (preToolUse hook), and so are reads \
             of credential files (beforeReadFile hook). \
             Restart Cursor to pick up the change."
        }
        Provider::Codex if opts.hooks => {
            "Codex shell commands and apply_patch edits are now checked by shellfirm \
             (PreToolUse hook; dangerous and risky ones are denied, because Codex hooks cannot \
             ask). Codex runs a new or changed hook only after you trust it: start `codex` \
             once and trust it (or open /hooks); `codex exec` skips an untrusted hook. Needs \
             Codex CLI 0.124.0 or later."
        }
        Provider::Gemini if opts.hooks => {
            "Gemini CLI shell commands, file writes and reads are now checked by shellfirm \
             (BeforeTool hook: risky -> ask, dangerous -> deny). Asking needs Gemini CLI \
             0.36.0 or later. Restart running Gemini CLI sessions."
        }
        Provider::Qwen if opts.hooks => {
            "Qwen Code shell commands, file writes and reads are now checked by shellfirm \
             (PreToolUse hook: risky -> ask, dangerous -> deny; headless runs deny what would \
             be asked). Restart running Qwen Code sessions."
        }
        Provider::Copilot if opts.hooks => {
            "GitHub Copilot CLI tool calls are now checked by shellfirm (preToolUse hook: \
             risky -> ask, dangerous -> deny; autopilot denies what would be asked). Needs \
             Copilot CLI 1.0.4 or later. Restart running Copilot sessions."
        }
        Provider::Windsurf if opts.hooks => {
            "Windsurf (Cascade) commands, file writes and reads are now checked by shellfirm \
             (pre_run_command / pre_write_code / pre_read_code hooks; dangerous and risky ones \
             are blocked, because Cascade hooks cannot ask). Restart Windsurf."
        }
        Provider::Cline if opts.hooks => {
            "Cline tool calls are now checked by shellfirm (PreToolUse hook; dangerous and \
             risky ones cancel the task, because Cline hooks cannot ask). In the VS Code \
             extension, turn on \"Enable Hooks\" in Cline's settings (Cline 3.36 or later)."
        }
        Provider::OpenCode if opts.hooks => {
            "OpenCode shell commands, file edits and reads are now checked by shellfirm \
             (plugin; dangerous and risky ones are blocked, because the plugin cannot ask). \
             Restart OpenCode."
        }
        _ => {
            "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,
                dirs: AgentDirs::default(),
            },
            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 an_unreadable_agent_folder_is_an_error_not_missing() {
        use std::os::unix::fs::PermissionsExt;
        let mut sb = sandbox();
        sb.env.dirs.claude_config_dir = Some(sb.tree.root.join("ccd"));
        for p in [Provider::ClaudeCode, Provider::Codex] {
            exec(p, &hooks_only(false), &sb.env).0.unwrap();
        }
        let dirs = [sb.tree.root.join("ccd"), sb.env.home.join(".codex")];
        for d in &dirs {
            fs::set_permissions(d, fs::Permissions::from_mode(0o000)).unwrap();
        }
        let rows = integration_status_in(&sb.env);
        for d in &dirs {
            fs::set_permissions(d, fs::Permissions::from_mode(0o755)).unwrap();
        }
        for tool in ["claude-code", "codex"] {
            let row = rows
                .iter()
                .find(|s| s.tool == tool && s.scope == "user")
                .unwrap();
            assert!(
                row.error
                    .as_deref()
                    .is_some_and(|e| e.contains("can't read")),
                "{row:?}"
            );
        }
    }

    #[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();
    }

    // -- Codex, Gemini, Qwen, Copilot, Windsurf, Cline, OpenCode --

    fn hooks_only(uninstall: bool) -> Options {
        opts(uninstall, true, false)
    }

    #[test]
    fn codex_install_merges_with_user_hooks_and_uninstall_keeps_them() {
        let sb = sandbox();
        let path = sb.env.home.join(".codex/hooks.json");
        // A user hook of their own, on the same event.
        write(
            &path,
            r#"{"description":"mine","hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"python3 ~/.codex/hooks/log.py"}]}],"Stop":[{"hooks":[{"type":"command","command":"notify"}]}]}}"#,
        );
        let (r, out) = exec(Provider::Codex, &hooks_only(false), &sb.env);
        r.unwrap();
        assert!(out.contains("trust"), "{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"],
            "python3 ~/.codex/hooks/log.py"
        );
        assert_eq!(groups[1]["matcher"], "Bash|apply_patch");
        assert_eq!(groups[1]["hooks"][0]["type"], "command");
        assert_eq!(
            groups[1]["hooks"][0]["command"],
            "shellfirm check --hook codex"
        );
        assert_eq!(groups[1]["hooks"][0]["timeout"], HOOK_TIMEOUT_SECS);
        // Codex rejects unknown top-level keys in hooks.json: only `hooks`
        // and the user's `description` are there.
        let keys: Vec<&String> = v.as_object().unwrap().keys().collect();
        assert_eq!(keys, ["description", "hooks"]);
        assert_eq!(backups(path.parent().unwrap()).len(), 1);

        let (r, out) = exec(Provider::Codex, &hooks_only(false), &sb.env);
        r.unwrap();
        assert!(out.contains("already connected"), "{out}");

        let (r, _) = exec(Provider::Codex, &hooks_only(true), &sb.env);
        r.unwrap();
        let v = read_json(&path);
        assert_eq!(v["hooks"]["PreToolUse"].as_array().unwrap().len(), 1);
        assert_eq!(v["hooks"]["Stop"][0]["hooks"][0]["command"], "notify");
    }

    #[test]
    fn codex_home_and_project_scope() {
        let sb = sandbox();
        let custom = sb.tree.root.join("codex-home");
        let env = ConnectEnv {
            dirs: AgentDirs {
                codex_home: Some(custom.clone()),
                ..AgentDirs::default()
            },
            ..sb.env.clone()
        };
        exec(Provider::Codex, &hooks_only(false), &env).0.unwrap();
        assert!(custom.join("hooks.json").exists());
        assert!(!sb.env.home.join(".codex/hooks.json").exists());
        let project = Options {
            scope: Scope::Project,
            ..hooks_only(false)
        };
        exec(Provider::Codex, &project, &env).0.unwrap();
        let v = read_json(&sb.env.cwd.join(".codex/hooks.json"));
        // A team file records no personal --config-dir.
        assert_eq!(
            v["hooks"]["PreToolUse"][0]["hooks"][0]["command"],
            "shellfirm check --hook codex"
        );
    }

    #[test]
    fn gemini_jsonc_settings_keep_other_keys_and_get_a_named_hook() {
        let sb = sandbox();
        let path = sb.env.home.join(".gemini/settings.json");
        write(
            &path,
            "{\n  // my theme\n  \"ui\": {\"theme\": \"GitHub\"},\n  \"hooks\": {\"AfterTool\": [{\"matcher\": \"write_file\", \"hooks\": [{\"type\": \"command\", \"command\": \"prettier\"}]}],},\n}\n",
        );
        let (r, out) = exec(Provider::Gemini, &hooks_only(false), &sb.env);
        r.unwrap();
        assert!(out.contains("comments or trailing commas"), "{out}");
        let v = read_json(&path);
        assert_eq!(v["ui"]["theme"], "GitHub");
        assert_eq!(
            v["hooks"]["AfterTool"][0]["hooks"][0]["command"],
            "prettier"
        );
        let g = &v["hooks"]["BeforeTool"][0];
        assert_eq!(
            g["matcher"],
            "^(run_shell_command|write_file|replace|read_file|read_many_files)$"
        );
        assert_eq!(g["hooks"][0]["name"], "shellfirm");
        // Gemini timeouts are milliseconds.
        assert_eq!(g["hooks"][0]["timeout"], HOOK_TIMEOUT_SECS * 1000);

        let mut env = sb.env.clone();
        env.config_dir = Some(sb.tree.root.join("cfg"));
        let (r, out) = exec(Provider::Gemini, &hooks_only(false), &env);
        r.unwrap();
        assert!(out.contains("pinned --config-dir"), "{out}");
        let v = read_json(&path);
        let cmd = v["hooks"]["BeforeTool"][0]["hooks"][0]["command"]
            .as_str()
            .unwrap();
        assert!(
            cmd.starts_with("shellfirm check --hook gemini --config-dir "),
            "{cmd}"
        );
        assert_eq!(v["hooks"]["BeforeTool"].as_array().unwrap().len(), 1);

        // Turned off in Gemini's own settings: status says so.
        let mut v = read_json(&path);
        v["hooksConfig"] = json!({"disabled": ["shellfirm"]});
        write(&path, &v.to_string());
        let st = integration_status_in(&sb.env);
        let row = st
            .iter()
            .find(|s| s.tool == "gemini" && s.scope == "user")
            .unwrap();
        assert!(row.hook_installed && row.file_tools_covered && !row.fail_closed);
        assert!(
            row.hook_off
                .iter()
                .any(|n| n.contains("hooksConfig.disabled")),
            "{row:?}"
        );
        assert!(row.mcp_file.is_none());

        // `connect` takes shellfirm out of hooksConfig.disabled.
        let (r, out) = exec(Provider::Gemini, &hooks_only(false), &sb.env);
        r.unwrap();
        assert!(
            out.contains("take shellfirm out of hooksConfig.disabled"),
            "{out}"
        );
        assert_eq!(read_json(&path)["hooksConfig"]["disabled"], json!([]));
        let st = integration_status_in(&sb.env);
        assert!(st.iter().all(|s| s.hook_off.is_empty()), "{st:?}");

        // Hooks turned off altogether: connect says so (it leaves the
        // switch to the user).
        let mut v = read_json(&path);
        v["hooksConfig"] = json!({"enabled": false});
        write(&path, &v.to_string());
        let (r, out) = exec(Provider::Gemini, &hooks_only(false), &sb.env);
        r.unwrap();
        assert!(
            out.contains("The hook does not run yet: hooks are turned off"),
            "{out}"
        );
        v["hooksConfig"] = json!({});
        write(&path, &v.to_string());

        exec(Provider::Gemini, &hooks_only(true), &sb.env)
            .0
            .unwrap();
        let v = read_json(&path);
        assert!(v["hooks"].get("BeforeTool").is_none(), "{v}");
        assert_eq!(
            v["hooks"]["AfterTool"][0]["hooks"][0]["command"],
            "prettier"
        );
    }

    #[test]
    fn group_hooks_are_deduped_and_a_partial_matcher_is_completed() {
        let sb = sandbox();
        let path = sb.env.home.join(".qwen/settings.json");
        write(
            &path,
            r#"{"hooks":{"PreToolUse":[
                {"matcher":"run_shell_command","hooks":[{"type":"command","command":"shellfirm check --hook qwen","timeout":5}]},
                {"matcher":"run_shell_command","hooks":[{"type":"command","command":"shellfirm check --hook qwen"},{"type":"command","command":"audit.sh"}]}
            ]}}"#,
        );
        let (r, out) = exec(Provider::Qwen, &hooks_only(false), &sb.env);
        r.unwrap();
        assert!(out.contains("duplicate"), "{out}");
        let v = read_json(&path);
        let groups = v["hooks"]["PreToolUse"].as_array().unwrap();
        // The user's own timeout is kept; the second group keeps only audit.sh;
        // a group for the file tools is added.
        assert_eq!(groups[0]["hooks"][0]["timeout"], 5);
        assert_eq!(groups[1]["hooks"].as_array().unwrap().len(), 1);
        assert_eq!(groups[1]["hooks"][0]["command"], "audit.sh");
        assert_eq!(
            groups[2]["matcher"],
            "write_file|edit|read_file|read_many_files"
        );
        let st = integration_status_in(&sb.env);
        let row = st
            .iter()
            .find(|s| s.tool == "qwen" && s.scope == "user")
            .unwrap();
        assert!(row.hook_installed && row.file_tools_covered);
    }

    #[test]
    fn copilot_owns_its_hook_file() {
        let sb = sandbox();
        let path = sb.env.home.join(".copilot/hooks/shellfirm.json");
        let (r, _) = exec(Provider::Copilot, &hooks_only(false), &sb.env);
        r.unwrap();
        let v = read_json(&path);
        assert_eq!(v["version"], 1);
        let e = &v["hooks"]["preToolUse"][0];
        assert_eq!(e["type"], "command");
        assert_eq!(e["bash"], "shellfirm check --hook copilot");
        assert_eq!(e["powershell"], "shellfirm check --hook copilot");
        assert_eq!(e["timeoutSec"], HOOK_TIMEOUT_SECS);
        assert!(
            e.get("matcher").is_none(),
            "every tool goes through the hook"
        );
        let st = integration_status_in(&sb.env);
        let row = st
            .iter()
            .find(|s| s.tool == "copilot" && s.scope == "user")
            .unwrap();
        assert!(row.hook_installed && row.fail_closed);

        // A dry run of the uninstall changes nothing; the uninstall removes
        // the file (nothing else was in it).
        let dry = Options {
            dry_run: true,
            ..hooks_only(true)
        };
        let (r, out) = exec(Provider::Copilot, &dry, &sb.env);
        r.unwrap();
        assert!(out.contains("Would update"), "{out}");
        assert!(path.exists());
        exec(Provider::Copilot, &hooks_only(true), &sb.env)
            .0
            .unwrap();
        assert!(!path.exists());

        // COPILOT_HOME moves the user directory; project scope writes
        // .github/hooks.
        let env = ConnectEnv {
            dirs: AgentDirs {
                copilot_home: Some(sb.tree.root.join("cp")),
                ..AgentDirs::default()
            },
            // Project-scope backups go outside the repository.
            backup_dir: Some(sb.tree.root.join("backups")),
            ..sb.env.clone()
        };
        exec(Provider::Copilot, &hooks_only(false), &env).0.unwrap();
        assert!(sb.tree.root.join("cp/hooks/shellfirm.json").exists());
        let project = Options {
            scope: Scope::Project,
            ..hooks_only(false)
        };
        exec(Provider::Copilot, &project, &env).0.unwrap();
        assert!(sb.env.cwd.join(".github/hooks/shellfirm.json").exists());
        let project_off = Options {
            scope: Scope::Project,
            ..hooks_only(true)
        };
        exec(Provider::Copilot, &project_off, &env).0.unwrap();
        // The `.github` directories shellfirm emptied are gone too.
        assert!(!sb.env.cwd.join(".github").exists());
    }

    #[test]
    fn windsurf_hooks_and_mcp() {
        let sb = sandbox();
        let hooks = sb.env.home.join(".codeium/windsurf/hooks.json");
        write(
            &hooks,
            r#"{"hooks":{"post_write_code":[{"command":"npx prettier --write","show_output":true}],"pre_run_command":[{"command":"python3 log.py"}]}}"#,
        );
        exec(Provider::Windsurf, &opts(false, true, true), &sb.env)
            .0
            .unwrap();
        let v = read_json(&hooks);
        assert_eq!(
            v["hooks"]["pre_run_command"][0]["command"],
            "python3 log.py"
        );
        assert_eq!(
            v["hooks"]["pre_run_command"][1]["command"],
            "shellfirm check --hook windsurf"
        );
        assert_eq!(v["hooks"]["pre_run_command"][1]["show_output"], false);
        for event in ["pre_write_code", "pre_read_code"] {
            assert_eq!(
                v["hooks"][event][0]["command"], "shellfirm check --hook windsurf",
                "{event}"
            );
        }
        let mcp = read_json(&sb.env.home.join(".codeium/windsurf/mcp_config.json"));
        assert_eq!(mcp["mcpServers"]["shellfirm"]["command"], "shellfirm");
        let st = integration_status_in(&sb.env);
        let row = st
            .iter()
            .find(|s| s.tool == "windsurf" && s.scope == "user")
            .unwrap();
        assert!(row.hook_installed && row.file_tools_covered && row.mcp_registered);
        assert!(!row.fail_closed);

        // --hooks-only uninstall keeps the MCP server and the user's hooks.
        exec(Provider::Windsurf, &opts(true, true, false), &sb.env)
            .0
            .unwrap();
        let v = read_json(&hooks);
        assert_eq!(v["hooks"]["pre_run_command"].as_array().unwrap().len(), 1);
        assert!(v["hooks"].get("pre_read_code").is_none());
        assert!(sb
            .env
            .home
            .join(".codeium/windsurf/mcp_config.json")
            .exists());
    }

    #[cfg(unix)]
    #[test]
    fn cline_script_is_owned_and_a_foreign_one_is_never_replaced() {
        let sb = sandbox();
        let env = ConnectEnv {
            dirs: AgentDirs {
                current_exe: Some(PathBuf::from("/opt/shellfirm/bin/shellfirm")),
                ..AgentDirs::default()
            },
            ..sb.env.clone()
        };
        let script = sb.env.home.join("Documents/Cline/Hooks/PreToolUse");
        exec(Provider::Cline, &hooks_only(false), &env).0.unwrap();
        let content = fs::read_to_string(&script).unwrap();
        assert!(
            content.starts_with("#!/bin/sh\n# shellfirm-hook: cline\n"),
            "{content}"
        );
        assert!(
            content.contains("SHELLFIRM=/opt/shellfirm/bin/shellfirm"),
            "{content}"
        );
        {
            use std::os::unix::fs::PermissionsExt;
            assert_eq!(
                fs::metadata(&script).unwrap().permissions().mode() & 0o777,
                0o755
            );
        }
        let (r, out) = exec(Provider::Cline, &hooks_only(false), &env);
        r.unwrap();
        assert!(out.contains("already connected"), "{out}");

        // An out-of-date shellfirm script is rewritten (with a backup).
        fs::write(
            &script,
            content.replace("check --hook cline", "check --hook cline --log debug"),
        )
        .unwrap();
        let (r, out) = exec(Provider::Cline, &hooks_only(false), &env);
        r.unwrap();
        assert!(out.contains("out of date"), "{out}");
        assert_eq!(fs::read_to_string(&script).unwrap(), content);
        assert_eq!(backups(script.parent().unwrap()).len(), 1);

        // Someone else's PreToolUse script: refused on install, kept on
        // uninstall.
        fs::write(&script, "#!/bin/sh\necho '{\"cancel\": false}'\n").unwrap();
        let (r, _) = exec(Provider::Cline, &hooks_only(false), &env);
        let err = r.unwrap_err();
        assert!(err.contains("not written by shellfirm"), "{err}");
        exec(Provider::Cline, &hooks_only(true), &env).0.unwrap();
        assert!(script.exists());
        let st = integration_status_in(&env);
        let row = st
            .iter()
            .find(|s| s.tool == "cline" && s.scope == "user")
            .unwrap();
        assert!(!row.hook_present);
        assert!(row
            .notes
            .iter()
            .any(|n| n.contains("not written by shellfirm")));
    }

    #[test]
    fn opencode_plugin_install_project_scope_and_uninstall() {
        let sb = sandbox();
        let env = ConnectEnv {
            dirs: AgentDirs {
                xdg_config_home: Some(sb.tree.root.join("xdg")),
                current_exe: Some(PathBuf::from("/usr/local/bin/shellfirm")),
                ..AgentDirs::default()
            },
            backup_dir: Some(sb.tree.root.join("backups")),
            ..sb.env.clone()
        };
        exec(Provider::OpenCode, &hooks_only(false), &env)
            .0
            .unwrap();
        let user = sb.tree.root.join("xdg/opencode/plugins/shellfirm.js");
        let content = fs::read_to_string(&user).unwrap();
        assert!(content.contains("\"tool.execute.before\""));
        assert!(content.contains("const FALLBACK = \"/usr/local/bin/shellfirm\""));
        let project = Options {
            scope: Scope::Project,
            ..hooks_only(false)
        };
        exec(Provider::OpenCode, &project, &env).0.unwrap();
        let shared = sb.env.cwd.join(".opencode/plugins/shellfirm.js");
        // A team file names no personal binary path.
        assert!(fs::read_to_string(&shared)
            .unwrap()
            .contains("const FALLBACK = null"));
        let st = integration_status_in(&env);
        for scope in ["user", "project"] {
            let row = st
                .iter()
                .find(|s| s.tool == "opencode" && s.scope == scope)
                .unwrap();
            assert!(row.hook_installed && row.fail_closed, "{scope}");
        }
        let project_off = Options {
            scope: Scope::Project,
            ..hooks_only(true)
        };
        exec(Provider::OpenCode, &project_off, &env).0.unwrap();
        assert!(!shared.exists());
        assert!(!sb.env.cwd.join(".opencode/plugins").exists());
        exec(Provider::OpenCode, &hooks_only(true), &env).0.unwrap();
        assert!(!user.exists());
    }

    #[test]
    fn hook_only_agents_have_no_mcp() {
        let sb = sandbox();
        for p in [
            Provider::Codex,
            Provider::Gemini,
            Provider::Qwen,
            Provider::Copilot,
            Provider::OpenCode,
        ] {
            let (r, _) = exec(p, &opts(false, false, false), &sb.env);
            assert!(r.unwrap_err().contains("--mcp-only"), "{p:?}");
        }
    }

    #[test]
    fn every_provider_round_trips_through_its_cli_name() {
        for p in ALL_PROVIDERS {
            assert_eq!(Provider::from_name(p.cli_name()), Some(p));
            assert!(SUPPORTED_PROVIDERS.contains(p.cli_name()));
            assert!(command().find_subcommand(p.cli_name()).is_some(), "{p:?}");
        }
    }

    fn project(uninstall: bool) -> Options {
        Options {
            scope: Scope::Project,
            ..opts(uninstall, true, false)
        }
    }

    #[cfg(unix)]
    #[test]
    fn project_scope_never_writes_through_a_symlink_out_of_the_project() {
        let sb = sandbox();
        // Another agent's user file, reached from a link planted in the repo.
        let victim = sb.env.home.join(".codex/hooks.json");
        write(&victim, r#"{"hooks":{}}"#);
        let claude = sb.env.cwd.join(".claude");
        fs::create_dir_all(&claude).unwrap();
        std::os::unix::fs::symlink("../../home/.codex/hooks.json", claude.join("settings.json"))
            .unwrap();
        let (r, _) = exec(Provider::ClaudeCode, &project(false), &sb.env);
        let e = r.unwrap_err();
        assert!(e.contains("outside this project"), "{e}");
        assert_eq!(fs::read_to_string(&victim).unwrap(), r#"{"hooks":{}}"#);
        let st = integration_status_in(&sb.env);
        let row = st
            .iter()
            .find(|s| s.tool == "claude-code" && s.scope == "project")
            .unwrap();
        assert!(
            row.error
                .as_deref()
                .unwrap()
                .contains("outside this project"),
            "{row:?}"
        );

        // A linked directory (`.cursor` -> the user's `~/.cursor`).
        fs::create_dir_all(sb.env.home.join(".cursor")).unwrap();
        std::os::unix::fs::symlink(sb.env.home.join(".cursor"), sb.env.cwd.join(".cursor"))
            .unwrap();
        let (r, _) = exec(Provider::Cursor, &project(false), &sb.env);
        assert!(r.unwrap_err().contains("outside this project"));
        assert!(!sb.env.home.join(".cursor/hooks.json").exists());

        // A link that stays inside the project is fine.
        fs::create_dir_all(sb.env.cwd.join("shared")).unwrap();
        std::os::unix::fs::symlink("shared", sb.env.cwd.join(".gemini")).unwrap();
        exec(Provider::Gemini, &project(false), &sb.env).0.unwrap();
        assert!(sb.env.cwd.join("shared/settings.json").exists());
    }

    #[test]
    fn claude_disable_all_hooks_in_any_scope_turns_the_hook_off() {
        let mut sb = sandbox();
        exec(Provider::ClaudeCode, &hooks_only(false), &sb.env)
            .0
            .unwrap();
        let off = |env: &ConnectEnv| -> Vec<String> {
            integration_status_in(env)
                .into_iter()
                .find(|s| s.tool == "claude-code" && s.scope == "user")
                .unwrap()
                .hook_off
        };
        assert!(off(&sb.env).is_empty());
        // One line in a teammate's settings.local.json.
        let local = sb.env.cwd.join(".claude/settings.local.json");
        write(&local, r#"{"disableAllHooks": true}"#);
        let notes = off(&sb.env);
        assert!(notes[0].contains("disableAllHooks is true in"), "{notes:?}");
        assert!(notes[0].contains("settings.local.json"), "{notes:?}");
        // Managed settings take precedence over every other file.
        let managed = sb.tree.root.join("managed-settings.json");
        write(&managed, r#"{"disableAllHooks": false}"#);
        sb.env.dirs.claude_managed = Some(managed.clone());
        assert!(off(&sb.env).is_empty());
        write(&managed, r#"{"disableAllHooks": true}"#);
        fs::remove_file(&local).unwrap();
        assert!(off(&sb.env)[0].contains("managed-settings.json"));
        // `connect` says the hook does not run.
        let (_, out) = exec(Provider::ClaudeCode, &hooks_only(false), &sb.env);
        assert!(
            out.contains("The hook does not run yet: disableAllHooks"),
            "{out}"
        );
    }

    #[test]
    fn codex_hooks_feature_flag_off_is_reported() {
        let sb = sandbox();
        exec(Provider::Codex, &hooks_only(false), &sb.env)
            .0
            .unwrap();
        let config = sb.env.home.join(".codex/config.toml");
        let off = || -> Vec<String> {
            integration_status_in(&sb.env)
                .into_iter()
                .find(|s| s.tool == "codex" && s.scope == "user")
                .unwrap()
                .hook_off
        };
        for (text, hit) in [
            ("[features]\nhooks = false\n", true),
            ("[features]\ncodex_hooks = false\n", true),
            ("[features]\nhooks = true\n", false),
            ("model = \"x\"\n", false),
        ] {
            write(&config, text);
            assert_eq!(!off().is_empty(), hit, "{text}");
        }
        // The project config overrides the user one.
        write(&config, "[features]\nhooks = false\n");
        write(
            &sb.env.cwd.join(".codex/config.toml"),
            "[features]\nhooks = true\n",
        );
        assert!(off().is_empty());
        // hooks.json with a key Codex does not accept.
        let hooks = sb.env.home.join(".codex/hooks.json");
        let mut v = read_json(&hooks);
        v["sandbox"] = json!({});
        write(&hooks, &v.to_string());
        assert!(off()[0].contains("top-level keys other than `hooks` (sandbox)"));
    }

    #[test]
    fn claude_config_dir_is_honoured() {
        let mut sb = sandbox();
        let dir = sb.tree.root.join("ccd");
        sb.env.dirs.claude_config_dir = Some(dir.clone());
        exec(Provider::ClaudeCode, &opts(false, true, true), &sb.env)
            .0
            .unwrap();
        assert!(claude_hooks_installed(&read_json(
            &dir.join("settings.json")
        )));
        assert!(server_installed(
            &read_json(&dir.join(".claude.json")),
            "mcpServers"
        ));
        assert!(!sb.env.home.join(".claude").exists());
        assert!(!sb.env.home.join(".claude.json").exists());
        let st = integration_status_in(&sb.env);
        let row = st.iter().find(|s| s.tool == "claude-code").unwrap();
        assert!(row.hook_installed && row.mcp_registered, "{row:?}");
    }

    #[test]
    fn cursor_reads_go_to_the_hook() {
        let sb = sandbox();
        exec(Provider::Cursor, &hooks_only(false), &sb.env)
            .0
            .unwrap();
        let path = sb.env.home.join(".cursor/hooks.json");
        let v = read_json(&path);
        let read = &v["hooks"]["beforeReadFile"];
        assert_eq!(read.as_array().unwrap().len(), 1, "{v}");
        assert!(is_cursor_hook(read[0]["command"].as_str().unwrap()));
        let row = integration_status_in(&sb.env)
            .into_iter()
            .find(|s| s.tool == "cursor")
            .unwrap();
        assert!(row.file_tools_covered);
        // Without it, file tools are not fully covered.
        let mut v = v;
        v["hooks"].as_object_mut().unwrap().remove("beforeReadFile");
        write(&path, &v.to_string());
        let row = integration_status_in(&sb.env)
            .into_iter()
            .find(|s| s.tool == "cursor")
            .unwrap();
        assert!(!row.file_tools_covered);
        // A re-run adds it back; uninstall removes it.
        let (_, out) = exec(Provider::Cursor, &hooks_only(false), &sb.env);
        assert!(out.contains("hooks.beforeReadFile"), "{out}");
        exec(Provider::Cursor, &hooks_only(true), &sb.env)
            .0
            .unwrap();
        assert!(!path.exists());
    }

    #[test]
    fn backups_next_to_a_user_file_are_capped() {
        let sb = sandbox();
        let path = sb.env.home.join(".gemini/settings.json");
        write(&path, r#"{"model":"x"}"#);
        // A backup of another file in the same directory is not touched.
        write(
            &sb.env.home.join(".gemini/other.json.shellfirm-bak-1"),
            "{}",
        );
        for i in 0..6 {
            let mut v = read_json(&path);
            v["n"] = json!(i);
            write(&path, &v.to_string());
            exec(Provider::Gemini, &hooks_only(i % 2 == 1), &sb.env)
                .0
                .unwrap();
        }
        let all = backups(&sb.env.home.join(".gemini"));
        let ours = all
            .iter()
            .filter(|p| p.to_string_lossy().contains("settings.json.shellfirm-bak-"))
            .count();
        assert_eq!(ours, BACKUPS_KEPT, "{all:?}");
        assert_eq!(all.len(), BACKUPS_KEPT + 1, "{all:?}");
    }

    #[cfg(unix)]
    #[test]
    fn a_failed_write_shows_only_the_error() {
        use std::os::unix::fs::PermissionsExt;
        let sb = sandbox();
        let path = sb.env.home.join(".gemini/settings.json");
        write(&path, "{}");
        fs::set_permissions(&path, fs::Permissions::from_mode(0o444)).unwrap();
        let (r, out) = exec(Provider::Gemini, &hooks_only(false), &sb.env);
        assert!(r.unwrap_err().contains("read-only"));
        assert!(!out.contains("add hooks.BeforeTool"), "{out}");
    }

    #[test]
    fn status_reports_files_connect_refuses() {
        let sb = sandbox();
        write(&sb.env.home.join(".qwen/settings.json"), r#"{"hooks":[]}"#);
        let st = integration_status_in(&sb.env);
        let qwen = st
            .iter()
            .find(|s| s.tool == "qwen" && s.scope == "user")
            .unwrap();
        assert!(
            qwen.error
                .as_deref()
                .unwrap()
                .contains("`hooks` is not an object"),
            "{qwen:?}"
        );
        if !cfg!(windows) {
            write(
                &sb.env.home.join("Documents/Cline/Hooks/PreToolUse"),
                "#!/bin/sh\necho '{}'\n",
            );
            let st = integration_status_in(&sb.env);
            let cline = st
                .iter()
                .find(|s| s.tool == "cline" && s.scope == "user")
                .unwrap();
            assert!(
                cline
                    .notes
                    .iter()
                    .any(|n| n.contains("not written by shellfirm")),
                "{cline:?}"
            );
        }
    }

    #[test]
    fn only_claude_code_accepts_no_fail_closed() {
        let parse = |agent: &str| {
            command()
                .try_get_matches_from(["connect", agent, "--no-fail-closed"])
                .is_ok()
        };
        assert!(parse("claude-code"));
        for agent in ["codex", "gemini", "cursor", "copilot"] {
            assert!(!parse(agent), "{agent}");
        }
    }

    #[test]
    fn a_shellfirm_hook_in_managed_settings_gets_its_own_row() {
        let mut sb = sandbox();
        exec(Provider::ClaudeCode, &hooks_only(false), &sb.env)
            .0
            .unwrap();
        let user = sb.env.dirs.claude_dir(&sb.env.home).join("settings.json");
        let hooks = read_value(&user).unwrap().unwrap()["hooks"].clone();
        let managed = sb.tree.root.join("managed-settings.json");
        sb.env.dirs.claude_managed = Some(managed.clone());
        let row = |env: &ConnectEnv, scope: &str| {
            integration_status_in(env)
                .into_iter()
                .find(|s| s.tool == "claude-code" && s.scope == scope)
        };
        // No managed file, or one without a shellfirm hook: no row.
        assert!(row(&sb.env, "managed").is_none());
        write(&managed, r#"{"allowManagedHooksOnly": true}"#);
        assert!(row(&sb.env, "managed").is_none());
        assert!(row(&sb.env, "user").unwrap().hook_off[0].contains("put the shellfirm hook"));

        write(
            &managed,
            &json!({"allowManagedHooksOnly": true, "hooks": hooks}).to_string(),
        );
        let m = row(&sb.env, "managed").unwrap();
        assert!(
            m.hook_installed && m.file_tools_covered && m.fail_closed,
            "{m:?}"
        );
        assert!(m.hook_off.is_empty() && m.error.is_none(), "{m:?}");
        assert_eq!(m.hook_file.as_deref(), Some(managed.to_str().unwrap()));
        let u = row(&sb.env, "user").unwrap();
        assert!(
            u.hook_off[0].contains("the shellfirm hook in that file runs instead"),
            "{u:?}"
        );

        // Uninstalling the user hook leaves the managed one (and its row).
        let o = Options {
            uninstall: true,
            ..hooks_only(false)
        };
        exec(Provider::ClaudeCode, &o, &sb.env).0.unwrap();
        assert!(row(&sb.env, "managed").unwrap().hook_installed);
        assert!(!row(&sb.env, "user").unwrap().hook_present);

        // Only managed settings turn a managed hook off.
        write(
            &managed,
            &json!({"disableAllHooks": true, "hooks": hooks}).to_string(),
        );
        assert!(row(&sb.env, "managed").unwrap().hook_off[0].contains("disableAllHooks"));
        write(&managed, "{");
        assert!(row(&sb.env, "managed").is_some_and(|m| m.error.is_some()));
    }

    #[test]
    fn managed_hooks_only_turns_the_user_and_project_hook_off() {
        let mut sb = sandbox();
        exec(Provider::ClaudeCode, &hooks_only(false), &sb.env)
            .0
            .unwrap();
        let managed = sb.tree.root.join("managed-settings.json");
        sb.env.dirs.claude_managed = Some(managed.clone());
        let off = |env: &ConnectEnv| -> Vec<String> {
            integration_status_in(env)
                .into_iter()
                .find(|s| s.tool == "claude-code" && s.scope == "user")
                .unwrap()
                .hook_off
        };
        write(&managed, r#"{"allowManagedHooksOnly": false}"#);
        assert!(off(&sb.env).is_empty());
        write(&managed, r#"{"allowManagedHooksOnly": true}"#);
        let notes = off(&sb.env);
        assert_eq!(notes.len(), 1, "{notes:?}");
        assert!(
            notes[0].contains("allowManagedHooksOnly is true in")
                && notes[0].contains("managed-settings.json"),
            "{notes:?}"
        );
        let (_, out) = exec(Provider::ClaudeCode, &hooks_only(false), &sb.env);
        assert!(
            out.contains("The hook does not run yet: allowManagedHooksOnly"),
            "{out}"
        );
    }

    #[test]
    fn a_relative_claude_config_dir_is_relative_to_the_current_directory() {
        let cwd = Path::new("/work/repo");
        assert_eq!(
            claude_config_dir_from(Some("rel".into()), Some(cwd)),
            Some(cwd.join("rel"))
        );
        assert_eq!(
            claude_config_dir_from(Some("~/cc".into()), Some(cwd)),
            Some(cwd.join("~/cc"))
        );
        assert_eq!(
            claude_config_dir_from(Some("/opt/cc".into()), Some(cwd)),
            Some(PathBuf::from("/opt/cc"))
        );
        assert_eq!(claude_config_dir_from(Some("".into()), Some(cwd)), None);
        assert_eq!(claude_config_dir_from(None, Some(cwd)), None);
    }

    #[test]
    fn only_backups_shellfirm_names_count_toward_the_cap() {
        assert!(is_backup_suffix("20261119T123456Z"));
        assert!(is_backup_suffix("20261119T123456Z-2"));
        for bad in [
            "mine",
            "20261119T123456Z-",
            "20261119T123456Z-x",
            "1",
            "2026111T123456Z",
        ] {
            assert!(!is_backup_suffix(bad), "{bad}");
        }
        let sb = sandbox();
        let path = sb.env.home.join(".gemini/settings.json");
        write(&path, r#"{"model":"x"}"#);
        let mine = sb.env.home.join(".gemini/settings.json.shellfirm-bak-mine");
        write(&mine, "{}");
        for i in 0..6 {
            exec(Provider::Gemini, &hooks_only(i % 2 == 1), &sb.env)
                .0
                .unwrap();
        }
        assert!(mine.exists());
        let all = backups(&sb.env.home.join(".gemini"));
        assert_eq!(all.len(), BACKUPS_KEPT + 1, "{all:?}");
    }

    #[cfg(unix)]
    #[test]
    fn a_fifo_at_a_settings_path_is_an_error_not_a_hang() {
        let sb = sandbox();
        let path = sb.env.home.join(".claude/settings.json");
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        assert!(std::process::Command::new("mkfifo")
            .arg(&path)
            .status()
            .unwrap()
            .success());
        for dry_run in [true, false] {
            let o = Options {
                dry_run,
                ..hooks_only(false)
            };
            let (r, _) = exec(Provider::ClaudeCode, &o, &sb.env);
            assert!(r.unwrap_err().contains("not a regular file"));
        }
        let row = integration_status_in(&sb.env)
            .into_iter()
            .find(|s| s.tool == "claude-code" && s.scope == "user")
            .unwrap();
        assert!(row.error.unwrap().contains("not a regular file"));
    }
}