codex-wrapper 0.4.5

A type-safe Codex CLI wrapper for Rust
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
//! A JSON-RPC client for `codex app-server`.
//!
//! `codex exec` takes one prompt per process and cannot change a turn that is
//! already running. The app-server can: [`AppServerHandle::turn_steer`] adds
//! input to the active turn and [`AppServerHandle::turn_interrupt`] stops it.
//! [`AppServer`] runs the server over stdio and gives a caller its events as
//! they arrive, plus a way to answer the requests the server sends back, such
//! as approvals.
//!
//! The CLI marks `app-server` experimental, so this module is behind the
//! `app-server` feature and wraps the transport, not the whole protocol. The
//! four methods with typed helpers ([`thread_start`], [`turn_start`],
//! [`turn_steer`], [`turn_interrupt`]) were exercised against a real server;
//! [`request`] reaches any other method without a wrapper change.
//!
//! [`thread_start`]: AppServerHandle::thread_start
//! [`turn_start`]: AppServerHandle::turn_start
//! [`turn_steer`]: AppServerHandle::turn_steer
//! [`turn_interrupt`]: AppServerHandle::turn_interrupt
//! [`request`]: AppServerHandle::request
//!
//! # Example
//!
//! ```no_run
//! use codex_wrapper::Codex;
//! use codex_wrapper::app_server::{
//!     AppServer, ServerMessage, ThreadStartParams, TurnStartParams, UserInput,
//! };
//!
//! # async fn example() -> codex_wrapper::Result<()> {
//! let codex = Codex::builder().build()?;
//! let mut server = AppServer::builder(&codex).start().await?;
//!
//! let thread = server
//!     .thread_start(ThreadStartParams::new().ephemeral(true))
//!     .await?;
//! let turn = server
//!     .turn_start(TurnStartParams::text(&thread.id, "run `sleep 10`, then say DONE"))
//!     .await?;
//!
//! // A handle can be cloned into another task, for example one that
//! // serves a "steer" command while this task reads events.
//! let handle = server.handle();
//! handle
//!     .turn_steer(&thread.id, &turn.id, vec![UserInput::text("also say STEERED")])
//!     .await?;
//!
//! let mut answer = None;
//! while let Some(message) = server.next_message().await? {
//!     match message {
//!         ServerMessage::Notification(notification) => {
//!             if let Some(message) = notification.agent_message()
//!                 && message.is_final_answer()
//!             {
//!                 answer = Some(message.text);
//!             }
//!             if let Some(done) = notification.turn_completed() {
//!                 println!("{:?}: {answer:?}", done.turn.status);
//!                 break;
//!             }
//!         }
//!         ServerMessage::Request(request) => {
//!             // An unanswered request stalls the turn.
//!             server.respond_error(request.id, -32601, "not supported")?;
//!         }
//!         _ => {}
//!     }
//! }
//! server.shutdown().await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Lifecycle and cancellation
//!
//! The server runs in its own process group (unless the client disables
//! [`CodexBuilder::process_group`](crate::CodexBuilder::process_group)).
//! That group does not include the shell commands a turn runs: the server
//! starts each in a group of its own. So there are two ways to stop, and they
//! differ in what happens to a command that is still running:
//!
//! - [`AppServer::shutdown`] closes the server's stdin and waits up to the
//!   client's termination grace for it to exit. The server stops its running
//!   commands and exits, in milliseconds when a turn is active. If it does
//!   not exit in time, this falls back to [`AppServer::terminate`]. Prefer it.
//! - [`AppServer::terminate`] has the settled contract of
//!   [`stream_exec_cancellable`](crate::streaming::stream_exec_cancellable):
//!   it asks the server's group to stop, waits through the grace period, kills
//!   what is left, and reaps the child before returning. A command the server
//!   started in its own group keeps running, as it does when `codex exec` is
//!   cancelled; the caller has to find and stop it.
//! - Dropping an [`AppServer`] kills the server's group without waiting, as
//!   dropping a streaming future does. The same caveat applies.
//!
//! [`AppServerHandle::turn_interrupt`] ends the turn, not the command it is
//! running.
//! - A failure that leaves the stream unusable (the output limit, or a line
//!   that starts with `{` but is not valid JSON) kills the child: on Unix as
//!   soon as it is detected, elsewhere when [`AppServer::next_message`] returns
//!   the error. The error also reaches any request still waiting.
//!
//! # Limits
//!
//! [`CodexBuilder::output_limit`](crate::CodexBuilder::output_limit) caps the
//! total bytes read from stdout over the life of the session, as it does for
//! streaming. [`CodexBuilder::timeout`](crate::CodexBuilder::timeout) applies
//! to each request, including the handshake, and not to the session; without
//! it a request waits until the server answers or exits. The server's stderr is
//! drained continuously and the last 16 KiB is kept for error messages.
//!
//! Messages wait in an unbounded queue until [`AppServer::next_message`]
//! returns them, so a caller that does not read them, or reads more slowly than
//! the server writes, holds them in memory. `output_limit` bounds the total.
//! Streaming deltas are most of the volume, and
//! [`AppServerBuilder::opt_out_notification`] stops the server sending them.
//!
//! # Verified against codex-cli 0.149.0
//!
//! Each of these was read from a captured session, not inferred. Re-capture
//! after a CLI upgrade and correct this block in the same change.
//!
//! - Messages are newline-delimited JSON with no `jsonrpc` field. A
//!   notification may carry extra top-level fields (`emittedAtMs`).
//! - A request before `initialize` gets `{"error":{"code":-32600,"message":
//!   "Not initialized"},"id":N}`, a second `initialize` gets `Already
//!   initialized`, and an unknown method gets code -32600 with the valid
//!   method names in the message.
//! - `initialize` takes `clientInfo` and optionally
//!   `capabilities.optOutNotificationMethods`, which suppresses those
//!   notifications for the connection. The client then sends an `initialized`
//!   notification.
//! - `thread/start` accepts `cwd`, `ephemeral`, `approvalPolicy`, `sandbox`,
//!   and `model`. It answers with `{"thread":{"id":...},"model":...}` and
//!   sends a `thread/started` notification.
//! - `turn/start` accepts `threadId`, `input`, `model`, `effort`,
//!   `outputSchema`, `cwd`, and `approvalPolicy`. It answers with
//!   `{"turn":{"id":...,"status":"inProgress"}}`. With `outputSchema` the
//!   captured final message was JSON that followed the schema.
//! - A turn ends with a `turn/completed` notification whose `turn.status` is
//!   `completed` or `interrupted`. Whether `turn.items` holds the final
//!   answer depends on the CLI: 0.149.0 includes it (`itemsView` `summary`),
//!   0.145.0 always sends `items: []` (`notLoaded`). Both send an
//!   `item/completed` notification for the final `agentMessage` (`phase`
//!   `final_answer`) before `turn/completed`, so that is what
//!   [`Notification::agent_message`] decodes.
//! - `turn/steer` answers `{"turnId":...}` at once. The text reaches the model
//!   at the next item boundary, not immediately. With no active turn it fails
//!   with `no active turn to steer`; with one, but a different
//!   `expectedTurnId`, with ``expected active turn id `X` but found `Y` ``.
//! - `turn/interrupt` answers `{}`. With no active turn it fails with `no
//!   active turn to interrupt`; with a different `turnId`, with `expected
//!   active turn id X but found Y`. It ends the turn and leaves a running
//!   shell command alone.
//! - The server runs each shell command in a process group of its own, with
//!   the server as its parent. With a command running, closing stdin made the
//!   server exit 0 and took the command with it; SIGTERM to the server, SIGTERM
//!   to its group, and SIGKILL to its group each left the command running.
//! - When the approval policy asks, the server sends a request
//!   (`item/commandExecution/requestApproval`, numeric id starting at 0) and
//!   the turn waits. The reply is `{"id":0,"result":{"decision":"decline"}}`,
//!   after which the server sends a `serverRequest/resolved` notification.
//! - The server logs to stderr, in colour, including MCP startup errors.
//! - Closing stdin ends the server with status 0. That took 5 seconds in one
//!   capture where no thread had been started, and milliseconds after a turn.
//! - A startup failure (an unknown `-c` key under `--strict-config`) writes one
//!   line to stderr and exits 1.
//!
//! # Assumed, not verified
//!
//! - Server-initiated requests may use string ids; [`RequestId`] accepts both.
//! - A JSON object with neither `method` nor a matching response `id` is
//!   logged and ignored. A line that does not start with `{` is ignored, as
//!   [`streaming`](crate::streaming) ignores it. A line that starts with `{`
//!   and is not valid JSON ends the session.

use std::collections::HashMap;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Duration;

use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize, Serializer};
use serde_json::{Map, Value, json};
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStderr, ChildStdout, Command};
use tokio::sync::{Notify, mpsc, oneshot};
use tracing::{Instrument, debug, warn};

use crate::Codex;
use crate::error::{Error, OutputStream, Result};
use crate::exec::{GroupKillGuard, SpanOutcome};
use crate::types::{ApprovalPolicy, SandboxMode};

/// How much of the server's stderr is kept for error messages.
const STDERR_TAIL_BYTES: usize = 16 * 1024;

/// How long a server that failed during the handshake is given to report why
/// it exited before it is terminated.
const STARTUP_EXIT_WAIT: Duration = Duration::from_secs(2);

/// How long to wait for the stderr reader to finish once the child has exited,
/// so the last lines it wrote make it into an error message.
const STDERR_SETTLE: Duration = Duration::from_millis(500);

/// A JSON-RPC request id.
///
/// The server has been seen to send numbers. The protocol allows strings, so
/// both are accepted, and a reply echoes whichever the server used.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RequestId {
    /// A numeric id.
    Number(i64),
    /// A string id.
    String(String),
}

impl std::fmt::Display for RequestId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Number(n) => write!(f, "{n}"),
            Self::String(s) => f.write_str(s),
        }
    }
}

/// A message the server sent without being asked.
///
/// `params` is `null` when the server sent none.
#[derive(Debug, Clone, PartialEq)]
pub struct Notification {
    /// The method name, for example `turn/completed`.
    pub method: String,
    /// The method's parameters.
    pub params: Value,
}

impl Notification {
    /// Decode an `item/completed` notification for an agent message, or `None`
    /// for anything else.
    ///
    /// A turn may produce several: progress remarks (`commentary`) and then
    /// the answer (`final_answer`). Unlike `turn/completed`, this notification
    /// carries the text on every CLI version tested.
    #[must_use]
    pub fn agent_message(&self) -> Option<AgentMessage> {
        if self.method != "item/completed" {
            return None;
        }
        let item = self.params.get("item")?;
        if item.get("type")?.as_str()? != "agentMessage" {
            return None;
        }
        Some(AgentMessage {
            thread_id: self.params.get("threadId")?.as_str()?.to_string(),
            turn_id: self.params.get("turnId")?.as_str()?.to_string(),
            text: item.get("text")?.as_str()?.to_string(),
            phase: item
                .get("phase")
                .and_then(Value::as_str)
                .map(str::to_string),
        })
    }

    /// Decode a `turn/completed` notification, or `None` for any other method
    /// or a payload that does not have the verified shape.
    #[must_use]
    pub fn turn_completed(&self) -> Option<TurnCompleted> {
        if self.method != "turn/completed" {
            return None;
        }
        serde_json::from_value(self.params.clone()).ok()
    }
}

/// A finished message from the model, decoded from an `item/completed`
/// notification by [`Notification::agent_message`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AgentMessage {
    /// The thread the message belongs to.
    pub thread_id: String,
    /// The turn the message belongs to.
    pub turn_id: String,
    /// The message text.
    pub text: String,
    /// `commentary` for a progress remark, `final_answer` for the answer.
    /// `None` if the server sent no phase.
    pub phase: Option<String>,
}

impl AgentMessage {
    /// Whether this is the turn's answer rather than a progress remark.
    #[must_use]
    pub fn is_final_answer(&self) -> bool {
        self.phase.as_deref() == Some("final_answer")
    }
}

