lash-provider-openai 0.1.0-alpha.113

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

use async_trait::async_trait;
use futures_util::{SinkExt, StreamExt};
use serde::Deserialize;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::http::HeaderValue;
use tokio_tungstenite::tungstenite::protocol::Message as WsMessage;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async};

use crate::common::{DEFAULT_HTTP_TRANSPORT, DEFAULT_MAX_OUTPUT_TOKENS, reasoning_intent};
use crate::reasoning::ReasoningWireIntent;
use crate::responses_shared as shared;
use lash_core::llm::transport::{LlmTransportError, ProviderFailure, ProviderFailureKind};
use lash_core::llm::types::{
    LlmOutputSpec, LlmRequest, LlmResponse, LlmStreamEvent, LlmTerminalReason, LlmUsage,
};
use lash_core::provider::{
    CacheRetention, DefaultProviderFailureClassifier, Provider, ProviderComponents,
    ProviderFactory, ProviderFailureClassifier, ProviderOptions, ProviderReliability,
    StreamTermination, resolve_generation_policy,
};
use lash_core::{ProviderSchemaCapabilities, SchemaPurpose};
use lash_llm_transport::streaming::{drive_sse_response, emit_stream_progress};
use lash_llm_transport::timeouts::response_start_timeout;
use lash_llm_transport::util::{emit_provider_request_trace, emit_provider_trace};
use lash_llm_transport::{
    LlmHttpMethod, LlmHttpRequest, LlmHttpTransport, first_header_value, header_contains,
    http_error_envelope, openai_terminal_reason_from_response_value,
    openai_usage_from_response_value, read_http_body_text,
};
use lash_provider_auth::{
    Credential, CredentialCallError, CredentialError, CredentialErrorKind, CredentialExecuteError,
    CredentialManager, CredentialRefresher, Lease, RefreshCause, classify_oauth_refresh_error,
};

pub mod oauth;
#[cfg(any(test, feature = "testing"))]
pub mod ws_testing;

/// Provider name used in shared-machinery error messages and trace events.
const PROVIDER: &str = "Codex";

const SESSION_WEBSOCKET_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
const SESSION_WEBSOCKET_FALLBACK_TTL: Duration = Duration::from_secs(60);
const MAX_SESSION_WEBSOCKET_CACHE_ENTRIES: usize = 32;
/// Per-socket bound on the closing handshake during shutdown drain. A half-dead
/// peer that never returns its Close frame must not stall the remaining cached
/// sockets, so each close is best-effort and abandoned after this elapses.
const SESSION_WEBSOCKET_CLOSE_TIMEOUT: Duration = Duration::from_secs(2);

/// Transport-selection knob for Codex. Production always runs `Auto` (try the
/// WebSocket transport, fall back to SSE). The non-`Auto` variants force a
/// specific path; hosts that must pin a path use
/// [`CodexProvider::force_sse_transport`] (e.g. the deterministic-simulation
/// harness driving Provider Wire Scripts through an injected transport) or
/// [`CodexProvider::force_websocket_transport`] (e.g. the runtime-level
/// WebSocket test) rather than naming these variants; `WebsocketCached`
/// remains a crate-internal test seam.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum CodexTransport {
    #[default]
    Auto,
    Sse,
    Websocket,
    WebsocketCached,
}

#[derive(Clone, Debug, Default)]
struct CodexContinuation {
    previous_response_id: String,
    request_input: Vec<Value>,
    response_items: Vec<Value>,
    body_fingerprint: String,
}

type CodexWsStream = WebSocketStream<MaybeTlsStream<tokio::net::TcpStream>>;

#[derive(Clone, Default)]
struct CodexWebsocketSessionCache {
    inner: Arc<Mutex<CodexWebsocketSessions>>,
}

impl std::fmt::Debug for CodexWebsocketSessionCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (sessions_len, fallback_sessions_len) = self
            .inner
            .lock()
            .map(|sessions| (sessions.by_scope.len(), sessions.fallback_by_scope.len()))
            .unwrap_or_default();
        f.debug_struct("CodexWebsocketSessionCache")
            .field("sessions", &sessions_len)
            .field("fallback_sessions", &fallback_sessions_len)
            .finish()
    }
}

#[derive(Default)]
struct CodexWebsocketSessions {
    by_scope: HashMap<String, CodexWebsocketSessionEntry>,
    fallback_by_scope: HashMap<String, CodexWebsocketFallbackState>,
}

struct CodexWebsocketFallbackState {
    until: Instant,
    reason: String,
}

struct CodexWebsocketSessionEntry {
    connection: Option<CodexWsStream>,
    continuation: Option<CodexContinuation>,
    busy: bool,
    last_used: Instant,
    credential_generation: u64,
}

impl CodexWebsocketSessionEntry {
    fn reserved(credential_generation: u64) -> Self {
        Self {
            connection: None,
            continuation: None,
            busy: true,
            last_used: Instant::now(),
            credential_generation,
        }
    }
}

struct CodexWebsocketLease {
    websocket: CodexWsStream,
    scope_key: Option<String>,
    reusable: bool,
    reused: bool,
    continuation: Option<CodexContinuation>,
    credential_generation: u64,
}

#[derive(Clone)]
struct CodexCredential {
    access_token: String,
    refresh_token: String,
    expires_at: u64,
    account_id: Option<String>,
}

impl std::fmt::Debug for CodexCredential {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("CodexCredential")
            .field("access_token", &"[REDACTED]")
            .field("refresh_token", &"[REDACTED]")
            .field("expires_at", &self.expires_at)
            .field(
                "account_id",
                &self.account_id.as_ref().map(|_| "[REDACTED]"),
            )
            .finish()
    }
}

impl std::fmt::Display for CodexCredential {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("CodexCredential([REDACTED])")
    }
}

impl Credential for CodexCredential {
    fn expires_at(&self) -> Option<SystemTime> {
        (self.expires_at != 0)
            .then(|| UNIX_EPOCH.checked_add(Duration::from_secs(self.expires_at)))
            .flatten()
    }
}

#[derive(Debug)]
struct CodexCredentialRefresher;

#[async_trait]
impl CredentialRefresher<CodexCredential> for CodexCredentialRefresher {
    async fn refresh(
        &self,
        current: &CodexCredential,
        _cause: RefreshCause,
    ) -> Result<CodexCredential, CredentialError> {
        let tokens = oauth::refresh_tokens(&current.refresh_token)
            .await
            .map_err(classify_oauth_refresh_error)?;
        Ok(CodexCredential {
            access_token: tokens.access_token,
            refresh_token: tokens.refresh_token,
            expires_at: tokens.expires_at,
            account_id: tokens.account_id.or_else(|| current.account_id.clone()),
        })
    }
}

fn credential_transport_error(error: CredentialError) -> LlmTransportError {
    let code = match error.kind {
        CredentialErrorKind::InvalidGrant => "credential_invalid_grant",
        CredentialErrorKind::Transient => "credential_refresh_transient",
        CredentialErrorKind::Other => "credential_refresh_failed",
    };
    LlmTransportError::new(error.to_string())
        .with_kind(ProviderFailureKind::Auth)
        .with_code(code)
        .retryable(error.retryable)
}

#[derive(Clone, Debug)]
struct CodexWebsocketRequestPlan {
    body: Value,
    cached: bool,
    continuation_available: bool,
    cache_miss_reason: Option<&'static str>,
    previous_response_id: Option<String>,
    full_input_items: usize,
    sent_input_items: usize,
}

#[derive(Clone, Debug)]
struct CodexWebsocketAttemptDiagnostics {
    configured_transport: CodexTransport,
    reused_connection: bool,
    cached_request: bool,
    continuation_available: bool,
    cache_miss_reason: Option<&'static str>,
    previous_response_id: Option<String>,
    full_input_items: usize,
    sent_input_items: usize,
    request_bytes: usize,
    retry_after_stale_previous_response: bool,
    retry_after_dead_reused_connection: bool,
}

struct CodexWebSocketAttemptError {
    error: LlmTransportError,
    events_seen: bool,
    output_started: bool,
    stale_previous_response: bool,
}

/// One-shot WebSocket retries already consumed by the current send loop.
#[derive(Clone, Copy, Default)]
struct CodexWebsocketRetryState {
    after_stale_previous_response: bool,
    after_dead_reused_connection: bool,
}

/// OpenAI Codex OAuth provider (ChatGPT Plus/Pro/Team via device-code flow).
///
/// Codex speaks the OpenAI Responses streaming protocol, so the request/stream
/// machinery is shared verbatim from [`crate::responses_shared`].
/// This module owns only the Codex-specific surface: the
/// `chatgpt.com/backend-api/codex/responses` endpoint, the `codex_cli_rs`
/// originator/User-Agent headers, the system→`instructions` request shape with
/// tool-result image folding, and Codex error/quota classification.
#[derive(Clone, Debug)]
pub struct CodexProvider {
    credentials: Arc<CredentialManager<CodexCredential>>,
    attempt_credential: Option<Lease<CodexCredential>>,
    pub options: ProviderOptions,
    pub(crate) transport: CodexTransport,
    websocket_sessions: CodexWebsocketSessionCache,
    responses_url: String,
    websocket_url: String,
    http_transport: Arc<dyn LlmHttpTransport>,
}

impl CodexProvider {
    const CODEX_ORIGINATOR: &'static str = "codex_cli_rs";
    const CODEX_RESPONSES_URL: &'static str = "https://chatgpt.com/backend-api/codex/responses";
    const CODEX_RESPONSES_WS_URL: &'static str = "wss://chatgpt.com/backend-api/codex/responses";
    const CODEX_RESPONSES_WS_BETA: &'static str = "responses_websockets=2026-02-06";

    pub fn new(
        access_token: impl Into<String>,
        refresh_token: impl Into<String>,
        expires_at: u64,
    ) -> Self {
        let credential = CodexCredential {
            access_token: access_token.into(),
            refresh_token: refresh_token.into(),
            expires_at,
            account_id: None,
        };
        Self {
            credentials: Arc::new(CredentialManager::new(
                credential,
                Arc::new(CodexCredentialRefresher),
            )),
            attempt_credential: None,
            options: ProviderOptions {
                reliability: ProviderReliability::codex(),
                ..ProviderOptions::default()
            },
            transport: CodexTransport::Auto,
            websocket_sessions: CodexWebsocketSessionCache::default(),
            responses_url: Self::CODEX_RESPONSES_URL.to_string(),
            websocket_url: Self::CODEX_RESPONSES_WS_URL.to_string(),
            http_transport: DEFAULT_HTTP_TRANSPORT.clone(),
        }
    }

    pub fn with_account_id(mut self, account_id: Option<String>) -> Self {
        let mut credential = self.credentials.snapshot();
        credential.account_id = account_id;
        self.credentials = Arc::new(CredentialManager::new(
            credential,
            Arc::new(CodexCredentialRefresher),
        ));
        self
    }

    pub fn with_options(mut self, options: ProviderOptions) -> Self {
        self.options = options;
        self
    }

    #[cfg(test)]
    fn with_transport(mut self, transport: CodexTransport) -> Self {
        self.transport = transport;
        self
    }

    /// Pin Codex to the HTTP/SSE transport, skipping the WebSocket path. This
    /// lets a host (notably the deterministic-simulation harness) drive Codex's
    /// HTTP/SSE path through an injected [`LlmHttpTransport`] without exposing
    /// the internal [`CodexTransport`] variants.
    pub fn force_sse_transport(mut self) -> Self {
        self.transport = CodexTransport::Sse;
        self
    }

    /// Pin Codex to the WebSocket transport, skipping the SSE fallback. The
    /// WebSocket counterpart of [`CodexProvider::force_sse_transport`]: a host
    /// (notably the runtime-level WebSocket test, which points the provider at
    /// a local scripted server via [`CodexProvider::with_endpoint_urls`]) uses
    /// it to exercise the WebSocket path deterministically instead of relying
    /// on `Auto`'s try-then-fall-back behavior.
    pub fn force_websocket_transport(mut self) -> Self {
        self.transport = CodexTransport::Websocket;
        self
    }

    /// Override the Codex Responses HTTP and WebSocket endpoint URLs. This is
    /// a constructor-level injection seam in the same spirit as
    /// [`CodexProvider::with_http_transport`]: production always uses the
    /// built-in `chatgpt.com` endpoints, and the override is never serialized
    /// into provider config ([`CodexProviderFactory`] always rebuilds with the
    /// production URLs), so tests can point a provider instance at local
    /// scripted servers without adding a user-facing behavior surface.
    pub fn with_endpoint_urls(
        mut self,
        responses_url: impl Into<String>,
        websocket_url: impl Into<String>,
    ) -> Self {
        self.responses_url = responses_url.into();
        self.websocket_url = websocket_url.into();
        self
    }

    /// Inject the HTTP/SSE transport seam. Production uses the shared reqwest
    /// transport; the deterministic-simulation harness and tests inject a
    /// scripted [`LlmHttpTransport`] to drive Provider Wire Scripts.
    pub fn with_http_transport(mut self, transport: Arc<dyn LlmHttpTransport>) -> Self {
        self.http_transport = transport;
        self
    }

    /// Translate a Codex error body into a user-friendly one-line message.
    /// Mirrors pi-mono's `openai-codex-responses.ts:880-904`: for a
    /// `usage_limit_reached`/`rate_limit_exceeded` code (or any 429),
    /// parse the `plan_type` and `resets_at` epoch and render
    /// `"You have hit your ChatGPT usage limit (plus plan). Try again in
    /// ~12 min."`. Returns `None` when the body isn't parseable or the
    /// status doesn't match the pattern, so the caller falls back to the
    /// raw status.
    fn codex_error_summary(status: u16, body_text: &str) -> Option<String> {
        let parsed: Value = serde_json::from_str(body_text).ok()?;
        if let Some(detail) = parsed.get("detail").and_then(|v| v.as_str()) {
            return Some(format!("Codex request failed with {status}: {detail}"));
        }
        let err = parsed.get("error")?;
        let code = err
            .get("code")
            .and_then(|v| v.as_str())
            .or_else(|| err.get("type").and_then(|v| v.as_str()))
            .unwrap_or("");
        let code_matches = {
            let lc = code.to_ascii_lowercase();
            lc.contains("usage_limit_reached")
                || lc.contains("usage_not_included")
                || lc.contains("rate_limit_exceeded")
        };
        if !code_matches && status != 429 {
            // Prefer the raw `error.message` if the server gave us one —
            // useful for refusals, invalid-request errors, etc.
            let msg = err.get("message").and_then(|v| v.as_str())?;
            return Some(format!("Codex request failed with {status}: {msg}"));
        }

        let plan = err
            .get("plan_type")
            .and_then(|v| v.as_str())
            .filter(|s| !s.is_empty())
            .map(|p| format!(" ({} plan)", p.to_ascii_lowercase()))
            .unwrap_or_default();
        let resets_at_secs = err.get("resets_at").and_then(|v| v.as_i64());
        let mins = resets_at_secs.and_then(|ts| {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .ok()?
                .as_secs() as i64;
            let delta_secs = ts - now;
            if delta_secs <= 0 {
                Some(0)
            } else {
                Some(((delta_secs + 30) / 60).max(0))
            }
        });
        let when = match mins {
            Some(m) => format!(" Try again in ~{m} min."),
            None => String::new(),
        };
        Some(format!(
            "You have hit your ChatGPT usage limit{plan}.{when}"
        ))
    }

