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

import json
import logging
import os
import signal
import subprocess
import sys
import threading
import time
import uuid
from contextlib import asynccontextmanager
from enum import Enum
from pathlib import Path
from typing import Literal

from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field, field_validator

from autoteam.config import API_KEY
from autoteam.runtime_resources import collect_runtime_resource_snapshot, log_runtime_resource_snapshot
from autoteam.textio import read_text

logger = logging.getLogger(__name__)


def _safe_runtime_resource_snapshot() -> dict:
    try:
        return collect_runtime_resource_snapshot()
    except Exception as exc:
        logger.warning("[资源] runtime resource snapshot failed: %s", exc)
        return {"error": "runtime_resource_snapshot_failed"}


def _safe_ipv6_pool_status() -> dict:
    try:
        from autoteam.ipv6_pool import ipv6_pool

        return ipv6_pool.status()
    except Exception as exc:
        logger.warning("[IPv6Pool] status unavailable: %s", exc)
        return {
            "enabled": False,
            "required": False,
            "ok": False,
            "count": 0,
            "unhealthy_count": 0,
            "expired_count": 0,
            "last_error": str(exc),
            "preflight": {"ok": False, "errors": ["status_unavailable"], "warnings": []},
            "entries": [],
        }


def _safe_cliproxy_health(**kwargs) -> dict:
    try:
        from autoteam.cliproxy_health import get_cliproxy_health

        return get_cliproxy_health(**kwargs)
    except Exception as exc:
        logger.warning("[API] CLIProxyAPI health check failed: %s", exc)
        return {
            "ok": False,
            "safe_read_only": True,
            "management_api": {"ok": False, "reason": "health_check_exception"},
            "provider_auth": {
                "ok": False,
                "provider": "codex",
                "model": os.environ.get("CLIPROXY_HEALTH_MODEL") or "gpt-5.5",
                "reason": "health_check_exception",
                "total": 0,
                "available": 0,
                "check_type": "management_metadata",
                "canary_required": True,
            },
            "error": str(exc),
        }


def _safe_multi_master_status() -> dict:
    try:
        from autoteam.multi_master import build_multi_master_status

        return build_multi_master_status()
    except Exception as exc:
        logger.warning("[API] multi-master status failed: %s", exc)
        return {"enabled": False, "owner_count": 0, "owners": [], "error": str(exc)}


def _require_sync_target_configs(action_label: str):
    """Require at least one fully configured enabled remote sync target."""
    from autoteam.sync_targets import get_enabled_sync_targets, get_missing_target_configs

    enabled_targets = get_enabled_sync_targets()
    if not enabled_targets:
        raise HTTPException(
            status_code=400, detail=f"{action_label} 前请先在配置面板启用至少一个远端同步目标(CPA 或 Sub2API)"
        )

    missing = get_missing_target_configs(enabled_targets)
    if missing:
        fields = "、".join(f"{key}{label})" for key, label in missing)
        raise HTTPException(status_code=400, detail=f"{action_label} 前请先在配置面板填写:{fields}")


# Round 7 P2.4 — FastAPI 现代 lifespan handler 替代已废弃的 @app.on_event。
# 启动期:修复 auths 认证文件权限 + 启动 _auto_check_loop 后台线程。
# 停止期:发 _auto_check_stop 信号让线程优雅退出。
# 引用的 _auto_check_loop / _auto_check_stop / ensure_auth_file_permissions 都在
# module 后段定义,Python 闭包延迟绑定符合预期(yield 时才会解析名字)。
@asynccontextmanager
async def app_lifespan(app: FastAPI):
    logger.info("[lifespan] starting")
    try:
        from autoteam.auth_storage import ensure_auth_file_permissions

        fixed = ensure_auth_file_permissions()
        if fixed:
            logger.info("[启动] 已修复 %d 个 auths 认证文件权限", fixed)
    except Exception as exc:
        logger.warning("[启动] 修复 auths 认证文件权限失败: %s", exc)

    # Round 9 RT-1 — 启动时跑 1 次 retroactive 重分类,解 "重启后 stale active" 根因。
    # spec/shared/master-subscription-health.md v1.1 §11.3。
    # 默认 ON,设 STARTUP_RETROACTIVE_DISABLE=1 关闭。后台线程跑,失败仅 warning。
    if os.getenv("STARTUP_RETROACTIVE_DISABLE", "").strip().lower() not in ("1", "true", "yes"):
        def _startup_retroactive():
            try:
                from autoteam.master_health import _apply_master_degraded_classification

                retro = _apply_master_degraded_classification()
                if retro and (retro.get("marked_grace") or retro.get("marked_standby") or retro.get("reverted_active")):
                    logger.info(
                        "[启动-retroactive] GRACE %d / STANDBY %d / 撤回 ACTIVE %d",
                        len(retro.get("marked_grace") or []),
                        len(retro.get("marked_standby") or []),
                        len(retro.get("reverted_active") or []),
                    )
                elif retro and retro.get("skipped_reason"):
                    logger.info("[启动-retroactive] skipped: %s", retro["skipped_reason"])
            except Exception as exc:
                logger.warning("[启动-retroactive] 异常(不阻塞启动): %s", exc)

        try:
            threading.Thread(target=_startup_retroactive, daemon=True).start()
        except Exception as exc:
            logger.warning("[启动-retroactive] 后台线程启动失败: %s", exc)

    try:
        from autoteam.accounts import STATUS_ACTIVE, is_account_disabled, load_accounts
        from autoteam.ipv6_pool import ipv6_pool

        active_emails = [
            acc["email"]
            for acc in load_accounts()
            if acc.get("status") == STATUS_ACTIVE
            and acc.get("email")
            and not _is_main_account_email(acc.get("email"))
            and not is_account_disabled(acc)
        ]
        ipv6_pool.start(active_emails=active_emails)
    except Exception as exc:
        logger.warning("[启动] IPv6 代理池启动失败: %s", exc)

    thread = threading.Thread(target=_auto_check_loop, daemon=True)
    thread.start()
    try:
        yield
    finally:
        logger.info("[lifespan] stopping")
        _auto_check_stop.set()
        try:
            _pw_executor.stop()
        except Exception as exc:
            logger.warning("[lifespan] stopping Playwright executor failed: %s", exc)
        try:
            from autoteam.ipv6_pool import ipv6_pool

            ipv6_pool.stop_all()
        except Exception as exc:
            logger.warning("[lifespan] stopping IPv6 proxy pool failed: %s", exc)


app = FastAPI(
    title="AutoTeam API",
    description="ChatGPT Team 账号自动轮转管理 API",
    version="0.1.0",
    lifespan=app_lifespan,
)


# ---------------------------------------------------------------------------
# 版本端点 (SPEC-3 §5) - 不鉴权,纯只读
# ---------------------------------------------------------------------------


class VersionResponse(BaseModel):
    """镜像版本指纹响应 - 来自 Dockerfile build args。"""

    git_sha: str
    build_time: str


@app.get(

    "/api/version",

    response_model=VersionResponse,

    summary="返回镜像构建期注入的 git-sha 与时间戳",

    tags=["meta"],

)
def api_version() -> VersionResponse:
    return VersionResponse(
        git_sha=os.getenv("AUTOTEAM_GIT_SHA", "unknown"),
        build_time=os.getenv("AUTOTEAM_BUILD_TIME", "unknown"),
    )


# ---------------------------------------------------------------------------
# API Key 鉴权中间件
# ---------------------------------------------------------------------------

_AUTH_SKIP_PATHS = {
    "/api/auth/check",
    "/api/setup/status",
    "/api/setup/save",
    "/api/version",
    "/api/setup/dns/check",  # Read-only setup diagnostic; route enforces Bearer once API_KEY exists.
    "/api/mail-provider/probe",  # SPEC-1 §3.5 — 条件鉴权:API_KEY 配置后路由内强制 Bearer
}


@app.middleware("http")
async def auth_middleware(request: Request, call_next):
    path = request.url.path
    # 不鉴权的路径:非 /api 路径、auth/check 端点
    if not path.startswith("/api/") or path in _AUTH_SKIP_PATHS:
        return await call_next(request)
    # 未配置 API_KEY 则跳过鉴权
    if not API_KEY:
        return await call_next(request)
    # 从 header 或 query param 获取 key
    auth_header = request.headers.get("authorization", "")
    if auth_header.startswith("Bearer "):
        token = auth_header[7:]
    else:
        token = request.query_params.get("key", "")
    if token != API_KEY:
        return JSONResponse(status_code=401, content={"detail": "未授权,请提供有效的 API Key"})
    return await call_next(request)


@app.get("/api/auth/check")
def check_auth(request: Request):
    """验证 API Key 是否有效。未配置 API_KEY 时始终返回成功。"""
    if not API_KEY:
        return {"authenticated": True, "auth_required": False}
    auth_header = request.headers.get("authorization", "")
    if auth_header.startswith("Bearer ") and auth_header[7:] == API_KEY:
        return {"authenticated": True, "auth_required": True}
    return JSONResponse(status_code=401, content={"authenticated": False, "auth_required": True})


# ---------------------------------------------------------------------------
# 初始配置 API(无需鉴权)
# ---------------------------------------------------------------------------


class SetupConfig(BaseModel):
    """`/api/setup/save` 请求体。SPEC-1 §2.1 — 含 cf_temp_email + maillab 两套 mail 字段。"""

    MAIL_PROVIDER: Literal["cf_temp_email", "cloudflare_temp_email", "maillab"] = "cf_temp_email"
    CLOUDMAIL_BASE_URL: str = ""
    CLOUDMAIL_EMAIL: str = ""
    CLOUDMAIL_PASSWORD: str = ""
    CLOUDMAIL_DOMAIN: str = ""
    MAILLAB_API_URL: str = ""
    MAILLAB_USERNAME: str = ""
    MAILLAB_PASSWORD: str = ""
    MAILLAB_DOMAIN: str = ""
    CPA_URL: str = "http://127.0.0.1:8317"
    CPA_KEY: str = ""
    ROTATE_NEW_ACCOUNT_MODE: str = "domain_auto_join_first"
    AUTOTEAM_AUTO_JOIN_DOMAINS: str = "auto"
    ROTATE_DOMAIN_AUTO_JOIN_FALLBACK_INVITE: str | bool = "true"
    PLAYWRIGHT_PROXY_URL: str = ""
    PLAYWRIGHT_PROXY_BYPASS: str = ""
    API_KEY: str = ""


class ProbeErrorCode(str, Enum):
    ROUTE_NOT_FOUND = "ROUTE_NOT_FOUND"
    PROVIDER_MISMATCH = "PROVIDER_MISMATCH"
    UNAUTHORIZED = "UNAUTHORIZED"
    FORBIDDEN_DOMAIN = "FORBIDDEN_DOMAIN"
    CAPTCHA_REQUIRED = "CAPTCHA_REQUIRED"
    NETWORK = "NETWORK"
    TIMEOUT = "TIMEOUT"
    EMPTY_DOMAIN_LIST = "EMPTY_DOMAIN_LIST"
    UNKNOWN = "UNKNOWN"


class MailProviderProbeRequest(BaseModel):
    """`/api/mail-provider/probe` 请求体。SPEC-1 §2.1。"""

    provider: Literal["cf_temp_email", "maillab"]
    step: Literal["fingerprint", "credentials", "domain_ownership"]
    base_url: str = Field(..., min_length=1, max_length=512)
    admin_password: str = ""
    username: str = ""
    password: str = ""
    domain: str = ""
    bearer_token: str = ""

    @field_validator("base_url")
    @classmethod
    def _normalize_base_url(cls, v: str) -> str:
        v = v.strip().rstrip("/")
        if not (v.startswith("http://") or v.startswith("https://")):
            raise ValueError("base_url 必须以 http:// 或 https:// 开头")
        return v

    @field_validator("domain")
    @classmethod
    def _normalize_domain(cls, v: str) -> str:
        return (v or "").strip().lstrip("@")


class MailProviderProbeResponse(BaseModel):
    ok: bool
    step: Literal["fingerprint", "credentials", "domain_ownership"]
    error_code: ProbeErrorCode | None = None
    message: str | None = None
    hint: str | None = None
    warnings: list[str] = []
    detected_provider: Literal["cf_temp_email", "maillab", "unknown"] | None = None
    domain_list: list[str] | None = None
    add_verify_open: bool | None = None
    register_verify_open: bool | None = None
    is_admin: bool | None = None
    user_email: str | None = None
    token_preview: str | None = None
    probe_email: str | None = None
    probe_account_id: int | None = None
    cleaned: bool | None = None
    leaked_probe: dict | None = None


class DNSRecordCheck(BaseModel):
    type: Literal["A", "AAAA", "CNAME", "MX", "TXT"]
    name: str = Field(..., min_length=1, max_length=253)
    expected: str = Field(..., min_length=1, max_length=2048)


class DNSDiagnosticRequest(BaseModel):
    domain: str = ""
    mail_host: str = ""
    mail_ip: str = ""
    mx_target: str = ""
    spf_value: str = ""
    openai_domain_verification: str = ""
    records: list[DNSRecordCheck] = Field(default_factory=list)

    @field_validator("domain", "mail_host", "mx_target")
    @classmethod
    def _normalize_dns_name(cls, v: str) -> str:
        return (v or "").strip().lstrip("@").rstrip(".").lower()


class DNSCheckResponse(BaseModel):
    type: Literal["A", "AAAA", "CNAME", "MX", "TXT"]
    name: str
    expected: str
    observed: list[str] = Field(default_factory=list)
    ok: bool
    error: str | None = None


class DNSDiagnosticResponse(BaseModel):
    ok: bool
    domain: str = ""
    checks: list[DNSCheckResponse] = Field(default_factory=list)
    all_ok: bool = False
    safe_read_only: bool = True
    error_code: str | None = None
    message: str | None = None


_CF_MAIL_KEYS = {"CLOUDMAIL_BASE_URL", "CLOUDMAIL_PASSWORD", "CLOUDMAIL_DOMAIN"}
_MAILLAB_KEYS = {"MAILLAB_API_URL", "MAILLAB_USERNAME", "MAILLAB_PASSWORD", "MAILLAB_DOMAIN"}
_ROTATE_NEW_ACCOUNT_MODES = {"domain_auto_join_first", "invite_first", "direct_first", "oauth_first"}


def _normalize_setup_bool(value, *, field: str) -> str:
    if isinstance(value, bool):
        return "true" if value else "false"
    text = str(value or "").strip().lower()
    if text in {"1", "true", "yes", "on", "enabled", "y", "t"}:
        return "true"
    if text in {"0", "false", "no", "off", "disabled", "n", "f"}:
        return "false"
    raise ValueError(f"{field} 必须是 true 或 false")


def _normalize_rotation_setup_values(data: dict) -> None:
    mode = str(data.get("ROTATE_NEW_ACCOUNT_MODE", "") or "").strip().lower()
    if mode:
        if mode not in _ROTATE_NEW_ACCOUNT_MODES:
            raise ValueError(
                "ROTATE_NEW_ACCOUNT_MODE 必须是 domain_auto_join_first、invite_first、direct_first 或 oauth_first"
            )
        data["ROTATE_NEW_ACCOUNT_MODE"] = mode

    domains = str(data.get("AUTOTEAM_AUTO_JOIN_DOMAINS", "") or "").strip()
    if domains:
        data["AUTOTEAM_AUTO_JOIN_DOMAINS"] = ",".join(
            part.strip().lower().lstrip("@").rstrip(".")
            for part in domains.split(",")
            if part.strip()
        ) or "auto"
    else:
        data["AUTOTEAM_AUTO_JOIN_DOMAINS"] = ""

    data["ROTATE_DOMAIN_AUTO_JOIN_FALLBACK_INVITE"] = _normalize_setup_bool(
        data.get("ROTATE_DOMAIN_AUTO_JOIN_FALLBACK_INVITE", "true"),
        field="ROTATE_DOMAIN_AUTO_JOIN_FALLBACK_INVITE",
    )


@app.get("/api/setup/status")
def get_setup_status():
    """检查配置是否完整 — SPEC-1 §3.6:按 provider 动态标 optional。"""
    from autoteam.setup_wizard import REQUIRED_CONFIGS, _read_env

    env = _read_env()
    provider = (env.get("MAIL_PROVIDER") or os.environ.get("MAIL_PROVIDER") or "cf_temp_email").strip().lower()
    fields = []
    all_ok = True
    for key, prompt, default, optional in REQUIRED_CONFIGS:
        # 不属于当前 provider 的 mail 字段强制 optional
        if provider == "maillab" and key in _CF_MAIL_KEYS:
            optional = True
        elif provider in ("cf_temp_email", "cloudflare_temp_email") and key in _MAILLAB_KEYS:
            optional = True
        val = env.get(key, "") or os.environ.get(key, "")
        ok = bool(val)
        if not ok and not optional:
            all_ok = False
        fields.append({"key": key, "prompt": prompt, "default": default, "optional": optional, "configured": ok})
    return {"configured": all_ok, "fields": fields, "provider": provider}


@app.post("/api/setup/save")
def post_setup_save(config: SetupConfig):
    """保存配置到 .env 并验证连通性"""
    import secrets as _secrets

    from autoteam.setup_wizard import REQUIRED_CONFIGS, _write_env

    data = config.model_dump()
    defaults = {key: default for key, _prompt, default, _optional in REQUIRED_CONFIGS}
    if not data.get("CPA_URL"):
        data["CPA_URL"] = defaults.get("CPA_URL", "http://127.0.0.1:8317")
    if not data.get("API_KEY"):
        data["API_KEY"] = _secrets.token_urlsafe(24)
    try:
        _normalize_rotation_setup_values(data)
    except ValueError as exc:
        return JSONResponse(status_code=400, content={"message": str(exc), "api_key": data["API_KEY"]})

    # SPEC-1 §3.6 — 按 provider 互斥写入,避免无关字段污染 .env
    provider = (data.get("MAIL_PROVIDER") or "cf_temp_email").strip().lower()
    if provider in ("cf_temp_email", "cloudflare_temp_email"):
        skip_keys = set(_MAILLAB_KEYS)
    elif provider == "maillab":
        # CLOUDMAIL_DOMAIN 保留作 maillab 的 fallback;仅跳过 base_url/password
        skip_keys = {"CLOUDMAIL_BASE_URL", "CLOUDMAIL_PASSWORD"}
    else:
        skip_keys = set()

    clearable_fields = {"AUTOTEAM_AUTO_JOIN_DOMAINS", "PLAYWRIGHT_PROXY_URL", "PLAYWRIGHT_PROXY_BYPASS"}
    for key, value in data.items():
        if key in skip_keys:
            continue
        if value or key in clearable_fields:
            _write_env(key, value)
            os.environ[key] = value

    # 重新加载模块
    import importlib

    import autoteam.config

    importlib.reload(autoteam.config)
    try:
        import autoteam.cloudmail

        importlib.reload(autoteam.cloudmail)
    except Exception:
        pass

    # 验证连通性
    errors = []
    from autoteam.setup_wizard import _verify_cloudmail, _verify_cpa

    if not _verify_cloudmail():
        errors.append("CloudMail 连接失败")
    if not _verify_cpa():
        errors.append("CPA 连接失败")

    if errors:
        return JSONResponse(status_code=400, content={"message": "、".join(errors), "api_key": data["API_KEY"]})

    # 更新运行时 API_KEY
    global API_KEY
    API_KEY = data["API_KEY"]

    return {"message": "配置保存成功", "api_key": data["API_KEY"], "configured": True}