/// A request the server sent and is waiting on, such as an approval.
///
/// Answer it with [`AppServerHandle::respond`] or
/// [`AppServerHandle::respond_error`]. The turn that raised it makes no
/// progress until then.
#[derive(Debug, Clone, PartialEq)]
pub struct ServerRequest {
    /// The id to echo in the reply.
    pub id: RequestId,
    /// The method name, for example `item/commandExecution/requestApproval`.
    pub method: String,
    /// The method's parameters.
    pub params: Value,
}

/// One message from the server that is not the answer to a request.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ServerMessage {
    /// An event, such as `turn/started` or `item/completed`.
    Notification(Notification),
    /// A request that needs a reply.
    Request(ServerRequest),
}

/// How a turn is going, from its `status` field.
///
/// `inProgress`, `completed`, and `interrupted` were captured. `failed` comes
/// from the protocol schema.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum TurnStatus {
    /// The turn is running.
    InProgress,
    /// The turn finished normally.
    Completed,
    /// The turn was stopped by `turn/interrupt`.
    Interrupted,
    /// The turn failed; see [`Turn::error`].
    Failed,
    /// A status this version of the wrapper does not know.
    #[serde(other)]
    Unknown,
}

/// A turn, as reported in `turn/start` responses and `turn/completed`
/// notifications.
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Turn {
    /// The turn's id, needed for [`AppServerHandle::turn_steer`] and
    /// [`AppServerHandle::turn_interrupt`].
    pub id: String,
    /// The turn's status.
    pub status: TurnStatus,
    /// The items the payload includes. Empty for a turn that was interrupted.
    #[serde(default)]
    pub items: Vec<Value>,
    /// Why the turn failed. Only set when the status is
    /// [`TurnStatus::Failed`].
    #[serde(default)]
    pub error: Option<Value>,
    /// How long the turn ran, in milliseconds, once it has ended.
    #[serde(default)]
    pub duration_ms: Option<i64>,
}

impl Turn {
    /// The text of the turn's final answer: the last `agentMessage` item in
    /// [`items`](Self::items) whose `phase` is `final_answer`.
    ///
    /// Only newer servers put the answer in `turn/completed` (0.149.0 does,
    /// 0.145.0 sends no items), and an interrupted turn never has one. To get
    /// the answer from any version, keep the last
    /// [`Notification::agent_message`] whose
    /// [`is_final_answer`](AgentMessage::is_final_answer) is true.
    #[must_use]
    pub fn final_message(&self) -> Option<&str> {
        self.items
            .iter()
            .rev()
            .filter(|item| {
                item.get("type").and_then(Value::as_str) == Some("agentMessage")
                    && item.get("phase").and_then(Value::as_str) == Some("final_answer")
            })
            .find_map(|item| item.get("text").and_then(Value::as_str))
    }
}

/// The payload of a `turn/completed` notification.
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TurnCompleted {
    /// The thread the turn belongs to.
    pub thread_id: String,
    /// The turn, in its final state.
    pub turn: Turn,
}

/// A thread, as returned by `thread/start`.
#[derive(Debug, Clone, PartialEq)]
pub struct Thread {
    /// The thread id, needed to start turns on it.
    pub id: String,
    /// The model the thread will use, when the server reports one.
    pub model: Option<String>,
    /// The complete response, for fields this type does not name.
    pub raw: Value,
}

/// What the server reported about itself in the `initialize` response.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct ServerInfo {
    /// The server's user agent string, which includes the CLI version.
    pub user_agent: String,
    /// The Codex home directory the server is using.
    pub codex_home: Option<String>,
    /// `unix` or `windows`.
    pub platform_family: Option<String>,
    /// The operating system name.
    pub platform_os: Option<String>,
}

/// One piece of user input for a turn.
#[derive(Debug, Clone, PartialEq)]
pub enum UserInput {
    /// Plain text.
    Text(String),
    /// Any other input item, sent as given. See the protocol schema for the
    /// shapes the server accepts.
    Raw(Value),
}

impl UserInput {
    /// A text input.
    #[must_use]
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text(text.into())
    }
}

impl Serialize for UserInput {
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        match self {
            Self::Text(text) => {
                let mut map = serializer.serialize_map(Some(3))?;
                map.serialize_entry("type", "text")?;
                map.serialize_entry("text", text)?;
                // Sent as the captured client sent it. The schema gives the
                // field a default, but a server that requires it is not ruled
                // out by that.
                map.serialize_entry("text_elements", &Vec::<Value>::new())?;
                map.end()
            }
            Self::Raw(value) => value.serialize(serializer),
        }
    }
}

/// Parameters for `thread/start`.
///
/// Every field is optional. Fields this type does not name can be set with
/// [`param`](Self::param).
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ThreadStartParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    cwd: Option<PathBuf>,
    #[serde(skip_serializing_if = "Option::is_none")]
    model: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    approval_policy: Option<ApprovalPolicy>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sandbox: Option<SandboxMode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ephemeral: Option<bool>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl ThreadStartParams {
    /// Parameters with nothing set, which starts a thread with the server's
    /// defaults.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The thread's working directory.
    #[must_use]
    pub fn cwd(mut self, dir: impl Into<PathBuf>) -> Self {
        self.cwd = Some(dir.into());
        self
    }

    /// The model the thread uses.
    #[must_use]
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// When the server asks before running a command or applying a change.
    #[must_use]
    pub fn approval_policy(mut self, policy: ApprovalPolicy) -> Self {
        self.approval_policy = Some(policy);
        self
    }

    /// The sandbox for commands the model runs.
    #[must_use]
    pub fn sandbox(mut self, mode: SandboxMode) -> Self {
        self.sandbox = Some(mode);
        self
    }

    /// Whether the thread is kept out of the session history.
    #[must_use]
    pub fn ephemeral(mut self, ephemeral: bool) -> Self {
        self.ephemeral = Some(ephemeral);
        self
    }

    /// Set a parameter this type has no method for, using the protocol's
    /// camelCase name. It is sent as given and not checked.
    #[must_use]
    pub fn param(mut self, name: impl Into<String>, value: Value) -> Self {
        self.extra.insert(name.into(), value);
        self
    }
}

/// Parameters for `turn/start`.
///
/// The thread and the input are required. The other fields override the
/// thread's settings for this turn and, per the protocol, later ones. Fields
/// this type does not name can be set with [`param`](Self::param).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TurnStartParams {
    thread_id: String,
    input: Vec<UserInput>,
    #[serde(skip_serializing_if = "Option::is_none")]
    model: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    effort: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    output_schema: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cwd: Option<PathBuf>,
    #[serde(skip_serializing_if = "Option::is_none")]
    approval_policy: Option<ApprovalPolicy>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl TurnStartParams {
    /// A turn on `thread_id` with the given input.
    #[must_use]
    pub fn new(thread_id: impl Into<String>, input: Vec<UserInput>) -> Self {
        Self {
            thread_id: thread_id.into(),
            input,
            model: None,
            effort: None,
            output_schema: None,
            cwd: None,
            approval_policy: None,
            extra: Map::new(),
        }
    }

    /// A turn on `thread_id` whose input is one text prompt.
    #[must_use]
    pub fn text(thread_id: impl Into<String>, prompt: impl Into<String>) -> Self {
        Self::new(thread_id, vec![UserInput::text(prompt)])
    }

    /// The model for this turn.
    #[must_use]
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// The reasoning effort for this turn. The valid values depend on the
    /// model, so this is a string.
    #[must_use]
    pub fn effort(mut self, effort: impl Into<String>) -> Self {
        self.effort = Some(effort.into());
        self
    }

    /// A JSON Schema the final message must follow.
    #[must_use]
    pub fn output_schema(mut self, schema: Value) -> Self {
        self.output_schema = Some(schema);
        self
    }

    /// The working directory for this turn.
    #[must_use]
    pub fn cwd(mut self, dir: impl Into<PathBuf>) -> Self {
        self.cwd = Some(dir.into());
        self
    }

    /// When the server asks before running a command or applying a change.
    #[must_use]
    pub fn approval_policy(mut self, policy: ApprovalPolicy) -> Self {
        self.approval_policy = Some(policy);
        self
    }

    /// Set a parameter this type has no method for, using the protocol's
    /// camelCase name. It is sent as given and not checked.
    #[must_use]
    pub fn param(mut self, name: impl Into<String>, value: Value) -> Self {
        self.extra.insert(name.into(), value);
        self
    }
}

/// A JSON-RPC error the server returned for a request.
struct RpcFailure {
    code: i64,
    message: String,
    data: Option<Value>,
}

/// Why the connection to the server ended.
#[derive(Debug, Clone)]
enum Closed {
    /// The server closed its stdout: it exited, or was stopped.
    Eof,
    /// Stdout passed the byte ceiling.
    OutputLimit { limit_bytes: usize },
    /// The stream cannot be trusted any more, or could not be read or written.
    Broken(String),
    /// The client ended the session.
    Terminated,
}

/// What a waiting request gets back.
enum Reply {
    Result(Value),
    Rpc(RpcFailure),
    /// A response with neither `result` nor `error`.
    Malformed,
    Closed(Closed),
}

/// What the reader hands to [`AppServer::next_message`].
enum Event {
    Message(ServerMessage),
    Closed(Closed),
}

/// What the writer task is asked to do.
enum Outgoing {
    Line(Vec<u8>),
    /// Stop writing and drop stdin, which the server sees as end of input.
    Close,
}

struct State {
    pending: HashMap<i64, oneshot::Sender<Reply>>,
    closed: Option<Closed>,
}

/// State shared by the handles, the owner, and the three I/O tasks.
struct Shared {
    state: Mutex<State>,
    next_id: AtomicI64,
    stderr_tail: Mutex<Vec<u8>>,
    /// Set, and `stderr_finished` notified, when the stderr reader has seen
    /// end of file, so its last lines are in `stderr_tail`.
    stderr_done: AtomicBool,
    stderr_finished: Notify,
    /// What the reader task may kill when the stream breaks. Taken away, under
    /// this lock, before the client reaps the child, so the reader can never
    /// signal a pid that has been released.
    kill: Mutex<Option<KillTarget>>,
    events: mpsc::UnboundedSender<Event>,
}

fn locked<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    // Nothing here panics while holding a lock, so a poisoned mutex holds
    // consistent data. Recovering keeps one panicking task from taking the
    // whole session down with it.
    mutex.lock().unwrap_or_else(PoisonError::into_inner)
}

impl Shared {
    /// End the session: fail every waiting request and tell the reader side.
    /// The first reason wins.
    fn fail(&self, reason: Closed) {
        let pending = {
            let mut state = locked(&self.state);
            if state.closed.is_some() {
                return;
            }
            state.closed = Some(reason.clone());
            std::mem::take(&mut state.pending)
        };
        for (_, waiter) in pending {
            let _ = waiter.send(Reply::Closed(reason.clone()));
        }
        let _ = self.events.send(Event::Closed(reason));
    }

    fn new(events: mpsc::UnboundedSender<Event>, kill: Option<KillTarget>) -> Self {
        Self {
            state: Mutex::new(State {
                pending: HashMap::new(),
                closed: None,
            }),
            next_id: AtomicI64::new(0),
            stderr_tail: Mutex::new(Vec::new()),
            stderr_done: AtomicBool::new(false),
            stderr_finished: Notify::new(),
            kill: Mutex::new(kill),
            events,
        }
    }

    /// Kill the server, if the reader still has the authority to. Called when
    /// the stream is unusable and nothing will read what the server writes.
    fn fire_kill(&self) {
        // Held across the signal, so `revoke_kill` cannot return while one is
        // being sent.
        if let Some(target) = *locked(&self.kill) {
            target.signal();
        }
    }

    /// Take away the reader's authority to kill. Must be called before the
    /// child is reaped: from then on the pid may belong to another process.
    fn revoke_kill(&self) {
        *locked(&self.kill) = None;
    }

    /// Wait, for a short while, until the stderr reader has read everything
    /// the server wrote. Stdout and stderr close together when the server
    /// exits, but the two readers are separate tasks, so an error built from
    /// the tail straight away could miss the last lines.
    async fn stderr_settled(&self) {
        let wait = async {
            loop {
                let notified = self.stderr_finished.notified();
                if self.stderr_done.load(Ordering::Acquire) {
                    return;
                }
                notified.await;
            }
        };
        let _ = tokio::time::timeout(STDERR_SETTLE, wait).await;
    }

