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

import base64
import hashlib
import json
import logging
import re
import secrets
import time
import urllib.parse
from pathlib import Path

from playwright.sync_api import sync_playwright

import autoteam.display  # noqa: F401
from autoteam import oauth_workspace as _oauth_workspace
from autoteam.accounts import is_supported_plan, normalize_plan_type
from autoteam.admin_state import (
    get_admin_email,
    get_admin_session_token,
    get_chatgpt_account_id,
    get_chatgpt_workspace_name,
)
from autoteam.auth_storage import AUTH_DIR, ensure_auth_dir, ensure_auth_file_permissions
from autoteam.config import get_playwright_context_options, get_playwright_launch_options
from autoteam.invite import (  # SPEC-2 shared/add-phone-detection §3 — OAuth 流程复用
    RegisterBlocked,
    assert_not_blocked,
)
from autoteam.playwright_lifecycle import close_playwright_objects
from autoteam.signup_profile import SignupProfile, generate_signup_profile
from autoteam.textio import read_text, write_text

logger = logging.getLogger(__name__)

PROJECT_ROOT = Path(__file__).parent.parent.parent
SCREENSHOT_DIR = PROJECT_ROOT / "screenshots"
_OAUTH_TRACE_LIMIT = 40
_OAUTH_TRACE_KEYWORDS = (
    "/oauth/authorize",
    "/sign-in-with-chatgpt/codex",
    "choose-an-account",
    "consent",
    "organization",
    "email-verification",
    "log-in",
    "/auth/callback",
    "no_valid_organizations",
)

# Codex OAuth 配置
CODEX_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"
CODEX_AUTH_URL = "https://auth.openai.com/oauth/authorize"
CODEX_TOKEN_URL = "https://auth.openai.com/oauth/token"
CODEX_CALLBACK_PORT = 1455
CODEX_REDIRECT_URI = f"http://localhost:{CODEX_CALLBACK_PORT}/auth/callback"
_OTP_REJECTION_FILE = AUTH_DIR / "otp_rejections.json"
_OTP_REJECTION_TTL_SECONDS = 2 * 60 * 60
_OTP_REJECTION_MAX_PER_EMAIL = 100

# SPEC-2 shared/quota-classification §4.4 I5 — Codex backend 最小推理端点(用于 uninitialized_seat 二次验证)
_CODEX_SMOKE_ENDPOINT = "https://chatgpt.com/backend-api/codex/responses"
# quota 关键词:codex backend 返回 4xx 时若 body 含这些词同样视为 auth_invalid(可能是配额相关 API 错误)
_CODEX_SMOKE_QUOTA_HINTS = ("quota", "no_quota", "rate_limit", "billing", "exceeded")
# Round 11 — codex backend 拒绝 model 时的 body 关键词(4xx),用于触发 fallback model chain
_CODEX_SMOKE_MODEL_NOT_SUPPORTED_HINTS = (
    "not supported",
    "model_not_supported",
    "is not supported",
    "invalid model",
    "unknown model",
)
# Round 11 — cheap_codex_smoke 默认 instructions(短回复 + 最小 token 消耗)
_CODEX_SMOKE_DEFAULT_INSTRUCTIONS = "You are a concise assistant. Reply with one short word."


def _generate_pkce():
    """生成 PKCE code_verifier 和 code_challenge"""
    verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()
    challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()
    return verifier, challenge


def _parse_jwt_payload(token):
    """解析 JWT payload(不验证签名)"""
    parts = token.split(".")
    if len(parts) < 2:
        return {}
    payload = parts[1]
    # 补齐 base64 padding
    payload += "=" * (4 - len(payload) % 4)
    try:
        return json.loads(base64.urlsafe_b64decode(payload))
    except Exception:
        return {}


def _otp_rejection_email_key(email: str | None) -> str:
    return str(email or "").strip().lower()


def _otp_rejection_hash(email: str | None, code: str | None) -> str:
    key = f"{_otp_rejection_email_key(email)}:{str(code or '').strip()}"
    return hashlib.sha256(key.encode("utf-8")).hexdigest()


def _load_otp_rejection_store() -> dict:
    try:
        if not _OTP_REJECTION_FILE.exists():
            return {}
        data = json.loads(read_text(_OTP_REJECTION_FILE))
        return data if isinstance(data, dict) else {}
    except Exception as exc:
        logger.debug("[Codex] failed to load OTP rejection cache: %s", exc)
        return {}


def _write_otp_rejection_store(data: dict) -> None:
    try:
        ensure_auth_dir()
        write_text(_OTP_REJECTION_FILE, json.dumps(data, ensure_ascii=False, indent=2, sort_keys=True))
        ensure_auth_file_permissions(_OTP_REJECTION_FILE)
    except Exception as exc:
        logger.debug("[Codex] failed to write OTP rejection cache: %s", exc)


def _coerce_int(value):
    try:
        return int(value)
    except Exception:
        return None


def _load_recent_otp_rejections(email: str | None, *, now: float | None = None):
    now = time.time() if now is None else float(now)
    cutoff = now - _OTP_REJECTION_TTL_SECONDS
    key = _otp_rejection_email_key(email)
    data = _load_otp_rejection_store()
    records = data.get(key, [])
    if not isinstance(records, list):
        return set(), set()

    code_hashes = set()
    email_ids = set()
    kept = []
    changed = False
    for record in records:
        if not isinstance(record, dict):
            changed = True
            continue
        try:
            created_at = float(record.get("ts") or 0)
        except Exception:
            created_at = 0
        if created_at and created_at < cutoff:
            changed = True
            continue
        code_hash = str(record.get("code_hash") or "").strip()
        if code_hash:
            code_hashes.add(code_hash)
        email_id = _coerce_int(record.get("email_id"))
        if email_id is not None:
            email_ids.add(email_id)
        kept.append(record)

    if changed:
        if kept:
            data[key] = kept[-_OTP_REJECTION_MAX_PER_EMAIL:]
        else:
            data.pop(key, None)
        _write_otp_rejection_store(data)

    return code_hashes, email_ids


def _record_otp_rejection(email: str | None, code: str | None, email_id, *, now: float | None = None):
    code = str(code or "").strip()
    if not code:
        return None

    now = time.time() if now is None else float(now)
    cutoff = now - _OTP_REJECTION_TTL_SECONDS
    key = _otp_rejection_email_key(email)
    code_hash = _otp_rejection_hash(email, code)
    email_id_int = _coerce_int(email_id)

    data = _load_otp_rejection_store()
    records = data.get(key, [])
    if not isinstance(records, list):
        records = []

    kept = []
    for record in records:
        if not isinstance(record, dict):
            continue
        try:
            created_at = float(record.get("ts") or 0)
        except Exception:
            created_at = 0
        if created_at and created_at < cutoff:
            continue
        if str(record.get("code_hash") or "") == code_hash:
            continue
        if email_id_int is not None and _coerce_int(record.get("email_id")) == email_id_int:
            continue
        kept.append(record)

    kept.append({"code_hash": code_hash, "email_id": email_id_int, "ts": now})
    data[key] = kept[-_OTP_REJECTION_MAX_PER_EMAIL:]
    _write_otp_rejection_store(data)
    return code_hash


def _page_excerpt(page, limit=240) -> str:
    try:
        text = page.locator("body").inner_text(timeout=1500)
        text = re.sub(r"\s+", " ", text).strip()
        return text[:limit]
    except Exception:
        return ""


def _normalize_trace_text(value, limit=240) -> str:
    text = re.sub(r"\s+", " ", str(value or "")).strip()
    return text[:limit]


def _should_trace_oauth_network(url: str | None) -> bool:
    lowered = (url or "").lower()
    if "auth.openai.com" not in lowered and f"localhost:{CODEX_CALLBACK_PORT}" not in lowered:
        return False
    return any(keyword in lowered for keyword in _OAUTH_TRACE_KEYWORDS)


def _append_oauth_trace(trace_events: list[dict], *, kind: str, url: str, **fields) -> None:
    if not _should_trace_oauth_network(url):
        return

    entry = {"kind": kind, "url": url}
    for key, value in fields.items():
        if value is None:
            continue
        if key in {"body_excerpt", "location", "failure", "resource_type"}:
            entry[key] = _normalize_trace_text(value, limit=320)
        else:
            entry[key] = value

    trace_events.append(entry)
    if len(trace_events) > _OAUTH_TRACE_LIMIT:
        del trace_events[:-_OAUTH_TRACE_LIMIT]


def _oauth_trace_has_login_challenge(trace_events: list[dict]) -> bool:
    for entry in trace_events[-12:]:
        for key in ("url", "location"):
            value = str(entry.get(key) or "").lower()
            if "/api/accounts/login" in value or "auth.openai.com/log-in" in value:
                return True
    return False


def _classify_oauth_failure(url: str | None, body_excerpt: str = ""):
    lowered_url = (url or "").lower()
    body = (body_excerpt or "").lower()

    if "add-phone" in lowered_url:
        return "add_phone", "需要手机号验证", False
    if "verify you are human" in body or "captcha" in body:
        return "human_verification", "命中人机验证", False
    if "operation timed out" in body:
        return "oauth_timeout", "OAuth 授权页操作超时", True
    if "unsupported_country_region_territory" in body or "country, region, or territory not supported" in body:
        return "unsupported_region", "OAuth 授权接口返回不支持当前地区/出口", True
    if "choose-an-account" in lowered_url:
        return "account_selection", "停留在账号选择页", True
    if "no_valid_organizations" in body or "no valid organizations" in body:
        return "no_valid_organizations", "OAuth 授权页未选中可用 organization", True
    if "unable to load site" in body or "try again later" in body or "status page" in body:
        return "site_unavailable", "站点暂时不可用或代理异常", True
    if "email-verification" in lowered_url:
        return "email_verification", "卡在邮箱验证码页", True
    if "workspace" in lowered_url:
        return "workspace_selection", "卡在 workspace 选择页", True
    if "/auth/login" in lowered_url or "/log-in" in lowered_url or "log-in-or-create-account" in lowered_url:
        return "login_state_lost", "登录态丢失或回到了登录页", True
    return "auth_code_missing", f"未获取到 auth code(停留在 {lowered_url or 'unknown'})", True


def _screenshot(page, name):
    SCREENSHOT_DIR.mkdir(exist_ok=True)
    page.screenshot(path=str(SCREENSHOT_DIR / name), full_page=True)


def _build_auth_url(code_challenge, state):
    params = {
        "client_id": CODEX_CLIENT_ID,
        "response_type": "code",
        "redirect_uri": CODEX_REDIRECT_URI,
        "scope": "openid email profile offline_access",
        "state": state,
        "code_challenge": code_challenge,
        "code_challenge_method": "S256",
        "prompt": "consent",
    }
    return f"{CODEX_AUTH_URL}?{urllib.parse.urlencode(params)}"


def _is_workspace_selection_page(page) -> bool:
    """Compatibility wrapper around the shared OAuth workspace detector."""
    return _oauth_workspace._is_workspace_selection_page(page)


def _workspace_label_candidates(page):
    """Compatibility wrapper around shared workspace label extraction."""
    return _oauth_workspace._workspace_label_candidates(page)


def _select_team_workspace(page, workspace_name: str) -> bool:
    """Compatibility wrapper around shared Team workspace selection."""
    return _oauth_workspace._select_team_workspace(page, workspace_name)


def _exchange_auth_code(auth_code, code_verifier, fallback_email=None):
    logger.info("[Codex] 获取到 auth code,交换 token...")

    import requests

    resp = requests.post(
        CODEX_TOKEN_URL,
        data={
            "grant_type": "authorization_code",
            "client_id": CODEX_CLIENT_ID,
            "code": auth_code,
            "redirect_uri": CODEX_REDIRECT_URI,
            "code_verifier": code_verifier,
        },
        headers={"Content-Type": "application/x-www-form-urlencoded"},
    )

    if resp.status_code != 200:
        logger.error("[Codex] Token 交换失败: %d %s", resp.status_code, resp.text[:200])
        return None

    token_data = resp.json()
    id_token = token_data.get("id_token", "")
    claims = _parse_jwt_payload(id_token)
    auth_claims = claims.get("https://api.openai.com/auth", {})

    raw_plan = auth_claims.get("chatgpt_plan_type", "unknown")
    bundle = {
        "access_token": token_data.get("access_token"),
        "refresh_token": token_data.get("refresh_token"),
        "id_token": id_token,
        "account_id": auth_claims.get("chatgpt_account_id", ""),
        "email": claims.get("email", fallback_email or ""),
        # SPEC-2 shared/plan-type-whitelist §2.3:plan_type 已归一化为小写;
        # plan_type_raw 保留 OpenAI 原始字面量便于事后排查;
        # plan_supported 是白名单判定结果,下游消费方应只读该字段不再自己 .lower() 比对。
        "plan_type": normalize_plan_type(raw_plan),
        "plan_type_raw": raw_plan,
        "plan_supported": is_supported_plan(raw_plan),
        "expired": time.time() + token_data.get("expires_in", 3600),
    }

    logger.info(
        "[Codex] 登录成功: %s (plan: %s, supported: %s)",
        bundle["email"],
        bundle["plan_type"],
        bundle["plan_supported"],
    )
    return bundle


def _password_grant_access_token(email, password):
    """Round 11 V8 fast-path(已弃用,保留作未来 hook)— OAuth 2.0 ROPC password grant.



    现状(V11 实证 2026-04-30):OAuth 2.1 已移除 ROPC,OpenAI auth.openai.com/oauth/token

    后端 30 次组合(5 账号 × 2 client_id × 3 scope)全员 HTTP 400

    `unknown_parameter:'username'` — 完全没部署 password grant,本 helper 永远返回 None。

    完整证据见 `research/v11-password-grant-probe-report.md`。



    实际使用 `fetch_nextauth_backend_access_token` 替代(NextAuth /api/auth/session.accessToken

    路径,V10 jshook trace 已实证 chatgpt.com /api/auth/session 响应含 accessToken 字段

    可作 Bearer 调 backend-api/*)。



    保留 helper 仅作未来 OpenAI 重启 ROPC 时的 hook;现有单测继续覆盖契约语义(空参 / 200 /

    401 / 网络异常 / 非 JSON / 缺字段),实现行为保持不变。



    Args:

        email: 子号邮箱,作为 `username` 字段。

        password: 注册时设的密码。



    Returns:

        access_token 字符串(200 OK + token_type=Bearer 时);否则 None。永不抛(M-I1 风格)。

        当前实测永远返回 None(OpenAI 后端 unknown_parameter:'username')。

    """
    if not email or not password:
        return None

    import requests

    try:
        resp = requests.post(
            CODEX_TOKEN_URL,
            data={
                "grant_type": "password",
                "client_id": CODEX_CLIENT_ID,
                "username": email,
                "password": password,
                "scope": "openid profile email offline_access",
            },
            headers={"Content-Type": "application/x-www-form-urlencoded"},
            timeout=15,
        )
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] _password_grant_access_token 请求异常: %s", exc)
        return None

    if resp.status_code != 200:
        logger.warning(
            "[Codex] _password_grant_access_token 返回 %d: %s",
            resp.status_code,
            (resp.text or "")[:200],
        )
        return None

    try:
        data = resp.json()
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] _password_grant_access_token 响应非 JSON: %s", exc)
        return None

    access_token = data.get("access_token")
    if not access_token:
        logger.warning(
            "[Codex] _password_grant_access_token 响应缺 access_token: %s",
            str(data)[:200],
        )
        return None

    logger.info("[Codex] _password_grant_access_token 成功 (token_type=%s)", data.get("token_type"))
    return access_token


def fetch_nextauth_backend_access_token(page):
    """Round 11 V12 P0.1 — 从已加载 chatgpt.com cookie 的浏览器 page 提取 NextAuth `accessToken`。



    背景:V11 探活实证 OpenAI `auth.openai.com/oauth/token` 已撤 ROPC password grant

    (30 个 client_id/scope/audience 变体全员 HTTP 400 `unknown_parameter: 'username'`),

    `_password_grant_access_token` 在新号场景永远返回 None。



    替代路径:V10 jshook trace 已实证 chatgpt.com `/api/auth/session` 响应含 `accessToken`

    字段(2080 字符),等价于 SPA 调 `/backend-api/*` 用的 Bearer token。在 register 成功

    后浏览器 context 已落 chatgpt.com NextAuth session cookie,直接 page.evaluate 调

    /api/auth/session credentials:include 即可拿到。



    Args:

        page: Playwright Page 对象,必须已加载 chatgpt.com 域(cookie 在该上下文)。



    Returns:

        access_token 字符串(200 OK + accessToken 字段非空时);否则 None。永不抛(M-I1 风格)。

    """
    if page is None:
        return None

    try:
        result = page.evaluate(
            """

            async () => {

                try {

                    const r = await fetch('/api/auth/session', {

                        credentials: 'include',

                        cache: 'no-store',

                        headers: { 'Accept': 'application/json' },

                    });

                    if (r.status !== 200) return { status: r.status, accessToken: null };

                    const ct = r.headers.get('content-type') || '';

                    if (!ct.includes('application/json')) return { status: r.status, accessToken: null, raw: 'non-json' };

                    const data = await r.json();

                    return { status: r.status, accessToken: data && data.accessToken ? data.accessToken : null };

                } catch (e) {

                    return { status: 0, accessToken: null, error: String(e) };

                }

            }

            """
        )
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] fetch_nextauth_backend_access_token page.evaluate 异常: %s", exc)
        return None

    if not isinstance(result, dict):
        logger.warning("[Codex] fetch_nextauth_backend_access_token 异常返回: %s", str(result)[:200])
        return None

    status = result.get("status")
    access_token = result.get("accessToken")
    if status != 200:
        logger.warning(
            "[Codex] fetch_nextauth_backend_access_token /api/auth/session 返回 %s (accessToken=%s)",
            status,
            "yes" if access_token else "no",
        )
        return None
    if not access_token:
        logger.warning(
            "[Codex] fetch_nextauth_backend_access_token /api/auth/session 200 但 accessToken 字段缺失/空"
        )
        return None

    logger.info(
        "[Codex] fetch_nextauth_backend_access_token 成功 (len=%d)",
        len(access_token),
    )
    return access_token


def _bundle_from_access_token(access_token, email, account_id):
    claims = _parse_jwt_payload(access_token)
    auth_claims = claims.get("https://api.openai.com/auth", {})
    raw_plan = auth_claims.get("chatgpt_plan_type", "unknown")
    resolved_email = claims.get("email", email or "")
    resolved_account_id = account_id or auth_claims.get("chatgpt_account_id", "")
    return {
        "access_token": access_token,
        "refresh_token": "",
        "id_token": access_token,
        "account_id": resolved_account_id,
        "email": resolved_email,
        "plan_type": normalize_plan_type(raw_plan),
        "plan_type_raw": raw_plan,
        "plan_supported": is_supported_plan(raw_plan),
        "expired": time.time() + 3600,
    }


def _bundle_from_session_token(session_token, email, account_id):
    return _bundle_from_access_token(session_token, email, account_id)


def _oauth_result_from_bundle(bundle):
    return {
        "ok": True,
        "bundle": bundle,
        "error_type": None,
        "error_detail": None,
        "retryable": False,
    }


def _inject_team_account_cookies(context, account_id: str | None) -> None:
    account_id = (account_id or "").strip()
    if not account_id:
        return
    try:
        context.add_cookies(
            [
                {
                    "name": "_account",
                    "value": account_id,
                    "domain": "chatgpt.com",
                    "path": "/",
                    "secure": True,
                    "sameSite": "Lax",
                },
                {
                    "name": "_account",
                    "value": account_id,
                    "domain": "auth.openai.com",
                    "path": "/",
                    "secure": True,
                    "sameSite": "Lax",
                },
            ]
        )
    except Exception as exc:
        logger.debug("[Codex-Fallback] 注入 Team account cookie 失败: %s", exc)


def _is_valid_team_access_token(access_token: str | None, account_id: str | None) -> bool:
    if not access_token:
        return False
    claims = _parse_jwt_payload(access_token)
    auth_claims = claims.get("https://api.openai.com/auth", {})
    plan_type = normalize_plan_type(auth_claims.get("chatgpt_plan_type", ""))
    token_account_id = str(auth_claims.get("chatgpt_account_id") or "").strip()
    expected_account_id = str(account_id or "").strip()
    if expected_account_id and token_account_id and token_account_id != expected_account_id:
        return False
    return plan_type == "team"


