eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
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
use crate::mcp::machine_codes;
use crate::mcp::response::{finding, verdict, ToolResponse};
use crate::text::inspect_prompt::{prompt_input_inspect, prompt_wire_recommendations};
use crate::text::measure::{char_category_metrics, word_metrics};
use crate::text::position::{TextPositionResult, TextWindowPosition, TextWindowResult};
use crate::text::transform::{
    TextFingerprintResult, TextHashResult, TextTransformResult, UnescapeTextResult,
};
use crate::text::unicode_tools::{
    detect_mixed_scripts as unicode_detect_mixed_scripts, detect_newline_style,
    find_invisibles as unicode_find_invisibles,
};
use crate::text::{count_graphemes, text_fingerprint};
use crate::tools::helpers::*;
use serde_json::Value;
use unicode_normalization::UnicodeNormalization;

// ---------------------------------------------------------------------------
// text_measure
// ---------------------------------------------------------------------------

pub fn text_measure(args: &Value) -> ToolResponse {
    let text = match _require_str(args, "text", "text_measure") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("Text exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_measure"),
        );
    }

    let valid_details = ["summary", "normal", "full"];
    if !valid_details.contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec![format!("Use one of: {}", valid_details.join(", "))]),
            Some("text_measure"),
        );
    }

    let bytes_utf8 = text.len();
    let codepoints = text.chars().count();
    let graphemes = crate::text::count_graphemes(text);
    let word_stats = word_metrics(text);
    let words = word_stats.words;
    let char_stats = char_category_metrics(text);
    let text_lines = split_lines(text);
    let lines = text_lines.len();
    let nonempty_lines = text_lines.iter().filter(|l| !l.is_empty()).count();
    let blank_lines = text_lines.iter().filter(|l| l.is_empty()).count();

    let mut warnings: Vec<String> = Vec::new();
    let combining_marks = char_stats.combining_marks;
    if combining_marks > 0 {
        warnings.push(format!("Text contains {} combining mark(s) - codepoint count diverges from user-perceived characters", combining_marks));
    }

    // Detect special sequences (matching Python's _detect_special_sequences)
    let mut zwj_count = 0;
    let mut variation_selector_count = 0;
    let mut regional_indicator_pairs = 0;
    let mut emoji_modifier_count = 0;
    let mut chars = text.chars().peekable();
    while let Some(c) = chars.next() {
        let cp = c as u32;
        if cp == 0x200D {
            // ZWJ
            zwj_count += 1;
        } else if (0xFE00..=0xFE0F).contains(&cp) {
            // variation selector
            variation_selector_count += 1;
        } else if (0x1F1E6..=0x1F1FF).contains(&cp) {
            // regional indicator
            if let Some(&next) = chars.peek() {
                let next_cp = next as u32;
                if (0x1F1E6..=0x1F1FF).contains(&next_cp) {
                    regional_indicator_pairs += 1;
                    chars.next(); // skip the pair
                }
            }
        } else if (0x1F3FB..=0x1F3FF).contains(&cp) {
            // emoji modifier
            emoji_modifier_count += 1;
        }
    }
    if zwj_count > 0 {
        warnings.push(format!(
            "Text contains {} zero-width joiner sequence(s) - sequences may affect display",
            zwj_count
        ));
    }
    if variation_selector_count > 0 {
        warnings.push(format!(
            "Text contains {} variation selector(s) - display may differ",
            variation_selector_count
        ));
    }
    if regional_indicator_pairs > 0 {
        warnings.push(format!(
            "Text contains {} regional indicator pair(s) - these render as flag emoji",
            regional_indicator_pairs
        ));
    }
    if emoji_modifier_count > 0 {
        warnings.push(format!(
            "Text contains {} emoji modifier(s) - modifies base emoji appearance",
            emoji_modifier_count
        ));
    }

    if detail == "summary" {
        let ascii = text.chars().filter(|c| c.is_ascii()).count();
        let non_ascii = codepoints - ascii;
        let result = serde_json::json!({
            "codepoints": codepoints,
            "graphemes": graphemes,
            "words": words,
            "bytes_utf8": bytes_utf8,
            "ascii": ascii,
            "non_ascii": non_ascii,
            "warnings": warnings,
        });
        return ToolResponse::success(result, Some("text_measure")).with_tool("text_measure");
    }

    // normal/full detail
    let unique_words_casefolded: usize = word_stats.unique_words_casefolded;
    let max_line_length_codepoints = text_lines
        .iter()
        .map(|l| l.chars().count())
        .max()
        .unwrap_or(0);
    let chars_no_whitespace = text.chars().filter(|c| !c.is_whitespace()).count();
    let ascii = text.chars().filter(|c| c.is_ascii()).count();
    let non_ascii = codepoints - ascii;
    let letters = char_stats.letters;
    let digits = char_stats.digits;
    let punctuation = char_stats.punctuation;
    let symbols = char_stats.symbols;
    let spaces = char_stats.spaces;
    let control_chars = char_stats.control_chars;
    let all_invisibles = unicode_find_invisibles(text);
    let invisible_chars = all_invisibles.len();

    let newline_style = detect_newline_style(text);
    let ends_with_newline = text.ends_with('\n') || text.ends_with('\r');

    let normalization = serde_json::json!({
        "is_nfc": text.nfc().eq(text.chars()),
        "is_nfd": text.nfd().eq(text.chars()),
        "is_nfkc": text.nfkc().eq(text.chars()),
        "is_nfkd": text.nfkd().eq(text.chars()),
    });

    let contains_invisibles = invisible_chars > 0;
    // Typed bidi membership (never a display-string predicate).
    let contains_bidi_controls = all_invisibles
        .iter()
        .any(|inv| crate::text::unicode_tools::is_bidi_control(inv.char));
    let mixed_script_result = unicode_detect_mixed_scripts(text);
    let scripts = mixed_script_result.scripts;
    let mixed_scripts = mixed_script_result.mixed_scripts;
    let unicode_risks = serde_json::json!({
        "contains_invisibles": contains_invisibles,
        "contains_bidi_controls": contains_bidi_controls,
        "mixed_scripts": mixed_scripts,
        "scripts": scripts,
    });

    let result = serde_json::json!({
        "bytes_utf8": bytes_utf8,
        "codepoints": codepoints,
        "graphemes": graphemes,
        "words": words,
        "unique_words_casefolded": unique_words_casefolded,
        "lines": lines,
        "nonempty_lines": nonempty_lines,
        "blank_lines": blank_lines,
        "max_line_length_codepoints": max_line_length_codepoints,
        "chars_no_whitespace": chars_no_whitespace,
        "ascii": ascii,
        "non_ascii": non_ascii,
        "letters": letters,
        "digits": digits,
        "punctuation": punctuation,
        "symbols": symbols,
        "spaces": spaces,
        "control_chars": control_chars,
        "combining_marks": combining_marks,
        "invisible_chars": invisible_chars,
        "newline_style": newline_style,
        "ends_with_newline": ends_with_newline,
        "normalization": normalization,
        "unicode_risks": unicode_risks,
        "warnings": warnings,
    });

    ToolResponse::success(result, Some("text_measure")).with_tool("text_measure")
}

// ---------------------------------------------------------------------------
// text_equal
// ---------------------------------------------------------------------------