    /// The last lines the server wrote to stderr, without colour codes.
    fn stderr_summary(&self) -> String {
        let tail = locked(&self.stderr_tail);
        let text = strip_ansi(&String::from_utf8_lossy(&tail));
        let lines: Vec<&str> = text
            .lines()
            .map(str::trim)
            .filter(|line| !line.is_empty())
            .collect();
        let start = lines.len().saturating_sub(5);
        lines[start..].join("\n")
    }

    fn error_for(&self, reason: &Closed, method: Option<&str>) -> Error {
        let protocol = |message: String| Error::AppServerProtocol { message };
        match reason {
            Closed::OutputLimit { limit_bytes } => Error::OutputLimitExceeded {
                stream: OutputStream::Stdout,
                limit_bytes: *limit_bytes,
            },
            Closed::Terminated => protocol("the app-server session was ended by the client".into()),
            Closed::Broken(message) => protocol(message.clone()),
            Closed::Eof => {
                let stderr = self.stderr_summary();
                let while_waiting =
                    method.map_or_else(String::new, |m| format!(" before answering `{m}`"));
                if stderr.is_empty() {
                    protocol(format!("codex app-server closed its output{while_waiting}"))
                } else {
                    protocol(format!(
                        "codex app-server closed its output{while_waiting}; stderr: {stderr}"
                    ))
                }
            }
        }
    }
}

/// Remove ANSI colour sequences (`ESC [ ... letter`) from server log lines.
fn strip_ansi(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut chars = text.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\u{1b}' && chars.peek() == Some(&'[') {
            chars.next();
            for c in chars.by_ref() {
                if c.is_ascii_alphabetic() {
                    break;
                }
            }
        } else {
            out.push(c);
        }
    }
    out
}

/// Removes a waiting request's entry when the request future is dropped, so a
/// cancelled request does not leave its slot behind.
struct PendingSlot<'a> {
    shared: &'a Shared,
    id: i64,
}

impl Drop for PendingSlot<'_> {
    fn drop(&mut self) {
        locked(&self.shared.state).pending.remove(&self.id);
    }
}

fn json_error(context: &str, source: serde_json::Error) -> Error {
    Error::Json {
        message: format!("{context}: {source}"),
        source,
    }
}

fn request_line(id: i64, method: &str, params: &Value) -> Result<Vec<u8>> {
    let method = serde_json::to_string(method).map_err(|e| json_error("method name", e))?;
    let mut line = format!("{{\"id\":{id},\"method\":{method}");
    if !params.is_null() {
        let params = serde_json::to_string(params).map_err(|e| json_error("request params", e))?;
        line.push_str(",\"params\":");
        line.push_str(&params);
    }
    line.push_str("}\n");
    Ok(line.into_bytes())
}

fn notification_line(method: &str, params: &Value) -> Result<Vec<u8>> {
    let method = serde_json::to_string(method).map_err(|e| json_error("method name", e))?;
    let mut line = format!("{{\"method\":{method}");
    if !params.is_null() {
        let params =
            serde_json::to_string(params).map_err(|e| json_error("notification params", e))?;
        line.push_str(",\"params\":");
        line.push_str(&params);
    }
    line.push_str("}\n");
    Ok(line.into_bytes())
}

fn reply_line(id: &RequestId, body_key: &str, body: &Value) -> Result<Vec<u8>> {
    let id = serde_json::to_string(id).map_err(|e| json_error("request id", e))?;
    let body = serde_json::to_string(body).map_err(|e| json_error("reply body", e))?;
    Ok(format!("{{\"id\":{id},\"{body_key}\":{body}}}\n").into_bytes())
}

/// A cloneable handle for sending to the server.
///
/// It can send requests and answer the server's, but not read events: that
/// stays with the [`AppServer`], so a task can steer or interrupt a turn while
/// another reads its notifications. Every method fails once the session has
/// ended.
#[derive(Clone)]
pub struct AppServerHandle {
    shared: Arc<Shared>,
    writer: mpsc::UnboundedSender<Outgoing>,
    timeout: Option<Duration>,
}

impl std::fmt::Debug for AppServerHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AppServerHandle")
            .field("timeout", &self.timeout)
            .finish_non_exhaustive()
    }
}

impl AppServerHandle {
    fn closed_error(&self, method: Option<&str>) -> Error {
        let reason = locked(&self.shared.state).closed.clone();
        match reason {
            Some(reason) => self.shared.error_for(&reason, method),
            None => Error::AppServerProtocol {
                message: "codex app-server is no longer accepting messages (its stdin is closed)"
                    .into(),
            },
        }
    }

    fn enqueue(&self, line: Vec<u8>, method: Option<&str>) -> Result<()> {
        if let Some(reason) = &locked(&self.shared.state).closed {
            return Err(self.shared.error_for(reason, method));
        }
        self.writer
            .send(Outgoing::Line(line))
            .map_err(|_| self.closed_error(method))
    }

    /// Send a request and wait for its answer, returning the `result` value.
    ///
    /// Requests may run concurrently; each answer is matched by id. A JSON-RPC
    /// error from the server is [`Error::AppServerRpc`]. The client's
    /// [`timeout`](crate::CodexBuilder::timeout), when set, bounds the wait
    /// and yields [`Error::Timeout`]. Pass [`Value::Null`] for no parameters.
    ///
    /// # Errors
    ///
    /// Fails if the server rejects the request, the wait times out, or the
    /// session ends first.
    pub async fn request(&self, method: &str, params: Value) -> Result<Value> {
        let id = self.shared.next_id.fetch_add(1, Ordering::Relaxed);
        let line = request_line(id, method, &params)?;
        let (waiter, answer) = oneshot::channel();
        {
            let mut state = locked(&self.shared.state);
            if let Some(reason) = &state.closed {
                return Err(self.shared.error_for(reason, Some(method)));
            }
            state.pending.insert(id, waiter);
        }
        let _slot = PendingSlot {
            shared: &self.shared,
            id,
        };
        self.writer
            .send(Outgoing::Line(line))
            .map_err(|_| self.closed_error(Some(method)))?;

        let reply = match self.timeout {
            Some(limit) => {
                tokio::time::timeout(limit, answer)
                    .await
                    .map_err(|_| Error::Timeout {
                        timeout_seconds: limit.as_secs(),
                    })?
            }
            None => answer.await,
        };
        match reply {
            Ok(Reply::Result(result)) => Ok(result),
            Ok(Reply::Rpc(failure)) => Err(Error::AppServerRpc {
                method: method.to_string(),
                code: failure.code,
                message: failure.message,
                data: failure.data,
            }),
            Ok(Reply::Malformed) => Err(Error::AppServerProtocol {
                message: format!(
                    "codex app-server answered `{method}` with neither result nor error"
                ),
            }),
            Ok(Reply::Closed(reason)) => Err(self.shared.error_for(&reason, Some(method))),
            Err(_) => Err(self.closed_error(Some(method))),
        }
    }

    /// Send a notification, which has no answer. Returns once it is queued
    /// for writing.
    ///
    /// # Errors
    ///
    /// Fails if the session has ended.
    pub fn notify(&self, method: &str, params: Value) -> Result<()> {
        self.enqueue(notification_line(method, &params)?, Some(method))
    }

    /// Answer a [`ServerRequest`] with a result. Returns once it is queued for
    /// writing.
    ///
    /// # Errors
    ///
    /// Fails if the session has ended.
    pub fn respond(&self, id: RequestId, result: Value) -> Result<()> {
        self.enqueue(reply_line(&id, "result", &result)?, None)
    }

    /// Answer a [`ServerRequest`] with a JSON-RPC error. Use it for a request
    /// the caller does not handle, so the turn that raised it is not left
    /// waiting. Returns once it is queued for writing.
    ///
    /// # Errors
    ///
    /// Fails if the session has ended.
    pub fn respond_error(
        &self,
        id: RequestId,
        code: i64,
        message: impl Into<String>,
    ) -> Result<()> {
        let body = json!({ "code": code, "message": message.into() });
        self.enqueue(reply_line(&id, "error", &body)?, None)
    }

    /// Start a thread (`thread/start`).
    ///
    /// # Errors
    ///
    /// As [`request`](Self::request), and a protocol error if the response has
    /// no thread id.
    pub async fn thread_start(&self, params: ThreadStartParams) -> Result<Thread> {
        let params =
            serde_json::to_value(params).map_err(|e| json_error("thread/start params", e))?;
        let raw = self.request("thread/start", params).await?;
        let id = raw
            .pointer("/thread/id")
            .and_then(Value::as_str)
            .ok_or_else(|| Error::AppServerProtocol {
                message: "thread/start returned no thread id".into(),
            })?
            .to_string();
        let model = raw.get("model").and_then(Value::as_str).map(str::to_string);
        Ok(Thread { id, model, raw })
    }

    /// Start a turn (`turn/start`). The turn runs in the background: watch for
    /// its `turn/completed` notification.
    ///
    /// # Errors
    ///
    /// As [`request`](Self::request), and a protocol error if the response has
    /// no turn.
    pub async fn turn_start(&self, params: TurnStartParams) -> Result<Turn> {
        let params =
            serde_json::to_value(params).map_err(|e| json_error("turn/start params", e))?;
        let result = self.request("turn/start", params).await?;
        let turn = result.get("turn").cloned().unwrap_or(Value::Null);
        serde_json::from_value(turn).map_err(|e| Error::AppServerProtocol {
            message: format!("turn/start returned no usable turn: {e}"),
        })
    }

    /// Add input to a running turn (`turn/steer`) and return the id of the
    /// turn it landed on.
    ///
    /// The server delivers it to the model at the next item boundary, so a
    /// long tool call delays it. `expected_turn_id` must be the active turn.
    /// With no active turn the server fails the request with `no active turn
    /// to steer`, and with a different one with `expected active turn id ...`;
    /// both are [`Error::AppServerRpc`].
    ///
    /// # Errors
    ///
    /// As [`request`](Self::request).
    pub async fn turn_steer(
        &self,
        thread_id: &str,
        expected_turn_id: &str,
        input: Vec<UserInput>,
    ) -> Result<String> {
        let params = json!({
            "threadId": thread_id,
            "expectedTurnId": expected_turn_id,
            "input": input,
        });
        let result = self.request("turn/steer", params).await?;
        result
            .get("turnId")
            .and_then(Value::as_str)
            .map(str::to_string)
            .ok_or_else(|| Error::AppServerProtocol {
                message: "turn/steer returned no turn id".into(),
            })
    }

    /// Stop a running turn (`turn/interrupt`). The turn then ends with a
    /// `turn/completed` notification whose status is
    /// [`TurnStatus::Interrupted`].
    ///
    /// With no active turn the server fails the request with `no active turn
    /// to interrupt`, and with a different one with `expected active turn id
    /// ...`; both are [`Error::AppServerRpc`].
    ///
    /// # Errors
    ///
    /// As [`request`](Self::request).
    pub async fn turn_interrupt(&self, thread_id: &str, turn_id: &str) -> Result<()> {
        let params = json!({ "threadId": thread_id, "turnId": turn_id });
        self.request("turn/interrupt", params).await.map(|_| ())
    }
}

/// Builds and starts an [`AppServer`].
///
/// Created by [`AppServer::builder`].
#[derive(Debug, Clone)]
pub struct AppServerBuilder<'a> {
    codex: &'a Codex,
    client_name: String,
    client_version: String,
    opt_out: Vec<String>,
}

impl<'a> AppServerBuilder<'a> {
    /// Name and version reported to the server in `initialize`. The server
    /// includes them in its user agent. Defaults to `codex-wrapper` and this
    /// crate's version.
    #[must_use]
    pub fn client_info(mut self, name: impl Into<String>, version: impl Into<String>) -> Self {
        self.client_name = name.into();
        self.client_version = version.into();
        self
    }