def _session_token_supports_team_quota(page, session_token, account_id):
    try:
        result = page.evaluate(
            """async ([teamAccountId, token]) => {

                try {

                    const resp = await fetch("/backend-api/wham/usage", {

                        credentials: "include",

                        headers: {

                            "Accept": "application/json",

                            "Authorization": `Bearer ${token || ""}`,

                        },

                    });

                    const text = await resp.text().catch(() => "");

                    return { status: resp.status, body: text };

                } catch (e) {

                    return { status: 0, body: String(e && e.message || e) };

                }

            }""",
            [account_id, session_token],
        )
    except Exception as exc:
        logger.info("[Codex-Fallback] Team quota probe exception: %s", exc)
        return False

    status = result.get("status") if isinstance(result, dict) else None
    if status == 200:
        return True
    logger.info(
        "[Codex-Fallback] Team quota probe rejected session token: status=%s body=%s",
        status,
        (result or {}).get("body", "") if isinstance(result, dict) else "",
    )
    return False


def _fetch_team_session_bundle_from_context(

    context,

    email: str,

    account_id: str | None,

    *,

    stage_label: str,

    attempts: int = 3,

) -> dict | None:
    """Use an already logged-in ChatGPT session as Codex auth material."""

    account_id = (account_id or "").strip()
    page = None
    try:
        _inject_team_account_cookies(context, account_id)
        page = context.new_page()
        target_url = f"https://chatgpt.com/admin/workspace/{account_id}" if account_id else "https://chatgpt.com/"
        page.goto(target_url, wait_until="domcontentloaded", timeout=30000)
        time.sleep(2)

        for attempt in range(1, max(1, attempts) + 1):
            session = page.evaluate(
                """async () => {

                    try {

                        const resp = await fetch("/api/auth/session");

                        return await resp.json();

                    } catch (e) {

                        return { error: String(e && e.message || e) };

                    }

                }"""
            )
            token = session.get("accessToken") if isinstance(session, dict) else None
            if _is_valid_team_access_token(token, account_id):
                bundle = _bundle_from_access_token(token, email, account_id)
                logger.info(
                    "[Codex-Fallback] %s 获取到 ChatGPT Team session token: email=%s plan=%s",
                    stage_label,
                    bundle.get("email") or email,
                    bundle.get("plan_type"),
                )
                return bundle

            if token and _session_token_supports_team_quota(page, token, account_id):
                bundle = _bundle_from_session_token(token, email, account_id)
                bundle["plan_type"] = "team"
                bundle["plan_supported"] = True
                logger.info(
                    "[Codex-Fallback] %s JWT claims 未切到 Team,但 wham/usage 已验证目标 Team 可用: email=%s account=%s",
                    stage_label,
                    bundle.get("email") or email,
                    account_id,
                )
                return bundle

            if token:
                claims = _parse_jwt_payload(token)
                auth_claims = claims.get("https://api.openai.com/auth", {})
                logger.info(
                    "[Codex-Fallback] %s session token 不匹配 Team workspace (attempt %d/%d): account=%s plan=%s",
                    stage_label,
                    attempt,
                    attempts,
                    auth_claims.get("chatgpt_account_id"),
                    auth_claims.get("chatgpt_plan_type"),
                )

            if attempt < attempts:
                page.reload(wait_until="domcontentloaded", timeout=30000)
                time.sleep(1)
        return None
    except Exception as exc:
        logger.warning("[Codex-Fallback] %s session bundle 提取失败: %s", stage_label, exc)
        return None
    finally:
        if page is not None:
            try:
                page.close()
            except Exception:
                pass


def fetch_personal_uuid(access_token):
    """Round 11 V8 — POST https://chatgpt.com/backend-api/accounts/personal idempotent getOrCreate.



    Master 控制实验(`research/v7c-master-replay.json`)证实该 endpoint 是 idempotent — 用 Bearer

    access_token 调一次,200 OK + 返回 `{id: "<personal_uuid>", structure: "personal", created: false}`,

    用户在 register 阶段已经被自动创建 personal workspace。此 UUID 下游用作 OAuth `allowed_workspace_id`

    query 参数,绕过 default_workspace_id sticky-Team 死锁。



    ⚠️ Round 11 V13 实证(2026-04-30):用 `requests` 直打该 endpoint 会被 Cloudflare 403 拦

    (无 cf_clearance / 真实 chrome UA / TLS fingerprint)。生产路径必须用

    `fetch_personal_uuid_via_page(page, access_token)` 在浏览器内 page.evaluate 调用,绕开 cf

    bot challenge。本 helper 仅保留作单测 / 离线 CTF 复现脚本用途。



    Args:

        access_token: 子号刚完成 password login 拿到的临时 Bearer token。



    Returns:

        UUID 字符串 / None(401 / 403 / 网络异常)。永不抛(M-I1 风格)。

    """
    if not access_token:
        return None

    import requests

    try:
        resp = requests.post(
            "https://chatgpt.com/backend-api/accounts/personal",
            headers={
                "Authorization": f"Bearer {access_token}",
                "Content-Type": "application/json",
                "Origin": "https://chatgpt.com",
            },
            json={},
            timeout=15,
        )
    except Exception as exc:  # noqa: BLE001 — 任何 transport 异常都不抛
        logger.warning("[Codex] fetch_personal_uuid 请求异常: %s", exc)
        return None

    if resp.status_code != 200:
        logger.warning(
            "[Codex] fetch_personal_uuid 返回 %d: %s",
            resp.status_code,
            (resp.text or "")[:200],
        )
        return None

    try:
        data = resp.json()
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] fetch_personal_uuid 响应非 JSON: %s", exc)
        return None

    uuid = data.get("id")
    if not uuid:
        logger.warning("[Codex] fetch_personal_uuid 响应缺少 id 字段: %s", str(data)[:200])
        return None

    logger.info(
        "[Codex] fetch_personal_uuid 成功: %s (created=%s)",
        uuid,
        data.get("created"),
    )
    return uuid


def fetch_personal_uuid_via_page(page, access_token=None):
    """Round 11 V13 P0.2 — 浏览器内调用 POST /backend-api/accounts/personal,绕过 Cloudflare。



    背景:V12 探活实证 `requests` 直打 `chatgpt.com/backend-api/accounts/personal` 全员被 Cloudflare

    403 拦(无 cf_clearance / 真实 chrome TLS fingerprint / 真实 UA)。但浏览器在 register 阶段已

    通过 cf challenge,context 里有 cf_clearance cookie + 完整 chrome 指纹,page.evaluate 在

    chatgpt.com origin 内 fetch 不会被拦。



    Args:

        page: Playwright Page 对象,必须已加载 chatgpt.com 域(cookie + cf_clearance 在该上下文)。

        access_token: 显式 Bearer token(从 `fetch_nextauth_backend_access_token` 拿到);

            None 时走 `credentials: 'include'` 用浏览器 NextAuth cookie session(SPA 默认行为)。



    Returns:

        UUID 字符串 / None(page=None / 401 / 403 / 缺 id / 异常)。永不抛(M-I1 风格)。

    """
    if page is None:
        return None

    js = """

    async (token) => {

        try {

            const headers = {

                'Content-Type': 'application/json',

                'Accept': 'application/json',

            };

            if (token) headers['Authorization'] = 'Bearer ' + token;

            const r = await fetch('/backend-api/accounts/personal', {

                method: 'POST',

                credentials: 'include',

                headers: headers,

                body: '{}',

            });

            if (r.status !== 200) {

                let body = '';

                try { body = (await r.text()).slice(0, 200); } catch (e) {}

                return { status: r.status, id: null, body: body };

            }

            const ct = r.headers.get('content-type') || '';

            if (!ct.includes('application/json')) {

                return { status: r.status, id: null, raw: 'non-json' };

            }

            const data = await r.json();

            return { status: r.status, id: data && data.id ? data.id : null, created: data ? data.created : null };

        } catch (e) {

            return { status: 0, id: null, error: String(e) };

        }

    }

    """

    try:
        result = page.evaluate(js, access_token or "")
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] fetch_personal_uuid_via_page page.evaluate 异常: %s", exc)
        return None

    if not isinstance(result, dict):
        logger.warning(
            "[Codex] fetch_personal_uuid_via_page 异常返回: %s", str(result)[:200]
        )
        return None

    status = result.get("status")
    uuid = result.get("id")
    if status != 200:
        logger.warning(
            "[Codex] fetch_personal_uuid_via_page /accounts/personal 返回 %s (id=%s, body=%s)",
            status,
            "yes" if uuid else "no",
            (result.get("body") or "")[:120],
        )
        return None
    if not uuid:
        logger.warning(
            "[Codex] fetch_personal_uuid_via_page 200 但缺 id 字段: %s", str(result)[:200]
        )
        return None

    logger.info(
        "[Codex] fetch_personal_uuid_via_page 成功: %s (created=%s)",
        uuid,
        result.get("created"),
    )
    return uuid


def is_token_pair_invalidated(auth_path):
    """Round 11 V7 — 双失效探测:access_token + refresh_token 是否被 server-side invalidate。



    研究报告 v7-v10-personal-uuid-exploitation.md §V7 确认 OpenAI 在 user kick 时同步废

    access_token(`token_invalidated` 401)+ refresh_token(`refresh_token_invalidated` 401)。

    任意一个仍可用 → 视为可救活,仅 access_token 死可走 refresh_access_token 重生;两个都死才

    判定 auth_invalid 触发外层清账(避免无谓重登)。



    Args:

        auth_path: 子号 auth_file 路径(JSON)。



    Returns:

        True 当且仅当 GET /backend-api/me 返回 401 + POST /oauth/token (refresh_token grant)

        也返回 401。任何其它结果(网络异常 / 200 / 非 401 错误)→ False(保守不动状态)。

    """
    if not auth_path:
        return False
    p = Path(auth_path)
    if not p.exists():
        return False

    try:
        data = json.loads(p.read_text(encoding="utf-8"))
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] is_token_pair_invalidated 读取 %s 异常: %s", auth_path, exc)
        return False

    access_token = data.get("access_token")
    refresh_token = data.get("refresh_token")
    if not access_token or not refresh_token:
        return False

    import requests

    try:
        me_resp = requests.get(
            "https://chatgpt.com/backend-api/me",
            headers={"Authorization": f"Bearer {access_token}"},
            timeout=15,
        )
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] is_token_pair_invalidated GET /me 异常: %s", exc)
        return False

    if me_resp.status_code != 401:
        return False

    try:
        rt_resp = requests.post(
            CODEX_TOKEN_URL,
            data={
                "grant_type": "refresh_token",
                "client_id": CODEX_CLIENT_ID,
                "refresh_token": refresh_token,
                "scope": "openid profile email",
            },
            headers={"Content-Type": "application/x-www-form-urlencoded"},
            timeout=15,
        )
    except Exception as exc:  # noqa: BLE001
        logger.warning("[Codex] is_token_pair_invalidated refresh 异常: %s", exc)
        return False

    if rt_resp.status_code != 401:
        return False

    logger.warning(
        "[Codex] is_token_pair_invalidated 命中:access_token + refresh_token 同时被 server-side invalidate (auth_path=%s)",
        auth_path,
    )
    return True


def _build_auth_url_with_allowed_workspace(code_challenge, state, allowed_workspace_id):
    """Round 11 V8 — 在 _build_auth_url 基础上拼 `&allowed_workspace_id=<personal_uuid>` query 参数。



    OAuth issuer 静默接受该参数(实测 v8-result.md:不报 unknown_parameter),让 issuer 在颁

    token 时优先选 personal workspace 而非 default_workspace_id 指向的 Team。

    """
    base = _build_auth_url(code_challenge, state)
    if not allowed_workspace_id:
        return base
    sep = "&" if "?" in base else "?"
    return f"{base}{sep}allowed_workspace_id={urllib.parse.quote(allowed_workspace_id, safe='')}"


def _write_auth_file(filepath, bundle):
    filepath = Path(filepath)
    ensure_auth_dir()
    filepath.parent.mkdir(exist_ok=True)

    auth_data = {
        "type": "codex",
        "id_token": bundle.get("id_token", ""),
        "access_token": bundle.get("access_token", ""),
        "refresh_token": bundle.get("refresh_token", ""),
        "account_id": bundle.get("account_id", ""),
        "email": bundle.get("email", ""),
        "expired": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(bundle.get("expired", 0))),
        "last_refresh": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
    }

    write_text(filepath, json.dumps(auth_data, indent=2))
    ensure_auth_file_permissions(filepath)
    logger.info("[Codex] 认证文件已保存: %s", filepath)
    return str(filepath)


def _inject_personal_session_cookies(context, session_token):
    """

    把注册阶段的 chatgpt.com __Secure-next-auth.session-token 注入到

    chatgpt.com + auth.openai.com 双域,让 personal OAuth 跳过 /log-in 表单。



    Round 11 四轮:

    - 单注入 auth.openai.com 域不够 —— NextAuth 跨域 issuer 校验严格,

      /oauth/authorize 不认 chatgpt.com 颁发的 token。

    - 必须双域同时注入,然后先 goto chatgpt.com 让服务端 next-auth API 校验

      session 并写齐配套 cookies,再 goto auth_url 进入 OAuth。



    切片规则与 SessionCodexAuthFlow._inject_auth_cookies / chatgpt_api._build_session_cookies

    保持一致:>3800 字节切两段 .0/.1,否则单个 cookie。

    """
    if not session_token:
        return

    def _build(domain):
        if len(session_token) > 3800:
            return [
                {
                    "name": "__Secure-next-auth.session-token.0",
                    "value": session_token[:3800],
                    "domain": domain,
                    "path": "/",
                    "httpOnly": True,
                    "secure": True,
                    "sameSite": "Lax",
                },
                {
                    "name": "__Secure-next-auth.session-token.1",
                    "value": session_token[3800:],
                    "domain": domain,
                    "path": "/",
                    "httpOnly": True,
                    "secure": True,
                    "sameSite": "Lax",
                },
            ]
        return [
            {
                "name": "__Secure-next-auth.session-token",
                "value": session_token,
                "domain": domain,
                "path": "/",
                "httpOnly": True,
                "secure": True,
                "sameSite": "Lax",
            }
        ]

    cookies = _build("chatgpt.com") + _build("auth.openai.com")
    context.add_cookies(cookies)


def _click_primary_auth_button(page, field, labels):
    """

    只点击当前输入框所在表单的主按钮,避免误点 Continue with Google/Apple/Microsoft。

    """
    label_re = re.compile(rf"^(?:{'|'.join(re.escape(label) for label in labels)})$", re.I)

    try:
        form = field.locator("xpath=ancestor::form[1]").first
        btn = form.get_by_role("button", name=label_re).first
        if btn.is_visible(timeout=2000):
            btn.click()
            return True
    except Exception:
        pass

    try:
        form = field.locator("xpath=ancestor::form[1]").first
        btn = form.locator('button[type="submit"], input[type="submit"]').first
        if btn.is_visible(timeout=2000):
            btn.click()
            return True
    except Exception:
        pass

    try:
        btn = page.get_by_role("button", name=label_re).last
        if btn.is_visible(timeout=2000):
            btn.click()
            return True
    except Exception:
        pass

    try:
        field.press("Enter")
        return True
    except Exception:
        return False


def _select_existing_api_organization(page) -> bool:
    trigger_selectors = (
        'button:has-text("New organization")',
        '[role="button"]:has-text("New organization")',
        'button:has-text("新组织")',
        '[role="button"]:has-text("新组织")',
    )

    for selector in trigger_selectors:
        try:
            trigger = page.locator(selector).first
            if not trigger.is_visible(timeout=800):
                continue

            trigger.click()
            time.sleep(0.5)
            for option in page.locator('[role="option"]').all():
                try:
                    label = option.inner_text(timeout=500).strip()
                except Exception:
                    continue
                lowered = label.lower()
                if not label or "new organization" in lowered or "新组织" in label:
                    continue
                option.click()
                logger.info("[Codex] 已切换到已有 API organization: %s", label)
                time.sleep(0.5)
                return True

            logger.warning("[Codex] API organization 下拉中未找到可用的已有组织,保留当前选择")
            return False
        except Exception:
            continue

    return False


_CHOOSE_ACCOUNT_PAGE_HINTS = (
    "choose an account",
    "select an account",
    "continue as",
    "choose a chatgpt account",
    "选择一个账号",
    "选择账号",
)
_CHOOSE_ACCOUNT_IGNORE_LABELS = {
    "choose an account",
    "select an account",
    "choose a chatgpt account",
    "continue as",
    "continue",
    "继续",
    "allow",
    "log in",
    "cancel",
    "back",
    "terms of use",
    "privacy policy",
}
_CHOOSE_ACCOUNT_IGNORE_SUBSTRINGS = (
    "terms of use",
    "privacy policy",
    "continue with",
)


def _is_choose_account_page(page) -> bool:
    url = (getattr(page, "url", "") or "").lower()
    if "choose-an-account" in url:
        return True

    try:
        body = page.locator("body").inner_text(timeout=1200).lower()
    except Exception:
        body = ""

    return any(hint in body for hint in _CHOOSE_ACCOUNT_PAGE_HINTS)


def _is_choose_account_ignored_label(text: str) -> bool:
    lowered = str(text or "").strip().lower()
    if lowered in _CHOOSE_ACCOUNT_IGNORE_LABELS:
        return True
    return any(token in lowered for token in _CHOOSE_ACCOUNT_IGNORE_SUBSTRINGS)


def _click_oauth_locator(loc) -> bool:
    try:
        loc.click(timeout=3000)
        return True
    except Exception:
        try:
            loc.click(force=True)
            return True
        except Exception:
            return False


def _choose_account_label_candidates(page):
    if not _is_choose_account_page(page):
        return []

    selectors = (
        "button",
        "a",
        '[role="button"]',
        '[role="option"]',
        '[aria-selected="true"]',
        '[aria-selected="false"]',
        "[data-state]",
        "li",
        "label",
        "div",
    )
    seen = set()
    candidates = []
    for selector in selectors:
        try:
            for loc in page.locator(selector).all():
                try:
                    if not loc.is_visible(timeout=100):
                        continue
                    text = re.sub(r"\s+", " ", loc.inner_text(timeout=200)).strip()
                except Exception:
                    continue
                lowered = text.lower()
                if not text or lowered in seen or len(text) > 160 or _is_choose_account_ignored_label(lowered):
                    continue
                seen.add(lowered)
                candidates.append((text, loc))
        except Exception:
            continue
    return candidates


def _select_oauth_account(page, email: str | None) -> bool:
    preferred_email = str(email or "").strip().lower()
    if not preferred_email:
        return False
    local_part = preferred_email.split("@", 1)[0] if "@" in preferred_email else preferred_email

    for text, loc in _choose_account_label_candidates(page):
        lowered = text.strip().lower()
        if preferred_email not in lowered and (not local_part or local_part not in lowered):
            continue
        if not _click_oauth_locator(loc):
            continue
        logger.info("[Codex] 选择 OAuth 账号: %s", text)
        time.sleep(2)
        try:
            confirm = page.locator(
                'button:has-text("Continue"), button:has-text("继续"), button:has-text("Allow")'
            ).first
            if confirm.is_visible(timeout=800):
                confirm.click()
                logger.info("[Codex] 已确认 OAuth 账号选择")
                time.sleep(3)
        except Exception:
            pass
        return True

    for selector in (
        f'text="{preferred_email}"',
        f"text=/{re.escape(preferred_email)}/i",
    ):
        try:
            loc = page.locator(selector).first
            if not loc.is_visible(timeout=500):
                continue
            if not _click_oauth_locator(loc):
                continue
            logger.info("[Codex] 选择 OAuth 账号: %s", preferred_email)
            time.sleep(2)
            return True
        except Exception:
            continue

    return False


def _select_choose_account(page, email: str | None) -> bool:
    return _select_oauth_account(page, email)


def _oauth_page_has_terminal_error(page) -> bool:
    body_excerpt = _page_excerpt(page, limit=400).lower()
    return (
        "no_valid_organizations" in body_excerpt
        or "an error occurred during authentication" in body_excerpt
        or "operation timed out" in body_excerpt
        or "unsupported_country_region_territory" in body_excerpt
        or "country, region, or territory not supported" in body_excerpt
    )


