tokenix 0.65.1

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

use crate::chunker::count_tokens;
use crate::store::{log_hook_event, HookEvent};

mod shell;

const POST_HOOK_TOOLS: &[&str] = &["Bash", "ListDirectory"];
const BASH_MAX_LINES: usize = 100;
const BASH_HEAD_LINES: usize = 40;
const BASH_TAIL_LINES: usize = 15;

/// Bash-aware compression: checks user TOML filters first, then built-in heuristics.
const UNFILTERED_LOG_CAP: u64 = 262_144; // 256 KB before rotation

fn log_unfiltered_cmd(cmd: &str) {
    let cmd = cmd.trim();
    // Skip empties and multi-line commands (for-loops, heredocs): they log one
    // entry per line and pollute the "what to filter next" signal with fragments
    // like `for`, `done`, and stray regex pieces rather than real command names.
    if cmd.is_empty() || cmd.contains('\n') {
        return;
    }
    // Write to the global ~/.tokenix/ dir, not the project dir, to avoid
    // accidentally committing internal tokenix logs.
    let log_path = match crate::store::global_dir() {
        Some(h) => h.join("unfiltered_cmds.log"),
        None => return,
    };
    if let Some(parent) = log_path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    // Rotate at the cap so the log cannot grow unbounded (one generation kept).
    if std::fs::metadata(&log_path).is_ok_and(|m| m.len() >= UNFILTERED_LOG_CAP) {
        let _ = std::fs::rename(&log_path, log_path.with_extension("log.1"));
    }
    // Shell input is where credentials actually live (`curl -H "Authorization:
    // Bearer …"`, `psql postgres://user:pw@host`). The failure tee already
    // masks them before writing; this log did not, and it is long-lived.
    let entry = format!("{}\n", crate::conversation_audit::redact_credentials(cmd));
    use std::io::Write;
    if let Ok(mut f) = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_path)
    {
        let _ = f.write_all(entry.as_bytes());
        crate::store::restrict_to_owner(&log_path);
    }
}

pub fn compress_bash_output(cmd: &str, s: &str) -> String {
    compress_bash_output_for_stream(cmd, s, false, None)
}

fn compress_bash_output_for_stream(
    cmd: &str,
    s: &str,
    is_stderr: bool,
    exit_ok: Option<bool>,
) -> String {
    // User-defined TOML filters take priority over built-in heuristics.
    let user_filters = crate::filters::load_filter_groups_for_command(cmd);
    if let Some(f) = crate::filters::find_filter_ranked(cmd, &user_filters) {
        if is_stderr && !f.filter_stderr {
            return compress_output(s);
        }
        // The global ceiling applies to filtered output too: a filter's own caps
        // are per-filter, and a passthrough fallback (`passthrough_when_emptied`,
        // failure-signal passthrough, `never_worse`) can hand back the full raw
        // output, which is exactly the shape this cap exists to bound.
        return enforce_token_budget(&crate::filters::apply_filter_with_exit(s, f, exit_ok));
    }

    // No filter matched — record for later analysis (tokenix filter list).
    log_unfiltered_cmd(cmd);

    let raw_lines: Vec<&str> = s.lines().collect();
    if is_status_poll_output(&raw_lines) {
        let out = compress_status_poll(&raw_lines);
        if out.len() < s.len() {
            // Early return still owes the global ceiling: this path skips
            // `compress_output`, where `enforce_token_budget` normally runs.
            return enforce_token_budget(&out);
        }
    }

    let base = compress_output(s);
    let lines: Vec<&str> = base.lines().collect();

    // `cargo metadata`: a single huge JSON blob compact_json cannot shrink — summarize
    // it to package count + workspace members instead of letting 500k tokens through.
    if is_cargo_metadata_command(cmd) {
        let out = compress_cargo_metadata(&base);
        if out.len() < base.len() {
            return out;
        }
    }

    // `cargo tree`: highly repetitive (subtrees repeat, marked `(*)`); collapse to the
    // unique crate set.
    if is_cargo_tree_command(cmd) {
        let out = compress_cargo_tree(&lines);
        if out.len() < base.len() {
            return out;
        }
    }

    // Plain grep: strip the indentation in matched content and group by file.
    if is_grep_command(cmd) {
        let out = compress_grep(&lines);
        if out.len() < base.len() {
            return out;
        }
    }

    // ps: keep the header + busiest processes by %CPU.
    if is_ps_command(cmd) {
        let out = compress_ps(&lines);
        if out.len() < base.len() {
            return out;
        }
    }

    // Cargo: always try (it filters signal from noise regardless of total length)
    if is_cargo_output(&lines) {
        let cargo_out = compress_cargo(&lines);
        if cargo_out.len() < base.len() {
            return cargo_out;
        }
    }

    if is_path_listing_command(cmd) {
        let listing_out = compress_path_listing(&lines);
        if listing_out.len() < base.len() {
            return listing_out;
        }
    }

    if is_git_status_command(cmd) {
        let status_out = compress_git_status(&lines);
        if status_out.len() < base.len() {
            return status_out;
        }
    }

    if is_git_log_command(cmd) || is_git_log(&lines) {
        let log_out = compress_git_log(&lines);
        if log_out.len() < base.len() {
            return log_out;
        }
    }

    if is_git_diff_command(cmd) {
        let diff_out = compress_git_diff(&lines);
        if diff_out.len() < base.len() {
            return diff_out;
        }
    }

    // Kubernetes: compress kubectl output
    if is_kubectl_command(cmd) {
        let kube_out = compress_kubectl(&lines);
        if kube_out.len() < base.len() {
            return kube_out;
        }
    }

    // npm/yarn/pnpm/bun: compress package manager output
    if is_pkg_manager_command(cmd) {
        let pkg_out = compress_pkg_manager(&lines);
        if pkg_out.len() < base.len() {
            return pkg_out;
        }
    }

    // Terraform: compress terraform output
    if is_terraform_command(cmd) {
        let tf_out = compress_terraform(&lines);
        if tf_out.len() < base.len() {
            return tf_out;
        }
    }

    // Docker compose: compress compose output
    if is_docker_compose_command(cmd) {
        let dc_out = compress_docker_compose(&lines);
        if dc_out.len() < base.len() {
            return dc_out;
        }
    }

    // Make/ninja/cmake: compress build system output
    if is_build_command(cmd) {
        let build_out = compress_build(&lines);
        if build_out.len() < base.len() {
            return build_out;
        }
    }

    // Generic: aggressive truncation for very long output
    if lines.len() > BASH_MAX_LINES * 2 {
        return aggressive_truncate(&lines);
    }

    if lines.len() <= BASH_MAX_LINES {
        return base;
    }

    truncate_head_tail(&lines, BASH_HEAD_LINES, BASH_TAIL_LINES)
}

fn is_path_listing_command(cmd: &str) -> bool {
    let trimmed = cmd.trim();
    // Plain ls / ls with flags / recursive find
    trimmed == "ls"
        || trimmed == "ls -R"
        || trimmed.starts_with("ls ")
        // POSIX find
        || trimmed.starts_with("find ")
        // Windows cmd / PowerShell dir
        || trimmed == "dir"
        || trimmed.starts_with("dir ")
        // PowerShell Get-ChildItem and its aliases
        || trimmed.starts_with("Get-ChildItem")
        || trimmed.starts_with("get-childitem")
        || trimmed == "gci"
        || trimmed.starts_with("gci ")
        // tree command (Unix and Windows)
        || trimmed == "tree"
        || trimmed.starts_with("tree ")
}

fn is_cargo_output(lines: &[&str]) -> bool {
    lines.iter().take(50).any(|l| {
        let t = l.trim();
        t.starts_with("Compiling ")
            || t.starts_with("Finished ")
            || t.starts_with("error[E")
            || t.contains("test result:")
    })
}

fn compress_cargo(lines: &[&str]) -> String {
    let mut out: Vec<&str> = Vec::new();
    let mut in_diagnostic = false;
    // Inside a test-failure stdout block (`---- <test> stdout ----` up to the
    // `test result:` summary). The reason a test failed — a custom panic message,
    // a pretty-assertion colour diff, a backtrace — is free-form and matches no
    // fixed prefix, so the block is captured verbatim instead of line-by-line.
    let mut in_failure_block = false;
    let mut warning_count: u32 = 0;
    const MAX_WARNINGS: u32 = 5;

    for line in lines {
        let t = line.trim();

        if !in_failure_block && t.starts_with("---- ") && t.ends_with("----") {
            in_failure_block = true;
        }
        if in_failure_block {
            out.push(line);
            // `test result:` ends the failure section; a `running ` line marks the
            // start of a fresh test binary, so it also closes a truncated block.
            if t.starts_with("test result:") || t.starts_with("running ") {
                in_failure_block = false;
            }
            continue;
        }

        let is_error = (t.starts_with("error[") || t == "error" || t.starts_with("error: "))
            && !t.starts_with("error_");
        let is_warning = t.starts_with("warning[") || t.starts_with("warning: ");
        let is_context = t.starts_with("-->")
            || (t.starts_with('|') && t.len() > 1)
            || t.starts_with("= note:")
            || t.starts_with("= help:")
            || t.starts_with("help:");
        let is_summary = t.starts_with("Finished ")
            || t.starts_with("error: aborting")
            || t.contains("test result:")
            || t.starts_with("running ")
            || t.starts_with("FAILED")
            || (t.starts_with("test ") && (t.ends_with("ok") || t.ends_with("FAILED")));
        // A panic outside a cargo-test stdout block (e.g. a plain binary run)
        // still carries the failure reason and must survive compression.
        let is_panic = t.contains("panicked at");

        if is_error || is_panic {
            out.push(line);
            in_diagnostic = true;
        } else if is_warning && warning_count < MAX_WARNINGS {
            out.push(line);
            in_diagnostic = true;
            warning_count += 1;
        } else if is_context && in_diagnostic {
            out.push(line);
        } else if is_summary {
            out.push(line);
            in_diagnostic = false;
        } else {
            in_diagnostic = false;
        }
    }

    if warning_count >= MAX_WARNINGS {
        out.push("  ... (additional warnings omitted)");
    }

    out.join("\n")
}

fn is_git_log(lines: &[&str]) -> bool {
    lines.iter().take(5).any(|l| l.starts_with("commit "))
}

fn is_git_log_command(cmd: &str) -> bool {
    let cmd = cmd.trim();
    cmd == "git log" || cmd.starts_with("git log ")
}

fn is_git_status_command(cmd: &str) -> bool {
    let cmd = cmd.trim();
    cmd == "git status" || cmd.starts_with("git status ")
}

fn is_git_diff_command(cmd: &str) -> bool {
    let cmd = cmd.trim();
    cmd == "git diff" || cmd.starts_with("git diff ")
}

fn compress_git_log(lines: &[&str]) -> String {
    let oneline: Vec<&str> = lines
        .iter()
        .map(|line| line.trim())
        .filter(|line| {
            line.len() > 8
                && line.chars().take_while(|c| c.is_ascii_hexdigit()).count() >= 7
                && line.chars().nth(7).is_some_and(|c| c.is_whitespace())
        })
        .collect();
    if oneline.len() >= 3 {
        let first = oneline.first().copied().unwrap_or_default();
        let last = oneline.last().copied().unwrap_or_default();
        return format!(
            "git log: {} commits\nfirst: {first}\nlast: {last}",
            oneline.len()
        );
    }

    const MAX_COMMITS: usize = 20;
    let mut commit_count: usize = 0;
    let mut keep_until: usize = 0;

    for (i, line) in lines.iter().enumerate() {
        if line.starts_with("commit ") {
            commit_count += 1;
            if commit_count > MAX_COMMITS {
                break;
            }
        }
        keep_until = i + 1;
    }

    if keep_until >= lines.len() {
        return lines.join("\n");
    }
    let omitted = lines.len() - keep_until;
    format!(
        "{}\n[... {} more lines omitted (>{} commits)]",
        lines[..keep_until].join("\n"),
        omitted,
        MAX_COMMITS
    )
}

/// Compact `git status` (verbose or `--short`/porcelain) to one terse
/// `CODE path` line per change (`M`/`A`/`D`/`R`/`T`/`C`/`??`). Verbose
/// `modified:   file` lines are far longer than the porcelain form, so this
/// rewrites them; porcelain lines pass through; the prose/headers are dropped.
fn compress_git_status(lines: &[&str]) -> String {
    const VERBOSE: &[(&str, &str)] = &[
        ("modified:", "M"),
        ("new file:", "A"),
        ("deleted:", "D"),
        ("renamed:", "R"),
        ("typechange:", "T"),
        ("copied:", "C"),
        ("both modified:", "U"),
    ];
    let mut out: Vec<String> = Vec::new();
    let mut in_untracked = false;
    for line in lines {
        let t = line.trim();
        if t.is_empty() {
            continue;
        }
        // Section markers from verbose output.
        if t.starts_with("Untracked files") {
            in_untracked = true;
            continue;
        }
        // Hint lines like `(use "git add"...)` sit inside a section — skip them
        // without changing the section state (untracked filenames follow).
        if t.starts_with('(') {
            continue;
        }
        if t.starts_with("Changes ")
            || t.starts_with("On branch")
            || t.starts_with("Your branch")
            || t.starts_with("no changes")
            || t.starts_with("nothing to commit")
            || t.contains("working tree clean")
        {
            in_untracked = false;
            continue;
        }
        // Verbose "modified:   file" → "M file".
        if let Some((_, code)) = VERBOSE.iter().find(|(kw, _)| t.starts_with(kw)) {
            let file = t.split_once(':').map(|x| x.1).unwrap_or("").trim();
            out.push(format!("{code} {file}"));
            continue;
        }
        // Porcelain ("?? file", " M file", "MM file"): already terse — keep.
        let code2: String = t.chars().take(2).collect();
        let is_porcelain = code2 == "??"
            || code2
                .chars()
                .all(|c| matches!(c, 'M' | 'A' | 'D' | 'R' | 'C' | 'U' | 'T' | ' '))
                && code2 != "  ";
        if is_porcelain && t.len() >= 3 {
            out.push(t.to_string());
            continue;
        }
        // Bare filename under "Untracked files:".
        if in_untracked {
            out.push(format!("?? {t}"));
        }
    }
    if out.is_empty() {
        return "git status: clean".to_string();
    }
    out.join("\n")
}

