codewhale-tui 0.9.8

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

use std::collections::{HashMap, VecDeque};
use std::future::Future;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use anyhow::{Result, anyhow};
use futures_util::StreamExt;
use serde_json::{Value, json};
use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt, BufReader, Lines};
use tokio_util::sync::CancellationToken;

use crate::client::DeepSeekClient;
use crate::config::{ApiProvider, Config};
use crate::core::engine::turn_loop::run_tool_call_before_hooks;
use crate::core::engine::{
    AutoReviewPlanDecision, ToolAskRuleDecision, auto_review_plan_decision_for_context,
    exec_shell_ask_rule_decision_for_policy, file_tool_ask_rule_decision_for_policy,
};
use crate::llm_client::{LlmClient, StreamEventBox};
use crate::models::{
    ContentBlock, ContentBlockStart, Delta, Message, MessageRequest, StreamEvent, SystemPrompt,
};
use crate::tools::spec::{ApprovalRequirement, PreparedToolCall, RichToolResult, ToolError};
use crate::tools::{ToolContext, ToolRegistry, ToolRegistryBuilder};
use crate::worker_profile::ShellPolicy;

const ACP_PROTOCOL_VERSION: u64 = 1;

/// Hard cap on LLM <-> tool round-trips within a single `session/prompt`
/// turn. Guards against a model that never stops calling tools; each round is
/// one provider stream plus zero or more tool executions.
const MAX_ACP_TOOL_ROUNDS: usize = 50;

/// Maximum number of concurrent sessions kept in memory. When this limit is
/// exceeded, the oldest session with no in-flight prompt is evicted.
const MAX_ACP_SESSIONS: usize = 64;

/// A conforming ACP client answers every pending permission request with a
/// `cancelled` outcome when the prompt is cancelled. Bound that hand-off so a
/// broken client cannot strand the stdio server forever after cancellation.
const ACP_PERMISSION_CANCEL_GRACE: Duration = Duration::from_secs(2);

/// Agent-originated JSON-RPC request ids have their own namespace. Strings
/// avoid the client-specific numeric response-id compatibility shim used for
/// replies to client-originated requests.
static NEXT_ACP_PERMISSION_REQUEST_ID: AtomicU64 = AtomicU64::new(1);

/// Content is streamed to the model in full (no truncation); this cap only
/// bounds how much of a tool's output is echoed into the `tool_call_update`
/// notification the editor renders, so a large `File`/`Bash`
/// result does not flood the client UI.
const TOOL_CALL_CONTENT_PREVIEW_CHARS: usize = 4_000;

pub async fn run_acp_server(config: Config, model: String, default_cwd: PathBuf) -> Result<()> {
    let stdin = tokio::io::stdin();
    let stdout = tokio::io::stdout();
    let mut reader = BufReader::new(stdin).lines();
    let mut writer = tokio::io::BufWriter::new(stdout);
    let mut server = AcpServer::new(config, model, default_cwd);

    while let Some(line) = reader.next_line().await? {
        if line.trim().is_empty() {
            continue;
        }

        let message: Value = match serde_json::from_str(&line) {
            Ok(value) => value,
            Err(err) => {
                write_jsonrpc_error(&mut writer, None, -32700, format!("invalid json: {err}"))
                    .await?;
                continue;
            }
        };

        if message.get("jsonrpc").and_then(Value::as_str) != Some("2.0") {
            write_jsonrpc_error(
                &mut writer,
                message
                    .get("id")
                    .cloned()
                    .map(|id| server.response_id_policy.response_id(id)),
                -32600,
                "jsonrpc version must be 2.0",
            )
            .await?;
            continue;
        }

        let id = message.get("id").cloned();
        let method = match message.get("method").and_then(Value::as_str) {
            Some(method) => method,
            None if is_jsonrpc_response(&message) => {
                // A late response to an agent-originated request (most notably
                // permission after cancellation) has no request semantics and
                // must not be answered with another JSON-RPC error.
                continue;
            }
            None => {
                write_jsonrpc_error(
                    &mut writer,
                    id.map(|id| server.response_id_policy.response_id(id)),
                    -32600,
                    "missing method",
                )
                .await?;
                continue;
            }
        };
        let params = message.get("params").cloned().unwrap_or_else(|| json!({}));

        // `session/prompt` is driven concurrently with the reader so a
        // `session/cancel` can interrupt the in-flight provider call or a
        // running tool. Every other method is request/response and handled
        // synchronously below.
        if method == "session/prompt" {
            match server.begin_prompt(params) {
                Ok(prepared) => {
                    let PreparedPrompt {
                        session_id,
                        messages,
                        cwd,
                    } = prepared;
                    let response_id_policy = server.response_id_policy;
                    let Some(tool_registry) = server.session_tool_registry(&session_id) else {
                        let id = id.map(|id| response_id_policy.response_id(id));
                        write_jsonrpc_error(&mut writer, id, -32603, "unknown sessionId").await?;
                        continue;
                    };
                    // Freeze the first round's fully composed system prompt
                    // for this entire `session/prompt`. Tool calls may edit
                    // AGENTS.md, memory, or configured instruction files, but
                    // self-authored content cannot become same-turn system
                    // authority on a later provider round.
                    let frozen_system_prompt =
                        Arc::new(std::sync::Mutex::new(None::<SystemPrompt>));
                    // The stream-opening closure borrows `&server` only
                    // briefly per round; each returned `StreamEventBox` is
                    // `'static`, so it can be raced against the reader
                    // without holding a borrow on the server across an
                    // await, and the main task keeps exclusive ownership of
                    // stdout.
                    let outcome = run_agentic_prompt_turn(
                        AcpTurnContext {
                            config: &server.config,
                            model: &server.model,
                            session_id: &session_id,
                            tool_registry: &tool_registry,
                            response_id_policy,
                        },
                        messages,
                        &mut reader,
                        &mut writer,
                        |msgs| {
                            // Rebind to references before the `async move`
                            // block: `async move` moves every path it
                            // touches, and these are already-`Copy`
                            // references, so only `msgs` (the per-round
                            // owned clone) is actually moved in — `server`,
                            // `cwd`, and `tool_registry` stay borrowed from
                            // the enclosing scope across every call this
                            // `FnMut` closure makes.
                            let server = &server;
                            let cwd = &cwd;
                            let tool_registry = &tool_registry;
                            let frozen_system_prompt = Arc::clone(&frozen_system_prompt);
                            async move {
                                server
                                    .open_prompt_stream(
                                        &msgs,
                                        cwd,
                                        tool_registry,
                                        &frozen_system_prompt,
                                    )
                                    .await
                            }
                        },
                    )
                    .await;
                    match outcome {
                        Ok((PromptOutcome::Completed(_text), full_messages)) => {
                            // Chunks were already streamed; record the full
                            // conversation (including any tool rounds) for
                            // the next prompt.
                            server.commit_turn_messages(&session_id, full_messages);
                            if let Some(id) = id {
                                let id = response_id_policy.response_id(id);
                                write_jsonrpc_result(
                                    &mut writer,
                                    id,
                                    json!({ "stopReason": "end_turn" }),
                                )
                                .await?;
                            }
                        }
                        Ok((PromptOutcome::Cancelled, partial_messages)) => {
                            // The turn driver keeps complete receipts for every
                            // proposed tool call, including calls cancelled
                            // before execution, so partial side effects remain
                            // visible and no dangling tool_use block is stored.
                            server.commit_turn_messages(&session_id, partial_messages);
                            if let Some(id) = id {
                                let id = response_id_policy.response_id(id);
                                write_jsonrpc_result(
                                    &mut writer,
                                    id,
                                    json!({ "stopReason": "cancelled" }),
                                )
                                .await?;
                            }
                        }
                        Ok((PromptOutcome::MaxRounds(_text), full_messages)) => {
                            // Max rounds reached — commit what we have
                            // (unlike cancel, this is a normal completion).
                            server.commit_turn_messages(&session_id, full_messages);
                            if let Some(id) = id {
                                let id = response_id_policy.response_id(id);
                                write_jsonrpc_result(
                                    &mut writer,
                                    id,
                                    json!({ "stopReason": "max_turn_requests" }),
                                )
                                .await?;
                            }
                        }
                        Err(err) => {
                            if let Some(partial_messages) = err.partial_messages {
                                // A later provider round failed after one or
                                // more tools completed. Preserve those
                                // side-effect receipts in session history.
                                server.commit_turn_messages(&session_id, partial_messages);
                            } else {
                                // The user message was already pushed into
                                // session history by `begin_prompt`; roll it
                                // back when no tool receipt exists yet.
                                server.rollback_user_message(&session_id);
                            }
                            let id = id.map(|id| response_id_policy.response_id(id));
                            write_jsonrpc_error(&mut writer, id, -32603, err.source.to_string())
                                .await?;
                        }
                    }
                }
                Err(err) => {
                    let id = id.map(|id| server.response_id_policy.response_id(id));
                    write_jsonrpc_error(&mut writer, id, err.code, err.message).await?;
                }
            }
            continue;
        }

        match server.handle_request(method, params).await {
            Ok(AcpDispatch::Response(result)) => {
                if let Some(id) = id {
                    let id = server.response_id_policy.response_id(id);
                    write_jsonrpc_result(&mut writer, id, result).await?;
                }
            }
            Ok(AcpDispatch::Shutdown) => {
                if let Some(id) = id {
                    let id = server.response_id_policy.response_id(id);
                    write_jsonrpc_result(&mut writer, id, json!(null)).await?;
                }
                break;
            }
            Err(err) => {
                let id = id.map(|id| server.response_id_policy.response_id(id));
                write_jsonrpc_error(&mut writer, id, err.code, err.message).await?;
            }
        }
    }

    Ok(())
}

fn is_jsonrpc_response(message: &Value) -> bool {
    message.get("id").is_some()
        && (message.get("result").is_some() || message.get("error").is_some())
}

/// Outcome of a `session/prompt` turn driven against the input stream.
#[derive(Debug, PartialEq, Eq)]
enum PromptOutcome {
    /// The provider call finished first; carries the assistant text.
    Completed(String),
    /// A matching `session/cancel` arrived before the call finished.
    Cancelled,
    /// The turn reached the maximum number of tool-call round-trips.
    /// Carries whatever text the model produced in the final round.
    MaxRounds(String),
}

#[derive(Debug)]
struct AgenticPromptError {
    source: anyhow::Error,
    /// Present once at least one complete tool-result batch has been appended.
    /// Those receipts may describe real side effects and must survive a later
    /// provider failure.
    partial_messages: Option<Vec<Message>>,
}

impl std::fmt::Display for AgenticPromptError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.source.fmt(f)
    }
}

impl AgenticPromptError {
    fn new(source: anyhow::Error, messages: &[Message], has_tool_receipts: bool) -> Self {
        Self {
            source,
            partial_messages: has_tool_receipts.then(|| messages.to_vec()),
        }
    }
}

/// A tool call the model requested, assembled from streamed
/// `content_block_start` / `content_block_delta` / `content_block_stop`
/// events. `parse_error` is set when the accumulated `input_json_delta`
/// bytes did not parse as JSON — the call is still surfaced (rather than
/// silently dropped) so the model gets a clear tool-result error instead of
/// the turn hanging.
#[derive(Debug, Clone)]
struct PendingToolCall {
    id: String,
    name: String,
    input: Value,
    parse_error: Option<String>,
}

/// Accumulates one streamed `tool_use` content block until its
/// `content_block_stop`.
#[derive(Debug, Default)]
struct ToolUseAccumulator {
    id: String,
    name: String,
    initial_input: Value,
    buffer: String,
}

impl ToolUseAccumulator {
    fn finalize(self) -> PendingToolCall {
        if self.buffer.trim().is_empty() {
            return PendingToolCall {
                id: self.id,
                name: self.name,
                input: self.initial_input,
                parse_error: None,
            };
        }
        match serde_json::from_str(&self.buffer) {
            Ok(input) => PendingToolCall {
                id: self.id,
                name: self.name,
                input,
                parse_error: None,
            },
            Err(_) => PendingToolCall {
                id: self.id,
                name: self.name,
                input: json!({}),
                parse_error: Some(self.buffer),
            },
        }
    }
}

/// The text payload an ACP client should see for a given stream event, if any.
/// ACP baseline is text-only, so thinking/tool/control events carry no chunk.
fn stream_text_chunk(event: &StreamEvent) -> Option<&str> {
    match event {
        StreamEvent::ContentBlockDelta {
            delta: Delta::TextDelta { text },
            ..
        } => Some(text),
        StreamEvent::ContentBlockStart {
            content_block: ContentBlockStart::Text { text },
            ..
        } => Some(text),
        _ => None,
    }
}

/// Consume a provider response `stream`, emitting each text delta as a
/// `session/update` chunk, while concurrently watching `reader` for a
/// `session/cancel` targeting `session_id`.
///
/// This is the streaming + cancellation control point. It is generic over the
/// reader/writer and takes the boxed stream, so it is unit-tested with canned
/// in-memory streams and readers — no real provider call required. The caller
/// keeps the only writer, so streamed chunks and acknowledgements all stay on
/// the single protocol-clean stdout stream.
///
/// Returns [`PromptOutcome::Completed`] with the full accumulated text once the
/// stream ends (or emits `message_stop`), plus any `tool_use` blocks the model
/// emitted during the round, so the caller can execute them and continue the
/// turn. A matching `session/cancel` (request or notification form) ends it
/// early with [`PromptOutcome::Cancelled`] — dropping the stream aborts the
/// underlying provider connection. The turn is single-flight: a cancel for a
/// different session is acknowledged and ignored; any other concurrent *request*
/// is rejected with a clear error so the client is not left waiting;
/// notifications without an id are ignored.
async fn drive_prompt_stream<R, W>(
    mut stream: StreamEventBox,
    session_id: &str,
    response_id_policy: JsonRpcResponseIdPolicy,
    reader: &mut Lines<R>,
    writer: &mut W,
) -> Result<(PromptOutcome, Vec<PendingToolCall>)>
where
    R: AsyncBufRead + Unpin,
    W: AsyncWrite + Unpin,
{
    let mut accumulated = String::new();
    let mut tool_calls: Vec<PendingToolCall> = Vec::new();
    let mut pending_tool_uses: HashMap<u32, ToolUseAccumulator> = HashMap::new();
    // Once input closes mid-turn we stop selecting on the reader and just drain
    // the stream to completion, rather than spinning on repeated EOFs.
    let mut reader_open = true;
    loop {
        tokio::select! {
            event = stream.next() => {
                match event {
                    // Stream exhausted without an explicit stop: turn is done.
                    None => return Ok((PromptOutcome::Completed(accumulated), tool_calls)),
                    Some(Ok(event)) => {
                        if let Some(text) = stream_text_chunk(&event)
                            && !text.is_empty() {
                                accumulated.push_str(text);
                                write_session_update(writer, session_id, text.to_string()).await?;
                            }
                        match event {
                            StreamEvent::ContentBlockStart {
                                index,
                                content_block: ContentBlockStart::ToolUse { id, name, input, ..},
                            } => {
                                pending_tool_uses.insert(
                                    index,
                                    ToolUseAccumulator {
                                        id,
                                        name,
                                        initial_input: input,
                                        buffer: String::new(),
                                    },
                                );
                            }
                            StreamEvent::ContentBlockDelta {
                                index,
                                delta: Delta::InputJsonDelta { partial_json },
                            } => {
                                if let Some(acc) = pending_tool_uses.get_mut(&index) {
                                    acc.buffer.push_str(&partial_json);
                                }
                            }
                            StreamEvent::ContentBlockStop { index } => {
                                if let Some(acc) = pending_tool_uses.remove(&index) {
                                    tool_calls.push(acc.finalize());
                                }
                            }
                            StreamEvent::MessageStop => {
                                return Ok((PromptOutcome::Completed(accumulated), tool_calls));
                            }
                            StreamEvent::Error { error } => {
                                return Err(anyhow!("provider stream error: {error}"));
                            }
                            _ => {}
                        }
                    }
                    Some(Err(err)) => return Err(err),
                }
            }
            line = reader.next_line(), if reader_open => {
                let line = match line? {
                    Some(line) => line,
                    // Input closed mid-turn: stop watching it, keep draining.
                    None => {
                        reader_open = false;
                        continue;
                    }
                };
                if line.trim().is_empty() {
                    continue;
                }
                let message: Value = match serde_json::from_str(&line) {
                    Ok(value) => value,
                    Err(err) => {
                        write_jsonrpc_error(writer, None, -32700, format!("invalid json: {err}"))
                            .await?;
                        continue;
                    }
                };
                let id = message.get("id").cloned();
                match message.get("method").and_then(Value::as_str) {
                    Some("session/cancel") => {
                        let target = message.pointer("/params/sessionId").and_then(Value::as_str);
                        // A cancel with no sessionId is treated as targeting the
                        // single in-flight turn.
                        if target.is_none() || target == Some(session_id) {
                            if let Some(id) = id {
                                let id = response_id_policy.response_id(id);
                                write_jsonrpc_result(writer, id, json!(null)).await?;
                            }
                            // Dropping `stream` on return aborts the provider call.
                            return Ok((PromptOutcome::Cancelled, tool_calls));
                        }
                        // Cancel for some other session: acknowledge, keep going.
                        if let Some(id) = id {
                            let id = response_id_policy.response_id(id);
                            write_jsonrpc_result(writer, id, json!(null)).await?;
                        }
                    }
                    _ => {
                        // The turn is single-flight; do not silently drop a
                        // request the client expects a response to.
                        if let Some(id) = id {
                            let id = response_id_policy.response_id(id);
                            write_jsonrpc_error(
                                writer,
                                Some(id),
                                -32603,
                                "a session/prompt turn is already in progress",
                            )
                            .await?;
                        }
                    }
                }
            }
        }
    }
}