# ---------------------------------------------------------------------------
# Mail Provider 在线探测 (SPEC-1 §3.5) — 三步分阶段验证
# ---------------------------------------------------------------------------

_probe_rate_buckets: dict[str, list[float]] = {}
_probe_rate_lock = threading.Lock()


def _enforce_probe_rate_limit(request: Request, max_per_min: int = 60):
    """SPEC §NFR-速率限制:setup 阶段 (无 API_KEY) 单 IP 60 req/min,防扫描。"""
    ip = request.client.host if request.client else "unknown"
    now = time.time()
    with _probe_rate_lock:
        bucket = _probe_rate_buckets.setdefault(ip, [])
        bucket[:] = [t for t in bucket if now - t < 60]
        if len(bucket) >= max_per_min:
            raise HTTPException(status_code=429, detail="probe 请求过频,请稍后再试")
        bucket.append(now)


def _enforce_setup_probe_access(request: Request):
    """Setup diagnostics are open before API_KEY exists and Bearer-gated after setup."""
    from autoteam.config import API_KEY as _key

    if _key:
        auth_header = request.headers.get("authorization", "")
        if not (auth_header.startswith("Bearer ") and auth_header[7:] == _key):
            raise HTTPException(status_code=401, detail="API_KEY 已配置,请提供 Bearer token")
    else:
        _enforce_probe_rate_limit(request)


@app.post("/api/mail-provider/probe", response_model=MailProviderProbeResponse)
def post_mail_provider_probe(req: MailProviderProbeRequest, request: Request):
    """SPEC-1 §3.5 — 三步分阶段:fingerprint → credentials → domain_ownership。



    鉴权策略:

      - API_KEY 未配置(setup 阶段):放行,但走 IP 速率限制

      - API_KEY 已配置:强制 Bearer(_AUTH_SKIP_PATHS 已加白,这里手工二次校验)

    """
    _enforce_setup_probe_access(request)

    from autoteam.mail import probe as mail_probe

    try:
        if req.step == "fingerprint":
            result = mail_probe.probe_fingerprint(req.base_url, req.provider)
        elif req.step == "credentials":
            result = mail_probe.probe_credentials(
                req.base_url,
                req.provider,
                username=req.username,
                password=req.password,
                admin_password=req.admin_password,
            )
        else:  # domain_ownership
            result = mail_probe.probe_domain_ownership(
                req.base_url,
                req.provider,
                bearer_token=req.bearer_token,
                admin_password=req.admin_password,
                domain=req.domain,
                username=req.username,
                password=req.password,
            )
    except mail_probe.ProbeError as exc:
        return MailProviderProbeResponse(
            ok=False,
            step=req.step,
            error_code=ProbeErrorCode(exc.error_code) if exc.error_code in ProbeErrorCode.__members__ else ProbeErrorCode.UNKNOWN,
            message=exc.message,
            hint=exc.hint,
        )
    except Exception as exc:  # noqa: BLE001 — 兜底
        logger.exception("[probe] %s 异常: %s", req.step, exc)
        return MailProviderProbeResponse(
            ok=False,
            step=req.step,
            error_code=ProbeErrorCode.UNKNOWN,
            message=str(exc),
        )

    # ProbeResult → ProbeResponse(忽略内部 bearer_token,前端不持有)
    payload = {k: v for k, v in vars(result).items() if k != "bearer_token"}
    if "error_code" in payload and payload["error_code"]:
        try:
            payload["error_code"] = ProbeErrorCode(payload["error_code"])
        except ValueError:
            payload["error_code"] = ProbeErrorCode.UNKNOWN
    return MailProviderProbeResponse(**payload)


@app.post("/api/setup/dns/check", response_model=DNSDiagnosticResponse)
def post_setup_dns_check(req: DNSDiagnosticRequest, request: Request):
    """Read-only DNS diagnostics for setup/domain verification records."""
    _enforce_setup_probe_access(request)

    from autoteam import dns_diagnostics

    admin_payload = req.model_dump(exclude={"records"})
    admin_payload["email_domain"] = admin_payload.pop("domain", "")
    extra_checks = [record.model_dump() for record in req.records]
    try:
        result = dns_diagnostics.check_admin_dns(admin_payload, extra_checks=extra_checks)
    except dns_diagnostics.DNSDiagnosticError as exc:
        return DNSDiagnosticResponse(ok=False, error_code="INVALID_REQUEST", message=str(exc))
    except Exception as exc:  # noqa: BLE001 - endpoint must not expose stack traces during setup
        logger.exception("[dns] setup DNS check failed: %s", exc)
        return DNSDiagnosticResponse(ok=False, error_code="UNKNOWN", message=str(exc))

    return DNSDiagnosticResponse(ok=bool(result.get("all_ok")), **result)


# ---------------------------------------------------------------------------
# 后台任务管理
# ---------------------------------------------------------------------------

_tasks: dict[str, dict] = {}
_playwright_lock = threading.Lock()
_current_task_id: str | None = None
_admin_login_api = None
_admin_login_step: str | None = None
_main_codex_flow = None
_main_codex_step: str | None = None
_main_codex_action: str | None = None
_manual_account_flow = None
MAX_TASK_HISTORY = 50
MAX_TASK_PROGRESS_HISTORY = 200


# ---------------------------------------------------------------------------
# Playwright 专用线程执行器(解决跨线程调用问题)
# ---------------------------------------------------------------------------

import queue as _queue

from autoteam._playwright_guard import assert_sync_context


class _PlaywrightExecutor:
    """将 Playwright 操作派发到专用线程执行,避免跨线程错误"""

    def __init__(self):
        self._queue: _queue.Queue = _queue.Queue()
        self._thread: threading.Thread | None = None

    def _worker(self):
        # SPEC-4 §3.2: worker 线程入口必须在普通线程,不允许 asyncio loop
        assert_sync_context()
        while True:
            item = self._queue.get()
            if item is None:
                break
            func, args, kwargs, result_event, result_holder = item
            try:
                result_holder["result"] = func(*args, **kwargs)
            except Exception as e:
                result_holder["error"] = e
            finally:
                result_event.set()

    def ensure_started(self):
        if self._thread is None or not self._thread.is_alive():
            self._thread = threading.Thread(target=self._worker, daemon=True)
            self._thread.start()

    def run(self, func, *args, **kwargs):
        """在专用线程中执行函数,阻塞等待结果(默认 5 分钟)"""
        return self.run_with_timeout(300, func, *args, **kwargs)

    def run_with_timeout(self, timeout: float, func, *args, **kwargs):
        """

        明确指定超时时间(秒)。适用于批量/长耗时操作。



        注意:超时后 worker 线程仍会继续跑完当前 func(Playwright 操作无法安全中断),

        后续通过 _pw_executor 提交的调用会在队列里等它自然完成。调用方需要自己

        确保不会越过 _playwright_lock 边界并发触发这种情况。

        """
        # SPEC-4 §3.2: 主线程前置检查,拦下 asyncio loop 误入
        assert_sync_context()
        self.ensure_started()
        result_event = threading.Event()
        result_holder: dict = {}
        self._queue.put((func, args, kwargs, result_event, result_holder))
        if not result_event.wait(timeout=timeout):
            raise TimeoutError(
                f"Playwright executor timed out after {timeout}s while running {getattr(func, '__name__', repr(func))}"
            )
        if "error" in result_holder:
            raise result_holder["error"]
        return result_holder.get("result")

    def stop(self):
        if self._thread and self._thread.is_alive():
            self._queue.put(None)
            self._thread.join(timeout=5)
            self._thread = None


_pw_executor = _PlaywrightExecutor()


def _current_busy_detail(default_message: str):
    if _admin_login_api:
        return {
            "message": default_message,
            "running_task": {
                "task_id": "admin-login",
                "command": "admin-login",
                "started_at": None,
            },
        }

    if _main_codex_flow:
        command = "main-codex-login" if _main_codex_action == "login" else "main-codex-sync"
        return {
            "message": default_message,
            "running_task": {
                "task_id": command,
                "command": command,
                "started_at": None,
            },
        }

    running = _tasks.get(_current_task_id, {})
    return {
        "message": default_message,
        "running_task": {
            "task_id": _current_task_id,
            "command": running.get("command", "unknown"),
            "started_at": running.get("started_at"),
        },
    }


def _prune_tasks():
    """保留最近 MAX_TASK_HISTORY 个任务"""
    if len(_tasks) <= MAX_TASK_HISTORY:
        return
    sorted_ids = sorted(_tasks, key=lambda k: _tasks[k]["created_at"])
    for tid in sorted_ids[: len(_tasks) - MAX_TASK_HISTORY]:
        if _tasks[tid]["status"] in ("completed", "failed"):
            del _tasks[tid]


_TASK_VALIDATION_COMMANDS = {"auto-fill", "auto-rotate", "rotate", "cleanup", "fill"}
_ROTATION_VALIDATION_COMMANDS = {"auto-fill", "auto-rotate", "rotate", "fill"}
_rotation_validation_cooldown = {
    "next_rotate_after": 0.0,
    "recorded_at": 0.0,
    "severity": "",
    "reason": "",
}


def _auto_check_int_env(name: str, default: int) -> int:
    try:
        return max(0, int(os.environ.get(name, str(default))))
    except (TypeError, ValueError):
        return default


def _validation_reasons(validation: dict | None) -> list[str]:
    if not isinstance(validation, dict):
        return []
    reasons = validation.get("reasons")
    if isinstance(reasons, list):
        return [str(item) for item in reasons if str(item)]
    reason = validation.get("reason")
    return [str(reason)] if reason else []


def _validation_failure_reason(validation: dict | None) -> str:
    reasons = _validation_reasons(validation)
    return "; ".join(reasons) if reasons else "unknown"


def _validation_is_hard_failure(validation: dict | None) -> bool:
    if not isinstance(validation, dict):
        return False
    if validation.get("severity") == "degraded":
        return False
    return not validation.get("ok", True)


def _record_rotation_validation_decision(command: str, validation: dict | None) -> None:
    if command not in _ROTATION_VALIDATION_COMMANDS or not isinstance(validation, dict):
        return

    severity = str(validation.get("severity") or ("ok" if validation.get("ok", True) else "failed"))
    if severity == "ok":
        _rotation_validation_cooldown.update(
            {
                "next_rotate_after": 0.0,
                "recorded_at": time.time(),
                "severity": "ok",
                "reason": "",
            }
        )
        return

    seconds = _auto_check_int_env(
        "AUTO_CHECK_DEGRADED_COOLDOWN_SECONDS" if severity == "degraded" else "AUTO_CHECK_FAILED_COOLDOWN_SECONDS",
        120 if severity == "degraded" else 300,
    )
    now = time.time()
    next_rotate_after = now + seconds if seconds > 0 else 0.0
    reason = _validation_failure_reason(validation)
    _rotation_validation_cooldown.update(
        {
            "next_rotate_after": next_rotate_after,
            "recorded_at": now,
            "severity": severity,
            "reason": reason,
        }
    )
    followup = validation.setdefault("followup", {})
    followup["cooldown_seconds"] = seconds
    followup["next_eligible_rotation_at"] = next_rotate_after


def _rotation_validation_cooldown_remaining() -> float:
    next_rotate_after = float(_rotation_validation_cooldown.get("next_rotate_after") or 0.0)
    if next_rotate_after <= 0:
        return 0.0
    return max(0.0, next_rotate_after - time.time())


def _log_task_runtime_validation(command: str) -> dict | None:
    """Record post-task runtime truth so completed tasks do not hide broken pool state."""
    if command not in _TASK_VALIDATION_COMMANDS:
        return None

    try:
        from autoteam.accounts import STATUS_ACTIVE, STATUS_AUTH_INVALID, is_account_disabled, load_accounts
        from autoteam.codex_auth import check_codex_quota
        from autoteam.manager import _pool_active_target

        accounts = load_accounts()
        target_seats = _resolve_auto_check_target_seats(_auto_check_config)
        pool_target = _pool_active_target(target_seats)
        team_count = _auto_check_team_member_count(timeout_seconds=20, retries=1)

        active_with_auth = 0
        auth_pending = 0
        auth_file_count = 0
        quota_ok = 0
        quota_exhausted = 0
        quota_auth_error = 0
        quota_unknown = 0
        primary_pcts: list[int] = []

        for acc in accounts:
            if _is_main_account_email(acc.get("email")) or is_account_disabled(acc):
                continue
            status = acc.get("status")
            auth_path = _resolve_status_auth_file(acc)
            if status == STATUS_AUTH_INVALID:
                auth_pending += 1
            if auth_path:
                auth_file_count += 1
            if status != STATUS_ACTIVE or not auth_path:
                continue

            active_with_auth += 1
            try:
                auth_data = json.loads(read_text(Path(auth_path)))
                access_token = auth_data.get("access_token")
                if not access_token:
                    quota_unknown += 1
                    continue
                quota_status, info = check_codex_quota(access_token)
                if quota_status == "ok":
                    quota_ok += 1
                elif quota_status == "exhausted":
                    quota_exhausted += 1
                elif quota_status == "auth_error":
                    quota_auth_error += 1
                else:
                    quota_unknown += 1
                if isinstance(info, dict) and isinstance(info.get("primary_pct"), (int, float)):
                    primary_pcts.append(int(info["primary_pct"]))
            except Exception:
                quota_unknown += 1

        logger.info(
            "[验收] %s 后: team=%s/%s active_with_auth=%d/%d auth_invalid=%d auth_files=%d "
            "quota_ok=%d exhausted=%d auth_error=%d unknown=%d primary_pct=%s",
            command,
            team_count if team_count >= 0 else "unknown",
            target_seats,
            active_with_auth,
            pool_target,
            auth_pending,
            auth_file_count,
            quota_ok,
            quota_exhausted,
            quota_auth_error,
            quota_unknown,
            ",".join(str(v) for v in primary_pcts[:8]) or "-",
        )
        validation = {
            "ok": True,
            "severity": "ok",
            "operation_success": True,
            "pool_health_ok": True,
            "followup_required": False,
            "reasons": [],
            "team_count": team_count,
            "target_seats": target_seats,
            "pool_target": pool_target,
            "active_with_auth": active_with_auth,
            "auth_pending": auth_pending,
            "auth_file_count": auth_file_count,
            "quota_ok": quota_ok,
            "quota_exhausted": quota_exhausted,
            "quota_auth_error": quota_auth_error,
            "quota_unknown": quota_unknown,
            "primary_pct": primary_pcts[:8],
        }
        hard_reasons: list[str] = []
        followup_reasons: list[str] = []

        if team_count >= 0 and team_count != target_seats:
            hard_reasons.append(f"team_count={team_count}/{target_seats}")
        if active_with_auth < pool_target:
            hard_reasons.append(f"active_with_auth={active_with_auth}/{pool_target}")
        if quota_auth_error > 0:
            hard_reasons.append(f"quota_auth_error={quota_auth_error}")
        if quota_ok < pool_target:
            followup_reasons.append(f"quota_ok={quota_ok}/{pool_target}")

        cpa_credential_gate = _collect_cpa_credential_gate()
        if cpa_credential_gate.get("enabled"):
            validation["cpa_credential_gate"] = cpa_credential_gate
            validation["provider_auth_available"] = cpa_credential_gate.get("available")
            if _cpa_provider_auth_below_pool_target(cpa_credential_gate, pool_target):
                available = int(cpa_credential_gate.get("available") or 0)
                followup_reasons.append(f"provider_auth_available={available}/{pool_target}")

        if hard_reasons:
            validation.update(
                {
                    "ok": False,
                    "severity": "failed",
                    "operation_success": False,
                    "pool_health_ok": False,
                    "followup_required": True,
                    "reasons": hard_reasons + followup_reasons,
                    "reason": "; ".join(hard_reasons + followup_reasons),
                }
            )
        elif followup_reasons:
            validation.update(
                {
                    "ok": False,
                    "severity": "degraded",
                    "operation_success": True,
                    "pool_health_ok": False,
                    "followup_required": True,
                    "reasons": followup_reasons,
                    "reason": "; ".join(followup_reasons),
                    "followup": {
                        "recommended_command": "rotate",
                        "reason": "; ".join(followup_reasons),
                    },
                }
            )

        return validation
    except Exception as exc:
        logger.warning("[验收] %s 后运行态核查失败: %s", command, exc)
        return None


def _run_task(task_id: str, func, *args, **kwargs):
    """在后台线程中执行任务"""
    from autoteam import cancel_signal

    global _current_task_id
    task = _tasks[task_id]

    _playwright_lock.acquire()
    # 顺序很关键: 先 reset() 再暴露 _current_task_id。否则 post_task_cancel 在
    # 两行之间读到新 task_id 并 request_cancel(),随后被我们的 reset() 清掉,
    # 用户的取消请求被静默吞掉。
    cancel_signal.reset()
    _current_task_id = task_id
    task["status"] = "running"
    task["started_at"] = time.time()
    task["last_progress_at"] = task["started_at"]
    task["progress"] = "started"
    _record_task_progress(task, "started", task["started_at"])

    try:
        result = func(*args, **kwargs)
        # 任务完成但中途确实收到取消 → 标 cancelled
        task["status"] = "cancelled" if cancel_signal.is_cancelled() else "completed"
        task["result"] = result
    except Exception as e:
        task["status"] = "cancelled" if cancel_signal.is_cancelled() else "failed"
        task["error"] = str(e)
        logger.error("[API] 任务 %s %s: %s", task_id[:8], task["status"], e)
    finally:
        _record_task_progress(task, "runtime_validation")
        validation = _log_task_runtime_validation(task.get("command", ""))
        task["finished_at"] = time.time()
        task["last_progress_at"] = task["finished_at"]
        if validation is not None:
            task["validation"] = validation
            _record_rotation_validation_decision(task.get("command", ""), validation)
            if task["status"] == "completed" and _validation_is_hard_failure(validation):
                task["status"] = "failed"
                task["error"] = f"runtime validation failed: {_validation_failure_reason(validation)}"
        _record_task_progress(task, "finished", task["finished_at"])
        _current_task_id = None
        _playwright_lock.release()


def _record_task_progress(task: dict, stage: str, now: float | None = None) -> None:
    """Record task stage timings for live diagnostics."""
    timestamp = time.time() if now is None else now
    stage_name = str(stage or task.get("progress") or "progress")
    history = task.setdefault("progress_history", [])

    if history and history[-1].get("stage") == stage_name:
        history[-1]["last_at"] = timestamp
        history[-1]["duration_sec"] = round(timestamp - float(history[-1].get("started_at", timestamp)), 3)
        return

    if history:
        previous = history[-1]
        previous["ended_at"] = timestamp
        previous["duration_sec"] = round(timestamp - float(previous.get("started_at", timestamp)), 3)

    history.append({"stage": stage_name, "started_at": timestamp, "last_at": timestamp})
    if len(history) > MAX_TASK_PROGRESS_HISTORY:
        del history[: len(history) - MAX_TASK_PROGRESS_HISTORY]


def bump_task_progress(stage: str = "") -> None:
    """Update the current background task heartbeat and optional stage label."""
    tid = _current_task_id
    if not tid:
        return
    task = _tasks.get(tid)
    if not task:
        return
    now = time.time()
    task["last_progress_at"] = now
    if stage:
        task["progress"] = stage
        _record_task_progress(task, stage, now)