fn compress_git_diff(lines: &[&str]) -> String {
    let files = lines
        .iter()
        .filter_map(|line| line.strip_prefix("diff --git "))
        .count();
    let hunks = lines.iter().filter(|line| line.starts_with("@@")).count();
    let additions = lines
        .iter()
        .filter(|line| line.starts_with('+') && !line.starts_with("+++"))
        .count();
    let deletions = lines
        .iter()
        .filter(|line| line.starts_with('-') && !line.starts_with("---"))
        .count();
    let mut keep = Vec::new();
    for line in lines {
        if line.starts_with("diff --git ")
            || line.starts_with("@@")
            || line.starts_with("+++")
            || line.starts_with("---")
        {
            keep.push(*line);
        }
        if keep.len() >= 40 {
            break;
        }
    }
    if files == 0 && hunks == 0 {
        return lines.join("\n");
    }
    // A header-only summary throws away every changed line, which is the whole
    // point of running `git diff`. Only worth it when the diff is big enough
    // that reading it in full is the actual problem.
    const DIFF_SUMMARY_MIN_LINES: usize = 200;
    if lines.len() < DIFF_SUMMARY_MIN_LINES {
        return lines.join("\n");
    }
    format!(
        "git diff: files={files} hunks={hunks} +{additions} -{deletions}\n{}",
        keep.join("\n")
    )
}

fn is_status_poll_output(lines: &[&str]) -> bool {
    lines
        .iter()
        .take(80)
        .filter(|line| {
            let t = line.trim();
            t.starts_with("phase=") || t.starts_with("status=") || t.starts_with("state=")
        })
        .count()
        >= 3
}

fn compress_status_poll(lines: &[&str]) -> String {
    let mut out = Vec::new();
    let mut last_poll: Option<String> = None;
    let mut last_count = 0usize;

    let flush = |out: &mut Vec<String>, last: &mut Option<String>, count: &mut usize| {
        if let Some(value) = last.take() {
            if *count > 1 {
                out.push(format!("{value} x{count}"));
            } else {
                out.push(value);
            }
        }
        *count = 0;
    };

    for line in lines {
        let t = line.trim();
        let is_poll =
            t.starts_with("phase=") || t.starts_with("status=") || t.starts_with("state=");

        if is_poll {
            match &last_poll {
                Some(last) if last == t => {
                    last_count += 1;
                }
                _ => {
                    flush(&mut out, &mut last_poll, &mut last_count);
                    last_poll = Some(t.to_string());
                    last_count = 1;
                }
            }
            continue;
        }

        flush(&mut out, &mut last_poll, &mut last_count);
        out.push(line.to_string());
    }

    flush(&mut out, &mut last_poll, &mut last_count);
    out.join("\n")
}

/// Aggressive truncation for very long output - keeps only errors, head, tail
fn aggressive_truncate(lines: &[&str]) -> String {
    const HEAD_KEEP: usize = 20;
    const TAIL_KEEP: usize = 10;
    const MAX_TOTAL: usize = HEAD_KEEP + TAIL_KEEP + 1;

    if lines.len() <= MAX_TOTAL {
        return lines.join("\n");
    }

    // Priority: error/warning lines
    let mut priority = Vec::new();
    let mut other_head = Vec::new();
    let mut other_tail = Vec::new();

    for (i, line) in lines.iter().enumerate() {
        let t = line.trim();
        let is_priority = t.starts_with("error")
            || t.starts_with("warning")
            || t.starts_with("FAIL")
            || t.starts_with("panic")
            || t.contains("error[")
            || t.contains("warning[")
            || t.contains("ERROR")
            || t.contains("FAILED");

        if is_priority && priority.len() < 50 {
            priority.push(line.to_string());
        } else if i < HEAD_KEEP {
            other_head.push(line.to_string());
        } else if i >= lines.len() - TAIL_KEEP {
            other_tail.push(line.to_string());
        }
    }

    let mut result = Vec::new();
    result.extend(other_head);
    if !priority.is_empty() {
        result.push("[PRIORITY LINES]".to_string());
        result.extend(priority);
    }
    result.push(format!(
        "[... {} lines omitted ...]",
        lines.len() - result.len() - other_tail.len()
    ));
    result.extend(other_tail);
    result.join("\n")
}

fn is_kubectl_command(cmd: &str) -> bool {
    let t = cmd.trim();
    t == "kubectl" || t.starts_with("kubectl ") || t == "k" || t.starts_with("k ")
}

fn compress_kubectl(lines: &[&str]) -> String {
    // kubectl get: tabular - keep header + first/last few rows
    // kubectl describe: verbose - summarize
    // kubectl logs: similar to plain logs
    let first_nonempty = lines.iter().find(|l| !l.trim().is_empty());
    if let Some(first) = first_nonempty {
        let t = first.trim();
        if t.starts_with("NAME") && t.contains("READY") {
            // kubectl get pods/nodes/etc - tabular
            let header = first.to_string();
            let data: Vec<&str> = lines
                .iter()
                .skip(1)
                .filter(|l| !l.trim().is_empty())
                .copied()
                .collect();
            if data.len() <= 20 {
                return lines.join("\n");
            }
            let mut out = vec![header];
            out.extend(data.iter().take(10).map(|s| s.to_string()));
            out.push(format!("... {} more rows ...", data.len() - 10));
            out.extend(data.iter().rev().take(5).rev().map(|s| s.to_string()));
            return out.join("\n");
        }
        if t.starts_with("Name:") || t.starts_with("Namespace:") {
            // kubectl describe: verbose, but the reason anyone runs it is the
            // Events/conditions tail. Replacing it with a fixed placeholder
            // fabricated a response and destroyed exactly that; keep a bounded
            // head/tail view instead.
            return truncate_head_tail(lines, 20, 25);
        }
    }
    lines.join("\n")
}

fn is_pkg_manager_command(cmd: &str) -> bool {
    let t = cmd.trim();
    t.starts_with("npm ")
        || t.starts_with("yarn ")
        || t.starts_with("pnpm ")
        || t.starts_with("bun ")
        || t.starts_with("cargo ")
        || t.starts_with("pip ")
        || t.starts_with("uv ")
        || t.starts_with("composer ")
        || t.starts_with("gem ")
        || t.starts_with("go ")
        || t.starts_with("gradle ")
        || t.starts_with("maven ")
        || t.starts_with("mvn ")
        || t.starts_with("dotnet ")
}

fn compress_pkg_manager(lines: &[&str]) -> String {
    // Generic package manager output: progress bars, downloading, extracting
    // Keep errors, summary, final status
    let mut result = Vec::new();
    let mut in_progress = false;

    for line in lines {
        let t = line.trim();
        if t.is_empty() {
            continue;
        }
        let is_progress = t.starts_with("Progress:")
            || t.starts_with("Downloading")
            || t.starts_with("Extracting")
            || t.starts_with("Installing")
            || t.starts_with("Building")
            || t.starts_with("Compiling")
            || t.starts_with("Resolving")
            || t.starts_with("Fetching")
            || t.contains("█")
            || t.contains("░")
            || t.contains("▓")
            || t.chars().filter(|c| *c == '=' || *c == '>').count() > 10;

        if is_progress {
            if !in_progress {
                result.push("[package operations...]".to_string());
                in_progress = true;
            }
        } else {
            in_progress = false;
            // Case-insensitive: package managers print `ERROR:`, `Error`, and
            // `npm ERR!` — a case-sensitive `starts_with("error")` dropped every
            // one of them once the first 30 lines were used up.
            let lower = t.to_ascii_lowercase();
            let is_important = [
                "error", "err!", "warning", "warn", "fail", "success", "done", "added", "removed",
                "updated", "npm err",
            ]
            .iter()
            .any(|p| lower.starts_with(p))
                || lower.contains("err!")
                || lower.contains("vulnerab")
                || lower.contains("audit");
            if is_important || result.len() < 30 {
                result.push(line.to_string());
            }
        }
    }
    result.join("\n")
}

fn is_terraform_command(cmd: &str) -> bool {
    let t = cmd.trim();
    t.starts_with("terraform ")
}

fn compress_terraform(lines: &[&str]) -> String {
    // Terraform: very verbose, keep plan summary, errors, apply status
    let mut result = Vec::new();
    let mut in_plan = false;

    for line in lines {
        let t = line.trim();
        if t.is_empty() {
            continue;
        }
        if t.starts_with("Plan:")
            || t.starts_with("Apply complete")
            || t.starts_with("Destroy complete")
        {
            result.push(line.to_string());
            continue;
        }
        if t.starts_with("Error:") || t.starts_with("Warning:") {
            result.push(line.to_string());
            continue;
        }
        if t.starts_with("+ ") || t.starts_with("~ ") || t.starts_with("- ") || t.starts_with("/ ")
        {
            if !in_plan {
                result.push("[resource changes...]".to_string());
                in_plan = true;
            }
        } else {
            in_plan = false;
        }
        if result.len() < 40 {
            result.push(line.to_string());
        }
    }
    result.join("\n")
}

fn is_docker_compose_command(cmd: &str) -> bool {
    let t = cmd.trim();
    t.starts_with("docker-compose ") || t.starts_with("docker compose ")
}

fn compress_docker_compose(lines: &[&str]) -> String {
    // Docker compose: container status, logs
    let mut result = Vec::new();
    for line in lines {
        let t = line.trim();
        if t.is_empty() {
            continue;
        }
        if t.starts_with("Creating")
            || t.starts_with("Starting")
            || t.starts_with("Stopping")
            || t.starts_with("Removing")
            || t.starts_with("Building")
            || t.starts_with("Pulling")
        {
            result.push("[container ops...]".to_string());
        } else if t.starts_with("error")
            || t.starts_with("Error")
            || t.starts_with("FAIL")
            || result.len() < 30
        {
            result.push(line.to_string());
        }
    }
    result.join("\n")
}

fn is_build_command(cmd: &str) -> bool {
    let t = cmd.trim();
    t.starts_with("make ")
        || t.starts_with("ninja ")
        || t.starts_with("cmake ")
        || t.starts_with("bazel ")
        || t.starts_with("sbt ")
        || t.starts_with("mvn ")
        || t.starts_with("gradle ")
        || t.starts_with("cargo build")
        || t.starts_with("cargo test")
        || t.starts_with("go build")
        || t.starts_with("go test")
        || t.starts_with("dotnet build")
        || t.starts_with("dotnet test")
}

fn compress_build(lines: &[&str]) -> String {
    // Build systems: lots of "Compiling", "Building", "Linking" lines
    // Keep errors, warnings, final status
    let mut result = Vec::new();
    let mut in_compile = false;

    for line in lines {
        let t = line.trim();
        if t.is_empty() {
            continue;
        }
        let is_compile = t.starts_with("Compiling")
            || t.starts_with("Building")
            || t.starts_with("Linking")
            || t.starts_with("Generating")
            || t.starts_with("Running")
            || t.starts_with("CC ")
            || t.starts_with("CXX ")
            || t.starts_with("LD ")
            || t.starts_with("AR ")
            || t.starts_with("[")
                && (t.contains("%")
                    || t.contains("/") && t.chars().filter(|c| c.is_ascii_digit()).count() > 3);

        if is_compile {
            if !in_compile {
                result.push("[build steps...]".to_string());
                in_compile = true;
            }
        } else {
            in_compile = false;
            let is_important = t.starts_with("error")
                || t.starts_with("warning")
                || t.starts_with("FAIL")
                || t.starts_with("Error")
                || t.starts_with("Warning")
                || t.starts_with("Finished")
                || t.starts_with("Built")
                || t.starts_with("Build complete")
                || t.starts_with("Build failed")
                || t.contains("test result:");
            if is_important || result.len() < 50 {
                result.push(line.to_string());
            }
        }
    }
    result.join("\n")
}

fn compress_path_listing(lines: &[&str]) -> String {
    let paths: Vec<&str> = lines
        .iter()
        .map(|line| line.trim())
        .filter(|line| {
            !line.is_empty()
                && !line.ends_with(':')
                && !line.contains(" -> ")
                && (line.contains('/') || line.contains('\\'))
        })
        .collect();
    if paths.len() < 4 {
        return lines.join("\n");
    }

    let mut counts = std::collections::BTreeMap::<String, usize>::new();
    for path in paths {
        let normalized = path.replace('\\', "/");
        let dir = normalized
            .rsplit_once('/')
            .map(|(dir, _)| dir)
            .unwrap_or(".")
            .to_string();
        *counts.entry(dir).or_insert(0) += 1;
    }

    let total_files: usize = counts.values().sum();
    // Collapse to top-level directories (first path component) and show only the
    // busiest few — a full per-leaf-dir listing balloons on deep trees (e.g. a
    // `find .` that descends into target/). This keeps the high-signal shape.
    let mut top: std::collections::BTreeMap<String, usize> = std::collections::BTreeMap::new();
    for (dir, count) in &counts {
        let head = dir
            .trim_start_matches("./")
            .split('/')
            .next()
            .filter(|s| !s.is_empty())
            .unwrap_or(".")
            .to_string();
        *top.entry(head).or_insert(0) += count;
    }
    let mut ranked: Vec<(String, usize)> = top.into_iter().collect();
    ranked.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
    const MAX_DIRS: usize = 8;
    let mut out = vec![format!(
        "{} files across {} top-level dir(s):",
        total_files,
        ranked.len()
    )];
    for (dir, count) in ranked.iter().take(MAX_DIRS) {
        out.push(format!("{}/ ({})", dir, count));
    }
    if ranked.len() > MAX_DIRS {
        out.push(format!("... +{} more dir(s)", ranked.len() - MAX_DIRS));
    }
    out.join("\n")
}

fn is_cargo_metadata_command(cmd: &str) -> bool {
    cmd.contains("cargo metadata")
}

/// Summarize `cargo metadata` (a single multi-hundred-KB JSON blob) to the package
/// count + workspace members. The full transitive metadata is almost never the
/// signal an agent needs, and it otherwise passes through uncompressed.
fn compress_cargo_metadata(s: &str) -> String {
    let Ok(v) = serde_json::from_str::<serde_json::Value>(s.trim()) else {
        return s.to_string();
    };
    let n_pkgs = v
        .get("packages")
        .and_then(|p| p.as_array())
        .map(|a| a.len())
        .unwrap_or(0);
    let members: Vec<String> = v
        .get("workspace_members")
        .and_then(|m| m.as_array())
        .map(|a| {
            a.iter()
                .filter_map(|m| m.as_str())
                .map(|id| id.split([' ', '@']).next().unwrap_or(id).to_string())
                .collect()
        })
        .unwrap_or_default();
    let root = v
        .get("resolve")
        .and_then(|r| r.get("root"))
        .and_then(|r| r.as_str())
        .map(|id| id.split([' ', '@']).next().unwrap_or(id).to_string());
    let mut out = format!("cargo metadata: {n_pkgs} packages in the dependency graph");
    if !members.is_empty() {
        out.push_str(&format!("\nworkspace members: {}", members.join(", ")));
    }
    if let Some(root) = root {
        out.push_str(&format!("\nroot: {root}"));
    }
    out
}

fn is_cargo_tree_command(cmd: &str) -> bool {
    let t = cmd.trim();
    t == "cargo tree" || t.starts_with("cargo tree ")
}