pub fn text_equal(args: &Value) -> ToolResponse {
    let a = match _require_str(args, "a", "text_equal") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let b = match _require_str(args, "b", "text_equal") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let casefold = args
        .get("casefold")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let normalization = args
        .get("normalization")
        .and_then(|v| v.as_str())
        .unwrap_or("raw");
    let trim = args.get("trim").and_then(|v| v.as_bool()).unwrap_or(false);
    let ignore_newline_style = args
        .get("ignore_newline_style")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let ignore_trailing_whitespace = args
        .get("ignore_trailing_whitespace")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let ignore_final_newline = args
        .get("ignore_final_newline")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    if !["raw", "NFC", "NFD", "NFKC", "NFKD"].contains(&normalization) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported normalization form: {}", normalization),
            Some(vec!["Use one of: raw, NFC, NFD, NFKC, NFKD".to_string()]),
            Some("text_equal"),
        );
    }

    let mut a_work = a.to_string();
    let mut b_work = b.to_string();

    if ignore_final_newline {
        while a_work.ends_with('\n') || a_work.ends_with('\r') {
            a_work.pop();
        }
        while b_work.ends_with('\n') || b_work.ends_with('\r') {
            b_work.pop();
        }
    }

    if ignore_trailing_whitespace {
        let lines_a: Vec<String> = split_lines(&a_work);
        let lines_b: Vec<String> = split_lines(&b_work);
        a_work = lines_a
            .iter()
            .map(|l| l.trim_end().to_string())
            .collect::<Vec<_>>()
            .join("\n");
        b_work = lines_b
            .iter()
            .map(|l| l.trim_end().to_string())
            .collect::<Vec<_>>()
            .join("\n");
    }

    if ignore_newline_style {
        a_work = a_work.replace("\r\n", "\n").replace("\r", "\n");
        b_work = b_work.replace("\r\n", "\n").replace("\r", "\n");
    }

    if trim {
        a_work = a_work.trim().to_string();
        b_work = b_work.trim().to_string();
    }

    let raw_equal = a_work == b_work;
    let casefold_equal = unicode_casefold(&a_work) == unicode_casefold(&b_work);
    let byte_equal = a_work.as_bytes() == b_work.as_bytes();
    let a_nfc: String = a_work.nfc().collect();
    let b_nfc: String = b_work.nfc().collect();
    let nfc_equal: bool = a_nfc == b_nfc;
    let a_nfd: String = a_work.nfd().collect();
    let b_nfd: String = b_work.nfd().collect();
    let nfd_equal: bool = a_nfd == b_nfd;
    let a_nfkc: String = a_work.nfkc().collect();
    let b_nfkc: String = b_work.nfkc().collect();
    let nfkc_equal: bool = a_nfkc == b_nfkc;
    let a_nfkd: String = a_work.nfkd().collect();
    let b_nfkd: String = b_work.nfkd().collect();
    let nfkd_equal: bool = a_nfkd == b_nfkd;

    let equal = if casefold {
        casefold_equal
    } else if normalization == "raw" {
        raw_equal
    } else {
        let a_norm: String = match normalization {
            "NFC" => a_work.nfc().collect(),
            "NFD" => a_work.nfd().collect(),
            "NFKC" => a_work.nfkc().collect(),
            "NFKD" => a_work.nfkd().collect(),
            _ => a_work.clone(),
        };
        let b_norm: String = match normalization {
            "NFC" => b_work.nfc().collect(),
            "NFD" => b_work.nfd().collect(),
            "NFKC" => b_work.nfkc().collect(),
            "NFKD" => b_work.nfkd().collect(),
            _ => b_work.clone(),
        };
        a_norm == b_norm
    };

    // Find first difference (always compute from post-trim raw values, matching Python)
    let mut a_chars = a_work.chars();
    let mut b_chars = b_work.chars();
    let mut a_idx = 0;
    let mut b_idx = 0;
    let mut found: Option<serde_json::Value> = None;
    loop {
        let a_ch = a_chars.next();
        let b_ch = b_chars.next();
        match (a_ch, b_ch) {
            (Some(ac), Some(bc)) => {
                if ac != bc {
                    found = Some(serde_json::json!({
                        "a_index": a_idx,
                        "b_index": b_idx,
                        "a_char": ac.to_string(),
                        "b_char": bc.to_string(),
                        "a_codepoint": format!("U+{:04X}", ac as u32),
                        "b_codepoint": format!("U+{:04X}", bc as u32),
                        "a_visible": build_safe_repr(&ac.to_string()),
                        "b_visible": build_safe_repr(&bc.to_string()),
                    }));
                    break;
                }
                a_idx += 1;
                b_idx += 1;
            }
            (None, None) => break,
            (Some(_), None) | (None, Some(_)) => {
                found = Some(serde_json::json!({
                    "a_index": a_idx,
                    "b_index": b_idx,
                    "a_char": a_ch.map(|c| c.to_string()).unwrap_or_default(),
                    "b_char": b_ch.map(|c| c.to_string()).unwrap_or_default(),
                    "a_codepoint": a_ch.map(|c| format!("U+{:04X}", c as u32)).unwrap_or_default(),
                    "b_codepoint": b_ch.map(|c| format!("U+{:04X}", c as u32)).unwrap_or_default(),
                    "a_visible": a_ch.map(|c| c.to_string()).unwrap_or_default(),
                    "b_visible": b_ch.map(|c| c.to_string()).unwrap_or_default(),
                }));
                break;
            }
        }
    }

    let a_chars_work: Vec<char> = a_work.chars().collect();
    let b_chars_work: Vec<char> = b_work.chars().collect();
    let length_diff = a_chars_work.len() != b_chars_work.len();
    let invisibles_detected = a_chars_work.iter().any(|c| is_invisible_char(*c))
        || b_chars_work.iter().any(|c| is_invisible_char(*c));

    let classification = classify_difference(
        raw_equal,
        nfc_equal,
        casefold_equal,
        byte_equal,
        length_diff,
        invisibles_detected,
    );

    ToolResponse::success(
        serde_json::json!({
            "equal": equal,
            "mode": {
                "normalization": normalization,
                "casefold": casefold,
                "trim": trim,
                "ignore_newline_style": ignore_newline_style,
                "ignore_trailing_whitespace": ignore_trailing_whitespace,
                "ignore_final_newline": ignore_final_newline,
            },
            "raw_equal": raw_equal,
            "nfc_equal": nfc_equal,
            "nfd_equal": nfd_equal,
            "nfkc_equal": nfkc_equal,
            "nfkd_equal": nfkd_equal,
            "casefold_equal": casefold_equal,
            "byte_equal": byte_equal,
            "lengths": {
                "a_codepoints": a_work.chars().count(),
                "b_codepoints": b_work.chars().count(),
                "a_bytes_utf8": a_work.len(),
                "b_bytes_utf8": b_work.len(),
            },
            "first_difference": found,
            "classification": classification,
        }),
        Some("text_equal"),
    )
    .with_tool("text_equal")
}

// ---------------------------------------------------------------------------
// text_diff_explain
// ---------------------------------------------------------------------------

pub fn text_diff_explain(args: &Value) -> ToolResponse {
    let budget_ctx = crate::mcp::budget::for_handler(crate::mcp::budget::ToolBudget::MODERATE);

    let a = match _require_str(args, "a", "text_diff_explain") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let b = match _require_str(args, "b", "text_diff_explain") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let max_diffs = args.get("max_diffs").and_then(|v| v.as_i64()).unwrap_or(20);
    if max_diffs < 0 {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("max_diffs must be non-negative, got {}", max_diffs),
            None,
            Some("text_diff_explain"),
        );
    }
    let max_diffs = max_diffs as usize;
    let include_codepoints = args
        .get("include_codepoints")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let include_context = args
        .get("include_context")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    if max_diffs > 10000 {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("max_diffs {} exceeds 10000", max_diffs),
            None,
            Some("text_diff_explain"),
        );
    }

    let a_length = a.chars().count();
    let b_length = b.chars().count();
    if a_length > MAX_TEXT_LENGTH || b_length > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("Input exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_diff_explain"),
        );
    }

    let valid_details = ["summary", "normal", "full"];
    if !valid_details.contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec![format!("Use one of: {}", valid_details.join(", "))]),
            Some("text_diff_explain"),
        );
    }

    let max_diffs_to_use = if detail == "summary" {
        max_diffs.min(5)
    } else {
        max_diffs
    };

    let raw_equal = a == b;
    let byte_equal = raw_equal;

    let a_nfc: String = a.nfc().collect();
    let b_nfc: String = b.nfc().collect();
    let nfc_equal = a_nfc == b_nfc;

    let a_nfkc: String = a.nfkc().collect();
    let b_nfkc: String = b.nfkc().collect();
    let nfkc_equal = a_nfkc == b_nfkc;

    let casefold_equal = unicode_casefold(a) == unicode_casefold(b);

    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    let same_length_codepoints = a_chars.len() == b_chars.len();
    let length_diff = !same_length_codepoints;

    let a_invisibles = a_chars.iter().any(|c| is_invisible_char(*c));
    let b_invisibles = b_chars.iter().any(|c| is_invisible_char(*c));
    let invisibles_detected = a_invisibles || b_invisibles;

    let mut classification = classify_difference(
        raw_equal,
        nfc_equal,
        casefold_equal,
        byte_equal,
        length_diff,
        invisibles_detected,
    );
    if classification == "ordinary_text_difference" && nfkc_equal {
        classification = "compatibility_normalization_only".to_string();
    }

    // Check the execution budget before running quadratic-time analysis
    // (Levenshtein + diff spans) so cancelled/over-budget calls bail early.
    if budget_ctx.should_stop() {
        return budget_ctx
            .check_should_stop("text_diff_explain")
            .unwrap_err();
    }

    let distance = if raw_equal {
        0
    } else {
        crate::text::levenshtein_distance(a, b)
    };

    if budget_ctx.should_stop() {
        return budget_ctx
            .check_should_stop("text_diff_explain")
            .unwrap_err();
    }

    let all_spans = crate::text::diff_spans(a, b, max_diffs_to_use);
    let truncated = all_spans.len() >= max_diffs_to_use;
    let max_diffs_applied = max_diffs_to_use;

    let prefix_len = common_prefix_len(a, b);
    let suffix_len = common_suffix_len(a, b);

    let equal_char_limit = if detail == "summary" { 50 } else { 200 };

    let mut diffs_output: Vec<serde_json::Value> = Vec::new();
    for s in &all_spans {
        let (a_text_show, b_text_show, _truncated_flag) =
            if s.kind == "equal" && s.a_text.chars().count() > equal_char_limit {
                let truncated_a: String = s.a_text.chars().take(equal_char_limit).collect();
                let truncated_b: String = s.b_text.chars().take(equal_char_limit).collect();
                (
                    format!("{}...", truncated_a),
                    format!("{}...", truncated_b),
                    true,
                )
            } else {
                (s.a_text.clone(), s.b_text.clone(), false)
            };
        let a_visible = build_safe_repr(&a_text_show);
        let b_visible = build_safe_repr(&b_text_show);

        let note = if s.kind == "equal" {
            "Matching text".to_string()
        } else if s.a_text.chars().count() != s.b_text.chars().count() {
            format!(
                "Length difference: {} vs {} codepoints",
                s.a_text.chars().count(),
                s.b_text.chars().count()
            )
        } else if nfc_equal {
            "Different raw codepoints, equal after NFC normalization".to_string()
        } else {
            "Different codepoints".to_string()
        };

        diffs_output.push(serde_json::json!({
            "kind": s.kind,
            "a_span": [s.a_start, s.a_end],
            "b_span": [s.b_start, s.b_end],
            "a_text": a_text_show,
            "b_text": b_text_show,
            "a_visible": a_visible,
            "b_visible": b_visible,
            "a_codepoints": if include_codepoints {
                s.a_text.chars().map(|c| {
                    serde_json::json!({
                        "char": format!("{}", c),
                        "codepoint": format!("U+{:04X}", c as u32),
                        "name": unicode_name_char(c),
                    })
                }).collect::<Vec<_>>()
            } else {
                vec![]
            },
            "b_codepoints": if include_codepoints {
                s.b_text.chars().map(|c| {
                    serde_json::json!({
                        "char": format!("{}", c),
                        "codepoint": format!("U+{:04X}", c as u32),
                        "name": unicode_name_char(c),
                    })
                }).collect::<Vec<_>>()
            } else {
                vec![]
            },
            "note": if include_context { note } else { String::new() },
        }));
    }

    let mut security_findings: Vec<serde_json::Value> = Vec::new();

    let a_invisible_count = a_chars.iter().filter(|c| is_invisible_char(**c)).count();
    let b_invisible_count = b_chars.iter().filter(|c| is_invisible_char(**c)).count();
    if a_invisible_count > 0 || b_invisible_count > 0 {
        security_findings.push(serde_json::json!({
            "kind": "invisible_characters",
            "a_count": a_invisible_count,
            "b_count": b_invisible_count,
        }));
    }

    let mut a_confusable_count = 0;
    let mut b_confusable_count = 0;
    for c in a.chars() {
        if crate::text::confusables::lookup(c).is_some() {
            a_confusable_count += 1;
        }
    }
    for c in b.chars() {
        if crate::text::confusables::lookup(c).is_some() {
            b_confusable_count += 1;
        }
    }
    if a_confusable_count > 0 || b_confusable_count > 0 {
        security_findings.push(serde_json::json!({
            "kind": "confusables",
            "a_count": a_confusable_count,
            "b_count": b_confusable_count,
        }));
    }

    let agent_instruction =
        generate_agent_instruction(&classification, raw_equal, nfc_equal, byte_equal);

    let equal = raw_equal;

    let a_metrics = serde_json::json!({
        "bytes_utf8": a.len(),
        "codepoints": a_chars.len(),
    });

    let b_metrics = serde_json::json!({
        "bytes_utf8": b.len(),
        "codepoints": b_chars.len(),
    });

    let summary = serde_json::json!({
        "raw_equal": raw_equal,
        "byte_equal": byte_equal,
        "nfc_equal": nfc_equal,
        "nfkc_equal": nfkc_equal,
        "casefold_equal": casefold_equal,
        "same_length_codepoints": same_length_codepoints,
        "edit_distance": distance,
        "common_prefix_len": prefix_len,
        "common_suffix_len": suffix_len,
        "truncated": truncated,
        "max_diffs_applied": max_diffs_applied,
    });

    let result = serde_json::json!({
        "equal": equal,
        "classification": classification,
        "summary": summary,
        "a_metrics": a_metrics,
        "b_metrics": b_metrics,
        "diffs": diffs_output,
        "security_findings": security_findings,
        "agent_instruction": agent_instruction,
    });

    ToolResponse::success(result, Some("text_diff_explain")).with_tool("text_diff_explain")
}

