omni-dev 0.44.0

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

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

use anyhow::{Context, Result};
use chrono::Utc;
use futures::stream::{self, FuturesUnordered, StreamExt as _};
use tokio::sync::{mpsc, Semaphore};

use crate::cli::gmail::helpers::label_ids_contain_any;
use crate::gmail::attachments::{extract_attachments, ExtractedAttachment};
use crate::gmail::client::GmailClient;
use crate::gmail::error::GmailError;
use crate::gmail::history_api::HistoryApi;
use crate::gmail::messages_api::{
    ListingProgress, MessageFormat, MessagesApi, GMAIL_QUOTA_UNITS_PER_SECOND, MAX_CONCURRENCY,
    MESSAGES_GET_COST_UNITS,
};
use crate::gmail::profile_api::{Profile, ProfileApi};
use crate::gmail::raw_message::{
    decode_raw_message, extract_attachment_filenames, extract_headers,
};
use crate::utils::rate_limit::TokenBucket;

use super::manifest::{Manifest, ManifestRecord};
use super::progress::SyncProgressEvent;
use super::report::{SyncAction, SyncError, SyncReport};
use super::shard::{attachments_dir, shard_path};
use super::state::{self, ArchiveState, LoadOutcome};

/// Options for one `gmail sync` invocation.
pub(crate) struct SyncOptions {
    pub(crate) output_dir: PathBuf,
    pub(crate) query: Option<String>,
    /// Label ids to exclude from the archive (#1780). Applied fully and
    /// generally on incremental passes (`run_incremental`, via label data
    /// history events already carry — no extra API calls); on full/backfill/
    /// reconciliation passes only the entries with a known `-in:` query
    /// translation take effect (currently `SPAM`/`TRASH`) — see
    /// `full_sync_query`'s doc comment.
    pub(crate) exclude_labels: Vec<String>,
    pub(crate) full: bool,
    pub(crate) concurrency: usize,
    pub(crate) dry_run: bool,
    pub(crate) extract_attachments: bool,
    /// A concurrency cap shared across every account's fetches in one
    /// `gmail sync-all` run (ADR-0068), layered underneath `concurrency`
    /// (each account's own local `buffer_unordered`/`FuturesUnordered`
    /// clamp). `None` for every single-account caller — `gmail sync`'s
    /// behavior is unaffected.
    pub(crate) shared_pool: Option<Arc<Semaphore>>,
}

fn state_path(output_dir: &Path) -> PathBuf {
    output_dir.join("state.json")
}

/// `pub(crate)` — also used by `gmail extract-attachments`
/// (`cli/gmail/extract_attachments/engine.rs`), which reads the same
/// on-disk manifest without going through a full `run_sync`.
pub(crate) fn manifest_path(output_dir: &Path) -> PathBuf {
    output_dir.join("manifest.jsonl")
}

/// Number of successfully-fetched messages between manifest checkpoints
/// during [`fetch_and_archive_messages`]'s fetch fan-out.
///
/// Bounds how much re-fetch work a crash mid-backfill can cost to about one
/// interval's worth (~4s at the documented 50 msg/s quota ceiling for `200`
/// messages), while keeping the number of full-manifest rewrites
/// proportional to `total / interval` rather than `total` (#1467).
const MANIFEST_CHECKPOINT_INTERVAL: usize = 200;

/// Runs one sync: resolves identity, decides backfill vs. incremental (with
/// 404 fallback), fetches whatever's missing, and returns a report. Never
/// panics on per-message failures — see
/// [`fetch_and_archive_messages_streaming`].
///
/// A thin wrapper around [`run_sync_with_progress`] with no progress
/// channel — kept as its own function so every existing caller/test stays
/// unaffected by #1502's progress-reporting addition.
pub(crate) async fn run_sync(client: &GmailClient, opts: &SyncOptions) -> Result<SyncReport> {
    run_sync_with_progress(client, opts, None).await
}

/// [`run_sync`], plus an optional channel to emit
/// [`SyncProgressEvent`]s to during the full-mailbox pass (backfill /
/// `--full` / 404-triggered reconciliation). `run_incremental`'s
/// `history.list` pass is typically a single page already, so it emits no
/// progress events regardless of whether `progress` is set.
///
/// Still performs no direct stdout/stderr I/O — `progress`, if present, is
/// just another channel this function writes structured data to, the same
/// as `report`; only the caller (`src/cli/gmail/sync.rs`) may render
/// anything (ADR-0064's amendment for #1502).
pub(crate) async fn run_sync_with_progress(
    client: &GmailClient,
    opts: &SyncOptions,
    progress: Option<&mpsc::UnboundedSender<SyncProgressEvent>>,
) -> Result<SyncReport> {
    guard_output_dir(&opts.output_dir)?;
    if !opts.dry_run {
        let messages_dir = opts.output_dir.join("messages");
        std::fs::create_dir_all(&messages_dir)
            .with_context(|| format!("Failed to create {}", messages_dir.display()))?;
    }

    let profile = ProfileApi::new(client).get().await?;
    let loaded = state::load(&state_path(&opts.output_dir));
    let mut manifest = Manifest::load(&manifest_path(&opts.output_dir))?;
    let mut report = SyncReport::default();
    let limiter = TokenBucket::new(GMAIL_QUOTA_UNITS_PER_SECOND, GMAIL_QUOTA_UNITS_PER_SECOND);

    let (history_id, carried) = match loaded {
        LoadOutcome::Present(state) if !opts.full => {
            state::validate_identity(&state, &profile.email_address)?;
            match run_incremental(
                client,
                &mut manifest,
                &state,
                opts,
                &limiter,
                &mut report,
                progress,
            )
            .await
            {
                Ok(id) => (id, Vec::new()),
                Err(e) if is_history_not_found(&e) => {
                    report.actions.push(SyncAction::Note {
                        message: "watermark expired (404 on startHistoryId); reconciling"
                            .to_string(),
                    });
                    run_full_sync(
                        client,
                        &mut manifest,
                        &profile,
                        opts,
                        &limiter,
                        &mut report,
                        progress,
                    )
                    .await?
                    .carry_forward(&state.pending_fetch)
                }
                Err(e) => return Err(e),
            }
        }
        LoadOutcome::Present(state) => {
            // `--full` with an otherwise-valid state.json still validates
            // identity — a forced reconciliation is not an excuse to skip
            // the one check that prevents mixing two mailboxes.
            state::validate_identity(&state, &profile.email_address)?;
            run_full_sync(
                client,
                &mut manifest,
                &profile,
                opts,
                &limiter,
                &mut report,
                progress,
            )
            .await?
            .carry_forward(&state.pending_fetch)
        }
        LoadOutcome::Absent => run_full_sync(
            client,
            &mut manifest,
            &profile,
            opts,
            &limiter,
            &mut report,
            progress,
        )
        .await?
        .carry_forward(&[]),
        LoadOutcome::Corrupt(reason) => {
            report.actions.push(SyncAction::Note {
                message: format!("state.json unreadable ({reason}); reconciling"),
            });
            run_full_sync(
                client,
                &mut manifest,
                &profile,
                opts,
                &limiter,
                &mut report,
                progress,
            )
            .await?
            .carry_forward(&[])
        }
    };

    if !opts.dry_run {
        // Always saved: successful per-message mutations (fetches, label
        // deltas, soft-deletes) are independently safe to keep even when
        // this run also hit errors elsewhere.
        manifest.save(&manifest_path(&opts.output_dir))?;
        // The watermark advances even when this run hit errors: the failed
        // ids are carried forward in `pending_fetch` for the next incremental
        // run to retry directly, so no history event needs to stay within
        // Gmail's ~1-week retention window for them to be found again. That
        // window used to be the only way back to a failed id, which meant a
        // chronically rate-limited account could never advance its watermark
        // and was pushed into a full-mailbox reconciliation (#1784).
        // No dedup needed: both fetch paths already fetch each id once, and
        // `carried` only holds ids this run's listing never named, so never
        // an id that could also have failed here.
        let pending_fetch = report
            .errors
            .iter()
            .map(|e| e.id.clone())
            .chain(carried)
            .collect();
        state::save(
            &ArchiveState {
                history_id,
                email_address: profile.email_address,
                last_sync: Utc::now(),
                query: opts.query.clone(),
                pending_fetch,
            },
            &state_path(&opts.output_dir),
        )?;
    }
    Ok(report)
}

/// Backfill, `--full`, and 404-triggered reconciliation are all this same
/// path: list every message currently on the server, fetch whatever's
/// missing on disk (an interrupted prior run's already-archived messages
/// are skipped for free), and soft-delete manifest records for ids that no
/// longer appear.
///
/// Listing and fetching run *concurrently*, joined on this one task via
/// [`tokio::join!`] rather than [`tokio::spawn`] (#1502) — that's what lets
/// both sides keep sharing a single `&TokenBucket` borrow with no `Arc`, and
/// what confines `&mut Manifest` to exactly one side (the fetch consumer,
/// [`fetch_and_archive_messages_streaming`]) with no `Arc<Mutex<_>>` either.
/// The two manifest-mutating passes that need the *complete* listing
/// (undelete-on-reappearance, stale-id soft-deletion) both run after the
/// join — previously undelete ran before the (then-sequential) fetch phase
/// and stale-deletion ran after, so this can change the *order* actions
/// appear in a [`SyncReport`], never which actions occur.
async fn run_full_sync(
    client: &GmailClient,
    manifest: &mut Manifest,
    profile: &Profile,
    opts: &SyncOptions,
    limiter: &TokenBucket,
    report: &mut SyncReport,
    progress: Option<&mpsc::UnboundedSender<SyncProgressEvent>>,
) -> Result<FullListing> {
    let (ids_tx, ids_rx) = mpsc::unbounded_channel::<String>();
    let messages_api = MessagesApi::new(client);

    let (effective_query, translated_exclude_labels) = full_sync_query(opts, report);
    let listing = messages_api.search_all_unbounded_streaming(
        effective_query.as_deref(),
        &[],
        limiter,
        ids_tx,
        |p: ListingProgress| {
            if let Some(tx) = progress {
                let _ = tx.send(SyncProgressEvent::ListingPage {
                    pages: p.page_no,
                    ids_discovered: p.ids_so_far,
                });
            }
        },
    );
    let fetching = fetch_and_archive_messages_streaming(
        client, manifest, ids_rx, limiter, opts, report, progress,
    );

    let (listing_result, fetch_result) = tokio::join!(listing, fetching);
    // A real `messages.list` failure is the more actionable root cause when
    // both sides error (the fetch side would just be draining a channel
    // that stopped growing) — check it first.
    listing_result?;
    let listed_ids = fetch_result?;
    if let Some(tx) = progress {
        let _ = tx.send(SyncProgressEvent::ListingDone);
    }

    for id in &listed_ids {
        let Some(record) = manifest.get(id) else {
            continue;
        };
        if record.deleted_at.is_none() {
            continue;
        }
        // A record whose cached `excluded_labels` names a label this run's
        // query could *not* have filtered server-side (a custom/untranslated
        // one — see `full_sync_query`'s doc comment) must not be resurrected
        // just because it still shows up in an otherwise-unfiltered listing:
        // `run_incremental`'s own labelsRemoved tracking is the only source
        // of truth for that label (#1780). A translated label (SPAM/TRASH)
        // is safe to trust here even if cached: the listing query itself
        // excludes it, so reappearing despite that is authoritative proof
        // it's gone.
        let stuck_on_untranslated_exclusion = record
            .excluded_labels
            .iter()
            .any(|label| !translated_exclude_labels.iter().any(|t| t == label));
        if stuck_on_untranslated_exclusion {
            continue;
        }
        if opts.dry_run {
            report
                .actions
                .push(SyncAction::WouldUndelete { id: id.clone() });
        } else {
            manifest.undelete(id);
            report
                .actions
                .push(SyncAction::Undeleted { id: id.clone() });
        }
    }

    let stale: Vec<String> = manifest
        .ids_not_deleted()
        .filter(|id| !listed_ids.contains(*id))
        .map(str::to_string)
        .collect();
    for id in stale {
        if opts.dry_run {
            report.actions.push(SyncAction::WouldDelete { id });
        } else {
            // Plain, reason-less soft-delete: this pass has no way to know
            // *which* configured label (if any) actually explains a given
            // id's absence — `--query` can narrow the listing for entirely
            // unrelated reasons too — so it must never guess and tag
            // `excluded_labels` (#1780). Leaving it untouched (cleared, via
            // `mark_deleted`) means only `run_incremental`'s own precise,
            // event-driven tracking ever populates that field, which is
            // exactly what keeps the undelete-on-reappearance check above
            // safe.
            manifest.mark_deleted(&id, Utc::now());
            report.actions.push(SyncAction::Deleted { id });
        }
    }

    // Snapshotted *before* the listing began (by the caller, via `profile`)
    // — mail arriving mid-listing is simply re-observed as an ordinary
    // `messagesAdded` event on the next incremental run, a self-healing
    // race window rather than a gap.
    Ok(FullListing {
        history_id: profile.history_id.clone(),
        scoped: effective_query.is_some_and(|q| !q.trim().is_empty()),
        listed_ids,
    })
}

/// What [`run_full_sync`] listed, for deciding which of a previous run's
/// `pending_fetch` ids survive it.
struct FullListing {
    history_id: String,
    /// Whether the listing was narrowed by `--query` or a translated
    /// `--exclude-label` clause, so an id's absence from it doesn't mean the
    /// message is gone.
    scoped: bool,
    listed_ids: HashSet<String>,
}

impl FullListing {
    /// Returns the watermark plus the `previous` pending ids to carry forward
    /// (#1784). A listed id was already retried by the listing itself, so it
    /// is never carried. An unlisted one is dropped after an unscoped
    /// listing, where absence means the message is gone, but carried after a
    /// scoped one, which never looked for it.
    fn carry_forward(self, previous: &[String]) -> (String, Vec<String>) {
        let carried = if self.scoped {
            previous
                .iter()
                .filter(|id| !self.listed_ids.contains(*id))
                .cloned()
                .collect()
        } else {
            Vec::new()
        };
        (self.history_id, carried)
    }
}

/// Label ids with a well-known, safe `-in:<keyword>` exclusion translation
/// for [`full_sync_query`]. Deliberately just these two, not every Gmail
/// system label: `UNREAD`/`STARRED`/`IMPORTANT` are matched via `is:`, not
/// `in:`, so guessing wrong would silently exclude nothing rather than
/// erroring — `SPAM`/`TRASH` are what #1780 and docs/gmail.md's existing
/// `--query "-in:spam"` example already name as the motivating case.
const FULL_SYNC_EXCLUDABLE_LABELS: &[(&str, &str)] = &[("SPAM", "spam"), ("TRASH", "trash")];