def _start_task(command: str, func, params: dict, *args, **kwargs) -> dict:
    """创建并启动后台任务,返回任务信息"""
    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再试"))
    _playwright_lock.release()

    task_id = uuid.uuid4().hex[:12]
    task = {
        "task_id": task_id,
        "command": command,
        "params": params,
        "status": "pending",
        "created_at": time.time(),
        "started_at": None,
        "finished_at": None,
        "result": None,
        "error": None,
        "last_progress_at": time.time(),
        "progress": "pending",
        "progress_history": [],
    }
    _tasks[task_id] = task
    _prune_tasks()

    thread = threading.Thread(target=_run_task, args=(task_id, func, *args), kwargs=kwargs, daemon=True)
    thread.start()

    return task


# ---------------------------------------------------------------------------
# 响应模型
# ---------------------------------------------------------------------------


class TaskParams(BaseModel):
    target: int = 3
    leave_workspace: bool = False  # cmd_fill 专用:True 表示生产免费号(注册后退出 Team 走 personal OAuth)


class MultiMasterFillParams(BaseModel):
    target: int = 3
    owner_workers: int | None = None
    direct_parallel: int | None = None
    workspace_ids: list[str] | None = None
    dry_run: bool = False


class CleanupParams(BaseModel):
    max_seats: int | None = None


class AdminEmailParams(BaseModel):
    email: str


class AdminSessionParams(BaseModel):
    email: str
    session_token: str


class AdminPasswordParams(BaseModel):
    password: str


class AdminCodeParams(BaseModel):
    code: str


class AdminWorkspaceParams(BaseModel):
    option_id: str


class ManualAccountCallbackParams(BaseModel):
    redirect_url: str


class TeamMemberRemoveParams(BaseModel):
    email: str
    user_id: str
    type: str


class RegisterDomainParams(BaseModel):
    domain: str
    verify: bool = True  # 默认写入前试探一次 CloudMail 是否接受该域


class PreferredSeatTypeParams(BaseModel):
    """SPEC-2 FR-G — 邀请席位偏好。"default"(优先 PATCH 升级 ChatGPT 完整席位) | "codex"(锁 codex-only)"""
    value: str  # "default" | "codex"


class SyncProbeParams(BaseModel):
    """SPEC-2 FR-E — sync_account_states 被踢探测的并发上限和去重冷却。"""
    concurrency: int | None = None  # 1..16
    cooldown_minutes: int | None = None  # 1..1440


class DeleteBatchParams(BaseModel):
    emails: list[str]
    continue_on_error: bool = True  # 部分失败时继续剩余账号,False 则遇错即停


class AccountDisableParams(BaseModel):
    emails: list[str]


def _normalized_email(value: str | None) -> str:
    return (value or "").strip().lower()


def _is_main_account_email(email: str | None) -> bool:
    from autoteam.admin_state import get_admin_email

    return bool(_normalized_email(email)) and _normalized_email(email) == _normalized_email(get_admin_email())


def _quota_snapshot_status(quota_info: dict | None) -> str:
    if not isinstance(quota_info, dict):
        return ""

    values = []
    for key in ("primary_pct", "weekly_pct"):
        value = quota_info.get(key)
        if isinstance(value, (int, float)):
            values.append(value)

    if not values:
        return ""
    return "exhausted" if any(value >= 100 for value in values) else "active"


def _resolve_status_auth_file(acc: dict) -> str:
    auth_file = (acc.get("auth_file") or "").strip()
    if auth_file and Path(auth_file).exists():
        return auth_file

    if _is_main_account_email(acc.get("email")):
        from autoteam.codex_auth import get_saved_main_auth_file

        saved_auth_file = get_saved_main_auth_file()
        if saved_auth_file and Path(saved_auth_file).exists():
            return saved_auth_file

    return ""


def _display_account_status(acc: dict, quota_snapshot: dict | None = None) -> str:
    from autoteam.accounts import is_account_disabled

    status = acc.get("status", "")
    if not _is_main_account_email(acc.get("email")):
        if is_account_disabled(acc):
            return "disabled"
        return status

    quota_status = _quota_snapshot_status(quota_snapshot) or _quota_snapshot_status(acc.get("last_quota"))
    if quota_status:
        return quota_status

    return "active" if _resolve_status_auth_file(acc) else status


def _sanitize_account(acc: dict, quota_snapshot: dict | None = None) -> dict:
    """脱敏账号信息(去掉 password 等敏感字段)"""
    sanitized = {k: v for k, v in acc.items() if k not in ("password", "cloudmail_account_id")}
    sanitized["is_main_account"] = _is_main_account_email(acc.get("email"))
    sanitized["raw_status"] = acc.get("status", "")
    sanitized["status"] = _display_account_status(acc, quota_snapshot)
    return sanitized


def _admin_status():
    from autoteam.admin_state import get_admin_state_summary

    status = get_admin_state_summary()
    status["login_step"] = _admin_login_step
    status["login_in_progress"] = _admin_login_api is not None
    if _admin_login_api and _admin_login_step == "workspace_required":
        status["workspace_options"] = getattr(_admin_login_api, "workspace_options_cache", []) or []
    else:
        status["workspace_options"] = []
    return status


def _main_codex_status():
    return {
        "in_progress": _main_codex_flow is not None,
        "step": _main_codex_step,
        "action": _main_codex_action,
    }


def _manual_account_status():
    status = {
        "in_progress": False,
        "status": "idle",
        "state": "",
        "auth_url": "",
        "started_at": None,
        "message": "",
        "error": "",
        "account": None,
        "callback_received": False,
        "callback_source": "",
        "auto_callback_available": False,
        "auto_callback_error": "",
    }
    if _manual_account_flow:
        status.update(_manual_account_flow.status())
    return status


def _finish_admin_login(completed: dict):
    global _admin_login_api, _admin_login_step
    api = _admin_login_api
    info = None
    try:
        info = _pw_executor.run(api.complete_admin_login)
    finally:
        if api:
            try:
                _pw_executor.run(api.stop)
            except Exception:
                pass
        _admin_login_api = None
        _admin_login_step = None
        if info and info.get("session_token") and info.get("account_id"):
            try:
                from autoteam.codex_auth import refresh_main_auth_file

                main_auth = _pw_executor.run(refresh_main_auth_file)
                if main_auth:
                    info["main_auth"] = main_auth
                    logger.info("[API] 管理员登录后已刷新主号认证文件: %s", main_auth.get("auth_file"))
            except Exception as exc:
                info["main_auth_error"] = str(exc)
                logger.warning("[API] 管理员登录完成,但刷新主号认证文件失败: %s", exc)
        if _playwright_lock.locked():
            _playwright_lock.release()
    return {"status": "completed", "admin": _admin_status(), "codex": _main_codex_status(), "info": info}


def _set_pending_admin_login(api, step):
    global _admin_login_api, _admin_login_step
    _admin_login_api = api
    _admin_login_step = step
    return {"status": step, "admin": _admin_status()}


def _finish_main_codex_flow():
    global _main_codex_flow, _main_codex_step, _main_codex_action
    flow = _main_codex_flow
    action = _main_codex_action or "sync"
    try:
        info = _pw_executor.run(flow.complete)
    finally:
        if flow:
            try:
                _pw_executor.run(flow.stop)
            except Exception:
                pass
        _main_codex_flow = None
        _main_codex_step = None
        _main_codex_action = None
        if _playwright_lock.locked():
            _playwright_lock.release()

    if action == "login":
        message = "主号 Codex 已登录"
    else:
        from autoteam.sync_targets import describe_sync_targets, get_enabled_sync_targets

        enabled_targets = get_enabled_sync_targets()
        target_label = describe_sync_targets(enabled_targets)
        message = (
            f"主号 Codex 已同步到 {target_label}"
            if enabled_targets
            else "主号 Codex 已保存,本轮未启用远端同步目标"
        )
    return {
        "status": "completed",
        "message": message,
        "codex": _main_codex_status(),
        "info": info,
    }


def _finish_main_codex_sync():
    return _finish_main_codex_flow()


def _set_pending_main_codex_flow(flow, step, action):
    global _main_codex_flow, _main_codex_step, _main_codex_action
    _main_codex_flow = flow
    _main_codex_step = step
    _main_codex_action = action
    return {"status": step, "codex": _main_codex_status()}


def _set_pending_main_codex_sync(flow, step):
    return _set_pending_main_codex_flow(flow, step, "sync")


def _finish_manual_account_flow(result: dict):
    return {**result, "manual_account": _manual_account_status()}


def _set_pending_manual_account_flow(flow, result):
    global _manual_account_flow
    _manual_account_flow = flow
    return {**result, "manual_account": _manual_account_status()}


# ---------------------------------------------------------------------------
# 同步端点
# ---------------------------------------------------------------------------


@app.get("/api/admin/status")
def get_admin_status():
    """获取管理员登录状态。"""
    return _admin_status()


@app.post("/api/admin/fix-account-id")
def post_admin_fix_account_id():
    """

    基于当前已保存的 session_token,重新从 /backend-api/accounts 拉取真实 workspace 列表,

    覆盖写入 admin_state.account_id / workspace_name。适用于: 之前导入的 session 把

    account_id 误写成了 OAI 缓存的陈旧 UUID,导致所有 admin 接口 401。



    不需要用户手动退出重登 —— 只是重算 account_id。

    """
    from autoteam.admin_state import (
        get_admin_email,
        get_admin_session_token,
        get_chatgpt_account_id,
        update_admin_state,
    )
    from autoteam.chatgpt_api import ChatGPTTeamAPI

    if not get_admin_session_token():
        raise HTTPException(status_code=400, detail="尚未保存 session_token,请先导入")

    def _do():
        api = ChatGPTTeamAPI()
        try:
            api._launch_browser()
            logger.info("[修复 account_id] 打开 chatgpt.com 注入 session...")
            api.page.goto("https://chatgpt.com/", wait_until="domcontentloaded", timeout=60000)
            time.sleep(3)
            api._wait_for_cloudflare()
            api._inject_session(get_admin_session_token())
            # 注入 session 后可能触发一次新的 CF 挑战,再等一次避免首个 _api_fetch 碰上 challenge 页
            api.page.goto("https://chatgpt.com/", wait_until="domcontentloaded", timeout=60000)
            time.sleep(2)
            api._wait_for_cloudflare()
            api._fetch_access_token()

            team, personal = api._list_real_workspaces()
            admin_roles = ("account-owner", "admin", "org-admin", "workspace-owner")
            chosen = None
            for acc in team:
                if str(acc.get("current_user_role") or "").lower() in admin_roles:
                    chosen = acc
                    break
            if not chosen and team:
                chosen = team[0]
            if not chosen:
                raise HTTPException(
                    status_code=400,
                    detail=(
                        f"当前 session ({get_admin_email()}) 没有 Team workspace,"
                        f" 只有: {[a.get('structure') for a in personal]}。"
                        f"请确认该账号已被邀请加入 Team。"
                    ),
                )

            new_account_id = str(chosen.get("id") or "")
            new_workspace_name = str(chosen.get("name") or "")

            # 用新 account_id 验证接口是否真能访问
            api.account_id = new_account_id
            verify = api._api_fetch("GET", f"/backend-api/accounts/{new_account_id}/settings")
            if verify.get("status") != 200:
                raise HTTPException(
                    status_code=400,
                    detail=(
                        f"新 account_id={new_account_id} 仍不可访问 "
                        f"status={verify.get('status')},session_token 可能已过期,请重新导入。"
                    ),
                )

            old_account_id = get_chatgpt_account_id()
            update_admin_state(account_id=new_account_id, workspace_name=new_workspace_name)
            logger.info(
                "[修复 account_id] 已更新: %s -> %s (workspace=%s)",
                old_account_id,
                new_account_id,
                new_workspace_name,
            )
            return {
                "message": "已修复",
                "old_account_id": old_account_id,
                "new_account_id": new_account_id,
                "workspace_name": new_workspace_name,
                "role": chosen.get("current_user_role"),
            }
        finally:
            try:
                api.stop()
            except Exception:
                pass

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行"))
    try:
        return _pw_executor.run(_do)
    finally:
        _playwright_lock.release()


@app.get("/api/admin/diagnose")
def get_admin_diagnose():
    """

    用当前管理员 session_token 探测 Team admin 接口,辅助诊断 401/403。

    返回四个关键接口的状态码 + body 前 200 字:

    - /api/auth/session  → access_token 是否拿到

    - /backend-api/me    → 当前登录用户是谁

    - /backend-api/accounts/<id>/settings  → workspace 是否可读

    - /backend-api/accounts/<id>/users     → admin 权限是否生效(真正的 fill-personal 卡点)

    """
    from autoteam.admin_state import get_admin_email, get_chatgpt_account_id
    from autoteam.chatgpt_api import ChatGPTTeamAPI

    def _do():
        # 只读诊断:必须走手动 launch+inject,不调 api.start()——start() 里的
        # _auto_detect_workspace 会写 admin_state,把诊断弄成副作用操作
        from autoteam.admin_state import get_admin_session_token

        api = ChatGPTTeamAPI()
        try:
            api._launch_browser()
            api.page.goto("https://chatgpt.com/", wait_until="domcontentloaded", timeout=60000)
            time.sleep(3)
            api._wait_for_cloudflare()
            session_token = get_admin_session_token()
            if session_token:
                api.account_id = get_chatgpt_account_id() or ""  # 让 _inject_session 把 _account cookie 带上
                api._inject_session(session_token)
                api.page.goto("https://chatgpt.com/", wait_until="domcontentloaded", timeout=60000)
                time.sleep(2)
                api._wait_for_cloudflare()
            api._fetch_access_token()
            account_id = api.account_id or get_chatgpt_account_id() or ""
            probes = {}

            session_result = api.page.evaluate(
                "async () => { const r = await fetch('/api/auth/session'); "
                "return { status: r.status, body: (await r.text()).slice(0, 400) }; }"
            )
            probes["auth_session"] = session_result

            for name, path in [
                ("backend_me", "/backend-api/me"),
                ("backend_accounts", "/backend-api/accounts"),
                ("workspace_settings", f"/backend-api/accounts/{account_id}/settings"),
                ("workspace_users", f"/backend-api/accounts/{account_id}/users"),
            ]:
                r = api._api_fetch("GET", path)
                probes[name] = {"status": r.get("status"), "body": (r.get("body") or "")[:500]}

            # Round 8 — SPEC-2 v1.5 §6.2:diagnose 内嵌 master_subscription_state(read-only,5min cache)
            try:
                from autoteam.master_health import is_master_subscription_healthy
                healthy, reason, evidence = is_master_subscription_healthy(api)
                master_state = {
                    "healthy": healthy,
                    "reason": reason,
                    "evidence": evidence,
                }
            except Exception as exc:
                master_state = {
                    "healthy": None,
                    "reason": "probe_exception",
                    "evidence": {"detail": str(exc)[:200]},
                }

            return {
                "admin_email": get_admin_email(),
                "account_id": account_id,
                "access_token_present": bool(api.access_token),
                "access_token_prefix": (api.access_token or "")[:30],
                "probes": probes,
                "master_subscription_state": master_state,
            }
        finally:
            try:
                api.stop()
            except Exception:
                pass

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行"))
    try:
        return _pw_executor.run(_do)
    finally:
        _playwright_lock.release()


@app.get("/api/admin/master-health")
def get_admin_master_health(request: Request):
    """Round 8 — 母号订阅健康度独立端点。



    Round 9 SPEC v1.1 §13 endpoint 守恒:任何场景**永不返回 5xx**,

    全部异常映射到 200 OK + business field(reason='auth_invalid' / 'network_error')。



    查询参数:

        force_refresh=1 → 跳过 5min cache 直接重测

    返回 is_master_subscription_healthy() 的完整 (healthy, reason, evidence) 三元组。

    UI 用于"立即重测"按钮。

    """
    force_refresh = str(request.query_params.get("force_refresh", "")).strip().lower() in (
        "1", "true", "yes",
    )

    def _do():
        from autoteam.chatgpt_api import ChatGPTTeamAPI
        from autoteam.master_health import is_master_subscription_healthy

        api = None
        try:
            try:
                api = ChatGPTTeamAPI()
                api.start()
            except Exception as exc:
                # spec §13.2 — start() 失败映射 auth_invalid 200 OK
                logger.warning("[master-health] ChatGPTTeamAPI start failed: %s", exc)
                return {
                    "healthy": False,
                    "reason": "auth_invalid",
                    "evidence": {
                        "http_status": None,
                        "detail": f"chatgpt_api_start_failed:{type(exc).__name__}",
                        "cache_hit": False,
                        "probed_at": time.time(),
                    },
                    "force_refresh": force_refresh,
                }
            try:
                healthy, reason, evidence = is_master_subscription_healthy(
                    api, force_refresh=force_refresh,
                )
                # Round 12 wire-up (C2) — feed every "force-refresh re-probe"
                # into WorkspacePool so connect 3 unhealthy → auto failover.
                # apply_pool_health_signal swallows internally (M-I1).
                try:
                    from autoteam.master_health import apply_pool_health_signal
                    apply_pool_health_signal(healthy, reason, evidence)
                except Exception as pool_exc:  # pragma: no cover — defensive
                    logger.warning(
                        "[master-health] pool signal failed: %s", pool_exc,
                    )
                return {
                    "healthy": healthy,
                    "reason": reason,
                    "evidence": evidence,
                    "force_refresh": force_refresh,
                }
            except Exception as exc:
                # spec §13.2 — probe 双保险,is_master_subscription_healthy 自身永不抛,
                # 这里兜底把任何意外异常映射 network_error 200 OK
                logger.warning(
                    "[master-health] probe unexpected exception: %s", exc,
                )
                return {
                    "healthy": False,
                    "reason": "network_error",
                    "evidence": {
                        "http_status": None,
                        "detail": f"probe_unexpected_exception:{type(exc).__name__}",
                        "cache_hit": False,
                        "probed_at": time.time(),
                    },
                    "force_refresh": force_refresh,
                }
        finally:
            try:
                if api is not None:
                    api.stop()
            except Exception:
                pass

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行"))
    try:
        try:
            return _pw_executor.run(_do)
        except Exception as exc:
            # spec §13.3 — _pw_executor 调度异常兜底,仍按 endpoint 守恒返回 200
            logger.error("[master-health] _pw_executor.run failed: %s", exc)
            return {
                "healthy": False,
                "reason": "network_error",
                "evidence": {
                    "http_status": None,
                    "detail": f"executor_failed:{type(exc).__name__}",
                    "cache_hit": False,
                    "probed_at": time.time(),
                },
                "force_refresh": force_refresh,
            }
    finally:
        _playwright_lock.release()


@app.post("/api/admin/reconcile")
def post_admin_reconcile(request: Request):
    """对账 Team 实际成员 vs 本地状态,修复残废 / 错位 / 耗尽未抛弃 / ghost。



    与 /api/admin/diagnose 使用同款鉴权模式(auth_middleware 已处理 API_KEY)。

    查询参数:

        dry_run=1 → 只诊断,不 KICK、不改 accounts.json

    返回 _reconcile_team_members 的完整结果 dict。

    """
    from autoteam.manager import cmd_reconcile

    dry_run = str(request.query_params.get("dry_run", "")).strip().lower() in ("1", "true", "yes")

    def _do():
        return cmd_reconcile(dry_run=dry_run)

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行"))
    try:
        return _pw_executor.run(_do)
    finally:
        _playwright_lock.release()


