arabica-cli 0.13.1

Arabica: a terminal coding agent with a fullscreen TUI, ACP support for Zed, and one-shot runs
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
//! `arabica acp`: Agent Client Protocol v1 over stdio.
//!
//! See `docs/runtime_core_architecture.md` Appendix B for the decision
//! record and `crates/arabica-cli/src/host.rs` for the composition this
//! builds on. Every `session/new` gets its own `SessionManager` (plan
//! decision #8: one lock per session, not a single lock serializing every
//! Zed window), and every `session/prompt` runs the turn on a spawned task
//! (`docs/runtime_core_architecture.md` §9) so the JSON-RPC dispatch loop
//! stays free to deliver `session/cancel` and permission responses while a
//! turn is in flight -- see `concepts::ordering` in the `agent-client-protocol`
//! crate for why a handler must never `.await` its own run to completion.

use crate::mcp::auth::CredentialBroker;

#[cfg(test)]
mod configuration_tests;
mod content;
mod evaluation;
mod mapping;
mod mcp_credentials;
mod permission;
mod session_metadata;
mod stdio;
mod stop_reason;
mod time;

use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use agent_client_protocol::schema::ProtocolVersion;
use agent_client_protocol::schema::v1::{
    AgentCapabilities, CancelNotification, CloseSessionRequest, CloseSessionResponse,
    Implementation, InitializeRequest, InitializeResponse, ListSessionsRequest,
    ListSessionsResponse, LoadSessionRequest, LoadSessionResponse, McpCapabilities, McpServer,
    NewSessionRequest, NewSessionResponse, PromptCapabilities, PromptRequest, PromptResponse,
    ResumeSessionRequest, ResumeSessionResponse, SessionCapabilities, SessionCloseCapabilities,
    SessionConfigOption, SessionConfigOptionCategory, SessionConfigSelectOption,
    SessionId as AcpSessionId, SessionInfo, SessionListCapabilities, SessionNotification,
    SessionResumeCapabilities, SetSessionConfigOptionRequest, SetSessionConfigOptionResponse,
};
use agent_client_protocol::{
    Agent, Client, ConnectionTo, Error as AcpError, Responder, Result as AcpResult,
};
#[cfg(test)]
use arabica_adapters::SqliteSessionStore;
use arabica_protocol::{
    Command, CommandEnvelope, CommandId, RunId as StructureRunId, SessionId as StructureSessionId,
    SessionStatus,
};
use arabica_runtime::{BlendRoutingPolicy, RunCancellation, RunControl, ToolPermissionGate};
use arabica_session::{
    DispatchControl, EventVisibility, FanOutObserver, IdAllocator, SessionError,
    SessionEventObserver, SessionManager,
};
use arabica_session::{NewSession, SessionStore, SessionWriter};

use crate::config::{ResolvedCliConfig, SOE_POLICY_ID};
use crate::host::{HostModel, HostRuntime, LocalRunnerPolicy, build_host_runtime_with_blend};
#[cfg(test)]
use arabica_provider::{ApiModelProvider, BlendProvider};
use arabica_provider::{ApiProviderConfig, ApiType};

#[derive(Clone)]
struct AcpProviderSettings {
    overrides: crate::host::HostConfigArgs,
    initial: ApiProviderConfig,
    models: Vec<String>,
    aliases: BTreeMap<String, String>,
    model_configs: BTreeMap<String, ApiProviderConfig>,
    blend_policy: Option<BlendRoutingPolicy>,
    blend_policies: BTreeMap<String, BlendRoutingPolicy>,
}

impl AcpProviderSettings {
    fn from_resolved(
        overrides: crate::host::HostConfigArgs,
        resolved: &ResolvedCliConfig,
    ) -> Result<Self, AcpError> {
        let catalog = resolved.model_catalog();
        let blend_policies = resolved.blend_policies.clone();
        if blend_policies.contains_key(SOE_POLICY_ID) {
            return Err(AcpError::invalid_params().data(
                "soe is reserved for the single-model policy in ACP; rename the configured policy",
            ));
        }
        let provider_config = catalog.provider.clone();
        let aliases = catalog.models.clone();
        let model_configs = catalog.model_configs.clone();
        let blend_policy = catalog.blend_policy.clone();
        let mut models = aliases.keys().cloned().collect::<Vec<_>>();
        let initial_selection = blend_policy
            .as_ref()
            .map(|policy| policy.default_model.clone());
        if !aliases.is_empty() {
            if initial_selection
                .as_ref()
                .is_none_or(|selected| !aliases.contains_key(selected))
            {
                return Err(AcpError::invalid_params()
                    .data("Blend default_model must name a configured model alias"));
            }
        } else if !models.contains(&provider_config.model) {
            models.insert(0, provider_config.model.clone());
        }
        Ok(Self {
            overrides,
            initial: provider_config,
            models,
            aliases,
            model_configs,
            blend_policy,
            blend_policies,
        })
    }

    fn configured_for_workspace(&self, home: &Path, cwd: &Path) -> AcpResult<Self> {
        // Named policies use the file's per-model thinking. Invocation and
        // workspace thinking overrides belong only to ACP's SOE policy.
        let mut overrides = self.overrides.clone();
        overrides.thinking = None;
        overrides.model_alias = None;
        if overrides.policy.as_deref() == Some(SOE_POLICY_ID) {
            overrides.policy = None;
        }
        match crate::config::resolve_cli_acp_config(&overrides, home, cwd, |key| {
            std::env::var(key).ok()
        }) {
            Ok(resolved) => Self::from_resolved(self.overrides.clone(), &resolved),
            Err(error) if home.join("config.toml").exists() => {
                Err(AcpError::invalid_params().data(error.to_string()))
            }
            // Tests can supply an in-memory provider catalog without a user file.
            Err(_) => Ok(self.clone()),
        }
    }

    fn for_workspace(&self, home: &Path, cwd: &Path) -> AcpResult<Self> {
        let mut next = self.configured_for_workspace(home, cwd)?;
        let preferences = crate::config::read_workspace_settings(home, cwd)
            .map_err(|error| AcpError::invalid_params().data(error.to_string()))?;
        let policy_id = self
            .overrides
            .policy
            .as_ref()
            .or(preferences.blend_policy.as_ref());
        if next.aliases.is_empty() || policy_id.is_some_and(|id| id == SOE_POLICY_ID) {
            return next.with_soe_preferences(&preferences);
        }
        if let Some(id) = policy_id {
            next.blend_policy = Some(next.blend_policies.get(id).cloned().ok_or_else(|| {
                AcpError::invalid_params().data(format!("unknown Blend policy {id:?}"))
            })?);
        }
        if let Some(policy) = &next.blend_policy {
            next.initial = next
                .model_configs
                .get(&policy.default_model)
                .cloned()
                .ok_or_else(|| {
                    AcpError::invalid_params().data("policy default model is unavailable")
                })?;
        }
        Ok(next)
    }

    fn with_soe_preferences(
        mut self,
        preferences: &crate::config::WorkspaceSettings,
    ) -> AcpResult<Self> {
        if !self.aliases.is_empty() {
            let alias = self
                .overrides
                .model_alias
                .clone()
                .or_else(|| preferences.model_alias.clone())
                .or_else(|| {
                    self.blend_policy
                        .as_ref()
                        .map(|policy| policy.default_model.clone())
                })
                .ok_or_else(|| AcpError::invalid_params().data("SOE model is unavailable"))?;
            if !self.aliases.contains_key(&alias) {
                return Err(
                    AcpError::invalid_params().data(format!("unknown model alias {alias:?}"))
                );
            }
            for (alias, thinking) in &preferences.model_thinking {
                if let Some(config) = self.model_configs.get_mut(alias) {
                    crate::config::set_thinking(config, thinking)
                        .map_err(|error| AcpError::invalid_params().data(error))?;
                }
            }
            let selected = self
                .model_configs
                .get_mut(&alias)
                .ok_or_else(|| AcpError::invalid_params().data("SOE model is unavailable"))?;
            if let Some(model) = self
                .overrides
                .model
                .clone()
                .or_else(|| preferences.model.clone())
            {
                selected.model = model;
            }
            if let Some(thinking) = self.overrides.thinking.as_ref().or_else(|| {
                (!preferences.model_thinking.contains_key(&alias))
                    .then_some(preferences.thinking.as_ref())
                    .flatten()
            }) {
                crate::config::set_thinking(selected, thinking)
                    .map_err(|error| AcpError::invalid_params().data(error))?;
            }
            self.initial = selected.clone();
            self.blend_policy = Some(crate::config::soe_policy(alias));
        } else {
            if let Some(model) = self
                .overrides
                .model
                .clone()
                .or_else(|| preferences.model.clone())
            {
                self.initial.model = model;
            }
            if let Some(thinking) = self
                .overrides
                .thinking
                .as_ref()
                .or(preferences.thinking.as_ref())
            {
                crate::config::set_thinking(&mut self.initial, thinking)
                    .map_err(|error| AcpError::invalid_params().data(error))?;
            }
            self.blend_policy = None;
        }
        Ok(self)
    }

    fn is_soe(&self, policy: Option<&BlendRoutingPolicy>) -> bool {
        self.aliases.is_empty() || policy.is_some_and(|policy| policy.policy_id == SOE_POLICY_ID)
    }

    fn build_model(&self) -> AcpResult<HostModel> {
        crate::host::HostModelCatalog {
            provider: self.initial.clone(),
            models: self.aliases.clone(),
            blend_policy: self.blend_policy.clone(),
            model_configs: self.model_configs.clone(),
        }
        .build_model()
        .map_err(provider_error)
    }

    fn options(
        &self,
        config: &ApiProviderConfig,
        policy: Option<&BlendRoutingPolicy>,
    ) -> Vec<SessionConfigOption> {
        let mut policies = vec![SessionConfigSelectOption::new(SOE_POLICY_ID, "SOE")];
        policies.extend(
            self.blend_policies
                .keys()
                .map(|id| SessionConfigSelectOption::new(id.clone(), id.clone())),
        );
        let selected_policy = match policy {
            Some(policy) if !self.is_soe(Some(policy)) => {
                crate::config::policy_selection_key(&self.blend_policies, policy).to_owned()
            }
            _ => SOE_POLICY_ID.to_owned(),
        };
        let mut options = vec![
            SessionConfigOption::select("blend.policy", "Policy", selected_policy, policies)
                .category(SessionConfigOptionCategory::ModelConfig),
        ];
        if !self.is_soe(policy) {
            return options;
        }
        let selected_model = policy
            .map(|policy| policy.default_model.as_str())
            .unwrap_or(&config.model);
        let models = self
            .models
            .iter()
            .map(|model| {
                SessionConfigSelectOption::new(
                    model.clone(),
                    if model == selected_model && self.aliases.contains_key(model) {
                        format!("{model} · {}", config.model)
                    } else {
                        self.model_configs.get(model).map_or_else(
                            || model.clone(),
                            |config| format!("{model} · {}", config.model),
                        )
                    },
                )
            })
            .collect::<Vec<_>>();
        options.push(
            SessionConfigOption::select("model", "Model", selected_model.to_owned(), models)
                .category(SessionConfigOptionCategory::Model),
        );
        if !matches!(
            config.api_type,
            ApiType::OpenAiChatCompletions | ApiType::OpenAiResponses
        ) {
            return options;
        }
        let levels = crate::config::thinking_levels(config.api_type);
        let current = crate::config::thinking_level(config);
        options.push(
            SessionConfigOption::select(
                "thinking",
                "Thinking",
                current.to_owned(),
                levels
                    .iter()
                    .copied()
                    .map(|level| SessionConfigSelectOption::new(level, level))
                    .collect::<Vec<_>>(),
            )
            .category(SessionConfigOptionCategory::ThoughtLevel),
        );
        options
    }
}

/// Structure's own session/run ids only need to be unique within one
/// `SessionManager`, but this host runs one `SessionManager` per ACP
/// session, and every session's ids end up in the same log stream (stderr,
/// and eventually a shared session store per Phase 2). Sequential ids would
/// make every session's first run "run-1"; UUIDv7 keeps them distinguishable
/// and, being time-ordered, still sort the way a human skimming a log
/// expects.
#[derive(Debug, Default)]
struct UuidIds;

impl IdAllocator for UuidIds {
    fn session_id(&mut self) -> StructureSessionId {
        StructureSessionId::new(uuid::Uuid::now_v7().to_string())
    }

    fn run_id(&mut self) -> StructureRunId {
        StructureRunId::new(uuid::Uuid::now_v7().to_string())
    }
}

struct SessionEntry {
    manager: Arc<tokio::sync::Mutex<SessionManager<HostRuntime>>>,
    arabica_session_id: StructureSessionId,
    cwd: PathBuf,
    /// The in-flight run's cancellation handle, if any. `session/cancel`
    /// reads this; `session/prompt` sets it for the run it starts and
    /// clears it when that run ends.
    current_run: Mutex<Option<RunCancellation>>,
    /// Persists every Event for this session across its whole ACP
    /// connection lifetime (every `session/prompt`, not just one), unlike
    /// `print.rs`'s `run_task`, which builds a fresh store for one run and
    /// drops it. `Arc` because `handle_prompt` shares it into a
    /// per-call `FanOutObserver` alongside the live `AcpObserver`.
    store: Arc<dyn SessionWriter>,
    provider_config: Option<Mutex<ApiProviderConfig>>,
    model_configs: Option<Mutex<BTreeMap<String, ApiProviderConfig>>>,
    blend_policy: Option<Mutex<BlendRoutingPolicy>>,
}

/// Builds one fresh [`HostModel`] per `session/new`: every ACP session gets
/// its own model instance (`HostModel` holds per-connection state and is not
/// `Clone`), all built from the same resolved configuration. Boxed so tests
/// can substitute a `Scripted` model without touching a real provider.
type ModelFactory = dyn Fn() -> Result<HostModel, AcpError> + Send + Sync;

