destructive_command_guard 0.4.3

A Claude Code hook that blocks destructive commands before they execute
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
//! Execution-context classification for shell commands.
//!
//! This module provides fast classification of which parts of a command line
//! are actually executed vs merely data. This enables the pattern matching
//! engine to reduce false positives by only checking executable contexts.
//!
//! # Design Principles
//!
//! 1. **Ambiguous → Executed**: If classification is uncertain, treat as executable
//! 2. **False positives over false negatives**: Better to block safe commands than allow dangerous ones
//! 3. **Performance**: Classification adds <100μs to typical commands
//!
//! # Examples
//!
//! ```ignore
//! use destructive_command_guard::context::{classify_command, SpanKind};
//!
//! let spans = classify_command("git commit -m 'Fix rm -rf detection'");
//! // The 'Fix rm -rf detection' part is classified as Data (single-quoted)
//! // The 'git commit -m' part is classified as Executed
//! ```

use aho_corasick::AhoCorasick;
use smallvec::SmallVec;
use std::borrow::Cow;
use std::ops::Range;
use std::sync::LazyLock;

/// Classification of a command-line span.
///
/// Each span is classified according to whether it will be executed by the shell
/// or is purely data. The pattern matching engine uses this to reduce matching
/// in known-safe contexts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SpanKind {
    /// Command word or unquoted argument - fully executed by shell.
    /// Pattern matching MUST be applied.
    Executed,

    /// Quoted argument to a known-safe command (e.g., `git commit -m "..."`).
    /// Pattern matching should be applied with lower priority.
    Argument,

    /// Content of -c/-e flags (bash -c, python -c, node -e).
    /// Pattern matching MUST be applied - this is code!
    InlineCode,

    /// Single-quoted string - no variable substitution possible.
    /// Pattern matching can be SKIPPED (safe to ignore).
    Data,

    /// Heredoc body - escalate to Tier 2/3 analysis.
    /// Pattern matching should be applied with heredoc-aware logic.
    HeredocBody,

    /// Ambiguous context - conservative treatment as Executed.
    /// Pattern matching MUST be applied.
    Unknown,

    /// Shell comment (starts with #).
    /// Pattern matching should be SKIPPED.
    Comment,
}

impl SpanKind {
    /// Returns true if this span should have destructive patterns checked.
    #[inline]
    #[must_use]
    pub const fn requires_pattern_check(self) -> bool {
        match self {
            Self::Executed | Self::InlineCode | Self::HeredocBody | Self::Unknown => true,
            Self::Argument | Self::Data | Self::Comment => false,
        }
    }

    /// Returns true if this span is definitely safe to skip pattern matching.
    #[inline]
    #[must_use]
    pub const fn is_safe_data(self) -> bool {
        matches!(self, Self::Data | Self::Comment)
    }

    /// Returns true if this is executed code (not a data argument).
    #[inline]
    #[must_use]
    pub const fn is_executable(self) -> bool {
        matches!(self, Self::Executed | Self::InlineCode | Self::Unknown)
    }
}

/// A classified span within a command string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Span {
    /// The classification of this span.
    pub kind: SpanKind,
    /// Byte range within the original command string.
    pub byte_range: Range<usize>,
}

impl Span {
    /// Create a new span.
    #[inline]
    #[must_use]
    pub const fn new(kind: SpanKind, start: usize, end: usize) -> Self {
        Self {
            kind,
            byte_range: start..end,
        }
    }

    /// Get the span text from the original command.
    #[inline]
    #[must_use]
    pub fn text<'a>(&self, command: &'a str) -> &'a str {
        &command[self.byte_range.clone()]
    }

    /// Returns the length of this span in bytes.
    #[inline]
    #[must_use]
    pub const fn len(&self) -> usize {
        self.byte_range.end - self.byte_range.start
    }

    /// Returns true if this span is empty.
    #[inline]
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.byte_range.start == self.byte_range.end
    }
}

type SpanVec = SmallVec<[Span; 32]>;

/// A collection of classified spans for a command.
///
/// This is the main output of the context classifier. It can be used to:
/// - Check if any executable spans contain destructive patterns
/// - Skip pattern matching for data-only spans
/// - Provide context for error messages and explanations
#[derive(Debug, Clone, Default)]
pub struct CommandSpans {
    /// All classified spans, in order of appearance.
    spans: SpanVec,
}

impl CommandSpans {
    /// Create an empty spans collection.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            spans: SpanVec::new(),
        }
    }

    /// Add a span to the collection.
    #[inline]
    pub fn push(&mut self, span: Span) {
        self.spans.push(span);
    }

    /// Get all spans.
    #[inline]
    #[must_use]
    pub fn spans(&self) -> &[Span] {
        &self.spans
    }

    /// Get only spans that require pattern checking.
    pub fn executable_spans(&self) -> impl Iterator<Item = &Span> {
        self.spans
            .iter()
            .filter(|s| s.kind.requires_pattern_check())
    }

    /// Get only data spans (safe to skip).
    pub fn data_spans(&self) -> impl Iterator<Item = &Span> {
        self.spans.iter().filter(|s| s.kind.is_safe_data())
    }

    /// Returns true if any span requires pattern checking.
    #[must_use]
    pub fn has_executable_content(&self) -> bool {
        self.spans.iter().any(|s| s.kind.requires_pattern_check())
    }

    /// Returns true if the entire command is safe data (rare).
    #[must_use]
    pub fn is_all_data(&self) -> bool {
        !self.spans.is_empty() && self.spans.iter().all(|s| s.kind.is_safe_data())
    }

    /// Extract the text content for all executable spans.
    #[must_use]
    pub fn executable_text<'a>(&self, command: &'a str) -> Vec<&'a str> {
        self.executable_spans().map(|s| s.text(command)).collect()
    }
}

/// State machine states for the shell tokenizer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TokenizerState {
    Normal,
    SingleQuote,
    DoubleQuote,
    CommandSubst, // Inside $(...), scanning for matching )
    Backtick,     // Inside `...`, scanning for matching `
    Comment,      // Inside #... (newline terminates)
}

/// Shell command tokenizer and context classifier.
pub struct ContextClassifier {
    /// Commands that take inline code as the next argument after -c/-e
    inline_code_commands: &'static [&'static str],
}

impl Default for ContextClassifier {
    fn default() -> Self {
        Self::new()
    }
}

