openlatch-client 0.1.18

OpenLatch runtime enforcement node — the capture-and-enforce client for the AI Operations Platform
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
//! Cloud forwarding worker — long-lived background task that POSTs hook events
//! to the OpenLatch cloud API.
//!
//! Architecture (D-01 through D-05):
//! - Single `tokio::spawn` task owns the `mpsc::Receiver<CloudEvent>`
//! - Events accumulate in a buffer and flush as one multi-event CloudEvents
//!   batch on whichever comes first: `batch_max_events` reached, or
//!   `batch_max_wait_ms` elapsed since the FIRST buffered event (D43/D45)
//! - Daemon handler uses `try_send()` only — zero blocking on the verdict path (CLOUD-09)
//! - Credential hot-reload via 60-second polling (D-07)
//! - Auth error state persisted to `cloud_state.json` for cross-process visibility (CLOUD-08)
//!
//! Error handling:
//! - 401/403: set auth_error flag, skip POSTs until credential refresh (D-14)
//! - 429: parse Retry-After, sleep, retry once (D-13)
//! - 5xx: sleep 2s, retry once, drop on second failure (D-12)
//! - Network: sleep 2s, retry once (D-12)
//! - Other 4xx: log, drop event (no retry)

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use chrono::Utc;

use secrecy::SecretString;
use tokio::sync::mpsc;

use super::outbox::{DrainLimits, DrainOutcome, Outbox};
use super::{CloudConfig, CloudError, CloudEvent, CloudState, CredentialProvider};
use crate::core::cloud::envelope::build_cloud_headers;

/// Hard platform cap on the serialized size of one ingest request body.
///
/// `POST /api/v1/events/ingest` enforces this itself via
/// `require_body_under_256kb`, which reads `Content-Length` *before* parsing
/// the body — so an oversized batch fails with 413 identically on every
/// retry. Capping on event count alone would turn a burst of large `data`
/// payloads into a permanent stall with no useful diagnostic.
const MAX_BATCH_BYTES: usize = 262_144;

/// Hard platform cap on the number of events in one ingest request.
/// `batch_max_events` is the tuning knob; this is non-negotiable.
const MAX_BATCH_EVENTS: usize = 100;

/// How long the shutdown flush may run before the buffer is spooled instead.
///
/// Deliberately shorter than the daemon's own shutdown budget: the daemon drops
/// this task's `JoinHandle` after its window and the runtime then aborts the
/// task mid-await, so the flush must reach a terminal state — delivered or
/// spooled — strictly inside that window or the events are lost.
const SHUTDOWN_FLUSH_BUDGET: Duration = Duration::from_secs(3);

/// A deadline far enough out that the batch-flush timer is inert.
///
/// Deliberately a large *finite* offset rather than `Instant::MAX`: tokio's
/// timer wheel rejects instants beyond its maximum representable duration and
/// would panic instead of sleeping forever. The branch is disabled by its
/// `if deadline.is_some()` guard anyway — this only has to be a valid instant.
fn far_future() -> tokio::time::Instant {
    tokio::time::Instant::now() + Duration::from_secs(86_400)
}

// Credential hot-reload interval is a runtime parameter on CloudConfig
// (`credential_poll_interval_ms`) so tests can exercise the reload path
// without sleeping 60s.

/// Background cloud health probe interval.
///
/// Production builds honor `CloudState::next_health_delay` — 60s while
/// healthy, exponential backoff 5s → 30s while degraded. Integration tests
/// hard-code a 50ms interval so `record_health_ok` recovery paths can be
/// exercised without a real-time wait.
#[cfg(not(test))]
fn health_delay(state: &CloudState) -> Duration {
    state.next_health_delay()
}
#[cfg(test)]
fn health_delay(_state: &CloudState) -> Duration {
    Duration::from_millis(50)
}

/// Emit a single recovery info log when the worker transitions out of a
/// degraded streak, then clear the latch. No-op when not degraded.
fn note_recovery(degraded: &mut Option<&'static str>, state: &CloudState) {
    if let Some(reason) = degraded.take() {
        tracing::info!(
            reason,
            spooled = state.consecutive_drops(),
            "cloud worker: connectivity restored"
        );
    }
}

/// Shared auth_error clear path used by both the credential-refresh branch
/// and the out-of-band-clear branch in the credential-poll loop. Mirrors
/// the cleared value into the cross-process `cloud_state.json` and logs at
/// debug level if the persist fails (we already have the in-memory state).
fn clear_auth_error_latch(
    local: &mut bool,
    cloud_state: &CloudState,
    openlatch_dir: &Path,
    reason: &'static str,
) {
    *local = false;
    cloud_state.clear_auth_error();
    if let Err(e) = persist_cloud_state(openlatch_dir, false) {
        tracing::warn!(
            error = %e,
            reason,
            "cloud worker: failed to persist cloud_state.json on auth clear"
        );
    }
}

/// Threshold of consecutive live-event drops required before the detector
/// engages emergency mode. Tuned to avoid false-positives on small bursts:
/// a single full-batch (100 events) plus a few stragglers is fine, but a
/// sustained 100+ streak signals real congestion.
const EMERGENCY_DROP_THRESHOLD: u64 = 100;
/// Minimum duration the live-drop streak must last before engaging
/// emergency mode. Keeps a fast spike from latching the detector — only
/// sustained congestion (>10 s of dropping with the cap pegged) trips it.
const EMERGENCY_WINDOW_MS: u64 = 10_000;
/// Consecutive health ticks with zero live drops required to clear
/// emergency mode. Two ticks debounces a brief lull mid-streak.
const EMERGENCY_RECOVERY_TICKS: u32 = 2;

/// Channel-depth (as a percentage of `channel_size`) at which the
/// high-water detector starts a pressure window. Sits above the replay
/// producer's 75 % yield gate so the detector only fires when yield-pacing
/// alone has failed to relieve pressure.
pub(crate) const HIGH_WATER_TRIP_PCT: u64 = 80;
/// Hysteresis floor — the window is cleared only after depth dips below
/// this. Sustained pressure between the trip and clear thresholds keeps
/// the streak alive.
pub(crate) const HIGH_WATER_CLEAR_PCT: u64 = 50;
/// Minimum duration the channel must sit at or above the trip threshold
/// before emergency mode engages on the high-water path. Mirrors
/// `EMERGENCY_WINDOW_MS` so a brief spike doesn't latch the detector.
pub(crate) const HIGH_WATER_WINDOW_MS: u64 = 10_000;

/// Per-event attempt cap inside a single daemon lifetime before an
/// outbox entry is quarantined. State is volatile (lost on restart) — if
/// the entry is still poison after a restart, it quarantines again in
/// another `OUTBOX_MAX_ATTEMPTS` passes. Five matches the typical retry
/// budget for permanently-rejecting 5xx envelopes without flushing
/// legitimately-transient failures too aggressively.
const OUTBOX_MAX_ATTEMPTS: u32 = 5;

/// Label fed to `cloud_channel_overflow_emergency.trigger`. Keeps the
/// PostHog dimension typo-proof on a closed enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum EmergencyTrigger {
    LiveDrops,
    ChannelHighWater,
}

impl EmergencyTrigger {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::LiveDrops => "live_drops",
            Self::ChannelHighWater => "channel_high_water",
        }
    }
}

/// Direction of an emergency-mode transition. Keeps telemetry labels
/// typo-proof — the constructor takes `as_str` rather than a raw string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum EmergencyTransition {
    Enter,
    Exit,
}

impl EmergencyTransition {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Enter => "enter",
            Self::Exit => "exit",
        }
    }
}

/// Detector tick: engage on sustained streak (drops OR high-water pressure),
/// clear after recovery debounce when BOTH signals are quiet.
fn update_emergency_mode(state: &CloudState, config: &CloudConfig, recovery_ticks: &mut u32) {
    let drops = state.consecutive_live_drops();
    let window_start = state.live_drops_window_start_ms();
    let window_duration = super::now_unix_ms().saturating_sub(window_start);
    let high_water_window_ms = state.channel_high_water_window_ms();
    let channel_size = config.channel_size;

    if !state.is_emergency_mode() {
        let live_drop_sustained = drops > EMERGENCY_DROP_THRESHOLD
            && window_start > 0
            && window_duration > EMERGENCY_WINDOW_MS;
        let high_water_sustained = high_water_window_ms > HIGH_WATER_WINDOW_MS;
        if !live_drop_sustained && !high_water_sustained {
            return;
        }
        state.set_emergency_mode(true);
        *recovery_ticks = 0;
        // Live drops dominate when both fire — they represent actual lost
        // events, the more urgent signal.
        let (trigger, reported_duration) = if live_drop_sustained {
            (EmergencyTrigger::LiveDrops, window_duration)
        } else {
            (EmergencyTrigger::ChannelHighWater, high_water_window_ms)
        };
        tracing::warn!(
            code = crate::error::ERR_CLOUD_CHANNEL_EMERGENCY,
            trigger = trigger.as_str(),
            drops_in_window = drops,
            window_duration_ms = reported_duration,
            channel_size,
            "cloud channel under emergency drop — pausing replay until backlog clears"
        );
        crate::telemetry::capture_global(
            crate::telemetry::Event::cloud_channel_overflow_emergency(
                EmergencyTransition::Enter.as_str(),
                trigger.as_str(),
                drops,
                reported_duration,
                channel_size,
                channel_size,
            ),
        );
        return;
    }

    // In emergency mode: clear only when BOTH signals are quiet for the
    // debounce window. A clean tick requires zero live drops AND no active
    // high-water streak so we don't re-engage immediately after clearing.
    if drops > 0 || high_water_window_ms > 0 {
        *recovery_ticks = 0;
        return;
    }
    *recovery_ticks = recovery_ticks.saturating_add(1);
    if *recovery_ticks < EMERGENCY_RECOVERY_TICKS {
        return;
    }
    state.set_emergency_mode(false);
    *recovery_ticks = 0;
    tracing::info!("cloud channel emergency recovered — resuming replay");
    crate::telemetry::capture_global(crate::telemetry::Event::cloud_channel_overflow_emergency(
        EmergencyTransition::Exit.as_str(),
        EmergencyTrigger::LiveDrops.as_str(),
        0,
        window_duration,
        0,
        channel_size,
    ));
}

/// Build the reqwest HTTP client used by the cloud worker.
///
/// Per D-04: owned by the worker, not shared with the daemon.
/// - `connect_timeout`: TCP connect timeout from config
/// - `timeout`: total request timeout from config
/// - `pool_max_idle_per_host(4)`: connection pooling (CLOUD-06)
/// - `use_rustls_tls()`: rustls-only TLS (security constraint: no OpenSSL)
///
/// # Panics
///
/// Panics if the client cannot be constructed (only possible with invalid configs).
pub fn build_cloud_client(config: &CloudConfig) -> reqwest::Client {
    reqwest::Client::builder()
        .connect_timeout(Duration::from_millis(config.timeout_connect_ms))
        .timeout(Duration::from_millis(config.timeout_total_ms))
        .pool_max_idle_per_host(4)
        .use_rustls_tls()
        .build()
        .expect("failed to build cloud reqwest client")
}

/// Main cloud worker loop.
///
/// Consumes events from `rx`, accumulates them into a batch, and POSTs the
/// batch to the cloud API. Handles all error states inline: auth errors, rate
/// limiting, server errors, network errors.
///
/// Exits when the channel is closed (all senders dropped at daemon shutdown)
/// or when `shutdown` fires, flushing whatever is buffered either way.
///
/// # Parameters
///
/// - `rx`: receiver end of the bounded mpsc channel
/// - `credential_provider`: source of the current API key (hot-reloaded every 60s)
/// - `config`: cloud forwarding configuration
/// - `cloud_state`: shared in-process auth error flag (readable by AppState/status command)
/// - `openlatch_dir`: base directory for writing `cloud_state.json`
/// - `outbox`: durable spool for events that survive their retry, if enabled
/// - `shutdown`: explicit shutdown signal. Channel closure alone is **not** a
///   reliable trigger — `cloud_tx` lives on the `Arc<AppState>` and is cloned
///   into the config monitor and the tamper reconciler, so the mpsc may not
///   close promptly (or at all) on `openlatch stop`. `None` means "no
///   explicit signal", used by tests that shut down by dropping the sender.
pub async fn run_cloud_worker(
    rx: mpsc::Receiver<CloudEvent>,
    credential_provider: Arc<dyn CredentialProvider>,
    config: CloudConfig,
    cloud_state: CloudState,
    openlatch_dir: PathBuf,
    outbox: Option<Arc<Outbox>>,
    shutdown: Option<tokio::sync::watch::Receiver<bool>>,
) {
    let mut rx = rx;
    run_cloud_worker_on(
        &mut rx,
        credential_provider,
        config,
        cloud_state,
        openlatch_dir,
        outbox,
        shutdown,
    )
    .await
}

