drip-cli 0.1.1

Delta Read Interception Proxy — sends only file diffs to your LLM agent
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
//! Semantic compression — replaces long function bodies with a one-line
//! placeholder so a first-read of a 400-line file becomes ~40 lines of
//! signatures + a few stubs. `drip refresh <file>` recovers the full
//! content.
//!
//! No real parser (tree-sitter, syn) — they'd quadruple the binary
//! size. Instead, language-aware line scanning + brace balancing.
//! Degrades gracefully: false negatives mean uncompressed output,
//! never mangled output.
//!
//! Languages with first-class support: Python, Rust, JS/TS, Go, and
//! the C-family (Java, C, C++, C#, Kotlin, Swift, Scala, PHP) via a
//! shared brace heuristic that handles K&R (`signature {`) and Allman
//! (`signature` + lone `{` line).
//!
//! Anything else returns `None` and the file is sent uncompressed.
//! Opt-OUT via `DRIP_NO_COMPRESS=1`; raise the threshold with
//! `DRIP_COMPRESS_MIN_BYTES=N` (default 1024).

use std::path::Path;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Language {
    Python,
    Rust,
    JavaScript,
    TypeScript,
    Go,
    Java,
    C,
    Cpp,
    CSharp,
    Kotlin,
    Swift,
    Scala,
    Php,
    Generic,
}

#[derive(Debug, Clone)]
pub struct Compressed {
    pub text: String,
    pub functions_elided: usize,
    pub lines_elided: usize,
    pub original_lines: usize,
    /// Names of elided functions, in source order. Stored as JSON on
    /// the `reads` row so the post-edit hook can warn when an edit
    /// targets a body the agent never saw.
    pub elided_function_names: Vec<String>,
    /// Compressed-line → original-line mapping. One entry per line in
    /// `text`. For visible lines start == end. For elided-body stubs
    /// the range spans every hidden line; `symbol_name` is set.
    #[allow(dead_code)]
    pub source_map: SourceMap,
}

/// One row of the compressed→original line mapping. All line numbers
/// are 1-indexed.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SourceMapEntry {
    pub compressed_line: usize,
    pub original_start: usize,
    pub original_end: usize,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub symbol_name: Option<String>,
    #[serde(default)]
    pub elided: bool,
}

pub type SourceMap = Vec<SourceMapEntry>;

/// Bodies shorter than this stay inline — eliding a short function
/// would cost more tokens than the function itself. Override with
/// `DRIP_COMPRESS_MIN_BODY` (clamped to a hard floor of 4).
const DEFAULT_MIN_BODY_LINES: usize = 15;
const MIN_BODY_LINES_FLOOR: usize = 4;

pub fn min_body_lines() -> usize {
    std::env::var("DRIP_COMPRESS_MIN_BODY")
        .ok()
        .and_then(|s| s.parse::<usize>().ok())
        .unwrap_or(DEFAULT_MIN_BODY_LINES)
        .max(MIN_BODY_LINES_FLOOR)
}
/// Files smaller than this don't benefit from compression. Configurable
/// via `DRIP_COMPRESS_MIN_BYTES`.
const DEFAULT_MIN_BYTES: usize = 1024;

pub fn detect_language(path: &Path) -> Language {
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .map(|s| s.to_lowercase())
        .unwrap_or_default();
    match ext.as_str() {
        "py" => Language::Python,
        "rs" => Language::Rust,
        "js" | "mjs" | "cjs" | "jsx" => Language::JavaScript,
        "ts" | "tsx" => Language::TypeScript,
        "go" => Language::Go,
        "java" => Language::Java,
        "c" | "h" => Language::C,
        "cpp" | "cc" | "cxx" | "hpp" | "hh" | "hxx" => Language::Cpp,
        "cs" => Language::CSharp,
        "kt" | "kts" => Language::Kotlin,
        "swift" => Language::Swift,
        "scala" | "sc" => Language::Scala,
        "php" | "phtml" => Language::Php,
        _ => Language::Generic,
    }
}

pub fn min_bytes() -> usize {
    std::env::var("DRIP_COMPRESS_MIN_BYTES")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(DEFAULT_MIN_BYTES)
}

pub fn enabled() -> bool {
    std::env::var("DRIP_NO_COMPRESS").as_deref() != Ok("1")
}

pub fn compress(content: &str, lang: Language) -> Option<Compressed> {
    if !enabled() || content.len() < min_bytes() {
        return None;
    }
    match lang {
        Language::Python => compress_python(content),
        Language::Rust => compress_brace(content, BraceFlavor::Rust),
        Language::JavaScript | Language::TypeScript => compress_brace(content, BraceFlavor::JsTs),
        Language::Go => compress_brace(content, BraceFlavor::Go),
        Language::Java
        | Language::C
        | Language::Cpp
        | Language::CSharp
        | Language::Kotlin
        | Language::Swift
        | Language::Scala
        | Language::Php => compress_brace(content, BraceFlavor::CFamily),
        Language::Generic => None,
    }
}

// -------- Python ---------------------------------------------------------

/// `def`, `async def`, `class` → indentation of the keyword.
fn python_block_start(line: &str) -> Option<usize> {
    let trimmed = line.trim_start();
    let indent = line.len() - trimmed.len();
    let rest = trimmed
        .strip_prefix("async def ")
        .or_else(|| trimmed.strip_prefix("def "))
        .or_else(|| trimmed.strip_prefix("class "))?;
    let first = rest.chars().next()?;
    if first.is_alphabetic() || first == '_' {
        Some(indent)
    } else {
        None
    }
}

fn leading_ws(line: &str) -> usize {
    line.chars().take_while(|c| *c == ' ' || *c == '\t').count()
}

/// Function/method name from a `def` / `async def` line. `None` for
/// `class` and unparseable input.
fn python_signature_name(line: &str) -> Option<String> {
    let trimmed = line.trim_start();
    let rest = trimmed
        .strip_prefix("async def ")
        .or_else(|| trimmed.strip_prefix("def "))?;
    let end = rest.find(|c: char| !c.is_alphanumeric() && c != '_')?;
    Some(rest[..end].to_string())
}

fn compress_python(content: &str) -> Option<Compressed> {
    let lines: Vec<&str> = content.lines().collect();
    let original_lines = lines.len();
    let mut out: Vec<String> = Vec::with_capacity(lines.len());
    let mut source_map: SourceMap = Vec::with_capacity(lines.len());
    let mut funcs_elided = 0usize;
    let mut lines_elided = 0usize;
    let mut elided_names: Vec<String> = Vec::new();
    let mut i = 0usize;
    process_python_range(
        &lines,
        &mut i,
        None,
        &mut out,
        &mut source_map,
        &mut funcs_elided,
        &mut lines_elided,
        &mut elided_names,
    );
    if funcs_elided == 0 {
        return None;
    }
    let mut text = out.join("\n");
    if content.ends_with('\n') {
        text.push('\n');
    }
    Some(Compressed {
        text,
        functions_elided: funcs_elided,
        lines_elided,
        original_lines,
        elided_function_names: elided_names,
        source_map,
    })
}

/// Walk Python lines eliding only `def` / `async def` bodies. Classes
/// are descended into so method signatures, decorators, attributes and
/// docstrings stay visible. `enclosing_indent = Some(N)` scopes the
/// walk to a class body; the walk returns when a non-blank line
/// dedents to or past that boundary.
#[allow(clippy::too_many_arguments)]
fn process_python_range(
    lines: &[&str],
    i: &mut usize,
    enclosing_indent: Option<usize>,
    out: &mut Vec<String>,
    source_map: &mut SourceMap,
    funcs_elided: &mut usize,
    lines_elided: &mut usize,
    elided_names: &mut Vec<String>,
) {
    while *i < lines.len() {
        let line = lines[*i];
        if let Some(enc) = enclosing_indent {
            if !line.trim().is_empty() && leading_ws(line) <= enc {
                return;
            }
        }
        if let Some(indent) = python_block_start(line) {
            let is_class = line.trim_start().starts_with("class ");
            let sig_first = *i;
            out.push(line.to_string());
            push_visible(source_map, out.len(), sig_first + 1);
            let mut sig_end = *i;
            while !lines[sig_end].trim_end().ends_with(':') && sig_end + 1 < lines.len() {
                sig_end += 1;
                out.push(lines[sig_end].to_string());
                push_visible(source_map, out.len(), sig_end + 1);
            }
            *i = sig_end + 1;
            if is_class {
                process_python_range(
                    lines,
                    i,
                    Some(indent),
                    out,
                    source_map,
                    funcs_elided,
                    lines_elided,
                    elided_names,
                );
            } else {
                let fn_name = python_signature_name(line);
                let body_start = *i;
                while *i < lines.len() {
                    let cur = lines[*i];
                    if cur.trim().is_empty() {
                        *i += 1;
                        continue;
                    }
                    let cur_indent = leading_ws(cur);
                    if cur_indent <= indent {
                        break;
                    }
                    *i += 1;
                }
                let body_len = *i - body_start;
                if body_len >= min_body_lines() {
                    let pad = " ".repeat(indent + 4);
                    let body_first_orig = body_start + 1;
                    let body_last_orig = *i;
                    out.push(format!(
                        "{pad}...  # [DRIP-elided: original L{body_first_orig}-L{body_last_orig}, {body_len} lines | drip refresh to expand]"
                    ));
                    source_map.push(SourceMapEntry {
                        compressed_line: out.len(),
                        original_start: body_first_orig,
                        original_end: body_last_orig,
                        symbol_name: fn_name.clone(),
                        elided: true,
                    });
                    *funcs_elided += 1;
                    *lines_elided += body_len.saturating_sub(1);
                    if let Some(n) = fn_name {
                        elided_names.push(n);
                    }
                } else {
                    for (offset, j) in lines[body_start..*i].iter().enumerate() {
                        out.push((*j).to_string());
                        push_visible(source_map, out.len(), body_start + offset + 1);
                    }
                }
            }
        } else {
            out.push(line.to_string());
            push_visible(source_map, out.len(), *i + 1);
            *i += 1;
        }
    }
}

/// 1-to-1 mapping for a verbatim line. Caller passes `out.len()` AFTER
/// pushing.
fn push_visible(source_map: &mut SourceMap, compressed_line: usize, original_line: usize) {
    source_map.push(SourceMapEntry {
        compressed_line,
        original_start: original_line,
        original_end: original_line,
        symbol_name: None,
        elided: false,
    });
}

// -------- Brace-balanced langs (Rust, JS/TS, Go) -------------------------