@app.get("/api/main-codex/status")
def get_main_codex_status():
    """获取主号 Codex 同步状态。"""
    return _main_codex_status()


@app.get("/api/manual-account/status")
def get_manual_account_status():
    """获取手动添加账号状态。"""
    return _manual_account_status()


@app.post("/api/admin/login/start")
def post_admin_login_start(params: AdminEmailParams):
    """开始管理员登录流程。"""
    global _admin_login_api, _admin_login_step

    if _admin_login_api:
        try:
            _pw_executor.run(_admin_login_api.stop)
        except Exception:
            pass
        _admin_login_api = None
        _admin_login_step = None
        if _playwright_lock.locked():
            _playwright_lock.release()

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(
            status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再进行管理员登录")
        )

    try:
        from autoteam.chatgpt_api import ChatGPTTeamAPI

        logger.info("[API] 开始管理员登录: %s", params.email.strip())

        def _do_start(email):
            api = ChatGPTTeamAPI()
            try:
                result = api.begin_admin_login(email)
                return api, result
            except Exception:
                api.stop()
                raise

        api, result = _pw_executor.run(_do_start, params.email.strip())
        step = result["step"]
        logger.info("[API] 管理员登录 start 返回: step=%s detail=%s", step, result.get("detail"))
        if step == "completed":
            _admin_login_api = api
            return _finish_admin_login(result)
        if step in ("password_required", "code_required", "workspace_required"):
            return _set_pending_admin_login(api, step)
        _pw_executor.run(api.stop)
        _playwright_lock.release()
        raise HTTPException(status_code=400, detail=result.get("detail") or "无法识别管理员登录步骤")
    except HTTPException:
        raise
    except Exception as exc:
        logger.exception("[API] 管理员登录 start 失败")
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/admin/login/session")
def post_admin_login_session(params: AdminSessionParams):
    """手动导入管理员 session_token。"""
    global _admin_login_api, _admin_login_step

    if _admin_login_api:
        post_admin_login_cancel()

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(
            status_code=409,
            detail=_current_busy_detail("有任务正在执行,请等待完成后再导入管理员 session_token"),
        )

    try:
        from autoteam.chatgpt_api import ChatGPTTeamAPI

        logger.info("[API] 导入管理员 session_token: %s", params.email.strip())

        def _do_import(email, session_token):
            api = ChatGPTTeamAPI()
            try:
                return api.import_admin_session(email, session_token)
            finally:
                api.stop()

        info = _pw_executor.run(_do_import, params.email.strip(), params.session_token.strip())
        if info.get("session_token") and info.get("account_id"):
            try:
                from autoteam.codex_auth import refresh_main_auth_file

                main_auth = _pw_executor.run(refresh_main_auth_file)
                if main_auth:
                    info["main_auth"] = main_auth
                    logger.info("[API] session_token 导入后已刷新主号认证文件: %s", main_auth.get("auth_file"))
            except Exception as exc:
                info["main_auth_error"] = str(exc)
                logger.warning("[API] session_token 导入完成,但刷新主号认证文件失败: %s", exc)
        _admin_login_api = None
        _admin_login_step = None
        return {"status": "completed", "admin": _admin_status(), "info": info}
    except HTTPException:
        raise
    except Exception as exc:
        logger.exception("[API] 导入管理员 session_token 失败")
        raise HTTPException(status_code=400, detail=str(exc))
    finally:
        if _playwright_lock.locked():
            _playwright_lock.release()


@app.post("/api/admin/login/password")
def post_admin_login_password(params: AdminPasswordParams):
    """提交管理员密码。"""
    global _admin_login_api, _admin_login_step
    if not _admin_login_api or _admin_login_step != "password_required":
        raise HTTPException(status_code=409, detail="当前没有等待密码的管理员登录流程")

    try:
        logger.info("[API] 提交管理员密码 | current_step=%s", _admin_login_step)
        result = _pw_executor.run(_admin_login_api.submit_admin_password, params.password)
        step = result["step"]
        logger.info("[API] 管理员密码提交返回: step=%s detail=%s", step, result.get("detail"))
        if step == "completed":
            return _finish_admin_login(result)
        if step in ("password_required", "code_required", "workspace_required"):
            _admin_login_step = step
            return {"status": step, "admin": _admin_status()}
        raise HTTPException(status_code=400, detail=result.get("detail") or "管理员密码登录失败")
    except HTTPException:
        raise
    except Exception as exc:
        logger.exception("[API] 管理员密码提交失败")
        try:
            _pw_executor.run(_admin_login_api.stop)
        except Exception:
            pass
        _admin_login_api = None
        _admin_login_step = None
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/admin/login/code")
def post_admin_login_code(params: AdminCodeParams):
    """提交管理员验证码。"""
    global _admin_login_api, _admin_login_step
    if not _admin_login_api or _admin_login_step != "code_required":
        raise HTTPException(status_code=409, detail="当前没有等待验证码的管理员登录流程")

    try:
        logger.info("[API] 提交管理员验证码 | current_step=%s code_len=%d", _admin_login_step, len(params.code.strip()))
        result = _pw_executor.run(_admin_login_api.submit_admin_code, params.code.strip())
        step = result["step"]
        logger.info("[API] 管理员验证码提交返回: step=%s detail=%s", step, result.get("detail"))
        if step == "completed":
            return _finish_admin_login(result)
        if step in ("password_required", "code_required", "workspace_required"):
            _admin_login_step = step
            return {"status": step, "admin": _admin_status()}
        raise HTTPException(status_code=400, detail=result.get("detail") or "管理员验证码登录失败")
    except HTTPException:
        raise
    except Exception as exc:
        logger.exception("[API] 管理员验证码提交失败")
        try:
            _pw_executor.run(_admin_login_api.stop)
        except Exception:
            pass
        _admin_login_api = None
        _admin_login_step = None
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/admin/login/workspace")
def post_admin_login_workspace(params: AdminWorkspaceParams):
    """提交管理员 workspace 选择。"""
    global _admin_login_api, _admin_login_step
    if not _admin_login_api or _admin_login_step != "workspace_required":
        raise HTTPException(status_code=409, detail="当前没有等待组织选择的管理员登录流程")

    try:
        logger.info("[API] 提交管理员 workspace 选择 | option_id=%s", params.option_id)
        result = _pw_executor.run(_admin_login_api.select_workspace_option, params.option_id)
        step = result["step"]
        logger.info("[API] 管理员 workspace 选择返回: step=%s detail=%s", step, result.get("detail"))
        if step == "completed":
            return _finish_admin_login(result)
        if step in ("password_required", "code_required", "workspace_required"):
            _admin_login_step = step
            return {"status": step, "admin": _admin_status()}
        raise HTTPException(status_code=400, detail=result.get("detail") or "管理员组织选择失败")
    except HTTPException:
        raise
    except Exception as exc:
        logger.exception("[API] 管理员 workspace 选择失败")
        try:
            _pw_executor.run(_admin_login_api.stop)
        except Exception:
            pass
        _admin_login_api = None
        _admin_login_step = None
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/admin/login/cancel")
def post_admin_login_cancel():
    """取消管理员登录流程。"""
    global _admin_login_api, _admin_login_step
    if _admin_login_api:
        try:
            _pw_executor.run(_admin_login_api.stop)
        except Exception:
            pass
        _admin_login_api = None
        _admin_login_step = None
        if _playwright_lock.locked():
            _playwright_lock.release()
    return {"message": "管理员登录已取消", "admin": _admin_status()}


@app.post("/api/admin/logout")
def post_admin_logout():
    """清除已保存的管理员登录态。"""
    from autoteam.admin_state import clear_admin_state

    if _admin_login_api:
        post_admin_login_cancel()
    clear_admin_state()
    return {"message": "管理员登录态已清除", "admin": _admin_status()}


def _clear_active_main_codex_flow():
    global _main_codex_flow, _main_codex_step, _main_codex_action

    if _main_codex_flow:
        try:
            _pw_executor.run(_main_codex_flow.stop)
        except Exception:
            pass
        _main_codex_flow = None
        _main_codex_step = None
        _main_codex_action = None
        if _playwright_lock.locked():
            _playwright_lock.release()


def _start_main_codex_flow(action="sync"):
    from autoteam.codex_auth import MainCodexLoginFlow, MainCodexSyncFlow

    flow_cls = MainCodexSyncFlow if action == "sync" else MainCodexLoginFlow

    def _do_start():
        flow = flow_cls()
        try:
            result = flow.start()
        except Exception:
            try:
                flow.stop()
            except Exception:
                pass
            raise
        return flow, result

    flow, result = _pw_executor.run(_do_start)
    step = result["step"]
    if step == "completed":
        _set_pending_main_codex_flow(flow, step, action)
        return step, _finish_main_codex_flow()
    if step in ("password_required", "code_required"):
        return step, _set_pending_main_codex_flow(flow, step, action)

    _pw_executor.run(flow.stop)
    raise RuntimeError(result.get("detail") or "无法识别主号 Codex 登录步骤")


@app.post("/api/main-codex/start")
def post_main_codex_start():
    """开始主号 Codex 登录并同步到已启用远端。"""
    _clear_active_main_codex_flow()
    _require_sync_target_configs("同步主号 Codex")

    from autoteam.codex_auth import get_saved_main_auth_file
    from autoteam.sync_targets import (
        describe_sync_targets,
        get_enabled_sync_targets,
        sync_main_codex_to_configured_targets,
    )

    saved_auth_file = get_saved_main_auth_file()
    if saved_auth_file:
        sync_main_codex_to_configured_targets(saved_auth_file)
        enabled_targets = get_enabled_sync_targets()
        target_label = describe_sync_targets(enabled_targets)
        return {
            "status": "completed",
            "message": f"主号 Codex 已同步到 {target_label}",
            "codex": _main_codex_status(),
            "info": {"auth_file": saved_auth_file},
        }

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(
            status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再同步主号 Codex")
    )

    try:
        _step, result = _start_main_codex_flow(action="sync")
        return result
    except HTTPException:
        raise
    except Exception as exc:
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/main-codex/login")
def post_main_codex_login():
    """开始主号 Codex 登录,仅保存本地认证文件。"""
    _clear_active_main_codex_flow()

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(
            status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再登录主号 Codex")
        )

    try:
        _step, result = _start_main_codex_flow(action="login")
        return result
    except HTTPException:
        raise
    except Exception as exc:
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/main-codex/password")
def post_main_codex_password(params: AdminPasswordParams):
    """提交主号 Codex 登录密码。"""
    global _main_codex_flow, _main_codex_step, _main_codex_action
    if not _main_codex_flow or _main_codex_step != "password_required":
        raise HTTPException(status_code=409, detail="当前没有等待密码的主号 Codex 登录流程")

    try:
        result = _pw_executor.run(_main_codex_flow.submit_password, params.password)
        step = result["step"]
        if step == "completed":
            return _finish_main_codex_sync()
        if step in ("password_required", "code_required"):
            _main_codex_step = step
            return {"status": step, "codex": _main_codex_status()}
        raise HTTPException(status_code=400, detail=result.get("detail") or "主号 Codex 密码登录失败")
    except HTTPException:
        raise
    except Exception as exc:
        try:
            _pw_executor.run(_main_codex_flow.stop)
        except Exception:
            pass
        _main_codex_flow = None
        _main_codex_step = None
        _main_codex_action = None
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/main-codex/code")
def post_main_codex_code(params: AdminCodeParams):
    """提交主号 Codex 登录验证码。"""
    global _main_codex_flow, _main_codex_step, _main_codex_action
    if not _main_codex_flow or _main_codex_step != "code_required":
        raise HTTPException(status_code=409, detail="当前没有等待验证码的主号 Codex 登录流程")

    try:
        result = _pw_executor.run(_main_codex_flow.submit_code, params.code.strip())
        step = result["step"]
        if step == "completed":
            return _finish_main_codex_sync()
        if step in ("password_required", "code_required"):
            _main_codex_step = step
            return {"status": step, "codex": _main_codex_status()}
        raise HTTPException(status_code=400, detail=result.get("detail") or "主号 Codex 验证码登录失败")
    except HTTPException:
        raise
    except Exception as exc:
        try:
            _pw_executor.run(_main_codex_flow.stop)
        except Exception:
            pass
        _main_codex_flow = None
        _main_codex_step = None
        _main_codex_action = None
        if _playwright_lock.locked():
            _playwright_lock.release()
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/main-codex/cancel")
def post_main_codex_cancel():
    """取消主号 Codex 登录流程。"""
    global _main_codex_flow, _main_codex_step, _main_codex_action
    if _main_codex_flow:
        try:
            _pw_executor.run(_main_codex_flow.stop)
        except Exception:
            pass
        _main_codex_flow = None
        _main_codex_step = None
        _main_codex_action = None
        if _playwright_lock.locked():
            _playwright_lock.release()
    return {"message": "主号 Codex 登录已取消", "codex": _main_codex_status()}


def _delete_main_codex_from_enabled_targets():
    from autoteam.sync_targets import (
        delete_main_codex_from_configured_targets,
        describe_sync_targets,
        get_enabled_sync_targets,
    )

    _require_sync_target_configs("删除主号 Codex 远端文件")

    enabled_targets = get_enabled_sync_targets()
    results = delete_main_codex_from_configured_targets()
    deleted: list[str] = []
    total_count = 0

    for target in enabled_targets:
        target_result = results.get(target) or {}
        target_deleted = [str(item) for item in (target_result.get("deleted") or [])]
        deleted.extend(target_deleted)
        try:
            total_count += int(target_result.get("count", len(target_deleted)) or 0)
        except (TypeError, ValueError):
            total_count += len(target_deleted)

    return {
        "message": f"已从 {describe_sync_targets(enabled_targets)} 删除 {total_count} 个主号认证文件",
        "deleted": deleted,
        "results": results,
    }


@app.post("/api/main-codex/delete-remote-files")
def post_main_codex_delete_remote_files():
    """删除已启用远端中已上传的主号 Codex 认证文件。"""
    return _delete_main_codex_from_enabled_targets()


@app.post("/api/main-codex/delete-cpa")
def post_main_codex_delete_cpa():
    """兼容旧接口:删除已启用远端中的主号 Codex 认证文件。"""
    return _delete_main_codex_from_enabled_targets()


@app.post("/api/manual-account/start")
def post_manual_account_start():
    """开始手动添加账号流程,返回 OAuth 链接。"""
    global _manual_account_flow

    if _manual_account_flow:
        try:
            _manual_account_flow.stop()
        except Exception:
            pass
        _manual_account_flow = None

    try:
        from autoteam.manual_account import ManualAccountFlow

        flow = ManualAccountFlow()
        result = flow.start()
        return _set_pending_manual_account_flow(flow, result)
    except HTTPException:
        raise
    except Exception as exc:
        if _manual_account_flow:
            try:
                _manual_account_flow.stop()
            except Exception:
                pass
            _manual_account_flow = None
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/manual-account/callback")
def post_manual_account_callback(params: ManualAccountCallbackParams):
    """提交 OAuth 回调 URL,完成手动添加账号。"""
    global _manual_account_flow
    if not _manual_account_flow:
        raise HTTPException(status_code=409, detail="当前没有等待回调的手动添加账号流程")

    try:
        result = _manual_account_flow.submit_callback(params.redirect_url)
        return _finish_manual_account_flow(result)
    except Exception as exc:
        raise HTTPException(status_code=400, detail=str(exc))


@app.post("/api/manual-account/cancel")
def post_manual_account_cancel():
    """取消手动添加账号流程。"""
    global _manual_account_flow
    if _manual_account_flow:
        try:
            _manual_account_flow.stop()
        except Exception:
            pass
        _manual_account_flow = None
    return {"message": "手动添加账号流程已取消", "manual_account": _manual_account_status()}


@app.get("/api/accounts")
def get_accounts():
    """获取所有账号列表"""
    from autoteam.accounts import load_accounts

    accounts = load_accounts()
    return [_sanitize_account(a) for a in accounts]


@app.get("/api/accounts/{email}/codex-auth")
def get_codex_auth(email: str):
    """导出账号的 Codex CLI 格式认证文件(~/.codex/auth.json)"""
    from autoteam.accounts import find_account, load_accounts
    from autoteam.codex_auth import get_saved_main_auth_file

    email = email.strip().lower()
    auth_file = ""

    if _is_main_account_email(email):
        auth_file = get_saved_main_auth_file()
        if not auth_file or not Path(auth_file).exists():
            raise HTTPException(status_code=404, detail="主号没有可导出的认证文件")
    else:
        acc = find_account(load_accounts(), email)
        if not acc:
            raise HTTPException(status_code=404, detail="账号不存在")
        auth_file = acc.get("auth_file") or ""
        if not auth_file or not Path(auth_file).exists():
            raise HTTPException(status_code=404, detail="该账号没有认证文件")

    auth_data = json.loads(Path(auth_file).read_text())

    # 转换为 Codex CLI 的 auth.json 格式
    codex_auth = {
        "auth_mode": "chatgpt",
        "OPENAI_API_KEY": None,
        "tokens": {
            "id_token": auth_data.get("id_token", ""),
            "access_token": auth_data.get("access_token", ""),
            "refresh_token": auth_data.get("refresh_token", ""),
            "account_id": auth_data.get("account_id", ""),
        },
        "last_refresh": auth_data.get("last_refresh", ""),
    }

    return {
        "email": email,
        "codex_auth": codex_auth,
        "hint": "将内容保存到 ~/.codex/auth.json(Linux/macOS)或 %APPDATA%\\codex\\auth.json(Windows)",
    }


@app.get("/api/accounts/active")
def get_active():
    """获取活跃账号"""
    from autoteam.accounts import get_active_accounts

    return [_sanitize_account(a) for a in get_active_accounts()]


@app.get("/api/accounts/standby")
def get_standby():
    """获取待命账号"""
    from autoteam.accounts import get_standby_accounts

    accounts = get_standby_accounts()
    return [_sanitize_account(a) for a in accounts]


def _toggle_account_disabled(email: str, disabled: bool):
    from autoteam.accounts import find_account, load_accounts, update_account

    email = _normalized_email(email)
    if not email:
        raise HTTPException(status_code=400, detail="请提供有效邮箱")
    if _is_main_account_email(email):
        raise HTTPException(status_code=400, detail="主号不允许禁用或启用")

    accounts = load_accounts()
    acc = find_account(accounts, email)
    if not acc:
        raise HTTPException(status_code=404, detail="账号不存在")

    update_account(email, disabled=bool(disabled))
    refreshed = find_account(load_accounts(), email)
    action = "禁用" if disabled else "启用"
    return {
        "message": f"已{action} {email}",
        "email": email,
        "disabled": bool(disabled),
        "account": _sanitize_account(refreshed or {**acc, "disabled": bool(disabled)}),
    }