/// Outcome of executing one batch of tool calls from a single round.
enum ToolBatchOutcome {
    /// Every tool call ran to completion; carries the `tool_result` messages
    /// to append to the conversation, in call order.
    Completed(Vec<Message>),
    /// A matching `session/cancel` arrived while a tool was awaiting approval
    /// or running. Carries receipts for every proposed call so completed or
    /// partially completed side effects never disappear from history.
    Cancelled(Vec<Message>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum AcpToolAdmission {
    Auto,
    RequestPermission(String),
    Block(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum AcpPermissionDecision {
    Allow,
    Reject(String),
    Cancelled,
}

fn acp_shell_command_requests_detach(command: &str) -> bool {
    let mut single_quoted = false;
    let mut double_quoted = false;
    let mut escaped = false;
    let chars = command.chars().collect::<Vec<_>>();
    for (index, ch) in chars.iter().copied().enumerate() {
        if escaped {
            escaped = false;
            continue;
        }
        if ch == '\\' && !single_quoted {
            escaped = true;
            continue;
        }
        if ch == '\'' && !double_quoted {
            single_quoted = !single_quoted;
            continue;
        }
        if ch == '"' && !single_quoted {
            double_quoted = !double_quoted;
            continue;
        }
        if ch != '&' || single_quoted || double_quoted {
            continue;
        }
        let previous = index.checked_sub(1).and_then(|i| chars.get(i)).copied();
        let next = chars.get(index + 1).copied();
        // `&&`, `&>`/`&>>`, and `>&` are chaining/redirection rather than a
        // detached child. Any other unquoted ampersand is a background
        // control operator and is unavailable in ACP.
        if previous != Some('&') && next != Some('&') && next != Some('>') && previous != Some('>')
        {
            return true;
        }
    }

    shell_words::split(command).is_ok_and(|words| {
        words.iter().any(|word| {
            matches!(
                word.to_ascii_lowercase().as_str(),
                "nohup" | "disown" | "setsid" | "daemonize"
            )
        })
    })
}

#[derive(Debug)]
struct PreparedAcpTool {
    call: PreparedToolCall,
    admission: AcpToolAdmission,
    additional_context: Option<String>,
}

/// Prepare one registered call and fold every policy layer that can tighten
/// its admission. ACP deliberately does not use the TUI's workspace-write
/// carve-out: a remembered exact allow rule may clear the ordinary tool hold,
/// but the built-in safety floor and repository law can always re-add a prompt
/// or hard block afterwards.
fn prepare_acp_tool_admission(
    config: &Config,
    registry: &ToolRegistry,
    call: &PendingToolCall,
) -> std::result::Result<(PreparedToolCall, AcpToolAdmission), ToolError> {
    let spec = registry.get(&call.name).ok_or_else(|| {
        ToolError::not_available(format!("tool '{}' is not registered", call.name))
    })?;
    let prepared = spec.prepare(call.input.clone(), registry.context())?;
    let canonical_name =
        crate::tools::canonical_action::canonical_action_alias(&call.name, &prepared.input);
    if matches!(call.name.as_str(), "bash" | "Bash" | "exec_shell")
        || canonical_name.starts_with("exec_shell")
    {
        let action = prepared
            .input
            .get("action")
            .and_then(Value::as_str)
            .unwrap_or("run");
        let requests_stateful_shell = action != "run"
            || prepared.starts_detached
            || prepared.input.get("interactive").and_then(Value::as_bool) == Some(true)
            || prepared.input.get("persist").and_then(Value::as_bool) == Some(true)
            || prepared
                .input
                .get("command")
                .and_then(Value::as_str)
                .is_some_and(acp_shell_command_requests_detach);
        if requests_stateful_shell {
            return Ok((
                prepared,
                AcpToolAdmission::Block(
                    "ACP v0.9.6 exposes foreground Bash runs only; background, TTY, interactive, persistent, and background-task control actions are unavailable."
                        .to_string(),
                ),
            ));
        }
    }
    let mut permission_reason =
        (prepared.approval != ApprovalRequirement::Auto).then(|| prepared.description.clone());
    let approval_mode = crate::tui::approval::ApprovalMode::Suggest;
    let workspace = registry.context().workspace.as_path();

    let typed_rule = exec_shell_ask_rule_decision_for_policy(
        &config.exec_policy_engine,
        &call.name,
        &prepared.input,
        workspace,
        approval_mode,
    )
    .or_else(|| {
        file_tool_ask_rule_decision_for_policy(
            &config.exec_policy_engine,
            &call.name,
            &prepared.input,
            workspace,
            approval_mode,
        )
    });
    match typed_rule {
        Some(ToolAskRuleDecision::Allow) => permission_reason = None,
        Some(ToolAskRuleDecision::Prompt(reason)) => permission_reason = Some(reason),
        Some(ToolAskRuleDecision::Block(reason)) => {
            return Ok((prepared, AcpToolAdmission::Block(reason)));
        }
        None => {}
    }

    let run_origin = if prepared.starts_detached {
        crate::tui::auto_review::RunOrigin::Background
    } else {
        crate::tui::auto_review::RunOrigin::Headless
    };
    let review_context = crate::tui::auto_review::AutoReviewContext::from_tool_call(
        &call.name,
        &prepared.input,
        run_origin,
        approval_mode,
        crate::config::is_workspace_trusted(workspace),
        Some(workspace),
    );
    let (auto_review, _audit) =
        auto_review_plan_decision_for_context(&config.auto_review_policy(), &review_context);
    match auto_review {
        AutoReviewPlanDecision::NoChange | AutoReviewPlanDecision::Allow => {}
        AutoReviewPlanDecision::ForcePrompt(reason) => permission_reason = Some(reason),
        // Headless adapters keep the deterministic-only tier: a fallback hold
        // that interactive Auto posture would send to the model guardian is
        // a hard block here (the reviewer is an interactive-session feature).
        AutoReviewPlanDecision::ConsultReviewer(reason) | AutoReviewPlanDecision::Block(reason) => {
            return Ok((prepared, AcpToolAdmission::Block(reason)));
        }
    }

    if let Some(repo_law) =
        crate::repo_law::repo_law_plan_decision(workspace, &call.name, &prepared.input)
    {
        match repo_law {
            crate::repo_law::RepoLawPlanDecision::ForcePrompt(reason) => {
                permission_reason = Some(reason);
            }
            crate::repo_law::RepoLawPlanDecision::Block(reason) => {
                return Ok((prepared, AcpToolAdmission::Block(reason)));
            }
        }
    }

    let admission = permission_reason
        .map(AcpToolAdmission::RequestPermission)
        .unwrap_or(AcpToolAdmission::Auto);
    Ok((prepared, admission))
}

/// Run the same strict pre-tool hook gate as the native turn loop, then
/// prepare and evaluate policy from the hook's final input. The initial
/// preparation is deliberately side-effect free and catches malformed input
/// before an operator hook is asked to reason about it; any rewrite is fully
/// re-prepared and all policy layers run again from that rewritten value.
async fn prepare_acp_tool_with_hooks(
    config: &Config,
    model: &str,
    registry: &ToolRegistry,
    call: &PendingToolCall,
) -> std::result::Result<PreparedAcpTool, ToolError> {
    let spec = registry.get(&call.name).ok_or_else(|| {
        ToolError::not_available(format!("tool '{}' is not registered", call.name))
    })?;
    // Initial validation mirrors the native prepare-before-hooks contract.
    spec.prepare(call.input.clone(), registry.context())?;

    let hook_outcome = run_tool_call_before_hooks(
        registry.context().runtime.hook_executor.as_ref(),
        &call.name,
        &call.id,
        &call.input,
        crate::tui::app::AppMode::Agent,
        registry.context().workspace.as_path(),
        model,
    )
    .await?;

    let mut final_call = call.clone();
    if let Some(updated_input) = hook_outcome.updated_input {
        final_call.input = updated_input;
    }
    let (prepared, mut admission) = prepare_acp_tool_admission(config, registry, &final_call)?;
    if hook_outcome.requires_approval && matches!(admission, AcpToolAdmission::Auto) {
        admission = AcpToolAdmission::RequestPermission(
            "A ToolCallBefore hook requires explicit approval for this call.".to_string(),
        );
    }

    Ok(PreparedAcpTool {
        call: prepared,
        admission,
        additional_context: hook_outcome.additional_context,
    })
}

fn next_acp_permission_request_id() -> Value {
    Value::String(format!(
        "codewhale-permission-{}",
        NEXT_ACP_PERMISSION_REQUEST_ID.fetch_add(1, Ordering::Relaxed)
    ))
}

async fn write_tool_permission_request<W>(
    writer: &mut W,
    request_id: &Value,
    session_id: &str,
    call: &PendingToolCall,
    reason: &str,
) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    write_json_line(
        writer,
        json!({
            "jsonrpc": "2.0",
            "id": request_id,
            "method": "session/request_permission",
            "params": {
                "sessionId": session_id,
                "toolCall": {
                    "toolCallId": call.id,
                    "title": tool_call_title(call),
                    "kind": tool_call_kind(call),
                    "status": "pending",
                    "rawInput": call.input,
                    "content": [{
                        "type": "content",
                        "content": { "type": "text", "text": reason }
                    }]
                },
                "options": [
                    {
                        "optionId": "allow-once",
                        "name": "Allow once",
                        "kind": "allow_once"
                    },
                    {
                        "optionId": "reject-once",
                        "name": "Reject",
                        "kind": "reject_once"
                    }
                ]
            }
        }),
    )
    .await
}

/// Ask the ACP client to approve one sensitive call. The client owns the UI
/// and ACP v1 requires it to answer a pending request with `cancelled` when the
/// prompt turn is cancelled. Unknown, malformed, or errored responses all fail
/// closed as rejection; only the exact offered `allow-once` id authorizes work.
async fn request_tool_permission<R, W>(
    reader: &mut Lines<R>,
    writer: &mut W,
    response_id_policy: JsonRpcResponseIdPolicy,
    session_id: &str,
    call: &PendingToolCall,
    reason: &str,
) -> Result<AcpPermissionDecision>
where
    R: AsyncBufRead + Unpin,
    W: AsyncWrite + Unpin,
{
    let request_id = next_acp_permission_request_id();
    write_tool_permission_request(writer, &request_id, session_id, call, reason).await?;
    let mut cancel_deadline = None;

    loop {
        let line = if let Some(deadline) = cancel_deadline {
            match tokio::time::timeout_at(deadline, reader.next_line()).await {
                Ok(line) => line?,
                Err(_) => return Ok(AcpPermissionDecision::Cancelled),
            }
        } else {
            reader.next_line().await?
        };
        let Some(line) = line else {
            return Ok(AcpPermissionDecision::Reject(
                "Permission denied: ACP client disconnected before answering.".to_string(),
            ));
        };
        if line.trim().is_empty() {
            continue;
        }
        let message: Value = match serde_json::from_str(&line) {
            Ok(value) => value,
            Err(err) => {
                write_jsonrpc_error(writer, None, -32700, format!("invalid json: {err}")).await?;
                continue;
            }
        };

        if let Some(method) = message.get("method").and_then(Value::as_str) {
            let message_id = message.get("id").cloned();
            if method == "session/cancel" {
                let target = message.pointer("/params/sessionId").and_then(Value::as_str);
                if target.is_none() || target == Some(session_id) {
                    if let Some(message_id) = message_id {
                        let message_id = response_id_policy.response_id(message_id);
                        write_jsonrpc_result(writer, message_id, json!(null)).await?;
                    }
                    cancel_deadline.get_or_insert_with(|| {
                        tokio::time::Instant::now() + ACP_PERMISSION_CANCEL_GRACE
                    });
                    continue;
                }
                if let Some(message_id) = message_id {
                    let message_id = response_id_policy.response_id(message_id);
                    write_jsonrpc_result(writer, message_id, json!(null)).await?;
                }
                continue;
            }

            if let Some(message_id) = message_id {
                let message_id = response_id_policy.response_id(message_id);
                write_jsonrpc_error(
                    writer,
                    Some(message_id),
                    -32603,
                    "a session/prompt turn is already in progress",
                )
                .await?;
            }
            continue;
        }

        if message.get("id") != Some(&request_id) {
            // This is a response, not a request; there is nothing valid to send
            // back. Ignore stale/unrelated agent-response traffic without
            // allowing it to satisfy the permission gate.
            continue;
        }
        if cancel_deadline.is_some() {
            return Ok(AcpPermissionDecision::Cancelled);
        }
        if message.get("jsonrpc").and_then(Value::as_str) != Some("2.0") {
            return Ok(AcpPermissionDecision::Reject(
                "Permission denied: malformed ACP response.".to_string(),
            ));
        }
        if message.get("error").is_some() {
            return Ok(AcpPermissionDecision::Reject(
                "Permission denied: ACP client returned an error.".to_string(),
            ));
        }
        match message
            .pointer("/result/outcome/outcome")
            .and_then(Value::as_str)
        {
            Some("cancelled") => return Ok(AcpPermissionDecision::Cancelled),
            Some("selected") => {
                let option_id = message
                    .pointer("/result/outcome/optionId")
                    .and_then(Value::as_str);
                return Ok(match option_id {
                    Some("allow-once") => AcpPermissionDecision::Allow,
                    Some("reject-once") => AcpPermissionDecision::Reject(
                        "Permission denied by the user; the tool was not executed.".to_string(),
                    ),
                    _ => AcpPermissionDecision::Reject(
                        "Permission denied: ACP client selected an unknown option.".to_string(),
                    ),
                });
            }
            _ => {
                return Ok(AcpPermissionDecision::Reject(
                    "Permission denied: malformed ACP response.".to_string(),
                ));
            }
        }
    }
}

async fn record_tool_execution_result<W>(
    writer: &mut W,
    session_id: &str,
    call: &PendingToolCall,
    result: std::result::Result<RichToolResult, ToolError>,
) -> Result<Message>
where
    W: AsyncWrite + Unpin,
{
    let (content, is_error, rich_blocks) = match result {
        Ok(tool_result) => (
            tool_result.result.content,
            !tool_result.result.success,
            tool_result.content_blocks,
        ),
        Err(err) => (format!("Error: {err}"), true, Vec::new()),
    };
    let status = if is_error { "failed" } else { "completed" };
    write_tool_call_update_with_blocks(
        writer,
        session_id,
        call,
        status,
        Some(&content),
        &rich_blocks,
    )
    .await?;
    Ok(tool_result_message_with_blocks(
        &call.id,
        content,
        is_error,
        rich_blocks
            .iter()
            .filter_map(|block| serde_json::to_value(block).ok())
            .collect(),
    ))
}

async fn record_unstarted_cancelled_calls<W, I>(
    writer: &mut W,
    session_id: &str,
    calls: I,
) -> Result<Vec<Message>>
where
    W: AsyncWrite + Unpin,
    I: IntoIterator<Item = PendingToolCall>,
{
    let mut messages = Vec::new();
    for call in calls {
        write_tool_call_start(writer, session_id, &call).await?;
        let content = "Cancelled before execution; the tool was not run.";
        write_tool_call_update(writer, session_id, &call, "failed", Some(content)).await?;
        messages.push(tool_result_message(&call.id, content.to_string(), true));
    }
    Ok(messages)
}

#[derive(Clone, Copy)]
struct AcpTurnContext<'a> {
    config: &'a Config,
    model: &'a str,
    session_id: &'a str,
    tool_registry: &'a ToolRegistry,
    response_id_policy: JsonRpcResponseIdPolicy,
}

/// Execute `tool_calls` in order against `registry`, reporting each one to
/// the client as `tool_call` / `tool_call_update` session updates, while
/// racing every execution against the reader for a `session/cancel`
/// targeting `session_id`. On cancel, the tool's [`CancellationToken`] is
/// signalled and the in-flight call is awaited to completion (so a
/// cancel-aware tool like `Bash` gets a chance to kill its child
/// process) before returning [`ToolBatchOutcome::Cancelled`].
async fn execute_tool_calls_with_cancellation<R, W>(
    context: AcpTurnContext<'_>,
    tool_calls: Vec<PendingToolCall>,
    reader: &mut Lines<R>,
    writer: &mut W,
) -> Result<ToolBatchOutcome>
where
    R: AsyncBufRead + Unpin,
    W: AsyncWrite + Unpin,
{
    let AcpTurnContext {
        config,
        model,
        session_id,
        tool_registry: registry,
        response_id_policy,
    } = context;
    let mut result_messages = Vec::with_capacity(tool_calls.len());
    let mut reader_open = true;
    let mut calls = tool_calls.into_iter();

    while let Some(mut call) = calls.next() {
        write_tool_call_start(writer, session_id, &call).await?;

        if let Some(parse_error) = call.parse_error.clone() {
            let content = format!("Error: tool arguments were not valid JSON: {parse_error}");
            write_tool_call_update(writer, session_id, &call, "failed", Some(&content)).await?;
            result_messages.push(tool_result_message(&call.id, content, true));
            continue;
        }

        let prepared = match prepare_acp_tool_with_hooks(config, model, registry, &call).await {
            Ok(prepared) => prepared,
            Err(err) => {
                let content = format!("Error: {err}");
                write_tool_call_update(writer, session_id, &call, "failed", Some(&content)).await?;
                result_messages.push(tool_result_message(&call.id, content, true));
                continue;
            }
        };
        call.input = prepared.call.input;

        match prepared.admission {
            AcpToolAdmission::Auto => {}
            AcpToolAdmission::Block(reason) => {
                let content = format!("Blocked by Codewhale policy: {reason}");
                write_tool_call_update(writer, session_id, &call, "failed", Some(&content)).await?;
                result_messages.push(tool_result_message(&call.id, content, true));
                continue;
            }
            AcpToolAdmission::RequestPermission(reason) => {
                match request_tool_permission(
                    reader,
                    writer,
                    response_id_policy,
                    session_id,
                    &call,
                    &reason,
                )
                .await?
                {
                    AcpPermissionDecision::Allow => {}
                    AcpPermissionDecision::Reject(content) => {
                        write_tool_call_update(writer, session_id, &call, "failed", Some(&content))
                            .await?;
                        result_messages.push(tool_result_message(&call.id, content, true));
                        continue;
                    }
                    AcpPermissionDecision::Cancelled => {
                        let content = "Cancelled while awaiting permission; the tool was not run.";
                        write_tool_call_update(writer, session_id, &call, "failed", Some(content))
                            .await?;
                        result_messages.push(tool_result_message(
                            &call.id,
                            content.to_string(),
                            true,
                        ));
                        result_messages.extend(
                            record_unstarted_cancelled_calls(writer, session_id, calls).await?,
                        );
                        return Ok(ToolBatchOutcome::Cancelled(result_messages));
                    }
                }
            }
        }

        write_tool_call_update(writer, session_id, &call, "in_progress", None).await?;

        let cancel_token = CancellationToken::new();
        let mut turn_context = registry.context().clone();
        turn_context.cancel_token = Some(cancel_token.clone());
        let exec_fut = registry.execute_rich_full_with_context(
            &call.name,
            call.input.clone(),
            Some(&turn_context),
        );
        tokio::pin!(exec_fut);

        let mut cancelled = false;
        let exec_result = loop {
            tokio::select! {
                result = &mut exec_fut => break result,
                line = reader.next_line(), if reader_open => {
                    let line = match line? {
                        Some(line) => line,
                        None => {
                            reader_open = false;
                            continue;
                        }
                    };
                    if line.trim().is_empty() {
                        continue;
                    }
                    let message: Value = match serde_json::from_str(&line) {
                        Ok(value) => value,
                        Err(err) => {
                            write_jsonrpc_error(writer, None, -32700, format!("invalid json: {err}"))
                                .await?;
                            continue;
                        }
                    };
                    let msg_id = message.get("id").cloned();
                    match message.get("method").and_then(Value::as_str) {
                        Some("session/cancel") => {
                            let target = message.pointer("/params/sessionId").and_then(Value::as_str);
                            if target.is_none() || target == Some(session_id) {
                                if let Some(msg_id) = msg_id {
                                    let msg_id = response_id_policy.response_id(msg_id);
                                    write_jsonrpc_result(writer, msg_id, json!(null)).await?;
                                }
                                cancel_token.cancel();
                                // Give the tool a chance to observe the token and
                                // wind down (e.g. kill a running child process)
                                // before we drop it.
                                cancelled = true;
                                break (&mut exec_fut).await;
                            }
                            if let Some(msg_id) = msg_id {
                                let msg_id = response_id_policy.response_id(msg_id);
                                write_jsonrpc_result(writer, msg_id, json!(null)).await?;
                            }
                        }
                        _ => {
                            if let Some(msg_id) = msg_id {
                                let msg_id = response_id_policy.response_id(msg_id);
                                write_jsonrpc_error(
                                    writer,
                                    Some(msg_id),
                                    -32603,
                                    "a session/prompt turn is already in progress",
                                )
                                .await?;
                            }
                        }
                    }
                }
            }
        };

        let exec_result = exec_result.map(|mut result| {
            if let Some(context) = prepared.additional_context.as_deref() {
                result.result.content =
                    format!("{}\n\n[hook context] {context}", result.result.content);
            }
            result
        });
        result_messages
            .push(record_tool_execution_result(writer, session_id, &call, exec_result).await?);
        if cancelled {
            result_messages
                .extend(record_unstarted_cancelled_calls(writer, session_id, calls).await?);
            return Ok(ToolBatchOutcome::Cancelled(result_messages));
        }
    }

    Ok(ToolBatchOutcome::Completed(result_messages))
}

fn tool_result_message(tool_use_id: &str, content: String, is_error: bool) -> Message {
    tool_result_message_with_blocks(tool_use_id, content, is_error, Vec::new())
}

fn tool_result_message_with_blocks(
    tool_use_id: &str,
    content: String,
    is_error: bool,
    content_blocks: Vec<Value>,
) -> Message {
    Message {
        role: "user".to_string(),
        content: vec![ContentBlock::ToolResult {
            tool_use_id: tool_use_id.to_string(),
            content,
            is_error: Some(is_error),
            content_blocks: (!content_blocks.is_empty()).then_some(content_blocks),
        }],
    }
}

/// Drive one `session/prompt` turn to completion, looping through as many
/// LLM <-> tool round-trips as the model requests (bounded by
/// [`MAX_ACP_TOOL_ROUNDS`]).
///
/// `open_stream` opens a fresh provider stream for the given message
/// history; production callers wire it to [`AcpServer::open_prompt_stream`],
/// while tests supply canned per-round streams so the loop can be exercised
/// without a real provider. Returns the outcome of the final round plus the
/// full message history (including any tool-call/tool-result rounds), which
/// the caller commits to session history only when the turn completed
/// normally.
///
/// `open_stream` takes the message history *by value* (a clone per round)
/// rather than `&[Message]`: an `async fn`'s returned future captures the
/// lifetime of every reference parameter, so a borrowed slice here would
/// force `Fut` to depend on each call's borrow lifetime — which `FnMut`'s
/// single associated `Fut` type cannot express. Taking ownership sidesteps
/// that; production callers move the clone into an `async move` block.
async fn run_agentic_prompt_turn<R, W, F, Fut>(
    context: AcpTurnContext<'_>,
    mut messages: Vec<Message>,
    reader: &mut Lines<R>,
    writer: &mut W,
    mut open_stream: F,
) -> std::result::Result<(PromptOutcome, Vec<Message>), AgenticPromptError>
where
    R: AsyncBufRead + Unpin,
    W: AsyncWrite + Unpin,
    F: FnMut(Vec<Message>) -> Fut,
    Fut: Future<Output = Result<StreamEventBox>>,
{
    let AcpTurnContext {
        session_id,
        response_id_policy,
        ..
    } = context;
    let mut has_tool_receipts = false;
    for _round in 0..MAX_ACP_TOOL_ROUNDS {
        let stream = open_stream(messages.clone())
            .await
            .map_err(|error| AgenticPromptError::new(error, &messages, has_tool_receipts))?;
        let (outcome, tool_calls) =
            drive_prompt_stream(stream, session_id, response_id_policy, reader, writer)
                .await
                .map_err(|error| AgenticPromptError::new(error, &messages, has_tool_receipts))?;

        let text = match outcome {
            PromptOutcome::Cancelled => return Ok((PromptOutcome::Cancelled, messages)),
            PromptOutcome::Completed(text) => text,
            PromptOutcome::MaxRounds(text) => text,
        };

        let mut assistant_content = Vec::new();
        if !text.is_empty() {
            assistant_content.push(ContentBlock::Text {
                text: text.clone(),
                cache_control: None,
            });
        }
        for call in &tool_calls {
            assistant_content.push(ContentBlock::ToolUse {
                id: call.id.clone(),
                name: call.name.clone(),
                input: call.input.clone(),
                caller: None,
                thought_signature: None,
            });
        }
        if !assistant_content.is_empty() {
            messages.push(Message {
                role: "assistant".to_string(),
                content: assistant_content,
            });
        }

        if tool_calls.is_empty() {
            return Ok((PromptOutcome::Completed(text), messages));
        }

        let batch = execute_tool_calls_with_cancellation(context, tool_calls, reader, writer)
            .await
            .map_err(|error| AgenticPromptError::new(error, &messages, has_tool_receipts))?;
        match batch {
            ToolBatchOutcome::Cancelled(tool_result_messages) => {
                messages.extend(tool_result_messages);
                return Ok((PromptOutcome::Cancelled, messages));
            }
            ToolBatchOutcome::Completed(tool_result_messages) => {
                messages.extend(tool_result_messages);
                has_tool_receipts = true;
            }
        }
    }

    // Max rounds reached: return the text accumulated in the final round
    // rather than an error, so the client gets a structured completion
    // with a clear stop reason.
    let final_text = messages
        .iter()
        .rev()
        .find(|m| m.role == "assistant")
        .and_then(|m| {
            m.content.iter().find_map(|b| match b {
                ContentBlock::Text { text, .. } => Some(text.clone()),
                _ => None,
            })
        })
        .unwrap_or_default();
    Ok((PromptOutcome::MaxRounds(final_text), messages))
}

struct AcpServer {
    config: Config,
    model: String,
    default_cwd: PathBuf,
    sessions: HashMap<String, AcpSession>,
    /// Insertion-order tracking of session ids. Used to evict the *oldest*
    /// session (by insertion order, not arbitrary HashMap iteration) when
    /// the session cap is reached.
    insertion_order: VecDeque<String>,
    /// Whether the connected client accepts `terminal` tool calls, from
    /// `initialize` params `clientCapabilities.terminal`. Defaults to `false`
    /// (restrictive): clients that omit the field get no shell access. Older
    /// ACP clients predating the `terminal` capability get a working agent
    /// without shell, which is safe; the client can re-declare support when it
    /// reconnects.
    client_supports_terminal: bool,
    response_id_policy: JsonRpcResponseIdPolicy,
}

struct AcpSession {
    cwd: PathBuf,
    messages: Vec<Message>,
    /// Built once per session over the session `cwd`, then reused for every
    /// prompt turn: `to_api_tools()` memoises the serialised catalog, and
    /// `file_read_tracker` / the shell manager need to persist across turns.
    tool_registry: Arc<ToolRegistry>,
}

/// The `&mut self` result of validating a `session/prompt`: the user turn is
/// already recorded, and the cloned conversation + cwd are ready for the
/// borrow-free provider call that the prompt driver races against cancellation.
struct PreparedPrompt {
    session_id: String,
    messages: Vec<Message>,
    cwd: PathBuf,
}

enum AcpDispatch {
    Response(Value),
    Shutdown,
}

#[derive(Debug)]
struct AcpError {
    code: i32,
    message: String,
}

impl AcpServer {
    fn new(config: Config, model: String, default_cwd: PathBuf) -> Self {
        Self {
            config,
            model,
            default_cwd,
            sessions: HashMap::new(),
            insertion_order: VecDeque::new(),
            client_supports_terminal: false,
            response_id_policy: JsonRpcResponseIdPolicy::Preserve,
        }
    }

    // `session/prompt` is handled in the main loop (it needs to run concurrently
    // with the reader for cancellation); every other method is request/response.
    async fn handle_request(
        &mut self,
        method: &str,
        params: Value,
    ) -> std::result::Result<AcpDispatch, AcpError> {
        match method {
            "initialize" => {
                if let Some(terminal) = params
                    .pointer("/clientCapabilities/terminal")
                    .and_then(Value::as_bool)
                {
                    self.client_supports_terminal = terminal;
                }
                self.response_id_policy = JsonRpcResponseIdPolicy::from_initialize_params(&params);
                Ok(AcpDispatch::Response(initialize_result(
                    params.get("protocolVersion").and_then(Value::as_u64),
                    &self.config,
                )))
            }
            "session/new" => Ok(AcpDispatch::Response(self.new_session(params)?)),
            "session/listProviders" => Ok(AcpDispatch::Response(self.list_providers())),
            "session/currentModel" => Ok(AcpDispatch::Response(self.current_model())),
            "session/selectModel" => Ok(AcpDispatch::Response(self.select_model(params)?)),
            // A cancel that arrives with no prompt in flight is an idempotent
            // no-op (the in-flight case is handled by the prompt driver).
            "session/cancel" => Ok(AcpDispatch::Response(json!(null))),
            "shutdown" => Ok(AcpDispatch::Shutdown),
            _ => Err(AcpError::method_not_found(method)),
        }
    }

    fn new_session(&mut self, params: Value) -> std::result::Result<Value, AcpError> {
        let cwd = params
            .get("cwd")
            .and_then(Value::as_str)
            .map(PathBuf::from)
            .unwrap_or_else(|| self.default_cwd.clone());
        let session_id = format!("codewhale-{}", uuid::Uuid::new_v4());
        let tool_registry = Arc::new(build_acp_tool_registry(
            &self.config,
            &cwd,
            self.client_supports_terminal,
        ));

        // Evict oldest session when at capacity.
        if self.sessions.len() >= MAX_ACP_SESSIONS {
            // `VecDeque` preserves true insertion order; HashMap iteration
            // does not. Pop from the front to evict the session created
            // earliest.
            if let Some(oldest) = self.insertion_order.pop_front() {
                self.sessions.remove(&oldest);
            }
        }

        self.insertion_order.push_back(session_id.clone());
        self.sessions.insert(
            session_id.clone(),
            AcpSession {
                cwd,
                messages: Vec::new(),
                tool_registry,
            },
        );
        Ok(json!({ "sessionId": session_id }))
    }

    fn session_tool_registry(&self, session_id: &str) -> Option<Arc<ToolRegistry>> {
        self.sessions
            .get(session_id)
            .map(|session| session.tool_registry.clone())
    }

    fn list_providers(&self) -> Value {
        let mut providers = ApiProvider::sorted_for_display()
            .into_iter()
            .map(|provider| {
                json!({
                    "id": provider.as_str(),
                    "displayName": provider.display_name(),
                    "defaultModel": provider.metadata().map(|metadata| metadata.default_model())
                })
            })
            .collect::<Vec<_>>();

        // Include user-defined `[providers.<name>]` custom entries so ACP
        // clients can discover and round-trip the provider names that
        // `session/selectModel` now accepts (#1519).
        if let Some(custom) = self.config.providers.as_ref().map(|p| &p.custom) {
            let mut names = custom.keys().collect::<Vec<_>>();
            names.sort();
            for name in names {
                providers.push(json!({
                    "id": name,
                    "displayName": name,
                    "defaultModel": custom.get(name).and_then(|cfg| cfg.model.clone())
                }));
            }
        }

        json!({ "providers": providers })
    }

    fn current_model(&self) -> Value {
        // Prefer the raw configured provider key so a custom `[providers.<name>]`
        // entry round-trips through ACP instead of canonicalizing to "custom".
        let provider = match self.config.provider.as_deref() {
            Some(name) if !name.trim().is_empty() => name.to_string(),
            _ => self.config.api_provider().as_str().to_string(),
        };
        json!({
            "provider": provider,
            "model": self.model.as_str()
        })
    }

    fn select_model(&mut self, params: Value) -> std::result::Result<Value, AcpError> {
        let model = params
            .get("model")
            .and_then(Value::as_str)
            .ok_or_else(|| AcpError::invalid_params("model is required"))?
            .to_string();

        if let Some(provider_value) = params.get("provider") {
            let provider_name = provider_value
                .as_str()
                .ok_or_else(|| AcpError::invalid_params("provider must be a string"))?;
            // Accept either a built-in provider id/alias or a user-defined
            // custom provider name that has a `[providers.<name>]` table. For
            // custom providers, preserve the raw key so routing can still find
            // the configured base URL / auth / model (#1519); canonicalizing to
            // "custom" would lose that table key.
            let is_custom = self
                .config
                .providers
                .as_ref()
                .and_then(|providers| providers.custom_provider_config(provider_name))
                .is_some();
            if !is_custom && ApiProvider::parse(provider_name).is_none() {
                return Err(AcpError::invalid_params(format!(
                    "unknown provider: {provider_name}"
                )));
            }
            self.config.provider = Some(provider_name.to_string());
        }

        self.model = model;
        Ok(self.current_model())
    }

    /// Validate a `session/prompt` request and append the user turn to history,
    /// returning the cloned conversation for the (borrow-free) provider call.
    ///
    /// This is the `&mut self` half of a prompt turn; the streaming provider
    /// call lives in [`AcpServer::open_prompt_stream`] (which borrows `&self`
    /// only and returns a `'static` stream) so it can be raced against the
    /// reader for cancellation.
    fn begin_prompt(&mut self, params: Value) -> std::result::Result<PreparedPrompt, AcpError> {
        let session_id = params
            .get("sessionId")
            .and_then(Value::as_str)
            .ok_or_else(|| AcpError::invalid_params("sessionId is required"))?
            .to_string();
        let prompt = extract_prompt_text(params.get("prompt"))
            .filter(|text| !text.trim().is_empty())
            .ok_or_else(|| AcpError::invalid_params("prompt must include text content"))?;

        let (messages, cwd) = {
            let session = self
                .sessions
                .get_mut(&session_id)
                .ok_or_else(|| AcpError::invalid_params("unknown sessionId"))?;
            session.messages.push(Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: prompt,
                    cache_control: None,
                }],
            });
            (session.messages.clone(), session.cwd.clone())
        };