#[derive(Clone)]
struct AcpState {
    model_factory: Arc<ModelFactory>,
    tool_policy: LocalRunnerPolicy,
    permission_policy: arabica_runtime::ToolPermissionPolicy,
    arabica_home: PathBuf,
    session_store: Arc<dyn SessionStore>,
    sessions: Arc<Mutex<HashMap<AcpSessionId, Arc<SessionEntry>>>>,
    provider_settings: Option<Arc<AcpProviderSettings>>,
}

impl AcpState {
    fn new(
        model_factory: Arc<ModelFactory>,
        tool_policy: LocalRunnerPolicy,
        arabica_home: PathBuf,
    ) -> Self {
        Self {
            model_factory,
            tool_policy,
            permission_policy: permission::default_policy(),
            session_store: crate::host::session_store(&arabica_home),
            arabica_home,
            sessions: Arc::new(Mutex::new(HashMap::new())),
            provider_settings: None,
        }
    }

    fn with_provider_settings(mut self, settings: AcpProviderSettings) -> Self {
        self.provider_settings = Some(Arc::new(settings));
        self
    }

    fn with_permission_policy(mut self, policy: arabica_runtime::ToolPermissionPolicy) -> Self {
        self.permission_policy = policy;
        self
    }

    fn config_options(settings: Option<&AcpProviderSettings>) -> Option<Vec<SessionConfigOption>> {
        settings.map(|settings| settings.options(&settings.initial, settings.blend_policy.as_ref()))
    }

    fn build_runtime(
        &self,
        model: HostModel,
        cwd: &Path,
        mcp: crate::mcp::McpTools,
        settings: Option<&AcpProviderSettings>,
    ) -> Result<HostRuntime, AcpError> {
        build_host_runtime_with_blend(
            model,
            cwd,
            self.tool_policy.clone(),
            &self.arabica_home,
            mcp,
            settings.and_then(|settings| settings.blend_policy.clone()),
        )
        .map_err(runtime_error)
    }

    fn entry(&self, session_id: &AcpSessionId) -> Option<Arc<SessionEntry>> {
        self.sessions
            .lock()
            .expect("session map lock poisoned")
            .get(session_id)
            .cloned()
    }

    async fn activate_restored_session(
        manager: &mut SessionManager<HostRuntime>,
        session_id: &StructureSessionId,
        store: &Arc<dyn SessionWriter>,
    ) -> AcpResult<()> {
        let status = manager
            .session(session_id)
            .ok_or_else(|| AcpError::internal_error().data("restored session is missing"))?
            .status;
        match status {
            SessionStatus::Active => Ok(()),
            SessionStatus::Suspended => {
                manager
                    .dispatch(
                        CommandEnvelope::new(
                            CommandId::new(uuid::Uuid::now_v7().to_string()),
                            Some(session_id.clone()),
                            Command::SessionResume,
                        ),
                        DispatchControl {
                            run: RunControl::default(),
                            observer: Some(Arc::clone(store) as Arc<dyn SessionEventObserver>),
                        },
                    )
                    .await
                    .map_err(session_error)?;
                Ok(())
            }
            SessionStatus::Closed => Err(AcpError::invalid_params()
                .data(format!("session {session_id} is permanently closed"))),
        }
    }

    async fn new_session(
        &self,
        request: NewSessionRequest,
        broker: Option<Arc<dyn CredentialBroker>>,
    ) -> AcpResult<NewSessionResponse> {
        if !request.cwd.is_absolute() {
            return Err(AcpError::invalid_params()
                .data(format!("cwd must be absolute: {}", request.cwd.display())));
        }
        let workspace_id = crate::host::workspace_id_for(&request.cwd);
        let session_settings = self
            .provider_settings
            .as_ref()
            .map(|settings| settings.for_workspace(&self.arabica_home, &request.cwd))
            .transpose()?;
        let model = match &session_settings {
            Some(settings) => settings.build_model()?,
            None => (self.model_factory)()?,
        };
        let mcp = connect_session_mcp(
            request.mcp_servers,
            &request.cwd,
            &self.arabica_home,
            broker,
        )
        .await?;
        let runtime = self.build_runtime(model, &request.cwd, mcp, session_settings.as_ref())?;
        let mut manager = SessionManager::with_ids(runtime, Box::new(UuidIds));
        let instructions_sha256 =
            crate::instructions::sha256(manager.runtime().system_instructions());
        let envelope = CommandEnvelope::new(
            CommandId::new(uuid::Uuid::now_v7().to_string()),
            None,
            Command::SessionCreate {
                workspace_id: workspace_id.clone(),
            },
        );
        let events = manager
            .dispatch(envelope, DispatchControl::default())
            .await
            .map_err(session_error)?;
        // dispatch's own return value always carries the produced Events
        // regardless of whether an observer was attached, which is the only
        // way to get at session.created here at all: the store below needs
        // this session's id to name its file, but that id is allocated
        // *inside* this very dispatch call, so the store cannot exist yet to
        // observe it live. `store.observe` a few lines down closes that gap
        // by hand (the same pattern `print.rs`'s `run_task` uses).
        let session_created = events
            .first()
            .ok_or_else(|| AcpError::internal_error().data("session.create produced no Events"))?;
        let arabica_session_id = session_created.session_id.clone();
        let acp_session_id = AcpSessionId::new(arabica_session_id.to_string());

        let store = self
            .session_store
            .create(NewSession {
                session_id: &arabica_session_id,
                workspace_id: &workspace_id,
                cwd: &request.cwd,
                profile: None,
                instructions_sha256: Some(&instructions_sha256),
            })
            .map_err(|error| AcpError::internal_error().data(error.to_string()))?;
        store.observe(session_created, EventVisibility::Client);

        self.sessions
            .lock()
            .expect("session map lock poisoned")
            .insert(
                acp_session_id.clone(),
                Arc::new(SessionEntry {
                    manager: Arc::new(tokio::sync::Mutex::new(manager)),
                    arabica_session_id,
                    cwd: request.cwd,
                    current_run: Mutex::new(None),
                    store,
                    provider_config: session_settings
                        .as_ref()
                        .map(|settings| Mutex::new(settings.initial.clone())),
                    model_configs: session_settings
                        .as_ref()
                        .map(|settings| Mutex::new(settings.model_configs.clone())),
                    blend_policy: session_settings
                        .as_ref()
                        .and_then(|settings| settings.blend_policy.clone())
                        .map(Mutex::new),
                }),
            );
        Ok(NewSessionResponse::new(acp_session_id)
            .config_options(Self::config_options(session_settings.as_ref())))
    }

    /// `session/load`: only reachable once `initialize` has advertised
    /// `agentCapabilities.loadSession` (set unconditionally in `serve` below,
    /// since this handler exists). Replay-then-respond is the client's
    /// contract (`agent_client_protocol::session::RestoreSessionBuilder`'s
    /// own doc comment: "replay notifications sent before the response are
    /// available"), so nothing is sent to the client until the session is
    /// known-restorable -- a `session/load` that fails must not have already
    /// shown the client a history for a session it cannot actually resume.
    async fn load_session(
        &self,
        request: LoadSessionRequest,
        connection: &ConnectionTo<Client>,
    ) -> AcpResult<LoadSessionResponse> {
        if !request.cwd.is_absolute() {
            return Err(AcpError::invalid_params()
                .data(format!("cwd must be absolute: {}", request.cwd.display())));
        }
        let workspace_id = crate::host::workspace_id_for(&request.cwd);
        let arabica_session_id = StructureSessionId::new(request.session_id.to_string());

        let stored = self
            .session_store
            .read(&workspace_id, &arabica_session_id)
            .map_err(|error| {
                AcpError::invalid_params().data(format!(
                    "cannot load session {}: {error}",
                    request.session_id
                ))
            })?;
        let original_events = stored.events.clone();

        let store = self
            .session_store
            .open(&workspace_id, &arabica_session_id)
            .map_err(|error| AcpError::internal_error().data(error.to_string()))?;

        let session_settings = self
            .provider_settings
            .as_ref()
            .map(|settings| settings.for_workspace(&self.arabica_home, &request.cwd))
            .transpose()?;
        let model = match &session_settings {
            Some(settings) => settings.build_model()?,
            None => (self.model_factory)()?,
        };
        let mcp = connect_session_mcp(
            request.mcp_servers,
            &request.cwd,
            &self.arabica_home,
            Some(Arc::new(mcp_credentials::AcpCredentialBroker::new(
                connection.clone(),
            ))),
        )
        .await?;
        let runtime = self.build_runtime(model, &request.cwd, mcp, session_settings.as_ref())?;
        let mut manager = SessionManager::with_ids(runtime, Box::new(UuidIds));
        let restore_report = manager
            .restore_session(
                stored.into_snapshot(),
                CommandId::new(uuid::Uuid::now_v7().to_string()),
                Some(store.as_ref() as &dyn SessionEventObserver),
            )
            .map_err(session_error)?;
        Self::activate_restored_session(&mut manager, &arabica_session_id, &store).await?;

        let acp_session_id = AcpSessionId::new(arabica_session_id.to_string());
        // Original history first, then any repair Events restore_session
        // synthesized for a run the previous connection never got to finish
        // (dangling tool calls, then run.failed) -- the repairs are new
        // Events appended after the stored history, not part of it, so they
        // belong after it in replay order too.
        let mut open_calls = mapping::OpenToolCalls::default();
        for envelope in original_events
            .iter()
            .chain(restore_report.repaired_events.iter())
        {
            let mut updates =
                mapping::replay_updates_for(&envelope.event, &envelope.run_id, &request.cwd);
            updates.extend(open_calls.track(&envelope.event, &envelope.run_id));
            for update in updates {
                if let Err(error) = connection
                    .send_notification(SessionNotification::new(acp_session_id.clone(), update))
                {
                    eprintln!(
                        "structure acp: dropped a session/update during session/load replay: {error}"
                    );
                }
            }
        }

        self.sessions
            .lock()
            .expect("session map lock poisoned")
            .insert(
                acp_session_id,
                Arc::new(SessionEntry {
                    manager: Arc::new(tokio::sync::Mutex::new(manager)),
                    arabica_session_id,
                    cwd: request.cwd,
                    current_run: Mutex::new(None),
                    store,
                    provider_config: session_settings
                        .as_ref()
                        .map(|settings| Mutex::new(settings.initial.clone())),
                    model_configs: session_settings
                        .as_ref()
                        .map(|settings| Mutex::new(settings.model_configs.clone())),
                    blend_policy: session_settings
                        .as_ref()
                        .and_then(|settings| settings.blend_policy.clone())
                        .map(Mutex::new),
                }),
            );

        Ok(LoadSessionResponse::new()
            .config_options(Self::config_options(session_settings.as_ref())))
    }

    /// `session/resume`: only reachable once `initialize` has advertised
    /// `agentCapabilities.sessionCapabilities.resume`. The restore half of
    /// `load_session`, without the replay half -- "resume without returning
    /// previous messages" is the method's own documented contract, not a
    /// shortcut taken here. Deliberately a near-twin of `load_session`
    /// rather than a shared helper: the two methods differ in exactly one
    /// self-contained slice (the replay loop and its `connection` parameter)
    /// bracketed by identical restore logic on both sides, and factoring
    /// that out would cost more in indirection than it would save in lines.
    async fn resume_session(
        &self,
        request: ResumeSessionRequest,
        broker: Option<Arc<dyn CredentialBroker>>,
    ) -> AcpResult<ResumeSessionResponse> {
        if !request.cwd.is_absolute() {
            return Err(AcpError::invalid_params()
                .data(format!("cwd must be absolute: {}", request.cwd.display())));
        }
        let workspace_id = crate::host::workspace_id_for(&request.cwd);
        let arabica_session_id = StructureSessionId::new(request.session_id.to_string());

        let stored = self
            .session_store
            .read(&workspace_id, &arabica_session_id)
            .map_err(|error| {
                AcpError::invalid_params().data(format!(
                    "cannot resume session {}: {error}",
                    request.session_id
                ))
            })?;

        let store = self
            .session_store
            .open(&workspace_id, &arabica_session_id)
            .map_err(|error| AcpError::internal_error().data(error.to_string()))?;

        let session_settings = self
            .provider_settings
            .as_ref()
            .map(|settings| settings.for_workspace(&self.arabica_home, &request.cwd))
            .transpose()?;
        let model = match &session_settings {
            Some(settings) => settings.build_model()?,
            None => (self.model_factory)()?,
        };
        let mcp = connect_session_mcp(
            request.mcp_servers,
            &request.cwd,
            &self.arabica_home,
            broker,
        )
        .await?;
        let runtime = self.build_runtime(model, &request.cwd, mcp, session_settings.as_ref())?;
        let mut manager = SessionManager::with_ids(runtime, Box::new(UuidIds));
        manager
            .restore_session(
                stored.into_snapshot(),
                CommandId::new(uuid::Uuid::now_v7().to_string()),
                Some(store.as_ref() as &dyn SessionEventObserver),
            )
            .map_err(session_error)?;
        Self::activate_restored_session(&mut manager, &arabica_session_id, &store).await?;

        let acp_session_id = AcpSessionId::new(arabica_session_id.to_string());
        self.sessions
            .lock()
            .expect("session map lock poisoned")
            .insert(
                acp_session_id,
                Arc::new(SessionEntry {
                    manager: Arc::new(tokio::sync::Mutex::new(manager)),
                    arabica_session_id,
                    cwd: request.cwd,
                    current_run: Mutex::new(None),
                    store,
                    provider_config: session_settings
                        .as_ref()
                        .map(|settings| Mutex::new(settings.initial.clone())),
                    model_configs: session_settings
                        .as_ref()
                        .map(|settings| Mutex::new(settings.model_configs.clone())),
                    blend_policy: session_settings
                        .as_ref()
                        .and_then(|settings| settings.blend_policy.clone())
                        .map(Mutex::new),
                }),
            );

        Ok(ResumeSessionResponse::new()
            .config_options(Self::config_options(session_settings.as_ref())))
    }