/// Collapse `cargo tree` to its unique crate set. The tree repeats whole subtrees
/// (marked `(*)`) and draws box characters; the useful signal is which crates are
/// in the graph.
fn compress_cargo_tree(lines: &[&str]) -> String {
    let mut crates: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    for line in lines {
        // Strip leading tree-drawing characters and whitespace.
        let stripped = line.trim_start_matches([
            ' ', '|', '`', '+', '-', '\u{2502}', '\u{251c}', '\u{2514}', '\u{2500}',
        ]);
        let stripped = stripped.trim();
        if stripped.is_empty() {
            continue;
        }
        // A crate line looks like "name v1.2.3" or "name v1.2.3 (proc-macro)".
        let mut it = stripped.split_whitespace();
        if let (Some(name), Some(ver)) = (it.next(), it.next()) {
            if ver.starts_with('v') && name.chars().next().is_some_and(|c| c.is_ascii_alphabetic())
            {
                crates.insert(format!("{name} {ver}"));
            }
        }
    }
    if crates.is_empty() {
        return lines.join("\n");
    }
    const MAX_SHOWN: usize = 15;
    let total = crates.len();
    let shown: Vec<String> = crates.into_iter().take(MAX_SHOWN).collect();
    let suffix = if total > MAX_SHOWN {
        format!(" (+{} more)", total - MAX_SHOWN)
    } else {
        String::new()
    };
    format!(
        "cargo tree: {} unique crates\n{}{}",
        total,
        shown.join(", "),
        suffix
    )
}

fn is_ps_command(cmd: &str) -> bool {
    let t = cmd.trim();
    t == "ps" || t.starts_with("ps ")
}

/// Compact `ps` output to the header + the busiest processes by %CPU. A raw
/// `ps aux` is hundreds of lines dominated by idle kernel threads; the signal is
/// what is actually consuming the machine.
fn compress_ps(lines: &[&str]) -> String {
    const TOP: usize = 4;
    const WIDTH: usize = 85;
    let trunc = |l: &str| -> String {
        if l.chars().count() > WIDTH {
            format!("{}…", l.chars().take(WIDTH).collect::<String>())
        } else {
            l.to_string()
        }
    };
    let nonempty: Vec<&str> = lines
        .iter()
        .copied()
        .filter(|l| !l.trim().is_empty())
        .collect();
    if nonempty.len() <= TOP + 1 {
        return nonempty
            .iter()
            .map(|l| trunc(l))
            .collect::<Vec<_>>()
            .join("\n");
    }
    let header = nonempty[0];
    // Locate the CPU column from the header instead of assuming index 2: that
    // holds for `ps aux` (USER PID %CPU …) but not for `ps -ef` (UID PID PPID C
    // …) or a custom `ps -eo`, where index 2 is PPID and the "sorted by %CPU"
    // claim was simply false.
    let cpu_col = header
        .split_whitespace()
        .position(|h| h.eq_ignore_ascii_case("%cpu"))
        .or_else(|| {
            header
                .split_whitespace()
                .position(|h| h.eq_ignore_ascii_case("c"))
        });
    let mut rows: Vec<&str> = nonempty[1..].to_vec();
    let sorted = match cpu_col {
        Some(col) => {
            let cpu_of = |l: &str| -> f32 {
                l.split_whitespace()
                    .nth(col)
                    .and_then(|c| c.parse::<f32>().ok())
                    .unwrap_or(0.0)
            };
            rows.sort_by(|a, b| {
                cpu_of(b)
                    .partial_cmp(&cpu_of(a))
                    .unwrap_or(std::cmp::Ordering::Equal)
            });
            true
        }
        None => false,
    };
    let mut out = vec![trunc(header)];
    for r in rows.iter().take(TOP) {
        out.push(trunc(r));
    }
    out.push(format!(
        "... {} more process(es) ({}, top {} shown)",
        rows.len() - TOP,
        if sorted {
            "sorted by CPU"
        } else {
            "original order"
        },
        TOP
    ));
    out.join("\n")
}

fn is_grep_command(cmd: &str) -> bool {
    let t = cmd.trim();
    // Plain grep only — not `git grep` (handled elsewhere) or ripgrep (rg.toml).
    (t == "grep" || t.starts_with("grep ")) && !t.starts_with("grep -V")
}

/// Compact plain grep output: drop blank lines, strip the indentation inside each
/// `path:line:CONTENT` match, and cap the number of lines.
fn compress_grep(lines: &[&str]) -> String {
    const MAX_MATCHES: usize = 50;
    let mut out: Vec<String> = Vec::new();
    let mut shown = 0usize;
    for line in lines {
        if line.trim().is_empty() {
            continue;
        }
        let colons: Vec<usize> = line.match_indices(':').map(|(i, _)| i).collect();
        let mut split_idx = None;
        for &idx in &colons {
            let prev_idx = colons.iter().rev().copied().find(|&p| p < idx);
            let start = prev_idx.map(|p| p + 1).unwrap_or(0);
            let part = &line[start..idx];
            if !part.is_empty() && part.chars().all(|c| c.is_ascii_digit()) {
                split_idx = Some(idx);
                break;
            }
        }
        let split_idx = split_idx.or_else(|| colons.last().copied());

        let compact = match split_idx {
            Some(idx) => {
                let (head, content) = line.split_at(idx + 1);
                format!("{head}{}", content.trim())
            }
            None => line.trim().to_string(),
        };
        if shown >= MAX_MATCHES {
            out.push(format!("... +{} more match line(s)", lines.len() - shown));
            break;
        }
        out.push(compact);
        shown += 1;
    }
    if out.is_empty() {
        return "grep: no matches".to_string();
    }
    out.join("\n")
}

fn truncate_head_tail(lines: &[&str], head: usize, tail: usize) -> String {
    let total = lines.len();
    if total <= head + tail {
        return lines.join("\n");
    }
    let omitted = total - head - tail;
    format!(
        "{}\n[... {} lines omitted ...]\n{}",
        lines[..head].join("\n"),
        omitted,
        lines[total - tail..].join("\n")
    )
}

/// Minimum length of an unbroken base64 run before it is treated as an embedded
/// blob and redacted. Prose, code, hashes, and even minified JS break the base64
/// alphabet with whitespace or punctuation long before this length, so a run this
/// long is effectively always a real payload (embedded image, tar/binary dump).
const BASE64_MIN: usize = 512;

/// Label a base64 run by its decoded magic bytes so the redaction says what was
/// dropped (`png image base64`) instead of an opaque `base64 blob`. Matching is on
/// the base64-encoded prefix — the first few base64 chars deterministically encode
/// the leading file-magic bytes.
fn base64_blob_kind(run: &[u8]) -> &'static str {
    let head = std::str::from_utf8(&run[..run.len().min(16)]).unwrap_or("");
    if head.starts_with("iVBORw0KGgo") {
        "png image base64"
    } else if head.starts_with("/9j/") {
        "jpeg image base64"
    } else if head.starts_with("R0lGOD") {
        "gif image base64"
    } else if head.starts_with("UklGR") {
        "webp/riff base64"
    } else if head.starts_with("JVBERi0") {
        "pdf base64"
    } else if head.starts_with("H4sI") {
        "gzip base64"
    } else {
        "base64 blob"
    }
}

/// A long run of base64-alphabet bytes is only a real blob if it looks like
/// encoded binary — mixed-case letters, or a base64 symbol (`+ / = - _`). Pure-hex
/// or all-digit runs (checksum manifests, id lists, hex dumps) are spared: real
/// base64 of binary is mixed-case/symbol-rich with overwhelming probability, so
/// this avoids silently eating a list of sha256 hashes or a numeric column.
fn looks_like_base64(run: &[u8]) -> bool {
    // `-` and `_` are base64url characters, but they are also how humans join
    // words: a long kebab/snake identifier chain (`build-step-1-step-2-...`,
    // a joined list of slugs) hit this and was silently redacted. Real base64
    // payloads do not decompose into many short separator-delimited groups, so
    // reject that shape before anything else.
    let separator_groups = run
        .split(|b| matches!(b, b'-' | b'_'))
        .filter(|g| !g.is_empty())
        .count();
    if separator_groups >= 4 {
        let avg_group = run.len() / separator_groups;
        if avg_group < 12 {
            return false;
        }
    }

    let mut has_lower = false;
    let mut has_upper = false;
    for &b in run {
        match b {
            // `+ / =` cannot appear in an identifier, so they are proof on their
            // own. `-` and `_` are NOT: treating them as proof made the very
            // first hyphen of a lowercase kebab chain end the scan with "base64"
            // before the mixed-case test could spare it.
            b'+' | b'/' | b'=' => return true,
            b'a'..=b'z' => has_lower = true,
            b'A'..=b'Z' => has_upper = true,
            _ => {}
        }
        if has_lower && has_upper {
            return true;
        }
    }
    false
}

/// Redact long base64 / data-URI blobs (embedded PNGs, tar/binary dumps) that
/// otherwise replay tens of thousands of tokens of pure noise into context. A
/// `data:<mime>;base64,` prefix is preserved (its `:`/`;`/`,` break the run), so
/// only the payload is dropped. Operates on bytes but only ever removes ASCII
/// base64 runs, so the result stays valid UTF-8.
fn strip_base64_blobs(s: &str) -> String {
    if s.len() < BASE64_MIN {
        return s.to_string();
    }
    let is_b64 = |b: u8| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'/' | b'=' | b'-' | b'_');
    let bytes = s.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    let mut changed = false;
    while i < bytes.len() {
        if is_b64(bytes[i]) {
            let start = i;
            while i < bytes.len() && is_b64(bytes[i]) {
                i += 1;
            }
            let run = i - start;
            let slice = &bytes[start..i];
            if run >= BASE64_MIN && looks_like_base64(slice) {
                let kind = base64_blob_kind(slice);
                out.extend_from_slice(format!("[{kind} omitted: {run} chars]").as_bytes());
                changed = true;
            } else {
                out.extend_from_slice(slice);
            }
        } else {
            out.push(bytes[i]);
            i += 1;
        }
    }
    if !changed {
        return s.to_string();
    }
    String::from_utf8(out).unwrap_or_else(|_| s.to_string())
}

/// Minimum encoded width of an interior line and minimum consecutive-line count for
/// a *wrapped* base64 block (MIME/PEM/`base64`/`openssl` wrap at 64–76 cols, which
/// the contiguous scanner misses because each line is under `BASE64_MIN`).
const WRAP_MIN_LINE: usize = 60;
const WRAP_MIN_LINES: usize = 5;

/// Collapse line-wrapped base64 blocks (PEM certs/keys, `base64 file`, binary git
/// patches) that `strip_base64_blobs` cannot see because the newline every ~76
/// chars keeps each run under `BASE64_MIN`. A block is ≥`WRAP_MIN_LINES` consecutive
/// pure-base64 lines of width ≥`WRAP_MIN_LINE` (plus one optional shorter padded
/// tail line). PEM `-----BEGIN/END-----` markers contain spaces/dashes-with-spaces
/// so they are not base64 lines and survive, keeping the block's context.
fn strip_wrapped_base64(s: &str) -> String {
    if s.len() < BASE64_MIN {
        return s.to_string();
    }
    let is_b64_line = |t: &str| {
        !t.is_empty()
            && t.bytes()
                .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'/' | b'=' | b'-' | b'_'))
    };
    let trailing = s.ends_with('\n');
    let lines: Vec<&str> = s.trim_end_matches('\n').split('\n').collect();
    let mut out: Vec<String> = Vec::with_capacity(lines.len());
    let mut i = 0;
    let mut changed = false;
    while i < lines.len() {
        let t = lines[i].trim();
        if is_b64_line(t) && t.len() >= WRAP_MIN_LINE {
            let start = i;
            let mut chars = 0usize;
            while i < lines.len()
                && is_b64_line(lines[i].trim())
                && lines[i].trim().len() >= WRAP_MIN_LINE
            {
                chars += lines[i].trim().len();
                i += 1;
            }
            // The final padded chunk of a real block is a shorter line, but a normal
            // all-alnum word (`done`, `trailer`) also qualifies — absorbing it would
            // eat legit text. Leave it: a single ≤76-char line is negligible tokens.
            let count = i - start;
            if count >= WRAP_MIN_LINES
                && chars >= BASE64_MIN
                && looks_like_base64(lines[start].trim().as_bytes())
            {
                let kind = base64_blob_kind(lines[start].trim().as_bytes());
                out.push(format!(
                    "[{kind} omitted: {chars} chars across {count} lines]"
                ));
                changed = true;
            } else {
                for line in &lines[start..i] {
                    out.push((*line).to_string());
                }
            }
        } else {
            out.push(lines[i].to_string());
            i += 1;
        }
    }
    if !changed {
        return s.to_string();
    }
    let mut joined = out.join("\n");
    if trailing {
        joined.push('\n');
    }
    joined
}

/// Redact both single-line/data-URI and line-wrapped base64 blobs.
fn redact_base64_blobs(s: &str) -> String {
    strip_wrapped_base64(&strip_base64_blobs(s))
}

/// The line terminator that dominates `s`.
///
/// Every `\r\n` also contains a `\n`, so CRLF dominates when it accounts for
/// more than half of all newlines.
pub(crate) fn dominant_eol(s: &str) -> &'static str {
    let crlf = s.matches("\r\n").count();
    let lf = s.matches('\n').count();
    if crlf > 0 && crlf * 2 > lf {
        "\r\n"
    } else {
        "\n"
    }
}

/// Restore `eol` over output that the line pipeline has normalized to LF.
///
/// Every transform in this crate splits with `.lines()` — which consumes `\r\n`
/// and `\n` alike — and rejoins with `"\n"`. On a CRLF input that silently hands
/// the agent a copy where *every* line ending differs from the bytes on disk. If
/// the agent then quotes those lines into an exact-match edit (SEARCH/REPLACE,
/// `str_replace`), the anchor cannot match: measured elsewhere as successful
/// patch application dropping from 27/40 to 15/40 when compression rewrote the
/// bytes an edit tool has to reproduce. Windows is this project's primary
/// platform, so this is the common case, not the corner case.
pub(crate) fn restore_eol(out: String, eol: &str) -> String {
    if eol != "\r\n" || out.is_empty() {
        return out;
    }
    // Normalize first so a partially-CRLF result cannot become `\r\r\n`.
    out.replace("\r\n", "\n").replace('\n', "\r\n")
}

pub fn compress_output(s: &str) -> String {
    let eol = dominant_eol(s);
    restore_eol(compress_output_inner(s), eol)
}