    /// Ask the server not to send a notification method on this connection,
    /// by exact name. Streaming deltas (`item/agentMessage/delta`) are the
    /// usual candidate for a caller that only needs completed items.
    #[must_use]
    pub fn opt_out_notification(mut self, method: impl Into<String>) -> Self {
        self.opt_out.push(method.into());
        self
    }

    /// The arguments [`start`](Self::start) passes after the client's global
    /// args: `app-server --listen stdio://`.
    ///
    /// `--listen stdio://` is the default transport. It is passed anyway so a
    /// change of default cannot move this client onto a socket.
    #[must_use]
    pub fn args(&self) -> Vec<String> {
        vec!["app-server".into(), "--listen".into(), "stdio://".into()]
    }

    /// The command [`start`](Self::start) will run, as a copy-pasteable shell
    /// string, including the client's global args.
    #[must_use]
    pub fn to_command_string(&self) -> String {
        crate::exec::command_string(self.codex, self.args())
    }

    /// Spawn `codex app-server` and complete the handshake.
    ///
    /// The command is `codex [global args] app-server --listen stdio://`. The
    /// client's binary, environment, working directory, process-group policy,
    /// `die_with_parent`, and `on_spawn` observer apply. The handshake is
    /// bounded by the client's timeout, when set.
    ///
    /// # Errors
    ///
    /// [`Error::Io`] if the process cannot be spawned. If the server exits
    /// before the handshake finishes, the failure is classified from its
    /// stderr as any failed command is, so a rejected `-c` key is
    /// [`Error::Config`]. Otherwise the handshake's own error.
    pub async fn start(self) -> Result<AppServer> {
        let codex = self.codex;
        let operation_args = self.args();
        let span = crate::exec::command_span("codex.app_server", codex, &operation_args);
        let args = crate::exec::assemble_args(codex, operation_args);
        let command_text = format!("{} {}", codex.binary.display(), args.join(" "));
        debug!(arg_count = args.len(), "starting codex app-server");

        let mut outcome = SpanOutcome::start(span.clone());
        let mut command = Command::new(&codex.binary);
        command.args(&args);
        command.stdin(std::process::Stdio::piped());
        command.stdout(std::process::Stdio::piped());
        command.stderr(std::process::Stdio::piped());
        // As for streaming: dropping the client must not leave the server
        // running with no handle left to stop it.
        command.kill_on_drop(true);
        crate::exec::own_process_group(&mut command, codex.process_group);
        crate::exec::apply_die_with_parent(&mut command, codex.die_with_parent);
        if let Some(dir) = &codex.working_dir {
            command.current_dir(dir);
        }
        crate::exec::apply_child_environment(&mut command, codex.clear_env, &codex.env);

        let mut child = match command.spawn() {
            Ok(child) => child,
            Err(e) => {
                outcome.settle("error", None);
                return Err(Error::Io {
                    message: format!("failed to spawn codex app-server: {e}"),
                    source: e,
                    working_dir: codex.working_dir.clone(),
                });
            }
        };
        let group = crate::exec::arm_and_notify(
            codex.process_group,
            codex.termination_signal,
            child.id(),
            codex.on_spawn.as_ref(),
        );
        let pid = child.id();

        let stdin = child.stdin.take().expect("stdin was configured as piped");
        let stdout = child.stdout.take().expect("stdout was configured as piped");
        let stderr = child.stderr.take().expect("stderr was configured as piped");

        let (events_tx, events_rx) = mpsc::unbounded_channel();
        let (writer_tx, writer_rx) = mpsc::unbounded_channel();
        let shared = Arc::new(Shared::new(
            events_tx,
            Some(KillTarget {
                pid,
                group: codex.process_group,
            }),
        ));

        tokio::spawn(read_stdout(
            stdout,
            Arc::clone(&shared),
            codex.output_limit,
            codex.working_dir.clone(),
        ));
        tokio::spawn(drain_stderr(stderr, Arc::clone(&shared)));
        tokio::spawn(write_stdin(stdin, writer_rx, Arc::clone(&shared)));

        let handle = AppServerHandle {
            shared,
            writer: writer_tx,
            timeout: codex.timeout,
        };
        let mut server = AppServer {
            handle,
            events: events_rx,
            finished: false,
            child,
            group,
            outcome,
            termination_grace: codex.termination_grace,
            working_dir: codex.working_dir.clone(),
            command: command_text,
            info: ServerInfo::default(),
        };

        let handshake = initialize(
            &server.handle,
            &self.client_name,
            &self.client_version,
            &self.opt_out,
        )
        .instrument(span)
        .await;
        match handshake {
            Ok(info) => {
                server.info = info;
                Ok(server)
            }
            Err(error) => Err(server.abandon(error).await),
        }
    }
}

async fn initialize(
    handle: &AppServerHandle,
    name: &str,
    version: &str,
    opt_out: &[String],
) -> Result<ServerInfo> {
    let mut params = json!({ "clientInfo": { "name": name, "version": version } });
    if !opt_out.is_empty() {
        params["capabilities"] = json!({ "optOutNotificationMethods": opt_out });
    }
    let result = handle.request("initialize", params).await?;
    let info = serde_json::from_value(result).map_err(|e| Error::AppServerProtocol {
        message: format!("initialize returned an unexpected response: {e}"),
    })?;
    handle.notify("initialized", Value::Null)?;
    Ok(info)
}

/// Which process a fatal stream error stops.
#[derive(Clone, Copy)]
struct KillTarget {
    pid: Option<u32>,
    group: bool,
}

impl KillTarget {
    /// Stop the server now. The stream is unusable and nothing will read what
    /// the server writes next, so leaving it running would only fill the pipe.
    ///
    /// Unix only: there is nothing to signal elsewhere, where
    /// [`AppServer::next_message`] kills the child when it returns the error.
    fn signal(self) {
        #[cfg(unix)]
        if let Some(pid) = self.pid {
            if self.group {
                crate::exec::signal_group(pid, libc::SIGKILL);
            } else if let Ok(pid) = i32::try_from(pid) {
                // SAFETY: `kill` on a pid this client spawned. If the child
                // has already been reaped the call fails, which is the state
                // we want.
                unsafe {
                    libc::kill(pid, libc::SIGKILL);
                }
            }
        }
        // Read on every platform, so the fields are not dead code where there
        // is no signal to send.
        #[cfg(not(unix))]
        let _ = (self.pid, self.group);
    }
}

/// Read the server's stdout: route answers to waiting requests and everything
/// else to the event queue.
async fn read_stdout(
    stdout: ChildStdout,
    shared: Arc<Shared>,
    output_limit: Option<usize>,
    working_dir: Option<PathBuf>,
) {
    let mut reader = BufReader::new(stdout);
    let mut captured = 0usize;
    let reason = loop {
        let line = match crate::streaming::read_bounded_line(
            &mut reader,
            &mut captured,
            output_limit,
            OutputStream::Stdout,
            &working_dir,
        )
        .await
        {
            Ok(Some(line)) => line,
            Ok(None) => {
                shared.stderr_settled().await;
                break Closed::Eof;
            }
            Err(Error::OutputLimitExceeded { limit_bytes, .. }) => {
                shared.fire_kill();
                break Closed::OutputLimit { limit_bytes };
            }
            Err(error) => {
                shared.fire_kill();
                break Closed::Broken(error.to_string());
            }
        };
        if !line.trim_start().starts_with('{') {
            debug!(
                bytes = line.len(),
                "ignoring a non-JSON line from codex app-server"
            );
            continue;
        }
        if let Err(reason) = route(&line, &shared) {
            shared.fire_kill();
            break reason;
        }
    };
    shared.fail(reason);
}

/// Classify one line from the server and deliver it.
fn route(line: &str, shared: &Shared) -> std::result::Result<(), Closed> {
    let value: Value = serde_json::from_str(line).map_err(|e| {
        let head: String = line.chars().take(120).collect();
        Closed::Broken(format!("codex app-server sent invalid JSON ({e}): {head}"))
    })?;
    let method = value.get("method").and_then(Value::as_str);
    let id = value.get("id");
    match (method, id) {
        (Some(method), Some(id)) => {
            let Ok(id) = serde_json::from_value::<RequestId>(id.clone()) else {
                warn!(
                    method_bytes = method.len(),
                    "ignoring a server request with an unusable id"
                );
                return Ok(());
            };
            let request = ServerRequest {
                id,
                method: method.to_string(),
                params: value.get("params").cloned().unwrap_or(Value::Null),
            };
            let _ = shared
                .events
                .send(Event::Message(ServerMessage::Request(request)));
        }
        (Some(method), None) => {
            let notification = Notification {
                method: method.to_string(),
                params: value.get("params").cloned().unwrap_or(Value::Null),
            };
            let _ = shared
                .events
                .send(Event::Message(ServerMessage::Notification(notification)));
        }
        (None, Some(id)) if value.get("result").is_some() || value.get("error").is_some() => {
            deliver_answer(&value, id, shared);
        }
        (None, Some(id)) => {
            // Shaped like an answer, but neither a result nor an error. If it
            // names a waiting request, fail that request rather than leave it
            // waiting for an answer that will not come.
            let waiter = id
                .as_i64()
                .and_then(|id| locked(&shared.state).pending.remove(&id));
            match waiter {
                Some(waiter) => {
                    let _ = waiter.send(Reply::Malformed);
                }
                None => warn!("ignoring an answer with neither result nor error"),
            }
        }
        _ => warn!("ignoring a JSON object from codex app-server that is not a message"),
    }
    Ok(())
}

fn deliver_answer(value: &Value, id: &Value, shared: &Shared) {
    let Some(id) = id.as_i64() else {
        // The server answers a request it could not parse with a null id.
        debug!("ignoring an answer with no request id");
        return;
    };
    let waiter = locked(&shared.state).pending.remove(&id);
    let Some(waiter) = waiter else {
        // The request was cancelled or timed out before the answer arrived.
        debug!(id, "ignoring an answer nobody is waiting for");
        return;
    };
    let reply = match value.get("error") {
        Some(error) => Reply::Rpc(RpcFailure {
            code: error.get("code").and_then(Value::as_i64).unwrap_or(0),
            message: error
                .get("message")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_string(),
            data: error.get("data").cloned(),
        }),
        None => Reply::Result(value.get("result").cloned().unwrap_or(Value::Null)),
    };
    let _ = waiter.send(reply);
}

/// Drain stderr for the life of the server. The server logs a lot there, and a
/// pipe nobody reads fills and stops it.
async fn drain_stderr(mut stderr: ChildStderr, shared: Arc<Shared>) {
    let mut buffer = [0u8; 4096];
    loop {
        match stderr.read(&mut buffer).await {
            Ok(0) | Err(_) => {
                shared.stderr_done.store(true, Ordering::Release);
                shared.stderr_finished.notify_waiters();
                break;
            }
            Ok(read) => {
                let mut tail = locked(&shared.stderr_tail);
                tail.extend_from_slice(&buffer[..read]);
                if tail.len() > STDERR_TAIL_BYTES {
                    let excess = tail.len() - STDERR_TAIL_BYTES;
                    tail.drain(..excess);
                }
            }
        }
    }
}

/// Write queued lines to the server's stdin, one whole line at a time.
///
/// Writing happens here, not in the request future, so a request that is
/// dropped part way through cannot leave half a line on the wire.
async fn write_stdin<W: AsyncWrite + Unpin>(
    mut stdin: W,
    mut lines: mpsc::UnboundedReceiver<Outgoing>,
    shared: Arc<Shared>,
) {
    while let Some(outgoing) = lines.recv().await {
        match outgoing {
            Outgoing::Line(line) => {
                let written = async {
                    stdin.write_all(&line).await?;
                    stdin.flush().await
                }
                .await;
                if let Err(error) = written {
                    if error.kind() == std::io::ErrorKind::BrokenPipe {
                        // The server closed its input, which it does by
                        // exiting. Its output closes with it, and the reader
                        // reports that together with the server's stderr and,
                        // at startup, its exit status. Failing the session
                        // here first would hide all of that behind "broken
                        // pipe", and which one wins is a race.
                        debug!("codex app-server closed its stdin");
                    } else {
                        shared.fail(Closed::Broken(format!(
                            "failed to write to codex app-server stdin: {error}"
                        )));
                    }
                    break;
                }
            }
            Outgoing::Close => break,
        }
    }
    // Dropping stdin closes the pipe, which the server treats as the end of
    // its input.
}