def _toggle_accounts_disabled(emails: list[str], disabled: bool):
    from autoteam.accounts import load_accounts, save_accounts

    normalized_emails = []
    seen = set()
    for value in emails or []:
        email = _normalized_email(value)
        if not email or email in seen:
            continue
        seen.add(email)
        normalized_emails.append(email)

    if not normalized_emails:
        raise HTTPException(status_code=400, detail="请至少提供一个有效邮箱")

    accounts = load_accounts()
    by_email = {_normalized_email(acc.get("email")): acc for acc in accounts if acc.get("email")}

    updated = []
    unchanged = []
    missing = []
    skipped_main = []

    for email in normalized_emails:
        if _is_main_account_email(email):
            skipped_main.append(email)
            continue
        acc = by_email.get(email)
        if not acc:
            missing.append(email)
            continue
        if bool(acc.get("disabled", False)) == bool(disabled):
            unchanged.append(email)
            continue
        acc["disabled"] = bool(disabled)
        updated.append(email)

    if updated:
        save_accounts(accounts)
        accounts = load_accounts()
        by_email = {_normalized_email(acc.get("email")): acc for acc in accounts if acc.get("email")}

    action = "禁用" if disabled else "启用"
    parts = [f"已{action} {len(updated)} 个账号"]
    if unchanged:
        parts.append(f"{len(unchanged)} 个已是目标状态")
    if skipped_main:
        parts.append(f"跳过主号 {len(skipped_main)} 个")
    if missing:
        parts.append(f"未找到 {len(missing)} 个")

    return {
        "message": ",".join(parts),
        "disabled": bool(disabled),
        "updated_count": len(updated),
        "updated_emails": updated,
        "unchanged_emails": unchanged,
        "skipped_main_accounts": skipped_main,
        "missing_emails": missing,
        "accounts": [_sanitize_account(by_email[email]) for email in updated if email in by_email],
    }


@app.post("/api/accounts/bulk/disable")
def post_bulk_disable_accounts(params: AccountDisableParams):
    """批量禁用账号:保留本地记录,但自动巡检、轮转和 CPA 同步会跳过。"""
    return _toggle_accounts_disabled(params.emails, True)


@app.post("/api/accounts/bulk/enable")
def post_bulk_enable_accounts(params: AccountDisableParams):
    """批量启用账号:恢复参与自动巡检、轮转和 CPA 同步。"""
    return _toggle_accounts_disabled(params.emails, False)


@app.post("/api/accounts/{email}/disable")
def post_disable_account(email: str):
    """禁用账号:保留本地记录,但自动巡检、轮转和 CPA 同步会跳过。"""
    return _toggle_account_disabled(email, True)


@app.post("/api/accounts/{email}/enable")
def post_enable_account(email: str):
    """启用账号:恢复参与自动巡检、轮转和 CPA 同步。"""
    return _toggle_account_disabled(email, False)


@app.delete("/api/accounts/{email}")
def delete_account(email: str):
    """删除本地管理账号及其关联资源。"""
    if not _playwright_lock.acquire(blocking=False):
        running = _tasks.get(_current_task_id, {})
        raise HTTPException(
            status_code=409,
            detail={
                "message": "有任务正在执行,请等待完成后再删除账号",
                "running_task": {
                    "task_id": _current_task_id,
                    "command": running.get("command", "unknown"),
                    "started_at": running.get("started_at"),
                },
            },
        )

    try:
        from autoteam.account_ops import delete_managed_account
        from autoteam.accounts import load_accounts

        if _is_main_account_email(email):
            raise HTTPException(status_code=400, detail="主号不允许删除")

        accounts = load_accounts()
        if not any(a["email"].lower() == email.lower() for a in accounts):
            raise HTTPException(status_code=404, detail="账号不存在")

        cleanup = _pw_executor.run(delete_managed_account, email)
        return {
            "message": "账号删除完成",
            "deleted_email": email,
            "cleanup": cleanup,
        }
    finally:
        _playwright_lock.release()


@app.post("/api/accounts/delete-batch")
def delete_accounts_batch(params: DeleteBatchParams):
    """

    批量删除本地管理账号。整批共享一个 chatgpt_api + mail_client,

    Team 成员/邀请状态只拉一次,CPA 在整批结束后同步一次,避免重复开销。

    """
    from autoteam.account_ops import delete_managed_account, fetch_team_state
    from autoteam.accounts import load_accounts
    from autoteam.chatgpt_api import ChatGPTTeamAPI
    from autoteam.cloudmail import CloudMailClient
    from autoteam.sync_targets import sync_to_configured_targets as sync_to_cpa

    raw_emails = [(e or "").strip() for e in (params.emails or [])]
    emails = [e for e in raw_emails if e]
    if not emails:
        raise HTTPException(status_code=400, detail="emails 不能为空")

    # 去重,保留首次出现顺序
    seen = set()
    dedup = []
    for e in emails:
        low = e.lower()
        if low in seen:
            continue
        seen.add(low)
        dedup.append(e)
    emails = dedup

    main_emails = [e for e in emails if _is_main_account_email(e)]
    if main_emails:
        raise HTTPException(status_code=400, detail=f"主号不允许删除: {main_emails}")

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再批量删除"))

    def _run():
        from autoteam.accounts import STATUS_AUTH_INVALID, STATUS_PERSONAL

        accounts = load_accounts()
        existing = {(a.get("email") or "").lower(): a for a in accounts}

        # SPEC-2 §3.5.2 + Round 6 PRD-5 FR-P1.4:批量删除若整批都是 personal/auth_invalid,
        # 整批跳过 ChatGPTTeamAPI 启动 — 这两类不需要 remote_state(成员/邀请),纯本地清理。
        # 关键:bool(targets_in_pool) 守卫空 list,避免 all([]) == True 误判;
        # 同时如果传入 emails 全部不存在(existing 没匹配),也不应短路(走原 chatgpt_api 路径
        # 让循环里给每条返回"账号不存在")。
        targets_in_pool = [
            existing[e.lower()]
            for e in emails
            if e.lower() in existing
        ]
        all_local_only = bool(targets_in_pool) and all(
            (a.get("status") in (STATUS_PERSONAL, STATUS_AUTH_INVALID))
            for a in targets_in_pool
        )

        chatgpt_api = None
        mail_client = None
        results = []
        try:
            remote_state = None
            if not all_local_only:
                # 至少一个号需要 Team 远端同步 — 启动共享 ChatGPTTeamAPI / mail_client
                chatgpt_api = ChatGPTTeamAPI()
                chatgpt_api.start()
                mail_client = CloudMailClient()
                mail_client.login()
                # 整批共享一次 Team 状态快照,避免每个删除都重查一次
                remote_state = fetch_team_state(chatgpt_api)
            else:
                # 全 personal/auth_invalid 路径:cloudmail 仍可能要清理(personal 有 cloudmail_account_id),
                # 但 mail_client 由 delete_managed_account 内部按需懒加载(own_mail_client 路径),
                # 这里不预启动 ChatGPTTeamAPI。
                logger.info(
                    "[批量删除] 整批 %d 个账号均为 personal/auth_invalid,跳过 ChatGPTTeamAPI 启动(FR-P1.4 短路)",
                    len(targets_in_pool),
                )

            for email in emails:
                if email.lower() not in existing:
                    results.append({"email": email, "ok": False, "error": "账号不存在"})
                    if not params.continue_on_error:
                        break
                    continue
                try:
                    cleanup = delete_managed_account(
                        email,
                        chatgpt_api=chatgpt_api,        # all_local_only=True 时为 None
                        mail_client=mail_client,        # 同上,delete_managed_account 内部懒加载
                        remote_state=remote_state,      # 同上
                        sync_cpa_after=False,            # 整批结束后统一同步
                    )
                    results.append({"email": email, "ok": True, "cleanup": cleanup})
                except Exception as exc:
                    logger.error("[批量删除] %s 失败: %s", email, exc)
                    results.append({"email": email, "ok": False, "error": str(exc)})
                    if not params.continue_on_error:
                        break
        finally:
            if chatgpt_api:
                try:
                    chatgpt_api.stop()
                except Exception as exc:
                    logger.debug("[批量删除] 关闭 chatgpt_api 异常: %s", exc)
            try:
                sync_to_cpa()
            except Exception as exc:
                logger.warning("[批量删除] 结尾 sync_to_cpa 失败: %s", exc)

        ok_count = sum(1 for r in results if r["ok"])
        return {
            "results": results,
            "summary": {
                "total": len(emails),
                "ok": ok_count,
                "failed": len(results) - ok_count,
                "skipped": len(emails) - len(results),
            },
        }

    try:
        # 每个账号平均 30s (拉取 team 状态 + kick + delete cloudmail),再给 120s 兜底余量。
        # 若仍超时会抛 TimeoutError,worker 线程会在后台继续跑完,但锁会释放 → 用户可以再提。
        timeout = max(300, 30 * len(emails) + 120)
        return _pw_executor.run_with_timeout(timeout, _run)
    finally:
        _playwright_lock.release()


# ---------------------------------------------------------------------------
# Round 11 — 子号实时探活 + 模型列表(用户 Q3 痛点)
# ---------------------------------------------------------------------------


class ProbeAccountParams(BaseModel):
    """`/api/accounts/{email}/probe` 请求体。"""

    force_codex_smoke: bool = True


@app.post("/api/accounts/{email}/probe")
def post_account_probe(email: str, params: ProbeAccountParams = ProbeAccountParams()):
    """Round 11 AC5 — 子号实时探活:并行 check_codex_quota + cheap_codex_smoke。



    用 access_token 直接探,不进队列,不抢 Playwright 锁(纯 HTTP 请求)。

    落 last_quota_check_at + last_quota,返回 status_before / status_after。

    """
    from autoteam.accounts import find_account, load_accounts, update_account
    from autoteam.codex_auth import cheap_codex_smoke, check_codex_quota

    email = email.strip().lower()
    if _is_main_account_email(email):
        raise HTTPException(status_code=400, detail="主号不属于子号探活对象,请用 /api/admin/master-health")

    accounts = load_accounts()
    acc = find_account(accounts, email)
    if not acc:
        raise HTTPException(status_code=404, detail="账号不存在")

    auth_file = acc.get("auth_file")
    if not auth_file or not Path(auth_file).exists():
        raise HTTPException(status_code=422, detail={
            "error": "auth_file_missing",
            "message": "账号无可用 auth_file,无法探活",
        })

    try:
        auth_data = json.loads(Path(auth_file).read_text(encoding="utf-8"))
    except Exception as exc:
        raise HTTPException(status_code=422, detail={
            "error": "auth_file_unreadable",
            "message": f"auth_file 解析失败: {type(exc).__name__}",
        })

    access_token = auth_data.get("access_token") or (auth_data.get("tokens") or {}).get("access_token")
    if not access_token:
        raise HTTPException(status_code=422, detail={
            "error": "access_token_missing",
            "message": "auth_file 中缺 access_token,无法探活",
        })

    account_id = (
        auth_data.get("account_id")
        or (auth_data.get("tokens") or {}).get("account_id")
        or acc.get("workspace_account_id")
    )

    status_before = acc.get("status")

    # 并行不强求,顺序也行 — quota 后跑 smoke
    try:
        quota_status, quota_info = check_codex_quota(access_token, account_id=account_id)
    except Exception as exc:
        logger.warning("[probe %s] check_codex_quota 异常: %s", email, exc)
        quota_status, quota_info = "network_error", None

    try:
        smoke_result, smoke_detail = cheap_codex_smoke(
            access_token,
            account_id=account_id,
            force=bool(params.force_codex_smoke),
        )
    except Exception as exc:
        logger.warning("[probe %s] cheap_codex_smoke 异常: %s", email, exc)
        smoke_result, smoke_detail = "uncertain", f"exception:{type(exc).__name__}"

    # 落 last_quota_check_at;quota_info 是 quota 快照才落
    update_fields = {"last_quota_check_at": time.time()}
    if quota_status == "ok" and isinstance(quota_info, dict):
        update_fields["last_quota"] = quota_info
    try:
        update_account(email, **update_fields)
    except Exception as exc:
        logger.warning("[probe %s] update_account 失败: %s", email, exc)

    # 重新读最新状态
    accounts2 = load_accounts()
    acc2 = find_account(accounts2, email) or acc

    return {
        "email": email,
        "status_before": status_before,
        "status_after": acc2.get("status"),
        "quota_status": quota_status,
        "quota_info": quota_info if isinstance(quota_info, dict) else None,
        "smoke_result": smoke_result,
        "smoke_detail": smoke_detail,
        "last_quota_check_at": update_fields["last_quota_check_at"],
    }


@app.get("/api/accounts/{email}/models")
def get_account_models(email: str):
    """Round 11 AC7 — 用 access_token 调 /backend-api/models 拿可用模型列表。



    返回 {email, plan_type, models: [{slug, name, description, ...}, ...]}

    401/403 → 401 + auth_invalid;timeout → 503;其他 → 502。

    """
    import requests

    from autoteam.accounts import find_account, load_accounts

    email = email.strip().lower()
    accounts = load_accounts()
    acc = find_account(accounts, email)
    if not acc:
        raise HTTPException(status_code=404, detail="账号不存在")

    auth_file = acc.get("auth_file")
    if not auth_file or not Path(auth_file).exists():
        raise HTTPException(status_code=422, detail={
            "error": "auth_file_missing",
            "message": "账号无可用 auth_file",
        })

    try:
        auth_data = json.loads(Path(auth_file).read_text(encoding="utf-8"))
    except Exception as exc:
        raise HTTPException(status_code=422, detail={
            "error": "auth_file_unreadable",
            "message": f"auth_file 解析失败: {type(exc).__name__}",
        })

    access_token = auth_data.get("access_token") or (auth_data.get("tokens") or {}).get("access_token")
    if not access_token:
        raise HTTPException(status_code=422, detail={
            "error": "access_token_missing",
        })

    account_id = (
        auth_data.get("account_id")
        or (auth_data.get("tokens") or {}).get("account_id")
        or acc.get("workspace_account_id")
    )

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

    try:
        resp = requests.get(
            "https://chatgpt.com/backend-api/models",
            headers=headers,
            timeout=10.0,
        )
    except requests.exceptions.Timeout:
        raise HTTPException(status_code=503, detail={"error": "timeout"})
    except requests.exceptions.RequestException as exc:
        raise HTTPException(status_code=502, detail={
            "error": "network_error",
            "message": f"{type(exc).__name__}",
        })

    if resp.status_code in (401, 403):
        raise HTTPException(status_code=401, detail={
            "error": "auth_invalid",
            "http_status": resp.status_code,
        })
    if resp.status_code != 200:
        raise HTTPException(status_code=502, detail={
            "error": f"upstream_status_{resp.status_code}",
            "body_preview": (resp.text or "")[:200],
        })

    try:
        body = resp.json()
    except Exception as exc:
        raise HTTPException(status_code=502, detail={
            "error": "json_parse_error",
            "message": f"{type(exc).__name__}",
        })

    models = body.get("models") if isinstance(body, dict) else None
    if not isinstance(models, list):
        models = []

    plan_type = None
    if isinstance(body, dict):
        plan_type = body.get("plan_type") or body.get("category") or None

    return {
        "email": email,
        "plan_type": plan_type,
        "models": models,
        "raw_keys": list(body.keys()) if isinstance(body, dict) else [],
    }


@app.post("/api/accounts/{email}/kick")
def post_kick_account(email: str):
    """将账号从 Team 中移出,状态变为 standby"""
    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再操作"))

    try:
        from autoteam.accounts import find_account, load_accounts, update_account
        from autoteam.manager import remove_from_team

        email = email.strip().lower()
        if _is_main_account_email(email):
            raise HTTPException(status_code=400, detail="主号不允许移出 Team")
        accounts = load_accounts()
        acc = find_account(accounts, email)
        if not acc:
            raise HTTPException(status_code=404, detail="账号不存在")
        if acc["status"] != "active":
            raise HTTPException(status_code=400, detail=f"账号状态为 {acc['status']},不是 active")

        def _do_kick():
            from autoteam.chatgpt_api import ChatGPTTeamAPI

            chatgpt = ChatGPTTeamAPI()
            try:
                chatgpt.start()
                return remove_from_team(chatgpt, email)
            finally:
                chatgpt.stop()

        ok = _pw_executor.run(_do_kick)
        if ok:
            update_account(email, status="standby")
            return {"message": f"已将 {email} 移出 Team", "email": email, "status": "standby"}
        raise HTTPException(status_code=500, detail=f"移出 {email} 失败")
    finally:
        _playwright_lock.release()


class LoginAccountParams(BaseModel):
    email: str


@app.post("/api/accounts/login", status_code=202)
def post_account_login(params: LoginAccountParams):
    """触发单个账号的 Codex 登录(后台执行)"""
    from autoteam.accounts import find_account, load_accounts

    email = params.email.strip().lower()
    if _is_main_account_email(email):
        raise HTTPException(status_code=400, detail="主号不属于账号池登录对象")
    accounts = load_accounts()
    acc = find_account(accounts, email)
    if not acc:
        raise HTTPException(status_code=404, detail="账号不存在")

    def _run():
        from autoteam.accounts import STATUS_ACTIVE, STATUS_PERSONAL, update_account
        from autoteam.cloudmail import CloudMailClient
        from autoteam.codex_auth import (
            check_codex_quota,
            login_codex_via_browser,
            quota_result_quota_info,
            quota_result_resets_at,
            save_auth_file,
        )
        from autoteam.invite import RegisterBlocked
        from autoteam.register_failures import record_failure

        # 账号状态决定登录模式:PERSONAL 走 use_personal=True 补个人号 OAuth;其他走 Team 模式
        use_personal = acc.get("status") == STATUS_PERSONAL

        mail_client = CloudMailClient()
        mail_client.login()
        # SPEC-2 §3.5.3 + Round 6 PRD-5 FR-P1.3:RegisterBlocked 转 409 phone_required / register_blocked
        # 注意:此函数在 _run_task 后台线程中运行,raise HTTPException 由 _run_task 转录到 task["error"];
        # task error 字符串携带 "phone_required" / "register_blocked" 关键字供前端解析(api.ts §SPEC-2 §1)
        try:
            bundle = login_codex_via_browser(
                email,
                acc.get("password", ""),
                mail_client=mail_client,
                use_personal=use_personal,
            )
        except RegisterBlocked as blocked:
            if blocked.is_phone:
                record_failure(
                    email,
                    category="oauth_phone_blocked",
                    reason=f"补登录触发 add-phone (step={blocked.step})",
                    step=blocked.step,
                    stage="api_login",
                )
                raise HTTPException(
                    status_code=409,
                    detail={
                        "error": "phone_required",
                        "step": blocked.step,
                        "reason": blocked.reason,
                    },
                )
            record_failure(
                email,
                category="exception",
                reason=f"补登录意外 RegisterBlocked: {blocked.reason}",
                step=blocked.step,
                stage="api_login",
            )
            raise HTTPException(
                status_code=409,
                detail={
                    "error": "register_blocked",
                    "step": blocked.step,
                    "reason": blocked.reason,
                },
            )
        if bundle:
            auth_file = save_auth_file(bundle)
            update_account(email, auth_file=auth_file, last_active_at=time.time())
            plan_type = (bundle.get("plan_type") or "").lower()

            if use_personal:
                # personal 补登录:不改状态(保持 PERSONAL),只刷新 auth_file
                update_account(email, status=STATUS_PERSONAL)
            elif plan_type == "team":
                from autoteam.admin_state import get_chatgpt_account_id

                update_account(email, status=STATUS_ACTIVE, workspace_account_id=get_chatgpt_account_id() or None)
                token = bundle.get("access_token")
                if token:
                    st, info = check_codex_quota(token)
                    if st == "ok" and isinstance(info, dict):
                        update_account(email, last_quota=info)
                    elif st == "exhausted":
                        quota_info = quota_result_quota_info(info)
                        if quota_info:
                            update_account(email, last_quota=quota_info)
                        update_account(
                            email,
                            status="exhausted",
                            quota_exhausted_at=time.time(),
                            quota_resets_at=quota_result_resets_at(info) or int(time.time() + 18000),
                        )
            # 同步到 CPA
            from autoteam.sync_targets import sync_to_configured_targets as sync_to_cpa

            sync_to_cpa()
            return {
                "email": email,
                "plan": bundle.get("plan_type"),
                "auth_file": auth_file,
                "mode": "personal" if use_personal else "team",
            }
        raise RuntimeError(f"Codex 登录失败: {email}")

    task = _start_task(f"login:{email}", _run, {"email": email})
    return task