fn compress_output_inner(s: &str) -> String {
    // Redact embedded base64 blobs first: a data URI inside a JSON string would
    // otherwise survive JSON compaction at full weight.
    let stripped = redact_base64_blobs(s);
    let s: &str = &stripped;

    // JSON compaction first: if output is pure JSON or NDJSON, compact and return early.
    // The other transforms (ANSI, emoji, blank lines) don't apply to JSON.
    let compacted = compact_json(s);
    if compacted != s {
        return enforce_token_budget(&compacted);
    }
    let s = strip_ansi(s);
    let s = remove_emojis(&s);
    let s = collapse_blank_lines(&s);
    let s = group_repeated_blocks(&s);
    let s = group_repeated_lines(&s);

    // Additional generic aggressive compression
    enforce_token_budget(&generic_aggressive_compress(&s))
}

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

    #[test]
    fn crlf_survives_the_compression_pipeline() {
        // Regression: `.lines()` + `join("\n")` laundered CRLF into LF, so every
        // line the agent got back differed from disk by one byte.
        let src = "fn main() {\r\n    println!(\"hi\");\r\n}\r\n";
        let out = compress_output(src);
        assert!(
            out.contains("\r\n"),
            "CRLF input must come back CRLF, got {out:?}"
        );
        assert!(
            !out.replace("\r\n", "").contains('\n'),
            "no bare LF may survive in a CRLF payload: {out:?}"
        );
    }

    #[test]
    fn lf_input_stays_lf() {
        let src = "fn main() {\n    println!(\"hi\");\n}\n";
        assert!(!compress_output(src).contains('\r'));
    }

    #[test]
    fn dominant_eol_picks_the_majority() {
        assert_eq!(dominant_eol("a\r\nb\r\nc\r\n"), "\r\n");
        assert_eq!(dominant_eol("a\nb\nc\n"), "\n");
        assert_eq!(dominant_eol(""), "\n");
        // Mixed endings: a lone CRLF in a mostly-LF payload must not flip it.
        assert_eq!(dominant_eol("a\nb\nc\r\nd\n"), "\n");
    }

    #[test]
    fn restore_eol_is_idempotent() {
        let once = restore_eol("a\nb\n".to_string(), "\r\n");
        assert_eq!(once, "a\r\nb\r\n");
        assert_eq!(restore_eol(once.clone(), "\r\n"), once);
    }
}

/// Absolute ceiling on how many tokens any single compressed output may cost.
/// The line-based caps (`generic_aggressive_compress`) cannot bound output that
/// is few-but-enormous lines (a one-line JSON payload, a minified bundle, a grep
/// hit with megabyte-long lines), and the `compact_json` early return bypasses
/// them entirely — a 3 MB tool result compacted by 10% still costs ~700k tokens.
/// Override with `TOKENIX_MAX_OUTPUT_TOKENS`; `0` disables the cap.
const DEFAULT_MAX_OUTPUT_TOKENS: usize = 8000;

fn max_output_tokens() -> usize {
    std::env::var("TOKENIX_MAX_OUTPUT_TOKENS")
        .ok()
        .and_then(|v| v.trim().parse::<usize>().ok())
        .unwrap_or(DEFAULT_MAX_OUTPUT_TOKENS)
}

/// Clip `s` to the global token budget, keeping a head and a tail window so both
/// the command's opening context and its (usually load-bearing) final lines
/// survive. Char boundaries are respected, so UTF-8 output stays valid.
fn enforce_token_budget(s: &str) -> String {
    let budget = max_output_tokens();
    if budget == 0 || s.is_empty() {
        return s.to_string();
    }
    let tokens = count_tokens(s);
    if tokens <= budget {
        return s.to_string();
    }

    // Convert the token budget into a char budget. `count_tokens` is chars/4
    // rounded up, so this ratio is ~4 by construction — it is written as a
    // division rather than a constant so the conversion stays correct if
    // `count_tokens` ever becomes a real BPE count (roadmap).
    let char_count = s.chars().count();
    let chars_per_token = (char_count as f64 / tokens as f64).max(1.0);
    let char_budget = (budget as f64 * chars_per_token) as usize;
    if char_budget >= char_count {
        return s.to_string();
    }
    let head_chars = char_budget * 3 / 4;
    let tail_chars = char_budget.saturating_sub(head_chars);

    let head_end = byte_index_at_char(s, head_chars);
    let tail_start = byte_index_at_char(s, char_count.saturating_sub(tail_chars));
    if tail_start <= head_end {
        return s.to_string();
    }

    let omitted = tokens.saturating_sub(budget);
    let facts = match preserved_identifiers(&s[head_end..tail_start]) {
        Some(ids) => format!("\n[ids kept from the omitted range: {ids}]"),
        None => String::new(),
    };
    format!(
        "{}\n[... {} tokens omitted by tokenix output cap ({} max; set TOKENIX_MAX_OUTPUT_TOKENS to change) ...]{}\n{}",
        &s[..head_end],
        omitted,
        budget,
        facts,
        &s[tail_start..]
    )
}

/// Identifiers a caller cannot reconstruct or guess: commit SHAs, UUIDs, URLs,
/// and compiler/error codes. Truncating a range that contained the one SHA the
/// agent needed forces a re-run, so they are carried over into the marker.
/// Everything else (prose, repeated log lines) is genuinely re-derivable and is
/// left dropped.
fn preserved_identifiers(omitted: &str) -> Option<String> {
    const MAX_FACTS: usize = 12;
    const MAX_CHARS: usize = 240;

    let mut facts: Vec<&str> = Vec::new();
    for token in omitted.split(|c: char| {
        c.is_whitespace() || matches!(c, '"' | '\'' | ',' | '(' | ')' | '[' | ']' | '{' | '}')
    }) {
        let token = token.trim_matches(|c: char| matches!(c, '.' | ':' | ';' | '`'));
        if token.len() < 5 || facts.len() >= MAX_FACTS || facts.contains(&token) {
            continue;
        }
        if is_identifier_like(token) {
            facts.push(token);
        }
    }
    if facts.is_empty() {
        return None;
    }
    let mut out = String::new();
    for fact in facts {
        if out.len() + fact.len() + 2 > MAX_CHARS {
            break;
        }
        if !out.is_empty() {
            out.push_str(", ");
        }
        out.push_str(fact);
    }
    (!out.is_empty()).then_some(out)
}

fn is_identifier_like(token: &str) -> bool {
    if token.starts_with("http://") || token.starts_with("https://") {
        return true;
    }
    // UUID: 8-4-4-4-12 hex groups.
    let groups: Vec<&str> = token.split('-').collect();
    if groups.len() == 5
        && [8, 4, 4, 4, 12]
            == [
                groups[0].len(),
                groups[1].len(),
                groups[2].len(),
                groups[3].len(),
                groups[4].len(),
            ]
        && groups
            .iter()
            .all(|g| g.chars().all(|c| c.is_ascii_hexdigit()))
    {
        return true;
    }
    // Git-style SHA: 7-40 hex chars, mixed letters and digits so plain numbers
    // and English words ("added", "decade") are not mistaken for one.
    let is_hex = token.len() >= 7
        && token.len() <= 40
        && token.chars().all(|c| c.is_ascii_hexdigit())
        && token.chars().any(|c| c.is_ascii_digit())
        && token.chars().any(|c| c.is_ascii_alphabetic());
    if is_hex {
        return true;
    }
    // Compiler/runtime error codes: E0308, ERR_MODULE_NOT_FOUND, TS2345.
    let code_like = token.chars().next().is_some_and(|c| c.is_ascii_uppercase())
        && token.chars().any(|c| c.is_ascii_digit())
        && token
            .chars()
            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_');
    code_like
}

fn byte_index_at_char(s: &str, char_idx: usize) -> usize {
    s.char_indices()
        .nth(char_idx)
        .map(|(i, _)| i)
        .unwrap_or(s.len())
}

/// Longest block a repeat scan will consider. Loop bodies that spam a context
/// window (a PowerShell exception block, a retry banner, a k8s poll report) are
/// typically a handful of lines; scanning wider costs time for no real gain.
const MAX_REPEAT_BLOCK_LINES: usize = 12;

/// Collapse consecutive repetitions of a multi-line *block*.
///
/// `group_repeated_lines` only sees runs of identical adjacent lines, so a
/// watch/poll loop that re-emits the same 8-line stanza hundreds of times slips
/// through untouched (measured: a single monitoring session cost ~617k tokens
/// this way). A block repeated 3+ times is kept once, annotated with the count.
fn group_repeated_blocks(s: &str) -> String {
    let trailing_newline = s.ends_with('\n');
    let source = if trailing_newline {
        &s[..s.len() - 1]
    } else {
        s
    };
    let lines: Vec<&str> = source.split('\n').collect();
    // Below 6 lines there is nothing a 3x block repeat can be built from; above
    // the ceiling the quadratic-ish scan is not worth it (the token cap and the
    // line caps already bound outputs that large).
    if lines.len() < 6 || lines.len() > 100_000 {
        return s.to_string();
    }

    let mut result = String::with_capacity(s.len());
    let mut i = 0;
    while i < lines.len() {
        let mut collapsed = false;
        // Prefer the smallest repeating unit: a 2-line stanza repeated 6x should
        // report 6, not 3 repeats of a 4-line block.
        for width in 2..=MAX_REPEAT_BLOCK_LINES.min((lines.len() - i) / 2) {
            let block = &lines[i..i + width];
            // A block of identical lines is a run, not a stanza: leave it to
            // `group_repeated_lines`, which reports it as one line + a count
            // instead of an arbitrary N-line window.
            if block.iter().all(|l| *l == block[0]) {
                continue;
            }
            let mut reps = 1;
            while i + (reps + 1) * width <= lines.len()
                && &lines[i + reps * width..i + (reps + 1) * width] == block
            {
                reps += 1;
            }
            if reps >= 3 {
                for line in block {
                    result.push_str(line);
                    result.push('\n');
                }
                result.push_str(&format!(
                    "[block of {} lines repeated {}x]\n",
                    width,
                    reps - 1
                ));
                i += reps * width;
                collapsed = true;
                break;
            }
        }
        if !collapsed {
            result.push_str(lines[i]);
            result.push('\n');
            i += 1;
        }
    }
    if !trailing_newline && result.ends_with('\n') {
        result.pop();
    }
    result
}

fn generic_aggressive_compress(s: &str) -> String {
    let lines: Vec<&str> = s.lines().collect();
    if lines.len() <= 50 {
        return s.to_string();
    }

    // Detect and compress common patterns
    let mut result: Vec<String> = Vec::new();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];

        // Skip progress bars and spinners
        if line.contains("█")
            || line.contains("░")
            || line.contains("▓")
            || line
                .chars()
                .filter(|c| *c == '=' || *c == '>' || *c == '#')
                .count()
                > 15
        {
            if result.is_empty() || !result.last().unwrap().contains("progress") {
                result.push("[progress bar omitted]".to_string());
            }
            i += 1;
            continue;
        }

        // Collapse repeated similar lines (download, extracting, etc.)
        let prefixes = [
            "Downloading ",
            "Extracting ",
            "Installing ",
            "Fetching ",
            "Resolving ",
            "Building ",
            "Compiling ",
            "Generating ",
            "Running ",
            "Testing ",
            "Checking ",
            "Verifying ",
        ];
        let mut matched_prefix = None;
        for prefix in &prefixes {
            if line.starts_with(prefix) {
                matched_prefix = Some(*prefix);
                break;
            }
        }
        if let Some(prefix) = matched_prefix {
            let mut count = 1;
            let mut j = i + 1;
            while j < lines.len() && lines[j].starts_with(prefix) && count < 100 {
                count += 1;
                j += 1;
            }
            if count >= 3 {
                result.push(format!("{} ({} similar lines)", line, count - 1));
                i = j;
                continue;
            }
        }

        result.push(line.to_string());
        i += 1;
    }

    // If still too long, apply head/tail truncation
    if result.len() > 100 {
        const HEAD: usize = 40;
        const TAIL: usize = 20;
        if result.len() > HEAD + TAIL {
            let omitted = result.len() - HEAD - TAIL;
            let mut truncated = result[..HEAD].to_vec();
            truncated.push(format!("[... {} lines omitted ...]", omitted));
            truncated.extend(result[result.len() - TAIL..].to_vec());
            return truncated.join("\n");
        }
    }

    result.join("\n")
}

/// Compact pretty-printed JSON (pure JSON or NDJSON) into single-line form.
/// Returns the original string unchanged if not JSON or if already compact.
fn compact_json(s: &str) -> String {
    let trimmed = s.trim();

    // Case 0: too short to be meaningful JSON
    if trimmed.len() < 2 {
        return s.to_string();
    }

    // Case 1: entire output is a JSON object or array
    if (trimmed.starts_with('{') || trimmed.starts_with('['))
        && serde_json::from_str::<serde::de::IgnoredAny>(trimmed).is_ok()
    {
        let compact = strip_json_whitespace(trimmed);
        if compact.len() < trimmed.len() {
            return if s.ends_with('\n') {
                compact + "\n"
            } else {
                compact
            };
        }
    }

    // Case 2: NDJSON — every non-empty line is a JSON object or array
    let lines: Vec<&str> = trimmed.lines().collect();
    if lines.len() > 1
        && lines.iter().all(|l| {
            let t = l.trim();
            t.is_empty()
                || (t.starts_with('{') && serde_json::from_str::<serde_json::Value>(t).is_ok())
                || (t.starts_with('[') && serde_json::from_str::<serde_json::Value>(t).is_ok())
        })
    {
        let compacted: String = lines
            .iter()
            .filter_map(|l| {
                let t = l.trim();
                if t.is_empty() {
                    return None;
                }
                Some(strip_json_whitespace(t))
            })
            .collect::<Vec<_>>()
            .join("\n");
        let result = if s.ends_with('\n') {
            compacted + "\n"
        } else {
            compacted
        };
        if result.len() < s.len() {
            return result;
        }
    }

    s.to_string()
}

/// Drop insignificant whitespace from *already-validated* JSON text, preserving
/// the bytes that matter. Re-serializing through `serde_json::Value` would be
/// lossy: integers past `u64` degrade to floats in scientific notation,
/// duplicate keys collapse, and key order is rewritten.
fn strip_json_whitespace(json: &str) -> String {
    let mut out = String::with_capacity(json.len());
    let mut in_string = false;
    let mut escaped = false;
    for c in json.chars() {
        if in_string {
            out.push(c);
            if escaped {
                escaped = false;
            } else if c == '\\' {
                escaped = true;
            } else if c == '"' {
                in_string = false;
            }
            continue;
        }
        match c {
            '"' => {
                in_string = true;
                out.push(c);
            }
            c if c.is_ascii_whitespace() => {}
            _ => out.push(c),
        }
    }
    out
}

fn compress_command_streams(
    command_str: &str,
    stdout_raw: &str,
    stderr_raw: &str,
    success: bool,
) -> (String, String) {
    let stdout_compressed = if stdout_raw.trim().is_empty() {
        if stderr_raw.trim().is_empty() && success {
            compress_bash_output_for_stream(command_str, stdout_raw, false, Some(success))
        } else {
            String::new()
        }
    } else {
        compress_bash_output_for_stream(command_str, stdout_raw, false, Some(success))
    };

    let stderr_compressed = if stderr_raw.trim().is_empty() {
        String::new()
    } else {
        compress_bash_output_for_stream(command_str, stderr_raw, true, Some(success))
    };

    (
        keep_final_newline(stdout_raw, stdout_compressed),
        keep_final_newline(stderr_raw, stderr_compressed),
    )
}