impl ContextClassifier {
    /// Create a new context classifier with default settings.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            inline_code_commands: &[
                "bash", "sh", "zsh", "ksh", "dash", // shells with -c
                "python", "python3", "python2", // python with -c
                "node", "nodejs", // node with -e
                "ruby",   // ruby with -e
                "perl",   // perl with -e
                "php",    // php with -r
                "lua",    // lua with -e
            ],
        }
    }

    /// Returns a `CommandSpans` structure containing classified spans.
    /// Each byte in the command will belong to exactly one span.
    #[must_use]
    #[allow(clippy::too_many_lines)]
    pub fn classify(&self, command: &str) -> CommandSpans {
        let bytes = command.as_bytes();
        let len = bytes.len();

        if len == 0 {
            return CommandSpans::new();
        }

        let mut spans = CommandSpans::new();
        let mut stack = vec![TokenizerState::Normal];

        // Start of the current span (token or subst)
        let mut span_start = 0;

        // Current classification kind for the active span (only relevant for Normal/DoubleQuote)
        let mut current_kind = SpanKind::Executed;

        // Track inline code flags (e.g. -c)
        let mut pending_inline_code = false;
        let mut last_word_start = 0;

        // Track whether we're in "command position" - where the next word would be an executable.
        // This is true at the start of a command, or right after a command separator (|, ||, &&, ;).
        // Used to treat double-quoted strings at command position as Executed rather than Argument.
        let mut in_command_position = true;

        let in_inline_context = |state_stack: &[TokenizerState]| {
            state_stack
                .iter()
                .any(|s| matches!(s, TokenizerState::CommandSubst | TokenizerState::Backtick))
        };

        let mut i = 0;
        while i < len {
            let byte = bytes[i];
            let Some(current_state) = stack.last().copied() else {
                break;
            };

            // Handle escapes first (except in SingleQuote where \ is literal)
            if byte == b'\\' && current_state != TokenizerState::SingleQuote {
                let effective = !matches!(current_state, TokenizerState::Comment);
                if effective {
                    // Consume the backslash and the next character
                    i += 1; // Skip \
                    if i < len {
                        // If newline, it's line continuation (ignored/joined).
                        // If char, it's escaped literal.
                        i += 1;
                    }
                    continue;
                }
            }

            match current_state {
                TokenizerState::Normal => {
                    match byte {
                        b'\'' => {
                            if i > span_start {
                                spans.push(Span::new(current_kind, span_start, i));
                                // If we pushed content before the quote, we're past command position
                                // (e.g., VAR='...' - VAR= is not the command, the quote is not at cmd start)
                                in_command_position = false;
                            }
                            span_start = i;
                            stack.push(TokenizerState::SingleQuote);
                            let inline_here = if pending_inline_code {
                                pending_inline_code = false;
                                true
                            } else if last_word_start < i {
                                let word = &command[last_word_start..i];
                                is_inline_code_flag(word)
                                    && self.check_inline_code_context(
                                        command,
                                        last_word_start,
                                        word,
                                    )
                            } else {
                                false
                            };
                            current_kind = if inline_here {
                                SpanKind::InlineCode
                            } else if in_command_position {
                                // Quoted string at command position is likely an executable
                                SpanKind::Executed
                            } else {
                                SpanKind::Data
                            };
                        }
                        b'"' => {
                            if i > span_start {
                                spans.push(Span::new(current_kind, span_start, i));
                                // If we pushed content before the quote, we're past command position
                                // (e.g., VAR="..." - VAR= is not the command, the quote is not at cmd start)
                                in_command_position = false;
                            }
                            span_start = i;
                            stack.push(TokenizerState::DoubleQuote);
                            let inline_here = if pending_inline_code {
                                pending_inline_code = false;
                                true
                            } else if last_word_start < i {
                                let word = &command[last_word_start..i];
                                is_inline_code_flag(word)
                                    && self.check_inline_code_context(
                                        command,
                                        last_word_start,
                                        word,
                                    )
                            } else {
                                false
                            };
                            current_kind = if inline_here {
                                SpanKind::InlineCode
                            } else if in_command_position {
                                // Quoted string at command position is likely an executable
                                // (e.g., "C:/Program Files/Git/bin/git" status)
                                SpanKind::Executed
                            } else {
                                SpanKind::Argument
                            };
                        }
                        b'$' if i + 1 < len && bytes[i + 1] == b'(' => {
                            if i > span_start {
                                spans.push(Span::new(current_kind, span_start, i));
                            }
                            span_start = i;
                            i += 1; // Skip (
                            stack.push(TokenizerState::CommandSubst);
                            // We don't set current_kind here because CommandSubst span
                            // will be emitted when we POP.
                        }
                        b'`' => {
                            if i > span_start {
                                spans.push(Span::new(current_kind, span_start, i));
                            }
                            span_start = i;
                            stack.push(TokenizerState::Backtick);
                        }
                        b'|' | b';' | b'&' => {
                            // Check for operators
                            // For simple classification, treat as break.
                            if i > span_start {
                                spans.push(Span::new(current_kind, span_start, i));
                            }
                            // Determine operator length
                            let mut op_len = 1;
                            if i + 1 < len {
                                let next = bytes[i + 1];
                                if (byte == b'|' && next == b'|') || (byte == b'&' && next == b'&')
                                {
                                    op_len = 2;
                                }
                            }

                            spans.push(Span::new(SpanKind::Executed, i, i + op_len));
                            i += op_len;
                            // Advance loop manually
                            span_start = i;
                            current_kind = SpanKind::Executed;
                            pending_inline_code = false;
                            // After a command separator, the next word is a new command
                            in_command_position = true;
                            continue;
                        }
                        b'#' => {
                            // Comment start if start of word (including after separators).
                            if i == 0
                                || bytes[i - 1].is_ascii_whitespace()
                                || matches!(bytes[i - 1], b'|' | b'&' | b';')
                            {
                                if i > span_start {
                                    spans.push(Span::new(current_kind, span_start, i));
                                }
                                span_start = i;
                                stack.push(TokenizerState::Comment);
                            }
                        }
                        b' ' | b'\t' | b'\n' => {
                            // Whitespace
                            if i > last_word_start {
                                let word = &command[last_word_start..i];
                                if is_inline_code_flag(word) {
                                    pending_inline_code = self.check_inline_code_context(
                                        command,
                                        last_word_start,
                                        word,
                                    );
                                }
                                // After the first word, we're no longer in command position
                                if in_command_position && !word.is_empty() {
                                    in_command_position = false;
                                }
                            }
                            last_word_start = i + 1;
                        }
                        _ => {}
                    }
                }
                TokenizerState::DoubleQuote => {
                    match byte {
                        b'"' => {
                            // Close double quote
                            stack.pop();
                            // Only emit span if we are not inside a command substitution
                            if !matches!(
                                stack.last(),
                                Some(TokenizerState::CommandSubst | TokenizerState::Backtick)
                            ) {
                                spans.push(Span::new(current_kind, span_start, i + 1));
                                span_start = i + 1;
                                // If this quoted string was the command, we're no longer in command position
                                if current_kind == SpanKind::Executed {
                                    in_command_position = false;
                                }
                                current_kind = SpanKind::Executed;
                            }
                        }
                        b'$' if i + 1 < len && bytes[i + 1] == b'(' => {
                            // Command substitution inside double quotes
                            // We treat it as InlineCode, but do NOT push CommandSubst to stack?
                            // If we don't push, we don't handle nested quotes inside it correctly.
                            // We MUST push CommandSubst to handle nesting.
                            // But when we pop back, we are still in DoubleQuote.

                            // The span logic in dcg expects flat spans.
                            // If we have "foo $(bar) baz", we want:
                            // "foo " (Argument)
                            // "$(bar)" (InlineCode)
                            // " baz" (Argument)

                            // Emit preceding string part
                            if i > span_start {
                                spans.push(Span::new(current_kind, span_start, i));
                            }
                            span_start = i;
                            i += 1; // Skip (
                            stack.push(TokenizerState::CommandSubst);
                        }
                        b'`' => {
                            if i > span_start {
                                spans.push(Span::new(current_kind, span_start, i));
                            }
                            span_start = i;
                            stack.push(TokenizerState::Backtick);
                        }
                        _ => {}
                    }
                }
                TokenizerState::SingleQuote => {
                    if byte == b'\'' {
                        stack.pop();
                        // Only emit span if we are not inside a command substitution
                        if !matches!(
                            stack.last(),
                            Some(TokenizerState::CommandSubst | TokenizerState::Backtick)
                        ) {
                            spans.push(Span::new(current_kind, span_start, i + 1));
                            span_start = i + 1;
                            // If this quoted string was the command, we're no longer in command position
                            if current_kind == SpanKind::Executed {
                                in_command_position = false;
                            }
                            current_kind = SpanKind::Executed;
                        }
                    }
                }
                TokenizerState::CommandSubst => {
                    // Inside $( ... )
                    // We scan for matching ) but respect quotes
                    match byte {
                        b')' => {
                            stack.pop();
                            // Only emit span if we have fully exited the command substitution structure.
                            // If we are still in CommandSubst (nested parens/subshell), we continue scanning.
                            if !in_inline_context(&stack) {
                                spans.push(Span::new(SpanKind::InlineCode, span_start, i + 1));
                                span_start = i + 1;
                                // Restore previous kind based on state we returned to
                                match stack.last() {
                                    Some(TokenizerState::Normal) => {
                                        current_kind = SpanKind::Executed;
                                    }
                                    Some(TokenizerState::DoubleQuote) => {
                                        current_kind = SpanKind::Argument;
                                    }
                                    _ => {}
                                }
                            }
                        }
                        b'(' => {
                            // Nested $( ( ... ) ) - subshell or nested subst?
                            // Just recurse CommandSubst to track parens nesting
                            // Note: $( ( ) ) - the inner ( ) are a subshell.
                            // We can just track depth by pushing another CommandSubst
                            stack.push(TokenizerState::CommandSubst);
                        }
                        b'"' => stack.push(TokenizerState::DoubleQuote),
                        b'\'' => stack.push(TokenizerState::SingleQuote),
                        b'`' => stack.push(TokenizerState::Backtick),
                        b'#' => {
                            if i == 0 || bytes[i - 1].is_ascii_whitespace() {
                                stack.push(TokenizerState::Comment);
                            }
                        }
                        _ => {}
                    }
                }
                TokenizerState::Backtick => {
                    if byte == b'`' {
                        stack.pop();
                        // Only emit span if we have fully exited the command substitution structure.
                        if !in_inline_context(&stack) {
                            spans.push(Span::new(SpanKind::InlineCode, span_start, i + 1));
                            span_start = i + 1;
                            match stack.last() {
                                Some(TokenizerState::Normal) => {
                                    current_kind = SpanKind::Executed;
                                }
                                Some(TokenizerState::DoubleQuote) => {
                                    current_kind = SpanKind::Argument;
                                }
                                _ => {}
                            }
                        }
                    }
                }
                TokenizerState::Comment => {
                    if byte == b'\n' {
                        stack.pop();
                        // Comment inside CommandSubst?
                        // If we are in CommandSubst, we don't emit span yet.
                        // If we are in Normal, we emit span.
                        if matches!(stack.last(), Some(TokenizerState::Normal)) {
                            spans.push(Span::new(SpanKind::Comment, span_start, i + 1));
                            span_start = i + 1;
                            current_kind = SpanKind::Executed;
                        }
                    }
                }
            }
            i += 1;
        }

        // Handle remaining
        if span_start < len {
            // Determine fallback kind based on state
            let kind = match stack.last() {
                Some(TokenizerState::Normal) => current_kind,
                Some(TokenizerState::DoubleQuote) => {
                    if current_kind == SpanKind::Argument {
                        SpanKind::Unknown
                    } else {
                        current_kind
                    }
                }
                Some(TokenizerState::SingleQuote) | None => SpanKind::Unknown,
                Some(TokenizerState::Comment) => SpanKind::Comment,
                Some(TokenizerState::CommandSubst | TokenizerState::Backtick) => {
                    SpanKind::InlineCode
                }
            };
            spans.push(Span::new(kind, span_start, len));
        }

        spans
    }

    /// Check if the word before a -c/-e flag is an inline-code command.
    fn check_inline_code_context(&self, command: &str, flag_start: usize, flag: &str) -> bool {
        // Special case for env -S: scan the whole segment for 'env'
        if flag == "-S" {
            // env -S "script" treats the argument as a script/command line.
            // Be conservative: treat as inline code if the current segment
            // contains an env invocation anywhere before the flag.
            return env_split_string_context(command, flag_start);
        }

        // For standard interpreters (python -c, bash -c), scan backwards skipping flags
        let before = &command[..flag_start];

        // Limit search to reasonable lookback (e.g. 20 tokens or start of segment)
        // to avoid performance cliffs on massive commands.
        // We use segment_start_before_flag to respect pipe boundaries.
        let segment_start = segment_start_before_flag(command, flag_start);
        let segment = &before[segment_start..];

        // Tokenize in reverse to find the command word
        for token in segment.split_whitespace().rev() {
            // Skip flags (heuristic: starts with -)
            if token.starts_with('-') && token.len() > 1 {
                continue;
            }

            // Skip env assignments (VAR=VAL)
            if token.contains('=') {
                continue;
            }

            // Strip quotes if present (handle "python", "/usr/bin/python", etc.)
            let token_unquoted = if (token.starts_with('"') && token.ends_with('"'))
                || (token.starts_with('\'') && token.ends_with('\''))
            {
                if token.len() >= 2 {
                    &token[1..token.len() - 1]
                } else {
                    token
                }
            } else {
                token
            };

            // Found a potential command word
            // Handle both Unix (/) and Windows (\) path separators
            let base_name = token_unquoted
                .rsplit(&['/', '\\'][..])
                .next()
                .unwrap_or(token_unquoted);
            // Strip Windows .exe extension if present
            let base_name = base_name
                .strip_suffix(".exe")
                .or_else(|| base_name.strip_suffix(".EXE"))
                .unwrap_or(base_name);

            // Skip wrappers that might precede the interpreter
            if matches!(base_name, "sudo" | "time" | "nohup" | "env" | "command") {
                continue;
            }

            // Check for known interpreter, allowing for version suffixes
            // e.g. "python3.11" matches "python", "node18" matches "node"
            let is_interpreter = self.inline_code_commands.iter().any(|&known| {
                if base_name == known {
                    return true;
                }
                if let Some(suffix) = base_name.strip_prefix(known) {
                    // Suffix must be non-empty and consist only of digits/dots
                    return !suffix.is_empty()
                        && suffix.chars().all(|c| c.is_ascii_digit() || c == '.');
                }
                false
            });

            if is_interpreter {
                return true;
            }

            // If it's not a known interpreter, it might be an argument to a previous flag.
            // Continue searching backwards.
        }

        false
    }
}

/// Classify a command string's execution contexts.
///
/// This is a convenience function that creates a default classifier
/// and classifies the given command.
///
/// # Example
///
/// ```ignore
/// let spans = classify_command("echo 'hello world' | cat");
/// for span in spans.spans() {
///     println!("{:?}: {}", span.kind, span.text("echo 'hello world' | cat"));
/// }
/// ```
#[inline]
#[must_use]
pub fn classify_command(command: &str) -> CommandSpans {
    ContextClassifier::new().classify(command)
}

// =============================================================================
// Safe String-Argument Registry (git_safety_guard-t8x.1)
// =============================================================================

/// A registry of commands and flags whose arguments are purely data, not code.
///
/// This enables the pattern matching engine to suppress false positives for
/// common developer workflows like:
/// - `git commit -m "Fix rm -rf detection"` (commit message is data)
/// - `rg "rm -rf" src/` (search pattern is data)
/// - `bd create --description="This blocks rm -rf"` (description is data)
///
/// # Design Principles
///
/// 1. **Conservative**: Only entries where we're 100% confident args are data
/// 2. **Explicit**: Each entry must have a test demonstrating its need
/// 3. **Versioned**: Registry can be extended over time with new entries
#[derive(Debug, Clone)]
pub struct SafeStringRegistry {
    /// Commands where ALL arguments are data (e.g., echo, printf)
    all_args_data: &'static [&'static str],
    /// Command + flag combinations where the flag's value is data
    flag_data_pairs: &'static [SafeFlagEntry],
}

/// An entry for a command+flag combination whose argument is data.
#[derive(Debug, Clone, Copy)]
pub struct SafeFlagEntry {
    /// The command (base name, without path)
    pub command: &'static str,
    /// The short flag (e.g., "-m")
    pub short_flag: Option<&'static str>,
    /// The long flag (e.g., "--message")
    pub long_flag: Option<&'static str>,
    /// Whether the flag can take multiple value tokens (until next flag).
    pub multi_value: bool,
}

impl SafeFlagEntry {
    /// Create a new entry with both short and long flags.
    #[must_use]
    pub const fn new(
        command: &'static str,
        short_flag: Option<&'static str>,
        long_flag: Option<&'static str>,
    ) -> Self {
        Self {
            command,
            short_flag,
            long_flag,
            multi_value: false,
        }
    }