    fn should_parse_stream(stream_requested: bool, content_type: Option<&str>) -> bool {
        stream_requested
            || content_type
                .map(|ct| ct.contains("text/event-stream"))
                .unwrap_or(false)
    }

    fn non_sse_body_read_error(
        status: u16,
        content_type: Option<&str>,
        err: LlmTransportError,
    ) -> LlmTransportError {
        let content_type_detail = content_type
            .map(|ct| format!(" ({ct})"))
            .unwrap_or_default();
        let code = err
            .code
            .clone()
            .unwrap_or_else(|| "body_read_failed".to_string());
        LlmTransportError::new(format!(
            "Codex returned HTTP {status} with non-SSE body{content_type_detail} but it could not be read: {}",
            err.message
        ))
        .retryable(err.retryable)
        .with_code(code)
    }

    fn build_tools(req: &LlmRequest) -> Result<Vec<Value>, LlmTransportError> {
        shared::build_tools(PROVIDER, req)
    }

    fn codex_user_agent() -> String {
        format!(
            "{}/{} ({}; {}) lash",
            Self::CODEX_ORIGINATOR,
            env!("CARGO_PKG_VERSION"),
            std::env::consts::OS,
            std::env::consts::ARCH
        )
    }

    pub(crate) fn build_request_body(
        &self,
        req: &LlmRequest,
        stream: bool,
    ) -> Result<Value, LlmTransportError> {
        shared::validate_responses_attachments(req, "OpenAI Codex")?;
        let tools = Self::build_tools(req)?;
        let (instructions, input) =
            shared::build_responses_input(req, shared::ResponsesInputOptions::CODEX);
        let requested_reasoning = reasoning_intent(req);
        let policy = resolve_generation_policy(
            &req.generation,
            &self.options,
            DEFAULT_MAX_OUTPUT_TOKENS,
            requested_reasoning,
        );
        let mut body = json!({
            "model": req.model,
            "instructions": instructions,
            "input": input,
            "tools": tools,
            "parallel_tool_calls": !req.tools.is_empty(),
            "stream": stream,
            "store": false,
            "include": ["reasoning.encrypted_content"],
            "text": {
                "verbosity": "medium",
            },
        });
        // `tool_choice` is only meaningful when the request advertises tools.
        // In RLM mode we intentionally send `tools: []` because tools are
        // documented in the prompt body and invoked via `lashlang`, not the
        // native tool-call envelope. Sending `tool_choice: "none"` on top of
        // an empty tool list adds a second "definitely don't call any
        // function" signal that reasoning-capable Codex models take literally,
        // causing them to refuse to emit `call` expressions in lashlang.
        if !req.tools.is_empty() {
            body["tool_choice"] = json!(shared::tool_choice_value(&req.tool_choice));
        }
        if let Some(config) = policy.thinking {
            let mut reasoning = match config {
                ReasoningWireIntent::Effort(effort) => json!({ "effort": effort }),
                ReasoningWireIntent::Budget(max_tokens) => json!({ "max_tokens": max_tokens }),
                ReasoningWireIntent::ToggleFalse => json!({ "enabled": false }),
            };
            if policy.expose_thinking {
                reasoning["summary"] = json!("auto");
            }
            body["reasoning"] = reasoning;
        }
        if policy.cache_retention != CacheRetention::None {
            body["prompt_cache_key"] = json!(req.continuation_key());
        }
        if let Some(output_spec) = &req.output_spec {
            body["text"]["format"] = match output_spec {
                LlmOutputSpec::JsonObject => json!({ "type": "json_object" }),
                LlmOutputSpec::JsonSchema(schema) => {
                    let capabilities = ProviderSchemaCapabilities::openai(false);
                    let projected = shared::projected_schema(
                        PROVIDER,
                        &schema.schema,
                        &capabilities,
                        SchemaPurpose::StructuredOutput,
                    )?;
                    json!({
                        "type": "json_schema",
                        "name": schema.name,
                        "schema": projected,
                        "strict": schema.strict,
                    })
                }
            };
        }
        Ok(body)
    }

    fn body_input(body: &Value) -> Vec<Value> {
        body.get("input")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default()
    }

    fn body_fingerprint(body: &Value) -> String {
        let mut comparable = body.clone();
        if let Some(obj) = comparable.as_object_mut() {
            obj.remove("input");
            obj.remove("previous_response_id");
        }
        comparable.to_string()
    }

    fn response_output_items(final_response: &Value) -> Vec<Value> {
        final_response
            .get("output")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default()
    }

    #[cfg(test)]
    fn cached_websocket_body(continuation: &CodexContinuation, full_body: &Value) -> Option<Value> {
        Self::cached_websocket_body_result(continuation, full_body).ok()
    }

    fn cached_websocket_body_result(
        continuation: &CodexContinuation,
        full_body: &Value,
    ) -> Result<Value, &'static str> {
        let current_fingerprint = Self::body_fingerprint(full_body);
        let current_input = Self::body_input(full_body);
        if continuation.body_fingerprint != current_fingerprint {
            return Err("body_fingerprint_mismatch");
        }
        let mut baseline = continuation.request_input.clone();
        baseline.extend(continuation.response_items.clone());
        if current_input.len() < baseline.len()
            || !current_input
                .iter()
                .take(baseline.len())
                .eq(baseline.iter())
        {
            return Err("input_prefix_mismatch");
        }