/// A running `codex app-server`.
///
/// Requests go through [`AppServerHandle`], which this type dereferences to,
/// so `server.turn_start(..)` works directly. [`handle`](Self::handle) gives a
/// clone that can be moved to another task. Events are read with
/// [`next_message`](Self::next_message), which needs `&mut self`.
///
/// Dropping the value kills the server's process group without waiting; use
/// [`shutdown`](Self::shutdown) or [`terminate`](Self::terminate) to stop it
/// and wait.
pub struct AppServer {
    handle: AppServerHandle,
    events: mpsc::UnboundedReceiver<Event>,
    finished: bool,
    child: Child,
    group: GroupKillGuard,
    outcome: SpanOutcome,
    termination_grace: Duration,
    working_dir: Option<PathBuf>,
    command: String,
    info: ServerInfo,
}

impl std::fmt::Debug for AppServer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AppServer")
            .field("pid", &self.child.id())
            .field("info", &self.info)
            .finish_non_exhaustive()
    }
}

impl Deref for AppServer {
    type Target = AppServerHandle;

    fn deref(&self) -> &AppServerHandle {
        &self.handle
    }
}

impl AppServer {
    /// Start configuring a server for `codex`.
    #[must_use]
    pub fn builder(codex: &Codex) -> AppServerBuilder<'_> {
        AppServerBuilder {
            codex,
            client_name: "codex-wrapper".to_string(),
            client_version: env!("CARGO_PKG_VERSION").to_string(),
            opt_out: Vec::new(),
        }
    }

    /// What the server reported about itself during the handshake.
    #[must_use]
    pub fn server_info(&self) -> &ServerInfo {
        &self.info
    }

    /// The server process's id, while it is running.
    #[must_use]
    pub fn pid(&self) -> Option<u32> {
        self.child.id()
    }

    /// A cloneable handle for sending requests from other tasks.
    #[must_use]
    pub fn handle(&self) -> AppServerHandle {
        self.handle.clone()
    }

    /// The next notification or server request, in the order the server sent
    /// them.
    ///
    /// Returns `Ok(None)` once the server's output has ended, after every
    /// earlier message has been returned. That is how a server that exited on
    /// its own looks; [`shutdown`](Self::shutdown) then reports its exit
    /// status. Cancel-safe: dropping the future loses no message.
    ///
    /// # Errors
    ///
    /// [`Error::OutputLimitExceeded`] or [`Error::AppServerProtocol`] when the
    /// stream became unusable. The server has been killed by then. The error
    /// is returned once; later calls return `Ok(None)`.
    pub async fn next_message(&mut self) -> Result<Option<ServerMessage>> {
        if self.finished {
            return Ok(None);
        }
        match self.events.recv().await {
            Some(Event::Message(message)) => Ok(Some(message)),
            Some(Event::Closed(reason)) => {
                self.finished = true;
                match reason {
                    Closed::Eof | Closed::Terminated => Ok(None),
                    reason => {
                        // Already killed on Unix, where the reader signals
                        // the group as soon as the stream fails. This covers
                        // the platforms that cannot.
                        let _ = self.child.start_kill();
                        Err(self.handle.shared.error_for(&reason, None))
                    }
                }
            }
            None => {
                self.finished = true;
                Ok(None)
            }
        }
    }

    /// End the session and wait for the server to exit.
    ///
    /// Closes the server's stdin and waits up to the client's termination
    /// grace for it to exit on its own. If it does not, this falls back to
    /// [`terminate`](Self::terminate) and returns `Ok`, since the caller asked
    /// for the end.
    ///
    /// # Errors
    ///
    /// If the server exits with a non-zero status by itself, the failure is
    /// classified from its stderr like any failed command
    /// ([`Error::CommandFailed`] and its refinements).
    pub async fn shutdown(mut self) -> Result<()> {
        // The child is about to be reaped, after which its pid is not ours to
        // signal.
        self.handle.shared.revoke_kill();
        let _ = self.handle.writer.send(Outgoing::Close);
        match tokio::time::timeout(self.termination_grace, self.child.wait()).await {
            Ok(Ok(status)) => {
                self.group.disarm();
                self.handle.shared.stderr_settled().await;
                self.handle.shared.fail(Closed::Terminated);
                let code = status.code().unwrap_or(-1);
                if status.success() {
                    self.outcome.settle("ok", Some(code));
                    return Ok(());
                }
                self.outcome.settle("failed", Some(code));
                Err(Error::from_command_failure(
                    self.command.clone(),
                    code,
                    String::new(),
                    self.handle.shared.stderr_summary(),
                    self.working_dir.clone(),
                ))
            }
            Ok(Err(error)) => Err(Error::Io {
                message: format!("failed to wait on codex app-server: {error}"),
                source: error,
                working_dir: self.working_dir.clone(),
            }),
            Err(_) => self.terminate().await,
        }
    }

    /// Stop the server and its process group, and wait until it is gone.
    ///
    /// Sends the client's configured termination signal to the group,
    /// waits through the client's termination grace,
    /// kills what remains, and reaps the child. Requests still waiting fail.
    ///
    /// Shell commands the server started run in groups of their own and are
    /// not reached by group signaling. Cooperative cleanup depends on the
    /// server; see the [module documentation](self#lifecycle-and-cancellation).
    /// [`shutdown`](Self::shutdown) is the way to stop those.
    ///
    /// # Errors
    ///
    /// [`Error::Io`] if the child cannot be waited on.
    pub async fn terminate(mut self) -> Result<()> {
        self.handle.shared.revoke_kill();
        self.handle.shared.fail(Closed::Terminated);
        let result = crate::exec::terminate_and_reap(
            &mut self.child,
            &mut self.group,
            self.termination_grace,
            self.working_dir.as_deref(),
        )
        .await;
        self.outcome.settle("cancelled", None);
        result
    }

    /// Clean up after a failed handshake and choose the error to report.
    async fn abandon(mut self, error: Error) -> Error {
        self.handle.shared.revoke_kill();
        let outcome = if matches!(error, Error::Timeout { .. }) {
            "timeout"
        } else {
            "failed"
        };
        // Only a server that closed its own output has failed by itself. When
        // the reader gave up on the stream it killed the child, so the exit
        // status says nothing and the error already says what happened.
        let by_server = matches!(locked(&self.handle.shared.state).closed, Some(Closed::Eof));

        // Has the child already ended? One that closed its output is given a
        // moment to finish exiting. Otherwise only look, so a server that is
        // merely silent is not waited on.
        let ended = if by_server {
            tokio::time::timeout(STARTUP_EXIT_WAIT, self.child.wait())
                .await
                .ok()
        } else {
            self.child.try_wait().transpose()
        };
        if let Some(Ok(status)) = ended {
            // Reaped: nothing is left to terminate, and its pid is no longer
            // ours to signal.
            self.group.disarm();
            self.handle.shared.stderr_settled().await;
            let code = status.code().unwrap_or(-1);
            self.outcome.settle(outcome, Some(code));
            if by_server && !status.success() {
                // A server that rejects its arguments or config exits before
                // it can answer. Its exit status and stderr say more than
                // "closed its output".
                return Error::from_command_failure(
                    self.command.clone(),
                    code,
                    String::new(),
                    self.handle.shared.stderr_summary(),
                    self.working_dir.clone(),
                );
            }
            return error;
        }

        self.handle.shared.fail(Closed::Terminated);
        let _ = crate::exec::terminate_and_reap(
            &mut self.child,
            &mut self.group,
            self.termination_grace,
            self.working_dir.as_deref(),
        )
        .await;
        self.outcome.settle(outcome, None);
        error
    }
}

impl Drop for AppServer {
    fn drop(&mut self) {
        // The fields that follow kill the group and let tokio reap the child.
        // After that the reader task must not signal the pid.
        self.handle.shared.revoke_kill();
    }
}

#[cfg(all(test, unix))]
mod tests {
    use super::*;
    use crate::CodexBuilder;
    use crate::test_support::{PidFile, is_running_for_test, wait_until_gone};

    fn fake(mode: &str) -> CodexBuilder {
        let script = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("tests")
            .join("fake-app-server.sh");
        Codex::builder()
            .binary("/bin/bash")
            .arg(script.to_str().expect("fixture path is utf-8"))
            .env("FAKE_APP_SERVER_MODE", mode)
            // Terminating always waits out the grace, so keep it short.
            .termination_grace(Duration::from_millis(100))
            // A lost answer should fail a test after this, not hang it.
            .timeout(Duration::from_secs(20))
    }

    async fn started(mode: &str) -> AppServer {
        let codex = fake(mode).build().expect("bash must exist");
        AppServer::builder(&codex)
            .start()
            .await
            .expect("the fake server starts")
    }

    /// Read messages until `turn/completed`, returning it and everything seen.
    async fn until_turn_completed(server: &mut AppServer) -> (TurnCompleted, Vec<ServerMessage>) {
        let mut seen = Vec::new();
        loop {
            let message = tokio::time::timeout(Duration::from_secs(10), server.next_message())
                .await
                .expect("the turn should finish")
                .expect("no stream error")
                .expect("the server should not close first");
            let done = match &message {
                ServerMessage::Notification(n) => n.turn_completed(),
                _ => None,
            };
            seen.push(message);
            if let Some(done) = done {
                return (done, seen);
            }
        }
    }

    fn methods(seen: &[ServerMessage]) -> Vec<&str> {
        seen.iter()
            .map(|message| match message {
                ServerMessage::Notification(n) => n.method.as_str(),
                ServerMessage::Request(r) => r.method.as_str(),
            })
            .collect()
    }