#[derive(Debug, Clone, Copy)]
enum BraceFlavor {
    Rust,
    JsTs,
    Go,
    /// Java / C / C++ / C# / Kotlin / Swift / Scala / PHP — shared
    /// brace + comment + string syntax. Line ends with `{`, contains
    /// `(...)`, doesn't start with a control/structural keyword.
    CFamily,
}

/// Keywords that must NEVER be treated as function-open lines.
/// Eliding an `if {...}` would hide real logic.
const CONTROL_KEYWORDS: &[&str] = &[
    "if",
    "else",
    "while",
    "for",
    "switch",
    "do",
    "try",
    "catch",
    "finally",
    "synchronized",
    "match",
    "loop",
    "unsafe",
];

/// Container-opening keywords (class / namespace / module). Their
/// inner method signatures are what we want the agent to see, so we
/// don't elide the container itself.
const STRUCTURAL_KEYWORDS: &[&str] = &[
    "class",
    "struct",
    "interface",
    "enum",
    "trait",
    "object",
    "namespace",
    "module",
    "package",
    "extension",
    "protocol",
    "impl",
    "union",
    "record",
];

/// Visibility / declaration modifiers that prefix a type or method
/// declaration in C-family languages. Stripped before classifying the
/// real keyword, so `public class Foo(int x)` is recognised as a type
/// declaration (with a primary constructor) rather than mis-classified
/// as a method because `class` isn't the very first word.
///
/// Kotlin-specific modifiers (`data`, `open`, `value`, `inline`,
/// Modifiers stripped before classifying a C-family declaration.
/// Includes Kotlin's `data`/`open`/`value`/`inline`/`annotation`/
/// `companion`/`fun` so `data class Foo(val x)` etc. resolve as type
/// declarations rather than methods.
const CFAMILY_MODIFIERS: &[&str] = &[
    "public ",
    "private ",
    "protected ",
    "internal ",
    "sealed ",
    "abstract ",
    "static ",
    "partial ",
    "readonly ",
    "unsafe ",
    "virtual ",
    "override ",
    "async ",
    "extern ",
    "final ",
    "synchronized ",
    "native ",
    "default ",
    // Kotlin-specific
    "data ",
    "open ",
    "value ",
    "inline ",
    "annotation ",
    "companion ",
    "fun ",
];

/// Strip every leading modifier so the caller sees the keyword that
/// actually opens the construct.
fn strip_cfamily_modifiers(head: &str) -> &str {
    let mut rest = head;
    'outer: loop {
        for m in CFAMILY_MODIFIERS {
            if let Some(r) = rest.strip_prefix(m) {
                rest = r;
                continue 'outer;
            }
        }
        return rest;
    }
}

/// True iff `head` declares a type. Tolerates visibility modifiers
/// and the C# `record class` / `record struct` pair. Short-circuits
/// primary-constructor lines like `public class Foo(int x)` that look
/// method-like to the brace heuristic.
fn is_type_declaration(head: &str, flavor: BraceFlavor) -> bool {
    if starts_with_word(head, STRUCTURAL_KEYWORDS) {
        return true;
    }
    if matches!(flavor, BraceFlavor::CFamily) {
        let bare = strip_cfamily_modifiers(head);
        if starts_with_word(bare, STRUCTURAL_KEYWORDS) {
            return true;
        }
        if let Some(rest) = bare.strip_prefix("record ") {
            if starts_with_word(rest.trim_start(), &["class", "struct"]) {
                return true;
            }
        }
    }
    false
}

/// True if `line` ends a function/method declaration with an opening
/// brace. The `{` must be the LAST non-whitespace char so struct
/// literals like `let x = Foo {` don't match.
fn is_func_open_line(line: &str, flavor: BraceFlavor) -> bool {
    let stripped = strip_line_comment(line, flavor).trim_end();
    if !stripped.ends_with('{') {
        return false;
    }
    let head = stripped[..stripped.len() - 1].trim_start();
    if starts_with_word(head, CONTROL_KEYWORDS) || is_type_declaration(head, flavor) {
        return false;
    }
    match flavor {
        BraceFlavor::Rust => head.contains("fn ") && head.contains('('),
        BraceFlavor::JsTs => {
            head.contains("function ")
                || head.contains("=>")
                || (head.contains('(') && head.contains(')'))
        }
        BraceFlavor::Go => head.starts_with("func "),
        BraceFlavor::CFamily => head.contains('(') && head.contains(')'),
    }
}

/// True if `line` is a method/function signature whose opening `{`
/// lives on a separate line (Allman style — C# default, common in
/// C++/Java). Caller must confirm the next non-blank line is `{`.
fn is_func_signature_line_allman(line: &str, flavor: BraceFlavor) -> bool {
    // Allman is C-family-only; other flavors are overwhelmingly K&R.
    if !matches!(flavor, BraceFlavor::CFamily) {
        return false;
    }
    let stripped = strip_line_comment(line, flavor).trim_end();
    if stripped.is_empty() {
        return false;
    }
    if stripped.ends_with(';') || stripped.ends_with('{') || stripped.ends_with(',') {
        return false;
    }
    if !(stripped.contains('(') && stripped.contains(')')) {
        return false;
    }
    let head = stripped.trim_start();
    if starts_with_word(head, CONTROL_KEYWORDS) || is_type_declaration(head, flavor) {
        return false;
    }
    // Skip attribute / annotation lines like `[Route("api")]`,
    // `@Cacheable(value = "x")` — they have `(...)` but aren't methods.
    if head.starts_with('[') || head.starts_with('@') {
        return false;
    }
    true
}

fn skip_blank_lines(lines: &[&str], mut idx: usize) -> usize {
    while idx < lines.len() && lines[idx].trim().is_empty() {
        idx += 1;
    }
    idx
}

/// Detect the *end* of a function-signature header. Three C-family
/// layouts: single-line K&R (`sig(args) {`), multi-line K&R (params
/// wrap, closing `) {` on a later line), and Allman (lone `{` on the
/// next non-blank line). Returns the index of the line ending with `{`.
fn detect_func_open(lines: &[&str], i: usize, flavor: BraceFlavor) -> Option<usize> {
    let line = lines[i];

    // Single-line K&R.
    if is_func_open_line(line, flavor) {
        return Some(i);
    }

    // Multi-line K&R: line ends with `{` and has more `)` than `(`.
    // Walk back tracking paren depth until we find the matching `(`.
    // That line must look like a real method declaration — otherwise
    // we'd elide value initialisers (`val x = build(\n …\n) { … }`)
    // or trailing-lambda blocks (`launch(\n …\n) { … }`).
    let stripped = strip_line_comment(line, flavor).trim_end();
    if let Some(head) = stripped.strip_suffix('{').map(str::trim_end) {
        let head_trim = head.trim_start();
        if !starts_with_word(head_trim, CONTROL_KEYWORDS) {
            let close_count = head.matches(')').count() as i32;
            let open_count = head.matches('(').count() as i32;
            let mut depth = close_count - open_count;
            if depth > 0 {
                let mut j = i;
                while j > 0 {
                    j -= 1;
                    let prev = strip_line_comment(lines[j], flavor);
                    depth += prev.matches(')').count() as i32;
                    depth -= prev.matches('(').count() as i32;
                    if depth <= 0 {
                        let prev_head = prev.trim_start();
                        if starts_with_word(prev_head, CONTROL_KEYWORDS)
                            || is_type_declaration(prev_head, flavor)
                        {
                            return None;
                        }
                        if !looks_like_method_signature_start(prev_head, flavor) {
                            return None;
                        }
                        return Some(i);
                    }
                }
            }
        }
    }

    // Allman.
    if is_func_signature_line_allman(line, flavor) {
        let brace_idx = skip_blank_lines(lines, i + 1);
        if brace_idx < lines.len() && lines[brace_idx].trim() == "{" {
            return Some(brace_idx);
        }
    }

    None
}

/// Conservative gate for the multi-line K&R case: does the line where
/// the `(` opens look like a method declaration? Anything starting
/// with `val`/`var`/`let`/`return`/etc., or with `=` before the `(`,
/// is rejected to avoid eliding trailing-lambda blocks as bodies.
fn looks_like_method_signature_start(head: &str, flavor: BraceFlavor) -> bool {
    match flavor {
        BraceFlavor::Rust => head.contains("fn "),
        BraceFlavor::Go => head.starts_with("func "),
        BraceFlavor::JsTs => true,
        BraceFlavor::CFamily => {
            const REJECT_PREFIXES: &[&str] = &[
                "val ",
                "var ",
                "let ",
                "const ",
                "return ",
                "throw ",
                "new ",
                "await ",
                "yield ",
                "co_await ",
                "co_yield ",
            ];
            if REJECT_PREFIXES.iter().any(|p| head.starts_with(p)) {
                return false;
            }
            // `=` before the first `(` ⇒ assignment-with-call.
            if let Some(paren_idx) = head.find('(') {
                if head[..paren_idx].contains('=') {
                    return false;
                }
            }
            true
        }
    }
}

/// True iff `head` starts with one of `keywords` followed by a space
/// or `(` — avoids matching `iframe` as `if`.
fn starts_with_word(head: &str, keywords: &[&str]) -> bool {
    for kw in keywords {
        if let Some(rest) = head.strip_prefix(kw) {
            match rest.chars().next() {
                Some(c) if c.is_whitespace() || c == '(' || c == '{' => return true,
                None => return true,
                _ => {}
            }
        }
    }
    false
}

/// True when DRIP shrinks Javadoc / KDoc / JSDoc blocks for `flavor`.
/// Rust's `///` line-doc style is left alone.
fn javadoc_compression_enabled(flavor: BraceFlavor) -> bool {
    if std::env::var("DRIP_COMPRESS_JAVADOC").as_deref() == Ok("0") {
        return false;
    }
    matches!(flavor, BraceFlavor::CFamily | BraceFlavor::JsTs)
}

/// One emitted Javadoc/KDoc/JSDoc line. `elided = true` is the
/// `[DRIP-javadoc-elided …]` marker; its range covers all dropped
/// lines.
#[derive(Debug, Clone)]
struct JdLine {
    text: String,
    orig_start: usize,
    orig_end: usize,
    elided: bool,
}