        Ok(PreparedPrompt {
            session_id,
            messages,
            cwd,
        })
    }

    /// Commit the full message list produced by a completed turn into the
    /// session's history — the original history plus every assistant/tool-
    /// call/tool-result round the turn drove.
    ///
    /// Called on **all** outcomes: normal completion, max-rounds, AND cancel.
    /// (The caller strips dangling assistant tool_use blocks on cancel before
    /// committing, which produces a clean partial history the next prompt can
    /// continue from instead of leaving the pre-turn state untouched.)
    fn commit_turn_messages(&mut self, session_id: &str, messages: Vec<Message>) {
        if let Some(session) = self.sessions.get_mut(session_id) {
            session.messages = messages;
        }
    }

    /// Remove the last user message from the session history. Used to unwind
    /// the `begin_prompt` push when the turn itself fails (e.g. provider
    /// stream error), so the next prompt doesn't start with two consecutive
    /// `user` messages.
    fn rollback_user_message(&mut self, session_id: &str) {
        if let Some(session) = self.sessions.get_mut(session_id)
            && session.messages.last().map(|m| m.role.as_str()) == Some("user")
        {
            session.messages.pop();
        }
    }

    /// Resolve the route, build the streaming request, and open the provider
    /// response stream. Borrows `&self` only to read config/model; the returned
    /// [`StreamEventBox`] is `'static`, so the caller can race it against the
    /// reader without holding any borrow on the server. The cwd guard only needs
    /// to cover route resolution and client construction, not stream
    /// consumption, so it is dropped here.
    async fn open_prompt_stream(
        &self,
        messages: &[Message],
        cwd: &PathBuf,
        tool_registry: &ToolRegistry,
        frozen_system_prompt: &std::sync::Mutex<Option<SystemPrompt>>,
    ) -> Result<StreamEventBox> {
        let _cwd_guard = ScopedCurrentDir::new(cwd)?;
        let last_user_text = messages
            .iter()
            .rev()
            .find_map(|m| {
                if m.role == "user" {
                    m.content.iter().find_map(|b| match b {
                        ContentBlock::Text { text, .. } => Some(text.as_str()),
                        _ => None,
                    })
                } else {
                    None
                }
            })
            .unwrap_or("");
        let route =
            crate::resolve_cli_auto_route(&self.config, &self.model, last_user_text).await?;
        let execution_config = crate::config_for_cli_route(&self.config, &route);
        let client = DeepSeekClient::new(&execution_config)?;
        let model = route.model;
        let request_route = client.effective_route_envelope(&model, chrono::Utc::now());
        let reasoning_effort = route
            .reasoning_effort
            .and_then(|effort| {
                effort.api_value_for_route(
                    execution_config.api_provider(),
                    &execution_config.deepseek_base_url(),
                    &model,
                )
            })
            .map(str::to_string);

        let tools = tool_registry.to_api_tools();
        let (route_limits, image_input) =
            resolve_acp_route_facts(&execution_config, request_route.provider, &model);
        let system = frozen_acp_system_prompt(
            frozen_system_prompt,
            &execution_config,
            cwd,
            request_route.provider,
            &request_route.model,
            route_limits,
        );

        let mut outbound_messages = messages.to_vec();
        crate::image_attach::strip_images_when_unsupported(
            &mut outbound_messages,
            image_input,
            &request_route.model,
        );
        let request = MessageRequest {
            model,
            messages: outbound_messages,
            max_tokens: crate::route_budget::effective_max_output_tokens_for_route(
                request_route.provider,
                &request_route.model,
                route_limits,
            ),
            system: Some(system),
            tools: Some(tools.clone()),
            tool_choice: if tools.is_empty() {
                None
            } else {
                Some(json!({ "type": "auto" }))
            },
            metadata: None,
            thinking: None,
            reasoning_effort,
            stream: Some(true),
            temperature: None,
            top_p: None,
        };

        client.create_message_stream(request).await
    }
}