def _recover_oauth_timeout_page(page) -> bool:
    body_excerpt = _page_excerpt(page, limit=400).lower()
    if "operation timed out" not in body_excerpt:
        return False

    for selector in (
        'button:has-text("Try again")',
        '[role="button"]:has-text("Try again")',
        'a:has-text("Try again")',
        'button:has-text("Retry")',
    ):
        try:
            btn = page.locator(selector).first
            if not btn.is_visible(timeout=800):
                continue
            logger.info("[Codex] 命中 OAuth 超时错误页,尝试点击 Try again 继续当前流程...")
            btn.click()
            time.sleep(3)
            return True
        except Exception:
            continue

    return False


def _recover_oauth_no_valid_organizations_page(page) -> bool:
    body_excerpt = _page_excerpt(page, limit=500).lower()
    if "no_valid_organizations" not in body_excerpt and "no valid organizations" not in body_excerpt:
        return False

    for selector in (
        'button:has-text("Try again")',
        '[role="button"]:has-text("Try again")',
        'a:has-text("Try again")',
        'button:has-text("Retry")',
        '[role="button"]:has-text("Retry")',
    ):
        try:
            btn = page.locator(selector).first
            if not btn.is_visible(timeout=800):
                continue
            logger.info("[Codex] 命中 no_valid_organizations 错误页,尝试点击 Try again 继续当前 OAuth 流程...")
            btn.click()
            time.sleep(4)
            return True
        except Exception:
            continue

    return False


def _is_oauth_login_challenge_page(page, trace_events=None) -> bool:
    try:
        url = (page.url or "").lower()
    except Exception:
        url = ""
    if "/api/accounts/login" in url or "auth.openai.com/log-in" in url:
        return True
    return bool(trace_events and _oauth_trace_has_login_challenge(trace_events) and "/oauth" not in url)


def _is_login_page_url(url: str | None) -> bool:
    url = (url or "").lower()
    return "/api/accounts/login" in url or "auth.openai.com/log-in" in url or "log-in-or-create-account" in url


def _wait_for_oauth_challenge_progress(page, timeout=20) -> bool:
    deadline = time.time() + timeout
    last_url = ""
    while time.time() < deadline:
        try:
            url = page.url or ""
            last_url = url
        except Exception:
            url = ""

        lower_url = url.lower()
        if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in lower_url:
            return True
        if (
            lower_url
            and "/api/accounts/login" not in lower_url
            and "auth.openai.com/log-in" not in lower_url
            and "email-verification" not in lower_url
        ):
            if any(marker in lower_url for marker in ("/oauth", "consent", "organization", "choose-an-account")):
                return True

        try:
            if _is_otp_input_visible(page, timeout=300):
                return False
        except Exception:
            pass

        time.sleep(0.5)

    logger.info("[Codex] OAuth login challenge progress wait timed out, last_url=%s", last_url)
    return False


def _complete_oauth_login_challenge(page, email, password, mail_client, min_email_id, used_email_ids) -> bool:
    acted = False

    try:
        for attempt in range(2):
            email_input = page.locator('input[name="email"], input[id="email-input"], input[id="email"]').first
            if not email_input.is_visible(timeout=3000):
                break
            email_input.fill(email)
            acted = True
            time.sleep(0.5)
            _click_primary_auth_button(page, email_input, ["Continue", "继续"])
            time.sleep(3)
            if not _is_google_redirect(page):
                break
            _screenshot(page, f"codex_login_challenge_google_email_{attempt + 1}.png")
            logger.warning("[Codex] OAuth login challenge email step redirected to Google; retrying")
            page.go_back(wait_until="domcontentloaded", timeout=30000)
            time.sleep(2)
    except Exception:
        pass

    try:
        for attempt in range(2):
            pwd_input = page.locator('input[name="password"], input[type="password"]').first
            if not pwd_input.is_visible(timeout=3000):
                break
            acted = True
            if password:
                pwd_input.fill(password)
                time.sleep(0.5)
                _click_primary_auth_button(page, pwd_input, ["Continue", "继续", "Log in"])
            else:
                otp_btn = page.locator(
                    'button:has-text("一次性验证码"), button:has-text("one-time"), button:has-text("email login")'
                ).first
                if otp_btn.is_visible(timeout=3000):
                    otp_btn.click()
                else:
                    _click_primary_auth_button(page, pwd_input, ["Continue", "继续", "Log in"])
            time.sleep(5)
            if not _is_google_redirect(page):
                break
            _screenshot(page, f"codex_login_challenge_google_password_{attempt + 1}.png")
            logger.warning("[Codex] OAuth login challenge password step redirected to Google; retrying")
            page.go_back(wait_until="domcontentloaded", timeout=30000)
            time.sleep(2)
    except Exception:
        pass

    try:
        if _is_otp_input_visible(page, timeout=3000):
            if not mail_client:
                logger.warning("[Codex] OAuth login challenge needs OTP but no mail_client is available")
                return acted
            submit_status = _resolve_email_verification(
                page,
                mail_client=mail_client,
                email=email,
                after_email_id=min_email_id,
                used_email_ids=used_email_ids,
                wait_log="[Codex] OAuth login challenge needs OTP, waiting for emailId > %d",
                wait_timeout=90,
            )
            if submit_status != "no_code":
                acted = True
                _wait_for_oauth_challenge_progress(page, timeout=20)
    except Exception:
        pass

    if "about-you" in (page.url or ""):
        _complete_oauth_about_you(page)
        acted = True

    return acted


def _is_google_redirect(page):
    url = (page.url or "").lower()
    if "accounts.google.com" in url:
        return True

    try:
        text = page.locator("body").inner_text(timeout=1000).lower()
        return "sign in with google" in text[:300]
    except Exception:
        return False


_OTP_INPUT_SELECTORS = (
    'input[name="code"], input[inputmode="numeric"], input[autocomplete="one-time-code"], '
    'input[placeholder*="验证码"], input[placeholder*="code" i]'
)
_OTP_SINGLE_INPUT_SELECTORS = 'input[maxlength="1"], input[data-input-otp], input[aria-label*="digit" i]'
_OTP_INVALID_HINTS = (
    "invalid code",
    "incorrect code",
    "wrong code",
    "expired code",
    "check the code and try again",
    "验证码无效",
    "验证码错误",
    "验证码已过期",
)


def _is_otp_input_visible(page, timeout=500):
    try:
        return page.locator(_OTP_INPUT_SELECTORS).first.is_visible(timeout=timeout)
    except Exception:
        return False


def _detect_otp_error(page):
    try:
        body = page.locator("body").inner_text(timeout=1500).lower().replace("\n", " ")
    except Exception:
        return None

    for hint in _OTP_INVALID_HINTS:
        if hint in body:
            return hint
    return None


def _is_otp_progress_url(url: str | None) -> bool:
    url = (url or "").lower()
    if not url:
        return False
    if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in url:
        return True
    return any(
        marker in url
        for marker in (
            "consent",
            "organization",
            "choose-an-account",
            "about-you",
            "/oauth/oauth2/auth",
            "/sign-in-with-chatgpt/codex",
        )
    ) and "email-verification" not in url


def _wait_for_otp_submit_result(page, timeout=12):
    """

    等待验证码提交结果:

    - accepted: 验证码输入框已消失 / 页面已前进

    - invalid: 页面明确提示验证码错误

    - pending: 既没报错也没明显前进(常见于页面较慢或状态未稳定)

    """
    deadline = time.time() + timeout

    while time.time() < deadline:
        try:
            if _is_otp_progress_url(page.url):
                return "accepted", None
        except Exception:
            pass
        err = _detect_otp_error(page)
        if err:
            return "invalid", err
        if not _is_otp_input_visible(page, timeout=250):
            return "accepted", None
        time.sleep(0.5)

    try:
        if _is_otp_progress_url(page.url):
            return "accepted", None
    except Exception:
        pass
    err = _detect_otp_error(page)
    if err:
        return "invalid", err
    return "pending", None


def _visible_otp_slot_inputs(page, timeout=300):
    try:
        candidates = page.locator(_OTP_SINGLE_INPUT_SELECTORS).all()
    except Exception:
        return []

    visible = []
    for loc in candidates:
        try:
            if loc.is_visible(timeout=timeout):
                visible.append(loc)
        except Exception:
            continue
    return visible


def _fill_otp_code(page, code: str) -> bool:
    value = str(code or "").strip()
    if not value:
        return False

    slot_inputs = _visible_otp_slot_inputs(page, timeout=200)
    if len(slot_inputs) >= min(4, len(value)):
        logger.info("[Codex] 检测到 %d 个单字符验证码输入框", len(slot_inputs))
        for index, char in enumerate(value):
            if index >= len(slot_inputs):
                break
            loc = slot_inputs[index]
            try:
                loc.click(force=True)
            except Exception:
                pass
            try:
                loc.fill("")
            except Exception:
                pass
            try:
                loc.fill(char)
            except Exception:
                try:
                    loc.type(char, delay=50)
                except Exception:
                    try:
                        page.keyboard.type(char, delay=50)
                    except Exception:
                        return False
            time.sleep(0.1)
        return True

    try:
        otp_input = page.locator(_OTP_INPUT_SELECTORS).first
        if otp_input.is_visible(timeout=2000):
            otp_input.fill(value)
            return True
    except Exception:
        pass

    return False


def _click_otp_submit_button(page) -> bool:
    for selector in (
        'button[type="submit"]',
        'button:has-text("Continue")',
        'button:has-text("继续")',
        'button:has-text("Verify")',
    ):
        try:
            button = page.locator(selector).first
            if button.is_visible(timeout=800):
                button.click()
                return True
        except Exception:
            continue
    return False


def _poll_mail_verification_code(

    mail_client,

    email: str,

    *,

    after_email_id: int,

    used_email_ids: set[int],

    timeout: int = 120,

    require_sender: bool = False,

):
    deadline = time.time() + timeout
    while time.time() < deadline:
        for em in mail_client.search_emails_by_recipient(email, size=5):
            email_id = em.get("emailId", 0)
            if email_id <= after_email_id or email_id in used_email_ids:
                continue

            if require_sender:
                sender = (em.get("sendEmail") or "").lower()
                if "openai" not in sender and "chatgpt" not in sender:
                    continue

            subject = (em.get("subject") or "").lower()
            if "invited" in subject or "invitation" in subject:
                continue

            code = mail_client.extract_verification_code(em)
            if code:
                return code, email_id
        time.sleep(3)

    return None, 0


def _resolve_email_verification(

    page,

    *,

    mail_client,

    email: str,

    after_email_id: int,

    used_email_ids: set[int],

    wait_log: str,

    require_sender: bool = False,

    wait_timeout: int = 120,

    submit_timeout: int = 15,

) -> str:
    logger.info(wait_log, after_email_id)

    otp, otp_email_id = _poll_mail_verification_code(
        mail_client,
        email,
        after_email_id=after_email_id,
        used_email_ids=used_email_ids,
        timeout=wait_timeout,
        require_sender=require_sender,
    )
    if not otp:
        logger.warning("[Codex] 未获取到验证码")
        return "no_code"

    logger.info("[Codex] 获取到验证码: %s", otp)

    for submit_attempt in range(1, 3):
        current_url = (getattr(page, "url", "") or "").lower()
        if current_url and "email-verification" not in current_url:
            used_email_ids.add(otp_email_id)
            return "accepted"

        if not _fill_otp_code(page, otp):
            if current_url and "email-verification" not in current_url:
                used_email_ids.add(otp_email_id)
                return "accepted"
            if submit_attempt < 2:
                logger.warning("[Codex] 验证码输入框不可用,准备重试第 %d/2 次", submit_attempt + 1)
                time.sleep(2)
                continue
            used_email_ids.add(otp_email_id)
            logger.warning("[Codex] 验证码输入框不可用,标记并跳过邮件 %s", otp_email_id)
            return "input_unavailable"

        time.sleep(0.5)
        _click_otp_submit_button(page)
        logger.info("[Codex] 已输入验证码: %s", otp)

        submit_status, submit_detail = _wait_for_otp_submit_result(page, timeout=submit_timeout)
        if submit_status == "accepted":
            used_email_ids.add(otp_email_id)
            return "accepted"
        if submit_status == "invalid":
            used_email_ids.add(otp_email_id)
            detail_suffix = f",命中提示: {submit_detail}" if submit_detail else ""
            logger.warning(
                "[Codex] 验证码邮件 %s(code=%s)被页面判定无效%s,标记并跳过该邮件",
                otp_email_id,
                otp,
                detail_suffix,
            )
            return "invalid"

        if submit_attempt < 2:
            logger.warning(
                "[Codex] 验证码邮件 %s(code=%s)提交后未确认成功,准备重试第 %d/2 次",
                otp_email_id,
                otp,
                submit_attempt + 1,
            )
            time.sleep(2)
        else:
            used_email_ids.add(otp_email_id)
            logger.warning(
                "[Codex] 验证码邮件 %s(code=%s)提交后仍未确认成功,标记并跳过该邮件",
                otp_email_id,
                otp,
            )
            return "pending"

    return "pending"


def _typewrite_credential(page, locator, value, *, delay_ms=50, post_sleep=1.0):
    """逐字符 keyboard.type 填入 credential,触发 React onChange 事件链.



    Round 11 五轮 — 阶段 2 fresh re-login 必备:

    OpenAI auth /log-in 页 React 表单对 Playwright fill() 不友好 —— fill 一次性

    setValue,不触发 input 事件 → React internal state 不更新 → Continue 按钮变灰禁用。

    keyboard.type 逐字符模拟真用户击键,触发 onChange/onInput 完整事件链。



    注:注册流的 /create-account/password 页用 fill() 是 OK 的(不同 React form impl);

    本 helper 仅用于 fresh login (/log-in) 阶段。

    """
    try:
        locator.click()
        time.sleep(0.3)
        # 清空可能预填的内容
        try:
            locator.press("Control+A")
            locator.press("Delete")
        except Exception:
            pass
        time.sleep(0.2)
        page.keyboard.type(value, delay=delay_ms)
        time.sleep(post_sleep)
        return True
    except Exception as exc:
        logger.warning("[Codex] _typewrite_credential 失败: %s", exc)
        return False


def _perform_fresh_relogin_in_context(context, email, password, mail_client, *, used_email_ids):
    """阶段 2 — fresh chatgpt.com login.



    Round 11 五轮 Option A:

    阶段 1(silent step-0 + cookie 注入)拿到 plan_type=team 或 bundle=None 时,

    说明 chatgpt.com session_token 内嵌的 user identity 已被锁死在原 Team workspace,

    NextAuth refresh 不能切。唯一兜底:清空 OAuth context 所有 cookies,做一次完整的

    chatgpt.com 登录(email + password),拿到 Personal-bound 全新 session,再走 OAuth。



    流程:

    1. context.clear_cookies() — 清空 stale 的 chatgpt.com / auth.openai.com 双域 session

    2. goto chatgpt.com/auth/login,过 Cloudflare

    3. 用 _typewrite_credential(keyboard.type)填 email + password — 不用 fill() 避免灰按钮

    4. 处理 OTP(若 mail_client 可用)

    5. 等 chatgpt.com 登录完成



    Returns:

        bool — True 表示 fresh login 成功(context 现持有新 session),False 表示失败

    """
    logger.info("[Codex] 阶段 2 fresh re-login 开始: 清空 context cookies → 重新登录 chatgpt.com")

    try:
        context.clear_cookies()
    except Exception as exc:
        logger.warning("[Codex] 清空 context cookies 失败: %s", exc)

    # 重新刷邮箱 ID snapshot,fresh login 阶段不复用阶段 1 的旧 snapshot
    fresh_email_id_before = 0
    if mail_client:
        try:
            _pre = mail_client.search_emails_by_recipient(email, size=1)
            if _pre:
                fresh_email_id_before = _pre[0].get("emailId", 0)
        except Exception:
            pass

    page = context.new_page()
    try:
        page.goto("https://chatgpt.com/auth/login", wait_until="domcontentloaded", timeout=60000)
        time.sleep(5)

        # 过 Cloudflare(若有)
        for _i in range(12):
            try:
                if "verify you are human" not in page.content()[:2000].lower():
                    break
            except Exception:
                break
            logger.info("[Codex] fresh re-login 等待 Cloudflare...(%ds)", _i * 5)
            time.sleep(5)

        # 点击 "登录" / "Log in" 按钮(若 chatgpt.com 首页落在欢迎页)
        try:
            login_btn = page.locator(
                'button:has-text("登录"), button:has-text("Log in"), a:has-text("Log in"), a:has-text("登录")'
            ).first
            if login_btn.is_visible(timeout=3000):
                login_btn.click()
                time.sleep(3)
        except Exception:
            pass

        _screenshot(page, "codex_relogin_01_login_page.png")

        # === Email 步骤 ===
        try:
            ei = page.locator(
                'input[name="email"], input[id="email-input"], input[id="email"], input[type="email"]'
            ).first
            if ei.is_visible(timeout=8000):
                logger.info("[Codex] fresh re-login: 用 keyboard.type 填入 email...")
                if not _typewrite_credential(page, ei, email):
                    logger.warning("[Codex] fresh re-login: keyboard.type email 失败")
                    return False
                _click_primary_auth_button(page, ei, ["Continue", "继续"])
                time.sleep(3)
                _screenshot(page, "codex_relogin_02_after_email.png")
            else:
                logger.warning("[Codex] fresh re-login: email input 不可见")
        except Exception as exc:
            logger.warning("[Codex] fresh re-login email 步骤异常: %s", exc)

        # === Password 步骤(关键:/log-in 页) ===
        try:
            pi = page.locator('input[name="password"], input[type="password"]').first
            if pi.is_visible(timeout=8000):
                if password:
                    logger.info("[Codex] fresh re-login: 用 keyboard.type 填入 password...")
                    if not _typewrite_credential(page, pi, password):
                        logger.warning("[Codex] fresh re-login: keyboard.type password 失败")
                        return False
                    _click_primary_auth_button(page, pi, ["Continue", "继续", "Log in"])
                    time.sleep(5)
                else:
                    # 无密码 → 一次性验证码登录
                    otp_btn = page.locator(
                        'button:has-text("一次性验证码"), button:has-text("one-time"), button:has-text("email login")'
                    ).first
                    if otp_btn.is_visible(timeout=3000):
                        logger.info("[Codex] fresh re-login: 无密码,点击一次性验证码登录")
                        otp_btn.click()
                        time.sleep(3)
                _screenshot(page, "codex_relogin_03_after_password.png")
        except Exception as exc:
            logger.warning("[Codex] fresh re-login password 步骤异常: %s", exc)

        # === 可能 OTP ===
        try:
            if _is_otp_input_visible(page, timeout=5000) and mail_client:
                _resolve_email_verification(
                    page,
                    mail_client=mail_client,
                    email=email,
                    after_email_id=fresh_email_id_before,
                    used_email_ids=used_email_ids,
                    wait_log="[Codex] fresh re-login: 需要 OTP,等待 emailId > %d 的新邮件...",
                    require_sender=True,
                )
                time.sleep(5)
                _screenshot(page, "codex_relogin_04_after_otp.png")
            elif _is_otp_input_visible(page, timeout=500):
                logger.warning("[Codex] fresh re-login: 需要 OTP 但无 mail_client")
        except Exception:
            pass

        # === 等 chatgpt.com 登录完成 ===
        # 成功条件:URL 不再含 /auth/login,且页面可正常渲染
        for _i in range(15):
            cur = page.url or ""
            if "auth/login" not in cur and "/log-in" not in cur:
                # Round 14 — 全新号在此处常停在 about-you("How old are you?" Full name/Age/
                # Finish creating account)。URL 已不含 /log-in 会被误判为"登录完成",但账号其实
                # 没建完:不填资料 + 点 Finish,后续 OAuth /authorize 会因账号未完成而回到登录页,
                # consent 永远拿不到 auth code。必须在此把 about-you 填完点 Finish creating account。
                if "about-you" in cur.lower():
                    logger.info("[Codex] fresh re-login: 命中 about-you,填写姓名/年龄并完成建号...")
                    _complete_oauth_about_you(page)
                    cur = page.url or ""
                logger.info("[Codex] fresh re-login: 登录完成,当前 URL: %s", cur[:120])
                _screenshot(page, "codex_relogin_05_logged_in.png")
                return True
            time.sleep(2)

        # 还停在 login 页 — 失败
        logger.warning("[Codex] fresh re-login: 等待登录完成超时,当前 URL: %s", page.url[:120])
        _screenshot(page, "codex_relogin_05_timeout.png")
        return False
    except Exception as exc:
        logger.warning("[Codex] fresh re-login 异常: %s", exc)
        return False
    finally:
        close_playwright_objects(page=page, logger=logger, label="codex-fresh-relogin")