    /// Create an entry with only a short flag.
    #[must_use]
    pub const fn short(command: &'static str, flag: &'static str) -> Self {
        Self {
            command,
            short_flag: Some(flag),
            long_flag: None,
            multi_value: false,
        }
    }

    /// Create an entry with only a long flag.
    #[must_use]
    pub const fn long(command: &'static str, flag: &'static str) -> Self {
        Self {
            command,
            short_flag: None,
            long_flag: Some(flag),
            multi_value: false,
        }
    }

    /// Create an entry with both short and long flags (convenience).
    #[must_use]
    pub const fn both(command: &'static str, short: &'static str, long: &'static str) -> Self {
        Self {
            command,
            short_flag: Some(short),
            long_flag: Some(long),
            multi_value: false,
        }
    }

    /// Create an entry with only a long flag that can take multiple values.
    #[must_use]
    pub const fn long_multi(command: &'static str, flag: &'static str) -> Self {
        Self {
            command,
            short_flag: None,
            long_flag: Some(flag),
            multi_value: true,
        }
    }

    /// Create an entry with both short and long flags that can take multiple values.
    #[must_use]
    pub const fn both_multi(
        command: &'static str,
        short: &'static str,
        long: &'static str,
    ) -> Self {
        Self {
            command,
            short_flag: Some(short),
            long_flag: Some(long),
            multi_value: true,
        }
    }
}

/// The default safe string registry with v1 entries.
pub static SAFE_STRING_REGISTRY: SafeStringRegistry = SafeStringRegistry {
    // Commands where ALL arguments are data (never executed by shell)
    all_args_data: &["echo", "printf"],

    // Command + flag combinations where the flag's value is data
    flag_data_pairs: &[
        // Git message flags - commit/tag messages are documentation
        SafeFlagEntry::both("git", "-m", "--message"),
        // Git log search - pattern is data (not executed)
        SafeFlagEntry::long("git", "--grep"),
        // Note: git commit -m is actually 'git' command with 'commit' subcommand + -m flag
        // We handle this at the command level since -m is always data for git

        // Beads CLI - descriptions and notes are documentation
        SafeFlagEntry::long_multi("bd", "--description"),
        SafeFlagEntry::long_multi("bd", "--title"),
        SafeFlagEntry::long_multi("bd", "--notes"),
        SafeFlagEntry::long_multi("bd", "--reason"),
        // Search tools - patterns are data, not executed (only pattern-supplying flags)
        SafeFlagEntry::both("grep", "-e", "--regexp"),
        SafeFlagEntry::both("rg", "-e", "--regexp"),
        SafeFlagEntry::both("ag", "-e", "--pattern"), // Silver Searcher
        SafeFlagEntry::both("ack", "-e", "--pattern"), // ack search tool
        // GitHub CLI - titles and bodies are documentation
        SafeFlagEntry::both("gh", "-t", "--title"),
        SafeFlagEntry::both("gh", "-b", "--body"),
        SafeFlagEntry::both("gh", "-m", "--message"),
        // curl - request data and headers are not executed
        SafeFlagEntry::both("curl", "-d", "--data"),
        SafeFlagEntry::both("curl", "-H", "--header"),
        SafeFlagEntry::long("curl", "--data-raw"),
        SafeFlagEntry::long("curl", "--data-binary"),
        // jq - variable values are data, not code
        SafeFlagEntry::long("jq", "--arg"),
        SafeFlagEntry::long("jq", "--argjson"),
        SafeFlagEntry::long("jq", "--slurpfile"),
        // Docker - labels are metadata, not code
        SafeFlagEntry::both("docker", "-l", "--label"),
        // kubectl - annotations and labels are metadata
        SafeFlagEntry::long("kubectl", "--annotation"),
        SafeFlagEntry::both("kubectl", "-l", "--label"),
        // xargs - placeholder string is literal
        SafeFlagEntry::short("xargs", "-I"),
        // Cargo/npm - package descriptions
        SafeFlagEntry::long("cargo", "--message"),
        SafeFlagEntry::long("npm", "--message"),
    ],
};

/// Pre-computed Aho-Corasick automaton for quick-reject in sanitization.
///
/// If none of these command names appear in the input, we can skip tokenization
/// entirely since no masking would occur. This is a significant optimization for
/// commands like heredocs that don't invoke any of the safe-registry commands.
static SAFE_COMMANDS_MATCHER: LazyLock<AhoCorasick> = LazyLock::new(|| {
    // Collect all unique command names from the registry plus special built-ins
    let commands: &[&str] = &[
        // all_args_data commands
        "echo", "printf", // Commands from flag_data_pairs
        "git", "bd", "grep", "rg", "ag", "ack", "gh", "curl", "jq", "docker", "kubectl", "xargs",
        "cargo", "npm",
        // Special built-in: `command -v/-V` queries mask their arguments
        "command",
    ];
    AhoCorasick::new(commands).expect("static patterns should compile")
});

impl SafeStringRegistry {
    /// Check if a command has ALL its arguments as data.
    ///
    /// For commands like `echo` and `printf`, everything after the command
    /// is purely printed output, not executed.
    #[must_use]
    pub fn is_all_args_data(&self, command: &str) -> bool {
        let base_name = command.rsplit('/').next().unwrap_or(command);
        self.all_args_data.contains(&base_name)
    }

    /// Check if a specific flag for a command has a data-only argument.
    ///
    /// Returns true if the flag's argument should be treated as data
    /// (safe to skip pattern matching).
    #[must_use]
    pub fn is_flag_data(&self, command: &str, flag: &str) -> bool {
        let base_name = command.rsplit('/').next().unwrap_or(command);

        self.flag_data_pairs.iter().any(|entry| {
            entry.command == base_name
                && (entry.short_flag == Some(flag) || entry.long_flag == Some(flag))
        })
    }

    /// Check if a flag's data can span multiple tokens (until the next flag).
    #[must_use]
    pub fn is_flag_data_multivalue(&self, command: &str, flag: &str) -> bool {
        let base_name = command.rsplit('/').next().unwrap_or(command);

        self.flag_data_pairs.iter().any(|entry| {
            entry.command == base_name
                && entry.multi_value
                && (entry.short_flag == Some(flag) || entry.long_flag == Some(flag))
        })
    }

    /// Find all data-only flags for a given command.
    #[must_use]
    pub fn data_flags_for_command(&self, command: &str) -> Vec<&'static str> {
        let base_name = command.rsplit('/').next().unwrap_or(command);

        self.flag_data_pairs
            .iter()
            .filter(|entry| entry.command == base_name)
            .flat_map(|entry| {
                let short = entry.short_flag.into_iter();
                let long = entry.long_flag.into_iter();
                short.chain(long)
            })
            .collect()
    }
}

/// Check if a command's argument at a given position should be treated as data.
///
/// This is a convenience function that uses the default registry to determine
/// if an argument is purely data (not executed by the shell).
///
/// # Arguments
///
/// * `command` - The full command string
/// * `preceding_flag` - The flag (e.g., `-m`, `--message`) that owns the
///   argument under consideration, if any
///
/// # Returns
///
/// `true` if the argument at that position is known to be data-only.
#[must_use]
pub fn is_argument_data(command: &str, preceding_flag: Option<&str>) -> bool {
    let parts: Vec<&str> = command.split_whitespace().collect();
    if parts.is_empty() {
        return false;
    }

    let cmd = parts[0];

    // Check if all args are data for this command
    if SAFE_STRING_REGISTRY.is_all_args_data(cmd) {
        return true;
    }

    // Check if the preceding flag makes this argument data
    if let Some(flag) = preceding_flag {
        return SAFE_STRING_REGISTRY.is_flag_data(cmd, flag);
    }

    false
}

/// Check if the current segment ends with a pipe (indicating potential code execution).
fn is_piped_segment(command: &str, tokens: &[SanitizeToken], current_idx: usize) -> bool {
    for token in &tokens[current_idx..] {
        if token.kind == SanitizeTokenKind::Separator {
            let sep = &command[token.byte_range.clone()];
            // Matches "|" (pipe) or "|&" (pipe with stderr)
            // Does NOT match "||" (OR) or ";" (sequence)
            return sep == "|" || sep == "|&";
        }
    }
    false
}

#[derive(Clone, Copy)]
struct PendingSafeFlag<'a> {
    flag: &'a str,
    multi_value: bool,
}