/// [`run_cloud_worker`] over a **borrowed** receiver.
///
/// The daemon runs the worker under its in-process task supervisor
/// (`core::supervision::task`), which re-invokes the future on every restart.
/// An owned receiver would die with the first panicking run and take every
/// queued event with it; borrowing lets the supervisor park the receiver in an
/// `Arc<tokio::sync::Mutex<_>>` and hand the same channel — queue intact — to
/// the replacement run. Every other caller (tests, non-supervised paths) keeps
/// using [`run_cloud_worker`], which is a thin by-value wrapper around this.
#[allow(clippy::too_many_arguments)]
pub async fn run_cloud_worker_on(
    rx: &mut mpsc::Receiver<CloudEvent>,
    credential_provider: Arc<dyn CredentialProvider>,
    config: CloudConfig,
    cloud_state: CloudState,
    openlatch_dir: PathBuf,
    outbox: Option<Arc<Outbox>>,
    shutdown: Option<tokio::sync::watch::Receiver<bool>>,
) {
    // When the caller supplied no signal, hold a live sender for the worker's
    // whole lifetime: a dropped sender makes `changed()` resolve immediately
    // and forever, which would spin the select! instead of never firing.
    let (_shutdown_keepalive, mut shutdown_rx) = match shutdown {
        Some(rx) => (None, rx),
        None => {
            let (tx, rx) = tokio::sync::watch::channel(false);
            (Some(tx), rx)
        }
    };

    let client = build_cloud_client(&config);
    let credential_poll_interval = Duration::from_millis(config.credential_poll_interval_ms);

    // Durable-outbox drain task. Triggered on daemon startup (first tick
    // below) and on each successful health probe via the shared
    // `cloud_state.drain_notify`. The task exits when the state's Arc is
    // dropped (i.e. when the daemon shuts down).
    if let Some(outbox) = outbox.as_ref() {
        let outbox = outbox.clone();
        let client = client.clone();
        let config = config.clone();
        let provider = credential_provider.clone();
        let state = cloud_state.clone();
        let dir = openlatch_dir.clone();
        let notify = state.drain_notify.clone();
        // Cached once at worker startup: the agent_id is stable for the
        // lifetime of the process (`openlatch init` is a prerequisite to
        // starting the daemon, and the id never rotates without a restart).
        let agent_id = crate::config::sniff_agent_id(&dir).unwrap_or_default();
        tokio::spawn(async move {
            // Volatile per-id attempt counts. Restart wipes the map and the
            // quarantine budget resets per daemon lifetime. Wrapped in
            // `Mutex` so the drain closure can update it across `.await`.
            let attempts: Arc<std::sync::Mutex<HashMap<String, u32>>> =
                Arc::new(std::sync::Mutex::new(HashMap::new()));
            let ctx = OutboxDrainCtx {
                outbox: &outbox,
                client: &client,
                config: &config,
                credential_provider: &provider,
                cloud_state: &state,
                openlatch_dir: &dir,
                agent_id: &agent_id,
                attempts: &attempts,
            };
            // Kick off an immediate drain attempt at startup in case the
            // previous daemon run left entries behind.
            drain_outbox_once(&ctx).await;
            loop {
                notify.notified().await;
                // Pause drains while the live channel is under emergency
                // congestion — replay events would just compete with live
                // hooks for the same headroom. The next health-recovery
                // notify will retry.
                if state.is_emergency_mode() {
                    tracing::debug!(
                        "outbox drain: skipping pass — cloud channel in emergency mode"
                    );
                    continue;
                }
                drain_outbox_once(&ctx).await;
            }
        });
    }

    // Load initial credential via spawn_blocking (keyring calls must not run in async context)
    let provider = credential_provider.clone();
    let mut current_key: Option<SecretString> =
        tokio::task::spawn_blocking(move || provider.retrieve())
            .await
            .unwrap_or(None);
    cloud_state.set_no_credential(current_key.is_none());

    let mut auth_error = false;

    // Publish the starting value immediately.
    //
    // `cloud_state.json` is otherwise only written on a *transition* — the
    // 401/403 latch setting it, or `clear_auth_error_latch` clearing it. A
    // worker always starts at `auth_error = false`, so it never transitions on
    // the way up and never rewrites the file. A daemon that died (or was
    // killed) while latched therefore leaves `{"auth_error":true}` on disk, and
    // every later daemon inherits that claim about itself for as long as it
    // stays healthy — the file outlives the process whose state it describes.
    //
    // Writing it here makes the file mean "the current worker's state" from the
    // moment it boots, rather than "the last transition any worker ever made".
    // Non-fatal: this is a reporting surface, and failing to publish it must
    // never stop the worker from forwarding events.
    if let Err(e) = persist_cloud_state(&openlatch_dir, auth_error) {
        tracing::warn!(
            error = %e,
            "cloud worker: failed to persist initial cloud_state.json"
        );
    }

    // Whether we have already emitted the OL-1200 "no API key" warning for the
    // current missing-credential streak. Reset whenever a credential is loaded,
    // so a disable/re-enable cycle warns again — but a long-lived missing-key
    // state produces one actionable WARN, not one per event.
    let mut missing_key_warned = current_key.is_none();
    // Active degraded-streak latch. Some(reason) → outage in progress; the
    // first failure emitted a WARN, subsequent failures drop to DEBUG. Cleared
    // by `note_recovery` on the next successful POST or health probe, which
    // emits a single INFO with the spooled count.
    let mut degraded: Option<&'static str> = None;
    if current_key.is_none() {
        tracing::warn!(
            code = "OL-1200",
            "cloud worker: no API key available — cloud forwarding disabled until you run \
             'openlatch auth login' or set [cloud] enabled = false in config.toml (fail-open, \
             events are still logged locally)"
        );
    }
    let mut last_credential_poll = tokio::time::Instant::now();
    let mut last_health_tick = tokio::time::Instant::now();
    let mut emergency_recovery_ticks: u32 = 0;

    // ------------------------------------------------------------------
    // Batch accumulator (D43)
    //
    // The deadline is anchored to the FIRST buffered event and is NEVER reset
    // on subsequent pushes.
    //
    // REJECTED — resetting the timer on every push. It is simpler to write,
    // but under any sustained event rate the timer is perpetually reset and
    // the batch only ever flushes on the SIZE trigger, so `batch_max_wait_ms`
    // silently stops bounding latency. That is precisely the bug the PRD's
    // acceptance criterion is written to catch: "the batch SHALL flush no
    // later than batch_max_wait_ms after the FIRST event was enqueued."
    // ------------------------------------------------------------------
    let size_trigger = config.batch_max_events.max(1);
    let mut buf: Vec<CloudEvent> = Vec::with_capacity(size_trigger);
    let mut deadline: Option<tokio::time::Instant> = None;
    let flush_ctx = FlushCtx {
        client: &client,
        config: &config,
        openlatch_dir: &openlatch_dir,
        outbox: outbox.as_ref(),
        cloud_state: &cloud_state,
    };

    loop {
        // Credential hot-reload: check every 60 seconds (D-07)
        if last_credential_poll.elapsed() >= credential_poll_interval {
            let provider = credential_provider.clone();
            let new_key = tokio::task::spawn_blocking(move || provider.retrieve())
                .await
                .unwrap_or(None);

            // Compare by exposing secret (both sides must match)
            let key_changed = match (&current_key, &new_key) {
                (None, None) => false,
                (Some(_), None) | (None, Some(_)) => true,
                (Some(old), Some(new)) => {
                    use secrecy::ExposeSecret;
                    old.expose_secret() != new.expose_secret()
                }
            };

            // The old unconditional reset on every poll caused a 60s flap
            // against permanently-revoked keys: clear → 401 → spool → repeat.
            // Now we only clear on a real key rotation, OR mirror an
            // out-of-band clear from `POST /admin/auth/refresh` so WR-01
            // (same-key re-login) still resumes POSTs immediately.
            if new_key.is_some() {
                let out_of_band_clear = !key_changed && auth_error && !cloud_state.is_auth_error();
                if key_changed {
                    tracing::info!(
                        "cloud worker: credential refreshed — resetting auth_error state"
                    );
                    clear_auth_error_latch(
                        &mut auth_error,
                        &cloud_state,
                        &openlatch_dir,
                        "credential refresh",
                    );
                } else if out_of_band_clear {
                    clear_auth_error_latch(
                        &mut auth_error,
                        &cloud_state,
                        &openlatch_dir,
                        "out-of-band auth clear",
                    );
                }
                missing_key_warned = false;
                current_key = new_key;
            }

            cloud_state.set_no_credential(current_key.is_none());
            last_credential_poll = tokio::time::Instant::now();
        }

        // The timer branch needs a valid instant even when disarmed; the
        // `if deadline.is_some()` guard is what actually disables it.
        let flush_at = deadline.unwrap_or_else(far_future);

        // Receive next event (with timeout to allow periodic credential polling)
        //
        // D44 — CANCEL SAFETY. `tokio::select!` drops the losing branch's
        // future. The concern is that `rx.recv()` might dequeue an item and
        // then be cancelled, losing it. It cannot: `mpsc::Receiver::recv` is
        // documented cancel-safe — "if recv is used as the event in a
        // tokio::select! statement and some other branch completes first, it
        // is guaranteed that no messages were received on this channel."
        // `Sleep` is trivially cancel-safe, and so is `watch::Receiver::changed`.
        //
        // REJECTED — wrapping the recv in a `tokio::spawn` + oneshot to "make
        // it safe". That adds a task per event, introduces a real loss window
        // at shutdown, and solves a problem that does not exist. This note
        // exists so nobody "fixes" it later.
        tokio::select! {
            // `biased;` with the TIMER FIRST. Without it select! polls
            // branches in a random order, so under a continuously-ready
            // channel an already-due timer only wins with some probability
            // per iteration — the flush bound degrades from "guaranteed" to
            // "0.5^k chance of being k iterations late", which is exactly the
            // kind of soft latency erosion D43 exists to rule out. A due timer
            // must always win, deterministically.
            biased;

            _ = tokio::time::sleep_until(flush_at), if deadline.is_some() => {
                flush_batch(
                    &flush_ctx,
                    &mut buf,
                    current_key.as_ref(),
                    "time",
                    &mut auth_error,
                    &mut degraded,
                ).await;
                deadline = None;
                continue;
            }
            // Explicit shutdown. Fires on an actual signal or on the sender
            // being dropped; either way the post-loop flush runs.
            _ = shutdown_rx.changed() => {
                tracing::info!("cloud worker: shutdown signalled, flushing in-flight batch");
                break;
            }
            // Wake up periodically to check credential reload deadline
            _ = tokio::time::sleep_until(
                last_credential_poll + credential_poll_interval
            ) => {
                // Re-enter loop to perform credential poll
                continue;
            }
            // Periodic cloud health probe — clears network-degraded status
            // once the cloud is reachable again, even on quiet machines.
            //
            // Adaptive cadence: 60s while healthy, backing off aggressively
            // (5s → 10s → 20s → 30s cap, +/-20% jitter) while degraded so
            // reconnect after an offline boot is detected within ~5–15s
            // instead of up to 60s.
            _ = tokio::time::sleep_until(
                last_health_tick + health_delay(&cloud_state)
            ) => {
                last_health_tick = tokio::time::Instant::now();
                match cloud_health_check(&client, &config).await {
                    Ok(()) => {
                        note_recovery(&mut degraded, &cloud_state);
                        cloud_state.record_health_ok();
                        // Cloud just recovered — wake the outbox drain task
                        // AND the daemon-side fallback-replay task so any
                        // events captured while offline flow out before the
                        // next live hook arrives.
                        cloud_state.notify_drain();
                    }
                    Err(e) => {
                        cloud_state.record_probe_failure();
                        tracing::debug!(
                            error = %e,
                            consecutive_failures = cloud_state.consecutive_probe_failures(),
                            "cloud health check failed"
                        );
                    }
                }
                // Detector tick shares the health probe cadence — no new task.
                update_emergency_mode(&cloud_state, &config, &mut emergency_recovery_ticks);
                continue;
            }
            maybe = rx.recv() => {
                let Some(event) = maybe else {
                    // Channel closed — all senders dropped, daemon is shutting down
                    tracing::info!("cloud worker: channel closed, exiting");
                    break;
                };

                // The two pre-POST guards run at RECV time, not flush time.
                // While auth is latched or no key is loaded the event is
                // spooled immediately and never enters the buffer, so the
                // buffer only ever holds sendable events and each event keeps
                // the exact spool reason it would have had before batching.
                // Checking at flush time instead would spool whole buffers
                // under a single, wrong reason.
                if auth_error {
                    tracing::debug!(
                        code = "OL-1201",
                        "cloud worker: auth_error active — spooling event to outbox until credential refresh"
                    );
                    spool_event(outbox.as_ref(), &event, SpoolReason::AuthError);
                    continue;
                }
                if current_key.is_none() {
                    if !missing_key_warned {
                        tracing::warn!(
                            code = "OL-1200",
                            "cloud worker: no API key available — cloud forwarding disabled until \
                             you run 'openlatch auth login' or set [cloud] enabled = false in \
                             config.toml (fail-open, events are still logged locally)"
                        );
                        missing_key_warned = true;
                    } else {
                        tracing::debug!(
                            code = "OL-1200",
                            "cloud worker: skipping event — still no API key available"
                        );
                    }
                    spool_event(outbox.as_ref(), &event, SpoolReason::NoCredential);
                    continue;
                }

                if buf.is_empty() {
                    deadline = Some(
                        tokio::time::Instant::now()
                            + Duration::from_millis(config.batch_max_wait_ms),
                    );
                }
                buf.push(event);
                if buf.len() >= size_trigger {
                    flush_batch(
                        &flush_ctx,
                        &mut buf,
                        current_key.as_ref(),
                        "size",
                        &mut auth_error,
                        &mut degraded,
                    ).await;
                    deadline = None;
                }
            }
        }
    }

    // Shutdown flush: anything still buffered is POSTed, and anything that
    // fails to POST lands in the outbox. Nothing is dropped on the floor.
    //
    // The flush is BOUNDED, and the buffer is snapshotted first, because the
    // daemon gives this task only a few seconds before it drops the JoinHandle
    // and the runtime aborts it (`daemon/mod.rs`). `flush_batch` starts with a
    // `mem::take`, and the retry ladder inside it can sleep for a server-
    // supplied `Retry-After` — so an abort part-way through would take the
    // already-taken events with it, leaving them neither delivered nor
    // spooled. That is precisely the loss acceptance C5 forbids.
    //
    // On timeout we spool the snapshot. A batch that actually landed just as
    // the deadline passed is therefore re-sent from the outbox on next start;
    // the platform dedups on `client_event_id`, so the cost of that race is a
    // duplicate, and the cost of the alternative is a lost event. Prefer
    // at-least-once.
    let snapshot = buf.clone();
    if tokio::time::timeout(
        SHUTDOWN_FLUSH_BUDGET,
        flush_batch(
            &flush_ctx,
            &mut buf,
            current_key.as_ref(),
            "shutdown",
            &mut auth_error,
            &mut degraded,
        ),
    )
    .await
    .is_err()
    {
        tracing::warn!(
            code = "OL-1200",
            buffered = snapshot.len(),
            "cloud worker: shutdown flush exceeded its budget — spooling the batch to the outbox"
        );
        for event in &snapshot {
            spool_event(flush_ctx.outbox, event, SpoolReason::Network);
        }
    }
}