// ---------------------------------------------------------------------------
// text_inspect
// ---------------------------------------------------------------------------

pub fn text_inspect(args: &Value) -> ToolResponse {
    let text = match _require_str(args, "text", "text_inspect") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let include_codepoints = args
        .get("include_codepoints")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let include_confusables = args
        .get("include_confusables")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");
    let normalize_form = args
        .get("normalize")
        .and_then(|v| v.as_str())
        .unwrap_or("none");
    let compare_normalized = args
        .get("compare_normalized")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let _ = include_codepoints; // codepoints are always included in invisibles/bidi items (matches Python)

    let valid_details = ["summary", "normal", "full"];
    if !valid_details.contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec![format!("Use one of: {}", valid_details.join(", "))]),
            Some("text_inspect"),
        );
    }

    let valid_norms = ["none", "NFC", "NFD", "NFKC", "NFKD"];
    if !valid_norms.contains(&normalize_form) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported normalization form: {}", normalize_form),
            Some(vec![format!("Use one of: {}", valid_norms.join(", "))]),
            Some("text_inspect"),
        );
    }

    let codepoints = text.chars().count();
    if codepoints > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("Text exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_inspect"),
        );
    }

    let mut limits_applied: Vec<String> = Vec::new();

    // --- Metrics ---
    let bytes_utf8 = text.len();
    let graphemes = count_graphemes(text);
    let word_stats = word_metrics(text);
    let words = word_stats.words;
    let text_lines = split_lines(text);
    let lines = text_lines.len();
    let nonempty_lines = text_lines.iter().filter(|l| !l.is_empty()).count();
    let blank_lines = text_lines.iter().filter(|l| l.is_empty()).count();

    let mut ascii_count = 0usize;
    let mut non_ascii = 0usize;
    let mut zwj_sequences = 0usize;
    let mut regional_indicator_pairs = 0usize;
    let mut emoji_modifiers = 0usize;
    let chars_vec: Vec<char> = text.chars().collect();
    let char_stats = char_category_metrics(text);
    let all_invisibles = unicode_find_invisibles(text);

    for (i, c) in chars_vec.iter().enumerate() {
        let cp = *c as u32;
        if cp <= 0x7F {
            ascii_count += 1;
        } else {
            non_ascii += 1;
        }
        if cp == 0x200D {
            zwj_sequences += 1;
        }
        if (0x1F1E6..=0x1F1FF).contains(&cp)
            && i + 1 < chars_vec.len()
            && (0x1F1E6..=0x1F1FF).contains(&(chars_vec[i + 1] as u32))
        {
            regional_indicator_pairs += 1;
        }
        if (0x1F3FB..=0x1F3FF).contains(&cp) {
            emoji_modifiers += 1;
        }
    }

    let unique_words_casefolded: usize = word_stats.unique_words_casefolded;
    let max_line_length_codepoints = text_lines
        .iter()
        .map(|l| l.chars().count())
        .max()
        .unwrap_or(0);
    let chars_no_whitespace = text.chars().filter(|c| !c.is_whitespace()).count();
    let letters = char_stats.letters;
    let digits = char_stats.digits;
    let punctuation = char_stats.punctuation;
    let symbols = char_stats.symbols;
    let spaces = char_stats.spaces;
    let control_chars = char_stats.control_chars;
    let combining_marks = char_stats.combining_marks;
    let invisible_chars = all_invisibles.len();

    let newline_style = detect_newline_style(text);
    let ends_with_newline = text.ends_with('\n') || text.ends_with('\r');

    // --- Normalization check ---
    let is_nfc: String = text.nfc().collect();
    let is_nfd: String = text.nfd().collect();
    let is_nfkc: String = text.nfkc().collect();
    let is_nfkd: String = text.nfkd().collect();
    let normalization_is_nfc = text == is_nfc;
    let normalization_is_nfd = text == is_nfd;
    let normalization_is_nfkc = text == is_nfkc;
    let normalization_is_nfkd = text == is_nfkd;
    let normalization_diff = !normalization_is_nfc;

    let safe_repr = build_safe_repr(text);
    let normals_repr = if normalization_diff {
        Some(is_nfc.clone())
    } else {
        None
    };

    // --- Invisibles and Bidi Controls ---
    let mut invisibles: Vec<serde_json::Value> = Vec::new();
    let mut bidi_controls: Vec<serde_json::Value> = Vec::new();

    for inv in &all_invisibles {
        let item = serde_json::json!({
            "index": inv.index,
            "char": format!("{}", inv.char),
            "codepoint": inv.codepoint,
            "name": inv.name,
            "category": inv.category,
            "display": inv.display,
        });
        // Typed bidi membership (never a display-string predicate): RLO and
        // friends report displays like "RLO", not "BIDI", so the old
        // `display.contains("BIDI")` check misclassified them.
        if crate::text::unicode_tools::is_bidi_control(inv.char) {
            bidi_controls.push(item);
        } else {
            invisibles.push(item);
        }
    }

    // --- Confusables ---
    let confusables: Vec<serde_json::Value> = if include_confusables {
        text.chars()
            .enumerate()
            .filter_map(|(i, c)| {
                crate::text::confusables::lookup(c).map(|sub| {
                    let name = unicode_names2::name(c)
                        .map(|n| n.to_string())
                        .unwrap_or_else(|| "<unknown>".to_string());
                    let confusable_chars: String = sub
                        .split_whitespace()
                        .filter_map(|cp| cp.strip_prefix("U+"))
                        .filter_map(|hex| u32::from_str_radix(hex, 16).ok())
                        .filter_map(char::from_u32)
                        .collect();
                    let confusable_name: String = confusable_chars
                        .chars()
                        .map(|ch| {
                            unicode_names2::name(ch)
                                .map(|n| n.to_string())
                                .unwrap_or_else(|| ch.to_string())
                        })
                        .collect::<Vec<_>>()
                        .join(" ");
                    serde_json::json!({
                        "index": i,
                        "char": format!("{}", c),
                        "codepoint": format!("U+{:04X}", c as u32),
                        "name": name,
                        "confusable_with": confusable_chars,
                        "confusable_name": confusable_name,
                    })
                })
            })
            .collect()
    } else {
        vec![]
    };

    // --- Mixed Scripts ---
    let mixed_script_result = unicode_detect_mixed_scripts(text);
    let scripts = mixed_script_result.scripts.clone();
    let script_positions: Vec<serde_json::Value> = mixed_script_result
        .positions
        .iter()
        .map(|pos| {
            serde_json::json!({
                "index": pos.index,
                "char": pos.char.to_string(),
                "script": pos.script,
                "codepoint": pos.codepoint,
            })
        })
        .collect();
    let mixed_scripts = mixed_script_result.mixed_scripts;

    // --- Apply detail limits (must run before warnings, since Python iterates over truncated lists) ---
    let max_items = inspect_max_items(detail);
    let invisibles_limited = apply_detail_limit(&invisibles, max_items);
    let confusables_limited = apply_detail_limit(&confusables, max_items);

    if invisibles.len() > max_items {
        limits_applied.push(format!("invisibles_limited={}", max_items));
    }
    if confusables.len() > max_items {
        limits_applied.push(format!("confusables_limited={}", max_items));
    }

    // --- Compute limits_applied_info (Python: computed once, then appended in loop AND at end) ---
    let mut limits_applied_info: Vec<String> = Vec::new();
    let total_invisibles_omitted = invisibles.len() - invisibles_limited.len();
    let total_confusables_omitted = confusables.len() - confusables_limited.len();
    if total_invisibles_omitted > 0 {
        limits_applied_info.push(format!("invisibles_omitted={}", total_invisibles_omitted));
    }
    if total_confusables_omitted > 0 {
        limits_applied_info.push(format!("confusables_omitted={}", total_confusables_omitted));
    }

    // --- Warnings ---
    let mut warnings: Vec<serde_json::Value> = Vec::new();
    for inv in invisibles_limited.iter() {
        let name_str = inv.get("name").and_then(|v| v.as_str()).unwrap_or("");
        let idx = inv.get("index").and_then(|v| v.as_u64()).unwrap_or(0);
        warnings.push(serde_json::json!({
            "severity": "warning",
            "kind": "invisible_character",
            "message": format!("Text contains {} at index {}", name_str, idx),
            "codepoint": inv.get("codepoint").cloned().unwrap_or(serde_json::Value::Null),
        }));
    }
    for bc in &bidi_controls {
        let name_str = bc.get("name").and_then(|v| v.as_str()).unwrap_or("");
        let idx = bc.get("index").and_then(|v| v.as_u64()).unwrap_or(0);
        warnings.push(serde_json::json!({
            "severity": "danger",
            "kind": "bidi_control",
            "message": format!("Text contains bidirectional control character {} at index {}", name_str, idx),
            "codepoint": bc.get("codepoint").cloned().unwrap_or(serde_json::Value::Null),
        }));
    }
    if mixed_scripts {
        warnings.push(serde_json::json!({
            "severity": "info",
            "kind": "mixed_scripts",
            "message": format!("Text contains mixed scripts: {}", scripts.join(", ")),
        }));
    }
    for conf in confusables_limited.iter() {
        let char_str = conf.get("char").and_then(|v| v.as_str()).unwrap_or("");
        let confusable_str = conf
            .get("confusable_with")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        warnings.push(serde_json::json!({
            "severity": "warning",
            "kind": "confusable",
            "message": format!("Text contains confusable character '{}' (looks like '{}')", char_str, confusable_str),
            "codepoint": conf.get("codepoint").cloned().unwrap_or(serde_json::Value::Null),
        }));
    }

    // --- In summary mode, surface omitted counts as info warnings and append to limits_applied (matches Python) ---
    if !limits_applied_info.is_empty() && detail == "summary" {
        for msg in &limits_applied_info {
            warnings.push(serde_json::json!({
                "severity": "info",
                "kind": "limits_applied",
                "message": msg,
            }));
            limits_applied.push(msg.clone());
        }
    }

    // --- Replicate Python's `limits_applied + limits_applied_info` at final return ---
    let final_limits_applied: Vec<String> = limits_applied;

    // --- metrics.warnings (string list, matches Python's MeasureTextResult.warnings) ---
    let mut metrics_warnings: Vec<String> = Vec::new();
    if combining_marks > 0 {
        metrics_warnings.push(format!(
            "Text contains {} combining mark(s) - codepoint count diverges from user-perceived characters",
            combining_marks
        ));
    }
    if zwj_sequences > 0 {
        metrics_warnings.push(format!(
            "Text contains {} zero-width joiner sequence(s) - sequences may affect display",
            zwj_sequences
        ));
    }
    if regional_indicator_pairs > 0 {
        metrics_warnings.push(format!(
            "Text contains {} regional indicator pair(s) - these render as flag emoji",
            regional_indicator_pairs
        ));
    }
    if emoji_modifiers > 0 {
        metrics_warnings.push(format!(
            "Text contains {} emoji modifier(s) - modifies base emoji appearance",
            emoji_modifiers
        ));
    }

    // --- Unicode Risks ---
    let contains_invisibles = !invisibles.is_empty();
    let contains_bidi_controls = !bidi_controls.is_empty();

    // --- Normalization Findings ---
    // Python only populates normalization_findings when compare_normalized=True AND normalize != "none"
    let mut normalization_findings: Vec<serde_json::Value> = Vec::new();

    // --- Normalized analysis (if compare_normalized) ---
    let normalized_output: Option<serde_json::Value> =
        if compare_normalized && normalize_form != "none" {
            let norm_text: String = match normalize_form {
                "NFC" => text.nfc().collect(),
                "NFD" => text.nfd().collect(),
                "NFKC" => text.nfkc().collect(),
                "NFKD" => text.nfkd().collect(),
                _ => text.to_string(),
            };
            let changed = text != norm_text;

            // Populate normalization_findings (matching Python behavior)
            match normalize_form {
                "NFKC" => {
                    normalization_findings.push(serde_json::json!({
                        "kind": "compatibility_fold",
                        "message": "NFKC changes fullwidth character to ASCII",
                    }));
                }
                "NFC" => {
                    if changed {
                        normalization_findings.push(serde_json::json!({
                            "kind": "canonical_composition",
                            "message": "NFC composes combining characters",
                        }));
                    }
                }
                "NFD" => {
                    if changed {
                        normalization_findings.push(serde_json::json!({
                            "kind": "canonical_decomposition",
                            "message": "NFD decomposes combined characters",
                        }));
                    }
                }
                "NFKD" => {
                    normalization_findings.push(serde_json::json!({
                        "kind": "compatibility_decomposition",
                        "message": "NFKD decomposes and converts compatibility characters",
                    }));
                }
                _ => {}
            }

            // Build per-character diff entries (matching Python's format)
            let mut diff_entries: Vec<serde_json::Value> = Vec::new();
            if changed {
                for (i, (c1, c2)) in text.chars().zip(norm_text.chars()).enumerate() {
                    if c1 != c2 {
                        diff_entries.push(serde_json::json!({
                            "index": i,
                            "original": format!("{}", c1),
                            "normalized": format!("{}", c2),
                            "original_codepoint": format!("U+{:04X}", c1 as u32),
                            "normalized_codepoint": format!("U+{:04X}", c2 as u32),
                        }));
                    }
                }
            }

            let norm_safe_repr = build_safe_repr(&norm_text);

            Some(serde_json::json!({
                "form": normalize_form,
                "text": norm_text,
                "safe_repr": norm_safe_repr,
                "changed": changed,
                "diff": diff_entries,
            }))
        } else {
            None
        };

    // --- Original dict (Python uses truncated invisibles/confusables) ---
    let original_dict = serde_json::json!({
        "safe_repr": safe_repr,
        "confusables": confusables_limited,
        "invisibles": invisibles_limited,
    });

    // --- Apply detail limits already computed above ---

    // --- Build result ---
    let result = serde_json::json!({
        "safe_repr": safe_repr,
        "metrics": {
            "bytes_utf8": bytes_utf8,
            "codepoints": codepoints,
            "graphemes": graphemes,
            "words": words,
            "unique_words_casefolded": unique_words_casefolded,
            "lines": lines,
            "nonempty_lines": nonempty_lines,
            "blank_lines": blank_lines,
            "max_line_length_codepoints": max_line_length_codepoints,
            "chars_no_whitespace": chars_no_whitespace,
            "ascii": ascii_count,
            "non_ascii": non_ascii,
            "letters": letters,
            "digits": digits,
            "punctuation": punctuation,
            "symbols": symbols,
            "spaces": spaces,
            "control_chars": control_chars,
            "combining_marks": combining_marks,
            "invisible_chars": invisible_chars,
            "newline_style": newline_style,
            "ends_with_newline": ends_with_newline,
            "normalization": {
                "is_nfc": normalization_is_nfc,
                "is_nfd": normalization_is_nfd,
                "is_nfkc": normalization_is_nfkc,
                "is_nfkd": normalization_is_nfkd,
            },
            "unicode_risks": {
                "contains_invisibles": contains_invisibles,
                "contains_bidi_controls": contains_bidi_controls,
                "mixed_scripts": mixed_scripts,
                "scripts": scripts,
            },
            "warnings": metrics_warnings,
        },
        "normalization": {
            "is_nfc": normalization_is_nfc,
            "is_nfkc": normalization_is_nfkc,
        },
        "normalization_diff": normalization_diff,
        "normals_repr": normals_repr,
        "invisibles": invisibles_limited,
        "bidi_controls": bidi_controls.clone(),
        "mixed_scripts": {
            "mixed_scripts": mixed_scripts,
            "scripts": scripts,
            "positions": script_positions,
        },
        "confusables": confusables_limited,
        "warnings": warnings,
        "limits_applied": final_limits_applied,
        "normalize": normalize_form,
        "compare_normalized": compare_normalized,
        "original": original_dict,
        "normalized": normalized_output,
        "normalization_findings": normalization_findings,
    });

    // --- Envelope findings (per-character, matching Python) ---
    let mut findings: Vec<serde_json::Value> = Vec::new();
    let mut machine_code: Option<String> = None;

    for inv in invisibles_limited.iter() {
        let idx = inv.get("index").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
        let name_str = inv
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown");
        findings.push(serde_json::json!({
            "code": "INVISIBLE_CHAR",
            "severity": "warn",
            "message": format!("Invisible character: {} at index {}", name_str, idx),
            "span": {"char_start": idx, "char_end": idx + 1},
            "details": {"codepoint": inv.get("codepoint"), "category": inv.get("category")},
        }));
    }
    for conf in confusables_limited.iter() {
        let idx = conf.get("index").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
        findings.push(serde_json::json!({
            "code": "CONFUSABLE_CHAR",
            "severity": "warn",
            "message": format!("Confusable character at index {}", idx),
            "span": {"char_start": idx, "char_end": idx + 1},
            "details": {"original": conf.get("char"), "confusable": conf.get("confusable_with")},
        }));
    }
    for bidi in &bidi_controls {
        let idx = bidi.get("index").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
        let name_str = bidi
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown");
        findings.push(serde_json::json!({
            "code": "BIDI_CONTROL",
            "severity": "warn",
            "message": format!("Bidirectional control character: {} at index {}", name_str, idx),
            "span": {"char_start": idx, "char_end": idx + 1},
            "details": {"codepoint": bidi.get("codepoint")},
        }));
    }

    // Determine machine_code with fixed priority: CONFUSABLES > BIDI > INVISIBLES
    if !findings.is_empty() {
        let codes: std::collections::HashSet<&str> = findings
            .iter()
            .filter_map(|f| f.get("code").and_then(|c| c.as_str()))
            .collect();
        if codes.contains("CONFUSABLE_CHAR") {
            machine_code = Some(machine_codes::CONFUSABLES_DETECTED.to_string());
        } else if codes.contains("BIDI_CONTROL") {
            machine_code = Some(machine_codes::BIDI_DETECTED.to_string());
        } else if codes.contains("INVISIBLE_CHAR") {
            machine_code = Some(machine_codes::INVISIBLES_DETECTED.to_string());
        }
    }

    let mut resp = ToolResponse::success(result, Some("text_inspect"));

    if !findings.is_empty() {
        resp = resp.with_findings(findings);
    }
    if let Some(code) = machine_code {
        resp = resp.with_machine_code(&code);
    }
    resp
}