def _wait_for_oauth_about_you_exit(page, *, timeout: float = 12.0) -> bool:
    deadline = time.time() + timeout
    while time.time() < deadline:
        if "about-you" not in (page.url or "").lower():
            return True
        time.sleep(0.5)
    return "about-you" not in (page.url or "").lower()


def _click_oauth_about_you_submit(page) -> None:
    for selector in (
        'button:has-text("Finish creating account")',
        'button:has-text("完成帐户创建")',
        'button:has-text("完成账户创建")',
        'button:has-text("Create account")',
        'button:has-text("Continue")',
        'button:has-text("继续")',
        'button[type="submit"]',
    ):
        try:
            button = page.locator(selector).first
            if button.is_visible(timeout=1000):
                button.click()
                return
        except Exception:
            continue
    page.keyboard.press("Enter")


def _complete_oauth_about_you(page, signup_profile: SignupProfile | None = None) -> bool:
    """Complete OAuth about-you with the same identity snapshot used at registration."""
    signup_profile = signup_profile or generate_signup_profile()

    logger.info("[Codex] 检测到 about-you 页面,填写个人信息...")
    try:
        birthday_orders = signup_profile.positional_birthday_orders()
        for attempt, values in enumerate(birthday_orders, 1):
            if "about-you" not in (page.url or "").lower():
                return True

            name_input = page.locator('input[name="name"]').first
            if name_input.is_visible(timeout=3000):
                name_input.fill(signup_profile.full_name)
                logger.info("[Codex] 已填入随机姓名: %s", signup_profile.full_name)

            spinbuttons = page.locator('[role="spinbutton"]').all()
            if len(spinbuttons) < 3:
                age_input = page.locator(
                    'input[name="age"], input[placeholder*="年龄"], input[placeholder*="Age"]'
                ).first
                try:
                    if age_input.is_visible(timeout=3000):
                        age_input.fill(signup_profile.age_text)
                        logger.info("[Codex] 已填入随机年龄: %s", signup_profile.age_text)
                except Exception:
                    logger.warning("[Codex] 未找到年龄/生日输入框")

                time.sleep(0.5)
                _click_oauth_about_you_submit(page)
                time.sleep(2)
                assert_not_blocked(page, "oauth_about_you_submit")
                _screenshot(page, "codex_03d_after_aboutyou.png")
                done = _wait_for_oauth_about_you_exit(page)
                logger.info("[Codex] about-you 完成=%s,当前 URL: %s", done, page.url)
                return done

            try:
                for label_sel in ("text=生日日期", "text=Date of birth"):
                    try:
                        page.locator(label_sel).first.click(timeout=1000)
                        time.sleep(0.3)
                        break
                    except Exception:
                        continue
            except Exception:
                pass

            for sb, val in zip(spinbuttons[:3], values):
                sb.click(force=True)
                time.sleep(0.2)
                try:
                    page.keyboard.press("ControlOrMeta+A")
                    time.sleep(0.1)
                except Exception:
                    pass
                page.keyboard.type(val, delay=80)
                time.sleep(0.3)

            logger.info(
                "[Codex] 已填入随机生日: %s (spinbutton, attempt=%d, values=%s)",
                signup_profile.birthday_text,
                attempt,
                values,
            )
            time.sleep(0.5)
            _click_oauth_about_you_submit(page)
            time.sleep(2)
            assert_not_blocked(page, "oauth_about_you_submit")
            _screenshot(page, "codex_03d_after_aboutyou.png")
            if _wait_for_oauth_about_you_exit(page):
                logger.info("[Codex] about-you 完成,当前 URL: %s", page.url)
                return True

            logger.warning(
                "[Codex] about-you 第 %d 次提交后仍停留在 profile 页,尝试下一生日顺序",
                attempt,
            )

        logger.warning("[Codex] about-you 多顺序尝试后仍未完成,当前 URL: %s", page.url)
        return False
    except RegisterBlocked:
        raise
    except Exception as exc:
        logger.error("[Codex] about-you 处理失败: %s", exc)
        return False