/// The parts of the worker's state that a flush needs and never mutates.
/// Bundled so `flush_batch` stays under the argument-count limit and so the
/// borrow set is obvious at the call site.
struct FlushCtx<'a> {
    client: &'a reqwest::Client,
    config: &'a CloudConfig,
    openlatch_dir: &'a Path,
    outbox: Option<&'a Arc<Outbox>>,
    cloud_state: &'a CloudState,
}

/// Post the accumulated buffer as one or more batches, then clear it.
///
/// Splits under both wire caps (D45), preserves the per-error-class behaviour
/// of the pre-batching single-event path verbatim, and — on terminal failure —
/// spools every envelope of the batch individually (D23).
///
/// `reason` is one of `size` / `time` / `shutdown` / `drain` and is logged
/// alongside the batch size at `debug`, which is exactly what the PRD's
/// Telemetry table asks for to tune the two knobs.
async fn flush_batch(
    ctx: &FlushCtx<'_>,
    buf: &mut Vec<CloudEvent>,
    key: Option<&SecretString>,
    reason: &'static str,
    auth_error: &mut bool,
    degraded: &mut Option<&'static str>,
) {
    if buf.is_empty() {
        return;
    }
    let events = std::mem::take(buf);
    tracing::debug!(
        batch_size = events.len(),
        reason,
        "cloud worker: flushing event batch"
    );

    let prepared = prepare_batch(&events, ctx.cloud_state);
    if prepared.is_empty() {
        return;
    }

    let Some(key) = key else {
        // Defensive: the recv-time guard keeps un-sendable events out of the
        // buffer, so this only fires if the credential vanished mid-flight.
        spool_group(ctx.outbox, &prepared, SpoolReason::NoCredential);
        return;
    };

    for group in split_batches(&prepared, ctx.config.batch_max_events) {
        if *auth_error {
            // An earlier group in this same flush latched 401/403. Sending the
            // rest would just burn POSTs against a credential the server has
            // already rejected.
            spool_group(ctx.outbox, group, SpoolReason::AuthError);
            continue;
        }
        let n = group.len() as u64;
        match post_batch(ctx.client, ctx.config, key, group, ctx.openlatch_dir).await {
            Ok(()) => {
                note_recovery(degraded, ctx.cloud_state);
                tracing::debug!(batch_size = n, "cloud worker: batch forwarded successfully");
                // Record the BATCH LENGTH, not one: every counter downstream
                // (`cloud_forwarded_count` on /metrics, `openlatch status`) is
                // per-event and would under-count by up to 100x otherwise.
                ctx.cloud_state.record_successful_forwards(n);
                // A live POST succeeding also signals connectivity — wake
                // the drain listeners so any queued outbox/fallback entries
                // flow out without waiting for the next health tick.
                ctx.cloud_state.notify_drain();
            }
            Err(CloudError::AuthError) => {
                tracing::warn!(
                    code = "OL-1201",
                    batch_size = n,
                    "cloud worker: auth error (401/403) — pausing POSTs until credential refresh"
                );
                *auth_error = true;
                ctx.cloud_state
                    .auth_error
                    .store(true, std::sync::atomic::Ordering::Relaxed);
                if let Err(e) = persist_cloud_state(ctx.openlatch_dir, true) {
                    tracing::warn!(error = %e, "cloud worker: failed to persist cloud_state.json");
                }
                spool_group(ctx.outbox, group, SpoolReason::AuthError);
            }
            Err(CloudError::RateLimit { retry_after_secs }) => {
                // A 429 is routine backpressure for a batched-ingest client, not
                // an outage: back off for Retry-After and retry once. Logged at
                // `debug` and it does NOT latch `degraded` — only actual data
                // deferral escalates (the spool below, then the outbox drain's
                // OL-1205/1207). This keeps a rate-limited boot drain quiet
                // instead of emitting one WARN per throttled batch.
                tracing::debug!(
                    code = "OL-1202",
                    retry_after_secs,
                    batch_size = n,
                    "cloud worker: rate limited (429) — backing off and retrying"
                );
                tokio::time::sleep(Duration::from_secs(retry_after_secs)).await;
                // Retry once after the wait; spool on second failure so the
                // drain task replays it once the rate-limit window clears.
                match post_batch(ctx.client, ctx.config, key, group, ctx.openlatch_dir).await {
                    // A batch that succeeds on the RETRY is still forwarded.
                    // Recording only the first-attempt success makes
                    // `cloud_forwarded_count` (and the `openlatch status` that
                    // reads it) silently undercount every batch that recovered
                    // from a transient failure — which is most of them on a
                    // flaky link.
                    Ok(()) => ctx.cloud_state.record_successful_forwards(n),
                    Err(_) => {
                        ctx.cloud_state.record_drops(n);
                        spool_group(ctx.outbox, group, SpoolReason::RateLimit);
                    }
                }
            }
            Err(CloudError::ServerError) => {
                if degraded.is_none() {
                    tracing::warn!(
                        code = "OL-1200",
                        batch_size = n,
                        "cloud worker: server error (5xx) — retrying once; suppressing further warnings until recovery"
                    );
                    *degraded = Some("server_error");
                } else {
                    tracing::debug!(code = "OL-1200", "cloud worker: 5xx during degraded streak");
                }
                tokio::time::sleep(Duration::from_millis(ctx.config.retry_delay_ms)).await;
                match post_batch(ctx.client, ctx.config, key, group, ctx.openlatch_dir).await {
                    Ok(()) => ctx.cloud_state.record_successful_forwards(n),
                    Err(_) => {
                        ctx.cloud_state.record_drops(n);
                        spool_group(ctx.outbox, group, SpoolReason::ServerError);
                    }
                }
            }
            Err(CloudError::Network) => {
                if degraded.is_none() {
                    tracing::warn!(
                        code = "OL-1200",
                        batch_size = n,
                        "cloud worker: network error — retrying once; suppressing further warnings until recovery"
                    );
                    *degraded = Some("network");
                } else {
                    tracing::debug!(
                        code = "OL-1200",
                        "cloud worker: network error during degraded streak"
                    );
                }
                tokio::time::sleep(Duration::from_millis(ctx.config.retry_delay_ms)).await;
                match post_batch(ctx.client, ctx.config, key, group, ctx.openlatch_dir).await {
                    Ok(()) => ctx.cloud_state.record_successful_forwards(n),
                    Err(_) => {
                        ctx.cloud_state.record_drops(n);
                        spool_group(ctx.outbox, group, SpoolReason::Network);
                    }
                }
            }
            Err(CloudError::ClientError(code)) => {
                // No retry, as before — but log the batch size so a dropped
                // batch is visible as more than a single lost event.
                tracing::warn!(
                    http_status = code,
                    batch_size = n,
                    "cloud worker: unexpected 4xx — dropping batch (no retry)"
                );
                // A dropped batch is N dropped events. Without this the 4xx
                // path is the one failure mode invisible to `cloud_drop_count`
                // — the counter would read zero while events were being
                // discarded.
                ctx.cloud_state.record_drops(n);
            }
        }
    }
}

/// Unauthenticated GET against `{api_url}/api/v1/health`. Used by the worker's
/// background tick to detect cloud recovery without waiting for a hook event.
/// Non-2xx responses and transport errors surface as `Err` so the caller can
/// log at debug level; they never update `drop_count` or flip `auth_error`.
async fn cloud_health_check(
    client: &reqwest::Client,
    config: &CloudConfig,
) -> Result<(), reqwest::Error> {
    let base = config.api_url.trim_end_matches('/');
    let url = format!("{base}/api/v1/health");
    let resp = client.get(&url).send().await?;
    resp.error_for_status().map(|_| ())
}

/// Failure category labels for the `cloud_event_spooled` telemetry event.
/// Enum (rather than `&'static str`) so a typo can't silently ship a
/// bad label, and so the closed set stays reviewable in one place.
#[derive(Debug, Clone, Copy)]
enum SpoolReason {
    Network,
    ServerError,
    RateLimit,
    AuthError,
    NoCredential,
}

impl SpoolReason {
    fn as_str(self) -> &'static str {
        match self {
            SpoolReason::Network => "network",
            SpoolReason::ServerError => "server_error",
            SpoolReason::RateLimit => "rate_limit",
            SpoolReason::AuthError => "auth_error",
            SpoolReason::NoCredential => "no_credential",
        }
    }
}

/// Best-effort append to the durable outbox. Never propagates errors — the
/// outbox is a recovery mechanism, not a correctness mechanism, and
/// instrumentation in the append path already surfaces OL-1204 if the write
/// failed. A None outbox is a silent no-op (outbox feature disabled).
fn spool_event(outbox: Option<&Arc<Outbox>>, event: &CloudEvent, reason: SpoolReason) {
    if outbox.is_none() {
        return;
    }
    spool_envelope(outbox, &stamp_extensions(event), reason);
}

/// Append one already-stamped envelope to the durable outbox.
fn spool_envelope(outbox: Option<&Arc<Outbox>>, envelope: &serde_json::Value, reason: SpoolReason) {
    let Some(outbox) = outbox else { return };
    match outbox.append(envelope) {
        Ok(()) => {
            crate::telemetry::capture_global(crate::telemetry::Event::cloud_event_spooled(
                reason.as_str(),
            ));
        }
        Err(e) => {
            tracing::warn!(
                code = crate::error::ERR_OUTBOX_WRITE_FAILED,
                error = %e,
                path = %outbox.path().display(),
                "cloud worker: failed to spool event to outbox"
            );
        }
    }
}

/// Spool an entire failed batch — **one line per event** (D23).
///
/// REJECTED — spooling the whole batch as one line. Fewer writes, but it
/// changes the outbox file format and breaks three things that operate per
/// entry: the drain path, the per-entry `OUTBOX_MAX_ATTEMPTS` counter, and the
/// quarantine logic (OL-1207). It would also break `openlatch status` and the
/// existing `outbox_max_bytes` drop-oldest eviction.
fn spool_group(outbox: Option<&Arc<Outbox>>, group: &[PreparedEvent], reason: SpoolReason) {
    if outbox.is_none() {
        return;
    }
    for prepared in group {
        spool_envelope(outbox, &prepared.envelope, reason);
    }
}