@app.get("/api/status")
def get_status(fast: bool = False):
    """获取所有账号状态 + active 账号实时额度"""
    from autoteam.accounts import (
        STATUS_ACTIVE,
        STATUS_AUTH_INVALID,
        STATUS_EXHAUSTED,
        STATUS_ORPHAN,
        STATUS_PENDING,
        STATUS_PERSONAL,
        STATUS_STANDBY,
        is_account_disabled,
        load_accounts,
    )
    from autoteam.codex_auth import check_codex_quota, quota_result_quota_info

    accounts = load_accounts()
    quota_cache = {}

    if not fast:
        for acc in accounts:
            if not _is_main_account_email(acc.get("email")) and is_account_disabled(acc):
                continue
            if acc["status"] not in (STATUS_ACTIVE, STATUS_PERSONAL) and not _is_main_account_email(acc.get("email")):
                continue

            auth_file = _resolve_status_auth_file(acc)
            if not auth_file:
                continue

            try:
                auth_data = json.loads(read_text(Path(auth_file)))
                access_token = auth_data.get("access_token")
                if access_token:
                    status, info = check_codex_quota(access_token)
                    if status == "ok" and isinstance(info, dict):
                        quota_cache[acc["email"]] = info
                    elif status == "exhausted":
                        quota_info = quota_result_quota_info(info)
                        if quota_info:
                            quota_cache[acc["email"]] = quota_info
            except Exception:
                pass

    sanitized_accounts = [_sanitize_account(a, quota_cache.get(a.get("email"))) for a in accounts]

    summary = {
        "active": sum(1 for a in sanitized_accounts if a["status"] == STATUS_ACTIVE),
        "standby": sum(1 for a in sanitized_accounts if a["status"] == STATUS_STANDBY),
        "exhausted": sum(1 for a in sanitized_accounts if a["status"] == STATUS_EXHAUSTED),
        "pending": sum(1 for a in sanitized_accounts if a["status"] == STATUS_PENDING),
        "personal": sum(1 for a in sanitized_accounts if a["status"] == STATUS_PERSONAL),
        "auth_invalid": sum(1 for a in sanitized_accounts if a["status"] == STATUS_AUTH_INVALID),
        "orphan": sum(1 for a in sanitized_accounts if a["status"] == STATUS_ORPHAN),
        "disabled": sum(1 for a in sanitized_accounts if a["status"] == "disabled"),
        "total": len(sanitized_accounts),
    }

    return {
        "accounts": sanitized_accounts,
        "summary": summary,
        "quota_cache": quota_cache,
        "runtime_resources": _safe_runtime_resource_snapshot(),
        "ipv6_pool": _safe_ipv6_pool_status(),
        "cliproxy": _safe_cliproxy_health(timeout=0.5, cache_ttl=30.0) if fast else _safe_cliproxy_health(),
        "multi_master": _safe_multi_master_status(),
        "status_mode": {
            "fast": fast,
            "quota_budget_seconds": 0 if fast else None,
            "live_quota": not fast,
        },
        "rotation_validation": {
            **_rotation_validation_cooldown,
            "cooldown_remaining_seconds": int(_rotation_validation_cooldown_remaining()),
        },
    }


@app.post("/api/sync")
def post_sync():
    """同步认证文件到 CPA"""
    from autoteam.sync_targets import sync_to_configured_targets as sync_to_cpa

    sync_to_cpa()
    return {"message": "同步完成"}


@app.post("/api/sync/from-cpa")
def post_sync_from_cpa():
    """从 CPA 反向同步认证文件到本地。"""
    from autoteam.cpa_sync import sync_from_cpa

    result = sync_from_cpa()
    return {"message": "已从 CPA 同步到本地", "result": result}


@app.get("/api/register-failures")
def get_register_failures_api(limit: int = 50):
    """返回最近的注册/OAuth 失败明细,前端用来展示"为什么账号没生产出来"。"""
    from autoteam.register_failures import count_by_category, list_failures

    return {
        "items": list_failures(limit=max(1, min(limit, 500))),
        "counts": count_by_category(),
    }


@app.get("/api/config/register-domain")
def get_register_domain_api():
    """读取当前子号注册使用的 CloudMail 域名。"""
    from autoteam.config import CLOUDMAIL_DOMAIN
    from autoteam.runtime_config import get, get_register_domain

    override = (get("register_domain") or "").strip()
    return {
        "domain": get_register_domain(),
        "override": override,
        "env_default": (CLOUDMAIL_DOMAIN or "").lstrip("@").strip(),
    }


@app.put("/api/config/register-domain")
def put_register_domain_api(params: RegisterDomainParams):
    """

    更新子号注册域名。verify=True(默认)会试探性调用 CloudMail new_address 验证服务端是否接受此域,

    成功则立即删除探测地址再保存;失败把 CloudMail 原始错误透传给前端。



    SPEC-1 §FR-005 — 与 `/api/mail-provider/probe?step=domain_ownership` 共享回收语义:

    本路径走 `CloudMailClient` 抽象(已对齐 maillab/cf_temp_email),

    `probe.probe_domain_ownership` 走无状态 HTTP 直连,二者最终行为一致(创建 + 立即 DELETE,

    回收失败 leaked_probe 透传)。改 probe 时需同步检查本函数。

    """
    from autoteam.cloudmail import CloudMailClient
    from autoteam.runtime_config import set_register_domain

    cleaned = (params.domain or "").strip().lstrip("@").strip()
    if not cleaned:
        raise HTTPException(status_code=400, detail="域名不能为空")

    leaked_probe = None
    if params.verify:
        probe_prefix = f"probe{int(time.time())}"
        acct_id = None
        probe_email = None
        try:
            client = CloudMailClient()
            client.login()
            acct_id, probe_email = client.create_temp_email(prefix=probe_prefix, domain=cleaned)
        except Exception as exc:
            # CloudMail 返回 "Invalid domain" 等错误直接透传
            raise HTTPException(status_code=400, detail=f"域名验证失败: {exc}") from exc
        # 探测地址用完立即回收;删除失败也要让前端看到,否则 CloudMail 会积压僵尸地址
        try:
            if acct_id is not None:
                client.delete_account(acct_id)
        except Exception as exc:
            logger.warning("[config] 删除域名探测邮箱失败 (%s, id=%s): %s", probe_email, acct_id, exc)
            leaked_probe = {"email": probe_email, "acct_id": acct_id, "error": str(exc)}

    set_register_domain(cleaned)
    logger.info("[config] register_domain 已切换为 @%s", cleaned)
    resp = {"message": f"注册域名已切换为 @{cleaned}", "domain": cleaned}
    if leaked_probe:
        resp["warning"] = (
            f"域名已保存,但探测邮箱 {leaked_probe['email']} 回收失败,请手动在 CloudMail 删除"
            f" (id={leaked_probe['acct_id']}): {leaked_probe['error']}"
        )
        resp["leaked_probe"] = leaked_probe
    return resp


@app.get("/api/config/preferred-seat-type")
def get_preferred_seat_type_api():
    """SPEC-2 FR-G — 读取邀请席位偏好。"""
    from autoteam.runtime_config import get_preferred_seat_type
    return {"value": get_preferred_seat_type()}


@app.put("/api/config/preferred-seat-type")
def put_preferred_seat_type_api(params: PreferredSeatTypeParams):
    """SPEC-2 FR-G — 切换邀请席位偏好(default | codex)。"""
    from autoteam.runtime_config import set_preferred_seat_type
    val = (params.value or "").strip().lower()
    if val not in ("default", "codex"):
        raise HTTPException(status_code=400, detail="value 必须为 'default' 或 'codex'")
    saved = set_preferred_seat_type(val)
    logger.info("[config] preferred_seat_type 已切换为 %s", saved)
    return {"value": saved, "message": f"邀请席位偏好已设为 {saved}"}


@app.get("/api/config/sync-probe")
def get_sync_probe_api():
    """SPEC-2 FR-E — 读取 sync_account_states 被踢探测的并发/冷却配置。"""
    from autoteam.runtime_config import get_sync_probe_concurrency, get_sync_probe_cooldown_minutes
    return {
        "concurrency": get_sync_probe_concurrency(),
        "cooldown_minutes": get_sync_probe_cooldown_minutes(),
    }


@app.put("/api/config/sync-probe")
def put_sync_probe_api(params: SyncProbeParams):
    """SPEC-2 FR-E — 更新 sync_account_states 被踢探测的并发/冷却(任一非空字段都生效)。"""
    from autoteam.runtime_config import (
        get_sync_probe_concurrency,
        get_sync_probe_cooldown_minutes,
        set_sync_probe_concurrency,
        set_sync_probe_cooldown_minutes,
    )
    if params.concurrency is not None:
        set_sync_probe_concurrency(params.concurrency)
    if params.cooldown_minutes is not None:
        set_sync_probe_cooldown_minutes(params.cooldown_minutes)
    return {
        "concurrency": get_sync_probe_concurrency(),
        "cooldown_minutes": get_sync_probe_cooldown_minutes(),
        "message": "sync 探测配置已更新",
    }


@app.post("/api/sync/accounts")
def post_sync_accounts():
    """从 auths 目录和 Team 成员同步账号到 accounts.json"""
    from autoteam.manager import sync_account_states

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再同步"))

    try:
        _pw_executor.run(sync_account_states)
    finally:
        _playwright_lock.release()

    from autoteam.accounts import load_accounts

    accounts = load_accounts()
    return {"message": f"同步完成,共 {len(accounts)} 个账号", "total": len(accounts)}


@app.get("/api/team/members")
def get_team_members():
    """获取 Team 全部成员(包括手动添加的外部成员)"""
    from autoteam.admin_state import get_admin_session_token, get_chatgpt_account_id

    if not get_admin_session_token() or not get_chatgpt_account_id():
        raise HTTPException(status_code=400, detail="请先完成管理员登录")

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再查询"))

    try:

        def _fetch_team_members():
            from autoteam.account_ops import (
                fetch_team_state,
                team_invite_email,
                team_member_email,
                team_member_role,
                team_member_user_id,
            )
            from autoteam.accounts import load_accounts
            from autoteam.chatgpt_api import ChatGPTTeamAPI

            chatgpt = ChatGPTTeamAPI()
            try:
                chatgpt.start()
                members, invites = fetch_team_state(chatgpt)
                local_emails = {a["email"].lower() for a in load_accounts()}

                result = []
                for m in members:
                    email = team_member_email(m)
                    result.append(
                        {
                            "email": email,
                            "role": team_member_role(m) or "",
                            "user_id": team_member_user_id(m) or "",
                            "is_local": email in local_emails,
                            "type": "member",
                        }
                    )
                for inv in invites:
                    email = team_invite_email(inv)
                    result.append(
                        {
                            "email": email,
                            "role": inv.get("role", ""),
                            "user_id": inv.get("id", ""),
                            "is_local": email in local_emails,
                            "type": "invite",
                        }
                    )
                return {"members": result, "total": len(members), "invites": len(invites)}
            finally:
                chatgpt.stop()

        try:
            return _pw_executor.run(_fetch_team_members)
        except HTTPException:
            raise
        except Exception as exc:
            raise HTTPException(status_code=502, detail=str(exc)) from exc
    finally:
        _playwright_lock.release()


@app.post("/api/team/members/remove")
def post_team_member_remove(params: TeamMemberRemoveParams):
    """移出 Team 成员或取消邀请。"""
    from autoteam.admin_state import get_admin_session_token, get_chatgpt_account_id

    if not get_admin_session_token() or not get_chatgpt_account_id():
        raise HTTPException(status_code=400, detail="请先完成管理员登录")

    if not _playwright_lock.acquire(blocking=False):
        raise HTTPException(status_code=409, detail=_current_busy_detail("有任务正在执行,请等待完成后再操作"))

    try:
        from autoteam.accounts import find_account, load_accounts, update_account

        email = params.email.strip().lower()
        user_id = params.user_id.strip()
        member_type = params.type.strip().lower()

        if not email or not user_id:
            raise HTTPException(status_code=400, detail="缺少必要参数")
        if _is_main_account_email(email):
            raise HTTPException(status_code=400, detail="主号不允许从 Team 成员页移出")
        if member_type not in ("member", "invite"):
            raise HTTPException(status_code=400, detail="无效的成员类型")

        account_id = get_chatgpt_account_id()

        def _do_remove_team_member():
            from autoteam.chatgpt_api import ChatGPTTeamAPI

            chatgpt = ChatGPTTeamAPI()
            try:
                chatgpt.start()
                if member_type == "invite":
                    path = f"/backend-api/accounts/{account_id}/invites/{user_id}"
                    action_text = "取消邀请"
                else:
                    path = f"/backend-api/accounts/{account_id}/users/{user_id}"
                    action_text = "移出 Team"

                result = chatgpt._api_fetch("DELETE", path)
                return result, action_text
            finally:
                chatgpt.stop()

        result, action_text = _pw_executor.run(_do_remove_team_member)
        if result["status"] not in (200, 204):
            raise HTTPException(status_code=500, detail=f"{action_text}失败: HTTP {result['status']}")

        accounts = load_accounts()
        acc = find_account(accounts, email)
        if acc:
            update_account(email, status="standby")

        return {
            "message": f"已{action_text}: {email}",
            "email": email,
            "type": member_type,
        }
    finally:
        _playwright_lock.release()


# ---------------------------------------------------------------------------
# 日志收集
# ---------------------------------------------------------------------------

_log_buffer: list[dict] = []
_LOG_BUFFER_MAX = 500


class _LogCollector(logging.Handler):
    """收集日志到内存 buffer,供前端查询"""

    def emit(self, record):
        entry = {
            "time": record.created,
            "level": record.levelname,
            "message": self.format(record),
        }
        _log_buffer.append(entry)
        if len(_log_buffer) > _LOG_BUFFER_MAX:
            del _log_buffer[: len(_log_buffer) - _LOG_BUFFER_MAX]


_log_collector = _LogCollector()
_log_collector.setFormatter(logging.Formatter("%(message)s"))
logging.getLogger().addHandler(_log_collector)


@app.get("/api/logs")
def get_logs(limit: int = 100, since: float = 0):
    """获取最近的日志"""
    if since > 0:
        entries = [e for e in _log_buffer if e["time"] > since]
    else:
        entries = _log_buffer[-limit:]
    return {"logs": entries, "total": len(_log_buffer)}


@app.post("/api/sync/main-codex")
def post_sync_main_codex():
    """兼容旧接口:开始主号 Codex 登录并同步到已启用远端目标。"""
    return post_main_codex_start()


@app.get("/api/cpa/files")
def get_cpa_files():
    """获取 CPA 中的认证文件列表"""
    from autoteam.cpa_sync import list_cpa_files

    return list_cpa_files()


# ---------------------------------------------------------------------------
# 后台任务端点
# ---------------------------------------------------------------------------


class CheckParams(BaseModel):
    include_standby: bool = False  # True 时额外探测 standby 池(限速+24h 去重)


@app.post("/api/tasks/check", status_code=202)
def post_check(params: CheckParams = CheckParams()):
    """检查所有 active 账号额度(后台执行)。include_standby=True 时追加探测 standby 池。"""
    from autoteam.manager import cmd_check

    include_standby = bool(params.include_standby)

    def _run():
        exhausted = cmd_check(include_standby=include_standby)
        return {"exhausted": [a["email"] for a in exhausted]}

    task = _start_task("check", _run, {"include_standby": include_standby})
    return task


@app.post("/api/tasks/rotate", status_code=202)
def post_rotate(params: TaskParams = TaskParams()):
    """智能轮转(后台执行)"""
    from autoteam.manager import cmd_rotate

    task = _start_task(
        "rotate",
        lambda target: cmd_rotate(target, force_auth_repair=True, background_post_sync=True),
        {"target": params.target},
        params.target,
    )
    return task


class ReplaceParams(BaseModel):
    email: str
    reason: str = "manual"


@app.post("/api/tasks/replace", status_code=202)
def post_replace(params: ReplaceParams):
    """定点替换一个 Team 子号:kick + 补一个(标准行为:优先 standby 复用,否则新号)。



    失效一个立即轮换一个的手动触发入口,也可由 auto-check 自动调用 cmd_replace_batch。

    """
    from autoteam.manager import cmd_replace_one

    email = (params.email or "").strip()
    if not email:
        raise HTTPException(status_code=400, detail="email 不能为空")
    task = _start_task(
        "replace",
        cmd_replace_one,
        {"email": email, "reason": params.reason},
        email,
        params.reason,
    )
    return task


@app.post("/api/tasks/add", status_code=202)
def post_add():
    """添加新账号(后台执行)"""
    from autoteam.manager import cmd_add

    task = _start_task("add", cmd_add, {})
    return task


@app.post("/api/tasks/oauth-fill", status_code=202)
def post_oauth_fill(params: TaskParams = TaskParams()):
    """OAuth-first 生产个人免费号(后台执行)。



    不依赖 Team / 母号 admin 会话:直接在 auth.openai.com OAuth 流程内建号

    (邮箱→设密码→邮箱 OTP→姓名/生日→consent),token 回写 CPA。

    target = 期望可用个人免费号数量(幂等,补差额)。

    """
    from autoteam.manager import cmd_oauth_fill

    return _start_task(
        "oauth-fill",
        lambda target: cmd_oauth_fill(target),
        {"target": params.target},
        params.target,
    )