        let mut body = full_body.clone();
        body["previous_response_id"] = json!(continuation.previous_response_id);
        body["input"] = Value::Array(current_input[baseline.len()..].to_vec());
        Ok(body)
    }

    fn websocket_continuation_enabled(&self) -> bool {
        matches!(
            self.transport,
            CodexTransport::Auto | CodexTransport::WebsocketCached
        )
    }

    fn websocket_request_plan(
        &self,
        full_body: &Value,
        continuation: Option<&CodexContinuation>,
        allow_cached_context: bool,
    ) -> CodexWebsocketRequestPlan {
        let full_input_items = Self::body_input(full_body).len();
        let continuation_available = continuation.is_some();
        let (body, cached, cache_miss_reason) = match (allow_cached_context, continuation) {
            (false, _) => (full_body.clone(), false, Some("disabled")),
            (true, None) => (full_body.clone(), false, Some("missing_continuation")),
            (true, Some(cached)) => match Self::cached_websocket_body_result(cached, full_body) {
                Ok(body) => (body, true, None),
                Err(reason) => (full_body.clone(), false, Some(reason)),
            },
        };
        let previous_response_id = body
            .get("previous_response_id")
            .and_then(Value::as_str)
            .map(str::to_string);
        let sent_input_items = Self::body_input(&body).len();
        CodexWebsocketRequestPlan {
            body,
            cached,
            continuation_available,
            cache_miss_reason,
            previous_response_id,
            full_input_items,
            sent_input_items,
        }
    }

    fn websocket_create_request(body: &Value) -> Value {
        let mut request = body
            .as_object()
            .cloned()
            .unwrap_or_else(serde_json::Map::new);
        request.insert("type".to_string(), json!("response.create"));
        Value::Object(request)
    }

    fn continuation_from_response(
        full_body: &Value,
        final_response: &Value,
    ) -> Option<CodexContinuation> {
        let completed = final_response
            .get("status")
            .and_then(Value::as_str)
            .is_some_and(|status| status == "completed");
        let response_id = final_response.get("id").and_then(Value::as_str)?;
        if !completed || response_id.is_empty() {
            return None;
        }
        Some(CodexContinuation {
            previous_response_id: response_id.to_string(),
            request_input: Self::body_input(full_body),
            response_items: Self::response_output_items(final_response),
            body_fingerprint: Self::body_fingerprint(full_body),
        })
    }

    fn clear_continuation(&self, req: &LlmRequest) {
        let scope_key = req.continuation_key();
        let mut sessions = self
            .websocket_sessions
            .inner
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if let Some(entry) = sessions.by_scope.get_mut(&scope_key) {
            entry.continuation = None;
        }
    }

    fn remove_websocket_scope(&self, scope_key: &str) {
        let mut sessions = self
            .websocket_sessions
            .inner
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        sessions.by_scope.remove(scope_key);
    }

    fn prune_idle_websocket_sessions(sessions: &mut CodexWebsocketSessions) {
        let now = Instant::now();
        Self::prune_expired_websocket_fallbacks(sessions, now);
        // Dropping a cached WebSocketStream closes the socket. This prune path is
        // deliberately synchronous because the cache lock is provider-local.
        sessions.by_scope.retain(|_, entry| {
            entry.busy || now.duration_since(entry.last_used) <= SESSION_WEBSOCKET_CACHE_TTL
        });
    }

    fn prune_expired_websocket_fallbacks(sessions: &mut CodexWebsocketSessions, now: Instant) {
        sessions
            .fallback_by_scope
            .retain(|_, fallback| fallback.until > now);
    }

    fn enforce_websocket_session_cache_cap(sessions: &mut CodexWebsocketSessions) {
        let excess = sessions
            .by_scope
            .len()
            .saturating_sub(MAX_SESSION_WEBSOCKET_CACHE_ENTRIES);
        if excess == 0 {
            return;
        }

        let mut removable = sessions
            .by_scope
            .iter()
            .filter(|(_, entry)| !entry.busy)
            .map(|(scope_key, entry)| (scope_key.clone(), entry.last_used))
            .collect::<Vec<_>>();
        removable.sort_by_key(|(_, last_used)| *last_used);
        for (scope_key, _) in removable.into_iter().take(excess) {
            sessions.by_scope.remove(&scope_key);
        }
    }

    fn evict_websocket_sessions_for_generation(
        sessions: &mut CodexWebsocketSessions,
        credential_generation: u64,
    ) {
        sessions
            .by_scope
            .retain(|_, entry| entry.credential_generation == credential_generation);
    }

    /// Drain the WebSocket session cache, sending a proper Close frame on every
    /// idle cached connection before dropping it.
    ///
    /// This is the shutdown counterpart to the synchronous idle prune: the prune
    /// path drops streams (a TCP-level close), whereas a host-driven shutdown
    /// wants the WebSocket closing handshake. Busy entries are leased out to an
    /// in-flight `complete` call — their stream is not held in the cache — so
    /// this closes only idle, reusable sessions; the lease closes or re-caches
    /// its own connection on release. The cache lock is provider-local and
    /// non-async, so connections are taken out under the lock and closed after
    /// it is released.
    async fn close_websocket_sessions(&self) {
        let connections: Vec<CodexWsStream> = {
            let mut sessions = self
                .websocket_sessions
                .inner
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let drained = sessions
                .by_scope
                .drain()
                .filter_map(|(_, entry)| entry.connection)
                .collect();
            sessions.fallback_by_scope.clear();
            drained
        };
        for mut websocket in connections {
            // Best-effort: a peer that already vanished cannot receive the frame,
            // and shutdown must not fail because one socket is already gone. Bound
            // each close so a half-dead peer that never returns its Close frame
            // cannot stall the drain of the sockets still queued behind it.
            let _ =
                tokio::time::timeout(SESSION_WEBSOCKET_CLOSE_TIMEOUT, websocket.close(None)).await;
        }
    }

    fn websocket_fallback_reason(&self, req: &LlmRequest) -> Option<String> {
        let scope_key = req.continuation_key();
        let mut sessions = self
            .websocket_sessions
            .inner
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        Self::prune_expired_websocket_fallbacks(&mut sessions, Instant::now());
        sessions
            .fallback_by_scope
            .get(&scope_key)
            .map(|fallback| fallback.reason.clone())
    }

    fn record_websocket_fallback(&self, req: &LlmRequest, error: &LlmTransportError) {
        let scope_key = req.continuation_key();
        let mut sessions = self
            .websocket_sessions
            .inner
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let now = Instant::now();
        Self::prune_expired_websocket_fallbacks(&mut sessions, now);
        let reason = error
            .code
            .as_deref()
            .map(|code| format!("{code}: {}", error.message))
            .unwrap_or_else(|| error.message.clone());
        sessions.fallback_by_scope.insert(
            scope_key,
            CodexWebsocketFallbackState {
                until: now + SESSION_WEBSOCKET_FALLBACK_TTL,
                reason,
            },
        );
    }

    fn clear_websocket_fallback(&self, req: &LlmRequest) {
        let scope_key = req.continuation_key();
        let mut sessions = self
            .websocket_sessions
            .inner
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        sessions.fallback_by_scope.remove(&scope_key);
    }

    async fn connect_websocket(
        &self,
        req: &LlmRequest,
        connect_timeout: Duration,
        credential: &CodexCredential,
    ) -> Result<CodexWsStream, CodexWebSocketAttemptError> {
        let mut ws_request =
            self.websocket_url
                .as_str()
                .into_client_request()
                .map_err(|error| CodexWebSocketAttemptError {
                    error: LlmTransportError::new(format!(
                        "Failed to build Codex WebSocket request: {error}"
                    )),
                    events_seen: false,
                    output_started: false,
                    stale_previous_response: false,
                })?;
        let headers = ws_request.headers_mut();
        headers.insert(
            "Authorization",
            HeaderValue::from_str(&format!("Bearer {}", credential.access_token)).map_err(
                |error| CodexWebSocketAttemptError {
                    error: LlmTransportError::new(format!(
                        "Invalid Codex WebSocket authorization header: {error}"
                    )),
                    events_seen: false,
                    output_started: false,
                    stale_previous_response: false,
                },
            )?,
        );
        headers.insert(
            "OpenAI-Beta",
            HeaderValue::from_static(Self::CODEX_RESPONSES_WS_BETA),
        );
        headers.insert(
            "originator",
            HeaderValue::from_static(Self::CODEX_ORIGINATOR),
        );
        headers.insert(
            "User-Agent",
            HeaderValue::from_str(&Self::codex_user_agent()).map_err(|error| {
                CodexWebSocketAttemptError {
                    error: LlmTransportError::new(format!(
                        "Invalid Codex WebSocket user-agent header: {error}"
                    )),
                    events_seen: false,
                    output_started: false,
                    stale_previous_response: false,
                }
            })?,
        );
        let session_value = HeaderValue::from_str(&req.scope.session_id).map_err(|error| {
            CodexWebSocketAttemptError {
                error: LlmTransportError::new(format!(
                    "Invalid Codex WebSocket session header: {error}"
                )),
                events_seen: false,
                output_started: false,
                stale_previous_response: false,
            }
        })?;
        let request_value = HeaderValue::from_str(&req.scope.request_id).map_err(|error| {
            CodexWebSocketAttemptError {
                error: LlmTransportError::new(format!(
                    "Invalid Codex WebSocket request header: {error}"
                )),
                events_seen: false,
                output_started: false,
                stale_previous_response: false,
            }
        })?;
        headers.insert("session-id", session_value);
        headers.insert("x-client-request-id", request_value);
        if let Some(account_id) = credential.account_id.as_deref() {
            headers.insert(
                "ChatGPT-Account-ID",
                HeaderValue::from_str(account_id).map_err(|error| CodexWebSocketAttemptError {
                    error: LlmTransportError::new(format!(
                        "Invalid Codex WebSocket account header: {error}"
                    )),
                    events_seen: false,
                    output_started: false,
                    stale_previous_response: false,
                })?,
            );
        }

        let connect = tokio::time::timeout(connect_timeout, connect_async(ws_request))
            .await
            .map_err(|_| CodexWebSocketAttemptError {
                error: LlmTransportError::new("Codex WebSocket connect timed out")
                    .with_kind(ProviderFailureKind::Timeout)
                    .retryable(true)
                    .with_code("websocket_connect_timeout"),
                events_seen: false,
                output_started: false,
                stale_previous_response: false,
            })?;
        connect.map(|(websocket, _)| websocket).map_err(|error| {
            let status = match &error {
                tokio_tungstenite::tungstenite::Error::Http(response) => {
                    Some(response.status().as_u16())
                }
                _ => None,
            };
            let mut transport_error =
                LlmTransportError::new(format!("Codex WebSocket connect failed: {error}"))
                    .retryable(true)
                    .with_code("websocket_connect");
            if let Some(status) = status {
                transport_error = transport_error.with_status(status);
            }
            CodexWebSocketAttemptError {
                error: transport_error,
                events_seen: false,
                output_started: false,
                stale_previous_response: false,
            }
        })
    }

    async fn acquire_websocket(
        &self,
        req: &LlmRequest,
        connect_timeout: Duration,
        credential: &CodexCredential,
        credential_generation: u64,
    ) -> Result<CodexWebsocketLease, CodexWebSocketAttemptError> {
        let scope_key = req.continuation_key();

        enum AcquireDecision {
            Reuse(Box<CodexWebsocketLease>),
            ConnectReusable(String),
            ConnectEphemeral,
        }

        let decision = {
            let mut sessions = self
                .websocket_sessions
                .inner
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            Self::prune_idle_websocket_sessions(&mut sessions);
            Self::enforce_websocket_session_cache_cap(&mut sessions);
            Self::evict_websocket_sessions_for_generation(&mut sessions, credential_generation);
            if let Some(entry) = sessions.by_scope.get_mut(&scope_key) {
                if entry.busy {
                    AcquireDecision::ConnectEphemeral
                } else if let Some(websocket) = entry.connection.take() {
                    entry.busy = true;
                    entry.last_used = Instant::now();
                    AcquireDecision::Reuse(Box::new(CodexWebsocketLease {
                        websocket,
                        scope_key: Some(scope_key),
                        reusable: true,
                        reused: true,
                        continuation: entry.continuation.clone(),
                        credential_generation,
                    }))
                } else {
                    *entry = CodexWebsocketSessionEntry::reserved(credential_generation);
                    AcquireDecision::ConnectReusable(scope_key.clone())
                }
            } else {
                sessions.by_scope.insert(
                    scope_key.clone(),
                    CodexWebsocketSessionEntry::reserved(credential_generation),
                );
                AcquireDecision::ConnectReusable(scope_key.clone())
            }
        };

        match decision {
            AcquireDecision::Reuse(lease) => Ok(*lease),
            AcquireDecision::ConnectEphemeral => {
                let websocket = self
                    .connect_websocket(req, connect_timeout, credential)
                    .await?;
                Ok(CodexWebsocketLease {
                    websocket,
                    scope_key: None,
                    reusable: false,
                    reused: false,
                    continuation: None,
                    credential_generation,
                })
            }
            AcquireDecision::ConnectReusable(scope_key) => {
                let websocket = match self
                    .connect_websocket(req, connect_timeout, credential)
                    .await
                {
                    Ok(websocket) => websocket,
                    Err(error) => {
                        self.remove_websocket_scope(&scope_key);
                        return Err(error);
                    }
                };
                Ok(CodexWebsocketLease {
                    websocket,
                    scope_key: Some(scope_key),
                    reusable: true,
                    reused: false,
                    continuation: None,
                    credential_generation,
                })
            }
        }
    }

    fn release_websocket_lease(
        &self,
        lease: CodexWebsocketLease,
        keep_connection: bool,
        continuation: Option<CodexContinuation>,
    ) {
        let Some(scope_key) = lease.scope_key else {
            return;
        };
        if !lease.reusable || !keep_connection {
            self.remove_websocket_scope(&scope_key);
            return;
        }
        let mut sessions = self
            .websocket_sessions
            .inner
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        sessions.by_scope.insert(
            scope_key,
            CodexWebsocketSessionEntry {
                connection: Some(lease.websocket),
                continuation,
                busy: false,
                last_used: Instant::now(),
                credential_generation: lease.credential_generation,
            },
        );
        Self::prune_idle_websocket_sessions(&mut sessions);
        Self::enforce_websocket_session_cache_cap(&mut sessions);
    }

    fn response_state_started_output(state: &shared::ResponsesStreamState) -> bool {
        !state.parts.is_empty()
            || !state.full_text.is_empty()
            || !state.pending_text_deltas.is_empty()
            || !state.reasoning_deltas.is_empty()
    }

    fn is_stale_previous_response_error(error: &LlmTransportError) -> bool {
        let haystack = format!(
            "{}\n{}\n{}",
            error.message,
            error.raw.as_deref().map(String::as_str).unwrap_or_default(),
            error.code.as_deref().unwrap_or_default()
        )
        .to_ascii_lowercase();
        haystack.contains("previous_response_id")
            || haystack.contains("previous response")
            || haystack.contains("previous response with id")
    }

    async fn complete_websocket(
        &self,
        req: LlmRequest,
        credential: &CodexCredential,
        credential_generation: u64,
    ) -> Result<LlmResponse, CodexWebSocketAttemptError> {
        let full_body =
            self.build_request_body(&req, true)
                .map_err(|error| CodexWebSocketAttemptError {
                    error,
                    events_seen: false,
                    output_started: false,
                    stale_previous_response: false,
                })?;
        let timeouts = self.options.llm_timeouts();
        let connect_timeout =
            response_start_timeout(timeouts.request_timeout, timeouts.chunk_timeout, true)
                .unwrap_or(timeouts.chunk_timeout);
        let mut retry_state = CodexWebsocketRetryState::default();
        let mut allow_cached_context = self.websocket_continuation_enabled();
        loop {
            let lease = self
                .acquire_websocket(&req, connect_timeout, credential, credential_generation)
                .await?;
            let reused_connection = lease.reused;
            let plan = self.websocket_request_plan(
                &full_body,
                lease.continuation.as_ref(),
                allow_cached_context && lease.reusable,
            );
            let cached_request = plan.cached;
            match self
                .run_websocket_attempt(
                    &req,
                    &full_body,
                    lease,
                    plan,
                    retry_state,
                    timeouts.chunk_timeout,
                )
                .await
            {
                Ok(response) => return Ok(response),
                Err(err)
                    if cached_request
                        && err.stale_previous_response
                        && !err.output_started
                        && !retry_state.after_stale_previous_response =>
                {
                    self.clear_continuation(&req);
                    retry_state.after_stale_previous_response = true;
                    allow_cached_context = false;
                    tracing::debug!(
                        target: "lash_core::llm::codex_oauth",
                        error = %err.error.message,
                        "Codex WebSocket cached continuation was stale; retrying once with full context"
                    );
                }
                Err(err)
                    if reused_connection
                        && !err.events_seen
                        && !retry_state.after_dead_reused_connection =>
                {
                    retry_state.after_dead_reused_connection = true;
                    allow_cached_context = false;
                    tracing::debug!(
                        target: "lash_core::llm::codex_oauth",
                        error = %err.error.message,
                        "Codex WebSocket cached connection failed before stream start; reconnecting once with full context"
                    );
                }
                Err(err) => return Err(err),
            }
        }
    }

    async fn run_websocket_attempt(
        &self,
        req: &LlmRequest,
        full_body: &Value,
        lease: CodexWebsocketLease,
        plan: CodexWebsocketRequestPlan,
        retry_state: CodexWebsocketRetryState,
        read_timeout: Duration,
    ) -> Result<LlmResponse, CodexWebSocketAttemptError> {
        let stream_events = req.stream_events.clone();
        let provider_trace = req.provider_trace.clone();
        let stream_termination = req
            .model_capability
            .stream_termination
            .unwrap_or(StreamTermination::RequireTerminalEvidence);
        let websocket_body = Self::websocket_create_request(&plan.body);
        let request_body = match serde_json::to_string(&websocket_body) {
            Ok(request_body) => request_body,
            Err(error) => {
                self.release_websocket_lease(lease, false, None);
                return Err(CodexWebSocketAttemptError {
                    error: LlmTransportError::new(format!(
                        "Failed to serialize Codex WebSocket body: {error}"
                    )),
                    events_seen: false,
                    output_started: false,
                    stale_previous_response: false,
                });
            }
        };
        emit_provider_request_trace(
            provider_trace.as_ref(),
            "codex",
            "responses",
            request_body.as_bytes(),
        );
        let diagnostics = CodexWebsocketAttemptDiagnostics {
            configured_transport: self.transport,
            reused_connection: lease.reused,
            cached_request: plan.cached,
            continuation_available: plan.continuation_available,
            cache_miss_reason: plan.cache_miss_reason,
            previous_response_id: plan.previous_response_id.clone(),
            full_input_items: plan.full_input_items,
            sent_input_items: plan.sent_input_items,
            request_bytes: request_body.len(),
            retry_after_stale_previous_response: retry_state.after_stale_previous_response,
            retry_after_dead_reused_connection: retry_state.after_dead_reused_connection,
        };
        self.emit_websocket_attempt_trace(provider_trace.as_ref(), &diagnostics);
        let mut lease = Some(lease);
        let mut events_seen = false;
        if let Err(error) = lease
            .as_mut()
            .expect("websocket lease is present")
            .websocket
            .send(WsMessage::Text(request_body.clone().into()))
            .await
        {
            self.release_websocket_lease(
                lease.take().expect("websocket lease is present"),
                false,
                None,
            );
            return Err(CodexWebSocketAttemptError {
                error: LlmTransportError::new(format!("Codex WebSocket send failed: {error}"))
                    .with_request_body(request_body.clone())
                    .retryable(true)
                    .with_code("websocket_send"),
                events_seen,
                output_started: false,
                stale_previous_response: false,
            });
        }

        let mut state = shared::ResponsesStreamState::default();
        let expose_thinking = self.options.expose_thinking;
        loop {
            let next_message = tokio::time::timeout(
                read_timeout,
                lease
                    .as_mut()
                    .expect("websocket lease is present")
                    .websocket
                    .next(),
            )
            .await;
            let Some(message) = (match next_message {
                Ok(message) => message,
                Err(_) => {
                    let output_started = Self::response_state_started_output(&state);
                    self.release_websocket_lease(
                        lease.take().expect("websocket lease is present"),
                        false,
                        None,
                    );
                    return Err(CodexWebSocketAttemptError {
                        error: LlmTransportError::new("Codex WebSocket stream chunk timed out")
                            .with_kind(ProviderFailureKind::Timeout)
                            .with_request_body(request_body.clone())
                            .retryable(true)
                            .with_code("websocket_idle_timeout"),
                        events_seen,
                        output_started,
                        stale_previous_response: false,
                    });
                }
            }) else {
                break;
            };
            let message = match message {
                Ok(message) => message,
                Err(error) => {
                    let output_started = Self::response_state_started_output(&state);
                    self.release_websocket_lease(
                        lease.take().expect("websocket lease is present"),
                        false,
                        None,
                    );
                    return Err(CodexWebSocketAttemptError {
                        error: LlmTransportError::new(format!(
                            "Codex WebSocket receive failed: {error}"
                        ))
                        .with_request_body(request_body.clone())
                        .retryable(true)
                        .with_code("websocket_receive"),
                        events_seen,
                        output_started,
                        stale_previous_response: false,
                    });
                }
            };
            let raw = match message {
                WsMessage::Text(text) => text.to_string(),
                WsMessage::Binary(bytes) => match String::from_utf8(bytes.to_vec()) {
                    Ok(text) => text,
                    Err(error) => {
                        let output_started = Self::response_state_started_output(&state);
                        self.release_websocket_lease(
                            lease.take().expect("websocket lease is present"),
                            false,
                            None,
                        );
                        return Err(CodexWebSocketAttemptError {
                            error: LlmTransportError::new(format!(
                                "Codex WebSocket binary frame was not UTF-8: {error}"
                            ))
                            .with_request_body(request_body.clone())
                            .with_code("websocket_protocol"),
                            events_seen,
                            output_started,
                            stale_previous_response: false,
                        });
                    }
                },
                WsMessage::Close(_) => break,
                WsMessage::Ping(_) | WsMessage::Pong(_) | WsMessage::Frame(_) => continue,
            };
            emit_provider_trace(provider_trace.as_ref(), "codex", &raw);
            events_seen = true;
            let prev_usage = state.usage.clone();
            let mut emitted_parts = Vec::new();
            let process_result = if Self::looks_like_sse_payload(&raw) {
                shared::parse_sse_payload(PROVIDER, &raw, &mut state)
            } else {
                shared::process_sse_event(PROVIDER, &raw, &mut state, Some(&mut emitted_parts))
            };
            if let Err(error) = process_result {
                let output_started = Self::response_state_started_output(&state);
                let stale_previous_response = Self::is_stale_previous_response_error(&error);
                self.release_websocket_lease(
                    lease.take().expect("websocket lease is present"),
                    false,
                    None,
                );
                return Err(CodexWebSocketAttemptError {
                    error: error.with_request_body(request_body.clone()),
                    events_seen,
                    output_started,
                    stale_previous_response,
                });
            }
            emit_stream_progress(
                stream_events.as_ref(),
                state.take_text_deltas(),
                &state.usage,
                &prev_usage,
            );
            if let Some(tx) = &stream_events {
                for piece in state.take_reasoning_deltas() {
                    if expose_thinking {
                        tx.send(LlmStreamEvent::ReasoningDelta(piece));
                    }
                }
                for part in emitted_parts {
                    if matches!(part, lash_core::llm::types::LlmOutputPart::Reasoning { .. })
                        && !expose_thinking
                    {
                        continue;
                    }
                    tx.send(LlmStreamEvent::Part(part));
                }
            } else {
                state.take_reasoning_deltas();
            }
            if state.terminal_event_seen {
                break;
            }
        }

        let terminal_response_seen = state.terminal_event_seen;
        if !terminal_response_seen
            && stream_termination == StreamTermination::RequireTerminalEvidence
        {
            let output_started = Self::response_state_started_output(&state);
            let mut partial = shared::response_from_stream_state(
                state.clone(),
                Some(request_body.clone()),
                self.websocket_http_summary(&diagnostics),
            );
            partial.terminal_reason = LlmTerminalReason::Unknown;
            self.release_websocket_lease(
                lease.take().expect("websocket lease is present"),
                false,
                None,
            );
            return Err(CodexWebSocketAttemptError {
                error: LlmTransportError::new("Codex WebSocket ended before response.completed")
                    .with_request_body(request_body)
                    .with_kind(ProviderFailureKind::Stream)
                    .retryable(true)
                    .with_code("websocket_closed_before_completed")
                    .with_partial_response(partial),
                events_seen,
                output_started,
                stale_previous_response: false,
            });
        }

        let final_response = state.final_response.clone();
        let continuation = final_response.as_ref().and_then(|response| {
            self.websocket_continuation_enabled()
                .then(|| Self::continuation_from_response(full_body, response))
                .flatten()
        });
        let mut response = shared::response_from_stream_state(
            state,
            Some(request_body.clone()),
            self.websocket_http_summary(&diagnostics),
        );
        response.http_summary = Some(self.websocket_http_summary(&diagnostics));
        self.release_websocket_lease(
            lease.take().expect("websocket lease is present"),
            true,
            continuation,
        );
        Ok(response)
    }

    fn websocket_http_summary(&self, diagnostics: &CodexWebsocketAttemptDiagnostics) -> String {
        format!(
            "WS {} transport={:?} reused={} cached={} cache_miss={} retry_after_stale={} retry_after_dead_reused={} input_items={}/{} previous_response_id={} request_bytes={}",
            self.websocket_url,
            diagnostics.configured_transport,
            diagnostics.reused_connection,
            diagnostics.cached_request,
            diagnostics.cache_miss_reason.unwrap_or("<none>"),
            diagnostics.retry_after_stale_previous_response,
            diagnostics.retry_after_dead_reused_connection,
            diagnostics.sent_input_items,
            diagnostics.full_input_items,
            diagnostics
                .previous_response_id
                .as_deref()
                .unwrap_or("<none>"),
            diagnostics.request_bytes
        )
    }

    fn emit_websocket_attempt_trace(
        &self,
        provider_trace: Option<&lash_core::llm::types::LlmProviderTraceSender>,
        diagnostics: &CodexWebsocketAttemptDiagnostics,
    ) {
        let raw = json!({
            "type": "lash.codex.websocket_request",
            "transport": format!("{:?}", diagnostics.configured_transport),
            "reused_connection": diagnostics.reused_connection,
            "cached_request": diagnostics.cached_request,
            "continuation_available": diagnostics.continuation_available,
            "cache_miss_reason": diagnostics.cache_miss_reason,
            "retry_after_stale_previous_response": diagnostics.retry_after_stale_previous_response,
            "retry_after_dead_reused_connection": diagnostics.retry_after_dead_reused_connection,
            "previous_response_id": diagnostics.previous_response_id,
            "full_input_items": diagnostics.full_input_items,
            "sent_input_items": diagnostics.sent_input_items,
            "request_bytes": diagnostics.request_bytes,
        })
        .to_string();
        emit_provider_trace(provider_trace, "codex", &raw);
    }

    fn looks_like_sse_payload(payload: &str) -> bool {
        let trimmed = payload.trim_start();
        trimmed.starts_with("event:")
            || trimmed.starts_with("data:")
            || payload.contains("\nevent:")
            || payload.contains("\ndata:")
    }

    #[cfg(test)]
    fn process_sse_event(
        raw: &str,
        state: &mut shared::ResponsesStreamState,
        emitted_parts: Option<&mut Vec<lash_core::llm::types::LlmOutputPart>>,
    ) -> Result<(), LlmTransportError> {
        shared::process_sse_event(PROVIDER, raw, state, emitted_parts)
    }
}