def login_codex_via_browser(

    email,

    password,

    mail_client=None,

    *,

    return_result=False,

    use_personal=False,

    chatgpt_session_token=None,

    prefetched_personal_uuid=None,

    pre_signed_in_cookies: list | None = None,

    signup_profile: SignupProfile | None = None,

    playwright_proxy_url: str | None = None,

):
    """

    通过 Playwright 自动完成 Codex OAuth 登录。

    mail_client: CloudMailClient 实例,用于自动读取登录验证码。

    use_personal: 若为 True,则走"个人账号"流程 —— 不注入 Team _account cookie,

                  workspace 选择时跳过 Team 直接用 Personal。用于已退出 Team 的子账号生成 free plan 的 rt/at。

    chatgpt_session_token: 注册阶段从 chatgpt.com 抽出的 __Secure-next-auth.session-token,

                           在 use_personal=True 时注入 auth.openai.com 跳过 /log-in 表单。

                           沿用 SessionCodexAuthFlow._inject_auth_cookies 的注入模式(主号专用扩展给子号)。

    prefetched_personal_uuid: Round 11 V8 — accounts.json 持久化的 personal_workspace_id;

                              非空时直接拼到 OAuth `auth_url` 的 allowed_workspace_id 参数,

                              省一次 silent step-0 内的 POST /accounts/personal。

    pre_signed_in_cookies: 直接注册后传入的已登录 chatgpt.com/auth.openai.com cookies。

                           提供时优先尝试 ChatGPT session fallback,避免新号二次 OAuth 登录挑战。

    signup_profile: 注册 about-you 使用过的身份快照;OAuth about-you 必须复用它。

    返回 auth bundle: {access_token, refresh_token, id_token, account_id, email, plan_type,

                       personal_workspace_id}

    return_result=True 时返回 {ok, bundle, error_type, error_detail, retryable}。



    Round 11 五轮 Option A — 两阶段 personal OAuth:

      阶段 1(快路径):有 chatgpt_session_token → silent step-0 双域注入 + NextAuth refresh,

                       直接 goto auth_url。注册→未踢出场景大部分用得上。

                       拿到 plan_type=free 直接返回。

      阶段 2(fresh re-login fallback):阶段 1 拿到 plan != free 或 bundle=None,

                       说明 session_token 内嵌 user identity 锁死原 Team。清空 OAuth context

                       所有 cookies,做一次完整 chatgpt.com 登录(keyboard.type 逐字符,

                       绕过 React 灰按钮),拿 Personal-bound 全新 session,再走 OAuth → plan=free。

    """
    code_verifier, code_challenge = _generate_pkce()
    state = secrets.token_urlsafe(16)
    _used_email_ids: set[int] = set()  # 记录已尝试过的邮件,避免重复提交同一封验证码邮件

    # personal 模式下不引导到 Team workspace
    chatgpt_account_id = "" if use_personal else get_chatgpt_account_id()

    auth_url = _build_auth_url(code_challenge, state)

    logger.info(
        "[Codex] 开始 OAuth 登录: %s (use_personal=%s, session_token=%s)",
        email,
        use_personal,
        "yes" if chatgpt_session_token else "no",
    )

    auth_code = None
    # Round 11 V8 — personal UUID 在 silent step-0 / stage2 re-login 完成后通过
    # POST /backend-api/accounts/personal getOrCreate 拿到,之后用 allowed_workspace_id
    # 注入 OAuth /authorize URL,绕过 default_workspace_id sticky-Team 死锁。
    # 注入既可来自调用方 prefetched_personal_uuid(accounts.json 持久化字段),
    # 也可来自 silent step-0 现场 fetch。
    personal_uuid = prefetched_personal_uuid

    # Round 11 V8 fast-path(verifier #2 P0)+ V12 P0.1 deprecation —
    # `_password_grant_access_token` 已被 V11 探活实证不可用:OpenAI auth.openai.com/oauth/token
    # 30 个 client_id/scope/audience 组合全员 HTTP 400 `unknown_parameter: 'username'`,
    # ROPC password grant 已撤(OAuth 2.1 demand 移除)。保留此 fast-path 仅作为未来
    # OpenAI 重启 ROPC 的 hook,实际效果在新号场景永远进 logger.warning 分支。
    # 真正落 personal_uuid 的路径切到 silent step-0 内的 fetch_nextauth_backend_access_token
    # → fetch_personal_uuid Bearer 链(V12 P0.1)。
    if use_personal and not personal_uuid and email and password:
        try:
            tmp_access = _password_grant_access_token(email, password)
            if tmp_access:
                fetched_uuid = fetch_personal_uuid(tmp_access)
                if fetched_uuid:
                    personal_uuid = fetched_uuid
                    logger.info(
                        "[Codex] fast-path Bearer 拿到 personal_workspace_id=%s",
                        personal_uuid,
                    )
                else:
                    logger.warning(
                        "[Codex] fast-path password grant 成功但 fetch_personal_uuid 返回 None,等 silent step-0 NextAuth fallback"
                    )
            else:
                logger.warning(
                    "[Codex] fast-path password grant 失败(V11 已知 ROPC 不可用),等 silent step-0 NextAuth fallback"
                )
        except Exception as exc:  # noqa: BLE001 — fast-path 失败不应阻断 OAuth 主流程
            logger.warning("[Codex] fast-path 异常,等 silent step-0 NextAuth fallback: %s", exc)

    with sync_playwright() as p:
        try:
            launch_kwargs = get_playwright_launch_options(proxy_url=playwright_proxy_url)
        except TypeError as exc:
            if "proxy_url" not in str(exc):
                raise
            launch_kwargs = get_playwright_launch_options()
        browser = p.chromium.launch(**launch_kwargs)
        context = browser.new_context(**get_playwright_context_options())

        if pre_signed_in_cookies:
            try:
                context.add_cookies(pre_signed_in_cookies)
                logger.info(
                    "[Codex] 已注入 %d 个 session cookie,优先尝试 ChatGPT session fallback",
                    len(pre_signed_in_cookies),
                )
            except Exception as exc:
                logger.warning("[Codex] 注入 session cookie 失败,回退到完整登录: %s", exc)
                pre_signed_in_cookies = None

        if pre_signed_in_cookies and not use_personal:
            session_fallback_bundle = _fetch_team_session_bundle_from_context(
                context,
                email,
                chatgpt_account_id,
                stage_label="pre-oauth",
            )
            if session_fallback_bundle:
                close_playwright_objects(context=context, browser=browser, logger=logger, label="codex-session-fallback")
                if return_result:
                    return _oauth_result_from_bundle(session_fallback_bundle)
                return session_fallback_bundle

        # Round 11 四轮 — Personal 模式 session_token 注入(silent step-0):
        # 实测刚踢出 Team 的新号在 OAuth /log-in 页 fill email 后 Continue 按钮变灰禁用,
        # login flow 永远卡在 email 步骤,bundle=None。根因疑似 Playwright fill() 的
        # input 事件被 OpenAI auth /log-in React 表单的 anti-bot 检测识别。
        # 单纯把 session_token 灌到 auth.openai.com 不够 —— NextAuth 对加密 token
        # 跨域 issuer 校验严格,/oauth/authorize 不认 chatgpt.com 颁发的 token。
        # 必须做"silent step-0":把 token 灌到 chatgpt.com 域,先 goto chatgpt.com
        # 让服务端 _next-auth API 校验 session 并写一套配套 cookies(oai-did /
        # __cflb / cf_clearance / _puid 等),然后再 goto auth_url。OpenAI auth
        # backend 看到来自 chatgpt.com 的有效会话引荐 → 直接跳过 /log-in 进 consent。
        # 同时 auth.openai.com 域也注入一份(冗余兜底),双域同步 SessionCodexAuthFlow。
        if use_personal and chatgpt_session_token:
            _inject_personal_session_cookies(context, chatgpt_session_token)
            logger.info(
                "[Codex] personal 模式注入 __Secure-next-auth.session-token (len=%d) 到 chatgpt.com + auth.openai.com",
                len(chatgpt_session_token),
            )

        # === Step 0: 先登录 ChatGPT 并切换到 Team workspace ===
        # Team 模式:登录前注入 _account cookie 引导登录进入 Team workspace。
        #
        # Round 11 三轮 — Personal 模式也需要 step-0:
        # 历史注释说"personal 模式跳过 step-0 因为 chatgpt_account_id="" 无 cookie 可注入"。
        # 但实测发现刚踢出 Team 的新号在 OpenAI auth backend 端 oai-oauth-session.workspaces=[]
        # (server-side 状态),直接走 auth_url 时 /oauth/authorize → /log-in → 永远循环,
        # 即便 consent loop 点 10 次 Continue,URL 仍卡在 /log-in 没有 auth_code。
        #
        # 根因:OpenAI 只在用户在 chatgpt.com 实际登录后才在 OAuth session 端 populate workspaces。
        # 新号注册流走 chatgpt.com 是另一个 browser context;OAuth 这个新 context 第一次访问
        # auth.openai.com,session 端没有任何 workspace 关联 → /oauth/authorize 拒绝颁 token。
        #
        # 修复:personal 模式也走 step-0,先 chatgpt.com 登录建立 Personal workspace 上下文,
        # auth.openai.com session 端 workspaces[] 被 populate 后,auth_url 即可正常 consent。
        # 区别:不注入 _account cookie(没有 Team workspace_id),让 chatgpt.com 自动用 Personal。

        # 在登录开始前记录当前最新邮件 ID,后续只接受比这个更新的
        _email_id_before_login = 0
        if mail_client:
            try:
                _pre = mail_client.search_emails_by_recipient(email, size=1)
                if _pre:
                    _email_id_before_login = _pre[0].get("emailId", 0)
            except Exception:
                pass

        if use_personal:
            if chatgpt_session_token:
                # Round 11 四轮 — silent step-0:cookie 已注入双域,只需 goto chatgpt.com
                # 让服务端 next-auth API 校验 session 并写齐配套 cookies(oai-did / _puid /
                # __cflb 等),OpenAI auth backend 看到来自 chatgpt.com 的有效会话引荐 →
                # /oauth/authorize 不再走 /log-in 表单,直接 consent。
                #
                # 进一步:踢出 Team 后,session_token 内 user.workspace 字段还指向 Team,
                # OAuth 仍拿到 plan_type=team。call /api/auth/session 强制 NextAuth 刷新
                # session,把 user.workspace 切到 Personal。然后再走 OAuth 才能拿 plan=free。
                logger.info("[Codex] personal 模式 silent step-0: cookie 注入后访问 chatgpt.com 验证 session...")
                _silent_page = context.new_page()
                try:
                    _silent_page.goto("https://chatgpt.com/", wait_until="domcontentloaded", timeout=60000)
                    time.sleep(5)
                    # 过 Cloudflare(若有)
                    for _i in range(8):
                        html_lower = _silent_page.content()[:2000].lower()
                        if "verify you are human" not in html_lower and "challenge" not in (_silent_page.url or "").lower():
                            break
                        logger.info("[Codex] silent step-0 等待 Cloudflare... (%ds)", _i * 5)
                        time.sleep(5)

                    # 强制 NextAuth session refresh:踢出 Team 后必须刷新 user.workspace
                    # 才能让 OAuth 拿 plan=free 而非缓存的 plan=team。
                    try:
                        refresh_resp = _silent_page.evaluate(
                            """

                            async () => {

                                const r = await fetch('/api/auth/session?update', { credentials: 'include', cache: 'no-store' });

                                const ct = r.headers.get('content-type') || '';

                                if (!ct.includes('application/json')) return { ok: r.ok, status: r.status, raw: 'non-json' };

                                const data = await r.json();

                                return { ok: r.ok, status: r.status, hasUser: !!data?.user, plan: data?.user?.plan ?? null };

                            }

                            """
                        )
                        logger.info("[Codex] silent step-0 NextAuth session refresh 结果: %s", refresh_resp)
                    except Exception as refresh_exc:
                        logger.warning("[Codex] silent step-0 NextAuth refresh 异常(忽略): %s", refresh_exc)

                    # 再调一次 backend-api/accounts/check,触发 server-side workspace 重新判定
                    try:
                        accounts_resp = _silent_page.evaluate(
                            """

                            async () => {

                                const r = await fetch('/backend-api/accounts/check', { credentials: 'include', cache: 'no-store' });

                                return { status: r.status };

                            }

                            """
                        )
                        logger.info("[Codex] silent step-0 accounts/check 结果: %s", accounts_resp)
                    except Exception as ck_exc:
                        logger.debug("[Codex] silent step-0 accounts/check 异常(忽略): %s", ck_exc)

                    # Round 11 V13 P0.2 — 浏览器内 fetch_personal_uuid_via_page 绕 Cloudflare。
                    # 已有 prefetched_personal_uuid 则跳过重复 fetch。否则:
                    #   primary: /api/auth/session 提 accessToken → page.evaluate Bearer
                    #            fetch_personal_uuid_via_page(避开 cf bot 拦截)
                    #   fallback: 原 in-browser POST /accounts/personal cookie 路径(新号场景已知 401)
                    if not personal_uuid:
                        try:
                            nextauth_token = fetch_nextauth_backend_access_token(_silent_page)
                            if nextauth_token:
                                fetched_uuid = fetch_personal_uuid_via_page(
                                    _silent_page, nextauth_token
                                )
                                if fetched_uuid:
                                    personal_uuid = fetched_uuid
                                    logger.info(
                                        "[Codex] silent step-0 NextAuth Bearer 路径(浏览器内)拿到 personal_workspace_id=%s",
                                        personal_uuid,
                                    )
                                else:
                                    logger.warning(
                                        "[Codex] silent step-0 NextAuth accessToken 拿到但 fetch_personal_uuid_via_page 返回 None,降级 cookie fallback"
                                    )
                        except Exception as nx_exc:
                            logger.warning(
                                "[Codex] silent step-0 NextAuth route 异常,降级 cookie fallback: %s",
                                nx_exc,
                            )

                    if not personal_uuid:
                        try:
                            personal_resp = _silent_page.evaluate(
                                """

                                async () => {

                                    const r = await fetch('/backend-api/accounts/personal', {

                                        method: 'POST',

                                        credentials: 'include',

                                        headers: {'Content-Type': 'application/json'},

                                        body: '{}',

                                        cache: 'no-store',

                                    });

                                    let data = null;

                                    try { data = await r.json(); } catch (_) {}

                                    return { status: r.status, id: data && data.id, created: data && data.created };

                                }

                                """
                            )
                            logger.info(
                                "[Codex] silent step-0 cookie fallback /accounts/personal 结果: %s",
                                personal_resp,
                            )
                            if isinstance(personal_resp, dict) and personal_resp.get("status") == 200:
                                fetched_uuid = personal_resp.get("id")
                                if fetched_uuid:
                                    personal_uuid = fetched_uuid
                                    logger.info(
                                        "[Codex] silent step-0 cookie fallback 拿到 personal_workspace_id=%s (created=%s)",
                                        personal_uuid,
                                        personal_resp.get("created"),
                                    )
                        except Exception as pu_exc:
                            logger.warning(
                                "[Codex] silent step-0 cookie fallback /accounts/personal fetch 异常(继续走 OAuth): %s",
                                pu_exc,
                            )

                    cur = _silent_page.url or ""
                    logger.info("[Codex] silent step-0 完成 URL: %s", cur[:120])
                    _screenshot(_silent_page, "codex_00b_silent_session_validate.png")
                except Exception as exc:
                    logger.warning("[Codex] silent step-0 异常(继续走 OAuth): %s", exc)
                finally:
                    close_playwright_objects(page=_silent_page, logger=logger, label="codex-silent-step0")
            else:
                logger.info("[Codex] personal 模式: 无 session_token,跳过 step-0,直接走 auth_url")
        else:
            if chatgpt_account_id:
                context.add_cookies(
                    [
                        {
                            "name": "_account",
                            "value": chatgpt_account_id,
                            "domain": "chatgpt.com",
                            "path": "/",
                            "secure": True,
                            "sameSite": "Lax",
                        },
                        {
                            "name": "_account",
                            "value": chatgpt_account_id,
                            "domain": "auth.openai.com",
                            "path": "/",
                            "secure": True,
                            "sameSite": "Lax",
                        },
                    ]
                )
                logger.debug("[Codex] 登录前已注入 _account cookie = %s", chatgpt_account_id)

            logger.info("[Codex] 先登录 ChatGPT 选择 Team workspace...")
            _page = context.new_page()
            _page.goto("https://chatgpt.com/auth/login", wait_until="domcontentloaded", timeout=60000)
            time.sleep(5)

            # Cloudflare
            for _i in range(12):
                if "verify you are human" not in _page.content()[:2000].lower():
                    break
                time.sleep(5)

            # 点击登录
            try:
                _page.locator('button:has-text("登录"), button:has-text("Log in")').first.click()
                time.sleep(3)
            except Exception:
                pass

            # 输入邮箱(避免误点 Google/Microsoft 第三方登录按钮)
            try:
                ei = _page.locator('input[name="email"], input[id="email-input"], input[id="email"]').first
                if ei.is_visible(timeout=5000):
                    ei.fill(email)
                    time.sleep(0.5)
                    _click_primary_auth_button(_page, ei, ["Continue", "继续"])
                    time.sleep(3)
            except Exception:
                pass

            # 输入密码 / 点击一次性验证码登录
            try:
                pi = _page.locator('input[type="password"]').first
                if pi.is_visible(timeout=5000):
                    if password:
                        pi.fill(password)
                        time.sleep(0.5)
                        _click_primary_auth_button(_page, pi, ["Continue", "继续", "Log in"])
                    else:
                        # 没有密码,点击"使用一次性验证码登录"
                        otp_btn = _page.locator(
                            'button:has-text("一次性验证码"), button:has-text("one-time"), button:has-text("email login")'
                        ).first
                        if otp_btn.is_visible(timeout=3000):
                            logger.info("[Codex] 无密码,点击一次性验证码登录")
                            otp_btn.click()
                        else:
                            # fallback: 提交空密码让页面报错,然后找验证码按钮
                            _click_primary_auth_button(_page, pi, ["Continue", "继续", "Log in"])
                    time.sleep(8)
            except Exception:
                pass

            # 可能需要邮箱验证码
            try:
                if mail_client and _is_otp_input_visible(_page, timeout=5000):
                    _resolve_email_verification(
                        _page,
                        mail_client=mail_client,
                        email=email,
                        after_email_id=_email_id_before_login,
                        used_email_ids=_used_email_ids,
                        wait_log="[Codex] ChatGPT 登录需要验证码,等待 emailId > %d 的新邮件...",
                    )
                    time.sleep(5)
            except Exception:
                pass

            _screenshot(_page, "codex_00_chatgpt_login.png")
            logger.info("[Codex] ChatGPT 登录后 URL: %s", _page.url)

            # 如果是 workspace 选择页面,Team 模式选配置的 workspace
            if "workspace" in _page.url:
                workspace_name = get_chatgpt_workspace_name()
                logger.info("[Codex] 检测到 workspace 选择页面...")
                try:
                    ws_btn = _page.locator(f'text="{workspace_name}"').first
                    if workspace_name and ws_btn.is_visible(timeout=3000):
                        logger.info("[Codex] 选择 workspace: %s", workspace_name)
                        ws_btn.click()
                        time.sleep(5)
                    else:
                        # fallback: 选第二个选项(第一个通常是"个人")
                        options = _page.locator('a, button, [role="button"]').all()
                        for opt in options:
                            try:
                                text = opt.inner_text(timeout=1000).strip()
                                if (
                                    text
                                    and "个人" not in text
                                    and "Personal" not in text
                                    and text not in ("ChatGPT", "")
                                ):
                                    logger.info("[Codex] 选择 workspace: %s", text)
                                    opt.click()
                                    time.sleep(5)
                                    break
                            except Exception:
                                continue
                except Exception:
                    pass
                _screenshot(_page, "codex_00_after_workspace.png")
                logger.info("[Codex] 选择 workspace 后 URL: %s", _page.url)

            # _account cookie 已在登录前注入

            # 关闭 ChatGPT 页面但保留 context
            close_playwright_objects(page=_page, logger=logger, label="codex-workspace-page")

        # 通过监听请求来捕获 OAuth callback redirect
        def on_request(request):
            nonlocal auth_code
            url = request.url
            if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in url:
                parsed = urllib.parse.urlparse(url)
                qs = urllib.parse.parse_qs(parsed.query)
                auth_code = qs.get("code", [None])[0]
                if auth_code:
                    logger.info("[Codex] 捕获到 auth code!")

        # 也监听 response/framenavigated 来捕获 redirect URL
        def on_response(response):
            nonlocal auth_code
            url = response.url
            if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in url and not auth_code:
                parsed = urllib.parse.urlparse(url)
                qs = urllib.parse.parse_qs(parsed.query)
                auth_code = qs.get("code", [None])[0]
                if auth_code:
                    logger.info("[Codex] 从 response 捕获到 auth code!")

        # Round 11 V8 — 拿到 personal UUID 后,把 OAuth /authorize URL 拼上 allowed_workspace_id,
        # 让 issuer 在颁 token 时优先选 personal workspace 而非 default_workspace_id 指向的 Team。
        if use_personal and personal_uuid:
            auth_url = _build_auth_url_with_allowed_workspace(code_challenge, state, personal_uuid)
            logger.info(
                "[Codex] OAuth /authorize 注入 allowed_workspace_id=%s",
                personal_uuid,
            )

        page = context.new_page()
        page.on("request", on_request)
        page.on("response", on_response)
        page.goto(auth_url, wait_until="domcontentloaded", timeout=60000)
        time.sleep(3)
        _screenshot(page, "codex_01_auth_page.png")

        # 输入邮箱(注意避免点到 Google/Microsoft/Apple 第三方登录按钮)
        try:
            for attempt in range(2):
                email_input = page.locator('input[name="email"], input[id="email-input"], input[id="email"]').first
                if not email_input.is_visible(timeout=5000):
                    break

                email_input.fill(email)
                time.sleep(0.5)
                _click_primary_auth_button(page, email_input, ["Continue", "继续"])
                time.sleep(3)

                if not _is_google_redirect(page):
                    break

                _screenshot(page, f"codex_02_google_redirect_attempt{attempt + 1}.png")
                logger.warning("[Codex] 邮箱步骤误跳转到 Google 登录,返回重试... (attempt %d)", attempt + 1)
                page.go_back(wait_until="domcontentloaded", timeout=30000)
                time.sleep(2)
            _screenshot(page, "codex_02_after_email.png")
        except Exception:
            _screenshot(page, "codex_02_no_email.png")

        # 输入密码
        try:
            for attempt in range(2):
                pwd_input = page.locator('input[name="password"], input[type="password"]').first
                if not pwd_input.is_visible(timeout=5000):
                    break

                pwd_input.fill(password)
                time.sleep(0.5)
                _click_primary_auth_button(page, pwd_input, ["Continue", "继续", "Log in"])
                time.sleep(5)

                if not _is_google_redirect(page):
                    break

                _screenshot(page, f"codex_03_google_redirect_attempt{attempt + 1}.png")
                logger.warning("[Codex] 密码步骤误跳转到 Google 登录,返回重试... (attempt %d)", attempt + 1)
                page.go_back(wait_until="domcontentloaded", timeout=30000)
                time.sleep(2)
            _screenshot(page, "codex_03_after_password.png")
        except Exception:
            _screenshot(page, "codex_03_no_password.png")

        # 可能需要邮箱登录验证码
        _screenshot(page, "codex_03b_check_otp.png")
        code_input = None
        try:
            code_input = page.locator(
                'input[name="code"], input[placeholder*="验证码"], input[placeholder*="code" i]'
            ).first
            if not code_input.is_visible(timeout=5000):
                code_input = None
        except Exception:
            code_input = None

        if code_input and mail_client:
            logger.info("[Codex] 需要登录验证码,等待 emailId > %d 的新邮件...", _email_id_before_login)

            start_t = time.time()
            otp_code = None
            otp_email_id = 0
            while time.time() - start_t < 120:
                emails = mail_client.search_emails_by_recipient(email, size=5)
                for em in emails:
                    email_id = em.get("emailId", 0)
                    if email_id <= _email_id_before_login or email_id in _used_email_ids:
                        continue
                    subj = em.get("subject", "").lower()
                    if "invited" in subj or "invitation" in subj:
                        continue
                    otp_code = mail_client.extract_verification_code(em)
                    if otp_code:
                        otp_email_id = email_id
                        break
                if otp_code:
                    break
                time.sleep(3)

            if otp_code:
                _used_email_ids.add(otp_email_id)
                logger.info("[Codex] 获取到验证码: %s", otp_code)
                code_input.fill(otp_code)
                time.sleep(0.5)
                page.locator(
                    'button:has-text("Continue"), button:has-text("继续"), button[type="submit"]'
                ).first.click()
                time.sleep(5)
                _screenshot(page, "codex_03c_after_otp.png")
            else:
                logger.warning("[Codex] 未获取到验证码")
        elif code_input:
            logger.warning("[Codex] 需要验证码但无 mail_client,无法自动获取")

        # SPEC-2 shared/add-phone-detection §4 (位点 C-P1):about-you 入口前先探针。
        # 注册流程提交 OTP 后 OpenAI 经常把账号引到 add-phone,等切到 about-you 再发现就太晚。
        assert_not_blocked(page, "oauth_about_you")

        # 处理 about-you 页面(可能出现在 OAuth 流程中)
        if "about-you" in page.url:
            if not _complete_oauth_about_you(page, signup_profile=signup_profile):
                logger.warning("[Codex] about-you 未完成,放弃本次 OAuth,交由上层重试/分类处理")
                close_playwright_objects(page, context, browser, logger=logger, label="codex-oauth")
                return None

        # Round 11 三轮 — Personal 模式: pre-consent workspace_select.
        # 历史:
        #   Round 8 加 explicit workspace_select 是因为"default 不会自动 unset"研究结论,
        #   想强制把 default 切到 Personal 再 consent。
        #   Round 11 二轮把 workspace_select 前置到 consent loop 之前,绕过"consent loop 1-2
        #   步抓到 auth_code → workspace_select 永远不被调用"的 bug。
        #
        # Round 11 三轮发现:刚踢出 Team 的新号在 OpenAI auth backend 端
        # `oai-oauth-session.workspaces=[]`(server-side 状态),/workspace UI 显示
        # "Workspaces not found in client auth session" 错误。force_select_personal_via_ui
        # 把浏览器 goto /workspace,落在错误页 → consent loop 找不到 consent button →
        # 永远 bundle=None,5 次外层重试全失败。
        #
        # 修复:把 pre-consent workspace_select 改成"尽力而为"——
        #   1. 如果 workspaces[] 非空,正常 POST /api/accounts/workspace/select 切 personal
        #   2. 如果 workspaces[] 空(刚踢出场景),不再 goto /workspace UI(肯定错),而是直接
        #      跳过 → 让 consent loop 在 auth_url 上自然运行。OAuth backend 用 default
        #      workspace 颁 token,如果 plan!=free,外层 5 次重试 + bundle plan_type 校验
        #      会拦下来,等后端最终一致性同步(回归 Round 4/e760be9 8s sleep 行为)
        #   3. 任何情况 finally 强制 goto auth_url,确保 consent loop 入口正确
        if use_personal:
            try:
                from autoteam.oauth_workspace import (
                    ensure_personal_workspace_selected as _pre_consent_ws_select,
                )

                # 用当前页面状态(login + about-you 完成,session cookie 已建立)调 workspace/select.
                # auth_url 作为 fallback 的 base 用,正常成功不会用到.
                pre_ws_ok, pre_ws_fail, pre_ws_ev = _pre_consent_ws_select(
                    page,
                    consent_url=auth_url,
                    skip_ui_fallback_on_empty=True,  # Round 11 三轮 — 空 workspaces[] 不再 goto /workspace UI
                )
                if pre_ws_ok:
                    logger.info(
                        "[Codex] Personal mode pre-consent workspace_select 成功 — "
                        "后续 consent 应颁 plan=free token"
                    )
                else:
                    logger.warning(
                        "[Codex] Personal mode pre-consent workspace_select 失败 "
                        "fail_category=%s evidence=%s,继续走 consent loop(由外层重试兜底)",
                        pre_ws_fail,
                        json.dumps(pre_ws_ev, ensure_ascii=False)[:300],
                    )
                _screenshot(page, "codex_03e_pre_consent_workspace_select.png")
            except Exception as exc:
                logger.warning(
                    "[Codex] Personal mode pre-consent workspace_select 异常: %s,"
                    "继续 consent loop",
                    exc,
                )

            # Round 11 三轮 — 仅在浏览器停留在 /workspace 错误页时导航回 auth_url。
            # /workspace 路径出错(force_select_personal_via_ui 走该 URL 但 server 返
            # "Workspaces not found in client auth session"),consent loop 找不到按钮永远 bundle=None。
            # 但若浏览器在 /log-in / /password / about-you 等正常 OAuth 流程页,goto(auth_url) 会
            # **重置** login 表单状态(email/password 输入清空),consent loop 再也跑不通。
            # 所以只针对已知的 /workspace 错误页恢复,其他保持 page 当前状态让流程自然跑完。
            try:
                current_url = (page.url or "")
                if "/workspace" in current_url:
                    logger.info(
                        "[Codex] pre-consent 后浏览器停在 /workspace 错误页,导航回 auth_url 恢复 consent flow (current_url=%s)",
                        current_url[:120],
                    )
                    page.goto(auth_url, wait_until="domcontentloaded", timeout=15000)
                    time.sleep(2)
            except Exception as exc:
                logger.warning("[Codex] 导航回 auth_url 失败: %s,consent loop 仍尝试", exc)

        # 处理多个授权/同意页面(可能有多步)
        for step in range(10):
            if auth_code:
                break

            # SPEC-2 shared/add-phone-detection §4 (位点 C-P2):consent 循环每轮开头探针。
            # 不在循环里拦,会被当作"workspace 没选好"反复重试,30s callback 等待白白耗费。
            assert_not_blocked(page, f"oauth_consent_{step}")

            _screenshot(page, f"codex_04_step{step + 1}_before.png")

            try:
                if _is_choose_account_page(page):
                    _screenshot(page, f"codex_04_choose_account_{step + 1}_before.png")
                    logger.info("[Codex] 检测到账号选择页 (step %d),尝试选择: %s", step + 1, email)
                    selected = _select_oauth_account(page, email)
                    _screenshot(page, f"codex_04_choose_account_{step + 1}_after.png")
                    if selected and not _is_choose_account_page(page):
                        continue
                    if not selected:
                        logger.warning("[Codex] 无法自动选择 OAuth 账号: %s (step %d)", email, step + 1)
            except Exception:
                pass

            # 在任何页面中,如果有 workspace/组织选择,先选 Team(personal 模式下选个人)
            try:
                # Round 11 — 与 cnitlrt/AutoTeam upstream codex_auth.py:772-815 对齐:
                # consent loop 每个 step 起始先用 upstream-style 健壮检测,避免页面变 workspace
                # 选择页时被当成 "consent button 不可见" → break → 30s callback 等待白白消耗。
                # Team 模式优先用 upstream helper;personal 模式留给 ensure_personal_workspace_selected
                # 在 consent loop 之后兜底(L916+)。
                if not use_personal:
                    from autoteam.oauth_workspace import (
                        _is_workspace_selection_page as _ws_is_selection_page,
                    )
                    from autoteam.oauth_workspace import (
                        _select_team_workspace as _ws_select_team,
                    )

                    workspace_name_upstream = get_chatgpt_workspace_name()
                    if _ws_is_selection_page(page):
                        _screenshot(page, f"codex_04_workspace_{step + 1}_before.png")
                        logger.info(
                            "[Codex] 检测到工作空间选择页 (step %d, upstream),尝试选择: %s",
                            step + 1,
                            workspace_name_upstream,
                        )
                        upstream_selected = _ws_select_team(page, workspace_name_upstream)
                        _screenshot(page, f"codex_04_workspace_{step + 1}_after.png")
                        if upstream_selected:
                            # 选完 workspace 后点"继续"按钮提交
                            try:
                                cont_btn = page.locator(
                                    'button:has-text("继续"), button:has-text("Continue")'
                                ).first
                                if cont_btn.is_visible(timeout=3000):
                                    cont_btn.click()
                                    time.sleep(3)
                                    logger.info("[Codex] 已点击继续 (step %d, upstream)", step + 1)
                            except Exception:
                                pass
                            continue
                        else:
                            logger.warning(
                                "[Codex] upstream-style 无法选择 workspace '%s' (step %d),回退现有 JS/locator 路径",
                                workspace_name_upstream,
                                step + 1,
                            )

                page_text = page.inner_text("body")[:1000]

                # personal 模式:检测到工作空间选择页时,直接选 Personal
                if use_personal and (
                    "选择一个工作空间" in page_text or "Select a workspace" in page_text or "选择工作空间" in page_text
                ):
                    _screenshot(page, f"codex_04_personal_ws_{step + 1}_before.png")
                    logger.info("[Codex] 检测到工作空间选择页 (step %d, personal 模式)", step + 1)
                    personal_selected = False
                    try:
                        personal_btn = page.locator("text=/个人|Personal/").first
                        if personal_btn.is_visible(timeout=2000):
                            personal_btn.click(force=True)
                            time.sleep(1)
                            personal_selected = True
                            logger.info("[Codex] 已选择 Personal workspace (step %d)", step + 1)
                    except Exception as e:
                        logger.warning("[Codex] 选择 Personal 失败: %s", e)
                    _screenshot(page, f"codex_04_personal_ws_{step + 1}_after.png")
                    if personal_selected:
                        try:
                            cont_btn = page.locator('button:has-text("继续"), button:has-text("Continue")').first
                            if cont_btn.is_visible(timeout=3000):
                                cont_btn.click()
                                time.sleep(3)
                        except Exception:
                            pass
                        continue

                # 选择 Team workspace(用配置的名称精确匹配)— upstream-style 检测失败后的 JS/locator 兜底
                workspace_name = "" if use_personal else get_chatgpt_workspace_name()
                # 检测"选择一个工作空间"页面,点击 Team workspace
                if workspace_name and (
                    "选择一个工作空间" in page_text or "Select a workspace" in page_text or "选择工作空间" in page_text
                ):
                    selected = False
                    _screenshot(page, f"codex_04_workspace_{step + 1}_before.png")
                    logger.info("[Codex] 检测到工作空间选择页 (step %d),尝试选择: %s", step + 1, workspace_name)

                    # 用 JS 直接点击包含 workspace 名称的元素(最可靠)
                    try:
                        clicked = page.evaluate(
                            """(name) => {

                            const els = document.querySelectorAll('*');

                            for (const el of els) {

                                const text = (el.textContent || '').trim();

                                if (text === name && !text.includes('个人') && !text.includes('Personal')) {

                                    // 找到最近的可点击父元素

                                    let target = el;

                                    while (target && target.tagName !== 'BODY') {

                                        const tag = target.tagName.toLowerCase();

                                        if (['button', 'a', 'li', 'label'].includes(tag)

                                            || target.getAttribute('role')

                                            || target.onclick

                                            || target.classList.length > 0) {

                                            target.click();

                                            return true;

                                        }

                                        target = target.parentElement;

                                    }

                                    el.click();

                                    return true;

                                }

                            }

                            return false;

                        }""",
                            workspace_name,
                        )
                        if clicked:
                            time.sleep(1)
                            selected = True
                            logger.info("[Codex] 已选择 workspace (JS): %s (step %d)", workspace_name, step + 1)
                    except Exception as e:
                        logger.warning("[Codex] JS 选择 workspace 失败: %s", e)

                    if not selected:
                        # fallback: Playwright 选择器
                        try:
                            ws_el = page.locator(f"text={workspace_name}").first
                            if ws_el.is_visible(timeout=2000):
                                ws_el.click(force=True)
                                time.sleep(1)
                                selected = True
                                logger.info(
                                    "[Codex] 已选择 workspace (force click): %s (step %d)", workspace_name, step + 1
                                )
                        except Exception:
                            pass

                    _screenshot(page, f"codex_04_workspace_{step + 1}_after.png")
                    if selected:
                        # 选完 workspace 后点"继续"按钮提交
                        try:
                            cont_btn = page.locator('button:has-text("继续"), button:has-text("Continue")').first
                            if cont_btn.is_visible(timeout=3000):
                                cont_btn.click()
                                time.sleep(3)
                                logger.info("[Codex] 已点击继续 (step %d)", step + 1)
                        except Exception:
                            pass
                        continue
                    else:
                        logger.warning("[Codex] 无法选择 workspace '%s' (step %d)", workspace_name, step + 1)

                elif workspace_name:
                    # 非工作空间选择页,但可能有 workspace 文本(如 organization 页)
                    try:
                        ws_btn = page.locator(f'text="{workspace_name}"').first
                        if ws_btn.is_visible(timeout=1000):
                            ws_btn.click()
                            time.sleep(1)
                            logger.info("[Codex] 已选择 workspace: %s (step %d)", workspace_name, step + 1)
                    except Exception:
                        pass

                # Organization 页面的下拉选择 — 与 upstream codex_auth.py:798-813 对齐
                if "organization" in page.url:
                    dropdown = page.locator("[aria-expanded], [aria-haspopup]").first
                    if dropdown.is_visible(timeout=2000):
                        dropdown.click()
                        time.sleep(1)
                        options = page.locator('[role="option"]').all()
                        for opt in options:
                            text = opt.inner_text(timeout=1000).strip()
                            if text and "新组织" not in text and "New" not in text:
                                opt.click()
                                logger.info("[Codex] 选择已有组织: %s", text)
                                break
                        else:
                            if options:
                                options[0].click()
                        time.sleep(1)
            except Exception:
                pass

            # 处理密码页面(可能在 consent 流程中出现)
            try:
                pwd_field = page.locator('input[name="password"], input[type="password"]').first
                if pwd_field.is_visible(timeout=2000):
                    if password:
                        logger.info("[Codex] 需要重新输入密码 (step %d)...", step + 1)
                        pwd_field.fill(password)
                        time.sleep(0.5)
                        _click_primary_auth_button(page, pwd_field, ["Continue", "继续", "Log in"])
                    else:
                        # 没密码,点"使用一次性验证码登录"
                        otp_btn = page.locator(
                            'button:has-text("一次性验证码"), button:has-text("one-time"), button:has-text("email login")'
                        ).first
                        if otp_btn.is_visible(timeout=3000):
                            logger.info("[Codex] 无密码,点击一次性验证码登录 (step %d)", step + 1)
                            otp_btn.click()
                        else:
                            _click_primary_auth_button(page, pwd_field, ["Continue", "继续", "Log in"])
                    time.sleep(5)
                    _screenshot(page, f"codex_04_password_{step + 1}.png")
                    continue
            except Exception:
                pass

            # 处理邮箱验证码页面(可能在 consent 流程中出现)
            try:
                if _is_otp_input_visible(page, timeout=2000) and mail_client:
                    submit_status = _resolve_email_verification(
                        page,
                        mail_client=mail_client,
                        email=email,
                        after_email_id=_email_id_before_login,
                        used_email_ids=_used_email_ids,
                        wait_log=f"[Codex] 需要邮箱验证码 (step {step + 1}),等待 emailId > %d 的新邮件...",
                        require_sender=True,
                    )
                    if submit_status == "accepted":
                        logger.info("[Codex] 验证码页已退出,继续后续授权流程")
                        continue
            except Exception:
                pass

            try:
                consent_btn = page.locator(
                    'button:has-text("继续"), button:has-text("Continue"), button:has-text("Allow")'
                ).first
                if consent_btn.is_visible(timeout=5000):
                    logger.info("[Codex] 点击同意/继续按钮 (step %d)...", step + 1)
                    consent_btn.click()
                    time.sleep(5)
                    _screenshot(page, f"codex_04_consent_{step + 1}.png")
                else:
                    break
            except Exception:
                break

        # Round 11 二轮 — pre-consent workspace_select 已前置(line 632+);此处作为兜底:
        # consent loop 自然结束(auth_code 未抓到,可能 workspace_select 仍未触发后端 default 切换)
        # 时再调一次 workspace_select。
        # Round 8 原始动机:personal OAuth 需要在 callback 之前**主动**选 personal workspace,
        # 否则 issuer 按 default_workspace_id(sticky 指向 Team)颁 token,拿到 plan_type=team。
        # 三层兜底:HTTP /api/accounts/workspace/select(主路径)→ Playwright UI fallback →
        # 失败则返回 fail_category 由外层 5 次重试承担。
        # Team 路径(use_personal=False)完全跳过 — 默认 default_workspace_id 已指向 Team。
        if use_personal and not auth_code:
            try:
                from autoteam.oauth_workspace import ensure_personal_workspace_selected

                consent_url_for_select = page.url or "https://auth.openai.com/sign-in-with-chatgpt/codex/consent"
                ws_ok, ws_fail_category, ws_evidence = ensure_personal_workspace_selected(
                    page,
                    consent_url=consent_url_for_select,
                    skip_ui_fallback_on_empty=True,  # Round 11 三轮 — 同 pre-consent
                )
                if ws_ok:
                    logger.info("[Codex] personal workspace 选择成功,继续等 callback")
                else:
                    logger.warning(
                        "[Codex] personal workspace 选择失败 fail_category=%s evidence=%s",
                        ws_fail_category,
                        json.dumps(ws_evidence, ensure_ascii=False)[:300],
                    )
                _screenshot(page, "codex_04b_after_workspace_select.png")
            except Exception as exc:
                logger.warning("[Codex] ensure_personal_workspace_selected 异常: %s", exc)

        # SPEC-2 shared/add-phone-detection §4 (位点 C-P3):等 callback 前探针。
        # add-phone 阻塞 = "callback 永远不来"的根因,30s 等待白白浪费。
        assert_not_blocked(page, "oauth_callback_wait")

        # 等待 redirect callback 获取 auth code
        for _ in range(30):
            if auth_code:
                break
            # 也从当前 URL 尝试提取(CPA 可能接收了回调)
            try:
                cur = page.url
                if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in cur:
                    parsed = urllib.parse.urlparse(cur)
                    qs = urllib.parse.parse_qs(parsed.query)
                    auth_code = qs.get("code", [None])[0]
                    if auth_code:
                        logger.info("[Codex] 从 URL 捕获到 auth code!")
                        break
            except Exception:
                pass
            time.sleep(1)

        if not auth_code:
            _screenshot(page, "codex_05_no_callback.png")
            logger.warning("[Codex] 未获取到 auth code,当前 URL: %s", page.url)

        # SPEC-2 §3.4.5 (位点 C-P4) + Round 6 PRD-5 FR-P1.1:personal 拒收 bundle 之前的最后一道关卡。
        # 防御性 — 通常 C-P1~C-P3 已拦下,但 add-phone 可能在 callback 阶段后晚到一两秒;
        # 此处在统一 cleanup 之前做最后一次探针,确保 page 仍可读。命中即抛 RegisterBlocked,
        # 上游 5 个调用方按 add-phone-detection.md §5.2 矩阵分类处置(personal/team/reinvite/api 各异)。
        try:
            assert_not_blocked(page, "oauth_personal_check")
        except Exception:
            # assert_not_blocked 命中 add-phone 会抛 RegisterBlocked — 必须传播给上层
            # 但要保证 browser 资源被释放
            close_playwright_objects(page, context, browser, logger=logger, label="codex-oauth")
            raise

        # Round 11 五轮 Option A — 阶段 1 完成,先尝试 exchange + plan 校验
        # 校验在 with sync_playwright() 块内做,失败时可以直接 fallback 到阶段 2
        # (使用同一 context,清空 cookies 后重新登录)而不需要重新启动 Playwright。
        stage1_bundle = None
        if auth_code:
            stage1_bundle = _exchange_auth_code(auth_code, code_verifier, fallback_email=email)
            if stage1_bundle:
                stage1_plan = (stage1_bundle.get("plan_type") or "").lower()
                logger.info(
                    "[Codex] 阶段 1 OAuth 完成: plan_type=%s account_id=%s",
                    stage1_plan or "unknown",
                    stage1_bundle.get("account_id"),
                )

        # 阶段 1 直接成功的判断:Team 路径不校验 plan,有 bundle 即可;
        # personal 路径仅 plan == "free" 算阶段 1 真成功。
        stage1_ok = bool(stage1_bundle) and (
            (not use_personal) or (stage1_bundle.get("plan_type") or "").lower() == "free"
        )

        # 阶段 2 触发条件 — 仅 personal 模式 + 阶段 1 失败(bundle=None 或 plan != free)
        # 触发后:context.clear_cookies() + fresh chatgpt.com login + 重新走 OAuth
        if (not stage1_ok) and use_personal:
            stage1_plan = (
                (stage1_bundle.get("plan_type") or "").lower() if stage1_bundle else "none"
            )
            logger.warning(
                "[Codex] 阶段 1 拿到 plan=%s(期望 free)bundle=%s,触发阶段 2 fresh re-login fallback",
                stage1_plan,
                "yes" if stage1_bundle else "no",
            )

            # === 阶段 2 入口 ===
            # 1) 清空 cookies + fresh chatgpt.com login(用 keyboard.type 绕过灰按钮)
            relogin_ok = _perform_fresh_relogin_in_context(
                context,
                email,
                password,
                mail_client,
                used_email_ids=_used_email_ids,
            )
            if not relogin_ok:
                logger.warning("[Codex] 阶段 2 fresh re-login 未成功,放弃")
                close_playwright_objects(page, context, browser, logger=logger, label="codex-oauth")
                return None

            # 2) 重新生成 PKCE + state(原 auth_code 已 used,新 OAuth 必须用新 code_verifier)
            stage2_code_verifier, stage2_code_challenge = _generate_pkce()
            stage2_state = secrets.token_urlsafe(16)

            # Round 11 V8 — fresh re-login 已清空 cookies,personal_uuid 之前在 silent step-0
            # 拿到的值仍可复用(personal workspace UUID 是稳定标识,不会因 re-login 重置)。
            # 若 stage 1 没拿到(prefetched_personal_uuid 也是 None),这里再尝试一次:
            #   V13 primary: NextAuth /api/auth/session accessToken → page.evaluate Bearer
            #                fetch_personal_uuid_via_page(浏览器内,避 cf 拦截)
            #   fallback: 原 in-browser POST /accounts/personal cookie 路径
            if use_personal and not personal_uuid:
                fetch_page = None
                try:
                    fetch_page = context.new_page()
                    fetch_page.goto("https://chatgpt.com/", wait_until="domcontentloaded", timeout=30000)
                    time.sleep(5)

                    # Round 14 — 全新 OAuth 建号(无 chatgpt_session_token)关键修复:
                    # fresh re-login 只在 auth.openai.com 端登录,chatgpt.com 端 session 尚未建立,
                    # 且首访常有 Cloudflare 挑战。直接 fetch /accounts/personal 会 401/403 →
                    # personal_uuid=None → consent skipped_empty_workspaces 永远拿不到 token。
                    # 仿照 silent step-0:先过 cf,再强制 NextAuth session refresh + accounts/check,
                    # 让 server 端用 auth.openai.com 会话 SSO 建立 chatgpt.com session 并 populate
                    # personal workspace,随后 getOrCreate 才会 200。
                    for _cf_i in range(8):
                        try:
                            _html_lower = fetch_page.content()[:2000].lower()
                        except Exception:
                            _html_lower = ""
                        if "verify you are human" not in _html_lower and "challenge" not in (fetch_page.url or "").lower():
                            break
                        logger.info("[Codex] 阶段 2 等待 chatgpt.com Cloudflare... (%ds)", _cf_i * 5)
                        time.sleep(5)
                    for _warm_path in ("/api/auth/session?update", "/backend-api/accounts/check"):
                        try:
                            fetch_page.evaluate(
                                "async (p) => { try { await fetch(p, {credentials:'include', cache:'no-store'}); } catch(e){} }",
                                _warm_path,
                            )
                        except Exception:
                            pass
                        time.sleep(1)

                    nextauth_token = fetch_nextauth_backend_access_token(fetch_page)
                    if nextauth_token:
                        fetched_uuid = fetch_personal_uuid_via_page(
                            fetch_page, nextauth_token
                        )
                        if fetched_uuid:
                            personal_uuid = fetched_uuid
                            logger.info(
                                "[Codex] 阶段 2 NextAuth Bearer 路径(浏览器内)拿到 personal_workspace_id=%s",
                                personal_uuid,
                            )

                    if not personal_uuid:
                        fetched_uuid = fetch_page.evaluate(
                            """

                            async () => {

                              try {

                                const resp = await fetch('/backend-api/accounts/personal', {

                                  method: 'POST',

                                  credentials: 'include',

                                  headers: {'Accept': 'application/json'},

                                });

                                if (!resp.ok) return null;

                                const j = await resp.json();

                                return j && j.id ? j.id : null;

                              } catch (e) { return null; }

                            }

                            """
                        )
                        if fetched_uuid:
                            personal_uuid = fetched_uuid
                            logger.info(
                                "[Codex] 阶段 2 cookie fallback 拿到 personal_workspace_id=%s",
                                personal_uuid,
                            )
                except Exception as exc:
                    logger.warning("[Codex] 阶段 2 fetch personal uuid 失败: %s", exc)
                finally:
                    if fetch_page is not None:
                        close_playwright_objects(page=fetch_page, logger=logger, label="codex-stage2-fetch")

            if use_personal and personal_uuid:
                stage2_auth_url = _build_auth_url_with_allowed_workspace(
                    stage2_code_challenge, stage2_state, personal_uuid
                )
                logger.info(
                    "[Codex] 阶段 2 OAuth /authorize 注入 allowed_workspace_id=%s",
                    personal_uuid,
                )
            else:
                stage2_auth_url = _build_auth_url(stage2_code_challenge, stage2_state)

            # 3) 重新走 OAuth — 在同一 context 内开新 page 监听 callback
            stage2_auth_code = None

            def _on_request_stage2(request):
                nonlocal stage2_auth_code
                url = request.url
                if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in url:
                    parsed = urllib.parse.urlparse(url)
                    qs = urllib.parse.parse_qs(parsed.query)
                    stage2_auth_code = qs.get("code", [None])[0]
                    if stage2_auth_code:
                        logger.info("[Codex] 阶段 2 捕获到 auth code!")

            def _on_response_stage2(response):
                nonlocal stage2_auth_code
                url = response.url
                if (
                    f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in url
                    and not stage2_auth_code
                ):
                    parsed = urllib.parse.urlparse(url)
                    qs = urllib.parse.parse_qs(parsed.query)
                    stage2_auth_code = qs.get("code", [None])[0]
                    if stage2_auth_code:
                        logger.info("[Codex] 阶段 2 从 response 捕获到 auth code!")

            stage2_page = context.new_page()
            stage2_page.on("request", _on_request_stage2)
            stage2_page.on("response", _on_response_stage2)
            try:
                stage2_page.goto(stage2_auth_url, wait_until="domcontentloaded", timeout=60000)
                time.sleep(3)
                _screenshot(stage2_page, "codex_relogin_06_oauth_start.png")

                # 因为 context 现在持有 fresh Personal-bound session,
                # /oauth/authorize 通常直接进 consent。
                # 简化的 consent loop(沿用现有 page 的 consent 处理逻辑)
                for s2_step in range(10):
                    if stage2_auth_code:
                        break

                    # add-phone 探针
                    try:
                        assert_not_blocked(stage2_page, f"oauth_consent_stage2_{s2_step}")
                    except Exception:
                        close_playwright_objects(stage2_page, context, browser, logger=logger, label="codex-oauth-stage2")
                        raise

                    _screenshot(stage2_page, f"codex_relogin_07_consent_step{s2_step + 1}.png")

                    # Round 14 — 账号选择页("Welcome back / Choose an account to continue to Codex")。
                    # 新建号完成 about-you 后,OAuth /authorize 在此页列出账号,必须点中自己的账号卡片
                    # 才能继续 consent → 拿 auth code。stage-2 简化 loop 原先只找 Continue/Allow 按钮,
                    # 在此页空点 10 次死循环(codex_relogin_08_no_callback 实证)。复用主流程 helper。
                    try:
                        if _is_choose_account_page(stage2_page):
                            logger.info("[Codex] 阶段 2 检测到账号选择页 (step %d),选择账号: %s", s2_step + 1, email)
                            if _select_oauth_account(stage2_page, email):
                                time.sleep(3)
                                continue
                            logger.warning("[Codex] 阶段 2 账号选择页未能选中 %s", email)
                    except Exception as exc:
                        logger.warning("[Codex] 阶段 2 账号选择异常: %s", exc)

                    # workspace 选择页(personal 路径)
                    try:
                        page_text = stage2_page.inner_text("body")[:1000]
                        if (
                            "选择一个工作空间" in page_text
                            or "Select a workspace" in page_text
                            or "选择工作空间" in page_text
                        ):
                            logger.info("[Codex] 阶段 2 检测到 workspace 选择页 (step %d)", s2_step + 1)
                            try:
                                personal_btn = stage2_page.locator("text=/个人|Personal/").first
                                if personal_btn.is_visible(timeout=2000):
                                    personal_btn.click(force=True)
                                    time.sleep(1)
                                    cont_btn = stage2_page.locator(
                                        'button:has-text("继续"), button:has-text("Continue")'
                                    ).first
                                    if cont_btn.is_visible(timeout=3000):
                                        cont_btn.click()
                                        time.sleep(3)
                                    continue
                            except Exception as exc:
                                logger.warning("[Codex] 阶段 2 选 Personal 失败: %s", exc)
                    except Exception:
                        pass

                    # 同意/继续按钮
                    try:
                        consent_btn = stage2_page.locator(
                            'button:has-text("继续"), button:has-text("Continue"), button:has-text("Allow")'
                        ).first
                        if consent_btn.is_visible(timeout=5000):
                            logger.info("[Codex] 阶段 2 点击同意按钮 (step %d)", s2_step + 1)
                            consent_btn.click()
                            time.sleep(5)
                        else:
                            break
                    except Exception:
                        break

                # 等 callback
                for _ in range(30):
                    if stage2_auth_code:
                        break
                    try:
                        cur2 = stage2_page.url
                        if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in cur2:
                            parsed = urllib.parse.urlparse(cur2)
                            qs = urllib.parse.parse_qs(parsed.query)
                            stage2_auth_code = qs.get("code", [None])[0]
                            if stage2_auth_code:
                                logger.info("[Codex] 阶段 2 从 URL 捕获到 auth code!")
                                break
                    except Exception:
                        pass
                    time.sleep(1)

                if not stage2_auth_code:
                    _screenshot(stage2_page, "codex_relogin_08_no_callback.png")
                    logger.warning(
                        "[Codex] 阶段 2 未获取到 auth code,当前 URL: %s",
                        (stage2_page.url or "")[:120],
                    )
            finally:
                close_playwright_objects(page=stage2_page, logger=logger, label="codex-stage2-page")

            close_playwright_objects(page, context, browser, logger=logger, label="codex-oauth")

            if not stage2_auth_code:
                logger.warning("[Codex] 阶段 2 OAuth 失败,放弃")
                return None

            stage2_bundle = _exchange_auth_code(
                stage2_auth_code, stage2_code_verifier, fallback_email=email
            )
            if not stage2_bundle:
                logger.warning("[Codex] 阶段 2 _exchange_auth_code 失败")
                return None

            stage2_plan = (stage2_bundle.get("plan_type") or "").lower()
            logger.info(
                "[Codex] 阶段 2 OAuth 完成: plan_type=%s account_id=%s",
                stage2_plan or "unknown",
                stage2_bundle.get("account_id"),
            )

            if stage2_plan != "free":
                logger.warning(
                    "[Codex] 阶段 2 仍拿到 plan_type=%s(期望 free),返回 None 由外层 5 次重试兜底",
                    stage2_plan or "unknown",
                )
                return None

            if personal_uuid:
                stage2_bundle["personal_workspace_id"] = personal_uuid
            if return_result:
                return _oauth_result_from_bundle(stage2_bundle)
            return stage2_bundle

        # 阶段 1 通过(非 personal 或 plan == free) — 走原退出路径
        close_playwright_objects(page, context, browser, logger=logger, label="codex-oauth")

    if not auth_code:
        logger.error("[Codex] OAuth 登录失败: 未获取到 authorization code")
        if return_result:
            return {
                "ok": False,
                "bundle": None,
                "error_type": "auth_code_missing",
                "error_detail": "未获取到 authorization code",
                "retryable": True,
            }
        return None

    if not stage1_bundle:
        if return_result:
            return {
                "ok": False,
                "bundle": None,
                "error_type": "token_exchange_failed",
                "error_detail": "Token 交换失败",
                "retryable": True,
            }
        return None

    # Personal 模式强校验 plan_type:当子号还挂在 Team workspace(OpenAI 后端 kick 同步延迟 /
    # default workspace 为 Team)时,auth.openai.com 会默认选 Team 颁发 token,拿到 plan_type=team
    # 的 bundle —— 这个 token 绑在 Team account_id 上,一旦子号离开 Team 就作废(refresh 401),
    # 本地却标成 PERSONAL,用户看到的是"可用免费号"但 Codex 跑不动。必须在这里拒收。
    #
    # Round 11 hotfix:本函数单次拒收返回 None,由 manager._run_post_register_oauth 外层 5 次重试承担
    # (W-I9 spec — workspace/select 主路径成功但 callback 拿到 plan!=free 时,需进入外层重试触发
    # 后端最终一致性,而非立即 fail-fast)。日志级别从 ERROR 调整为 WARNING,因为这不是真正的错误
    # 而是预期的 retry 触发器。
    #
    # Round 11 五轮:阶段 1 plan != free 已在 with 块内被 stage2 fallback 拦截,
    # 走到这里说明阶段 1 就是 plan == free(或非 personal 路径,无校验)。
    if use_personal:
        plan = (stage1_bundle.get("plan_type") or "").lower()
        if plan != "free":
            logger.warning(
                "[Codex] personal 模式拿到 plan_type=%s(期望 free),account_id=%s。"
                "本次拒收(返回 None),由外层 5 次重试触发后端最终一致性同步。",
                plan or "unknown",
                stage1_bundle.get("account_id"),
            )
            if return_result:
                return {
                    "ok": False,
                    "bundle": None,
                    "error_type": "non_free_plan",
                    "error_detail": f"personal OAuth plan={plan or 'unknown'}",
                    "retryable": True,
                }
            return None

    if personal_uuid:
        stage1_bundle["personal_workspace_id"] = personal_uuid
    if return_result:
        return _oauth_result_from_bundle(stage1_bundle)
    return stage1_bundle