fn resolve_acp_route_facts(
    config: &Config,
    provider: ApiProvider,
    model: &str,
) -> (
    Option<codewhale_config::route::RouteLimits>,
    crate::model_profile::SupportState,
) {
    let Ok(route) = crate::route_runtime::resolve_runtime_route(config, provider, Some(model))
    else {
        return (None, crate::model_profile::SupportState::Unknown);
    };
    (
        crate::route_budget::known_route_limits(route.candidate.limits()),
        route.candidate.capabilities().image_input,
    )
}

/// Return the first fully composed ACP system prompt for this user turn.
/// Later tool rounds clone that exact value instead of re-reading mutable
/// instruction sources from disk.
fn frozen_acp_system_prompt(
    slot: &std::sync::Mutex<Option<SystemPrompt>>,
    config: &Config,
    workspace: &std::path::Path,
    provider: ApiProvider,
    model: &str,
    route_limits: Option<codewhale_config::route::RouteLimits>,
) -> SystemPrompt {
    let mut slot = match slot.lock() {
        Ok(slot) => slot,
        Err(poisoned) => poisoned.into_inner(),
    };
    if let Some(system) = slot.as_ref() {
        return system.clone();
    }
    let system = build_acp_system_prompt(config, workspace, provider, model, route_limits);
    *slot = Some(system.clone());
    system
}

/// Compose ACP's stable prompt through the same headless host seam as
/// `codewhale exec`. Tool availability remains owned by the request catalog;
/// this function supplies the shared constitution, project instructions,
/// configured instruction files, memory, locale, and route context.
fn build_acp_system_prompt(
    config: &Config,
    workspace: &std::path::Path,
    provider: ApiProvider,
    model: &str,
    route_limits: Option<codewhale_config::route::RouteLimits>,
) -> SystemPrompt {
    let settings = crate::settings::Settings::load().unwrap_or_default();
    let locale_tag = crate::localization::resolve_locale(&settings.locale)
        .tag()
        .to_string();
    let instructions = config
        .instructions_paths()
        .into_iter()
        .map(crate::prompts::InstructionSource::from)
        .collect::<Vec<_>>();
    let skills_dir = config.skills_dir();
    let user_memory_block = crate::native_memory::native_prompt_block(
        config.memory_enabled(),
        &config.memory_path(),
        workspace,
    );

    crate::prompts::system_prompt_for_mode_with_context_skills_session_and_approval_for_host(
        workspace,
        None,
        Some(&skills_dir),
        Some(&instructions),
        crate::prompts::PromptSessionContext {
            user_memory_block: user_memory_block.as_deref(),
            goal_objective: None,
            project_context_pack_enabled: config.project_context_pack_enabled(),
            locale_tag: &locale_tag,
            translation_enabled: false,
            model_id: model,
            context_window_override: Some(crate::route_budget::route_context_window_tokens(
                provider,
                model,
                route_limits,
            )),
            verbosity: config.verbosity.as_deref(),
            skills_scan_codewhale_only: config.skills_config().scan_codewhale_only(),
            plugin_registry: None,
            mode: crate::tui::app::AppMode::Agent,
        },
        crate::prompts::PromptHost::Headless,
    )
}

/// Build the tool registry for one ACP session, rooted at the session's
/// `cwd`. Reuses the shared registry builders used by headless `exec` and the
/// MCP adapter — no ACP-specific tool implementations.
///
/// `Bash` is registered only when all three independent gates allow it: the
/// client declares `clientCapabilities.terminal`, headless shell access is
/// explicitly enabled in config, and the stable shell feature is enabled.
/// Omitting any gate fails closed. The context also inherits the current
/// mode-derived/configured sandbox boundary.
/// `ToolContext::new` leaves `auto_approve` at its default (`false`), so the
/// shell's own last-line safety check remains active after ACP's shared
/// prepared-call, typed-policy, auto-review, repository-law, and explicit
/// `session/request_permission` gates have admitted the call.
fn build_acp_tool_registry(
    config: &Config,
    workspace: &std::path::Path,
    client_supports_terminal: bool,
) -> ToolRegistry {
    let features = config.features();
    let external_sandbox_requested = config.sandbox_backend.as_deref().is_some_and(|kind| {
        let kind = kind.trim();
        !kind.is_empty() && !kind.eq_ignore_ascii_case("none")
    });
    let sandbox_backend = match crate::sandbox::backend::create_backend(config) {
        Ok(backend) => backend.map(std::sync::Arc::from),
        Err(error) => {
            tracing::warn!("Failed to create ACP sandbox backend: {error}");
            None
        }
    };
    // A requested external sandbox is an execution boundary, not a hint. If
    // it cannot be constructed, omit Bash instead of silently running the
    // command on the local host.
    let sandbox_backend_ready = !external_sandbox_requested || sandbox_backend.is_some();
    let allow_shell = client_supports_terminal
        && config.allow_shell()
        && features.enabled(crate::features::Feature::ShellTool)
        && sandbox_backend_ready;
    let shell_policy = if allow_shell {
        ShellPolicy::Full
    } else {
        ShellPolicy::None
    };
    let sandbox_policy = crate::core::authority::sandbox_policy_for_turn(
        crate::tui::app::AppMode::Agent,
        crate::tui::approval::ApprovalMode::Suggest,
        config.sandbox_mode.as_deref(),
        workspace,
    );
    let mut context = ToolContext::new(workspace)
        .with_shell_policy(shell_policy)
        .with_elevated_sandbox_policy(sandbox_policy);
    match context.shell_manager.lock() {
        Ok(mut manager) => manager.set_prefer_bwrap(config.prefer_bwrap.unwrap_or(false)),
        Err(poisoned) => poisoned
            .into_inner()
            .set_prefer_bwrap(config.prefer_bwrap.unwrap_or(false)),
    }
    if let Some(backend) = sandbox_backend {
        context = context.with_sandbox_backend(backend);
    }
    let hooks_config =
        crate::hooks::HooksConfig::load_with_project(config.hooks_config(), workspace);
    context.runtime.hook_executor = Some(Arc::new(crate::hooks::HookExecutor::new(
        hooks_config,
        workspace.to_path_buf(),
    )));

    let mut builder = ToolRegistryBuilder::new()
        .with_file_tools()
        .with_search_tools()
        .with_git_tools();
    if features.enabled(crate::features::Feature::ApplyPatch) {
        builder = builder.with_patch_tools();
    }
    if allow_shell {
        builder = builder.with_foreground_shell_tools();
    }

    let mut registry = builder.build(context);
    // ACP does not load arbitrary plugin replacements in v0.9.6, but it must
    // never fall through to a built-in the operator disabled or replaced.
    if let Some(overrides) = config
        .tools
        .as_ref()
        .and_then(|tools| tools.overrides.as_ref())
    {
        for tool_name in overrides.keys() {
            remove_acp_overridden_builtin(&mut registry, tool_name);
        }
    }
    registry
}

/// ACP does not load executable tool replacements in v0.9.6. Remove the
/// built-in compatibility family for every configured override so neither a
/// hidden legacy alias nor a newly canonical lowercase name can fall through
/// to the original implementation.
fn remove_acp_overridden_builtin(registry: &mut ToolRegistry, tool_name: &str) {
    let aliases: &[&str] = match tool_name {
        "bash" | "Bash" | "exec_shell" => &["bash", "Bash", "exec_shell"],
        "read" | "write" | "edit" | "File" | "read_file" | "write_file" | "edit_file" => &[
            "read",
            "write",
            "edit",
            "File",
            "read_file",
            "write_file",
            "edit_file",
        ],
        "apply_patch" => &["apply_patch"],
        _ => std::slice::from_ref(&tool_name),
    };
    for alias in aliases {
        registry.remove_tool(alias);
    }
}

/// ACP `kind` hint for a tool call, used by the client to pick an icon/label.
/// Falls back to `"other"` for tools without an obvious category.
///
/// `File` is a single canonical tool covering read/list/search/write/edit/
/// patch (#4625), so its kind depends on the `action` argument rather than
/// the tool name alone.
fn tool_call_kind(call: &PendingToolCall) -> &'static str {
    match call.name.as_str() {
        "File" => match call.input.get("action").and_then(Value::as_str) {
            Some("write" | "edit" | "patch") => "edit",
            _ => "read",
        },
        "apply_patch" => "edit",
        "Git" => "read",
        "bash" | "Bash" | "terminal/run" | "terminal/send" | "terminal/wait"
        | "terminal/cancel" | "terminal/reset" => "execute",
        _ => "other",
    }
}

/// Human-readable title for a tool call: the tool name plus its primary
/// argument (path/command/pattern) when present, so the client's tool-call
/// card is legible without expanding raw input.
fn tool_call_title(call: &PendingToolCall) -> String {
    let detail = call
        .input
        .get("path")
        .or_else(|| call.input.get("command"))
        .or_else(|| call.input.get("pattern"))
        .or_else(|| call.input.get("task_id"))
        .and_then(Value::as_str);
    match detail {
        Some(detail) => format!("{}: {}", call.name, detail),
        None => call.name.clone(),
    }
}

fn truncate_for_acp(content: &str) -> String {
    if content.chars().count() <= TOOL_CALL_CONTENT_PREVIEW_CHARS {
        return content.to_string();
    }
    let truncated: String = content
        .chars()
        .take(TOOL_CALL_CONTENT_PREVIEW_CHARS)
        .collect();
    format!("{truncated}\n… [truncated for display; the full result was sent to the model]")
}