// ---------------------------------------------------------------------------
// text_count
// ---------------------------------------------------------------------------

pub fn text_count(args: &Value) -> ToolResponse {
    let text = match _require_str(args, "text", "text_count") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let target = match args.get("target") {
        Some(v) => match v.as_str() {
            Some(s) => Some(s),
            None => {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!("target must be a string, got {}", json_type_name(v)),
                    None,
                    Some("text_count"),
                )
            }
        },
        None => None,
    };
    let count_mode = args
        .get("count_mode")
        .and_then(|v| v.as_str())
        .unwrap_or("codepoint");
    let normalization = args
        .get("normalization")
        .and_then(|v| v.as_str())
        .unwrap_or("raw");

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("Text exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_count"),
        );
    }

    let valid_count_modes = ["codepoint", "grapheme", "byte", "substring"];
    if !valid_count_modes.contains(&count_mode) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported count_mode: {}", count_mode),
            Some(vec![format!(
                "Use one of: {}",
                valid_count_modes.join(", ")
            )]),
            Some("text_count"),
        );
    }

    if !["raw", "NFC", "NFKC"].contains(&normalization) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported normalization form: {}", normalization),
            Some(vec!["Use one of: raw, NFC, NFKC".to_string()]),
            Some("text_count"),
        );
    }

    let work_text = normalize_text_count_input(text, normalization);

    if let Some(target) = target {
        const MAX_TARGET_LENGTH: usize = 1000;
        if target.chars().count() > MAX_TARGET_LENGTH {
            return ToolResponse::error_with_code(
                "input_too_large",
                machine_codes::INPUT_TOO_LARGE,
                &format!(
                    "target length {} exceeds {}",
                    target.chars().count(),
                    MAX_TARGET_LENGTH
                ),
                None,
                Some("text_count"),
            );
        }

        let work_target = normalize_text_count_input(target, normalization);
        if let Some(error) = validate_text_count_target(target, &work_target, count_mode) {
            return error;
        }

        let (count, positions) = text_count_matches(&work_text, &work_target, count_mode);

        let text_length_codepoints = match count_mode {
            "grapheme" => crate::text::count_graphemes(&work_text),
            _ => work_text.chars().count(),
        };

        ToolResponse::success(
            serde_json::json!({
                "count": count,
                "target": target,
                "normalization": normalization,
                "positions": positions,
                "text_length_codepoints": text_length_codepoints,
            }),
            Some("text_count"),
        )
        .with_tool("text_count")
    } else {
        let freq = crate::text::char_frequency(&work_text);
        let freq_value =
            serde_json::to_value(freq).unwrap_or(serde_json::Value::Object(Default::default()));
        ToolResponse::success(freq_value, Some("text_count")).with_tool("text_count")
    }
}