/// Clone the envelope and stamp the daemon-owned CloudEvents extension
/// attributes onto it. Shared by the spool path and the POST path so both write
/// the same bytes for the same event.
///
/// `clientversion` is stamped **unconditionally**, on the same rule
/// `daemon/handlers.rs::process_envelope` applies to hook envelopes: the
/// attribute identifies the *forwarder*, not the emitter, and this worker is the
/// single egress for every producer. Doing it here rather than in each builder
/// is what makes the attribute total — `boundary/emit.rs` and the two
/// `config_monitor` builders hand-roll their envelopes and omitted it, which
/// left `unified_events.clientversion` NULL for every economics and config row
/// and starved the platform's fleet-readiness materialization.
///
/// Note this deliberately stamps only forwarder identity. The `ol*` policy
/// extensions stay in `handlers.rs` — an `olverdict` on an
/// `ai.openlatch.config.*` event would be a verdict nobody produced.
fn stamp_extensions(event: &CloudEvent) -> serde_json::Value {
    let mut envelope = event.envelope.clone();
    if let Some(obj) = envelope.as_object_mut() {
        obj.insert(
            "agentid".to_string(),
            serde_json::Value::String(event.agent_id.clone()),
        );
        // OPENLATCH_VERSION, not CARGO_PKG_VERSION — see build.rs.
        obj.insert(
            "clientversion".to_string(),
            serde_json::Value::String(env!("OPENLATCH_VERSION").to_string()),
        );
    }
    envelope
}

/// An envelope stamped with `agentid` + `clientversion` and serialized
/// **exactly once**.
///
/// The serialized form is used for both the byte-cap accounting in
/// `split_batches` and the request body in `post_batch`; serializing twice
/// would double the CPU cost of every flush for no benefit. The parsed value
/// is retained so a failed batch can be spooled without re-parsing.
struct PreparedEvent {
    /// Serialized `agentid`-stamped envelope — one element of the batch array.
    json: String,
    /// The same envelope, for the outbox spool path.
    envelope: serde_json::Value,
}

/// Stamp + serialize a single event, or `None` if it can never be sent.
///
/// An event whose own serialized form does not fit in a request body cannot
/// be batched *or* sent alone: the platform rejects it with 413 before it even
/// parses the body, so every retry fails identically. Dropping it here with
/// OL-1002 is the only way to avoid wedging the queue behind it.
fn prepare_one(envelope: serde_json::Value) -> Option<PreparedEvent> {
    let json = match serde_json::to_string(&envelope) {
        Ok(json) => json,
        Err(e) => {
            tracing::warn!(
                code = crate::error::ERR_EVENT_TOO_LARGE,
                error = %e,
                "cloud worker: dropping event that cannot be serialized"
            );
            return None;
        }
    };
    // `[` + element + `]` is the smallest body that can carry it.
    if json.len() + 2 > MAX_BATCH_BYTES {
        // Recurring, expected condition: some sources routinely emit payloads
        // over the cap, and it fails identically on every retry — so this logs
        // at `debug`, not `warn`, to avoid flooding the operator. The drop stays
        // visible via `CloudState::record_drop` (telemetry counts it).
        tracing::debug!(
            code = crate::error::ERR_EVENT_TOO_LARGE,
            bytes = json.len(),
            max_bytes = MAX_BATCH_BYTES,
            "cloud worker: dropping oversized event — it cannot fit in any batch"
        );
        return None;
    }
    Some(PreparedEvent { json, envelope })
}

/// Stamp + serialize a whole buffer, dropping (and counting) anything that
/// can never be sent.
fn prepare_batch(events: &[CloudEvent], cloud_state: &CloudState) -> Vec<PreparedEvent> {
    let mut out = Vec::with_capacity(events.len());
    for event in events {
        match prepare_one(stamp_extensions(event)) {
            Some(prepared) => out.push(prepared),
            None => cloud_state.record_drop(),
        }
    }
    out
}

/// Greedy split under BOTH caps (D45).
///
/// `batch_max_events` is the tuning knob; `MAX_BATCH_EVENTS` /
/// `MAX_BATCH_BYTES` are the platform's hard limits and are non-negotiable.
///
/// Accounting: each envelope is already serialized once, so this sums `len()`
/// and adds the JSON array framing (`[` + `]` + one `,` per element beyond the
/// first). A batch closes when adding the next event *would* exceed the byte
/// cap, or when the element count reaches `min(batch_max_events, 100)`.
///
/// Returns slices into `prepared` — `post_batch` reuses the same bytes for the
/// request body.
fn split_batches(prepared: &[PreparedEvent], batch_max_events: usize) -> Vec<&[PreparedEvent]> {
    let cap = batch_max_events.clamp(1, MAX_BATCH_EVENTS);
    let mut out = Vec::new();
    let mut start = 0usize;
    let mut payload = 0usize;
    let mut count = 0usize;
    for (i, event) in prepared.iter().enumerate() {
        if count > 0 {
            // Framing for `count + 1` elements: two brackets + `count` commas.
            let framed = payload + event.json.len() + 2 + count;
            if count >= cap || framed > MAX_BATCH_BYTES {
                out.push(&prepared[start..i]);
                start = i;
                payload = 0;
                count = 0;
            }
        }
        payload += event.json.len();
        count += 1;
    }
    if count > 0 {
        out.push(&prepared[start..]);
    }
    out
}

/// Shared context for the outbox drain task. All fields are short-lived
/// borrows captured per drain pass; ownership lives on the spawned task.
struct OutboxDrainCtx<'a> {
    outbox: &'a Arc<Outbox>,
    client: &'a reqwest::Client,
    config: &'a CloudConfig,
    credential_provider: &'a Arc<dyn CredentialProvider>,
    cloud_state: &'a CloudState,
    openlatch_dir: &'a Path,
    agent_id: &'a str,
    attempts: &'a Arc<std::sync::Mutex<HashMap<String, u32>>>,
}

/// Drain the outbox once: read queued envelopes in contiguous groups and
/// repost them via the same `post_batch` path used by live events, so replay
/// traffic has the same shape as live traffic. Halts on the first failed group
/// so a transient outage doesn't drain the entire queue into a retry storm.
/// Skips silently when no credential is available.
async fn drain_outbox_once(ctx: &OutboxDrainCtx<'_>) {
    // Pull a fresh credential before every drain so a recent `openlatch auth
    // login` is picked up without waiting for the 60s credential poll.
    let provider = ctx.credential_provider.clone();
    let key: Option<SecretString> = tokio::task::spawn_blocking(move || provider.retrieve())
        .await
        .unwrap_or(None);
    let Some(key) = key else {
        // No credential — drain is a no-op until auth is configured. The
        // daemon will signal `drain_notify` again the next time a health
        // probe succeeds after a credential lands.
        return;
    };

    // Mirror the live path's wire caps so a replayed group is the same shape
    // as a live batch. `post_batch` re-splits under the exact byte cap before
    // sending, so these limits only have to be in the right ballpark.
    let limits = DrainLimits {
        max_entries: ctx.config.batch_max_events.clamp(1, MAX_BATCH_EVENTS),
        max_bytes: MAX_BATCH_BYTES,
    };

    let stats = ctx
        .outbox
        .drain(limits, |envelopes| {
            let client = ctx.client.clone();
            let config = ctx.config.clone();
            let key = key.clone();
            let openlatch_dir = ctx.openlatch_dir.to_path_buf();
            let agent_id = ctx.agent_id.to_string();
            let attempts = ctx.attempts.clone();
            async move {
                let group_len = envelopes.len();
                // One outcome per input entry, positionally.
                let mut outcomes = vec![DrainOutcome::Forwarded; group_len];
                let mut ids: Vec<Option<String>> = Vec::with_capacity(group_len);
                let mut prepared: Vec<PreparedEvent> = Vec::with_capacity(group_len);

                for (idx, envelope) in envelopes.into_iter().enumerate() {
                    ids.push(
                        envelope
                            .get("id")
                            .and_then(|v| v.as_str())
                            .filter(|s| !s.is_empty())
                            .map(str::to_string),
                    );
                    // Spooled envelopes already carry `agentid`; re-stamping
                    // is idempotent and keeps replay identical to the live
                    // path for entries written by an older client.
                    let event = CloudEvent {
                        envelope,
                        agent_id: agent_id.clone(),
                    };
                    match prepare_one(stamp_extensions(&event)) {
                        Some(p) => prepared.push(p),
                        // Can never be sent — advance past it rather than
                        // wedging the queue, and count it as quarantined
                        // rather than delivered.
                        None => outcomes[idx] = DrainOutcome::Quarantined,
                    }
                }

                let mut failure: Option<CloudError> = None;
                for batch in split_batches(&prepared, config.batch_max_events) {
                    match post_batch(&client, &config, &key, batch, &openlatch_dir).await {
                        Ok(()) => {}
                        // ClientError is terminal — treat as drained so the
                        // entries are removed. Everything else keeps them for
                        // the next drain cycle.
                        Err(CloudError::ClientError(code)) => {
                            tracing::warn!(
                                http_status = code,
                                batch_size = batch.len(),
                                "outbox drain: dropping entries on unexpected 4xx"
                            );
                        }
                        Err(e) => {
                            failure = Some(e);
                            break;
                        }
                    }
                }

                let Some(e) = failure else {
                    // Healthy drain: the attempt map is empty in the common
                    // case. A `try_lock + is_empty` check would race with
                    // concurrent inserts; an unconditional `is_empty` peek
                    // under the mutex is the cheap path — one atomic and one
                    // length compare, no allocation.
                    let mut guard = attempts.lock().unwrap();
                    if !guard.is_empty() {
                        for id in ids.iter().flatten() {
                            guard.remove(id);
                        }
                    }
                    return Ok(outcomes);
                };

                // Per-id attempt cap: after OUTBOX_MAX_ATTEMPTS transient
                // failures within a single daemon lifetime an entry is
                // quarantined so the queue can drain past it. Without this,
                // one permanently-rejecting envelope at the head of
                // `outbox.jsonl` blocks every entry behind it.
                //
                // A failed group charges EVERY entry in it an attempt — the
                // group is what gets retried, so the whole group is what pays.
                // The group only advances once every entry has exhausted its
                // budget; until then the pass halts and nothing moves.
                let mut all_exhausted = true;
                {
                    let mut guard = attempts.lock().unwrap();
                    for id in &ids {
                        let Some(id) = id else {
                            // No id to key on — cannot track attempts, so this
                            // group can never be quarantined out of the way.
                            all_exhausted = false;
                            continue;
                        };
                        let entry = guard.entry(id.clone()).or_insert(0);
                        *entry = entry.saturating_add(1);
                        if *entry < OUTBOX_MAX_ATTEMPTS {
                            all_exhausted = false;
                        }
                    }
                }

                if all_exhausted {
                    let mut guard = attempts.lock().unwrap();
                    for id in ids.iter().flatten() {
                        tracing::warn!(
                            code = crate::error::ERR_OUTBOX_QUARANTINED,
                            event_id = %id,
                            attempts = OUTBOX_MAX_ATTEMPTS,
                            error = %e,
                            "outbox: quarantining repeatedly-failing entry"
                        );
                        guard.remove(id);
                    }
                    return Ok(vec![DrainOutcome::Quarantined; group_len]);
                }

                tracing::debug!(
                    error = %e,
                    batch_size = group_len,
                    "outbox drain: halted on transient failure — retaining remaining entries"
                );
                Err(())
            }
        })
        .await;

    match stats {
        Ok(stats) => {
            if stats.drained > 0 || stats.failed > 0 || stats.corrupt > 0 || stats.quarantined > 0 {
                tracing::info!(
                    drained = stats.drained,
                    failed = stats.failed,
                    corrupt = stats.corrupt,
                    quarantined = stats.quarantined,
                    remaining = ctx.outbox.pending_count(),
                    "outbox drain completed"
                );
                crate::telemetry::capture_global(crate::telemetry::Event::cloud_outbox_drained(
                    stats.drained,
                    stats.failed,
                    stats.corrupt,
                    stats.quarantined,
                ));
            }
            if stats.drained > 0 {
                ctx.cloud_state
                    .forwarded_count
                    .fetch_add(stats.drained, std::sync::atomic::Ordering::Relaxed);
            }
            if stats.failed > 0 {
                tracing::warn!(
                    code = crate::error::ERR_OUTBOX_DRAIN_PARTIAL,
                    failed = stats.failed,
                    remaining = ctx.outbox.pending_count(),
                    "outbox drain: partial — some entries could not be replayed yet"
                );
            }
        }
        Err(e) => {
            tracing::warn!(
                code = crate::error::ERR_OUTBOX_DRAIN_PARTIAL,
                error = %e,
                "outbox drain: I/O error while replaying"
            );
        }
    }
}