async fn write_tool_call_start<W>(
    writer: &mut W,
    session_id: &str,
    call: &PendingToolCall,
) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    let notification = json!({
        "jsonrpc": "2.0",
        "method": "session/update",
        "params": {
            "sessionId": session_id,
            "update": {
                "sessionUpdate": "tool_call",
                "toolCallId": call.id,
                "title": tool_call_title(call),
                "kind": tool_call_kind(call),
                "status": "pending",
                "rawInput": call.input,
            }
        }
    });
    write_json_line(writer, notification).await
}

async fn write_tool_call_update<W>(
    writer: &mut W,
    session_id: &str,
    call: &PendingToolCall,
    status: &str,
    content: Option<&str>,
) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    write_tool_call_update_with_blocks(writer, session_id, call, status, content, &[]).await
}

async fn write_tool_call_update_with_blocks<W>(
    writer: &mut W,
    session_id: &str,
    call: &PendingToolCall,
    status: &str,
    content: Option<&str>,
    rich_blocks: &[codewhale_tools::ToolResultContentBlock],
) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    let mut update = json!({
        "sessionUpdate": "tool_call_update",
        "toolCallId": call.id,
        "status": status,
    });
    if content.is_some() || !rich_blocks.is_empty() {
        let mut blocks = Vec::with_capacity(rich_blocks.len() + usize::from(content.is_some()));
        if let Some(content) = content {
            blocks.push(json!({
                "type": "content",
                "content": { "type": "text", "text": truncate_for_acp(content) }
            }));
        }
        blocks.extend(rich_blocks.iter().map(|block| match block {
            codewhale_tools::ToolResultContentBlock::Image { mime_type, data } => json!({
                "type": "content",
                "content": { "type": "image", "data": data, "mimeType": mime_type }
            }),
        }));
        update["content"] = json!(blocks);
    }
    let notification = json!({
        "jsonrpc": "2.0",
        "method": "session/update",
        "params": {
            "sessionId": session_id,
            "update": update
        }
    });
    write_json_line(writer, notification).await
}

struct ScopedCurrentDir {
    prior: PathBuf,
}

impl ScopedCurrentDir {
    fn new(cwd: &PathBuf) -> Result<Self> {
        let prior = std::env::current_dir()?;
        if cwd.as_os_str().is_empty() {
            return Ok(Self { prior });
        }
        std::env::set_current_dir(cwd)
            .map_err(|err| anyhow!("failed to enter ACP session cwd {}: {err}", cwd.display()))?;
        Ok(Self { prior })
    }
}

impl Drop for ScopedCurrentDir {
    fn drop(&mut self) {
        let _ = std::env::set_current_dir(&self.prior);
    }
}

impl AcpError {
    fn invalid_params(message: impl Into<String>) -> Self {
        Self {
            code: -32602,
            message: message.into(),
        }
    }

    fn method_not_found(method: &str) -> Self {
        Self {
            code: -32601,
            message: format!("method not found: {method}"),
        }
    }
}

fn initialize_result(client_protocol_version: Option<u64>, config: &Config) -> Value {
    json!({
        "protocolVersion": client_protocol_version
            .map(|version| version.min(ACP_PROTOCOL_VERSION))
            .unwrap_or(ACP_PROTOCOL_VERSION),
        "agentCapabilities": {
            "loadSession": false,
            "modelSelection": true,
            "promptCapabilities": {
                "image": false,
                "audio": false,
                "embeddedContext": true
            },
            "mcpCapabilities": {
                "http": false,
                "sse": false
            },
            "sessionCapabilities": {}
        },
        "agentInfo": {
            "name": "codewhale",
            "title": "codewhale",
            "version": env!("CARGO_PKG_VERSION")
        },
        "authMethods": acp_auth_methods(config)
    })
}

fn acp_auth_methods(config: &Config) -> Value {
    let provider = config.api_provider().as_str();
    json!([
        {
            "id": "codewhale-terminal-auth",
            "name": "Set Codewhale API key",
            "description": format!("Run Codewhale's terminal credential setup for the {provider} provider."),
            "type": "terminal",
            "args": ["auth", "set", "--provider", provider],
            "env": {}
        }
    ])
}

fn extract_prompt_text(prompt: Option<&Value>) -> Option<String> {
    match prompt? {
        Value::String(text) => Some(text.clone()),
        Value::Array(blocks) => {
            let parts = blocks
                .iter()
                .filter_map(content_block_text)
                .collect::<Vec<_>>();
            (!parts.is_empty()).then(|| parts.join("\n\n"))
        }
        _ => None,
    }
}

fn content_block_text(block: &Value) -> Option<String> {
    match block.get("type").and_then(Value::as_str)? {
        "text" => block
            .get("text")
            .and_then(Value::as_str)
            .map(str::to_string),
        "resource" => resource_text(block),
        "resource_link" | "resourceLink" => resource_link_text(block),
        _ => None,
    }
}

fn resource_text(block: &Value) -> Option<String> {
    let resource = block.get("resource").unwrap_or(block);
    if let Some(text) = resource.get("text").and_then(Value::as_str) {
        return Some(text.to_string());
    }
    resource_link_text(resource)
}

fn resource_link_text(block: &Value) -> Option<String> {
    let uri = block
        .get("uri")
        .or_else(|| block.pointer("/resource/uri"))
        .and_then(Value::as_str)?;
    Some(format!("@{uri}"))
}

async fn write_session_update<W>(writer: &mut W, session_id: &str, text: String) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    let notification = json!({
        "jsonrpc": "2.0",
        "method": "session/update",
        "params": {
            "sessionId": session_id,
            "update": {
                "sessionUpdate": "agent_message_chunk",
                "content": {
                    "type": "text",
                    "text": text
                }
            }
        }
    });
    write_json_line(writer, notification).await
}

async fn write_jsonrpc_result<W>(writer: &mut W, id: Value, result: Value) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    write_json_line(
        writer,
        json!({
            "jsonrpc": "2.0",
            "id": id,
            "result": result
        }),
    )
    .await
}

async fn write_jsonrpc_error<W>(
    writer: &mut W,
    id: Option<Value>,
    code: i32,
    message: impl Into<String>,
) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    write_json_line(
        writer,
        json!({
            "jsonrpc": "2.0",
            "id": id,
            "error": {
                "code": code,
                "message": message.into()
            }
        }),
    )
    .await
}