/// Folds `opts.exclude_labels` into the query used for [`run_full_sync`]'s
/// listing pass, for whichever entries have a known translation (see
/// [`FULL_SYNC_EXCLUDABLE_LABELS`]). An entry with no known translation (a
/// custom/user label, or a system label outside the allow-list) can't be
/// reliably excluded server-side by id alone, so it's left out of the query
/// and a [`SyncAction::Note`] is pushed instead: `run_incremental` still
/// filters it going forward (it works generally, off `label_ids` rather
/// than a query translation), but this pass won't retroactively exclude it
/// unless the caller also sets `--query`.
///
/// A caller-supplied `--query` is parenthesized before any exclusion
/// clause is appended — `q OR r -in:spam` would parse as `q OR (r AND
/// -in:spam)` (Gmail's implicit AND-by-juxtaposition binds tighter than an
/// explicit `OR`), silently narrowing the exclusion to only the right-hand
/// side of the caller's own query; `(q OR r) -in:spam` applies it to the
/// whole thing, which is what `--exclude-label` promises regardless of
/// what `--query` happens to contain (#1780). Parenthesizing is skipped
/// when no exclusion clause ends up being appended, so a caller who never
/// uses `--exclude-label` sees `--query` passed through byte-for-byte,
/// unchanged from before this flag existed.
///
/// Also returns the subset of `exclude_labels` that *did* get a
/// translation, deduplicated — [`run_full_sync`]'s undelete-on-reappearance
/// pass uses it to tell a translated (trustworthy, since the listing query
/// itself enforces it) cached exclusion reason from an untranslated
/// (custom-label) one it must not casually undo.
fn full_sync_query(opts: &SyncOptions, report: &mut SyncReport) -> (Option<String>, Vec<String>) {
    // Tokens already present in the caller's own `--query`, so a repeated
    // `--exclude-label` value that duplicates `--query`'s own text doesn't
    // fold in the same `-in:` token twice.
    let existing_tokens: HashSet<&str> = opts
        .query
        .as_deref()
        .unwrap_or_default()
        .split_whitespace()
        .collect();
    let mut translated = Vec::new();
    let mut exclusion_tokens: Vec<String> = Vec::new();
    let mut seen_tokens: HashSet<String> = HashSet::new();
    for label in &opts.exclude_labels {
        match FULL_SYNC_EXCLUDABLE_LABELS
            .iter()
            .find(|(id, _)| *id == label)
        {
            Some((_, keyword)) => {
                if !translated.contains(label) {
                    translated.push(label.clone());
                }
                let token = format!("-in:{keyword}");
                if !existing_tokens.contains(token.as_str()) && seen_tokens.insert(token.clone()) {
                    exclusion_tokens.push(token);
                }
            }
            None => report.actions.push(SyncAction::Note {
                message: format!(
                    "--exclude-label {label} has no full-sync query translation; it only \
                     filters incremental syncs going forward — add it to --query yourself to \
                     also exclude it on this pass"
                ),
            }),
        }
    }
    if exclusion_tokens.is_empty() {
        return (opts.query.clone(), translated);
    }
    let mut query = match opts.query.as_deref().filter(|q| !q.is_empty()) {
        Some(user_query) => format!("({user_query})"),
        None => String::new(),
    };
    for token in exclusion_tokens {
        if !query.is_empty() {
            query.push(' ');
        }
        query.push_str(&token);
    }
    (Some(query), translated)
}

/// Applies `messagesAdded`/`messagesDeleted`/`labelsAdded`/`labelsRemoved`
/// history events since `previous.history_id`. A 404 (watermark past Gmail's
/// retention window) propagates unchanged for [`run_sync`] to catch.
///
/// A message can be added and deleted again within the same history
/// window (routine server-side churn — an auto-filtered message, a sent
/// mail immediately recalled) — every deleted id is pre-scanned across the
/// *whole* response first specifically so `to_fetch` can exclude them:
/// fetching one would just 404 (it's already gone by the time we'd ask),
/// and that 404 has nothing to do with a real failure. Symmetrically,
/// [`SyncAction::Deleted`] is only reported — and the manifest only
/// touched — for an id [`Manifest::mark_deleted`] actually had a record
/// for; a same-window churn id never got archived, so there's nothing to
/// report deleting.
///
/// `history.list`'s own pagination isn't streamed (unlike
/// [`run_full_sync`]'s listing — see its module-level rationale): this path
/// is typically a single page already (`docs/gmail.md`'s Sync section), so
/// this only emits one before/after [`SyncProgressEvent::ListingPage`]/
/// [`SyncProgressEvent::ListingDone`] pair around the whole call rather than
/// per-page updates — enough that `sync`'s progress bars don't sit frozen
/// at their initial state for an incremental run's entire duration, without
/// a second streaming primitive to get there.
///
/// `opts.exclude_labels` (#1780) filters this path fully and generally,
/// off the label data history events already carry for free: a
/// `messagesAdded` event for an excluded id is never fetched at all (no
/// manifest record is ever created for it — see [`SyncAction::Excluded`]);
/// a `labelsAdded`/`labelsRemoved` event that changes an already-archived
/// record's current label set across the excluded boundary
/// soft-deletes/undeletes it via [`Manifest::mark_excluded`]/
/// [`Manifest::undelete`] — see [`reconcile_label_exclusion`], which this
/// only calls *after* the fetch phase below (not inline in the event
/// loop), so a message added and given an excluded label within the same
/// `history.list` response still has a manifest record to reconcile
/// against by the time its exclusion state is decided. One known gap, not
/// solved here: a message that arrives already excluded is never archived,
/// so if the excluded label is later removed there is no manifest record
/// for the `labelsRemoved` event to act on — it stays un-archived until
/// the next `--full`/reconciliation pass re-lists and fetches it fresh,
/// the same "self-healing on the next full pass" framing this module
/// already uses for a listing race (see [`run_full_sync`]'s doc comment).
///
/// `previous.pending_fetch` is the previous run's failed-fetch ids (#1784),
/// retried here alongside this window's `messagesAdded` ids through the
/// same presence-checked fetch: one that is
/// already archived is skipped, one that 404s is [`SyncAction::Vanished`],
/// and one deleted in this window is not fetched at all. Its exclusion state
/// is decided from the labels the fetch itself returns, since the
/// `messagesAdded` event that carried its labels was consumed by an earlier
/// run.
async fn run_incremental(
    client: &GmailClient,
    manifest: &mut Manifest,
    previous: &ArchiveState,
    opts: &SyncOptions,
    limiter: &TokenBucket,
    report: &mut SyncReport,
    progress: Option<&mpsc::UnboundedSender<SyncProgressEvent>>,
) -> Result<String> {
    let history = HistoryApi::new(client)
        .list_all_unbounded(&previous.history_id, &[], limiter)
        .await?;

    let deleted_ids: HashSet<String> = history
        .history
        .iter()
        .flat_map(|record| &record.messages_deleted)
        .map(|deleted| deleted.message.id.clone())
        .collect();

    let mut seen = HashSet::new();
    let mut to_fetch = Vec::new();
    let mut pending_retried = Vec::new();
    for id in &previous.pending_fetch {
        if !deleted_ids.contains(id) && seen.insert(id.clone()) {
            to_fetch.push(id.clone());
            pending_retried.push(id.clone());
        }
    }
    if !pending_retried.is_empty() {
        report.actions.push(SyncAction::Note {
            message: format!(
                "retrying {} message(s) that failed on a previous run",
                pending_retried.len()
            ),
        });
    }
    // Every id touched by a labelsAdded/labelsRemoved event this window,
    // in first-seen order — reconciled against `--exclude-label` only
    // *after* the fetch phase (see this function's doc comment).
    let mut labels_touched = Vec::new();
    let mut labels_touched_seen = HashSet::new();
    for record in &history.history {
        for added in &record.messages_added {
            if !deleted_ids.contains(&added.message.id) && seen.insert(added.message.id.clone()) {
                if label_ids_contain_any(&added.message.label_ids, &opts.exclude_labels) {
                    report.actions.push(SyncAction::Excluded {
                        id: added.message.id.clone(),
                    });
                    continue;
                }
                to_fetch.push(added.message.id.clone());
            }
        }
        for deleted in &record.messages_deleted {
            if manifest.get(&deleted.message.id).is_some() {
                manifest.mark_deleted(&deleted.message.id, Utc::now());
                report.actions.push(SyncAction::Deleted {
                    id: deleted.message.id.clone(),
                });
            }
        }
        for change in &record.labels_added {
            manifest.add_labels(&change.message.id, &change.label_ids);
            report.actions.push(SyncAction::LabelsUpdated {
                id: change.message.id.clone(),
                added: change.label_ids.clone(),
                removed: Vec::new(),
            });
            if labels_touched_seen.insert(change.message.id.clone()) {
                labels_touched.push(change.message.id.clone());
            }
        }
        for change in &record.labels_removed {
            manifest.remove_labels(&change.message.id, &change.label_ids);
            report.actions.push(SyncAction::LabelsUpdated {
                id: change.message.id.clone(),
                added: Vec::new(),
                removed: change.label_ids.clone(),
            });
            if labels_touched_seen.insert(change.message.id.clone()) {
                labels_touched.push(change.message.id.clone());
            }
        }
    }

    if let Some(tx) = progress {
        let _ = tx.send(SyncProgressEvent::ListingPage {
            pages: 1,
            ids_discovered: to_fetch.len(),
        });
        let _ = tx.send(SyncProgressEvent::ListingDone);
    }

    fetch_and_archive_messages(client, manifest, &to_fetch, limiter, opts, report, progress)
        .await?;

    for id in labels_touched.iter().chain(
        pending_retried
            .iter()
            .filter(|id| !labels_touched_seen.contains(*id)),
    ) {
        reconcile_label_exclusion(manifest, report, id, &opts.exclude_labels);
    }

    Ok(history
        .history_id
        .unwrap_or_else(|| previous.history_id.clone()))
}

/// Recomputes and applies `--exclude-label` state for `id` from its
/// manifest record's *current* `label_ids` (#1780) — the one place that
/// current set is known precisely, since it's built by replaying real
/// `labelsAdded`/`labelsRemoved` events rather than guessed. A no-op if
/// `id` has no manifest record (e.g. it was itself excluded at add-time, so
/// never archived — see [`run_incremental`]'s "known gap" doc note).
///
/// Three outcomes, decided by comparing the freshly-recomputed exclusion
/// set against the record's current state:
/// - not deleted → now excluded: soft-deletes via
///   [`Manifest::mark_excluded`], recording exactly which label(s) caused
///   it.
/// - deleted *because of* a previously-tracked exclusion (i.e.
///   [`ManifestRecord::excluded_labels`] was non-empty) → no longer
///   excluded: undeletes. This is the one branch [`Manifest::mark_deleted`]
///   exists specifically to keep safe: a record soft-deleted for *any other
///   reason* (a real `messagesDeleted` event, or `run_full_sync`'s
///   listing-absence pass) always has an empty `excluded_labels`, so this
///   condition can never fire for it — an unrelated label change on such a
///   record does not resurrect it.
/// - anything else (still excluded by a possibly-different label subset,
///   or deleted for an unrelated reason) → only the cached reason is kept
///   current via [`Manifest::set_excluded_labels`], `deleted_at` untouched.
fn reconcile_label_exclusion(
    manifest: &mut Manifest,
    report: &mut SyncReport,
    id: &str,
    exclude_labels: &[String],
) {
    let Some(record) = manifest.get(id) else {
        return;
    };
    let now_excluded: Vec<String> = exclude_labels
        .iter()
        .filter(|label| record.label_ids.iter().any(|existing| existing == *label))
        .cloned()
        .collect();
    let was_tracked_excluded = !record.excluded_labels.is_empty();
    let currently_deleted = record.deleted_at.is_some();

    if !currently_deleted && !now_excluded.is_empty() {
        manifest.mark_excluded(id, Utc::now(), now_excluded);
        report
            .actions
            .push(SyncAction::Deleted { id: id.to_string() });
    } else if currently_deleted && was_tracked_excluded && now_excluded.is_empty() {
        manifest.undelete(id);
        report
            .actions
            .push(SyncAction::Undeleted { id: id.to_string() });
    } else if was_tracked_excluded {
        manifest.set_excluded_labels(id, now_excluded);
    }
}