// ---------------------------------------------------------------------------
// text_truncate
// ---------------------------------------------------------------------------

pub fn text_truncate(args: &Value) -> ToolResponse {
    let text = match args.get("text") {
        Some(v) => match v.as_str() {
            Some(s) => s,
            None => {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!("text must be a string, got {}", json_type_name(v)),
                    None,
                    Some("text_truncate"),
                )
            }
        },
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "text must be a string, got NoneType",
                None,
                Some("text_truncate"),
            )
        }
    };
    let max_graphemes = match args.get("max_graphemes").and_then(|v| v.as_i64()) {
        Some(n) => n,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'max_graphemes' parameter",
                None,
                Some("text_truncate"),
            )
        }
    };

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("text_truncate"),
        );
    }

    if max_graphemes < 0 {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("max_graphemes must be non-negative, got {}", max_graphemes),
            Some(vec!["Set max_graphemes to 0 or higher".to_string()]),
            Some("text_truncate"),
        );
    }

    let original_graphemes = crate::text::count_graphemes(text) as i64;

    if original_graphemes <= max_graphemes {
        return ToolResponse::success(
            serde_json::json!({
                "original_graphemes": original_graphemes,
                "truncated_graphemes": original_graphemes,
                "truncated": false,
                "text": text,
            }),
            Some("text_truncate"),
        )
        .with_tool("text_truncate");
    }

    let truncated_text = crate::text::truncate_to_grapheme(text, max_graphemes as usize);
    ToolResponse::success(
        serde_json::json!({
            "original_graphemes": original_graphemes,
            "truncated_graphemes": max_graphemes,
            "truncated": true,
            "text": truncated_text,
        }),
        Some("text_truncate"),
    )
    .with_tool("text_truncate")
}

// ---------------------------------------------------------------------------
// text_fingerprint_tool
// ---------------------------------------------------------------------------

pub fn text_fingerprint_tool(args: &Value) -> ToolResponse {
    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'text' parameter",
                None,
                Some("text_fingerprint"),
            )
        }
    };
    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("Text exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_fingerprint"),
        );
    }

    let unicode = args
        .get("unicode")
        .and_then(|v| v.as_str())
        .unwrap_or("raw");
    let newline = args
        .get("newline")
        .and_then(|v| v.as_str())
        .unwrap_or("raw");
    let trim_final_newline = args
        .get("trim_final_newline")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let casefold = args
        .get("casefold")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    let valid_unicode = ["raw", "NFC", "NFD", "NFKC", "NFKD"];
    if !valid_unicode.contains(&unicode) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported unicode normalization: {}", unicode),
            Some(vec![format!("Use one of: {}", valid_unicode.join(", "))]),
            Some("text_fingerprint"),
        );
    }

    let valid_newline = ["raw", "LF"];
    if !valid_newline.contains(&newline) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported newline normalization: {}", newline),
            Some(vec![format!("Use one of: {}", valid_newline.join(", "))]),
            Some("text_fingerprint"),
        );
    }

    let result: TextFingerprintResult =
        text_fingerprint(text, unicode, newline, trim_final_newline, casefold);
    ToolResponse::success(
        serde_json::json!({
            "sha256": result.sha256,
            "bytes_utf8": result.bytes_utf8,
            "codepoints": result.codepoints,
            "graphemes": result.graphemes,
            "newline_style": result.newline_style,
            "normalization": result.normalization,
            "summary": result.summary,
        }),
        Some("text_fingerprint"),
    )
    .with_tool("text_fingerprint")
}

// ---------------------------------------------------------------------------
// text_hash
// ---------------------------------------------------------------------------

pub fn text_hash(args: &Value) -> ToolResponse {
    let text = match _require_str(args, "text", "text_hash") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let algorithms = match args.get("algorithms") {
        Some(Value::Array(arr)) => {
            if arr.len() > 10 {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!("algorithms list length {} exceeds 10", arr.len()),
                    None,
                    Some("text_hash"),
                );
            }
            let non_str_indices: Vec<usize> = arr
                .iter()
                .enumerate()
                .filter(|(_, v)| !v.is_string())
                .map(|(i, _)| i)
                .collect();
            if !non_str_indices.is_empty() {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    "All algorithms must be strings",
                    Some(vec![format!(
                        "Non-string items at indices: {:?}",
                        &non_str_indices[..5.min(non_str_indices.len())]
                    )]),
                    Some("text_hash"),
                );
            }
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        }
        None => vec!["sha256".to_string()],
        _ => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                &format!(
                    "algorithms must be a list, got {}",
                    json_type_name(args.get("algorithms").unwrap_or(&Value::Null))
                ),
                None,
                Some("text_hash"),
            )
        }
    };
    let encoding = args
        .get("encoding")
        .and_then(|v| v.as_str())
        .unwrap_or("utf-8");
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    if !["summary", "normal", "full"].contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec!["Use one of: summary, normal, full".to_string()]),
            Some("text_hash"),
        );
    }

    let valid_encodings = [
        "utf-8",
        "utf8",
        "ascii",
        "us-ascii",
        "latin-1",
        "latin1",
        "iso-8859-1",
        "utf-16",
        "utf16",
        "utf-16-le",
        "utf-16-be",
        "utf-32",
        "utf32",
        "utf-32-le",
        "utf-32-be",
        "cp1252",
        "windows-1252",
        "shift_jis",
        "shiftjis",
        "cp932",
        "euc-jp",
        "euc_jp",
        "gb2312",
        "gbk",
        "gb18030",
        "big5",
        "euc-kr",
        "euc_kr",
        "iso-8859-2",
        "iso-8859-3",
        "iso-8859-4",
        "iso-8859-5",
        "iso-8859-6",
        "iso-8859-7",
        "iso-8859-8",
        "iso-8859-9",
        "iso-8859-10",
        "iso-8859-13",
        "iso-8859-14",
        "iso-8859-15",
        "iso-8859-16",
        "koi8-r",
        "koi8_u",
        "cp866",
        "cp874",
    ];
    let enc_lower = encoding.to_lowercase().replace(['-', '_'], "");
    let known = valid_encodings
        .iter()
        .any(|e| e.replace(['-', '_'], "") == enc_lower);
    if !known {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Invalid encoding: {}", encoding),
            Some(vec![
                "Use a valid encoding name like 'utf-8', 'ascii', 'latin-1'".to_string(),
            ]),
            Some("text_hash"),
        );
    }

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds MAX_TEXT_LENGTH {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("text_hash"),
        );
    }

    let result: TextHashResult = crate::text::text_hash(text, &algorithms, encoding);

    if detail == "summary" {
        ToolResponse::success(
            serde_json::json!({
                "summary": result.summary,
            }),
            Some("text_hash"),
        )
        .with_tool("text_hash")
    } else {
        ToolResponse::success(
            serde_json::json!({
                "encoding": result.encoding,
                "bytes": result.bytes,
                "codepoints": result.codepoints,
                "hashes": result.hashes,
                "warnings": result.warnings,
                "summary": result.summary,
            }),
            Some("text_hash"),
        )
        .with_tool("text_hash")
    }
}

// ---------------------------------------------------------------------------
// text_position
// ---------------------------------------------------------------------------

pub fn text_position(args: &Value) -> ToolResponse {
    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'text' parameter",
                None,
                Some("text_position"),
            )
        }
    };
    let byte_offset = args
        .get("byte_offset")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let codepoint_index = args
        .get("codepoint_index")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let line = args
        .get("line")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let column = args
        .get("column")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let utf16_offset = args
        .get("utf16_offset")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let line_base = args.get("line_base").and_then(|v| v.as_u64()).unwrap_or(1) as usize;
    let column_base = args
        .get("column_base")
        .and_then(|v| v.as_u64())
        .unwrap_or(1) as usize;
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    let valid_details = ["summary", "normal", "full"];
    if !valid_details.contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec![format!("Use one of: {}", valid_details.join(", "))]),
            Some("text_position"),
        );
    }

    if line_base != 0 && line_base != 1 {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("line_base must be 0 or 1, got {}", line_base),
            Some(vec!["Set line_base to 0 or 1".to_string()]),
            Some("text_position"),
        );
    }
    if column_base != 0 && column_base != 1 {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("column_base must be 0 or 1, got {}", column_base),
            Some(vec!["Set column_base to 0 or 1".to_string()]),
            Some("text_position"),
        );
    }

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds MAX_TEXT_LENGTH {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("text_position"),
        );
    }

    let result: TextPositionResult = crate::text::position::text_position(
        text,
        byte_offset,
        codepoint_index,
        line,
        column,
        utf16_offset,
        line_base,
        column_base,
    );

    if !result.valid {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            result.error.as_deref().unwrap_or("Invalid position"),
            None,
            Some("text_position"),
        );
    }

    let output = if detail == "summary" {
        serde_json::json!({
            "summary": result.summary,
        })
    } else {
        serde_json::json!({
            "valid": result.valid,
            "byte_offset": result.byte_offset,
            "codepoint_index": result.codepoint_index,
            "utf16_offset": result.utf16_offset,
            "line": result.line,
            "column": result.column,
            "line_base": result.line_base,
            "column_base": result.column_base,
            "char": result.char,
            "codepoint": result.codepoint,
            "name": result.name,
            "line_text_preview": result.line_text_preview,
            "error": result.error,
            "summary": result.summary,
        })
    };

    ToolResponse::success(output, Some("text_position")).with_tool("text_position")
}