impl CodexProvider {
    pub fn into_components(self) -> ProviderComponents {
        ProviderComponents::new(Box::new(self))
            .with_failure_classifier(std::sync::Arc::new(CodexFailureClassifier))
    }
}

#[derive(Debug)]
struct CodexFailureClassifier;

impl ProviderFailureClassifier for CodexFailureClassifier {
    fn classify(&self, failure: ProviderFailure) -> ProviderFailure {
        // The default classifier already covers everything Codex needs from a
        // status/text standpoint: HTTP-status → kind/retryability, the
        // usage-limit/quota and content-filter text markers, and context
        // overflow. Codex's only genuine delta is rewriting the user-facing
        // message into a friendly "you hit your ChatGPT usage limit" form.
        let status = failure
            .status
            .or_else(|| failure.code.as_deref().and_then(|code| code.parse().ok()));
        let summary = status.and_then(|status| {
            CodexProvider::codex_error_summary(
                status,
                failure
                    .raw
                    .as_deref()
                    .map(String::as_str)
                    .unwrap_or_default(),
            )
        });
        let mut failure = DefaultProviderFailureClassifier.classify(failure);
        if let Some(summary) = summary {
            failure.message = summary;
        }
        failure
    }
}

#[async_trait]
impl Provider for CodexProvider {
    fn kind(&self) -> &'static str {
        "codex"
    }

    fn options(&self) -> ProviderOptions {
        self.options.clone()
    }

    fn set_options(&mut self, options: ProviderOptions) {
        self.options = options;
    }

    fn serialize_config(&self) -> serde_json::Value {
        let credential = self.credentials.snapshot();
        let mut map = serde_json::Map::new();
        map.insert(
            "access_token".to_string(),
            serde_json::Value::String(credential.access_token),
        );
        map.insert(
            "refresh_token".to_string(),
            serde_json::Value::String(credential.refresh_token),
        );
        map.insert(
            "expires_at".to_string(),
            serde_json::Value::Number(credential.expires_at.into()),
        );
        if let Some(account_id) = &credential.account_id {
            map.insert(
                "account_id".to_string(),
                serde_json::Value::String(account_id.clone()),
            );
        } else {
            map.insert("account_id".to_string(), serde_json::Value::Null);
        }
        if !self.options.is_default() {
            map.insert(
                "options".to_string(),
                serde_json::to_value(&self.options).unwrap_or(serde_json::Value::Null),
            );
        }
        if self.transport != CodexTransport::Auto {
            map.insert(
                "transport".to_string(),
                serde_json::to_value(self.transport).unwrap_or(serde_json::Value::Null),
            );
        }
        serde_json::Value::Object(map)
    }

    fn requires_streaming(&self) -> bool {
        true
    }

    async fn complete(&mut self, req: LlmRequest) -> Result<LlmResponse, LlmTransportError> {
        if self.attempt_credential.is_none() {
            let manager = Arc::clone(&self.credentials);
            let provider = self.clone();
            return manager
                .execute(move |lease| {
                    let mut provider = provider.clone();
                    let req = req.clone();
                    provider.attempt_credential = Some(lease);
                    async move {
                        match Box::pin(provider.complete(req)).await {
                            Ok(response) => Ok(response),
                            Err(error) if error.status == Some(401) => {
                                Err(CredentialCallError::PreOutputAuth(error))
                            }
                            Err(error) => Err(CredentialCallError::Failed(error)),
                        }
                    }
                })
                .await
                .map_err(|error| match error {
                    CredentialExecuteError::Credential(error) => credential_transport_error(error),
                    CredentialExecuteError::Call(error) => error,
                });
        }
        let credential_lease = self
            .attempt_credential
            .take()
            .expect("credential attempt is configured");
        let credential = &credential_lease.value;
        let stream_termination = req
            .model_capability
            .stream_termination
            .unwrap_or(StreamTermination::RequireTerminalEvidence);
        if !matches!(self.transport, CodexTransport::Sse) {
            let fallback_reason = matches!(self.transport, CodexTransport::Auto)
                .then(|| self.websocket_fallback_reason(&req))
                .flatten();
            if let Some(reason) = fallback_reason {
                emit_provider_trace(
                    req.provider_trace.as_ref(),
                    "codex",
                    &json!({
                        "type": "lash.codex.websocket_fallback_skip",
                        "transport": format!("{:?}", self.transport),
                        "reason": reason,
                    })
                    .to_string(),
                );
                tracing::debug!(
                    target: "lash_core::llm::codex_oauth",
                    reason = %reason,
                    "Skipping Codex WebSocket for session with active Auto fallback"
                );
            } else {
                match self
                    .complete_websocket(req.clone(), credential, credential_lease.generation)
                    .await
                {
                    Ok(response) => {
                        self.clear_websocket_fallback(&req);
                        return Ok(response);
                    }
                    Err(err)
                        if matches!(self.transport, CodexTransport::Auto) && !err.events_seen =>
                    {
                        self.record_websocket_fallback(&req, &err.error);
                        tracing::debug!(
                            target: "lash_core::llm::codex_oauth",
                            error = %err.error.message,
                            "Codex WebSocket failed before stream start; falling back to SSE"
                        );
                    }
                    Err(err) => {
                        self.clear_continuation(&req);
                        return Err(err.error);
                    }
                }
            }
        }
        let stream_events = req.stream_events.clone();
        let provider_trace = req.provider_trace.clone();
        let timeouts = self.options.llm_timeouts();

        let body = self.build_request_body(&req, stream_events.is_some())?;

        let request_body = serde_json::to_string(&body).ok();
        let body_bytes = serde_json::to_vec(&body).map_err(|e| {
            LlmTransportError::new(format!("Failed to serialize Codex request: {e}"))
        })?;
        emit_provider_request_trace(provider_trace.as_ref(), "codex", "responses", &body_bytes);

        let access_token = credential.access_token.clone();
        let account_id = credential.account_id.clone();
        let mut headers = vec![
            (
                "Authorization".to_string(),
                format!("Bearer {access_token}"),
            ),
            ("Content-Type".to_string(), "application/json".to_string()),
            ("Accept".to_string(), "text/event-stream".to_string()),
            (
                "OpenAI-Beta".to_string(),
                "responses=experimental".to_string(),
            ),
            ("originator".to_string(), Self::CODEX_ORIGINATOR.to_string()),
            ("User-Agent".to_string(), Self::codex_user_agent()),
            ("session-id".to_string(), req.scope.session_id.clone()),
            (
                "x-client-request-id".to_string(),
                req.scope.request_id.clone(),
            ),
        ];
        if let Some(id) = account_id.as_deref() {
            headers.push(("ChatGPT-Account-ID".to_string(), id.to_string()));
        }
        let http_request = LlmHttpRequest {
            method: LlmHttpMethod::Post,
            url: self.responses_url.clone(),
            headers,
            body: bytes::Bytes::from(body_bytes),
            body_for_error: request_body.clone(),
            response_start_timeout_message: Some("Codex response start timed out".to_string()),
        };
        let resp = self
            .http_transport
            .send(
                http_request,
                response_start_timeout(
                    timeouts.request_timeout,
                    timeouts.chunk_timeout,
                    stream_events.is_some(),
                ),
            )
            .await?;
        let status = resp.status;
        let content_type = first_header_value(&resp.headers, "content-type").map(str::to_string);
        let response_headers = resp.headers.clone();
        let is_sse = header_contains(&resp.headers, "content-type", "text/event-stream");
        let success = resp.is_success();
        let body = resp.body;
        if !success {
            let text = read_http_body_text(
                body,
                timeouts.request_timeout,
                "Codex response body timed out",
            )
            .await
            .unwrap_or_default();
            let message = Self::codex_error_summary(status, &text).unwrap_or_else(|| {
                format!(
                    "Codex request failed with {}{}",
                    status,
                    content_type
                        .as_deref()
                        .map(|ct| format!(" ({ct})"))
                        .unwrap_or_default()
                )
            });
            // Retryability is decided centrally by `CodexFailureClassifier`
            // from the attached HTTP status; no inline override here.
            return Err(http_error_envelope(
                message,
                status,
                response_headers,
                text,
                request_body.clone(),
            ));
        }

        let parse_stream =
            Self::should_parse_stream(stream_events.is_some(), content_type.as_deref());

        if !parse_stream {
            let text = read_http_body_text(
                body,
                timeouts.request_timeout,
                "Codex response body timed out",
            )
            .await
            .map_err(|err| Self::non_sse_body_read_error(status, content_type.as_deref(), err))?;
            emit_provider_trace(provider_trace.as_ref(), "codex", &text);
            if Self::looks_like_sse_payload(&text) {
                let mut state = shared::ResponsesStreamState::default();
                shared::parse_sse_payload(PROVIDER, &text, &mut state)?;
                let response = shared::response_from_stream_state(
                    state,
                    request_body,
                    format!("HTTP POST {} (stream/fallback)", self.responses_url),
                );
                if let Some(tx) = &stream_events {
                    if response.usage != LlmUsage::default() {
                        tx.send(LlmStreamEvent::Usage(response.usage.clone()));
                    }
                    for part in &response.parts {
                        if let lash_core::llm::types::LlmOutputPart::Text { text, .. } = part
                            && !text.is_empty()
                        {
                            tx.send(LlmStreamEvent::Delta(text.clone()));
                        }
                    }
                    for part in &response.parts {
                        match part {
                            lash_core::llm::types::LlmOutputPart::ToolCall { .. } => {
                                tx.send(LlmStreamEvent::Part(part.clone()));
                            }
                            lash_core::llm::types::LlmOutputPart::Reasoning { text, .. }
                                if !text.is_empty() && self.options.expose_thinking =>
                            {
                                tx.send(LlmStreamEvent::ReasoningDelta(text.clone()));
                            }
                            _ => {}
                        }
                    }
                }
                return Ok(response);
            }
            let value: Value = serde_json::from_str(&text).map_err(|e| {
                LlmTransportError::new(format!("Invalid Codex response JSON: {e}"))
                    .with_raw(text.clone())
            })?;
            let content = shared::extract_text(&value);
            let provider_usage = value.get("usage").cloned();
            let usage = openai_usage_from_response_value(&value);
            let mut parts = shared::response_parts_from_value(&value);
            if parts.is_empty() && !content.is_empty() {
                parts.push(lash_core::llm::types::LlmOutputPart::Text {
                    text: content.clone(),
                    response_meta: None,
                });
            }
            if let Some(tx) = &stream_events {
                if usage != LlmUsage::default() {
                    tx.send(LlmStreamEvent::Usage(usage.clone()));
                }
                if !content.is_empty() {
                    tx.send(LlmStreamEvent::Delta(content.clone()));
                }
            }
            let terminal_reason = openai_terminal_reason_from_response_value(&value, &parts);
            return Ok(LlmResponse {
                full_text: content,
                parts,
                usage,
                terminal_reason,
                terminal_diagnostic: None,
                provider_usage,
                request_body,
                http_summary: Some(format!("HTTP POST {}", self.responses_url)),
                execution_evidence: None,
                response_metadata: Default::default(),
            });
        }

        if stream_events.is_some() && !is_sse {
            tracing::debug!(
                target: "lash_core::llm::codex_oauth",
                status,
                content_type = content_type.as_deref().unwrap_or("<missing>"),
                "Codex streaming response did not advertise SSE; parsing as stream because stream=true was requested"
            );
        }

        let mut state = shared::ResponsesStreamState::default();
        let expose_thinking = self.options.expose_thinking;
        let stream_result = drive_sse_response(
            body,
            timeouts.chunk_timeout,
            "Codex stream chunk timed out",
            |raw| {
                emit_provider_trace(provider_trace.as_ref(), "codex", raw);
                let prev_usage = state.usage.clone();
                let mut emitted_parts = Vec::new();
                shared::process_sse_event(PROVIDER, raw, &mut state, Some(&mut emitted_parts))?;
                emit_stream_progress(
                    stream_events.as_ref(),
                    state.take_text_deltas(),
                    &state.usage,
                    &prev_usage,
                );
                if let Some(tx) = &stream_events {
                    for piece in state.take_reasoning_deltas() {
                        if expose_thinking {
                            tx.send(LlmStreamEvent::ReasoningDelta(piece));
                        }
                    }
                    for part in emitted_parts {
                        if matches!(part, lash_core::llm::types::LlmOutputPart::Reasoning { .. })
                            && !expose_thinking
                        {
                            continue;
                        }
                        tx.send(LlmStreamEvent::Part(part));
                    }
                }
                Ok(())
            },
        )
        .await;

        if let Err(error) = stream_result {
            let mut partial = shared::response_from_stream_state(
                state.clone(),
                request_body.clone(),
                format!("HTTP POST {} (stream)", self.responses_url),
            );
            partial.terminal_reason = LlmTerminalReason::Unknown;
            return Err(error.with_partial_response(partial));
        }

        if stream_termination == StreamTermination::RequireTerminalEvidence
            && !state.terminal_event_seen
        {
            let mut partial = shared::response_from_stream_state(
                state.clone(),
                request_body.clone(),
                format!("HTTP POST {} (stream)", self.responses_url),
            );
            partial.terminal_reason = LlmTerminalReason::Unknown;
            return Err(LlmTransportError::new(
                "Codex stream ended before a terminal response event",
            )
            .with_kind(ProviderFailureKind::Stream)
            .with_code("stream_ended_before_terminal_response")
            .retryable(true)
            .with_partial_response(partial));
        }

        if state.final_response.is_none()
            && state.parts.is_empty()
            && state.pending_text_deltas.is_empty()
        {
            return Err(LlmTransportError::new(format!(
                "Codex stream ended without SSE events (HTTP {}{})",
                status,
                content_type
                    .as_deref()
                    .map(|ct| format!(", content-type {ct}"))
                    .unwrap_or_else(|| ", missing content-type".to_string())
            ))
            .retryable(true)
            .with_code("empty_stream"));
        }

        Ok(shared::response_from_stream_state(
            state,
            request_body,
            format!("HTTP POST {} (stream)", self.responses_url),
        ))
    }

    async fn close(&self) -> Result<(), LlmTransportError> {
        // Drain the provider-local WebSocket session cache with real Close
        // frames. The cache is shared across clones (Arc), so closing any handle
        // a host retained releases the cached sockets for all of them.
        self.close_websocket_sessions().await;
        Ok(())
    }

    fn clone_boxed(&self) -> Box<dyn Provider> {
        Box::new(self.clone())
    }
}