/// Filters rejoin lines without a trailing terminator. When the agent's
/// harness merges both streams, a missing one glues this stream's last line
/// onto the other's first (`[... omitted ...]test result: FAILED`).
fn keep_final_newline(raw: &str, mut out: String) -> String {
    if !out.is_empty() && raw.ends_with('\n') && !out.ends_with('\n') {
        out.push_str(if raw.ends_with("\r\n") { "\r\n" } else { "\n" });
    }
    out
}

/// Remove ANSI/VT100 escape sequences (CSI, OSC, and single-char sequences).
pub(crate) fn strip_ansi(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut result: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] != 0x1b {
            result.push(bytes[i]);
            i += 1;
            continue;
        }
        i += 1;
        if i >= bytes.len() {
            break;
        }
        match bytes[i] {
            b'[' => {
                i += 1;
                // CSI: skip until final byte (0x40–0x7E)
                while i < bytes.len() {
                    let b = bytes[i];
                    i += 1;
                    if (0x40..=0x7E).contains(&b) {
                        break;
                    }
                }
            }
            b']' => {
                i += 1;
                // OSC: skip until BEL or ST (ESC \)
                while i < bytes.len() {
                    if bytes[i] == 0x07 {
                        i += 1;
                        break;
                    }
                    if bytes[i] == 0x1b && i + 1 < bytes.len() && bytes[i + 1] == b'\\' {
                        i += 2;
                        break;
                    }
                    i += 1;
                }
            }
            b if b.is_ascii() => {
                i += 1; // single-char sequence: ESC + one ASCII byte
            }
            _ => {
                // ESC followed by a non-ASCII byte is not an escape sequence.
                // Consuming it would split a multi-byte character and make the
                // whole result invalid UTF-8 — which used to blank the entire
                // output via `unwrap_or_default()`. Drop the stray ESC only.
            }
        }
    }
    // ANSI sequences are pure ASCII; remaining bytes are still valid UTF-8.
    // Fall back to lossy rather than an empty string: losing one character is
    // recoverable, losing the whole command output is not.
    match String::from_utf8(result) {
        Ok(s) => s,
        Err(e) => String::from_utf8_lossy(e.as_bytes()).into_owned(),
    }
}

/// Remove emoji characters by unicode code-point range.
fn remove_emojis(s: &str) -> String {
    s.chars().filter(|&c| !is_emoji_char(c)).collect()
}

fn is_emoji_char(c: char) -> bool {
    matches!(c,
        '\u{1F000}'..='\u{1FFFF}' // Emoticons, misc symbols and pictographs, transport, etc.
        | '\u{2600}'..='\u{26FF}' // Misc symbols (☀☁⚡ etc.)
        | '\u{2700}'..='\u{27BF}' // Dingbats (✈✉✔ etc.)
        | '\u{FE00}'..='\u{FE0F}' // Variation selectors (emoji presentation)
        | '\u{200D}'              // Zero-width joiner (emoji combiner)
        | '\u{20E3}'              // Combining enclosing keycap
    )
}

/// Collapse 3+ consecutive newlines down to 2 (one blank line between paragraphs).
fn collapse_blank_lines(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut newline_run = 0usize;
    for c in s.chars() {
        if c == '\n' {
            newline_run += 1;
            if newline_run <= 2 {
                result.push('\n');
            }
        } else {
            newline_run = 0;
            result.push(c);
        }
    }
    result
}

/// Group consecutive identical lines that appear 3+ times into one line + annotation.
/// Lines appearing 1–2 times in a row are left unchanged.
/// Also performs fuzzy grouping for common patterns (e.g., progress bars, file listings).
fn group_repeated_lines(s: &str) -> String {
    let trailing_newline = s.ends_with('\n');
    let source = if trailing_newline {
        &s[..s.len() - 1]
    } else {
        s
    };
    let lines: Vec<&str> = source.split('\n').collect();
    let mut result = String::with_capacity(s.len());
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];

        // 1. Exact match grouping
        let mut end = i + 1;
        while end < lines.len() && lines[end] == line {
            end += 1;
        }
        let count = end - i;
        if count >= 3 {
            result.push_str(line);
            result.push('\n');
            result.push_str(&format!("[repeated {}x]\n", count - 1));
            i = end;
            continue;
        }

        // 2. Fuzzy grouping (similarity)
        if let Some(fuzzy_count) = try_fuzzy_group(&lines, i) {
            if fuzzy_count >= 3 {
                result.push_str(line);
                result.push_str(" ... (and ");
                result.push_str(&(fuzzy_count - 1).to_string());
                result.push_str(" similar lines)\n");
                i += fuzzy_count;
                continue;
            }
        }

        result.push_str(line);
        result.push('\n');
        i += 1;
    }
    if !trailing_newline && result.ends_with('\n') {
        result.pop();
    }
    result
}

fn try_fuzzy_group(lines: &[&str], start: usize) -> Option<usize> {
    let line = lines[start];
    if line.len() < 5 {
        return None;
    }

    // Patterns for fuzzy grouping:
    let prefixes = [
        "Removing ",
        "Compiling ",
        "Installing ",
        "Download ",
        "Extracting ",
        "Checked ",
        "test ",
    ];

    for prefix in prefixes {
        if line.starts_with(prefix) {
            if prefix == "test " && !line.contains(" ... ok") {
                continue;
            }
            let mut count = 1;
            for next_line in lines.iter().skip(start + 1) {
                if next_line.starts_with(prefix) {
                    count += 1;
                } else {
                    break;
                }
            }
            if count >= 3 {
                return Some(count);
            }
        }
    }
    None
}

fn find_repo_root() -> PathBuf {
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    crate::store::find_project_root(&cwd)
}

pub fn now_ts() -> f64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs_f64()
}

/// Extract plain text from a PostToolUse tool_response value.
/// Handles: plain string, {"output": "..."}, and content-array format.
fn extract_response_text(response: &serde_json::Value) -> Option<String> {
    if let Some(s) = response.as_str() {
        return Some(s.to_string());
    }
    if let Some(s) = response["output"].as_str() {
        return Some(s.to_string());
    }
    // Claude Code Bash tool_response: { stdout, stderr, interrupted, ... }.
    let stdout = response["stdout"].as_str().unwrap_or("");
    let stderr = response["stderr"].as_str().unwrap_or("");
    if !stdout.is_empty() || !stderr.is_empty() {
        let mut combined = stdout.to_string();
        if !stderr.is_empty() {
            if !combined.is_empty() {
                combined.push('\n');
            }
            combined.push_str(stderr);
        }
        return Some(combined);
    }
    if let Some(arr) = response["content"].as_array() {
        let text: String = arr
            .iter()
            .filter_map(|item| {
                if item["type"].as_str() == Some("text") {
                    item["text"].as_str().map(str::to_string)
                } else {
                    None
                }
            })
            .collect::<Vec<_>>()
            .join("\n");
        if !text.is_empty() {
            return Some(text);
        }
    }
    None
}

/// Output dialect for a PostToolUse hook, selected by which agent invoked it.
#[derive(Debug, PartialEq)]
enum PostDialect {
    /// Claude Code / Codex: PostToolUse cannot replace or shorten a tool result,
    /// so compression here is a no-op — exit 0 silently without logging savings.
    ClaudeNoop,
    /// GitHub Copilot CLI: print `{"modifiedResult":{...}}` JSON on stdout, exit 0.
    CopilotJson,
}

/// A PostToolUse payload normalized across Claude Code and Copilot CLI formats.
struct PostHookInput {
    tool_name: String,
    command: String,
    text: String,
    dialect: PostDialect,
}

/// Decode Copilot's `toolArgs`, which may arrive as a JSON-encoded string or an object.
fn decode_tool_args(v: &serde_json::Value) -> serde_json::Value {
    match v.as_str() {
        Some(raw) => serde_json::from_str(raw).unwrap_or(serde_json::Value::Null),
        None => v.clone(),
    }
}

/// Map an agent's shell/list tool name onto tokenix's internal POST_HOOK_TOOLS name.
/// Copilot uses `bash`/`powershell`; Claude Code already uses `Bash`/`ListDirectory`.
fn normalize_post_tool(name: &str) -> String {
    match name.to_ascii_lowercase().as_str() {
        "bash"
        | "powershell"
        | "shell"
        | "run_shell_command"
        | "default_api:run_shell_command"
        | "run_command"
        | "default_api:run_command"
        | "get_terminal_output"
        | "default_api:get_terminal_output" => "Bash".to_string(),
        "listdirectory" | "default_api:list_directory" => "ListDirectory".to_string(),
        _ => name.to_string(),
    }
}

/// Extract the LLM-facing text from a Copilot `toolResult` (object or string,
/// camelCase `textResultForLlm` or VS Code snake_case `text_result_for_llm`).
fn extract_copilot_result(tr: &serde_json::Value) -> Option<String> {
    if let Some(s) = tr.as_str() {
        return Some(s.to_string());
    }
    tr["textResultForLlm"]
        .as_str()
        .or_else(|| tr["text_result_for_llm"].as_str())
        .map(str::to_string)
}

/// Normalize a PostToolUse payload. Copilot CLI sends camelCase `toolName`/`toolResult`;
/// Claude Code sends snake_case `tool_name`/`tool_response`.
fn parse_post_input(v: &serde_json::Value) -> Option<PostHookInput> {
    // GitHub Copilot CLI: camelCase toolName + toolResult + (maybe string-encoded) toolArgs.
    if let Some(raw_name) = v["toolName"].as_str() {
        let args = decode_tool_args(&v["toolArgs"]);
        let command = args["command"]
            .as_str()
            .or_else(|| args["CommandLine"].as_str())
            .or_else(|| args["commandLine"].as_str())
            .or_else(|| args["command_line"].as_str())
            .unwrap_or("")
            .to_string();
        return Some(PostHookInput {
            tool_name: normalize_post_tool(raw_name),
            command,
            text: extract_copilot_result(&v["toolResult"])?,
            dialect: PostDialect::CopilotJson,
        });
    }

    // Claude Code / Codex: snake_case tool_name + tool_response.
    let raw_name = v["tool_name"].as_str()?;
    let command = v["tool_input"]["command"]
        .as_str()
        .or_else(|| v["tool_input"]["CommandLine"].as_str())
        .or_else(|| v["tool_input"]["commandLine"].as_str())
        .or_else(|| v["tool_input"]["command_line"].as_str())
        .unwrap_or("")
        .to_string();
    Some(PostHookInput {
        tool_name: normalize_post_tool(raw_name),
        command,
        text: extract_response_text(&v["tool_response"])?,
        dialect: PostDialect::ClaudeNoop,
    })
}

pub fn run_hook_post() -> Result<()> {
    let raw_stdin = std::io::read_to_string(std::io::stdin()).unwrap_or_default();
    let clean = raw_stdin.trim_start_matches('\u{feff}').trim();

    let v: serde_json::Value = match serde_json::from_str(clean) {
        Ok(v) => v,
        Err(_) => std::process::exit(0),
    };

    let input = match parse_post_input(&v) {
        Some(i) if !i.text.is_empty() => i,
        _ => std::process::exit(0),
    };

    // Redaction runs first and unconditionally — on every tool this hook
    // fires on, independent of dialect. This is the path that reaches tool
    // results the PreToolUse Bash rewrite never sees (Read results, MCP
    // results, ListDirectory, ...). Historically Claude/Codex non-shell
    // results exited here immediately on the (stale) belief that Claude Code
    // PostToolUse can never replace a result — as of Claude Code v2.1.121+,
    // `hookSpecificOutput.updatedToolOutput` is honored for all tools, not
    // only MCP, so that early exit was silently skipping real redaction.
    let (redacted, secret_hit) = crate::secrets_scan::redact_known_secrets(&input.text);

    let is_shell = POST_HOOK_TOOLS.contains(&input.tool_name.as_str());
    let compressed = if input.tool_name == "Bash" {
        compress_bash_output(&input.command, &redacted)
    } else if is_shell {
        compress_output(&redacted)
    } else if input.dialect == PostDialect::ClaudeNoop {
        // Non-shell, Claude/Codex dialect: no command-oriented compression
        // heuristics apply — redaction above is the whole job.
        redacted
    } else {
        // Non-shell, Copilot dialect: unchanged behavior, redact embedded
        // base64 blobs — the dominant token waste in agent histories — on
        // top of the secret redaction already applied.
        redact_base64_blobs(&redacted)
    };

    let changed = compressed != input.text;
    if !secret_hit && !changed {
        std::process::exit(0);
    }

    let repo_root = find_repo_root();
    let original_tokens = count_tokens(&input.text) as i64;
    let actual_tokens = count_tokens(&compressed) as i64;

    if secret_hit {
        let _ = log_hook_event(
            &repo_root,
            &HookEvent {
                ts: now_ts(),
                tool: input.tool_name.clone(),
                action: "intercepted".to_string(),
                phase: "post".to_string(),
                reason: "secret-redacted".to_string(),
                saved_tokens: 0,
                actual_tokens,
                original_estimate: original_tokens,
                input_preview: clean.chars().take(200).collect(),
                command: input.command.clone(),
            },
        );
    }
    if changed {
        let _ = log_hook_event(
            &repo_root,
            &HookEvent {
                ts: now_ts(),
                tool: input.tool_name.clone(),
                action: "intercepted".to_string(),
                phase: "post".to_string(),
                reason: "size-compressed".to_string(),
                saved_tokens: (original_tokens - actual_tokens).max(0),
                actual_tokens,
                original_estimate: original_tokens,
                input_preview: clean.chars().take(200).collect(),
                command: input.command.clone(),
            },
        );
    }

    match input.dialect {
        PostDialect::ClaudeNoop => {
            // As of Claude Code v2.1.121+, updatedToolOutput is honored for
            // all tools. Fail open on older installs: the field is simply
            // ignored and the original tool result stands — never a
            // blocking error, never malformed stdout.
            let out = serde_json::json!({
                "hookSpecificOutput": {
                    "hookEventName": "PostToolUse",
                    "updatedToolOutput": compressed,
                }
            });
            println!("{}", serde_json::to_string(&out).unwrap_or_default());
        }
        PostDialect::CopilotJson => {
            let out = serde_json::json!({
                "modifiedResult": {
                    "resultType": "success",
                    "textResultForLlm": compressed,
                }
            });
            println!("{}", serde_json::to_string(&out).unwrap_or_default());
        }
    }
    std::process::exit(0);
}

/// Pick the PowerShell binary: prefer PowerShell 7+ (`pwsh`, UTF-8 native) and
/// fall back to Windows PowerShell 5.1 (`powershell`) when pwsh is absent.
fn powershell_program() -> &'static str {
    static PROGRAM: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new();
    PROGRAM.get_or_init(|| {
        let probe = std::process::Command::new("pwsh")
            .args(["-NoProfile", "-NonInteractive", "-Command", "$null"])
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status();
        if probe.is_ok() {
            "pwsh"
        } else {
            "powershell"
        }
    })
}