    #[tokio::test]
    async fn the_handshake_reports_the_server_and_delivers_unsolicited_notifications() {
        let mut server = started("turn").await;
        assert!(server.server_info().user_agent.contains("0.149.0"));
        assert_eq!(
            server.server_info().codex_home.as_deref(),
            Some("/home/user/.codex")
        );

        // The real server sends this right after `initialized`, unasked.
        let first = server.next_message().await.unwrap().unwrap();
        let ServerMessage::Notification(notification) = first else {
            panic!("expected a notification, got {first:?}");
        };
        assert_eq!(notification.method, "remoteControl/status/changed");
        assert_eq!(notification.params["status"], "disabled");
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn a_turn_runs_to_completion_and_yields_its_final_message() {
        let mut server = started("turn").await;
        let thread = server
            .thread_start(
                ThreadStartParams::new()
                    .cwd("/tmp/fixture")
                    .ephemeral(true)
                    .approval_policy(ApprovalPolicy::Never)
                    .sandbox(SandboxMode::ReadOnly),
            )
            .await
            .unwrap();
        assert_eq!(thread.id, "01a0d1a2-5f2e-7263-a97b-cabf3e78caff");
        assert_eq!(thread.model.as_deref(), Some("gpt-5.6-sol"));

        let turn = server
            .turn_start(TurnStartParams::text(&thread.id, "say ok"))
            .await
            .unwrap();
        assert_eq!(turn.status, TurnStatus::InProgress);
        assert_eq!(turn.id, "01a0d1a2-5f9f-75c1-9305-fa382e7b37bc");

        let (done, seen) = until_turn_completed(&mut server).await;
        assert_eq!(done.thread_id, thread.id);
        assert_eq!(done.turn.id, turn.id);
        assert_eq!(done.turn.status, TurnStatus::Completed);
        assert_eq!(done.turn.final_message(), Some("ok"));
        let methods = methods(&seen);
        assert!(methods.contains(&"thread/started"));
        assert!(methods.contains(&"turn/started"));
        assert!(methods.contains(&"item/agentMessage/delta"));
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn steering_adds_input_to_the_running_turn() {
        let mut server = started("held").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        let turn = server
            .turn_start(TurnStartParams::text(&thread.id, "sleep, then say DONE"))
            .await
            .unwrap();

        let steered = server
            .turn_steer(
                &thread.id,
                &turn.id,
                vec![UserInput::text("Also append STEERED")],
            )
            .await
            .unwrap();
        assert_eq!(steered, turn.id);

        let (done, seen) = until_turn_completed(&mut server).await;
        assert_eq!(done.turn.final_message(), Some("DONE STEERED"));
        // The fixture echoes the steered text as a user message, so this shows
        // the text reached the server.
        let echoed = seen.iter().any(|message| match message {
            ServerMessage::Notification(n) => {
                n.method == "item/completed"
                    && n.params.pointer("/item/content/0/text")
                        == Some(&json!("Also append STEERED"))
            }
            _ => false,
        });
        assert!(echoed, "the server never saw the steered text: {seen:?}");
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn steering_from_another_task_while_events_are_read() {
        let mut server = started("held").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        let turn = server
            .turn_start(TurnStartParams::text(&thread.id, "go"))
            .await
            .unwrap();

        let handle = server.handle();
        let (thread_id, turn_id) = (thread.id.clone(), turn.id.clone());
        let steer = tokio::spawn(async move {
            handle
                .turn_steer(&thread_id, &turn_id, vec![UserInput::text("more")])
                .await
        });

        let (done, _) = until_turn_completed(&mut server).await;
        assert_eq!(done.turn.status, TurnStatus::Completed);
        assert_eq!(steer.await.unwrap().unwrap(), turn.id);
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn steering_the_wrong_turn_is_an_rpc_error_that_names_both_ids() {
        let server = started("held").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        let turn = server
            .turn_start(TurnStartParams::text(&thread.id, "go"))
            .await
            .unwrap();

        let error = server
            .turn_steer(&thread.id, "not-the-turn", vec![UserInput::text("x")])
            .await
            .unwrap_err();
        match error {
            Error::AppServerRpc {
                method,
                code,
                message,
                ..
            } => {
                assert_eq!(method, "turn/steer");
                assert_eq!(code, -32600);
                assert_eq!(
                    message,
                    format!(
                        "expected active turn id `not-the-turn` but found `{}`",
                        turn.id
                    )
                );
            }
            other => panic!("expected an rpc error, got {other:?}"),
        }

        let error = server
            .turn_interrupt(&thread.id, "not-the-turn")
            .await
            .unwrap_err();
        assert!(
            matches!(&error, Error::AppServerRpc { message, .. }
                if message == &format!("expected active turn id not-the-turn but found {}", turn.id)),
            "{error:?}"
        );
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn steering_or_interrupting_with_no_active_turn_is_an_rpc_error() {
        let server = started("held").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();

        let error = server
            .turn_steer(&thread.id, "any", vec![UserInput::text("x")])
            .await
            .unwrap_err();
        assert!(
            matches!(&error, Error::AppServerRpc { message, .. } if message == "no active turn to steer"),
            "{error:?}"
        );
        let error = server.turn_interrupt(&thread.id, "any").await.unwrap_err();
        assert!(
            matches!(&error, Error::AppServerRpc { message, .. } if message == "no active turn to interrupt"),
            "{error:?}"
        );
        server.shutdown().await.unwrap();
    }

    /// codex-cli 0.145.0 sends `items: []` (`itemsView: notLoaded`) in
    /// `turn/completed` even when the turn completed, so the answer has to come
    /// from the `item/completed` stream.
    #[tokio::test]
    async fn the_final_answer_is_available_when_turn_completed_carries_no_items() {
        let codex = fake("turn")
            .env("FAKE_APP_SERVER_ITEMS", "legacy")
            .build()
            .unwrap();
        let mut server = AppServer::builder(&codex).start().await.unwrap();
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        server
            .turn_start(TurnStartParams::text(&thread.id, "say ok"))
            .await
            .unwrap();

        let (done, seen) = until_turn_completed(&mut server).await;
        assert_eq!(done.turn.status, TurnStatus::Completed);
        assert!(done.turn.items.is_empty());
        assert_eq!(done.turn.final_message(), None);
        let answer = seen
            .iter()
            .filter_map(|message| match message {
                ServerMessage::Notification(n) => n.agent_message(),
                _ => None,
            })
            .rfind(AgentMessage::is_final_answer)
            .expect("the item stream carries the answer");
        assert_eq!(answer.text, "ok");
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn interrupting_ends_the_turn_as_interrupted_with_no_final_message() {
        let mut server = started("held").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        let turn = server
            .turn_start(TurnStartParams::text(&thread.id, "go"))
            .await
            .unwrap();

        server.turn_interrupt(&thread.id, &turn.id).await.unwrap();

        let (done, _) = until_turn_completed(&mut server).await;
        assert_eq!(done.turn.status, TurnStatus::Interrupted);
        assert!(done.turn.items.is_empty());
        assert_eq!(done.turn.final_message(), None);
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn a_server_request_is_delivered_and_the_reply_reaches_the_server() {
        let mut server = started("approval").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        server
            .turn_start(TurnStartParams::text(&thread.id, "touch a file"))
            .await
            .unwrap();

        let mut seen = Vec::new();
        let request = loop {
            let message = tokio::time::timeout(Duration::from_secs(10), server.next_message())
                .await
                .unwrap()
                .unwrap()
                .unwrap();
            if let ServerMessage::Request(request) = message {
                break request;
            }
            seen.push(message);
        };
        assert_eq!(request.method, "item/commandExecution/requestApproval");
        assert_eq!(request.id, RequestId::Number(0));
        assert_eq!(
            request.params["command"],
            "/bin/zsh -lc 'touch created.txt'"
        );

        server
            .respond(request.id, json!({ "decision": "decline" }))
            .unwrap();

        let (done, after) = until_turn_completed(&mut server).await;
        // The fixture reports the decision it received.
        assert_eq!(done.turn.final_message(), Some("decision: decline"));
        assert!(methods(&after).contains(&"serverRequest/resolved"));
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn concurrent_requests_are_matched_to_their_answers_by_id() {
        let server = started("turn").await;
        let (first, second, unknown) = tokio::join!(
            server.thread_start(ThreadStartParams::new()),
            server.request("thread/start", json!({})),
            server.request("no/such/method", json!({})),
        );
        assert!(first.is_ok());
        assert!(second.unwrap().pointer("/thread/id").is_some());
        match unknown.unwrap_err() {
            Error::AppServerRpc { message, .. } => {
                assert!(
                    message.contains("unknown variant `no/such/method`"),
                    "{message}"
                );
            }
            other => panic!("expected an rpc error, got {other:?}"),
        }
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn a_second_initialize_is_rejected_by_the_server() {
        let server = started("turn").await;
        let error = server
            .request(
                "initialize",
                json!({ "clientInfo": { "name": "x", "version": "0" } }),
            )
            .await
            .unwrap_err();
        assert!(
            matches!(&error, Error::AppServerRpc { message, .. } if message == "Already initialized"),
            "{error:?}"
        );
        server.shutdown().await.unwrap();
    }

    /// What the client puts on the wire, read back from the fixture's log.
    #[tokio::test]
    async fn requests_are_written_as_the_server_expects_them() {
        let log = std::env::temp_dir().join(format!(
            "codex-wrapper-{}-app-server-wire.log",
            std::process::id()
        ));
        let _ = std::fs::remove_file(&log);
        let codex = fake("held")
            .env("FAKE_APP_SERVER_LOG", log.to_str().unwrap())
            .build()
            .unwrap();
        let mut server = AppServer::builder(&codex)
            .client_info("probe", "1.2.3")
            .opt_out_notification("item/agentMessage/delta")
            .opt_out_notification("mcpServer/startupStatus/updated")
            .start()
            .await
            .unwrap();
        let thread = server
            .thread_start(
                ThreadStartParams::new()
                    .cwd("/tmp/fixture")
                    .model("gpt-5.6-sol")
                    .approval_policy(ApprovalPolicy::Never)
                    .sandbox(SandboxMode::ReadOnly)
                    .ephemeral(true),
            )
            .await
            .unwrap();
        let schema = json!({ "type": "object", "required": ["answer"] });
        let turn = server
            .turn_start(
                TurnStartParams::text(&thread.id, "hi")
                    .model("gpt-5.6-sol")
                    .effort("low")
                    .output_schema(schema.clone())
                    .cwd("/tmp/fixture")
                    .approval_policy(ApprovalPolicy::Never)
                    .param("summary", json!("none")),
            )
            .await
            .unwrap();
        server
            .turn_steer(&thread.id, &turn.id, vec![UserInput::text("more")])
            .await
            .unwrap();
        let _ = until_turn_completed(&mut server).await;
        server.shutdown().await.unwrap();

        let lines: Vec<Value> = std::fs::read_to_string(&log)
            .unwrap()
            .lines()
            .map(|line| serde_json::from_str(line).expect("every client line is JSON"))
            .collect();
        let _ = std::fs::remove_file(&log);

        assert_eq!(
            lines[0],
            json!({
                "id": 0,
                "method": "initialize",
                "params": {
                    "clientInfo": { "name": "probe", "version": "1.2.3" },
                    "capabilities": {
                        "optOutNotificationMethods": [
                            "item/agentMessage/delta",
                            "mcpServer/startupStatus/updated"
                        ]
                    }
                }
            })
        );
        assert_eq!(lines[1], json!({ "method": "initialized" }));
        assert_eq!(
            lines[2],
            json!({
                "id": 1,
                "method": "thread/start",
                "params": {
                    "cwd": "/tmp/fixture",
                    "model": "gpt-5.6-sol",
                    "approvalPolicy": "never",
                    "sandbox": "read-only",
                    "ephemeral": true
                }
            })
        );
        assert_eq!(
            lines[3],
            json!({
                "id": 2,
                "method": "turn/start",
                "params": {
                    "threadId": thread.id,
                    "input": [{ "type": "text", "text": "hi", "text_elements": [] }],
                    "model": "gpt-5.6-sol",
                    "effort": "low",
                    "outputSchema": schema,
                    "cwd": "/tmp/fixture",
                    "approvalPolicy": "never",
                    "summary": "none"
                }
            })
        );
        assert_eq!(
            lines[4],
            json!({
                "id": 3,
                "method": "turn/steer",
                "params": {
                    "threadId": thread.id,
                    "expectedTurnId": turn.id,
                    "input": [{ "type": "text", "text": "more", "text_elements": [] }]
                }
            })
        );
    }

    /// A server that exits 0 before it answers has been reaped by the time the
    /// failure is reported, so there is nothing left to terminate. Waiting out
    /// the grace period (and signalling a pid that is no longer ours) is the
    /// failure this guards.
    #[tokio::test]
    async fn a_server_that_exits_cleanly_before_answering_fails_the_start_without_waiting() {
        let codex = fake("exit-clean")
            .termination_grace(Duration::from_secs(4))
            .build()
            .unwrap();
        let started_at = std::time::Instant::now();
        let error = AppServer::builder(&codex).start().await.unwrap_err();
        assert!(
            matches!(error, Error::AppServerProtocol { .. }),
            "{error:?}"
        );
        assert!(
            started_at.elapsed() < Duration::from_secs(3),
            "start waited out the grace period for a server that had already exited"
        );
    }

    /// The reader kills the server when the stream breaks. That must not be
    /// mistaken for the server failing by itself, or the useful message is
    /// replaced by an exit status.
    #[tokio::test]
    async fn invalid_json_during_the_handshake_is_reported_as_invalid_json() {
        let codex = fake("bad-init").build().unwrap();
        let error = AppServer::builder(&codex).start().await.unwrap_err();
        assert!(
            matches!(&error, Error::AppServerProtocol { message } if message.contains("invalid JSON")),
            "{error:?}"
        );
        assert_eq!(error.exit_code(), None);
    }

    /// A response that names a waiting request but has neither `result` nor
    /// `error` is not a valid answer. Ignoring it would leave the request
    /// waiting for one that will never come.
    #[tokio::test]
    async fn a_response_with_neither_result_nor_error_fails_the_request_it_names() {
        let server = started("no-result").await;
        let error = server
            .thread_start(ThreadStartParams::new())
            .await
            .unwrap_err();
        assert!(
            matches!(&error, Error::AppServerProtocol { message }
                if message.contains("neither result nor error") && message.contains("thread/start")),
            "{error:?}"
        );
        // The session itself is still usable.
        assert!(server.notify("initialized", Value::Null).is_ok());
        server.shutdown().await.unwrap();
    }

    /// The reader task holds a way to kill the server. It must stop being able
    /// to once the client is done with the child: after that the pid may
    /// belong to someone else.
    #[tokio::test]
    async fn the_readers_kill_switch_is_revoked_when_the_client_is_done_with_the_child() {
        // Never signals this process: the target is a `sleep` started here.
        let sleeper = |seconds: &str| {
            std::process::Command::new("sleep")
                .arg(seconds)
                .spawn()
                .unwrap()
        };
        let (events, _received) = mpsc::unbounded_channel();

        let mut live = sleeper("60");
        let shared = Shared::new(
            events.clone(),
            Some(KillTarget {
                pid: Some(live.id()),
                group: false,
            }),
        );
        shared.fire_kill();
        assert!(
            live.wait().unwrap().code().is_none(),
            "the switch should have killed the process it was armed with"
        );

        let mut revoked = sleeper("60");
        let shared = Shared::new(
            events,
            Some(KillTarget {
                pid: Some(revoked.id()),
                group: false,
            }),
        );
        shared.revoke_kill();
        shared.fire_kill();
        std::thread::sleep(Duration::from_millis(200));
        assert!(
            revoked.try_wait().unwrap().is_none(),
            "a revoked switch still killed the process"
        );
        revoked.kill().unwrap();
        revoked.wait().unwrap();
    }

    #[tokio::test]
    async fn every_way_of_ending_the_session_revokes_the_kill_switch() {
        let armed = |shared: &Shared| locked(&shared.kill).is_some();

        let server = started("turn").await;
        let shared = Arc::clone(&server.handle.shared);
        assert!(armed(&shared));
        server.shutdown().await.unwrap();
        assert!(!armed(&shared), "shutdown left the switch armed");

        let server = started("turn").await;
        let shared = Arc::clone(&server.handle.shared);
        server.terminate().await.unwrap();
        assert!(!armed(&shared), "terminate left the switch armed");

        let server = started("turn").await;
        let shared = Arc::clone(&server.handle.shared);
        drop(server);
        assert!(!armed(&shared), "dropping left the switch armed");
    }

    #[test]
    fn the_previewed_command_includes_the_clients_global_args() {
        let codex = Codex::builder()
            .binary("/bin/echo")
            .config("model=\"o3\"")
            .build()
            .unwrap();
        let builder = AppServer::builder(&codex);
        assert_eq!(builder.args(), ["app-server", "--listen", "stdio://"]);
        assert_eq!(
            builder.to_command_string(),
            "/bin/echo -c 'model=\"o3\"' app-server --listen stdio://"
        );
    }

    #[tokio::test]
    async fn a_silent_server_times_out_the_handshake_and_is_stopped() {
        let pid = Arc::new(Mutex::new(None));
        let observed = Arc::clone(&pid);
        let codex = fake("silent")
            .timeout(Duration::from_millis(300))
            .on_spawn(Arc::new(move |info| {
                *observed.lock().unwrap() = Some(info.pid);
            }))
            .build()
            .unwrap();

        let error = AppServer::builder(&codex).start().await.unwrap_err();
        assert!(matches!(error, Error::Timeout { .. }), "{error:?}");

        let pid = pid.lock().unwrap().expect("the server was spawned");
        assert!(
            wait_until_gone(pid).await,
            "the silent server ({pid}) survived a failed start"
        );
    }

    #[tokio::test]
    async fn a_server_that_exits_at_startup_is_reported_with_its_stderr() {
        let codex = fake("exit-on-start").build().unwrap();
        let error = AppServer::builder(&codex).start().await.unwrap_err();
        assert_eq!(error.exit_code(), Some(1), "{error:?}");
        assert_eq!(
            error.failure_kind(),
            Some(crate::FailureKind::Config),
            "{error:?}"
        );
        assert!(error.to_string().contains("bogus_key"), "{error}");
    }

    /// A server that dies at startup can be noticed by the client's write (the
    /// pipe is closed, EPIPE) before its read (the stream has ended). Either
    /// way the server went away by itself, and what says why is its stderr and
    /// exit status, which the reader reports at the end of the stream. A write
    /// error must not become the session's failure and hide that; this was
    /// seen as a flaky failure on Linux CI.
    ///
    /// An in-memory pipe stands in for the child's stdin. A real one made this
    /// test hang under load: on macOS a pipe's close-on-exec flag is set after
    /// it is created, so a process forked by a concurrent test can inherit the
    /// read end, and then the write never fails.
    #[tokio::test]
    async fn a_broken_stdin_pipe_leaves_the_verdict_to_the_reader() {
        let (stdin, server_end) = tokio::io::duplex(64);
        drop(server_end);

        let (events, _received) = mpsc::unbounded_channel();
        let shared = Arc::new(Shared::new(events, None));
        let (lines, receiver) = mpsc::unbounded_channel();
        lines.send(Outgoing::Line(b"{}\n".to_vec())).unwrap();

        // Ends by itself when the write fails. If it did not fail, `lines` is
        // still open and this would wait forever, so bound it.
        tokio::time::timeout(
            Duration::from_secs(10),
            write_stdin(stdin, receiver, Arc::clone(&shared)),
        )
        .await
        .expect("the write to a closed pipe did not fail");

        assert!(
            locked(&shared.state).closed.is_none(),
            "a write to a closed pipe was recorded as the session's failure"
        );
        drop(lines);
    }

    /// End to end: a server that closes its input and then fails is reported
    /// by its exit status and stderr. The race that once made this flaky on
    /// Linux CI is pinned by `a_broken_stdin_pipe_leaves_the_verdict_to_the_reader`.
    #[tokio::test]
    async fn a_server_that_closes_its_input_and_then_fails_is_reported_by_its_exit_status() {
        let codex = fake("close-stdin").build().unwrap();
        let error = AppServer::builder(&codex).start().await.unwrap_err();
        assert_eq!(error.exit_code(), Some(1), "{error:?}");
        assert_eq!(
            error.failure_kind(),
            Some(crate::FailureKind::Config),
            "{error:?}"
        );
        assert!(error.to_string().contains("bogus_key"), "{error}");
    }

    /// The server writes about 500 KB to stderr before its first answer. An
    /// undrained pipe holds about 64 KB, so this hangs if stderr is not read.
    #[tokio::test]
    async fn heavy_stderr_does_not_stall_the_session() {
        let codex = fake("noisy-stderr").build().unwrap();
        let server =
            tokio::time::timeout(Duration::from_secs(20), AppServer::builder(&codex).start())
                .await
                .expect("the handshake stalled behind an undrained stderr pipe")
                .unwrap();
        server.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn a_server_that_exits_mid_request_fails_it_with_the_stderr() {
        let mut server = started("exit-on-turn").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        let error = server
            .turn_start(TurnStartParams::text(&thread.id, "go"))
            .await
            .unwrap_err();
        let text = error.to_string();
        assert!(text.contains("turn/start"), "{text}");
        assert!(
            text.contains("connection to the model provider was lost"),
            "{text}"
        );

        // Earlier messages are still delivered, then the stream ends cleanly.
        while server.next_message().await.unwrap().is_some() {}
        assert!(server.next_message().await.unwrap().is_none());

        // New requests fail at once instead of waiting for a timeout.
        let again = server.request("thread/start", json!({})).await.unwrap_err();
        assert!(
            matches!(again, Error::AppServerProtocol { .. }),
            "{again:?}"
        );

        // The exit status is reported by shutdown.
        let error = server.shutdown().await.unwrap_err();
        assert_eq!(error.exit_code(), Some(5), "{error:?}");
    }

    #[tokio::test]
    async fn the_output_limit_ends_the_session_and_kills_the_server() {
        let pid = Arc::new(Mutex::new(None));
        let observed = Arc::clone(&pid);
        let codex = fake("flood")
            .output_limit(2048)
            .on_spawn(Arc::new(move |info| {
                *observed.lock().unwrap() = Some(info.pid);
            }))
            .build()
            .unwrap();
        let mut server = AppServer::builder(&codex).start().await.unwrap();

        let error = loop {
            match server.next_message().await {
                Ok(Some(_)) => {}
                Ok(None) => panic!("the limit error was swallowed"),
                Err(error) => break error,
            }
        };
        assert!(
            matches!(
                error,
                Error::OutputLimitExceeded {
                    stream: OutputStream::Stdout,
                    limit_bytes: 2048
                }
            ),
            "{error:?}"
        );
        assert!(server.next_message().await.unwrap().is_none());

        let pid = pid.lock().unwrap().expect("the server was spawned");
        assert!(
            wait_until_gone(pid).await,
            "the server ({pid}) kept running after the output limit"
        );
    }

    #[tokio::test]
    async fn invalid_json_ends_the_session() {
        let mut server = started("bad-json").await;
        let error = loop {
            match server.next_message().await {
                Ok(Some(_)) => {}
                Ok(None) => panic!("the malformed line was swallowed"),
                Err(error) => break error,
            }
        };
        assert!(
            matches!(&error, Error::AppServerProtocol { message } if message.contains("invalid JSON")),
            "{error:?}"
        );
    }

    #[tokio::test]
    async fn terminate_stops_the_whole_process_group() {
        let pid_file = PidFile::new("app-server-terminate");
        let codex = fake("spawns-child")
            .env(
                "CODEX_WRAPPER_TEST_PIDFILE",
                pid_file.path().to_str().unwrap(),
            )
            // Long enough for the fixture's SIGTERM handler to run on a
            // loaded machine, so the marker below means what it says.
            .termination_grace(Duration::from_secs(1))
            .build()
            .unwrap();
        let server = AppServer::builder(&codex).start().await.unwrap();
        let (parent, child) = read_pids(&pid_file).await;
        assert!(is_running_for_test(parent) && is_running_for_test(child));

        server.terminate().await.unwrap();

        // The direct child has been reaped. Its subprocess got SIGKILL at the
        // same moment, but nothing waits for it, so allow it a moment to die.
        assert!(
            wait_until_gone(parent).await,
            "the server ({parent}) survived"
        );
        assert!(
            wait_until_gone(child).await,
            "the server's subprocess ({child}) survived terminate"
        );
        // The fixture writes this marker only if it is asked to stop with
        // SIGTERM first, which is what separates terminate from a kill.
        let marker = term_marker(&pid_file);
        assert!(
            marker.exists(),
            "terminate killed the server without asking it to stop first"
        );
        let _ = std::fs::remove_file(marker);
    }

    #[tokio::test]
    async fn dropping_during_app_server_termination_grace_kills_the_group() {
        let fixture = crate::test_support::GracePeriodFixture::new("app-server-grace-drop");
        let codex = fixture.configure(fake("ignores-eof")).build().unwrap();
        let server = AppServer::builder(&codex).start().await.unwrap();
        fixture.assert_drop_during_grace(server.terminate()).await;
    }

    #[tokio::test]
    async fn dropping_the_client_kills_the_process_group() {
        let pid_file = PidFile::new("app-server-drop");
        let codex = fake("spawns-child")
            .env(
                "CODEX_WRAPPER_TEST_PIDFILE",
                pid_file.path().to_str().unwrap(),
            )
            .build()
            .unwrap();
        let server = AppServer::builder(&codex).start().await.unwrap();
        let (parent, child) = read_pids(&pid_file).await;

        drop(server);

        assert!(
            wait_until_gone(parent).await,
            "the server ({parent}) survived"
        );
        assert!(
            wait_until_gone(child).await,
            "the server's subprocess ({child}) survived the drop"
        );
        // Dropping is the abrupt path: no SIGTERM, so no marker.
        assert!(!term_marker(&pid_file).exists());
    }

    #[tokio::test]
    async fn shutdown_lets_a_server_that_exits_on_eof_finish_by_itself() {
        // A fallback to terminate would wait out the whole grace, so a long
        // grace makes the two paths easy to tell apart without a tight bound.
        let codex = fake("turn")
            .termination_grace(Duration::from_secs(4))
            .build()
            .unwrap();
        let server = AppServer::builder(&codex).start().await.unwrap();
        let pid = server.pid().expect("running");
        let started_at = std::time::Instant::now();
        server.shutdown().await.unwrap();
        assert!(
            started_at.elapsed() < Duration::from_secs(3),
            "shutdown waited out the grace instead of seeing the exit"
        );
        assert!(wait_until_gone(pid).await);
    }

    #[tokio::test]
    async fn shutdown_falls_back_to_termination_when_the_server_ignores_eof() {
        let pid_file = PidFile::new("app-server-ignores-eof");
        let codex = fake("ignores-eof")
            .env(
                "CODEX_WRAPPER_TEST_PIDFILE",
                pid_file.path().to_str().unwrap(),
            )
            // Long enough for the fixture's SIGTERM handler to run on a
            // loaded machine, so the marker below means what it says.
            .termination_grace(Duration::from_secs(1))
            .build()
            .unwrap();
        let server = AppServer::builder(&codex).start().await.unwrap();
        let (parent, child) = read_pids(&pid_file).await;

        server.shutdown().await.unwrap();

        // Asked to stop before it was killed, not just killed.
        let marker = term_marker(&pid_file);
        assert!(
            marker.exists(),
            "shutdown killed the server without asking it to stop"
        );
        let _ = std::fs::remove_file(marker);

        assert!(
            wait_until_gone(parent).await,
            "the server ({parent}) survived"
        );
        assert!(
            wait_until_gone(child).await,
            "the server's subprocess ({child}) survived shutdown"
        );
    }

    #[tokio::test]
    async fn requests_after_shutdown_fail_on_a_surviving_handle() {
        let server = started("turn").await;
        let handle = server.handle();
        server.shutdown().await.unwrap();
        let error = handle.request("thread/start", json!({})).await.unwrap_err();
        assert!(
            matches!(error, Error::AppServerProtocol { .. }),
            "{error:?}"
        );
        assert!(handle.notify("initialized", Value::Null).is_err());
    }

    fn term_marker(pid_file: &PidFile) -> std::path::PathBuf {
        let mut path = pid_file.path().as_os_str().to_owned();
        path.push(".term");
        std::path::PathBuf::from(path)
    }

    async fn read_pids(pid_file: &PidFile) -> (u32, u32) {
        // Up to ten seconds: starting bash can be slow on a loaded machine.
        for _ in 0..1000 {
            if let Ok(contents) = std::fs::read_to_string(pid_file.path()) {
                let field = |key: &str| {
                    contents
                        .lines()
                        .find_map(|line| line.strip_prefix(key))
                        .and_then(|value| value.trim().parse::<u32>().ok())
                };
                if let (Some(parent), Some(child)) = (field("parent="), field("child=")) {
                    return (parent, child);
                }
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        panic!("the fake server never recorded its pids");
    }

    #[tokio::test]
    async fn a_request_that_times_out_leaves_the_session_usable_and_no_entry_behind() {
        // The short timeout is for the requests below, not the handshake,
        // which has to be allowed to take as long as a loaded machine needs.
        let server = started("mute-after-init").await;
        let mut handle = server.handle();
        handle.timeout = Some(Duration::from_millis(150));

        for _ in 0..2 {
            let error = handle.request("thread/start", json!({})).await.unwrap_err();
            assert!(matches!(error, Error::Timeout { .. }), "{error:?}");
        }
        assert!(
            locked(&handle.shared.state).pending.is_empty(),
            "a timed-out request left its slot behind"
        );
        // A timeout is a verdict on one request, not on the session.
        assert!(handle.notify("initialized", Value::Null).is_ok());
        server.terminate().await.unwrap();
    }

    /// A request future that is dropped after it has queued its line must not
    /// leave half a line behind: the next request still gets a clean answer,
    /// and every line the server received is whole JSON.
    #[tokio::test]
    async fn dropping_a_request_future_does_not_corrupt_the_wire() {
        let log = std::env::temp_dir().join(format!(
            "codex-wrapper-{}-app-server-dropped.log",
            std::process::id()
        ));
        let _ = std::fs::remove_file(&log);
        let codex = fake("turn")
            .env("FAKE_APP_SERVER_LOG", log.to_str().unwrap())
            .build()
            .unwrap();
        let server = AppServer::builder(&codex).start().await.unwrap();

        for _ in 0..20 {
            // Poll once, so the line is queued, then drop the future before
            // any answer can be read.
            let request = server.request("thread/start", json!({ "ephemeral": true }));
            let mut request = std::pin::pin!(request);
            let mut context = std::task::Context::from_waker(std::task::Waker::noop());
            assert!(request.as_mut().poll(&mut context).is_pending());
        }
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        assert!(!thread.id.is_empty());
        server.shutdown().await.unwrap();

        let text = std::fs::read_to_string(&log).unwrap();
        let _ = std::fs::remove_file(&log);
        let mut count = 0;
        for line in text.lines() {
            serde_json::from_str::<Value>(line).expect("the server received a partial line");
            count += 1;
        }
        // initialize, initialized, twenty abandoned requests, one that was awaited.
        assert_eq!(count, 23, "{text}");
    }

    #[tokio::test]
    async fn next_message_loses_nothing_when_its_future_is_dropped() {
        let mut server = started("turn").await;
        let thread = server.thread_start(ThreadStartParams::new()).await.unwrap();
        server
            .turn_start(TurnStartParams::text(&thread.id, "go"))
            .await
            .unwrap();

        // Poll with a timeout that keeps expiring, so the future is dropped
        // over and over while messages arrive. Bounded by time, not by the
        // number of polls, so a slow machine only makes it take longer.
        let mut seen = Vec::new();
        let mut finished = false;
        let deadline = std::time::Instant::now() + Duration::from_secs(20);
        while std::time::Instant::now() < deadline {
            if let Ok(next) =
                tokio::time::timeout(Duration::from_micros(200), server.next_message()).await
            {
                let message = next.unwrap().expect("the stream ended early");
                if let ServerMessage::Notification(n) = &message {
                    finished |= n.turn_completed().is_some();
                }
                seen.push(message);
            }
            if finished {
                break;
            }
        }
        assert!(
            finished,
            "never saw the turn complete: {:?}",
            methods(&seen)
        );
        let methods = methods(&seen);
        // Every message the fixture sends after `initialized`, in order.
        let expected = [
            "remoteControl/status/changed",
            "thread/started",
            "thread/status/changed",
            "turn/started",
            "item/started",
            "item/agentMessage/delta",
            "item/completed",
            "thread/tokenUsage/updated",
            "thread/status/changed",
            "turn/completed",
        ];
        assert_eq!(methods, expected);
        server.shutdown().await.unwrap();
    }

    #[test]
    fn turn_final_message_takes_the_last_final_answer_and_ignores_commentary() {
        let turn: Turn = serde_json::from_value(json!({
            "id": "t",
            "status": "completed",
            "items": [
                { "type": "agentMessage", "text": "working", "phase": "commentary" },
                { "type": "commandExecution", "text": "not a message", "phase": "final_answer" },
                { "type": "agentMessage", "text": "first", "phase": "final_answer" },
                { "type": "agentMessage", "text": "last", "phase": "final_answer" }
            ]
        }))
        .unwrap();
        assert_eq!(turn.final_message(), Some("last"));
    }

    #[test]
    fn agent_messages_are_decoded_from_item_completed_only() {
        let message = |phase: Option<&str>| Notification {
            method: "item/completed".into(),
            params: json!({
                "item": {
                    "type": "agentMessage",
                    "id": "m",
                    "text": "DONE STEERED",
                    "phase": phase,
                },
                "threadId": "th",
                "turnId": "tu",
            }),
        };
        let answer = message(Some("final_answer")).agent_message().unwrap();
        assert_eq!(answer.text, "DONE STEERED");
        assert_eq!(
            (answer.thread_id.as_str(), answer.turn_id.as_str()),
            ("th", "tu")
        );
        assert!(answer.is_final_answer());
        assert!(
            !message(Some("commentary"))
                .agent_message()
                .unwrap()
                .is_final_answer()
        );
        assert!(!message(None).agent_message().unwrap().is_final_answer());

        // Other items, other methods, and a delta are not agent messages.
        let mut command = message(None);
        command.params["item"]["type"] = json!("commandExecution");
        assert!(command.agent_message().is_none());
        let mut delta = message(None);
        delta.method = "item/agentMessage/delta".into();
        assert!(delta.agent_message().is_none());
    }

    #[test]
    fn an_unknown_turn_status_is_kept_apart_from_the_known_ones() {
        let turn: Turn =
            serde_json::from_value(json!({ "id": "t", "status": "someFutureStatus" })).unwrap();
        assert_eq!(turn.status, TurnStatus::Unknown);
    }

    #[test]
    fn only_a_well_formed_turn_completed_notification_decodes() {
        let other = Notification {
            method: "turn/started".into(),
            params: json!({}),
        };
        assert!(other.turn_completed().is_none());
        let malformed = Notification {
            method: "turn/completed".into(),
            params: json!({ "threadId": "x" }),
        };
        assert!(malformed.turn_completed().is_none());
    }

    #[test]
    fn request_ids_round_trip_as_numbers_and_strings() {
        assert_eq!(serde_json::to_string(&RequestId::Number(7)).unwrap(), "7");
        assert_eq!(
            serde_json::to_string(&RequestId::String("a".into())).unwrap(),
            "\"a\""
        );
        assert_eq!(
            serde_json::from_str::<RequestId>("0").unwrap(),
            RequestId::Number(0)
        );
        assert_eq!(
            serde_json::from_str::<RequestId>("\"x\"").unwrap(),
            RequestId::String("x".into())
        );
    }

    #[test]
    fn reply_lines_echo_the_servers_id() {
        let line = reply_line(
            &RequestId::Number(0),
            "result",
            &json!({ "decision": "decline" }),
        )
        .unwrap();
        assert_eq!(
            String::from_utf8(line).unwrap(),
            "{\"id\":0,\"result\":{\"decision\":\"decline\"}}\n"
        );
        let line = reply_line(
            &RequestId::String("r-1".into()),
            "error",
            &json!({ "code": -32601, "message": "no" }),
        )
        .unwrap();
        assert_eq!(
            String::from_utf8(line).unwrap(),
            "{\"id\":\"r-1\",\"error\":{\"code\":-32601,\"message\":\"no\"}}\n"
        );
    }

    #[test]
    fn colour_codes_are_removed_from_server_logs() {
        let raw = "\u{1b}[2m2026-09-24T04:17:33Z\u{1b}[0m \u{1b}[31mERROR\u{1b}[0m worker quit";
        assert_eq!(strip_ansi(raw), "2026-09-24T04:17:33Z ERROR worker quit");
    }

    #[test]
    fn a_line_that_is_not_a_message_is_ignored_and_bad_json_is_fatal() {
        let (events, mut received) = mpsc::unbounded_channel();
        let shared = Shared::new(events, None);
        assert!(route(r#"{"unrelated":true}"#, &shared).is_ok());
        assert!(
            route(
                r#"{"id":null,"error":{"code":-32600,"message":"x"}}"#,
                &shared
            )
            .is_ok()
        );
        assert!(route(r#"{"id":99,"result":{}}"#, &shared).is_ok());
        assert!(received.try_recv().is_err(), "nothing should be delivered");

        assert!(route(r#"{"method":"a/b","params":{"k":1}}"#, &shared).is_ok());
        assert!(matches!(
            received.try_recv(),
            Ok(Event::Message(ServerMessage::Notification(n))) if n.method == "a/b"
        ));
        assert!(route(r#"{"method":"a/b","id":"s"}"#, &shared).is_ok());
        assert!(matches!(
            received.try_recv(),
            Ok(Event::Message(ServerMessage::Request(r))) if r.id == RequestId::String("s".into())
        ));
        assert!(matches!(
            route(r#"{"method": oops"#, &shared),
            Err(Closed::Broken(_))
        ));
    }
}