// ---------------------------------------------------------------------------
// text_window
// ---------------------------------------------------------------------------

pub fn text_window(args: &Value) -> ToolResponse {
    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'text' parameter",
                None,
                Some("text_window"),
            )
        }
    };
    let position = match args.get("position").and_then(|v| v.as_object()) {
        Some(obj) => obj.clone(),
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'position' parameter",
                None,
                Some("text_window"),
            )
        }
    };
    let context_lines = match args.get("context_lines") {
        Some(v) => {
            if let Some(n) = v.as_i64() {
                if n < 0 {
                    return ToolResponse::error_with_code(
                        "invalid_arguments",
                        machine_codes::INVALID_ARGUMENTS,
                        &format!("context_lines must be non-negative, got {}", n),
                        Some(vec!["Set context_lines to 0 or higher".to_string()]),
                        Some("text_window"),
                    );
                }
                if n > 10000 {
                    return ToolResponse::error_with_code(
                        "invalid_arguments",
                        machine_codes::INVALID_ARGUMENTS,
                        &format!("context_lines {} exceeds 10000", n),
                        None,
                        Some("text_window"),
                    );
                }
                n as usize
            } else {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!(
                        "context_lines must be an integer, got {}",
                        json_type_name(v)
                    ),
                    None,
                    Some("text_window"),
                );
            }
        }
        None => 2,
    };
    let include_visible_repr = args
        .get("include_visible_repr")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds MAX_TEXT_LENGTH {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("text_window"),
        );
    }

    // Validate position kind
    let valid_kinds = [
        "byte_offset",
        "codepoint_index",
        "grapheme_index",
        "line_column",
    ];
    let pos_kind = position
        .get("kind")
        .and_then(|v| v.as_str())
        .unwrap_or("codepoint_index");
    if !valid_kinds.contains(&pos_kind) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unknown position kind: {}", pos_kind),
            Some(vec![format!("Use one of: {}", valid_kinds.join(", "))]),
            Some("text_window"),
        );
    }

    // Validate integer fields in position
    let max_pos = MAX_TEXT_LENGTH * 16;
    for key in &[
        "value",
        "byte_offset",
        "codepoint_index",
        "grapheme_index",
        "line",
        "column",
    ] {
        if let Some(v) = position.get(*key) {
            if let Some(n) = v.as_i64() {
                if n < 0 || n as usize > max_pos {
                    return ToolResponse::error_with_code(
                        "invalid_arguments",
                        machine_codes::INVALID_ARGUMENTS,
                        &format!("position.{}={} out of range [0, {}]", key, n, max_pos),
                        None,
                        Some("text_window"),
                    );
                }
            } else if v.is_boolean() {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!(
                        "position.{} must be an integer, got {}",
                        key,
                        if v.as_bool().unwrap() {
                            "boolean (true)"
                        } else {
                            "boolean (false)"
                        }
                    ),
                    None,
                    Some("text_window"),
                );
            } else {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!(
                        "position.{} must be an integer, got {}",
                        key,
                        json_type_name(v)
                    ),
                    None,
                    Some("text_window"),
                );
            }
        }
    }

    let pos_value = position
        .get("value")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let pos_byte_offset = position
        .get("byte_offset")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let pos_codepoint_index = position
        .get("codepoint_index")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let pos_grapheme_index = position
        .get("grapheme_index")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let pos_line = position
        .get("line")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let pos_column = position
        .get("column")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let pos_line_base = position
        .get("line_base")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);
    let pos_column_base = position
        .get("column_base")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize);

    let text_position = TextWindowPosition {
        kind: pos_kind.to_string(),
        value: pos_value,
        byte_offset: pos_byte_offset,
        codepoint_index: pos_codepoint_index,
        grapheme_index: pos_grapheme_index,
        line: pos_line,
        column: pos_column,
        line_base: pos_line_base,
        column_base: pos_column_base,
    };

    let result: TextWindowResult = crate::text::position::text_window(
        text,
        &text_position,
        context_lines,
        include_visible_repr,
    );

    ToolResponse::success(
        serde_json::json!({
            "position": result.position,
            "line_text": result.line_text,
            "line_visible_repr": result.line_visible_repr,
            "before": result.before,
            "after": result.after,
            "newline_style": result.newline_style,
            "at_codepoint": result.at_codepoint,
            "warnings": result.warnings,
        }),
        Some("text_window"),
    )
    .with_tool("text_window")
}

// ---------------------------------------------------------------------------
// text_transform
// ---------------------------------------------------------------------------

pub fn text_transform(args: &Value) -> ToolResponse {
    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'text' parameter",
                None,
                Some("text_transform"),
            )
        }
    };
    let operations = match args.get("operations").and_then(|v| v.as_array()) {
        Some(arr) => {
            let mut ops = Vec::new();
            for (i, v) in arr.iter().enumerate() {
                match v.as_str() {
                    Some(s) => ops.push(s.to_string()),
                    None => {
                        return ToolResponse::error_with_code(
                            "invalid_arguments",
                            machine_codes::INVALID_ARGUMENTS,
                            &format!(
                                "operations list items must be strings, operation {} is {}",
                                i,
                                json_type_name(v)
                            ),
                            None,
                            Some("text_transform"),
                        )
                    }
                }
            }
            ops
        }
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'operations' parameter",
                None,
                Some("text_transform"),
            )
        }
    };
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds MAX_TEXT_LENGTH {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("text_transform"),
        );
    }

    if operations.len() > 100 {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!(
                "operations list too large ({} items, max 100)",
                operations.len()
            ),
            Some(vec!["Maximum 100 operations allowed per call".to_string()]),
            Some("text_transform"),
        );
    }

    let valid_operations = [
        "normalize_nfc",
        "normalize_nfd",
        "normalize_nfkc",
        "normalize_nfkd",
        "casefold",
        "trim",
        "trim_trailing_whitespace",
        "normalize_newlines_lf",
        "ensure_final_newline",
        "strip_final_newline",
        "remove_zero_width",
        "remove_bidi_controls",
        "visible_repr",
    ];
    let mut unknown_ops: Vec<String> = Vec::new();
    for op in &operations {
        if !valid_operations.contains(&op.to_lowercase().as_str()) {
            unknown_ops.push(op.clone());
        }
    }
    if !unknown_ops.is_empty() {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unknown operation(s): {}", unknown_ops.join(", ")),
            Some(vec![format!(
                "Valid operations: {}",
                valid_operations.join(", ")
            )]),
            Some("text_transform"),
        );
    }

    if !["summary", "normal", "full"].contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec!["Use one of: summary, normal, full".to_string()]),
            Some("text_transform"),
        );
    }

    let result: TextTransformResult = crate::text::text_transform(text, &operations);

    let output = match detail {
        "summary" => serde_json::json!({
            "changed": result.changed,
            "text": result.text,
            "operations_applied": result.operations_applied,
            "warnings": result.warnings,
            "summary": result.summary,
        }),
        _ => serde_json::json!({
            "changed": result.changed,
            "text": result.text,
            "operations_applied": result.operations_applied,
            "removed": result.removed,
            "warnings": result.warnings,
            "summary": result.summary,
        }),
    };

    ToolResponse::success(output, Some("text_transform")).with_tool("text_transform")
}

// ---------------------------------------------------------------------------
// text_replace_check
// ---------------------------------------------------------------------------

pub fn text_replace_check_tool(args: &Value) -> ToolResponse {
    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'text' parameter",
                None,
                Some("text_replace_check"),
            )
        }
    };
    let old = match args.get("old").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'old' parameter",
                None,
                Some("text_replace_check"),
            )
        }
    };
    let new = match args.get("new").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'new' parameter",
                None,
                Some("text_replace_check"),
            )
        }
    };
    let mode = args.get("mode").and_then(|v| v.as_str()).unwrap_or("exact");
    let expected_count = args
        .get("expected_count")
        .and_then(|v| v.as_u64())
        .map(|n| n as usize);
    let allow_multiple = args
        .get("allow_multiple")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let newline_policy = args
        .get("newline_policy")
        .and_then(|v| v.as_str())
        .unwrap_or("preserve");
    let return_preview = args
        .get("return_preview")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    let max_preview_chars = if let Some(v) = args.get("max_preview_chars") {
        if let Some(n) = v.as_i64() {
            if n < 0 {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!("max_preview_chars must be non-negative, got {}", n),
                    None,
                    Some("text_replace_check"),
                );
            }
            n as usize
        } else {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "max_preview_chars must be an integer",
                None,
                Some("text_replace_check"),
            );
        }
    } else {
        2000
    };

    const MAX_PREVIEW_CHARS: usize = 100_000;
    if max_preview_chars > MAX_PREVIEW_CHARS {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!(
                "max_preview_chars {} exceeds {}",
                max_preview_chars, MAX_PREVIEW_CHARS
            ),
            None,
            Some("text_replace_check"),
        );
    }

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("Text exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_replace_check"),
        );
    }

    if old.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("old exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_replace_check"),
        );
    }

    if new.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("new exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_replace_check"),
        );
    }

    let valid_modes = ["exact", "nfc", "nfkc", "casefold", "whitespace_collapse"];
    if !valid_modes.contains(&mode) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported mode: {}", mode),
            Some(vec![format!("Use one of: {}", valid_modes.join(", "))]),
            Some("text_replace_check"),
        );
    }

    let valid_newline_policies = ["preserve", "normalize_lf", "normalize_crlf"];
    if !valid_newline_policies.contains(&newline_policy) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported newline_policy: {}", newline_policy),
            Some(vec![format!(
                "Use one of: {}",
                valid_newline_policies.join(", ")
            )]),
            Some("text_replace_check"),
        );
    }

    match crate::text::replace::text_replace_check(
        text,
        old,
        new,
        mode,
        expected_count,
        allow_multiple,
        newline_policy,
        return_preview,
        max_preview_chars,
    ) {
        Ok(result) => ToolResponse::success(
            serde_json::json!({
                "match_count": result.match_count,
                "unique_match": result.unique_match,
                "expected_count_met": result.expected_count_met,
                "would_change": result.would_change,
                "positions": result.positions,
                "changed_text_fingerprint": result.changed_text_fingerprint,
                "newline_style_before": result.newline_style_before,
                "newline_style_after": result.newline_style_after,
                "preview_before": result.preview_before,
                "preview_after": result.preview_after,
                "findings": result.findings,
            }),
            Some("text_replace_check"),
        )
        .with_tool("text_replace_check"),
        Err(e) => ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &e,
            None,
            Some("text_replace_check"),
        ),
    }
}