def login_codex_via_session():
    """使用管理员 session 复用统一流程完成主号 Codex OAuth 登录。



    Round 10 重构(2026-04-28):删除 1003-1177 行的遗留 inline 实现,改为 thin wrapper

    委托给 SessionCodexAuthFlow(对齐 upstream cnitlrt/AutoTeam:1017-1043)。

    遗留实现的 chatgpt.com/auth/login 重试 fallback 实测无效(只刷新 chatgpt.com 域,

    不影响 auth.openai.com session),且漏了"落 email-input 页时自动填 admin email"这步,

    导致主号 OAuth 必失败。



    SessionCodexAuthFlow 内 _advance → _auto_fill_email 会在 email-input 页自动填

    admin email 并继续 consent 流程,这是 upstream 已经实证的解法。

    """
    logger.info("[Codex] 开始使用 session 登录主号 Codex...")

    flow = SessionCodexAuthFlow(
        email=get_admin_email(),
        session_token=get_admin_session_token(),
        account_id=get_chatgpt_account_id(),
        workspace_name=get_chatgpt_workspace_name(),
        password="",
        password_callback=None,
        auth_file_callback=lambda _bundle: "",
    )

    try:
        result = flow.start()
        step = result.get("step")
        detail = result.get("detail")
        logger.info("[Codex] 主号 session OAuth 初始结果: step=%s detail=%s", step, detail)
        if step != "completed":
            logger.warning("[Codex] 主号 session OAuth 未直接完成: step=%s detail=%s", step, detail)
            return None

        info = flow.complete()
        return info.get("bundle")
    finally:
        flow.stop()