/// POST one batch of cloud events to the ingestion endpoint.
///
/// Takes an already-prepared slice — the caller stamped `agentid` and
/// serialized each envelope once, and `split_batches` guaranteed the slice is
/// within both wire caps. Attaches headers with a UUIDv7 request ID and
/// interprets the HTTP response status.
///
/// The status mapping is unchanged from the pre-batching single-event path;
/// only the *unit* it operates on went from one event to N.
///
/// # Returns
///
/// - `Ok(())` on 2xx success
/// - `Err(CloudError::AuthError)` on 401/403
/// - `Err(CloudError::RateLimit { ... })` on 429 (parses `Retry-After` header)
/// - `Err(CloudError::ServerError)` on 5xx
/// - `Err(CloudError::Network)` on transport errors
/// - `Err(CloudError::ClientError(code))` on other 4xx
async fn post_batch(
    client: &reqwest::Client,
    config: &CloudConfig,
    key: &SecretString,
    batch: &[PreparedEvent],
    _openlatch_dir: &Path,
) -> Result<(), CloudError> {
    // Generate UUIDv7 request ID (D-19)
    let request_id = uuid::Uuid::now_v7().to_string();

    // CloudEvents v1.0.2 structured-mode batch format: a bare JSON array of
    // envelopes. Assembled from the already-serialized elements rather than
    // re-serializing — `build_cloud_headers` sets
    // `Content-Type: application/cloudevents-batch+json`, which `.body()`
    // leaves alone.
    let mut body = String::with_capacity(batch.iter().map(|e| e.json.len() + 1).sum::<usize>() + 2);
    body.push('[');
    for (i, event) in batch.iter().enumerate() {
        if i > 0 {
            body.push(',');
        }
        body.push_str(&event.json);
    }
    body.push(']');

    // Build headers (D-17)
    let headers = build_cloud_headers(key, &request_id);

    // WR-02: Strip trailing slash to prevent double-slash URLs and guard against
    // query-injection if api_url ends with a query separator.
    let base = config.api_url.trim_end_matches('/');
    let url = format!("{base}/api/v1/events/ingest");

    let response = client
        .post(&url)
        .headers(headers)
        .body(body)
        .send()
        .await
        .map_err(|_| CloudError::Network)?;

    let status = response.status();

    if status.is_success() {
        return Ok(());
    }

    match status.as_u16() {
        401 | 403 => Err(CloudError::AuthError),
        429 => {
            // Parse Retry-After header; default to config value if absent or unparsable
            let retry_after_secs = response
                .headers()
                .get("Retry-After")
                .and_then(|v| v.to_str().ok())
                .and_then(|s| s.parse::<u64>().ok())
                .unwrap_or(config.rate_limit_default_secs);
            Err(CloudError::RateLimit { retry_after_secs })
        }
        500..=599 => Err(CloudError::ServerError),
        code => Err(CloudError::ClientError(code)),
    }
}