// ---------------------------------------------------------------------------
// text_security_inspect
// ---------------------------------------------------------------------------

pub fn text_security_inspect(args: &Value) -> ToolResponse {
    let budget_ctx = crate::mcp::budget::for_handler(crate::mcp::budget::ToolBudget::HEAVY);

    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'text' parameter",
                None,
                Some("text_security_inspect"),
            )
        }
    };
    let policy = args
        .get("policy")
        .and_then(|v| v.as_str())
        .unwrap_or("default");
    let normalize = args
        .get("normalize")
        .and_then(|v| v.as_str())
        .unwrap_or("none");
    // `compare_normalized` only affects text_inspect's normalized analysis,
    // which this pipeline does not consume; parsed for compat, ignored.
    let _compare_normalized = args
        .get("compare_normalized")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("summary");

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!("Text exceeds {} bytes", MAX_TEXT_LENGTH),
            None,
            Some("text_security_inspect"),
        );
    }

    let valid_policies = ["default", "source_code", "prompt", "markdown", "identifier"];
    if !valid_policies.contains(&policy) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("policy must be one of: {}", valid_policies.join(", ")),
            None,
            Some("text_security_inspect"),
        );
    }

    let valid_normalizations = ["none", "NFC", "NFD", "NFKC", "NFKD"];
    if !valid_normalizations.contains(&normalize) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported normalize form: {}", normalize),
            Some(vec![format!(
                "Use one of: {}",
                valid_normalizations.join(", ")
            )]),
            Some("text_security_inspect"),
        );
    }

    let valid_details = ["summary", "normal", "full"];
    if !valid_details.contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec![format!("Use one of: {}", valid_details.join(", "))]),
            Some("text_security_inspect"),
        );
    }

    // Typed composition: delegate to the canonical service (no sibling
    // adapter JSON envelopes). `compare_normalized` only affects
    // text_inspect's normalized analysis, which this pipeline does not
    // consume (it reads warnings/invisibles/confusables only), so it is
    // intentionally not forwarded.
    let should_stop = || budget_ctx.should_stop();
    let inspection =
        match crate::services::inspect_text_security(text, policy, normalize, detail, &should_stop)
        {
            Ok(inspection) => inspection,
            Err(_) => {
                return budget_ctx
                    .check_should_stop("text_security_inspect")
                    .unwrap_err();
            }
        };

    let mut subresults: serde_json::Map<String, serde_json::Value> = serde_json::Map::new();
    for (k, v) in inspection.subresults {
        subresults.insert(k, v);
    }
    let mut all_findings: Vec<serde_json::Value> = inspection
        .findings
        .iter()
        .map(|f| {
            finding(
                &f.code,
                &f.severity,
                &f.message,
                f.disposition.as_deref(),
                None,
            )
        })
        .collect();
    // Preserve original ordering: service already emits in pipeline order.
    let mut code_list: Vec<String> = inspection.machine_codes.clone();
    let mut unique_machine_codes: Vec<String> = Vec::new();
    for mc in &code_list {
        if !unique_machine_codes.contains(mc) {
            unique_machine_codes.push(mc.clone());
        }
    }
    let primary_machine_code = inspection.machine_code.clone();
    let response_verdict = inspection.verdict.clone();
    let summary = inspection.summary.clone();
    let recommended_action = inspection.recommended_action.clone();
    let normalized_changed = inspection.normalized_changed;

    // Silence unused-mut for ports that keep the original names.
    let _ = &mut code_list;
    let _ = &mut all_findings;

    let next_tool = if response_verdict != verdict::ALLOW {
        Some(ToolResponse::next_tool(
            "text_diff_explain",
            &recommended_action,
            None,
        ))
    } else {
        None
    };

    let mut result = serde_json::json!({
        "verdict": response_verdict,
        "policy": policy,
        "findings": all_findings,
        "machine_code": primary_machine_code,
        "normalized_changed": normalized_changed,
        "recommended_action": recommended_action,
        "summary": summary,
    });

    // Only include subresults when detail is "normal" or "full" (matching Python)
    if detail == "normal" || detail == "full" {
        result["subresults"] = serde_json::Value::Object(subresults);
    }

    let mut resp = ToolResponse::success(result, Some("text_security_inspect"))
        .with_tool("text_security_inspect");
    resp = resp
        .with_machine_code(&primary_machine_code)
        .with_verdict(&response_verdict);
    if !all_findings.is_empty() {
        resp = resp.with_findings(all_findings);
    }
    if let Some(nt) = next_tool {
        resp = resp.with_recommended_next_tool(nt);
    }
    resp
}

// ---------------------------------------------------------------------------
// escape_text
// ---------------------------------------------------------------------------

pub fn escape_text(args: &Value) -> ToolResponse {
    let text = match _require_str(args, "text", "escape_text") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let mode = match _require_str(args, "mode", "escape_text") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    if !["summary", "normal", "full"].contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec!["Use one of: summary, normal, full".to_string()]),
            Some("escape_text"),
        );
    }

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds MAX_TEXT_LENGTH {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("escape_text"),
        );
    }

    let valid_modes = [
        "html_text",
        "json_string",
        "markdown_code_block",
        "markdown_inline_code",
        "posix_shell_single",
        "python_string",
        "regex_literal",
        "rust_string",
        "url_component",
    ];
    if !valid_modes.contains(&mode) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported escape mode: {}", mode),
            Some(vec![format!("Valid modes: {}", valid_modes.join(", "))]),
            Some("escape_text"),
        );
    }

    match crate::text::escape_text(text, mode) {
        Ok(result) => {
            if detail == "summary" {
                ToolResponse::success(
                    serde_json::json!({
                        "mode": result.mode,
                        "changed": result.changed,
                        "summary": result.summary,
                    }),
                    Some("escape_text"),
                )
                .with_tool("escape_text")
            } else {
                ToolResponse::success(
                    serde_json::json!({
                        "mode": result.mode,
                        "escaped": result.escaped,
                        "changed": result.changed,
                        "summary": result.summary,
                    }),
                    Some("escape_text"),
                )
                .with_tool("escape_text")
            }
        }
        Err(e) => ToolResponse::error_with_code(
            "internal_error",
            machine_codes::INTERNAL_ERROR,
            &e,
            None,
            Some("escape_text"),
        ),
    }
}

// ---------------------------------------------------------------------------
// unescape_text
// ---------------------------------------------------------------------------

pub fn unescape_text(args: &Value) -> ToolResponse {
    let text = match _require_str(args, "text", "unescape_text") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let mode = match _require_str(args, "mode", "unescape_text") {
        Ok(s) => s,
        Err(e) => return *e,
    };
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    if !["summary", "normal", "full"].contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec!["Use one of: summary, normal, full".to_string()]),
            Some("unescape_text"),
        );
    }

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds MAX_TEXT_LENGTH {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("unescape_text"),
        );
    }

    let valid_modes = [
        "json_string",
        "python_string",
        "unicode_escape",
        "url_component",
    ];
    if !valid_modes.contains(&mode) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported unescape mode: {}", mode),
            Some(vec![format!("Valid modes: {}", valid_modes.join(", "))]),
            Some("unescape_text"),
        );
    }

    let result: UnescapeTextResult = crate::text::unescape_text(text, mode);
    if detail == "summary" {
        ToolResponse::success(
            serde_json::json!({
                "mode": result.mode,
                "changed": result.changed,
                "error": result.error,
                "summary": result.summary,
            }),
            Some("unescape_text"),
        )
        .with_tool("unescape_text")
    } else {
        ToolResponse::success(
            serde_json::json!({
                "mode": result.mode,
                "unescaped": result.unescaped,
                "changed": result.changed,
                "error": result.error,
                "summary": result.summary,
            }),
            Some("unescape_text"),
        )
        .with_tool("unescape_text")
    }
}

// ---------------------------------------------------------------------------
// line_range_extract_tool
// ---------------------------------------------------------------------------

pub fn line_range_extract_tool(args: &Value) -> ToolResponse {
    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                &format!(
                    "text must be a string, got {}",
                    json_type_name(args.get("text").unwrap_or(&Value::Null))
                ),
                None,
                Some("line_range_extract"),
            )
        }
    };
    let start_line = match require_non_negative_int_arg(args, "start_line", "line_range_extract") {
        Ok(value) => value,
        Err(response) => return *response,
    };
    let end_line = match require_non_negative_int_arg(args, "end_line", "line_range_extract") {
        Ok(value) => value,
        Err(response) => return *response,
    };
    if let Err(response) = validate_line_range_order(start_line, end_line, "line_range_extract") {
        return *response;
    }
    let line_base = args.get("line_base").and_then(|v| v.as_u64()).unwrap_or(1) as usize;
    let include_line_numbers = args
        .get("include_line_numbers")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let include_fingerprint = args
        .get("include_fingerprint")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    let char_count = text.chars().count();
    if char_count > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "text length {} exceeds MAX_TEXT_LENGTH {}",
                char_count, MAX_TEXT_LENGTH
            ),
            Some(vec![format!(
                "Maximum input length is {} characters",
                MAX_TEXT_LENGTH
            )]),
            Some("line_range_extract"),
        );
    }

    match crate::text::line_range::line_range_extract(
        text,
        start_line,
        end_line,
        line_base,
        include_line_numbers,
        include_fingerprint,
    ) {
        Ok(result) => ToolResponse::success(
            serde_json::json!({
                "line_count_total": result.line_count_total,
                "start_line": result.start_line,
                "end_line": result.end_line,
                "valid_range": result.valid_range,
                "text": result.text,
                "lines": result.lines,
                "byte_start": result.byte_start,
                "byte_end": result.byte_end,
                "char_start": result.char_start,
                "char_end": result.char_end,
                "newline_style": result.newline_style,
                "ends_with_newline": result.ends_with_newline,
                "fingerprint": result.fingerprint,
                "findings": result.findings,
            }),
            Some("line_range_extract"),
        )
        .with_tool("line_range_extract"),
        Err(e) => ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &e,
            None,
            Some("line_range_extract"),
        ),
    }
}