/// Below this, the recovery hint costs more than the content it points at.
const RECOVERY_HINT_MIN_DROPPED_BYTES: usize = 500;

const TEE_MAX_FILES: usize = 20;
const TEE_MAX_BYTES: usize = 1_000_000;
const TEE_MIN_RAW_BYTES: usize = 500;

/// Failure tee: when a command fails and compression dropped content, persist
/// the full raw output under `~/.tokenix/tee/` so the agent can Read the rest
/// instead of re-running the command (the most expensive recovery path — the
/// re-run pays the raw cost twice and still goes through the hook). Returns
/// the file path to reference in a recovery hint. `TOKENIX_TEE=0` disables.
fn tee_raw_output(command_str: &str, stdout_raw: &str, stderr_raw: &str) -> Option<PathBuf> {
    if std::env::var("TOKENIX_TEE").is_ok_and(|v| v == "0") {
        return None;
    }
    let dir = crate::store::global_dir()?.join("tee");
    std::fs::create_dir_all(&dir).ok()?;
    crate::store::restrict_to_owner(&dir);

    let epoch = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .ok()?
        .as_secs();
    let slug: String = command_str
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
        .take(40)
        .collect::<String>()
        .trim_matches('-')
        .to_string();
    let path = dir.join(format!("{epoch}_{slug}.log"));

    // The command line itself is the most reliable place to find a credential in
    // a failing run (`curl -H "Authorization: …"`, `psql postgres://u:pw@h`), and
    // this file outlives the session. Mask it like every other persisted view.
    let mut body = format!(
        "$ {}\n",
        crate::conversation_audit::redact_credentials(command_str)
    );
    let mut push_stream = |label: &str, raw: &str| {
        if !raw.trim().is_empty() {
            body.push_str(&format!("--- {label} ---\n"));
            let mut cut = raw.len().min(TEE_MAX_BYTES);
            while cut > 0 && !raw.is_char_boundary(cut) {
                cut -= 1;
            }
            // A failing command is exactly the one that dumps its environment or
            // echoes an auth header. The masking is shape-targeted (auth headers,
            // URL userinfo, `token=`/`password=`, PEM headers), so stack traces
            // and compiler errors — the reason this file exists — survive intact.
            body.push_str(&crate::conversation_audit::redact_credentials(&raw[..cut]));
            if cut < raw.len() {
                body.push_str("\n[tee capped at 1MB]");
            }
            if !body.ends_with('\n') {
                body.push('\n');
            }
        }
    };
    push_stream("stdout", stdout_raw);
    push_stream("stderr", stderr_raw);
    std::fs::write(&path, &body).ok()?;
    crate::store::restrict_to_owner(&path);

    // Rotation: keep the newest TEE_MAX_FILES logs (epoch prefix sorts).
    if let Ok(entries) = std::fs::read_dir(&dir) {
        let mut logs: Vec<PathBuf> = entries
            .flatten()
            .map(|e| e.path())
            .filter(|p| p.extension().and_then(|e| e.to_str()) == Some("log"))
            .collect();
        if logs.len() > TEE_MAX_FILES {
            logs.sort();
            for old in &logs[..logs.len() - TEE_MAX_FILES] {
                let _ = std::fs::remove_file(old);
            }
        }
    }
    Some(path)
}

/// Build the child `Command` for `tokenix run`, shared by the compressed and
/// raw execution paths so the two can never drift on how the command is
/// actually invoked.
fn build_shell_command(
    command_str: &str,
    shell: &str,
    cwd: Option<&Path>,
) -> std::process::Command {
    let is_powershell = matches!(shell, "pwsh" | "powershell");
    let mut cmd = if is_powershell {
        // Force UTF-8 so captured bytes decode cleanly (Windows PowerShell 5.1
        // otherwise emits UTF-16/codepage output that from_utf8_lossy mangles).
        let wrapped =
            format!("[Console]::OutputEncoding=[System.Text.Encoding]::UTF8; {command_str}");
        let mut c = std::process::Command::new(powershell_program());
        c.args(["-NoProfile", "-NonInteractive", "-Command", &wrapped]);
        c
    } else if cfg!(windows) {
        match shell::git_bash() {
            Some(bash) => {
                let mut c = std::process::Command::new(bash);
                c.args(["-c", command_str]);
                c
            }
            None => {
                let mut c = std::process::Command::new("cmd");
                c.args(["/C", command_str]);
                c
            }
        }
    } else {
        let mut c = std::process::Command::new("sh");
        c.args(["-c", command_str]);
        c
    };

    // `tokenix run --path <dir>` must actually run there.
    if let Some(dir) = cwd {
        cmd.current_dir(dir);
    }
    cmd
}

/// Run the command and print its stdout/stderr byte-for-byte, with no
/// compression, dedup, or recall stash — `tokenix run --raw` / `TOKENIX_RAW=1`.
///
/// Exists because auto-detecting "this output feeds a script, not an LLM" is
/// not a safe deterministic signal here: the agent harness that is the normal
/// caller of `tokenix run` also captures stdout through a pipe, so a piped
/// script and a harness invocation are indistinguishable by `isatty` alone.
/// An explicit opt-out is the only form of this guard that cannot misfire.
pub fn run_command_raw(command_str: &str, shell: &str, cwd: Option<&Path>) -> Result<i32> {
    let mut cmd = build_shell_command(command_str, shell, cwd);
    // Inherit rather than capture: streams the child's bytes straight through
    // untouched (encoding included), and avoids buffering output this path
    // never needs to look at.
    cmd.stdout(std::process::Stdio::inherit());
    cmd.stderr(std::process::Stdio::inherit());
    let status = cmd.status()?;
    Ok(status.code().unwrap_or(0))
}