/// Create a sanitized view of `command` for regex-based pattern matching.
///
/// This function replaces known-safe *string arguments* (commit messages, issue
/// descriptions, grep patterns, etc.) so dangerous substrings inside those
/// arguments don't trigger false-positive blocks.
///
/// The sanitizer is intentionally conservative:
/// - It only strips arguments in the explicit [`SAFE_STRING_REGISTRY`].
/// - It never strips any token that appears to contain shell-executed constructs
///   like `$(` or backticks (even if the flag/command is otherwise safe).
///
/// This is designed to be be used on the hot path, so it returns a borrowed view
/// when no sanitization is required.
#[must_use]
#[allow(clippy::too_many_lines)] // Single-pass masking logic; refactor only if it becomes unreadable
pub fn sanitize_for_pattern_matching(command: &str) -> Cow<'_, str> {
    // Quick-reject: if no safe-registry commands appear in the input AND no comments,
    // no masking is possible. Skip expensive tokenization entirely. This is a significant
    // optimization for heredocs and other large inputs that don't use these commands.
    // Note: We must still process commands with `#` to mask comments, even if no safe
    // commands are present (e.g., `python -c '...' # rm -rf /` should mask the comment).
    let has_comment_char = command.contains('#');
    if !SAFE_COMMANDS_MATCHER.is_match(command) && !has_comment_char {
        return Cow::Borrowed(command);
    }

    let tokens = tokenize_command(command);
    if tokens.is_empty() {
        return Cow::Borrowed(command);
    }

    let mut mask_ranges: SmallVec<[Range<usize>; 8]> = SmallVec::new();

    // Per-segment state (segments split on shell separators like |, ;, &&, ||).
    let mut segment_cmd: Option<&str> = None;
    let mut segment_cmd_is_all_args_data = false;
    let mut pending_safe_flag: Option<PendingSafeFlag<'_>> = None; // Safe flag waiting for value(s)
    let mut options_ended = false;
    let mut search_pattern_masked = false;
    let mut wrapper: WrapperState = WrapperState::None;
    let mut command_query_mode = false;
    let mut search_cmd_override: Option<&str> = None;
    let mut git_subcommand: Option<&str> = None;
    let mut git_waiting_for_value = false;
    let mut git_options_ended = false;

    for (i, token) in tokens.iter().enumerate() {
        if token.kind == SanitizeTokenKind::Separator {
            segment_cmd = None;
            segment_cmd_is_all_args_data = false;
            pending_safe_flag = None;
            options_ended = false;
            search_pattern_masked = false;
            wrapper = WrapperState::None;
            command_query_mode = false;
            search_cmd_override = None;
            git_subcommand = None;
            git_waiting_for_value = false;
            git_options_ended = false;
            continue;
        }

        if token.kind == SanitizeTokenKind::Comment {
            mask_ranges.push(token.byte_range.clone());
            continue;
        }

        let Some(token_text) = token.text(command) else {
            return Cow::Borrowed(command);
        };

        // Skip line continuations (backslash at end of line)
        if token_text == "\\\n" || token_text == "\\\r\n" {
            continue;
        }

        if command_query_mode {
            // `command -v/-V` is query-only; treat all remaining args as data.
            if !token.has_inline_code {
                mask_ranges.push(token.byte_range.clone());
            }
            continue;
        }

        if segment_cmd.is_none() {
            // Wrapper / prefix handling: allow stacked wrappers like `sudo env VAR=1 git ...`.
            if let Some(next_wrapper) = WrapperState::from_command_word(token_text) {
                wrapper = next_wrapper;
                continue;
            }
            if matches!(
                wrapper,
                WrapperState::Command {
                    options_ended: false,
                    ..
                }
            ) && command_option_is_query(token_text)
            {
                command_query_mode = true;
            }
            let (next_wrapper, skip) = wrapper.consume_token(token_text);
            wrapper = next_wrapper;
            if skip {
                continue;
            }
            if is_env_assignment(token_text) {
                continue;
            }

            segment_cmd = Some(token_text);
            segment_cmd_is_all_args_data = SAFE_STRING_REGISTRY.is_all_args_data(token_text);
            search_cmd_override = None;
            git_subcommand = None;
            git_waiting_for_value = false;
            git_options_ended = false;

            // If this command feeds into a pipe, its output is likely code (e.g. echo ... | sh).
            // Do NOT treat arguments as data in this case.
            if segment_cmd_is_all_args_data && is_piped_segment(command, &tokens, i) {
                segment_cmd_is_all_args_data = false;
            }

            pending_safe_flag = None;
            options_ended = false;
            search_pattern_masked = false;
            continue;
        }

        let Some(cmd) = segment_cmd else {
            // Should be unreachable because we set segment_cmd on the first word of each segment,
            // but fail open for safety.
            continue;
        };

        let mut is_git_subcommand_token = false;
        if cmd == "git" && git_subcommand.is_none() {
            if git_waiting_for_value {
                git_waiting_for_value = false;
            } else if token_text == "--" {
                git_options_ended = true;
            } else if !git_options_ended && token_text.starts_with('-') && token_text != "-" {
                let takes_value = matches!(
                    token_text,
                    "-C" | "-c"
                        | "--git-dir"
                        | "--work-tree"
                        | "--namespace"
                        | "--exec-path"
                        | "--pager"
                        | "--config-env"
                ) || token_text.starts_with("-C")
                    || token_text.starts_with("-c")
                    || token_text.starts_with("--git-dir=")
                    || token_text.starts_with("--work-tree=")
                    || token_text.starts_with("--namespace=")
                    || token_text.starts_with("--exec-path=")
                    || token_text.starts_with("--pager=")
                    || token_text.starts_with("--config-env=");
                if takes_value && !token_text.contains('=') {
                    git_waiting_for_value = true;
                }
            } else {
                git_subcommand = Some(token_text);
                if token_text == "grep" {
                    search_cmd_override = Some("grep");
                    is_git_subcommand_token = true;
                }
            }
        }

        if segment_cmd_is_all_args_data {
            // For commands like echo/printf, treat all args as data, but never strip inline code.
            if !token.has_inline_code {
                mask_ranges.push(token.byte_range.clone());
            }
            continue;
        }

        if let Some(pending) = pending_safe_flag {
            let is_flag_token = token_text.starts_with('-') && token_text != "-";
            if pending.multi_value {
                if token.has_inline_code || is_flag_token {
                    pending_safe_flag = None;
                } else {
                    if !token.has_inline_code {
                        mask_ranges.push(token.byte_range.clone());
                        if is_search_pattern_flag(cmd, pending.flag) {
                            search_pattern_masked = true;
                        }
                    }
                    pending_safe_flag = Some(pending);
                    continue;
                }
            } else {
                pending_safe_flag = None;
                if !token.has_inline_code {
                    mask_ranges.push(token.byte_range.clone());
                    if is_search_pattern_flag(cmd, pending.flag) {
                        search_pattern_masked = true;
                    }
                }
                continue;
            }
        }

        // Handle --flag=value (and similar) forms.
        if let Some((flag, value_range)) = split_flag_assignment(token_text, token.byte_range.start)
        {
            if SAFE_STRING_REGISTRY.is_flag_data(cmd, flag) && !token.has_inline_code {
                // Mask only the value portion (after '='). Keep the flag prefix for readability.
                mask_ranges.push(value_range);

                // For search tools, masking a flag-supplied pattern should prevent masking the
                // first positional argument as a pattern.
                if is_search_pattern_flag(cmd, flag) {
                    search_pattern_masked = true;
                }
            }
            continue;
        }

        // Handle attached short-flag values like `-e"pattern"` or `-m"commit message"`.
        // This also covers combined clusters where the data-flag appears before the value
        // (e.g., `git commit -am"msg"`).
        if let Some((flag, value_range)) =
            split_short_flag_attached_value(cmd, token_text, token.byte_range.start)
        {
            if !token.has_inline_code {
                mask_ranges.push(value_range);
                if is_search_pattern_flag(cmd, flag) {
                    search_pattern_masked = true;
                }
            }
            continue;
        }

        // Handle separate flag + value forms.
        if SAFE_STRING_REGISTRY.is_flag_data(cmd, token_text) {
            pending_safe_flag = Some(PendingSafeFlag {
                flag: token_text,
                multi_value: SAFE_STRING_REGISTRY.is_flag_data_multivalue(cmd, token_text),
            });
            continue;
        }
        if let Some(data_flag) = combined_short_data_flag_value(cmd, token_text) {
            pending_safe_flag = Some(PendingSafeFlag {
                flag: data_flag,
                multi_value: SAFE_STRING_REGISTRY.is_flag_data_multivalue(cmd, data_flag),
            });
            continue;
        }

        // Search tools: treat the first positional argument as pattern (when not already supplied
        // via -e/--regexp/etc).
        let search_cmd = search_cmd_override.unwrap_or(cmd);
        if is_search_command(search_cmd) {
            if is_git_subcommand_token {
                continue;
            }
            if token_text == "--" {
                options_ended = true;
                continue;
            }

            let is_option = !options_ended && token_text.starts_with('-') && token_text != "-";
            if is_option {
                continue;
            }

            if !search_pattern_masked && !token.has_inline_code {
                mask_ranges.push(token.byte_range.clone());
                search_pattern_masked = true;
            }
        }
    }

    if mask_ranges.is_empty() {
        return Cow::Borrowed(command);
    }

    // Merge overlapping ranges and apply masks.
    mask_ranges.sort_by_key(|r| r.start);
    let merged = merge_ranges(&mask_ranges);

    let bytes = command.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut last = 0;
    for range in merged {
        if range.start > last {
            out.extend_from_slice(&bytes[last..range.start]);
        }
        out.extend(std::iter::repeat_n(
            b' ',
            range.end.saturating_sub(range.start),
        ));
        last = range.end;
    }
    if last < bytes.len() {
        out.extend_from_slice(&bytes[last..]);
    }

    String::from_utf8(out).map_or(Cow::Borrowed(command), Cow::Owned)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WrapperState {
    None,
    Sudo {
        options_ended: bool,
        pending_value: bool,
    },
    Env {
        options_ended: bool,
        pending_value: bool,
    },
    Command {
        options_ended: bool,
        pending_value: bool,
    },
}

impl WrapperState {
    #[inline]
    #[must_use]
    fn from_command_word(word: &str) -> Option<Self> {
        let base_name = word.rsplit('/').next().unwrap_or(word);
        match base_name {
            "sudo" => Some(Self::Sudo {
                options_ended: false,
                pending_value: false,
            }),
            "env" => Some(Self::Env {
                options_ended: false,
                pending_value: false,
            }),
            "command" => Some(Self::Command {
                options_ended: false,
                pending_value: false,
            }),
            _ => None,
        }
    }

    #[inline]
    #[must_use]
    fn consume_token(self, token: &str) -> (Self, bool) {
        match self {
            Self::None => (Self::None, false),
            Self::Sudo {
                options_ended,
                pending_value,
            } => consume_wrapper_token(
                token,
                Self::Sudo {
                    options_ended,
                    pending_value,
                },
                sudo_option_takes_value,
            ),
            Self::Env {
                options_ended,
                pending_value,
            } => consume_wrapper_token(
                token,
                Self::Env {
                    options_ended,
                    pending_value,
                },
                env_option_takes_value,
            ),
            Self::Command {
                options_ended,
                pending_value,
            } => consume_wrapper_token(
                token,
                Self::Command {
                    options_ended,
                    pending_value,
                },
                |_t| None,
            ),
        }
    }
}