// ---------------------------------------------------------------------------
// line_range_compare_tool
// ---------------------------------------------------------------------------

pub fn line_range_compare_tool(args: &Value) -> ToolResponse {
    let left_text = match args.get("left_text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                &format!(
                    "left_text must be a string, got {}",
                    json_type_name(args.get("left_text").unwrap_or(&Value::Null))
                ),
                None,
                Some("line_range_compare"),
            )
        }
    };
    let right_text = match args.get("right_text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                &format!(
                    "right_text must be a string, got {}",
                    json_type_name(args.get("right_text").unwrap_or(&Value::Null))
                ),
                None,
                Some("line_range_compare"),
            )
        }
    };
    let start_line = match require_non_negative_int_arg(args, "start_line", "line_range_compare") {
        Ok(value) => value,
        Err(response) => return *response,
    };
    let end_line = match require_non_negative_int_arg(args, "end_line", "line_range_compare") {
        Ok(value) => value,
        Err(response) => return *response,
    };
    if let Err(response) = validate_line_range_order(start_line, end_line, "line_range_compare") {
        return *response;
    }
    let line_base = args.get("line_base").and_then(|v| v.as_u64()).unwrap_or(1) as usize;
    let comparison_mode = args
        .get("comparison_mode")
        .and_then(|v| v.as_str())
        .unwrap_or("exact");

    for (label, t) in [("left_text", left_text), ("right_text", right_text)] {
        let char_count = t.chars().count();
        if char_count > MAX_TEXT_LENGTH {
            return ToolResponse::error_with_code(
                "input_too_large",
                machine_codes::INPUT_TOO_LARGE,
                &format!(
                    "{} length {} exceeds MAX_TEXT_LENGTH {}",
                    label, char_count, MAX_TEXT_LENGTH
                ),
                Some(vec![format!(
                    "Maximum input length is {} characters",
                    MAX_TEXT_LENGTH
                )]),
                Some("line_range_compare"),
            );
        }
    }

    let valid_modes = ["exact", "ignore_trailing_whitespace", "normalize_newlines"];
    if !valid_modes.contains(&comparison_mode) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported comparison_mode: {}", comparison_mode),
            Some(vec![format!("Use one of: {}", valid_modes.join(", "))]),
            Some("line_range_compare"),
        );
    }

    match crate::text::line_range::line_range_compare(
        left_text,
        right_text,
        start_line,
        end_line,
        line_base,
        comparison_mode,
    ) {
        Ok(result) => ToolResponse::success(
            serde_json::json!({
                "equal": result.equal,
                "left_fingerprint": result.left_fingerprint,
                "right_fingerprint": result.right_fingerprint,
                "diff_summary": result.diff_summary,
                "first_difference": result.first_difference,
            }),
            Some("line_range_compare"),
        )
        .with_tool("line_range_compare"),
        Err(e) => ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &e,
            None,
            Some("line_range_compare"),
        ),
    }
}

// ---------------------------------------------------------------------------
// prompt_input_inspect_tool
// ---------------------------------------------------------------------------

/// Build the wire-format `summary` string for `prompt_input_inspect` from a
/// list of typed-core findings. Pure projection of `PromptInspectResult`
/// fields — no semantic interpretation.
fn prompt_inspect_wire_summary(findings: &[serde_json::Value], risk_score: i64) -> String {
    if findings.is_empty() {
        return "No red flags detected.".to_string();
    }
    let mut code_counts: std::collections::BTreeMap<String, usize> =
        std::collections::BTreeMap::new();
    let mut error_count = 0usize;
    let mut warn_count = 0usize;
    let mut info_count = 0usize;
    for f in findings {
        let code = f
            .get("code")
            .and_then(|v| v.as_str())
            .unwrap_or("UNKNOWN")
            .to_string();
        *code_counts.entry(code).or_insert(0) += 1;
        match f.get("severity").and_then(|v| v.as_str()).unwrap_or("info") {
            "error" => error_count += 1,
            "warn" => warn_count += 1,
            _ => info_count += 1,
        }
    }
    let codes: Vec<String> = code_counts
        .iter()
        .map(|(code, n)| format!("{} {}", n, code))
        .collect();
    let mut sev_parts: Vec<String> = Vec::new();
    if error_count > 0 {
        sev_parts.push(format!("{} error", error_count));
    }
    if warn_count > 0 {
        sev_parts.push(format!("{} warn", warn_count));
    }
    if info_count > 0 {
        sev_parts.push(format!("{} info", info_count));
    }
    format!(
        "{} finding(s): {}. Severity: {}. Risk score: {}.",
        findings.len(),
        codes.join(", "),
        sev_parts.join(", "),
        risk_score
    )
}

pub fn prompt_input_inspect_tool(args: &Value) -> ToolResponse {
    let text = match args.get("text").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'text' parameter",
                None,
                Some("prompt_input_inspect"),
            )
        }
    };

    if text.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "Input length {} exceeds MAX_TEXT_LENGTH {}",
                text.chars().count(),
                MAX_TEXT_LENGTH
            ),
            None,
            Some("prompt_input_inspect"),
        );
    }

    // Parse checks parameter.
    let valid_check_names: &[&str] = &[
        "unicode_hidden",
        "bidi",
        "html_comments",
        "markdown_links",
        "ansi_escapes",
        "terminal_controls",
        "base64_like_blobs",
        "instruction_phrases",
        "long_minified_lines",
    ];
    let all_check_set: std::collections::HashSet<&str> =
        valid_check_names.iter().copied().collect();

    let active_checks: Vec<String> = match args.get("checks") {
        Some(Value::Array(arr)) => {
            let mut set: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
            let mut invalid: Vec<&str> = Vec::new();
            for v in arr {
                if let Some(s) = v.as_str() {
                    if !all_check_set.contains(s) {
                        invalid.push(s);
                    } else {
                        set.insert(s.to_string());
                    }
                }
            }
            if !invalid.is_empty() {
                return ToolResponse::error_with_code(
                    "invalid_arguments",
                    machine_codes::INVALID_ARGUMENTS,
                    &format!("Unknown check(s): {}", invalid.join(", ")),
                    Some(vec![format!(
                        "Valid checks: {}",
                        valid_check_names.join(", ")
                    )]),
                    Some("prompt_input_inspect"),
                );
            }
            set.into_iter().collect()
        }
        None => valid_check_names.iter().map(|s| s.to_string()).collect(),
        _ => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "checks must be a list of strings",
                None,
                Some("prompt_input_inspect"),
            );
        }
    };

    // Parse phrase_patterns parameter.
    let phrase_patterns: Option<Vec<String>> = match args.get("phrase_patterns") {
        Some(Value::Array(arr)) => {
            let patterns: Vec<String> = arr
                .iter()
                .map(|v| match v.as_str() {
                    Some(s) => s.to_string(),
                    None => v.to_string(),
                })
                .collect();
            if patterns.len() > MAX_LIST_ITEMS {
                return ToolResponse::error_with_code(
                    "input_too_large",
                    machine_codes::INPUT_TOO_LARGE,
                    &format!(
                        "phrase_patterns count {} exceeds MAX_LIST_ITEMS {}",
                        patterns.len(),
                        MAX_LIST_ITEMS
                    ),
                    None,
                    Some("prompt_input_inspect"),
                );
            }
            Some(patterns)
        }
        None => None,
        _ => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                &format!(
                    "phrase_patterns must be a list, got {}",
                    json_type_name(args.get("phrase_patterns").unwrap())
                ),
                None,
                Some("prompt_input_inspect"),
            );
        }
    };

    // Delegate semantic inspection to the typed core. The typed
    // `PromptInspectResult::recommended_next_tool` (single preferred) is
    // preserved for Rust callers; the MCP/tool wire uses the separate legacy
    // compatibility projection (null/string/array, historical order,
    // duplicates preserved) derived from the same findings.
    let result = prompt_input_inspect(text, Some(&active_checks), phrase_patterns.as_deref());
    let findings = result.findings;
    let risk_score = result.risk_score;
    let summary = prompt_inspect_wire_summary(&findings, risk_score);
    // Historical wire candidates in fixed order; the adapter only serializes.
    let wire_candidates = prompt_wire_recommendations(&findings);
    let recommended_next_tool_json = match wire_candidates.len() {
        0 => serde_json::Value::Null,
        1 => serde_json::Value::String(wire_candidates[0].clone()),
        _ => serde_json::Value::Array(
            wire_candidates
                .iter()
                .map(|s| serde_json::Value::String(s.clone()))
                .collect(),
        ),
    };

    let result_json = serde_json::json!({
        "findings": findings.clone(),
        "summary": summary,
        "risk_score": risk_score,
        "recommended_next_tool": recommended_next_tool_json,
        "text_length": result.text_length,
        "checks_run": result.checks_run,
        "findings_truncated": result.findings_truncated,
    });

    // Determine machine_code and envelope findings.
    let has_findings = !findings.is_empty();
    let codes: std::collections::HashSet<String> = findings
        .iter()
        .filter_map(|f| f.get("code").and_then(|v| v.as_str()).map(String::from))
        .collect();

    let mut machine_code: Option<String> = None;
    if has_findings {
        if codes
            .iter()
            .any(|c| c == "HIDDEN_CHAR" || c == "BIDI_CONTROL" || c == "ANSI_ESCAPE")
        {
            machine_code = Some(machine_codes::PROMPT_HIDDEN_CONTENT.to_string());
        } else {
            machine_code = Some(machine_codes::PROMPT_HAS_FLAGS.to_string());
        }
    }

    let envelope_findings: Vec<serde_json::Value> = findings
        .iter()
        .map(|f| {
            serde_json::json!({
                "code": f.get("code").and_then(|v| v.as_str()).unwrap_or("UNKNOWN"),
                "severity": f.get("severity").and_then(|v| v.as_str()).unwrap_or("info"),
                "message": f.get("message").and_then(|v| v.as_str()).unwrap_or(""),
                "span": f.get("span"),
                "details": f.get("details"),
            })
        })
        .collect();

    let mut resp = ToolResponse::success(result_json, Some("prompt_input_inspect"))
        .with_tool("prompt_input_inspect");

    if !envelope_findings.is_empty() {
        resp = resp.with_findings(envelope_findings);
    }
    if let Some(code) = machine_code {
        resp = resp.with_machine_code(&code);
    }
    if !recommended_next_tool_json.is_null() {
        resp = resp.with_recommended_next_tool(recommended_next_tool_json.clone());
    }
    resp
}