/// [`run_full_sync`]'s fetch/consumer side of the listing+fetch pipeline
/// (#1502): the presence-on-disk filter, the bounded/throttled fan-out, and
/// the manifest checkpointing all still work exactly as
/// [`fetch_and_archive_messages`] describes below — the only thing that
/// changed is that ids now arrive one at a time from `ids_rx` instead of as
/// a pre-collected `Vec`.
///
/// A plain `stream::iter(..).buffer_unordered(..)` (as
/// [`fetch_and_archive_messages`] uses) can't work here: that combinator
/// needs a complete `Vec` of ids *before* it borrows `manifest` for the
/// drain loop, so the presence-check filter and the drain loop never borrow
/// `manifest` at the same time. Once ids stream in instead, that filter has
/// to run per-id, interleaved with the drain loop — both wanting
/// `&mut Manifest` at once, which the borrow checker rejects as chained
/// stream combinators. Instead this is a manual pump loop: `tokio::select!`
/// alternates between pulling the next id (and synchronously filtering it
/// against `manifest`) and draining the next completed fetch (and
/// synchronously applying its result to `manifest`) — every manifest touch
/// is a synchronous statement inside a `select!` arm, never inside a future
/// stored in `in_flight`, so only one borrow of `manifest` is ever live.
///
/// Also returns every id seen on `ids_rx` (regardless of whether it needed
/// fetching) — [`run_full_sync`] needs that complete set, once listing
/// finishes, for its undelete/stale-deletion passes.
async fn fetch_and_archive_messages_streaming(
    client: &GmailClient,
    manifest: &mut Manifest,
    mut ids_rx: mpsc::UnboundedReceiver<String>,
    limiter: &TokenBucket,
    opts: &SyncOptions,
    report: &mut SyncReport,
    progress: Option<&mpsc::UnboundedSender<SyncProgressEvent>>,
) -> Result<HashSet<String>> {
    let output_dir = &opts.output_dir;
    let concurrency = opts.concurrency.clamp(1, MAX_CONCURRENCY);
    let mut seen = HashSet::new();
    let mut listed_ids = HashSet::new();
    let mut in_flight = FuturesUnordered::new();
    let mut since_checkpoint = 0usize;
    let mut ids_open = true;

    loop {
        tokio::select! {
            maybe_id = ids_rx.recv(), if ids_open && in_flight.len() < concurrency => {
                match maybe_id {
                    Some(id) => {
                        listed_ids.insert(id.clone());
                        if !seen.insert(id.clone()) {
                            continue;
                        }
                        // Not `shard_path(output_dir, id).exists()`: a
                        // not-yet-fetched message's shard depends on its
                        // `internal_date`, which isn't known until after
                        // it's fetched. The manifest's already-recorded
                        // `path` is the only presence check available
                        // before a fetch happens.
                        let already_archived = manifest
                            .get(&id)
                            .is_some_and(|record| output_dir.join(&record.path).exists());
                        if already_archived {
                            continue;
                        }
                        if opts.dry_run {
                            report.actions.push(SyncAction::WouldFetch { id });
                            continue;
                        }
                        let extract_attachments_flag = opts.extract_attachments;
                        let shared_pool = opts.shared_pool.clone();
                        in_flight.push(async move {
                            let _permit = match &shared_pool {
                                Some(pool) => match pool.acquire().await {
                                    Ok(permit) => Some(permit),
                                    Err(e) => {
                                        return (
                                            id,
                                            Err(anyhow::anyhow!(
                                                "sync-all's shared semaphore closed \
                                                 unexpectedly: {e}"
                                            )),
                                        );
                                    }
                                },
                                None => None,
                            };
                            limiter.acquire(MESSAGES_GET_COST_UNITS).await;
                            let result = fetch_and_write_one(
                                client,
                                output_dir,
                                &id,
                                extract_attachments_flag,
                            )
                            .await;
                            (id, result)
                        });
                        if let Some(tx) = progress {
                            let _ = tx.send(SyncProgressEvent::FetchQueued);
                        }
                    }
                    None => ids_open = false,
                }
            }
            Some((id, result)) = in_flight.next(), if !in_flight.is_empty() => {
                let mut failed = false;
                match result {
                    Ok(record) => {
                        report.actions.push(SyncAction::Fetched {
                            id,
                            path: record.path.clone(),
                            bytes: record.size,
                        });
                        manifest.upsert(record);
                    }
                    Err(e) if is_message_not_found(&e) => {
                        report.actions.push(SyncAction::Vanished { id });
                    }
                    Err(e) => {
                        failed = true;
                        report.errors.push(SyncError {
                            id,
                            reason: format!("{e:#}"),
                        });
                    }
                }
                since_checkpoint += 1;
                if since_checkpoint >= MANIFEST_CHECKPOINT_INTERVAL {
                    manifest.save(&manifest_path(output_dir))?;
                    since_checkpoint = 0;
                }
                if let Some(tx) = progress {
                    let _ = tx.send(SyncProgressEvent::FetchCompleted { failed });
                }
            }
            else => break,
        }
    }
    Ok(listed_ids)
}

/// The one place presence-on-disk is checked before any fetch, and the
/// bounded, throttled fan-out over whatever's actually missing.
///
/// A per-message failure is pushed to `report.errors` and never aborts the
/// batch — the opposite of the Facebook harvester's whole-run-fatal
/// `?`-propagation. Concurrency is bounded by `buffer_unordered` (order
/// doesn't matter for archival, unlike `search`'s `buffered`), and each
/// future acquires its slot in that bound before drawing from `limiter` —
/// debiting quota units before a concurrency slot is actually available
/// would desync the bucket's notion of "spent" from real request issuance.
///
/// Results are applied as each fetch *completes* (not batched through an
/// intermediate `Vec` after the whole fan-out finishes), and the manifest is
/// checkpointed to disk every [`MANIFEST_CHECKPOINT_INTERVAL`] completions —
/// see the module doc's interruption note. The final partial interval is
/// still covered by `run_sync`'s own unconditional save once this function
/// returns, so no extra flush is needed here on the way out.
///
/// Used by [`run_incremental`] only — [`run_full_sync`]'s pipelined listing
/// uses [`fetch_and_archive_messages_streaming`] instead (#1502).
async fn fetch_and_archive_messages(
    client: &GmailClient,
    manifest: &mut Manifest,
    ids: &[String],
    limiter: &TokenBucket,
    opts: &SyncOptions,
    report: &mut SyncReport,
    progress: Option<&mpsc::UnboundedSender<SyncProgressEvent>>,
) -> Result<()> {
    let output_dir = &opts.output_dir;
    let mut seen = HashSet::new();
    let mut to_fetch = Vec::new();
    for id in ids {
        if !seen.insert(id.clone()) {
            continue;
        }
        // Not `shard_path(output_dir, id).exists()`: a not-yet-fetched
        // message's shard depends on its `internal_date`, which isn't known
        // until after it's fetched. The manifest's already-recorded `path`
        // (the same check `run_full_sync` uses) is the only presence check
        // available before a fetch happens.
        let already_archived = manifest
            .get(id)
            .is_some_and(|record| output_dir.join(&record.path).exists());
        if !already_archived {
            to_fetch.push(id.clone());
        }
    }
    if to_fetch.is_empty() {
        return Ok(());
    }

    if opts.dry_run {
        for id in to_fetch {
            report.actions.push(SyncAction::WouldFetch { id });
        }
        return Ok(());
    }

    // The whole batch is already known (unlike run_full_sync's live-streamed
    // listing), so the fetch bar's total is knowable up front — one
    // `FetchQueued` per item gives a fully determinate bar from the start
    // rather than one that grows as ids trickle in.
    if let Some(tx) = progress {
        for _ in 0..to_fetch.len() {
            let _ = tx.send(SyncProgressEvent::FetchQueued);
        }
    }

    let concurrency = opts.concurrency.clamp(1, MAX_CONCURRENCY);
    let mut fetches = stream::iter(to_fetch)
        .map(|id| async move {
            let _permit = match opts.shared_pool.as_ref() {
                Some(pool) => match pool.acquire().await {
                    Ok(permit) => Some(permit),
                    Err(e) => {
                        return (
                            id,
                            Err(anyhow::anyhow!(
                                "sync-all's shared semaphore closed unexpectedly: {e}"
                            )),
                        );
                    }
                },
                None => None,
            };
            limiter.acquire(MESSAGES_GET_COST_UNITS).await;
            let result =
                fetch_and_write_one(client, output_dir, &id, opts.extract_attachments).await;
            (id, result)
        })
        .buffer_unordered(concurrency);

    let mut since_checkpoint = 0usize;
    while let Some((id, result)) = fetches.next().await {
        let mut failed = false;
        match result {
            Ok(record) => {
                report.actions.push(SyncAction::Fetched {
                    id,
                    path: record.path.clone(),
                    bytes: record.size,
                });
                manifest.upsert(record);
            }
            Err(e) if is_message_not_found(&e) => {
                report.actions.push(SyncAction::Vanished { id });
            }
            Err(e) => {
                failed = true;
                report.errors.push(SyncError {
                    id,
                    reason: format!("{e:#}"),
                });
            }
        }
        since_checkpoint += 1;
        if since_checkpoint >= MANIFEST_CHECKPOINT_INTERVAL {
            manifest.save(&manifest_path(output_dir))?;
            since_checkpoint = 0;
        }
        if let Some(tx) = progress {
            let _ = tx.send(SyncProgressEvent::FetchCompleted { failed });
        }
    }
    Ok(())
}

/// Fetches one message as `format=raw`, decodes it, writes the byte-exact
/// `.eml`, and builds its manifest record — `Subject`/`From`/`Message-Id`
/// come from scanning the already-decoded bytes ([`extract_headers`]), not
/// a second `format=metadata` round-trip, since a second fetch would double
/// the request volume for data already in hand.
///
/// When `extract_attachments` is set, also writes each `Content-Disposition:
/// attachment` part's decoded bytes into the message's `attachments/`
/// directory (see [`extract_attachments`] / ADR-0065) — a purely additive
/// step that never touches the manifest record built here, which keeps
/// coming from the cheap [`extract_attachment_filenames`] heuristic exactly
/// as before.
async fn fetch_and_write_one(
    client: &GmailClient,
    output_dir: &Path,
    id: &str,
    extract_attachments_flag: bool,
) -> Result<ManifestRecord> {
    let message = MessagesApi::new(client)
        .get(id, MessageFormat::Raw, &[])
        .await?;
    let bytes = decode_raw_message(&message)?;
    let headers = extract_headers(
        &bytes,
        &[
            "Subject",
            "From",
            "To",
            "Message-Id",
            "In-Reply-To",
            "References",
        ],
    );
    let attachments = extract_attachment_filenames(&bytes);
    let date = message.internal_date_utc();

    let path = shard_path(output_dir, id, date);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("Failed to create {}", parent.display()))?;
    }
    write_atomic(&path, &bytes)?;
    let relative = path.strip_prefix(output_dir).unwrap_or(&path).to_path_buf();

    if extract_attachments_flag {
        let extracted = extract_attachments(&bytes);
        if !extracted.is_empty() {
            let dir = attachments_dir(output_dir, id, date);
            write_attachments_atomically(&dir, &extracted)?;
        }
    }

    Ok(ManifestRecord {
        id: id.to_string(),
        thread_id: message.thread_id,
        label_ids: message.label_ids,
        internal_date: message.internal_date,
        subject: headers.get("Subject").cloned(),
        from: headers.get("From").cloned(),
        to: headers.get("To").cloned(),
        rfc822_msgid: headers.get("Message-Id").cloned(),
        in_reply_to: headers.get("In-Reply-To").cloned(),
        references: headers.get("References").cloned(),
        attachment_count: attachments.count as u32,
        attachment_filenames: attachments.filenames,
        path: relative,
        size: bytes.len() as u64,
        history_id: message.history_id,
        deleted_at: None,
        excluded_labels: Vec::new(),
    })
}

/// Writes `contents` to `path` via a sibling dotfile + rename, so a crash
/// mid-write can never leave a partial `.eml` that a later run's
/// presence-on-disk check would mistake for a complete archive.
///
/// `pub(crate)` — also used by `gmail extract-attachments`
/// (`cli/gmail/extract_attachments/engine.rs`) to write extracted
/// attachment files, the exact same durability requirement this was
/// written for.
pub(crate) fn write_atomic(path: &Path, contents: &[u8]) -> Result<()> {
    let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("out");
    let tmp = path.with_file_name(format!(".{file_name}.tmp"));
    std::fs::write(&tmp, contents).with_context(|| format!("Failed to write {}", tmp.display()))?;
    std::fs::rename(&tmp, path).with_context(|| format!("Failed to finalise {}", path.display()))
}

/// Writes every attachment in `attachments` into `dir`, publishing the
/// whole set in one atomic step — the directory-level counterpart of
/// [`write_atomic`]. Builds the files in a hidden sibling directory first
/// and only `rename`s it to `dir` once every file has landed, so a
/// mid-batch failure (disk full, a permission error, ...) never leaves
/// `dir` existing-but-partial: either `dir` ends up fully populated, or it
/// never exists at all. That is load-bearing for callers that treat
/// `dir.exists()` as "already extracted" — `gmail extract-attachments`
/// (`cli/gmail/extract_attachments/engine.rs`) relies on exactly that to
/// decide whether a message is safe to skip on a re-run, and a
/// partially-written `dir` would otherwise be skipped forever, never
/// retried.
///
/// `pub(crate)` — shared by [`fetch_and_write_one`] and
/// `extract_attachments::engine::run_extract_attachments`, which both
/// write a `Vec<ExtractedAttachment>` into a message's `attachments/`
/// directory.
pub(crate) fn write_attachments_atomically(
    dir: &Path,
    attachments: &[ExtractedAttachment],
) -> Result<()> {
    let dir_name = dir.file_name().and_then(|n| n.to_str()).unwrap_or("out");
    let tmp_dir = dir.with_file_name(format!(".{dir_name}.tmp"));
    if tmp_dir.exists() {
        // Left behind by an earlier run that failed partway through this
        // same message — clear it before rebuilding from scratch.
        std::fs::remove_dir_all(&tmp_dir)
            .with_context(|| format!("Failed to clear stale {}", tmp_dir.display()))?;
    }
    std::fs::create_dir_all(&tmp_dir)
        .with_context(|| format!("Failed to create {}", tmp_dir.display()))?;
    for attachment in attachments {
        write_atomic(&tmp_dir.join(&attachment.filename), &attachment.contents)?;
    }
    std::fs::rename(&tmp_dir, dir).with_context(|| format!("Failed to finalise {}", dir.display()))
}

/// A 404 on `history.list` is Google's documented signal for a
/// `startHistoryId` older than the mailbox's retention window. Treated as
/// history-not-found when the error's `reason` is absent/unparseable (fails
/// toward reconciling, matching `state.rs`'s disposable-state posture) or
/// equals `"notFound"`; a 404 with a *different*, present reason is
/// something else and must not silently trigger a full reconciliation —
/// mirroring how [`crate::gmail::client`]'s 403 quota check requires a
/// specific reason rather than matching on status alone.
fn is_history_not_found(err: &anyhow::Error) -> bool {
    match err.downcast_ref::<GmailError>() {
        Some(e @ GmailError::ApiRequestFailed { status: 404, .. }) => {
            e.reason().is_none_or(|r| r == "notFound")
        }
        _ => false,
    }
}

/// A 404 on `messages.get` for an id `history.list` just reported as added
/// means the message was permanently deleted from the server in the window
/// between the two calls — a real race under concurrent fetch fan-out, and
/// never going to succeed on retry (#1509). Unlike [`is_history_not_found`],
/// this does *not* fail open on an absent/unparseable reason: a message
/// fetch 404 for any other reason is a real failure that must still surface
/// as a [`SyncError`].
fn is_message_not_found(err: &anyhow::Error) -> bool {
    match err.downcast_ref::<GmailError>() {
        Some(e @ GmailError::ApiRequestFailed { status: 404, .. }) => {
            e.reason() == Some("notFound")
        }
        _ => false,
    }
}