#[inline]
#[must_use]
fn consume_wrapper_token<F>(
    token: &str,
    state: WrapperState,
    takes_value: F,
) -> (WrapperState, bool)
where
    F: Fn(&str) -> Option<WrapperOptionValueMode>,
{
    let (options_ended, pending_value) = match state {
        WrapperState::Sudo {
            options_ended,
            pending_value,
        }
        | WrapperState::Env {
            options_ended,
            pending_value,
        }
        | WrapperState::Command {
            options_ended,
            pending_value,
        } => (options_ended, pending_value),
        WrapperState::None => return (WrapperState::None, false),
    };

    if pending_value {
        return (
            set_wrapper_pending(state, options_ended, false),
            true, // skip value token
        );
    }

    if options_ended {
        return (state, false);
    }

    if token == "--" {
        return (
            set_wrapper_options_ended(state, true),
            true, // skip `--`
        );
    }

    if !token.starts_with('-') {
        return (state, false);
    }

    // Skip wrapper options. Some wrapper options take a value; skip that too.
    let pending_value = match takes_value(token) {
        Some(WrapperOptionValueMode::SeparateToken) => true,
        Some(WrapperOptionValueMode::Attached) | None => false,
    };

    (
        set_wrapper_pending(state, options_ended, pending_value),
        true,
    )
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WrapperOptionValueMode {
    Attached,
    SeparateToken,
}

#[inline]
#[must_use]
const fn set_wrapper_options_ended(state: WrapperState, options_ended: bool) -> WrapperState {
    match state {
        WrapperState::Sudo { pending_value, .. } => WrapperState::Sudo {
            options_ended,
            pending_value,
        },
        WrapperState::Env { pending_value, .. } => WrapperState::Env {
            options_ended,
            pending_value,
        },
        WrapperState::Command { pending_value, .. } => WrapperState::Command {
            options_ended,
            pending_value,
        },
        WrapperState::None => WrapperState::None,
    }
}

#[inline]
#[must_use]
const fn set_wrapper_pending(
    state: WrapperState,
    options_ended: bool,
    pending_value: bool,
) -> WrapperState {
    match state {
        WrapperState::Sudo { .. } => WrapperState::Sudo {
            options_ended,
            pending_value,
        },
        WrapperState::Env { .. } => WrapperState::Env {
            options_ended,
            pending_value,
        },
        WrapperState::Command { .. } => WrapperState::Command {
            options_ended,
            pending_value,
        },
        WrapperState::None => WrapperState::None,
    }
}

#[inline]
#[must_use]
fn sudo_option_takes_value(token: &str) -> Option<WrapperOptionValueMode> {
    // Common sudo options that take an argument: -u user, -g group, -h host, -p prompt, -C num,
    // -r role, -D directory. These show up in automation and are important for correct wrapper stripping.
    const SHORT_VALUE_OPTS: &[&str] = &["-u", "-g", "-h", "-p", "-C", "-t", "-a", "-U", "-r", "-D"];
    const LONG_VALUE_OPTS: &[&str] = &[
        "--user", "--group", "--host", "--prompt", "--role", "--chdir",
    ];

    if token.starts_with("--") {
        for opt in LONG_VALUE_OPTS {
            if token == *opt {
                return Some(WrapperOptionValueMode::SeparateToken);
            }
            if token
                .strip_prefix(opt)
                .is_some_and(|rest| rest.starts_with('='))
            {
                return Some(WrapperOptionValueMode::Attached);
            }
        }
        return None;
    }

    for opt in SHORT_VALUE_OPTS {
        if token == *opt {
            return Some(WrapperOptionValueMode::SeparateToken);
        }
        if token.starts_with(opt) && token.len() > opt.len() {
            return Some(WrapperOptionValueMode::Attached);
        }
    }

    None
}

#[inline]
#[must_use]
fn env_option_takes_value(token: &str) -> Option<WrapperOptionValueMode> {
    // GNU env: -u NAME / --unset=NAME / -C DIR.
    const SHORT_VALUE_OPTS: &[&str] = &["-u", "-C"];
    const LONG_VALUE_OPTS: &[&str] = &["--unset", "--chdir"];

    if token.starts_with("--") {
        for opt in LONG_VALUE_OPTS {
            if token == *opt {
                return Some(WrapperOptionValueMode::SeparateToken);
            }
            if token
                .strip_prefix(opt)
                .is_some_and(|rest| rest.starts_with('='))
            {
                return Some(WrapperOptionValueMode::Attached);
            }
        }
        return None;
    }

    for opt in SHORT_VALUE_OPTS {
        if token == *opt {
            return Some(WrapperOptionValueMode::SeparateToken);
        }
        if token.starts_with(opt) && token.len() > opt.len() {
            return Some(WrapperOptionValueMode::Attached);
        }
    }

    None
}

#[inline]
#[must_use]
fn command_option_is_query(token: &str) -> bool {
    if !token.starts_with('-') || token == "--" || token.starts_with("--") || token.len() <= 1 {
        return false;
    }

    token[1..].bytes().any(|b| b == b'v' || b == b'V')
}

#[inline]
#[must_use]
fn is_env_assignment(token: &str) -> bool {
    // Rough heuristic for KEY=VALUE tokens used as env assignments.
    let Some((key, _value)) = token.split_once('=') else {
        return false;
    };
    !key.is_empty()
        && key.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_')
        && !token.starts_with('-')
}

#[inline]
#[must_use]
fn is_search_command(cmd: &str) -> bool {
    let base_name = cmd.rsplit('/').next().unwrap_or(cmd);
    matches!(base_name, "rg" | "grep" | "ag" | "ack")
}

#[inline]
#[must_use]
fn is_search_pattern_flag(cmd: &str, flag: &str) -> bool {
    let base_name = cmd.rsplit('/').next().unwrap_or(cmd);
    match base_name {
        "rg" => matches!(flag, "-e" | "--regexp"),
        "grep" => matches!(flag, "-e" | "--regexp"),
        "ag" => matches!(flag, "-e" | "--pattern"),
        "ack" => matches!(flag, "-e" | "--pattern"),
        _ => false,
    }
}

#[must_use]
fn split_flag_assignment(token: &str, token_start: usize) -> Option<(&str, Range<usize>)> {
    // Only consider tokens that start like a flag.
    if !token.starts_with('-') {
        return None;
    }

    let (flag, value) = token.split_once('=')?;
    if value.is_empty() {
        return None;
    }

    // Compute the byte range for the value part within the original command.
    // `split_once` is by bytes, so this is safe.
    let eq_offset = flag.len();
    let value_start = token_start + eq_offset + 1;
    let value_end = token_start + token.len();
    if value_start >= value_end {
        return None;
    }

    Some((flag, value_start..value_end))
}

#[must_use]
fn split_short_flag_attached_value(
    cmd: &str,
    token: &str,
    token_start: usize,
) -> Option<(&'static str, Range<usize>)> {
    if !token.starts_with('-') || token.starts_with("--") || token.len() <= 2 || token.contains('=')
    {
        return None;
    }

    let base_name = cmd.rsplit('/').next().unwrap_or(cmd);
    let bytes = token.as_bytes();
    let flags = bytes.get(1..)?;

    for (offset, b) in flags.iter().enumerate() {
        let token_index = 1 + offset;
        let next_index = token_index + 1;
        if next_index >= bytes.len() {
            continue;
        }

        let Some(short_flag) = SAFE_STRING_REGISTRY
            .flag_data_pairs
            .iter()
            .filter(|entry| entry.command == base_name)
            .filter_map(|entry| entry.short_flag)
            .find(|short| short.as_bytes().get(1) == Some(b))
        else {
            continue;
        };

        let value_start = token_start + next_index;
        let value_end = token_start + token.len();
        if value_start >= value_end {
            continue;
        }

        return Some((short_flag, value_start..value_end));
    }

    None
}

#[must_use]
fn combined_short_data_flag_value(cmd: &str, token: &str) -> Option<&'static str> {
    // Handle combined short flags like `git commit -am "msg"` where `-m` consumes the next token.
    if !token.starts_with('-') || token.starts_with("--") || token.len() <= 2 || token.contains('=')
    {
        return None;
    }

    let base_name = cmd.rsplit('/').next().unwrap_or(cmd);
    let flags = token.as_bytes().get(1..)?;
    let last = flags.last()?;

    SAFE_STRING_REGISTRY
        .flag_data_pairs
        .iter()
        .filter(|entry| entry.command == base_name)
        .filter_map(|entry| entry.short_flag)
        .find(|short| short.as_bytes().get(1) == Some(last))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SanitizeTokenKind {
    Word,
    Separator,
    Comment,
}

#[derive(Debug, Clone)]
struct SanitizeToken {
    kind: SanitizeTokenKind,
    byte_range: Range<usize>,
    has_inline_code: bool,
}

impl SanitizeToken {
    #[inline]
    #[must_use]
    fn text<'a>(&self, command: &'a str) -> Option<&'a str> {
        command.get(self.byte_range.clone())
    }
}

type SanitizeTokens = SmallVec<[SanitizeToken; 16]>;

fn tokenize_command(command: &str) -> SanitizeTokens {
    let bytes = command.as_bytes();
    let len = bytes.len();

    let mut tokens = SanitizeTokens::new();
    let mut i = 0;

    while i < len {
        // Skip whitespace, but STOP at newline (it's a separator)
        while i < len && bytes[i].is_ascii_whitespace() && bytes[i] != b'\n' {
            i += 1;
        }
        if i >= len {
            break;
        }

        // Newline is a separator
        if bytes[i] == b'\n' {
            tokens.push(SanitizeToken {
                kind: SanitizeTokenKind::Separator,
                byte_range: i..i + 1,
                has_inline_code: false,
            });
            i += 1;
            continue;
        }

        if let Some(end) = consume_separator_token(bytes, i, len, &mut tokens) {
            i = end;
            continue;
        }

        // Check for comment start
        if i < len && bytes[i] == b'#' {
            // Consume until newline
            let start = i;
            while i < len && bytes[i] != b'\n' {
                i += 1;
            }
            tokens.push(SanitizeToken {
                kind: SanitizeTokenKind::Comment,
                byte_range: start..i,
                has_inline_code: false,
            });
            continue;
        }

        let start = i;
        let (end, has_inline_code) = consume_word_token(command, bytes, i, len);
        i = end;

        if start < i {
            tokens.push(SanitizeToken {
                kind: SanitizeTokenKind::Word,
                byte_range: start..i,
                has_inline_code,
            });
        }
    }

    tokens
}

#[inline]
fn consume_separator_token(
    bytes: &[u8],
    i: usize,
    len: usize,
    tokens: &mut SanitizeTokens,
) -> Option<usize> {
    match bytes[i] {
        b'|' => {
            let end = if i + 1 < len && bytes[i + 1] == b'|' {
                i + 2
            } else {
                i + 1
            };
            tokens.push(SanitizeToken {
                kind: SanitizeTokenKind::Separator,
                byte_range: i..end,
                has_inline_code: false,
            });
            Some(end)
        }
        b';' => {
            tokens.push(SanitizeToken {
                kind: SanitizeTokenKind::Separator,
                byte_range: i..i + 1,
                has_inline_code: false,
            });
            Some(i + 1)
        }
        b'&' => {
            let end = if i + 1 < len && bytes[i + 1] == b'&' {
                i + 2
            } else {
                i + 1
            };
            tokens.push(SanitizeToken {
                kind: SanitizeTokenKind::Separator,
                byte_range: i..end,
                has_inline_code: false,
            });
            Some(end)
        }
        _ => None,
    }
}

#[must_use]
fn consume_word_token(command: &str, bytes: &[u8], mut i: usize, len: usize) -> (usize, bool) {
    let mut has_inline_code = false;

    while i < len {
        let b = bytes[i];

        if b.is_ascii_whitespace() {
            break;
        }

        if matches!(b, b'|' | b';' | b'&') {
            break;
        }

        match b {
            b'\\' => {
                // Handle CRLF escape (consumes 3 bytes: \, \r, \n)
                if i + 2 < len && bytes[i + 1] == b'\r' && bytes[i + 2] == b'\n' {
                    i += 3;
                } else {
                    // Skip escaped byte. This is conservative for UTF-8: if the escape
                    // is used with a multibyte char, this may desync, but we fail open
                    // (no masking) if slicing becomes invalid.
                    i = (i + 2).min(len);
                }
            }
            b'\'' => {
                // Single-quoted segment (no escapes)
                i += 1;
                while i < len && bytes[i] != b'\'' {
                    i += 1;
                }
                if i < len {
                    i += 1; // consume closing quote
                }
            }
            b'"' => {
                // Double-quoted segment (escapes + substitution)
                i += 1;
                while i < len {
                    match bytes[i] {
                        b'"' => {
                            i += 1;
                            break;
                        }
                        b'\\' => {
                            i = (i + 2).min(len);
                        }
                        b'$' if i + 1 < len && bytes[i + 1] == b'(' => {
                            has_inline_code = true;
                            i += 1; // Skip past (
                            // Note: we don't track nesting inside double quotes for simplicity
                            // This is conservative (treats more as potentially dangerous)
                        }
                        b'`' => {
                            has_inline_code = true;
                            i = consume_backticks(command, i);
                        }
                        _ => {
                            i += 1;
                        }
                    }
                }
            }
            b'$' if i + 1 < len && bytes[i + 1] == b'(' => {
                has_inline_code = true;
                i = consume_dollar_paren(command, i);
            }
            b'`' => {
                has_inline_code = true;
                i = consume_backticks(command, i);
            }
            _ => {
                i += 1;
            }
        }
    }

    (i, has_inline_code)
}

#[must_use]
fn consume_dollar_paren(command: &str, start: usize) -> usize {
    consume_dollar_paren_recursive(command, start, 0)
}

fn consume_dollar_paren_recursive(command: &str, start: usize, recursion_depth: usize) -> usize {
    // Prevent stack overflow on pathological input
    if recursion_depth > 500 {
        return command.len(); // Fail safe: consume rest of command to ensure masking
    }

    let bytes = command.as_bytes();
    let len = bytes.len();

    debug_assert!(bytes.get(start) == Some(&b'$'));
    debug_assert!(bytes.get(start + 1) == Some(&b'('));

    let mut i = start + 2;
    let mut depth: u32 = 1;

    while i < len {
        match bytes[i] {
            b'(' => {
                depth += 1;
                i += 1;
            }
            b')' => {
                if depth == 1 {
                    // End of command substitution
                    return i + 1;
                }
                depth = depth.saturating_sub(1);
                i += 1;
            }
            b'\\' => {
                i = (i + 2).min(len);
            }
            b'\'' => {
                // Single quotes inside: consume until closing
                i += 1;
                while i < len && bytes[i] != b'\'' {
                    i += 1;
                }
                if i < len {
                    i += 1;
                }
            }
            b'"' => {
                // Double quotes inside: consume until closing, respecting escapes
                i += 1;
                while i < len {
                    match bytes[i] {
                        b'"' => {
                            i += 1;
                            break;
                        }
                        b'\\' => {
                            i = (i + 2).min(len);
                        }
                        b'$' if i + 1 < len && bytes[i + 1] == b'(' => {
                            // Recursively consume nested command substitution inside double quotes
                            // to ensure we don't treat its contents (like closing quotes) as ours.
                            i = consume_dollar_paren_recursive(command, i, recursion_depth + 1);
                        }
                        _ => {
                            i += 1;
                        }
                    }
                }
            }
            _ => {
                i += 1;
            }
        }
    }

    len
}