async fn write_json_line<W>(writer: &mut W, value: Value) -> Result<()>
where
    W: AsyncWrite + Unpin,
{
    writer.write_all(value.to_string().as_bytes()).await?;
    writer.write_all(b"\n").await?;
    writer.flush().await?;
    Ok(())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum JsonRpcResponseIdPolicy {
    /// JSON-RPC's normal contract: echo the request id without changing type.
    Preserve,
    /// Zed's ACP client currently decodes response ids as strings even when it
    /// sent a number. Keep this narrow compatibility mode client-identified.
    StringifyNumeric,
}

impl JsonRpcResponseIdPolicy {
    fn from_initialize_params(params: &Value) -> Self {
        let client_name = params
            .pointer("/clientInfo/name")
            .and_then(Value::as_str)
            .unwrap_or_default();
        if client_name.eq_ignore_ascii_case("zed") {
            Self::StringifyNumeric
        } else {
            Self::Preserve
        }
    }

    fn response_id(self, id: Value) -> Value {
        match (self, id) {
            (Self::StringifyNumeric, Value::Number(number)) => Value::String(number.to_string()),
            (_, id) => id,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::RefCell;
    use std::collections::VecDeque;

    #[tokio::test]
    async fn tool_update_emits_typed_acp_image_content() {
        let mut output = Vec::new();
        let call = PendingToolCall {
            id: "call_image_1".to_string(),
            name: "read".to_string(),
            input: json!({"path": "shot.png"}),
            parse_error: None,
        };
        write_tool_call_update_with_blocks(
            &mut output,
            "session_1",
            &call,
            "completed",
            Some("screenshot captured"),
            &[codewhale_tools::ToolResultContentBlock::Image {
                mime_type: "image/png".to_string(),
                data: "QUJD".to_string(),
            }],
        )
        .await
        .expect("ACP update");

        let lines = parse_lines(output);
        let content = lines[0]["params"]["update"]["content"]
            .as_array()
            .expect("ACP content blocks");
        assert_eq!(content[0]["content"]["type"], "text");
        assert_eq!(content[1]["content"]["type"], "image");
        assert_eq!(content[1]["content"]["mimeType"], "image/png");
        assert_eq!(content[1]["content"]["data"], "QUJD");
    }

    #[test]
    fn initialize_advertises_baseline_acp_agent() {
        let result = initialize_result(Some(1), &Config::default());

        assert_eq!(result["protocolVersion"], 1);
        assert_eq!(result["agentInfo"]["name"], "codewhale");
        assert_eq!(result["agentCapabilities"]["loadSession"], false);
        assert_eq!(
            result["agentCapabilities"]["promptCapabilities"]["embeddedContext"],
            true
        );
        assert_eq!(result["authMethods"][0]["type"], "terminal");
        assert_eq!(
            result["authMethods"][0]["args"],
            json!(["auth", "set", "--provider", "deepseek"])
        );
    }

    #[test]
    fn initialize_advertises_model_selection_capability() {
        let result = initialize_result(Some(1), &Config::default());

        assert_eq!(result["agentCapabilities"]["modelSelection"], true);
    }

    #[test]
    fn list_providers_returns_provider_set() {
        let server = AcpServer::new(
            Config::default(),
            "deepseek-chat".into(),
            PathBuf::from("/tmp"),
        );
        let result = server.list_providers();
        let providers = result["providers"].as_array().expect("providers array");

        assert!(!providers.is_empty());
        assert!(
            providers
                .iter()
                .any(|provider| provider["id"] == "deepseek")
        );
    }

    #[test]
    fn current_model_reflects_constructor_default() {
        let config = Config::default();
        let expected_provider = config.api_provider().as_str();
        let server = AcpServer::new(config, "deepseek-reasoner".into(), PathBuf::from("/tmp"));
        let result = server.current_model();

        assert_eq!(result["provider"], expected_provider);
        assert_eq!(result["model"], "deepseek-reasoner");
    }

    #[test]
    fn select_model_updates_active_selection() {
        let mut server = AcpServer::new(
            Config::default(),
            "deepseek-chat".into(),
            PathBuf::from("/tmp"),
        );

        let result = server
            .select_model(json!({ "provider": "openai", "model": "gpt-4o" }))
            .expect("select model");

        assert_eq!(result["provider"], "openai");
        assert_eq!(result["model"], "gpt-4o");
        assert_eq!(server.current_model()["provider"], "openai");
        assert_eq!(server.current_model()["model"], "gpt-4o");
    }

    #[test]
    fn select_model_rejects_unknown_provider() {
        let mut server = AcpServer::new(
            Config::default(),
            "deepseek-chat".into(),
            PathBuf::from("/tmp"),
        );
        let before = server.current_model();

        let err = server
            .select_model(json!({ "provider": "unknown-provider", "model": "gpt-4o" }))
            .expect_err("unknown provider rejected");

        assert_eq!(err.code, -32602);
        assert_eq!(server.current_model(), before);
    }

    #[test]
    fn select_model_rejects_missing_model() {
        let mut server = AcpServer::new(
            Config::default(),
            "deepseek-chat".into(),
            PathBuf::from("/tmp"),
        );

        let err = server
            .select_model(json!({ "provider": "openai" }))
            .expect_err("missing model rejected");

        assert_eq!(err.code, -32602);
    }

    #[test]
    fn extract_prompt_text_accepts_text_and_resource_blocks() {
        let prompt = json!([
            { "type": "text", "text": "Review this file" },
            {
                "type": "resource",
                "resource": {
                    "uri": "file:///tmp/app.rs",
                    "mimeType": "text/rust",
                    "text": "fn main() {}"
                }
            },
            { "type": "resource_link", "uri": "file:///tmp/lib.rs" }
        ]);

        let text = extract_prompt_text(Some(&prompt)).expect("prompt text");

        assert!(text.contains("Review this file"));
        assert!(text.contains("fn main() {}"));
        assert!(text.contains("@file:///tmp/lib.rs"));
    }

    #[tokio::test]
    async fn session_update_is_protocol_clean_single_line_json() {
        let mut out = Vec::new();

        write_session_update(&mut out, "sess_1", "hello\nworld".to_string())
            .await
            .expect("write update");

        let line = String::from_utf8(out).expect("utf8");
        assert_eq!(line.lines().count(), 1);
        let value: Value = serde_json::from_str(line.trim()).expect("json");
        assert_eq!(value["method"], "session/update");
        assert_eq!(value["params"]["sessionId"], "sess_1");
        assert_eq!(value["params"]["update"]["content"]["text"], "hello\nworld");
    }

    #[tokio::test]
    async fn jsonrpc_result_preserves_numeric_ids_for_avante_acp() {
        let mut out = Vec::new();

        let params = json!({
            "protocolVersion": 1,
            "clientCapabilities": {}
        });
        let id = JsonRpcResponseIdPolicy::from_initialize_params(&params).response_id(json!(1));
        write_jsonrpc_result(&mut out, id, json!({"ok": true}))
            .await
            .expect("write result");

        let line = String::from_utf8(out).expect("utf8");
        let value: Value = serde_json::from_str(line.trim()).expect("json");
        // Numeric ID must stay numeric — avante.nvim's Lua client uses
        // strict table keys (callbacks[1] ≠ callbacks["1"]).
        assert!(
            value["id"].is_number(),
            "numeric id must stay numeric, got {:?}",
            value["id"]
        );
        assert_eq!(value["result"], json!({"ok": true}));
    }

    #[tokio::test]
    async fn jsonrpc_result_stringifies_numeric_ids_for_zed_acp() {
        let mut out = Vec::new();

        let params = json!({
            "protocolVersion": 1,
            "clientCapabilities": {},
            "clientInfo": {
                "name": "zed",
                "version": "1.2.6"
            }
        });
        let id = JsonRpcResponseIdPolicy::from_initialize_params(&params).response_id(json!(1));
        write_jsonrpc_result(&mut out, id, json!({"ok": true}))
            .await
            .expect("write result");

        let line = String::from_utf8(out).expect("utf8");
        let value: Value = serde_json::from_str(line.trim()).expect("json");
        assert_eq!(value["id"], "1");
        assert_eq!(value["result"], json!({"ok": true}));
    }

    #[tokio::test]
    async fn jsonrpc_error_keeps_absent_id_null() {
        let mut out = Vec::new();

        write_jsonrpc_error(&mut out, None, -32700, "invalid json")
            .await
            .expect("write error");

        let line = String::from_utf8(out).expect("utf8");
        let value: Value = serde_json::from_str(line.trim()).expect("json");
        assert_eq!(value["id"], Value::Null);
        assert_eq!(value["error"]["code"], -32700);
    }

    #[test]
    fn new_session_starts_with_empty_messages() {
        let mut server = AcpServer::new(
            Config::default(),
            "test-model".to_string(),
            PathBuf::from("/tmp"),
        );
        let result = server
            .new_session(json!({ "cwd": "/tmp" }))
            .expect("new session");
        let session_id = result["sessionId"].as_str().expect("session id");
        let session = server.sessions.get(session_id).expect("session exists");
        assert!(session.messages.is_empty());
    }

    #[test]
    fn prompt_appends_user_and_assistant_messages_to_history() {
        let mut server = AcpServer::new(
            Config::default(),
            "test-model".to_string(),
            PathBuf::from("/tmp"),
        );
        let result = server
            .new_session(json!({ "cwd": "/tmp" }))
            .expect("new session");
        let session_id = result["sessionId"].as_str().unwrap().to_string();

        // Simulate adding a user message (same logic as prompt() but without LLM call)
        {
            let session = server.sessions.get_mut(&session_id).unwrap();
            session.messages.push(Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: "1+1".to_string(),
                    cache_control: None,
                }],
            });
        }

        // Simulate assistant response
        {
            let session = server.sessions.get_mut(&session_id).unwrap();
            session.messages.push(Message {
                role: "assistant".to_string(),
                content: vec![ContentBlock::Text {
                    text: "2".to_string(),
                    cache_control: None,
                }],
            });
        }

        // Second user message
        {
            let session = server.sessions.get_mut(&session_id).unwrap();
            session.messages.push(Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: "add one more".to_string(),
                    cache_control: None,
                }],
            });
        }

        // Verify full conversation history
        let session = server.sessions.get(&session_id).unwrap();
        assert_eq!(session.messages.len(), 3);
        assert_eq!(session.messages[0].role, "user");
        assert_eq!(session.messages[1].role, "assistant");
        assert_eq!(session.messages[2].role, "user");

        // Verify text content
        assert_eq!(
            match &session.messages[0].content[0] {
                ContentBlock::Text { text, .. } => text.clone(),
                _ => String::new(),
            },
            "1+1"
        );
        assert_eq!(
            match &session.messages[1].content[0] {
                ContentBlock::Text { text, .. } => text.clone(),
                _ => String::new(),
            },
            "2"
        );
        assert_eq!(
            match &session.messages[2].content[0] {
                ContentBlock::Text { text, .. } => text.clone(),
                _ => String::new(),
            },
            "add one more"
        );
    }

    fn lines_from(input: &'static str) -> Lines<BufReader<&'static [u8]>> {
        BufReader::new(input.as_bytes()).lines()
    }

    fn text_delta(text: &str) -> StreamEvent {
        StreamEvent::ContentBlockDelta {
            index: 0,
            delta: Delta::TextDelta {
                text: text.to_string(),
            },
        }
    }

    /// Simulate one streamed `tool_use` content block at `index`: a start
    /// event carrying the id/name, an `input_json_delta` with the full
    /// arguments JSON, and the closing stop event — matching the real
    /// provider's per-index streaming shape closely enough to exercise
    /// [`drive_prompt_stream`]'s accumulator.
    fn tool_use_events(index: u32, id: &str, name: &str, input_json: &str) -> Vec<StreamEvent> {
        vec![
            StreamEvent::ContentBlockStart {
                index,
                content_block: ContentBlockStart::ToolUse {
                    id: id.to_string(),
                    name: name.to_string(),
                    input: json!({}),
                    caller: None,
                    thought_signature: None,
                },
            },
            StreamEvent::ContentBlockDelta {
                index,
                delta: Delta::InputJsonDelta {
                    partial_json: input_json.to_string(),
                },
            },
            StreamEvent::ContentBlockStop { index },
        ]
    }

    /// A stream that yields the given events immediately, then ends.
    fn ready_stream(events: Vec<StreamEvent>) -> StreamEventBox {
        Box::pin(futures_util::stream::iter(
            events.into_iter().map(Ok::<_, anyhow::Error>),
        ))
    }

    fn error_stream(message: &'static str) -> StreamEventBox {
        Box::pin(futures_util::stream::iter(vec![Err(anyhow!(message))]))
    }

    /// A stream that never yields, so a concurrent cancel always wins.
    fn pending_stream() -> StreamEventBox {
        Box::pin(futures_util::stream::pending::<Result<StreamEvent>>())
    }

    /// A stream that yields `events` immediately, then emits `message_stop`
    /// after a short delay — long enough that an already-buffered reader line is
    /// processed first, making the ordering deterministic in tests.
    fn events_then_delayed_stop(events: Vec<StreamEvent>) -> StreamEventBox {
        let head = futures_util::stream::iter(events.into_iter().map(Ok::<_, anyhow::Error>));
        let tail = futures_util::stream::once(async {
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
            Ok(StreamEvent::MessageStop)
        });
        Box::pin(head.chain(tail))
    }

    fn parse_lines(out: Vec<u8>) -> Vec<Value> {
        String::from_utf8(out)
            .expect("utf8")
            .lines()
            .filter(|line| !line.trim().is_empty())
            .map(|line| serde_json::from_str(line).expect("json"))
            .collect()
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum PermissionClientScript {
        Allow,
        Reject,
        WrongIdThenReject,
        CancelThenLateAllow,
        AllowThenCancelRunning,
    }

    async fn write_client_message(writer: &mut tokio::io::DuplexStream, message: Value) {
        writer
            .write_all(format!("{message}\n").as_bytes())
            .await
            .expect("write simulated ACP client message");
    }

    async fn drive_permission_client(
        output: tokio::io::DuplexStream,
        mut input: tokio::io::DuplexStream,
        script: PermissionClientScript,
        must_not_exist_before_response: Option<PathBuf>,
    ) -> Vec<Value> {
        let mut output = BufReader::new(output).lines();
        let mut seen = Vec::new();
        let mut sent_running_cancel = false;
        while let Some(line) = output.next_line().await.expect("read agent output") {
            let message: Value = serde_json::from_str(&line).expect("agent output json");
            seen.push(message.clone());

            if message.get("method").and_then(Value::as_str) == Some("session/request_permission") {
                if let Some(path) = must_not_exist_before_response.as_ref() {
                    assert!(
                        !path.exists(),
                        "sensitive tool ran before the permission response: {}",
                        path.display()
                    );
                }
                let request_id = message["id"].clone();
                let selected = |option_id: &str| {
                    json!({
                        "jsonrpc": "2.0",
                        "id": request_id.clone(),
                        "result": {
                            "outcome": {
                                "outcome": "selected",
                                "optionId": option_id
                            }
                        }
                    })
                };
                match script {
                    PermissionClientScript::Allow
                    | PermissionClientScript::AllowThenCancelRunning => {
                        write_client_message(&mut input, selected("allow-once")).await;
                    }
                    PermissionClientScript::Reject => {
                        write_client_message(&mut input, selected("reject-once")).await;
                    }
                    PermissionClientScript::WrongIdThenReject => {
                        write_client_message(
                            &mut input,
                            json!({
                                "jsonrpc": "2.0",
                                "id": "wrong-agent-request-id",
                                "result": {
                                    "outcome": {
                                        "outcome": "selected",
                                        "optionId": "allow-once"
                                    }
                                }
                            }),
                        )
                        .await;
                        write_client_message(&mut input, selected("reject-once")).await;
                    }
                    PermissionClientScript::CancelThenLateAllow => {
                        write_client_message(
                            &mut input,
                            json!({
                                "jsonrpc": "2.0",
                                "method": "session/cancel",
                                "params": { "sessionId": "sess_1" }
                            }),
                        )
                        .await;
                        write_client_message(
                            &mut input,
                            json!({
                                "jsonrpc": "2.0",
                                "id": request_id.clone(),
                                "result": { "outcome": { "outcome": "cancelled" } }
                            }),
                        )
                        .await;
                        write_client_message(&mut input, selected("allow-once")).await;
                    }
                }
            }

            let update_status = message
                .pointer("/params/update/status")
                .and_then(Value::as_str);
            if script == PermissionClientScript::AllowThenCancelRunning
                && update_status == Some("in_progress")
                && !sent_running_cancel
            {
                sent_running_cancel = true;
                tokio::time::sleep(Duration::from_millis(100)).await;
                write_client_message(
                    &mut input,
                    json!({
                        "jsonrpc": "2.0",
                        "id": 7,
                        "method": "session/cancel",
                        "params": { "sessionId": "sess_1" }
                    }),
                )
                .await;
            }
            if matches!(update_status, Some("completed" | "failed")) {
                break;
            }
        }
        seen
    }

    async fn execute_one_with_permission_client(
        config: &Config,
        registry: &ToolRegistry,
        call: PendingToolCall,
        script: PermissionClientScript,
        response_id_policy: JsonRpcResponseIdPolicy,
        must_not_exist_before_response: Option<PathBuf>,
    ) -> (ToolBatchOutcome, Vec<Value>, Option<Value>) {
        let (client_input, agent_input) = tokio::io::duplex(64 * 1024);
        let (agent_output, client_output) = tokio::io::duplex(64 * 1024);
        let client = tokio::spawn(drive_permission_client(
            client_output,
            client_input,
            script,
            must_not_exist_before_response,
        ));
        let mut reader = BufReader::new(agent_input).lines();
        let mut writer = agent_output;

        let outcome = execute_tool_calls_with_cancellation(
            AcpTurnContext {
                config,
                model: "test-model",
                session_id: "sess_1",
                tool_registry: registry,
                response_id_policy,
            },
            vec![call],
            &mut reader,
            &mut writer,
        )
        .await
        .expect("execute ACP tool batch");
        let late_response = if script == PermissionClientScript::CancelThenLateAllow {
            let line = tokio::time::timeout(Duration::from_secs(1), reader.next_line())
                .await
                .expect("late permission response arrived")
                .expect("read late permission response")
                .expect("late permission response line");
            Some(serde_json::from_str(&line).expect("late response json"))
        } else {
            None
        };
        drop(writer);
        let seen = client.await.expect("simulated ACP client joins");
        (outcome, seen, late_response)
    }

    #[tokio::test]
    async fn drive_prompt_streams_each_delta_as_a_chunk_then_completes() {
        let stream = ready_stream(vec![
            text_delta("hello"),
            text_delta(" world"),
            StreamEvent::MessageStop,
        ]);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let (outcome, tool_calls) = drive_prompt_stream(
            stream,
            "sess_1",
            JsonRpcResponseIdPolicy::Preserve,
            &mut reader,
            &mut out,
        )
        .await
        .expect("driver ok");

        // Full text is accumulated for history...
        assert_eq!(outcome, PromptOutcome::Completed("hello world".to_string()));
        assert!(tool_calls.is_empty());
        // ...and each delta was emitted as its own session/update chunk.
        let updates = parse_lines(out);
        assert_eq!(updates.len(), 2);
        assert!(updates.iter().all(|u| u["method"] == "session/update"));
        assert_eq!(updates[0]["params"]["update"]["content"]["text"], "hello");
        assert_eq!(updates[1]["params"]["update"]["content"]["text"], " world");
    }

    #[tokio::test]
    async fn drive_prompt_cancels_when_matching_cancel_arrives() {
        // A provider stream that never finishes within the test.
        let stream = pending_stream();
        let mut reader = lines_from(
            r#"{"jsonrpc":"2.0","method":"session/cancel","params":{"sessionId":"sess_1"}}"#,
        );
        let mut out = Vec::new();

        let (outcome, tool_calls) = drive_prompt_stream(
            stream,
            "sess_1",
            JsonRpcResponseIdPolicy::Preserve,
            &mut reader,
            &mut out,
        )
        .await
        .expect("driver ok");

        assert_eq!(outcome, PromptOutcome::Cancelled);
        assert!(tool_calls.is_empty());
        // Notification-form cancel (no id) is acknowledged by acting, not writing.
        assert!(out.is_empty());
    }

    #[tokio::test]
    async fn drive_prompt_ignores_cancel_for_a_different_session() {
        // The unrelated cancel line is buffered and ready; the delayed stop makes
        // it process first, proving it does not abort the turn.
        let stream = events_then_delayed_stop(vec![text_delta("kept")]);
        let mut reader = lines_from(
            r#"{"jsonrpc":"2.0","id":7,"method":"session/cancel","params":{"sessionId":"other"}}"#,
        );
        let mut out = Vec::new();

        let (outcome, _tool_calls) = drive_prompt_stream(
            stream,
            "sess_1",
            JsonRpcResponseIdPolicy::StringifyNumeric,
            &mut reader,
            &mut out,
        )
        .await
        .expect("driver ok");

        assert_eq!(outcome, PromptOutcome::Completed("kept".to_string()));
        // The other-session cancel carried an id, so it was acknowledged with null.
        let lines = parse_lines(out);
        assert!(
            lines
                .iter()
                .any(|v| v["id"] == "7" && v["result"] == Value::Null),
            "expected a null ack for the other-session cancel, got {lines:?}"
        );
    }

    #[tokio::test]
    async fn drive_prompt_rejects_a_concurrent_request_but_keeps_running() {
        let stream = events_then_delayed_stop(vec![text_delta("done")]);
        // A non-cancel request arrives mid-turn.
        let mut reader =
            lines_from(r#"{"jsonrpc":"2.0","id":9,"method":"session/new","params":{}}"#);
        let mut out = Vec::new();

        let (outcome, _tool_calls) = drive_prompt_stream(
            stream,
            "sess_1",
            JsonRpcResponseIdPolicy::StringifyNumeric,
            &mut reader,
            &mut out,
        )
        .await
        .expect("driver ok");

        assert_eq!(outcome, PromptOutcome::Completed("done".to_string()));
        let lines = parse_lines(out);
        assert!(
            lines
                .iter()
                .any(|v| v["id"] == "9" && v["error"]["code"] == -32603),
            "expected a prompt-in-progress error for the concurrent request, got {lines:?}"
        );
    }

    #[tokio::test]
    async fn drive_prompt_assembles_a_single_streamed_tool_call() {
        let mut events = tool_use_events(0, "call_1", "read_file", r#"{"path":"src/lib.rs"}"#);
        events.push(StreamEvent::MessageStop);
        let stream = ready_stream(events);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let (outcome, tool_calls) = drive_prompt_stream(
            stream,
            "sess_1",
            JsonRpcResponseIdPolicy::Preserve,
            &mut reader,
            &mut out,
        )
        .await
        .expect("driver ok");

        assert_eq!(outcome, PromptOutcome::Completed(String::new()));
        assert_eq!(tool_calls.len(), 1);
        assert_eq!(tool_calls[0].id, "call_1");
        assert_eq!(tool_calls[0].name, "read_file");
        assert_eq!(tool_calls[0].input, json!({"path": "src/lib.rs"}));
        assert!(tool_calls[0].parse_error.is_none());
    }

    #[tokio::test]
    async fn drive_prompt_assembles_multiple_parallel_tool_calls_in_order() {
        let mut events = tool_use_events(0, "call_1", "read_file", r#"{"path":"a.rs"}"#);
        events.extend(tool_use_events(
            1,
            "call_2",
            "read_file",
            r#"{"path":"b.rs"}"#,
        ));
        events.push(StreamEvent::MessageStop);
        let stream = ready_stream(events);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let (_outcome, tool_calls) = drive_prompt_stream(
            stream,
            "sess_1",
            JsonRpcResponseIdPolicy::Preserve,
            &mut reader,
            &mut out,
        )
        .await
        .expect("driver ok");

        assert_eq!(tool_calls.len(), 2);
        assert_eq!(tool_calls[0].id, "call_1");
        assert_eq!(tool_calls[1].id, "call_2");
    }

    #[tokio::test]
    async fn drive_prompt_reports_malformed_tool_arguments_instead_of_dropping_the_call() {
        let mut events = tool_use_events(0, "call_1", "read_file", "{not json");
        events.push(StreamEvent::MessageStop);
        let stream = ready_stream(events);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let (_outcome, tool_calls) = drive_prompt_stream(
            stream,
            "sess_1",
            JsonRpcResponseIdPolicy::Preserve,
            &mut reader,
            &mut out,
        )
        .await
        .expect("driver ok");

        assert_eq!(tool_calls.len(), 1);
        assert!(tool_calls[0].parse_error.is_some());
    }

    #[test]
    fn different_sessions_have_independent_history() {
        let mut server = AcpServer::new(
            Config::default(),
            "test-model".to_string(),
            PathBuf::from("/tmp"),
        );
        let result1 = server
            .new_session(json!({ "cwd": "/tmp" }))
            .expect("session 1");
        let result2 = server
            .new_session(json!({ "cwd": "/tmp" }))
            .expect("session 2");
        let sid1 = result1["sessionId"].as_str().unwrap().to_string();
        let sid2 = result2["sessionId"].as_str().unwrap().to_string();

        // Add messages to session 1
        {
            let session = server.sessions.get_mut(&sid1).unwrap();
            session.messages.push(Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: "hello".to_string(),
                    cache_control: None,
                }],
            });
        }

        // Session 2 should remain empty
        let session2 = server.sessions.get(&sid2).unwrap();
        assert!(session2.messages.is_empty());

        // Session 1 should have the message
        let session1 = server.sessions.get(&sid1).unwrap();
        assert_eq!(session1.messages.len(), 1);
    }

    #[test]
    fn concurrent_sessions_each_get_their_own_tool_registry() {
        let mut server = AcpServer::new(
            Config {
                allow_shell: Some(true),
                ..Config::default()
            },
            "test-model".to_string(),
            PathBuf::from("/tmp"),
        );
        // Both config opt-in and the client terminal capability are present.
        server.client_supports_terminal = true;
        let s1 = server.new_session(json!({ "cwd": "/tmp" })).unwrap();
        let s2 = server.new_session(json!({ "cwd": "/tmp" })).unwrap();
        let id1 = s1["sessionId"].as_str().unwrap();
        let id2 = s2["sessionId"].as_str().unwrap();

        let reg1 = server.session_tool_registry(id1).expect("registry 1");
        let reg2 = server.session_tool_registry(id2).expect("registry 2");
        assert!(!Arc::ptr_eq(&reg1, &reg2));
        // Both sessions expose the same reusable tool surface: the
        // canonical `File` action tool (read/list/search/write/edit),
        // `Git`, the `apply_patch` back-compat alias, and `Bash` (#4625
        // consolidated the old per-action tool names).
        assert!(reg1.contains("File"));
        assert!(reg1.contains("Git"));
        assert!(reg1.contains("apply_patch"));
        assert!(reg1.contains("bash"));
        assert!(reg1.contains("Bash"));
        assert!(
            reg1.names()
                .into_iter()
                .all(|name| !name.starts_with("terminal/")),
            "ACP must not expose stateful terminal tools"
        );
        assert!(reg1.context().runtime.hook_executor.is_some());
    }

    #[test]
    fn shell_tool_omitted_when_client_declares_no_terminal_support() {
        let workspace = std::env::temp_dir();
        let config = Config {
            allow_shell: Some(true),
            ..Config::default()
        };
        let registry = build_acp_tool_registry(&config, &workspace, false);
        assert!(!registry.contains("Bash"));
        assert!(registry.contains("File"));
    }

    #[test]
    fn shell_tool_omitted_without_headless_config_opt_in() {
        let workspace = std::env::temp_dir();
        let registry = build_acp_tool_registry(&Config::default(), &workspace, true);
        assert!(!registry.contains("Bash"));
        assert_eq!(registry.context().shell_policy, ShellPolicy::None);
        assert!(!registry.context().auto_approve);
    }

    #[test]
    fn acp_shell_uses_configured_external_sandbox_or_fails_closed() {
        let workspace = std::env::temp_dir();
        let configured = Config {
            allow_shell: Some(true),
            sandbox_backend: Some("opensandbox".to_string()),
            sandbox_url: Some("http://127.0.0.1:8080".to_string()),
            ..Config::default()
        };
        let registry = build_acp_tool_registry(&configured, &workspace, true);
        assert!(registry.contains("bash"));
        assert!(registry.context().sandbox_backend.is_some());

        let unsupported = Config {
            allow_shell: Some(true),
            sandbox_backend: Some("unsupported-backend".to_string()),
            ..Config::default()
        };
        let registry = build_acp_tool_registry(&unsupported, &workspace, true);
        assert!(!registry.contains("bash"));
        assert!(!registry.contains("Bash"));
        assert!(registry.context().sandbox_backend.is_none());
    }

    #[test]
    fn acp_tool_override_removes_every_builtin_compatibility_alias() {
        let mut overrides = std::collections::HashMap::new();
        overrides.insert("Bash".to_string(), crate::config::ToolOverride::Disabled);
        let config = Config {
            allow_shell: Some(true),
            tools: Some(crate::config::ToolsConfig {
                overrides: Some(overrides),
                ..crate::config::ToolsConfig::default()
            }),
            ..Config::default()
        };
        let registry = build_acp_tool_registry(&config, &std::env::temp_dir(), true);
        assert!(!registry.contains("bash"));
        assert!(!registry.contains("Bash"));
        assert!(
            registry
                .names()
                .into_iter()
                .all(|name| !name.starts_with("terminal/"))
        );
    }

    #[test]
    fn acp_prompt_uses_the_stable_headless_composer() {
        let workspace = tempfile::tempdir().expect("workspace");
        std::fs::write(
            workspace.path().join("AGENTS.md"),
            "# ACP project law\n\nKeep the acp-project-marker visible.",
        )
        .expect("write project instructions");
        let extra = workspace.path().join("maintainer-instructions.md");
        std::fs::write(&extra, "Keep the acp-config-marker visible.")
            .expect("write configured instructions");
        let config = Config {
            instructions: Some(vec![extra.to_string_lossy().into_owned()]),
            ..Config::default()
        };

        let prompt = build_acp_system_prompt(
            &config,
            workspace.path(),
            ApiProvider::Deepseek,
            "deepseek-v4-pro",
            None,
        );
        let text = crate::prompts::system_prompt_flat_text(&prompt);

        assert!(text.contains(crate::prompts::text::HEADLESS_BASE_PROMPT.trim()));
        assert!(text.contains("acp-project-marker"));
        assert!(text.contains("acp-config-marker"));
        assert!(!text.contains("You are a coding assistant inside an ACP-compatible editor."));
    }

    #[test]
    fn acp_system_prompt_is_byte_stable_after_round_one_agents_write() {
        let workspace = tempfile::tempdir().expect("workspace");
        let agents = workspace.path().join("AGENTS.md");
        std::fs::write(&agents, "round-one-authority").expect("write initial AGENTS");
        let config = Config::default();
        let slot = std::sync::Mutex::new(None);

        let round_one = frozen_acp_system_prompt(
            &slot,
            &config,
            workspace.path(),
            ApiProvider::Deepseek,
            "deepseek-v4-pro",
            None,
        );
        // Simulate a model tool changing project instructions during round 1.
        std::fs::write(&agents, "round-two-self-authored-authority")
            .expect("mutate AGENTS between rounds");
        let round_two = frozen_acp_system_prompt(
            &slot,
            &config,
            workspace.path(),
            ApiProvider::Deepseek,
            "deepseek-v4-pro",
            None,
        );

        assert_eq!(
            serde_json::to_vec(&round_one).unwrap(),
            serde_json::to_vec(&round_two).unwrap(),
            "later rounds must receive the byte-identical first-round system prompt"
        );
        let freshly_composed = build_acp_system_prompt(
            &config,
            workspace.path(),
            ApiProvider::Deepseek,
            "deepseek-v4-pro",
            None,
        );
        assert!(
            crate::prompts::system_prompt_flat_text(&freshly_composed)
                .contains("round-two-self-authored-authority"),
            "fixture must prove the mutable source really changed"
        );
        assert!(
            !crate::prompts::system_prompt_flat_text(&round_two)
                .contains("round-two-self-authored-authority")
        );
    }

    fn workspace_registry() -> (tempfile::TempDir, ToolRegistry) {
        let dir = tempfile::tempdir().expect("tempdir");
        let config = Config {
            allow_shell: Some(true),
            ..Config::default()
        };
        let registry = build_acp_tool_registry(&config, dir.path(), true);
        (dir, registry)
    }

    fn pending_call(name: &str, input: Value) -> PendingToolCall {
        PendingToolCall {
            id: "call_1".to_string(),
            name: name.to_string(),
            input,
            parse_error: None,
        }
    }

    fn config_with_policy_rule(rule: codewhale_execpolicy::ToolAskRule) -> Config {
        Config {
            exec_policy_engine: codewhale_execpolicy::ExecPolicyEngine::with_rulesets(vec![
                codewhale_execpolicy::Ruleset::user(vec![], vec![]).with_ask_rules(vec![rule]),
            ]),
            ..Config::default()
        }
    }

    fn tool_call_hook_command(payload: &Value) -> String {
        let payload = payload.to_string();
        if cfg!(windows) {
            format!("echo {payload}")
        } else {
            format!("printf '%s\\n' '{payload}'")
        }
    }

    fn config_with_tool_call_hook(mut config: Config, payload: Value, strict: bool) -> Config {
        let mut hook = crate::hooks::Hook::new(
            crate::hooks::HookEvent::ToolCallBefore,
            &tool_call_hook_command(&payload),
        );
        hook.continue_on_error = !strict;
        config.hooks = Some(crate::hooks::HooksConfig {
            enabled: true,
            hooks: vec![hook],
            ..crate::hooks::HooksConfig::default()
        });
        config
    }

    #[tokio::test]
    async fn acp_strict_tool_call_before_deny_blocks_before_execution() {
        let dir = tempfile::tempdir().expect("tempdir");
        let config = config_with_tool_call_hook(
            Config::default(),
            json!({"decision": "deny", "reason": "release gate"}),
            true,
        );
        let registry = build_acp_tool_registry(&config, dir.path(), false);
        let error = prepare_acp_tool_with_hooks(
            &config,
            "test-model",
            &registry,
            &pending_call("File", json!({"action": "read", "path": "safe.txt"})),
        )
        .await
        .expect_err("strict hook must deny");

        assert!(error.to_string().contains("release gate"));
    }

    #[tokio::test]
    async fn acp_hook_rewrite_is_reprepared_and_policy_is_re_evaluated() {
        let dir = tempfile::tempdir().expect("tempdir");
        let deny_rewritten_write = codewhale_execpolicy::ToolAskRule {
            action: codewhale_execpolicy::PermissionAction::Deny,
            ..codewhale_execpolicy::ToolAskRule::file_path("write_file", "rewritten.txt")
        };
        let config = config_with_tool_call_hook(
            config_with_policy_rule(deny_rewritten_write),
            json!({
                "updatedInput": {
                    "action": "write",
                    "path": "rewritten.txt",
                    "content": "rewritten by hook"
                }
            }),
            true,
        );
        let registry = build_acp_tool_registry(&config, dir.path(), false);
        let raw = pending_call("File", json!({"action": "read", "path": "safe.txt"}));
        let (_, raw_admission) = prepare_acp_tool_admission(&config, &registry, &raw).unwrap();
        assert_eq!(raw_admission, AcpToolAdmission::Auto);

        let prepared = prepare_acp_tool_with_hooks(&config, "test-model", &registry, &raw)
            .await
            .expect("hook rewrite prepares");
        assert_eq!(
            prepared.call.input.get("action").and_then(Value::as_str),
            Some("write")
        );
        assert!(matches!(prepared.admission, AcpToolAdmission::Block(_)));
        assert!(!dir.path().join("rewritten.txt").exists());
    }

    #[test]
    fn acp_admission_is_input_specific_and_has_no_workspace_write_carve_out() {
        let (dir, registry) = workspace_registry();
        let config = Config::default();
        let read = pending_call("File", json!({"action": "read", "path": "src/lib.rs"}));
        let write = pending_call(
            "File",
            json!({"action": "write", "path": "src/lib.rs", "content": "new"}),
        );

        let (_, read_admission) = prepare_acp_tool_admission(&config, &registry, &read).unwrap();
        let (_, write_admission) = prepare_acp_tool_admission(&config, &registry, &write).unwrap();

        assert_eq!(read_admission, AcpToolAdmission::Auto);
        assert!(matches!(
            write_admission,
            AcpToolAdmission::RequestPermission(_)
        ));
        assert_eq!(registry.context().workspace, dir.path());
    }

    #[test]
    fn acp_admission_folds_typed_rules_then_headless_safety_floor() {
        let (dir, registry) = workspace_registry();
        let workspace = dir.path().to_string_lossy().into_owned();
        let input = json!({"action": "write", "path": "allowed.txt", "content": "new"});
        let call = pending_call("File", input.clone());

        let allow = codewhale_execpolicy::ToolAskRule::file_path("write_file", "allowed.txt")
            .into_exact_workspace_allow(workspace.clone());
        let (_, admission) =
            prepare_acp_tool_admission(&config_with_policy_rule(allow), &registry, &call).unwrap();
        assert_eq!(admission, AcpToolAdmission::Auto);

        let ask = codewhale_execpolicy::ToolAskRule::file_path("write_file", "allowed.txt");
        let (_, admission) =
            prepare_acp_tool_admission(&config_with_policy_rule(ask), &registry, &call).unwrap();
        assert!(matches!(
            admission,
            AcpToolAdmission::RequestPermission(reason) if reason.contains("requires approval")
        ));

        let deny = codewhale_execpolicy::ToolAskRule {
            action: codewhale_execpolicy::PermissionAction::Deny,
            ..codewhale_execpolicy::ToolAskRule::file_path("write_file", "allowed.txt")
        };
        let (_, admission) =
            prepare_acp_tool_admission(&config_with_policy_rule(deny), &registry, &call).unwrap();
        assert!(matches!(admission, AcpToolAdmission::Block(_)));

        let command = "rm -rf ~/";
        let shell_allow = codewhale_execpolicy::ToolAskRule::exec_shell(command)
            .into_exact_workspace_allow(workspace);
        let shell_call = pending_call("Bash", json!({"command": command}));
        let (_, admission) = prepare_acp_tool_admission(
            &config_with_policy_rule(shell_allow),
            &registry,
            &shell_call,
        )
        .unwrap();
        assert!(matches!(
            admission,
            AcpToolAdmission::RequestPermission(reason)
                if reason.contains("Built-in safety gate")
        ));
    }

    #[test]
    fn acp_admission_blocks_detached_and_stateful_bash_inputs() {
        let (_dir, registry) = workspace_registry();
        for input in [
            json!({"command": "sleep 30", "background": true}),
            json!({"command": "sleep 30", "tty": true}),
            json!({"command": "echo hi", "interactive": true}),
            json!({"command": "serve", "background": true, "persist": true}),
            json!({"command": "sleep 30 &"}),
            json!({"command": "nohup sleep 30"}),
            json!({"action": "wait", "task_id": "shell-1"}),
            json!({"action": "cancel", "task_id": "shell-1"}),
        ] {
            let call = pending_call("Bash", input);
            let (_, admission) =
                prepare_acp_tool_admission(&Config::default(), &registry, &call).unwrap();
            assert!(matches!(
                admission,
                AcpToolAdmission::Block(reason)
                    if reason.contains("foreground Bash runs only")
            ));
        }
        assert!(!acp_shell_command_requests_detach("echo '&' && echo done"));
    }

    #[test]
    fn acp_admission_auto_review_and_repo_law_override_typed_allow() {
        let (dir, registry) = workspace_registry();
        let workspace = dir.path().to_string_lossy().into_owned();
        let command = "cargo test";
        let shell_allow = codewhale_execpolicy::ToolAskRule::exec_shell(command)
            .into_exact_workspace_allow(workspace.clone());
        let mut auto_review_block = config_with_policy_rule(shell_allow);
        auto_review_block.auto_review = Some(crate::config::AutoReviewConfig {
            block: vec![crate::config::AutoReviewRuleConfig {
                id: Some("acp-shell-block".to_string()),
                action_kind: Some("shell".to_string()),
                reason: Some("ACP shell is disabled by policy".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        });
        let (_, admission) = prepare_acp_tool_admission(
            &auto_review_block,
            &registry,
            &pending_call("Bash", json!({"command": command})),
        )
        .unwrap();
        assert!(matches!(
            admission,
            AcpToolAdmission::Block(reason) if reason.contains("ACP shell is disabled by policy")
        ));

        let law_dir = dir.path().join(".codewhale");
        std::fs::create_dir_all(&law_dir).unwrap();
        std::fs::write(
            law_dir.join("constitution.json"),
            r#"{
                "protected_invariants": [
                    { "text": "Never rewrite the wire", "paths": ["wire.rs"], "action": "block" },
                    { "text": "Review release notes", "paths": ["CHANGELOG.md"] }
                ]
            }"#,
        )
        .unwrap();

        for (path, expected_block) in [("wire.rs", true), ("CHANGELOG.md", false)] {
            let allow = codewhale_execpolicy::ToolAskRule::file_path("write_file", path)
                .into_exact_workspace_allow(workspace.clone());
            let config = config_with_policy_rule(allow);
            let call = pending_call(
                "File",
                json!({"action": "write", "path": path, "content": "new"}),
            );
            let (_, admission) = prepare_acp_tool_admission(&config, &registry, &call).unwrap();
            if expected_block {
                assert!(matches!(
                    admission,
                    AcpToolAdmission::Block(reason) if reason.contains("Never rewrite the wire")
                ));
            } else {
                assert!(matches!(
                    admission,
                    AcpToolAdmission::RequestPermission(reason)
                        if reason.contains("Review release notes")
                ));
            }
        }
    }

    #[tokio::test]
    async fn acp_read_runs_without_permission_but_reports_pending_before_in_progress() {
        let (dir, registry) = workspace_registry();
        std::fs::write(dir.path().join("read.txt"), "safe").unwrap();
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let outcome = execute_tool_calls_with_cancellation(
            AcpTurnContext {
                config: &Config::default(),
                model: "test-model",
                session_id: "sess_1",
                tool_registry: &registry,
                response_id_policy: JsonRpcResponseIdPolicy::Preserve,
            },
            vec![pending_call(
                "File",
                json!({"action": "read", "path": "read.txt"}),
            )],
            &mut reader,
            &mut out,
        )
        .await
        .unwrap();

        assert!(matches!(outcome, ToolBatchOutcome::Completed(_)));
        let messages = parse_lines(out);
        assert!(!messages.iter().any(|message| {
            message.get("method").and_then(Value::as_str) == Some("session/request_permission")
        }));
        let statuses = messages
            .iter()
            .filter_map(|message| {
                message
                    .pointer("/params/update/status")
                    .and_then(Value::as_str)
            })
            .collect::<Vec<_>>();
        assert_eq!(statuses, vec!["pending", "in_progress", "completed"]);
    }

    #[tokio::test]
    async fn acp_permission_allow_executes_write_once_after_response() {
        let (dir, registry) = workspace_registry();
        let target = dir.path().join("allowed.txt");
        let (outcome, messages, late) = execute_one_with_permission_client(
            &Config::default(),
            &registry,
            pending_call(
                "File",
                json!({"action": "write", "path": "allowed.txt", "content": "written once"}),
            ),
            PermissionClientScript::Allow,
            JsonRpcResponseIdPolicy::Preserve,
            Some(target.clone()),
        )
        .await;

        assert!(late.is_none());
        assert!(matches!(outcome, ToolBatchOutcome::Completed(_)));
        assert_eq!(std::fs::read_to_string(target).unwrap(), "written once");
        let request = messages
            .iter()
            .find(|message| message["method"] == "session/request_permission")
            .expect("permission request");
        assert_eq!(request["params"]["toolCall"]["status"], "pending");
        assert_eq!(request["params"]["options"][0]["kind"], "allow_once");
        assert_eq!(request["params"]["options"][1]["kind"], "reject_once");
        let statuses = messages
            .iter()
            .filter_map(|message| {
                message
                    .pointer("/params/update/status")
                    .and_then(Value::as_str)
            })
            .collect::<Vec<_>>();
        assert_eq!(statuses, vec!["pending", "in_progress", "completed"]);
    }

    #[tokio::test]
    async fn acp_permission_reject_and_wrong_id_fail_closed_without_write() {
        for script in [
            PermissionClientScript::Reject,
            PermissionClientScript::WrongIdThenReject,
        ] {
            let (dir, registry) = workspace_registry();
            let target = dir.path().join("denied.txt");
            let (outcome, messages, _) = execute_one_with_permission_client(
                &Config::default(),
                &registry,
                pending_call(
                    "File",
                    json!({"action": "write", "path": "denied.txt", "content": "forbidden"}),
                ),
                script,
                JsonRpcResponseIdPolicy::Preserve,
                Some(target.clone()),
            )
            .await;

            assert!(!target.exists(), "{script:?} must not authorize the write");
            let ToolBatchOutcome::Completed(results) = outcome else {
                panic!("rejection should complete with a failed tool result");
            };
            assert_eq!(results.len(), 1);
            let ContentBlock::ToolResult { is_error, .. } = &results[0].content[0] else {
                panic!("expected tool result");
            };
            assert_eq!(*is_error, Some(true));
            assert!(!messages.iter().any(|message| {
                message
                    .pointer("/params/update/status")
                    .and_then(Value::as_str)
                    == Some("in_progress")
            }));
        }
    }

    #[tokio::test]
    async fn acp_permission_cancel_ignores_late_allow_and_never_runs_tool() {
        let (dir, registry) = workspace_registry();
        let target = dir.path().join("cancelled.txt");
        let (outcome, messages, late) = execute_one_with_permission_client(
            &Config::default(),
            &registry,
            pending_call(
                "File",
                json!({"action": "write", "path": "cancelled.txt", "content": "forbidden"}),
            ),
            PermissionClientScript::CancelThenLateAllow,
            JsonRpcResponseIdPolicy::Preserve,
            Some(target.clone()),
        )
        .await;

        assert!(!target.exists());
        assert!(matches!(outcome, ToolBatchOutcome::Cancelled(_)));
        assert!(messages.iter().any(|message| {
            message
                .pointer("/params/update/status")
                .and_then(Value::as_str)
                == Some("failed")
        }));
        let late = late.expect("late allow remains queued for the outer dispatcher");
        assert!(is_jsonrpc_response(&late));
        assert_eq!(
            late.pointer("/result/outcome/optionId")
                .and_then(Value::as_str),
            Some("allow-once")
        );
    }

    #[tokio::test]
    async fn tool_registry_read_file_returns_real_contents() {
        let (dir, registry) = workspace_registry();
        std::fs::write(dir.path().join("hello.txt"), "hi there").unwrap();

        let result = registry
            .execute_full("File", json!({"action": "read", "path": "hello.txt"}))
            .await
            .expect("read_file succeeds");

        assert!(result.success);
        assert!(result.content.contains("hi there"));
    }

    #[tokio::test]
    async fn tool_registry_write_file_creates_a_real_file() {
        let (dir, registry) = workspace_registry();

        let result = registry
            .execute_full(
                "File",
                json!({"action": "write", "path": "created.txt", "content": "new content"}),
            )
            .await
            .expect("write_file succeeds");

        assert!(result.success);
        let on_disk = std::fs::read_to_string(dir.path().join("created.txt")).unwrap();
        assert_eq!(on_disk, "new content");
    }

    #[tokio::test]
    async fn tool_registry_list_dir_reports_real_directory_contents() {
        let (dir, registry) = workspace_registry();
        std::fs::write(dir.path().join("a.txt"), "a").unwrap();
        std::fs::write(dir.path().join("b.txt"), "b").unwrap();

        let result = registry
            .execute_full("File", json!({"action": "list", "path": "."}))
            .await
            .expect("list_dir succeeds");

        assert!(result.success);
        assert!(result.content.contains("a.txt"));
        assert!(result.content.contains("b.txt"));
    }

    #[tokio::test]
    async fn tool_registry_bash_runs_a_real_command() {
        let (_dir, registry) = workspace_registry();

        let result = registry
            .execute_full("Bash", json!({"command": "echo acp-terminal-check"}))
            .await
            .expect("Bash succeeds");

        assert!(result.content.contains("acp-terminal-check"));
    }

    #[tokio::test]
    async fn tool_registry_read_file_reports_failure_for_missing_path() {
        let (_dir, registry) = workspace_registry();

        let err = registry
            .execute_full(
                "File",
                json!({"action": "read", "path": "does-not-exist.txt"}),
            )
            .await
            .expect_err("missing file is a tool error");

        assert!(!err.to_string().is_empty());
    }

    /// Feeds [`run_agentic_prompt_turn`] a fixed sequence of canned per-round
    /// streams (no real provider), so the multi-round tool loop — including
    /// nested tool calls across several rounds — is exercised end-to-end
    /// against the real file-tool registry. A plain struct + inherent async
    /// method (rather than a boxed closure) lets each test's `|msgs| scripted.next()`
    /// closure return the async method's own anonymous future type directly,
    /// so `Fut` is inferred without needing a `dyn Future` trait object.
    struct ScriptedStreams(RefCell<VecDeque<StreamEventBox>>);

    impl ScriptedStreams {
        fn new(streams: Vec<StreamEventBox>) -> Self {
            Self(RefCell::new(VecDeque::from(streams)))
        }

        async fn next(&self) -> Result<StreamEventBox> {
            Ok(self
                .0
                .borrow_mut()
                .pop_front()
                .expect("test provided enough scripted rounds"))
        }
    }

    #[tokio::test]
    async fn agentic_turn_executes_a_tool_call_then_streams_the_final_answer() {
        let (dir, registry) = workspace_registry();
        std::fs::write(dir.path().join("VERSION"), "9.9.9").unwrap();

        let round1 = ready_stream({
            let mut events =
                tool_use_events(0, "call_1", "File", r#"{"action":"read","path":"VERSION"}"#);
            events.push(StreamEvent::MessageStop);
            events
        });
        let round2 = ready_stream(vec![
            text_delta("The version is 9.9.9"),
            StreamEvent::MessageStop,
        ]);

        let scripted = ScriptedStreams::new(vec![round1, round2]);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let (outcome, messages) = run_agentic_prompt_turn(
            AcpTurnContext {
                config: &Config::default(),
                model: "test-model",
                session_id: "sess_1",
                tool_registry: &registry,
                response_id_policy: JsonRpcResponseIdPolicy::Preserve,
            },
            vec![Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: "What version is this?".to_string(),
                    cache_control: None,
                }],
            }],
            &mut reader,
            &mut out,
            |_msgs| scripted.next(),
        )
        .await
        .expect("turn completes");

        assert_eq!(
            outcome,
            PromptOutcome::Completed("The version is 9.9.9".to_string())
        );
        // user -> assistant(tool_use) -> user(tool_result) -> assistant(text)
        assert_eq!(messages.len(), 4);
        assert!(matches!(
            messages[1].content[0],
            ContentBlock::ToolUse { .. }
        ));
        let ContentBlock::ToolResult {
            content, is_error, ..
        } = &messages[2].content[0]
        else {
            panic!("expected a tool_result message");
        };
        assert!(content.contains("9.9.9"));
        assert_eq!(*is_error, Some(false));

        // The client saw a tool_call start, a completed update, and the
        // streamed final-answer chunk.
        let lines = parse_lines(out);
        assert!(
            lines
                .iter()
                .any(|v| v["params"]["update"]["sessionUpdate"] == "tool_call")
        );
        assert!(lines.iter().any(
            |v| v["params"]["update"]["sessionUpdate"] == "tool_call_update"
                && v["params"]["update"]["status"] == "completed"
        ));
        assert!(lines.iter().any(|v| v["params"]["update"]["sessionUpdate"]
            == "agent_message_chunk"
            && v["params"]["update"]["content"]["text"] == "The version is 9.9.9"));
    }

    #[tokio::test]
    async fn agentic_turn_preserves_tool_receipts_when_later_provider_round_fails() {
        let (dir, registry) = workspace_registry();
        std::fs::write(dir.path().join("receipt.txt"), "observed").unwrap();
        let round1 = ready_stream({
            let mut events = tool_use_events(
                0,
                "call_receipt",
                "File",
                r#"{"action":"read","path":"receipt.txt"}"#,
            );
            events.push(StreamEvent::MessageStop);
            events
        });
        let scripted = ScriptedStreams::new(vec![round1, error_stream("provider unavailable")]);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let error = run_agentic_prompt_turn(
            AcpTurnContext {
                config: &Config::default(),
                model: "test-model",
                session_id: "sess_1",
                tool_registry: &registry,
                response_id_policy: JsonRpcResponseIdPolicy::Preserve,
            },
            vec![Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: "Read receipt.txt".to_string(),
                    cache_control: None,
                }],
            }],
            &mut reader,
            &mut out,
            |_msgs| scripted.next(),
        )
        .await
        .expect_err("second provider round fails");

        assert!(error.source.to_string().contains("provider unavailable"));
        let messages = error
            .partial_messages
            .expect("completed tool receipt must be returned for commit");
        assert_eq!(messages.len(), 3);
        assert!(matches!(
            &messages[2].content[0],
            ContentBlock::ToolResult { tool_use_id, .. } if tool_use_id == "call_receipt"
        ));
    }

    #[tokio::test]
    async fn agentic_turn_chains_nested_tool_calls_across_rounds() {
        let (dir, registry) = workspace_registry();
        std::fs::write(dir.path().join("a.txt"), "contents-of-a").unwrap();
        std::fs::write(dir.path().join("b.txt"), "contents-of-b").unwrap();

        let round1 = ready_stream({
            let mut events =
                tool_use_events(0, "call_1", "File", r#"{"action":"read","path":"a.txt"}"#);
            events.push(StreamEvent::MessageStop);
            events
        });
        // After seeing a.txt's contents, the model asks for b.txt too.
        let round2 = ready_stream({
            let mut events =
                tool_use_events(0, "call_2", "File", r#"{"action":"read","path":"b.txt"}"#);
            events.push(StreamEvent::MessageStop);
            events
        });
        let round3 = ready_stream(vec![
            text_delta("Both files read"),
            StreamEvent::MessageStop,
        ]);

        let scripted = ScriptedStreams::new(vec![round1, round2, round3]);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let (outcome, messages) = run_agentic_prompt_turn(
            AcpTurnContext {
                config: &Config::default(),
                model: "test-model",
                session_id: "sess_1",
                tool_registry: &registry,
                response_id_policy: JsonRpcResponseIdPolicy::Preserve,
            },
            vec![Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: "Read both files".to_string(),
                    cache_control: None,
                }],
            }],
            &mut reader,
            &mut out,
            |_msgs| scripted.next(),
        )
        .await
        .expect("turn completes");

        assert_eq!(
            outcome,
            PromptOutcome::Completed("Both files read".to_string())
        );
        // user, assistant(tool_use a), user(result a), assistant(tool_use b),
        // user(result b), assistant(text)
        assert_eq!(messages.len(), 6);
        let ContentBlock::ToolResult {
            content: a_content, ..
        } = &messages[2].content[0]
        else {
            panic!("expected tool_result for a.txt");
        };
        assert!(a_content.contains("contents-of-a"));
        let ContentBlock::ToolResult {
            content: b_content, ..
        } = &messages[4].content[0]
        else {
            panic!("expected tool_result for b.txt");
        };
        assert!(b_content.contains("contents-of-b"));
    }

    #[tokio::test]
    async fn agentic_turn_reports_a_tool_failure_back_to_the_model_and_keeps_going() {
        let (_dir, registry) = workspace_registry();

        let round1 = ready_stream({
            let mut events = tool_use_events(
                0,
                "call_1",
                "File",
                r#"{"action":"read","path":"missing.txt"}"#,
            );
            events.push(StreamEvent::MessageStop);
            events
        });
        let round2 = ready_stream(vec![
            text_delta("That file does not exist"),
            StreamEvent::MessageStop,
        ]);

        let scripted = ScriptedStreams::new(vec![round1, round2]);
        let mut reader = lines_from("");
        let mut out = Vec::new();

        let (outcome, messages) = run_agentic_prompt_turn(
            AcpTurnContext {
                config: &Config::default(),
                model: "test-model",
                session_id: "sess_1",
                tool_registry: &registry,
                response_id_policy: JsonRpcResponseIdPolicy::Preserve,
            },
            vec![Message {
                role: "user".to_string(),
                content: vec![ContentBlock::Text {
                    text: "Read missing.txt".to_string(),
                    cache_control: None,
                }],
            }],
            &mut reader,
            &mut out,
            |_msgs| scripted.next(),
        )
        .await
        .expect("turn completes even though the tool failed");

        assert_eq!(
            outcome,
            PromptOutcome::Completed("That file does not exist".to_string())
        );
        let ContentBlock::ToolResult { is_error, .. } = &messages[2].content[0] else {
            panic!("expected a tool_result message");
        };
        assert_eq!(*is_error, Some(true));

        let lines = parse_lines(out);
        assert!(lines.iter().any(
            |v| v["params"]["update"]["sessionUpdate"] == "tool_call_update"
                && v["params"]["update"]["status"] == "failed"
        ));
    }

    #[cfg(windows)]
    const SLOW_SHELL_COMMAND: &str = "ping -n 6 127.0.0.1 >NUL";
    #[cfg(not(windows))]
    const SLOW_SHELL_COMMAND: &str = "sleep 5";

    #[tokio::test]
    async fn tool_batch_cancels_a_running_bash_and_applies_response_id_policy() {
        let (_dir, registry) = workspace_registry();
        let started = std::time::Instant::now();

        let (outcome, messages, _) = execute_one_with_permission_client(
            &Config::default(),
            &registry,
            pending_call("Bash", json!({ "command": SLOW_SHELL_COMMAND })),
            PermissionClientScript::AllowThenCancelRunning,
            JsonRpcResponseIdPolicy::StringifyNumeric,
            None,
        )
        .await;

        assert!(matches!(outcome, ToolBatchOutcome::Cancelled(_)));
        assert!(
            started.elapsed() < std::time::Duration::from_secs(4),
            "Bash cancellation must preempt the five-second command"
        );
        assert!(
            messages
                .iter()
                .any(|value| value["id"] == "7" && value["result"].is_null()),
            "Zed-compatible cancellation response id was not stringified: {messages:?}"
        );
    }

    #[tokio::test]
    async fn concurrent_acp_sessions_execute_tools_independently() {
        let (dir1, registry1) = workspace_registry();
        let (dir2, registry2) = workspace_registry();
        std::fs::write(dir1.path().join("f.txt"), "session-one").unwrap();
        std::fs::write(dir2.path().join("f.txt"), "session-two").unwrap();

        let (result1, result2) = tokio::join!(
            registry1.execute_full("File", json!({"action": "read", "path": "f.txt"})),
            registry2.execute_full("File", json!({"action": "read", "path": "f.txt"})),
        );

        assert!(
            result1
                .expect("session 1 read")
                .content
                .contains("session-one")
        );
        assert!(
            result2
                .expect("session 2 read")
                .content
                .contains("session-two")
        );
    }
}