/// Refuses an obviously-wrong `--output-dir`: `$HOME` itself, or anywhere
/// inside omni-dev's own settings directory.
///
/// `output_dir` may not exist yet (sync creates it), so ancestors are
/// canonicalised walking up to the deepest existing one first — the same
/// approach `src/cli/ai/claude/history/common.rs::is_inside` uses, kept as
/// a small local copy here rather than a new shared extraction (sync's
/// "source" is the network, not a local tree, so this guards a different
/// thing: an obviously-wrong destination, not source/target overlap).
fn guard_output_dir(output_dir: &Path) -> Result<()> {
    let Some(home) = dirs::home_dir() else {
        return Ok(());
    };
    let canon_output = canonicalize_best_effort(output_dir);
    if canon_output == canonicalize_best_effort(&home) {
        anyhow::bail!(
            "refusing to sync directly into your home directory ({}); use a dedicated \
             subdirectory",
            home.display()
        );
    }
    let omni_dev_dir = canonicalize_best_effort(&home.join(".omni-dev"));
    if canon_output == omni_dev_dir || canon_output.starts_with(&omni_dev_dir) {
        anyhow::bail!(
            "refusing to sync into {}: it is inside omni-dev's own settings directory",
            output_dir.display()
        );
    }
    Ok(())
}