    fn set_config_option(
        &self,
        request: SetSessionConfigOptionRequest,
    ) -> AcpResult<SetSessionConfigOptionResponse> {
        let initial_settings = self.provider_settings.as_ref().ok_or_else(|| {
            AcpError::invalid_request().data("provider configuration is unavailable")
        })?;
        let entry = self.entry(&request.session_id).ok_or_else(|| {
            AcpError::invalid_params().data(format!("unknown session {}", request.session_id))
        })?;
        let settings = initial_settings.configured_for_workspace(&self.arabica_home, &entry.cwd)?;
        let config_lock = entry.provider_config.as_ref().ok_or_else(|| {
            AcpError::invalid_request().data("provider configuration is unavailable")
        })?;
        let mut manager = entry.manager.try_lock().map_err(|_| {
            AcpError::invalid_request().data("a prompt is already in progress for this session")
        })?;
        let mut config = config_lock.lock().expect("provider config lock poisoned");
        let value = request
            .value
            .as_value_id()
            .ok_or_else(|| AcpError::invalid_params().data("expected a select value"))?
            .to_string();
        let config_id = request.config_id.to_string();
        if config_id == "blend.policy" {
            let mut next = if value == SOE_POLICY_ID {
                let preferences =
                    crate::config::read_workspace_settings(&self.arabica_home, &entry.cwd)
                        .map_err(|error| AcpError::invalid_params().data(error.to_string()))?;
                settings.with_soe_preferences(&preferences)?
            } else {
                let policy = settings
                    .blend_policies
                    .get(&value)
                    .cloned()
                    .ok_or_else(|| AcpError::invalid_params().data("unknown Blend policy"))?;
                let mut next = settings;
                next.initial = next
                    .model_configs
                    .get(&policy.default_model)
                    .cloned()
                    .ok_or_else(|| {
                        AcpError::invalid_params().data("policy default model is unavailable")
                    })?;
                next.blend_policy = Some(policy);
                next
            };
            // No-alias API hosts have intrinsic SOE without Blend routing.
            if next.aliases.is_empty() {
                next.blend_policy = None;
            }
            let selected_alias = next
                .blend_policy
                .as_ref()
                .map(|policy| policy.default_model.clone());
            return self.apply_provider_settings(
                &entry,
                manager.runtime_mut(),
                &mut config,
                next,
                |preferences| {
                    preferences.blend_policy = Some(value.clone());
                    if value == SOE_POLICY_ID
                        && let Some(alias) = selected_alias
                    {
                        preferences.model_alias = Some(alias);
                    }
                    // SOE's remembered model and thinking remain available when
                    // visiting a named policy, but do not affect its providers.
                },
            );
        }
        let current_policy = entry
            .blend_policy
            .as_ref()
            .map(|lock| lock.lock().expect("Blend policy lock poisoned").clone());
        if !settings.is_soe(current_policy.as_ref())
            && matches!(config_id.as_str(), "model" | "thinking")
        {
            return Err(AcpError::invalid_params().data(
                "model and thinking can be changed only under SOE; edit the configuration file for named policies",
            ));
        }
        let mut next = settings;
        next.initial = config.clone();
        next.blend_policy = current_policy;
        next.model_configs = entry
            .model_configs
            .as_ref()
            .map(|configs| configs.lock().expect("model configs lock poisoned").clone())
            .unwrap_or_else(|| next.model_configs.clone());
        let mut selected_alias = None;
        match config_id.as_str() {
            "model" if next.aliases.contains_key(&value) => {
                next.initial = next.model_configs.get(&value).cloned().ok_or_else(|| {
                    AcpError::invalid_params().data("selected model is unavailable")
                })?;
                next.blend_policy = Some(crate::config::soe_policy(value.clone()));
                selected_alias = Some(value.clone());
            }
            "model" if next.models.contains(&value) => next.initial.model = value.clone(),
            "thinking" => crate::config::set_thinking(&mut next.initial, &value)
                .map_err(|error| AcpError::invalid_params().data(error))?,
            "model" => {
                return Err(AcpError::invalid_params()
                    .data("model is not a configured model or Blend alias"));
            }
            _ => return Err(AcpError::invalid_params().data("unknown configuration option")),
        }
        let active_alias = next
            .blend_policy
            .as_ref()
            .map(|policy| policy.default_model.clone());
        if let Some(alias) = &active_alias {
            next.model_configs
                .insert(alias.clone(), next.initial.clone());
        }
        let selected_model = next.initial.model.clone();
        self.apply_provider_settings(
            &entry,
            manager.runtime_mut(),
            &mut config,
            next,
            |preferences| {
                preferences.blend_policy = Some(SOE_POLICY_ID.to_owned());
                if config_id == "thinking" {
                    if let Some(alias) = &active_alias {
                        preferences.thinking = None;
                        preferences
                            .model_thinking
                            .insert(alias.clone(), value.clone());
                    } else {
                        preferences.thinking = Some(value.clone());
                    }
                } else if let Some(alias) = &selected_alias {
                    preferences.model = None;
                    preferences.model_alias = Some(alias.clone());
                    preferences.thinking = None;
                } else {
                    preferences.model = Some(selected_model);
                }
            },
        )
    }

    fn apply_provider_settings(
        &self,
        entry: &SessionEntry,
        runtime: &mut HostRuntime,
        config: &mut ApiProviderConfig,
        next: AcpProviderSettings,
        edit_preferences: impl FnOnce(&mut crate::config::WorkspaceSettings),
    ) -> AcpResult<SetSessionConfigOptionResponse> {
        let model = next.build_model()?;
        let previous_policy = entry
            .blend_policy
            .as_ref()
            .map(|lock| lock.lock().expect("Blend policy lock poisoned").clone());
        let previous_model = std::mem::replace(runtime.model_mut(), model);
        if let Err(error) = runtime.set_blend_policy(next.blend_policy.clone()) {
            *runtime.model_mut() = previous_model;
            return Err(runtime_error(error));
        }
        if let Err(error) =
            crate::config::save_workspace_settings(&self.arabica_home, &entry.cwd, edit_preferences)
        {
            *runtime.model_mut() = previous_model;
            runtime
                .set_blend_policy(previous_policy)
                .map_err(runtime_error)?;
            return Err(AcpError::internal_error().data(error.to_string()));
        }
        *config = next.initial.clone();
        if let Some(lock) = &entry.blend_policy
            && let Some(policy) = &next.blend_policy
        {
            *lock.lock().expect("Blend policy lock poisoned") = policy.clone();
        }
        if let Some(configs) = &entry.model_configs {
            *configs.lock().expect("model configs lock poisoned") = next.model_configs.clone();
        }
        Ok(SetSessionConfigOptionResponse::new(
            next.options(config, next.blend_policy.as_ref()),
        ))
    }

    /// `session/list`: only reachable once `initialize` has advertised
    /// `agentCapabilities.sessionCapabilities.list` (set unconditionally in
    /// `serve` below, since this handler exists). Synchronous: unlike
    /// `new_session`/`load_session`, nothing here touches a runtime or a
    /// model, only `$ARABICA_HOME` on disk.
    fn list_sessions(&self, request: ListSessionsRequest) -> AcpResult<ListSessionsResponse> {
        if let Some(cwd) = &request.cwd
            && !cwd.is_absolute()
        {
            return Err(
                AcpError::invalid_params().data(format!("cwd must be absolute: {}", cwd.display()))
            );
        }
        // No cwd filter means every workspace, the same "cwd absent" ->
        // "no workspace scope" mapping print mode's `structure sessions
        // list --all` uses (`SessionStore::list`'s own
        // `workspace_id: Option<&WorkspaceId>` parameter).
        let workspace_id = request
            .cwd
            .as_ref()
            .map(|cwd| crate::host::workspace_id_for(cwd));
        let listings = self
            .session_store
            .list(workspace_id.as_ref())
            .map_err(|error| AcpError::internal_error().data(error.to_string()))?;
        let sessions = listings
            .into_iter()
            .map(|listing| {
                let mut info = SessionInfo::new(
                    AcpSessionId::new(listing.header.id.to_string()),
                    listing.header.cwd,
                );
                if let Some(title) = listing.header.title {
                    info = info.title(title);
                }
                if let Some(modified_at_ms) = listing.updated_at_ms {
                    info = info.updated_at(time::to_iso8601(modified_at_ms));
                }
                info
            })
            .collect();
        Ok(ListSessionsResponse::new(sessions))
    }

    fn cancel(&self, session_id: &AcpSessionId) {
        let Some(entry) = self.entry(session_id) else {
            return;
        };
        if let Some(cancellation) = entry
            .current_run
            .lock()
            .expect("current_run lock poisoned")
            .as_ref()
        {
            cancellation.cancel();
        }
    }

    /// `session/close`: only reachable once `initialize` has advertised
    /// `agentCapabilities.sessionCapabilities.close`. Maps to
    /// `Command::SessionSuspend`, not `Command::SessionClose` -- Structure's
    /// own `session.close` is a terminal state no further Command can move a
    /// session out of, but ACP's `session/close` only means "this
    /// connection is done with the session for now," and the same session
    /// can still come back later through `session/load`/`session/resume`.
    /// Any in-flight run is cancelled first (the same non-blocking signal
    /// `session/cancel` sends) before waiting on the session's own lock, so
    /// this does not hang waiting for a run that would otherwise keep going
    /// on its own; removed from `self.sessions` only once the suspend
    /// itself has actually succeeded, so a rejected suspend leaves the
    /// session exactly as promptable as it was before this call.
    async fn close_session(&self, request: CloseSessionRequest) -> AcpResult<CloseSessionResponse> {
        let Some(entry) = self.entry(&request.session_id) else {
            return Err(
                AcpError::invalid_params().data(format!("unknown session {}", request.session_id))
            );
        };
        self.cancel(&request.session_id);

        let envelope = CommandEnvelope::new(
            CommandId::new(uuid::Uuid::now_v7().to_string()),
            Some(entry.arabica_session_id.clone()),
            Command::SessionSuspend,
        );
        let control = DispatchControl {
            run: RunControl::default(),
            observer: Some(Arc::clone(&entry.store) as Arc<dyn SessionEventObserver>),
        };
        let mut guard = entry.manager.lock().await;
        guard
            .dispatch(envelope, control)
            .await
            .map_err(session_error)?;
        drop(guard);

        self.sessions
            .lock()
            .expect("session map lock poisoned")
            .remove(&request.session_id);

        Ok(CloseSessionResponse::new())
    }
}

fn provider_error(error: arabica_provider::ProviderError) -> AcpError {
    AcpError::internal_error().data(error.to_string())
}

fn runtime_error(error: arabica_runtime::RuntimeError) -> AcpError {
    AcpError::internal_error().data(error.to_string())
}

fn session_error(error: SessionError) -> AcpError {
    match error.code {
        arabica_protocol::ErrorCode::SessionNotFound
        | arabica_protocol::ErrorCode::InvalidCommand
        | arabica_protocol::ErrorCode::InvalidSessionState
        | arabica_protocol::ErrorCode::ProtocolVersionMismatch => {
            AcpError::invalid_params().data(error.message)
        }
        _ => AcpError::internal_error().data(error.message),
    }
}

/// Runs `arabica acp` to completion (until stdin closes). Every prompt
/// turn asks the ACP client for permission through `permission::bridge` and
/// streams progress through `mapping::AcpObserver`; neither ever writes to
/// stdout, which carries only this connection's own JSON-RPC frames.
pub async fn run(resolved: ResolvedCliConfig, tool_policy: LocalRunnerPolicy) -> AcpResult<()> {
    let arabica_home = arabica_adapters::default_arabica_home()
        .map_err(|error| AcpError::internal_error().data(error.to_string()))?;
    let settings = AcpProviderSettings::from_resolved(resolved.invocation.clone(), &resolved)?;
    let permission_policy = resolved.permission_policy.clone();
    let model_catalog = resolved.model_catalog();
    let model_factory: Arc<ModelFactory> =
        Arc::new(move || model_catalog.build_model().map_err(provider_error));
    let (transport, stdin_closed) = stdio::transport();
    stdio::serve_until_shutdown(
        serve(
            AcpState::new(model_factory, tool_policy, arabica_home)
                .with_provider_settings(settings)
                .with_permission_policy(permission_policy),
            transport,
        ),
        stdin_closed,
        stdio::SHUTDOWN_GRACE,
    )
    .await
}