pub fn run_command_and_compress(command_str: &str, shell: &str, cwd: Option<&Path>) -> Result<i32> {
    let is_powershell = matches!(shell, "pwsh" | "powershell");
    let output = build_shell_command(command_str, shell, cwd).output()?;

    let stdout_raw = String::from_utf8_lossy(&output.stdout);
    let stderr_raw = String::from_utf8_lossy(&output.stderr);

    let (stdout_compressed, mut stderr_compressed) = compress_command_streams(
        command_str,
        &stdout_raw,
        &stderr_raw,
        output.status.success(),
    );

    // Failure tee: the compressed view of a failure may not be enough to fix
    // it. Persist the raw output and point at it, so recovery is a targeted
    // Read instead of a full re-run. Only when compression actually dropped
    // content and the raw is big enough to be worth a file.
    let raw_len = stdout_raw.len() + stderr_raw.len();
    let compressed_len = stdout_compressed.len() + stderr_compressed.len();
    if !output.status.success() && raw_len >= TEE_MIN_RAW_BYTES && compressed_len + 80 < raw_len {
        if let Some(path) = tee_raw_output(command_str, &stdout_raw, &stderr_raw) {
            // Say "credentials masked" rather than implying a byte-for-byte copy:
            // the agent must not conclude a redacted token was the literal value.
            stderr_compressed.push_str(&format!(
                "\n[full output (credentials masked): {}]\n",
                path.display()
            ));
        }
    }

    let repo_root = find_repo_root();
    // Dedup and recall are scoped per project: the index is machine-wide, and a
    // pointer to "the identical output you already have" is only true within the
    // checkout that produced it.
    let project = repo_root.to_string_lossy().to_string();

    // Cross-call dedup: agents re-run `git status`/`cargo check`/`kubectl get`
    // and pay full price for byte-identical output every time. Only on success
    // — a repeated failure still needs its error text in front of the model.
    let mut stdout_compressed = stdout_compressed;
    let mut deduped_against: Option<String> = None;
    if output.status.success() {
        let stdout_tokens = count_tokens(&stdout_compressed);
        if let Some(hit) = crate::recall::find_identical(
            &project,
            command_str,
            &stdout_compressed,
            stdout_tokens,
            now_ts(),
        ) {
            let marker = crate::recall::dedup_marker(&hit, now_ts());
            // Never trade a short output for a longer marker.
            if marker.len() < stdout_compressed.len() {
                deduped_against = Some(hit.key.clone());
                stdout_compressed = format!("{marker}\n");
            }
        }
        if deduped_against.is_none() && stdout_tokens >= 1 {
            let dropped = stdout_raw.len().saturating_sub(stdout_compressed.len());
            let key = crate::recall::remember(
                &project,
                command_str,
                &stdout_compressed,
                &stdout_raw,
                stdout_tokens,
                now_ts(),
            );
            // Advertise the recovery key on the *first* clipped run, not only on a
            // later dedup hit. Compression is reversible by design, but the key
            // was being stashed and never shown, so the one output most worth
            // recovering — a big first result the caps just trimmed — had no way
            // back short of re-running the command.
            if let Some(key) = key {
                if dropped >= RECOVERY_HINT_MIN_DROPPED_BYTES {
                    if !stdout_compressed.ends_with('\n') {
                        stdout_compressed.push('\n');
                    }
                    stdout_compressed.push_str(&format!(
                        "[tokenix: {dropped} bytes not shown — `tokenix retrieve {key}` returns the full output]\n"
                    ));
                }
            }
        }
    }

    // Print to standard streams
    print!("{}", stdout_compressed);
    eprint!("{}", stderr_compressed);

    // Write log event of the actual execution savings
    // Capture raw output if a `tokenix filter record` session is active.
    crate::recordings::capture(&repo_root, command_str, &stdout_raw, &stderr_raw);
    let original_tokens = (count_tokens(&stdout_raw) + count_tokens(&stderr_raw)) as i64;
    let actual_tokens =
        (count_tokens(&stdout_compressed) + count_tokens(&stderr_compressed)) as i64;
    let saved = (original_tokens - actual_tokens).max(0);

    // Log every measured run, including the ones that saved nothing.
    //
    // Skipping `saved == 0` made `gain`'s denominator the set of commands where
    // compression already worked, so `pct_saved` answered "how much do we save
    // when we save" — a number that cannot go down and is not the savings rate
    // anyone reads it as. A run that went through `tokenix run` and came back
    // the same size is a real measurement and belongs in the sample.
    let (action, reason) = if saved > 0 {
        ("intercepted", "compressed command output")
    } else {
        ("pass", "no reduction available")
    };
    let _ = log_hook_event(
        &repo_root,
        &HookEvent {
            ts: now_ts(),
            tool: if is_powershell { "PowerShell" } else { "Bash" }.to_string(),
            action: action.to_string(),
            phase: "ToolOutputCompressed".to_string(),
            reason: reason.to_string(),
            saved_tokens: saved,
            actual_tokens,
            original_estimate: original_tokens,
            input_preview: command_str.chars().take(200).collect(),
            command: command_str.to_string(),
        },
    );

    Ok(output.status.code().unwrap_or(0))
}

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

    #[test]
    fn run_command_raw_preserves_exit_code() {
        // stdout/stderr content is inherited straight to the process' own
        // streams (asserting the bytes would mean capturing the whole test
        // binary's output); the exit code is the one signal this path can
        // check without that. Manually verified byte-exactness against
        // `tokenix run --raw` in homologation.
        let code = run_command_raw("exit 7", "auto", None).unwrap();
        assert_eq!(code, 7);
    }

    #[test]
    fn compact_json_is_lossless() {
        // Regression: round-tripping through serde_json::Value turned integers
        // past u64 into scientific notation, collapsed duplicate keys, and
        // alphabetized the object.
        let big = r#"{ "id" : 123456789012345678901234567890 , "b" : 1 , "a" : 2 }"#;
        let out = compact_json(big);
        assert!(out.contains("123456789012345678901234567890"), "{out}");
        assert!(out.starts_with(r#"{"id":"#), "key order changed: {out}");
        // Whitespace inside strings is preserved.
        assert_eq!(compact_json(r#"{ "k" : "a  b" }"#), r#"{"k":"a  b"}"#);
    }

    #[test]
    fn strip_ansi_does_not_blank_output_on_esc_before_multibyte() {
        // Regression: ESC + a UTF-8 lead byte consumed half a character, so
        // `from_utf8` failed and `unwrap_or_default()` wiped the whole output.
        let raw = String::from_utf8(vec![b'o', b'k', 0x1b, 0xC3, 0xA9, b'!']).unwrap();
        let out = strip_ansi(&raw);
        assert!(out.starts_with("ok"), "output was blanked: {out:?}");
        assert!(out.ends_with('!'), "tail lost: {out:?}");
    }

    #[test]
    fn pkg_manager_keeps_uppercase_error_lines() {
        // Regression: `starts_with("error")` was case-sensitive, so `ERROR:` and
        // `npm ERR!` were dropped once the first 30 lines were used up.
        let mut lines: Vec<&str> = (0..40).map(|_| "added 1 package").collect();
        lines.push("ERROR: could not resolve dependency");
        lines.push("npm ERR! code ERESOLVE");
        let out = compress_pkg_manager(&lines);
        assert!(out.contains("ERROR: could not resolve"), "{out}");
        assert!(out.contains("npm ERR!"), "{out}");
    }

    #[test]
    fn kebab_identifier_chain_is_not_redacted_as_base64() {
        // Regression: `-` counted as proof of base64, so the scan returned true
        // on the first hyphen of a lowercase slug chain.
        let run = "build-step-one-build-step-two-build-step-three".repeat(20);
        assert!(!looks_like_base64(run.as_bytes()));
        // A real base64url payload is mixed-case and still detected.
        let b64 = "aGVsbG9Xb3JsZFRoaXNJc0Jhc2U2NA".repeat(20);
        assert!(looks_like_base64(b64.as_bytes()));
    }

    #[test]
    fn small_git_diff_keeps_its_changed_lines() {
        let diff = "diff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-old\n+new";
        let lines: Vec<&str> = diff.lines().collect();
        let out = compress_git_diff(&lines);
        assert!(out.contains("+new") && out.contains("-old"), "{out}");
    }

    #[test]
    fn kubectl_describe_is_not_replaced_by_a_placeholder() {
        let mut lines = vec!["Name:         my-pod", "Namespace:    default"];
        for i in 0..80 {
            lines.push(if i == 79 {
                "  Warning  BackOff  pod crashlooping"
            } else {
                "  filler: value"
            });
        }
        let out = compress_kubectl(&lines);
        assert!(!out.contains("<resource details>"), "fabricated: {out}");
        assert!(out.contains("BackOff"), "events tail lost: {out}");
    }

    #[test]
    fn strips_data_uri_image_blob() {
        let blob = "Zm9vQmFyBaz1".repeat(200); // 2400 mixed-case base64 chars
        let n = blob.len();
        let raw = format!("here is the image data:image/png;base64,{blob} end");
        let out = compress_output(&raw);
        assert!(out.contains("data:image/png;base64,"), "prefix kept: {out}");
        assert!(
            out.contains(&format!("[base64 blob omitted: {n} chars]")),
            "redacted: {out}"
        );
        assert!(!out.contains(&blob), "raw blob must be gone");
        assert!(out.starts_with("here is the image"));
        assert!(out.trim_end().ends_with("end"));
    }

    #[test]
    fn strips_bare_base64_blob() {
        // A tar/binary dump with no data: prefix (the `unknown-large` audit shape).
        let blob = "iVBORw0KGgoAAAANSUhEUg".repeat(60); // > 512 base64 chars, PNG magic
        let out = strip_base64_blobs(&blob);
        assert_eq!(
            out,
            format!("[png image base64 omitted: {} chars]", blob.len())
        );
    }

    #[test]
    fn strips_line_wrapped_base64_block() {
        // 10 wrapped lines of 64 base64 chars — the MIME/`base64 file` shape the
        // contiguous scanner misses (each line < BASE64_MIN).
        let line = "Zm9v".repeat(16); // 64 mixed-case base64 chars per line
        let block = std::iter::repeat_n(line.as_str(), 10)
            .collect::<Vec<_>>()
            .join("\n");
        let raw = format!("preamble\n{block}\ntrailer\n");
        let out = strip_wrapped_base64(&raw);
        assert!(out.contains("omitted:"), "collapsed: {out}");
        assert!(out.contains("across 10 lines"), "line count: {out}");
        assert!(out.starts_with("preamble\n"));
        // A trailing all-alnum word must NOT be absorbed into the blob.
        assert!(out.ends_with("trailer\n"), "trailer kept: {out}");
        assert!(!out.contains(&line), "raw blob gone");
    }

    #[test]
    fn strips_pem_body_keeps_markers() {
        // 64-char lines (PEM wrap width) so each clears WRAP_MIN_LINE.
        let body = std::iter::repeat_n(
            "MIIDdummyBase64ContentLinePadded0123456789ABCDEFabcdefXYZ01234567",
            8,
        )
        .collect::<Vec<_>>()
        .join("\n");
        let pem = format!("-----BEGIN CERTIFICATE-----\n{body}\n-----END CERTIFICATE-----\n");
        let out = strip_wrapped_base64(&pem);
        assert!(
            out.contains("-----BEGIN CERTIFICATE-----"),
            "begin kept: {out}"
        );
        assert!(out.contains("-----END CERTIFICATE-----"), "end kept: {out}");
        assert!(out.contains("omitted:"), "body collapsed: {out}");
    }

    #[test]
    fn wrapped_stripper_spares_short_or_prose_blocks() {
        // 4 long mixed-case base64 lines (>512 chars total, past the length gate)
        // but below WRAP_MIN_LINES → untouched (exercises the line-count gate).
        let short = format!("{a}\n{a}\n{a}\n{a}", a = "Zm9v".repeat(35));
        assert_eq!(strip_wrapped_base64(&short), short);
        // Consecutive prose lines with spaces are not base64 lines.
        let prose = "the quick brown fox jumped\n".repeat(30);
        assert_eq!(strip_wrapped_base64(&prose), prose);
    }

    #[test]
    fn base64_blob_kind_labels_by_magic() {
        let jpeg = format!("/9j/{}", "A".repeat(600));
        assert!(strip_base64_blobs(&jpeg).contains("[jpeg image base64 omitted:"));
        let gif = format!("R0lGOD{}", "A".repeat(600));
        assert!(strip_base64_blobs(&gif).contains("[gif image base64 omitted:"));
        let opaque = "Zm9v".repeat(150); // 600 mixed-case chars, no magic prefix
        assert!(strip_base64_blobs(&opaque).contains("[base64 blob omitted:"));
    }

    #[test]
    fn base64_stripper_spares_hex_and_digit_runs() {
        // A long lowercase-hex run (concatenated checksums) is NOT base64 — sparing
        // it avoids silently eating a sha256 manifest or hex dump.
        let hex = "deadbeef0123456789abcdef".repeat(30); // 720 hex chars, single case
        assert_eq!(strip_base64_blobs(&hex), hex);
        // A long numeric id column is spared too.
        let digits = "1234567890".repeat(70); // 700 digits
        assert_eq!(strip_base64_blobs(&digits), digits);
    }

    #[test]
    fn wrapped_stripper_spares_hash_manifest() {
        // 8 lines of 64-char lowercase hex (a `.sha256` manifest) must not collapse.
        let line = "0123456789abcdef".repeat(4); // 64 hex chars
        let manifest = std::iter::repeat_n(line.as_str(), 8)
            .collect::<Vec<_>>()
            .join("\n");
        assert_eq!(strip_wrapped_base64(&manifest), manifest);
    }

    #[test]
    fn base64_stripper_spares_normal_text() {
        // Long minified-looking code stays intact: punctuation breaks the run.
        let code = "const x=(a,b)=>{return a+b;};".repeat(80);
        assert_eq!(strip_base64_blobs(&code), code);
        // A short token below threshold is untouched.
        let short = "AAAABBBBCCCCDDDD";
        assert_eq!(strip_base64_blobs(short), short);
        // UTF-8 around a blob survives.
        let mixed = format!("café ☕ {} über", "Zm9vYmFy".repeat(80));
        let out = strip_base64_blobs(&mixed);
        assert!(out.starts_with("café ☕ "));
        assert!(out.trim_end().ends_with("über"));
    }

    #[test]
    fn strips_ansi_colors() {
        assert_eq!(strip_ansi("\x1b[32mOK\x1b[0m"), "OK");
        assert_eq!(strip_ansi("\x1b[1;31mError\x1b[0m: bad"), "Error: bad");
    }

    #[test]
    fn git_status_verbose_to_porcelain() {
        let raw = "On branch main\nYour branch is up to date with 'origin/main'.\n\nChanges not staged for commit:\n  (use \"git add <file>...\")\n\tmodified:   src/main.rs\n\tdeleted:    old.rs\n\nUntracked files:\n  (use \"git add <file>...\")\n\tnew.rs\n";
        let lines: Vec<&str> = raw.lines().collect();
        let out = compress_git_status(&lines);
        assert_eq!(out, "M src/main.rs\nD old.rs\n?? new.rs");
        // Clean repo → terse marker.
        let clean = ["On branch main", "nothing to commit, working tree clean"];
        assert_eq!(compress_git_status(&clean), "git status: clean");
    }

    #[test]
    fn grep_strips_indentation() {
        let lines = [
            "src/a.rs:10:    let x = 5;",
            "src/b.rs:2:        fn main() {}",
            "",
        ];
        let out = compress_grep(&lines);
        assert_eq!(out, "src/a.rs:10:let x = 5;\nsrc/b.rs:2:fn main() {}");
    }

    // Golden/safety-net for the dominant savings path: `cargo build` is ~96% of
    // all tokens tokenix saves globally, and it flows through `compress_cargo`.
    // This pins the exact kept-vs-dropped behavior so any accidental change to the
    // cargo heuristic shows up as a failing snapshot instead of a silent regression.
    #[test]
    fn cargo_build_golden_keeps_signal_drops_noise() {
        let raw = "\
   Compiling libc v0.2.1
   Compiling serde v1.0.2
   Compiling tokenix v0.57.1
warning: function is never used: `foo`
error[E0425]: cannot find value `y`
    Finished `dev` profile [optimized] target(s) in 4.2s";
        let lines: Vec<&str> = raw.lines().collect();
        let out = compress_cargo(&lines);

        // Exact snapshot: warning + error + Finished survive; every `Compiling` drops.
        assert_eq!(
            out,
            "warning: function is never used: `foo`\n\
             error[E0425]: cannot find value `y`\n\
             \x20   Finished `dev` profile [optimized] target(s) in 4.2s"
        );
        // Invariants (robust to formatting tweaks in the snapshot above).
        assert!(!out.contains("Compiling "), "build noise dropped");
        assert!(out.contains("error[E0425]"), "errors preserved");
        assert!(out.contains("Finished "), "summary preserved");
        assert!(out.len() < raw.len(), "net reduction");
        // Deterministic: same input → same output.
        assert_eq!(compress_cargo(&lines), out);
    }

    #[test]
    fn cargo_metadata_summarized() {
        let json = r#"{"packages":[{"name":"a","version":"1.0.0"},{"name":"b","version":"2.0.0"}],"workspace_members":["tokenix 0.1.0 (path+file:///x)"],"resolve":{"root":"tokenix 0.1.0 (path+file:///x)"}}"#;
        let out = compress_cargo_metadata(json);
        assert!(out.contains("2 packages"));
        assert!(out.contains("workspace members: tokenix"));
        assert!(out.len() < json.len());
    }

    #[test]
    fn cargo_tree_dedupes() {
        let lines = [
            "tokenix v0.1.0",
            "├── anyhow v1.0.0",
            "│   └── anyhow v1.0.0 (*)",
            "└── serde v1.0.0",
        ];
        let out = compress_cargo_tree(&lines);
        assert!(out.starts_with("cargo tree: "));
        assert!(out.contains("anyhow v1.0.0"));
        assert!(out.contains("serde v1.0.0"));
        // anyhow appears twice in input but once in the unique set.
        assert_eq!(out.matches("anyhow v1.0.0").count(), 1);
    }

    #[test]
    fn command_streams_do_not_turn_stderr_errors_into_success() {
        let (stdout, stderr) = compress_command_streams(
            "cargo build",
            "",
            "error: could not find `Cargo.toml` in this directory\n",
            false,
        );

        assert_eq!(stdout, "");
        assert!(
            stderr.contains("could not find"),
            "stderr error must be preserved, got: {stderr:?}"
        );
        assert!(
            !stderr.contains("build succeeded"),
            "stderr must not emit success sentinel: {stderr:?}"
        );
    }

    #[test]
    fn cargo_warnings_on_stderr_reach_the_filter() {
        let warning = "warning: unused variable: `x`\n --> src/lib.rs:2:9\n  |\n2 |     let x = 5;\n  |         ^ help: if this is intentional, prefix it with an underscore: `_x`\n  |\n  = note: `#[warn(unused_variables)]` on by default\n\n";
        let stderr_raw = format!("   Compiling demo v0.1.0\n{warning}    Finished `dev` profile\n");
        let (_, stderr) = compress_command_streams("cargo clippy", "", &stderr_raw, true);
        assert!(
            stderr.contains("warning: unused variable: `x`\n --> src/lib.rs:2:9"),
            "{stderr}"
        );
        assert!(
            !stderr.contains("let x = 5"),
            "rustc writes to stderr; the cargo filter must see it: {stderr}"
        );
        assert!(
            stderr.ends_with('\n'),
            "a merged stdout would glue onto the last line: {stderr:?}"
        );
    }

    #[test]
    fn silent_success_stays_empty() {
        // on_empty sentinels were removed: a silent successful command must not
        // fabricate a "succeeded" string — genuinely empty output stays empty.
        let (stdout, stderr) = compress_command_streams("cargo build", "", "", true);
        assert_eq!(stdout, "");
        assert_eq!(stderr, "");
    }

    #[test]
    fn ps_keeps_top_by_cpu() {
        let lines = [
            "USER PID %CPU %MEM CMD",
            "u 1 0.0 0.1 idle",
            "u 2 99.0 5.0 hot",
            "u 3 0.1 0.2 warm",
            "u 4 0.0 0.0 idle2",
            "u 5 0.0 0.0 idle3",
            "u 6 0.0 0.0 idle4",
        ];
        let out = compress_ps(&lines);
        let hot_pos = out.find("hot").expect("busiest process kept");
        let idle_pos = out.find("idle3");
        // The 99% process is kept and ranks above the idle ones (which may be dropped).
        assert!(out.starts_with("USER PID"));
        assert!(idle_pos.is_none() || hot_pos < idle_pos.unwrap());
    }

    #[test]
    fn path_listing_collapses_to_top_level() {
        let lines = [
            "./src/a.rs",
            "./src/b.rs",
            "./target/debug/x.rs",
            "./target/debug/y.rs",
            "./benchmark/c.rs",
        ];
        let out = compress_path_listing(&lines);
        assert!(out.contains("5 files across"));
        assert!(out.contains("target/ (2)") || out.contains("src/ (2)"));
        assert!(out.len() < lines.join("\n").len());
    }

    #[test]
    fn cargo_test_failure_detail_is_preserved() {
        // A failing `cargo test` must keep the WHOLE failure block — the reason a
        // test failed is free-form (custom panic message, pretty-assertion colour
        // diff, backtrace) and matches no fixed prefix. Compression should still
        // drop noise (Compiling lines, passing tests) but never the failure signal.
        let raw = "\
Compiling foo v0.1.0
running 2 tests
test tests::ok_one ... ok
test tests::adds ... FAILED

failures:

---- tests::adds stdout ----
thread 'tests::adds' panicked at src/lib.rs:10:9:
custom failure: widget count drifted by 1
Diff < left / right > :
<4
>5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    tests::adds

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out";
        let lines: Vec<&str> = raw.lines().collect();
        let out = compress_cargo(&lines);
        assert!(out.contains("panicked at"), "panic line must be preserved");
        // Free-form lines that no prefix matches — the stateful block capture keeps them.
        assert!(
            out.contains("custom failure: widget count drifted by 1"),
            "custom panic message must be preserved"
        );
        assert!(
            out.contains("Diff < left / right > :") && out.contains("<4") && out.contains(">5"),
            "pretty-assertion diff must be preserved"
        );
        assert!(
            out.contains("---- tests::adds stdout ----"),
            "failing test name marker must be preserved"
        );
        assert!(
            out.contains("test result: FAILED"),
            "summary must be preserved"
        );
        // Still compresses: the noisy Compiling line is dropped.
        assert!(!out.contains("Compiling foo"), "noise should be dropped");
    }

    #[test]
    fn strips_osc_sequences() {
        assert_eq!(strip_ansi("\x1b]0;title\x07text"), "text");
    }

    #[test]
    fn removes_emojis() {
        assert_eq!(remove_emojis("🚀 Build done"), " Build done");
        assert_eq!(remove_emojis("no emojis here"), "no emojis here");
    }

    #[test]
    fn collapses_blank_lines() {
        let input = "a\n\n\n\n\nb";
        let output = collapse_blank_lines(input);
        assert_eq!(output, "a\n\nb");
    }

    #[test]
    fn groups_repeated_lines() {
        let input = "line1\nline1\nline1\nline1\nline2\n";
        let output = group_repeated_lines(input);
        assert_eq!(output, "line1\n[repeated 3x]\nline2\n");
    }

    #[test]
    fn does_not_group_two_identical_lines() {
        let input = "a\na\nb\n";
        assert_eq!(group_repeated_lines(input), "a\na\nb\n");
    }

    #[test]
    fn groups_repeated_multiline_blocks() {
        // The monitoring-loop shape: an 3-line stanza re-emitted every poll.
        let stanza = "SetValueInvocationException:\n  Line | 3 | $RawUI.CursorPosition\n  Exception setting CursorPosition\n";
        let input = format!("{}header\n", stanza.repeat(5));
        let output = group_repeated_blocks(&input);
        assert!(
            output.contains("[block of 3 lines repeated 4x]"),
            "got: {output}"
        );
        assert_eq!(output.matches("SetValueInvocationException").count(), 1);
        assert!(output.ends_with("header\n"));
    }

    #[test]
    fn block_grouping_prefers_smallest_unit() {
        let input = "a\nb\na\nb\na\nb\na\nb\nz\n";
        let output = group_repeated_blocks(input);
        assert_eq!(output, "a\nb\n[block of 2 lines repeated 3x]\nz\n");
    }

    #[test]
    fn block_grouping_defers_identical_line_runs_to_line_grouping() {
        // "a" x6 must stay a line run, not become a 2-line "block".
        let input = "a\na\na\na\na\na\nz\n";
        assert_eq!(group_repeated_blocks(input), input);
        assert_eq!(group_repeated_lines(input), "a\n[repeated 5x]\nz\n");
    }

    #[test]
    fn block_grouping_leaves_two_repeats_alone() {
        let input = "a\nb\nc\na\nb\nc\nz\n";
        assert_eq!(group_repeated_blocks(input), input);
    }

    #[test]
    fn block_grouping_preserves_non_repeating_output() {
        let input = "one\ntwo\nthree\nfour\nfive\nsix\nseven\n";
        assert_eq!(group_repeated_blocks(input), input);
    }

    #[test]
    fn token_budget_caps_single_giant_line() {
        // The gap this closes: one enormous line has no line count to truncate on.
        let giant = format!(
            "prefix {} suffix",
            "lorem ipsum dolor sit amet ".repeat(20_000)
        );
        let out = enforce_token_budget(&giant);
        assert!(out.contains("tokens omitted by tokenix output cap"));
        assert!(count_tokens(&out) < count_tokens(&giant) / 2);
        assert!(out.starts_with("prefix "));
        assert!(out.ends_with(" suffix"));
    }

    #[test]
    fn token_budget_keeps_identifiers_from_the_dropped_range() {
        // A dropped SHA/UUID/URL cannot be re-derived — losing it forces the
        // re-run the cap exists to avoid.
        let filler = "routine log line with nothing worth keeping\n".repeat(6000);
        let s = format!(
            "start\n{filler}commit 9fceb02d1a3e5b7c failed at https://ci.example.com/run/42 with E0308 and id 550e8400-e29b-41d4-a716-446655440000\n{filler}end\n"
        );
        let out = enforce_token_budget(&s);
        assert!(
            out.contains("ids kept from the omitted range"),
            "got: {}",
            &out[..400.min(out.len())]
        );
        assert!(out.contains("9fceb02d1a3e5b7c"));
        assert!(out.contains("https://ci.example.com/run/42"));
        assert!(out.contains("E0308"));
        assert!(out.contains("550e8400-e29b-41d4-a716-446655440000"));
    }

    #[test]
    fn base64_stripper_spares_long_kebab_identifier_chains() {
        // Found by smoke-testing a real command: `-` is a base64url character,
        // so a long hyphen-joined slug list was redacted as a blob (data loss).
        let chain = (1..=80)
            .map(|i| format!("chunk{i}"))
            .collect::<Vec<_>>()
            .join("-");
        assert!(chain.len() > BASE64_MIN, "fixture must reach the run floor");
        let out = redact_base64_blobs(&format!("SMOKE-{chain}"));
        assert!(out.contains("chunk80"), "identifier chain was eaten: {out}");
        assert!(!out.contains("omitted"));

        // Snake_case chains are the same shape.
        let snake = (1..=80)
            .map(|i| format!("Step{i}"))
            .collect::<Vec<_>>()
            .join("_");
        assert!(redact_base64_blobs(&snake).contains("Step80"));
    }

    #[test]
    fn base64_stripper_still_redacts_base64url_payloads() {
        // A real base64url blob has few, long separator-free stretches — the
        // guard above must not give it a pass.
        let blob = format!(
            "{}-{}",
            "Zm9vQmFyBaz1".repeat(30),
            "QmFyRm9vYmF6".repeat(30)
        );
        let out = redact_base64_blobs(&blob);
        assert!(
            out.contains("omitted"),
            "real base64url must still be cut: {out}"
        );
    }

    #[test]
    fn identifier_scan_ignores_ordinary_prose() {
        // "decade"/"facade" are hex-only words; plain numbers are not identifiers.
        assert_eq!(
            preserved_identifiers("the decade long facade of 123456 lines added"),
            None
        );
    }

    #[test]
    fn token_budget_leaves_small_output_untouched() {
        let s = "short output\nsecond line\n";
        assert_eq!(enforce_token_budget(s), s);
    }

    #[test]
    fn token_budget_is_utf8_safe() {
        let s = "café ☕ ".repeat(30_000);
        let out = enforce_token_budget(&s);
        assert!(out.len() < s.len());
        // Round-tripping through String proves no char boundary was split.
        assert!(std::str::from_utf8(out.as_bytes()).is_ok());
    }

    #[test]
    fn compress_output_caps_giant_compacted_json() {
        // compact_json returned early with no cap: a huge payload shrank ~10% and
        // still cost hundreds of thousands of tokens.
        let items: Vec<String> = (0..40_000)
            .map(|i| format!("{{\n  \"id\": {i},\n  \"name\": \"item number {i}\"\n}}"))
            .collect();
        let raw = format!("[\n{}\n]", items.join(",\n"));
        let out = compress_output(&raw);
        assert!(
            count_tokens(&out) <= DEFAULT_MAX_OUTPUT_TOKENS + 64,
            "cap not applied: {} tokens",
            count_tokens(&out)
        );
        assert!(out.contains("tokens omitted by tokenix output cap"));
    }

    #[test]
    fn compacts_pretty_json_object() {
        let input = "{\n  \"status\": \"ok\",\n  \"count\": 42\n}\n";
        let output = compact_json(input);
        // key order is not guaranteed; verify it compacted (shorter) and parses to same value
        assert!(output.len() < input.len(), "should be shorter");
        assert!(output.ends_with('\n'));
        let v_in: serde_json::Value = serde_json::from_str(input.trim()).unwrap();
        let v_out: serde_json::Value = serde_json::from_str(output.trim()).unwrap();
        assert_eq!(v_in, v_out);
    }

    #[test]
    fn compacts_pretty_json_array() {
        let input = "[\n  1,\n  2,\n  3\n]";
        let output = compact_json(input);
        assert_eq!(output, "[1,2,3]");
    }

    #[test]
    fn passes_through_already_compact_json() {
        let input = "{\"a\":1}\n";
        assert_eq!(compact_json(input), input);
    }

    #[test]
    fn compacts_ndjson() {
        let input = "{ \"level\": \"info\", \"msg\": \"started\" }\n{ \"level\": \"error\", \"msg\": \"failed\" }\n";
        let output = compact_json(input);
        assert_eq!(
            output,
            "{\"level\":\"info\",\"msg\":\"started\"}\n{\"level\":\"error\",\"msg\":\"failed\"}\n"
        );
    }

    #[test]
    fn passes_through_plain_text() {
        let input = "On branch main\nnothing to commit\n";
        assert_eq!(compact_json(input), input);
    }

    #[test]
    fn compress_is_idempotent_on_clean_input() {
        let clean = "hello\nworld\n";
        assert_eq!(compress_output(clean), clean);
    }

    #[test]
    fn full_compression_pipeline() {
        let input = "\x1b[32m🚀 Starting\x1b[0m\n\n\n\nline\nline\nline\nline\ndone\n";
        let output = compress_output(input);
        assert!(output.contains("Starting"));
        assert!(!output.contains("\x1b["));
        assert!(!output.contains('🚀'));
        assert!(output.contains("[repeated"));
        assert!(!output.contains("\n\n\n"));
    }

    #[test]
    fn bash_short_output_passes_through() {
        let input = "hello\nworld\n";
        assert_eq!(compress_bash_output("", input), input);
    }

    #[test]
    fn bash_generic_truncation_over_100_lines() {
        let lines: String = (1..=150).map(|i| format!("line {}\n", i)).collect();
        let out = compress_bash_output("", &lines);
        assert!(out.contains("lines omitted"), "should truncate: {}", out);
        assert!(out.contains("line 1\n"));
        assert!(out.contains("line 150"));
    }

    #[test]
    fn bash_path_listing_groups_by_directory() {
        let input = [
            "src/main.rs",
            "src/query.rs",
            "src/hook.rs",
            "benchmark/samples/database_client.ts",
        ]
        .join("\n");

        let out = compress_bash_output("ls -R", &input);
        assert!(
            out.contains("4 files across 2 top-level dir(s)"),
            "output: {}",
            out
        );
        assert!(out.contains("src/ (3)"), "output: {}", out);
        assert!(out.contains("benchmark/ (1)"), "output: {}", out);
    }

    #[test]
    fn bash_cargo_extracts_errors() {
        let mut input = String::new();
        for i in 0..60 {
            input.push_str(&format!("Compiling crate{} v0.1.0\n", i));
        }
        input.push_str("error[E0425]: cannot find value `foo`\n");
        input.push_str("  --> src/main.rs:3:5\n");
        input.push_str("   |\n");
        input.push_str("3  |     foo();\n");
        input.push_str("error: aborting due to 1 previous error\n");
        input.push_str("Finished dev in 1.23s\n");

        let out = compress_bash_output("", &input);
        assert!(out.contains("error[E0425]"), "should keep error: {}", out);
        assert!(out.contains("Finished"), "should keep summary: {}", out);
        assert!(
            !out.contains("Compiling crate0"),
            "should strip Compiling lines"
        );
    }

    #[test]
    fn bash_git_log_truncated_after_20_commits() {
        let mut input = String::new();
        for i in 0..30 {
            input.push_str(&format!("commit {:040}\n", i));
            input.push_str("Author: Test\nDate: Today\n\n    message\n\n");
        }
        let out = compress_bash_output("", &input);
        assert!(out.contains("lines omitted"), "should truncate: {}", out);
    }

    #[test]
    fn bash_status_poll_collapses_repeated_states() {
        let input = "\
phase=Pending
phase=Pending
phase=Pending
phase=Running
phase=Running
=== LOGS ===
daily job failed validation
";
        let out = compress_bash_output(
            "for i in $(seq 1 40); do phase=$(kubectl get pod); echo phase=$phase; done",
            input,
        );
        assert!(out.contains("phase=Pending x3"), "output: {out}");
        assert!(out.contains("phase=Running x2"), "output: {out}");
        assert!(out.contains("daily job failed validation"), "output: {out}");
        assert!(
            !out.contains("phase=Pending\nphase=Pending"),
            "output: {out}"
        );
    }

    #[test]
    fn parses_claude_post_input() {
        let v = serde_json::json!({
            "tool_name": "Bash",
            "tool_input": {"command": "git status"},
            "tool_response": "On branch main\n"
        });
        let input = parse_post_input(&v).unwrap();
        assert_eq!(input.tool_name, "Bash");
        assert_eq!(input.command, "git status");
        assert_eq!(input.text, "On branch main\n");
        assert_eq!(input.dialect, PostDialect::ClaudeNoop);
    }

    #[test]
    fn parses_claude_bash_stdout_stderr_shape() {
        // Real Claude Code Bash PostToolUse payload: tool_response is an object
        // with stdout/stderr, not `output`/`content`.
        let v = serde_json::json!({
            "tool_name": "Bash",
            "tool_input": {"command": "npm install"},
            "tool_response": {
                "stdout": "added 120 packages in 3s\n",
                "stderr": "npm warn deprecated foo\n",
                "interrupted": false,
                "isImage": false
            }
        });
        let input = parse_post_input(&v).unwrap();
        assert_eq!(input.tool_name, "Bash");
        assert_eq!(input.command, "npm install");
        assert!(input.text.contains("added 120 packages"));
        assert!(input.text.contains("npm warn deprecated foo"));
        assert_eq!(input.dialect, PostDialect::ClaudeNoop);
    }

    #[test]
    fn parses_copilot_post_input_camelcase() {
        let v = serde_json::json!({
            "toolName": "bash",
            "toolArgs": {"command": "git diff"},
            "toolResult": {"resultType": "success", "textResultForLlm": "diff output"}
        });
        let input = parse_post_input(&v).unwrap();
        assert_eq!(input.tool_name, "Bash"); // bash → Bash
        assert_eq!(input.command, "git diff");
        assert_eq!(input.text, "diff output");
        assert_eq!(input.dialect, PostDialect::CopilotJson);
    }

    #[test]
    fn parses_copilot_post_input_string_encoded_args() {
        // Copilot may send toolArgs as a JSON-encoded string.
        let v = serde_json::json!({
            "toolName": "powershell",
            "toolArgs": "{\"command\":\"git status\"}",
            "toolResult": {"textResultForLlm": "status output"}
        });
        let input = parse_post_input(&v).unwrap();
        assert_eq!(input.tool_name, "Bash"); // powershell → Bash
        assert_eq!(input.command, "git status");
        assert_eq!(input.text, "status output");
    }

    #[test]
    fn extract_copilot_result_handles_both_casings() {
        let camel = serde_json::json!({"textResultForLlm": "a"});
        let snake = serde_json::json!({"text_result_for_llm": "b"});
        let plain = serde_json::Value::String("c".to_string());
        assert_eq!(extract_copilot_result(&camel).as_deref(), Some("a"));
        assert_eq!(extract_copilot_result(&snake).as_deref(), Some("b"));
        assert_eq!(extract_copilot_result(&plain).as_deref(), Some("c"));
    }

    #[test]
    fn normalize_post_tool_maps_shells_to_bash() {
        assert_eq!(normalize_post_tool("bash"), "Bash");
        assert_eq!(normalize_post_tool("powershell"), "Bash");
        assert_eq!(normalize_post_tool("Bash"), "Bash"); // Claude casing preserved
        assert_eq!(normalize_post_tool("run_command"), "Bash");
        assert_eq!(normalize_post_tool("default_api:run_command"), "Bash");
        assert_eq!(normalize_post_tool("ListDirectory"), "ListDirectory");
        assert_eq!(normalize_post_tool("view"), "view"); // unmapped → unchanged
    }

    #[test]
    fn parses_claude_post_input_run_command() {
        let v = serde_json::json!({
            "tool_name": "default_api:run_command",
            "tool_input": {"CommandLine": "git diff"},
            "tool_response": "diff output"
        });
        let input = parse_post_input(&v).unwrap();
        assert_eq!(input.tool_name, "Bash");
        assert_eq!(input.command, "git diff");
        assert_eq!(input.text, "diff output");
        assert_eq!(input.dialect, PostDialect::ClaudeNoop);
    }
}