#[derive(Deserialize)]
struct CodexProviderConfig {
    access_token: String,
    refresh_token: String,
    expires_at: u64,
    #[serde(default)]
    account_id: Option<String>,
    #[serde(default = "default_codex_options")]
    options: ProviderOptions,
    #[serde(default)]
    transport: CodexTransport,
}

fn default_codex_options() -> ProviderOptions {
    ProviderOptions {
        reliability: ProviderReliability::codex(),
        ..ProviderOptions::default()
    }
}

/// Factory that materializes [`CodexProvider`] from a host-owned
/// [`ProviderSpec`](lash_core::ProviderSpec).
pub struct CodexProviderFactory;

impl ProviderFactory for CodexProviderFactory {
    fn kind(&self) -> &'static str {
        "codex"
    }
    fn deserialize(&self, config: serde_json::Value) -> Result<ProviderComponents, String> {
        let cfg: CodexProviderConfig =
            serde_json::from_value(config).map_err(|err| err.to_string())?;
        Ok(CodexProvider {
            options: cfg.options,
            transport: cfg.transport,
            ..CodexProvider::new(cfg.access_token, cfg.refresh_token, cfg.expires_at)
                .with_account_id(cfg.account_id)
        }
        .into_components())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lash_core::llm::types::{
        LlmJsonSchema, LlmMessage, LlmOutputPart, LlmProviderTraceSender, LlmRequestScope, LlmRole,
        LlmTerminalReason, LlmToolChoice, LlmToolSpec, ResponseTextMeta,
    };
    use lash_core::provider::{ModelCapability, Provider, ReasoningCapability, RequestTimeout};
    use shared::ResponsesStreamState as CodexStreamState;
    use std::num::NonZeroUsize;
    use std::sync::{Arc, Mutex};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;
    use tokio::task::JoinHandle;
    use ws_testing::{ScriptedWsAction, assistant_item, spawn_scripted_websocket};

    fn process_event(state: &mut CodexStreamState, event: Value) {
        CodexProvider::process_sse_event(&event.to_string(), state, None).unwrap();
    }

    fn process_event_with_parts(
        state: &mut CodexStreamState,
        event: Value,
        emitted_parts: &mut Vec<LlmOutputPart>,
    ) {
        CodexProvider::process_sse_event(&event.to_string(), state, Some(emitted_parts)).unwrap();
    }

    fn response_from_state(state: CodexStreamState) -> LlmResponse {
        shared::response_from_stream_state(state, None, "test".to_string())
    }

    fn reasoning_capability() -> ModelCapability {
        ModelCapability {
            reasoning: Some(ReasoningCapability {
                efforts: vec!["medium".to_string(), "high".to_string()],
                default_effort: Some("medium".to_string()),
                disable: Some(lash_core::provider::ReasoningDisableEncoding::Effort(
                    "none".to_string(),
                )),
                ..ReasoningCapability::default()
            }),
            cache_control: None,
            stream_termination: None,
        }
    }

    fn request(messages: Vec<LlmMessage>) -> LlmRequest {
        LlmRequest {
            model: "gpt-5.4".to_string(),
            messages,
            attachments: Vec::new(),
            resolved_stored: Default::default(),
            tools: Arc::new(Vec::<LlmToolSpec>::new()),
            tool_choice: LlmToolChoice::Auto,
            model_variant: Default::default(),
            model_capability: ModelCapability::default(),
            scope: LlmRequestScope::new(
                "session-1",
                "session-1:frame:test",
                "session-1:request:test",
            ),
            output_spec: None,
            stream_events: None,
            generation: lash_core::GenerationOptions::default(),
            provider_trace: None,
        }
    }

    fn traced_request(messages: Vec<LlmMessage>, trace: Arc<Mutex<Vec<Value>>>) -> LlmRequest {
        let mut req = request(messages);
        req.provider_trace = Some(LlmProviderTraceSender::new(move |event| {
            if let Ok(value) = serde_json::from_str::<Value>(&event.raw) {
                trace
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner)
                    .push(value);
            }
        }));
        req
    }

    fn websocket_diagnostics(trace: &Arc<Mutex<Vec<Value>>>) -> Vec<Value> {
        trace
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .iter()
            .filter(|value| {
                value
                    .get("type")
                    .and_then(Value::as_str)
                    .is_some_and(|kind| kind == "lash.codex.websocket_request")
            })
            .cloned()
            .collect()
    }

    fn websocket_test_provider(
        transport: CodexTransport,
        responses_url: String,
        websocket_url: String,
    ) -> CodexProvider {
        CodexProvider::new("access", "refresh", 0)
            .with_transport(transport)
            .with_options(ProviderOptions {
                reliability: ProviderReliability::codex()
                    .request_timeout(Some(RequestTimeout::Millis(5_000)))
                    .stream_chunk_timeout_ms(Some(50)),
                ..ProviderOptions::default()
            })
            .with_endpoint_urls(responses_url, websocket_url)
    }

    fn assistant_message_with_meta(message_id: &str, text: &str) -> LlmMessage {
        LlmMessage::new(
            LlmRole::Assistant,
            vec![lash_core::llm::types::LlmContentBlock::Text {
                text: text.into(),
                response_meta: Some(ResponseTextMeta {
                    id: Some(message_id.to_string()),
                    status: Some("completed".to_string()),
                    phase: Some("final_answer".to_string()),
                    ..ResponseTextMeta::default()
                }),
                cache_breakpoint: false,
            }],
        )
    }

    struct HttpSseServer {
        url: String,
        captured: Arc<Mutex<Vec<String>>>,
        task: JoinHandle<()>,
    }

    impl HttpSseServer {
        fn captured(&self) -> Vec<String> {
            self.captured
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .clone()
        }

        fn captured_len(&self) -> usize {
            self.captured
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .len()
        }
    }

    impl Drop for HttpSseServer {
        fn drop(&mut self) {
            self.task.abort();
        }
    }

    async fn spawn_http_sse(
        response_id: &'static str,
        message_id: &'static str,
        text: &'static str,
    ) -> HttpSseServer {
        spawn_http_sse_sequence(vec![(response_id, message_id, text)]).await
    }

    async fn spawn_http_sse_sequence(
        responses: Vec<(&'static str, &'static str, &'static str)>,
    ) -> HttpSseServer {
        let listener = TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("bind http");
        let addr = listener.local_addr().expect("http addr");
        let captured = Arc::new(Mutex::new(Vec::new()));
        let task_captured = Arc::clone(&captured);
        let task = tokio::spawn(async move {
            for (response_id, message_id, text) in responses {
                let Ok((mut stream, _)) = listener.accept().await else {
                    return;
                };
                let mut request = Vec::new();
                let mut buf = [0u8; 1024];
                loop {
                    let Ok(n) = stream.read(&mut buf).await else {
                        return;
                    };
                    if n == 0 {
                        return;
                    }
                    request.extend_from_slice(&buf[..n]);
                    if request.windows(4).any(|window| window == b"\r\n\r\n") {
                        break;
                    }
                }
                task_captured
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner)
                    .push(String::from_utf8_lossy(&request).into_owned());
                let item = assistant_item(message_id, text);
                let body = format!(
                    "data: {}\n\ndata: {}\n\n",
                    json!({"type":"response.output_item.done","output_index":0,"item":item}),
                    json!({"type":"response.completed","response":{"id":response_id,"status":"completed","output":[assistant_item(message_id, text)],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}})
                );
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                    body.len(),
                    body
                );
                let _ = stream.write_all(response.as_bytes()).await;
            }
        });
        HttpSseServer {
            url: format!("http://{addr}/codex/responses"),
            captured,
            task,
        }
    }

    #[test]
    fn codex_null_incomplete_details_does_not_map_to_output_limit() {
        let terminal_reason = openai_terminal_reason_from_response_value(
            &json!({"status":"completed","incomplete_details":null}),
            &[LlmOutputPart::Text {
                text: "Hi".to_string(),
                response_meta: None,
            }],
        );

        assert_eq!(terminal_reason, LlmTerminalReason::Stop);
    }

    #[test]
    fn codex_content_filter_incomplete_maps_to_content_filter() {
        let terminal_reason = openai_terminal_reason_from_response_value(
            &json!({"status":"incomplete","incomplete_details":{"reason":"content_filter"}}),
            &[],
        );

        assert_eq!(terminal_reason, LlmTerminalReason::ContentFilter);
    }

    #[test]
    fn codex_request_body_emits_reasoning_from_capability_variant() {
        let mut req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        req.model = "custom-codex-model".to_string();
        req.model_variant = lash_core::provider::ReasoningSelection::Effort("high".to_string());
        req.model_capability = reasoning_capability();

        let body = CodexProvider::new("access", "refresh", 0)
            .build_request_body(&req, true)
            .unwrap();

        assert_eq!(body["reasoning"], json!({ "effort": "high" }));
    }

    #[test]
    fn codex_request_body_emits_none_effort_for_disabled_selection() {
        let mut req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        req.model_variant = lash_core::provider::ReasoningSelection::Disabled;
        req.model_capability = reasoning_capability();

        let body = CodexProvider::new("access", "refresh", 0)
            .build_request_body(&req, true)
            .unwrap();

        assert_eq!(body["reasoning"], json!({ "effort": "none" }));
    }

    #[test]
    fn codex_request_body_omits_reasoning_without_capability() {
        let mut req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        req.model = "custom-codex-model".to_string();
        req.model_variant = lash_core::provider::ReasoningSelection::Effort("high".to_string());

        let body = CodexProvider::new("access", "refresh", 0)
            .build_request_body(&req, true)
            .unwrap();

        assert!(body.get("reasoning").is_none());
    }

    #[test]
    fn codex_request_body_exposes_reasoning_summary_only_when_configured() {
        let mut req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        req.model_variant = lash_core::provider::ReasoningSelection::Effort("medium".to_string());
        req.model_capability = reasoning_capability();

        let hidden = CodexProvider::new("access", "refresh", 0)
            .build_request_body(&req, true)
            .unwrap();
        assert_eq!(hidden["reasoning"], json!({ "effort": "medium" }));

        let exposed = CodexProvider::new("access", "refresh", 0)
            .with_options(ProviderOptions {
                expose_thinking: true,
                ..ProviderOptions::default()
            })
            .build_request_body(&req, true)
            .unwrap();
        assert_eq!(exposed["reasoning"]["summary"], "auto");
    }

    #[test]
    fn codex_request_omits_output_token_cap() {
        let provider = CodexProvider::new("access", "refresh", 0).with_options(ProviderOptions {
            max_output_tokens: Some(9_999),
            ..ProviderOptions::default()
        });
        let provider_limited = provider
            .build_request_body(
                &request(vec![LlmMessage::text(LlmRole::User, "hello")]),
                false,
            )
            .unwrap();
        assert!(provider_limited.get("max_output_tokens").is_none());

        let mut req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        req.generation.output_token_cap = NonZeroUsize::new(2_048);
        let request_limited = provider.build_request_body(&req, false).unwrap();
        assert!(request_limited.get("max_output_tokens").is_none());
    }

    #[test]
    fn codex_error_summary_uses_top_level_detail() {
        let summary =
            CodexProvider::codex_error_summary(400, r#"{"detail":"Unsupported parameter: foo"}"#);
        assert_eq!(
            summary.as_deref(),
            Some("Codex request failed with 400: Unsupported parameter: foo")
        );
    }

    #[test]
    fn response_failed_server_error_is_retryable() {
        let mut state = CodexStreamState::default();
        let err = CodexProvider::process_sse_event(
            r#"{"type":"response.failed","response":{"status":"failed","error":{"code":"server_error","message":"internal stream ended unexpectedly"}}}"#,
            &mut state,
            None,
        )
        .unwrap_err();

        assert!(err.retryable);
        assert_eq!(err.message, "internal stream ended unexpectedly");
    }

    #[test]
    fn codex_request_uses_openai_schema_projection() {
        let mut req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        req.tools = Arc::new(vec![LlmToolSpec {
            name: "empty".to_string(),
            description: "Empty".to_string(),
            input_schema: json!({"type": "object"}).into(),
            output_schema: json!({}).into(),
        }]);
        req.output_spec = Some(LlmOutputSpec::JsonSchema(LlmJsonSchema {
            name: "result".to_string(),
            schema: json!({
                "type": "object",
                "properties": { "summary": { "type": "string" } }
            })
            .into(),
            strict: true,
        }));

        let body = CodexProvider::new("access", "refresh", 0)
            .build_request_body(&req, false)
            .unwrap();
        assert_eq!(body["tools"][0]["parameters"]["properties"], json!({}));
        assert_eq!(
            body["text"]["format"]["schema"]["required"],
            json!(["summary"])
        );
        assert_eq!(
            body["text"]["format"]["schema"]["additionalProperties"],
            false
        );
    }

    #[test]
    fn codex_request_history_preserves_assistant_message_metadata() {
        let req = request(vec![LlmMessage::new(
            LlmRole::Assistant,
            vec![lash_core::llm::types::LlmContentBlock::Text {
                text: "final".into(),
                response_meta: Some(ResponseTextMeta {
                    id: Some("msg_1".to_string()),
                    status: Some("completed".to_string()),
                    phase: Some("final_answer".to_string()),
                    ..ResponseTextMeta::default()
                }),
                cache_breakpoint: false,
            }],
        )]);

        let body = CodexProvider::new("access", "refresh", 0)
            .build_request_body(&req, false)
            .unwrap();

        assert_eq!(body["input"][0]["type"], "message");
        assert_eq!(body["input"][0]["id"], "msg_1");
        assert_eq!(body["input"][0]["status"], "completed");
        assert_eq!(body["input"][0]["phase"], "final_answer");
        assert_eq!(body["input"][0]["content"][0]["type"], "output_text");
        assert!(body["input"][0]["content"][0]["annotations"].is_array());
    }

    #[test]
    fn codex_cached_continuation_sends_delta_after_prior_request_and_response_items() {
        let provider = CodexProvider::new("access", "refresh", 0)
            .with_transport(CodexTransport::WebsocketCached);
        let first = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        let first_body = provider.build_request_body(&first, true).unwrap();
        let assistant_item = json!({
            "type": "message",
            "id": "msg_1",
            "role": "assistant",
            "status": "completed",
            "phase": "final_answer",
            "content": [{"type": "output_text", "text": "answer", "annotations": []}]
        });
        let continuation = CodexProvider::continuation_from_response(
            &first_body,
            &json!({
                "id": "resp_1",
                "status": "completed",
                "output": [assistant_item]
            }),
        )
        .expect("completed continuation");

        let second = request(vec![
            LlmMessage::text(LlmRole::User, "hello"),
            LlmMessage::new(
                LlmRole::Assistant,
                vec![lash_core::llm::types::LlmContentBlock::Text {
                    text: "answer".into(),
                    response_meta: Some(ResponseTextMeta {
                        id: Some("msg_1".to_string()),
                        status: Some("completed".to_string()),
                        phase: Some("final_answer".to_string()),
                        ..ResponseTextMeta::default()
                    }),
                    cache_breakpoint: false,
                }],
            ),
            LlmMessage::text(LlmRole::User, "next"),
        ]);
        let second_body = provider.build_request_body(&second, true).unwrap();
        let cached_body =
            CodexProvider::cached_websocket_body(&continuation, &second_body).expect("cached body");

        assert_eq!(cached_body["previous_response_id"], "resp_1");
        assert_eq!(
            cached_body["input"].as_array().expect("delta input").len(),
            1
        );
        assert_eq!(cached_body["input"][0]["role"], "user");
        assert_eq!(cached_body["input"][0]["content"][0]["text"], "next");
    }

    #[test]
    fn codex_websocket_cache_miss_reasons_are_explicit() {
        let provider = CodexProvider::new("access", "refresh", 0)
            .with_transport(CodexTransport::WebsocketCached);
        let first = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        let first_body = provider.build_request_body(&first, true).unwrap();
        let continuation = CodexProvider::continuation_from_response(
            &first_body,
            &json!({
                "id": "resp_1",
                "status": "completed",
                "output": [assistant_item("msg_1", "answer")]
            }),
        )
        .expect("completed continuation");

        let mut fingerprint_mismatch_body = first_body.clone();
        fingerprint_mismatch_body["model"] = json!("gpt-5-codex");
        let fingerprint_plan =
            provider.websocket_request_plan(&fingerprint_mismatch_body, Some(&continuation), true);
        assert!(!fingerprint_plan.cached);
        assert_eq!(
            fingerprint_plan.cache_miss_reason,
            Some("body_fingerprint_mismatch")
        );

        let prefix_mismatch = request(vec![
            LlmMessage::text(LlmRole::User, "hello"),
            LlmMessage::text(LlmRole::User, "next without prior assistant"),
        ]);
        let prefix_mismatch_body = provider.build_request_body(&prefix_mismatch, true).unwrap();
        let prefix_plan =
            provider.websocket_request_plan(&prefix_mismatch_body, Some(&continuation), true);
        assert!(!prefix_plan.cached);
        assert_eq!(prefix_plan.cache_miss_reason, Some("input_prefix_mismatch"));
    }

    #[test]
    fn codex_websocket_request_uses_response_create_event_shape() {
        let provider = CodexProvider::new("access", "refresh", 0);
        let req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        let body = provider.build_request_body(&req, true).unwrap();
        let websocket_body = CodexProvider::websocket_create_request(&body);

        assert_eq!(websocket_body["type"], "response.create");
        assert_eq!(websocket_body["model"], body["model"]);
        assert_eq!(websocket_body["input"], body["input"]);
        assert_eq!(websocket_body["stream"], true);
        assert!(websocket_body.get("response").is_none());
    }

    #[test]
    fn codex_websocket_request_keeps_cached_previous_response_id() {
        let provider = CodexProvider::new("access", "refresh", 0);
        let req = request(vec![LlmMessage::text(LlmRole::User, "next")]);
        let mut body = provider.build_request_body(&req, true).unwrap();
        body["previous_response_id"] = json!("resp_1");
        body["input"] = json!([]);

        let websocket_body = CodexProvider::websocket_create_request(&body);

        assert_eq!(websocket_body["type"], "response.create");
        assert_eq!(websocket_body["previous_response_id"], "resp_1");
        assert_eq!(websocket_body["input"], json!([]));
    }

    #[test]
    fn codex_websocket_scope_cache_prunes_idle_entries_and_caps_oldest() {
        let now = Instant::now();
        let mut sessions = CodexWebsocketSessions::default();
        sessions.by_scope.insert(
            "idle".to_string(),
            CodexWebsocketSessionEntry {
                connection: None,
                continuation: None,
                busy: false,
                last_used: now - SESSION_WEBSOCKET_CACHE_TTL - Duration::from_secs(1),
                credential_generation: 0,
            },
        );
        sessions.by_scope.insert(
            "busy".to_string(),
            CodexWebsocketSessionEntry {
                connection: None,
                continuation: None,
                busy: true,
                last_used: now - SESSION_WEBSOCKET_CACHE_TTL - Duration::from_secs(1),
                credential_generation: 0,
            },
        );

        CodexProvider::prune_idle_websocket_sessions(&mut sessions);

        assert!(!sessions.by_scope.contains_key("idle"));
        assert!(sessions.by_scope.contains_key("busy"));

        sessions.by_scope.clear();
        for index in 0..(MAX_SESSION_WEBSOCKET_CACHE_ENTRIES + 3) {
            sessions.by_scope.insert(
                format!("scope-{index}"),
                CodexWebsocketSessionEntry {
                    connection: None,
                    continuation: None,
                    busy: false,
                    last_used: now - Duration::from_secs((100 - index) as u64),
                    credential_generation: 0,
                },
            );
        }

        CodexProvider::enforce_websocket_session_cache_cap(&mut sessions);

        assert_eq!(sessions.by_scope.len(), MAX_SESSION_WEBSOCKET_CACHE_ENTRIES);
        assert!(!sessions.by_scope.contains_key("scope-0"));
        assert!(sessions.by_scope.contains_key(&format!(
            "scope-{}",
            MAX_SESSION_WEBSOCKET_CACHE_ENTRIES + 2
        )));
    }

    #[test]
    fn codex_websocket_scope_cache_evicts_rotated_credentials() {
        let mut sessions = CodexWebsocketSessions::default();
        sessions.by_scope.insert(
            "old".to_string(),
            CodexWebsocketSessionEntry {
                connection: None,
                continuation: None,
                busy: false,
                last_used: Instant::now(),
                credential_generation: 4,
            },
        );
        sessions.by_scope.insert(
            "current".to_string(),
            CodexWebsocketSessionEntry {
                connection: None,
                continuation: None,
                busy: false,
                last_used: Instant::now(),
                credential_generation: 5,
            },
        );

        CodexProvider::evict_websocket_sessions_for_generation(&mut sessions, 5);

        assert!(!sessions.by_scope.contains_key("old"));
        assert!(sessions.by_scope.contains_key("current"));
    }

    async fn assert_trace_cached_delta_for_transport(transport: CodexTransport) {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::Complete {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "answer",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "done",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            transport,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );
        let trace = Arc::new(Mutex::new(Vec::new()));

        provider
            .complete(traced_request(
                vec![LlmMessage::text(LlmRole::User, "hello")],
                Arc::clone(&trace),
            ))
            .await
            .expect("first response");
        let response = provider
            .complete(traced_request(
                vec![
                    LlmMessage::text(LlmRole::User, "hello"),
                    assistant_message_with_meta("msg_1", "answer"),
                    LlmMessage::text(LlmRole::User, "next"),
                ],
                Arc::clone(&trace),
            ))
            .await
            .expect("cached follow-up response");

        assert_eq!(response.full_text, "done");
        let diagnostics = websocket_diagnostics(&trace);
        assert_eq!(diagnostics.len(), 2, "{transport:?}");
        assert_eq!(diagnostics[0]["transport"], format!("{transport:?}"));
        assert_eq!(diagnostics[0]["cached_request"], false);
        assert_eq!(diagnostics[0]["cache_miss_reason"], "missing_continuation");
        assert_eq!(diagnostics[1]["transport"], format!("{transport:?}"));
        assert_eq!(diagnostics[1]["reused_connection"], true);
        assert_eq!(diagnostics[1]["cached_request"], true);
        assert_eq!(diagnostics[1]["previous_response_id"], "resp_1");
        assert_eq!(diagnostics[1]["sent_input_items"], 1);
        assert_eq!(diagnostics[1]["retry_after_stale_previous_response"], false);
    }

    async fn assert_trace_stale_retry_for_transport(transport: CodexTransport) {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::Complete {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "answer",
            },
            ScriptedWsAction::Error {
                message: "Previous response with id 'resp_1' not found",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "recovered",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            transport,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );
        let trace = Arc::new(Mutex::new(Vec::new()));

        provider
            .complete(traced_request(
                vec![LlmMessage::text(LlmRole::User, "hello")],
                Arc::clone(&trace),
            ))
            .await
            .expect("first response");
        let response = provider
            .complete(traced_request(
                vec![
                    LlmMessage::text(LlmRole::User, "hello"),
                    assistant_message_with_meta("msg_1", "answer"),
                    LlmMessage::text(LlmRole::User, "next"),
                ],
                Arc::clone(&trace),
            ))
            .await
            .expect("stale retry response");

        assert_eq!(response.full_text, "recovered");
        let diagnostics = websocket_diagnostics(&trace);
        assert_eq!(diagnostics.len(), 3, "{transport:?}");
        assert_eq!(diagnostics[1]["reused_connection"], true);
        assert_eq!(diagnostics[1]["cached_request"], true);
        assert_eq!(diagnostics[1]["previous_response_id"], "resp_1");
        assert_eq!(diagnostics[2]["cached_request"], false);
        assert_eq!(diagnostics[2]["cache_miss_reason"], "disabled");
        assert_eq!(diagnostics[2]["retry_after_stale_previous_response"], true);
        assert!(
            diagnostics[2]
                .get("previous_response_id")
                .is_none_or(Value::is_null)
        );
    }

    #[tokio::test]
    async fn codex_scripted_websocket_trace_diagnostics_cover_cached_delta_and_stale_retry() {
        for transport in [CodexTransport::WebsocketCached, CodexTransport::Auto] {
            assert_trace_cached_delta_for_transport(transport).await;
            assert_trace_stale_retry_for_transport(transport).await;
        }
    }

    #[tokio::test]
    async fn codex_scripted_websocket_full_turn_sends_response_create() {
        let ws = spawn_scripted_websocket(vec![ScriptedWsAction::Complete {
            response_id: "resp_1",
            message_id: "msg_1",
            text: "ok",
        }])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::Websocket,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        let response = provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("websocket response");

        assert_eq!(response.full_text, "ok");
        let captured = ws.captured();
        assert_eq!(captured.len(), 1);
        assert_eq!(captured[0]["type"], "response.create");
        assert!(captured[0].get("previous_response_id").is_none());
        let headers = ws.handshakes();
        assert_eq!(headers.len(), 1);
        let header = |name: &str| {
            headers[0]
                .iter()
                .find_map(|(header_name, value)| (header_name == name).then_some(value.as_str()))
        };
        assert_eq!(header("session-id"), Some("session-1"));
        assert_eq!(
            header("x-client-request-id"),
            Some("session-1:request:test")
        );
        assert_eq!(header("session_id"), None);
    }

    #[tokio::test]
    async fn codex_scripted_websocket_cached_follow_up_omits_previous_assistant_output() {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::Complete {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "answer",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "done",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::WebsocketCached,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("first response");
        let second = request(vec![
            LlmMessage::text(LlmRole::User, "hello"),
            LlmMessage::new(
                LlmRole::Assistant,
                vec![lash_core::llm::types::LlmContentBlock::Text {
                    text: "answer".into(),
                    response_meta: Some(ResponseTextMeta {
                        id: Some("msg_1".to_string()),
                        status: Some("completed".to_string()),
                        phase: Some("final_answer".to_string()),
                        ..ResponseTextMeta::default()
                    }),
                    cache_breakpoint: false,
                }],
            ),
            LlmMessage::text(LlmRole::User, "next"),
        ]);
        let response = provider.complete(second).await.expect("second response");

        assert_eq!(response.full_text, "done");
        assert!(
            response
                .http_summary
                .as_deref()
                .unwrap_or_default()
                .contains("cached=true")
        );
        let captured = ws.captured();
        assert_eq!(captured.len(), 2);
        assert_eq!(captured[1]["previous_response_id"], "resp_1");
        assert_eq!(captured[1]["input"].as_array().unwrap().len(), 1);
        assert_eq!(captured[1]["input"][0]["content"][0]["text"], "next");
    }

    #[tokio::test]
    async fn codex_provider_close_sends_websocket_close_frame_for_cached_session() {
        let ws = spawn_scripted_websocket(vec![ScriptedWsAction::Complete {
            response_id: "resp_1",
            message_id: "msg_1",
            text: "answer",
        }])
        .await;
        let provider = websocket_test_provider(
            CodexTransport::WebsocketCached,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        // A completed turn leaves a reusable WebSocket session cached.
        let mut running = provider.clone();
        running
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("first response");
        assert_eq!(ws.close_frame_count(), 0, "no close before shutdown");

        // The host-callable close drains the cache with a proper Close frame,
        // not a bare TCP drop. The cache is shared across clones, so closing the
        // retained clone releases the socket the running handle cached.
        provider.close().await.expect("provider close");

        let deadline = Instant::now() + Duration::from_secs(5);
        while ws.close_frame_count() == 0 && Instant::now() < deadline {
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        assert_eq!(
            ws.close_frame_count(),
            1,
            "close() must send a WebSocket Close frame to the cached session"
        );

        // The cache is empty after close.
        assert!(
            provider
                .websocket_sessions
                .inner
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .by_scope
                .is_empty(),
            "close() drains the session cache"
        );
    }

    #[tokio::test]
    async fn codex_provider_close_drains_a_dead_cached_socket_within_bound() {
        // A peer that closed its side leaves a dead socket in the cache. The
        // bounded, best-effort per-socket close must tolerate it: close() returns
        // promptly and still empties the cache, so a wedged socket can never fail
        // the drain or stall the sockets queued behind it.
        let ws = spawn_scripted_websocket(vec![ScriptedWsAction::CompleteAndClose {
            response_id: "resp_1",
            message_id: "msg_1",
            text: "answer",
        }])
        .await;
        let provider = websocket_test_provider(
            CodexTransport::WebsocketCached,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        let mut running = provider.clone();
        running
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("first response");
        // Let the peer's Close frame land so the cached socket is genuinely dead,
        // without a reuse ever polling (and evicting) it first.
        tokio::time::sleep(Duration::from_millis(20)).await;
        assert_eq!(
            provider
                .websocket_sessions
                .inner
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .by_scope
                .len(),
            1,
            "the completed turn leaves a (now dead) socket cached for the drain"
        );

        // An unbounded close on a wedged socket could hang here; the per-socket
        // timeout keeps the drain moving. The outer guard turns a regression into
        // a failure instead of hanging the whole suite.
        let started = Instant::now();
        tokio::time::timeout(Duration::from_secs(20), provider.close())
            .await
            .expect("close() must not hang draining a dead cached socket")
            .expect("provider close");
        assert!(
            started.elapsed() < Duration::from_secs(10),
            "each socket close is bounded, drain took {:?}",
            started.elapsed()
        );
        assert!(
            provider
                .websocket_sessions
                .inner
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .by_scope
                .is_empty(),
            "close() drains the cache even when a cached socket is dead"
        );
    }

    #[tokio::test]
    async fn codex_scripted_websocket_same_session_different_frame_does_not_reuse_continuation() {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::Complete {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "answer",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "done",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::WebsocketCached,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("first response");
        let mut second = request(vec![
            LlmMessage::text(LlmRole::User, "hello"),
            LlmMessage::new(
                LlmRole::Assistant,
                vec![lash_core::llm::types::LlmContentBlock::Text {
                    text: "answer".into(),
                    response_meta: Some(ResponseTextMeta {
                        id: Some("msg_1".to_string()),
                        status: Some("completed".to_string()),
                        phase: Some("final_answer".to_string()),
                        ..ResponseTextMeta::default()
                    }),
                    cache_breakpoint: false,
                }],
            ),
            LlmMessage::text(LlmRole::User, "next"),
        ]);
        second.scope = LlmRequestScope::new(
            "session-1",
            "session-1:frame:other",
            "session-1:request:other",
        );
        let response = provider.complete(second).await.expect("second response");

        assert_eq!(response.full_text, "done");
        assert!(
            response
                .http_summary
                .as_deref()
                .unwrap_or_default()
                .contains("cache_miss=missing_continuation")
        );
        let captured = ws.captured();
        assert_eq!(captured.len(), 2);
        assert!(captured[1].get("previous_response_id").is_none());
        assert_eq!(captured[1]["input"].as_array().unwrap().len(), 3);
        let handshakes = ws.handshakes();
        assert_eq!(
            handshakes[1]
                .iter()
                .find_map(|(name, value)| (name == "session-id").then_some(value.as_str())),
            Some("session-1")
        );
        assert_eq!(
            handshakes[1].iter().find_map(|(name, value)| {
                (name == "x-client-request-id").then_some(value.as_str())
            }),
            Some("session-1:request:other")
        );
    }

    #[tokio::test]
    async fn codex_scripted_websocket_stale_previous_response_retries_full_context_once() {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::Complete {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "answer",
            },
            ScriptedWsAction::Error {
                message: "Previous response with id 'resp_1' not found",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "recovered",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::WebsocketCached,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("first response");
        let second = request(vec![
            LlmMessage::text(LlmRole::User, "hello"),
            LlmMessage::new(
                LlmRole::Assistant,
                vec![lash_core::llm::types::LlmContentBlock::Text {
                    text: "answer".into(),
                    response_meta: Some(ResponseTextMeta {
                        id: Some("msg_1".to_string()),
                        status: Some("completed".to_string()),
                        phase: Some("final_answer".to_string()),
                        ..ResponseTextMeta::default()
                    }),
                    cache_breakpoint: false,
                }],
            ),
            LlmMessage::text(LlmRole::User, "next"),
        ]);
        let full_body = provider.build_request_body(&second, true).unwrap();
        let response = provider
            .complete(second)
            .await
            .expect("stale retry response");

        assert_eq!(response.full_text, "recovered");
        assert!(
            response
                .http_summary
                .as_deref()
                .unwrap_or_default()
                .contains("retry_after_stale=true")
        );
        let captured = ws.captured();
        assert_eq!(captured.len(), 3);
        assert_eq!(captured[1]["previous_response_id"], "resp_1");
        assert!(captured[2].get("previous_response_id").is_none());
        assert_eq!(captured[2]["input"], full_body["input"]);
    }

    #[tokio::test]
    async fn codex_scripted_websocket_dead_reused_socket_reconnects_full_context() {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::CompleteAndClose {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "answer",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "reconnected",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::WebsocketCached,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("first response");
        tokio::time::sleep(Duration::from_millis(20)).await;
        let second = request(vec![
            LlmMessage::text(LlmRole::User, "hello"),
            LlmMessage::new(
                LlmRole::Assistant,
                vec![lash_core::llm::types::LlmContentBlock::Text {
                    text: "answer".into(),
                    response_meta: Some(ResponseTextMeta {
                        id: Some("msg_1".to_string()),
                        status: Some("completed".to_string()),
                        phase: Some("final_answer".to_string()),
                        ..ResponseTextMeta::default()
                    }),
                    cache_breakpoint: false,
                }],
            ),
            LlmMessage::text(LlmRole::User, "next"),
        ]);
        let full_body = provider.build_request_body(&second, true).unwrap();
        let response = provider
            .complete(second)
            .await
            .expect("dead reused socket reconnect response");

        assert_eq!(response.full_text, "reconnected");
        assert!(
            response
                .http_summary
                .as_deref()
                .unwrap_or_default()
                .contains("retry_after_dead_reused=true")
        );
        let captured = ws.captured();
        assert_eq!(captured.len(), 2);
        assert!(captured[1].get("previous_response_id").is_none());
        assert_eq!(captured[1]["input"], full_body["input"]);
        assert_eq!(ws.handshakes().len(), 2);
    }

    #[tokio::test]
    async fn codex_scripted_websocket_incomplete_terminal_response_is_not_cached() {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::Incomplete {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "partial",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "fresh",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::WebsocketCached,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );

        provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("incomplete terminal response");
        let second = request(vec![
            LlmMessage::text(LlmRole::User, "hello"),
            assistant_message_with_meta("msg_1", "partial"),
            LlmMessage::text(LlmRole::User, "next"),
        ]);
        let full_body = provider.build_request_body(&second, true).unwrap();
        let response = provider
            .complete(second)
            .await
            .expect("fresh response after incomplete terminal");

        assert_eq!(response.full_text, "fresh");
        assert!(
            response
                .http_summary
                .as_deref()
                .unwrap_or_default()
                .contains("cache_miss=missing_continuation")
        );
        let captured = ws.captured();
        assert_eq!(captured.len(), 2);
        assert!(captured[1].get("previous_response_id").is_none());
        assert_eq!(captured[1]["input"], full_body["input"]);
    }

    #[tokio::test]
    async fn codex_auto_with_distinct_scopes_uses_uncached_websockets() {
        let ws = spawn_scripted_websocket(vec![
            ScriptedWsAction::Complete {
                response_id: "resp_1",
                message_id: "msg_1",
                text: "one",
            },
            ScriptedWsAction::Complete {
                response_id: "resp_2",
                message_id: "msg_2",
                text: "two",
            },
        ])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::Auto,
            "http://127.0.0.1:9/unused".to_string(),
            ws.url.clone(),
        );
        let mut first = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        first.scope = LlmRequestScope::new("direct-a", "direct-a:frame", "direct-a:request");
        let mut second = request(vec![LlmMessage::text(LlmRole::User, "next")]);
        second.scope = LlmRequestScope::new("direct-b", "direct-b:frame", "direct-b:request");

        let first_response = provider.complete(first).await.expect("first response");
        let second_response = provider.complete(second).await.expect("second response");

        assert_eq!(first_response.full_text, "one");
        assert_eq!(second_response.full_text, "two");
        assert!(
            second_response
                .http_summary
                .as_deref()
                .unwrap_or_default()
                .contains("reused=false")
        );
        assert_eq!(ws.captured().len(), 2);
        let handshakes = ws.handshakes();
        assert_eq!(handshakes.len(), 2);
        fn header<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> {
            headers
                .iter()
                .find_map(|(header_name, value)| (header_name == name).then_some(value.as_str()))
        }
        assert_eq!(header(&handshakes[0], "session-id"), Some("direct-a"));
        assert_eq!(
            header(&handshakes[0], "x-client-request-id"),
            Some("direct-a:request")
        );
        assert_eq!(header(&handshakes[1], "session-id"), Some("direct-b"));
        assert_eq!(
            header(&handshakes[1], "x-client-request-id"),
            Some("direct-b:request")
        );
    }

    #[tokio::test]
    async fn codex_scripted_websocket_mid_stream_failure_does_not_fallback() {
        let ws = spawn_scripted_websocket(vec![ScriptedWsAction::MidStreamError {
            message_id: "msg_1",
            text: "partial",
            message: "stream exploded",
        }])
        .await;
        let http = spawn_http_sse("resp_http", "msg_http", "fallback").await;
        let mut provider =
            websocket_test_provider(CodexTransport::Auto, http.url.clone(), ws.url.clone());

        let err = provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect_err("mid-stream websocket failure");

        assert!(err.message.contains("stream exploded"));
        assert_eq!(http.captured_len(), 0);
        assert_eq!(ws.captured().len(), 1);
    }

    #[tokio::test]
    async fn codex_scripted_websocket_idle_before_start_falls_back_to_sse() {
        let ws = spawn_scripted_websocket(vec![ScriptedWsAction::IdleBeforeStart]).await;
        let http = spawn_http_sse("resp_http", "msg_http", "fallback").await;
        let mut provider =
            websocket_test_provider(CodexTransport::Auto, http.url.clone(), ws.url.clone());

        let response = provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("sse fallback response");

        assert_eq!(response.full_text, "fallback");
        assert_eq!(ws.captured().len(), 1);
        assert_eq!(http.captured_len(), 1);
    }

    #[tokio::test]
    async fn codex_auto_skips_websocket_while_session_fallback_is_active() {
        let http = spawn_http_sse_sequence(vec![
            ("resp_http_1", "msg_http_1", "fallback-one"),
            ("resp_http_2", "msg_http_2", "fallback-two"),
        ])
        .await;
        let mut provider = websocket_test_provider(
            CodexTransport::Auto,
            http.url.clone(),
            "ws://127.0.0.1:1/codex/responses".to_string(),
        );

        let first = provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect("first SSE fallback response");

        assert_eq!(first.full_text, "fallback-one");
        assert!(
            provider
                .websocket_fallback_reason(&request(vec![LlmMessage::text(LlmRole::User, "hello")]))
                .is_some()
        );

        let ws = spawn_scripted_websocket(vec![ScriptedWsAction::Complete {
            response_id: "resp_ws",
            message_id: "msg_ws",
            text: "should-not-run",
        }])
        .await;
        provider.websocket_url = ws.url.clone();
        let second = provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "next")]))
            .await
            .expect("second SSE fallback response");

        assert_eq!(second.full_text, "fallback-two");
        assert_eq!(ws.captured().len(), 0);
        assert_eq!(http.captured_len(), 2);
        let sse_request = http.captured().remove(0);
        assert!(sse_request.contains("session-id: session-1"));
        assert!(sse_request.contains("x-client-request-id: session-1:request:test"));
        assert!(!sse_request.contains("session_id:"));
    }

    #[tokio::test]
    async fn codex_scripted_websocket_idle_after_output_is_terminal_error() {
        let ws = spawn_scripted_websocket(vec![ScriptedWsAction::IdleAfterStart {
            message_id: "msg_1",
            text: "partial",
        }])
        .await;
        let http = spawn_http_sse("resp_http", "msg_http", "fallback").await;
        let mut provider =
            websocket_test_provider(CodexTransport::Auto, http.url.clone(), ws.url.clone());

        let err = provider
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect_err("idle after output");

        assert_eq!(err.code.as_deref(), Some("websocket_idle_timeout"));
        assert_eq!(http.captured_len(), 0);
        assert_eq!(ws.captured().len(), 1);
    }

    #[tokio::test]
    async fn codex_websocket_clean_eof_requires_terminal_event_unless_explicitly_tolerated() {
        let action = ScriptedWsAction::CloseAfterStart {
            response_id: "resp_partial",
            message_id: "msg_partial",
            text: "partial",
        };
        let strict_ws = spawn_scripted_websocket(vec![action.clone()]).await;
        let http = spawn_http_sse("resp_http", "msg_http", "unused").await;
        let mut strict = websocket_test_provider(
            CodexTransport::Websocket,
            http.url.clone(),
            strict_ws.url.clone(),
        );

        let error = strict
            .complete(request(vec![LlmMessage::text(LlmRole::User, "hello")]))
            .await
            .expect_err("clean EOF without a terminal event must fail");

        assert_eq!(
            error.code.as_deref(),
            Some("websocket_closed_before_completed")
        );
        let partial = error.partial_response.as_deref().expect("partial response");
        assert_eq!(partial.full_text, "partial");
        assert_eq!(partial.usage.input_tokens, 4);
        assert_eq!(partial.usage.output_tokens, 1);
        assert!(partial.provider_usage.is_some());
        assert_eq!(http.captured_len(), 0);

        let tolerant_ws = spawn_scripted_websocket(vec![action]).await;
        let mut tolerant = websocket_test_provider(
            CodexTransport::Websocket,
            http.url.clone(),
            tolerant_ws.url.clone(),
        );
        let mut tolerant_request = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        tolerant_request.model_capability.stream_termination =
            Some(StreamTermination::EofTolerated);
        let response = tolerant
            .complete(tolerant_request)
            .await
            .expect("explicit EOF tolerance accepts clean close");
        assert_eq!(response.full_text, "partial");
        assert_eq!(http.captured_len(), 0);
    }

    #[test]
    fn codex_schema_projection_failure_is_local_validation_error() {
        let mut req = request(vec![LlmMessage::text(LlmRole::User, "hello")]);
        req.output_spec = Some(LlmOutputSpec::JsonSchema(LlmJsonSchema {
            name: "bad".to_string(),
            schema: json!({"type": "object", "allOf": []}).into(),
            strict: true,
        }));

        let err = CodexProvider::new("access", "refresh", 0)
            .build_request_body(&req, false)
            .unwrap_err();
        assert_eq!(err.kind, ProviderFailureKind::Validation);
        assert!(err.message.contains("allOf"));
    }

    #[test]
    fn codex_stream_response_carries_raw_usage_sidecar() {
        let mut state = CodexStreamState::default();
        process_event(
            &mut state,
            json!({"type":"response.completed","response":{
                "id":"resp_usage",
                "status":"completed",
                "output":[assistant_item("msg_usage","hi")],
                "usage":{"input_tokens":3,"output_tokens":2,"total_tokens":5}
            }}),
        );

        let response = response_from_state(state);

        assert_eq!(
            response.provider_usage,
            Some(json!({"input_tokens":3,"output_tokens":2,"total_tokens":5}))
        );
        assert_eq!(response.usage.input_tokens, 3);
        assert_eq!(response.usage.output_tokens, 2);
    }

    #[test]
    fn codex_stream_assembles_single_message_item_once() {
        let mut state = CodexStreamState::default();

        process_event(
            &mut state,
            json!({"type":"response.output_item.added","item":{"type":"message","id":"msg_1","status":"in_progress","phase":"commentary"}}),
        );
        process_event(
            &mut state,
            json!({"type":"response.output_text.delta","item_id":"msg_1","delta":"Hel"}),
        );
        process_event(
            &mut state,
            json!({"type":"response.output_item.done","item":{"type":"message","id":"msg_1","status":"completed","phase":"commentary","content":[{"type":"output_text","text":"Hello"}]}}),
        );

        let response = response_from_state(state);
        assert_eq!(response.full_text, "Hello");
        assert_eq!(response.parts.len(), 1);
        assert_eq!(
            response.parts[0],
            LlmOutputPart::Text {
                text: "Hello".to_string(),
                response_meta: Some(ResponseTextMeta {
                    id: Some("msg_1".to_string()),
                    status: Some("completed".to_string()),
                    phase: Some("commentary".to_string()),
                    ..ResponseTextMeta::default()
                }),
            }
        );
    }

    #[test]
    fn codex_stream_replayed_message_item_does_not_duplicate_text() {
        let mut state = CodexStreamState::default();

        for event in [
            json!({"type":"response.output_item.added","item":{"type":"message","id":"msg_1"}}),
            json!({"type":"response.output_text.delta","item_id":"msg_1","delta":"The sentence."}),
            json!({"type":"response.output_item.done","item":{"type":"message","id":"msg_1","status":"completed","content":[{"type":"output_text","text":"The sentence."}]}}),
            json!({"type":"response.output_item.added","item":{"type":"message","id":"msg_1"}}),
            json!({"type":"response.output_item.done","item":{"type":"message","id":"msg_1","status":"completed","content":[{"type":"output_text","text":"The sentence."}]}}),
        ] {
            process_event(&mut state, event);
        }

        let response = response_from_state(state);
        assert_eq!(response.full_text, "The sentence.");
        assert_eq!(
            response
                .parts
                .iter()
                .filter(|part| matches!(part, LlmOutputPart::Text { .. }))
                .count(),
            1
        );
    }

    #[test]
    fn codex_stream_completed_response_merges_existing_message_by_id() {
        let mut state = CodexStreamState::default();

        for event in [
            json!({"type":"response.output_item.added","item":{"type":"message","id":"msg_1"}}),
            json!({"type":"response.output_text.delta","item_id":"msg_1","delta":"Final answer."}),
            json!({"type":"response.output_item.done","item":{"type":"message","id":"msg_1","status":"completed","content":[{"type":"output_text","text":"Final answer."}]}}),
            json!({"type":"response.completed","response":{"id":"resp_1","output_text":"Final answer.","output":[{"type":"message","id":"msg_1","status":"completed","content":[{"type":"output_text","text":"Final answer."}]}]}}),
        ] {
            process_event(&mut state, event);
        }

        let response = response_from_state(state);
        assert_eq!(response.full_text, "Final answer.");
        assert_eq!(response.parts.len(), 1);
    }

    #[test]
    fn codex_stream_distinct_message_ids_stay_separate_without_inserted_separator() {
        let mut state = CodexStreamState::default();

        for event in [
            json!({"type":"response.output_item.added","item":{"type":"message","id":"msg_1"}}),
            json!({"type":"response.output_text.delta","item_id":"msg_1","delta":"One."}),
            json!({"type":"response.output_item.done","item":{"type":"message","id":"msg_1","status":"completed","content":[{"type":"output_text","text":"One."}]}}),
            json!({"type":"response.output_item.added","item":{"type":"message","id":"msg_2"}}),
            json!({"type":"response.output_text.delta","item_id":"msg_2","delta":"Two."}),
            json!({"type":"response.output_item.done","item":{"type":"message","id":"msg_2","status":"completed","content":[{"type":"output_text","text":"Two."}]}}),
        ] {
            process_event(&mut state, event);
        }

        let response = response_from_state(state);
        assert_eq!(response.full_text, "One.Two.");
        assert_eq!(response.parts.len(), 2);
        assert_eq!(
            response.parts,
            vec![
                LlmOutputPart::Text {
                    text: "One.".to_string(),
                    response_meta: Some(ResponseTextMeta {
                        id: Some("msg_1".to_string()),
                        status: Some("completed".to_string()),
                        phase: None,
                        ..ResponseTextMeta::default()
                    }),
                },
                LlmOutputPart::Text {
                    text: "Two.".to_string(),
                    response_meta: Some(ResponseTextMeta {
                        id: Some("msg_2".to_string()),
                        status: Some("completed".to_string()),
                        phase: None,
                        ..ResponseTextMeta::default()
                    }),
                },
            ]
        );
    }

    #[test]
    fn codex_stream_preserves_reasoning_message_and_tool_call_once() {
        let mut state = CodexStreamState::default();
        let mut emitted_parts = Vec::new();

        for event in [
            json!({"type":"response.reasoning_summary_part.added"}),
            json!({"type":"response.reasoning_summary_text.delta","delta":"Think"}),
            json!({"type":"response.reasoning_summary_part.done"}),
            json!({"type":"response.output_item.done","item":{"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"Think"}],"encrypted_content":"enc"}}),
            json!({"type":"response.output_item.added","item":{"type":"message","id":"msg_1","phase":"final_answer"}}),
            json!({"type":"response.output_text.delta","item_id":"msg_1","delta":"Hi"}),
            json!({"type":"response.output_item.done","item":{"type":"message","id":"msg_1","status":"completed","phase":"final_answer","content":[{"type":"output_text","text":"Hi"}]}}),
            json!({"type":"response.output_item.added","item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"tool","arguments":""}}),
            json!({"type":"response.function_call_arguments.delta","item_id":"fc_1","delta":"{\"x\""}),
            json!({"type":"response.function_call_arguments.done","item_id":"fc_1","arguments":"{\"x\":1}"}),
            json!({"type":"response.output_item.done","item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"tool","arguments":"{\"x\":1}","status":"completed"}}),
        ] {
            process_event_with_parts(&mut state, event, &mut emitted_parts);
        }
        process_event_with_parts(
            &mut state,
            json!({"type":"response.completed","response":{"id":"resp_1","output":[
                {"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"Think"}],"encrypted_content":"enc"},
                {"type":"message","id":"msg_1","status":"completed","phase":"final_answer","content":[{"type":"output_text","text":"Hi"}]},
                {"type":"function_call","id":"fc_1","call_id":"call_1","name":"tool","arguments":"{\"x\":1}","status":"completed"}
            ],"output_text":"Hi"}}),
            &mut emitted_parts,
        );

        let response = response_from_state(state);
        assert_eq!(response.full_text, "Hi");
        assert_eq!(emitted_parts.len(), 1);
        assert_eq!(
            response
                .parts
                .iter()
                .filter(|part| matches!(part, LlmOutputPart::Reasoning { .. }))
                .count(),
            1
        );
        assert_eq!(
            response
                .parts
                .iter()
                .filter(|part| matches!(part, LlmOutputPart::Text { .. }))
                .count(),
            1
        );
        assert_eq!(
            response
                .parts
                .iter()
                .filter(|part| matches!(part, LlmOutputPart::ToolCall { .. }))
                .count(),
            1
        );
    }

    /// Cross-provider response-normalization conformance. Codex shares OpenAI's
    /// Responses-API normalizers (`shared::*`), so this wires those into the
    /// shared suite with Responses-API wire fixtures.
    #[cfg(feature = "testing")]
    mod conformance {
        use super::super::{PROVIDER, shared};
        use lash_core::llm::types::{LlmOutputPart, LlmTerminalReason, LlmUsage};
        use lash_llm_transport::conformance::{
            CanonicalUsage as U, ProviderNormalizer, ProviderWire, Scenario, StreamAssembly,
            provider_conformance,
        };
        use lash_llm_transport::{
            openai_terminal_reason_from_response_value, openai_usage_from_response_value,
        };
        use serde_json::{Value, json};

        struct CodexNormalizer;

        impl ProviderNormalizer for CodexNormalizer {
            fn name(&self) -> &str {
                "codex-responses"
            }

            fn wire_for(&self, scenario: Scenario) -> Option<ProviderWire> {
                let wire = match scenario {
                    Scenario::PlainTextStop => ProviderWire::body(json!({
                        "status": "completed",
                        "output": [{
                            "type": "message", "id": "msg_1", "status": "completed",
                            "content": [{ "type": "output_text", "text": "hello" }]
                        }],
                        "usage": { "input_tokens": U::BASE_INPUT, "output_tokens": U::BASE_OUTPUT }
                    })),
                    Scenario::OutputCapped => ProviderWire::body(json!({
                        "status": "incomplete",
                        "incomplete_details": { "reason": "max_output_tokens" },
                        "output": [{
                            "type": "message", "id": "msg_1", "status": "incomplete",
                            "content": [{ "type": "output_text", "text": "trunc" }]
                        }]
                    })),
                    Scenario::ContentFilter => ProviderWire::body(json!({
                        "status": "incomplete",
                        "incomplete_details": { "reason": "content_filter" },
                        "output": []
                    })),
                    Scenario::NonStreamingToolUse => ProviderWire::body(json!({
                        "status": "completed",
                        "output": [{
                            "type": "function_call", "id": "fc_1", "call_id": "call_1",
                            "name": "lookup", "arguments": "{\"q\":\"x\"}", "status": "completed"
                        }]
                    })),
                    Scenario::StreamingTextAssembly => {
                        ProviderWire::body(json!({})).with_text_stream(
                            vec![
                                r#"{"type":"response.output_text.delta","item_id":"msg_1","delta":"hello "}"#.to_string(),
                                r#"{"type":"response.output_text.delta","item_id":"msg_1","delta":"world"}"#.to_string(),
                            ],
                            "hello world",
                        )
                    }
                    Scenario::StreamingToolArgumentMerge => {
                        ProviderWire::body(json!({})).with_tool_call_stream(
                            vec![
                                r#"{"type":"response.output_item.added","item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"lookup","arguments":""}}"#.to_string(),
                                // arguments deliberately split across two delta events
                                r#"{"type":"response.function_call_arguments.delta","item_id":"fc_1","delta":"{\"q\":"}"#.to_string(),
                                r#"{"type":"response.function_call_arguments.delta","item_id":"fc_1","delta":"\"x\"}"}"#.to_string(),
                                r#"{"type":"response.output_item.done","item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"lookup","arguments":"{\"q\":\"x\"}","status":"completed"}}"#.to_string(),
                            ],
                            "lookup",
                            json!({ "q": "x" }),
                        )
                    }
                    Scenario::UsageCacheHit => ProviderWire::body(json!({
                        "status": "completed",
                        "output": [{
                            "type": "message", "id": "msg_1", "status": "completed",
                            "content": [{ "type": "output_text", "text": "ok" }]
                        }],
                        "usage": {
                            "input_tokens": U::BASE_INPUT,
                            "output_tokens": U::BASE_OUTPUT,
                            "input_tokens_details": { "cached_tokens": U::CACHED_INPUT }
                        }
                    })),
                    Scenario::UsageReasoning => ProviderWire::body(json!({
                        "status": "completed",
                        "output": [{
                            "type": "message", "id": "msg_1", "status": "completed",
                            "content": [{ "type": "output_text", "text": "ok" }]
                        }],
                        "usage": {
                            "input_tokens": U::BASE_INPUT,
                            "output_tokens": U::OUTPUT_WITH_REASONING,
                            "output_tokens_details": { "reasoning_tokens": U::REASONING }
                        }
                    })),
                    Scenario::ReasoningExtraction => ProviderWire::body(json!({
                        "status": "completed",
                        "output": [
                            {
                                "type": "reasoning", "id": "rs_1",
                                "summary": [{ "type": "summary_text", "text": "thinking about it" }]
                            },
                            {
                                "type": "message", "id": "msg_1", "status": "completed",
                                "content": [{ "type": "output_text", "text": "answer" }]
                            }
                        ]
                    }))
                    .with_reasoning_text("thinking about it"),
                    Scenario::StreamingUsageMerge => {
                        ProviderWire::body(json!({})).with_usage_merge_stream(vec![
                            // input arrives on an early event
                            format!(
                                r#"{{"type":"response.output_text.delta","delta":"hi","usage":{{"input_tokens":{}}}}}"#,
                                U::BASE_INPUT
                            ),
                            // output arrives on a later event; merge must keep input
                            format!(
                                r#"{{"type":"response.output_text.delta","delta":"!","usage":{{"output_tokens":{}}}}}"#,
                                U::BASE_OUTPUT
                            ),
                        ])
                    }
                };
                Some(wire)
            }

            fn parts_from_wire(&self, body: &Value) -> Vec<LlmOutputPart> {
                shared::response_parts_from_value(body)
            }

            fn usage_from_wire(&self, body: &Value) -> LlmUsage {
                openai_usage_from_response_value(body)
            }

            fn terminal_from_wire(
                &self,
                body: &Value,
                parts: &[LlmOutputPart],
            ) -> LlmTerminalReason {
                openai_terminal_reason_from_response_value(body, parts)
            }

            fn assemble_stream(&self, sse_events: &[String]) -> StreamAssembly {
                let mut state = shared::ResponsesStreamState::default();
                for raw in sse_events {
                    shared::process_sse_event(PROVIDER, raw, &mut state, None)
                        .expect("responses sse event parses");
                }
                StreamAssembly {
                    parts: state.response_parts(),
                    usage: state.usage.clone(),
                }
            }
        }

        #[test]
        fn codex_satisfies_provider_conformance() {
            provider_conformance(&CodexNormalizer);
        }
    }
}