class SessionCodexAuthFlow:
    EMAIL_SELECTORS = [
        'input[name="email"]',
        'input[id="email-input"]',
        'input[id="email"]',
        'input[type="email"]',
        'input[placeholder*="email" i]',
        'input[placeholder*="邮箱"]',
        'input[autocomplete="email"]',
        'input[autocomplete="username"]',
    ]
    PASSWORD_SELECTORS = [
        'input[name="password"]',
        'input[type="password"]',
    ]
    CODE_SELECTORS = [
        'input[name="code"]',
        'input[placeholder*="验证码"]',
        'input[placeholder*="code" i]',
        'input[inputmode="numeric"]',
        'input[autocomplete="one-time-code"]',
    ]
    OTP_OPTION_SELECTORS = [
        'button:has-text("一次性验证码")',
        'button:has-text("邮箱验证码")',
        'button:has-text("Email login")',
        'button:has-text("email login")',
        'button:has-text("one-time")',
        'button:has-text("One-time")',
        'button:has-text("email code")',
        'button:has-text("Email code")',
        'a:has-text("一次性验证码")',
        'a:has-text("邮箱验证码")',
        'a:has-text("Email login")',
        'a:has-text("one-time")',
    ]

    def __init__(

        self,

        *,

        email,

        session_token,

        account_id,

        workspace_name="",

        password="",

        password_callback=None,

        auth_file_callback=None,

    ):
        self.email = email or ""
        self.password = password or ""
        self.workspace_name = workspace_name or ""
        self.account_id = account_id or ""
        self.session_token = session_token or ""
        self.password_callback = password_callback
        self.auth_file_callback = auth_file_callback or save_auth_file
        self.code_verifier, code_challenge = _generate_pkce()
        self.state = secrets.token_urlsafe(16)
        self.auth_url = _build_auth_url(code_challenge, self.state)
        self.auth_code = None
        self.chatgpt = None
        self.page = None

    def _visible_locator(self, selectors, timeout_ms=5000):
        if not self.page:
            return None

        selector = ", ".join(selectors)
        deadline = time.time() + timeout_ms / 1000
        while time.time() < deadline:
            frames = [self.page.main_frame]
            frames.extend(frame for frame in self.page.frames if frame != self.page.main_frame)
            for frame in frames:
                try:
                    locator = frame.locator(selector).first
                    if locator.is_visible(timeout=250):
                        return locator
                except Exception:
                    pass
            time.sleep(0.2)
        return None

    def _detect_step(self):
        if self.auth_code:
            return "completed", None

        cur = self.page.url if self.page else ""
        if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in cur:
            parsed = urllib.parse.urlparse(cur)
            qs = urllib.parse.parse_qs(parsed.query)
            self.auth_code = qs.get("code", [None])[0]
            if self.auth_code:
                return "completed", None

        if self._visible_locator(self.CODE_SELECTORS, timeout_ms=800):
            return "code_required", None
        if self._visible_locator(self.PASSWORD_SELECTORS, timeout_ms=800):
            return "password_required", None
        if self._visible_locator(self.EMAIL_SELECTORS, timeout_ms=800):
            return "email_required", None
        return "unknown", cur

    def _attach_callback_listeners(self):
        def on_request(request):
            if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in request.url:
                parsed = urllib.parse.urlparse(request.url)
                qs = urllib.parse.parse_qs(parsed.query)
                self.auth_code = qs.get("code", [None])[0]

        def on_response(response):
            if self.auth_code:
                return
            if f"localhost:{CODEX_CALLBACK_PORT}/auth/callback" in response.url:
                parsed = urllib.parse.urlparse(response.url)
                qs = urllib.parse.parse_qs(parsed.query)
                self.auth_code = qs.get("code", [None])[0]

        self.page.on("request", on_request)
        self.page.on("response", on_response)

    def _inject_auth_cookies(self):
        cookies = []
        if len(self.session_token) > 3800:
            cookies.extend(
                [
                    {
                        "name": "__Secure-next-auth.session-token.0",
                        "value": self.session_token[:3800],
                        "domain": "auth.openai.com",
                        "path": "/",
                        "httpOnly": True,
                        "secure": True,
                        "sameSite": "Lax",
                    },
                    {
                        "name": "__Secure-next-auth.session-token.1",
                        "value": self.session_token[3800:],
                        "domain": "auth.openai.com",
                        "path": "/",
                        "httpOnly": True,
                        "secure": True,
                        "sameSite": "Lax",
                    },
                ]
            )
        else:
            cookies.append(
                {
                    "name": "__Secure-next-auth.session-token",
                    "value": self.session_token,
                    "domain": "auth.openai.com",
                    "path": "/",
                    "httpOnly": True,
                    "secure": True,
                    "sameSite": "Lax",
                }
            )

        if self.account_id:
            cookies.append(
                {
                    "name": "_account",
                    "value": self.account_id,
                    "domain": "auth.openai.com",
                    "path": "/",
                    "secure": True,
                    "sameSite": "Lax",
                }
            )

        cookies.append(
            {
                "name": "oai-did",
                "value": self.chatgpt.oai_device_id,
                "domain": "auth.openai.com",
                "path": "/",
                "secure": True,
                "sameSite": "Lax",
            }
        )
        self.chatgpt.context.add_cookies(cookies)

    def _click_workspace_or_consent(self):
        acted = False

        try:
            if "workspace" in self.page.url and self.workspace_name:
                ws_btn = self.page.locator(f'text="{self.workspace_name}"').first
                if ws_btn.is_visible(timeout=1000):
                    ws_btn.click()
                    logger.info("[Codex] 主号选择 workspace: %s", self.workspace_name)
                    time.sleep(2)
                    acted = True
        except Exception:
            pass

        try:
            consent_btn = self.page.locator(
                'button:has-text("继续"), button:has-text("Continue"), button:has-text("Allow")'
            ).first
            if consent_btn.is_visible(timeout=1000):
                consent_btn.click()
                logger.info("[Codex] 主号点击继续/授权")
                time.sleep(3)
                acted = True
        except Exception:
            pass

        return acted

    def _auto_fill_email(self):
        email_input = self._visible_locator(self.EMAIL_SELECTORS, timeout_ms=1000)
        if not email_input or not self.email:
            return False

        email_input.fill(self.email)
        time.sleep(0.5)
        _click_primary_auth_button(self.page, email_input, ["Continue", "继续", "Log in"])
        time.sleep(3)
        return True

    def _auto_fill_password(self):
        password_input = self._visible_locator(self.PASSWORD_SELECTORS, timeout_ms=1000)
        if not password_input or not self.password:
            return False

        password_input.fill(self.password)
        time.sleep(0.5)
        _click_primary_auth_button(self.page, password_input, ["Continue", "继续", "Log in"])
        time.sleep(5)
        return True

    def _switch_password_to_otp(self):
        otp_entry = self._visible_locator(self.OTP_OPTION_SELECTORS, timeout_ms=1500)
        if not otp_entry:
            return False

        try:
            otp_entry.click()
        except Exception:
            try:
                otp_entry.click(force=True)
            except Exception:
                return False

        logger.info("[Codex] 主号流程检测到密码页,自动切换到一次性验证码登录")
        time.sleep(3)
        return True

    def _advance(self, attempts=12):
        for _ in range(attempts):
            step, detail = self._detect_step()
            if step == "completed":
                return {"step": "completed", "detail": detail}
            if step == "code_required":
                return {"step": "code_required", "detail": detail}
            if step == "password_required":
                if self._switch_password_to_otp():
                    continue
                if self._auto_fill_password():
                    continue
                return {
                    "step": "unsupported_password",
                    "detail": "主号 Codex 当前停留在密码页,且未找到一次性验证码入口",
                }

            if step == "email_required":
                if self._auto_fill_email():
                    continue
                return {"step": "email_required", "detail": detail}

            if self._click_workspace_or_consent():
                continue

            time.sleep(1)

        final_step, detail = self._detect_step()
        return {"step": final_step, "detail": detail}

    def start(self):
        if not self.session_token:
            raise RuntimeError("缺少登录 session")
        if not self.email:
            raise RuntimeError("缺少登录邮箱")

        from autoteam.chatgpt_api import ChatGPTTeamAPI

        self.chatgpt = ChatGPTTeamAPI()
        try:
            self.chatgpt.start_with_session(
                self.session_token,
                self.account_id,
                self.workspace_name,
                require_browser=True,
            )
            self.page = self.chatgpt.context.new_page()
            self._attach_callback_listeners()
            self._inject_auth_cookies()
            self.page.goto(self.auth_url, wait_until="domcontentloaded", timeout=60000)
            time.sleep(3)
            return self._advance()
        except Exception:
            self.stop()
            raise

    def submit_password(self, password):
        self.password = password
        if self.password_callback:
            self.password_callback(password)
        password_input = self._visible_locator(self.PASSWORD_SELECTORS, timeout_ms=5000)
        if not password_input:
            raise RuntimeError("当前 Codex 登录不是密码输入步骤")

        password_input.fill(password)
        time.sleep(0.5)
        _click_primary_auth_button(self.page, password_input, ["Continue", "继续", "Log in"])
        time.sleep(5)
        return self._advance()

    def submit_code(self, code):
        code_input = self._visible_locator(self.CODE_SELECTORS, timeout_ms=5000)
        if not code_input:
            raise RuntimeError("当前 Codex 登录不是验证码输入步骤")

        code_input.fill(code)
        time.sleep(0.5)
        _click_primary_auth_button(self.page, code_input, ["Continue", "继续", "Verify"])
        time.sleep(5)
        return self._advance()

    def complete(self):
        if not self.auth_code:
            raise RuntimeError("未获取到 Codex authorization code")

        bundle = _exchange_auth_code(self.auth_code, self.code_verifier, fallback_email=self.email)
        if not bundle:
            raise RuntimeError("Codex token 交换失败")

        filepath = self.auth_file_callback(bundle)
        return {
            "email": bundle.get("email"),
            "auth_file": filepath,
            "plan_type": bundle.get("plan_type"),
            "bundle": bundle,
        }

    def stop(self):
        if self.chatgpt:
            self.chatgpt.stop()
        self.chatgpt = None
        self.page = None


class MainCodexLoginFlow(SessionCodexAuthFlow):
    def __init__(self):
        from autoteam.admin_state import get_admin_password

        super().__init__(
            email=get_admin_email(),
            session_token=get_admin_session_token(),
            account_id=get_chatgpt_account_id(),
            workspace_name=get_chatgpt_workspace_name(),
            password=get_admin_password(),
            password_callback=None,
            auth_file_callback=save_main_auth_file,
        )

    def complete(self):
        info = super().complete()
        return {
            "email": info.get("email"),
            "auth_file": info.get("auth_file"),
            "plan_type": info.get("plan_type"),
        }


class MainCodexSyncFlow(MainCodexLoginFlow):
    def complete(self):
        info = super().complete()
        from autoteam.sync_targets import sync_main_codex_to_configured_targets as sync_main_codex_to_cpa

        sync_main_codex_to_cpa(info["auth_file"])
        return {
            "email": info.get("email"),
            "auth_file": info.get("auth_file"),
            "plan_type": info.get("plan_type"),
        }


def login_main_codex():
    """主号 Codex 登录:使用已保存的管理员 session。"""
    return login_codex_via_session()


def save_auth_file(bundle):
    """保存 CPA 兼容的认证文件。同一邮箱只保留一个文件,优先 team。"""
    ensure_auth_dir()

    email = bundle["email"]
    plan_type = bundle.get("plan_type", "unknown")
    account_id = bundle.get("account_id", "")
    hash_id = hashlib.md5(account_id.encode()).hexdigest()[:8]

    # 清理同一邮箱的旧文件(避免 free/team 并存)
    for old in AUTH_DIR.glob(f"codex-{email}-*.json"):
        old.unlink()
        logger.info("[Codex] 清理旧文件: %s", old.name)

    filename = f"codex-{email}-{plan_type}-{hash_id}.json"
    filepath = AUTH_DIR / filename
    return _write_auth_file(filepath, bundle)


def save_main_auth_file(bundle):
    """保存主号 Codex 认证文件,不进入账号池。"""
    account_id = bundle.get("account_id") or hashlib.md5(bundle.get("email", "main").encode()).hexdigest()[:8]

    for old in AUTH_DIR.glob("codex-main-*.json"):
        old.unlink()
        logger.info("[Codex] 清理旧主号文件: %s", old.name)

    filepath = AUTH_DIR / f"codex-main-{account_id}.json"
    return _write_auth_file(filepath, bundle)


def get_saved_main_auth_file():
    """获取本地已保存的主号 Codex 认证文件路径。"""
    candidates = []
    for path in AUTH_DIR.glob("codex-main-*.json"):
        if not path.is_file():
            continue
        try:
            stat = path.stat()
        except Exception:
            continue
        candidates.append((stat.st_mtime, path.name, path))

    if not candidates:
        return ""

    candidates.sort(reverse=True)
    return str(candidates[0][2].resolve())


def refresh_main_auth_file():
    """基于已保存的管理员登录态,刷新并保存主号 Codex 认证文件。"""
    bundle = login_codex_via_session()
    if not bundle:
        raise RuntimeError("无法基于管理员登录态生成主号 Codex 认证文件")

    auth_file = save_main_auth_file(bundle)
    return {
        "email": bundle.get("email"),
        "auth_file": auth_file,
        "plan_type": bundle.get("plan_type"),
    }


def quota_result_quota_info(info):
    """从 check_codex_quota 返回值中提取额度快照。"""
    if not isinstance(info, dict):
        return None
    quota_info = info.get("quota_info")
    if isinstance(quota_info, dict):
        return quota_info
    if "primary_pct" in info or "weekly_pct" in info:
        return info
    return None


def quota_result_resets_at(info):
    """从 check_codex_quota 返回值中提取恢复时间。"""
    if isinstance(info, dict):
        value = info.get("resets_at")
    else:
        value = info

    try:
        return int(value or 0)
    except Exception:
        return 0


def get_quota_exhausted_info(quota_info, *, limit_reached=False):
    """根据额度快照判断是否已耗尽,并返回耗尽详情。



    SPEC-2 shared/quota-classification §4.2:no_quota 优先级**高于** exhausted。

    `primary_total == 0` / `reset_at == 0 + used_pct == 0` 表示 workspace 未分配配额,

    返回 window="no_quota" 形态,resets_at 给 24h 占位(不应被用作重试依据)。

    """
    if not isinstance(quota_info, dict):
        return None

    primary_pct = int(quota_info.get("primary_pct", 0) or 0)
    weekly_pct = int(quota_info.get("weekly_pct", 0) or 0)
    primary_reset = int(quota_info.get("primary_resets_at", 0) or 0)
    weekly_reset = int(quota_info.get("weekly_resets_at", 0) or 0)
    primary_total = quota_info.get("primary_total")
    primary_remaining = quota_info.get("primary_remaining")

    # SPEC-2 shared/quota-classification I4 — no_quota 短路必须先于 exhausted 判定
    no_quota_signals = []
    if primary_total == 0:
        no_quota_signals.append("primary_total==0")
    elif primary_total is None and primary_pct == 0 and primary_reset == 0 and not limit_reached:
        # rate_limit 字段缺失 / primary_window 缺失走这一支(上游 quota_info 取值都是 0)
        no_quota_signals.append("rate_limit_empty")
    if primary_remaining == 0 and (primary_total == 0 or primary_total is None) and primary_pct == 0:
        no_quota_signals.append("remaining==0+total==0")

    if no_quota_signals and not limit_reached and primary_pct < 100 and weekly_pct < 100:
        return {
            "window": "no_quota",
            "resets_at": int(time.time() + 86400),  # 24h 占位
            "quota_info": quota_info,
            "limit_reached": False,
            "no_quota_signals": no_quota_signals,
        }

    # SPEC-2 shared/quota-classification §4.4 I5 (Round 6 PRD-5 FR-P0) — uninitialized_seat 形态。
    # OpenAI fresh seat 懒初始化:wham 给了占位 reset_at>0 但 total/remaining=null,且 pct=0。
    # 此形态在 wham 层无法与"真无配额"区分,**不**直接判 no_quota,而是返回 window=
    # "uninitialized_seat" + needs_codex_smoke=True,要求上游调 cheap_codex_smoke 二次验证。
    if (
        primary_total is None
        and primary_remaining is None
        and primary_pct == 0
        and weekly_pct == 0
        and primary_reset > 0
        and not limit_reached
    ):
        return {
            "window": "uninitialized_seat",
            "resets_at": int(time.time() + 86400),
            "quota_info": quota_info,
            "limit_reached": False,
            "needs_codex_smoke": True,
            "no_quota_signals": ["workspace_uninitialized"],
        }

    primary_exhausted = primary_pct >= 100
    weekly_exhausted = weekly_pct >= 100
    if not (limit_reached or primary_exhausted or weekly_exhausted):
        return None

    reset_candidates = []
    if primary_exhausted and primary_reset:
        reset_candidates.append(primary_reset)
    if weekly_exhausted and weekly_reset:
        reset_candidates.append(weekly_reset)

    if not reset_candidates:
        if primary_reset:
            reset_candidates.append(primary_reset)
        if weekly_reset:
            reset_candidates.append(weekly_reset)

    resets_at = max(reset_candidates) if reset_candidates else int(time.time() + 18000)

    if primary_exhausted and weekly_exhausted:
        window = "combined"
    elif weekly_exhausted:
        window = "weekly"
    elif primary_exhausted:
        window = "primary"
    else:
        window = "limit"

    return {
        "window": window,
        "resets_at": resets_at,
        "quota_info": quota_info,
        "limit_reached": bool(limit_reached),
    }