@app.post("/api/tasks/fill", status_code=202)
def post_fill(params: TaskParams = TaskParams()):
    """补满 Team 成员(后台执行)。leave_workspace=True 时切换为"生产免费号"模式



    fill-personal 模式下额外做一次轻量预检:Team 子号已满 TEAM_SUB_ACCOUNT_HARD_CAP

    则直接返回 409,不启动后台任务(队列化拒绝,Solution C)。本地状态足够用,无需启动

    Playwright 远程查询,避免给前端按错按钮带来额外开销。

    """
    from autoteam.manager import (
        TEAM_SUB_ACCOUNT_HARD_CAP,
        _count_local_team_seat_accounts,
        _schedule_post_task_sync,
        cmd_fill,
    )

    if params.leave_workspace:
        from autoteam.accounts import load_accounts

        in_team_local = _count_local_team_seat_accounts(load_accounts())
        if in_team_local >= TEAM_SUB_ACCOUNT_HARD_CAP:
            raise HTTPException(
                status_code=409,
                detail=(
                    f"Team 子号已满 {in_team_local}/{TEAM_SUB_ACCOUNT_HARD_CAP},"
                    "fill-personal 拒绝执行。请先等子号自然 exhausted 或手动腾位置后再试"
                ),
            )

    # Round 8 M-T3 + Round 9 AC-B4 — fill 任务起点统一 master probe,Team / Personal 都 fail-fast。
    # cancel 状态下整批 fill 必拿 plan_type=team,直接 503 fail-fast 不启动 task。
    try:
        from autoteam.chatgpt_api import ChatGPTTeamAPI
        from autoteam.master_health import is_master_subscription_healthy

        def _probe_master():
            api = ChatGPTTeamAPI()
            try:
                api.start()
                return is_master_subscription_healthy(api)
            finally:
                try:
                    api.stop()
                except Exception:
                    pass

        if _playwright_lock.acquire(blocking=False):
            try:
                healthy, reason, evidence = _pw_executor.run(_probe_master)
            finally:
                _playwright_lock.release()
            if not healthy and reason == "subscription_cancelled":
                msg = (
                    "母号 ChatGPT Team 订阅已 cancel(eligible_for_auto_reactivation=true),"
                    "fill 必拿 plan_type=team / free。请先续订或更换母号"
                )
                raise HTTPException(
                    status_code=503,
                    detail={
                        "error": "master_subscription_degraded",
                        "reason": reason,
                        "evidence": evidence,
                        "message": msg,
                        "leave_workspace": params.leave_workspace,
                    },
                )
        else:
            # playwright 锁拿不到 → 别阻塞 fill,放行让任务自身的 M-T1 / M-T2 兜底
            pass
    except HTTPException:
        raise
    except Exception:
        # probe 异常按 spec §6.1 不阻塞 — M-T1 / M-T2 在 _run_post_register_oauth 兜底
        pass

    def _cmd_fill_api_task(target: int, *, leave_workspace: bool = False):
        if leave_workspace:
            return cmd_fill(target, leave_workspace=True)
        result = cmd_fill(target, leave_workspace=False, post_sync=False, print_status=False)
        _schedule_post_task_sync("[填充]")
        return result

    command = "fill-personal" if params.leave_workspace else "fill"
    task = _start_task(
        command,
        _cmd_fill_api_task,
        {"target": params.target, "leave_workspace": params.leave_workspace},
        params.target,
        leave_workspace=params.leave_workspace,
    )
    return task


@app.post("/api/tasks/multi-master/fill", status_code=202)
def post_multi_master_fill(params: MultiMasterFillParams = MultiMasterFillParams()):
    """多母号并行补齐 Team 子号。"""
    from autoteam.multi_master import run_multi_master_fill

    payload = params.model_dump()

    def _run():
        return run_multi_master_fill(
            target_seats=params.target,
            owner_workers=params.owner_workers,
            direct_parallel=params.direct_parallel,
            workspace_ids=params.workspace_ids,
            dry_run=params.dry_run,
        )

    if params.dry_run:
        return {
            "task_id": None,
            "command": "multi-master-fill",
            "status": "completed",
            "params": payload,
            "result": _run(),
        }
    task = _start_task(
        "multi-master-fill",
        _run,
        payload,
    )
    return task


@app.post("/api/tasks/cleanup", status_code=202)
def post_cleanup(params: CleanupParams = CleanupParams()):
    """清理多余成员(后台执行)"""
    from autoteam.manager import cmd_cleanup

    task = _start_task("cleanup", cmd_cleanup, {"max_seats": params.max_seats}, params.max_seats)
    return task


@app.post("/api/tasks/reset-quota", status_code=202)
def post_reset_quota():
    """清空本地额度恢复记录,并恢复 exhausted 账号为可检查状态(后台执行)"""
    from autoteam.manager import cmd_reset_quota_recovery

    task = _start_task("reset-quota", cmd_reset_quota_recovery, {})
    return task


@app.get("/api/tasks")
def get_tasks():
    """查看所有任务"""
    sorted_tasks = sorted(_tasks.values(), key=lambda t: t["created_at"], reverse=True)
    return sorted_tasks


@app.get("/api/tasks/{task_id}")
def get_task(task_id: str):
    """查看任务状态"""
    task = _tasks.get(task_id)
    if not task:
        raise HTTPException(status_code=404, detail="任务不存在")
    return task


@app.post("/api/tasks/cancel", status_code=202)
def post_task_cancel():
    """

    请求当前正在运行的任务在下一个安全点退出。

    协作式:后台 worker 在每个批次/账号边界检查 cancel_signal.is_cancelled(),

    调用这里后等 10-30s 让当前步骤跑完,任务状态会在 task["status"] 里显示为 "cancelled"。

    """
    from autoteam import cancel_signal

    if not _current_task_id:
        raise HTTPException(status_code=404, detail="当前没有正在运行的任务")
    task = _tasks.get(_current_task_id) or {}
    if task.get("status") not in ("running", "pending"):
        raise HTTPException(status_code=400, detail=f"任务当前状态 {task.get('status')} 无法取消")
    cancel_signal.request_cancel(f"手动停止 task={_current_task_id[:8]}")
    task["cancel_requested"] = True
    return {
        "message": "已请求中止,等待当前步骤安全退出",
        "task_id": _current_task_id,
        "command": task.get("command"),
    }


# ---------------------------------------------------------------------------
# Round 12 F2 — SSE: rotate 实时进度推送
# ---------------------------------------------------------------------------
# 订阅 S1 commit ef1637c 的事件总线 (default_machine.subscribe),把每次状态
# 转移序列化为 SSE event 行 (data: {...}\n\n)。心跳 15s 一次 (": heartbeat\n\n")
# 维持 EventSource 连接、绕过 proxy idle timeout。
#
# 设计要点:
# - subscribe 回调在调用线程上同步执行 (S1 实现);用线程安全 SimpleQueue 把事件
#   中转给 sync generator,避免 generator 阻塞而 callback 被持续触发导致 OOM。
# - 客户端断开 (浏览器 close / refresh / 进程退出) 时 FastAPI 关闭 generator,
#   try/finally 块负责 unsubscribe 防泄漏。
# - generator 是 sync 而非 async,FastAPI 用 anyio threadpool 跑,uvicorn 不会被阻塞。
# - 每条 event payload schema (与 account_state.Transition.to_jsonl 对齐):
#     {"email":..., "from":..., "to":..., "reason":..., "ts":..., "extra":{...}}
# - 没有 manager.py 改动 (本任务范围外);S3/S4 任务把 transition 调用塞进
#   manager.py 的 rotate 路径,本端点会自动看到事件。

import queue as _sse_queue


def _build_sse_event_stream(machine, *, heartbeat_seconds: float = 15.0):
    """Return a sync generator yielding SSE-formatted bytes from machine subscriber.



    Pulled out of the route so unit tests can drive it without spinning up

    Starlette/uvicorn — see ``tests/unit/test_round12_rotate_sse_stream.py``.

    """
    q: _sse_queue.Queue = _sse_queue.Queue(maxsize=1024)
    _SENTINEL = object()

    def _on_transition(transition):
        try:
            payload = {
                "email": transition.email,
                "from": transition.from_state.value if transition.from_state else None,
                "to": transition.to_state.value,
                "reason": transition.reason or "",
                "ts": transition.timestamp,
                "extra": dict(transition.extra or {}),
            }
            # Round 12 wire-up (minor m7) — bounded queue. If the slow SSE
            # client lets the buffer fill, drop oldest to make room rather
            # than growing unbounded memory.
            try:
                q.put_nowait(payload)
            except _sse_queue.Full:
                try:
                    q.get_nowait()  # drop oldest
                except _sse_queue.Empty:
                    pass
                try:
                    q.put_nowait(payload)
                except _sse_queue.Full:
                    logger.warning("[sse] queue still full after drop, event lost")
        except Exception:
            logger.exception("[sse] failed to enqueue transition %r", transition)

    machine.subscribe(_on_transition)

    def _generator():
        try:
            # 立刻发一次 retry 提示 + 心跳,让前端尽早确认连接成功
            yield b"retry: 5000\n\n"
            yield b": connected\n\n"
            while True:
                try:
                    payload = q.get(timeout=heartbeat_seconds)
                except _sse_queue.Empty:
                    yield b": heartbeat\n\n"
                    continue
                if payload is _SENTINEL:
                    break
                line = "data: " + json.dumps(payload, ensure_ascii=False) + "\n\n"
                yield line.encode("utf-8")
        finally:
            machine.unsubscribe(_on_transition)

    return _generator(), q, _on_transition


@app.get("/api/rotate/stream")
def get_rotate_stream(request: Request):
    """Server-Sent Events: 推送账号状态转移事件到前端 (rotate 实时进度面板)。



    Content-Type: text/event-stream

    Cache-Control: no-cache (代理不要缓存)

    X-Accel-Buffering: no (Nginx 不要缓冲,确保 chunk 及时落地浏览器)

    """
    from autoteam.account_state import default_machine

    generator, _q, _cb = _build_sse_event_stream(default_machine)

    return StreamingResponse(
        generator,
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "X-Accel-Buffering": "no",
            "Connection": "keep-alive",
        },
    )


# ---------------------------------------------------------------------------
# 后台自动巡检
# ---------------------------------------------------------------------------

from autoteam.config import (
    AUTO_CHECK_INTERVAL as _DEFAULT_INTERVAL,
)
from autoteam.config import (
    AUTO_CHECK_MIN_LOW as _DEFAULT_MIN_LOW,
)
from autoteam.config import (
    AUTO_CHECK_TARGET_SEATS as _DEFAULT_TARGET_SEATS,
)
from autoteam.config import (
    AUTO_CHECK_THRESHOLD as _DEFAULT_THRESHOLD,
)

# 运行时可修改的巡检配置
_auto_check_config = {
    "interval": _DEFAULT_INTERVAL,
    "target_seats": _DEFAULT_TARGET_SEATS,
    "threshold": _DEFAULT_THRESHOLD,
    "min_low": _DEFAULT_MIN_LOW,
}
_auto_check_stop = threading.Event()
_auto_check_restart = threading.Event()  # 配置变更时通知线程重启


def _resolve_auto_check_target_seats(cfg: dict[str, int | bool]) -> int:
    try:
        return max(1, min(3, int(cfg.get("target_seats", 3))))
    except Exception:
        return 3

# auto-fill watchdog 冷却:防止反复触发 cmd_rotate 导致 OpenAI 对短时间内
# 多次 invite/kick 的子号批量 revoke token。30 分钟内只触发一次,给 OpenAI
# 风控系统冷却时间。0 表示从未触发过。
_auto_fill_last_trigger_ts = 0.0
_AUTO_FILL_COOLDOWN_SECONDS = 1800  # 30 min


def _playwright_probe_command(*args: str) -> list[str]:
    return [sys.executable, "-m", "autoteam.playwright_probe", *args]


def _kill_subprocess_group(proc: subprocess.Popen) -> None:
    try:
        if os.name == "nt":
            subprocess.run(
                ["taskkill", "/F", "/T", "/PID", str(proc.pid)],
                capture_output=True,
                text=True,
                check=False,
            )
        else:
            os.killpg(proc.pid, signal.SIGKILL)
    except Exception:
        try:
            proc.kill()
        except Exception:
            pass


def _parse_playwright_probe_stdout(stdout: str) -> dict:
    text = (stdout or "").strip()
    if not text:
        return {}

    lines = [line.strip() for line in text.splitlines() if line.strip()]
    for line in reversed(lines):
        if not line.startswith(("{", "[")):
            continue
        try:
            parsed = json.loads(line)
        except json.JSONDecodeError:
            continue
        return parsed if isinstance(parsed, dict) else {"result": parsed}

    parsed = json.loads(text)
    return parsed if isinstance(parsed, dict) else {"result": parsed}


def _run_playwright_probe(*args: str, timeout_seconds: float = 30) -> dict:
    cmd = _playwright_probe_command(*args)
    env = os.environ.copy()
    env["AUTOTEAM_PROBE_MODE"] = "1"
    proc = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        env=env,
        start_new_session=True,
    )
    try:
        stdout, stderr = proc.communicate(timeout=max(1.0, float(timeout_seconds)))
    except subprocess.TimeoutExpired as exc:
        _kill_subprocess_group(proc)
        try:
            proc.communicate(timeout=1)
        except Exception:
            pass
        raise TimeoutError(f"Playwright probe timeout: {' '.join(args)}") from exc

    stdout = (stdout or "").strip()
    stderr = (stderr or "").strip()
    if proc.returncode != 0:
        detail = stderr or stdout or f"exit={proc.returncode}"
        raise RuntimeError(detail)

    return _parse_playwright_probe_stdout(stdout) if stdout else {}


class _TeamMemberCount(int):
    def __new__(cls, count: int, *, invites: int = 0, occupancy: int | None = None):
        obj = int.__new__(cls, count)
        obj.invites = max(0, int(invites or 0))
        obj.occupancy = int(occupancy) if occupancy is not None else int(count) + obj.invites
        return obj


def _team_member_invite_count(team_count) -> int:
    try:
        return max(0, int(getattr(team_count, "invites", 0) or 0))
    except (TypeError, ValueError):
        return 0


def _team_member_occupancy(team_count) -> int:
    try:
        count = int(team_count)
    except (TypeError, ValueError):
        return -1
    if count < 0:
        return -1
    try:
        occupancy = int(team_count.occupancy)
    except (AttributeError, TypeError, ValueError):
        occupancy = count + _team_member_invite_count(team_count)
    return max(count, occupancy)


def _auto_check_team_member_count(timeout_seconds: float = 30, retries: int = 2) -> int:
    """Return Team member count via a killable subprocess probe; -1 means unknown."""

    for attempt in range(1, max(1, retries) + 1):
        try:
            result = _run_playwright_probe("team-member-count", timeout_seconds=timeout_seconds)
        except TimeoutError:
            if attempt < retries:
                logger.warning(
                    "[巡检] Team 人数探针超时(>%ss),准备重试第 %d/%d 次",
                    timeout_seconds,
                    attempt + 1,
                    retries,
                )
                continue
            logger.warning("[巡检] Team 人数探针超时(>%ss,已重试 %d 次),本轮视为未知", timeout_seconds, retries)
            return -1
        except Exception as exc:
            logger.warning("[巡检] Team 人数探针失败: %s", exc)
            return -1

        try:
            count = int(result.get("count", -1))
            invites = int(result.get("invites", 0) or 0)
            occupancy = int(result.get("occupancy", count + invites if count >= 0 else -1))
            return _TeamMemberCount(count, invites=invites, occupancy=occupancy)
        except Exception:
            return -1


def _collect_cpa_credential_gate() -> dict:
    """Read-only CPA provider-auth gate for auto-check decisions."""
    try:
        from autoteam.sync_targets import SYNC_TARGET_CPA, is_sync_target_enabled

        enabled = is_sync_target_enabled(SYNC_TARGET_CPA)
    except Exception as exc:
        logger.warning("[巡检] CPA 可用凭证 gate 配置检查失败: %s", exc)
        return {
            "enabled": False,
            "safe_read_only": True,
            "management_ok": False,
            "available": None,
            "total": None,
            "reason": "config_check_failed",
            "zero_available": False,
            "error": str(exc),
        }

    if not enabled:
        return {
            "enabled": False,
            "safe_read_only": True,
            "management_ok": False,
            "available": None,
            "total": None,
            "reason": "cpa_sync_disabled",
            "zero_available": False,
        }

    try:
        from autoteam.cliproxy_health import get_cliproxy_health

        health = get_cliproxy_health(cache_ttl=0.0, force_refresh=True)
    except Exception as exc:
        logger.warning("[巡检] CPA 可用凭证 gate 只读检查失败: %s", exc)
        return {
            "enabled": True,
            "safe_read_only": True,
            "management_ok": False,
            "available": None,
            "total": None,
            "reason": "health_check_exception",
            "zero_available": False,
            "error": str(exc),
        }

    management_api = health.get("management_api") if isinstance(health, dict) else {}
    provider_auth = health.get("provider_auth") if isinstance(health, dict) else {}
    management_ok = bool(isinstance(management_api, dict) and management_api.get("ok"))
    safe_read_only = bool(isinstance(health, dict) and health.get("safe_read_only", True))
    if not isinstance(provider_auth, dict):
        provider_auth = {}

    try:
        available = int(provider_auth.get("available") or 0)
    except (TypeError, ValueError):
        available = 0
    try:
        total = int(provider_auth.get("total") or 0)
    except (TypeError, ValueError):
        total = 0

    reason = str(
        provider_auth.get("reason")
        or (management_api.get("reason") if isinstance(management_api, dict) else "")
        or "unknown"
    )
    return {
        "enabled": True,
        "safe_read_only": safe_read_only,
        "management_ok": management_ok,
        "available": available,
        "total": total,
        "reason": reason,
        "zero_available": safe_read_only and management_ok and available <= 0,
    }


def _cpa_provider_auth_below_pool_target(gate: dict | None, pool_target: int) -> bool:
    if not isinstance(gate, dict):
        return False
    if not gate.get("enabled") or not gate.get("safe_read_only") or not gate.get("management_ok"):
        return False
    try:
        available = int(gate.get("available"))
        target = max(1, int(pool_target))
    except (TypeError, ValueError):
        return False
    return available < target