/// If `lines[start]` opens `/**`, compress the block: keep the summary
/// (first 2 non-blank, non-tag lines) and `@param`/`@return`/`@throws`/
/// `@since`/`@deprecated`/`@exception` tags verbatim; replace the rest
/// with a single `[DRIP-javadoc-elided …]` marker. Returns
/// `(closing_idx, rewritten_lines, lines_elided)` or `None` when the
/// block isn't worth compressing.
fn try_compress_javadoc(
    lines: &[&str],
    start: usize,
    flavor: BraceFlavor,
) -> Option<(usize, Vec<JdLine>, usize)> {
    if !javadoc_compression_enabled(flavor) {
        return None;
    }
    let opener = lines[start].trim_start();
    if !opener.starts_with("/**") {
        return None;
    }
    if opener.contains("*/") {
        return None;
    }
    let mut end = start + 1;
    while end < lines.len() {
        if lines[end].trim_end().ends_with("*/") {
            break;
        }
        end += 1;
    }
    if end == lines.len() {
        return None;
    }
    let block_len = end - start + 1;
    if block_len < 6 {
        return None;
    }

    let mut kept: Vec<usize> = Vec::with_capacity(block_len);
    let mut hit_tag = false;
    let mut summary_kept = 0usize;
    for (idx, line) in lines.iter().enumerate().take(end + 1).skip(start) {
        let trimmed = line
            .trim_start()
            .trim_start_matches('*')
            .trim_start_matches(' ')
            .trim_end();
        if idx == start || idx == end {
            kept.push(idx);
            continue;
        }
        if let Some(tag) = trimmed.split_whitespace().next() {
            if tag.starts_with('@') {
                if matches!(
                    tag,
                    "@param" | "@return" | "@throws" | "@since" | "@deprecated" | "@exception"
                ) {
                    kept.push(idx);
                    hit_tag = true;
                }
                continue;
            }
        }
        if trimmed.is_empty() {
            continue;
        }
        if !hit_tag && summary_kept < 2 {
            kept.push(idx);
            summary_kept += 1;
        }
    }
    let elided = block_len - kept.len();
    if elided < 2 {
        return None;
    }

    let indent = leading_ws(lines[start]);
    let pad = " ".repeat(indent);
    let mut out: Vec<JdLine> = Vec::with_capacity(kept.len() + 1);
    let last = *kept.last().unwrap();
    let kept_set: std::collections::HashSet<usize> = kept.iter().copied().collect();
    let drop_start = (start..=end)
        .find(|i| !kept_set.contains(i))
        .unwrap_or(start);
    let drop_end = (start..=end)
        .rev()
        .find(|i| !kept_set.contains(i))
        .unwrap_or(end);
    for idx in &kept {
        if *idx == last {
            out.push(JdLine {
                text: format!(
                    "{pad} * [DRIP-javadoc-elided: original L{}-L{}, {} lines | drip refresh for full]",
                    drop_start + 1,
                    drop_end + 1,
                    elided,
                ),
                orig_start: drop_start + 1,
                orig_end: drop_end + 1,
                elided: true,
            });
        }
        out.push(JdLine {
            text: lines[*idx].to_string(),
            orig_start: *idx + 1,
            orig_end: *idx + 1,
            elided: false,
        });
    }
    Some((end, out, elided))
}

fn strip_line_comment(line: &str, _flavor: BraceFlavor) -> &str {
    // Every brace flavor we support uses `//` as line comment.
    if let Some(idx) = find_outside_strings(line, "//") {
        &line[..idx]
    } else {
        line
    }
}

/// Find `needle` in `hay`, ignoring occurrences inside string literals.
/// Tracks `"…"`, `'…'`, and (for JS) `` `…` `` minimally — escapes count.
fn find_outside_strings(hay: &str, needle: &str) -> Option<usize> {
    let bytes = hay.as_bytes();
    let n = bytes.len();
    let nlen = needle.len();
    let mut i = 0;
    let mut state: Option<u8> = None; // active string delimiter, if any
    while i < n {
        let c = bytes[i];
        match state {
            Some(d) => {
                if c == b'\\' && i + 1 < n {
                    i += 2;
                    continue;
                }
                if c == d {
                    state = None;
                }
                i += 1;
            }
            None => {
                if c == b'"' || c == b'\'' || c == b'`' {
                    state = Some(c);
                    i += 1;
                    continue;
                }
                if i + nlen <= n && &bytes[i..i + nlen] == needle.as_bytes() {
                    return Some(i);
                }
                i += 1;
            }
        }
    }
    None
}

/// Find the line index of the `}` matching the `{` that opened the
/// scope just before `start`. Counts braces outside strings/comments.
fn find_matching_brace_end(lines: &[&str], start: usize, flavor: BraceFlavor) -> Option<usize> {
    let mut depth: i32 = 1;
    let mut in_block_comment = false;
    let mut i = start;
    while i < lines.len() {
        let mut line = lines[i].to_string();
        if !in_block_comment {
            line = strip_line_comment(&line, flavor).to_string();
        }
        let bytes = line.as_bytes();
        let n = bytes.len();
        let mut j = 0;
        let mut state: Option<u8> = None;
        while j < n {
            let c = bytes[j];
            if in_block_comment {
                if c == b'*' && j + 1 < n && bytes[j + 1] == b'/' {
                    in_block_comment = false;
                    j += 2;
                    continue;
                }
                j += 1;
                continue;
            }
            if state.is_none() && c == b'/' && j + 1 < n && bytes[j + 1] == b'*' {
                in_block_comment = true;
                j += 2;
                continue;
            }
            match state {
                Some(d) => {
                    if c == b'\\' && j + 1 < n {
                        j += 2;
                        continue;
                    }
                    if c == d {
                        state = None;
                    }
                    j += 1;
                    continue;
                }
                None => {
                    if c == b'"' || c == b'\'' || c == b'`' {
                        state = Some(c);
                        j += 1;
                        continue;
                    }
                    if c == b'{' {
                        depth += 1;
                    } else if c == b'}' {
                        depth -= 1;
                        if depth == 0 {
                            return Some(i);
                        }
                    }
                    j += 1;
                }
            }
        }
        i += 1;
    }
    None
}

fn compress_brace(content: &str, flavor: BraceFlavor) -> Option<Compressed> {
    let lines: Vec<&str> = content.lines().collect();
    let original_lines = lines.len();
    let mut out: Vec<String> = Vec::with_capacity(lines.len());
    let mut source_map: SourceMap = Vec::with_capacity(lines.len());
    let mut funcs_elided = 0;
    let mut lines_elided = 0;
    let mut elided_names: Vec<String> = Vec::new();
    let mut javadoc_blocks_compressed = 0usize;
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];

        // Javadoc / KDoc / JSDoc compression — runs first so a long
        // doc block above a short method still pays off even when the
        // body itself is too small to elide.
        if let Some((jd_end, jd_out, jd_elided)) = try_compress_javadoc(&lines, i, flavor) {
            for jd_line in jd_out {
                out.push(jd_line.text);
                source_map.push(SourceMapEntry {
                    compressed_line: out.len(),
                    original_start: jd_line.orig_start,
                    original_end: jd_line.orig_end,
                    symbol_name: None,
                    elided: jd_line.elided,
                });
            }
            lines_elided += jd_elided;
            javadoc_blocks_compressed += 1;
            i = jd_end + 1;
            continue;
        }

        // For multi-line K&R, the parameter-list lines between the `(`
        // opener and this closing `{` have already been pushed by
        // earlier iterations — they're part of the signature header.
        let sig_end_line = match detect_func_open(&lines, i, flavor) {
            Some(end) => end,
            None => {
                out.push(line.to_string());
                push_visible(&mut source_map, out.len(), i + 1);
                i += 1;
                continue;
            }
        };
        let body_start = sig_end_line + 1;

        // Signature start through `{` line, including continuations
        // and a lone Allman brace.
        for (offset, header_line) in lines[i..=sig_end_line].iter().enumerate() {
            out.push((*header_line).to_string());
            push_visible(&mut source_map, out.len(), i + offset + 1);
        }
        match find_matching_brace_end(&lines, body_start, flavor) {
            Some(end) => {
                let body_len = end - body_start;
                if body_len >= min_body_lines() {
                    let indent = (body_start..end)
                        .map(|j| lines[j])
                        .find(|l| !l.trim().is_empty())
                        .map(leading_ws)
                        .unwrap_or(4);
                    let pad = " ".repeat(indent);
                    let body_first_orig = body_start + 1;
                    let body_last_orig = end;
                    let fn_name = brace_signature_name(line, flavor);
                    out.push(format!(
                        "{pad}/* [DRIP-elided: original L{}-L{}, {} lines | drip refresh to expand] */",
                        body_first_orig, body_last_orig, body_len,
                    ));
                    source_map.push(SourceMapEntry {
                        compressed_line: out.len(),
                        original_start: body_first_orig,
                        original_end: body_last_orig,
                        symbol_name: fn_name.clone(),
                        elided: true,
                    });
                    out.push(lines[end].to_string());
                    push_visible(&mut source_map, out.len(), end + 1);
                    funcs_elided += 1;
                    lines_elided += body_len;
                    if let Some(n) = fn_name {
                        elided_names.push(n);
                    }
                    i = end + 1;
                    continue;
                }
                for (offset, j) in lines[body_start..=end].iter().enumerate() {
                    out.push((*j).to_string());
                    push_visible(&mut source_map, out.len(), body_start + offset + 1);
                }
                i = end + 1;
            }
            None => {
                // Unbalanced braces — bail safely (header already pushed).
                i = sig_end_line + 1;
            }
        }
    }
    if funcs_elided == 0 && javadoc_blocks_compressed == 0 {
        return None;
    }
    let mut text = out.join("\n");
    if content.ends_with('\n') {
        text.push('\n');
    }
    Some(Compressed {
        text,
        functions_elided: funcs_elided,
        lines_elided,
        original_lines,
        elided_function_names: elided_names,
        source_map,
    })
}