# Round 7 FR-D6 — manager 24h 去重 cheap_codex_smoke。
# 同一 account_id 在 24h 内重复调用 cheap_codex_smoke 时,直接返回上次落盘的结果,
# 不再走网络。R2 风险缓解(fresh seat 命中率 > 5% 时 smoke 调用密度爆炸)。
_CODEX_SMOKE_DEDUP_SECONDS = 86400


def _read_codex_smoke_cache(account_id):
    """从 accounts.json 读 last_codex_smoke_at + last_smoke_result。



    匹配规则:account_id 优先按 workspace_account_id 比对,其次 email 比对。

    返回 (epoch_at, result_str) 或 None(无 account_id / 无记录 / 字段缺失)。

    """
    if not account_id:
        return None
    try:
        from autoteam.accounts import load_accounts

        accounts = load_accounts()
    except Exception:
        return None
    target = str(account_id)
    for acc in accounts:
        if acc.get("workspace_account_id") == target or acc.get("email") == target:
            ts = acc.get("last_codex_smoke_at")
            res = acc.get("last_smoke_result")
            if ts and res:
                try:
                    return (float(ts), str(res))
                except (TypeError, ValueError):
                    return None
    return None


def _write_codex_smoke_cache(account_id, result):
    """落盘 last_codex_smoke_at + last_smoke_result,用于 24h 去重。



    匹配规则同 _read_codex_smoke_cache。result 取值 alive/auth_invalid/uncertain。

    任何异常静默吞,cache 失败不阻塞主流程。

    """
    if not account_id or not result:
        return
    try:
        from autoteam.accounts import load_accounts, update_account
    except Exception:
        return
    target = str(account_id)
    try:
        accounts = load_accounts()
    except Exception:
        return
    for acc in accounts:
        if acc.get("workspace_account_id") == target or acc.get("email") == target:
            try:
                update_account(
                    acc["email"],
                    last_codex_smoke_at=time.time(),
                    last_smoke_result=str(result),
                )
            except Exception as exc:
                logger.debug("[Codex smoke] cache 落盘失败(忽略): %s", exc)
            return


def cheap_codex_smoke(

    access_token,

    account_id=None,

    *,

    model="gpt-5.5",

    fallback_models=None,

    instructions=None,

    max_output_tokens=64,

    timeout=15.0,

    force=False,

):
    """SPEC-2 shared/quota-classification §4.4 — uninitialized_seat 二次验证。

    Round 11 升级(v2 — codex backend payload schema 改版):

      - 默认 model 升 gpt-5.5(team-only,主路径)

      - fallback_models 默认 ["gpt-5.4"](通用模型,team + free 都能用),主 model 撞 model_not_supported 时自动 fallback

      - payload 严格按最新 codex backend schema:

          * instructions 必填(默认 "You are a concise assistant. Reply with one short word.")

          * input 改为 list 格式 [{type:message, role:user, content:[{type:input_text, text:"ping"}]}]

          * 新增 store: false(必填)

          * 删除 max_output_tokens(后端不再支持,函数签名保留供未来扩展)



    PRD-Round11 Q4:gpt-5.5 是 team-only 模型(只 team 号能用),gpt-5.4 是通用模型

    (free + team 都能用)。所以 team 号默认走 gpt-5.5,model_not_supported 时

    fallback 到 gpt-5.4(free 号在主 model 立即 fallback)。



    Round 7 FR-D6:24h 去重 cache。account_id 在 24h 内已有 cache 时直接返回,不走网络;

    传 force=True 可绕过 cache(用于强制刷新场景)。



    返回 (result, detail):

        ("alive", {model, response_text, raw_event, tokens?})  — HTTP 200 + response.completed → 真活号

                                                                detail.model 反映最终成功用的 model(可能是 fallback)

        ("alive", None) (cache hit only)                       — 24h cache 命中

        ("auth_invalid", reason_str)  — HTTP 401/403/429 / 4xx 含 quota 关键词 → token/seat 真失效

        ("uncertain", reason_str)     — HTTP 5xx / network / timeout / 解析异常 / 主+fallback 全 model_not_supported

        cache 命中时 detail 为 "cache_hit_<原 result>"



    向后兼容:max_output_tokens 参数保留但**不再传给后端**(spike 实测后端拒收);

    instructions=None 时用模块级默认。

    """
    if not access_token:
        return "auth_invalid", "empty_access_token"

    if not account_id:
        try:
            account_id = get_chatgpt_account_id()
        except Exception:
            account_id = None

    # Round 7 FR-D6 — 24h 去重 cache 命中直接返回(force=True 时绕过)
    if not force and account_id:
        cached = _read_codex_smoke_cache(account_id)
        if cached:
            cached_at, cached_result = cached
            if (time.time() - cached_at) < _CODEX_SMOKE_DEDUP_SECONDS:
                logger.debug(
                    "[Codex smoke] 24h cache 命中 account_id=%s result=%s age=%.0fs",
                    account_id,
                    cached_result,
                    time.time() - cached_at,
                )
                return cached_result, f"cache_hit_{cached_result}"

    # 构造 model 尝试链:主 model + fallback chain(去重保序)
    if fallback_models is None:
        fallback_models = ["gpt-5.4"]
    elif not isinstance(fallback_models, (list, tuple)):
        fallback_models = [fallback_models]
    model_chain: list[str] = []
    seen: set[str] = set()
    for m in [model] + list(fallback_models):
        if not m:
            continue
        if m in seen:
            continue
        seen.add(m)
        model_chain.append(m)

    last_result: tuple[str, object] = ("uncertain", "no_models_attempted")
    for idx, candidate in enumerate(model_chain):
        result, detail = _cheap_codex_smoke_network(
            access_token,
            account_id,
            model=candidate,
            instructions=instructions,
            max_output_tokens=max_output_tokens,
            timeout=timeout,
        )
        # alive / auth_invalid → 直接返回(终态)
        if result == "alive" or result == "auth_invalid":
            _write_codex_smoke_cache(account_id, result)
            return result, detail
        # uncertain 路径下:仅当 detail 是 model_not_supported 才继续尝试 fallback
        last_result = (result, detail)
        is_model_not_supported = (
            isinstance(detail, str) and detail.startswith("model_not_supported")
        )
        if not is_model_not_supported:
            break  # 非 model 问题(网络 / 5xx / 解析)— 不重试
        # else:模型不被支持,继续下一个 fallback
        if idx + 1 < len(model_chain):
            logger.info(
                "[Codex smoke] model=%s 不被支持,fallback → %s",
                candidate,
                model_chain[idx + 1],
            )

    result, detail = last_result
    _write_codex_smoke_cache(account_id, result)
    return result, detail


def _cheap_codex_smoke_network(

    access_token,

    account_id,

    *,

    model="gpt-5.5",

    instructions=None,

    max_output_tokens=64,  # 保留参数供未来扩展;后端拒收此字段,不再写入 payload

    timeout=15.0,

):
    """实际走网络的 cheap_codex_smoke 内部函数(Round 7 FR-D6 拆出 + Round 11 v2 schema 升级)。



    与 cheap_codex_smoke 不同点:不查也不写 cache,直接调 codex backend。

    Round 11:读完整 SSE 帧累积 output_text,见 response.completed 时返回 dict 含 response_text。

    Round 11 v2:payload schema 严格按 codex backend 最新版(spike 实测验证):

      - instructions 必填(默认 _CODEX_SMOKE_DEFAULT_INSTRUCTIONS)

      - input 是 list 格式 [{type:message, role:user, content:[{type:input_text, text}]}]

      - 必含 store: false

      - **不再**写 max_output_tokens(后端拒收"Unsupported parameter")



    返回值语义:

      alive 路径 detail = dict {"model", "response_text", "raw_event", "tokens"}

      auth_invalid 路径 detail = str(http_<code>[_quota_hint])

      uncertain 路径 detail = str

        - "model_not_supported_<code>":4xx 且 body 含 not supported 关键字 → 上层触发 fallback

        - "http_<code>" / "no_response_created_frame" / "stream:..." / "network:..." 等

    """
    import requests

    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json",
        "Accept": "text/event-stream",
    }
    if account_id:
        headers["Chatgpt-Account-Id"] = account_id

    payload = {
        "model": model,
        "instructions": instructions if instructions else _CODEX_SMOKE_DEFAULT_INSTRUCTIONS,
        "input": [
            {
                "type": "message",
                "role": "user",
                "content": [{"type": "input_text", "text": "ping"}],
            }
        ],
        "stream": True,
        "store": False,
        "reasoning": {"effort": "none"},
    }

    try:
        resp = requests.post(
            _CODEX_SMOKE_ENDPOINT,
            headers=headers,
            json=payload,
            stream=True,
            timeout=timeout,
        )
    except (
        requests.exceptions.ConnectionError,
        requests.exceptions.Timeout,
        requests.exceptions.SSLError,
    ) as exc:
        logger.warning("[Codex smoke] 网络异常(uncertain): %s", exc)
        return "uncertain", f"network:{type(exc).__name__}"
    except Exception as exc:
        logger.warning("[Codex smoke] 未知异常(uncertain): %s", exc)
        return "uncertain", f"exception:{type(exc).__name__}"

    try:
        status_code = resp.status_code
        if status_code in (401, 403, 429):
            try:
                resp.close()
            except Exception:
                pass
            return "auth_invalid", f"http_{status_code}"

        if 500 <= status_code < 600:
            try:
                resp.close()
            except Exception:
                pass
            return "uncertain", f"http_{status_code}"

        if status_code != 200:
            # 4xx 非 401/403/429:
            #   1) body 含 model_not_supported 关键词 → uncertain + sentinel,触发上层 fallback
            #   2) body 含 quota 关键词 → auth_invalid
            #   3) 其他 → uncertain + http_<code>
            try:
                body_preview = (resp.text or "").lower()[:1500]
            except Exception:
                body_preview = ""
            try:
                resp.close()
            except Exception:
                pass
            if any(hint in body_preview for hint in _CODEX_SMOKE_MODEL_NOT_SUPPORTED_HINTS):
                return "uncertain", f"model_not_supported_{status_code}"
            if any(hint in body_preview for hint in _CODEX_SMOKE_QUOTA_HINTS):
                return "auth_invalid", f"http_{status_code}_quota_hint"
            return "uncertain", f"http_{status_code}"

        # HTTP 200 — 读完整 SSE 帧累积 output_text(Round 11)
        response_text_parts = []
        seen_created = False
        completed_event = False
        output_tokens = None
        frames_read = 0
        try:
            for line in resp.iter_lines(decode_unicode=True):
                if not line:
                    continue
                frames_read += 1
                # SSE 行通常为 "data: {...}" 或 "event: response.created" 等
                # 先剥离 "data: " 前缀,失败按原文匹配
                payload_text = line[6:].strip() if line.startswith("data: ") else line.strip()
                if "response.created" in line:
                    seen_created = True
                if "response.completed" in line:
                    # 见 completed 帧结束读流
                    completed_event = True
                    # 尝试解析 token 数(usage.output_tokens)
                    try:
                        ev_obj = json.loads(payload_text)
                        usage = (ev_obj.get("response") or {}).get("usage") or {}
                        output_tokens = usage.get("output_tokens")
                    except Exception:
                        pass
                    break
                # 累积 output_text.delta 帧文本
                if "response.output_text.delta" in line or "output_text.delta" in line:
                    try:
                        ev_obj = json.loads(payload_text)
                        delta_str = ev_obj.get("delta")
                        if isinstance(delta_str, str) and delta_str:
                            response_text_parts.append(delta_str)
                    except Exception:
                        pass
                # 安全兜底:30 帧仍未见 completed,跳出当作 alive(已 reach model)
                if frames_read >= 30:
                    break
        except Exception as exc:
            logger.debug("[Codex smoke] iter_lines 异常(uncertain): %s", exc)
            return "uncertain", f"stream:{type(exc).__name__}"
        finally:
            try:
                resp.close()
            except Exception:
                pass

        # 8 行内仍没拿到 response.created 视为 uncertain
        if not seen_created and frames_read < 8:
            return "uncertain", "no_response_created_frame"
        if not seen_created:
            return "uncertain", "no_response_created_frame"

        response_text = "".join(response_text_parts)
        raw_event = "response.completed" if completed_event else "no_completed_within_30_frames"
        detail = {
            "model": model,
            "response_text": response_text,
            "raw_event": raw_event,
        }
        if output_tokens is not None:
            detail["tokens"] = output_tokens
        return "alive", detail
    finally:
        try:
            resp.close()
        except Exception:
            pass


def check_codex_quota(access_token, account_id=None):
    """

    通过 /backend-api/wham/usage 查询 Codex 额度状态,不消耗额度。

    返回:

        ("ok", quota_info)         — HTTP 200 + 成功解析,额度未触发上限

        ("exhausted", info)        — HTTP 200 + quota 用尽(get_quota_exhausted_info 命中)

        ("auth_error", None)       — **仅** HTTP 401/403,token/seat 真失效

        ("network_error", None)    — DNS/timeout/SSL/连接异常 / 5xx / 429 / json 解析失败 / 其他临时错误



    auth_error 与 network_error 必须严格区分:auth_error 会触发"标记 AUTH_INVALID/重登"等

    破坏性流程,网络抖动绝不能落入该分支(否则一次网络故障可能批量误删账号)。

    quota_info = {"primary_pct": int, "primary_resets_at": int, "weekly_pct": int, "weekly_resets_at": int}

    """
    import requests

    if not account_id:
        account_id = get_chatgpt_account_id()

    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json",
    }
    if account_id:
        headers["Chatgpt-Account-Id"] = account_id

    try:
        resp = requests.get(
            "https://chatgpt.com/backend-api/wham/usage",
            headers=headers,
            timeout=30,
        )
    except (
        requests.exceptions.ConnectionError,
        requests.exceptions.Timeout,
        requests.exceptions.SSLError,
    ) as e:
        logger.warning("[Codex] 网络异常(归类 network_error): %s", e)
        return "network_error", None
    except requests.exceptions.RequestException as e:
        # 其他 requests 异常(ChunkedEncodingError 等)同样归为 network_error,而不是 auth_error
        logger.warning("[Codex] requests 异常(归类 network_error): %s", e)
        return "network_error", None
    except Exception as e:
        # 兜底:未知异常宁可归 network_error,避免因为一次网络抖动批量误标 AUTH_INVALID
        logger.warning("[Codex] 未知异常(归类 network_error,保守处理): %s", e)
        return "network_error", None

    if resp.status_code in (401, 403):
        return "auth_error", None

    # 429 限流 / 5xx 服务端错误 → 临时性故障,归为 network_error,不动账号 status
    if resp.status_code == 429 or 500 <= resp.status_code < 600:
        logger.warning("[Codex] wham/usage 临时错误 %d(归类 network_error): %s", resp.status_code, resp.text[:200])
        return "network_error", None

    if resp.status_code != 200:
        # 4xx(非 401/403/429) 也归为 network_error:可能是 OpenAI 接口在调整,
        # 不能因为一次接口变更把全部账号误判 token 失效
        logger.warning("[Codex] wham/usage 非预期状态 %d(归类 network_error): %s", resp.status_code, resp.text[:200])
        return "network_error", None

    try:
        data = resp.json()
    except Exception as e:
        logger.warning("[Codex] wham/usage 响应 JSON 解析失败(归类 network_error): %s", e)
        return "network_error", None

    rate_limit = data.get("rate_limit") or {}
    primary = rate_limit.get("primary_window") or {}
    secondary = rate_limit.get("secondary_window") or {}

    # SPEC-2 shared/quota-classification §2.2 — 扩 primary_total / primary_remaining,
    # 让 get_quota_exhausted_info 能区分 no_quota(workspace 未分配)与 exhausted(已用完)
    primary_total_raw = primary.get("limit", primary.get("total"))
    primary_remaining_raw = primary.get("remaining")
    quota_info = {
        "primary_pct": primary.get("used_percent", 0),
        "primary_resets_at": primary.get("reset_at", 0),
        "primary_total": primary_total_raw if isinstance(primary_total_raw, (int, float)) else None,
        "primary_remaining": primary_remaining_raw if isinstance(primary_remaining_raw, (int, float)) else None,
        "weekly_pct": secondary.get("used_percent", 0),
        "weekly_resets_at": secondary.get("reset_at", 0),
        # Round 7 P2.5:把 wham/usage 的原始 rate_limit + primary_window 子树注入 quota_info,
        # 让 manager 在 record_failure(no_quota_assigned) 时能附 raw_rate_limit 用于事后排查。
        "raw_rate_limit": rate_limit,
        "primary_window": primary,
    }

    # SPEC-2 shared/quota-classification §4.2 — no_quota 单独分支:rate_limit 字段
    # 完全缺失 / primary_window 缺失也归 no_quota(空载也是无配额信号)
    rate_limit_missing = (not rate_limit) or (not primary)
    if rate_limit_missing:
        return "no_quota", {
            "window": "no_quota",
            "resets_at": int(time.time() + 86400),
            "quota_info": quota_info,
            "limit_reached": False,
            "no_quota_signals": ["rate_limit_or_primary_missing"],
            "raw_rate_limit": rate_limit,
        }

    exhausted_info = get_quota_exhausted_info(quota_info, limit_reached=bool(rate_limit.get("limit_reached")))
    if exhausted_info:
        # window="no_quota" 是 get_quota_exhausted_info 内部短路出的形态;独立分类返回
        if exhausted_info.get("window") == "no_quota":
            return "no_quota", exhausted_info
        # window="uninitialized_seat"(I5)— Round 6 PRD-5 FR-P0:必须用 cheap_codex_smoke 二次验证
        if exhausted_info.get("window") == "uninitialized_seat":
            smoke_result, smoke_detail = cheap_codex_smoke(access_token, account_id=account_id)
            exhausted_info["last_smoke_result"] = smoke_result
            if smoke_detail is not None:
                exhausted_info["last_smoke_detail"] = smoke_detail
            if smoke_result == "alive":
                # fresh seat 真活,token 有效 → 维持 ok,但带 smoke_verified 标记
                quota_info_verified = dict(quota_info)
                quota_info_verified["smoke_verified"] = True
                quota_info_verified["last_smoke_result"] = "alive"
                return "ok", quota_info_verified
            if smoke_result == "auth_invalid":
                # codex backend 401/403/quota → 当作真 auth_error,触发 reconcile 重登
                return "auth_error", None
            # uncertain (5xx/network/timeout) → 保持原状态等下轮,归 network_error
            return "network_error", None
        return "exhausted", exhausted_info

    return "ok", quota_info


def refresh_access_token(refresh_token):
    """刷新 access token"""
    import requests

    resp = requests.post(
        CODEX_TOKEN_URL,
        data={
            "grant_type": "refresh_token",
            "client_id": CODEX_CLIENT_ID,
            "refresh_token": refresh_token,
            "scope": "openid profile email",
        },
        headers={"Content-Type": "application/x-www-form-urlencoded"},
    )

    if resp.status_code != 200:
        logger.error("[Codex] Token 刷新失败: %d", resp.status_code)
        return None

    data = resp.json()
    return {
        "access_token": data.get("access_token"),
        "refresh_token": data.get("refresh_token", refresh_token),
        "id_token": data.get("id_token", ""),
        "expires_in": data.get("expires_in", 3600),
    }