fn canonicalize_best_effort(path: &Path) -> PathBuf {
    let mut existing = path;
    let mut tail: Vec<&std::ffi::OsStr> = Vec::new();
    loop {
        if let Ok(mut canon) = existing.canonicalize() {
            for component in tail.into_iter().rev() {
                canon.push(component);
            }
            return canon;
        }
        match existing.parent() {
            Some(parent) => {
                if let Some(name) = existing.file_name() {
                    tail.push(name);
                }
                existing = parent;
            }
            None => return path.to_path_buf(),
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use base64::Engine as _;
    use chrono::DateTime;
    use std::sync::atomic::Ordering;

    use crate::gmail::auth::{GmailCredentials, GmailScope};
    use crate::utils::secret::Secret;

    fn test_credentials() -> GmailCredentials {
        GmailCredentials {
            client_id: "client-1".to_string(),
            client_secret: Secret::new("secret-1"),
            refresh_token: Secret::new("refresh-1"),
            scope: GmailScope::ReadOnly,
        }
    }

    async fn client_with_bootstrapped_token(server: &wiremock::MockServer) -> GmailClient {
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path("/token"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "access_token": "test-token",
                    "expires_in": 3600,
                })),
            )
            .mount(server)
            .await;

        let mut client = GmailClient::new(&server.uri(), &test_credentials()).unwrap();
        crate::gmail::client::test_support::replace_session(
            &mut client,
            &test_credentials(),
            &format!("{}/token", server.uri()),
        );
        client
    }

    async fn mount_profile(server: &wiremock::MockServer, email: &str, history_id: &str) {
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/profile"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "emailAddress": email,
                    "messagesTotal": 1,
                    "threadsTotal": 1,
                    "historyId": history_id,
                })),
            )
            .mount(server)
            .await;
    }

    async fn mount_message_list(server: &wiremock::MockServer, ids: &[&str]) {
        let messages: Vec<serde_json::Value> = ids
            .iter()
            .map(|id| serde_json::json!({"id": id, "threadId": "t1"}))
            .collect();
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"messages": messages})),
            )
            .mount(server)
            .await;
    }

    fn raw_message_body(id: &str, subject: &str) -> String {
        let source = format!("Subject: {subject}\r\nFrom: a@example.com\r\n\r\nBody of {id}.");
        base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(source)
    }

    async fn mount_raw_get(server: &wiremock::MockServer, id: &str, subject: &str) {
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path(format!(
                "/gmail/v1/users/me/messages/{id}"
            )))
            .and(wiremock::matchers::query_param("format", "raw"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "id": id,
                    "threadId": "t1",
                    "labelIds": ["INBOX"],
                    "internalDate": "1700000000000",
                    "historyId": "500",
                    "raw": raw_message_body(id, subject),
                })),
            )
            .mount(server)
            .await;
    }

    /// The date `mount_raw_get`'s `internalDate` ("1700000000000") resolves
    /// to — shared so tests asserting a fetched message's on-disk shard
    /// location don't hardcode a second, independently-computed date.
    fn mock_internal_date() -> DateTime<Utc> {
        DateTime::from_timestamp_millis(1_700_000_000_000).unwrap()
    }

    fn opts(output_dir: PathBuf) -> SyncOptions {
        SyncOptions {
            output_dir,
            query: None,
            exclude_labels: Vec::new(),
            full: false,
            concurrency: 4,
            dry_run: false,
            extract_attachments: false,
            shared_pool: None,
        }
    }

    // ── write_attachments_atomically ────────────────────────────────

    #[test]
    fn write_attachments_atomically_writes_every_file_and_leaves_no_tmp_dir() {
        let root = tempfile::tempdir().unwrap();
        let dir = root.path().join("m1").join("attachments");
        let attachments = vec![
            ExtractedAttachment {
                filename: "a.txt".to_string(),
                contents: b"A".to_vec(),
            },
            ExtractedAttachment {
                filename: "b.txt".to_string(),
                contents: b"B".to_vec(),
            },
        ];

        write_attachments_atomically(&dir, &attachments).unwrap();

        assert_eq!(std::fs::read(dir.join("a.txt")).unwrap(), b"A");
        assert_eq!(std::fs::read(dir.join("b.txt")).unwrap(), b"B");
        assert!(!dir.with_file_name(".attachments.tmp").exists());
    }

    #[test]
    fn write_attachments_atomically_never_leaves_a_partial_dir_on_failure() {
        let root = tempfile::tempdir().unwrap();
        let dir = root.path().join("m1").join("attachments");
        let attachments = vec![
            ExtractedAttachment {
                filename: "a.txt".to_string(),
                contents: b"A".to_vec(),
            },
            // A NUL byte is never a filename `extract_attachments` would
            // actually produce (its `attachment_filename` sanitiser rules
            // it out), but it's a deterministic, portable way to force
            // `write_atomic` to fail partway through the batch here.
            ExtractedAttachment {
                filename: "b\0.txt".to_string(),
                contents: b"B".to_vec(),
            },
        ];

        assert!(write_attachments_atomically(&dir, &attachments).is_err());

        // The whole point: a failure partway through must never leave
        // `dir` existing-but-partial, since callers key their
        // already-extracted skip on `dir.exists()` alone. A stray hidden
        // tmp dir is fine — the next attempt clears it before rebuilding.
        assert!(!dir.exists());
    }

    #[test]
    fn write_attachments_atomically_recovers_from_a_stale_tmp_dir() {
        let root = tempfile::tempdir().unwrap();
        let dir = root.path().join("m1").join("attachments");
        let tmp_dir = dir.with_file_name(".attachments.tmp");
        std::fs::create_dir_all(&tmp_dir).unwrap();
        std::fs::write(tmp_dir.join("leftover-from-a-crash.bin"), b"stale").unwrap();

        let attachments = vec![ExtractedAttachment {
            filename: "a.txt".to_string(),
            contents: b"A".to_vec(),
        }];

        write_attachments_atomically(&dir, &attachments).unwrap();

        assert_eq!(std::fs::read(dir.join("a.txt")).unwrap(), b"A");
        // The stale leftover from the aborted attempt must not survive
        // into the finalised directory.
        assert!(!dir.join("leftover-from-a-crash.bin").exists());
    }

    // ── backfill ─────────────────────────────────────────────────────

    #[tokio::test]
    async fn run_sync_backfills_on_first_run_and_persists_the_watermark() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        mount_message_list(&server, &["m1"]).await;
        mount_raw_get(&server, "m1", "Hello").await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Fetched { id, .. } if id == "m1")));
        assert!(shard_path(&output_dir, "m1", Some(mock_internal_date())).exists());

        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => {
                assert_eq!(s.history_id, "500");
                assert_eq!(s.email_address, "user@example.com");
            }
            _ => panic!("expected a persisted state after a clean backfill"),
        }

        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert_eq!(record.subject.as_deref(), Some("Hello"));
        assert_eq!(record.from.as_deref(), Some("a@example.com"));
    }

    #[tokio::test]
    async fn run_sync_records_threading_headers_and_attachment_metadata() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        mount_message_list(&server, &["m1"]).await;

        let source = "Subject: Report\r\n\
From: a@example.com\r\n\
To: b@example.com\r\n\
Message-Id: <m1@example.com>\r\n\
In-Reply-To: <parent@example.com>\r\n\
References: <parent@example.com>\r\n\
MIME-Version: 1.0\r\n\
Content-Type: multipart/mixed; boundary=\"BOUNDARY\"\r\n\
\r\n\
--BOUNDARY\r\n\
Content-Type: text/plain\r\n\
\r\n\
Hello\r\n\
--BOUNDARY\r\n\
Content-Type: application/pdf\r\n\
Content-Disposition: attachment; filename=\"report.pdf\"\r\n\
\r\n\
not-really-a-pdf\r\n\
--BOUNDARY--\r\n";
        let encoded = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(source);
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .and(wiremock::matchers::query_param("format", "raw"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "id": "m1",
                    "threadId": "t1",
                    "labelIds": ["INBOX"],
                    "internalDate": "1700000000000",
                    "historyId": "500",
                    "raw": encoded,
                })),
            )
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();
        assert!(report.errors.is_empty());

        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert_eq!(record.to.as_deref(), Some("b@example.com"));
        assert_eq!(record.in_reply_to.as_deref(), Some("<parent@example.com>"));
        assert_eq!(record.references.as_deref(), Some("<parent@example.com>"));
        assert_eq!(record.attachment_count, 1);
        assert_eq!(record.attachment_filenames, vec!["report.pdf".to_string()]);
    }

    // ── 404 → reconciliation ─────────────────────────────────────────

    #[tokio::test]
    async fn run_sync_404_on_watermark_triggers_reconciliation_not_a_gap() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "1".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(404).set_body_json(serde_json::json!({
                    "error": {"message": "Not Found", "errors": [{"reason": "notFound"}]}
                })),
            )
            .mount(&server)
            .await;
        mount_message_list(&server, &["m1"]).await;
        mount_raw_get(&server, "m1", "Hello").await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Note { message } if message.contains("reconciling"))));
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Fetched { id, .. } if id == "m1")));
        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => assert_eq!(s.history_id, "999"),
            _ => panic!("expected reconciliation to persist the profile's current historyId"),
        }
    }

    // ── valid watermark applies all 4 event types ─────────────────────

    #[tokio::test]
    async fn run_sync_applies_added_deleted_and_label_events_from_a_valid_watermark() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        // Pre-existing m2 (to be deleted) and m3 (to have labels changed) —
        // both already archived, neither should be re-fetched or rewritten.
        let mut manifest = Manifest::default();
        for id in ["m2", "m3"] {
            let path = shard_path(&output_dir, id, None);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(&path, format!("From: a@example.com\r\n\r\n{id} body")).unwrap();
            manifest.upsert(ManifestRecord {
                id: id.to_string(),
                thread_id: Some("t1".to_string()),
                label_ids: vec!["INBOX".to_string(), "UNREAD".to_string()],
                internal_date: None,
                subject: None,
                from: None,
                to: None,
                rfc822_msgid: None,
                in_reply_to: None,
                references: None,
                attachment_count: 0,
                attachment_filenames: Vec::new(),
                path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
                size: std::fs::metadata(&path).unwrap().len(),
                history_id: Some("50".to_string()),
                deleted_at: None,
                excluded_labels: Vec::new(),
            });
        }
        manifest.save(&manifest_path(&output_dir)).unwrap();
        let m3_bytes_before = std::fs::read(shard_path(&output_dir, "m3", None)).unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .and(wiremock::matchers::query_param("startHistoryId", "100"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "history": [{
                    "id": "150",
                    "messagesAdded": [{"message": {"id": "m1", "threadId": "t1", "labelIds": ["INBOX"]}}],
                    "messagesDeleted": [{"message": {"id": "m2", "threadId": "t1"}}],
                    "labelsAdded": [{"message": {"id": "m3", "threadId": "t1"}, "labelIds": ["IMPORTANT"]}],
                    "labelsRemoved": [{"message": {"id": "m3", "threadId": "t1"}, "labelIds": ["UNREAD"]}],
                }],
                "historyId": "300",
            })))
            .mount(&server)
            .await;
        mount_raw_get(&server, "m1", "New message").await;
        // Deliberately no mock for GET .../messages/m3 — a label-only change
        // must never fetch the message it applies to.

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();
        assert!(report.errors.is_empty());

        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(manifest.get("m1").is_some(), "m1 should have been fetched");
        assert!(
            manifest.get("m2").unwrap().deleted_at.is_some(),
            "m2 should be soft-deleted"
        );
        assert!(
            shard_path(&output_dir, "m2", None).exists(),
            "m2's .eml must survive a soft-delete"
        );
        let m3 = manifest.get("m3").unwrap();
        assert!(m3.label_ids.contains(&"IMPORTANT".to_string()));
        assert!(!m3.label_ids.contains(&"UNREAD".to_string()));
        assert_eq!(
            std::fs::read(shard_path(&output_dir, "m3", None)).unwrap(),
            m3_bytes_before,
            "a label-only change must never rewrite the .eml"
        );

        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => assert_eq!(s.history_id, "300"),
            _ => panic!("expected the new historyId to be persisted"),
        }
    }

    #[tokio::test]
    async fn run_sync_ignores_a_message_added_and_deleted_within_the_same_history_window() {
        // Routine server-side churn (an auto-filtered message, a sent mail
        // immediately recalled): a message can be added and deleted again
        // before the next sync ever sees it. Fetching it would just 404,
        // and that isn't a real failure — see `run_incremental`'s doc
        // comment.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "messagesAdded": [{"message": {"id": "churn1", "threadId": "t1"}}],
                        "messagesDeleted": [{"message": {"id": "churn1", "threadId": "t1"}}],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;
        // Deliberately no mock for GET .../messages/churn1 — fetching it at
        // all (not just erroring) is the bug this test guards against.

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(
            !report
                .actions
                .iter()
                .any(|a| matches!(a, SyncAction::Deleted { id } if id == "churn1")),
            "a message never archived shouldn't be reported as deleted"
        );
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(
            manifest.get("churn1").is_none(),
            "a same-window add+delete should never create a manifest record"
        );
    }

    // ── --exclude-label (#1780) ─────────────────────────────────────────

    #[tokio::test]
    async fn run_incremental_never_undeletes_a_record_with_no_tracked_exclusion_reason_by_default()
    {
        // Regression test: a user who never passes --exclude-label at all
        // must see run_incremental behave exactly as it did before this
        // flag existed — never undeleting anything. Before the fix, this
        // failed because `!label_ids_excluded(labels, [])` was vacuously
        // `true`, collapsing the undelete guard to just "is it currently
        // soft-deleted" for every default caller, not only --exclude-label
        // adopters.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        // m1 is already soft-deleted for a reason with nothing to do with
        // labels (a real messagesDeleted event, or run_full_sync's
        // listing-absence pass) — plain mark_deleted, no exclusion
        // tracking.
        let mut manifest = Manifest::default();
        let path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "From: a@example.com\r\n\r\nm1 body").unwrap();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: std::fs::metadata(&path).unwrap().len(),
            history_id: Some("50".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.mark_deleted("m1", Utc::now());
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "labelsRemoved": [
                            {"message": {"id": "m1", "threadId": "t1"}, "labelIds": ["IMPORTANT"]},
                        ],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;

        // Default opts(): exclude_labels is empty, i.e. --exclude-label was
        // never passed.
        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(
            !report
                .actions
                .iter()
                .any(|a| matches!(a, SyncAction::Undeleted { id } if id == "m1")),
            "an unrelated labelsRemoved event must never undelete a message soft-deleted for a \
             reason unrelated to label exclusion, even when --exclude-label was never configured"
        );
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(manifest.get("m1").unwrap().deleted_at.is_some());
    }

    #[tokio::test]
    async fn run_incremental_excludes_a_message_labeled_within_the_same_history_window_it_was_added_in(
    ) {
        // Regression test: a message added via messagesAdded (not yet
        // excluded per that event's own label set) and then given an
        // excluded label via labelsAdded, both within the *same*
        // history.list response, must still end up excluded — not archived
        // despite matching --exclude-label within this very run. Before the
        // fix, the exclusion recheck ran inline in the event-scanning loop,
        // before the batched fetch (which only happens after that loop)
        // had created a manifest record for the message, so the recheck was
        // always a no-op for a same-window id.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "messagesAdded": [
                            {"message": {"id": "m1", "threadId": "t1", "labelIds": ["INBOX"]}},
                        ],
                    }, {
                        "id": "151",
                        "labelsAdded": [
                            {"message": {"id": "m1", "threadId": "t1"}, "labelIds": ["SPAM"]},
                        ],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;
        // Not `mount_raw_get` (hardcoded to `labelIds: ["INBOX"]`): the
        // live fetch is authoritative for the message's label state *at
        // fetch time*, which is after the labelsAdded event above already
        // happened server-side — so the mock must reflect that SPAM is
        // already present, exactly as the real Gmail API would return.
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .and(wiremock::matchers::query_param("format", "raw"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "id": "m1",
                    "threadId": "t1",
                    "labelIds": ["INBOX", "SPAM"],
                    "internalDate": "1700000000000",
                    "historyId": "500",
                    "raw": raw_message_body("m1", "New but spammed"),
                })),
            )
            .mount(&server)
            .await;

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(
            report
                .actions
                .iter()
                .any(|a| matches!(a, SyncAction::Deleted { id } if id == "m1")),
            "a message excluded within the same window it was added in must still be excluded"
        );
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert!(record.deleted_at.is_some());
        assert_eq!(record.excluded_labels, vec!["SPAM".to_string()]);
    }

    #[tokio::test]
    async fn run_incremental_skips_fetching_a_new_message_with_an_excluded_label() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "messagesAdded": [{"message": {"id": "spam1", "threadId": "t1", "labelIds": ["SPAM"]}}],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;
        // Deliberately no mock for GET .../messages/spam1 — fetching an
        // excluded message at all is the bug this test guards against.

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Excluded { id } if id == "spam1")));
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(
            manifest.get("spam1").is_none(),
            "an excluded new message should never be archived"
        );
    }

    #[tokio::test]
    async fn run_incremental_soft_deletes_an_archived_message_labeled_with_an_excluded_label() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        let mut manifest = Manifest::default();
        let path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "From: a@example.com\r\n\r\nm1 body").unwrap();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: std::fs::metadata(&path).unwrap().len(),
            history_id: Some("50".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "labelsAdded": [{"message": {"id": "m1", "threadId": "t1"}, "labelIds": ["SPAM"]}],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Deleted { id } if id == "m1")));
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(manifest.get("m1").unwrap().deleted_at.is_some());
        assert!(
            shard_path(&output_dir, "m1", None).exists(),
            "the .eml must survive a label-triggered soft-delete"
        );
    }

    #[tokio::test]
    async fn run_incremental_undeletes_only_once_every_excluded_label_is_removed() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        let mut manifest = Manifest::default();
        for id in ["m1", "m2"] {
            let path = shard_path(&output_dir, id, None);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(&path, format!("From: a@example.com\r\n\r\n{id} body")).unwrap();
            manifest.upsert(ManifestRecord {
                id: id.to_string(),
                thread_id: Some("t1".to_string()),
                label_ids: vec!["SPAM".to_string(), "TRASH".to_string()],
                internal_date: None,
                subject: None,
                from: None,
                to: None,
                rfc822_msgid: None,
                in_reply_to: None,
                references: None,
                attachment_count: 0,
                attachment_filenames: Vec::new(),
                path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
                size: std::fs::metadata(&path).unwrap().len(),
                history_id: Some("50".to_string()),
                deleted_at: None,
                excluded_labels: Vec::new(),
            });
            // Simulate an earlier incremental run's own tracked exclusion
            // (via `mark_excluded`, not a raw `deleted_at`) — only that
            // precise tracking makes an automatic undelete safe (#1780).
            manifest.mark_excluded(
                id,
                Utc::now(),
                vec!["SPAM".to_string(), "TRASH".to_string()],
            );
        }
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        // m1 loses both excluded labels; m2 only loses SPAM
                        // and still carries TRASH.
                        "labelsRemoved": [
                            {"message": {"id": "m1", "threadId": "t1"}, "labelIds": ["SPAM", "TRASH"]},
                            {"message": {"id": "m2", "threadId": "t1"}, "labelIds": ["SPAM"]},
                        ],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string(), "TRASH".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Undeleted { id } if id == "m1")));
        assert!(
            !report
                .actions
                .iter()
                .any(|a| matches!(a, SyncAction::Undeleted { id } if id == "m2")),
            "m2 still carries an excluded label (TRASH) and must stay soft-deleted"
        );
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(manifest.get("m1").unwrap().deleted_at.is_none());
        assert!(manifest.get("m2").unwrap().deleted_at.is_some());
    }

    #[tokio::test]
    async fn run_full_sync_folds_known_system_labels_into_the_listing_query() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .and(wiremock::matchers::query_param("q", "-in:spam -in:trash"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"messages": []})),
            )
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string(), "TRASH".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
    }

    #[tokio::test]
    async fn run_full_sync_does_not_duplicate_an_in_token_already_named_by_query() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        // `--query "-in:spam"` and `--exclude-label SPAM` both name spam —
        // the folded query must not repeat the `-in:spam` token. The
        // caller's query is still parenthesized before `-in:trash` is
        // appended (#1780) even though nothing here actually needs the
        // protection — parenthesizing is unconditional once at least one
        // exclusion clause is appended, not just when the query contains an
        // `OR` that needs it.
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .and(wiremock::matchers::query_param("q", "(-in:spam) -in:trash"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"messages": []})),
            )
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let opts = SyncOptions {
            query: Some("-in:spam".to_string()),
            exclude_labels: vec!["SPAM".to_string(), "TRASH".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
    }

    #[tokio::test]
    async fn run_full_sync_parenthesizes_an_or_query_before_appending_an_exclusion() {
        // Gmail's implicit AND-by-juxtaposition binds tighter than an
        // explicit `OR` — appending `-in:spam` as a bare trailing token to
        // `from:x OR from:y` would parse as `from:x OR (from:y AND
        // -in:spam)`, silently narrowing the exclusion to only the
        // right-hand side. Parenthesizing the caller's query first
        // (`(from:x OR from:y) -in:spam`) applies it to the whole
        // expression instead (#1780).
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .and(wiremock::matchers::query_param(
                "q",
                "(from:x OR from:y) -in:spam",
            ))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"messages": []})),
            )
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let opts = SyncOptions {
            query: Some("from:x OR from:y".to_string()),
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
    }

    #[tokio::test]
    async fn run_full_sync_leaves_query_untouched_when_no_exclude_label_translates() {
        // Parenthesizing is conditional on actually appending an exclusion
        // clause — a caller who passes `--query` alone (no
        // `--exclude-label`, or one with no known translation) must see it
        // reach the API byte-for-byte, unchanged from before this flag
        // existed.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .and(wiremock::matchers::query_param("q", "from:x OR from:y"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"messages": []})),
            )
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let opts = SyncOptions {
            query: Some("from:x OR from:y".to_string()),
            exclude_labels: vec!["Label_16".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
    }

    #[tokio::test]
    async fn run_full_sync_notes_an_exclude_label_it_cannot_translate_into_a_query() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"messages": []})),
            )
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let opts = SyncOptions {
            exclude_labels: vec!["Label_16".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report.actions.iter().any(|a| matches!(
            a,
            SyncAction::Note { message } if message.contains("Label_16") && message.contains("--query")
        )));
    }

    #[tokio::test]
    async fn run_full_sync_vanished_record_is_never_bogus_undeleted_by_an_unrelated_label_change() {
        // A record `run_full_sync` soft-deletes via listing-absence never
        // learns *why* it vanished (`--query` can narrow a listing for
        // reasons that have nothing to do with `--exclude-label`, so
        // guessing would be unsound — see `run_full_sync`'s stale-deletion
        // doc comment). `excluded_labels` therefore stays empty for it, and
        // that's what a later unrelated `labelsRemoved` event must respect
        // (#1780): `reconcile_label_exclusion` only ever undeletes a record
        // whose `excluded_labels` shows it was *this* mechanism that
        // deleted it.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        // m1 is already archived, with label_ids that have never recorded
        // SPAM — the state a mailbox's existing archive is in the moment
        // `--exclude-label SPAM` first gets configured.
        let mut manifest = Manifest::default();
        let path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "From: a@example.com\r\n\r\nm1 body").unwrap();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: std::fs::metadata(&path).unwrap().len(),
            history_id: Some("50".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "100").await;
        // m1 no longer appears in the `-in:spam`-filtered listing — it's
        // been spammed on the server, though this pass never fetches it to
        // confirm that.
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .and(wiremock::matchers::query_param("q", "-in:spam"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"messages": []})),
            )
            .mount(&server)
            .await;

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Deleted { id } if id == "m1")));
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert!(record.deleted_at.is_some());
        assert!(
            record.excluded_labels.is_empty(),
            "run_full_sync doesn't know *why* a listing-absent record vanished, so it must \
             never guess and tag excluded_labels (#1780)"
        );
        assert_eq!(
            record.label_ids,
            vec!["INBOX".to_string()],
            "a plain stale-deletion must never rewrite label_ids"
        );

        // An incremental run now observes a totally unrelated label change
        // on the same message. run_incremental must not resurrect it: it
        // has no tracked exclusion reason for m1 at all, so
        // reconcile_label_exclusion's undelete branch can't fire regardless
        // of what changed.
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "labelsRemoved": [
                            {"message": {"id": "m1", "threadId": "t1"}, "labelIds": ["IMPORTANT"]},
                        ],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;

        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(
            !report
                .actions
                .iter()
                .any(|a| matches!(a, SyncAction::Undeleted { id } if id == "m1")),
            "m1 has no tracked exclusion reason; an unrelated label change must not resurrect it"
        );
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(manifest.get("m1").unwrap().deleted_at.is_some());
    }

    #[tokio::test]
    async fn run_full_sync_reappearance_does_not_undelete_a_record_excluded_by_a_custom_label() {
        // A record run_incremental soft-deleted via a custom (untranslated)
        // `--exclude-label` must not be resurrected just because it still
        // appears in a full-sync listing that couldn't have filtered that
        // label out server-side (#1780) — see run_full_sync's
        // undelete-on-reappearance doc comment.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let mut manifest = Manifest::default();
        let path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "From: a@example.com\r\n\r\nm1 body").unwrap();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string(), "Label_16".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: std::fs::metadata(&path).unwrap().len(),
            history_id: Some("50".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        // Simulate a prior incremental run's tracked exclusion via the
        // custom label.
        manifest.mark_excluded("m1", Utc::now(), vec!["Label_16".to_string()]);
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "100").await;
        // No `-in:` clause can express `Label_16`, so the listing is
        // unfiltered — m1 still appears in it even though it's still
        // excluded per our own tracking.
        mount_message_list(&server, &["m1"]).await;

        let opts = SyncOptions {
            exclude_labels: vec!["Label_16".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(
            !report
                .actions
                .iter()
                .any(|a| matches!(a, SyncAction::Undeleted { id } if id == "m1")),
            "m1 is still tracked as excluded by a custom label the listing query couldn't \
             filter; merely reappearing in it must not resurrect it"
        );
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert!(record.deleted_at.is_some());
        assert_eq!(record.excluded_labels, vec!["Label_16".to_string()]);
    }

    #[tokio::test]
    async fn run_full_sync_reappearance_undeletes_a_record_once_a_translated_label_is_confirmed_gone(
    ) {
        // Unlike a custom label, a translated one (SPAM/TRASH) reappearing
        // in a listing this run's own query excludes it from is
        // authoritative proof it's gone — the reappearance-undelete must
        // still fire even though excluded_labels is non-empty (#1780).
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let mut manifest = Manifest::default();
        let path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "From: a@example.com\r\n\r\nm1 body").unwrap();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: std::fs::metadata(&path).unwrap().len(),
            history_id: Some("50".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.mark_excluded("m1", Utc::now(), vec!["SPAM".to_string()]);
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "100").await;
        // m1 reappears in the `-in:spam`-filtered listing: confirmation
        // it's no longer spam on the server.
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .and(wiremock::matchers::query_param("q", "-in:spam"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "messages": [{"id": "m1", "threadId": "t1"}]
                })),
            )
            .mount(&server)
            .await;

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Undeleted { id } if id == "m1")));
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert!(record.deleted_at.is_none());
        assert!(record.excluded_labels.is_empty());
    }

    #[tokio::test]
    async fn run_incremental_via_run_sync_with_progress_emits_fetch_events() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "messagesAdded": [{"message": {"id": "m1", "threadId": "t1"}}],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;
        mount_raw_get(&server, "m1", "New message").await;

        let (tx, mut rx) = mpsc::unbounded_channel();
        let report = run_sync_with_progress(&client, &opts(output_dir), Some(&tx))
            .await
            .unwrap();
        drop(tx);

        let mut events = Vec::new();
        while let Some(event) = rx.recv().await {
            events.push(event);
        }

        assert!(report.errors.is_empty());
        // The bars must not sit frozen at their initial state for an
        // incremental run's whole duration (the bug this test guards
        // against): at least one real listing update plus a matched
        // queued/completed pair for the one message fetched.
        assert!(events.iter().any(|e| matches!(
            e,
            SyncProgressEvent::ListingPage {
                ids_discovered: 1,
                ..
            }
        )));
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, SyncProgressEvent::ListingDone))
                .count(),
            1
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, SyncProgressEvent::FetchQueued))
                .count(),
            1
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, SyncProgressEvent::FetchCompleted { failed: false }))
                .count(),
            1
        );
    }

    // ── account-identity validation ────────────────────────────────────

    #[tokio::test]
    async fn run_sync_rejects_a_state_json_for_a_different_account() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "1".to_string(),
                email_address: "wrong@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();
        let manifest_before = b"".to_vec();
        std::fs::write(manifest_path(&output_dir), &manifest_before).unwrap();

        mount_profile(&server, "user@example.com", "999").await;

        let err = run_sync(&client, &opts(output_dir.clone()))
            .await
            .unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("wrong@example.com"));
        assert!(msg.contains("user@example.com"));

        // Neither file was touched by the rejected run.
        assert_eq!(
            std::fs::read(manifest_path(&output_dir)).unwrap(),
            manifest_before
        );
        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => assert_eq!(s.email_address, "wrong@example.com"),
            _ => panic!("expected the original (mismatched) state to survive untouched"),
        }
    }

    // ── idempotence ────────────────────────────────────────────────────

    #[tokio::test]
    async fn run_sync_rerun_with_no_server_side_changes_fetches_and_writes_nothing_new() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        mount_message_list(&server, &["m1"]).await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .and(wiremock::matchers::query_param("format", "raw"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "id": "m1", "threadId": "t1", "labelIds": ["INBOX"],
                "internalDate": "1700000000000", "historyId": "500",
                "raw": raw_message_body("m1", "Hello"),
            })))
            .expect(1) // exactly once across BOTH runs below
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let o = opts(output_dir.clone());
        let first = run_sync(&client, &o).await.unwrap();
        assert!(first.errors.is_empty());
        let manifest_before = std::fs::read(manifest_path(&output_dir)).unwrap();

        // Second run takes the incremental path against the watermark the
        // first run just persisted; an empty history means nothing to do.
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [], "historyId": "500",
                })),
            )
            .mount(&server)
            .await;

        let second = run_sync(&client, &o).await.unwrap();
        assert!(second
            .actions
            .iter()
            .all(|a| !matches!(a, SyncAction::Fetched { .. })));
        let manifest_after = std::fs::read(manifest_path(&output_dir)).unwrap();
        assert_eq!(manifest_before, manifest_after);
    }

    // ── interruption / the Facebook-harvester regression ────────────────

    #[tokio::test]
    async fn run_sync_survives_a_missing_state_json_without_refetching_or_truncating() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");

        // An existing archive from a prior, already-completed run.
        let eml_path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(eml_path.parent().unwrap()).unwrap();
        let original_bytes = b"From: a@example.com\r\n\r\nOriginal body.".to_vec();
        std::fs::write(&eml_path, &original_bytes).unwrap();
        let mut manifest = Manifest::default();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: eml_path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: original_bytes.len() as u64,
            history_id: Some("1".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.save(&manifest_path(&output_dir)).unwrap();
        // `state.json` is deliberately absent — as if it were deleted, or
        // this is the very first run after `preload_prior`/`open_sink`-style
        // machinery would otherwise have truncated an existing archive.

        mount_profile(&server, "user@example.com", "999").await;
        mount_message_list(&server, &["m1"]).await;
        // Deliberately no mock for GET .../messages/m1 — a re-fetch here
        // would mean the regression reappeared.

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report
            .actions
            .iter()
            .all(|a| !matches!(a, SyncAction::Fetched { id, .. } if id == "m1")));
        assert_eq!(
            std::fs::read(&eml_path).unwrap(),
            original_bytes,
            "the pre-existing archive must survive a missing state.json intact"
        );
        let manifest_after = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert_eq!(
            manifest_after.get("m1").unwrap().size,
            original_bytes.len() as u64
        );
    }

    // ── per-item failure ────────────────────────────────────────────────

    #[tokio::test]
    async fn run_sync_one_unfetchable_message_yields_an_error_and_the_run_completes() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "999").await;
        mount_message_list(&server, &["m1", "m2"]).await;
        mount_raw_get(&server, "m1", "Good").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m2"))
            .respond_with(wiremock::ResponseTemplate::new(500).set_body_string("boom"))
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert_eq!(report.errors.len(), 1);
        assert_eq!(report.errors[0].id, "m2");
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Fetched { id, .. } if id == "m1")));

        // A backfill with an error still records its watermark, carrying
        // the failed id forward, so the next run is incremental rather than
        // another full-mailbox listing (#1784).
        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => {
                assert_eq!(s.history_id, "999");
                assert_eq!(s.pending_fetch, ["m2"]);
            }
            _ => panic!("expected state.json to be written"),
        }
        // And the successfully-fetched message's manifest entry survives.
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(manifest.get("m1").is_some());
    }

    #[tokio::test]
    async fn run_sync_advances_the_watermark_and_carries_an_incremental_failure_forward() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "history": [{"id": "150", "messagesAdded": [{"message": {"id": "m1", "threadId": "t1"}}]}],
                "historyId": "200",
            })))
            .mount(&server)
            .await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .respond_with(wiremock::ResponseTemplate::new(500).set_body_string("boom"))
            .mount(&server)
            .await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();
        assert_eq!(report.errors.len(), 1);

        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => {
                assert_eq!(s.history_id, "200", "watermark must advance");
                assert_eq!(s.pending_fetch, ["m1"], "the failure is carried forward");
            }
            _ => panic!("expected state.json to be written"),
        }
    }

    // ── vanished message during fetch (#1509) ───────────────────────────

    #[tokio::test]
    async fn run_sync_treats_a_vanished_message_404_as_benign_not_an_error() {
        // A message reported by history.list can be permanently deleted from
        // the server before its own messages.get call runs — real once you
        // add concurrency to the fetch fan-out. Unlike an ordinary
        // per-message failure, this can never succeed on retry, so it must
        // not be carried forward in `pending_fetch` (unlike
        // `run_sync_advances_the_watermark_and_carries_an_incremental_failure_forward`
        // above, whose 500 genuinely is worth retrying).
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "messagesAdded": [{"message": {"id": "vanished1", "threadId": "t1"}}],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path(
                "/gmail/v1/users/me/messages/vanished1",
            ))
            .respond_with(
                wiremock::ResponseTemplate::new(404).set_body_json(serde_json::json!({
                    "error": {"message": "Not Found", "errors": [{"reason": "notFound"}]}
                })),
            )
            .mount(&server)
            .await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(
            report.errors.is_empty(),
            "a vanished message must not be a SyncError"
        );
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Vanished { id } if id == "vanished1")));

        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => assert_eq!(
                s.history_id, "300",
                "watermark must advance past a vanished-message 404"
            ),
            _ => panic!("expected the watermark to be saved"),
        }
    }

    #[tokio::test]
    async fn run_sync_message_404_with_a_different_reason_is_still_an_error() {
        // The reason check must not fail open the way `is_history_not_found`
        // deliberately does for `history.list` — a 404 on messages.get for
        // any reason other than `notFound` is a real failure and must still
        // be carried forward for retry.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "messagesAdded": [{"message": {"id": "m1", "threadId": "t1"}}],
                    }],
                    "historyId": "300",
                })),
            )
            .mount(&server)
            .await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .respond_with(
                wiremock::ResponseTemplate::new(404).set_body_json(serde_json::json!({
                    "error": {"message": "Backend error", "errors": [{"reason": "backendError"}]}
                })),
            )
            .mount(&server)
            .await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert_eq!(report.errors.len(), 1);
        assert_eq!(report.errors[0].id, "m1");
        assert!(!report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Vanished { .. })));

        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => {
                assert_eq!(s.history_id, "300", "watermark must advance");
                assert_eq!(
                    s.pending_fetch,
                    ["m1"],
                    "a non-notFound 404 is retried next run"
                );
            }
            _ => panic!("expected state.json to be written"),
        }
    }

    // ── pending_fetch carry-forward (#1784) ─────────────────────────────

    fn save_state_with_pending(output_dir: &Path, pending: &[&str]) {
        std::fs::create_dir_all(output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "100".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: pending.iter().map(|id| (*id).to_string()).collect(),
            },
            &state_path(output_dir),
        )
        .unwrap();
    }

    async fn mount_history(server: &wiremock::MockServer, body: serde_json::Value) {
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(body))
            .mount(server)
            .await;
    }

    async fn mount_get_never_called(server: &wiremock::MockServer, id: &str) {
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path(format!(
                "/gmail/v1/users/me/messages/{id}"
            )))
            .respond_with(wiremock::ResponseTemplate::new(500))
            .expect(0)
            .mount(server)
            .await;
    }

    fn load_present(output_dir: &Path) -> ArchiveState {
        match state::load(&state_path(output_dir)) {
            LoadOutcome::Present(s) => s,
            _ => panic!("expected state.json to be written"),
        }
    }

    #[tokio::test]
    async fn run_sync_retries_a_pending_fetch_even_with_an_empty_history_window() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1"]);
        mount_profile(&server, "user@example.com", "999").await;
        mount_history(&server, serde_json::json!({"historyId": "200"})).await;
        mount_raw_get(&server, "m1", "Retried").await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Fetched { id, .. } if id == "m1")));
        assert!(report.actions.iter().any(|a| matches!(
            a,
            SyncAction::Note { message } if message.contains("retrying 1 message")
        )));
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(manifest.get("m1").is_some());
        let s = load_present(&output_dir);
        assert_eq!(s.history_id, "200");
        assert!(s.pending_fetch.is_empty());
    }

    #[tokio::test]
    async fn run_sync_keeps_a_pending_fetch_that_fails_again_and_still_advances() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1"]);
        mount_profile(&server, "user@example.com", "999").await;
        mount_history(&server, serde_json::json!({"historyId": "200"})).await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .respond_with(wiremock::ResponseTemplate::new(500).set_body_string("boom"))
            .mount(&server)
            .await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert_eq!(report.errors.len(), 1);
        let s = load_present(&output_dir);
        assert_eq!(s.history_id, "200");
        assert_eq!(s.pending_fetch, ["m1"]);
    }

    #[tokio::test]
    async fn run_sync_drops_a_pending_fetch_that_has_since_vanished() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1"]);
        mount_profile(&server, "user@example.com", "999").await;
        mount_history(&server, serde_json::json!({"historyId": "200"})).await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .respond_with(
                wiremock::ResponseTemplate::new(404).set_body_json(serde_json::json!({
                    "error": {"message": "Not Found", "errors": [{"reason": "notFound"}]}
                })),
            )
            .mount(&server)
            .await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Vanished { id } if id == "m1")));
        assert!(load_present(&output_dir).pending_fetch.is_empty());
    }

    #[tokio::test]
    async fn run_sync_does_not_refetch_a_pending_id_already_archived_on_disk() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1"]);

        let mut manifest = Manifest::default();
        let path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "From: a@example.com\r\n\r\nm1 body").unwrap();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: std::fs::metadata(&path).unwrap().len(),
            history_id: Some("50".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        mount_history(&server, serde_json::json!({"historyId": "200"})).await;
        mount_get_never_called(&server, "m1").await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(load_present(&output_dir).pending_fetch.is_empty());
    }

    #[tokio::test]
    async fn run_sync_does_not_fetch_a_pending_id_deleted_in_this_window() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1"]);
        mount_profile(&server, "user@example.com", "999").await;
        mount_history(
            &server,
            serde_json::json!({
                "history": [{
                    "id": "150",
                    "messagesDeleted": [{"message": {"id": "m1", "threadId": "t1"}}],
                }],
                "historyId": "200",
            }),
        )
        .await;
        mount_get_never_called(&server, "m1").await;

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(load_present(&output_dir).pending_fetch.is_empty());
    }

    #[tokio::test]
    async fn run_sync_soft_deletes_a_retried_pending_fetch_that_now_carries_an_excluded_label() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1"]);
        mount_profile(&server, "user@example.com", "999").await;
        mount_history(&server, serde_json::json!({"historyId": "200"})).await;
        // Not `mount_raw_get` (hardcoded to `labelIds: ["INBOX"]`): the
        // label that moved it into SPAM arrived in a window an earlier run
        // consumed, so only the fetch itself can tell this run about it.
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "id": "m1",
                    "threadId": "t1",
                    "labelIds": ["INBOX", "SPAM"],
                    "internalDate": "1700000000000",
                    "historyId": "500",
                    "raw": raw_message_body("m1", "Spammy"),
                })),
            )
            .mount(&server)
            .await;

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert!(record.deleted_at.is_some());
        assert_eq!(record.excluded_labels, ["SPAM"]);
    }

    #[tokio::test]
    async fn run_sync_full_replaces_pending_fetch_with_only_this_runs_errors() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        // "gone" no longer exists server-side, so a full listing drops it;
        // "m2" fails on this run and becomes the new pending set.
        save_state_with_pending(&output_dir, &["gone"]);
        mount_profile(&server, "user@example.com", "999").await;
        mount_message_list(&server, &["m1", "m2"]).await;
        mount_raw_get(&server, "m1", "Good").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m2"))
            .respond_with(wiremock::ResponseTemplate::new(500).set_body_string("boom"))
            .mount(&server)
            .await;
        mount_get_never_called(&server, "gone").await;

        let opts = SyncOptions {
            full: true,
            ..opts(output_dir.clone())
        };
        run_sync(&client, &opts).await.unwrap();

        let s = load_present(&output_dir);
        assert_eq!(s.history_id, "999");
        assert_eq!(s.pending_fetch, ["m2"]);
    }

    #[tokio::test]
    async fn run_sync_full_with_query_carries_forward_a_pending_id_it_did_not_list() {
        // A `--query`-scoped listing never looked for "outside", so its
        // absence proves nothing: it must stay pending for the next
        // incremental run rather than be dropped. "m1" was listed, so the
        // listing itself retried it and it is not carried.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1", "outside"]);
        mount_profile(&server, "user@example.com", "999").await;
        mount_message_list(&server, &["m1"]).await;
        mount_raw_get(&server, "m1", "Listed").await;
        mount_get_never_called(&server, "outside").await;

        let opts = SyncOptions {
            full: true,
            query: Some("from:boss@example.com".to_string()),
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        let s = load_present(&output_dir);
        assert_eq!(s.history_id, "999");
        assert_eq!(s.pending_fetch, ["outside"]);
    }

    #[tokio::test]
    async fn run_sync_excludes_a_pending_retry_also_labeled_this_window() {
        // A pending-retry id can also be touched by a labelsAdded/labelsRemoved
        // event in the very window that retries it, so it appears in both
        // `pending_retried` and `labels_touched`. It must still have its
        // exclusion reconciled against the labels the retry fetch returns.
        // This does not pin that the reconcile runs only once:
        // `reconcile_label_exclusion` is idempotent (a second call finds the
        // record already excluded for the same labels and pushes no action),
        // so a duplicate call is unobservable here.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        save_state_with_pending(&output_dir, &["m1"]);
        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "history": [{
                        "id": "150",
                        "labelsAdded": [
                            {"message": {"id": "m1", "threadId": "t1"}, "labelIds": ["SPAM"]},
                        ],
                    }],
                    "historyId": "200",
                })),
            )
            .mount(&server)
            .await;
        // Not `mount_raw_get` (hardcoded to `labelIds: ["INBOX"]`): the
        // retry fetch is what tells this run m1 now carries SPAM — the
        // labelsAdded event above only proves the label was added, not
        // what else is currently set (m1 was never archived by an earlier
        // run, so there's no prior manifest label set to merge onto).
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/m1"))
            .and(wiremock::matchers::query_param("format", "raw"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "id": "m1",
                    "threadId": "t1",
                    "labelIds": ["INBOX", "SPAM"],
                    "internalDate": "1700000000000",
                    "historyId": "500",
                    "raw": raw_message_body("m1", "Retried and spammed"),
                })),
            )
            .mount(&server)
            .await;

        let opts = SyncOptions {
            exclude_labels: vec!["SPAM".to_string()],
            ..opts(output_dir.clone())
        };
        let report = run_sync(&client, &opts).await.unwrap();

        assert!(report.errors.is_empty());
        let deleted_count = report
            .actions
            .iter()
            .filter(|a| matches!(a, SyncAction::Deleted { id } if id == "m1"))
            .count();
        assert_eq!(
            deleted_count, 1,
            "the retried id must be soft-deleted as excluded"
        );
        let manifest = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let record = manifest.get("m1").unwrap();
        assert!(record.deleted_at.is_some());
        assert_eq!(record.excluded_labels, ["SPAM"]);
        assert!(load_present(&output_dir).pending_fetch.is_empty());
    }

    // ── --dry-run ──────────────────────────────────────────────────────

    #[tokio::test]
    async fn run_sync_dry_run_reports_planned_actions_and_touches_no_files() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "999").await;
        mount_message_list(&server, &["m1"]).await;
        // Deliberately no mock for GET .../messages/m1 — dry-run must never
        // fetch a message body.

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        let report = run_sync(
            &client,
            &SyncOptions {
                output_dir: output_dir.clone(),
                query: None,
                exclude_labels: Vec::new(),
                full: false,
                concurrency: 4,
                dry_run: true,
                extract_attachments: false,
                shared_pool: None,
            },
        )
        .await
        .unwrap();

        assert!(matches!(
            report.actions.as_slice(),
            [SyncAction::WouldFetch { id }] if id == "m1"
        ));
        assert!(
            !output_dir.exists(),
            "dry-run must not create the output directory"
        );
    }

    // ── guard_output_dir ─────────────────────────────────────────────────

    #[test]
    fn guard_output_dir_rejects_home_directory_itself() {
        let _guard = crate::gmail::test_support::EnvGuard::take();
        let home = tempfile::tempdir().unwrap();
        std::env::set_var("HOME", home.path());
        let err = guard_output_dir(home.path()).unwrap_err();
        assert!(err.to_string().contains("home directory"));
    }

    #[test]
    fn guard_output_dir_rejects_inside_omni_dev_settings_dir() {
        let _guard = crate::gmail::test_support::EnvGuard::take();
        let home = tempfile::tempdir().unwrap();
        std::env::set_var("HOME", home.path());
        let target = home.path().join(".omni-dev").join("mail-archive");
        std::fs::create_dir_all(&target).unwrap();
        let err = guard_output_dir(&target).unwrap_err();
        assert!(err.to_string().contains("settings directory"));
    }

    #[test]
    fn guard_output_dir_accepts_an_ordinary_subdirectory() {
        let _guard = crate::gmail::test_support::EnvGuard::take();
        let home = tempfile::tempdir().unwrap();
        std::env::set_var("HOME", home.path());
        assert!(guard_output_dir(&home.path().join("mail-archive")).is_ok());
    }

    // ── corrupt manifest is a hard failure ──────────────────────────────

    #[tokio::test]
    async fn run_sync_propagates_a_corrupt_manifest_as_a_hard_error() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "1").await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        std::fs::write(manifest_path(&output_dir), "not json\n").unwrap();

        let err = run_sync(&client, &opts(output_dir)).await.unwrap_err();
        assert!(err.to_string().contains("Failed to parse manifest"));
    }

    // ── reconciliation must not truncate at HARD_CAP (#1467) ────────────

    #[tokio::test]
    async fn run_sync_full_reconciliation_does_not_mislabel_mail_past_hard_cap() {
        use crate::gmail::messages_api::HARD_CAP;

        /// Serves `ids` back across as many `messages.list` pages as it
        /// takes, terminating only when exhausted — mirrors real Gmail
        /// pagination rather than hand-mounting one `Mock` per page.
        struct SequentialIdPages {
            ids: Vec<String>,
            calls: std::sync::atomic::AtomicUsize,
        }
        impl wiremock::Respond for SequentialIdPages {
            fn respond(&self, _req: &wiremock::Request) -> wiremock::ResponseTemplate {
                let call = self.calls.fetch_add(1, Ordering::SeqCst);
                let page_size = 500usize;
                let start = call * page_size;
                let page: Vec<serde_json::Value> = self
                    .ids
                    .iter()
                    .skip(start)
                    .take(page_size)
                    .map(|id| serde_json::json!({"id": id, "threadId": "t1"}))
                    .collect();
                let mut body = serde_json::json!({"messages": page});
                if start + page_size < self.ids.len() {
                    body["nextPageToken"] = serde_json::json!(format!("token-{}", call + 1));
                }
                wiremock::ResponseTemplate::new(200).set_body_json(body)
            }
        }

        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        // One more id than `HARD_CAP`. Before the fix, `run_full_sync`'s
        // listing truncated to `HARD_CAP`, so every id past that point
        // would have been wrongly soft-deleted below, even though the
        // listing (mocked here to return every one of them) says otherwise.
        let ids: Vec<String> = (0..HARD_CAP + 10).map(|i| format!("m{i}")).collect();
        let mut manifest = Manifest::default();
        for id in &ids {
            let path = shard_path(&output_dir, id, None);
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(&path, b"From: a@example.com\r\n\r\nbody").unwrap();
            manifest.upsert(ManifestRecord {
                id: id.clone(),
                thread_id: Some("t1".to_string()),
                label_ids: vec!["INBOX".to_string()],
                internal_date: None,
                subject: None,
                from: None,
                to: None,
                rfc822_msgid: None,
                in_reply_to: None,
                references: None,
                attachment_count: 0,
                attachment_filenames: Vec::new(),
                path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
                size: 4,
                history_id: Some("1".to_string()),
                deleted_at: None,
                excluded_labels: Vec::new(),
            });
        }
        manifest.save(&manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages"))
            .respond_with(SequentialIdPages {
                ids: ids.clone(),
                calls: std::sync::atomic::AtomicUsize::new(0),
            })
            .mount(&server)
            .await;
        // Deliberately no mock for any `messages/<id>?format=raw` fetch —
        // every id is already archived on disk, so a re-fetch here would
        // mean the presence-on-disk check regressed too.

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty(), "no id should need re-fetching");
        assert!(
            !report
                .actions
                .iter()
                .any(|a| matches!(a, SyncAction::Deleted { .. })),
            "no already-archived message should be marked deleted just because it fell \
             outside a truncated listing"
        );
        let manifest_after = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert_eq!(
            manifest_after.ids_not_deleted().count(),
            ids.len(),
            "every id should still be present and not soft-deleted"
        );
    }

    // ── manifest checkpointing during the fetch loop (#1467) ─────────────

    #[tokio::test]
    async fn fetch_and_archive_messages_checkpoints_the_manifest_across_multiple_intervals() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let total = MANIFEST_CHECKPOINT_INTERVAL * 2 + 5;
        let ids: Vec<String> = (0..total).map(|i| format!("m{i}")).collect();
        for id in &ids {
            mount_raw_get(&server, id, "Hello").await;
        }

        let mut manifest = Manifest::default();
        let limiter = TokenBucket::new(1_000_000, 1_000_000);
        let mut report = SyncReport::default();
        let opts = SyncOptions {
            output_dir: output_dir.clone(),
            query: None,
            exclude_labels: Vec::new(),
            full: false,
            concurrency: 20,
            dry_run: false,
            extract_attachments: false,
            shared_pool: None,
        };

        fetch_and_archive_messages(
            &client,
            &mut manifest,
            &ids,
            &limiter,
            &opts,
            &mut report,
            None,
        )
        .await
        .unwrap();
        assert!(report.errors.is_empty());

        // Two checkpoint boundaries' worth of records should already have
        // landed on disk *before* any final save — proving multiple
        // checkpoints accumulate correctly rather than each one clobbering
        // the last. `buffer_unordered` completes out of dispatch order, so
        // this checks a count, not which specific ids made it.
        let checkpointed = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let expected_checkpointed =
            (total / MANIFEST_CHECKPOINT_INTERVAL) * MANIFEST_CHECKPOINT_INTERVAL;
        assert_eq!(
            checkpointed.ids_not_deleted().count(),
            expected_checkpointed,
            "expected exactly two checkpoints' worth of records on disk before any final save"
        );

        // The final partial interval (the last 5 ids) is only on disk once
        // the caller does its own final save — the same thing `run_sync`
        // always does after this function returns.
        manifest.save(&manifest_path(&output_dir)).unwrap();
        let final_on_disk = Manifest::load(&manifest_path(&output_dir)).unwrap();
        for id in &ids {
            assert!(
                final_on_disk.get(id).is_some(),
                "missing {id} after the final save"
            );
        }
    }

    #[tokio::test]
    async fn fetch_and_archive_messages_checkpoints_before_the_whole_batch_completes() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let fast_ids: Vec<String> = (0..MANIFEST_CHECKPOINT_INTERVAL)
            .map(|i| format!("fast{i}"))
            .collect();
        let slow_ids: Vec<String> = (0..5).map(|i| format!("slow{i}")).collect();
        for id in &fast_ids {
            mount_raw_get(&server, id, "Hello").await;
        }
        for id in &slow_ids {
            wiremock::Mock::given(wiremock::matchers::method("GET"))
                .and(wiremock::matchers::path(format!(
                    "/gmail/v1/users/me/messages/{id}"
                )))
                .and(wiremock::matchers::query_param("format", "raw"))
                .respond_with(
                    wiremock::ResponseTemplate::new(200)
                        .set_body_json(serde_json::json!({
                            "id": id, "threadId": "t1", "labelIds": ["INBOX"],
                            "internalDate": "1700000000000", "historyId": "500",
                            "raw": raw_message_body(id, "Slow"),
                        }))
                        .set_delay(std::time::Duration::from_secs(3600)),
                )
                .mount(&server)
                .await;
        }

        let mut ids = fast_ids.clone();
        ids.extend(slow_ids.clone());
        let mut manifest = Manifest::default();
        let limiter = TokenBucket::new(1_000_000, 1_000_000);
        let mut report = SyncReport::default();
        let opts = SyncOptions {
            output_dir: output_dir.clone(),
            query: None,
            exclude_labels: Vec::new(),
            full: false,
            concurrency: 20,
            dry_run: false,
            extract_attachments: false,
            shared_pool: None,
        };

        // The slow ids' 3600s delay never elapses within this test, so the
        // only way `poll_checkpoint` can win this race is if the manifest
        // was actually flushed to disk *during* the fetch loop — proving
        // checkpointing isn't just an artifact of the final save `run_sync`
        // does once this function returns.
        let poll_checkpoint = async {
            loop {
                if let Ok(on_disk) = Manifest::load(&manifest_path(&output_dir)) {
                    if fast_ids.iter().all(|id| on_disk.get(id).is_some()) {
                        return;
                    }
                }
                tokio::time::sleep(std::time::Duration::from_millis(20)).await;
            }
        };

        tokio::time::timeout(std::time::Duration::from_secs(10), async {
            tokio::select! {
                _ = fetch_and_archive_messages(
                    &client, &mut manifest, &ids, &limiter, &opts, &mut report, None,
                ) => {
                    panic!(
                        "fetch_and_archive_messages returned before the slow ids' delay could \
                         possibly elapse — checkpointing must have regressed"
                    );
                }
                () = poll_checkpoint => {}
            }
        })
        .await
        .expect("manifest was never checkpointed for the fast batch within 10s");

        let on_disk = Manifest::load(&manifest_path(&output_dir)).unwrap();
        for id in &fast_ids {
            assert!(
                on_disk.get(id).is_some(),
                "checkpoint should have covered {id}"
            );
        }
    }

    // ── pipelined listing+fetch and progress events (#1502) ──────────────

    #[tokio::test]
    async fn fetch_and_archive_messages_streaming_checkpoints_the_manifest_across_multiple_intervals(
    ) {
        // Mirrors `fetch_and_archive_messages_checkpoints_the_manifest_across_multiple_intervals`
        // above, but feeds ids through a channel instead of a slice — the
        // highest-risk piece of new async control flow (the `select!` pump)
        // must preserve the exact same checkpointing behavior.
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let total = MANIFEST_CHECKPOINT_INTERVAL * 2 + 5;
        let ids: Vec<String> = (0..total).map(|i| format!("m{i}")).collect();
        for id in &ids {
            mount_raw_get(&server, id, "Hello").await;
        }

        let mut manifest = Manifest::default();
        let limiter = TokenBucket::new(1_000_000, 1_000_000);
        let mut report = SyncReport::default();
        let opts = SyncOptions {
            output_dir: output_dir.clone(),
            query: None,
            exclude_labels: Vec::new(),
            full: false,
            concurrency: 20,
            dry_run: false,
            extract_attachments: false,
            shared_pool: None,
        };

        let (ids_tx, ids_rx) = mpsc::unbounded_channel();
        for id in &ids {
            ids_tx.send(id.clone()).unwrap();
        }
        drop(ids_tx);

        let listed_ids = fetch_and_archive_messages_streaming(
            &client,
            &mut manifest,
            ids_rx,
            &limiter,
            &opts,
            &mut report,
            None,
        )
        .await
        .unwrap();
        assert!(report.errors.is_empty());
        assert_eq!(listed_ids.len(), total);

        let checkpointed = Manifest::load(&manifest_path(&output_dir)).unwrap();
        let expected_checkpointed =
            (total / MANIFEST_CHECKPOINT_INTERVAL) * MANIFEST_CHECKPOINT_INTERVAL;
        assert_eq!(
            checkpointed.ids_not_deleted().count(),
            expected_checkpointed,
            "expected exactly two checkpoints' worth of records on disk before any final save"
        );

        manifest.save(&manifest_path(&output_dir)).unwrap();
        let final_on_disk = Manifest::load(&manifest_path(&output_dir)).unwrap();
        for id in &ids {
            assert!(
                final_on_disk.get(id).is_some(),
                "missing {id} after the final save"
            );
        }
    }

    #[tokio::test]
    async fn fetch_and_archive_messages_streaming_emits_queued_and_completed_progress_events() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        mount_raw_get(&server, "ok1", "Hello").await;
        mount_raw_get(&server, "ok2", "Hello").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/messages/bad1"))
            .and(wiremock::matchers::query_param("format", "raw"))
            .respond_with(wiremock::ResponseTemplate::new(500).set_body_string("boom"))
            .mount(&server)
            .await;

        let mut manifest = Manifest::default();
        let limiter = TokenBucket::new(1_000_000, 1_000_000);
        let mut report = SyncReport::default();
        let opts = opts(output_dir.clone());

        let (ids_tx, ids_rx) = mpsc::unbounded_channel();
        for id in ["ok1", "ok2", "bad1"] {
            ids_tx.send(id.to_string()).unwrap();
        }
        drop(ids_tx);

        let (progress_tx, mut progress_rx) = mpsc::unbounded_channel();
        fetch_and_archive_messages_streaming(
            &client,
            &mut manifest,
            ids_rx,
            &limiter,
            &opts,
            &mut report,
            Some(&progress_tx),
        )
        .await
        .unwrap();
        drop(progress_tx);

        let mut events = Vec::new();
        while let Some(event) = progress_rx.recv().await {
            events.push(event);
        }

        assert_eq!(report.errors.len(), 1);
        let queued = events
            .iter()
            .filter(|e| matches!(e, SyncProgressEvent::FetchQueued))
            .count();
        let completed_ok = events
            .iter()
            .filter(|e| matches!(e, SyncProgressEvent::FetchCompleted { failed: false }))
            .count();
        let completed_failed = events
            .iter()
            .filter(|e| matches!(e, SyncProgressEvent::FetchCompleted { failed: true }))
            .count();
        assert_eq!(queued, 3, "one FetchQueued per dispatched fetch");
        assert_eq!(completed_ok, 2);
        assert_eq!(completed_failed, 1);
    }

    #[tokio::test]
    async fn run_sync_with_progress_emits_listing_events_ending_in_listing_done() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "500").await;
        mount_message_list(&server, &["m1", "m2"]).await;
        mount_raw_get(&server, "m1", "Hello").await;
        mount_raw_get(&server, "m2", "Hello").await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");

        let (tx, mut rx) = mpsc::unbounded_channel();
        let report = run_sync_with_progress(&client, &opts(output_dir), Some(&tx))
            .await
            .unwrap();
        drop(tx);

        let mut events = Vec::new();
        while let Some(event) = rx.recv().await {
            events.push(event);
        }

        assert!(report.errors.is_empty());
        assert!(
            events
                .iter()
                .any(|e| matches!(e, SyncProgressEvent::ListingPage { .. })),
            "expected at least one ListingPage event"
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, SyncProgressEvent::ListingDone))
                .count(),
            1,
            "expected exactly one ListingDone event"
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, SyncProgressEvent::FetchQueued))
                .count(),
            2
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, SyncProgressEvent::FetchCompleted { failed: false }))
                .count(),
            2
        );
        // Both sides of the `tokio::join!` have already finished by the
        // time `run_full_sync` sends `ListingDone` — it's always the last
        // event this run emits.
        assert!(matches!(
            events.last(),
            Some(SyncProgressEvent::ListingDone)
        ));
    }

    #[tokio::test]
    async fn run_sync_full_reconciliation_undeletes_reappeared_mail_and_deletes_vanished_mail() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        mount_profile(&server, "user@example.com", "999").await;
        // Only "back" is listed as still present on the server; "gone" no
        // longer appears at all.
        mount_message_list(&server, &["back"]).await;
        mount_raw_get(&server, "back", "Hello").await;

        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let back_path = shard_path(&output_dir, "back", None);
        std::fs::create_dir_all(back_path.parent().unwrap()).unwrap();
        std::fs::write(&back_path, b"From: a@example.com\r\n\r\nbody").unwrap();
        let gone_path = shard_path(&output_dir, "gone", None);
        std::fs::write(&gone_path, b"From: a@example.com\r\n\r\nbody").unwrap();

        let mut manifest = Manifest::default();
        manifest.upsert(ManifestRecord {
            id: "back".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: back_path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: 4,
            history_id: Some("1".to_string()),
            // Previously soft-deleted; the server lists it again this run.
            deleted_at: Some(Utc::now()),
            excluded_labels: Vec::new(),
        });
        manifest.upsert(ManifestRecord {
            id: "gone".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: gone_path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: 4,
            history_id: Some("1".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.save(&manifest_path(&output_dir)).unwrap();

        let report = run_sync(&client, &opts(output_dir.clone())).await.unwrap();

        assert!(report.errors.is_empty());
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Undeleted { id } if id == "back")));
        assert!(report
            .actions
            .iter()
            .any(|a| matches!(a, SyncAction::Deleted { id } if id == "gone")));

        let on_disk = Manifest::load(&manifest_path(&output_dir)).unwrap();
        assert!(on_disk.get("back").unwrap().deleted_at.is_none());
        assert!(on_disk.get("gone").unwrap().deleted_at.is_some());
    }

    // ── --dry-run reconciliation reports planned actions (#1467) ────────

    #[tokio::test]
    async fn run_sync_dry_run_reconciliation_reports_would_delete_without_mutating_manifest() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let path = shard_path(&output_dir, "m1", None);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, b"From: a@example.com\r\n\r\nbody").unwrap();
        let mut manifest = Manifest::default();
        manifest.upsert(ManifestRecord {
            id: "m1".to_string(),
            thread_id: Some("t1".to_string()),
            label_ids: vec!["INBOX".to_string()],
            internal_date: None,
            subject: None,
            from: None,
            to: None,
            rfc822_msgid: None,
            in_reply_to: None,
            references: None,
            attachment_count: 0,
            attachment_filenames: Vec::new(),
            path: path.strip_prefix(&output_dir).unwrap().to_path_buf(),
            size: 4,
            history_id: Some("1".to_string()),
            deleted_at: None,
            excluded_labels: Vec::new(),
        });
        manifest.save(&manifest_path(&output_dir)).unwrap();
        let manifest_bytes_before = std::fs::read(manifest_path(&output_dir)).unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        mount_message_list(&server, &[]).await; // m1 no longer appears server-side

        let report = run_sync(
            &client,
            &SyncOptions {
                output_dir: output_dir.clone(),
                query: None,
                exclude_labels: Vec::new(),
                full: false,
                concurrency: 4,
                dry_run: true,
                extract_attachments: false,
                shared_pool: None,
            },
        )
        .await
        .unwrap();

        assert!(matches!(
            report.actions.as_slice(),
            [SyncAction::WouldDelete { id }] if id == "m1"
        ));
        assert_eq!(
            std::fs::read(manifest_path(&output_dir)).unwrap(),
            manifest_bytes_before,
            "dry-run must not mutate the manifest, in memory or on disk"
        );
    }

    // ── 404 reason-check tightening (#1467) ──────────────────────────────

    #[tokio::test]
    async fn run_sync_404_with_a_different_reason_is_not_treated_as_history_not_found() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();
        state::save(
            &ArchiveState {
                history_id: "1".to_string(),
                email_address: "user@example.com".to_string(),
                last_sync: Utc::now(),
                query: None,
                pending_fetch: Vec::new(),
            },
            &state_path(&output_dir),
        )
        .unwrap();

        mount_profile(&server, "user@example.com", "999").await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/gmail/v1/users/me/history"))
            .respond_with(
                wiremock::ResponseTemplate::new(404).set_body_json(serde_json::json!({
                    "error": {"message": "Backend Error", "errors": [{"reason": "backendError"}]}
                })),
            )
            .mount(&server)
            .await;
        // Deliberately no mock for messages.list — a wrongly-triggered
        // reconciliation would attempt one and fail differently than
        // asserted below.

        let err = run_sync(&client, &opts(output_dir.clone()))
            .await
            .unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("404"));
        assert!(msg.contains("backendError"));

        match state::load(&state_path(&output_dir)) {
            LoadOutcome::Present(s) => {
                assert_eq!(
                    s.history_id, "1",
                    "never treated as a reconciliation trigger"
                );
            }
            _ => panic!("expected the original state to survive"),
        }
    }

    // ── shared concurrency pool for `sync-all` (ADR-0068) ────────────────

    #[tokio::test]
    async fn shared_pool_of_one_never_lets_two_fetches_be_in_flight_at_once() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        // Both ids' responses never arrive within this test — the only way
        // a second `messages.get` request could ever reach the server is if
        // the shared semaphore (sized to 1) incorrectly let a second fetch
        // start before the first one's permit was released.
        let slow_ids = ["slow0", "slow1"];
        for id in slow_ids {
            wiremock::Mock::given(wiremock::matchers::method("GET"))
                .and(wiremock::matchers::path(format!(
                    "/gmail/v1/users/me/messages/{id}"
                )))
                .and(wiremock::matchers::query_param("format", "raw"))
                .respond_with(
                    wiremock::ResponseTemplate::new(200)
                        .set_body_json(serde_json::json!({
                            "id": id, "threadId": "t1", "labelIds": ["INBOX"],
                            "internalDate": "1700000000000", "historyId": "500",
                            "raw": raw_message_body(id, "Slow"),
                        }))
                        .set_delay(std::time::Duration::from_secs(3600)),
                )
                .mount(&server)
                .await;
        }

        let mut manifest = Manifest::default();
        let limiter = TokenBucket::new(1_000_000, 1_000_000);
        let mut report = SyncReport::default();
        let ids: Vec<String> = slow_ids.iter().copied().map(str::to_string).collect();
        let opts = SyncOptions {
            output_dir: output_dir.clone(),
            query: None,
            exclude_labels: Vec::new(),
            full: false,
            // A generous *local* concurrency: only the shared pool should
            // be the thing capping in-flight requests to 1 here.
            concurrency: 20,
            dry_run: false,
            extract_attachments: false,
            shared_pool: Some(Arc::new(Semaphore::new(1))),
        };

        let never_more_than_one_in_flight = async {
            // Wait for the one fetch the semaphore should admit.
            loop {
                let received = server.received_requests().await.unwrap_or_default();
                if received
                    .iter()
                    .any(|r| r.url.path().contains("/messages/slow"))
                {
                    break;
                }
                tokio::time::sleep(std::time::Duration::from_millis(20)).await;
            }
            // Give a broken (unbounded) implementation ample opportunity to
            // also have started the second fetch by now.
            tokio::time::sleep(std::time::Duration::from_millis(300)).await;
            let received = server.received_requests().await.unwrap_or_default();
            let in_flight = received
                .iter()
                .filter(|r| r.url.path().contains("/messages/slow"))
                .count();
            assert_eq!(
                in_flight, 1,
                "shared_pool sized to 1 should never admit a second concurrent fetch"
            );
        };

        tokio::time::timeout(std::time::Duration::from_secs(5), async {
            tokio::select! {
                _ = fetch_and_archive_messages(
                    &client, &mut manifest, &ids, &limiter, &opts, &mut report, None,
                ) => {
                    panic!(
                        "fetch_and_archive_messages returned before the slow ids' 3600s delay \
                         could possibly elapse"
                    );
                }
                () = never_more_than_one_in_flight => {}
            }
        })
        .await
        .expect("the single admitted fetch was never observed within 5s");
    }

    // A closed `Semaphore` can't happen through any code path today (nothing
    // ever calls `Semaphore::close`) — these two tests exercise the
    // defensive `pool.acquire()` error arms directly, the same way a real
    // `AcquireError` would surface, rather than leaving them unreachable in
    // practice and untested in principle.

    #[tokio::test]
    async fn fetch_and_archive_messages_streaming_reports_error_when_shared_pool_closed() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let mut manifest = Manifest::default();
        let limiter = TokenBucket::new(1_000_000, 1_000_000);
        let mut report = SyncReport::default();
        let pool = Arc::new(Semaphore::new(1));
        pool.close();
        let opts = SyncOptions {
            output_dir,
            query: None,
            exclude_labels: Vec::new(),
            full: false,
            concurrency: 4,
            dry_run: false,
            extract_attachments: false,
            shared_pool: Some(pool),
        };

        let (ids_tx, ids_rx) = mpsc::unbounded_channel();
        ids_tx.send("id1".to_string()).unwrap();
        drop(ids_tx);

        fetch_and_archive_messages_streaming(
            &client,
            &mut manifest,
            ids_rx,
            &limiter,
            &opts,
            &mut report,
            None,
        )
        .await
        .unwrap();

        assert_eq!(report.errors.len(), 1);
        assert!(report.errors[0]
            .reason
            .contains("sync-all's shared semaphore closed unexpectedly"));
    }

    #[tokio::test]
    async fn fetch_and_archive_messages_reports_error_when_shared_pool_closed() {
        let server = wiremock::MockServer::start().await;
        let client = client_with_bootstrapped_token(&server).await;
        let dir = tempfile::tempdir().unwrap();
        let output_dir = dir.path().join("archive");
        std::fs::create_dir_all(&output_dir).unwrap();

        let mut manifest = Manifest::default();
        let limiter = TokenBucket::new(1_000_000, 1_000_000);
        let mut report = SyncReport::default();
        let pool = Arc::new(Semaphore::new(1));
        pool.close();
        let opts = SyncOptions {
            output_dir,
            query: None,
            exclude_labels: Vec::new(),
            full: false,
            concurrency: 4,
            dry_run: false,
            extract_attachments: false,
            shared_pool: Some(pool),
        };

        fetch_and_archive_messages(
            &client,
            &mut manifest,
            &["id1".to_string()],
            &limiter,
            &opts,
            &mut report,
            None,
        )
        .await
        .unwrap();

        assert_eq!(report.errors.len(), 1);
        assert!(report.errors[0]
            .reason
            .contains("sync-all's shared semaphore closed unexpectedly"));
    }
}