def _auto_check_loop():
    """后台巡检线程:定期检查额度,多个账号低于阈值时自动轮转"""
    from autoteam.accounts import STATUS_ACTIVE, is_account_disabled, load_accounts
    from autoteam.codex_auth import check_codex_quota

    while not _auto_check_stop.is_set():
        cfg = _auto_check_config
        logger.info(
            "[巡检] 等待 %d 分钟后执行下一轮检查(阈值: %d%%, 模式: 任意失效立即 1v1 替换)",
            cfg["interval"] // 60,
            cfg["threshold"],
        )

        # 等待 interval 秒,期间可被 restart 或 stop 唤醒
        _auto_check_restart.clear()
        if _auto_check_stop.wait(cfg["interval"]):
            break
        if _auto_check_restart.is_set():
            continue  # 配置变更,跳到下一轮重新读取配置

        if _playwright_lock.locked() or _current_task_id:
            logger.info("[巡检] 有任务正在执行,跳过本轮自动巡检昂贵探测")
            continue

        try:
            cfg = _auto_check_config  # 重新读取
            target_seats = _resolve_auto_check_target_seats(cfg)
            sub_account_target = max(0, target_seats - 1)
            log_runtime_resource_snapshot(logger, label="auto-check")
            accounts = load_accounts()
            active = [
                a
                for a in accounts
                if a["status"] == STATUS_ACTIVE
                and not _is_main_account_email(a.get("email"))
                and not is_account_disabled(a)
                and a.get("auth_file")
                and Path(a["auth_file"]).exists()
            ]
            try:
                from autoteam.manager import _replaceable_pool_blocker_reason

                replaceable_blockers = [
                    {
                        "email": a.get("email"),
                        "reason": reason,
                    }
                    for a in accounts
                    if not _is_main_account_email(a.get("email"))
                    and not is_account_disabled(a)
                    and (reason := _replaceable_pool_blocker_reason(a))
                ]
            except Exception as exc:
                logger.warning("[巡检] 本地占席 blocker 分类失败: %s", exc)
                replaceable_blockers = []
            cpa_credential_gate = _collect_cpa_credential_gate()
            if cpa_credential_gate.get("enabled"):
                logger.info(
                    "[巡检] CPA 可用凭证 gate: management_ok=%s available=%s/%s reason=%s safe_read_only=%s",
                    cpa_credential_gate.get("management_ok"),
                    cpa_credential_gate.get("available"),
                    cpa_credential_gate.get("total"),
                    cpa_credential_gate.get("reason"),
                    cpa_credential_gate.get("safe_read_only"),
                )

            provider_auth_below_target = _cpa_provider_auth_below_pool_target(cpa_credential_gate, sub_account_target)
            actual_team_count = _auto_check_team_member_count(timeout_seconds=30, retries=2)
            actual_invite_count = _team_member_invite_count(actual_team_count)
            actual_team_occupancy = _team_member_occupancy(actual_team_count)

            if actual_team_occupancy > target_seats:
                if not _playwright_lock.acquire(blocking=False):
                    logger.info("[巡检] Team 远端占用超出目标,但有任务在跑,本轮跳过自动清理")
                    continue
                _playwright_lock.release()

                logger.warning(
                    "[巡检] Team 远端占用超出目标: members=%d invites=%d total=%d/%d,触发自动清理",
                    int(actual_team_count),
                    actual_invite_count,
                    actual_team_occupancy,
                    target_seats,
                )
                from autoteam.manager import cmd_cleanup

                try:
                    _start_task(
                        "auto-cleanup",
                        cmd_cleanup,
                        {
                            "max_seats": target_seats,
                            "trigger": "auto-check",
                            "team_count": int(actual_team_count),
                            "invite_count": actual_invite_count,
                            "team_occupancy": actual_team_occupancy,
                        },
                        target_seats,
                    )
                except Exception as e:
                    logger.error("[巡检] 自动清理失败: %s", e)
                continue

            # Watchdog:active 账号数 < 子号目标时自动补位。
            # 之前的 `if not active: continue` 在 active 全 kick 进 standby
            # 之后会让 Team 永远萎缩。但触发频率必须节制 —— OpenAI 对短时间内反复
            # invite/kick 同一批子号会 revoke token(token_revoked 错误),所以加
            # 30 分钟冷却,避免巡检每 5 分钟无脑触发 cmd_rotate 把账号全洗成废号。
            global _auto_fill_last_trigger_ts
            if len(active) < sub_account_target:
                now_ts = time.time()
                should_start_auto_fill = False
                cooldown_remaining = (_auto_fill_last_trigger_ts + _AUTO_FILL_COOLDOWN_SECONDS) - now_ts
                if cooldown_remaining > 0:
                    logger.info(
                        "[巡检] active=%d < %d,但 auto-fill 冷却中(还剩 %d 分钟)",
                        len(active),
                        sub_account_target,
                        int(cooldown_remaining / 60),
                    )
                    # 冷却期内仍然继续做"低额度替换"(下面的 low_accounts 逻辑),
                    # 只是不触发全量 cmd_rotate。例外:Team 真实人数不足时必须继续补位,
                    # 否则 cooldown 会把实际席位缺口拖到下一轮甚至 4h backoff 之后。
                    if actual_team_count >= target_seats:
                        if replaceable_blockers:
                            logger.warning(
                                "[巡检] auto-fill 冷却中但 Team 已满且存在 %d 个不可用占席子号,继续触发轮转: %s",
                                len(replaceable_blockers),
                                ", ".join(
                                    f"{item['email']}({item['reason']})" for item in replaceable_blockers[:5]
                                ),
                            )
                            should_start_auto_fill = True
                        elif cpa_credential_gate.get("zero_available"):
                            logger.warning(
                                "[巡检] auto-fill 冷却中但 Team 已满、本地 active=%d/%d,且 CPA 可用凭证为 0/%s,继续补位",
                                len(active),
                                sub_account_target,
                                cpa_credential_gate.get("total"),
                            )
                            should_start_auto_fill = True
                        elif provider_auth_below_target:
                            logger.warning(
                                "[巡检] auto-fill 冷却中但 CPA/CLIProxy provider-auth 可用凭证低水位: "
                                "available=%s/%d local_active=%d/%d,继续补位/同步",
                                cpa_credential_gate.get("available"),
                                sub_account_target,
                                len(active),
                                sub_account_target,
                            )
                            should_start_auto_fill = True
                        else:
                            logger.info(
                                "[巡检] auto-fill 冷却中且 Team 实际成员数=%d 已满;本轮只保留低额度替换检查",
                                actual_team_count,
                            )
                    elif actual_team_count >= 0:
                        logger.info(
                            "[巡检] auto-fill 冷却中但 Team 实际成员不足(%d/%d),继续补位",
                            actual_team_count,
                            target_seats,
                        )
                        should_start_auto_fill = True
                    else:
                        logger.info("[巡检] auto-fill 冷却中且 Team 实际成员数未知;本轮不触发全量补位")
                else:
                    # Round 11 — OAuth 连续失败 backoff:
                    # 最近 2 小时内 master workspace 累积 ≥3 个 auth_invalid 账号 → fill 已稳定失败,
                    # 延长有效冷却到 4 小时,避免每 30 分钟无脑循环浪费 cloudmail 邮箱 + 累积僵尸账号。
                    # 触发条件用 status=auth_invalid + workspace_account_id=master 简单可靠,
                    # 不依赖具体 OAuth 失败原因(根因可能是 ChatGPT consent 页面变化或 master 订阅问题)。
                    backoff_triggered = False
                    try:
                        from autoteam.accounts import STATUS_AUTH_INVALID
                        from autoteam.admin_state import get_chatgpt_account_id

                        master_aid = get_chatgpt_account_id() or ""
                        recent_window = 2 * 3600  # 2 小时
                        recent_failures = [
                            a for a in accounts
                            if a.get("status") == STATUS_AUTH_INVALID
                            and (a.get("workspace_account_id") or "") == master_aid
                            and (a.get("created_at") or 0) >= now_ts - recent_window
                        ]
                        if len(recent_failures) >= 3:
                            # 强制延长冷却,记 last_trigger_ts 让下次巡检也走 cooldown 分支
                            _auto_fill_last_trigger_ts = now_ts - _AUTO_FILL_COOLDOWN_SECONDS + 4 * 3600
                            logger.warning(
                                "[巡检] active=%d < %d 但近 2h 累积 %d 个 OAuth 失败账号 → "
                                "backoff 生效,延长冷却到 4h(避免无谓循环)。"
                                "请检查 codex_auth consent 页面或 master 订阅",
                                len(active),
                                sub_account_target,
                                len(recent_failures),
                            )
                            backoff_triggered = True
                    except Exception as exc:
                        logger.warning("[巡检] OAuth backoff 检查异常: %s,按原逻辑继续", exc)

                    if backoff_triggered:
                        continue

                    if actual_team_count >= target_seats:
                        if replaceable_blockers:
                            logger.warning(
                                "[巡检] Team 已满但本地 active=%d/%d 且存在不可用占席子号,触发 auto-fill 修复: %s",
                                len(active),
                                sub_account_target,
                                ", ".join(
                                    f"{item['email']}({item['reason']})" for item in replaceable_blockers[:5]
                                ),
                            )
                        elif cpa_credential_gate.get("zero_available"):
                            logger.warning(
                                "[巡检] Team 已满但本地 active=%d/%d,且 CPA 可用凭证为 0/%s,触发 auto-fill 补位",
                                len(active),
                                sub_account_target,
                                cpa_credential_gate.get("total"),
                            )
                        elif provider_auth_below_target:
                            logger.warning(
                                "[巡检] Team 已满且 CPA/CLIProxy provider-auth 可用凭证低水位: "
                                "available=%s/%d local_active=%d/%d,触发 auto-fill 补位/同步",
                                cpa_credential_gate.get("available"),
                                sub_account_target,
                                len(active),
                                sub_account_target,
                            )
                        else:
                            logger.info(
                                "[巡检] 本地 active=%d < %d,但 Team 实际成员数=%d 已满;先跳过 auto-fill,等待同步/对账稳定",
                                len(active),
                                sub_account_target,
                                actual_team_count,
                            )
                            continue
                    should_start_auto_fill = True

                if should_start_auto_fill:
                    if not _playwright_lock.acquire(blocking=False):
                        logger.info(
                            "[巡检] active=%d < %d 但有任务在跑,本轮先跳过自动补位",
                            len(active),
                            sub_account_target,
                        )
                        continue
                    _playwright_lock.release()

                    # oauth_first:个人免费号生产,不走 Team cmd_rotate(它需要母号 admin 会话,
                    # 且会把 personal 账号当成 Team 缺额死循环)。改用 Team-independent 的 cmd_oauth_fill。
                    from autoteam.manager import _runtime_new_account_mode

                    if _runtime_new_account_mode() == "oauth_first":
                        logger.warning(
                            "[巡检] active 账号 %d < %d,mode=oauth_first 触发 OAuth 建个人免费号补位",
                            len(active),
                            sub_account_target,
                        )
                        from autoteam.manager import cmd_oauth_fill

                        try:
                            _start_task(
                                "oauth-fill",
                                lambda target: cmd_oauth_fill(target),
                                {"target": sub_account_target, "trigger": "auto-check"},
                                sub_account_target,
                            )
                            _auto_fill_last_trigger_ts = now_ts
                        except Exception as e:
                            logger.error("[巡检] oauth-fill 启动失败: %s", e)
                        continue

                    logger.warning(
                        "[巡检] active 账号 %d < %d,触发 auto-fill(cmd_rotate 全流程补位)",
                        len(active),
                        sub_account_target,
                    )
                    from autoteam.manager import cmd_rotate

                    try:
                        task_params = {"target_seats": target_seats}
                        if cpa_credential_gate.get("enabled"):
                            try:
                                provider_available = int(cpa_credential_gate.get("available") or 0)
                            except (TypeError, ValueError):
                                provider_available = 0
                            task_params.update(
                                {
                                    "trigger": "auto-check",
                                    "shortage": max(
                                        0,
                                        sub_account_target - min(len(active), provider_available),
                                    ),
                                    "cpa_credential_gate": cpa_credential_gate,
                                    "provider_auth_below_target": provider_auth_below_target,
                                    "provider_auth_available": cpa_credential_gate.get("available"),
                                    "provider_auth_target": sub_account_target,
                                }
                            )
                        _start_task(
                            "auto-fill",
                            cmd_rotate,
                            task_params,
                            target_seats,
                            background_post_sync=True,
                        )
                        _auto_fill_last_trigger_ts = now_ts
                    except Exception as e:
                        logger.error("[巡检] auto-fill 启动失败: %s", e)
                    # 触发后本轮不再做"低额度替换",免得跟 cmd_rotate 抢锁
                    continue

            if not active:
                continue

            low_accounts = []
            for acc in active:
                try:
                    auth_data = json.loads(read_text(Path(acc["auth_file"])))
                    access_token = auth_data.get("access_token")
                    if not access_token:
                        continue
                    status, info = check_codex_quota(access_token)
                    if status == "ok" and isinstance(info, dict):
                        remaining = 100 - info.get("primary_pct", 0)
                        if remaining < cfg["threshold"]:
                            low_accounts.append((acc["email"], remaining))
                    elif status == "exhausted":
                        low_accounts.append((acc["email"], 0))
                except Exception:
                    pass

            if low_accounts:
                logger.info(
                    "[巡检] %d 个账号额度不足: %s", len(low_accounts), ", ".join(f"{e}({r}%)" for e, r in low_accounts)
                )

                # 有任务在跑则本轮跳过(下轮再替换,避免重复 kick)
                if not _playwright_lock.acquire(blocking=False):
                    logger.info("[巡检] 有任务正在执行,本轮跳过即时替换")
                    continue
                _playwright_lock.release()

                # 先标记 exhausted,cmd_check 入口的对账在此之后再看到就会补 kick(双保险)。
                # 必须同时写 quota_resets_at —— 否则 get_standby_accounts() 看到 None 就默认
                # _quota_recovered=True,导致后续 rotate/replace 立刻把这个 0% 账号当可复用号
                # 反复 reinvite 进 Team,席位来回洗同一批耗尽账号永远不换新鲜的。
                # 阈值默认 5h(18000s),与 check_codex_quota 无返回 resets_at 时的 fallback 一致。
                from autoteam.accounts import STATUS_EXHAUSTED, update_account

                now_ts = time.time()
                emails_to_replace = []
                for email, remaining in low_accounts:
                    logger.info("[巡检] %s 剩余 %d%%,立即替换", email, remaining)
                    update_account(
                        email,
                        status=STATUS_EXHAUSTED,
                        quota_exhausted_at=now_ts,
                        quota_resets_at=now_ts + 18000,
                    )
                    emails_to_replace.append(email)

                # 失效一个立即轮换一个:逐个 kick+补一个,不等凑 min_low 也不走全量 cmd_rotate。
                # min_low 字段保留作兼容(当前不参与判断),前端可继续配置但无语义效果。
                logger.info("[巡检] 触发即时替换 (%d 个)...", len(emails_to_replace))
                from autoteam.manager import cmd_replace_batch

                try:
                    _start_task(
                        "auto-replace",
                        cmd_replace_batch,
                        {"emails": emails_to_replace, "trigger": "auto-check"},
                        emails_to_replace,
                        "auto-check",
                    )
                except Exception as e:
                    logger.error("[巡检] 即时替换启动失败: %s", e)
            elif provider_auth_below_target and actual_team_count >= target_seats:
                try:
                    provider_available = int(cpa_credential_gate.get("available") or 0)
                except (TypeError, ValueError):
                    provider_available = 0
                shortage = max(1, sub_account_target - min(len(active), provider_available))

                if not _playwright_lock.acquire(blocking=False):
                    logger.info("[巡检] CPA/CLIProxy provider-auth 低水位,但有任务在跑,本轮跳过预防性轮转")
                    continue
                _playwright_lock.release()

                logger.warning(
                    "[巡检] CPA/CLIProxy provider-auth 可用凭证低水位: "
                    "available=%d/%d local_active=%d/%d; trigger preventive auto-rotate/sync",
                    provider_available,
                    sub_account_target,
                    len(active),
                    sub_account_target,
                )
                from autoteam.manager import cmd_rotate

                try:
                    _start_task(
                        "auto-rotate",
                        cmd_rotate,
                        {
                            "target": target_seats,
                            "trigger": "auto-check",
                            "shortage": shortage,
                            "low_accounts": 0,
                            "cpa_credential_gate": cpa_credential_gate,
                            "provider_auth_below_target": True,
                            "provider_auth_available": cpa_credential_gate.get("available"),
                            "provider_auth_target": sub_account_target,
                        },
                        target_seats,
                        background_post_sync=True,
                    )
                except Exception as e:
                    logger.error("[巡检] provider-auth 低水位自动轮转失败: %s", e)
            elif provider_auth_below_target:
                logger.info(
                    "[巡检] CPA/CLIProxy provider-auth 低水位,但 Team 实际成员数=%s 未满或未知,本轮不做预防性轮转",
                    actual_team_count if actual_team_count >= 0 else "unknown",
                )
            else:
                logger.info("[巡检] 额度正常,无需替换")

        except Exception as e:
            logger.error("[巡检] 巡检异常: %s", e)

        # Round 9 RT-2 — 后台巡检每 interval 跑一次 retroactive(走 5min cache,失败 warning)。
        # spec/shared/master-subscription-health.md v1.1 §11.3。
        try:
            from autoteam.master_health import _apply_master_degraded_classification

            retro = _apply_master_degraded_classification()
            if retro and (retro.get("marked_grace") or retro.get("marked_standby") or retro.get("reverted_active")):
                logger.info(
                    "[巡检] retroactive: GRACE %d / STANDBY %d / 撤回 ACTIVE %d",
                    len(retro.get("marked_grace") or []),
                    len(retro.get("marked_standby") or []),
                    len(retro.get("reverted_active") or []),
                )
        except Exception as exc:
            logger.warning("[巡检] retroactive helper 异常: %s", exc)


class AutoCheckConfig(BaseModel):
    interval: int = 300  # 巡检间隔(秒)
    target_seats: int = 3  # 自动巡检目标 Team seat 数,最多 1 母 + 2 子
    threshold: int = 10  # 额度阈值(%)
    min_low: int = 2  # 触发轮转的最少账号数


@app.get("/api/config/auto-check")
def get_auto_check_config():
    """获取巡检配置"""
    return _auto_check_config.copy()


@app.put("/api/config/auto-check")
def set_auto_check_config(cfg: AutoCheckConfig):
    """修改巡检配置(运行时生效)"""
    _auto_check_config["interval"] = max(60, cfg.interval)  # 最少 1 分钟
    _auto_check_config["target_seats"] = max(1, min(3, cfg.target_seats))
    _auto_check_config["threshold"] = max(1, min(100, cfg.threshold))
    _auto_check_config["min_low"] = max(1, cfg.min_low)
    _auto_check_restart.set()  # 唤醒巡检线程,立即应用新配置
    logger.info(
        "[巡检] 配置已更新: 间隔=%ds 目标 seat=%d 阈值=%d%%(min_low 已废弃,任意失效立即 1v1 替换)",
        _auto_check_config["interval"],
        _auto_check_config["target_seats"],
        _auto_check_config["threshold"],
    )
    return _auto_check_config.copy()


# Round 7 P2.4 — startup/shutdown 已迁移到顶部 app_lifespan,这里不再重复挂 handler。


# ---------------------------------------------------------------------------
# 前端静态文件
# ---------------------------------------------------------------------------

DIST_DIR = Path(__file__).parent / "web" / "dist"

if DIST_DIR.exists():
    # Vite 构建的 assets 目录
    assets_dir = DIST_DIR / "assets"
    if assets_dir.exists():
        app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="assets")

    @app.get("/{path:path}")
    def serve_frontend(path: str):
        """兜底路由:serve 前端 SPA"""
        file = DIST_DIR / path
        if file.is_file() and ".." not in path:
            return FileResponse(str(file))
        return FileResponse(str(DIST_DIR / "index.html"))


class _QuietAccessLog(logging.Filter):
    """过滤前端轮询产生的高频访问日志"""

    _quiet_paths = (
        "/api/status",
        "/api/tasks",
        "/api/config/auto-check",
        "/api/admin/status",
        "/api/main-codex/status",
        "/api/manual-account/status",
        "/api/auth/check",
        "/api/setup/status",
    )

    def filter(self, record):
        msg = record.getMessage()
        return not any(p in msg for p in self._quiet_paths)


def start_server(host: str = "0.0.0.0", port: int = 8787):
    """启动 API 服务器"""
    import uvicorn

    # 过滤轮询日志,避免刷屏
    logging.getLogger("uvicorn.access").addFilter(_QuietAccessLog())
    # 首次启动检查配置
    from autoteam.setup_wizard import check_and_setup

    check_and_setup(interactive=True)

    # 重新读取 API_KEY(可能刚刚被向导写入)
    global API_KEY
    from autoteam.config import API_KEY as _fresh_key

    API_KEY = _fresh_key or os.environ.get("API_KEY", "")
    if API_KEY:
        logger.info("[API] API Key 鉴权已启用")
    else:
        logger.warning("[API] 未设置 API_KEY,所有接口无需认证")
    logger.info("[API] 启动 AutoTeam API 服务器 http://%s:%d", host, port)
    if DIST_DIR.exists():
        logger.info("[API] 前端面板 http://%s:%d", host, port)
    logger.info("[API] API 文档 http://%s:%d/docs", host, port)
    uvicorn.run(app, host=host, port=port, log_level="info")