/// The handler chain, generic over the transport so tests can drive it
/// through an in-memory [`agent_client_protocol::Channel`] instead of real
/// stdio.
async fn serve(
    state: AcpState,
    transport: impl agent_client_protocol::ConnectTo<Agent>,
) -> AcpResult<()> {
    Agent
        .builder()
        .name("arabica")
        .on_receive_request(
            async move |request: InitializeRequest,
                        responder: Responder<InitializeResponse>,
                        _connection: ConnectionTo<Client>| {
                let _ = request.protocol_version;
                responder.respond(
                    InitializeResponse::new(ProtocolVersion::V1)
                        .agent_capabilities(
                            AgentCapabilities::new()
                                .prompt_capabilities(PromptCapabilities::new().image(true))
                                .load_session(true)
                                .mcp_capabilities(McpCapabilities::new().http(true))
                                .session_capabilities(
                                    SessionCapabilities::new()
                                        .list(SessionListCapabilities::new())
                                        .resume(SessionResumeCapabilities::new())
                                        .close(SessionCloseCapabilities::new()),
                                ),
                        )
                        .agent_info(Implementation::new("arabica", env!("CARGO_PKG_VERSION")))
                        .meta(serde_json::Map::from_iter([
                            (
                                "arabica.evaluation".into(),
                                serde_json::json!({
                                    "schemaVersion": 1,
                                    "get": "_arabica/evaluation/get",
                                    "refresh": "_arabica/evaluation/refresh",
                                    "policyAction": "_arabica/evaluation/policy/action",
                                    "asynchronous": true
                                }),
                            ),
                            (
                                "arabica.mcpAuth".into(),
                                serde_json::json!({
                                    "schemaVersion": 1,
                                    "metaKey": crate::mcp::auth::META_KEY,
                                    "modes": ["broker", "oauth"],
                                    "credential": crate::mcp::auth::CREDENTIAL_METHOD
                                }),
                            ),
                            (
                                "arabica.session".into(),
                                serde_json::json!({
                                    "schemaVersion": 1,
                                    "rename": session_metadata::RENAME_METHOD,
                                    "maxTitleLength": session_metadata::MAX_TITLE_LENGTH
                                }),
                            ),
                        ])),
                )
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: NewSessionRequest,
                            responder: Responder<NewSessionResponse>,
                            connection: ConnectionTo<Client>| {
                    // Session setup may ask the client for MCP credentials,
                    // so it runs off the dispatch loop that delivers the reply.
                    let state = state.clone();
                    let broker: Arc<dyn CredentialBroker> = Arc::new(
                        mcp_credentials::AcpCredentialBroker::new(connection.clone()),
                    );
                    connection.spawn(async move {
                        match state.new_session(request, Some(broker)).await {
                            Ok(response) => responder.respond(response),
                            Err(error) => responder.respond_with_error(error),
                        }
                    })
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: PromptRequest,
                            responder: Responder<PromptResponse>,
                            connection: ConnectionTo<Client>| {
                    handle_prompt(state.clone(), request, responder, connection)
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: SetSessionConfigOptionRequest,
                            responder: Responder<SetSessionConfigOptionResponse>,
                            _connection: ConnectionTo<Client>| {
                    match state.set_config_option(request) {
                        Ok(response) => responder.respond(response),
                        Err(error) => responder.respond_with_error(error),
                    }
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: LoadSessionRequest,
                            responder: Responder<LoadSessionResponse>,
                            connection: ConnectionTo<Client>| {
                    let state = state.clone();
                    let task_connection = connection.clone();
                    connection.spawn(async move {
                        match state.load_session(request, &task_connection).await {
                            Ok(response) => responder.respond(response),
                            Err(error) => responder.respond_with_error(error),
                        }
                    })
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: ListSessionsRequest,
                            responder: Responder<ListSessionsResponse>,
                            _connection: ConnectionTo<Client>| {
                    match state.list_sessions(request) {
                        Ok(response) => responder.respond(response),
                        Err(error) => responder.respond_with_error(error),
                    }
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: ResumeSessionRequest,
                            responder: Responder<ResumeSessionResponse>,
                            connection: ConnectionTo<Client>| {
                    let state = state.clone();
                    let broker: Arc<dyn CredentialBroker> = Arc::new(
                        mcp_credentials::AcpCredentialBroker::new(connection.clone()),
                    );
                    connection.spawn(async move {
                        match state.resume_session(request, Some(broker)).await {
                            Ok(response) => responder.respond(response),
                            Err(error) => responder.respond_with_error(error),
                        }
                    })
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: CloseSessionRequest,
                            responder: Responder<CloseSessionResponse>,
                            _connection: ConnectionTo<Client>| {
                    match state.close_session(request).await {
                        Ok(response) => responder.respond(response),
                        Err(error) => responder.respond_with_error(error),
                    }
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: session_metadata::RenameSessionRequest,
                            responder: Responder<session_metadata::RenameSessionResponse>,
                            _connection: ConnectionTo<Client>| {
                    match session_metadata::rename(&state, request) {
                        Ok(response) => responder.respond(response),
                        Err(error) => responder.respond_with_error(error),
                    }
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: evaluation::GetEvaluationRequest,
                            responder: Responder<serde_json::Value>,
                            _connection: ConnectionTo<Client>| {
                    match evaluation::query(&state, &request.session_id, false) {
                        Ok(view) => responder.respond(view),
                        Err(error) => responder.respond_with_error(error),
                    }
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: evaluation::RefreshEvaluationRequest,
                            responder: Responder<serde_json::Value>,
                            _connection: ConnectionTo<Client>| {
                    match evaluation::query(&state, &request.session_id, true) {
                        Ok(view) => responder.respond(view),
                        Err(error) => responder.respond_with_error(error),
                    }
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_request(
            {
                let state = state.clone();
                async move |request: evaluation::PolicyActionRequest,
                            responder: Responder<serde_json::Value>,
                            _connection: ConnectionTo<Client>| {
                    match evaluation::policy_action(&state, &request).await {
                        Ok(view) => responder.respond(view),
                        Err(error) => responder.respond_with_error(error),
                    }
                }
            },
            agent_client_protocol::on_receive_request!(),
        )
        .on_receive_notification(
            {
                let state = state.clone();
                async move |notification: CancelNotification, _connection: ConnectionTo<Client>| {
                    state.cancel(&notification.session_id);
                    Ok(())
                }
            },
            agent_client_protocol::on_receive_notification!(),
        )
        .connect_to(transport)
        .await
}

/// Connect an ACP request's client-supplied MCP servers, then the ones
/// declared in the user config file behind them; when both declare the same
/// name the client's entry wins, because the editor is the more specific
/// configuration. A client-supplied server that fails to connect fails the
/// request (the client asked for it); a user-config server that fails is
/// reported on stderr and skipped, so one broken entry in the config file
/// cannot take the session down.
async fn connect_session_mcp(
    client_servers: Vec<McpServer>,
    cwd: &Path,
    arabica_home: &Path,
    broker: Option<Arc<dyn CredentialBroker>>,
) -> AcpResult<crate::mcp::McpTools> {
    let environment = crate::mcp::auth::AuthEnvironment {
        arabica_home: Some(arabica_home.to_path_buf()),
        broker,
    };
    let client_names: HashSet<String> = client_servers
        .iter()
        .map(crate::mcp::server_name)
        .map(str::to_owned)
        .collect();
    let mut tools = crate::mcp::McpTools::connect(client_servers, cwd, &environment)
        .await
        .map_err(|error| AcpError::invalid_params().data(error))?;
    let configured = crate::config::user_config_mcp(arabica_home)
        .map_err(|error| AcpError::internal_error().data(error.to_string()))?;
    let not_supplied_by_client = configured
        .into_iter()
        .filter(|server| !client_names.contains(crate::mcp::server_name(server)))
        .collect::<Vec<_>>();
    let (extra, diagnostics) =
        crate::mcp::McpTools::connect_lenient(not_supplied_by_client, cwd, &environment).await;
    for diagnostic in diagnostics {
        eprintln!("structure: {diagnostic}");
    }
    tools
        .extend(extra)
        .map_err(|error| AcpError::invalid_params().data(error))?;
    Ok(tools)
}

/// The `session/prompt` handler proper, split out of the closure only for
/// readability: look up the session and claim its manager synchronously (so
/// a concurrent second prompt on the same session is rejected immediately,
/// not queued), then hand the actual turn to a spawned task and return.
fn handle_prompt(
    state: AcpState,
    request: PromptRequest,
    responder: Responder<PromptResponse>,
    connection: ConnectionTo<Client>,
) -> AcpResult<()> {
    let Some(entry) = state.entry(&request.session_id) else {
        return responder.respond_with_error(
            AcpError::invalid_params().data(format!("unknown session {}", request.session_id)),
        );
    };
    let Ok(mut guard) = Arc::clone(&entry.manager).try_lock_owned() else {
        return responder.respond_with_error(
            AcpError::invalid_request().data("a prompt is already in progress for this session"),
        );
    };

    let images = match content::prompt_images(&request.prompt) {
        Ok(images) => images,
        Err(message) => {
            return responder.respond_with_error(AcpError::invalid_params().data(message));
        }
    };
    let content = content::prompt_text(&request.prompt);
    let envelope = CommandEnvelope::new(
        CommandId::new(uuid::Uuid::now_v7().to_string()),
        Some(entry.arabica_session_id.clone()),
        Command::MessageSend { content, images },
    );
    let cancellation = RunCancellation::new();
    *entry.current_run.lock().expect("current_run lock poisoned") = Some(cancellation.clone());

    let (permission_tx, permission_rx) = tokio::sync::mpsc::unbounded_channel();
    let acp_observer = Arc::new(mapping::AcpObserver::new(
        connection.clone(),
        request.session_id.clone(),
        entry.cwd.clone(),
    ));
    let progress = acp_observer.progress_sink();
    // Re-read the workspace `AGENTS.md` files so a file edited mid-session
    // reaches this turn's model request, matching terminal chat's per-turn
    // refresh.
    guard
        .runtime_mut()
        .set_system_instructions(crate::host::system_instructions(&entry.cwd));
    let model = guard.runtime_mut().model_mut();
    match model {
        HostModel::Api(_) => {
            let old = std::mem::replace(
                model,
                HostModel::Scripted(crate::host::ScriptedModel::default()),
            );
            if let HostModel::Api(provider) = old {
                *model = HostModel::StreamingApi(provider, progress);
            }
        }
        HostModel::StreamingApi(_, sink) => *sink = progress,
        HostModel::Blend(_) => {}
        HostModel::Scripted(_) => {}
    }
    let mut observers: Vec<Arc<dyn SessionEventObserver>> = vec![
        Arc::clone(&entry.store) as Arc<dyn SessionEventObserver>,
        acp_observer as Arc<dyn SessionEventObserver>,
    ];
    crate::evaluation::attach(&mut observers, &state.arabica_home);
    let observer: Arc<dyn SessionEventObserver> = Arc::new(FanOutObserver::new(observers));
    let control = DispatchControl {
        run: RunControl {
            cancellation: Some(cancellation),
            permissions: Some(ToolPermissionGate {
                policy: state.permission_policy.clone(),
                approver: Some(permission_tx),
            }),
        },
        observer: Some(observer),
    };

    let session_id = request.session_id.clone();
    let permission_connection = connection.clone();
    let permission_cwd = entry.cwd.clone();
    connection.spawn(async move {
        tokio::spawn(permission::bridge(
            permission_rx,
            permission_connection,
            session_id,
            permission_cwd,
        ));
        let mut guard = guard;
        let result = guard.dispatch(envelope, control).await;
        drop(guard);
        *entry.current_run.lock().expect("current_run lock poisoned") = None;

        let response = match result {
            Ok(events) => match stop_reason::classify(&events) {
                stop_reason::PromptOutcome::Stopped(reason) => {
                    responder.respond(PromptResponse::new(reason))
                }
                stop_reason::PromptOutcome::Failed(message) => {
                    responder.respond_with_error(AcpError::internal_error().data(message))
                }
            },
            Err(error) => responder.respond_with_error(session_error(error)),
        };
        if let Err(error) = response {
            eprintln!("structure acp: failed to send a session/prompt response: {error}");
        }
        Ok(())
    })
}

#[cfg(test)]
mod round_trip {
    //! Drives `serve` over an in-memory `Channel::duplex()` with a real
    //! `Client` builder standing in for the editor, so the concurrency this
    //! module depends on (`cx.spawn`, the live observer, the permission
    //! bridge) is exercised for real rather than assumed from reading the
    //! code. A fuller scenario matrix (cancellation mid-run, step limits,
    //! provider errors, a real `arabica acp` binary against a mock HTTP
    //! server) is T9's job; this is the minimum that must work for T7 to be
    //! trustworthy at all.

    use std::path::Path;
    use std::sync::Mutex as StdMutex;
    use std::time::Duration;

    use agent_client_protocol::schema::v1::SessionConfigKind;
    use agent_client_protocol::schema::v1::{
        CloseSessionRequest as AcpCloseSessionRequest, ContentBlock as AcpContentBlock,
        EnvVariable, InitializeRequest as AcpInitializeRequest,
        ListSessionsRequest as AcpListSessionsRequest, LoadSessionRequest as AcpLoadSessionRequest,
        McpServer, McpServerStdio, NewSessionRequest as AcpNewSessionRequest, PermissionOptionKind,
        PromptRequest as AcpPromptRequest, RequestPermissionOutcome, RequestPermissionRequest,
        RequestPermissionResponse, ResumeSessionRequest as AcpResumeSessionRequest,
        SelectedPermissionOutcome, SessionNotification, SessionUpdate, StopReason, ToolCallStatus,
    };
    use agent_client_protocol::{Channel, Client as ClientRole, Responder};
    use arabica_model::{
        FinishReason, MessageItem, RuntimeItem, RuntimeResponse, RuntimeRole, RuntimeUsage,
        ToolCallItem,
    };
    use arabica_provider::{ModelProvider, ModelRunResult};

    use super::*;
    use crate::host::ScriptedModel;

    fn text_result(text: &str) -> ModelRunResult {
        ModelRunResult {
            final_output: Some(text.to_owned()),
            prepared_request: None,
            response: Some(RuntimeResponse {
                items: vec![RuntimeItem::Message(MessageItem::text(
                    RuntimeRole::Assistant,
                    text,
                ))],
                finish_reason: Some(FinishReason::Stop),
                usage: RuntimeUsage::default(),
                provider_state: None,
            }),
        }
    }

    fn tool_call_result(name: &str, arguments: serde_json::Value) -> ModelRunResult {
        ModelRunResult {
            final_output: None,
            prepared_request: None,
            response: Some(RuntimeResponse {
                items: vec![RuntimeItem::ToolCall(ToolCallItem {
                    id: None,
                    call_id: "call-1".to_owned(),
                    name: name.to_owned(),
                    arguments,
                    provider_state: None,
                })],
                finish_reason: Some(FinishReason::ToolCalls),
                usage: RuntimeUsage::default(),
                provider_state: None,
            }),
        }
    }

    fn temp_root(label: &str) -> PathBuf {
        let unique = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock is valid")
            .as_nanos();
        let root = std::env::temp_dir().join(format!("structure-acp-round-trip-{label}-{unique}"));
        std::fs::create_dir_all(&root).expect("test root is created");
        root
    }

    fn scripted_state(
        results: Vec<ModelRunResult>,
        tool_policy: LocalRunnerPolicy,
        arabica_home: &Path,
    ) -> AcpState {
        let results = StdMutex::new(Some(results));
        let model_factory: Arc<ModelFactory> = Arc::new(move || {
            Ok(HostModel::Scripted(ScriptedModel::new(
                results
                    .lock()
                    .expect("results lock poisoned")
                    .take()
                    .unwrap_or_default(),
            )))
        });
        AcpState::new(model_factory, tool_policy, arabica_home.to_path_buf())
    }

    /// The exact policy `main.rs` builds for `arabica acp` (`coding()`
    /// plus `shell`, opted in at the policy layer and gated by the
    /// permission bridge): tests that exercise the shell tool use this so a
    /// policy drift between the real binary and its own tests fails loudly.
    fn acp_tool_policy() -> LocalRunnerPolicy {
        LocalRunnerPolicy::coding().with_tool(arabica_runner::LocalTool::Shell)
    }

    /// Runs the agent side on a background task and returns its `JoinHandle`
    /// alongside the client-role `ConnectionTo` half of an in-memory
    /// channel pair. Callers drive the client through `client`.
    fn spawn_agent(state: AcpState) -> (tokio::task::JoinHandle<AcpResult<()>>, Channel) {
        let (agent_channel, client_channel) = Channel::duplex();
        (tokio::spawn(serve(state, agent_channel)), client_channel)
    }

    #[tokio::test]
    async fn streaming_chat_updates_reach_acp_once() {
        use axum::Json;
        use axum::routing::post;
        use axum::{Router, serve};
        async fn completion(
            Json(body): Json<serde_json::Value>,
        ) -> ([(axum::http::HeaderName, &'static str); 1], String) {
            assert_eq!(body["stream"], true);
            assert_eq!(body["thinking"]["type"], "enabled");
            let chunks = [
                serde_json::json!({"choices":[{"delta":{"reasoning_content":"reason "}}]}),
                serde_json::json!({"choices":[{"delta":{"reasoning_content":"here","content":"hello "}}]}),
                serde_json::json!({"choices":[{"delta":{"content":"there"},"finish_reason":"stop"}]}),
            ];
            let body = chunks
                .iter()
                .map(|chunk| format!("data: {chunk}\n\n"))
                .collect::<String>()
                + "data: [DONE]\n\n";
            (
                [(axum::http::header::CONTENT_TYPE, "text/event-stream")],
                body,
            )
        }
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        tokio::spawn(async move {
            serve(
                listener,
                Router::new().route("/v1/chat/completions", post(completion)),
            )
            .await
            .unwrap();
        });
        let config = ApiProviderConfig::new(
            ApiType::OpenAiChatCompletions,
            "test-key",
            format!("http://{address}/v1"),
            "mock-model",
        );
        let initial = config.clone();
        let factory: Arc<ModelFactory> = Arc::new(move || {
            ApiModelProvider::new(config.clone())
                .map(HostModel::Api)
                .map_err(provider_error)
        });
        let root = temp_root("streaming");
        let home = temp_root("streaming-home");
        let state = AcpState::new(factory, LocalRunnerPolicy::coding(), home.clone())
            .with_provider_settings(AcpProviderSettings {
                overrides: Default::default(),
                initial,
                models: vec!["mock-model".to_owned()],
                aliases: BTreeMap::new(),
                model_configs: BTreeMap::new(),
                blend_policy: None,
                blend_policies: BTreeMap::new(),
            });
        let (server, channel) = spawn_agent(state);
        let updates = Arc::new(StdMutex::new(Vec::<SessionUpdate>::new()));
        let received = Arc::clone(&updates);
        let outcome = ClientRole
            .builder()
            .name("stream-client")
            .on_receive_notification(
                async move |notification: SessionNotification, _connection| {
                    received.lock().unwrap().push(notification.update);
                    Ok(())
                },
                agent_client_protocol::on_receive_notification!(),
            )
            .connect_with(channel, async move |cx| {
                cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                    .block_task()
                    .await?;
                let session = cx
                    .send_request(AcpNewSessionRequest::new(root.clone()))
                    .block_task()
                    .await?;
                assert_eq!(session.config_options.as_ref().map(Vec::len), Some(3));
                let changed = cx
                    .send_request(SetSessionConfigOptionRequest::new(
                        session.session_id.clone(),
                        "thinking",
                        "on",
                    ))
                    .block_task()
                    .await?;
                assert_eq!(changed.config_options.len(), 3);
                let result = cx
                    .send_request(AcpPromptRequest::new(
                        session.session_id,
                        vec![AcpContentBlock::from("hello")],
                    ))
                    .block_task()
                    .await?;
                Ok(result.stop_reason)
            })
            .await
            .expect("ACP stream succeeds");
        assert_eq!(outcome, StopReason::EndTurn);
        let updates = updates.lock().unwrap();
        let mut message = String::new();
        let mut thought = String::new();
        for update in updates.iter() {
            let target = match update {
                SessionUpdate::AgentMessageChunk(_) => &mut message,
                SessionUpdate::AgentThoughtChunk(_) => &mut thought,
                _ => continue,
            };
            let chunk = match update {
                SessionUpdate::AgentMessageChunk(chunk)
                | SessionUpdate::AgentThoughtChunk(chunk) => chunk,
                _ => unreachable!(),
            };
            if let agent_client_protocol::schema::v1::ContentBlock::Text(text) = &chunk.content {
                target.push_str(&text.text);
            }
        }
        assert_eq!(message, "hello there");
        assert_eq!(thought, "reason here");
        drop(updates);
        server.abort();
        std::fs::remove_dir_all(home).ok();
    }

    #[tokio::test]
    async fn acp_config_switches_model_and_thinking_for_the_next_turn() {
        let root = temp_root("config");
        let arabica_home = temp_root("config-home");
        let initial = ApiProviderConfig::new(
            ApiType::OpenAiChatCompletions,
            "test-key",
            "http://127.0.0.1:9/v1",
            "first-model",
        );
        let factory_config = initial.clone();
        let model_factory: Arc<ModelFactory> = Arc::new(move || {
            ApiModelProvider::new(factory_config.clone())
                .map(HostModel::Api)
                .map_err(provider_error)
        });
        let state = AcpState::new(
            model_factory,
            LocalRunnerPolicy::coding(),
            arabica_home.clone(),
        )
        .with_provider_settings(AcpProviderSettings {
            overrides: Default::default(),
            initial,
            models: vec!["first-model".to_owned(), "second-model".to_owned()],
            aliases: BTreeMap::new(),
            model_configs: BTreeMap::new(),
            blend_policy: None,
            blend_policies: BTreeMap::new(),
        });
        let session = state
            .new_session(NewSessionRequest::new(root.clone()), None)
            .await
            .unwrap();
        assert_eq!(session.config_options.as_ref().unwrap().len(), 3);
        let response = state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "model",
                "second-model",
            ))
            .unwrap();
        assert_eq!(response.config_options.len(), 3);
        state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "thinking",
                "on",
            ))
            .unwrap();
        let entry = state.entry(&session.session_id).unwrap();
        let manager = entry.manager.lock().await;
        let HostModel::Api(ApiModelProvider::OpenAiChatCompletions(provider)) =
            manager.runtime().model()
        else {
            panic!("expected Chat Completions provider");
        };
        assert_eq!(provider.config().model, "second-model");
        assert!(provider.config().thinking_enabled);
        drop(manager);
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn acp_soe_model_thinking_survive_named_policy_visits_and_resume() {
        let root = temp_root("blend-config");
        let arabica_home = temp_root("blend-config-home");
        let initial = ApiProviderConfig::new(
            ApiType::OpenAiResponses,
            "test-key",
            "http://127.0.0.1:9/v1",
            "model-fast",
        );
        let aliases = BTreeMap::from([
            ("fast".to_owned(), "model-fast".to_owned()),
            ("strong".to_owned(), "model-strong".to_owned()),
        ]);
        let model_configs = aliases
            .iter()
            .map(|(alias, model_id)| {
                let mut config = initial.clone();
                config.model = model_id.clone();
                (alias.clone(), config)
            })
            .collect::<BTreeMap<_, _>>();
        let policy = BlendRoutingPolicy {
            policy_id: "acp-test".to_owned(),
            version: 1,
            default_model: "fast".to_owned(),
            after_tool_success: Some("fast".to_owned()),
            after_tool_error: Some("strong".to_owned()),
            recovery_model: Some("strong".to_owned()),
            planning_model: None,
            tool_routes: Vec::new(),
            recovery_after_no_progress_steps: 2,
            minimum_model_dwell_steps: 1,
            tool_call_capable_models: Default::default(),
            typed_completion_capable_models: Default::default(),
        };
        let factory_config = initial.clone();
        let factory_aliases = aliases.clone();
        let factory_policy = policy.clone();
        let model_factory: Arc<ModelFactory> = Arc::new(move || {
            BlendProvider::from_shared_config(
                factory_config.clone(),
                &factory_policy.default_model,
                factory_aliases.clone(),
            )
            .map(HostModel::Blend)
            .map_err(provider_error)
        });
        let alternate_policy = BlendRoutingPolicy {
            policy_id: "simple".to_owned(),
            version: 1,
            default_model: "strong".to_owned(),
            after_tool_success: None,
            after_tool_error: None,
            recovery_model: None,
            planning_model: None,
            tool_routes: Vec::new(),
            recovery_after_no_progress_steps: 2,
            minimum_model_dwell_steps: 1,
            tool_call_capable_models: Default::default(),
            typed_completion_capable_models: Default::default(),
        };
        let blend_policies = BTreeMap::from([
            (policy.policy_id.clone(), policy.clone()),
            (alternate_policy.policy_id.clone(), alternate_policy),
        ]);
        let state = AcpState::new(
            model_factory,
            LocalRunnerPolicy::coding(),
            arabica_home.clone(),
        )
        .with_provider_settings(AcpProviderSettings {
            overrides: Default::default(),
            initial,
            models: aliases.keys().cloned().collect(),
            aliases,
            model_configs,
            blend_policy: Some(policy),
            blend_policies,
        });
        let session = state
            .new_session(NewSessionRequest::new(root.clone()), None)
            .await
            .unwrap();
        let options = session.config_options.as_ref().unwrap();
        assert_eq!(options.len(), 1);
        assert_eq!(options[0].id.to_string(), "blend.policy");
        let SessionConfigKind::Select(policy_select) = &options[0].kind else {
            panic!("policy option should be a select");
        };
        assert_eq!(policy_select.current_value.to_string(), "acp-test");
        for (option, value) in [("model", "strong"), ("thinking", "max")] {
            assert!(
                state
                    .set_config_option(SetSessionConfigOptionRequest::new(
                        session.session_id.clone(),
                        option,
                        value,
                    ))
                    .is_err()
            );
        }
        assert!(
            crate::config::read_workspace_settings(&arabica_home, &root)
                .unwrap()
                .blend_policy
                .is_none()
        );
        state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "blend.policy",
                SOE_POLICY_ID,
            ))
            .unwrap();
        let response = state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "model",
                "strong",
            ))
            .unwrap();
        let updated_option = response
            .config_options
            .iter()
            .find(|option| option.id.to_string() == "model")
            .unwrap();
        let SessionConfigKind::Select(updated_select) = &updated_option.kind else {
            panic!("model option should be a select");
        };
        assert_eq!(updated_select.current_value.to_string(), "strong");
        state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "thinking",
                "max",
            ))
            .unwrap();
        state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "model",
                "fast",
            ))
            .unwrap();
        state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "thinking",
                "low",
            ))
            .unwrap();
        let entry = state.entry(&session.session_id).unwrap();
        let policy_response = state
            .set_config_option(SetSessionConfigOptionRequest::new(
                session.session_id.clone(),
                "blend.policy",
                "simple",
            ))
            .unwrap();
        let updated_policy_option = policy_response
            .config_options
            .iter()
            .find(|option| option.id.to_string() == "blend.policy")
            .unwrap();
        let SessionConfigKind::Select(updated_policy_select) = &updated_policy_option.kind else {
            panic!("policy option should be a select");
        };
        assert_eq!(updated_policy_select.current_value.to_string(), "simple");
        {
            let policy = entry.blend_policy.as_ref().unwrap().lock().unwrap();
            assert_eq!(policy.policy_id, "simple");
            assert_eq!(policy.default_model, "strong");
            assert_eq!(policy.after_tool_error, None);
        }
        let manager = entry.manager.lock().await;
        assert_eq!(manager.runtime().model().model_id(), Some("model-strong"));
        assert!(manager.runtime().model().supports_model_alias("fast"));
        assert_eq!(
            entry
                .blend_policy
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .default_model,
            "strong"
        );
        drop(manager);
        let saved = crate::config::read_workspace_settings(&arabica_home, &root).unwrap();
        assert_eq!(saved.blend_policy.as_deref(), Some("simple"));
        assert_eq!(saved.model_alias.as_deref(), Some("fast"));
        assert_eq!(saved.model_thinking["strong"], "max");
        assert_eq!(saved.model_thinking["fast"], "low");
        let restored = state
            .new_session(NewSessionRequest::new(root.clone()), None)
            .await
            .unwrap();
        let restored_entry = state.entry(&restored.session_id).unwrap();
        assert_eq!(
            restored_entry
                .blend_policy
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .policy_id,
            "simple"
        );
        assert_eq!(
            restored_entry
                .provider_config
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .reasoning_effort
                .as_deref(),
            None
        );
        let other_root = temp_root("blend-other-workspace");
        let isolated = state
            .new_session(NewSessionRequest::new(other_root.clone()), None)
            .await
            .unwrap();
        let isolated_entry = state.entry(&isolated.session_id).unwrap();
        assert_eq!(
            isolated_entry
                .blend_policy
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .default_model,
            "fast"
        );
        assert!(
            !isolated_entry
                .provider_config
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .thinking_enabled
        );
        let active = restored_entry.manager.lock().await;
        assert!(
            state
                .set_config_option(SetSessionConfigOptionRequest::new(
                    restored.session_id.clone(),
                    "blend.policy",
                    SOE_POLICY_ID,
                ))
                .is_err()
        );
        drop(active);
        state
            .set_config_option(SetSessionConfigOptionRequest::new(
                restored.session_id.clone(),
                "blend.policy",
                SOE_POLICY_ID,
            ))
            .unwrap();
        assert_eq!(
            restored_entry
                .blend_policy
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .default_model,
            "fast"
        );
        assert_eq!(
            crate::config::thinking_level(
                &restored_entry
                    .provider_config
                    .as_ref()
                    .unwrap()
                    .lock()
                    .unwrap()
            ),
            "low"
        );
        state
            .set_config_option(SetSessionConfigOptionRequest::new(
                restored.session_id.clone(),
                "model",
                "strong",
            ))
            .unwrap();
        assert_eq!(
            crate::config::thinking_level(
                &restored_entry
                    .provider_config
                    .as_ref()
                    .unwrap()
                    .lock()
                    .unwrap()
            ),
            "max"
        );
        // Release the test's retained writer before reopening the same session.
        drop(restored_entry);
        state
            .close_session(CloseSessionRequest::new(restored.session_id.clone()))
            .await
            .unwrap();
        let resumed = state
            .resume_session(
                ResumeSessionRequest::new(restored.session_id.clone(), root.clone()),
                None,
            )
            .await
            .unwrap();
        assert_eq!(resumed.config_options.as_ref().unwrap().len(), 3);
        let resumed_entry = state.entry(&restored.session_id).unwrap();
        assert_eq!(
            resumed_entry
                .blend_policy
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .policy_id,
            SOE_POLICY_ID
        );
        assert_eq!(
            resumed_entry
                .blend_policy
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .default_model,
            "strong"
        );
        assert_eq!(
            crate::config::thinking_level(
                &resumed_entry
                    .provider_config
                    .as_ref()
                    .unwrap()
                    .lock()
                    .unwrap()
            ),
            "max"
        );
        std::fs::remove_dir_all(other_root).ok();
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn client_supplied_stdio_mcp_tool_is_discovered_approved_and_executed() {
        let fixture =
            PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mcp_server.rs");
        let root = temp_root("mcp-tool");
        let server_binary = root.join(format!("mcp-server{}", std::env::consts::EXE_SUFFIX));
        let compile = std::process::Command::new("rustc")
            .args(["--edition=2024", "-o"])
            .arg(&server_binary)
            .arg(&fixture)
            .output()
            .expect("rustc is available to compile the MCP fixture");
        assert!(
            compile.status.success(),
            "MCP fixture compilation failed: {}",
            String::from_utf8_lossy(&compile.stderr)
        );
        let marker = root.join("mcp-result.txt");
        let arabica_home = temp_root("mcp-tool-home");
        let state = scripted_state(
            vec![
                tool_call_result("mcp__fixture__echo", serde_json::json!({"value": "worked"})),
                text_result("done"),
            ],
            acp_tool_policy(),
            &arabica_home,
        );
        let (server, client_channel) = spawn_agent(state);
        let asked = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mcp_server = McpServer::Stdio(McpServerStdio::new("fixture", server_binary).env(vec![
            EnvVariable::new("MCP_TEST_MARKER", marker.display().to_string()),
        ]));
        let outcome = tokio::time::timeout(Duration::from_secs(20), {
            let asked = asked.clone();
            let root = root.clone();
            ClientRole
                .builder()
                .name("test-client")
                .on_receive_request(
                    async move |request: RequestPermissionRequest,
                                responder: Responder<RequestPermissionResponse>,
                                _connection| {
                        asked.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                        let allow_once = request
                            .options
                            .iter()
                            .find(|option| option.kind == PermissionOptionKind::AllowOnce)
                            .expect("allow once is offered")
                            .option_id
                            .clone();
                        responder.respond(RequestPermissionResponse::new(
                            RequestPermissionOutcome::Selected(SelectedPermissionOutcome::new(
                                allow_once,
                            )),
                        ))
                    },
                    agent_client_protocol::on_receive_request!(),
                )
                .connect_with(client_channel, async move |cx| {
                    let initialize = cx
                        .send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    assert!(initialize.agent_capabilities.mcp_capabilities.http);
                    let session = cx
                        .send_request(AcpNewSessionRequest::new(root).mcp_servers(vec![mcp_server]))
                        .block_task()
                        .await?;
                    let prompt = cx
                        .send_request(AcpPromptRequest::new(
                            session.session_id,
                            vec![AcpContentBlock::from("echo worked")],
                        ))
                        .block_task()
                        .await?;
                    Ok(prompt.stop_reason)
                })
        })
        .await
        .expect("MCP round trip timed out")
        .expect("MCP round trip succeeded");
        assert_eq!(outcome, StopReason::EndTurn);
        assert_eq!(asked.load(std::sync::atomic::Ordering::SeqCst), 1);
        assert_eq!(
            std::fs::read_to_string(&marker).expect("MCP tool ran"),
            "worked"
        );
        server.abort();
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    fn named_tool_call(call_id: &str, name: &str, arguments: serde_json::Value) -> ModelRunResult {
        let mut result = tool_call_result(name, arguments);
        if let Some(RuntimeResponse { items, .. }) = &mut result.response
            && let Some(RuntimeItem::ToolCall(call)) = items.first_mut()
        {
            call.call_id = call_id.to_owned();
        }
        result
    }

    #[tokio::test]
    async fn broker_credentials_rotate_within_one_acp_session_and_stay_per_binding() {
        use crate::mcp::auth::{BrokerRequest, McpAuth, ProvidedCredential};
        use crate::mcp::auth_tests::{Fixture, Lifecycle};
        use agent_client_protocol::schema::v1::McpServerHttp;

        let fixture = Fixture::start(Lifecycle::Modern, true, &["a-1", "b-1"]).await;
        let root = temp_root("mcp-broker");
        let arabica_home = temp_root("mcp-broker-home");
        let state = scripted_state(
            vec![
                named_tool_call("call-a", "mcp__a__echo", serde_json::json!({"value": "a"})),
                named_tool_call("call-b", "mcp__b__echo", serde_json::json!({"value": "b"})),
                text_result("first turn"),
                named_tool_call(
                    "call-a2",
                    "mcp__a__echo",
                    serde_json::json!({"value": "a-rotated"}),
                ),
                text_result("second turn"),
            ],
            acp_tool_policy(),
            &arabica_home,
        );
        let (server, client_channel) = spawn_agent(state);
        // The platform's view of each account's current token.
        let current = Arc::new(StdMutex::new(HashMap::from([
            ("account-a".to_owned(), "a-1".to_owned()),
            ("account-b".to_owned(), "b-1".to_owned()),
        ])));
        let asked = Arc::new(StdMutex::new(Vec::<(String, String)>::new()));
        let declare = |name: &str, binding: &str| {
            McpServer::Http(
                McpServerHttp::new(name, fixture.url.clone()).meta(
                    McpAuth::Broker {
                        binding: binding.to_owned(),
                    }
                    .to_meta(),
                ),
            )
        };
        let servers = vec![declare("a", "account-a"), declare("b", "account-b")];
        let result = tokio::time::timeout(Duration::from_secs(20), {
            let current = current.clone();
            let asked = asked.clone();
            let root = root.clone();
            let fixture = &fixture;
            ClientRole
                .builder()
                .name("test-client")
                .on_receive_request(
                    {
                        let current = current.clone();
                        async move |request: BrokerRequest,
                                    responder: Responder<ProvidedCredential>,
                                    _connection| {
                            asked.lock().unwrap().push((
                                request.binding.clone(),
                                serde_json::to_value(request.reason)
                                    .unwrap()
                                    .as_str()
                                    .unwrap()
                                    .to_owned(),
                            ));
                            let token = current.lock().unwrap().get(&request.binding).cloned();
                            responder.respond(match token {
                                Some(access_token) => ProvidedCredential::Granted {
                                    access_token,
                                    expires_in_seconds: Some(3600),
                                },
                                None => ProvidedCredential::NeedsAuthorization { message: None },
                            })
                        }
                    },
                    agent_client_protocol::on_receive_request!(),
                )
                .on_receive_request(
                    async move |request: RequestPermissionRequest,
                                responder: Responder<RequestPermissionResponse>,
                                _connection| {
                        let allow_once = request
                            .options
                            .iter()
                            .find(|option| option.kind == PermissionOptionKind::AllowOnce)
                            .expect("allow once is offered")
                            .option_id
                            .clone();
                        responder.respond(RequestPermissionResponse::new(
                            RequestPermissionOutcome::Selected(SelectedPermissionOutcome::new(
                                allow_once,
                            )),
                        ))
                    },
                    agent_client_protocol::on_receive_request!(),
                )
                .connect_with(client_channel, async move |cx| {
                    let initialize = cx
                        .send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    let advertised = &initialize.meta.as_ref().unwrap()["arabica.mcpAuth"];
                    assert_eq!(advertised["credential"], "_arabica/mcp/credential");
                    assert_eq!(advertised["metaKey"], "arabica.auth");
                    let session = cx
                        .send_request(AcpNewSessionRequest::new(root).mcp_servers(servers))
                        .block_task()
                        .await?;
                    let first = cx
                        .send_request(AcpPromptRequest::new(
                            session.session_id.clone(),
                            vec![AcpContentBlock::from("use both accounts")],
                        ))
                        .block_task()
                        .await?;
                    // The platform rotates account A; the agent session and
                    // its MCP connections stay as they are.
                    fixture.rotate("a-1", "a-2");
                    current
                        .lock()
                        .unwrap()
                        .insert("account-a".to_owned(), "a-2".to_owned());
                    let second = cx
                        .send_request(AcpPromptRequest::new(
                            session.session_id,
                            vec![AcpContentBlock::from("use account A again")],
                        ))
                        .block_task()
                        .await?;
                    Ok((first.stop_reason, second.stop_reason))
                })
        })
        .await
        .expect("broker round trip timed out")
        .expect("broker round trip succeeded");
        assert_eq!(result, (StopReason::EndTurn, StopReason::EndTurn));
        assert_eq!(
            *asked.lock().unwrap(),
            [
                ("account-a".to_owned(), "initial".to_owned()),
                ("account-b".to_owned(), "initial".to_owned()),
                ("account-a".to_owned(), "rejected".to_owned()),
            ]
        );
        let calls = fixture
            .observed()
            .into_iter()
            .filter(|request| request.rpc_method == "tools/call")
            .map(|request| request.authorization.unwrap())
            .collect::<Vec<_>>();
        assert_eq!(
            calls,
            ["Bearer a-1", "Bearer b-1", "Bearer a-1", "Bearer a-2"]
        );
        server.abort();
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn evaluation_extensions_work_while_session_manager_is_locked() {
        let root = temp_root("evaluation-client");
        let home = temp_root("evaluation-client-home");
        let state = scripted_state(
            vec![text_result("unused")],
            LocalRunnerPolicy::coding(),
            &home,
        );
        let (server, channel) = spawn_agent(state.clone());
        let outcome = tokio::time::timeout(
            Duration::from_secs(5),
            ClientRole
                .builder()
                .name("evaluation-client")
                .connect_with(channel, async move |cx| {
                    let initialized = cx
                        .send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    assert_eq!(
                        initialized.meta.as_ref().unwrap()["arabica.evaluation"]["asynchronous"],
                        true
                    );
                    let session = cx
                        .send_request(AcpNewSessionRequest::new(root))
                        .block_task()
                        .await?;
                    let entry = state.entry(&session.session_id).unwrap();
                    let _guard = entry.manager.lock().await;
                    let view = cx
                        .send_request(evaluation::GetEvaluationRequest {
                            session_id: session.session_id.clone(),
                        })
                        .block_task()
                        .await?;
                    assert_eq!(view["schema_version"], 1);
                    assert!(view["report"].is_null());
                    let refreshed = cx
                        .send_request(evaluation::RefreshEvaluationRequest {
                            session_id: session.session_id,
                        })
                        .block_task()
                        .await?;
                    assert_eq!(refreshed["refresh_accepted"], true);
                    assert!(
                        cx.send_request(evaluation::GetEvaluationRequest {
                            session_id: AcpSessionId::new("unknown")
                        })
                        .block_task()
                        .await
                        .is_err()
                    );
                    Ok(())
                }),
        )
        .await
        .expect("evaluation query blocked on the running session");
        outcome.expect("evaluation extension round trip");
        server.abort();
    }

    #[tokio::test]
    async fn rename_extension_persists_loaded_and_historical_sessions_without_model_calls() {
        let root = temp_root("rename-client");
        let home = temp_root("rename-client-home");
        let state = scripted_state(vec![], LocalRunnerPolicy::coding(), &home);
        let retained = state.clone();
        let (server, channel) = spawn_agent(state);
        let session_id = tokio::time::timeout(
            Duration::from_secs(5),
            ClientRole
                .builder()
                .name("rename-client")
                .connect_with(channel, {
                    let root = root.clone();
                    async move |cx| {
                        let initialized = cx
                            .send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                            .block_task()
                            .await?;
                        assert_eq!(
                            initialized.meta.as_ref().unwrap()["arabica.session"],
                            serde_json::json!({
                                "schemaVersion":1,
                                "rename":"_arabica/session/rename",
                                "maxTitleLength":200,
                            })
                        );
                        let session = cx
                            .send_request(AcpNewSessionRequest::new(root.clone()))
                            .block_task()
                            .await?;
                        let other = cx
                            .send_request(AcpNewSessionRequest::new(root))
                            .block_task()
                            .await?;
                        let entry = retained.entry(&session.session_id).unwrap();
                        let workspace_id = crate::host::workspace_id_for(&entry.cwd);
                        let before = retained
                            .session_store
                            .read(&workspace_id, &entry.arabica_session_id)
                            .unwrap();
                        let guard = entry.manager.lock().await;
                        let response = cx
                            .send_request(session_metadata::RenameSessionRequest {
                                session_id: session.session_id.clone(),
                                title: "  A useful title  ".to_owned(),
                            })
                            .block_task()
                            .await?;
                        assert_eq!(response.session_id, session.session_id);
                        assert_eq!(response.title, "A useful title");
                        let after = retained
                            .session_store
                            .read(&workspace_id, &entry.arabica_session_id)
                            .unwrap();
                        assert_eq!(after.events, before.events);
                        let mut expected = before.header;
                        expected.title = Some(response.title);
                        assert_eq!(
                            serde_json::to_value(after.header).unwrap(),
                            serde_json::to_value(expected).unwrap()
                        );
                        let listings = cx
                            .send_request(AcpListSessionsRequest::new())
                            .block_task()
                            .await?;
                        assert_eq!(
                            listings
                                .sessions
                                .iter()
                                .find(|item| item.session_id == other.session_id)
                                .unwrap()
                                .title,
                            None
                        );
                        for (session_id, title) in [
                            (session.session_id.clone(), "   "),
                            (AcpSessionId::new("../invalid"), "Title"),
                            (AcpSessionId::new("unknown"), "Title"),
                        ] {
                            assert!(
                                cx.send_request(session_metadata::RenameSessionRequest {
                                    session_id,
                                    title: title.to_owned(),
                                })
                                .block_task()
                                .await
                                .is_err()
                            );
                        }
                        drop(guard);
                        drop(entry);
                        cx.send_request(AcpCloseSessionRequest::new(session.session_id.clone()))
                            .block_task()
                            .await?;
                        Ok(session.session_id)
                    }
                }),
        )
        .await
        .expect("rename must not wait for the session manager")
        .expect("rename round trip succeeds");
        server.await.unwrap().unwrap();

        let factory: Arc<ModelFactory> = Arc::new(|| panic!("renaming must not construct a model"));
        let state = AcpState::new(factory, LocalRunnerPolicy::coding(), home.clone());
        let retained = state.clone();
        let (server, channel) = spawn_agent(state);
        ClientRole
            .builder()
            .name("historical-rename-client")
            .connect_with(channel, async move |cx| {
                cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                    .block_task()
                    .await?;
                assert!(retained.entry(&session_id).is_none());
                let before = retained.session_store.list(None).unwrap();
                let listing = before
                    .iter()
                    .find(|listing| listing.header.id.to_string() == session_id.to_string())
                    .unwrap();
                let events = retained
                    .session_store
                    .read(&listing.header.workspace_id, &listing.header.id)
                    .unwrap()
                    .events;
                let response = cx
                    .send_request(session_metadata::RenameSessionRequest {
                        session_id: session_id.clone(),
                        title: "Historical title".to_owned(),
                    })
                    .block_task()
                    .await?;
                assert_eq!(response.title, "Historical title");
                assert!(retained.entry(&session_id).is_none());
                let after = retained.session_store.list(None).unwrap();
                let renamed = after
                    .iter()
                    .find(|item| item.header.id == listing.header.id)
                    .unwrap();
                assert_eq!(renamed.header.title.as_deref(), Some("Historical title"));
                assert!(renamed.updated_at_ms >= listing.updated_at_ms);
                assert_eq!(
                    retained
                        .session_store
                        .read(&listing.header.workspace_id, &listing.header.id)
                        .unwrap()
                        .events,
                    events
                );
                Ok(())
            })
            .await
            .expect("historical rename round trip succeeds");
        server.await.unwrap().unwrap();
        let listing = SqliteSessionStore::list_sessions(&home, None)
            .unwrap()
            .into_iter()
            .find(|listing| listing.header.title.as_deref() == Some("Historical title"))
            .unwrap();
        assert_eq!(listing.header.cwd, root.display().to_string());
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(home).ok();
    }

    #[tokio::test]
    async fn rename_respects_other_connections_writer_ownership() {
        let root = temp_root("rename-ownership");
        let home = temp_root("rename-ownership-home");
        let owner = scripted_state(vec![], LocalRunnerPolicy::coding(), &home);
        let session = owner
            .new_session(AcpNewSessionRequest::new(root.clone()), None)
            .await
            .unwrap();
        let other = scripted_state(vec![], LocalRunnerPolicy::coding(), &home);
        let request = session_metadata::RenameSessionRequest {
            session_id: session.session_id.clone(),
            title: "Changed elsewhere".to_owned(),
        };
        assert!(session_metadata::rename(&other, request.clone()).is_err());
        assert_eq!(
            owner.session_store.list(None).unwrap()[0].header.title,
            None
        );
        owner
            .close_session(AcpCloseSessionRequest::new(session.session_id))
            .await
            .unwrap();
        assert!(session_metadata::rename(&other, request).is_ok());
        drop(owner);
        drop(other);
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(home).ok();
    }

    #[test]
    fn rename_rejects_ambiguous_historical_ids_without_mutating_either_session() {
        let root = temp_root("rename-ambiguous");
        let home = temp_root("rename-ambiguous-home");
        let state = scripted_state(vec![], LocalRunnerPolicy::coding(), &home);
        let session_id = StructureSessionId::new("duplicate-id");
        for id in ["workspace-a", "workspace-b"] {
            state
                .session_store
                .create(NewSession {
                    session_id: &session_id,
                    workspace_id: &arabica_protocol::WorkspaceId::new(id),
                    cwd: &root,
                    profile: None,
                    instructions_sha256: None,
                })
                .unwrap();
        }
        assert!(
            session_metadata::rename(
                &state,
                session_metadata::RenameSessionRequest {
                    session_id: AcpSessionId::new("duplicate-id"),
                    title: "Wrong target".to_owned(),
                },
            )
            .is_err()
        );
        let listings = state.session_store.list(None).unwrap();
        assert_eq!(listings.len(), 2);
        assert!(
            listings
                .iter()
                .all(|listing| listing.header.title.is_none())
        );
        drop(state);
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(home).ok();
    }

    #[tokio::test]
    async fn a_plain_text_prompt_ends_the_turn_and_streams_the_message() {
        let root = temp_root("plain-text");
        let arabica_home = temp_root("plain-text-home");
        let state = scripted_state(
            vec![text_result("hello from structure")],
            LocalRunnerPolicy::coding(),
            &arabica_home,
        );
        let (server, client_channel) = spawn_agent(state);
        let updates: Arc<StdMutex<Vec<SessionUpdate>>> = Arc::new(StdMutex::new(Vec::new()));

        let outcome = tokio::time::timeout(Duration::from_secs(10), {
            let updates = updates.clone();
            let root = root.clone();
            ClientRole
                .builder()
                .name("test-client")
                .on_receive_notification(
                    {
                        let updates = updates.clone();
                        async move |notification: SessionNotification, _connection| {
                            updates
                                .lock()
                                .expect("updates lock poisoned")
                                .push(notification.update);
                            Ok(())
                        }
                    },
                    agent_client_protocol::on_receive_notification!(),
                )
                .connect_with(client_channel, async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    let new_session = cx
                        .send_request(AcpNewSessionRequest::new(root))
                        .block_task()
                        .await?;
                    let prompt = cx
                        .send_request(AcpPromptRequest::new(
                            new_session.session_id,
                            vec![AcpContentBlock::from("say hello")],
                        ))
                        .block_task()
                        .await?;
                    Ok(prompt.stop_reason)
                })
        })
        .await
        .expect("client round trip did not time out")
        .expect("client round trip succeeded");

        assert_eq!(outcome, StopReason::EndTurn);
        assert!(
            updates
                .lock()
                .expect("updates lock poisoned")
                .iter()
                .any(|update| matches!(update, SessionUpdate::AgentMessageChunk(_))),
            "expected a live agent_message_chunk update, got {:?}",
            updates.lock().expect("updates lock poisoned")
        );

        server
            .await
            .expect("server task did not panic")
            .expect("server run completed cleanly");
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn a_prompt_turn_persists_its_session_to_disk() {
        let root = temp_root("persist");
        let arabica_home = temp_root("persist-home");
        let state = scripted_state(
            vec![text_result("hello from structure")],
            LocalRunnerPolicy::coding(),
            &arabica_home,
        );
        let (server, client_channel) = spawn_agent(state);

        let session_id = tokio::time::timeout(Duration::from_secs(10), {
            let root = root.clone();
            ClientRole
                .builder()
                .name("test-client")
                .connect_with(client_channel, async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    let new_session = cx
                        .send_request(AcpNewSessionRequest::new(root))
                        .block_task()
                        .await?;
                    cx.send_request(AcpPromptRequest::new(
                        new_session.session_id.clone(),
                        vec![AcpContentBlock::from("say hello")],
                    ))
                    .block_task()
                    .await?;
                    Ok(new_session.session_id)
                })
        })
        .await
        .expect("client round trip did not time out")
        .expect("client round trip succeeded");

        server
            .await
            .expect("server task did not panic")
            .expect("server run completed cleanly");

        // The ACP session id is exactly the stringified Arabica session
        // id (`AcpState::new_session`), so this round-trips it back rather
        // than re-deriving anything the store itself would not have used.
        let workspace_id = crate::host::workspace_id_for(&root);
        let stored = SqliteSessionStore::read_session(
            &arabica_home,
            &workspace_id,
            &arabica_protocol::SessionId::new(session_id.to_string()),
        )
        .expect("the session persisted by the live observer is readable back");
        assert_eq!(stored.header.id.to_string(), session_id.to_string());
        assert!(
            matches!(
                stored.events.first().map(|envelope| &envelope.event),
                Some(arabica_protocol::Event::SessionCreated { .. })
            ),
            "the first persisted event must be session.created, got {:?}",
            stored.events.first()
        );
        assert!(
            stored.events.len() > 1,
            "the prompt turn must have appended events beyond session.created, got {:?}",
            stored.events
        );

        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    fn chunk_text(update: &SessionUpdate) -> Option<&str> {
        let SessionUpdate::AgentMessageChunk(chunk) = update else {
            return None;
        };
        let AcpContentBlock::Text(text) = &chunk.content else {
            return None;
        };
        Some(&text.text)
    }

    #[tokio::test]
    async fn session_load_replays_history_then_accepts_a_new_prompt() {
        let root = temp_root("load");
        let arabica_home = temp_root("load-home");

        // Connection 1: create a session and send one prompt, then let the
        // connection end -- simulating the editor (or the agent process)
        // disconnecting. Nothing here ever calls session/load.
        let state1 = scripted_state(
            vec![text_result("hello from turn one")],
            LocalRunnerPolicy::coding(),
            &arabica_home,
        );
        let (server1, client_channel1) = spawn_agent(state1);
        let session_id = tokio::time::timeout(Duration::from_secs(10), {
            let root = root.clone();
            ClientRole.builder().name("test-client-1").connect_with(
                client_channel1,
                async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    let new_session = cx
                        .send_request(AcpNewSessionRequest::new(root))
                        .block_task()
                        .await?;
                    cx.send_request(AcpPromptRequest::new(
                        new_session.session_id.clone(),
                        vec![AcpContentBlock::from("say hello")],
                    ))
                    .block_task()
                    .await?;
                    Ok(new_session.session_id)
                },
            )
        })
        .await
        .expect("first client round trip did not time out")
        .expect("first client round trip succeeded");

        server1
            .await
            .expect("server task did not panic")
            .expect("first connection's server run completed cleanly");

        // Connection 2: a brand new AcpState -- its `sessions` map starts
        // empty, with no entry for this session at all, the same as a fresh
        // `arabica acp` process would have -- pointed at the SAME
        // arabica_home. session/load must find the session on disk,
        // replay its history as session/update notifications, then accept a
        // new prompt on the session it just restored.
        let state2 = scripted_state(
            vec![text_result("hello again after loading")],
            LocalRunnerPolicy::coding(),
            &arabica_home,
        );
        let (server2, client_channel2) = spawn_agent(state2);
        let updates: Arc<StdMutex<Vec<SessionUpdate>>> = Arc::new(StdMutex::new(Vec::new()));

        let second_stop_reason = tokio::time::timeout(Duration::from_secs(10), {
            let updates = updates.clone();
            let root = root.clone();
            let session_id = session_id.clone();
            ClientRole
                .builder()
                .name("test-client-2")
                .on_receive_notification(
                    {
                        let updates = updates.clone();
                        async move |notification: SessionNotification, _connection| {
                            updates
                                .lock()
                                .expect("updates lock poisoned")
                                .push(notification.update);
                            Ok(())
                        }
                    },
                    agent_client_protocol::on_receive_notification!(),
                )
                .connect_with(client_channel2, async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    cx.send_request(AcpLoadSessionRequest::new(session_id.clone(), root))
                        .block_task()
                        .await?;
                    {
                        let seen = updates.lock().expect("updates lock poisoned");
                        let messages: Vec<_> = seen
                            .iter()
                            .filter_map(|update| {
                                let (role, chunk) = match update {
                                    SessionUpdate::UserMessageChunk(chunk) => ("user", chunk),
                                    SessionUpdate::AgentMessageChunk(chunk) => ("assistant", chunk),
                                    _ => return None,
                                };
                                let AcpContentBlock::Text(text) = &chunk.content else {
                                    return None;
                                };
                                Some((role, text.text.as_str()))
                            })
                            .collect();
                        assert_eq!(
                            messages,
                            vec![("user", "say hello"), ("assistant", "hello from turn one")],
                            "session/load must replay both sides in order before responding"
                        );
                    }
                    let prompt = cx
                        .send_request(AcpPromptRequest::new(
                            session_id,
                            vec![AcpContentBlock::from("what did you say before?")],
                        ))
                        .block_task()
                        .await?;
                    Ok(prompt.stop_reason)
                })
        })
        .await
        .expect("second client round trip did not time out")
        .expect("second client round trip succeeded");

        assert_eq!(second_stop_reason, StopReason::EndTurn);
        {
            let seen = updates.lock().expect("updates lock poisoned");
            assert!(
                seen.iter()
                    .any(|update| chunk_text(update) == Some("hello from turn one")),
                "session/load must replay the first turn's message as a session/update \
                 before responding, got {seen:?}"
            );
            assert!(
                seen.iter()
                    .any(|update| chunk_text(update) == Some("hello again after loading")),
                "the post-load prompt must also stream live, got {seen:?}"
            );
        }

        server2
            .await
            .expect("server task did not panic")
            .expect("second connection's server run completed cleanly");

        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn session_resume_does_not_replay_history_but_does_accept_a_new_prompt() {
        let root = temp_root("resume");
        let arabica_home = temp_root("resume-home");

        // Connection 1: same as session/load's own test -- create a
        // session, send one prompt, let the connection end.
        let state1 = scripted_state(
            vec![text_result("hello from turn one")],
            LocalRunnerPolicy::coding(),
            &arabica_home,
        );
        let (server1, client_channel1) = spawn_agent(state1);
        let session_id = tokio::time::timeout(Duration::from_secs(10), {
            let root = root.clone();
            ClientRole.builder().name("test-client-1").connect_with(
                client_channel1,
                async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    let new_session = cx
                        .send_request(AcpNewSessionRequest::new(root))
                        .block_task()
                        .await?;
                    cx.send_request(AcpPromptRequest::new(
                        new_session.session_id.clone(),
                        vec![AcpContentBlock::from("say hello")],
                    ))
                    .block_task()
                    .await?;
                    Ok(new_session.session_id)
                },
            )
        })
        .await
        .expect("first client round trip did not time out")
        .expect("first client round trip succeeded");

        server1
            .await
            .expect("server task did not panic")
            .expect("first connection's server run completed cleanly");

        // Connection 2: an independent AcpState (same reasoning as
        // session/load's test -- avoids the first connection's still-locked
        // SqliteSessionStore causing spurious lock contention) resumes the
        // session and sends a new prompt. Unlike session/load, no
        // session/update notifications should arrive before the prompt: the
        // whole point of session/resume is skipping that replay.
        let state2 = scripted_state(
            vec![text_result("hello again after resuming")],
            LocalRunnerPolicy::coding(),
            &arabica_home,
        );
        let (server2, client_channel2) = spawn_agent(state2);
        let updates: Arc<StdMutex<Vec<SessionUpdate>>> = Arc::new(StdMutex::new(Vec::new()));

        let second_stop_reason = tokio::time::timeout(Duration::from_secs(10), {
            let updates = updates.clone();
            let root = root.clone();
            let session_id = session_id.clone();
            ClientRole
                .builder()
                .name("test-client-2")
                .on_receive_notification(
                    {
                        let updates = updates.clone();
                        async move |notification: SessionNotification, _connection| {
                            updates
                                .lock()
                                .expect("updates lock poisoned")
                                .push(notification.update);
                            Ok(())
                        }
                    },
                    agent_client_protocol::on_receive_notification!(),
                )
                .connect_with(client_channel2, async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    cx.send_request(AcpResumeSessionRequest::new(session_id.clone(), root))
                        .block_task()
                        .await?;
                    let prompt = cx
                        .send_request(AcpPromptRequest::new(
                            session_id,
                            vec![AcpContentBlock::from("what did you say before?")],
                        ))
                        .block_task()
                        .await?;
                    Ok(prompt.stop_reason)
                })
        })
        .await
        .expect("second client round trip did not time out")
        .expect("second client round trip succeeded");

        assert_eq!(second_stop_reason, StopReason::EndTurn);
        {
            let seen = updates.lock().expect("updates lock poisoned");
            assert!(
                !seen
                    .iter()
                    .any(|update| chunk_text(update) == Some("hello from turn one")),
                "session/resume must NOT replay the first turn's message, got {seen:?}"
            );
            assert!(
                seen.iter()
                    .any(|update| chunk_text(update) == Some("hello again after resuming")),
                "the post-resume prompt must still stream live, got {seen:?}"
            );
        }

        server2
            .await
            .expect("server task did not panic")
            .expect("second connection's server run completed cleanly");

        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn session_close_suspends_and_forgets_the_session_in_this_connection() {
        let root = temp_root("close");
        let arabica_home = temp_root("close-home");
        let model_factory: Arc<ModelFactory> = Arc::new(|| {
            Ok(HostModel::Scripted(ScriptedModel::new([text_result(
                "hello after reopening",
            )])))
        });
        let state = AcpState::new(
            model_factory,
            LocalRunnerPolicy::coding(),
            arabica_home.clone(),
        );
        let (server, client_channel) = spawn_agent(state);

        let (session_id, prompt_after_close_was_rejected, loaded_stop, resumed_stop) =
            tokio::time::timeout(Duration::from_secs(10), {
                let root = root.clone();
                ClientRole.builder().name("test-client").connect_with(
                    client_channel,
                    async move |cx| {
                        cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                            .block_task()
                            .await?;
                        let new_session = cx
                            .send_request(AcpNewSessionRequest::new(root.clone()))
                            .block_task()
                            .await?;
                        cx.send_request(AcpPromptRequest::new(
                            new_session.session_id.clone(),
                            vec![AcpContentBlock::from("say hello")],
                        ))
                        .block_task()
                        .await?;
                        cx.send_request(AcpCloseSessionRequest::new(
                            new_session.session_id.clone(),
                        ))
                        .block_task()
                        .await?;
                        // The session's own state (now Suspended) rejects a
                        // further message on its own -- state validation is
                        // a second, independent layer that would catch this
                        // even if close_session's own map bookkeeping had a
                        // bug, so this alone cannot prove that bookkeeping
                        // is correct.
                        let prompt_rejected = cx
                            .send_request(AcpPromptRequest::new(
                                new_session.session_id.clone(),
                                vec![AcpContentBlock::from("are you still there?")],
                            ))
                            .block_task()
                            .await
                            .is_err();
                        // This probe is the one that actually depends on
                        // close_session removing its own SessionEntry: a
                        // stale entry would keep its Arc<dyn SessionWriter>
                        // (and the exclusive flock that comes with it) alive
                        // forever, so session/load's own
                        // SqliteSessionStore::open_existing for the SAME
                        // session id would fail on lock contention with that
                        // never-released handle. Success here means no
                        // stale entry survived close.
                        cx.send_request(AcpLoadSessionRequest::new(
                            new_session.session_id.clone(),
                            root.clone(),
                        ))
                        .block_task()
                        .await?;
                        let loaded_stop = cx
                            .send_request(AcpPromptRequest::new(
                                new_session.session_id.clone(),
                                vec![AcpContentBlock::from("after load")],
                            ))
                            .block_task()
                            .await?
                            .stop_reason;
                        cx.send_request(AcpCloseSessionRequest::new(
                            new_session.session_id.clone(),
                        ))
                        .block_task()
                        .await?;
                        cx.send_request(AcpResumeSessionRequest::new(
                            new_session.session_id.clone(),
                            root,
                        ))
                        .block_task()
                        .await?;
                        let resumed_stop = cx
                            .send_request(AcpPromptRequest::new(
                                new_session.session_id.clone(),
                                vec![AcpContentBlock::from("after resume")],
                            ))
                            .block_task()
                            .await?
                            .stop_reason;
                        Ok((
                            new_session.session_id,
                            prompt_rejected,
                            loaded_stop,
                            resumed_stop,
                        ))
                    },
                )
            })
            .await
            .expect("client round trip did not time out")
            .expect("client round trip succeeded");

        assert!(
            prompt_after_close_was_rejected,
            "a session/prompt for a session already closed in this connection must be rejected"
        );
        assert_eq!(loaded_stop, StopReason::EndTurn);
        assert_eq!(resumed_stop, StopReason::EndTurn);

        server
            .await
            .expect("server task did not panic")
            .expect("server run completed cleanly");

        let workspace_id = crate::host::workspace_id_for(&root);
        let stored = SqliteSessionStore::read_session(
            &arabica_home,
            &workspace_id,
            &arabica_protocol::SessionId::new(session_id.to_string()),
        )
        .expect("the session file is still readable after close");
        assert!(
            stored.events.iter().any(|envelope| matches!(
                envelope.event,
                arabica_protocol::Event::SessionSuspended
            )),
            "session/close must persist session.suspend, not session.close (a terminal \
             state session/load or session/resume could never reopen), got {:?}",
            stored.events
        );
        assert_eq!(
            stored
                .events
                .iter()
                .filter(|envelope| matches!(
                    envelope.event,
                    arabica_protocol::Event::SessionResumed
                ))
                .count(),
            2,
            "both ACP load and resume must activate a suspended session"
        );

        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn session_list_scopes_by_cwd_and_lists_across_workspaces_without_it() {
        let root_a = temp_root("list-a");
        let root_b = temp_root("list-b");
        let arabica_home = temp_root("list-home");

        // Two independent connections, sharing one arabica_home, each
        // create one session in a different workspace -- session/new never
        // dispatches MessageSend, so an empty scripted-results queue is
        // enough; no prompt is needed to produce a session worth listing.
        for root in [&root_a, &root_b] {
            let state = scripted_state(vec![], LocalRunnerPolicy::coding(), &arabica_home);
            let (server, client_channel) = spawn_agent(state);
            let root = root.clone();
            tokio::time::timeout(
                Duration::from_secs(10),
                ClientRole
                    .builder()
                    .name("test-client-create")
                    .connect_with(client_channel, async move |cx| {
                        cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                            .block_task()
                            .await?;
                        cx.send_request(AcpNewSessionRequest::new(root))
                            .block_task()
                            .await?;
                        Ok(())
                    }),
            )
            .await
            .expect("create round trip did not time out")
            .expect("create round trip succeeded");
            server
                .await
                .expect("server task did not panic")
                .expect("create connection's server run completed cleanly");
        }

        for listing in SqliteSessionStore::list_sessions(&arabica_home, None).unwrap() {
            SqliteSessionStore::open_existing(&listing.path)
                .unwrap()
                .set_title("Persisted title")
                .unwrap();
        }

        // A third, independent connection does the listing.
        let state = scripted_state(vec![], LocalRunnerPolicy::coding(), &arabica_home);
        let (server, client_channel) = spawn_agent(state);
        let (scoped, all) = tokio::time::timeout(Duration::from_secs(10), {
            let root_a = root_a.clone();
            ClientRole.builder().name("test-client-list").connect_with(
                client_channel,
                async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    let scoped = cx
                        .send_request(AcpListSessionsRequest::new().cwd(Some(root_a)))
                        .block_task()
                        .await?;
                    let all = cx
                        .send_request(AcpListSessionsRequest::new())
                        .block_task()
                        .await?;
                    Ok((scoped, all))
                },
            )
        })
        .await
        .expect("list round trip did not time out")
        .expect("list round trip succeeded");

        server
            .await
            .expect("server task did not panic")
            .expect("list connection's server run completed cleanly");

        assert_eq!(
            scoped.sessions.len(),
            1,
            "a cwd-scoped session/list must return exactly the one session in that \
             workspace, got {:?}",
            scoped.sessions
        );
        assert_eq!(scoped.sessions[0].cwd, root_a);
        assert_eq!(scoped.sessions[0].title.as_deref(), Some("Persisted title"));
        assert!(
            scoped.sessions[0].updated_at.is_some(),
            "a session with a real file on disk must report updated_at"
        );
        assert_eq!(
            all.sessions.len(),
            2,
            "an unscoped session/list must return sessions from every workspace, got {:?}",
            all.sessions
        );

        std::fs::remove_dir_all(root_a).ok();
        std::fs::remove_dir_all(root_b).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }

    #[tokio::test]
    async fn a_shell_call_is_gated_approved_executed_and_reported_before_the_turn_ends() {
        let root = temp_root("shell-allow");
        let arabica_home = temp_root("shell-allow-home");
        let state = scripted_state(
            vec![
                tool_call_result("shell", serde_json::json!({"command": "true"})),
                text_result("done"),
            ],
            acp_tool_policy(),
            &arabica_home,
        );
        let (server, client_channel) = spawn_agent(state);
        let updates: Arc<StdMutex<Vec<SessionUpdate>>> = Arc::new(StdMutex::new(Vec::new()));
        let asked = Arc::new(std::sync::atomic::AtomicUsize::new(0));

        let outcome = tokio::time::timeout(Duration::from_secs(10), {
            let updates = updates.clone();
            let asked = asked.clone();
            let root = root.clone();
            ClientRole
                .builder()
                .name("test-client")
                .on_receive_notification(
                    {
                        let updates = updates.clone();
                        async move |notification: SessionNotification, _connection| {
                            updates
                                .lock()
                                .expect("updates lock poisoned")
                                .push(notification.update);
                            Ok(())
                        }
                    },
                    agent_client_protocol::on_receive_notification!(),
                )
                .on_receive_request(
                    async move |request: RequestPermissionRequest,
                                responder: Responder<RequestPermissionResponse>,
                                _connection| {
                        asked.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                        let allow_once = request
                            .options
                            .iter()
                            .find(|option| option.kind == PermissionOptionKind::AllowOnce)
                            .expect("the agent must offer an allow-once option")
                            .option_id
                            .clone();
                        responder.respond(RequestPermissionResponse::new(
                            RequestPermissionOutcome::Selected(SelectedPermissionOutcome::new(
                                allow_once,
                            )),
                        ))
                    },
                    agent_client_protocol::on_receive_request!(),
                )
                .connect_with(client_channel, async move |cx| {
                    cx.send_request(AcpInitializeRequest::new(ProtocolVersion::V1))
                        .block_task()
                        .await?;
                    let new_session = cx
                        .send_request(AcpNewSessionRequest::new(root))
                        .block_task()
                        .await?;
                    let prompt = cx
                        .send_request(AcpPromptRequest::new(
                            new_session.session_id,
                            vec![AcpContentBlock::from("run something")],
                        ))
                        .block_task()
                        .await?;
                    Ok(prompt.stop_reason)
                })
        })
        .await
        .expect("client round trip did not time out")
        .expect("client round trip succeeded");

        assert_eq!(outcome, StopReason::EndTurn);
        assert_eq!(
            asked.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "the agent must actually gate shell through session/request_permission, not just report success"
        );
        {
            // Scoped so the guard is dropped well before `server.await`
            // below: holding a `std::sync::MutexGuard` across an await point
            // is a bug even when nothing else can ever lock it, since it
            // would poison the lock on a panic mid-await.
            let seen = updates.lock().expect("updates lock poisoned");
            assert!(
                seen.iter()
                    .any(|update| matches!(update, SessionUpdate::ToolCall(_))),
                "expected a tool_call announcement, got {seen:?}"
            );
            assert!(
                seen.iter().any(|update| matches!(
                    update,
                    SessionUpdate::ToolCallUpdate(u) if u.fields.status == Some(ToolCallStatus::Completed)
                )),
                "expected the shell call to be reported completed, got {seen:?}"
            );
        }

        server
            .await
            .expect("server task did not panic")
            .expect("server run completed cleanly");
        std::fs::remove_dir_all(root).ok();
        std::fs::remove_dir_all(arabica_home).ok();
    }
}