#[must_use]
fn consume_backticks(command: &str, start: usize) -> usize {
    let bytes = command.as_bytes();
    let len = bytes.len();

    debug_assert!(bytes.get(start) == Some(&b'`'));

    let mut i = start + 1;
    while i < len {
        match bytes[i] {
            b'\\' => {
                i = (i + 2).min(len);
            }
            b'`' => {
                i += 1;
                break;
            }
            _ => {
                i += 1;
            }
        }
    }
    i
}

#[must_use]
fn env_split_string_context(command: &str, flag_start: usize) -> bool {
    let segment_start = segment_start_before_flag(command, flag_start);
    let segment = &command[segment_start..flag_start];

    segment.split_whitespace().any(|token| {
        let token = token.trim_start_matches('\\');
        token == "env" || token.ends_with("/env")
    })
}

#[inline]
#[must_use]
fn is_inline_code_flag(word: &str) -> bool {
    if word == "-S" {
        return true;
    }
    if !word.starts_with('-') || word.starts_with("--") || word.len() < 2 {
        return false;
    }

    word.as_bytes()
        .iter()
        .skip(1)
        .any(|b| matches!(b.to_ascii_lowercase(), b'c' | b'e' | b'r'))
}

#[must_use]
fn segment_start_before_flag(command: &str, flag_start: usize) -> usize {
    let bytes = command.as_bytes();
    let mut i = flag_start.min(bytes.len());

    while i > 0 {
        i -= 1;
        match bytes[i] {
            b'|' => {
                if i > 0 && bytes[i - 1] == b'|' {
                    return i + 1;
                }
                return i + 1;
            }
            b'&' => {
                if i > 0 && bytes[i - 1] == b'&' {
                    return i + 1;
                }
                return i + 1;
            }
            b';' => return i + 1,
            _ => {}
        }
    }

    0
}