/// Function/method name on a brace-flavor open line.
fn brace_signature_name(line: &str, flavor: BraceFlavor) -> Option<String> {
    let stripped = strip_line_comment(line, flavor).trim_end();
    let head = if let Some(h) = stripped.strip_suffix('{') {
        h.trim()
    } else {
        stripped.trim()
    };
    match flavor {
        BraceFlavor::Rust => {
            let after = head.find("fn ").map(|i| &head[i + 3..])?;
            let end = after.find(|c: char| !c.is_alphanumeric() && c != '_')?;
            Some(after[..end].to_string())
        }
        BraceFlavor::JsTs => {
            if let Some(after) = head
                .find("function ")
                .map(|i| &head[i + "function ".len()..])
            {
                let end = after
                    .find(|c: char| !c.is_alphanumeric() && c != '_')
                    .unwrap_or(after.len());
                if end > 0 {
                    return Some(after[..end].to_string());
                }
            }
            // Arrow / method shorthand: last identifier before `(`.
            let paren = head.find('(')?;
            let pre = head[..paren].trim_end();
            let last_word_start = pre
                .rfind(|c: char| !c.is_alphanumeric() && c != '_')
                .map(|i| i + 1)
                .unwrap_or(0);
            let n = pre[last_word_start..].to_string();
            if n.is_empty() {
                None
            } else {
                Some(n)
            }
        }
        BraceFlavor::Go => {
            let after = head.strip_prefix("func ")?;
            let after = if after.starts_with('(') {
                let close = after.find(')')?;
                after[close + 1..].trim_start()
            } else {
                after
            };
            let end = after.find(|c: char| !c.is_alphanumeric() && c != '_')?;
            Some(after[..end].to_string())
        }
        BraceFlavor::CFamily => {
            // `type name(args)` — last identifier before `(`.
            let paren = head.find('(')?;
            let pre = head[..paren].trim_end();
            let last_word_start = pre
                .rfind(|c: char| !c.is_alphanumeric() && c != '_')
                .map(|i| i + 1)
                .unwrap_or(0);
            let n = pre[last_word_start..].to_string();
            if n.is_empty() {
                None
            } else {
                Some(n)
            }
        }
    }
}

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

    /// Compress tests mutate global env vars; serialise to avoid
    /// races between parallel tests' `set_var`/`remove_var`.
    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn lock_env() -> std::sync::MutexGuard<'static, ()> {
        ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner())
    }

    fn long_python_fn(name: &str) -> String {
        format!(
            "def {name}(x, y):\n    \
             a = x + y\n    \
             b = a * 2\n    \
             c = b - 1\n    \
             d = c ** 2\n    \
             return d\n"
        )
    }

    #[test]
    fn python_compresses_long_function() {
        let src = format!("import os\n\n{}", long_python_fn("foo")) + "\nprint('bye')\n";
        // Big enough to clear the min_bytes default? probably not. Bypass:
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let c = compress(&src, Language::Python).expect("expected compression");
        assert_eq!(c.functions_elided, 1);
        assert!(c.text.contains("DRIP-elided"));
        assert!(c.text.contains("def foo("));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn python_source_map_records_function_signature_visible_and_body_elided() {
        // Source layout (1-indexed):
        //   1: import os
        //   2: <blank>
        //   3: def foo(x, y):
        //   4-8: body lines
        //   9: <blank>
        //  10: print('bye')
        // Expected source map after compression:
        //   compressed L1 → orig L1 (visible)
        //   compressed L2 → orig L2 (visible)
        //   compressed L3 → orig L3 (signature, visible)
        //   compressed L4 → orig L4-L8 (elided body, symbol="foo")
        //   compressed L5 → orig L9 (visible)
        //   compressed L6 → orig L10 (visible)
        let src = format!("import os\n\n{}", long_python_fn("foo")) + "\nprint('bye')\n";
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let c = compress(&src, Language::Python).expect("compression should fire");
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");

        let by_compressed: std::collections::HashMap<usize, &SourceMapEntry> = c
            .source_map
            .iter()
            .map(|e| (e.compressed_line, e))
            .collect();
        assert!(
            !c.source_map.is_empty(),
            "source_map should be populated when compression fires",
        );
        // The signature line (`def foo(...)`) must appear as a 1-to-1
        // visible mapping at original line 3.
        let sig_entry = by_compressed
            .values()
            .find(|e| !e.elided && e.original_start == 3)
            .expect("signature entry");
        assert_eq!(sig_entry.original_start, sig_entry.original_end);
        // The elided stub MUST be present, MUST be flagged elided,
        // MUST carry the function name, MUST cover the body range.
        // The Python body walker consumes trailing blank lines as
        // part of the body (it can't tell where the function stops
        // and the next top-level whitespace begins until it sees a
        // non-indented non-blank line), so the elided range may
        // include the L9 blank that separates `foo` from `print`.
        let elided_entry = c
            .source_map
            .iter()
            .find(|e| e.elided)
            .expect("elided entry expected");
        assert_eq!(elided_entry.symbol_name.as_deref(), Some("foo"));
        assert!(
            elided_entry.original_start == 4 && (8..=9).contains(&elided_entry.original_end),
            "elided range {}-{} should start at L4 and end at L8 or L9",
            elided_entry.original_start,
            elided_entry.original_end,
        );
        // The stub text in `c.text` must include the new "original L{}-L{}" notice.
        assert!(
            c.text.contains(&format!(
                "original L{}-L{}",
                elided_entry.original_start, elided_entry.original_end
            )),
            "stub should embed the original line range, got text:\n{}",
            c.text
        );
    }

    #[test]
    fn brace_source_map_covers_body_range() {
        // 12-line Rust function — long enough to trigger elision.
        let body: String = (1..=12)
            .map(|i| format!("    let v_{i} = {i};\n"))
            .collect();
        let src = format!("fn outer() -> i32 {{\n{body}    42\n}}\n");
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let c = compress(&src, Language::Rust).expect("compression");
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");

        let elided = c
            .source_map
            .iter()
            .find(|e| e.elided)
            .expect("elided entry");
        assert_eq!(elided.symbol_name.as_deref(), Some("outer"));
        // Body lines are L2..=L14 (after the signature on L1 and
        // before the closing brace).
        assert!(
            elided.original_start == 2 && elided.original_end >= 13,
            "expected body span starting at L2, got {}-{}",
            elided.original_start,
            elided.original_end,
        );
        assert!(c.text.contains(&format!(
            "original L{}-L{}",
            elided.original_start, elided.original_end
        )));
    }

    #[test]
    fn source_map_has_one_entry_per_compressed_line() {
        // Invariant: every line of `Compressed::text` is represented
        // by exactly one source-map entry. Without this, callers
        // that scan by `compressed_line` (the `drip source-map
        // --line N` lookup, the pre-edit guard's overlap check) can
        // silently miss lines and return wrong answers.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        // Mix of long bodies (elided) + short helper (inlined) so the
        // map exercises both visible-line and elided-stub branches.
        let mut src = String::from("import os\n\n");
        src.push_str(&long_python_fn("big_one"));
        src.push('\n');
        src.push_str("def tiny():\n    return 1\n");
        src.push('\n');
        src.push_str(&long_python_fn("big_two"));
        let c = compress(&src, Language::Python).expect("compression");
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");

        let n_lines = c.text.lines().count();
        assert_eq!(
            c.source_map.len(),
            n_lines,
            "source_map length must equal compressed-text line count: \
             map={} lines={}\n--- compressed text ---\n{}\n--- source map ---\n{:#?}",
            c.source_map.len(),
            n_lines,
            c.text,
            c.source_map,
        );

        // Compressed-line numbers must cover 1..=n_lines without gaps.
        let mut seen: Vec<usize> = c.source_map.iter().map(|e| e.compressed_line).collect();
        seen.sort_unstable();
        let expected: Vec<usize> = (1..=n_lines).collect();
        assert_eq!(seen, expected, "compressed_line values must cover 1..=N");
    }

    #[test]
    fn javadoc_collapse_records_elided_range_in_source_map() {
        // A long /** Javadoc */ block above a function gets compressed
        // to a one-line stub. The source map must record the original
        // span so `drip source-map --line N` can answer "L4 of the
        // compressed view → original L2-L7" instead of the identity
        // mapping the line scanner would otherwise emit.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "package x;\n\
                   /**\n\
                    * Long javadoc block describing things in detail.\n\
                    * Continues for several lines so the collapser fires.\n\
                    * Includes @param annotations and notes.\n\
                    * Plus a final line of context.\n\
                    */\n\
                   public void worker() {\n\
                       int total = 0;\n\
                   }\n";
        let c = compress(src, Language::Java).expect("javadoc compression");
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
        let elided: Vec<_> = c.source_map.iter().filter(|e| e.elided).collect();
        assert!(
            !elided.is_empty(),
            "expected at least one elided source-map entry from javadoc collapse, got map:\n{:#?}",
            c.source_map
        );
        // At least one elided entry should span multiple original lines
        // (the javadoc block) — the function-body elision separately
        // emits its own entry, which is fine; we just need ONE that
        // covers the comment span.
        let multi_line_count = elided
            .iter()
            .filter(|e| e.original_end > e.original_start)
            .count();
        assert!(
            multi_line_count >= 1,
            "expected at least one multi-line elided entry (the javadoc block), got: {:#?}",
            elided
        );
    }

    #[test]
    fn python_short_body_kept_inline() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "def tiny():\n    return 1\n";
        assert!(compress(src, Language::Python).is_none());
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn python_class_methods_elided_individually_pricing_engine() {
        // Class bodies must be descended into; only def/async-def
        // bodies get elided — never the class itself.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
from decimal import Decimal
from typing import List

class PricingEngine:
    \"\"\"Moteur de tarification.\"\"\"

    DEFAULT_TAX = 0.20

    def __init__(self, registry: dict) -> None:
        self._registry = registry
        self._currency = \"EUR\"
        self._items = []
        self._tax = self.DEFAULT_TAX

    @property
    def currency(self) -> str:
        return self._currency

    @staticmethod
    def validate(amount: Decimal) -> bool:
        if amount < 0:
            return False
        if amount > 1000000:
            return False
        return True

    def compute(self, items: List[dict]) -> Decimal:
        total = Decimal(0)
        for item in items:
            price = Decimal(str(item[\"price\"]))
            qty = item.get(\"qty\", 1)
            line = price * qty
            total += line
        tax_amount = total * Decimal(str(self._tax))
        total += tax_amount
        rounded = total.quantize(Decimal(\"0.01\"))
        self._items.extend(items)
        self._last_total = rounded
        return rounded
";
        let c = compress(src, Language::Python).expect("expected compression");
        let t = &c.text;

        // Class signature visible.
        assert!(
            t.contains("class PricingEngine:"),
            "class header missing: {t}"
        );
        // Class docstring + attribute preserved.
        assert!(
            t.contains("\"\"\"Moteur de tarification.\"\"\""),
            "docstring missing: {t}"
        );
        assert!(
            t.contains("DEFAULT_TAX = 0.20"),
            "class attribute missing: {t}"
        );

        // Every method signature visible.
        assert!(t.contains("def __init__(self, registry: dict) -> None:"));
        assert!(t.contains("def currency(self) -> str:"));
        assert!(t.contains("def validate(amount: Decimal) -> bool:"));
        assert!(t.contains("def compute(self, items: List[dict]) -> Decimal:"));

        // Decorators preserved verbatim with correct indent.
        assert!(t.contains("    @property"), "@property missing: {t}");
        assert!(
            t.contains("    @staticmethod"),
            "@staticmethod missing: {t}"
        );

        // Short body (1 line) kept inline.
        assert!(
            t.contains("return self._currency"),
            "single-line body should stay inline: {t}"
        );

        // Long bodies elided. Three methods qualify (__init__: 4 lines,
        // validate: 5 lines, compute: 11 lines). currency is 1 line.
        assert_eq!(c.functions_elided, 3, "got: {t}");

        // The class body itself MUST NOT be elided as one block.
        // Concretely: the elision pad after `class PricingEngine:` is 4
        // spaces (method indent + 4), not 4 spaces aligned at the class
        // level — so ensure no top-level `    ...  # [DRIP-elided` line
        // appears immediately after the class header.
        let lines: Vec<&str> = t.lines().collect();
        for (idx, line) in lines.iter().enumerate() {
            if line.starts_with("class PricingEngine") {
                // Look at the next few non-blank lines: none of them
                // should be a class-level elision line.
                let mut k = idx + 1;
                while k < lines.len() && lines[k].trim().is_empty() {
                    k += 1;
                }
                if let Some(next) = lines.get(k) {
                    assert!(
                        !next.trim_start().starts_with("...  # [DRIP-elided")
                            || next.starts_with("        "),
                        "class body was elided wholesale: {next:?}"
                    );
                }
            }
        }

        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn python_class_with_only_short_methods_returns_none() {
        // No def has a long body → nothing to elide → compress returns
        // None. We don't elide the class itself.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Tiny:
    def a(self):
        return 1

    def b(self):
        return 2
";
        assert!(compress(src, Language::Python).is_none());
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn python_async_method_inside_class_elided() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Worker:
    async def run(self, payload):
        result = await self.fetch(payload)
        await self.persist(result)
        await self.notify(result)
        return result
";
        let c = compress(src, Language::Python).expect("expected compression");
        assert_eq!(c.functions_elided, 1);
        assert!(c.text.contains("class Worker:"));
        assert!(c.text.contains("async def run(self, payload):"));
        assert!(c.text.contains("DRIP-elided"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn python_nested_class_methods_visible() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Outer:
    class Inner:
        def deep(self):
            a = 1
            b = 2
            c = 3
            d = 4
            return a + b + c + d

    def shallow(self):
        x = 1
        y = 2
        z = 3
        w = 4
        return x + y + z + w
";
        let c = compress(src, Language::Python).expect("expected compression");
        assert_eq!(c.functions_elided, 2, "got: {}", c.text);
        assert!(c.text.contains("class Outer:"));
        assert!(c.text.contains("    class Inner:"));
        assert!(c.text.contains("def deep(self):"));
        assert!(c.text.contains("def shallow(self):"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn python_multiline_method_signature_preserved() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class API:
    def request(
        self,
        method: str,
        url: str,
        headers: dict,
    ) -> dict:
        conn = self._open()
        resp = conn.send(method, url, headers)
        body = resp.read()
        return self._parse(body)
";
        let c = compress(src, Language::Python).expect("expected compression");
        assert_eq!(c.functions_elided, 1);
        // Multi-line signature emitted in full.
        assert!(c.text.contains("def request("));
        assert!(c.text.contains("method: str,"));
        assert!(c.text.contains("headers: dict,"));
        assert!(c.text.contains(") -> dict:"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn python_top_level_function_alongside_class() {
        // Mixed: top-level def + class with methods. Both kinds of long
        // bodies should be elided independently.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
def helper(x):
    a = x + 1
    b = a * 2
    c = b - 3
    d = c ** 2
    return d

class Service:
    def run(self):
        a = 1
        b = 2
        c = 3
        d = 4
        return a + b + c + d
";
        let c = compress(src, Language::Python).expect("expected compression");
        assert_eq!(c.functions_elided, 2);
        assert!(c.text.contains("def helper(x):"));
        assert!(c.text.contains("class Service:"));
        assert!(c.text.contains("def run(self):"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn rust_compresses_function_bodies() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
pub fn foo(x: i32) -> i32 {
    let a = x + 1;
    let b = a * 2;
    let c = b - 1;
    let d = c.pow(2);
    d
}

pub fn bar() {
    println!(\"hi\");
}
";
        let c = compress(src, Language::Rust).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got: {}", c.text);
        assert!(c.text.contains("pub fn foo"));
        assert!(c.text.contains("DRIP-elided"));
        // Short fn `bar` stayed inline.
        assert!(c.text.contains("println!"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn js_arrow_function_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
const handler = (req, res) => {
    const body = req.body;
    const tag = body.tag;
    const value = body.value;
    res.send({ ok: true, tag, value });
    return value;
};
";
        let c = compress(src, Language::JavaScript).expect("expected compression");
        assert_eq!(c.functions_elided, 1);
        assert!(c.text.contains("(req, res) =>"));
        assert!(c.text.contains("DRIP-elided"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn no_compression_when_disabled() {
        std::env::set_var("DRIP_NO_COMPRESS", "1");
        let src = "def foo():\n    a = 1\n    b = 2\n    c = 3\n    d = 4\n    return d\n";
        assert!(compress(src, Language::Python).is_none());
        std::env::remove_var("DRIP_NO_COMPRESS");
    }

    #[test]
    fn brace_inside_string_does_not_break_balancing() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        // The literal '}' inside a string would close an unguarded
        // counter and truncate the body. Verify we handle it.
        let src = "\
fn foo() {
    let s = \"} not real\";
    let t = \"{ also fake\";
    let u = 1;
    let v = 2;
    println!(\"{}\", u + v);
}
";
        let c = compress(src, Language::Rust).expect("expected compression");
        assert_eq!(c.functions_elided, 1);
        // Make sure we didn't truncate inside the string.
        assert!(c.text.contains("DRIP-elided"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn detect_language_by_extension() {
        assert_eq!(detect_language(Path::new("a.py")), Language::Python);
        assert_eq!(detect_language(Path::new("b.rs")), Language::Rust);
        assert_eq!(detect_language(Path::new("c.tsx")), Language::TypeScript);
        assert_eq!(detect_language(Path::new("d.go")), Language::Go);
        assert_eq!(detect_language(Path::new("e.txt")), Language::Generic);
        // New language coverage
        assert_eq!(detect_language(Path::new("Foo.java")), Language::Java);
        assert_eq!(detect_language(Path::new("foo.c")), Language::C);
        assert_eq!(detect_language(Path::new("foo.h")), Language::C);
        assert_eq!(detect_language(Path::new("foo.cpp")), Language::Cpp);
        assert_eq!(detect_language(Path::new("foo.hpp")), Language::Cpp);
        assert_eq!(detect_language(Path::new("Foo.cs")), Language::CSharp);
        assert_eq!(detect_language(Path::new("Foo.kt")), Language::Kotlin);
        assert_eq!(detect_language(Path::new("Foo.swift")), Language::Swift);
        assert_eq!(detect_language(Path::new("Foo.scala")), Language::Scala);
        assert_eq!(detect_language(Path::new("foo.php")), Language::Php);
        // Case-insensitive
        assert_eq!(detect_language(Path::new("FOO.JAVA")), Language::Java);
    }

    #[test]
    fn java_compresses_methods_but_keeps_class_signatures_visible() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
package com.example;

public class UserService {
    public User findById(long id) {
        if (id <= 0) {
            throw new IllegalArgumentException(\"bad id\");
        }
        User user = repo.fetch(id);
        if (user == null) throw new NotFound();
        return user;
    }

    public void delete(long id) {
        repo.remove(id);
    }
}
";
        let c = compress(src, Language::Java).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got: {}", c.text);
        // Class declaration must remain visible — eliding it would hide
        // the entire method list, defeating the purpose.
        assert!(c.text.contains("public class UserService {"));
        // Long method elided.
        assert!(c.text.contains("public User findById"));
        assert!(c.text.contains("DRIP-elided"));
        // Short method kept inline.
        assert!(c.text.contains("repo.remove(id);"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn control_flow_blocks_are_never_elided() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public int compute(int n) {
    if (n < 0) {
        n = -n;
        n = n * 2;
        n = n + 1;
        n = n - 1;
    }
    return n;
}
";
        let c = compress(src, Language::Java).expect("expected compression");
        // The OUTER method body is elided (≥4 lines). The INNER `if`
        // would be inside that elided body, so we shouldn't see its
        // contents in the output.
        assert!(c.text.contains("DRIP-elided"));
        // But the if block's contents should NOT appear separately —
        // verify we didn't accidentally collapse just the `if {}`.
        let elision_count = c.text.matches("DRIP-elided").count();
        assert_eq!(
            elision_count, 1,
            "should elide the method body, NOT the inner if-block separately: {}",
            c.text
        );
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn cpp_compresses_function_with_namespace_qualified_name() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
#include <vector>

void Foo::bar(int x) {
    std::vector<int> v;
    v.push_back(x);
    v.push_back(x + 1);
    v.push_back(x + 2);
    process(v);
}
";
        let c = compress(src, Language::Cpp).expect("expected compression");
        assert_eq!(c.functions_elided, 1);
        assert!(c.text.contains("void Foo::bar(int x) {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_func_keyword_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Repo {
    suspend fun fetchAll(): List<User> {
        val raw = api.list()
        val parsed = raw.map { it.toUser() }
        val filtered = parsed.filter { it.active }
        return filtered.sortedBy { it.id }
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got: {}", c.text);
        assert!(c.text.contains("class Repo {"));
        assert!(c.text.contains("suspend fun fetchAll"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn swift_func_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
import Foundation

class Service {
    func fetchUsers(completion: @escaping ([User]) -> Void) {
        let url = URL(string: \"https://api.example.com\")!
        let task = session.dataTask(with: url) { data, _, error in
            guard let data = data, error == nil else { return }
            let users = try? decoder.decode([User].self, from: data)
            completion(users ?? [])
        }
        task.resume()
    }
}
";
        let c = compress(src, Language::Swift).expect("expected compression");
        assert!(c.functions_elided >= 1, "got: {}", c.text);
        assert!(c.text.contains("class Service {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn php_function_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "<?php
class UserController {
    public function show(int $id) {
        $user = $this->repo->find($id);
        if (!$user) {
            return response()->json(['error' => 'not found'], 404);
        }
        return response()->json($user);
    }
}
";
        let c = compress(src, Language::Php).expect("expected compression");
        assert_eq!(c.functions_elided, 1);
        assert!(c.text.contains("class UserController {"));
        assert!(c.text.contains("public function show"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn csharp_method_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
namespace MyApp.Services {
    public class UserService {
        public async Task<User> GetUserAsync(long id) {
            if (id <= 0) throw new ArgumentException(\"bad id\");
            var user = await _repo.FetchAsync(id);
            if (user == null) throw new NotFoundException();
            _logger.LogInformation(\"Fetched user {Id}\", id);
            return user;
        }
    }
}
";
        let c = compress(src, Language::CSharp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got: {}", c.text);
        assert!(c.text.contains("public class UserService {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn csharp_allman_method_compresses() {
        // Allman brace style is the C# default — `{` lives on its own
        // line below the signature. The original brace heuristic only
        // matched K&R, so every Allman-styled C# method slipped past.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class OrderService
{
    public async Task<Order?> GetByIdAsync(Guid id, CancellationToken ct)
    {
        _logger.LogDebug(\"Fetching {OrderId}\", id);
        var q = _db.Orders.AsNoTracking();
        q = q.Include(o => o.Items);
        q = q.Where(o => o.Id == id);
        return await q.FirstOrDefaultAsync(ct);
    }
}
";
        let c = compress(src, Language::CSharp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got: {}", c.text);
        assert!(c.text.contains("public async Task<Order?> GetByIdAsync"));
        // The lone Allman `{` line stays in the output; only the body
        // between it and the matching `}` is elided.
        assert!(c.text.contains("DRIP-elided"));
        // Class header and closing brace also preserved.
        assert!(c.text.contains("public class OrderService"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn csharp_allman_attributes_stay_with_signature() {
        // Attributes like [Authorize] / [HttpGet] live on lines above
        // the signature. They are not signatures themselves and must be
        // preserved verbatim — never elided as part of a body.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class UsersController
{
    [HttpGet(\"/users/{id}\")]
    [Authorize(Roles = \"admin\")]
    public async Task<IActionResult> Get(long id)
    {
        var u = await _svc.LookupAsync(id);
        if (u is null) return NotFound();
        var dto = _mapper.Map<UserDto>(u);
        _logger.LogInformation(\"served {Id}\", id);
        return Ok(dto);
    }
}
";
        let c = compress(src, Language::CSharp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("[HttpGet(\"/users/{id}\")]"));
        assert!(c.text.contains("[Authorize(Roles = \"admin\")]"));
        assert!(c.text.contains("public async Task<IActionResult> Get"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn csharp_primary_constructor_class_is_not_elided_as_method() {
        // `public class Foo(int x)` is a C# 12 primary-constructor
        // class declaration. The brace-balanced body is the CLASS body
        // — eliding it would erase every inner method signature, which
        // is exactly the structural information we want to preserve.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class CartTotals(decimal subtotal, decimal tax, decimal shipping)
{
    public decimal Subtotal { get; } = subtotal;
    public decimal Tax { get; } = tax;
    public decimal Shipping { get; } = shipping;
    public decimal Grand => Subtotal + Tax + Shipping;
    public bool IsTaxFree => Tax == 0m;
}
";
        // Either no compression at all, or specifically: the inner
        // method bodies are not elided (and the class body certainly
        // isn't elided as if it were a method).
        let result = compress(src, Language::CSharp);
        if let Some(c) = result {
            assert_eq!(
                c.functions_elided, 0,
                "primary-constructor class was incorrectly elided as a method: {}",
                c.text
            );
        }
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn csharp_record_with_semicolon_is_left_alone() {
        // Positional records that end in `;` have no body. The
        // compressor must leave them entirely alone (no signature
        // misclassification, no spurious elision attempt).
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public record CreateOrderLine(string Sku, int Quantity, decimal UnitPrice);
public record class Money(decimal Amount, string Currency);

public class Holder
{
    public void Use(CreateOrderLine line)
    {
        var s = line.Sku;
        var q = line.Quantity;
        var p = line.UnitPrice;
        Console.WriteLine($\"{s} x{q} @ {p}\");
        Console.WriteLine($\"total = {q * p}\");
    }
}
";
        let c = compress(src, Language::CSharp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        // Both record lines untouched.
        assert!(c.text.contains(
            "public record CreateOrderLine(string Sku, int Quantity, decimal UnitPrice);"
        ));
        assert!(c
            .text
            .contains("public record class Money(decimal Amount, string Currency);"));
        // The actual method's body got elided.
        assert!(c.text.contains("public void Use(CreateOrderLine line)"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn csharp_allman_with_generic_constraints_compresses() {
        // C# generics often carry a `where T : ...` constraint clause
        // after the parameter list. Allman places `{` on the next line
        // after the constraint — the signature line ends with
        // `class` / `struct` / `new()`, not `)`.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class Repo
{
    public T LoadOr<T>(Guid id, T fallback) where T : class, new()
    {
        var raw = _store.Read(id);
        if (raw is null) return fallback;
        var typed = _serializer.Deserialize<T>(raw);
        if (typed is null) return fallback;
        return typed;
    }
}
";
        let c = compress(src, Language::CSharp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("where T : class, new()"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_data_class_is_not_elided_as_method() {
        // `data class` is a Kotlin keyword. The class body holds
        // val/var declarations that the agent must see — eliding it
        // (because `data class Foo(val x: Int) {` ends with `{` and
        // contains `(...)`) would erase exactly the structural info
        // we care about.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
data class User(val id: UUID, val email: String, val displayName: String) {
    val isEmpty: Boolean = email.isEmpty()
    val isCorporate: Boolean = email.endsWith(\"@corp.example\")
    val displayLabel: String = if (isEmpty) \"(no email)\" else displayName
    fun isLikelyBot(): Boolean {
        val lower = email.lowercase()
        val patterns = listOf(\"bot\", \"noreply\", \"daemon\", \"automation\")
        return patterns.any { lower.contains(it) }
    }
}
";
        let result = compress(src, Language::Kotlin);
        // The data class itself must not be elided as a method body.
        // The inner method `isLikelyBot` may or may not be elided
        // depending on body length — that's fine — but the class
        // header must remain visible either way.
        if let Some(c) = &result {
            assert!(c.text.contains("data class User"));
            assert!(c.text.contains("val isEmpty"));
            assert!(c.text.contains("val isCorporate"));
        }
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_open_class_is_not_elided_as_method() {
        // `open class` allows subclassing — same risk as `data class`.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
open class Repo(private val db: Database) {
    open suspend fun load(id: UUID): Entity? {
        val raw = db.read(id)
        if (raw == null) return null
        val parsed = parse(raw)
        if (parsed == null) return null
        return validate(parsed)
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert!(c.text.contains("open class Repo"));
        // The inner method *can* be elided, but the class header stays.
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_value_class_is_not_elided_as_method() {
        // `value class` (formerly `inline class`) is a singleton
        // wrapper around a primitive — body holds member functions
        // that must stay visible.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
@JvmInline
value class Cents(val amount: Long) {
    operator fun plus(other: Cents): Cents = Cents(amount + other.amount)
    operator fun minus(other: Cents): Cents = Cents(amount - other.amount)
    fun toDollars(): Double = amount / 100.0
    override fun toString(): String = \"$\" + (amount / 100.0)
}
";
        let result = compress(src, Language::Kotlin);
        if let Some(c) = &result {
            assert!(c.text.contains("value class Cents"));
            assert!(c.text.contains("operator fun plus"));
            assert!(c.text.contains("operator fun minus"));
        }
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_fun_interface_is_not_elided_as_method() {
        // `fun interface` is Kotlin's SAM (Single Abstract Method)
        // interface keyword. The `fun` here is a *modifier* on the
        // interface, not the start of a function declaration —
        // eliding the interface body would erase the abstract method
        // signature.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
fun interface Predicate<T> {
    fun test(item: T): Boolean
}

class Holder {
    fun filter(items: List<Int>, p: Predicate<Int>): List<Int> {
        val out = mutableListOf<Int>()
        for (it in items) {
            if (p.test(it)) out.add(it)
        }
        return out
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert!(c.text.contains("fun interface Predicate<T>"));
        assert!(c.text.contains("fun test(item: T): Boolean"));
        // The implementation method `filter` *can* be elided.
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_multi_line_signature_compresses() {
        // Long parameter lists wrap across several lines per the
        // Kotlin style guide. The closing `): Type {` line carries the
        // body-opening brace but doesn't contain `(`, so the
        // single-line K&R heuristic misses it. The multi-line K&R
        // path detects this by walking back to the line that opened
        // the unmatched `(`.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Service {
    suspend fun processOrder(
        orderId: UUID,
        customerId: UUID,
        amount: Money,
        currency: String,
    ): OrderResult {
        validate(orderId)
        val ctx = lookup(customerId)
        val converted = convert(amount, currency)
        val saved = persist(orderId, ctx, converted)
        publish(saved)
        return OrderResult.success(saved)
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        // Param lines must remain in the output verbatim.
        assert!(c.text.contains("orderId: UUID,"));
        assert!(c.text.contains("currency: String,"));
        assert!(c.text.contains("): OrderResult {"));
        // And of course the body got the placeholder.
        assert!(c.text.contains("DRIP-elided"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_value_initializer_with_trailing_lambda_is_not_elided() {
        // The lookback heuristic for multi-line K&R must not
        // mis-classify `val x = build(...) { ... }` as a method
        // declaration. The `=` before the `(` is the giveaway.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Loader {
    fun setup(): Config {
        val cfg = configure(
            host,
            port,
            tls,
        ) {
            timeout = 30
            retries = 5
            backoff = 2.0
            useGzip = true
            useHttp2 = true
        }
        return cfg
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        // We expect the *outer* `setup()` method to be elided
        // (its body is long enough), but the inner `configure(...) {
        // ... }` block must NOT be detected as a separate method,
        // i.e. we should see exactly one elision, not two.
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_extension_fun_with_generics_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
fun <T> Flow<T>.bufferedPaged(pageSize: Int): Flow<List<T>> {
    val buffer = ArrayList<T>(pageSize)
    collect { value ->
        buffer += value
        if (buffer.size >= pageSize) {
            emit(buffer.toList())
            buffer.clear()
        }
    }
    return buffer
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("fun <T> Flow<T>.bufferedPaged"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_companion_object_fun_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Repo {
    companion object {
        fun fromConfig(config: Config): Repo {
            val client = HttpClient(config.host, config.port)
            val cache = LruCache<String, Any>(config.cacheSize)
            val mapper = ObjectMapper().apply { registerModule(KotlinModule()) }
            val timeoutMs = config.timeoutSeconds * 1000L
            val maxRetries = config.maxRetries.coerceAtLeast(1)
            return Repo(client, cache, mapper, timeoutMs, maxRetries)
        }
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("fun fromConfig"));
        assert!(c.text.contains("companion object"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_inline_reified_fun_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
inline fun <reified T> deserialize(data: String, mapper: ObjectMapper): T {
    val cleaned = data.trim().removePrefix(\"\\u0000\")
    if (cleaned.isEmpty()) {
        throw IllegalArgumentException(\"empty payload\")
    }
    val tree = mapper.readTree(cleaned)
    val node = tree.path(\"data\").takeIf { !it.isMissingNode } ?: tree
    return mapper.treeToValue(node, T::class.java)
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("inline fun <reified T> deserialize"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_visibility_modifier_fun_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Cache {
    private fun extractIdReflectively(entity: Any): UUID? {
        val cls = entity::class.java
        val field = cls.declaredFields.firstOrNull { it.name == \"id\" } ?: return null
        field.isAccessible = true
        return field.get(entity) as? UUID
    }
    internal fun buildQuery(filter: Filter): Query {
        val base = Query.from(\"items\")
        val withFilter = base.where(filter.toPredicate())
        val withSort = withFilter.orderBy(\"createdAt\")
        val limited = withSort.limit(filter.maxResults.coerceAtMost(1000))
        return limited
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert_eq!(c.functions_elided, 2, "got:\n{}", c.text);
        assert!(c.text.contains("private fun extractIdReflectively"));
        assert!(c.text.contains("internal fun buildQuery"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn kotlin_anonymous_object_override_fun_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Service {
    val cache = object : Cache<String, User> {
        override fun get(key: String): User? {
            val raw = backing.lookup(key) ?: return null
            val parsed = mapper.readValue<User>(raw)
            metrics.incrementHit(key)
            log.debug(\"cache hit for {}\", key)
            log.trace(\"value = {}\", parsed)
            return parsed
        }
    }
}
";
        let c = compress(src, Language::Kotlin).expect("expected compression");
        assert!(c.functions_elided >= 1, "got:\n{}", c.text);
        assert!(c.text.contains("override fun get"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn java_long_javadoc_is_compressed() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class Repo {
    /**
     * Finds a user by their unique identifier.
     *
     * <p>This method is cached and will return a stale result for up to
     * {@code TTL_SECONDS} seconds. Callers requiring strong consistency
     * must invalidate the cache explicitly via {@link #evictCache(UUID)}.
     *
     * <p>The lookup goes through the L1 entity cache first, then falls
     * back to the L2 region cache, then finally to the database.
     *
     * @param id the user identifier (non-null)
     * @return Optional containing the user, or empty if not found
     * @throws DataAccessException on database failure
     */
    public Optional<User> findById(UUID id) {
        Objects.requireNonNull(id);
        User cached = cache.get(id);
        if (cached != null) {
            return Optional.of(cached);
        }
        User fresh = entityManager.find(User.class, id);
        if (fresh != null) {
            cache.put(id, fresh);
        }
        return Optional.ofNullable(fresh);
    }
}
";
        let c = compress(src, Language::Java).expect("expected compression");
        assert!(c.text.contains("[DRIP-javadoc-elided"), "got:\n{}", c.text);
        // Tags must survive verbatim.
        assert!(c.text.contains("@param id"));
        assert!(c.text.contains("@return"));
        assert!(c.text.contains("@throws"));
        // Summary must survive (first line).
        assert!(c.text.contains("Finds a user by their unique identifier"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn java_short_javadoc_is_kept_full() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class Repo {
    /**
     * Counts every active user.
     * @return total active count
     */
    public long count() {
        long total = entityManager.createQuery(\"SELECT COUNT(u) FROM User u\", Long.class)
                .getSingleResult();
        if (total < 0) {
            throw new IllegalStateException(\"negative count\");
        }
        if (total > Integer.MAX_VALUE) {
            log.warn(\"user count exceeds int range: {}\", total);
        }
        return total;
    }
}
";
        let c = compress(src, Language::Java).expect("expected compression");
        // 4-line Javadoc must NOT be touched.
        assert!(!c.text.contains("[DRIP-javadoc-elided"), "got:\n{}", c.text);
        assert!(c.text.contains("Counts every active user"));
        assert!(c.text.contains("@return total active count"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn javadoc_compression_can_be_disabled_via_env() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        std::env::set_var("DRIP_COMPRESS_JAVADOC", "0");
        let src = "\
public class Repo {
    /**
     * Long doc.
     *
     * <p>Paragraph one explaining things at length.
     * <p>Paragraph two with even more details.
     * <p>Paragraph three for good measure.
     *
     * @param id the id
     * @return result
     */
    public User findById(UUID id) {
        return entityManager.find(User.class, id);
    }
}
";
        let c = compress(src, Language::Java);
        // With Javadoc compression off and a tiny body, there's nothing
        // left to elide → compress returns None.
        assert!(c.is_none() || !c.unwrap().text.contains("[DRIP-javadoc-elided"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
        std::env::remove_var("DRIP_COMPRESS_JAVADOC");
    }

    #[test]
    fn java_multi_line_signature_compresses() {
        // Same multi-line K&R behaviour for Java, where wrapping
        // long parameter lists is just as common.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class Reporter {
    public void emitMetric(
            String name,
            long value,
            Map<String, String> tags,
            Instant when) {
        Metric m = new Metric();
        m.setName(name);
        m.setValue(value);
        m.setTags(tags);
        m.setTimestamp(when);
        sink.write(m);
    }
}
";
        let c = compress(src, Language::Java).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("Map<String, String> tags,"));
        assert!(c.text.contains("Instant when) {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- Rust audit ----------------------------------------------------

    #[test]
    fn rust_async_fn_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
pub async fn fetch_all(client: &Client) -> Result<Vec<Item>, Error> {
    let resp = client.get(\"/items\").send().await?;
    let parsed: Vec<Item> = resp.json().await?;
    let filtered: Vec<Item> = parsed.into_iter().filter(|x| x.active).collect();
    let sorted = sort_by_priority(filtered);
    Ok(sorted)
}
";
        let c = compress(src, Language::Rust).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("pub async fn fetch_all"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn rust_impl_block_methods_elided_individually() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
impl Repo {
    pub fn new() -> Self {
        let inner = Vec::new();
        let cap = 16;
        let count = 0;
        Self { inner, cap, count }
    }

    pub fn push(&mut self, item: Item) -> usize {
        self.inner.push(item);
        let len = self.inner.len();
        self.count = len;
        let idx = len - 1;
        idx
    }
}
";
        let c = compress(src, Language::Rust).expect("expected compression");
        assert_eq!(c.functions_elided, 2, "got:\n{}", c.text);
        assert!(c.text.contains("impl Repo {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn rust_trait_with_default_body_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
pub trait Validator {
    fn name(&self) -> &str;

    fn validate(&self, value: &str) -> Result<(), String> {
        if value.is_empty() {
            return Err(\"empty\".into());
        }
        if value.len() > 256 {
            return Err(\"too long\".into());
        }
        Ok(())
    }
}
";
        let c = compress(src, Language::Rust).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("pub trait Validator"));
        assert!(c.text.contains("fn name(&self) -> &str;"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn rust_multi_line_signature_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
pub fn build_session(
    user_id: Uuid,
    ip: IpAddr,
    user_agent: String,
    config: &SessionConfig,
) -> Result<Session, Error> {
    let id = Uuid::new_v4();
    let now = Utc::now();
    let expires_at = now + config.absolute_timeout;
    let session = Session::new(id, user_id, ip, user_agent, now, expires_at);
    Ok(session)
}
";
        let c = compress(src, Language::Rust).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("user_id: Uuid,"));
        assert!(c.text.contains(") -> Result<Session, Error> {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn rust_struct_literal_is_not_a_function_open() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
pub fn make_thing() -> Thing {
    let x = 1;
    let y = 2;
    let z = 3;
    Thing { x, y, z, label: build_label(x, y, z) }
}
";
        let c = compress(src, Language::Rust).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- JavaScript / TypeScript audit -----------------------------------

    #[test]
    fn js_async_function_declaration_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
async function fetchAndStore(url, store) {
    const resp = await fetch(url);
    const json = await resp.json();
    const sanitised = sanitise(json);
    const persisted = await store.write(sanitised);
    return persisted;
}
";
        let c = compress(src, Language::JavaScript).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn js_generator_function_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
function* range(start, end, step) {
    let value = start;
    while (value < end) {
        yield value;
        const next = value + step;
        value = next;
    }
}
";
        let c = compress(src, Language::JavaScript).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn ts_class_methods_with_modifiers_compress() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
export class Service {
    public async fetch<T>(url: string): Promise<T> {
        const resp = await this.http.get(url);
        const text = await resp.text();
        const parsed = JSON.parse(text) as T;
        const cached = await this.cache.set(url, parsed);
        return cached;
    }

    private retry<T>(fn: () => Promise<T>, attempts: number): Promise<T> {
        const cap = Math.min(attempts, 5);
        const factor = 1.7;
        const out = this.runWithBackoff(fn, cap, factor);
        const wrapped = this.attachAbortLogic(out);
        return wrapped;
    }
}
";
        let c = compress(src, Language::TypeScript).expect("expected compression");
        assert_eq!(c.functions_elided, 2, "got:\n{}", c.text);
        assert!(c.text.contains("public async fetch<T>"));
        assert!(c.text.contains("private retry<T>"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn ts_type_guard_signature_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
export function isUser(value: unknown): value is User {
    if (value === null) return false;
    if (typeof value !== \"object\") return false;
    const v = value as Record<string, unknown>;
    if (typeof v.id !== \"string\") return false;
    if (typeof v.email !== \"string\") return false;
    return true;
}
";
        let c = compress(src, Language::TypeScript).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("isUser(value: unknown): value is User"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn ts_export_class_is_not_elided() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
export class Calculator {
    add(a: number, b: number): number {
        const x = a + 0;
        const y = b + 0;
        const z = x + y;
        const out = z;
        return out;
    }
}
";
        let c = compress(src, Language::TypeScript).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("export class Calculator"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- Go audit --------------------------------------------------------

    #[test]
    fn go_top_level_function_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
package handlers

func ProcessOrder(ctx context.Context, order *Order) error {
    if order == nil {
        return ErrNilOrder
    }
    if err := validate(order); err != nil {
        return err
    }
    if err := persist(ctx, order); err != nil {
        return err
    }
    return nil
}
";
        let c = compress(src, Language::Go).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("func ProcessOrder"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn go_method_receiver_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
func (s *Server) HandleCreate(w http.ResponseWriter, r *http.Request) {
    var req CreateRequest
    if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    user, err := s.store.Create(r.Context(), &req)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    w.WriteHeader(http.StatusCreated)
    json.NewEncoder(w).Encode(user)
}
";
        let c = compress(src, Language::Go).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("func (s *Server) HandleCreate"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn go_multi_line_signature_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
func ListUsers(
    ctx context.Context,
    filter *UserFilter,
    page int,
    pageSize int,
) ([]*User, int, error) {
    items, err := store.Find(ctx, filter, page, pageSize)
    if err != nil {
        return nil, 0, fmt.Errorf(\"list users: %w\", err)
    }
    total, err := store.Count(ctx, filter)
    if err != nil {
        return nil, 0, fmt.Errorf(\"count users: %w\", err)
    }
    return items, total, nil
}
";
        let c = compress(src, Language::Go).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("ctx context.Context,"));
        assert!(c.text.contains(") ([]*User, int, error) {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn go_interface_methods_stay_visible() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
type Store interface {
    Get(ctx context.Context, id string) (*Item, error)
    List(ctx context.Context, q string) ([]*Item, error)
    Save(ctx context.Context, item *Item) error
    Delete(ctx context.Context, id string) error
}

func RunSync(ctx context.Context, store Store) error {
    items, err := store.List(ctx, \"\")
    if err != nil {
        return err
    }
    for _, item := range items {
        if err := store.Save(ctx, item); err != nil {
            return err
        }
    }
    return nil
}
";
        let c = compress(src, Language::Go).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("type Store interface"));
        assert!(c
            .text
            .contains("Get(ctx context.Context, id string) (*Item, error)"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- C audit ---------------------------------------------------------

    #[test]
    fn c_function_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
#include <stddef.h>
#include <string.h>

static size_t parse_header(const char *buf, size_t len, header_t *out) {
    if (buf == NULL || out == NULL) return 0;
    if (len < HEADER_MIN_LEN) return 0;
    out->magic = read_u32_le(buf);
    out->version = read_u16_le(buf + 4);
    out->payload_len = read_u32_le(buf + 8);
    return HEADER_FIXED_LEN + out->payload_len;
}
";
        let c = compress(src, Language::C).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("static size_t parse_header"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn c_multi_line_function_signature_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
int compress_block(
        const uint8_t *src,
        size_t src_len,
        uint8_t *dst,
        size_t dst_cap,
        size_t *dst_len) {
    if (src == NULL || dst == NULL || dst_len == NULL) return -1;
    if (src_len == 0) {
        *dst_len = 0;
        return 0;
    }
    int rc = lz_compress(src, src_len, dst, dst_cap, dst_len);
    if (rc < 0) return rc;
    return 0;
}
";
        let c = compress(src, Language::C).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("size_t *dst_len) {"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn c_function_pointer_assignment_is_not_elided() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
int (*global_handler)(int) = NULL;

void register_handler(int (*fp)(int)) {
    if (fp == NULL) return;
    global_handler = fp;
    on_handler_registered();
    audit_log(\"handler set\");
    g_handler_count += 1;
}
";
        let c = compress(src, Language::C).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- C++ extra audit -------------------------------------------------

    #[test]
    fn cpp_template_method_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
template <typename T>
std::optional<T> Cache::get(const std::string &key) const {
    auto it = entries_.find(key);
    if (it == entries_.end()) return std::nullopt;
    if (is_expired(it->second.stored_at)) return std::nullopt;
    auto raw = it->second.bytes;
    auto parsed = deserialize<T>(raw);
    return parsed;
}
";
        let c = compress(src, Language::Cpp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("std::optional<T> Cache::get"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn cpp_constructor_with_member_init_list_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
Connection::Connection(const Endpoint &ep, std::chrono::seconds timeout)
    : endpoint_(ep), timeout_(timeout), state_(State::Connecting) {
    socket_.bind(ep);
    socket_.set_timeout(timeout);
    handshake_.begin();
    state_machine_.start();
    metrics_.connections_initiated += 1;
}
";
        let c = compress(src, Language::Cpp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("Connection::Connection"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- Swift audit -----------------------------------------------------

    #[test]
    fn swift_method_with_throws_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
struct Loader {
    func load(from url: URL) async throws -> [Item] {
        let (data, response) = try await session.data(from: url)
        guard let http = response as? HTTPURLResponse else {
            throw LoaderError.notHTTP
        }
        guard http.statusCode == 200 else {
            throw LoaderError.status(http.statusCode)
        }
        let items = try decoder.decode([Item].self, from: data)
        return items
    }
}
";
        let c = compress(src, Language::Swift).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("func load(from url: URL) async throws"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn swift_init_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Cache {
    init(capacity: Int, ttl: TimeInterval, evictionPolicy: EvictionPolicy) {
        self.capacity = capacity
        self.ttl = ttl
        self.evictionPolicy = evictionPolicy
        self.storage = [:]
        self.accessOrder = []
        self.hits = 0
    }
}
";
        let c = compress(src, Language::Swift).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("init(capacity: Int, ttl: TimeInterval"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn swift_extension_methods_compress() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
extension String {
    func sanitised() -> String {
        let trimmed = self.trimmingCharacters(in: .whitespaces)
        let lowered = trimmed.lowercased()
        let folded = lowered.folding(options: .diacriticInsensitive, locale: .current)
        let stripped = folded.replacingOccurrences(of: \"_\", with: \" \")
        return stripped
    }
}
";
        let c = compress(src, Language::Swift).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("extension String {"));
        assert!(c.text.contains("func sanitised()"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- Scala audit -----------------------------------------------------

    #[test]
    fn scala_def_method_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class UserService(repo: UserRepo) {
    def createUser(email: String, name: String): Either[Error, User] = {
        if (email.isEmpty) return Left(Error.EmailRequired)
        if (!email.contains(\"@\")) return Left(Error.InvalidEmail)
        val normalised = email.trim.toLowerCase
        val user = User(UUID.randomUUID(), normalised, name, Instant.now())
        val saved = repo.insert(user)
        Right(saved)
    }
}
";
        let c = compress(src, Language::Scala).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("class UserService"));
        assert!(c.text.contains("def createUser"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn scala_case_class_is_not_elided() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
case class Money(amount: BigDecimal, currency: String) {
    def +(other: Money): Money = {
        require(currency == other.currency, \"currency mismatch\")
        val total = amount + other.amount
        val out = Money(total, currency)
        val rounded = out.rounded
        rounded
    }
}
";
        let c = compress(src, Language::Scala).expect("expected compression");
        assert!(c.text.contains("case class Money"));
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn scala_object_methods_compress() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
object PriceFormatter {
    def format(amount: BigDecimal, currency: String, locale: Locale): String = {
        val rounded = amount.setScale(2, BigDecimal.RoundingMode.HALF_EVEN)
        val symbol = currencySymbol(currency, locale)
        val parts = rounded.toString.split(\"\\\\.\")
        val whole = groupDigits(parts(0), locale)
        val decimal = parts.lift(1).getOrElse(\"00\")
        symbol + whole + decimalSep(locale) + decimal
    }
}
";
        let c = compress(src, Language::Scala).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("object PriceFormatter"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- PHP audit -------------------------------------------------------

    #[test]
    fn php_class_method_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
<?php

class Mailer {
    public function send(string $to, string $subject, string $body): bool {
        if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
            return false;
        }
        $headers = $this->buildHeaders($to, $subject);
        $envelope = $this->buildEnvelope($to, $subject, $body);
        $result = $this->transport->deliver($envelope, $headers);
        $this->logger->info('sent', ['to' => $to, 'ok' => $result]);
        return $result;
    }
}
";
        let c = compress(src, Language::Php).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("public function send"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn php_static_method_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
<?php

class Tools {
    public static function normalize(string $value): string {
        $trimmed = trim($value);
        $lowered = strtolower($trimmed);
        $folded = iconv('UTF-8', 'ASCII//TRANSLIT', $lowered);
        $cleaned = preg_replace('/[^a-z0-9]+/', '-', $folded);
        $stripped = trim($cleaned ?? '', '-');
        return $stripped;
    }
}
";
        let c = compress(src, Language::Php).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("public static function normalize"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    // ---- Java extra audit ------------------------------------------------

    #[test]
    fn java_annotated_method_with_throws_compresses() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public class Handler {
    @Override
    @Transactional(readOnly = false)
    public Result process(Input input) throws ValidationException, IOException {
        if (input == null) {
            throw new ValidationException(\"null input\");
        }
        Result r = new Result();
        r.setStartedAt(Instant.now());
        r.setInput(input);
        r.setStatus(Status.PROCESSED);
        return r;
    }
}
";
        let c = compress(src, Language::Java).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("@Override"));
        assert!(c.text.contains("@Transactional(readOnly = false)"));
        assert!(c.text.contains("public Result process"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn java_record_with_body_keeps_inner_methods_visible() {
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
public record Range(int low, int high) {
    public Range {
        if (low > high) throw new IllegalArgumentException(\"low > high\");
    }

    public int span() {
        int diff = high - low;
        int abs = Math.abs(diff);
        int normalised = abs;
        int adjusted = normalised + 0;
        return adjusted;
    }
}
";
        let c = compress(src, Language::Java).expect("expected compression");
        assert!(c.text.contains("public record Range"));
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c.text.contains("public int span()"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }

    #[test]
    fn cpp_allman_method_compresses() {
        // Allman is also common in C++. Same compressor pathway.
        let _g = lock_env();
        std::env::set_var("DRIP_COMPRESS_MIN_BYTES", "0");
        std::env::set_var("DRIP_COMPRESS_MIN_BODY", "4");
        let src = "\
class Parser
{
    int parse_value(std::string_view in) const
    {
        if (in.empty()) return -1;
        auto t = next_token(in);
        if (!t) return -1;
        process(t);
        finalize();
        return 0;
    }
};
";
        let c = compress(src, Language::Cpp).expect("expected compression");
        assert_eq!(c.functions_elided, 1, "got:\n{}", c.text);
        assert!(c
            .text
            .contains("int parse_value(std::string_view in) const"));
        std::env::remove_var("DRIP_COMPRESS_MIN_BYTES");
    }
}