/// Write the cloud auth error state to disk for cross-process visibility (CLOUD-08).
///
/// Writes `{openlatch_dir}/cloud_state.json` atomically:
/// 1. Write to `cloud_state.json.tmp`
/// 2. Rename to `cloud_state.json`
///
/// This prevents `openlatch status` (a separate process) from reading a partial file.
/// The parent directory is created if it does not exist.
///
/// # Format
///
/// ```json
/// {"auth_error": true, "updated_at": "2026-04-09T12:00:00Z"}
/// ```
pub fn persist_cloud_state(openlatch_dir: &Path, auth_error: bool) -> std::io::Result<()> {
    // Create parent directory if missing
    std::fs::create_dir_all(openlatch_dir)?;

    let updated_at = Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();

    let content = format!(
        "{{\"auth_error\":{},\"updated_at\":\"{}\"}}\n",
        auth_error, updated_at
    );

    let tmp_path = openlatch_dir.join("cloud_state.json.tmp");
    let final_path = openlatch_dir.join("cloud_state.json");

    std::fs::write(&tmp_path, &content)?;
    std::fs::rename(&tmp_path, &final_path)?;

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use secrecy::SecretString;
    use std::sync::atomic::Ordering;
    use std::sync::Mutex;
    use tokio::sync::mpsc;

    // ---------------------------------------------------------------------------
    // Test credential provider
    // ---------------------------------------------------------------------------

    struct TestCredentialProvider {
        key: Mutex<Option<String>>,
        /// Number of `retrieve()` calls. Lets a test observe that the
        /// credential-poll branch of the `select!` is still running.
        retrievals: std::sync::atomic::AtomicU64,
    }

    impl TestCredentialProvider {
        fn with_key(key: &str) -> Arc<Self> {
            Arc::new(Self {
                key: Mutex::new(Some(key.to_string())),
                retrievals: std::sync::atomic::AtomicU64::new(0),
            })
        }

        fn empty() -> Arc<Self> {
            Arc::new(Self {
                key: Mutex::new(None),
                retrievals: std::sync::atomic::AtomicU64::new(0),
            })
        }

        fn set_key(&self, key: &str) {
            *self.key.lock().unwrap() = Some(key.to_string());
        }

        fn retrievals(&self) -> u64 {
            self.retrievals.load(Ordering::Relaxed)
        }
    }

    impl CredentialProvider for TestCredentialProvider {
        fn retrieve(&self) -> Option<SecretString> {
            self.retrievals.fetch_add(1, Ordering::Relaxed);
            self.key
                .lock()
                .ok()
                .and_then(|g| g.as_ref().map(|k| SecretString::from(k.clone())))
        }
    }

    #[test]
    fn test_update_emergency_mode_engages_after_sustained_streak() {
        use std::time::{SystemTime, UNIX_EPOCH};
        let state = CloudState::new();
        let config = CloudConfig::default();
        let mut recovery_ticks = 0u32;

        // Bump drops above the threshold, then back-date the window-start
        // so the detector sees a >10s streak immediately.
        for _ in 0..(EMERGENCY_DROP_THRESHOLD + 1) {
            state.record_live_drop();
        }
        let now_ms = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;
        state.live_drops_window_start.store(
            now_ms.saturating_sub(EMERGENCY_WINDOW_MS + 1_000),
            Ordering::Relaxed,
        );

        update_emergency_mode(&state, &config, &mut recovery_ticks);
        assert!(
            state.is_emergency_mode(),
            "detector must engage on sustained streak"
        );
    }

    #[test]
    fn test_update_emergency_mode_does_not_engage_on_brief_spike() {
        let state = CloudState::new();
        let config = CloudConfig::default();
        let mut recovery_ticks = 0u32;

        // Drops above threshold but the streak window is fresh (<1ms old) —
        // detector must not engage.
        for _ in 0..(EMERGENCY_DROP_THRESHOLD + 1) {
            state.record_live_drop();
        }
        update_emergency_mode(&state, &config, &mut recovery_ticks);
        assert!(
            !state.is_emergency_mode(),
            "detector must not engage on a brief spike (window too short)"
        );
    }

    #[test]
    fn test_update_emergency_mode_clears_after_two_clean_ticks() {
        let state = CloudState::new();
        let config = CloudConfig::default();
        let mut recovery_ticks = 0u32;

        // Force emergency mode on, drops + high-water cleared (simulating
        // successful POSTs draining the channel below the clear threshold).
        state.set_emergency_mode(true);
        assert!(state.is_emergency_mode());

        // First clean tick: counter advances but flag stays.
        update_emergency_mode(&state, &config, &mut recovery_ticks);
        assert!(state.is_emergency_mode());
        assert_eq!(recovery_ticks, 1);

        // Second clean tick: flag clears.
        update_emergency_mode(&state, &config, &mut recovery_ticks);
        assert!(!state.is_emergency_mode());
    }

    #[test]
    fn test_update_emergency_mode_engages_on_sustained_high_water() {
        let state = CloudState::new();
        let config = CloudConfig::default();
        let mut recovery_ticks = 0u32;

        // Back-date the high-water window so the detector sees a >10s streak
        // *without* a live-drop streak — proves the new trigger path works
        // independently of consecutive_live_drops.
        let now_ms = super::super::now_unix_ms();
        state.channel_high_water_start_ms.store(
            now_ms.saturating_sub(HIGH_WATER_WINDOW_MS + 1_000),
            Ordering::Relaxed,
        );
        assert_eq!(state.consecutive_live_drops(), 0);

        update_emergency_mode(&state, &config, &mut recovery_ticks);
        assert!(
            state.is_emergency_mode(),
            "high-water sustained should engage emergency mode without live drops"
        );
    }

    #[test]
    fn test_update_emergency_mode_does_not_clear_while_high_water_active() {
        let state = CloudState::new();
        let config = CloudConfig::default();
        let mut recovery_ticks = 0u32;

        // Emergency mode on, drops cleared, but high-water still latched
        // (start stamped a few ms in the past so `window_ms` is non-zero).
        // Recovery debounce must NOT advance.
        state.set_emergency_mode(true);
        let now_ms = super::super::now_unix_ms();
        state
            .channel_high_water_start_ms
            .store(now_ms.saturating_sub(500), Ordering::Relaxed);

        update_emergency_mode(&state, &config, &mut recovery_ticks);
        assert!(state.is_emergency_mode());
        assert_eq!(
            recovery_ticks, 0,
            "recovery debounce must stay at 0 while high-water remains"
        );
    }

    #[test]
    fn test_build_cloud_client_creates_client_with_pool_max_idle_per_host() {
        // build_cloud_client must succeed and produce a working client
        let config = CloudConfig::default();
        let _client = build_cloud_client(&config);
        // The client is valid if no panic occurred — reqwest doesn't expose internals for assertion
    }

    #[tokio::test]
    async fn test_worker_exits_cleanly_when_channel_closed() {
        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-key");
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();

        // Drop the sender immediately — worker should detect closed channel and exit
        drop(tx);

        // Run worker; it should exit (not hang)
        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            CloudConfig::default(),
            state,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Give the worker 1 second to exit
        let result = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        assert!(
            result.is_ok(),
            "worker must exit when channel is closed (timed out waiting)"
        );
    }

    #[tokio::test]
    async fn test_worker_skips_posts_when_no_credential_available() {
        // Worker with no credential — no HTTP calls should be made
        // (We verify indirectly: no panic, events are skipped, worker keeps running)
        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::empty();
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            CloudConfig::default(),
            state,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Send an event — worker will skip it (no key) without panicking
        let _ = tx
            .send(CloudEvent {
                envelope: serde_json::json!({}),
                agent_id: "agt_test".to_string(),
            })
            .await;

        // Close channel
        drop(tx);

        let result = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;
        assert!(result.is_ok(), "worker must exit cleanly");
    }

    #[test]
    fn test_persist_cloud_state_writes_valid_json_with_auth_error_true() {
        let dir = tempfile::tempdir().unwrap();
        persist_cloud_state(dir.path(), true).expect("persist must succeed");

        let content = std::fs::read_to_string(dir.path().join("cloud_state.json"))
            .expect("cloud_state.json must exist");

        let parsed: serde_json::Value = serde_json::from_str(&content).expect("must be valid JSON");

        assert_eq!(
            parsed["auth_error"], true,
            "auth_error must be true: {content}"
        );
        assert!(
            parsed["updated_at"].as_str().is_some(),
            "updated_at must be present: {content}"
        );
    }

    #[test]
    fn test_persist_cloud_state_writes_valid_json_with_auth_error_false() {
        let dir = tempfile::tempdir().unwrap();
        persist_cloud_state(dir.path(), false).expect("persist must succeed");

        let content = std::fs::read_to_string(dir.path().join("cloud_state.json"))
            .expect("cloud_state.json must exist");

        let parsed: serde_json::Value = serde_json::from_str(&content).expect("must be valid JSON");
        assert_eq!(parsed["auth_error"], false);
    }

    #[test]
    fn test_persist_cloud_state_creates_parent_directory_if_missing() {
        let base = tempfile::tempdir().unwrap();
        let nested = base.path().join("a").join("b").join("c");
        // Directory does not exist yet
        assert!(!nested.exists());

        persist_cloud_state(&nested, false).expect("must create directories and write");

        assert!(nested.join("cloud_state.json").exists());
    }

    #[tokio::test]
    async fn test_worker_auth_error_set_when_credential_available_but_server_returns_401() {
        // This test verifies the auth_error flag is set after a 401 response.
        // We use a mock server that returns 401.
        use std::sync::atomic::Ordering;

        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(401)
            .with_body("{}")
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();

        let config = CloudConfig {
            api_url: server.url(),
            // Batching is on by default (50 events / 5s). This test sends a
            // single event and asserts on the POST before closing the
            // channel, so it needs the time trigger to fire promptly.
            batch_max_wait_ms: 50,
            ..Default::default()
        };

        let state_clone = state.clone();
        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state_clone,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Send an event — should trigger 401, set auth_error
        let _ = tx
            .send(CloudEvent {
                envelope: serde_json::json!({"id": "evt_test"}),
                agent_id: "agt_test".to_string(),
            })
            .await;

        // Give worker time to process the event
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        // auth_error flag must be set
        assert!(
            state.auth_error.load(Ordering::Relaxed),
            "auth_error must be true after 401 response"
        );

        // cloud_state.json must exist and contain auth_error: true
        let state_path = dir.path().join("cloud_state.json");
        assert!(state_path.exists(), "cloud_state.json must be written");
        let content = std::fs::read_to_string(&state_path).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();
        assert_eq!(parsed["auth_error"], true);

        // Cleanup
        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn worker_startup_clears_a_stale_auth_error_left_by_a_dead_daemon() {
        // A daemon that is killed while latched leaves {"auth_error":true} on
        // disk. The replacement worker starts at auth_error=false and never
        // transitions, so without an explicit write at startup it would inherit
        // — and keep publishing — the dead daemon's claim about itself.
        let dir = tempfile::tempdir().unwrap();
        let state_path = dir.path().join("cloud_state.json");
        persist_cloud_state(dir.path(), true).unwrap();
        let stale: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&state_path).unwrap()).unwrap();
        assert_eq!(
            stale["auth_error"], true,
            "precondition: stale latch on disk"
        );

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            CloudConfig::default(),
            state.clone(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // No events sent: the correction must come from starting up, not from
        // a successful POST.
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        let parsed: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&state_path).unwrap()).unwrap();
        assert_eq!(
            parsed["auth_error"], false,
            "a freshly started worker must publish its own state, not inherit \
             the previous daemon's latch"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
    }

    #[tokio::test]
    async fn test_worker_retries_once_on_5xx_then_drops() {
        // Mock server that returns 500 — worker should retry exactly once (2 requests total)
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(500)
            .with_body("{}")
            .expect(2) // exactly 2 requests: original + 1 retry
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();

        let config = CloudConfig {
            api_url: server.url(),
            retry_delay_ms: 10, // Fast retry for tests
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        let _ = tx
            .send(CloudEvent {
                envelope: serde_json::json!({}),
                agent_id: "agt_test".to_string(),
            })
            .await;

        // Wait for the retry cycle (10ms delay + processing time)
        tokio::time::sleep(std::time::Duration::from_millis(300)).await;

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn test_worker_honors_retry_after_header_on_429() {
        // Mock returns 429 with Retry-After: 1 — worker should wait then retry
        let mut server = mockito::Server::new_async().await;
        // First call: 429 with Retry-After
        let mock_429 = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(429)
            .with_header("Retry-After", "1")
            .with_body("{}")
            .expect(1)
            .create_async()
            .await;
        // Retry call: 200 success
        let mock_200 = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(200)
            .with_body("{\"status\":\"accepted\"}")
            .expect(1)
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();

        let config = CloudConfig {
            api_url: server.url(),
            // Batching is on by default (50 events / 5s). This test sends a
            // single event and asserts on the POST before closing the
            // channel, so it needs the time trigger to fire promptly.
            batch_max_wait_ms: 50,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        let _ = tx
            .send(CloudEvent {
                envelope: serde_json::json!({}),
                agent_id: "agt_test".to_string(),
            })
            .await;

        // Wait for retry cycle: 1s Retry-After + processing overhead
        tokio::time::sleep(std::time::Duration::from_millis(1500)).await;

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        mock_429.assert_async().await;
        mock_200.assert_async().await;
    }

    #[tokio::test]
    async fn test_health_tick_clears_consecutive_drops_on_2xx() {
        let mut server = mockito::Server::new_async().await;
        let mock_health = server
            .mock("GET", "/api/v1/health")
            .with_status(200)
            .with_body(r#"{"status":"ok"}"#)
            .expect_at_least(1)
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        state.record_drop();
        state.record_drop();
        state.record_drop();
        assert_eq!(state.consecutive_drops(), 3);
        assert_eq!(state.drop_count(), 3);
        let dir = tempfile::tempdir().unwrap();

        let config = CloudConfig {
            api_url: server.url(),
            ..Default::default()
        };

        let state_clone = state.clone();
        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state_clone,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Health tick fires every 50ms in test builds — wait long enough for at least one.
        tokio::time::sleep(std::time::Duration::from_millis(250)).await;

        assert_eq!(
            state.consecutive_drops(),
            0,
            "health tick must clear consecutive_drops on 2xx"
        );
        assert_eq!(
            state.drop_count(),
            3,
            "lifetime drop_count must be preserved"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        mock_health.assert_async().await;
    }

    #[tokio::test]
    async fn test_health_tick_does_not_clear_on_5xx() {
        let mut server = mockito::Server::new_async().await;
        let mock_health = server
            .mock("GET", "/api/v1/health")
            .with_status(500)
            .with_body("{}")
            .expect_at_least(1)
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        state.record_drop();
        state.record_drop();
        let dir = tempfile::tempdir().unwrap();

        let config = CloudConfig {
            api_url: server.url(),
            ..Default::default()
        };

        let state_clone = state.clone();
        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state_clone,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        tokio::time::sleep(std::time::Duration::from_millis(250)).await;

        assert_eq!(
            state.consecutive_drops(),
            2,
            "failed health probe must not reset consecutive_drops"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        mock_health.assert_async().await;
    }

    #[tokio::test]
    async fn test_successful_post_clears_consecutive_drops() {
        let mut server = mockito::Server::new_async().await;
        let _mock_health = server
            .mock("GET", "/api/v1/health")
            .with_status(500)
            .with_body("{}")
            .create_async()
            .await;
        let mock_ingest = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(200)
            .with_body(r#"{"status":"accepted"}"#)
            .expect(1)
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        state.record_drop();
        state.record_drop();
        state.record_drop();
        let dir = tempfile::tempdir().unwrap();

        let config = CloudConfig {
            api_url: server.url(),
            // Batching is on by default (50 events / 5s). This test sends a
            // single event and asserts on the POST before closing the
            // channel, so it needs the time trigger to fire promptly.
            batch_max_wait_ms: 50,
            ..Default::default()
        };

        let state_clone = state.clone();
        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state_clone,
            dir.path().to_path_buf(),
            None,
            None,
        ));

        let _ = tx
            .send(CloudEvent {
                envelope: serde_json::json!({"id": "evt_test"}),
                agent_id: "agt_test".to_string(),
            })
            .await;

        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        assert_eq!(
            state.consecutive_drops(),
            0,
            "successful forward must clear consecutive_drops"
        );
        assert_eq!(state.drop_count(), 3, "lifetime drop_count preserved");
        assert_eq!(state.forwarded_count(), 1);

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        mock_ingest.assert_async().await;
    }

    /// Regression: when the worker starts with no credential and receives
    /// events, it must skip them silently after the first warning, then pick
    /// up a late-arriving credential via the hot-reload tick and resume POSTs.
    ///
    /// The observable assertion is that exactly one HTTP POST reaches the
    /// ingestion endpoint — the three events sent before the credential was
    /// loaded must not hit the server.
    #[tokio::test]
    async fn test_worker_recovers_when_credential_appears_after_startup() {
        let mut server = mockito::Server::new_async().await;
        let mock_ingest = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(200)
            .with_body(r#"{"status":"accepted"}"#)
            .expect(1)
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::empty();
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();

        let config = CloudConfig {
            api_url: server.url(),
            // 50ms reload so the test can flip the credential and observe
            // recovery without a real-time wait.
            credential_poll_interval_ms: 50,
            ..Default::default()
        };

        let provider_handle = provider.clone();
        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state.clone(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Three events arrive while the credential is missing — all must be
        // skipped without reaching the mock server.
        for i in 0..3 {
            let _ = tx
                .send(CloudEvent {
                    envelope: serde_json::json!({"id": format!("evt_{i}")}),
                    agent_id: "agt_test".to_string(),
                })
                .await;
        }
        tokio::time::sleep(std::time::Duration::from_millis(30)).await;
        assert_eq!(
            state.forwarded_count(),
            0,
            "events must not be forwarded before a credential is available"
        );

        // Credential appears — the 50ms test-mode reload tick will pick it up.
        provider_handle.set_key("late-arriving-key");
        tokio::time::sleep(std::time::Duration::from_millis(120)).await;

        // Next event must actually be POSTed.
        let _ = tx
            .send(CloudEvent {
                envelope: serde_json::json!({"id": "evt_after_reload"}),
                agent_id: "agt_test".to_string(),
            })
            .await;
        tokio::time::sleep(std::time::Duration::from_millis(150)).await;

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(1), handle).await;
        mock_ingest.assert_async().await;
        assert_eq!(
            state.forwarded_count(),
            1,
            "exactly one event should be forwarded after credential hot-reload"
        );
    }

    /// End-to-end poison-pill: when the same outbox entry fails on every
    /// drain pass, the worker must quarantine it after OUTBOX_MAX_ATTEMPTS
    /// so the queue can drain past it. Without this behaviour, a single
    /// permanently-rejecting envelope at the head of `outbox.jsonl` blocks
    /// every entry behind it forever (the pre-fix bug).
    #[tokio::test]
    async fn test_outbox_quarantines_on_persistent_5xx() {
        use crate::core::cloud::outbox::Outbox;

        let mut server = mockito::Server::new_async().await;
        // Every POST returns 500. We expect: `OUTBOX_MAX_ATTEMPTS` attempts
        // per envelope on the live path (1 initial + 1 retry inside
        // post_event, multiplied across the 5-attempt budget) plus drain
        // retries until quarantine kicks in. We accept any number ≥ the
        // minimum to avoid timing flakiness — the important assertion is
        // that `pending_count()` returns to 0.
        let _mock_ingest = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(500)
            .with_body("boom")
            .expect_at_least(2)
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let outbox = Arc::new(Outbox::new(dir.path(), 0));

        let config = CloudConfig {
            api_url: server.url(),
            retry_delay_ms: 5,
            ..Default::default()
        };

        // Pre-seed the outbox with one envelope so we observe the drain
        // task's quarantine path directly (rather than the live retry path).
        outbox
            .append(&serde_json::json!({"id": "evt_poison"}))
            .unwrap();
        assert_eq!(outbox.pending_count(), 1);

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state.clone(),
            dir.path().to_path_buf(),
            Some(outbox.clone()),
            None,
        ));

        // Trigger repeated drain attempts via successive notify_drain. The
        // health tick fires every 50ms in test builds, and each failed POST
        // notifies again, so within ~1s the per-id attempt budget exhausts.
        for _ in 0..(OUTBOX_MAX_ATTEMPTS as usize + 4) {
            state.notify_drain();
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        }

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;

        assert_eq!(
            outbox.pending_count(),
            0,
            "quarantine must drain the outbox past the poison entry"
        );
    }

    /// End-to-end outbox replay: when the first POST fails, the envelope
    /// must land in `outbox.jsonl`. When the cloud later recovers, the
    /// drain task fired from a successful health probe must flush it. The
    /// observable assertions are:
    /// - Mockito receives exactly two successful ingest calls: one for the
    ///   health recovery tick (a GET) and one for the replayed event.
    /// - `outbox.jsonl` is empty after recovery.
    /// - `forwarded_count` reflects the replayed event.
    #[tokio::test]
    async fn test_outbox_spools_on_failure_and_drains_on_recovery() {
        use crate::core::cloud::outbox::Outbox;

        // First request returns 500 (live POST fails). Second wave (health
        // probe GET + replayed POST) returns 200.
        let mut server = mockito::Server::new_async().await;
        let mock_fail_ingest = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(500)
            .with_body("boom")
            .expect(2) // live attempt + 1 retry inside the flush
            .create_async()
            .await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let outbox = Arc::new(Outbox::new(dir.path(), 0));

        let config = CloudConfig {
            api_url: server.url(),
            // Batching is on by default (50 events / 5s). This test sends a
            // single event and asserts on the POST before closing the
            // channel, so it needs the time trigger to fire promptly.
            batch_max_wait_ms: 50,
            retry_delay_ms: 10,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state.clone(),
            dir.path().to_path_buf(),
            Some(outbox.clone()),
            None,
        ));

        // Live POST fails twice (initial + retry) → envelope lands in outbox.
        let _ = tx
            .send(CloudEvent {
                envelope: serde_json::json!({"id": "evt_offline_1"}),
                agent_id: "agt_test".to_string(),
            })
            .await;
        tokio::time::sleep(std::time::Duration::from_millis(300)).await;
        mock_fail_ingest.assert_async().await;
        assert_eq!(outbox.pending_count(), 1, "envelope must be spooled");
        assert_eq!(state.forwarded_count(), 0);

        // Cloud recovers. Swap mockito: health probe returns 200, and the
        // drain task's replay POST also returns 200.
        server.reset();
        let mock_health = server
            .mock("GET", "/api/v1/health")
            .with_status(200)
            .expect_at_least(1)
            .create_async()
            .await;
        let mock_replay = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(200)
            .with_body(r#"{"status":"accepted"}"#)
            .expect(1)
            .create_async()
            .await;

        // The test-mode health tick is 50ms. Give the drain task ample
        // time to run after recovery signal fires.
        tokio::time::sleep(std::time::Duration::from_millis(300)).await;

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;

        mock_health.assert_async().await;
        mock_replay.assert_async().await;
        assert_eq!(
            outbox.pending_count(),
            0,
            "outbox must be drained after cloud recovery"
        );
        assert!(
            state.forwarded_count() >= 1,
            "forwarded_count must reflect the replayed event, got {}",
            state.forwarded_count()
        );
    }

    // =================================================================
    // Batching (plan 03)
    //
    // The assertions below are on REQUEST COUNT, not just event delivery.
    // A worker that posts one request per event still delivers every event;
    // only the request count catches the regression.
    // =================================================================

    /// Request bodies captured from the ingest endpoint, in arrival order.
    type Captured = Arc<Mutex<Vec<String>>>;

    /// Mount `POST /api/v1/events/ingest` and record every request body.
    ///
    /// `with_body_from_request` is the only hook mockito offers that runs once
    /// per matched request and can see the request body; the `Vec<u8>` it
    /// returns is the response the worker reads back.
    async fn capture_ingest(
        server: &mut mockito::ServerGuard,
        status: usize,
    ) -> (mockito::Mock, Captured) {
        let captured: Captured = Arc::new(Mutex::new(Vec::new()));
        let sink = captured.clone();
        let mock = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(status)
            .with_body_from_request(move |req| {
                let body = req
                    .utf8_lossy_body()
                    .map(|b| b.to_string())
                    .unwrap_or_default();
                sink.lock().unwrap().push(body);
                br#"{"status":"accepted"}"#.to_vec()
            })
            .expect_at_least(0)
            .create_async()
            .await;
        (mock, captured)
    }

    /// Mount a permissive, always-failing health probe.
    ///
    /// `health_delay` is `#[cfg(test)]`-overridden to 50ms and
    /// `cloud_health_check` GETs this path on the SAME mockito base URL. A
    /// 2xx probe calls `notify_drain()`, which wakes the outbox drain task and
    /// issues EXTRA POSTs to the ingest endpoint — silently corrupting every
    /// request-count assertion here. Returning 500 keeps the branch exercised
    /// without that side effect.
    async fn quiet_health(server: &mut mockito::ServerGuard) -> mockito::Mock {
        server
            .mock("GET", "/api/v1/health")
            .with_status(500)
            .with_body("{}")
            .expect_at_least(0)
            .create_async()
            .await
    }

    fn evt(id: &str) -> CloudEvent {
        CloudEvent {
            envelope: serde_json::json!({"id": id, "specversion": "1.0"}),
            agent_id: "agt_test".to_string(),
        }
    }

    /// Element counts of each captured batch, in arrival order.
    fn batch_sizes(captured: &Captured) -> Vec<usize> {
        captured
            .lock()
            .unwrap()
            .iter()
            .map(|body| {
                serde_json::from_str::<Vec<serde_json::Value>>(body)
                    .expect("every request body must be a JSON array")
                    .len()
            })
            .collect()
    }

    /// The PRD's named case: 120 events at `batch_max_events = 50` is
    /// **three** requests, not 120. The time trigger is pushed out of reach so
    /// this isolates the size trigger plus the shutdown flush.
    #[tokio::test]
    async fn exact_request_count_for_n_events() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(200);
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            state.clone(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        for i in 0..120 {
            tx.send(evt(&format!("evt_{i:03}"))).await.unwrap();
        }
        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(5), handle).await;

        assert_eq!(
            batch_sizes(&captured),
            vec![50, 50, 20],
            "120 events at batch_max_events=50 must be exactly 3 requests"
        );
        assert_eq!(state.forwarded_count(), 120);
    }

    /// Under the size trigger, the time trigger still gets the batch out.
    #[tokio::test]
    async fn flushes_on_time_when_under_size() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 150,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            CloudState::new(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        for i in 0..3 {
            tx.send(evt(&format!("evt_{i}"))).await.unwrap();
        }
        // Well past the deadline, well short of the 50-event size trigger.
        tokio::time::sleep(std::time::Duration::from_millis(600)).await;

        assert_eq!(
            batch_sizes(&captured),
            vec![3],
            "3 events must leave as one time-triggered request"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;
    }

    /// **The D43 regression test.** The deadline is anchored to the FIRST
    /// buffered event and is never reset by later ones.
    ///
    /// Timeline with `batch_max_wait_ms = 1000`: push at t=0, push at t=700,
    /// observe at t=1200. Anchored to the first event the flush is due at
    /// t=1000 and has happened. Reset on every push it would be due at
    /// t=1700 and nothing would have been sent — which is exactly how
    /// `batch_max_wait_ms` silently stops bounding latency under load.
    #[tokio::test]
    async fn deadline_anchored_to_first_item() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 1000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            CloudState::new(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        tx.send(evt("evt_first")).await.unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(700)).await;
        tx.send(evt("evt_second")).await.unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;

        assert_eq!(
            batch_sizes(&captured),
            vec![2],
            "the batch must flush 1000ms after the FIRST event, not after the last"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;
    }

    /// No request may exceed either wire cap. The payloads here are sized so
    /// the BYTE cap bites first — capping on element count alone would
    /// produce a body the platform rejects with 413 before it parses it.
    #[tokio::test]
    async fn no_request_exceeds_caps() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(400);
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 100,
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            CloudState::new(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        let padding = "x".repeat(4096);
        for i in 0..300 {
            tx.send(CloudEvent {
                envelope: serde_json::json!({"id": format!("evt_{i:03}"), "data": padding}),
                agent_id: "agt_test".to_string(),
            })
            .await
            .unwrap();
        }
        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(10), handle).await;

        let bodies = captured.lock().unwrap().clone();
        assert!(!bodies.is_empty(), "events must have been forwarded");
        let mut total = 0usize;
        let mut byte_capped = false;
        for body in &bodies {
            assert!(
                body.len() <= MAX_BATCH_BYTES,
                "request body of {} bytes exceeds the 256KB cap",
                body.len()
            );
            let elements: Vec<serde_json::Value> = serde_json::from_str(body).unwrap();
            assert!(
                elements.len() <= MAX_BATCH_EVENTS,
                "request carried {} events, over the 100-event cap",
                elements.len()
            );
            if elements.len() < 100 {
                byte_capped = true;
            }
            total += elements.len();
        }
        assert_eq!(total, 300, "every event must still be delivered");
        assert!(
            byte_capped,
            "4KB payloads must close batches on the byte cap before the 100-event cap"
        );
    }

    /// An event too large to fit in any batch is dropped with OL-1002 rather
    /// than wedging the queue behind a POST that fails identically forever.
    /// The events behind it still flush.
    #[tokio::test]
    async fn oversized_single_event_dropped() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 100,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            state.clone(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        tx.send(CloudEvent {
            envelope: serde_json::json!({"id": "evt_huge", "data": "x".repeat(300_000)}),
            agent_id: "agt_test".to_string(),
        })
        .await
        .unwrap();
        tx.send(evt("evt_ok_1")).await.unwrap();
        tx.send(evt("evt_ok_2")).await.unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;

        assert_eq!(
            batch_sizes(&captured),
            vec![2],
            "only the two normal events may be posted — the queue must not wedge"
        );
        assert_eq!(
            state.drop_count(),
            1,
            "the oversized event counts as a drop"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;
    }

    /// D23 — a failed batch spools EVERY event individually. Asserted by LINE
    /// COUNT, not by file presence: spooling the batch as one line would
    /// satisfy "the file exists" while breaking the drain, the per-entry
    /// attempt counter and the quarantine logic.
    #[tokio::test]
    async fn failed_batch_spools_every_event() {
        use crate::core::cloud::outbox::Outbox;

        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 500).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(20);
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let outbox = Arc::new(Outbox::new(dir.path(), 0));
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 100,
            retry_delay_ms: 10,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            state.clone(),
            dir.path().to_path_buf(),
            Some(outbox.clone()),
            None,
        ));

        for i in 0..7 {
            tx.send(evt(&format!("evt_{i}"))).await.unwrap();
        }
        tokio::time::sleep(std::time::Duration::from_millis(600)).await;

        assert_eq!(
            batch_sizes(&captured),
            vec![7, 7],
            "one batch attempt plus exactly one retry"
        );
        let body = std::fs::read_to_string(outbox.path()).expect("outbox.jsonl must exist");
        assert_eq!(
            body.lines().count(),
            7,
            "a failed batch of 7 must spool 7 LINES, not one batch line: {body}"
        );
        for line in body.lines() {
            serde_json::from_str::<serde_json::Value>(line)
                .expect("every spooled line must be a standalone envelope");
        }
        assert_eq!(state.drop_count(), 7, "a spooled batch of 7 is 7 drops");

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;
    }

    /// The drain re-batches: 120 seeded outbox entries replay as 3 requests,
    /// not 120. Without the group drain in `outbox.rs` this is impossible —
    /// the callback used to receive one entry at a time.
    #[tokio::test]
    async fn outbox_drain_rebatches() {
        use crate::core::cloud::outbox::Outbox;

        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(10);
        let dir = tempfile::tempdir().unwrap();
        let outbox = Arc::new(Outbox::new(dir.path(), 0));
        for i in 0..120 {
            outbox
                .append(&serde_json::json!({"id": format!("evt_{i:03}")}))
                .unwrap();
        }

        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            CloudState::new(),
            dir.path().to_path_buf(),
            Some(outbox.clone()),
            None,
        ));

        // The drain task fires once at worker startup; no live events are
        // sent, so every request below comes from the replay path.
        tokio::time::sleep(std::time::Duration::from_millis(800)).await;

        assert_eq!(
            batch_sizes(&captured),
            vec![50, 50, 20],
            "120 outbox entries must replay as 3 requests"
        );
        assert_eq!(outbox.pending_count(), 0, "the outbox must be empty");

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;
    }

    /// C5 — nothing is lost at shutdown. The explicit signal (not channel
    /// closure) is what fires here, because `cloud_tx` clones outlive the
    /// daemon's own reference.
    #[tokio::test]
    async fn shutdown_flushes_in_flight() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(20);
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            state.clone(),
            dir.path().to_path_buf(),
            None,
            Some(shutdown_rx),
        ));

        for i in 0..7 {
            tx.send(evt(&format!("evt_{i}"))).await.unwrap();
        }
        tokio::time::sleep(std::time::Duration::from_millis(150)).await;
        assert!(
            captured.lock().unwrap().is_empty(),
            "the batch must still be in flight — neither trigger has fired"
        );

        // The sender is deliberately still alive: channel closure is NOT what
        // ends the worker here.
        shutdown_tx.send(true).unwrap();
        let exited = tokio::time::timeout(std::time::Duration::from_secs(3), handle).await;
        assert!(exited.is_ok(), "worker must exit on the shutdown signal");

        assert_eq!(
            batch_sizes(&captured),
            vec![7],
            "all 7 buffered events must flush at shutdown — zero lost"
        );
        assert_eq!(state.forwarded_count(), 7);
        drop(tx);
    }

    /// D44 — the `select!` loses nothing across randomised arrival timing.
    /// Delivered + spooled must equal what was enqueued.
    #[tokio::test]
    async fn select_loop_loses_nothing() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(100);
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 25,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            state.clone(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Deterministic pseudo-random pacing: bursts, yields and short sleeps
        // so events land on every combination of "timer armed / buffer
        // partially full / flush in flight".
        for i in 0..1000u32 {
            tx.send(evt(&format!("evt_{i:04}"))).await.unwrap();
            match i % 7 {
                0 => tokio::time::sleep(std::time::Duration::from_millis(1)).await,
                3 => tokio::task::yield_now().await,
                _ => {}
            }
        }
        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(30), handle).await;

        let delivered: usize = batch_sizes(&captured).iter().sum();
        assert_eq!(delivered, 1000, "every enqueued event must be delivered");
        assert_eq!(state.forwarded_count(), 1000);
    }

    /// Every element of every batch carries the `agentid` extension —
    /// stamping is per envelope, not per request.
    #[tokio::test]
    async fn agentid_stamped_on_every_envelope() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(20);
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 3,
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            CloudState::new(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        for i in 0..9 {
            tx.send(CloudEvent {
                envelope: serde_json::json!({"id": format!("evt_{i}")}),
                agent_id: format!("agt_{i}"),
            })
            .await
            .unwrap();
        }
        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(5), handle).await;

        let bodies = captured.lock().unwrap().clone();
        assert_eq!(
            bodies.len(),
            3,
            "9 events at batch_max_events=3 is 3 requests"
        );
        let mut seen = 0;
        for body in &bodies {
            let elements: Vec<serde_json::Value> = serde_json::from_str(body).unwrap();
            for element in elements {
                let id = element["id"].as_str().unwrap();
                let n = id.trim_start_matches("evt_");
                assert_eq!(
                    element["agentid"].as_str(),
                    Some(format!("agt_{n}").as_str()),
                    "every envelope must carry its own agentid: {element}"
                );
                seen += 1;
            }
        }
        assert_eq!(seen, 9);
    }

    /// `clientversion` is stamped at egress even when the producer never set
    /// it. This is the regression test for the economics + config streams:
    /// `boundary/emit.rs` and both `config_monitor` builders hand-roll their
    /// envelopes without the attribute, which left
    /// `unified_events.clientversion` NULL for every row they produced.
    #[test]
    fn clientversion_stamped_when_producer_omitted_it() {
        // The exact shape `boundary::emit::assemble_event` builds — no
        // `clientversion` anywhere in it.
        let event = CloudEvent {
            envelope: serde_json::json!({
                "specversion": "1.0",
                "id": "evt_boundary",
                "source": "claude-code",
                "type": "ai.openlatch.economics.usage",
                "datacontenttype": "application/json",
                "data": {"tokens": 42},
            }),
            agent_id: "agt_test".to_string(),
        };

        let stamped = stamp_extensions(&event);

        assert_eq!(
            stamped["clientversion"].as_str(),
            Some(env!("OPENLATCH_VERSION")),
            "an envelope that reaches egress without a version must leave with one"
        );
        // The pre-existing stamp must survive alongside the new one.
        assert_eq!(stamped["agentid"].as_str(), Some("agt_test"));
        // And nothing the producer set may be disturbed.
        assert_eq!(stamped["data"]["tokens"], 42);
        assert_eq!(stamped["type"], "ai.openlatch.economics.usage");
    }

    /// The attribute identifies the **forwarder**, not the emitter, so the
    /// daemon's own version always wins — the same rule
    /// `daemon/handlers.rs::process_envelope` applies to hook envelopes. A
    /// stale `openlatch-hook` binary reporting an older version must not be
    /// what the platform's fleet-readiness check reads.
    #[test]
    fn clientversion_overwrites_a_stale_producer_value() {
        let event = CloudEvent {
            envelope: serde_json::json!({
                "id": "evt_stale_hook",
                "clientversion": "0.0.1-stale",
            }),
            agent_id: "agt_test".to_string(),
        };

        let stamped = stamp_extensions(&event);

        assert_eq!(
            stamped["clientversion"].as_str(),
            Some(env!("OPENLATCH_VERSION")),
            "the forwarder's version must win over the emitter's"
        );
    }

    /// Egress coverage is total: every element of every batch carries
    /// `clientversion`, including producers that never set it. Complements the
    /// unit tests above by proving it through the real worker + POST path.
    #[tokio::test]
    async fn clientversion_stamped_on_every_envelope() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(20);
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 3,
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            CloudState::new(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Mixed producers: unversioned (boundary/config) and a stale hook.
        for i in 0..6 {
            let envelope = if i % 2 == 0 {
                serde_json::json!({"id": format!("evt_{i}")})
            } else {
                serde_json::json!({"id": format!("evt_{i}"), "clientversion": "0.0.1-stale"})
            };
            tx.send(CloudEvent {
                envelope,
                agent_id: format!("agt_{i}"),
            })
            .await
            .unwrap();
        }
        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(5), handle).await;

        let bodies = captured.lock().unwrap().clone();
        let mut seen = 0;
        for body in &bodies {
            let elements: Vec<serde_json::Value> = serde_json::from_str(body).unwrap();
            for element in elements {
                assert_eq!(
                    element["clientversion"].as_str(),
                    Some(env!("OPENLATCH_VERSION")),
                    "every envelope on the wire must carry the forwarder version: {element}"
                );
                seen += 1;
            }
        }
        assert_eq!(seen, 6, "every enqueued event must reach the wire");
    }

    /// The status → `CloudError` mapping is unchanged by batching; only the
    /// unit it operates on went from one event to N.
    #[tokio::test]
    async fn error_mapping_unchanged() {
        let dir = tempfile::tempdir().unwrap();
        let key = SecretString::from("test-api-key".to_string());
        let batch = vec![prepare_one(serde_json::json!({"id": "evt_1"})).unwrap()];

        for (status, header, expected) in [
            (401u16, None, CloudError::AuthError),
            (403, None, CloudError::AuthError),
            (
                429,
                Some(("Retry-After", "7")),
                CloudError::RateLimit {
                    retry_after_secs: 7,
                },
            ),
            (500, None, CloudError::ServerError),
            (503, None, CloudError::ServerError),
            (418, None, CloudError::ClientError(418)),
        ] {
            let mut server = mockito::Server::new_async().await;
            let mut mock = server
                .mock("POST", "/api/v1/events/ingest")
                .with_status(status as usize);
            if let Some((name, value)) = header {
                mock = mock.with_header(name, value);
            }
            let _mock = mock.with_body("{}").create_async().await;

            let config = CloudConfig {
                api_url: server.url(),
                ..Default::default()
            };
            let client = build_cloud_client(&config);
            let err = post_batch(&client, &config, &key, &batch, dir.path())
                .await
                .expect_err("non-2xx must map to an error");
            assert_eq!(
                format!("{err:?}"),
                format!("{expected:?}"),
                "HTTP {status} must map to {expected:?}"
            );
        }

        // 429 with no parsable Retry-After falls back to the config default.
        let mut server = mockito::Server::new_async().await;
        let _mock = server
            .mock("POST", "/api/v1/events/ingest")
            .with_status(429)
            .with_body("{}")
            .create_async()
            .await;
        let config = CloudConfig {
            api_url: server.url(),
            rate_limit_default_secs: 42,
            ..Default::default()
        };
        let client = build_cloud_client(&config);
        let err = post_batch(&client, &config, &key, &batch, dir.path())
            .await
            .unwrap_err();
        assert!(
            matches!(
                err,
                CloudError::RateLimit {
                    retry_after_secs: 42
                }
            ),
            "missing Retry-After must fall back to rate_limit_default_secs, got {err:?}"
        );

        // Transport failure — nothing listening on port 1.
        let config = CloudConfig {
            api_url: "http://127.0.0.1:1".to_string(),
            timeout_connect_ms: 500,
            timeout_total_ms: 1000,
            ..Default::default()
        };
        let client = build_cloud_client(&config);
        let err = post_batch(&client, &config, &key, &batch, dir.path())
            .await
            .unwrap_err();
        assert!(
            matches!(err, CloudError::Network),
            "transport failure must map to Network, got {err:?}"
        );
    }

    /// The regression test for "extend the `select!`, don't replace it": the
    /// credential-refresh and health-probe branches must keep firing while the
    /// buffer is non-empty and the batch timer is armed. Replacing the loop
    /// with a two-branch version would silently break auth recovery, the
    /// `cloud_status` on `/metrics`, outbox-recovery notifications and
    /// emergency-mode transitions.
    #[tokio::test]
    async fn existing_select_branches_still_fire() {
        let mut server = mockito::Server::new_async().await;
        let health = server
            .mock("GET", "/api/v1/health")
            .with_status(200)
            .with_body(r#"{"status":"ok"}"#)
            .expect_at_least(1)
            .create_async()
            .await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(20);
        let provider = TestCredentialProvider::with_key("test-api-key");
        let observer = provider.clone();
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            credential_poll_interval_ms: 50,
            batch_max_events: 50,
            // Neither trigger may fire during the observation window: the
            // point is that the OTHER branches still run while the batch waits.
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            provider,
            config,
            state.clone(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        for i in 0..3 {
            tx.send(evt(&format!("evt_{i}"))).await.unwrap();
        }
        tokio::time::sleep(std::time::Duration::from_millis(400)).await;

        assert!(
            captured.lock().unwrap().is_empty(),
            "the buffer must still be held — otherwise this proves nothing"
        );
        health.assert_async().await;
        assert!(
            observer.retrievals() >= 2,
            "the credential-poll branch must keep firing while a batch waits, got {} retrievals",
            observer.retrievals()
        );
        assert_eq!(
            state.consecutive_probe_failures(),
            0,
            "successful health probes must still be recorded"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(3), handle).await;
        assert_eq!(batch_sizes(&captured), vec![3]);
    }

    /// The guarantee `biased;` + timer-first exists to protect: with the
    /// channel continuously ready and the size trigger out of reach, the batch
    /// still leaves on the timer rather than piling up until shutdown.
    ///
    /// Scope, honestly: this asserts the *guarantee*, not the *keyword*.
    /// Deleting `biased;` does NOT fail this test — an unbiased `select!`
    /// still gives the due timer roughly even odds on each iteration, so it
    /// wins after a couple of loops and the flush is merely late by
    /// microseconds, not absent. `biased;` is what makes the bound
    /// deterministic instead of probabilistic (starvation probability decays
    /// as 0.5^k rather than being zero), which is why it stays. Do not read a
    /// green run here as licence to remove it.
    #[tokio::test]
    async fn due_timer_wins_over_saturated_channel() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(64);
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            // Out of reach: the timer is the ONLY thing that can flush.
            batch_max_events: 100_000,
            batch_max_wait_ms: 25,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            CloudState::new(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        // Keep `rx` continuously ready for ~100ms — four batch deadlines.
        let producer_tx = tx.clone();
        let producer = tokio::spawn(async move {
            for i in 0..2000u32 {
                if producer_tx.send(evt(&format!("evt_{i:04}"))).await.is_err() {
                    break;
                }
                if i % 20 == 0 {
                    tokio::time::sleep(std::time::Duration::from_millis(1)).await;
                }
            }
        });
        producer.await.unwrap();

        // Read BEFORE closing the channel: the shutdown flush would otherwise
        // hand us a request the timer never produced.
        let mid_stream = captured.lock().unwrap().len();
        assert!(
            mid_stream >= 2,
            "the due timer must win against a continuously-ready channel — \
             expected multiple mid-stream flushes, got {mid_stream}"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(10), handle).await;
    }

    /// `cloud_forwarded_count` counts EVENTS, not requests. Recording one per
    /// successful batch would under-report by up to 100x on `/metrics` and in
    /// `openlatch status`.
    #[tokio::test]
    async fn forwarded_count_is_batch_length() {
        let mut server = mockito::Server::new_async().await;
        let _health = quiet_health(&mut server).await;
        let (_ingest, captured) = capture_ingest(&mut server, 200).await;

        let (tx, rx) = mpsc::channel::<CloudEvent>(100);
        let state = CloudState::new();
        let dir = tempfile::tempdir().unwrap();
        let config = CloudConfig {
            api_url: server.url(),
            batch_max_events: 50,
            batch_max_wait_ms: 600_000,
            ..Default::default()
        };

        let handle = tokio::spawn(run_cloud_worker(
            rx,
            TestCredentialProvider::with_key("test-api-key"),
            config,
            state.clone(),
            dir.path().to_path_buf(),
            None,
            None,
        ));

        for i in 0..50 {
            tx.send(evt(&format!("evt_{i:02}"))).await.unwrap();
        }
        tokio::time::sleep(std::time::Duration::from_millis(400)).await;

        assert_eq!(
            batch_sizes(&captured),
            vec![50],
            "one request for 50 events"
        );
        assert_eq!(
            state.forwarded_count(),
            50,
            "forwarded_count must be the batch LENGTH, not 1"
        );

        drop(tx);
        let _ = tokio::time::timeout(std::time::Duration::from_secs(2), handle).await;
    }

    // -----------------------------------------------------------------
    // split_batches — unit coverage for the greedy split itself
    // -----------------------------------------------------------------

    fn prepared_of(size: usize, count: usize) -> Vec<PreparedEvent> {
        (0..count)
            .map(|i| {
                prepare_one(serde_json::json!({"id": format!("evt_{i}"), "data": "x".repeat(size)}))
                    .expect("fixture must be sendable")
            })
            .collect()
    }

    #[test]
    fn split_batches_caps_on_element_count() {
        let prepared = prepared_of(8, 250);
        let groups = split_batches(&prepared, 50);
        assert_eq!(
            groups.iter().map(|g| g.len()).collect::<Vec<_>>(),
            vec![50; 5]
        );
    }

    #[test]
    fn split_batches_never_exceeds_the_platform_element_cap() {
        let prepared = prepared_of(8, 250);
        // The tuning knob may not raise the batch above the platform's cap.
        let groups = split_batches(&prepared, 100_000);
        assert!(groups.iter().all(|g| g.len() <= MAX_BATCH_EVENTS));
        assert_eq!(groups[0].len(), MAX_BATCH_EVENTS);
    }

    #[test]
    fn split_batches_closes_on_the_byte_cap() {
        // ~16KB each: 16 fit under 256KB, 17 do not.
        let prepared = prepared_of(16_000, 40);
        let groups = split_batches(&prepared, 100);
        for group in &groups {
            let framed: usize = group.iter().map(|e| e.json.len()).sum::<usize>()
                + 2
                + group.len().saturating_sub(1);
            assert!(
                framed <= MAX_BATCH_BYTES,
                "group of {} serialises to {framed} bytes",
                group.len()
            );
        }
        assert_eq!(
            groups.iter().map(|g| g.len()).sum::<usize>(),
            40,
            "the split must not lose events"
        );
        assert!(groups.len() > 1, "40 x 16KB cannot be one request");
    }

    #[test]
    fn split_batches_handles_a_single_event() {
        let prepared = prepared_of(8, 1);
        let groups = split_batches(&prepared, 50);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].len(), 1);
    }

    #[test]
    fn prepare_one_rejects_an_unbatchable_event() {
        assert!(
            prepare_one(serde_json::json!({"id": "big", "data": "x".repeat(300_000)})).is_none(),
            "an event that cannot fit in any batch must be dropped, not retried forever"
        );
        assert!(prepare_one(serde_json::json!({"id": "small"})).is_some());
    }
}