#[must_use]
fn merge_ranges(ranges: &[Range<usize>]) -> SmallVec<[Range<usize>; 8]> {
    let mut merged: SmallVec<[Range<usize>; 8]> = SmallVec::new();
    for range in ranges {
        if let Some(last) = merged.last_mut() {
            if range.start <= last.end {
                last.end = last.end.max(range.end);
                continue;
            }
        }
        merged.push(range.clone());
    }
    merged
}

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

    #[test]
    fn test_simple_command() {
        let spans = classify_command("git status");
        assert_eq!(spans.spans().len(), 1);
        assert_eq!(spans.spans()[0].kind, SpanKind::Executed);
    }

    #[test]
    fn test_single_quoted_string() {
        let cmd = "git commit -m 'Fix rm -rf detection'";
        let spans = classify_command(cmd);

        // Should have: "git commit -m " (Executed), "'Fix rm -rf detection'" (Data)
        assert!(spans.spans().len() >= 2);

        // Find the single-quoted span
        let data_span = spans.spans().iter().find(|s| s.kind == SpanKind::Data);
        assert!(data_span.is_some());
        let data_span = data_span.unwrap();
        assert_eq!(data_span.text(cmd), "'Fix rm -rf detection'");
    }

    #[test]
    fn test_double_quoted_string() {
        let cmd = "echo \"hello world\"";
        let spans = classify_command(cmd);

        // Find the double-quoted span
        let arg_span = spans.spans().iter().find(|s| s.kind == SpanKind::Argument);
        assert!(arg_span.is_some());
        assert_eq!(arg_span.unwrap().text(cmd), "\"hello world\"");
    }

    #[test]
    fn test_unclosed_single_quote_is_unknown() {
        let cmd = "echo 'rm -rf /";
        let spans = classify_command(cmd);

        let last_span = spans.spans().last().expect("last span");
        assert_eq!(last_span.kind, SpanKind::Unknown);
        assert!(last_span.text(cmd).contains("rm -rf"));
    }

    #[test]
    fn test_comment_at_eof_is_comment_span() {
        let cmd = "echo safe # rm -rf /";
        let spans = classify_command(cmd);

        let comment_span = spans.spans().iter().find(|s| s.kind == SpanKind::Comment);
        assert!(comment_span.is_some());
        assert_eq!(comment_span.unwrap().text(cmd), "# rm -rf /");
    }

    #[test]
    fn test_comment_after_separator_is_comment_span() {
        let cmd = "echo safe;# rm -rf /";
        let spans = classify_command(cmd);

        let comment_span = spans.spans().iter().find(|s| s.kind == SpanKind::Comment);
        assert!(comment_span.is_some());
        assert_eq!(comment_span.unwrap().text(cmd), "# rm -rf /");
    }

    #[test]
    fn test_command_substitution() {
        let cmd = "echo $(rm -rf /)";
        let spans = classify_command(cmd);

        // Should have InlineCode span for $(rm -rf /)
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(inline_span.is_some());
        assert_eq!(inline_span.unwrap().text(cmd), "$(rm -rf /)");
    }

    #[test]
    fn test_backtick_substitution() {
        let cmd = "echo `rm -rf /`";
        let spans = classify_command(cmd);

        // Should have InlineCode span for `rm -rf /`
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(inline_span.is_some());
        assert_eq!(inline_span.unwrap().text(cmd), "`rm -rf /`");
    }

    #[test]
    fn test_env_split_string_marks_inline_code() {
        let cmd = "env FOO=1 -S \"rm -rf /\"";
        let spans = classify_command(cmd);

        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(inline_span.is_some());
        assert_eq!(inline_span.unwrap().text(cmd), "\"rm -rf /\"");
    }

    #[test]
    fn test_pipe() {
        let cmd = "echo hi | cat";
        let spans = classify_command(cmd);

        // Should have spans for: "echo hi ", "|", " cat"
        assert!(spans.spans().len() >= 2);

        // All parts around pipe should be Executed
        for span in spans.executable_spans() {
            assert!(span.kind.is_executable());
        }
    }

    #[test]
    fn test_semicolon_separator() {
        let cmd = "echo a; echo b";
        let spans = classify_command(cmd);

        // Both parts should be Executed
        assert!(spans.has_executable_content());
    }

    #[test]
    fn test_and_separator() {
        let cmd = "true && rm -rf /";
        let spans = classify_command(cmd);

        // Both parts should be Executed
        let executable_text: Vec<_> = spans.executable_text(cmd);
        assert!(!executable_text.is_empty());
    }

    #[test]
    fn test_bash_c_inline_code() {
        let cmd = "bash -c \"rm -rf /\"";
        let spans = classify_command(cmd);

        // The quoted part after -c should be InlineCode
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after bash -c"
        );
    }

    #[test]
    fn test_bash_c_single_quote_inline_code() {
        let cmd = "bash -c 'rm -rf /'";
        let spans = classify_command(cmd);

        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after bash -c with single quotes"
        );
    }

    #[test]
    fn test_bash_c_attached_single_quote_inline_code() {
        let cmd = "bash -c'rm -rf /'";
        let spans = classify_command(cmd);

        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after bash -c without space"
        );
    }

    #[test]
    fn test_bash_lc_inline_code() {
        let cmd = "bash -lc \"rm -rf /\"";
        let spans = classify_command(cmd);

        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after bash -lc"
        );
    }

    #[test]
    fn test_bash_lc_attached_single_quote_inline_code() {
        let cmd = "bash -lc'echo rm -rf /'";
        let spans = classify_command(cmd);

        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after bash -lc without space"
        );
    }

    #[test]
    fn test_python_c_inline_code() {
        let cmd = "python -c \"import os; os.system('rm -rf /')\"";
        let spans = classify_command(cmd);

        // The quoted part after -c should be InlineCode
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after python -c"
        );
    }

    #[test]
    fn test_node_e_inline_code() {
        let cmd = "node -e \"require('child_process').execSync('rm -rf /')\"";
        let spans = classify_command(cmd);

        // The quoted part after -e should be InlineCode
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after node -e"
        );
    }

    #[test]
    fn test_double_quote_with_substitution() {
        let cmd = "echo \"$(rm -rf /)\"";
        let spans = classify_command(cmd);

        // Should be Unknown or InlineCode (not safe Argument)
        for span in spans.spans() {
            if span.text(cmd).contains("$(") {
                assert!(
                    span.kind == SpanKind::Unknown || span.kind == SpanKind::InlineCode,
                    "Double-quoted string with substitution should not be safe Argument"
                );
            }
        }
    }

    #[test]
    fn test_escaped_quote() {
        let cmd = "echo \"hello \\\"world\\\"\"";
        let spans = classify_command(cmd);

        // Should handle escaped quotes correctly
        assert!(!spans.spans().is_empty());
    }

    #[test]
    fn test_false_positive_case_bd_create() {
        // This should NOT trigger destructive pattern matching
        let cmd = "bd create --description=\"This pattern blocks rm -rf\"";
        let spans = classify_command(cmd);

        // The quoted description should be Argument (not requiring pattern check)
        let desc_span = spans
            .spans()
            .iter()
            .find(|s| s.text(cmd).contains("rm -rf"));
        if let Some(span) = desc_span {
            assert!(
                !span.kind.requires_pattern_check() || span.kind == SpanKind::Argument,
                "Description argument should not require pattern check"
            );
        }
    }

    #[test]
    fn test_false_positive_case_git_commit() {
        // This should NOT trigger destructive pattern matching
        let cmd = "git commit -m \"Fix git reset --hard detection\"";
        let spans = classify_command(cmd);

        // The quoted message should be Argument
        let msg_span = spans
            .spans()
            .iter()
            .find(|s| s.text(cmd).contains("reset --hard"));
        if let Some(span) = msg_span {
            assert_eq!(span.kind, SpanKind::Argument);
        }
    }

    #[test]
    fn test_false_positive_case_rg_pattern() {
        // This should NOT trigger destructive pattern matching
        let cmd = "rg -n \"rm -rf\" src/main.rs";
        let spans = classify_command(cmd);

        // The quoted pattern should be Argument
        let pattern_span = spans.spans().iter().find(|s| s.text(cmd) == "\"rm -rf\"");
        if let Some(span) = pattern_span {
            assert_eq!(span.kind, SpanKind::Argument);
        }
    }

    #[test]
    fn test_span_kind_requires_pattern_check() {
        assert!(SpanKind::Executed.requires_pattern_check());
        assert!(SpanKind::InlineCode.requires_pattern_check());
        assert!(SpanKind::HeredocBody.requires_pattern_check());
        assert!(SpanKind::Unknown.requires_pattern_check());
        assert!(!SpanKind::Data.requires_pattern_check());
        assert!(!SpanKind::Argument.requires_pattern_check());
    }

    #[test]
    fn test_span_kind_is_safe_data() {
        assert!(SpanKind::Data.is_safe_data());
        assert!(!SpanKind::Argument.is_safe_data());
        assert!(!SpanKind::Executed.is_safe_data());
    }

    #[test]
    fn test_empty_command() {
        let spans = classify_command("");
        assert!(spans.spans().is_empty());
    }

    #[test]
    fn test_whitespace_only() {
        let spans = classify_command("   ");
        // Should have one Executed span (conservative)
        assert!(!spans.spans().is_empty());
    }

    #[test]
    fn test_nested_command_substitution() {
        let cmd = "echo $(echo $(rm -rf /))";
        let spans = classify_command(cmd);

        // Should detect the outer command substitution as InlineCode
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(inline_span.is_some());
    }

    #[test]
    fn test_nested_command_substitution_parens() {
        // Test nested parentheses inside command substitution
        let cmd = "echo $( ( echo inner ) )";
        let spans = classify_command(cmd);

        // Should include the nested parens in the InlineCode span
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(inline_span.is_some());
        assert_eq!(inline_span.unwrap().text(cmd), "$( ( echo inner ) )");
    }

    #[test]
    fn test_command_substitution_with_comment() {
        // Test that parentheses inside comments are ignored
        let cmd = "echo $(echo # ) \n)";
        let spans = classify_command(cmd);

        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(inline_span.is_some());
        assert_eq!(inline_span.unwrap().text(cmd), "$(echo # ) \n)");
    }

    #[test]
    fn test_single_quote_preserves_special_chars() {
        let cmd = "echo '$HOME'";
        let spans = classify_command(cmd);

        // '$HOME' should be Data (single quotes prevent expansion)
        let data_span = spans.spans().iter().find(|s| s.kind == SpanKind::Data);
        assert!(data_span.is_some());
        assert_eq!(data_span.unwrap().text(cmd), "'$HOME'");
    }

    #[test]
    fn test_mixed_quotes() {
        let cmd = "echo 'single' \"double\" plain";
        let spans = classify_command(cmd);

        // Should have at least 3 distinct spans
        let data_count = spans
            .spans()
            .iter()
            .filter(|s| s.kind == SpanKind::Data)
            .count();
        let arg_count = spans
            .spans()
            .iter()
            .filter(|s| s.kind == SpanKind::Argument)
            .count();

        assert!(data_count >= 1, "Should have single-quoted Data span");
        assert!(arg_count >= 1, "Should have double-quoted Argument span");
    }

    #[test]
    fn test_or_operator() {
        let cmd = "false || rm -rf /";
        let spans = classify_command(cmd);

        // Both parts should be executable
        assert!(spans.has_executable_content());
    }

    #[test]
    fn test_path_prefixed_command() {
        let cmd = "/usr/bin/bash -c \"rm -rf /\"";
        let spans = classify_command(cmd);

        // Should still detect inline code after path-prefixed bash
        let inline_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(
            inline_span.is_some(),
            "Should detect inline code after /usr/bin/bash -c"
        );
    }

    #[test]
    fn test_performance_typical_commands() {
        use std::time::Instant;

        // Test a variety of typical commands
        let commands = [
            "git status",
            "git commit -m 'Fix bug in parser'",
            "echo \"hello world\" | cat",
            "ls -la /tmp",
            "cargo test --release",
            "python -c \"print('hello')\"",
            "bash -c \"echo test && echo done\"",
            "docker ps --all --format '{{.Names}}'",
        ];

        // Warm up
        for cmd in &commands {
            let _ = classify_command(cmd);
        }

        // Time 1000 iterations
        let iterations = 1000;
        let start = Instant::now();
        for _ in 0..iterations {
            for cmd in &commands {
                let _ = classify_command(cmd);
            }
        }
        let elapsed = start.elapsed();

        // Calculate average per command
        let total_commands = iterations * commands.len();
        let avg_nanoseconds = elapsed.as_nanos() / total_commands as u128;
        #[allow(clippy::cast_precision_loss)]
        let avg_microseconds = avg_nanoseconds as f64 / 1000.0;

        // Assert performance is under 100μs per command
        assert!(
            avg_microseconds < 100.0,
            "Average classification time {avg_microseconds:.2}μs exceeds 100μs budget"
        );

        // Print for visibility in test output
        eprintln!(
            "Context classification performance: {avg_microseconds:.2}μs/command ({} commands, {} iterations)",
            commands.len(),
            iterations
        );
    }

    // =========================================================================
    // Safe String Registry Tests (git_safety_guard-t8x.1)
    // =========================================================================

    #[test]
    fn test_registry_echo_is_all_data() {
        // echo command - all args are data, never executed
        assert!(SAFE_STRING_REGISTRY.is_all_args_data("echo"));
        assert!(SAFE_STRING_REGISTRY.is_all_args_data("echo"));
        assert!(SAFE_STRING_REGISTRY.is_all_args_data("/bin/echo"));
        assert!(SAFE_STRING_REGISTRY.is_all_args_data("/usr/bin/echo"));
    }

    #[test]
    fn test_registry_printf_is_all_data() {
        // printf command - all args are data, never executed
        assert!(SAFE_STRING_REGISTRY.is_all_args_data("printf"));
        assert!(SAFE_STRING_REGISTRY.is_all_args_data("/usr/bin/printf"));
    }

    #[test]
    fn test_registry_bash_is_not_all_data() {
        // bash is NOT all-data - it executes code!
        assert!(!SAFE_STRING_REGISTRY.is_all_args_data("bash"));
        assert!(!SAFE_STRING_REGISTRY.is_all_args_data("sh"));
        assert!(!SAFE_STRING_REGISTRY.is_all_args_data("python"));
    }

    #[test]
    fn test_registry_git_message_flags() {
        // git -m and --message are data (commit messages)
        assert!(SAFE_STRING_REGISTRY.is_flag_data("git", "-m"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("git", "--message"));
        // But other git flags are NOT data
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("git", "-c"));
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("git", "--exec"));
    }

    #[test]
    fn test_registry_bd_description_flags() {
        // bd --description, --title, --notes, --reason are data
        assert!(SAFE_STRING_REGISTRY.is_flag_data("bd", "--description"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("bd", "--title"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("bd", "--notes"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("bd", "--reason"));
    }

    #[test]
    fn test_registry_bd_multivalue_flags() {
        assert!(SAFE_STRING_REGISTRY.is_flag_data_multivalue("bd", "--notes"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data_multivalue("bd", "--description"));
        assert!(!SAFE_STRING_REGISTRY.is_flag_data_multivalue("git", "-m"));
        assert!(!SAFE_STRING_REGISTRY.is_flag_data_multivalue("grep", "-e"));
    }

    #[test]
    fn test_registry_grep_pattern_flags() {
        // grep -e/--regexp take pattern arguments (data, not code)
        assert!(SAFE_STRING_REGISTRY.is_flag_data("grep", "-e"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("grep", "--regexp"));
        // grep -F/--fixed-strings do NOT take pattern arguments (pattern remains positional)
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("grep", "-F"));
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("grep", "--fixed-strings"));
    }

    #[test]
    fn test_registry_gh_cli_flags() {
        // gh -t, -b, -m and their long forms are data
        assert!(SAFE_STRING_REGISTRY.is_flag_data("gh", "-t"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("gh", "--title"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("gh", "-b"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("gh", "--body"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("gh", "-m"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("gh", "--message"));
    }

    #[test]
    fn test_registry_data_flags_for_git() {
        let flags = SAFE_STRING_REGISTRY.data_flags_for_command("git");
        assert!(flags.contains(&"-m"));
        assert!(flags.contains(&"--message"));
    }

    #[test]
    fn test_registry_data_flags_for_grep() {
        let flags = SAFE_STRING_REGISTRY.data_flags_for_command("grep");
        assert!(flags.contains(&"-e"));
        assert!(flags.contains(&"--regexp"));
        assert!(!flags.contains(&"-F"));
        assert!(!flags.contains(&"--fixed-strings"));
    }

    #[test]
    fn test_is_argument_data_echo() {
        // echo "rm -rf /" - the argument is data
        assert!(is_argument_data("echo \"rm -rf /\"", None));
    }

    #[test]
    fn test_is_argument_data_git_commit_message() {
        // git commit -m "..." - the -m argument is data
        assert!(is_argument_data("git commit -m \"Fix rm -rf\"", Some("-m")));
    }

    #[test]
    fn test_is_argument_data_rg_pattern() {
        // rg -e "rm -rf" - the -e argument is data
        assert!(is_argument_data("rg -e \"rm -rf\" src/", Some("-e")));
    }

    #[test]
    fn test_is_argument_data_bash_c_is_not_data() {
        // bash -c "rm -rf /" - the -c argument is CODE, not data!
        assert!(!is_argument_data("bash -c \"rm -rf /\"", Some("-c")));
    }

    #[test]
    fn test_counterexample_bash_executes() {
        // Counterexample: bash -c MUST still be treated as code
        // This test ensures we don't accidentally suppress bash -c
        assert!(!SAFE_STRING_REGISTRY.is_all_args_data("bash"));
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("bash", "-c"));
    }

    #[test]
    fn test_counterexample_python_executes() {
        // Counterexample: python -c MUST still be treated as code
        assert!(!SAFE_STRING_REGISTRY.is_all_args_data("python"));
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("python", "-c"));
    }

    #[test]
    fn test_counterexample_xargs_executes() {
        // Counterexample: xargs can execute commands
        assert!(!SAFE_STRING_REGISTRY.is_all_args_data("xargs"));
    }

    #[test]
    fn test_false_positive_git_commit_message() {
        // This should NOT trigger pattern matching on commit message
        let cmd = "git commit -m \"Fix git reset --hard detection\"";

        // Using the registry, we know -m flag args are data
        assert!(SAFE_STRING_REGISTRY.is_flag_data("git", "-m"));

        // Combined with context classification, the quoted part is Argument
        let spans = classify_command(cmd);
        let msg_span = spans
            .spans()
            .iter()
            .find(|s| s.text(cmd).contains("reset --hard"));
        assert!(msg_span.is_some());
        // The message should be classified as Argument (not requiring pattern check)
        assert_eq!(msg_span.unwrap().kind, SpanKind::Argument);
    }

    #[test]
    fn test_false_positive_rg_pattern() {
        // This should NOT trigger pattern matching on rg pattern
        let cmd = "rg -e \"rm -rf\" src/";

        // Using the registry, we know -e flag args are data
        assert!(SAFE_STRING_REGISTRY.is_flag_data("rg", "-e"));

        // The quoted pattern should be classified as Argument
        let spans = classify_command(cmd);
        let pattern_span = spans.spans().iter().find(|s| s.text(cmd) == "\"rm -rf\"");
        assert!(pattern_span.is_some());
        assert_eq!(pattern_span.unwrap().kind, SpanKind::Argument);
    }

    #[test]
    fn test_false_positive_bd_create() {
        // This should NOT trigger pattern matching on bd description
        let cmd = "bd create --description=\"This pattern blocks rm -rf\"";

        // Using the registry
        assert!(SAFE_STRING_REGISTRY.is_flag_data("bd", "--description"));

        // The description should be classified as Argument
        let spans = classify_command(cmd);
        let desc_span = spans
            .spans()
            .iter()
            .find(|s| s.text(cmd).contains("rm -rf"));
        assert!(desc_span.is_some());
        // Note: current classification treats this as Argument (attached to flag)
        assert_eq!(desc_span.unwrap().kind, SpanKind::Argument);
    }

    #[test]
    fn test_true_positive_bash_c() {
        // This MUST trigger pattern matching - bash -c is CODE!
        let cmd = "bash -c \"rm -rf /\"";

        // bash -c is NOT in the data registry
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("bash", "-c"));

        // The classifier should detect this as InlineCode
        let spans = classify_command(cmd);
        let code_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(code_span.is_some(), "bash -c content must be InlineCode");
    }

    #[test]
    fn test_true_positive_python_c() {
        // This MUST trigger pattern matching - python -c is CODE!
        let cmd = "python -c \"import os; os.system('rm -rf /')\"";

        // python -c is NOT in the data registry
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("python", "-c"));

        // The classifier should detect this as InlineCode
        let spans = classify_command(cmd);
        let code_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);
        assert!(code_span.is_some(), "python -c content must be InlineCode");
    }

    #[test]
    fn sanitize_strips_bd_description_value() {
        let cmd = r#"bd create --description="This pattern blocks rm -rf""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("bd create"));
        assert!(sanitized.as_ref().contains("--description="));
    }

    #[test]
    fn sanitize_strips_bd_notes_unquoted_multiword() {
        let cmd = "bd create --notes This references git reset hard";
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("git reset"));
        assert!(sanitized.as_ref().contains("bd create --notes"));
    }

    #[test]
    fn sanitize_stops_multivalue_on_next_flag() {
        let cmd = "bd create --notes This blocks rm rf --priority 2";
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm rf"));
        assert!(sanitized.as_ref().contains("--priority 2"));
    }

    #[test]
    fn sanitize_multivalue_keeps_inline_code_visible() {
        let cmd = "bd create --notes $(rm -rf /) and more";
        let sanitized = sanitize_for_pattern_matching(cmd);

        // Inline code must remain visible; no masking should occur here.
        assert!(matches!(sanitized, std::borrow::Cow::Borrowed(_)));
        assert!(sanitized.as_ref().contains("rm -rf"));
    }

    #[test]
    fn sanitize_does_not_strip_when_inline_code_present() {
        let cmd = r#"bd create --description="$(rm -rf /)""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        // Inline code must remain visible to the pattern matcher.
        assert!(matches!(sanitized, std::borrow::Cow::Borrowed(_)));
        assert!(sanitized.as_ref().contains("rm -rf"));
    }

    #[test]
    fn sanitize_strips_rg_positional_pattern() {
        let cmd = r#"rg -n "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("rg -n"));
    }

    #[test]
    fn sanitize_strips_git_grep_positional_pattern() {
        let cmd = r#"git grep "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("git grep"));
    }

    #[test]
    fn sanitize_handles_git_grep_with_global_options() {
        let cmd = r#"git -C /tmp -c color.ui=auto grep -e "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(
            sanitized
                .as_ref()
                .contains("git -C /tmp -c color.ui=auto grep -e")
        );
        assert!(sanitized.as_ref().contains("src/main.rs"));
    }

    #[test]
    fn sanitize_strips_ag_positional_pattern() {
        let cmd = r#"ag "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("ag"));
    }

    #[test]
    fn sanitize_strips_ack_positional_pattern() {
        let cmd = r#"ack "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("ack"));
    }

    #[test]
    fn sanitize_handles_rg_fixed_strings_flag_with_other_options() {
        let cmd = r#"rg --fixed-strings -n "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("rg --fixed-strings -n"));
    }

    #[test]
    fn sanitize_handles_grep_fixed_strings_flag_with_other_options() {
        let cmd = r#"grep -F -n "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("grep -F -n"));
    }

    #[test]
    fn sanitize_handles_attached_search_pattern_value_rg() {
        let cmd = r#"rg -e"rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("rg -e"));
    }

    #[test]
    fn sanitize_handles_attached_search_pattern_value_grep() {
        let cmd = r#"grep -e"rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("grep -e"));
    }

    #[test]
    fn sanitize_handles_attached_search_pattern_value_ag() {
        let cmd = r#"ag -e"rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("ag -e"));
        assert!(sanitized.as_ref().contains("src/main.rs"));
    }

    #[test]
    fn sanitize_handles_attached_search_pattern_value_ack() {
        let cmd = r#"ack -e"rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("ack -e"));
        assert!(sanitized.as_ref().contains("src/main.rs"));
    }

    #[test]
    fn sanitize_handles_attached_git_commit_message() {
        let cmd = r#"git commit -m"Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("git commit -m"));
    }

    #[test]
    fn sanitize_handles_sudo_wrapper() {
        let cmd = r#"sudo git commit -m "Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("sudo git commit -m"));
    }

    #[test]
    fn sanitize_handles_sudo_wrapper_with_path() {
        let cmd = r#"/usr/bin/sudo git commit -m "Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("/usr/bin/sudo git commit -m"));
    }

    #[test]
    fn sanitize_handles_sudo_u_wrapper() {
        let cmd = r#"sudo -u root git commit -m "Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("sudo -u root git commit -m"));
    }

    #[test]
    fn sanitize_handles_env_unset_wrapper() {
        let cmd = r#"env -u FOO rg -n "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("env -u FOO rg -n"));
    }

    #[test]
    fn sanitize_handles_env_unset_wrapper_with_path() {
        let cmd = r#"/usr/bin/env -u FOO rg -n "rm -rf" src/main.rs"#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("/usr/bin/env -u FOO rg -n"));
    }

    #[test]
    fn sanitize_masks_command_query_v() {
        let cmd = r#"command -v "rm -rf""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("command -v"));
    }

    #[test]
    fn sanitize_masks_command_query_v_combined() {
        let cmd = r#"command -pv "rm -rf""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("command -pv"));
    }

    #[test]
    fn sanitize_does_not_mask_command_p_wrapper() {
        let cmd = r"command -p rm -rf /tmp";
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Borrowed(_)));
        assert!(sanitized.as_ref().contains("rm -rf"));
    }

    #[test]
    fn sanitize_handles_combined_short_flags_with_data_value() {
        let cmd = r#"git commit -am "Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("git commit -am"));
    }

    #[test]
    fn sanitize_handles_sudo_d_chdir_wrapper() {
        // sudo -D changes to directory before running command
        let cmd = r#"sudo -D /tmp git commit -m "Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("sudo -D /tmp git commit -m"));
    }

    #[test]
    fn sanitize_handles_sudo_r_role_wrapper() {
        // sudo -r uses specified role
        let cmd = r#"sudo -r myrole git commit -m "Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(sanitized.as_ref().contains("sudo -r myrole git commit -m"));
    }

    #[test]
    fn sanitize_handles_sudo_chdir_long_wrapper() {
        // sudo --chdir changes to directory before running command
        let cmd = r#"sudo --chdir=/tmp git commit -m "Fix rm -rf detection""#;
        let sanitized = sanitize_for_pattern_matching(cmd);

        assert!(matches!(sanitized, std::borrow::Cow::Owned(_)));
        assert!(!sanitized.as_ref().contains("rm -rf"));
        assert!(
            sanitized
                .as_ref()
                .contains("sudo --chdir=/tmp git commit -m")
        );
    }

    #[test]
    fn test_regression_quoted_interpreter_identifies_inline_code() {
        // Regression test for bug where quoted interpreter paths (e.g. "/usr/bin/python")
        // caused check_inline_code_context to fail, treating -c content as safe argument.

        let cmd = r#""/usr/bin/python" -c "rm -rf /""#;
        let spans = classify_command(cmd);

        let code_span = spans
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);

        assert!(
            code_span.is_some(),
            "Quoted interpreter path must still detect -c as InlineCode"
        );

        let cmd_simple = r#""python" -c "rm -rf /""#;
        let spans_simple = classify_command(cmd_simple);
        let code_span_simple = spans_simple
            .spans()
            .iter()
            .find(|s| s.kind == SpanKind::InlineCode);

        assert!(
            code_span_simple.is_some(),
            "Quoted interpreter name must still detect -c as InlineCode"
        );
    }

    // =========================================================================
    // Safe String Registry Tests - New Entries (oien.2.1)
    // =========================================================================

    #[test]
    fn test_registry_git_grep_flag() {
        // git --grep takes a pattern for searching commit messages (data, not code)
        assert!(SAFE_STRING_REGISTRY.is_flag_data("git", "--grep"));
        // Short version doesn't exist for --grep
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("git", "-g"));
    }

    #[test]
    fn test_registry_ag_pattern_flags() {
        // ag (Silver Searcher) -e/--pattern take search patterns
        assert!(SAFE_STRING_REGISTRY.is_flag_data("ag", "-e"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("ag", "--pattern"));
    }

    #[test]
    fn test_registry_ack_pattern_flags() {
        // ack -e/--pattern take search patterns
        assert!(SAFE_STRING_REGISTRY.is_flag_data("ack", "-e"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("ack", "--pattern"));
    }

    #[test]
    fn test_registry_curl_data_flags() {
        // curl -d/--data, -H/--header, --data-raw, --data-binary are data
        assert!(SAFE_STRING_REGISTRY.is_flag_data("curl", "-d"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("curl", "--data"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("curl", "-H"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("curl", "--header"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("curl", "--data-raw"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("curl", "--data-binary"));
        // But --url is NOT data (it could be code injection target)
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("curl", "--url"));
    }

    #[test]
    fn test_registry_jq_variable_flags() {
        // jq --arg, --argjson, --slurpfile pass data values
        assert!(SAFE_STRING_REGISTRY.is_flag_data("jq", "--arg"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("jq", "--argjson"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("jq", "--slurpfile"));
        // But -f/--from-file takes a file path that gets executed
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("jq", "-f"));
    }

    #[test]
    fn test_registry_docker_label_flags() {
        // docker -l/--label sets metadata
        assert!(SAFE_STRING_REGISTRY.is_flag_data("docker", "-l"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("docker", "--label"));
        // But --entrypoint is NOT data (it's code)
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("docker", "--entrypoint"));
    }

    #[test]
    fn test_registry_kubectl_annotation_label_flags() {
        // kubectl --annotation, -l/--label set metadata
        assert!(SAFE_STRING_REGISTRY.is_flag_data("kubectl", "--annotation"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("kubectl", "-l"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("kubectl", "--label"));
        // But --command is NOT data (it's code)
        assert!(!SAFE_STRING_REGISTRY.is_flag_data("kubectl", "--command"));
    }

    #[test]
    fn test_registry_xargs_placeholder_flag() {
        // xargs -I sets a placeholder string
        assert!(SAFE_STRING_REGISTRY.is_flag_data("xargs", "-I"));
        // Counterexample: xargs itself can execute commands
        assert!(!SAFE_STRING_REGISTRY.is_all_args_data("xargs"));
    }

    #[test]
    fn test_registry_cargo_npm_message_flags() {
        // cargo/npm --message for version messages
        assert!(SAFE_STRING_REGISTRY.is_flag_data("cargo", "--message"));
        assert!(SAFE_STRING_REGISTRY.is_flag_data("npm", "--message"));
    }

    #[test]
    fn test_false_positive_curl_data() {
        // curl -d with destructive-looking data should NOT trigger
        let cmd = r#"curl -d "rm -rf /" https://api.example.com"#;

        // Using the registry, we know -d flag args are data
        assert!(SAFE_STRING_REGISTRY.is_flag_data("curl", "-d"));

        // The data should be classified as Argument
        let spans = classify_command(cmd);
        let data_span = spans
            .spans()
            .iter()
            .find(|s| s.text(cmd).contains("rm -rf"));
        assert!(data_span.is_some());
        assert_eq!(data_span.unwrap().kind, SpanKind::Argument);
    }

    #[test]
    fn test_false_positive_ag_pattern() {
        // ag -e with destructive-looking pattern should NOT trigger
        let cmd = r#"ag -e "rm -rf" src/"#;

        // Using the registry
        assert!(SAFE_STRING_REGISTRY.is_flag_data("ag", "-e"));

        // The pattern should be classified as Argument
        let spans = classify_command(cmd);
        let pattern_span = spans.spans().iter().find(|s| s.text(cmd) == "\"rm -rf\"");
        assert!(pattern_span.is_some());
        assert_eq!(pattern_span.unwrap().kind, SpanKind::Argument);
    }

    #[test]
    fn test_false_positive_docker_label() {
        // docker --label with destructive-looking label should NOT trigger
        let cmd = r#"docker run --label "cleanup=rm -rf /tmp" nginx"#;

        // Using the registry
        assert!(SAFE_STRING_REGISTRY.is_flag_data("docker", "--label"));

        // The label should be classified as Argument
        let spans = classify_command(cmd);
        let label_span = spans
            .spans()
            .iter()
            .find(|s| s.text(cmd).contains("rm -rf"));
        assert!(label_span.is_some());
        assert_eq!(label_span.unwrap().kind, SpanKind::Argument);
    }
}