tastytrade 0.5.0

Library for trading through tastytrade's API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
// For quote_streamer.rs
use crate::TastyTrade;
use crate::api::quote_streaming::AsStreamerSymbol;
use crate::streaming::reconnect::{BackoffPolicy, ConnectionState};
use crate::types::dxfeed;
use crate::types::dxfeed::{CandlePeriod, EventKind};
use crate::{TastyResult, TastyTradeError};
use chrono::{DateTime, Utc};
use dxlink::{DXLinkClient, EventType, FeedSubscription, MarketEvent, OverflowPolicy};
use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::Serialize;
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::{Notify, RwLock, mpsc, oneshot};
use tracing::{debug, error, info, warn};

#[derive(DebugPretty, DisplaySimple, Serialize, PartialEq, Eq, Hash, Clone, Copy)]
/// Identifies one subscription within a streamer.
pub struct SubscriptionId(usize);

/// How often the feed client's own loss counter is sampled while a connection
/// is idle.
///
/// The counter only matters at the boundaries of a historical replay, so a
/// coarse sample is enough: it bounds how stale the figure behind a snapshot
/// marker's `lossless` can be, and costs one atomic store per interval.
const DROP_MIRROR_INTERVAL: Duration = Duration::from_millis(100);

/// How many markers may wait for a consumer that is not reading.
///
/// Markers are never dropped for a full queue, which is what makes an ending
/// worth trusting — but "never" has to stop somewhere. A consumer that stops
/// reading while the connection reconnects repeatedly would otherwise grow
/// this without bound, one begin and one end per generation per series, while
/// its ordinary events are being dropped on the floor beside it.
///
/// When the backlog is full the **oldest** markers go, which is the harmless
/// direction: generations only move forward, so what a consumer needs on
/// waking is the phase its series are in now, not the ones they passed
/// through on the way. The newest phase change always survives.
const MAX_PENDING_MARKERS: usize = 64;

/// A cheap, clonable handle to the streamer's command loop and its channel.
///
/// This is everything a subscription needs from the streamer: a way to send
/// commands. It used to hold a whole cloned `QuoteStreamer` instead, and that
/// clone's `Drop` sent `Disconnect` on the shared command channel, so dropping
/// a subscription tore down the connection the real streamer was still using.
/// A handle owns no connection, so dropping one cannot end anybody's stream.
#[derive(Clone)]
struct StreamerHandle {
    commands: Option<mpsc::Sender<DXLinkCommand>>,
}

/// A set of symbols and event types, and the events they produce.
///
/// Holds a handle to the streamer rather than the streamer itself, so dropping
/// a subscription cannot end the connection.
pub struct QuoteSubscription {
    /// This subscription's identity within its streamer.
    pub id: SubscriptionId,
    streamer: StreamerHandle,
    /// The event types this subscription asked for.
    ///
    /// Was an `i32` bitmask of three `DXF_ET_*` constants. A typed set, so the
    /// eleven the feed models are all reachable and a caller cannot ask for a
    /// bit that means nothing.
    kinds: BTreeSet<EventKind>,
    event_receiver: flume::Receiver<dxfeed::Event>, // Keep for compatibility
    dxlink_receiver: mpsc::Receiver<Delivery>,      // New DXLink event receiver
    /// What this subscription is actually subscribed to.
    ///
    /// Shared with the copy the streamer keeps in its `subscription_map`, and
    /// that sharing is the fix rather than an optimisation: `create_sub`
    /// stores one clone and hands the caller another, so a `Vec` on each
    /// meant `add_symbols` updated the caller's copy while `close_sub` read
    /// the streamer's, which stayed empty forever. Unsubscribes were
    /// therefore derived from an empty list and never sent, leaving the
    /// subscription alive on the venue.
    ///
    /// A set, so asking twice subscribes once.
    targets: Arc<Mutex<BTreeSet<FeedTarget>>>,
    /// How many events this subscription has lost by not keeping up.
    ///
    /// Shared with the sink the forwarder delivers into, so it counts what
    /// actually happened to *this* consumer.
    lagged: Arc<AtomicU64>,
    /// Where each of this subscription's candle series stands.
    ///
    /// The same map the forwarder writes, so a question about history is
    /// answered from what was actually delivered rather than from a copy that
    /// can be stale.
    progress: CandleProgress,
    /// Told when this consumer reads, so the forwarder can send what it parked.
    drained: Arc<Notify>,
    /// Told when any series changes phase, so a waiter re-checks.
    history: Arc<Notify>,
    /// Set by `close_sub`, under the targets lock, before it drains the set.
    ///
    /// Read by `subscribe_targets` under the same lock, so once a close has
    /// begun no new target can be reserved: a close is total, and a series
    /// added after its snapshot would otherwise be subscribed on the wire
    /// with the sender that could deliver it about to go.
    closed: Arc<AtomicBool>,
}

impl QuoteSubscription {
    /// Subscribes this subscription to `symbols`.
    ///
    /// `symbols` are **streaming** names, not instrument ones. See
    /// [`AsStreamerSymbol`] for why the distinction is a type rather than a
    /// convention, and [`TastyTrade::get_streamer_symbol`](crate::TastyTrade::get_streamer_symbol)
    /// for how to turn one into the other.
    ///
    /// Returns once the venue has accepted the subscription, not merely once
    /// the command was queued. Symbols already subscribed are skipped, so
    /// calling twice with the same symbol subscribes once.
    ///
    /// Subscribes each symbol under every event type this subscription asked
    /// for **except candles**: a candle needs a period and a history start,
    /// and a bare symbol has neither. Use [`QuoteSubscription::add_candles`].
    ///
    /// # Errors
    ///
    /// [`TastyTradeError::Precondition`] when the subscription asked for
    /// nothing but candles, because there is then nothing this call can
    /// subscribe. Otherwise fails when the streamer has no open channel, when
    /// it is closed, or when the venue refuses. On any of those the symbols
    /// are not recorded, so a later close does not try to unsubscribe
    /// something that was never subscribed.
    pub async fn add_symbols<S: AsStreamerSymbol>(&self, symbols: &[S]) -> TastyResult<()> {
        let kinds: Vec<EventKind> = self
            .kinds
            .iter()
            .copied()
            .filter(|kind| !kind.needs_a_period())
            .collect();

        if kinds.is_empty() {
            return Err(TastyTradeError::Precondition(
                "this subscription asked for candles only, and a candle needs a period and a \
                 start time; use add_candles"
                    .to_string(),
            ));
        }

        let requested: Vec<FeedTarget> = symbols
            .iter()
            .flat_map(|symbol| {
                let symbol = symbol.as_streamer_symbol();
                kinds.iter().map(move |kind| FeedTarget {
                    kind: *kind,
                    symbol: symbol.0.clone(),
                    from_time: None,
                })
            })
            .collect();

        self.subscribe_targets(requested).await
    }

    /// Subscribes this subscription to candles for `symbols`.
    ///
    /// `symbols` are **base streaming** names, without a period suffix: this
    /// appends it. They are not instrument symbols — see
    /// [`AsStreamerSymbol`], which is what stops `/ES` being accepted where
    /// the feed wants `ES`.
    ///
    /// A candle subscription is addressed by a symbol that carries its own
    /// period — `AAPL{=5m}` — so two periods of one underlying are two
    /// different streamer symbols and never deliver into each other. The
    /// events come back under that same symbol, which is what
    /// [`dxfeed::Event::sym`] holds, and
    /// [`CandlePeriod::base_symbol`] turns that back into what this method
    /// and [`remove_candles`](Self::remove_candles) take.
    ///
    /// `from_time` is required, not optional. A candle subscription without
    /// one replays an unbounded history: the documented sizing is about 1440
    /// events for a day of one-minute bars, and there is no upper bound at
    /// all on "everything you have".
    ///
    /// # Errors
    ///
    /// [`TastyTradeError::Precondition`] when the subscription did not ask for
    /// [`EventKind::Candle`] — a channel is only configured for the types its
    /// subscriptions requested, so this would subscribe to something that
    /// cannot arrive. Otherwise as [`QuoteSubscription::add_symbols`].
    pub async fn add_candles<S: AsStreamerSymbol>(
        &self,
        symbols: &[S],
        period: CandlePeriod,
        from_time: DateTime<Utc>,
    ) -> TastyResult<()> {
        if !self.kinds.contains(&EventKind::Candle) {
            return Err(TastyTradeError::Precondition(
                "this subscription did not ask for candles, so the channel is not configured to \
                 deliver them; create one with EventKind::Candle"
                    .to_string(),
            ));
        }

        // Milliseconds. dxlink documents `FeedSubscription::from_time` as a
        // Unix timestamp in milliseconds and it is the code that serialises
        // it, so that is what this follows. Taking a `DateTime` rather than an
        // integer keeps the choice in one place instead of at every call site.
        let from_time = from_time.timestamp_millis();

        let requested: Vec<FeedTarget> = symbols
            .iter()
            .map(|symbol| FeedTarget {
                kind: EventKind::Candle,
                symbol: period.streamer_symbol(&symbol.as_streamer_symbol().0),
                from_time: Some(from_time),
            })
            .collect();

        self.subscribe_targets(requested).await
    }

    /// How many events this subscription has lost by not keeping up.
    ///
    /// Zero means the stream is complete as far as this subscription is
    /// concerned — which is the question a caller assembling a price series
    /// actually needs answered, and had no way to ask. A non-zero count is not
    /// recoverable by reading faster: those events are gone.
    ///
    /// Candles are the exception, and only across a reconnect: a bar that was
    /// dropped stops the resume point advancing, so the next connection asks
    /// for it again. Within one connection a dropped bar stays dropped.
    pub fn lagged(&self) -> u64 {
        self.lagged.load(Ordering::Relaxed)
    }

    /// Every streamer symbol this subscription is subscribed to, with its
    /// event type.
    ///
    /// Candle symbols carry their period, which is how a caller tells
    /// `AAPL{=5m}` from `AAPL{=h}`.
    pub fn subscribed(&self) -> Vec<(String, EventKind)> {
        targets_of(&self.targets)
            .iter()
            .map(|target| (target.symbol.clone(), target.kind))
            .collect()
    }

    /// Reserves `requested`, asks the venue, and gives the reservation back if
    /// it is refused.
    async fn subscribe_targets(&self, requested: Vec<FeedTarget>) -> TastyResult<()> {
        // Checked and reserved in one lock section. Filtering against the set
        // and inserting afterwards let two concurrent callers both see a
        // target as absent and both subscribe to it. Reserving here means the
        // second caller sees the first one's claim; a failure below removes
        // the reservation again.
        let targets: Vec<FeedTarget> = {
            let mut known = targets_of(&self.targets);
            if self.closed.load(Ordering::Acquire) {
                return Err(TastyTradeError::Streaming(
                    "the subscription is closed; create a new one to subscribe".to_string(),
                ));
            }
            requested
                .into_iter()
                .filter(|target| known.insert(target.clone()))
                .collect()
        };

        let subscriptions = feed_subscriptions(&targets);

        if subscriptions.is_empty() {
            return Ok(());
        }

        // Awaited rather than spawned. A detached task meant this returned
        // success before the command was even accepted, so a caller could not
        // tell a subscription that worked from one that never left, and it
        // panicked outright when called without a Tokio runtime.
        let sub_id = self.id.0 as u32;
        let Some(tx) = &self.streamer.commands else {
            let mut known = targets_of(&self.targets);
            for target in &targets {
                known.remove(target);
            }
            return Err(TastyTradeError::Streaming(
                "the quote streamer has no command channel; reconnect before subscribing"
                    .to_string(),
            ));
        };

        let (ack, answered) = oneshot::channel();
        let queued = tx
            .send(DXLinkCommand::Subscribe(
                subscriptions,
                targets.iter().map(|target| target.kind).collect(),
                sub_id,
                self.targets.clone(),
                Some(ack),
            ))
            .await
            .map_err(|_| {
                TastyTradeError::Streaming(
                    "the quote streamer is closed; reconnect before subscribing".to_string(),
                )
            });

        // Reaching the command queue is not the venue accepting the
        // subscription: the loop can still be refused by DXLink. Wait for the
        // real answer, and give back the reservation if it is a refusal, so a
        // target that is not subscribed is never later unsubscribed as though
        // it were.
        let outcome = match queued {
            Ok(()) => answered.await.unwrap_or_else(|_| {
                Err(TastyTradeError::Streaming(
                    "the quote streamer closed before the subscription was confirmed".to_string(),
                ))
            }),
            Err(e) => Err(e),
        };

        if outcome.is_err() {
            let mut known = targets_of(&self.targets);
            for target in &targets {
                known.remove(target);
            }
        }

        outcome
    }

    /// Receive one event from feed. Yields if there are no events.
    /// Compatible with previous interface
    pub async fn get_event(&mut self) -> Result<dxfeed::Event, flume::RecvError> {
        // A loop rather than one match: dxlink decodes event types this crate
        // has no `EventData` for, and one of those arriving must not look to
        // the caller like the stream ended.
        loop {
            let Some(delivery) = self.dxlink_receiver.recv().await else {
                // Every sender is gone, which is what happens when the
                // streamer is dropped. The flume receiver behind this is
                // already disconnected, so this reports the end rather than
                // waiting for something that cannot arrive.
                return self.event_receiver.recv_async().await;
            };

            // Room has been made. A marker that did not fit is waiting for
            // exactly this, and nothing else would wake the forwarder to send
            // it: after a snapshot ends, the next event on that series may be
            // a long way off.
            self.drained.notify_one();

            match delivery {
                // Synthesised by this crate, so there is nothing to convert.
                Delivery::Marker(event) => return Ok(event),
                Delivery::Market(market_event) => {
                    if let Some(event) = convert_event(market_event) {
                        return Ok(event);
                    }
                }
            }
        }
    }

    /// Whether this symbol's historical replay has finished.
    ///
    /// `symbol` is the full streamer symbol, period suffix included — the
    /// string a bar or a marker arrives under and
    /// [`subscribed`](Self::subscribed) reports, not the base name
    /// [`add_candles`](Self::add_candles) takes, because each period replays
    /// as its own snapshot. [`CandlePeriod::base_symbol`] converts the other
    /// way, for [`remove_candles`](Self::remove_candles).
    ///
    /// `false` for a series that has no replay in progress yet, one still
    /// replaying, and one whose connection dropped: a reconnect starts a new
    /// replay, so history stops being loaded the moment the socket does rather
    /// than when the venue gets around to saying so.
    ///
    /// This answers the same question as waiting for
    /// [`EventData::SnapshotEnd`](crate::dxfeed::EventData::SnapshotEnd) on
    /// [`get_event`](Self::get_event), for a caller that would rather ask than
    /// watch. It says nothing about whether the history is **complete**; that
    /// is [`DxfSnapshotEndT::lossless`](crate::dxfeed::DxfSnapshotEndT::lossless).
    pub fn history_loaded(&self, symbol: &str) -> bool {
        self.finished_history(symbol).is_some()
    }

    /// The finished replay for `symbol`, if there is one.
    fn finished_history(&self, symbol: &str) -> Option<dxfeed::DxfSnapshotEndT> {
        let seen = self
            .progress
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let resume = seen.get(&(self.id.0 as u32, symbol.to_string()))?;

        if resume.phase != SnapshotPhase::Ended {
            return None;
        }

        Some(dxfeed::DxfSnapshotEndT {
            generation: resume.generation,
            kind: resume.ended_as?,
            lossless: resume.lossless,
        })
    }

    /// Waits until this symbol's historical replay has finished.
    ///
    /// `symbol` is the full streamer symbol, period suffix included — the
    /// string a bar or a marker arrives under and
    /// [`subscribed`](Self::subscribed) reports, not the base name
    /// [`add_candles`](Self::add_candles) takes. A series is a symbol *and* a
    /// period, so the bare name identifies nothing here.
    ///
    /// Resolves with the same payload the
    /// [`EventData::SnapshotEnd`](crate::dxfeed::EventData::SnapshotEnd) event
    /// carries, and resolves immediately if the replay already finished. A
    /// reconnect does **not** resolve it: the generation it was waiting on is
    /// superseded, so it keeps waiting for the new one to finish, which is
    /// what a caller asking "is the history in yet" means.
    ///
    /// There is no timeout here, because the right one belongs to the caller:
    /// a venue that never terminates a snapshot leaves this pending forever.
    /// Wrap it in [`tokio::time::timeout`].
    ///
    /// # Errors
    ///
    /// [`TastyTradeError::Precondition`] when this subscription holds no
    /// candle target for `symbol`, so the wait could never end — a mistyped
    /// symbol is a bug worth reporting rather than a hang.
    pub async fn await_history(&self, symbol: &str) -> TastyResult<dxfeed::DxfSnapshotEndT> {
        loop {
            // Armed before the checks, never after. A phase change landing
            // between them would otherwise be missed and the caller would wait
            // for a notification that had already been sent.
            let notified = self.history.notified();
            tokio::pin!(notified);
            notified.as_mut().enable();

            // Checked every time round rather than once on the way in.
            // `remove_candles` can take the series away while this waits, and
            // it wakes this waiter when it does; without the re-check the
            // waiter would find no finished replay, park again, and wait for a
            // series nobody is subscribed to any more.
            if !self.holds_candle_series(symbol) {
                return Err(TastyTradeError::Precondition(format!(
                    "this subscription has no candle series for {symbol}, so its history can \
                     never finish; subscribe with add_candles first, and pass the streamer \
                     symbol with its period suffix"
                )));
            }

            if let Some(end) = self.finished_history(symbol) {
                return Ok(end);
            }

            notified.await;
        }
    }

    /// Whether this subscription holds a candle series for `symbol`.
    fn holds_candle_series(&self, symbol: &str) -> bool {
        targets_of(&self.targets)
            .iter()
            .any(|target| target.kind == EventKind::Candle && target.symbol == symbol)
    }

    /// Unsubscribes `symbols` at `period` from this subscription.
    ///
    /// `symbols` are **base** streaming names, without the period suffix:
    /// this appends it, exactly as `add_candles` did when it subscribed. A
    /// marker or a bar names its series *with* the suffix, so
    /// [`CandlePeriod::base_symbol`] is the way back from one to the other.
    ///
    /// A partial teardown, not a close: every other series on the subscription
    /// keeps running, including the same symbol at another period. The venue
    /// stops sending the removed ones, they leave
    /// [`subscribed`](Self::subscribed), and a reconnect does not bring them
    /// back — the replay is built from the same set this removes them from.
    ///
    /// Written for the shape a candle consumer actually has: load history at
    /// several periods, and as each one finishes, stop paying for a live feed
    /// nobody is reading. Another subscription watching the same series is
    /// unaffected; routes are held per subscription.
    ///
    /// A series this subscription does not hold is not an error and is not
    /// sent to the venue, so removing twice is safe. Removing a series while
    /// a concurrent [`add_candles`](Self::add_candles) re-subscribes it with
    /// another history start is safe too, whichever the venue hears first: the
    /// removal only lets go of what this subscription held when it was called,
    /// and never of a target added since.
    ///
    /// # Errors
    ///
    /// [`TastyTradeError::Streaming`] when the streamer has no command
    /// channel, when it is closed, or when the venue refuses the unsubscribe.
    /// On a refusal the targets stay recorded, because that record is what a
    /// retry needs.
    pub async fn remove_candles<S: AsStreamerSymbol>(
        &self,
        symbols: &[S],
        period: CandlePeriod,
    ) -> TastyResult<()> {
        let wanted: Vec<String> = symbols
            .iter()
            .map(|symbol| period.streamer_symbol(&symbol.as_streamer_symbol().0))
            .collect();

        self.unsubscribe_targets(&wanted).await
    }

    /// Unsubscribes this subscription's candle targets for `symbols`.
    async fn unsubscribe_targets(&self, symbols: &[String]) -> TastyResult<()> {
        // Matched on kind and symbol, never on the whole target: a stored
        // candle target carries the history start it was subscribed with, and
        // a caller removing a series has no reason to know that number.
        let targets: Vec<FeedTarget> = {
            let known = targets_of(&self.targets);
            known
                .iter()
                .filter(|target| {
                    target.kind == EventKind::Candle && symbols.contains(&target.symbol)
                })
                .cloned()
                .collect()
        };

        if targets.is_empty() {
            // Nothing this subscription holds, so nothing to ask the venue.
            return Ok(());
        }

        let sub_id = self.id.0 as u32;
        let Some(tx) = &self.streamer.commands else {
            return Err(TastyTradeError::Streaming(
                "the quote streamer has no command channel; reconnect before unsubscribing"
                    .to_string(),
            ));
        };

        // Taken out of the shared set before the venue is asked, and put back
        // if it refuses. Recording it afterwards instead left a window in
        // which the command loop had already dropped the route while the set
        // still claimed the target: a concurrent `add_candles` would see it as
        // present and skip resubscribing, leaving a series with nowhere to
        // deliver, and a reconnect could replay something already being taken
        // away. `subscribe_targets` reserves in the same direction for the
        // same reason.
        {
            let mut known = targets_of(&self.targets);
            for target in &targets {
                known.remove(target);
            }
        }
        let pending = RemovedTargets {
            targets: &self.targets,
            removed: targets,
            closing: None,
        };

        let (ack, answered) = oneshot::channel();
        let queued = tx
            .send(DXLinkCommand::Unsubscribe(
                feed_subscriptions(&pending.removed),
                sub_id,
                Some(self.targets.clone()),
                Some(ack),
            ))
            .await
            .map_err(|_| {
                TastyTradeError::Streaming(
                    "the quote streamer is closed; reconnect before unsubscribing".to_string(),
                )
            });

        let outcome = match queued {
            Ok(()) => answered.await.unwrap_or_else(|_| {
                Err(TastyTradeError::Streaming(
                    "the quote streamer closed before the unsubscribe was confirmed".to_string(),
                ))
            }),
            Err(e) => Err(e),
        };

        // Dropping the guard on this path is what puts them back.
        outcome?;

        // Confirmed, so the restore is disarmed.
        let targets = pending.commit();

        // The history goes back to knowing nothing about these series, but the
        // generation counter does not restart. A consumer that saw generation
        // 2 before the removal must not be handed a second generation 2 for a
        // different replay if the series is subscribed again, and leaving the
        // phase `Ended` would report a history as loaded before any of it had
        // arrived.
        //
        // Only for a series this subscription holds nothing for any more,
        // decided under the targets lock, which is the lock a concurrent
        // `add_candles` reserves under. The venue's acknowledgement can sit
        // unread while a newer target for the same series is subscribed and
        // its replay finishes; resetting then would erase a history the
        // consumer has already been told is in, and `await_history` would
        // wait for an ending that already happened.
        {
            let known = targets_of(&self.targets);
            let mut seen = self
                .progress
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            for target in &targets {
                let still_held = known
                    .iter()
                    .any(|held| held.kind == target.kind && held.symbol == target.symbol);
                if still_held {
                    continue;
                }
                if let Some(resume) = seen.get_mut(&(sub_id, target.symbol.clone())) {
                    *resume = CandleResume {
                        generation: resume.generation,
                        dxlink_drops_at_start: resume.dxlink_drops_at_start,
                        ..CandleResume::new(resume.dxlink_drops_at_start)
                    };
                }
            }
        }
        self.history.notify_waiters();

        Ok(())
    }
}

/// Converts one dxlink event into this crate's own event type.
///
/// `None` only for an event whose symbol this crate cannot read, which cannot
/// happen for a variant that is modelled — every one of the eleven carries an
/// `eventSymbol`. It exists so the caller of this function does not have to
/// care, and so a future variant does not silently become a `Quote`.
///
/// `f64` throughout, and only here: `types::dxfeed` holds the native feed
/// types, where the representation is the feed's to choose. Nothing on the
/// REST path is allowed to widen that.
fn convert_event(event: MarketEvent) -> Option<dxfeed::Event> {
    let data = match event {
        MarketEvent::Quote(quote) => {
            return Some(dxfeed::Event {
                sym: quote.event_symbol,
                data: dxfeed::EventData::Quote(dxfeed::DxfQuoteT {
                    time: 0,
                    sequence: 0,
                    time_nanos: 0,
                    bid_time: 0,
                    bid_exchange_code: 0,
                    bid_price: quote.bid_price,
                    ask_price: quote.ask_price,
                    bid_size: quote.bid_size as i64,
                    ask_time: 0,
                    ask_size: quote.ask_size as i64,
                    ask_exchange_code: 0,
                    scope: 0,
                }),
            });
        }
        MarketEvent::Trade(trade) => {
            return Some(dxfeed::Event {
                sym: trade.event_symbol,
                data: dxfeed::EventData::Trade(dxfeed::DxfTradeT {
                    time: 0,
                    sequence: 0,
                    time_nanos: 0,
                    exchange_code: 0,
                    price: trade.price,
                    size: trade.size as i64,
                    tick: 0,
                    change: 0.0,
                    day_id: 0,
                    day_volume: trade.day_volume,
                    day_turnover: 0.0,
                    raw_flags: 0,
                    direction: 0,
                    is_eth: 0,
                    scope: 0,
                }),
            });
        }
        MarketEvent::Greeks(greeks) => {
            return Some(dxfeed::Event {
                sym: greeks.event_symbol,
                // DXLink's GreeksEvent carries no price or time, so those stay 0.
                data: dxfeed::EventData::Greeks(dxfeed::DxfGreeksT {
                    event_flags: 0,
                    index: 0,
                    time: 0,
                    price: 0.0,
                    volatility: greeks.volatility,
                    delta: greeks.delta,
                    gamma: greeks.gamma,
                    theta: greeks.theta,
                    vega: greeks.vega,
                    rho: greeks.rho,
                }),
            });
        }
        MarketEvent::TradeETH(trade) => (
            trade.event_symbol.clone(),
            dxfeed::EventData::TradeEth(Box::new(dxfeed::DxfTradeEthT {
                event_time: trade.event_time,
                time: trade.time,
                time_nano_part: trade.time_nano_part,
                sequence: trade.sequence,
                exchange_code: trade.exchange_code,
                price: trade.price,
                change: trade.change,
                size: trade.size,
                day_id: trade.day_id,
                day_volume: trade.day_volume,
                day_turnover: trade.day_turnover,
                tick_direction: trade.tick_direction,
                extended_trading_hours: trade.extended_trading_hours,
            })),
        ),
        MarketEvent::Candle(candle) => (
            candle.event_symbol.clone(),
            dxfeed::EventData::Candle(Box::new(dxfeed::DxfCandleT {
                event_time: candle.event_time,
                event_flags: candle.event_flags,
                index: candle.index,
                time: candle.time,
                sequence: candle.sequence,
                count: candle.count,
                open: candle.open,
                high: candle.high,
                low: candle.low,
                close: candle.close,
                volume: candle.volume,
                vwap: candle.vwap,
                bid_volume: candle.bid_volume,
                ask_volume: candle.ask_volume,
                imp_volatility: candle.imp_volatility,
                open_interest: candle.open_interest,
            })),
        ),
        MarketEvent::Summary(summary) => (
            summary.event_symbol.clone(),
            dxfeed::EventData::Summary(Box::new(dxfeed::DxfSummaryT {
                event_time: summary.event_time,
                day_id: summary.day_id,
                day_open_price: summary.day_open_price,
                day_high_price: summary.day_high_price,
                day_low_price: summary.day_low_price,
                day_close_price: summary.day_close_price,
                day_close_price_type: summary.day_close_price_type,
                prev_day_id: summary.prev_day_id,
                prev_day_close_price: summary.prev_day_close_price,
                prev_day_close_price_type: summary.prev_day_close_price_type,
                prev_day_volume: summary.prev_day_volume,
                open_interest: summary.open_interest,
            })),
        ),
        MarketEvent::TimeAndSale(sale) => (
            sale.event_symbol.clone(),
            dxfeed::EventData::TimeAndSale(Box::new(dxfeed::DxfTimeAndSaleT {
                event_time: sale.event_time,
                event_flags: sale.event_flags,
                index: sale.index,
                time: sale.time,
                time_nano_part: sale.time_nano_part,
                sequence: sale.sequence,
                exchange_code: sale.exchange_code,
                price: sale.price,
                size: sale.size,
                bid_price: sale.bid_price,
                ask_price: sale.ask_price,
                exchange_sale_conditions: sale.exchange_sale_conditions,
                trade_through_exempt: sale.trade_through_exempt,
                aggressor_side: sale.aggressor_side,
                spread_leg: sale.spread_leg,
                extended_trading_hours: sale.extended_trading_hours,
                valid_tick: sale.valid_tick,
                sale_type: sale.sale_type,
                buyer: sale.buyer,
                seller: sale.seller,
            })),
        ),
        MarketEvent::Profile(profile) => (
            profile.event_symbol.clone(),
            dxfeed::EventData::Profile(Box::new(dxfeed::DxfProfileT {
                event_time: profile.event_time,
                description: profile.description,
                short_sale_restriction: profile.short_sale_restriction,
                trading_status: profile.trading_status,
                status_reason: profile.status_reason,
                halt_start_time: profile.halt_start_time,
                halt_end_time: profile.halt_end_time,
                high_limit_price: profile.high_limit_price,
                low_limit_price: profile.low_limit_price,
                high_52_week_price: profile.high_52_week_price,
                low_52_week_price: profile.low_52_week_price,
                beta: profile.beta,
                earnings_per_share: profile.earnings_per_share,
                dividend_frequency: profile.dividend_frequency,
                ex_dividend_amount: profile.ex_dividend_amount,
                ex_dividend_day_id: profile.ex_dividend_day_id,
                shares: profile.shares,
                free_float: profile.free_float,
            })),
        ),
        MarketEvent::Underlying(underlying) => (
            underlying.event_symbol.clone(),
            dxfeed::EventData::Underlying(Box::new(dxfeed::DxfUnderlyingT {
                event_time: underlying.event_time,
                event_flags: underlying.event_flags,
                index: underlying.index,
                time: underlying.time,
                sequence: underlying.sequence,
                volatility: underlying.volatility,
                front_volatility: underlying.front_volatility,
                back_volatility: underlying.back_volatility,
                call_volume: underlying.call_volume,
                put_volume: underlying.put_volume,
                put_call_ratio: underlying.put_call_ratio,
            })),
        ),
        MarketEvent::TheoPrice(theo) => (
            theo.event_symbol.clone(),
            dxfeed::EventData::TheoPrice(Box::new(dxfeed::DxfTheoPriceT {
                event_time: theo.event_time,
                event_flags: theo.event_flags,
                index: theo.index,
                time: theo.time,
                sequence: theo.sequence,
                price: theo.price,
                underlying_price: theo.underlying_price,
                delta: theo.delta,
                gamma: theo.gamma,
                dividend: theo.dividend,
                interest: theo.interest,
            })),
        ),
        MarketEvent::Series(series) => (
            series.event_symbol.clone(),
            dxfeed::EventData::Series(Box::new(dxfeed::DxfSeriesT {
                event_time: series.event_time,
                event_flags: series.event_flags,
                index: series.index,
                time: series.time,
                sequence: series.sequence,
                expiration: series.expiration,
                volatility: series.volatility,
                call_volume: series.call_volume,
                put_volume: series.put_volume,
                put_call_ratio: series.put_call_ratio,
                forward_price: series.forward_price,
                dividend: series.dividend,
                interest: series.interest,
            })),
        ),
    };

    Some(dxfeed::Event {
        sym: data.0,
        data: data.1,
    })
}

// Commands for DXLink client to execute.
// Subscribe/Unsubscribe carry the subscription id so events can be routed
// back to the subscription that requested each symbol.
/// Replies to whoever is waiting on a command, if anyone still is.
///
/// A caller that stopped waiting is not an error: dropping the receiver is how
/// a fire-and-forget caller opts out.
fn answer(ack: Option<oneshot::Sender<TastyResult<()>>>, outcome: TastyResult<()>) {
    if let Some(ack) = ack {
        let _ = ack.send(outcome);
    }
}

/// How many events a subscription may fall behind by before losing some.
///
/// A candle history is the reason this is not the old hundred: a day of
/// one-minute bars is about 1440 events and they arrive at once, so a hundred
/// meant a caller reading a series lost most of it before it ever looked.
/// Large enough to absorb a documented history, still bounded — an unbounded
/// channel turns a slow consumer into unbounded memory.
///
/// A default rather than a rule: a caller who knows their own history size
/// chooses with [`QuoteStreamer::create_sub_with_capacity`].
pub const DEFAULT_EVENT_CHANNEL_CAPACITY: usize = 4096;

/// One consumer of a subscription's events, and what it has lost.
///
/// The counter travels with the sender so the forwarder can charge a drop to
/// the consumer it actually happened to, rather than to a symbol or a
/// subscription that has several.
#[derive(Clone)]
struct Subscriber {
    events: mpsc::Sender<Delivery>,
    lagged: Arc<AtomicU64>,
    /// Markers that did not fit, waiting for the consumer to make room.
    ///
    /// Only markers are ever parked here. A dropped bar is a countable loss a
    /// consumer can react to; a dropped snapshot terminator is a consumer
    /// waiting forever for a phase change that already happened, which is the
    /// failure this whole mechanism exists to prevent.
    pending: Arc<Mutex<VecDeque<Delivery>>>,
}

/// One thing handed to a consumer's queue.
///
/// Markers travel the same queue as market data on purpose. A queue is the
/// only thing that keeps "after those bars, before anything newer" true; a
/// separate channel for markers would race with the bars it is meant to
/// follow, and the position in the stream is the marker's entire meaning.
#[derive(Debug, Clone)]
enum Delivery {
    /// An event the feed sent.
    Market(MarketEvent),
    /// A snapshot marker this crate synthesised.
    Marker(dxfeed::Event),
}

/// dxFeed `IndexedEvent` flag bits, as the feed sets them in `eventFlags`.
///
/// The crate owns these so no consumer has to. They are the reason a
/// historical replay is tellable from live updates at all, and reading them
/// wrongly is silent: a missed terminator is a consumer that never starts.
mod snapshot_flags {
    /// The event belongs to a transaction that is not complete. Nothing may be
    /// concluded from it until an event arrives with this clear.
    pub const TX_PENDING: i64 = 0x01;
    /// The event removes what it identifies instead of adding it.
    pub const REMOVE_EVENT: i64 = 0x02;
    /// The first event of a historical snapshot.
    pub const SNAPSHOT_BEGIN: i64 = 0x04;
    /// The last event of a snapshot the venue served in full.
    pub const SNAPSHOT_END: i64 = 0x08;
    /// The last event of a snapshot the venue cut short.
    pub const SNAPSHOT_SNIP: i64 = 0x10;
}

/// What one event's flags say about its series' historical replay.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SnapshotSignals {
    /// A replay starts at this event.
    begin: bool,
    /// The terminator this event carries, if any.
    end: Option<dxfeed::SnapshotEndKind>,
    /// Whether the event sits inside an incomplete transaction.
    tx_pending: bool,
    /// Whether the event carries no bar.
    ///
    /// `REMOVE_EVENT` on its own is an ordinary removal and carries a real
    /// event. `REMOVE_EVENT` **together with** a terminator is how the venue
    /// ends a snapshot when there is nothing left to send: the row exists to
    /// carry the flags, and its index and time are placeholders. Delivering it
    /// as a bar would put a fabricated price in a series, and letting it reach
    /// the resume bookkeeping would move a resume point to a time no bar ever
    /// had.
    data_less: bool,
}

impl SnapshotSignals {
    fn of(flags: i64) -> Self {
        let end = if flags & snapshot_flags::SNAPSHOT_SNIP != 0 {
            Some(dxfeed::SnapshotEndKind::Snip)
        } else if flags & snapshot_flags::SNAPSHOT_END != 0 {
            Some(dxfeed::SnapshotEndKind::End)
        } else {
            None
        };

        Self {
            begin: flags & snapshot_flags::SNAPSHOT_BEGIN != 0,
            end,
            tx_pending: flags & snapshot_flags::TX_PENDING != 0,
            data_less: end.is_some() && flags & snapshot_flags::REMOVE_EVENT != 0,
        }
    }
}

/// A snapshot marker for `symbol`.
fn marker_event(symbol: &str, data: dxfeed::EventData) -> dxfeed::Event {
    dxfeed::Event {
        sym: symbol.to_string(),
        data,
    }
}

/// Hands `delivery` to one consumer, behind whatever is already parked for it.
///
/// Returns whether the delivery reached the consumer or is guaranteed to.
/// Data events are dropped when there is no room, exactly as before — and also
/// while a marker is parked, because letting one through then would put it
/// ahead of the marker.
fn offer(subscriber: &Subscriber, delivery: Delivery) -> bool {
    let mut pending = subscriber
        .pending
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());

    let room = flush_into(subscriber, &mut pending);
    let marker = matches!(delivery, Delivery::Marker(_));

    if !room {
        if marker {
            park(&mut pending, delivery);
            return true;
        }
        return false;
    }

    match subscriber.events.try_send(delivery) {
        Ok(()) => true,
        Err(mpsc::error::TrySendError::Full(delivery)) => {
            if marker {
                park(&mut pending, delivery);
                true
            } else {
                false
            }
        }
        // The consumer is gone. Parking anything for it would be a slow leak
        // of events nobody will ever read.
        Err(mpsc::error::TrySendError::Closed(_)) => false,
    }
}

/// Parks a marker, discarding the oldest if the backlog is full.
///
/// See [`MAX_PENDING_MARKERS`] for why the oldest is the one to lose.
fn park(pending: &mut VecDeque<Delivery>, delivery: Delivery) {
    while pending.len() >= MAX_PENDING_MARKERS {
        pending.pop_front();
    }
    pending.push_back(delivery);
}

/// Sends what is parked, oldest first. Returns whether nothing is left.
fn flush_into(subscriber: &Subscriber, pending: &mut VecDeque<Delivery>) -> bool {
    while let Some(front) = pending.pop_front() {
        match subscriber.events.try_send(front) {
            Ok(()) => {}
            Err(mpsc::error::TrySendError::Full(returned)) => {
                pending.push_front(returned);
                return false;
            }
            Err(mpsc::error::TrySendError::Closed(_)) => {
                pending.clear();
                return true;
            }
        }
    }
    true
}

/// Flushes every consumer's parked markers.
///
/// Called when a consumer reports having read something, and once when a
/// connection's forwarder starts — a marker parked across a reconnect is
/// waiting for room, not for an event, and the next event may be a long way
/// off or never come.
async fn flush_pending(routing: &Arc<RwLock<EventRouting>>) {
    let routing = routing.read().await;
    for subscribers in routing.senders.values() {
        for subscriber in subscribers {
            let mut pending = subscriber
                .pending
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            if pending.is_empty() {
                continue;
            }
            flush_into(subscriber, &mut pending);
        }
    }
}

/// Where a candle subscription should resume, per subscription and symbol.
///
/// `through` is the last bar **delivered contiguously** — not the highest one
/// seen. The difference is the whole point: a maximum steps over a gap, and a
/// gap in a price series is invisible to everything downstream.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct CandleResume {
    /// The last bar handed to the consumer with nothing missing before it.
    ///
    /// `Option`, and that is not cosmetic: a series whose **first** bar was
    /// dropped has no delivered bar to resume from, and seeding this with the
    /// dropped bar's own time would move the resume point past a bar the
    /// consumer never received — the exact failure the contiguity rule exists
    /// to prevent, at the one boundary where it is easiest to miss.
    through: Option<i64>,
    /// Whether a bar has been dropped since. While this is set, `through`
    /// stops advancing, so the reconnect asks for everything from the last
    /// known-good bar and the gap is refilled.
    gap: bool,
    /// Which historical replay of this series is current, counting from one.
    ///
    /// A generation is what makes a terminator attributable. Without one, an
    /// end that arrives late — queued behind bars a slow consumer had not read
    /// when the connection dropped — is indistinguishable from the end of the
    /// replay that followed it.
    generation: u64,
    /// Where the current generation is in its life.
    phase: SnapshotPhase,
    /// Whether every bar of the current generation reached this consumer.
    ///
    /// Tracked per generation and reset when one opens, so a loss during an
    /// old replay does not condemn the one that replaced it.
    lossless: bool,
    /// The feed client's own loss counter when this generation opened.
    ///
    /// Loss inside dxlink happens before the forwarder ever sees an event, so
    /// it is invisible to the per-consumer accounting. Comparing the counter
    /// across a generation is the only way to notice it.
    dxlink_drops_at_start: u64,
    /// A terminator seen inside an open transaction, not yet acted on, and the
    /// generation it arrived in.
    ///
    /// Stamped, because an armed terminator that outlived its replay would
    /// otherwise fire on the first event of the next one and declare a
    /// snapshot finished on its opening bar.
    pending_end: Option<(u64, dxfeed::SnapshotEndKind)>,
    /// How the current generation ended, once it has.
    ended_as: Option<dxfeed::SnapshotEndKind>,
}

/// Where a series' current historical replay is in its life.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SnapshotPhase {
    /// No replay seen for this series yet.
    Idle,
    /// A replay is in progress and has been announced to consumers.
    Open,
    /// The current generation's replay has finished.
    Ended,
}

impl CandleResume {
    /// A series nothing is known about yet.
    ///
    /// `dxlink_drops` is sampled now rather than when a generation opens, so a
    /// snapshot whose `SNAPSHOT_BEGIN` never arrived is still measured against
    /// a real baseline instead of against zero.
    fn new(dxlink_drops: u64) -> Self {
        Self {
            through: None,
            gap: false,
            generation: 0,
            phase: SnapshotPhase::Idle,
            lossless: true,
            dxlink_drops_at_start: dxlink_drops,
            pending_end: None,
            ended_as: None,
        }
    }
}

/// The bookkeeping for one subscription's series, created on first sight.
fn series_entry<'a>(
    seen: &'a mut HashMap<(u32, String), CandleResume>,
    sub_id: u32,
    symbol: &str,
    dxlink_drops: &Arc<AtomicU64>,
) -> &'a mut CandleResume {
    seen.entry((sub_id, symbol.to_string()))
        .or_insert_with(|| CandleResume::new(dxlink_drops.load(Ordering::Relaxed)))
}

/// Opens a new generation, unless one is already open.
///
/// `None` for a `SNAPSHOT_BEGIN` that repeats one already announced — a
/// re-emission, or the venue's own begin for the replay a reconnect announced
/// ahead of it. One generation, one marker.
fn open_generation(
    progress: &CandleProgress,
    sub_id: u32,
    symbol: &str,
    dxlink_drops: &Arc<AtomicU64>,
) -> Option<dxfeed::DxfSnapshotBeginT> {
    let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
    let resume = series_entry(&mut seen, sub_id, symbol, dxlink_drops);

    // A begin says "discard what you have, a snapshot starts here", so a
    // terminator still waiting for a transaction to close is stale whatever
    // else this begin turns out to be. Left armed, it would fire on the very
    // bar that opened the new replay and declare it finished.
    resume.pending_end = None;

    if resume.phase == SnapshotPhase::Open {
        return None;
    }

    resume.generation += 1;
    resume.phase = SnapshotPhase::Open;
    resume.lossless = true;
    resume.pending_end = None;
    resume.ended_as = None;
    resume.dxlink_drops_at_start = dxlink_drops.load(Ordering::Relaxed);

    Some(dxfeed::DxfSnapshotBeginT {
        generation: resume.generation,
    })
}

/// Records that this generation's history is no longer complete.
fn mark_lossy(progress: &CandleProgress, sub_id: u32, symbol: &str, dxlink_drops: &Arc<AtomicU64>) {
    let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
    let resume = series_entry(&mut seen, sub_id, symbol, dxlink_drops);

    // A replay that has ended has its answer, and rewriting it would tell a
    // consumer its history had holes that its history never had. Anything
    // before that counts, including a drop on a series whose `SNAPSHOT_BEGIN`
    // has not arrived: `finish_generation` numbers that replay retroactively,
    // so its losses have to be remembered retroactively too.
    if resume.phase != SnapshotPhase::Ended {
        resume.lossless = false;
    }
}

/// Remembers a terminator that arrived inside an open transaction.
fn arm_end(
    progress: &CandleProgress,
    sub_id: u32,
    symbol: &str,
    kind: dxfeed::SnapshotEndKind,
    dxlink_drops: &Arc<AtomicU64>,
) {
    let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
    let resume = series_entry(&mut seen, sub_id, symbol, dxlink_drops);
    if resume.phase == SnapshotPhase::Ended {
        return;
    }
    resume.pending_end = Some((resume.generation, kind));
}

/// The terminator waiting for a transaction to close, if there is one.
fn armed_end(
    progress: &CandleProgress,
    sub_id: u32,
    symbol: &str,
    dxlink_drops: &Arc<AtomicU64>,
) -> Option<dxfeed::SnapshotEndKind> {
    let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
    let resume = series_entry(&mut seen, sub_id, symbol, dxlink_drops);
    let (generation, kind) = resume.pending_end?;

    // A terminator armed in an earlier replay says nothing about this one.
    // Firing it here would end a snapshot on the first bar it ever sent.
    if generation != resume.generation {
        resume.pending_end = None;
        return None;
    }

    Some(kind)
}

/// Closes the current generation, returning the marker to send.
///
/// `None` when there is no open replay to close, which is what makes a
/// re-emitted terminator idempotent: the venue may send the end again, and the
/// consumer still sees exactly one.
fn finish_generation(
    progress: &CandleProgress,
    sub_id: u32,
    symbol: &str,
    kind: dxfeed::SnapshotEndKind,
    dxlink_drops: &Arc<AtomicU64>,
) -> Option<dxfeed::DxfSnapshotEndT> {
    let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
    let resume = series_entry(&mut seen, sub_id, symbol, dxlink_drops);

    if resume.phase == SnapshotPhase::Ended {
        return None;
    }

    // A snapshot whose begin never arrived still ends. It is numbered like any
    // other generation so the consumer can tell it from the next one.
    if resume.phase == SnapshotPhase::Idle {
        resume.generation += 1;
    }

    // Loss inside the feed client counts too: those bars never reached this
    // crate, so no consumer below could have noticed them missing.
    let shed = dxlink_drops.load(Ordering::Relaxed) > resume.dxlink_drops_at_start;
    let lossless = resume.lossless && !shed;

    resume.phase = SnapshotPhase::Ended;
    resume.pending_end = None;
    resume.ended_as = Some(kind);
    resume.lossless = lossless;

    Some(dxfeed::DxfSnapshotEndT {
        generation: resume.generation,
        kind,
        lossless,
    })
}

/// Ends every series' current generation without waiting for the venue.
///
/// A reconnect makes the previous replay stop being current the moment the
/// socket drops, not when the next `SNAPSHOT_BEGIN` arrives: a consumer that
/// waited for the venue would believe stale history was still loading for as
/// long as the backoff lasted. Each affected series gets a new generation and
/// a `SnapshotBegin` queued behind whatever it had already been sent, so the
/// consumer sees the phase change in the right place in its own stream, an end
/// still queued from the old generation is recognisable by its number rather
/// than lost, and the venue's own begin for the same replay adds nothing.
/// Ends the current generations and baselines the next ones for a connection
/// that does not exist yet.
///
/// The order is the point, and it is why this is a function rather than two
/// statements. The mirror is zeroed **first**: the next connection is a new
/// feed client counting its own losses from zero, so a baseline carried over
/// from the connection that just died is a number the new counter can never
/// exceed. Every generation of the rebuilt session would then report itself
/// complete, however much it shed.
async fn open_generations_for_the_next_connection(
    progress: &CandleProgress,
    routing: &Arc<RwLock<EventRouting>>,
    history: &Arc<Notify>,
    dxlink_drops: &Arc<AtomicU64>,
) {
    dxlink_drops.store(0, Ordering::Relaxed);
    invalidate_history(progress, routing, history, dxlink_drops).await;
}

async fn invalidate_history(
    progress: &CandleProgress,
    routing: &Arc<RwLock<EventRouting>>,
    history: &Arc<Notify>,
    dxlink_drops: &Arc<AtomicU64>,
) {
    // Computed under the std lock and sent outside it: that guard must never
    // be held across an await.
    let opened: Vec<(u32, String, u64)> = {
        let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
        let drops = dxlink_drops.load(Ordering::Relaxed);
        seen.iter_mut()
            .filter_map(|((sub_id, symbol), resume)| {
                // A series that never saw a replay has nothing to invalidate.
                if resume.phase == SnapshotPhase::Idle && resume.pending_end.is_none() {
                    return None;
                }
                resume.generation += 1;
                resume.phase = SnapshotPhase::Open;
                resume.lossless = true;
                resume.pending_end = None;
                resume.ended_as = None;
                resume.dxlink_drops_at_start = drops;
                Some((*sub_id, symbol.clone(), resume.generation))
            })
            .collect()
    };

    if opened.is_empty() {
        return;
    }

    {
        let routing = routing.read().await;
        for (sub_id, symbol, generation) in &opened {
            let Some(subscribers) = routing.senders.get(sub_id) else {
                continue;
            };
            let marker = marker_event(
                symbol,
                dxfeed::EventData::SnapshotBegin(dxfeed::DxfSnapshotBeginT {
                    generation: *generation,
                }),
            );
            for subscriber in subscribers {
                offer(subscriber, Delivery::Marker(marker.clone()));
            }
        }
    }

    history.notify_waiters();
}

/// One thing a subscription is subscribed to.
///
/// A symbol alone is not enough to describe a subscription any more. Candles
/// are addressed by a symbol that carries their period — `AAPL{=5m}` — so two
/// periods of one underlying are two different streamer symbols, and each
/// needs its own history start. Routing, unsubscribing and the reconnect
/// replay all work from these.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct FeedTarget {
    /// The event type this target is for.
    ///
    /// Part of the identity: a subscription that asked for Quote on AAPL must
    /// not receive the Trade prints another subscription asked for.
    kind: EventKind,
    /// The streamer symbol exactly as the venue is told it, period suffix
    /// included. It is also the `eventSymbol` the events come back under.
    symbol: String,
    /// Where a candle history starts, in epoch milliseconds.
    ///
    /// `None` for everything else. A candle subscription without one replays
    /// an unbounded history, which is why `add_candles` requires it.
    from_time: Option<i64>,
}

/// The DXLink subscription requests for `targets`.
///
/// Shared by subscribing, unsubscribing and the reconnect replay, so a target
/// is restored under exactly the event type, symbol and history start it was
/// subscribed with.
fn feed_subscriptions(targets: &[FeedTarget]) -> Vec<FeedSubscription> {
    targets
        .iter()
        .map(|target| FeedSubscription {
            event_type: target.kind.wire_name().to_string(),
            symbol: target.symbol.clone(),
            from_time: target.from_time,
            source: None,
        })
        .collect()
}

/// Puts removed targets back unless the removal is confirmed.
///
/// `unsubscribe_targets` takes them out of the shared set **before** asking
/// the venue, so that a concurrent `add_candles` resubscribes rather than
/// skipping them. Every way out that is not a confirmed removal therefore has
/// to put them back, and one of those ways is the caller simply dropping the
/// future while it waits — which no `?` and no error branch can catch.
///
/// `close_sub` uses it the same way, and additionally hands over the flag it
/// set to keep new targets out: a close the venue refused leaves the
/// subscription usable, exactly as it was.
struct RemovedTargets<'a> {
    targets: &'a Arc<Mutex<BTreeSet<FeedTarget>>>,
    removed: Vec<FeedTarget>,
    closing: Option<&'a AtomicBool>,
}

impl RemovedTargets<'_> {
    /// The venue confirmed. Hands the targets over and disarms the restore.
    fn commit(mut self) -> Vec<FeedTarget> {
        self.closing = None;
        std::mem::take(&mut self.removed)
    }
}

impl Drop for RemovedTargets<'_> {
    fn drop(&mut self) {
        // Still subscribed as far as anybody knows, so the record has to say
        // so: a retry needs it, and a reconnect has to replay it. Under the
        // lock the flag is read under, so nobody sees the flag down and the
        // set still empty.
        let mut known = targets_of(self.targets);
        for target in self.removed.drain(..) {
            known.insert(target);
        }
        if let Some(closing) = self.closing {
            closing.store(false, Ordering::Release);
        }
    }
}

/// Recovers a poisoned lock rather than panicking.
///
/// The value behind it is a set of subscription targets. A thread panicking
/// while holding the lock cannot leave that set in a state the next reader
/// cannot understand, so poisoning here carries no information worth aborting
/// a caller's process over.
fn targets_of(
    set: &Mutex<BTreeSet<FeedTarget>>,
) -> std::sync::MutexGuard<'_, BTreeSet<FeedTarget>> {
    set.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}

enum DXLinkCommand {
    // No channel id: the caller's copy is a snapshot from connect time, and a
    // reconnect opens a new channel. The supervisor addresses the live one.
    // The kinds travel with the request: the channel has to be configured for
    // an event type before the venue will accept a subscription to it, and
    // only this loop knows what the live channel is already configured for.
    // The set is the subscription's live targets, so the loop can check
    // that what it is about to subscribe is still reserved by the time it
    // gets there; see `withdrawn_request`.
    Subscribe(
        Vec<FeedSubscription>,
        BTreeSet<EventKind>,
        u32,
        Arc<Mutex<BTreeSet<FeedTarget>>>,
        Option<oneshot::Sender<TastyResult<()>>>,
    ),
    // The third field is the subscription's live target set, when the
    // removal is conditional on it. By the time the loop gets here a
    // concurrent `add_candles` may have re-reserved the same series with a
    // different history start, and a target is finer-grained than the route
    // that delivers it: dropping the route, or telling the venue, would take
    // away what the newer call just subscribed. `None` is a close, which
    // lets go of everything whatever the set says.
    Unsubscribe(
        Vec<FeedSubscription>,
        u32,
        Option<Arc<Mutex<BTreeSet<FeedTarget>>>>,
        Option<oneshot::Sender<TastyResult<()>>>,
    ),
    AddEventSender(u32, Subscriber),
    RemoveEventSender(u32),
}

// Live routing registry shared between the command loop and the event
// forwarding task, so senders registered at any time are always visible.
#[derive(Default)]
struct EventRouting {
    senders: HashMap<u32, Vec<Subscriber>>,
    /// Which subscriptions want which `(streamer symbol, event type)`.
    ///
    /// Keyed by both halves, not by the symbol alone. The symbol carries a
    /// candle's period — `AAPL{=5m}` — so two periods of one underlying are
    /// already distinct; the event type is what stops a subscription that
    /// asked for Quote on AAPL from receiving the Trade prints another
    /// subscription asked for.
    routes: HashMap<(String, EventKind), HashSet<u32>>,
}

/// Where each subscription's candle series should resume.
///
/// Keyed by **subscription and symbol**, not by symbol alone. Two
/// subscriptions can watch the same bars and fall behind by different amounts,
/// and a replay is per subscription — so a shared counter would let a
/// subscription that kept up decide where a subscription that did not resumes
/// from. The symbol carries its period, which keeps `AAPL{=5m}` and
/// `AAPL{=h}` apart within one subscription.
type CandleProgress = Arc<Mutex<HashMap<(u32, String), CandleResume>>>;

/// Owns the DXLink connection and the subscriptions on it.
///
/// Deliberately not `Clone`: exactly one value owns the connection, and
/// dropping it disconnects. Subscriptions get a handle instead.
pub struct QuoteStreamer {
    /// Signals the supervisor to disconnect.
    ///
    /// Held only by the owner, never by a handle, so it is dropped exactly when
    /// the streamer is. A oneshot cannot be refused for lack of room the way a
    /// `try_send` into the bounded command queue could, and the supervisor
    /// selects on it during a backoff so a dropped streamer does not leave a
    /// task waiting out thirty seconds for nobody.
    shutdown: Option<oneshot::Sender<()>>,
    next_sub_id: usize,
    subscription_map: HashMap<SubscriptionId, QuoteSubscription>,
    dxlink_command_tx: Option<mpsc::Sender<DXLinkCommand>>,
    /// What a reconnect has to restore, shared with the supervisor.
    registry: Registry,
    state: Arc<RwLock<ConnectionState>>,
    /// Where each subscription's candle series stands, shared with the
    /// forwarder. Answers `history_loaded` without asking the venue anything.
    progress: CandleProgress,
    /// Consumers announcing they have made room, so a parked marker can go out
    /// without waiting for the next event.
    drained: Arc<Notify>,
    /// A series changed phase, so anybody awaiting one should look again.
    history: Arc<Notify>,
}

/// What each subscription is subscribed to, as the supervisor needs it.
///
/// The symbol set is the same `Arc` the subscription and the streamer share,
/// so a replay sends exactly what the venue confirmed — never a symbol whose
/// subscribe was refused, and never one that was already unsubscribed.
#[derive(Clone)]
struct SubscriptionRecord {
    kinds: BTreeSet<EventKind>,
    targets: Arc<Mutex<BTreeSet<FeedTarget>>>,
}

/// Subscription id to what it is subscribed to.
type Registry = Arc<Mutex<HashMap<u32, SubscriptionRecord>>>;

impl QuoteStreamer {
    /// Opens a DXLink connection and its market-data channel.
    ///
    /// Reconnects under [`BackoffPolicy::default`]; see
    /// [`QuoteStreamer::connect_with_policy`] to choose your own.
    ///
    /// # Errors
    ///
    /// Fails when the streamer token cannot be obtained, the connection
    /// cannot be established, or the channel cannot be configured.
    pub async fn connect(tasty: &TastyTrade) -> TastyResult<Self> {
        Self::connect_with_policy(tasty, BackoffPolicy::default()).await
    }

    /// Opens a DXLink connection with an explicit reconnection policy.
    ///
    /// The first connection is established before returning, so a caller who
    /// cannot reach the venue at all learns immediately rather than through a
    /// stream that never produces anything. After that a supervisor owns the
    /// connection: when it is lost, the supervisor waits out the backoff,
    /// fetches a fresh streamer token, reconnects, and resubscribes every
    /// symbol the subscriptions were subscribed to. Subscriptions keep working
    /// across that — they hold a handle to the command loop, not to the
    /// connection.
    ///
    /// # Errors
    ///
    /// Fails when the streamer token cannot be obtained, the connection
    /// cannot be established, or the channel cannot be configured.
    pub async fn connect_with_policy(
        tasty: &TastyTrade,
        policy: BackoffPolicy,
    ) -> TastyResult<Self> {
        // Prove the venue is reachable before handing back a streamer.
        let connection = connect_dxlink(tasty).await?;

        let (command_tx, command_rx) = mpsc::channel::<DXLinkCommand>(100);

        // Shutdown travels on its own channel. Routing it through the bounded
        // command queue meant a full queue could discard it, and the loop
        // cannot simply exit when the command sender dies either, because
        // every subscription handle holds a clone of that sender.
        let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

        // Owned out here, so it survives a reconnect. A subscription's route
        // is registered once; rebuilding the map per connection would drop the
        // first events of every reconnect, and re-registering is not something
        // a caller can be asked to do.
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let registry: Registry = Arc::new(Mutex::new(HashMap::new()));
        let state = Arc::new(RwLock::new(ConnectionState::Connected));
        // Outside the connection loop for the same reason `routing` is: a
        // reconnect must resume a candle series where the consumer was left,
        // and a per-connection map would forget that every time.
        let progress: CandleProgress = Arc::new(Mutex::new(HashMap::new()));
        // Both outlive a connection for the same reason `progress` does: a
        // marker parked by one connection is flushed by the next, and a
        // consumer awaiting history keeps waiting across a reconnect.
        let drained = Arc::new(Notify::new());
        let history = Arc::new(Notify::new());

        tokio::spawn(supervise(
            tasty.clone(),
            policy,
            connection,
            command_rx,
            shutdown_rx,
            routing,
            registry.clone(),
            state.clone(),
            progress.clone(),
            drained.clone(),
            history.clone(),
        ));

        Ok(Self {
            shutdown: Some(shutdown_tx),
            next_sub_id: 0,
            subscription_map: HashMap::new(),
            dxlink_command_tx: Some(command_tx),
            registry,
            state,
            progress,
            drained,
            history,
        })
    }

    /// Where the connection is in its lifecycle.
    ///
    /// Carries no token, credential or account identifier, so it is safe to
    /// log or show. A reconnect that happens silently is indistinguishable
    /// from one that is not happening, which is what this exists to answer.
    pub async fn state(&self) -> ConnectionState {
        self.state.read().await.clone()
    }

    /// A handle a subscription can hold without owning the connection.
    fn handle(&self) -> StreamerHandle {
        StreamerHandle {
            commands: self.dxlink_command_tx.clone(),
        }
    }

    /// Creates a subscription for the given event types.
    ///
    /// Was `create_sub(flags: i32)`, a raw dxfeed bitmask a caller had to know
    /// the constants for, covering three of the eleven types the feed models.
    /// **Breaking**, and deliberately so: the replacement is a typed set, and
    /// there is no bit pattern that silently means "nothing".
    ///
    /// Including [`EventKind::Candle`] is what allows
    /// [`QuoteSubscription::add_candles`]; candles are not subscribed by
    /// [`QuoteSubscription::add_symbols`], because a bare symbol has neither a
    /// period nor a history start.
    ///
    /// **`async`, and that is the fix rather than an inconvenience.** The
    /// returned subscription's event route is registered with the command loop
    /// *before* this returns. It used to be registered from a detached
    /// `tokio::spawn`, so a caller that subscribed immediately could have the
    /// subscribe reach the loop first — and a candle subscription's history
    /// arrives at once, so the first bars were routed to a subscription the
    /// loop did not know about yet and dropped.
    ///
    /// # Errors
    ///
    /// Fails when the streamer is closed, which is the only way registration
    /// can not happen. Returning a subscription that can never receive
    /// anything would be the same silent failure in a different place.
    pub async fn create_sub(
        &mut self,
        kinds: impl IntoIterator<Item = EventKind>,
    ) -> TastyResult<Box<QuoteSubscription>> {
        self.create_sub_with_capacity(kinds, DEFAULT_EVENT_CHANNEL_CAPACITY)
            .await
    }

    /// The same, with a chosen buffer size.
    ///
    /// The default absorbs a documented candle history — about 1440 events for
    /// a day of one-minute bars, arriving together — and a caller who knows
    /// their own history is larger, or who is reading a firehose of
    /// `TimeAndSale` slowly, should say so rather than discovering the number
    /// through [`QuoteSubscription::lagged`].
    ///
    /// Bounded either way. An unbounded channel does not remove the problem,
    /// it converts a slow consumer into unbounded memory.
    ///
    /// # Errors
    ///
    /// As [`QuoteStreamer::create_sub`], plus
    /// [`TastyTradeError::Precondition`] for a capacity of zero, which is a
    /// subscription that can never deliver anything.
    pub async fn create_sub_with_capacity(
        &mut self,
        kinds: impl IntoIterator<Item = EventKind>,
        capacity: usize,
    ) -> TastyResult<Box<QuoteSubscription>> {
        if capacity == 0 {
            return Err(TastyTradeError::Precondition(
                "a subscription with no buffer cannot deliver anything; it would drop every \
                 event and report itself as lagging"
                    .to_string(),
            ));
        }

        let kinds: BTreeSet<EventKind> = kinds.into_iter().collect();
        let id = SubscriptionId(self.next_sub_id);
        self.next_sub_id += 1;
        let sub_id = id.0 as u32;

        let (caller_tx, caller_rx) = mpsc::channel(capacity);
        let lagged = Arc::new(AtomicU64::new(0));
        let (_event_sender, event_receiver) = flume::unbounded();

        let Some(commands) = &self.dxlink_command_tx else {
            return Err(TastyTradeError::Streaming(
                "the quote streamer has no command channel; reconnect before subscribing"
                    .to_string(),
            ));
        };

        // **One** registered consumer, the caller's. The streamer used to
        // register a second for the copy it keeps, and nothing could ever read
        // that one: `get_sub` hands out a `&QuoteSubscription` and `get_event`
        // needs `&mut`. So it filled to capacity and then charged a drop for
        // every event afterwards — manufacturing exactly the lag this change
        // exists to measure.
        commands
            .send(DXLinkCommand::AddEventSender(
                sub_id,
                Subscriber {
                    events: caller_tx,
                    lagged: lagged.clone(),
                    pending: Arc::new(Mutex::new(VecDeque::new())),
                },
            ))
            .await
            .map_err(|_| {
                TastyTradeError::Streaming(
                    "the quote streamer is closed; it cannot route events to a new subscription"
                        .to_string(),
                )
            })?;

        // Create subscription
        let targets = Arc::new(Mutex::new(BTreeSet::new()));

        // The supervisor replays from this. Registering the shared set rather
        // than a copy is what makes the replay send exactly what the venue
        // confirmed, including targets added long after this call.
        self.registry
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .insert(
                sub_id,
                SubscriptionRecord {
                    kinds: kinds.clone(),
                    targets: targets.clone(),
                },
            );

        // The streamer's own copy exists for `close_sub` and `get_sub`, which
        // read its targets and its identity. Its event channel is closed from
        // the start rather than being fed into a void: a receiver nobody can
        // reach should say so if anybody ever asks it.
        //
        // The lag counter is the **same** `Arc` the caller holds, not a fresh
        // one. A private counter here would make `get_sub(id).lagged()` report
        // zero however far behind the caller's subscription had fallen — a
        // number that is worse than no number, because it looks like an
        // answer.
        let (_closed, closed_rx) = mpsc::channel(1);
        let closed = Arc::new(AtomicBool::new(false));
        self.subscription_map.insert(
            id,
            QuoteSubscription {
                id,
                streamer: self.handle(),
                kinds: kinds.clone(),
                event_receiver: event_receiver.clone(),
                dxlink_receiver: closed_rx,
                targets: targets.clone(),
                lagged: lagged.clone(),
                progress: self.progress.clone(),
                drained: self.drained.clone(),
                history: self.history.clone(),
                closed: closed.clone(),
            },
        );

        Ok(Box::new(QuoteSubscription {
            id,
            streamer: self.handle(),
            kinds,
            event_receiver,
            dxlink_receiver: caller_rx,
            targets,
            lagged,
            progress: self.progress.clone(),
            drained: self.drained.clone(),
            history: self.history.clone(),
            closed,
        }))
    }

    /// Retrieve a subscription by id.
    pub fn get_sub(&self, id: SubscriptionId) -> Option<&QuoteSubscription> {
        self.subscription_map.get(&id)
    }

    /// Unsubscribes a subscription's symbols and removes it.
    ///
    /// Total, even against a concurrent `add_candles` on the same
    /// subscription: one that starts after the close began is refused, and
    /// one that was queued before it is refused when the loop reaches it, so
    /// nothing is left on the wire that the closed subscription would have
    /// been the only one to hold.
    ///
    /// # Errors
    ///
    /// Fails when the venue refuses the unsubscribe or the streamer is
    /// closed. On failure the subscription's symbols stay recorded and it
    /// stays usable, because that record is what a retry needs.
    pub async fn close_sub(&mut self, id: SubscriptionId) -> TastyResult<()> {
        // Get symbols from subscription to close. This is the shared set, so
        // it holds what add_symbols actually subscribed rather than the empty
        // vector this used to read.
        if let Some(subscription) = self.subscription_map.get(&id) {
            // Closed and drained in one lock section, before the venue is
            // asked. From here no `add_candles` can reserve a target (the
            // flag), and one that reserved earlier but whose subscribe has
            // not been processed yet is refused when it is, because its
            // target is no longer in the set. Without both, a series added
            // during the close ended up on the wire with no route and no
            // record. The guard puts everything back if the close fails.
            let pending = {
                let mut known = targets_of(&subscription.targets);
                subscription.closed.store(true, Ordering::Release);
                let removed: Vec<FeedTarget> = known.iter().cloned().collect();
                known.clear();
                RemovedTargets {
                    targets: &subscription.targets,
                    removed,
                    closing: Some(&subscription.closed),
                }
            };

            let unsubscribe_requests = feed_subscriptions(&pending.removed);

            // Awaited, and the local state is only discarded once the venue
            // has confirmed. Clearing it on a queued-but-unconfirmed command
            // threw away the one record of what still needs unsubscribing.
            if let Some(tx) = &self.dxlink_command_tx {
                let sub_id = id.0 as u32;

                let closed = |_| {
                    TastyTradeError::Streaming(
                        "the quote streamer is closed; the subscription is gone with it"
                            .to_string(),
                    )
                };

                if !unsubscribe_requests.is_empty() {
                    let (ack, answered) = oneshot::channel();
                    // Unconditional: the set was drained above, and a close
                    // is total.
                    tx.send(DXLinkCommand::Unsubscribe(
                        unsubscribe_requests,
                        sub_id,
                        None,
                        Some(ack),
                    ))
                    .await
                    .map_err(closed)?;

                    answered.await.unwrap_or_else(|_| {
                        Err(TastyTradeError::Streaming(
                            "the quote streamer closed before the unsubscribe was confirmed"
                                .to_string(),
                        ))
                    })?;
                }

                // Only now: the venue has stopped sending, so there is nothing
                // left to route.
                tx.send(DXLinkCommand::RemoveEventSender(sub_id))
                    .await
                    .map_err(closed)?;
            }

            // Confirmed unsubscribed, so the drained set stays drained and
            // the subscription stays closed. Reached only on success: an
            // early return above drops the guard, which puts the targets back
            // and reopens the subscription, which is what a retry needs.
            pending.commit();
        }

        // Remove subscription from map, and from what a reconnect restores:
        // a closed subscription must not come back on the next connection.
        self.subscription_map.remove(&id);
        self.registry
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .remove(&(id.0 as u32));

        // And its history with it. A handle kept after the close would
        // otherwise keep answering `history_loaded` for a series nobody is
        // subscribed to, and every closed subscription's entries would be
        // walked again on every later reconnect.
        self.progress
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .retain(|(sub_id, _), _| *sub_id != id.0 as u32);
        self.history.notify_waiters();

        Ok(())
    }
}

/// One live DXLink connection and everything the supervisor needs from it.
struct LiveConnection {
    client: DXLinkClient,
    channel_id: u32,
    /// The receiver `DXLinkClient::connect` hands back.
    ///
    /// Two things at once. It is the *only* path market events take out of
    /// dxlink — the client's event sender is created inside `connect`, so a
    /// later `event_stream()` call is refused with "Event stream already
    /// created", and dropping this receiver disconnects the feed from its
    /// consumer permanently, which is what used to happen here. And it closes
    /// when the session ends, which is how a drop becomes visible without this
    /// client writing anything.
    events: mpsc::Receiver<MarketEvent>,
}

/// Opens a connection, its feed channel, and configures the event types.
async fn connect_dxlink(tasty: &TastyTrade) -> TastyResult<LiveConnection> {
    let tokens = tasty.quote_streamer_tokens().await?;
    // The token itself is never logged, here or anywhere: the streamer-token
    // response is a credential.
    debug!(
        "Obtained DXLink streamer token ({} bytes)",
        tokens.token.len()
    );

    // dxlink can reconnect on its own (`with_reconnect`), and it is
    // deliberately not installed: this crate already reconnects under
    // `BackoffPolicy`, replaying the symbols each subscription confirmed, and
    // two policies over one socket would count a single drop twice, under two
    // budgets, with two sets of attempt numbers reaching the caller through
    // `state()`. What is used from dxlink instead is the fact it reports the
    // session ending — the event stream closes — which is the part this crate
    // cannot observe for itself.
    // Block rather than drop. dxlink's default sheds events when its consumer
    // falls behind, and this crate's consumer is the forwarder, which never
    // waits on anybody: it hands events over with `try_send` and drops them
    // itself when a subscriber is full, where the loss is counted and, for a
    // snapshot, reflected in the marker. Letting dxlink drop as well would add
    // a second, invisible loss above the one place that can explain it. The
    // documented hazard of blocking does not apply here, because this crate
    // registers no dxlink callbacks and always reads the stream.
    let mut client = DXLinkClient::new(&tokens.streamer_url, &tokens.token)
        .with_overflow_policy(OverflowPolicy::Block);

    info!("Connecting to DXLink server: {}", tokens.streamer_url);
    let events = client.connect().await.map_err(|e| {
        // Through From, so an authentication refusal arrives as Auth and the
        // policy can tell it apart from a socket that dropped.
        let error: TastyTradeError = e.into();
        error
    })?;

    let channel_id = client
        .create_feed_channel("AUTO")
        .await
        .map_err(TastyTradeError::from)?;
    info!("DXLink channel created: {}", channel_id);

    // Deliberately no `setup_feed` here. The channel is configured when the
    // first subscription says what it wants, and reconfigured whenever a later
    // one wants more. Setting it up at connect time meant hardcoding a list —
    // it was Quote, Trade and Greeks — so a subscription asking for candles
    // was accepted locally and then never delivered anything, which is
    // indistinguishable from a quiet market.
    Ok(LiveConnection {
        client,
        channel_id,
        events,
    })
}

/// Configures the channel for `wanted`, if it is not already.
///
/// dxlink refuses a subscription to an event type the channel has no validated
/// configuration for, and `setup_feed` replaces the configuration rather than
/// adding to it — so this always sends the union, never just the new types.
///
/// # Errors
///
/// [`TastyTradeError::Connection`] when the socket is gone, so the caller can
/// tell that apart from a configuration the venue simply refused.
async fn ensure_configured(
    client: &mut DXLinkClient,
    channel_id: u32,
    configured: &mut BTreeSet<EventKind>,
    wanted: &BTreeSet<EventKind>,
) -> TastyResult<()> {
    if wanted.is_subset(configured) {
        return Ok(());
    }

    let union: BTreeSet<EventKind> = configured.union(wanted).copied().collect();
    let types: Vec<EventType> = union.iter().copied().map(feed_event_type).collect();

    debug!(
        "Configuring feed channel {channel_id} for {} event type(s)",
        types.len()
    );

    match client.setup_feed(channel_id, &types).await {
        Ok(()) => {
            *configured = union;
            Ok(())
        }
        Err(e) => {
            let lost = is_connection_lost(&e);
            let message = format!("the venue refused the feed configuration: {e}");
            Err(if lost {
                TastyTradeError::Connection(message)
            } else {
                TastyTradeError::Streaming(message)
            })
        }
    }
}

/// Why a connection stopped being used.
enum Ended {
    /// The owner dropped the streamer, or the command channel closed.
    Owner,
    /// A write failed in a way that says the socket is gone.
    ConnectionLost,
}

/// Forwards market events to the subscriptions registered for their symbol.
///
/// `saw_event` is the reconnect milestone: an event that actually arrived is
/// evidence the feed works, which a successful handshake is not. dxlink's
/// `subscribe` returns as soon as the write succeeds — the venue does not
/// acknowledge it — so resetting the attempt budget on a subscribe would reset
/// it on a connection that accepts the socket and then sends nothing, which is
/// the accept-then-reject loop the policy exists to bound.
/// Returns when the stream closes, which dxlink does when the session ends.
/// The supervisor watches this task for exactly that reason.
async fn forward_events(
    mut events: mpsc::Receiver<MarketEvent>,
    routing: Arc<RwLock<EventRouting>>,
    progress: CandleProgress,
    saw_event: Arc<AtomicBool>,
    drained: Arc<Notify>,
    dxlink_drops: Arc<AtomicU64>,
    history: Arc<Notify>,
) {
    // A marker parked before a reconnect is waiting for room, not for an
    // event, so it goes out even if this connection never delivers anything.
    flush_pending(&routing).await;

    loop {
        let event = tokio::select! {
            biased;
            // A consumer read something, so there may be room for what is
            // parked. Without this a marker would wait for the next event on
            // its own series, which after a snapshot ends may never come.
            () = drained.notified() => {
                flush_pending(&routing).await;
                continue;
            }
            event = events.recv() => match event {
                Some(event) => event,
                None => return,
            },
        };

        // Anything arriving is evidence the feed works, whether or not this
        // crate models it. That is what the milestone is for.
        saw_event.store(true, Ordering::Relaxed);

        let Some(symbol) = event_symbol(&event) else {
            continue;
        };
        let symbol = symbol.to_string();
        let kind = event_kind(&event);

        // Read once, ahead of delivery: a terminator has to be recognised
        // before the event it rides on is handed over, or the marker cannot be
        // placed immediately after it.
        let snapshot = match &event {
            MarketEvent::Candle(candle) => SnapshotSignals::of(candle.event_flags),
            _ => SnapshotSignals::of(0),
        };

        let routes = routing.read().await;
        let Some(sub_ids) = routes.routes.get(&(symbol.clone(), kind)) else {
            debug!("No subscription registered for {kind} on {symbol}");
            continue;
        };

        let mut phase_changed = false;

        // Delivery is charged per subscription: one consumer falling behind
        // must not decide where another one resumes from, and a replay is per
        // subscription so its phase is too.
        for sub_id in sub_ids {
            let Some(subscribers) = routes.senders.get(sub_id) else {
                continue;
            };

            // 1. Announce the replay before any of its bars.
            if snapshot.begin
                && let Some(begin) = open_generation(&progress, *sub_id, &symbol, &dxlink_drops)
            {
                phase_changed = true;
                let marker = marker_event(&symbol, dxfeed::EventData::SnapshotBegin(begin));
                for subscriber in subscribers {
                    offer(subscriber, Delivery::Marker(marker.clone()));
                }
            }

            // 2. The event itself, unless it is a bare terminator: that row
            //    carries flags and placeholders, never a bar.
            if !snapshot.data_less {
                let mut delivered = false;
                let mut dropped = 0usize;
                for subscriber in subscribers {
                    // A consumer that is not keeping up loses events rather
                    // than stalling everyone else's. What changed is that
                    // losing them is now countable and, for candles,
                    // recoverable.
                    if offer(subscriber, Delivery::Market(event.clone())) {
                        delivered = true;
                    } else {
                        subscriber.lagged.fetch_add(1, Ordering::Relaxed);
                        dropped += 1;
                    }
                }

                if dropped > 0 {
                    // The symbol and the type only — market data never travels
                    // with the warning.
                    warn!(
                        "A consumer fell behind: dropped {kind} for {symbol} on {dropped} \
                         channel(s) of subscription {sub_id}"
                    );
                }

                if let MarketEvent::Candle(candle) = &event {
                    record_bar(
                        &progress,
                        *sub_id,
                        &symbol,
                        candle.time,
                        delivered && dropped == 0,
                        &dxlink_drops,
                    );
                    if dropped > 0 {
                        // "The replay finished" and "you have all of it" are
                        // different answers. This is what makes them differ.
                        mark_lossy(&progress, *sub_id, &symbol, &dxlink_drops);
                    }
                }
            }

            // 3. The terminator, after the bar it may have arrived with, so a
            //    consumer reading in order sees the last bar and then the end.
            if kind == EventKind::Candle {
                let finished = match snapshot.end {
                    // A terminator inside an open transaction is armed, not
                    // fired: the transaction is not a fact until an event
                    // closes it, and acting early would announce a history
                    // that is still being amended.
                    Some(end) if snapshot.tx_pending => {
                        arm_end(&progress, *sub_id, &symbol, end, &dxlink_drops);
                        None
                    }
                    Some(end) => finish_generation(&progress, *sub_id, &symbol, end, &dxlink_drops),
                    // Not a terminator itself, but one may have been waiting
                    // for an event to close its transaction.
                    None if !snapshot.tx_pending => {
                        match armed_end(&progress, *sub_id, &symbol, &dxlink_drops) {
                            Some(end) => {
                                finish_generation(&progress, *sub_id, &symbol, end, &dxlink_drops)
                            }
                            None => None,
                        }
                    }
                    None => None,
                };

                if let Some(end) = finished {
                    phase_changed = true;
                    let marker = marker_event(&symbol, dxfeed::EventData::SnapshotEnd(end));
                    for subscriber in subscribers {
                        offer(subscriber, Delivery::Marker(marker.clone()));
                    }
                }
            }
        }

        drop(routes);
        if phase_changed {
            history.notify_waiters();
        }
    }
}

/// Moves a subscription's resume point, or marks that it cannot move.
///
/// The rule in one place because it is the subtle part. A bar that reached the
/// consumer with nothing missing before it advances `through`. A bar that was
/// dropped — for this consumer, on any of its channels — sets `gap`, and from
/// then on nothing advances until a replay clears it.
///
/// The alternative, taking the maximum of what was delivered, reads correctly
/// and is wrong: bar *n* dropped and bar *n+1* delivered moves the resume point
/// past *n*, so the reconnect never asks for it again and the series has a hole
/// nothing downstream can see.
fn record_bar(
    progress: &CandleProgress,
    sub_id: u32,
    symbol: &str,
    time: i64,
    complete: bool,
    dxlink_drops: &Arc<AtomicU64>,
) {
    let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
    // Nothing delivered yet, so nothing to resume past. A new entry starts
    // empty rather than at this bar's time: if this bar was dropped, seeding
    // it here would skip it forever.
    let resume = series_entry(&mut seen, sub_id, symbol, dxlink_drops);

    if !complete {
        resume.gap = true;
        return;
    }
    if !resume.gap {
        resume.through = Some(match resume.through {
            Some(through) => through.max(time),
            None => time,
        });
    }
}

/// Re-subscribes every symbol the subscriptions still hold.
///
/// Returns whether all of them landed. A partial replay is reported as a
/// failure, because a subscription silently missing half its symbols is worse
/// than one more reconnect.
async fn replay(
    client: &mut DXLinkClient,
    channel_id: u32,
    registry: &Registry,
    progress: &CandleProgress,
    configured: &mut BTreeSet<EventKind>,
) -> bool {
    let pending = pending_replay(registry, progress);

    if pending.is_empty() {
        return true;
    }

    // The new channel has to be configured before the venue will accept any
    // of this, and for exactly the event types the surviving subscriptions
    // hold — not for a fixed three, and not for all eleven.
    let wanted: BTreeSet<EventKind> = registry
        .lock()
        .unwrap_or_else(|p| p.into_inner())
        .values()
        .flat_map(|record| record.kinds.iter().copied())
        .collect();
    if let Err(e) = ensure_configured(client, channel_id, configured, &wanted).await {
        warn!("Could not configure the feed channel after reconnecting: {e}");
        return false;
    }

    debug!(
        "Restoring {} subscription(s) after a reconnect",
        pending.len()
    );

    // The replay is what refills a gap, so the flag that stopped a resume
    // point advancing is cleared as the request goes out. Leaving it set would
    // freeze every future reconnect at the same bar.
    {
        let mut seen = progress.lock().unwrap_or_else(|p| p.into_inner());
        for resume in seen.values_mut() {
            resume.gap = false;
        }
    }

    for (sub_id, requests) in pending {
        if let Err(e) = client.subscribe(channel_id, requests).await {
            warn!("Could not restore subscription {sub_id}: {e}");
            return false;
        }
    }
    true
}

/// What a replay would send, per subscription.
///
/// Separate from sending it because this is the part worth pinning: a
/// subscription that was closed, or one whose subscribe the venue refused,
/// must not come back on the next connection.
///
/// The lock is released before the caller awaits anything — a
/// `std::sync::Mutex` guard must not be held across an await.
fn pending_replay(
    registry: &Registry,
    progress: &CandleProgress,
) -> Vec<(u32, Vec<FeedSubscription>)> {
    let seen = progress.lock().unwrap_or_else(|p| p.into_inner()).clone();
    let registry = registry.lock().unwrap_or_else(|p| p.into_inner());
    registry
        .iter()
        .map(|(sub_id, record)| {
            let targets: Vec<FeedTarget> = targets_of(&record.targets)
                .iter()
                .cloned()
                .map(|target| resume_from(*sub_id, target, &seen))
                .collect();
            (*sub_id, feed_subscriptions(&targets))
        })
        .filter(|(_, requests)| !requests.is_empty())
        .collect()
}

/// Where a candle subscription should pick up after a reconnect.
///
/// Replaying the original `from_time` would re-send every bar the consumer has
/// already been given — for one-minute candles over a day that is about 1440
/// events per symbol, duplicated on every reconnect, and a reconnect loop
/// multiplies it. So the replay starts from one millisecond past the last bar
/// delivered **contiguously**, and falls back to the original start when none
/// has been.
///
/// Contiguously is the word that matters. A subscription that dropped a bar
/// stops advancing its resume point, so the replay comes back from before the
/// gap and refills it — at the cost of duplicates after that point, which is
/// the safe direction: a duplicate bar is visible to a consumer and a missing
/// one is not.
///
/// Deliberately not "now": a drop that lasted a minute would leave a hole for
/// exactly the same reason.
fn resume_from(
    sub_id: u32,
    mut target: FeedTarget,
    seen: &HashMap<(u32, String), CandleResume>,
) -> FeedTarget {
    if target.kind != EventKind::Candle {
        return target;
    }
    // Only a bar that was actually delivered moves the start. A series that
    // has only ever dropped bars keeps the caller's own `from_time`, so the
    // reconnect asks for the whole thing again.
    if let Some(resume) = seen.get(&(sub_id, target.symbol.clone()))
        && let Some(through) = resume.through
    {
        target.from_time = Some(match target.from_time {
            Some(original) => original.max(through.saturating_add(1)),
            None => through.saturating_add(1),
        });
    }
    target
}

/// The symbol an event is about.
///
/// Every one of the eleven types the feed models carries an `eventSymbol`, so
/// this always answers. It stays an `Option` because the alternative is an
/// unwrap on a value the compiler cannot prove.
///
/// For a candle the symbol carries its period — `AAPL{=5m}` — which is what
/// keeps two periods of one underlying from delivering into each other.
fn event_symbol(event: &MarketEvent) -> Option<&str> {
    Some(match event {
        MarketEvent::Quote(quote) => &quote.event_symbol,
        MarketEvent::Trade(trade) => &trade.event_symbol,
        MarketEvent::TradeETH(trade) => &trade.event_symbol,
        MarketEvent::Greeks(greeks) => &greeks.event_symbol,
        MarketEvent::Candle(candle) => &candle.event_symbol,
        MarketEvent::Summary(summary) => &summary.event_symbol,
        MarketEvent::TimeAndSale(sale) => &sale.event_symbol,
        MarketEvent::Profile(profile) => &profile.event_symbol,
        MarketEvent::Underlying(underlying) => &underlying.event_symbol,
        MarketEvent::TheoPrice(theo) => &theo.event_symbol,
        MarketEvent::Series(series) => &series.event_symbol,
    })
}

/// Which of the eleven types an event is.
///
/// Exhaustive without a wildcard on purpose. Upstream `MarketEvent` is **not**
/// `#[non_exhaustive]`, so a twelfth variant breaks the build here — which is
/// the moment to decide whether this crate models it, rather than discovering
/// months later that its events were being dropped. That tripwire is what
/// produced this change: `0.3.1` added `TradeETH` and `Series` and the build
/// stopped compiling.
///
/// If upstream ever does add `#[non_exhaustive]`, replace this with a test
/// over the full type list rather than losing it to a `_ =>` arm.
fn event_kind(event: &MarketEvent) -> EventKind {
    match event {
        MarketEvent::Quote(_) => EventKind::Quote,
        MarketEvent::Trade(_) => EventKind::Trade,
        MarketEvent::TradeETH(_) => EventKind::TradeEth,
        MarketEvent::Greeks(_) => EventKind::Greeks,
        MarketEvent::Candle(_) => EventKind::Candle,
        MarketEvent::Summary(_) => EventKind::Summary,
        MarketEvent::TimeAndSale(_) => EventKind::TimeAndSale,
        MarketEvent::Profile(_) => EventKind::Profile,
        MarketEvent::Underlying(_) => EventKind::Underlying,
        MarketEvent::TheoPrice(_) => EventKind::TheoPrice,
        MarketEvent::Series(_) => EventKind::Series,
    }
}

/// The dxlink event type a kind subscribes as.
fn feed_event_type(kind: EventKind) -> EventType {
    match kind {
        EventKind::Quote => EventType::Quote,
        EventKind::Trade => EventType::Trade,
        EventKind::TradeEth => EventType::TradeETH,
        EventKind::Greeks => EventType::Greeks,
        EventKind::Candle => EventType::Candle,
        EventKind::Summary => EventType::Summary,
        EventKind::TimeAndSale => EventType::TimeAndSale,
        EventKind::Profile => EventType::Profile,
        EventKind::Underlying => EventType::Underlying,
        EventKind::TheoPrice => EventType::TheoPrice,
        EventKind::Series => EventType::Series,
    }
}

/// Whether `sub_id` still has a consumer to deliver to.
///
/// `create_sub` registers the sender before it hands the subscription out,
/// and `close_sub` removes it last, so this is the loop's own record of
/// whether the subscription exists. It is read at the moment a command is
/// processed, which is the only moment that matters for a command that was
/// queued before the close and reached the loop after it.
async fn subscription_is_open(routing: &Arc<RwLock<EventRouting>>, sub_id: u32) -> bool {
    routing.read().await.senders.contains_key(&sub_id)
}

/// The first of `requested` that `held` no longer reserves, if any.
///
/// A subscribe reserves its targets before it is queued and gives them back
/// if it is refused, so a target missing from the set when the command is
/// processed was withdrawn in between: by a `remove_candles` that ran after
/// the reservation, or by a `close_sub`. Subscribing anyway would record a
/// route for a target the subscription no longer claims and put a series on
/// the wire that nothing later takes off, while the caller is told it worked.
fn withdrawn_request(
    held: &Mutex<BTreeSet<FeedTarget>>,
    requested: &[FeedSubscription],
) -> Option<String> {
    let held = targets_of(held);
    requested
        .iter()
        .find(|request| {
            !held.iter().any(|target| {
                target.symbol == request.symbol
                    && target.kind.wire_name() == request.event_type
                    && target.from_time == request.from_time
            })
        })
        .map(|request| request.symbol.clone())
}

/// Records that `sub_id` wants these `(symbol, event type)` pairs.
///
/// Called before the subscribe is written, so no event can arrive for a route
/// that does not exist yet.
async fn record_routes(
    routing: &Arc<RwLock<EventRouting>>,
    sub_id: u32,
    subscriptions: &[FeedSubscription],
) {
    let mut routing = routing.write().await;
    for route in routes_of(subscriptions) {
        routing.routes.entry(route).or_default().insert(sub_id);
    }
}

/// The requests nobody but `sub_id` is still subscribed to.
///
/// Every subscription on a streamer shares one feed channel, so an unsubscribe
/// is channel-wide: asking the venue to stop a series another subscription is
/// still watching would cut its feed too, silently and with no way for it to
/// notice. Only a series this subscription is the last holder of may leave the
/// wire.
///
/// A request with no route at all is not on the wire for anybody: the route
/// is recorded before the venue is asked and taken back when it refuses, so
/// its absence means the subscribe never landed, or was already released.
/// There is nothing for the venue to stop.
async fn orphaned_subscriptions(
    routing: &Arc<RwLock<EventRouting>>,
    sub_id: u32,
    subscriptions: &[FeedSubscription],
) -> Vec<FeedSubscription> {
    let routing = routing.read().await;
    subscriptions
        .iter()
        .filter(|subscription| {
            routes_of(std::slice::from_ref(*subscription))
                .first()
                .is_some_and(|route| {
                    routing
                        .routes
                        .get(route)
                        .is_some_and(|holders| holders.iter().all(|holder| *holder == sub_id))
                })
        })
        .cloned()
        .collect()
}

/// What an unsubscribe still has to do by the time the loop processes it.
struct UnsubscribePlan {
    /// The requests `sub_id` is actually letting go of, and whose routes it
    /// therefore gives up: those it holds no target for any more.
    released: Vec<FeedSubscription>,
    /// The subset of `released` nobody else holds, which is all the venue may
    /// be told.
    orphaned: Vec<FeedSubscription>,
}

/// Decides what an unsubscribe request may still do once it reaches the loop.
///
/// A route is keyed by `(streamer symbol, event type)`; a target also carries
/// a candle's history start. So a subscription can hold two targets for one
/// route, and does exactly that in the window between a `remove_candles` and
/// a concurrent `add_candles` of the same series with a different start. The
/// remove was queued for the older target, but if it is processed after the
/// add it must not undo it: the route stays, and the venue is not told, or
/// the series the add just subscribed would be cut with nothing to say so.
///
/// `still_held` is the subscription's live target set, the same one both
/// calls reserve in before they queue anything, so reading it here sees the
/// add's claim however the two commands were ordered. `None` means a close,
/// which releases everything.
async fn plan_unsubscribe(
    routing: &Arc<RwLock<EventRouting>>,
    sub_id: u32,
    still_held: Option<&Mutex<BTreeSet<FeedTarget>>>,
    requested: &[FeedSubscription],
) -> UnsubscribePlan {
    let released: Vec<FeedSubscription> = match still_held {
        None => requested.to_vec(),
        Some(targets) => {
            let held = targets_of(targets);
            requested
                .iter()
                .filter(|request| {
                    !held.iter().any(|target| {
                        target.symbol == request.symbol
                            && target.kind.wire_name() == request.event_type
                    })
                })
                .cloned()
                .collect()
        }
    };
    let orphaned = orphaned_subscriptions(routing, sub_id, &released).await;
    UnsubscribePlan { released, orphaned }
}

/// Takes those routes back.
///
/// Used when the venue refuses the subscribe and when an unsubscribe lands. A
/// refused subscribe that keeps its route means the subscription receives
/// events for a symbol it was told it does not have, as soon as anybody else
/// subscribes to that symbol — and `add_symbols` has already given up its
/// reservation by then, so nothing else would ever clean it up.
async fn forget_routes(
    routing: &Arc<RwLock<EventRouting>>,
    sub_id: u32,
    subscriptions: &[FeedSubscription],
) {
    let mut routing = routing.write().await;
    for route in routes_of(subscriptions) {
        if let Some(subs) = routing.routes.get_mut(&route) {
            subs.remove(&sub_id);
            if subs.is_empty() {
                routing.routes.remove(&route);
            }
        }
    }
}

/// The routes a set of subscription requests covers.
///
/// A request whose event type this crate does not model has no route: nothing
/// could deliver it, and inventing a key would leave an entry nothing ever
/// removes.
fn routes_of(subscriptions: &[FeedSubscription]) -> Vec<(String, EventKind)> {
    subscriptions
        .iter()
        .filter_map(|sub| {
            EventKind::ALL
                .iter()
                .find(|kind| kind.wire_name() == sub.event_type)
                .map(|kind| (sub.symbol.clone(), *kind))
        })
        .collect()
}

/// Runs one connection until it is lost or the owner goes away.
#[allow(clippy::too_many_arguments)]
async fn run_connection(
    client: &mut DXLinkClient,
    channel_id: u32,
    commands: &mut mpsc::Receiver<DXLinkCommand>,
    shutdown: &mut oneshot::Receiver<()>,
    forwarder: &mut tokio::task::JoinHandle<()>,
    routing: &Arc<RwLock<EventRouting>>,
    configured: &mut BTreeSet<EventKind>,
    dxlink_drops: &Arc<AtomicU64>,
) -> Ended {
    loop {
        // Sampled, not pushed: dxlink counts its own losses and this is the
        // only way to read them without reaching into its hot path. Sampling
        // here covers a busy loop; the tick below covers a quiet one, so the
        // figure a snapshot marker reads is never more than one interval
        // stale.
        dxlink_drops.store(client.dropped_event_count(), Ordering::Relaxed);

        let cmd = tokio::select! {
            biased;
            () = tokio::time::sleep(DROP_MIRROR_INTERVAL) => {
                dxlink_drops.store(client.dropped_event_count(), Ordering::Relaxed);
                continue;
            }
            _ = &mut *shutdown => {
                debug!("Quote streamer owner dropped, disconnecting");
                return Ended::Owner;
            }
            // The event stream closing is dxlink saying the session is over.
            // It is the only signal that arrives without this client writing
            // anything, so it is what makes a drop visible to a consumer that
            // only reads — the ordinary shape of a market-data client, and the
            // case a write-failure check can never see.
            joined = &mut *forwarder => {
                match joined {
                    Ok(()) => debug!("The DXLink event stream closed; the session is over"),
                    // A forwarder that died is not a venue problem, and
                    // reconnecting will not fix it: the next one runs the same
                    // code over the same routing. Saying so is the difference
                    // between a diagnosable bug and a stream that quietly
                    // reconnects forever.
                    //
                    // Classification only. `JoinError`'s Display renders the
                    // panic payload, and a panic message can quote whatever
                    // the task was holding — here, market data.
                    Err(e) => warn!(
                        "The event forwarding task ended abnormally (panicked: {}, cancelled: {})",
                        e.is_panic(),
                        e.is_cancelled()
                    ),
                }
                return Ended::ConnectionLost;
            }
            cmd = commands.recv() => match cmd {
                Some(cmd) => cmd,
                None => return Ended::Owner,
            },
        };

        match cmd {
            // The channel id in the command is the one the handle was built
            // with. After a reconnect that number is stale, and only this loop
            // knows the live one.
            DXLinkCommand::Subscribe(subscriptions, kinds, sub_id, held, ack) => {
                // A subscription whose sender is gone was closed while this
                // was queued. Subscribing now would record a route nothing
                // delivers and put a series on the wire nothing takes off.
                if !subscription_is_open(routing, sub_id).await {
                    answer(
                        ack,
                        Err(TastyTradeError::Streaming(
                            "the subscription was closed before the venue was asked; nothing \
                             was subscribed"
                                .to_string(),
                        )),
                    );
                    continue;
                }

                // Likewise a target the subscription withdrew while this was
                // queued. All or nothing: subscribing the rest and reporting
                // success would hide which series went missing.
                if let Some(symbol) = withdrawn_request(&held, &subscriptions) {
                    answer(
                        ack,
                        Err(TastyTradeError::Streaming(format!(
                            "{symbol} was removed from the subscription before the venue was \
                             asked; nothing was subscribed"
                        ))),
                    );
                    continue;
                }

                // The venue refuses a subscription to an event type the
                // channel was not configured for, and only this loop knows
                // what the live channel is configured for. Nothing is
                // hardcoded: the channel ends up carrying exactly the types
                // the subscriptions asked for, and nothing else.
                if let Err(e) = ensure_configured(client, channel_id, configured, &kinds).await {
                    let lost = matches!(&e, TastyTradeError::Connection(_));
                    answer(ack, Err(e));
                    if lost {
                        return Ended::ConnectionLost;
                    }
                    continue;
                }

                record_routes(routing, sub_id, &subscriptions).await;

                match client.subscribe(channel_id, subscriptions.clone()).await {
                    Ok(()) => answer(ack, Ok(())),
                    Err(e) => {
                        let lost = is_connection_lost(&e);
                        error!("Error subscribing to symbols: {}", e);

                        // The route was recorded before the write, so a
                        // refused write has to take it back.
                        forget_routes(routing, sub_id, &subscriptions).await;

                        answer(
                            ack,
                            Err(TastyTradeError::Streaming(format!(
                                "the venue refused the subscription: {e}"
                            ))),
                        );
                        if lost {
                            return Ended::ConnectionLost;
                        }
                    }
                }
            }
            DXLinkCommand::Unsubscribe(subscriptions, sub_id, still_held, ack) => {
                // Every subscription on this streamer shares one feed channel,
                // so a `FEED_SUBSCRIPTION { remove }` stops the venue sending
                // that series to **all** of them. Only the ones nobody else
                // still wants may be taken off the wire; the rest are dropped
                // locally, which is all this subscription asked for. And a
                // series this subscription re-reserved while the command was
                // queued is not released at all: see `plan_unsubscribe`.
                let plan =
                    plan_unsubscribe(routing, sub_id, still_held.as_deref(), &subscriptions).await;

                if plan.orphaned.is_empty() {
                    // Nothing for the venue: either somebody else is still
                    // watching all of it, or this subscription still is.
                    // Taking back the routes it did release is the whole job.
                    forget_routes(routing, sub_id, &plan.released).await;
                    answer(ack, Ok(()));
                    continue;
                }

                // The venue is told first. Dropping the route before knowing
                // the unsubscribe landed leaves a subscription running with
                // nowhere to deliver, and the local state that could have
                // retried it already gone.
                let outcome = client.unsubscribe(channel_id, plan.orphaned).await;

                if outcome.is_ok() {
                    forget_routes(routing, sub_id, &plan.released).await;
                }

                match outcome {
                    Ok(()) => answer(ack, Ok(())),
                    Err(e) => {
                        let lost = is_connection_lost(&e);
                        error!("Error unsubscribing from symbols: {}", e);
                        answer(
                            ack,
                            Err(TastyTradeError::Streaming(format!(
                                "the venue refused the unsubscribe: {e}"
                            ))),
                        );
                        if lost {
                            return Ended::ConnectionLost;
                        }
                    }
                }
            }
            DXLinkCommand::AddEventSender(subscription_id, sender) => {
                let mut routing = routing.write().await;
                routing
                    .senders
                    .entry(subscription_id)
                    .or_default()
                    .push(sender);
                debug!("Added event sender for subscription {}", subscription_id);
            }
            DXLinkCommand::RemoveEventSender(subscription_id) => {
                let mut routing = routing.write().await;
                routing.senders.remove(&subscription_id);
                routing.routes.retain(|_, subs| {
                    subs.remove(&subscription_id);
                    !subs.is_empty()
                });
                debug!("Removed event senders for subscription {}", subscription_id);
            }
        }
    }
}

/// Whether a dxlink failure means the socket is gone rather than the request
/// being wrong.
///
/// A refused subscription is the venue disagreeing with one request; a dead
/// socket makes every later request pointless. Only the second is worth
/// reconnecting for.
///
/// The second of two drop signals, and the faster one when this client is
/// writing: a failed write says so immediately, where the closing event stream
/// says so when dxlink's reader notices. A consumer that only reads has just
/// the stream, which is why both exist.
fn is_connection_lost(error: &dxlink::DXLinkError) -> bool {
    matches!(
        error,
        dxlink::DXLinkError::Connection(_) | dxlink::DXLinkError::WebSocket(_)
    )
}

/// Records why no further attempts will be made.
async fn terminal(state: &Arc<RwLock<ConnectionState>>, reason: String) {
    warn!("Quote stream gave up: {reason}");
    *state.write().await = ConnectionState::Disconnected { reason };
}

/// Waits out the backoff for the next attempt.
///
/// Returns false when the policy says to stop, or when the streamer was
/// dropped while waiting.
async fn schedule(
    policy: &BackoffPolicy,
    attempt: &mut u32,
    state: &Arc<RwLock<ConnectionState>>,
    shutdown: &mut oneshot::Receiver<()>,
) -> bool {
    *attempt = attempt.saturating_add(1);

    // Jitter source. A clock read is enough entropy to stop a fleet of clients
    // synchronising on the same venue restart, and it costs no dependency.
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.subsec_nanos() as u64)
        .unwrap_or(0);

    let Some(delay) = policy.delay_for(*attempt, nanos) else {
        terminal(state, format!("gave up after {} attempts", *attempt - 1)).await;
        return false;
    };

    debug!("Quote stream reconnecting, attempt {attempt} in {delay:?}");
    *state.write().await = ConnectionState::Reconnecting {
        attempt: *attempt,
        delay,
    };

    // Cancellable: a caller who drops the streamer should not wait out a
    // thirty-second backoff for a task nobody is listening to.
    tokio::select! {
        _ = &mut *shutdown => false,
        _ = tokio::time::sleep(delay) => true,
    }
}

/// Owns the connection for the streamer's whole life, replacing it when lost.
#[allow(clippy::too_many_arguments)]
async fn supervise(
    tasty: TastyTrade,
    policy: BackoffPolicy,
    first: LiveConnection,
    mut commands: mpsc::Receiver<DXLinkCommand>,
    mut shutdown: oneshot::Receiver<()>,
    routing: Arc<RwLock<EventRouting>>,
    registry: Registry,
    state: Arc<RwLock<ConnectionState>>,
    progress: CandleProgress,
    drained: Arc<Notify>,
    history: Arc<Notify>,
) {
    let mut attempt = 0u32;
    let mut next = Some(first);
    // One counter for the whole streamer, not per connection: a reconnect
    // creates a new client whose count starts at zero, and a generation that
    // spans the two would read that as the counter going backwards. Resetting
    // the baseline on reconnect, which `invalidate_history` does, is what keeps
    // the comparison honest.
    let dxlink_drops = Arc::new(AtomicU64::new(0));

    loop {
        let LiveConnection {
            mut client,
            channel_id,
            events,
        } = match next.take() {
            Some(connection) => connection,
            None => match connect_dxlink(&tasty).await {
                Ok(connection) => connection,
                Err(e) => {
                    // A rejected session is not a dropped socket: presenting
                    // the same credentials again will be refused again.
                    if !policy.should_retry(&e) {
                        terminal(&state, format!("reconnect refused: {e}")).await;
                        return;
                    }
                    if !schedule(&policy, &mut attempt, &state, &mut shutdown).await {
                        return;
                    }
                    continue;
                }
            },
        };

        // Forwarding starts before anything is subscribed, and `routing` is
        // the one that survived the reconnect, so an event that arrives the
        // instant the replay lands already has somewhere to go.
        let saw_event = Arc::new(AtomicBool::new(false));
        // A new client counts its own losses from zero, so the mirror has to
        // start there too or the first generation of every reconnect would be
        // measured against the previous connection's total.
        dxlink_drops.store(0, Ordering::Relaxed);
        let mut forwarder = tokio::spawn(forward_events(
            events,
            routing.clone(),
            progress.clone(),
            saw_event.clone(),
            drained.clone(),
            dxlink_drops.clone(),
            history.clone(),
        ));

        // A fresh channel is configured for nothing. What it ends up carrying
        // is the union of what the subscriptions ask for, and no more.
        let mut configured: BTreeSet<EventKind> = BTreeSet::new();

        let restored = replay(
            &mut client,
            channel_id,
            &registry,
            &progress,
            &mut configured,
        )
        .await;
        // The first replay's bars start arriving here, before `run_connection`
        // reaches its own sampling loop, so without this the generation they
        // open would be measured against the previous connection's total.
        dxlink_drops.store(client.dropped_event_count(), Ordering::Relaxed);

        if restored {
            // Connected is claimed only once what was being watched is watched
            // again. Reporting it before restoration leaves a caller believing
            // they are receiving events they are not.
            *state.write().await = ConnectionState::Connected;
        }

        let ended = if restored {
            run_connection(
                &mut client,
                channel_id,
                &mut commands,
                &mut shutdown,
                &mut forwarder,
                &routing,
                &mut configured,
                &dxlink_drops,
            )
            .await
        } else {
            warn!("Could not restore every subscription; reconnecting");
            Ended::ConnectionLost
        };

        forwarder.abort();
        // Aborting asks; joining knows. The old forwarder can still be inside
        // an event, and letting it run on while the generation below is bumped
        // would let a superseded event mutate the new generation or land
        // behind the marker that announces it. Skipped when the handle has
        // already been awaited to completion, which polling again would panic
        // on.
        if !forwarder.is_finished() {
            let _ = (&mut forwarder).await;
        }

        // Before any backoff, and before the state even says so: the replay
        // that was in progress stops being current the moment the socket does,
        // and a consumer asking `history_loaded` during a thirty-second
        // backoff must not be told the old answer.
        if matches!(ended, Ended::ConnectionLost) {
            open_generations_for_the_next_connection(&progress, &routing, &history, &dxlink_drops)
                .await;
        }

        // Why the session ended, when dxlink observed it rather than this
        // client. Logged, not carried into `ConnectionState`: the text comes
        // from whatever the socket reported, and the state value is public and
        // meant to be safe to show anywhere.
        if let Some(reason) = client.disconnect_reason() {
            debug!("DXLink reported the session ended: {reason}");
        }

        if let Err(e) = client.disconnect().await {
            debug!("Error disconnecting the previous DXLink client: {e}");
        }

        match ended {
            Ended::Owner => {
                *state.write().await = ConnectionState::Disconnected {
                    reason: "the streamer was dropped".to_string(),
                };
                debug!("DXLink supervisor terminated");
                return;
            }
            Ended::ConnectionLost => {
                // Reset only for a connection that actually delivered. A
                // handshake proves the venue accepts sockets, not that it
                // sends data; resetting on one lets an accepting-but-silent
                // venue loop forever at attempt one.
                if saw_event.load(Ordering::Relaxed) {
                    attempt = 0;
                }
                if !schedule(&policy, &mut attempt, &state, &mut shutdown).await {
                    return;
                }
            }
        }
    }
}

impl Drop for QuoteStreamer {
    fn drop(&mut self) {
        // No per-subscription unsubscribe loop here. Disconnecting ends every
        // subscription on the far side anyway, and close_sub is async now, so
        // it could not be awaited from Drop even if it were worth doing.

        // A oneshot send is synchronous, so this works outside a Tokio runtime
        // where tokio::spawn would panic, and it cannot be discarded by a full
        // command queue. Only the owner holds it, so nobody else can send it.
        if let Some(shutdown) = self.shutdown.take() {
            let _ = shutdown.send(());
        }
    }
}

#[cfg(test)]
mod lifecycle_tests {
    use super::*;
    use crate::api::quote_streaming::DxFeedSymbol;

    /// The regression #19 exists for: create_sub stores one clone of the
    /// subscription and hands the caller another, so a per-copy Vec meant
    /// add_symbols updated the caller's while close_sub read the streamer's,
    /// which stayed empty. Unsubscribes were derived from that empty list and
    /// never sent, leaving the subscription alive on the venue.
    #[tokio::test]
    async fn both_copies_of_a_subscription_see_the_same_symbols() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);
        let loop_handle = spawn_command_loop(rx, || Ok(()));

        let sub = streamer
            .create_sub([EventKind::Quote])
            .await
            .expect("the streamer is open");
        sub.add_symbols(&[
            DxFeedSymbol("AAPL".to_string()),
            DxFeedSymbol("MSFT".to_string()),
        ])
        .await
        .expect("subscribing succeeds");

        // The streamer's own copy sees them, which is what close_sub reads.
        {
            let stored = targets_of(
                &streamer
                    .subscription_map
                    .get(&sub.id)
                    .expect("the streamer kept a copy")
                    .targets,
            );
            assert_eq!(stored.len(), 2, "the streamer's copy must see the symbols");
            assert!(stored.iter().any(|target| target.symbol == "AAPL"));
        }

        // Both the streamer and the subscription hold a command sender, so
        // the loop only ends when both are gone. That shared ownership is the
        // same property that made shutdown-by-command-queue wrong.
        drop(sub);
        drop(streamer);
        let sent = loop_handle.await.expect("the stand-in loop finishes");
        assert!(sent.contains(&"AAPL".to_string()));
        assert!(sent.contains(&"MSFT".to_string()));
    }

    /// Reaching the command queue is not the venue accepting. A refusal must
    /// leave nothing recorded, or the symbol is unsubscribed later as though
    /// it had been subscribed.
    #[tokio::test]
    async fn a_refused_subscription_records_nothing() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);
        let _loop_handle = spawn_command_loop(rx, || {
            Err(TastyTradeError::Streaming("venue said no".to_string()))
        });

        let sub = streamer
            .create_sub([EventKind::Quote])
            .await
            .expect("the streamer is open");
        let error = sub
            .add_symbols(&[DxFeedSymbol("AAPL".to_string())])
            .await
            .expect_err("a refused subscription is not a success");

        // The venue's own answer reaches the caller rather than being
        // flattened into a generic failure.
        assert!(format!("{error}").contains("venue said no"), "{error}");
        assert!(
            targets_of(&sub.targets).is_empty(),
            "a refused symbol must not stay reserved"
        );
    }

    /// A set, so asking twice subscribes once.
    #[tokio::test]
    async fn a_repeated_symbol_is_not_subscribed_twice() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);
        let loop_handle = spawn_command_loop(rx, || Ok(()));

        let sub = streamer
            .create_sub([EventKind::Quote])
            .await
            .expect("the streamer is open");
        sub.add_symbols(&[DxFeedSymbol("AAPL".to_string())])
            .await
            .unwrap();
        sub.add_symbols(&[DxFeedSymbol("AAPL".to_string())])
            .await
            .unwrap();

        drop(sub);
        drop(streamer);
        let sent = loop_handle.await.expect("the stand-in loop finishes");
        assert_eq!(
            sent.len(),
            1,
            "the second request had nothing new to say: {sent:?}"
        );
    }

    /// A symbol whose command never left must not be unsubscribed later as
    /// though it had.
    #[tokio::test]
    async fn a_failed_subscribe_records_nothing() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);

        let sub = streamer
            .create_sub([EventKind::Quote])
            .await
            .expect("the streamer is open");
        drop(rx); // the command loop is gone

        sub.add_symbols(&[DxFeedSymbol("AAPL".to_string())])
            .await
            .expect_err("a closed streamer cannot subscribe");

        assert!(
            targets_of(&sub.targets).is_empty(),
            "nothing was subscribed, so nothing may be recorded"
        );
    }

    /// The regression this change exists for: a subscription used to hold a
    /// cloned `QuoteStreamer`, and that clone's `Drop` sent `Disconnect` on the
    /// shared command channel. Dropping a subscription therefore tore down the
    /// connection the real streamer was still using.
    ///
    /// A handle owns no connection, so dropping one has to leave the channel
    /// alive and silent.
    #[tokio::test]
    async fn dropping_a_handle_does_not_disconnect_anyone() {
        let (tx, mut rx) = mpsc::channel::<DXLinkCommand>(8);

        let handle = StreamerHandle {
            commands: Some(tx.clone()),
        };
        let second = handle.clone();

        drop(handle);
        drop(second);

        // Nothing was sent, and the channel is still usable by its owner.
        assert!(
            rx.try_recv().is_err(),
            "dropping a handle must not send a command"
        );
        tx.send(DXLinkCommand::RemoveEventSender(9))
            .await
            .expect("the owner's channel is still alive after handles are dropped");
        assert!(matches!(
            rx.recv().await,
            Some(DXLinkCommand::RemoveEventSender(9))
        ));
    }

    /// `Drop` must not need a Tokio runtime. `try_send` is synchronous;
    /// `tokio::spawn` would panic here, which is what this asserts by simply
    /// not panicking.
    /// Stands in for the command loop: drains commands, answers every one with
    /// `outcome`, and hands back what it saw. Without it `add_symbols` waits
    /// forever, which is the point — a subscription is not confirmed until
    /// something confirms it.
    pub(super) fn spawn_command_loop(
        mut rx: mpsc::Receiver<DXLinkCommand>,
        outcome: fn() -> TastyResult<()>,
    ) -> tokio::task::JoinHandle<Vec<String>> {
        tokio::spawn(async move {
            let mut seen = Vec::new();
            while let Some(cmd) = rx.recv().await {
                match cmd {
                    DXLinkCommand::Subscribe(requests, _, _, _, ack) => {
                        seen.extend(requests.into_iter().map(|r| r.symbol));
                        answer(ack, outcome());
                    }
                    DXLinkCommand::Unsubscribe(_, _, _, ack) => answer(ack, outcome()),
                    _ => {}
                }
            }
            seen
        })
    }

    pub(super) fn streamer_with(
        commands: mpsc::Sender<DXLinkCommand>,
        shutdown: oneshot::Sender<()>,
    ) -> QuoteStreamer {
        QuoteStreamer {
            shutdown: Some(shutdown),
            next_sub_id: 0,
            subscription_map: HashMap::new(),
            dxlink_command_tx: Some(commands),
            registry: Arc::new(Mutex::new(HashMap::new())),
            state: Arc::new(RwLock::new(ConnectionState::Connected)),
            progress: Arc::new(Mutex::new(HashMap::new())),
            drained: Arc::new(Notify::new()),
            history: Arc::new(Notify::new()),
        }
    }

    #[test]
    fn dropping_the_owner_outside_a_runtime_does_not_panic() {
        let (tx, _rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();

        drop(streamer_with(tx, shutdown_tx));

        // Signalled synchronously rather than spawned, which is what a plain
        // #[test] proves by not panicking without a runtime.
        assert!(
            shutdown_rx.try_recv().is_ok(),
            "the owner must signal shutdown on drop"
        );
    }

    /// The command queue is bounded, so shutdown must not travel on it: a full
    /// queue would discard the disconnect and leave the client connected while
    /// its owner is gone.
    #[test]
    fn shutdown_survives_a_full_command_queue() {
        let (tx, _rx) = mpsc::channel::<DXLinkCommand>(1);
        let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();

        tx.try_send(DXLinkCommand::RemoveEventSender(9))
            .expect("the first command fits");
        assert!(
            tx.try_send(DXLinkCommand::RemoveEventSender(9)).is_err(),
            "the queue must actually be full for this test to mean anything"
        );

        drop(streamer_with(tx, shutdown_tx));

        assert!(
            shutdown_rx.try_recv().is_ok(),
            "a full command queue must not be able to swallow the shutdown"
        );
    }
}

#[cfg(test)]
mod reconnect_tests {
    use super::lifecycle_tests::{spawn_command_loop, streamer_with};
    use super::*;
    use crate::api::quote_streaming::DxFeedSymbol;
    use dxlink::events::QuoteEvent;
    use std::time::Duration;

    fn quote(symbol: &str) -> MarketEvent {
        MarketEvent::Quote(QuoteEvent {
            event_type: "Quote".to_string(),
            event_symbol: symbol.to_string(),
            bid_price: 1.0,
            ask_price: 2.0,
            bid_size: 1.0,
            ask_size: 1.0,
        })
    }

    /// A target for a symbol under one event type, with no candle history.
    fn target(kind: EventKind, symbol: &str) -> FeedTarget {
        FeedTarget {
            kind,
            symbol: symbol.to_string(),
            from_time: None,
        }
    }

    /// No candle has been delivered yet.
    fn no_progress() -> CandleProgress {
        Arc::new(Mutex::new(HashMap::new()))
    }

    /// A consumer sink with its own loss counter.
    fn sink(events: mpsc::Sender<Delivery>) -> Subscriber {
        Subscriber {
            events,
            lagged: Arc::new(AtomicU64::new(0)),
            pending: Arc::new(Mutex::new(VecDeque::new())),
        }
    }

    /// The market event inside a delivery.
    fn market_of(delivery: Delivery) -> MarketEvent {
        match delivery {
            Delivery::Market(event) => event,
            Delivery::Marker(marker) => {
                panic!("expected market data, got the marker {:?}", marker.data)
            }
        }
    }

    /// The snapshot marker inside a delivery.
    fn marker_of(delivery: Delivery) -> dxfeed::Event {
        match delivery {
            Delivery::Marker(event) => event,
            Delivery::Market(event) => panic!(
                "expected a snapshot marker, got {} data for {:?}",
                event_kind(&event),
                event_symbol(&event)
            ),
        }
    }

    /// One candle for `symbol` at `time`, carrying `flags` in `eventFlags`.
    fn flagged_candle(symbol: &str, time: i64, flags: i64) -> MarketEvent {
        let MarketEvent::Candle(mut bar) = candle(symbol, time) else {
            unreachable!("candle builds a candle")
        };
        bar.event_flags = flags;
        MarketEvent::Candle(bar)
    }

    /// One candle for `symbol` at `time`.
    fn candle(symbol: &str, time: i64) -> MarketEvent {
        let MarketEvent::Candle(mut candle) = every_event_type(symbol)
            .into_iter()
            .find(|event| matches!(event, MarketEvent::Candle(_)))
            .expect("a candle")
        else {
            unreachable!("filtered on the variant")
        };
        candle.time = time;
        MarketEvent::Candle(candle)
    }

    /// One event of every type the feed models, all for `symbol`.
    fn every_event_type(symbol: &str) -> Vec<MarketEvent> {
        use dxlink::events::*;

        let sym = || symbol.to_string();
        vec![
            quote(symbol),
            MarketEvent::Trade(TradeEvent {
                event_type: "Trade".to_string(),
                event_symbol: sym(),
                price: 1.0,
                size: 1.0,
                day_volume: 10.0,
            }),
            MarketEvent::TradeETH(TradeETHEvent {
                event_type: "TradeETH".to_string(),
                event_symbol: sym(),
                event_time: 0,
                time: 0,
                time_nano_part: 0,
                sequence: 0,
                exchange_code: "Q".to_string(),
                price: 1.0,
                change: 0.0,
                size: 1.0,
                day_id: 0,
                day_volume: 0.0,
                day_turnover: 0.0,
                tick_direction: "Up".to_string(),
                extended_trading_hours: true,
            }),
            MarketEvent::Greeks(GreeksEvent {
                event_type: "Greeks".to_string(),
                event_symbol: sym(),
                delta: 0.5,
                gamma: 0.1,
                theta: -0.05,
                vega: 0.2,
                rho: 0.03,
                volatility: 0.25,
            }),
            MarketEvent::Candle(CandleEvent {
                event_type: "Candle".to_string(),
                event_symbol: sym(),
                event_time: 0,
                event_flags: 0,
                index: 0,
                time: 1_700_000_000_000,
                sequence: 0,
                count: 1,
                open: 1.0,
                high: 2.0,
                low: 0.5,
                close: 1.5,
                volume: 100.0,
                vwap: 1.4,
                bid_volume: 50.0,
                ask_volume: 50.0,
                imp_volatility: 0.2,
                open_interest: 0.0,
            }),
            MarketEvent::Summary(SummaryEvent {
                event_type: "Summary".to_string(),
                event_symbol: sym(),
                event_time: 0,
                day_id: 0,
                day_open_price: 0.0,
                day_high_price: 0.0,
                day_low_price: 0.0,
                day_close_price: 0.0,
                day_close_price_type: "Final".to_string(),
                prev_day_id: 0,
                prev_day_close_price: 0.0,
                prev_day_close_price_type: "Final".to_string(),
                prev_day_volume: 0.0,
                open_interest: 0.0,
            }),
            MarketEvent::TimeAndSale(TimeAndSaleEvent {
                event_type: "TimeAndSale".to_string(),
                event_symbol: sym(),
                event_time: 0,
                event_flags: 0,
                index: 0,
                time: 0,
                time_nano_part: 0,
                sequence: 0,
                exchange_code: "Q".to_string(),
                price: 1.0,
                size: 1.0,
                bid_price: 0.9,
                ask_price: 1.1,
                exchange_sale_conditions: String::new(),
                trade_through_exempt: String::new(),
                aggressor_side: "Buy".to_string(),
                spread_leg: false,
                extended_trading_hours: false,
                valid_tick: true,
                sale_type: String::new(),
                buyer: String::new(),
                seller: String::new(),
            }),
            MarketEvent::Profile(ProfileEvent {
                event_type: "Profile".to_string(),
                event_symbol: sym(),
                event_time: 0,
                description: "Apple".to_string(),
                short_sale_restriction: "Inactive".to_string(),
                trading_status: "Active".to_string(),
                status_reason: String::new(),
                halt_start_time: 0,
                halt_end_time: 0,
                high_limit_price: 0.0,
                low_limit_price: 0.0,
                high_52_week_price: 0.0,
                low_52_week_price: 0.0,
                beta: 0.0,
                earnings_per_share: 0.0,
                dividend_frequency: 0.0,
                ex_dividend_amount: 0.0,
                ex_dividend_day_id: 0,
                shares: 0.0,
                free_float: 0.0,
            }),
            MarketEvent::Underlying(UnderlyingEvent {
                event_type: "Underlying".to_string(),
                event_symbol: sym(),
                event_time: 0,
                event_flags: 0,
                index: 0,
                time: 0,
                sequence: 0,
                volatility: 0.2,
                front_volatility: 0.21,
                back_volatility: 0.19,
                call_volume: 10.0,
                put_volume: 8.0,
                put_call_ratio: 0.8,
            }),
            MarketEvent::TheoPrice(TheoPriceEvent {
                event_type: "TheoPrice".to_string(),
                event_symbol: sym(),
                event_time: 0,
                event_flags: 0,
                index: 0,
                time: 0,
                sequence: 0,
                price: 1.0,
                underlying_price: 100.0,
                delta: 0.5,
                gamma: 0.1,
                dividend: 0.0,
                interest: 0.0,
            }),
            MarketEvent::Series(SeriesEvent {
                event_type: "Series".to_string(),
                event_symbol: sym(),
                event_time: 0,
                event_flags: 0,
                index: 0,
                time: 0,
                sequence: 0,
                expiration: 20_260_918,
                volatility: 0.2,
                call_volume: 10.0,
                put_volume: 8.0,
                put_call_ratio: 0.8,
                forward_price: 100.0,
                dividend: 0.0,
                interest: 0.0,
            }),
        ]
    }

    fn policy() -> BackoffPolicy {
        BackoffPolicy {
            initial: Duration::from_millis(10),
            max_delay: Duration::from_millis(40),
            max_attempts: Some(2),
            jitter: 0.0,
        }
    }

    /// A symbol is restored under exactly the event types it was subscribed
    /// with. Replaying a Quote-only subscription as Quote+Trade would start a
    /// stream the caller never asked for and cannot see.
    #[test]
    fn a_replay_asks_for_the_event_types_the_subscription_had() {
        let requests = feed_subscriptions(&[
            target(EventKind::Quote, "AAPL"),
            target(EventKind::Greeks, "AAPL"),
            target(EventKind::Quote, "MSFT"),
            target(EventKind::Greeks, "MSFT"),
        ]);

        assert_eq!(requests.len(), 4, "two symbols by two event types");
        let types: BTreeSet<&str> = requests.iter().map(|r| r.event_type.as_str()).collect();
        assert_eq!(
            types,
            BTreeSet::from(["Greeks", "Quote"]),
            "Trade was not asked for: {types:?}"
        );
    }

    /// The replay is derived from the confirmed symbols, so a subscription
    /// that never got any contributes nothing.
    #[test]
    fn a_subscription_with_no_confirmed_symbols_is_not_replayed() {
        let registry: Registry = Arc::new(Mutex::new(HashMap::new()));
        registry.lock().unwrap().insert(
            7,
            SubscriptionRecord {
                kinds: BTreeSet::from([EventKind::Quote]),
                targets: Arc::new(Mutex::new(BTreeSet::new())),
            },
        );

        assert!(
            pending_replay(&registry, &no_progress()).is_empty(),
            "a refused subscribe records nothing, so there is nothing to restore"
        );
    }

    /// A closed subscription must not come back on the next connection: the
    /// caller unsubscribed it, and a reconnect is not a reason to overrule
    /// that.
    #[tokio::test]
    async fn a_closed_subscription_is_not_replayed() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);
        let _loop_handle = spawn_command_loop(rx, || Ok(()));

        let sub = streamer
            .create_sub([EventKind::Quote])
            .await
            .expect("the streamer is open");
        sub.add_symbols(&[DxFeedSymbol("AAPL".to_string())])
            .await
            .expect("subscribing succeeds");

        let pending = pending_replay(&streamer.registry, &no_progress());
        assert_eq!(pending.len(), 1, "a live subscription is restored");
        assert_eq!(pending[0].1[0].symbol, "AAPL");

        streamer.close_sub(sub.id).await.expect("closing succeeds");

        assert!(
            pending_replay(&streamer.registry, &no_progress()).is_empty(),
            "a closed subscription must not be resubscribed by a reconnect"
        );
    }

    /// The routing registry outlives a connection, so an event arriving right
    /// after a reconnect already has somewhere to go. This drives the
    /// forwarding side of that: a route registered before the connection
    /// existed still delivers.
    #[tokio::test]
    async fn a_forwarded_event_reaches_the_subscription_registered_for_it() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let (sub_tx, mut sub_rx) = mpsc::channel::<Delivery>(4);
        {
            let mut routing = routing.write().await;
            routing.senders.insert(1, vec![sink(sub_tx)]);
            routing
                .routes
                .insert(("AAPL".to_string(), EventKind::Quote), HashSet::from([1]));
        }

        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(4);
        let saw_event = Arc::new(AtomicBool::new(false));
        let forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            no_progress(),
            saw_event.clone(),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        events_tx
            .send(quote("AAPL"))
            .await
            .expect("the feed accepts");
        let received = sub_rx
            .recv()
            .await
            .expect("the subscription is delivered to");
        assert!(matches!(market_of(received), MarketEvent::Quote(q) if q.event_symbol == "AAPL"));

        // A symbol nobody is subscribed to is dropped rather than panicking or
        // being broadcast to everyone.
        events_tx
            .send(quote("TSLA"))
            .await
            .expect("the feed accepts");
        assert!(
            tokio::time::timeout(Duration::from_millis(50), sub_rx.recv())
                .await
                .is_err(),
            "an unrouted symbol must not be delivered"
        );

        assert!(
            saw_event.load(Ordering::Relaxed),
            "an event that arrived is the milestone the backoff resets on"
        );
        forwarder.abort();
    }

    /// A subscription the venue refused must not keep its route. `add_symbols`
    /// gives up its reservation on failure, so nothing else would ever remove
    /// it, and the subscription would start receiving events for that symbol
    /// as soon as anybody else subscribed to it.
    #[tokio::test]
    async fn a_refused_subscribe_takes_its_route_back() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let wanted = feed_subscriptions(&[target(EventKind::Quote, "AAPL")]);

        // Two subscriptions ask for the same symbol; one of them is refused.
        record_routes(&routing, 3, &wanted).await;
        record_routes(&routing, 4, &wanted).await;
        forget_routes(&routing, 3, &wanted).await;

        let routes = routing.read().await;
        let subs = routes
            .routes
            .get(&("AAPL".to_string(), EventKind::Quote))
            .expect("the accepted subscription still holds the symbol");
        assert!(
            !subs.contains(&3),
            "a refused subscribe must not leave a route behind"
        );
        assert!(subs.contains(&4), "the accepted one keeps its route");
    }

    /// The symbol goes with the last subscription that wanted it, so an
    /// unrouted event is dropped rather than delivered to a stale id.
    #[tokio::test]
    async fn the_last_route_removed_takes_the_symbol_with_it() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let wanted = feed_subscriptions(&[target(EventKind::Quote, "AAPL")]);

        record_routes(&routing, 3, &wanted).await;
        forget_routes(&routing, 3, &wanted).await;

        assert!(
            routing.read().await.routes.is_empty(),
            "an empty set must not be left behind as a route"
        );
    }

    /// The whole point of #67. A consumer that only reads — the ordinary shape
    /// of a market-data client — never issues a command, so a write-failure
    /// check can never see its connection die. dxlink 0.3 closes the event
    /// stream when the session ends, and this is the supervisor noticing.
    #[tokio::test]
    async fn a_closed_event_stream_ends_the_connection_without_any_write() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(4);
        let saw_event = Arc::new(AtomicBool::new(false));
        let mut forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            no_progress(),
            saw_event.clone(),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        // Never connected: the point is that nothing is written to it.
        let mut client = DXLinkClient::new("wss://127.0.0.1:1", "unused");
        let (_commands_tx, mut commands_rx) = mpsc::channel::<DXLinkCommand>(4);
        let (_shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();

        // The session ends: dxlink drops its side of the event stream.
        drop(events_tx);

        let ended = tokio::time::timeout(
            Duration::from_secs(2),
            run_connection(
                &mut client,
                1,
                &mut commands_rx,
                &mut shutdown_rx,
                &mut forwarder,
                &routing,
                &mut BTreeSet::new(),
                &Arc::new(AtomicU64::new(0)),
            ),
        )
        .await
        .expect("the closing stream must end the connection, not be waited on forever");

        assert!(
            matches!(ended, Ended::ConnectionLost),
            "a closed event stream is a lost connection"
        );
    }

    /// A forwarder that dies is not a venue problem, and the next connection
    /// runs the same code over the same routing. It still ends the connection,
    /// but it must not do so silently — that is the difference between a
    /// diagnosable bug and a stream that reconnects forever for no visible
    /// reason.
    #[tokio::test]
    async fn a_forwarder_that_panics_is_reported_rather_than_read_as_a_venue_drop() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let mut forwarder = tokio::spawn(async { panic!("the forwarder died") });

        let mut client = DXLinkClient::new("wss://127.0.0.1:1", "unused");
        let (_commands_tx, mut commands_rx) = mpsc::channel::<DXLinkCommand>(4);
        let (_shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();

        let ended = tokio::time::timeout(
            Duration::from_secs(2),
            run_connection(
                &mut client,
                1,
                &mut commands_rx,
                &mut shutdown_rx,
                &mut forwarder,
                &routing,
                &mut BTreeSet::new(),
                &Arc::new(AtomicU64::new(0)),
            ),
        )
        .await
        .expect("a dead forwarder must end the connection, not be waited on");

        assert!(matches!(ended, Ended::ConnectionLost));
    }

    /// The owner still wins over a live connection: dropping the streamer must
    /// end the supervisor rather than wait for the venue to do something.
    #[tokio::test]
    async fn the_owner_still_takes_precedence_over_a_live_stream() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let (_events_tx, events_rx) = mpsc::channel::<MarketEvent>(4);
        let mut forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            no_progress(),
            Arc::new(AtomicBool::new(false)),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        let mut client = DXLinkClient::new("wss://127.0.0.1:1", "unused");
        let (_commands_tx, mut commands_rx) = mpsc::channel::<DXLinkCommand>(4);
        let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();

        drop(shutdown_tx);

        let ended = tokio::time::timeout(
            Duration::from_secs(2),
            run_connection(
                &mut client,
                1,
                &mut commands_rx,
                &mut shutdown_rx,
                &mut forwarder,
                &routing,
                &mut BTreeSet::new(),
                &Arc::new(AtomicU64::new(0)),
            ),
        )
        .await
        .expect("a dropped owner ends the connection promptly");

        assert!(matches!(ended, Ended::Owner));
    }

    /// Every one of the eleven types the feed models has to reach the caller.
    /// Eight of them used to be logged and dropped, which for `Candle` meant
    /// there was no route to historical bars anywhere in this crate, and for
    /// `TradeETH` no route to an extended-hours price.
    #[tokio::test]
    async fn all_eleven_event_types_convert_and_are_delivered() {
        let events = every_event_type("AAPL");
        assert_eq!(events.len(), EventKind::ALL.len());

        let (tx, rx) = mpsc::channel::<Delivery>(32);
        let (_unused_tx, event_receiver) = flume::unbounded();
        let mut subscription = QuoteSubscription {
            id: SubscriptionId(0),
            streamer: StreamerHandle { commands: None },
            kinds: EventKind::ALL.into_iter().collect(),
            event_receiver,
            dxlink_receiver: rx,
            targets: Arc::new(Mutex::new(BTreeSet::new())),
            lagged: Arc::new(AtomicU64::new(0)),
            progress: Arc::new(Mutex::new(HashMap::new())),
            drained: Arc::new(Notify::new()),
            history: Arc::new(Notify::new()),
            closed: Arc::new(AtomicBool::new(false)),
        };

        for event in &events {
            // Every variant knows its own symbol and its own kind.
            assert_eq!(event_symbol(event), Some("AAPL"), "{:?}", event_kind(event));
            tx.send(Delivery::Market(event.clone()))
                .await
                .expect("the feed accepts");
        }

        let mut seen = BTreeSet::new();
        for _ in 0..events.len() {
            let event = tokio::time::timeout(Duration::from_secs(2), subscription.get_event())
                .await
                .expect("no modelled event may stall the reader")
                .expect("every modelled event is readable");
            assert_eq!(event.sym, "AAPL");
            seen.insert(event.data.kind());
        }

        assert_eq!(
            seen,
            EventKind::ALL.into_iter().collect::<BTreeSet<_>>(),
            "every event type must arrive as its own variant"
        );
    }

    /// The tripwire that produced this change. `MarketEvent` is not
    /// `#[non_exhaustive]`, so a twelfth variant breaks the build rather than
    /// being silently dropped — and this pins the count so the list cannot
    /// quietly shrink either.
    #[test]
    fn the_kinds_this_crate_routes_cover_every_variant_the_feed_models() {
        let kinds: BTreeSet<EventKind> = every_event_type("AAPL").iter().map(event_kind).collect();

        assert_eq!(kinds, EventKind::ALL.into_iter().collect::<BTreeSet<_>>());
    }

    /// The routing fix candles need. Two periods of one underlying are two
    /// streamer symbols, so a subscription watching five-minute bars must not
    /// be handed the hourly ones. The incoming symbols are spelled out as the
    /// venue sends them rather than generated with the request's formatter,
    /// so the hourly one exercises the count-of-one form (#137).
    #[tokio::test]
    async fn two_candle_periods_of_one_underlying_do_not_cross_deliver() {
        let five = CandlePeriod::minutes(5).expect("a period");
        let hour = CandlePeriod::hours(1).expect("a period");

        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let (five_tx, mut five_rx) = mpsc::channel::<Delivery>(4);
        let (hour_tx, mut hour_rx) = mpsc::channel::<Delivery>(4);
        {
            let mut routing = routing.write().await;
            routing.senders.insert(1, vec![sink(five_tx)]);
            routing.senders.insert(2, vec![sink(hour_tx)]);
        }

        record_routes(
            &routing,
            1,
            &feed_subscriptions(&[FeedTarget {
                kind: EventKind::Candle,
                symbol: five.streamer_symbol("AAPL"),
                from_time: Some(0),
            }]),
        )
        .await;
        record_routes(
            &routing,
            2,
            &feed_subscriptions(&[FeedTarget {
                kind: EventKind::Candle,
                symbol: hour.streamer_symbol("AAPL"),
                from_time: Some(0),
            }]),
        )
        .await;

        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(4);
        let progress = no_progress();
        let forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            progress.clone(),
            Arc::new(AtomicBool::new(false)),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        // The venue's spelling, not ours: `{=h}` for one hour.
        events_tx
            .send(candle("AAPL{=h}", 3_600_000))
            .await
            .expect("the feed accepts");
        let delivered = hour_rx.recv().await.expect("the hourly subscription");
        assert_eq!(event_symbol(&market_of(delivered)), Some("AAPL{=h}"));
        assert!(
            tokio::time::timeout(Duration::from_millis(50), five_rx.recv())
                .await
                .is_err(),
            "the five-minute subscription must not see an hourly bar"
        );

        events_tx
            .send(candle("AAPL{=5m}", 300_000))
            .await
            .expect("the feed accepts");
        let delivered = five_rx.recv().await.expect("the five-minute subscription");
        assert_eq!(event_symbol(&market_of(delivered)), Some("AAPL{=5m}"));
        assert!(
            tokio::time::timeout(Duration::from_millis(50), hour_rx.recv())
                .await
                .is_err(),
            "the hourly subscription must not see a five-minute bar"
        );

        // A quote for the bare underlying belongs to neither.
        events_tx
            .send(quote("AAPL"))
            .await
            .expect("the feed accepts");
        assert!(
            tokio::time::timeout(Duration::from_millis(50), five_rx.recv())
                .await
                .is_err()
        );

        // And each bar was recorded under its own series, which is what a
        // reconnect resumes from.
        let seen = progress.lock().expect("not poisoned in tests").clone();
        assert_eq!(
            seen.get(&(1, "AAPL{=5m}".to_string()))
                .and_then(|r| r.through),
            Some(300_000)
        );
        assert_eq!(
            seen.get(&(2, "AAPL{=h}".to_string()))
                .and_then(|r| r.through),
            Some(3_600_000)
        );

        forwarder.abort();
    }

    /// Routing is keyed by symbol **and** event type, so a subscription that
    /// asked for quotes on AAPL does not receive the trade prints another
    /// subscription asked for.
    #[tokio::test]
    async fn a_subscription_only_receives_the_event_types_it_asked_for() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let (quotes_tx, mut quotes_rx) = mpsc::channel::<Delivery>(4);
        routing
            .write()
            .await
            .senders
            .insert(1, vec![sink(quotes_tx)]);

        record_routes(
            &routing,
            1,
            &feed_subscriptions(&[target(EventKind::Quote, "AAPL")]),
        )
        .await;

        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(4);
        let forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            no_progress(),
            Arc::new(AtomicBool::new(false)),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        let trade = every_event_type("AAPL")
            .into_iter()
            .find(|event| matches!(event, MarketEvent::Trade(_)))
            .expect("a trade");
        events_tx.send(trade).await.expect("the feed accepts");
        assert!(
            tokio::time::timeout(Duration::from_millis(50), quotes_rx.recv())
                .await
                .is_err(),
            "a Quote subscription must not receive Trade prints"
        );

        events_tx
            .send(quote("AAPL"))
            .await
            .expect("the feed accepts");
        assert!(quotes_rx.recv().await.is_some(), "the quote still arrives");

        forwarder.abort();
    }

    /// A reconnect must not re-send a day of bars the consumer already has,
    /// and must not skip the ones it missed either.
    #[test]
    fn a_candle_replay_resumes_after_the_last_contiguous_bar() {
        let original = FeedTarget {
            kind: EventKind::Candle,
            symbol: "AAPL{=5m}".to_string(),
            from_time: Some(1_000),
        };
        let seen = HashMap::from([(
            (1u32, "AAPL{=5m}".to_string()),
            CandleResume {
                through: Some(5_000),
                ..CandleResume::new(0)
            },
        )]);

        assert_eq!(
            resume_from(1, original.clone(), &seen).from_time,
            Some(5_001),
            "the replay picks up one millisecond past the last bar delivered"
        );

        // Nothing delivered yet: the caller's own start still stands.
        assert_eq!(
            resume_from(1, original.clone(), &HashMap::new()).from_time,
            Some(1_000)
        );

        // Another subscription's progress is not this one's. A consumer that
        // kept up must not decide where a consumer that did not resumes from.
        assert_eq!(
            resume_from(2, original.clone(), &seen).from_time,
            Some(1_000)
        );

        // A different period is a different series.
        let other = FeedTarget {
            symbol: "AAPL{=h}".to_string(),
            ..original.clone()
        };
        assert_eq!(resume_from(1, other, &seen).from_time, Some(1_000));

        // Only candles carry one at all.
        assert_eq!(
            resume_from(1, target(EventKind::Quote, "AAPL"), &seen).from_time,
            None
        );
    }

    /// `from_time` reaches the wire. It was already on `FeedSubscription` and
    /// always `None`, which is why there was no way to ask for history.
    #[test]
    fn a_candle_request_carries_its_history_start() {
        let requests = feed_subscriptions(&[FeedTarget {
            kind: EventKind::Candle,
            symbol: "AAPL{=5m}".to_string(),
            from_time: Some(1_700_000_000_000),
        }]);

        assert_eq!(requests.len(), 1);
        assert_eq!(requests[0].event_type, "Candle");
        assert_eq!(requests[0].symbol, "AAPL{=5m}");
        assert_eq!(requests[0].from_time, Some(1_700_000_000_000));
    }

    /// A candle needs a period and a start time, so the two subscription
    /// calls are not interchangeable and each says so rather than quietly
    /// subscribing to something that cannot arrive.
    #[tokio::test]
    async fn candles_and_bare_symbols_are_not_interchangeable() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);
        let _loop_handle = spawn_command_loop(rx, || Ok(()));

        let quotes = streamer
            .create_sub([EventKind::Quote])
            .await
            .expect("the streamer is open");
        let error = quotes
            .add_candles(
                &[DxFeedSymbol("AAPL".to_string())],
                CandlePeriod::minutes(5).expect("a period"),
                DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp"),
            )
            .await
            .expect_err("the channel is not configured for candles");
        assert!(
            matches!(error, TastyTradeError::Precondition(_)),
            "{error:?}"
        );

        let candles = streamer
            .create_sub([EventKind::Candle])
            .await
            .expect("the streamer is open");
        let error = candles
            .add_symbols(&[DxFeedSymbol("AAPL".to_string())])
            .await
            .expect_err("a bare symbol has no period and no start time");
        assert!(
            matches!(error, TastyTradeError::Precondition(_)),
            "{error:?}"
        );
    }

    /// The candle path end to end: the symbol the venue is told carries the
    /// period, and that is what the subscription records.
    #[tokio::test]
    async fn a_candle_subscription_is_recorded_under_its_period_symbol() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);
        let loop_handle = spawn_command_loop(rx, || Ok(()));

        let sub = streamer
            .create_sub([EventKind::Candle])
            .await
            .expect("the streamer is open");
        sub.add_candles(
            &[DxFeedSymbol("AAPL".to_string())],
            CandlePeriod::minutes(5).expect("a period"),
            DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp"),
        )
        .await
        .expect("subscribing succeeds");

        assert_eq!(
            sub.subscribed(),
            vec![("AAPL{=5m}".to_string(), EventKind::Candle)]
        );

        drop(sub);
        drop(streamer);
        let sent = loop_handle.await.expect("the stand-in loop finishes");
        assert_eq!(sent, vec!["AAPL{=5m}".to_string()]);
    }

    /// The ordering bug candles made acute. `create_sub` used to register the
    /// returned subscription's event route from a detached `tokio::spawn`, so
    /// a caller that subscribed immediately could have the subscribe reach the
    /// command loop first — and a candle history arrives at once, so the first
    /// bars were routed to a subscription the loop did not know about and
    /// dropped.
    ///
    /// Registration is now awaited, so it is on the queue before `create_sub`
    /// returns and therefore before anything the caller does next.
    #[tokio::test]
    async fn the_event_route_is_registered_before_a_subscription_can_be_used() {
        let (tx, mut rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);

        let sub = streamer
            .create_sub([EventKind::Candle])
            .await
            .expect("the streamer is open");

        // Already queued — nothing was left to a task that may or may not have
        // run.
        let mut registrations = 0;
        while let Ok(DXLinkCommand::AddEventSender(id, _)) = rx.try_recv() {
            assert_eq!(id, sub.id.0 as u32);
            registrations += 1;
        }
        assert_eq!(
            registrations, 1,
            "exactly one consumer is registered: the caller's. The streamer's own \
             copy used to register a second that nothing could ever read, so it \
             filled up and then charged a drop for every event afterwards"
        );
    }

    /// A closed streamer cannot route events, so handing back a subscription
    /// that can never receive anything would be the same silent failure in a
    /// different place.
    #[tokio::test]
    async fn creating_a_subscription_on_a_closed_streamer_fails() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);
        drop(rx);

        let Err(error) = streamer.create_sub([EventKind::Quote]).await else {
            panic!("a closed streamer cannot register a route");
        };
        assert!(matches!(error, TastyTradeError::Streaming(_)), "{error:?}");
    }

    /// A bar that was dropped for a slow consumer must not advance the resume
    /// point: the reconnect would skip it permanently, and a hole in a price
    /// series is worse than a duplicate because nothing downstream can see it.
    ///
    /// The subtle half is *contiguity*. Taking the maximum of what was
    /// delivered reads correctly and is wrong: bar 1000 dropped and bar 3000
    /// delivered would move the resume point to 3000 and lose 2000 forever.
    #[tokio::test]
    async fn a_dropped_bar_freezes_the_resume_point_rather_than_being_stepped_over() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        // Capacity one and nothing ever reads it: the first bar fits, the rest
        // are dropped.
        let (full_tx, _never_read) = mpsc::channel::<Delivery>(1);
        let lagged = Arc::new(AtomicU64::new(0));
        routing.write().await.senders.insert(
            1,
            vec![Subscriber {
                events: full_tx,
                lagged: lagged.clone(),
                pending: Arc::new(Mutex::new(VecDeque::new())),
            }],
        );
        record_routes(
            &routing,
            1,
            &feed_subscriptions(&[FeedTarget {
                kind: EventKind::Candle,
                symbol: "AAPL{=5m}".to_string(),
                from_time: Some(0),
            }]),
        )
        .await;

        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(8);
        let progress = no_progress();
        let forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            progress.clone(),
            Arc::new(AtomicBool::new(false)),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        for time in [1_000i64, 2_000, 3_000] {
            events_tx
                .send(candle("AAPL{=5m}", time))
                .await
                .expect("the feed accepts");
        }
        tokio::time::sleep(Duration::from_millis(100)).await;

        let resume = progress
            .lock()
            .expect("not poisoned in tests")
            .get(&(1, "AAPL{=5m}".to_string()))
            .copied()
            .expect("the series was seen");

        assert_eq!(
            resume.through,
            Some(1_000),
            "only the bar that was actually delivered may be resumed past"
        );
        assert!(
            resume.gap,
            "a drop has to be remembered, or the next delivery steps over it"
        );

        // And the consumer can find out, which is the whole point: two of the
        // three bars never reached it.
        assert_eq!(lagged.load(Ordering::Relaxed), 2);

        forwarder.abort();
    }

    /// The venue echoes a one-minute candle under `AAPL{=m}`, not the `{=1m}`
    /// a naive rendering sends. Routes are keyed on the symbol the crate sent,
    /// so the two have to agree or every bar is dropped as unrouted (#137).
    #[tokio::test]
    async fn a_one_minute_candle_is_routed_under_the_symbol_the_venue_echoes() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let (sink_tx, mut sink_rx) = mpsc::channel::<Delivery>(8);
        routing.write().await.senders.insert(1, vec![sink(sink_tx)]);

        // Registered exactly as add_candles would: through the period type.
        let period = CandlePeriod::minutes(1).expect("one minute is a period");
        record_routes(
            &routing,
            1,
            &feed_subscriptions(&[FeedTarget {
                kind: EventKind::Candle,
                symbol: period.streamer_symbol("AAPL"),
                from_time: Some(0),
            }]),
        )
        .await;

        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(8);
        let progress = no_progress();
        let forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            progress.clone(),
            Arc::new(AtomicBool::new(false)),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        // What the venue actually sends back.
        events_tx
            .send(candle("AAPL{=m}", 1_000))
            .await
            .expect("the feed accepts");

        let delivered = tokio::time::timeout(Duration::from_secs(2), sink_rx.recv())
            .await
            .expect("the bar must be routed, not dropped as unregistered")
            .expect("the sink is open");
        assert_eq!(event_symbol(&market_of(delivered)), Some("AAPL{=m}"));

        // The resume bookkeeping is keyed on the same string, so a reconnect
        // continues the right series instead of replaying from the start.
        let seen = progress.lock().expect("not poisoned in tests").clone();
        let resume = seen
            .get(&(1, "AAPL{=m}".to_string()))
            .copied()
            .expect("the series was seen under its canonical symbol");
        assert_eq!(resume.through, Some(1_000));
        let replayed = resume_from(
            1,
            FeedTarget {
                kind: EventKind::Candle,
                symbol: period.streamer_symbol("AAPL"),
                from_time: Some(0),
            },
            &seen,
        );
        assert_eq!(
            replayed.from_time,
            Some(1_001),
            "the replay must continue past the delivered bar"
        );

        forwarder.abort();
    }

    /// Removal goes through the same key as registration, so taking one
    /// subscription off a one-minute series leaves the other subscriber's
    /// route in place and clears the key only when nobody is left.
    #[tokio::test]
    async fn canonical_candle_routes_are_removed_per_subscription() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let period = CandlePeriod::minutes(1).expect("one minute is a period");
        let wanted = feed_subscriptions(&[FeedTarget {
            kind: EventKind::Candle,
            symbol: period.streamer_symbol("AAPL"),
            from_time: Some(0),
        }]);
        record_routes(&routing, 1, &wanted).await;
        record_routes(&routing, 2, &wanted).await;

        let key = ("AAPL{=m}".to_string(), EventKind::Candle);
        assert_eq!(
            routing.read().await.routes.get(&key).map(|s| s.len()),
            Some(2),
            "both subscriptions share the canonical route"
        );

        // A refused subscribe or an unsubscribe for one of them.
        forget_routes(&routing, 1, &wanted).await;
        assert_eq!(
            routing.read().await.routes.get(&key).cloned(),
            Some(HashSet::from([2])),
            "the other subscriber keeps the route"
        );

        forget_routes(&routing, 2, &wanted).await;
        assert!(
            !routing.read().await.routes.contains_key(&key),
            "nobody left, so the key goes"
        );
    }

    /// A dropped one-minute bar freezes the resume point under the canonical
    /// key, same as any other period: the replay asks for it again rather
    /// than stepping over it.
    #[tokio::test]
    async fn a_dropped_one_minute_bar_freezes_the_canonical_resume_point() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        // Capacity one and never read: the first bar fits, the second drops.
        let (full_tx, _never_read) = mpsc::channel::<Delivery>(1);
        routing.write().await.senders.insert(1, vec![sink(full_tx)]);
        let period = CandlePeriod::minutes(1).expect("one minute is a period");
        let target = FeedTarget {
            kind: EventKind::Candle,
            symbol: period.streamer_symbol("AAPL"),
            from_time: Some(0),
        };
        record_routes(
            &routing,
            1,
            &feed_subscriptions(std::slice::from_ref(&target)),
        )
        .await;

        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(8);
        let progress = no_progress();
        let forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            progress.clone(),
            Arc::new(AtomicBool::new(false)),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));
        for time in [1_000i64, 2_000] {
            events_tx
                .send(candle("AAPL{=m}", time))
                .await
                .expect("the feed accepts");
        }
        tokio::time::sleep(Duration::from_millis(100)).await;

        let seen = progress.lock().expect("not poisoned in tests").clone();
        let resume = seen
            .get(&(1, "AAPL{=m}".to_string()))
            .copied()
            .expect("the series was seen");
        assert_eq!(resume.through, Some(1_000));
        assert!(resume.gap, "the drop is remembered");
        assert_eq!(
            resume_from(1, target, &seen).from_time,
            Some(1_001),
            "the replay resumes right after the last delivered bar"
        );

        forwarder.abort();
    }

    /// The boundary the contiguity rule is easiest to get wrong at: the very
    /// first bar of a series being dropped. Seeding the resume point with that
    /// bar's own time would move it past a bar the consumer never received,
    /// and the reconnect would never ask for it again.
    #[tokio::test]
    async fn a_series_whose_first_bar_was_dropped_resumes_from_the_beginning() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        // Capacity one, already full: every bar is dropped, including the first.
        let (full_tx, _never_read) = mpsc::channel::<Delivery>(1);
        full_tx
            .try_send(Delivery::Market(quote("filler")))
            .expect("the one slot is taken");
        routing.write().await.senders.insert(
            1,
            vec![Subscriber {
                events: full_tx,
                lagged: Arc::new(AtomicU64::new(0)),
                pending: Arc::new(Mutex::new(VecDeque::new())),
            }],
        );
        record_routes(
            &routing,
            1,
            &feed_subscriptions(&[FeedTarget {
                kind: EventKind::Candle,
                symbol: "AAPL{=5m}".to_string(),
                from_time: Some(1_000),
            }]),
        )
        .await;

        let (events_tx, events_rx) = mpsc::channel::<MarketEvent>(8);
        let progress = no_progress();
        let forwarder = tokio::spawn(forward_events(
            events_rx,
            routing.clone(),
            progress.clone(),
            Arc::new(AtomicBool::new(false)),
            Arc::new(Notify::new()),
            Arc::new(AtomicU64::new(0)),
            Arc::new(Notify::new()),
        ));

        events_tx
            .send(candle("AAPL{=5m}", 9_000))
            .await
            .expect("the feed accepts");
        tokio::time::sleep(Duration::from_millis(100)).await;

        let seen = progress.lock().expect("not poisoned in tests").clone();
        assert_eq!(
            seen.get(&(1, "AAPL{=5m}".to_string()))
                .and_then(|r| r.through),
            None,
            "no bar was delivered, so there is nothing to resume past"
        );

        // And the replay therefore asks for the caller's own start, not 9_001.
        let target = FeedTarget {
            kind: EventKind::Candle,
            symbol: "AAPL{=5m}".to_string(),
            from_time: Some(1_000),
        };
        assert_eq!(
            resume_from(1, target, &seen).from_time,
            Some(1_000),
            "a series that has only ever dropped bars asks for the whole thing again"
        );

        forwarder.abort();
    }

    /// `get_sub` hands back the streamer's own copy, and a private counter
    /// there would report zero however far behind the caller had fallen — a
    /// number worse than no number, because it looks like an answer.
    #[tokio::test]
    async fn the_streamers_copy_reports_the_same_lag_as_the_callers() {
        let (tx, mut rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);

        let sub = streamer
            .create_sub([EventKind::Quote])
            .await
            .expect("the streamer is open");

        // The registered sink is what the forwarder charges a drop to.
        let Some(DXLinkCommand::AddEventSender(_, subscriber)) = rx.try_recv().ok() else {
            panic!("the consumer is registered before create_sub returns");
        };
        subscriber.lagged.fetch_add(3, Ordering::Relaxed);

        assert_eq!(sub.lagged(), 3, "the caller sees its own loss");
        assert_eq!(
            streamer
                .get_sub(sub.id)
                .expect("the streamer kept a copy")
                .lagged(),
            3,
            "and so does the copy the streamer hands out"
        );
    }

    /// A gap is refilled by the replay, so the flag that froze the resume
    /// point has to be cleared when the request goes out — otherwise every
    /// future reconnect asks from the same bar forever.
    #[test]
    fn a_replay_resumes_from_before_the_gap() {
        let seen = HashMap::from([(
            (1u32, "AAPL{=5m}".to_string()),
            CandleResume {
                through: Some(1_000),
                gap: true,
                ..CandleResume::new(0)
            },
        )]);

        let target = FeedTarget {
            kind: EventKind::Candle,
            symbol: "AAPL{=5m}".to_string(),
            from_time: Some(0),
        };

        assert_eq!(
            resume_from(1, target, &seen).from_time,
            Some(1_001),
            "the replay comes back from before the gap and refills it"
        );
    }

    /// A subscription with no buffer would drop every event and then report
    /// itself as lagging, which is a worse way to learn about it.
    #[tokio::test]
    async fn a_subscription_with_no_buffer_is_refused() {
        let (tx, _rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let mut streamer = streamer_with(tx, shutdown_tx);

        let Err(error) = streamer
            .create_sub_with_capacity([EventKind::Quote], 0)
            .await
        else {
            panic!("a zero-capacity subscription cannot deliver anything");
        };
        assert!(
            matches!(error, TastyTradeError::Precondition(_)),
            "{error:?}"
        );
    }

    /// The budget is bounded, and running out is reported rather than
    /// retried silently forever.
    #[tokio::test]
    async fn the_backoff_gives_up_and_says_why() {
        let state = Arc::new(RwLock::new(ConnectionState::Connected));
        let (_shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();
        let mut attempt = 0u32;

        assert!(schedule(&policy(), &mut attempt, &state, &mut shutdown_rx).await);
        assert_eq!(
            *state.read().await,
            ConnectionState::Reconnecting {
                attempt: 1,
                delay: Duration::from_millis(10)
            }
        );

        assert!(schedule(&policy(), &mut attempt, &state, &mut shutdown_rx).await);
        assert!(
            !schedule(&policy(), &mut attempt, &state, &mut shutdown_rx).await,
            "one past the limit must stop"
        );

        let ConnectionState::Disconnected { reason } = state.read().await.clone() else {
            panic!("giving up must be terminal, not another retry");
        };
        assert!(reason.contains("2 attempts"), "{reason}");
    }

    /// Dropping the streamer during a backoff must end the supervisor then,
    /// not after the full delay: a caller who let go should not keep a task
    /// waiting thirty seconds for nobody.
    #[tokio::test]
    async fn a_backoff_is_interrupted_by_the_owner_going_away() {
        let state = Arc::new(RwLock::new(ConnectionState::Connected));
        let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();
        let slow = BackoffPolicy {
            initial: Duration::from_secs(30),
            max_attempts: None,
            ..policy()
        };
        let mut attempt = 0u32;

        drop(shutdown_tx);

        assert!(
            !schedule(&slow, &mut attempt, &state, &mut shutdown_rx).await,
            "a dropped owner ends the wait"
        );
    }

    /// A refused subscription is the venue disagreeing with one request; a
    /// dead socket makes every later request pointless. Reconnecting on the
    /// first would turn a bad symbol into a reconnect loop.
    #[test]
    fn only_a_dead_socket_counts_as_a_lost_connection() {
        assert!(is_connection_lost(&dxlink::DXLinkError::Connection(
            "closed".to_string()
        )));
        assert!(!is_connection_lost(&dxlink::DXLinkError::Protocol(
            "unknown symbol".to_string()
        )));
        assert!(!is_connection_lost(&dxlink::DXLinkError::Authentication(
            "token expired".to_string()
        )));
    }

    /// An authentication failure is not retried: the same token will be
    /// refused again, and the policy is what says so.
    #[test]
    fn a_rejected_session_is_not_worth_retrying() {
        let policy = BackoffPolicy::default();
        let refused: TastyTradeError = dxlink::DXLinkError::Authentication("nope".into()).into();

        assert!(!policy.should_retry(&refused), "{refused:?}");
        assert!(policy.should_retry(&TastyTradeError::Connection("dropped".into())));
    }

    // ---------------------------------------------------------------------
    // Historical replay markers (#142) and partial unsubscribe (#143).
    // ---------------------------------------------------------------------

    use snapshot_flags::{REMOVE_EVENT, SNAPSHOT_BEGIN, SNAPSHOT_END, SNAPSHOT_SNIP, TX_PENDING};

    /// A running forwarder plus the handles a test needs to drive it.
    ///
    /// Everything a snapshot marker depends on is shared state between the
    /// forwarder, the supervisor and a subscription, so a test that builds the
    /// pieces separately proves nothing about how they meet.
    struct Harness {
        routing: Arc<RwLock<EventRouting>>,
        progress: CandleProgress,
        drained: Arc<Notify>,
        dxlink_drops: Arc<AtomicU64>,
        history: Arc<Notify>,
        events: mpsc::Sender<MarketEvent>,
        forwarder: tokio::task::JoinHandle<()>,
    }

    impl Harness {
        fn start() -> Self {
            let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
            let progress = no_progress();
            let drained = Arc::new(Notify::new());
            let dxlink_drops = Arc::new(AtomicU64::new(0));
            let history = Arc::new(Notify::new());
            let (events, events_rx) = mpsc::channel::<MarketEvent>(64);

            let forwarder = tokio::spawn(forward_events(
                events_rx,
                routing.clone(),
                progress.clone(),
                Arc::new(AtomicBool::new(false)),
                drained.clone(),
                dxlink_drops.clone(),
                history.clone(),
            ));

            Self {
                routing,
                progress,
                drained,
                dxlink_drops,
                history,
                events,
                forwarder,
            }
        }

        /// Registers a consumer for one candle series, as `add_candles` would.
        async fn watch(
            &self,
            sub_id: u32,
            symbol: &str,
            capacity: usize,
        ) -> (mpsc::Receiver<Delivery>, Arc<AtomicU64>) {
            let (tx, rx) = mpsc::channel::<Delivery>(capacity);
            let lagged = Arc::new(AtomicU64::new(0));
            self.routing
                .write()
                .await
                .senders
                .entry(sub_id)
                .or_default()
                .push(Subscriber {
                    events: tx,
                    lagged: lagged.clone(),
                    pending: Arc::new(Mutex::new(VecDeque::new())),
                });
            record_routes(
                &self.routing,
                sub_id,
                &feed_subscriptions(&[candle_target(symbol)]),
            )
            .await;
            (rx, lagged)
        }

        async fn send(&self, event: MarketEvent) {
            self.events.send(event).await.expect("the feed accepts");
        }

        /// What `record_bar` believes about a series.
        fn resume_of(&self, sub_id: u32, symbol: &str) -> Option<CandleResume> {
            self.progress
                .lock()
                .expect("not poisoned in tests")
                .get(&(sub_id, symbol.to_string()))
                .copied()
        }
    }

    impl Drop for Harness {
        fn drop(&mut self) {
            self.forwarder.abort();
        }
    }

    fn candle_target(symbol: &str) -> FeedTarget {
        FeedTarget {
            kind: EventKind::Candle,
            symbol: symbol.to_string(),
            from_time: Some(0),
        }
    }

    /// The next delivery, failing rather than hanging.
    async fn next(rx: &mut mpsc::Receiver<Delivery>) -> Delivery {
        tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("timed out waiting for a delivery")
            .expect("the consumer's channel is open")
    }

    /// Asserts nothing arrives for long enough to mean it.
    async fn nothing_more(rx: &mut mpsc::Receiver<Delivery>) {
        if let Ok(Some(delivery)) =
            tokio::time::timeout(Duration::from_millis(150), rx.recv()).await
        {
            panic!("nothing should have arrived, got {delivery:?}");
        }
    }

    fn begin_of(delivery: Delivery) -> dxfeed::DxfSnapshotBeginT {
        match marker_of(delivery).data {
            dxfeed::EventData::SnapshotBegin(begin) => begin,
            other => panic!("expected a snapshot begin, got {other:?}"),
        }
    }

    fn end_of(delivery: Delivery) -> dxfeed::DxfSnapshotEndT {
        match marker_of(delivery).data {
            dxfeed::EventData::SnapshotEnd(end) => end,
            other => panic!("expected a snapshot end, got {other:?}"),
        }
    }

    /// The bar's start time, for asserting order.
    fn bar_time(delivery: Delivery) -> i64 {
        match market_of(delivery) {
            MarketEvent::Candle(bar) => bar.time,
            other => panic!("expected a candle, got {other:?}"),
        }
    }

    /// The whole shape, in the order it has to arrive: the replay is announced,
    /// its bars follow, and the terminator lands after the last of them rather
    /// than among them.
    #[tokio::test]
    async fn a_replay_ends_after_its_last_bar() {
        let harness = Harness::start();
        let (mut rx, lagged) = harness.watch(1, "AAPL{=5m}", 8).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness.send(flagged_candle("AAPL{=5m}", 2_000, 0)).await;
        harness
            .send(flagged_candle("AAPL{=5m}", 3_000, SNAPSHOT_END))
            .await;

        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        assert_eq!(bar_time(next(&mut rx).await), 2_000);
        // The terminator carried a real bar, so both are kept.
        assert_eq!(bar_time(next(&mut rx).await), 3_000);

        let end = end_of(next(&mut rx).await);
        assert_eq!(end.generation, 1);
        assert_eq!(end.kind, dxfeed::SnapshotEndKind::End);
        assert!(end.lossless, "nothing was dropped");
        assert_eq!(lagged.load(Ordering::Relaxed), 0);

        // The last bar is a bar like any other, so it moves the resume point.
        assert_eq!(
            harness.resume_of(1, "AAPL{=5m}").and_then(|r| r.through),
            Some(3_000)
        );
        nothing_more(&mut rx).await;
    }

    /// A history the venue cut short is a different answer from one it served
    /// in full, and a consumer sizing a chart needs to know which it got.
    #[tokio::test]
    async fn a_snipped_replay_says_so() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 8).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_SNIP))
            .await;

        let _ = begin_of(next(&mut rx).await);
        let _ = bar_time(next(&mut rx).await);
        let _ = bar_time(next(&mut rx).await);
        assert_eq!(
            end_of(next(&mut rx).await).kind,
            dxfeed::SnapshotEndKind::Snip
        );
    }

    /// An empty history: the venue has nothing and says so with a row that
    /// exists only to carry the flags. It must not become a bar, and its
    /// placeholder timestamp must not become a resume point.
    #[tokio::test]
    async fn an_empty_replay_delivers_markers_and_no_bar() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 8).await;

        harness
            .send(flagged_candle(
                "AAPL{=5m}",
                0,
                SNAPSHOT_BEGIN | SNAPSHOT_END | REMOVE_EVENT,
            ))
            .await;

        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        let end = end_of(next(&mut rx).await);
        assert_eq!(end.generation, 1);
        assert!(end.lossless);
        nothing_more(&mut rx).await;

        assert_eq!(
            harness.resume_of(1, "AAPL{=5m}").and_then(|r| r.through),
            None,
            "a placeholder timestamp must not become a resume point"
        );
    }

    /// `REMOVE_EVENT` on its own is an ordinary removal carrying a real event,
    /// not a terminator. Treating every remove as an end would finish a replay
    /// that is still running.
    #[tokio::test]
    async fn a_plain_remove_is_an_event_and_not_an_ending() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 8).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, REMOVE_EVENT))
            .await;

        let _ = begin_of(next(&mut rx).await);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        assert_eq!(
            bar_time(next(&mut rx).await),
            2_000,
            "a removal is still an event the consumer has to see"
        );
        nothing_more(&mut rx).await;

        assert_eq!(
            harness.resume_of(1, "AAPL{=5m}").map(|r| r.phase),
            Some(SnapshotPhase::Open),
            "the replay is still running"
        );
    }

    /// dxFeed re-emits a terminator. The consumer must still see exactly one
    /// end for the replay, or every re-emission looks like a new history.
    #[tokio::test]
    async fn a_re_emitted_terminator_ends_the_replay_once() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 8).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        // The re-emission: the same ending, with nothing left to carry.
        harness
            .send(flagged_candle("AAPL{=5m}", 0, SNAPSHOT_END | REMOVE_EVENT))
            .await;

        let _ = begin_of(next(&mut rx).await);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        assert_eq!(bar_time(next(&mut rx).await), 2_000);
        assert_eq!(end_of(next(&mut rx).await).generation, 1);
        nothing_more(&mut rx).await;
    }

    /// A terminator inside an open transaction is not a fact yet. Acting on it
    /// would announce a history the venue is still amending.
    #[tokio::test]
    async fn a_terminator_inside_a_transaction_waits_for_it_to_close() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 8).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle(
                "AAPL{=5m}",
                2_000,
                SNAPSHOT_END | TX_PENDING,
            ))
            .await;

        let _ = begin_of(next(&mut rx).await);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        assert_eq!(bar_time(next(&mut rx).await), 2_000);
        nothing_more(&mut rx).await;
        assert_eq!(
            harness.resume_of(1, "AAPL{=5m}").map(|r| r.phase),
            Some(SnapshotPhase::Open),
            "the transaction is still open, so the replay has not ended"
        );

        // The event that closes the transaction fires the ending, after itself.
        harness.send(flagged_candle("AAPL{=5m}", 3_000, 0)).await;
        assert_eq!(bar_time(next(&mut rx).await), 3_000);
        assert_eq!(end_of(next(&mut rx).await).generation, 1);
    }

    /// A consumer that cannot keep up still gets the ending, and is told the
    /// history behind it has holes. "The replay finished" and "you have all of
    /// it" are different answers and this is where they part.
    #[tokio::test]
    async fn a_slow_consumer_is_told_its_history_is_incomplete() {
        let harness = Harness::start();
        let (mut rx, lagged) = harness.watch(1, "AAPL{=5m}", 2).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        for time in [2_000i64, 3_000, 4_000, 5_000] {
            harness.send(flagged_candle("AAPL{=5m}", time, 0)).await;
        }
        harness
            .send(flagged_candle(
                "AAPL{=5m}",
                6_000,
                SNAPSHOT_END | REMOVE_EVENT,
            ))
            .await;

        // Two slots: the begin marker and the first bar.
        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);

        // Reading made room, which is what lets the parked ending through.
        harness.drained.notify_one();
        let end = end_of(next(&mut rx).await);
        assert!(
            !end.lossless,
            "bars of this replay never reached the consumer"
        );
        assert_eq!(end.generation, 1);
        assert!(lagged.load(Ordering::Relaxed) >= 1, "the loss is countable");
    }

    /// The ending arrives exactly when there is no room for it. It must not be
    /// dropped, and nothing that comes after may overtake it: the marker's
    /// position in the stream is its entire meaning.
    #[tokio::test]
    async fn a_full_queue_parks_the_ending_instead_of_losing_it() {
        let harness = Harness::start();
        let (mut rx, lagged) = harness.watch(1, "AAPL{=5m}", 2).await;

        // Fills both slots: the begin marker and the bar it rode in on.
        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        // No room: dropped and counted.
        harness.send(flagged_candle("AAPL{=5m}", 2_000, 0)).await;
        // No room either, but an ending is never dropped.
        harness
            .send(flagged_candle(
                "AAPL{=5m}",
                3_000,
                SNAPSHOT_END | REMOVE_EVENT,
            ))
            .await;
        // Live data arriving behind a parked ending must not jump the queue.
        harness.send(flagged_candle("AAPL{=5m}", 4_000, 0)).await;

        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        harness.drained.notify_one();
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        harness.drained.notify_one();

        assert_eq!(
            end_of(next(&mut rx).await).generation,
            1,
            "the ending survived a full queue"
        );
        assert_eq!(
            lagged.load(Ordering::Relaxed),
            2,
            "both live bars were dropped rather than overtaking the ending"
        );

        // And the stream is usable again afterwards.
        harness.send(flagged_candle("AAPL{=5m}", 5_000, 0)).await;
        assert_eq!(bar_time(next(&mut rx).await), 5_000);
    }

    /// Loss inside the feed client happens before the forwarder sees anything,
    /// so no per-consumer counter can notice it. The replay still has holes.
    #[tokio::test]
    async fn loss_inside_the_feed_client_makes_a_replay_lossy() {
        let harness = Harness::start();
        let (mut rx, lagged) = harness.watch(1, "AAPL{=5m}", 8).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        let _ = begin_of(next(&mut rx).await);
        let _ = bar_time(next(&mut rx).await);

        // dxlink shed events while this replay was running.
        harness.dxlink_drops.store(3, Ordering::Relaxed);

        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        let _ = bar_time(next(&mut rx).await);

        let end = end_of(next(&mut rx).await);
        assert!(
            !end.lossless,
            "events the feed client dropped are still missing history"
        );
        assert_eq!(
            lagged.load(Ordering::Relaxed),
            0,
            "this consumer kept up; the loss was above it"
        );
    }

    /// A reconnect ends the current replay's claim to be current immediately.
    /// Waiting for the venue would leave a consumer believing stale history was
    /// still loading for as long as the backoff lasts.
    #[tokio::test]
    async fn a_reconnect_starts_a_new_generation_without_waiting_for_the_venue() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 16).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        // Deliberately unread: the old events are still queued when the
        // connection drops, which is the case a generation number exists for.
        tokio::time::sleep(Duration::from_millis(50)).await;
        assert_eq!(
            harness.resume_of(1, "AAPL{=5m}").map(|r| r.phase),
            Some(SnapshotPhase::Ended)
        );

        invalidate_history(
            &harness.progress,
            &harness.routing,
            &harness.history,
            &harness.dxlink_drops,
        )
        .await;

        assert_eq!(
            harness.resume_of(1, "AAPL{=5m}").map(|r| r.phase),
            Some(SnapshotPhase::Open),
            "history stops being loaded the moment the socket drops"
        );

        // The consumer's own stream stays in order: the old replay, then the
        // announcement of the new one.
        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        assert_eq!(bar_time(next(&mut rx).await), 2_000);
        assert_eq!(
            end_of(next(&mut rx).await).generation,
            1,
            "an ending from the old replay is identifiable, not lost"
        );
        assert_eq!(begin_of(next(&mut rx).await).generation, 2);

        // The venue's own begin for the replay the reconnect already announced
        // adds nothing: one generation, one marker.
        harness
            .send(flagged_candle("AAPL{=5m}", 3_000, SNAPSHOT_BEGIN))
            .await;
        assert_eq!(bar_time(next(&mut rx).await), 3_000);
        harness
            .send(flagged_candle("AAPL{=5m}", 4_000, SNAPSHOT_END))
            .await;
        assert_eq!(bar_time(next(&mut rx).await), 4_000);
        assert_eq!(end_of(next(&mut rx).await).generation, 2);
        nothing_more(&mut rx).await;
    }

    /// Adds another series to a subscription that already has a consumer.
    ///
    /// Separate from `watch` because a subscription has one queue and many
    /// series: registering a second consumer instead would test something no
    /// consumer ever does.
    async fn also_watch(harness: &Harness, sub_id: u32, symbol: &str) {
        record_routes(
            &harness.routing,
            sub_id,
            &feed_subscriptions(&[candle_target(symbol)]),
        )
        .await;
    }

    /// Stands in for the command loop, including the route bookkeeping it does
    /// once the venue confirms an unsubscribe.
    fn spawn_routing_command_loop(
        rx: mpsc::Receiver<DXLinkCommand>,
        routing: Arc<RwLock<EventRouting>>,
    ) -> tokio::task::JoinHandle<()> {
        let (handle, _) = spawn_recording_command_loop(rx, routing);
        handle
    }

    /// The same, plus what it would have taken off the wire.
    ///
    /// The subscriptions share one feed channel, so what reaches the venue is
    /// the difference between dropping a route and cutting somebody else's
    /// feed. A test that only reads the routing table cannot see that.
    fn spawn_recording_command_loop(
        mut rx: mpsc::Receiver<DXLinkCommand>,
        routing: Arc<RwLock<EventRouting>>,
    ) -> (tokio::task::JoinHandle<()>, Arc<Mutex<Vec<String>>>) {
        let unsubscribed: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
        let seen = unsubscribed.clone();

        let handle = tokio::spawn(async move {
            while let Some(cmd) = rx.recv().await {
                match cmd {
                    DXLinkCommand::Subscribe(requests, _, sub_id, held, ack) => {
                        if let Some(symbol) = withdrawn_request(&held, &requests) {
                            answer(ack, Err(TastyTradeError::Streaming(symbol)));
                            continue;
                        }
                        record_routes(&routing, sub_id, &requests).await;
                        answer(ack, Ok(()));
                    }
                    DXLinkCommand::Unsubscribe(requests, sub_id, still_held, ack) => {
                        // The real loop's own decision, with the venue call
                        // stubbed: a copy of that logic here would drift from
                        // it, which is how #148 stayed hidden.
                        let plan =
                            plan_unsubscribe(&routing, sub_id, still_held.as_deref(), &requests)
                                .await;
                        seen.lock()
                            .expect("not poisoned in tests")
                            .extend(plan.orphaned.into_iter().map(|request| request.symbol));
                        forget_routes(&routing, sub_id, &plan.released).await;
                        answer(ack, Ok(()));
                    }
                    _ => {}
                }
            }
        });

        (handle, unsubscribed)
    }

    /// A subscription wired to `harness`, as `create_sub` would build one.
    fn subscription_for(
        sub_id: u32,
        commands: mpsc::Sender<DXLinkCommand>,
        harness: &Harness,
        targets: Arc<Mutex<BTreeSet<FeedTarget>>>,
    ) -> QuoteSubscription {
        subscription_with(
            sub_id,
            commands,
            harness.progress.clone(),
            harness.drained.clone(),
            harness.history.clone(),
            targets,
        )
    }

    /// A subscription sharing the given state, whoever owns it.
    fn subscription_with(
        sub_id: u32,
        commands: mpsc::Sender<DXLinkCommand>,
        progress: CandleProgress,
        drained: Arc<Notify>,
        history: Arc<Notify>,
        targets: Arc<Mutex<BTreeSet<FeedTarget>>>,
    ) -> QuoteSubscription {
        let (_closed, closed_rx) = mpsc::channel::<Delivery>(1);
        let (_unused, event_receiver) = flume::unbounded();
        QuoteSubscription {
            id: SubscriptionId(sub_id as usize),
            streamer: StreamerHandle {
                commands: Some(commands),
            },
            kinds: BTreeSet::from([EventKind::Candle]),
            event_receiver,
            dxlink_receiver: closed_rx,
            targets,
            lagged: Arc::new(AtomicU64::new(0)),
            progress,
            drained,
            history,
            closed: Arc::new(AtomicBool::new(false)),
        }
    }

    fn shared_targets(symbols: &[&str]) -> Arc<Mutex<BTreeSet<FeedTarget>>> {
        Arc::new(Mutex::new(
            symbols.iter().map(|symbol| candle_target(symbol)).collect(),
        ))
    }

    /// The same question `SnapshotEnd` answers, for a caller that would rather
    /// ask than watch the stream.
    #[tokio::test]
    async fn await_history_resolves_when_the_replay_ends() {
        let harness = Harness::start();
        let (_rx, _) = harness.watch(1, "AAPL{=5m}", 8).await;
        let (commands, _command_rx) = mpsc::channel::<DXLinkCommand>(4);
        let subscription = subscription_for(1, commands, &harness, shared_targets(&["AAPL{=5m}"]));

        assert!(
            !subscription.history_loaded("AAPL{=5m}"),
            "nothing has replayed yet"
        );

        let feed = async {
            harness
                .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
                .await;
            harness
                .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
                .await;
        };
        let waiting = tokio::time::timeout(
            Duration::from_secs(2),
            subscription.await_history("AAPL{=5m}"),
        );

        let (_, resolved) = tokio::join!(feed, waiting);
        let end = resolved
            .expect("the wait must not hang")
            .expect("the series is subscribed");

        assert_eq!(end.generation, 1);
        assert_eq!(end.kind, dxfeed::SnapshotEndKind::End);
        assert!(end.lossless);
        assert!(subscription.history_loaded("AAPL{=5m}"));

        // Already finished, so it resolves at once rather than waiting for a
        // notification that has already been sent.
        let again = tokio::time::timeout(
            Duration::from_secs(2),
            subscription.await_history("AAPL{=5m}"),
        )
        .await
        .expect("an answer that is already known must not wait");
        assert_eq!(again.expect("still subscribed").generation, 1);
    }

    /// A mistyped symbol would wait forever. Saying so is a bug report; hanging
    /// is not.
    #[tokio::test]
    async fn await_history_refuses_a_series_this_subscription_does_not_hold() {
        let harness = Harness::start();
        let (commands, _command_rx) = mpsc::channel::<DXLinkCommand>(4);
        let subscription = subscription_for(1, commands, &harness, shared_targets(&["AAPL{=5m}"]));

        let error = tokio::time::timeout(
            Duration::from_secs(2),
            // The bare underlying, without its period: a real mistake, and one
            // that can never be delivered.
            subscription.await_history("AAPL"),
        )
        .await
        .expect("it must answer rather than wait")
        .expect_err("an unsubscribed series can never finish");

        assert!(matches!(error, TastyTradeError::Precondition(_)));
    }

    /// A reconnect supersedes the generation a waiter was told about, so a
    /// caller asking again keeps waiting for the new one.
    #[tokio::test]
    async fn a_reconnect_makes_a_finished_history_unfinished_again() {
        let harness = Harness::start();
        let (_rx, _) = harness.watch(1, "AAPL{=5m}", 16).await;
        let (commands, _command_rx) = mpsc::channel::<DXLinkCommand>(4);
        let subscription = subscription_for(1, commands, &harness, shared_targets(&["AAPL{=5m}"]));

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        let end = tokio::time::timeout(
            Duration::from_secs(2),
            subscription.await_history("AAPL{=5m}"),
        )
        .await
        .expect("the wait must not hang")
        .expect("the series is subscribed");
        assert_eq!(end.generation, 1);

        invalidate_history(
            &harness.progress,
            &harness.routing,
            &harness.history,
            &harness.dxlink_drops,
        )
        .await;

        assert!(
            !subscription.history_loaded("AAPL{=5m}"),
            "the replay that finished is not the current one any more"
        );
        assert!(
            tokio::time::timeout(
                Duration::from_millis(150),
                subscription.await_history("AAPL{=5m}")
            )
            .await
            .is_err(),
            "a waiter must not be satisfied by the superseded generation"
        );

        harness
            .send(flagged_candle("AAPL{=5m}", 3_000, SNAPSHOT_END))
            .await;
        let end = tokio::time::timeout(
            Duration::from_secs(2),
            subscription.await_history("AAPL{=5m}"),
        )
        .await
        .expect("the wait must not hang")
        .expect("the series is subscribed");
        assert_eq!(end.generation, 2, "the replay the reconnect started");
    }

    /// The shape a candle consumer actually has, end to end.
    ///
    /// Three series load their history independently on one subscription. One
    /// finishes and is dropped, which must not disturb the other two, must not
    /// disturb a second subscription watching the same series, and must not
    /// come back on the next connection.
    #[tokio::test]
    async fn a_finished_series_is_dropped_without_disturbing_the_others() {
        let harness = Harness::start();

        // One consumer, three series — a subscription has one queue.
        let (mut mine, _) = harness.watch(1, "AAPL{=5m}", 64).await;
        also_watch(&harness, 1, "AAPL{=h}").await;
        also_watch(&harness, 1, "MSFT{=5m}").await;

        // A second consumer sharing exactly one of them.
        let (mut theirs, _) = harness.watch(2, "AAPL{=5m}", 64).await;

        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let loop_handle = spawn_routing_command_loop(command_rx, harness.routing.clone());

        let mine_targets = shared_targets(&["AAPL{=5m}", "AAPL{=h}", "MSFT{=5m}"]);
        let theirs_targets = shared_targets(&["AAPL{=5m}"]);
        let subscription = subscription_for(1, commands, &harness, mine_targets.clone());

        // Each series replays on its own, and each one ends on its own.
        for symbol in ["AAPL{=5m}", "AAPL{=h}", "MSFT{=5m}"] {
            harness
                .send(flagged_candle(symbol, 1_000, SNAPSHOT_BEGIN))
                .await;
            harness
                .send(flagged_candle(symbol, 2_000, SNAPSHOT_END))
                .await;
        }

        let mut five_minute_series = None;
        for symbol in ["AAPL{=5m}", "AAPL{=h}", "MSFT{=5m}"] {
            assert_eq!(begin_of(next(&mut mine).await).generation, 1, "{symbol}");
            assert_eq!(bar_time(next(&mut mine).await), 1_000, "{symbol}");
            assert_eq!(bar_time(next(&mut mine).await), 2_000, "{symbol}");

            let ending = marker_of(next(&mut mine).await);
            assert_eq!(ending.sym, symbol, "a marker names its own series");
            match ending.data {
                dxfeed::EventData::SnapshotEnd(end) => {
                    assert_eq!(end.generation, 1, "{symbol}")
                }
                other => panic!("expected a snapshot end for {symbol}, got {other:?}"),
            }
            assert!(subscription.history_loaded(symbol), "{symbol}");

            if symbol == "AAPL{=5m}" {
                five_minute_series = Some(ending.sym);
            }
        }

        // The shared series replayed for the other subscription too.
        let _ = begin_of(next(&mut theirs).await);
        assert_eq!(bar_time(next(&mut theirs).await), 1_000);
        assert_eq!(bar_time(next(&mut theirs).await), 2_000);
        assert_eq!(end_of(next(&mut theirs).await).generation, 1);

        // One series is done, so stop paying for a live feed nobody reads.
        // The symbol comes back out of the marker rather than being retyped: a
        // marker names its series with the period suffix, remove_candles takes
        // it without, and `CandlePeriod` is what converts between them. That
        // round trip is why both exist.
        let five = CandlePeriod::minutes(5).expect("a period");
        let wire = five_minute_series.expect("the five-minute replay ended");
        let base = five
            .base_symbol(&wire)
            .expect("the marker names a five-minute series");
        assert_eq!(base, DxFeedSymbol("AAPL".to_string()));

        subscription
            .remove_candles(&[base], five)
            .await
            .expect("the venue accepted the unsubscribe");

        let left: Vec<String> = subscription
            .subscribed()
            .into_iter()
            .map(|(symbol, _)| symbol)
            .collect();
        assert_eq!(
            left,
            vec!["AAPL{=h}".to_string(), "MSFT{=5m}".to_string()],
            "only the removed series goes"
        );
        let forgotten = harness
            .resume_of(1, "AAPL{=5m}")
            .expect("the generation counter outlives the removal");
        assert_eq!(
            forgotten.phase,
            SnapshotPhase::Idle,
            "a resubscribe must not inherit a finished replay"
        );
        assert_eq!(
            forgotten.generation, 1,
            "the counter is kept, so a later replay cannot reuse a number the \
             consumer has already seen"
        );

        // Live data on the removed series reaches the subscription that still
        // wants it, and nobody else.
        harness.send(flagged_candle("AAPL{=5m}", 3_000, 0)).await;
        harness.send(flagged_candle("AAPL{=h}", 3_000, 0)).await;

        assert_eq!(
            bar_time(next(&mut theirs).await),
            3_000,
            "the other subscription still gets the series it shares"
        );
        assert_eq!(
            bar_time(next(&mut mine).await),
            3_000,
            "the first event to arrive here must be the series that was kept"
        );
        assert_eq!(
            harness.resume_of(2, "AAPL{=5m}").map(|r| r.phase),
            Some(SnapshotPhase::Ended),
            "removing a series from one subscription leaves another's alone"
        );
        nothing_more(&mut mine).await;

        // And a reconnect does not bring back what was removed.
        let registry: Registry = Arc::new(Mutex::new(HashMap::from([
            (
                1u32,
                SubscriptionRecord {
                    kinds: BTreeSet::from([EventKind::Candle]),
                    targets: mine_targets,
                },
            ),
            (
                2u32,
                SubscriptionRecord {
                    kinds: BTreeSet::from([EventKind::Candle]),
                    targets: theirs_targets,
                },
            ),
        ])));

        let replay: HashMap<u32, Vec<String>> = pending_replay(&registry, &harness.progress)
            .into_iter()
            .map(|(sub_id, requests)| {
                (
                    sub_id,
                    requests.into_iter().map(|request| request.symbol).collect(),
                )
            })
            .collect();

        assert_eq!(
            replay.get(&1).expect("the subscription is replayed"),
            &vec!["AAPL{=h}".to_string(), "MSFT{=5m}".to_string()],
            "the removed series must not be resubscribed"
        );
        assert_eq!(
            replay.get(&2).expect("the other subscription is replayed"),
            &vec!["AAPL{=5m}".to_string()],
            "and the subscription that kept it still gets it back"
        );

        loop_handle.abort();
    }

    /// A replay's answer is fixed once it ends. A live bar dropped afterwards
    /// is a live bar, and rewriting the finished generation would tell a
    /// consumer its history had holes that its history never had.
    #[tokio::test]
    async fn a_live_bar_dropped_after_the_replay_does_not_rewrite_its_answer() {
        let harness = Harness::start();
        // Exactly the begin marker, the bar and the end marker: full when the
        // replay ends, which is what makes the next bar a drop.
        let (_rx, lagged) = harness.watch(1, "AAPL{=5m}", 3).await;

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle(
                "AAPL{=5m}",
                2_000,
                SNAPSHOT_END | REMOVE_EVENT,
            ))
            .await;
        tokio::time::sleep(Duration::from_millis(100)).await;

        let ended = harness
            .resume_of(1, "AAPL{=5m}")
            .expect("the series was seen");
        assert_eq!(ended.phase, SnapshotPhase::Ended);
        assert!(ended.lossless, "nothing was dropped during the replay");

        // Live data the consumer cannot keep up with, after the fact.
        harness.send(flagged_candle("AAPL{=5m}", 3_000, 0)).await;
        tokio::time::sleep(Duration::from_millis(100)).await;

        assert_eq!(
            lagged.load(Ordering::Relaxed),
            1,
            "the live bar was dropped"
        );
        assert!(
            harness
                .resume_of(1, "AAPL{=5m}")
                .expect("the series is still known")
                .lossless,
            "the finished replay's answer must not change afterwards"
        );
    }

    /// Removing a series wakes anybody waiting on its history. Without a
    /// re-check they would park again and wait for a series nobody holds.
    #[tokio::test]
    async fn await_history_gives_up_when_the_series_is_removed() {
        let harness = Harness::start();
        let (_rx, _) = harness.watch(1, "AAPL{=5m}", 8).await;
        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let loop_handle = spawn_routing_command_loop(command_rx, harness.routing.clone());
        let subscription = subscription_for(1, commands, &harness, shared_targets(&["AAPL{=5m}"]));

        // Nothing has replayed, so the wait is genuinely pending when the
        // series is taken away underneath it.
        let waiting = tokio::time::timeout(
            Duration::from_secs(2),
            subscription.await_history("AAPL{=5m}"),
        );
        let removing = async {
            tokio::time::sleep(Duration::from_millis(50)).await;
            subscription
                .remove_candles(
                    &[DxFeedSymbol("AAPL".to_string())],
                    CandlePeriod::minutes(5).expect("a period"),
                )
                .await
        };

        let (waited, removed) = tokio::join!(waiting, removing);
        removed.expect("the venue accepted the unsubscribe");

        let error = waited
            .expect("the wait must end rather than hang")
            .expect_err("a series nobody holds can never finish");
        assert!(matches!(error, TastyTradeError::Precondition(_)));

        loop_handle.abort();
    }

    /// A closed subscription stops answering for its series. A handle kept
    /// afterwards would otherwise report a history that belongs to nothing,
    /// and the entries would be walked again on every later reconnect.
    #[tokio::test]
    async fn closing_a_subscription_forgets_its_history() {
        let (tx, rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let loop_handle = spawn_command_loop(rx, || Ok(()));
        let mut streamer = streamer_with(tx.clone(), shutdown_tx);

        // A finished replay, as the forwarder would have left it.
        streamer
            .progress
            .lock()
            .expect("not poisoned in tests")
            .insert(
                (0u32, "AAPL{=5m}".to_string()),
                CandleResume {
                    generation: 1,
                    phase: SnapshotPhase::Ended,
                    ended_as: Some(dxfeed::SnapshotEndKind::End),
                    ..CandleResume::new(0)
                },
            );

        let subscription = subscription_with(
            0,
            tx,
            streamer.progress.clone(),
            streamer.drained.clone(),
            streamer.history.clone(),
            shared_targets(&["AAPL{=5m}"]),
        );
        assert!(subscription.history_loaded("AAPL{=5m}"));

        let id = SubscriptionId(0);
        streamer.subscription_map.insert(id, subscription);
        streamer
            .close_sub(id)
            .await
            .expect("the venue accepted the close");

        assert!(
            streamer
                .progress
                .lock()
                .expect("not poisoned in tests")
                .is_empty(),
            "a closed subscription must not keep answering for its series"
        );

        loop_handle.abort();
    }

    /// The round trip on a symbol that is nothing like its instrument name.
    ///
    /// A futures contract the REST API calls `/ESU3` streams as
    /// `/ESU23:XCME`: a slash it keeps, a month code that changes and an
    /// exchange suffix the REST name never had. There is no rule to apply, so
    /// the test does what a consumer must do — carry the string through
    /// untouched — and checks that every stage preserves it exactly.
    #[tokio::test]
    async fn a_futures_series_round_trips_without_being_rewritten() {
        const CONTRACT: &str = "/ESU23:XCME";
        let hourly = CandlePeriod::hours(1).expect("a period");
        let five = CandlePeriod::minutes(5).expect("a period");

        // What add_candles would put on the wire for each period.
        let hourly_wire = hourly.streamer_symbol(CONTRACT);
        let five_wire = five.streamer_symbol(CONTRACT);
        assert_eq!(hourly_wire, "/ESU23:XCME{=h}");
        assert_eq!(five_wire, "/ESU23:XCME{=5m}");

        let harness = Harness::start();
        let (mut mine, _) = harness.watch(1, &hourly_wire, 32).await;
        also_watch(&harness, 1, &five_wire).await;

        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let loop_handle = spawn_routing_command_loop(command_rx, harness.routing.clone());
        let subscription = subscription_for(
            1,
            commands,
            &harness,
            shared_targets(&[&hourly_wire, &five_wire]),
        );

        // Both series replay.
        for symbol in [&hourly_wire, &five_wire] {
            harness
                .send(flagged_candle(symbol, 1_000, SNAPSHOT_BEGIN))
                .await;
            harness
                .send(flagged_candle(symbol, 2_000, SNAPSHOT_END))
                .await;
        }

        // The hourly one first, and its marker names it exactly as subscribed.
        assert_eq!(begin_of(next(&mut mine).await).generation, 1);
        assert_eq!(bar_time(next(&mut mine).await), 1_000);
        assert_eq!(bar_time(next(&mut mine).await), 2_000);
        let ending = marker_of(next(&mut mine).await);
        assert_eq!(
            ending.sym, hourly_wire,
            "the marker must carry the venue's own string, not a rewritten one"
        );

        // Drain the five-minute replay so what is left later is unambiguous.
        let _ = begin_of(next(&mut mine).await);
        assert_eq!(bar_time(next(&mut mine).await), 1_000);
        assert_eq!(bar_time(next(&mut mine).await), 2_000);
        assert_eq!(marker_of(next(&mut mine).await).sym, five_wire);

        // history_loaded and await_history take the full wire symbol, suffix
        // included: they answer per series, and a series is a symbol *and* a
        // period.
        assert!(subscription.history_loaded(&hourly_wire));
        assert!(subscription.history_loaded(&five_wire));
        assert!(
            !subscription.history_loaded(CONTRACT),
            "the bare contract names no series on its own"
        );
        let awaited = tokio::time::timeout(
            Duration::from_secs(2),
            subscription.await_history(&hourly_wire),
        )
        .await
        .expect("already finished, so it must not wait")
        .expect("the series is subscribed");
        assert_eq!(awaited.generation, 1);

        // The round trip: marker symbol, back through the period, into
        // remove_candles. The base has to come out byte for byte.
        let base = hourly
            .base_symbol(&ending.sym)
            .expect("the marker names an hourly series");
        assert_eq!(base, DxFeedSymbol(CONTRACT.to_string()));

        subscription
            .remove_candles(&[base], hourly)
            .await
            .expect("the venue accepted the unsubscribe");

        // Only the hourly series went.
        let left: Vec<String> = subscription
            .subscribed()
            .into_iter()
            .map(|(symbol, _)| symbol)
            .collect();
        assert_eq!(
            left,
            vec![five_wire.clone()],
            "removing one period must leave the other alone"
        );
        assert!(!subscription.history_loaded(&hourly_wire));
        assert!(
            subscription.history_loaded(&five_wire),
            "the period that was kept keeps its history too"
        );

        // And the venue's live data proves the routing followed.
        harness.send(flagged_candle(&hourly_wire, 3_000, 0)).await;
        harness.send(flagged_candle(&five_wire, 3_000, 0)).await;
        let live = market_of(next(&mut mine).await);
        assert_eq!(
            event_symbol(&live),
            Some(five_wire.as_str()),
            "the first thing to arrive must be the series that was kept"
        );
        nothing_more(&mut mine).await;

        loop_handle.abort();
    }
    // ---------------------------------------------------------------------
    // The six defects the review of #144 found.
    // ---------------------------------------------------------------------

    /// One feed channel serves every subscription, so a wire-level remove is
    /// channel-wide. A series somebody else is still watching must never
    /// reach the venue as an unsubscribe: their feed would stop with no error
    /// and no way to notice.
    #[tokio::test]
    async fn removing_a_shared_series_does_not_take_it_off_the_wire() {
        let harness = Harness::start();
        let (_mine, _) = harness.watch(1, "AAPL{=5m}", 16).await;
        also_watch(&harness, 1, "MSFT{=5m}").await;
        let (_theirs, _) = harness.watch(2, "AAPL{=5m}", 16).await;

        // The production decision, asked directly: this is what the command
        // loop hands to `client.unsubscribe`.
        let shared = feed_subscriptions(&[candle_target("AAPL{=5m}")]);
        assert!(
            orphaned_subscriptions(&harness.routing, 1, &shared)
                .await
                .is_empty(),
            "a series another subscription still holds must not leave the wire"
        );
        let mine_alone = feed_subscriptions(&[candle_target("MSFT{=5m}")]);
        assert_eq!(
            orphaned_subscriptions(&harness.routing, 1, &mine_alone)
                .await
                .into_iter()
                .map(|request| request.symbol)
                .collect::<Vec<_>>(),
            vec!["MSFT{=5m}".to_string()],
            "a series nobody else holds is the one to take off the wire"
        );

        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let (loop_handle, unsubscribed) =
            spawn_recording_command_loop(command_rx, harness.routing.clone());
        let subscription = subscription_for(
            1,
            commands.clone(),
            &harness,
            shared_targets(&["AAPL{=5m}", "MSFT{=5m}"]),
        );
        let five = CandlePeriod::minutes(5).expect("a period");

        // And end to end: shared with subscription 2, so nothing reaches the
        // venue.
        subscription
            .remove_candles(&[DxFeedSymbol("AAPL".to_string())], five)
            .await
            .expect("the removal is accepted");
        assert!(
            unsubscribed
                .lock()
                .expect("not poisoned in tests")
                .is_empty(),
            "a series another subscription still holds must not leave the wire"
        );
        // And it is gone locally, which is all this subscription asked for.
        assert_eq!(
            subscription.subscribed(),
            vec![("MSFT{=5m}".to_string(), EventKind::Candle)]
        );
        assert!(
            harness
                .routing
                .read()
                .await
                .routes
                .contains_key(&("AAPL{=5m}".to_string(), EventKind::Candle)),
            "the other subscription keeps its route"
        );

        // Nobody else holds MSFT, so that one does reach the venue.
        subscription
            .remove_candles(&[DxFeedSymbol("MSFT".to_string())], five)
            .await
            .expect("the removal is accepted");
        assert_eq!(
            *unsubscribed.lock().expect("not poisoned in tests"),
            vec!["MSFT{=5m}".to_string()],
            "a series nobody is left watching is the one to unsubscribe"
        );

        loop_handle.abort();
    }

    /// A terminator waiting for its transaction to close is stale the moment
    /// a new snapshot begins. Left armed it fires on the bar that opened the
    /// new replay, declaring a history finished before any of it arrived.
    #[tokio::test]
    async fn a_new_snapshot_is_not_ended_by_the_terminator_of_the_last_one() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 16).await;

        // A replay whose ending is announced inside an open transaction.
        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle(
                "AAPL{=5m}",
                2_000,
                SNAPSHOT_END | TX_PENDING,
            ))
            .await;
        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        assert_eq!(bar_time(next(&mut rx).await), 2_000);
        nothing_more(&mut rx).await;

        // The venue starts a new snapshot instead of closing that
        // transaction. Its first bar must not be read as the old ending.
        harness
            .send(flagged_candle("AAPL{=5m}", 3_000, SNAPSHOT_BEGIN))
            .await;
        assert_eq!(bar_time(next(&mut rx).await), 3_000);
        nothing_more(&mut rx).await;
        assert_eq!(
            harness.resume_of(1, "AAPL{=5m}").map(|r| r.phase),
            Some(SnapshotPhase::Open),
            "the new replay is still running"
        );

        // And it ends when it actually ends.
        harness
            .send(flagged_candle("AAPL{=5m}", 4_000, SNAPSHOT_END))
            .await;
        assert_eq!(bar_time(next(&mut rx).await), 4_000);
        assert_eq!(end_of(next(&mut rx).await).generation, 1);
    }

    /// Generations identify a replay, so a number may not be handed out twice.
    /// Removing a series and subscribing to it again is a different replay.
    #[tokio::test]
    async fn a_resubscribed_series_does_not_reuse_a_generation() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 16).await;

        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let loop_handle = spawn_routing_command_loop(command_rx, harness.routing.clone());
        let targets = shared_targets(&["AAPL{=5m}"]);
        let subscription = subscription_for(1, commands, &harness, targets.clone());
        let five = CandlePeriod::minutes(5).expect("a period");

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        let _ = bar_time(next(&mut rx).await);
        let _ = bar_time(next(&mut rx).await);
        assert_eq!(end_of(next(&mut rx).await).generation, 1);

        subscription
            .remove_candles(&[DxFeedSymbol("AAPL".to_string())], five)
            .await
            .expect("the removal is accepted");
        assert!(
            !subscription.history_loaded("AAPL{=5m}"),
            "a series nobody holds has no loaded history"
        );

        // Subscribed again, as add_candles would.
        targets_of(&targets).insert(candle_target("AAPL{=5m}"));
        record_routes(
            &harness.routing,
            1,
            &feed_subscriptions(&[candle_target("AAPL{=5m}")]),
        )
        .await;

        harness
            .send(flagged_candle("AAPL{=5m}", 5_000, SNAPSHOT_BEGIN))
            .await;
        assert_eq!(
            begin_of(next(&mut rx).await).generation,
            2,
            "the second replay must be distinguishable from the first"
        );

        loop_handle.abort();
    }

    /// The targets come out of the shared set before the venue is asked, so
    /// every way out has to put them back — including the caller dropping the
    /// future mid-wait, which no error branch can catch.
    #[tokio::test]
    async fn a_cancelled_removal_leaves_the_subscription_intact() {
        let harness = Harness::start();
        // No command loop at all: the send lands in the queue and the answer
        // never comes, which is the window a cancellation falls into.
        let (commands, _command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let subscription = subscription_for(1, commands, &harness, shared_targets(&["AAPL{=5m}"]));
        let five = CandlePeriod::minutes(5).expect("a period");

        assert!(
            tokio::time::timeout(
                Duration::from_millis(100),
                subscription.remove_candles(&[DxFeedSymbol("AAPL".to_string())], five),
            )
            .await
            .is_err(),
            "the removal must still be waiting when it is dropped"
        );

        assert_eq!(
            subscription.subscribed(),
            vec![("AAPL{=5m}".to_string(), EventKind::Candle)],
            "a removal nobody confirmed must leave the series subscribed, or a \
             reconnect stops replaying something the venue is still serving"
        );
    }

    /// A reconnect brings a new feed client counting its own losses from zero.
    /// A baseline carried over from the old one is a number the new counter
    /// can never exceed, so every replay would report itself complete.
    #[tokio::test]
    async fn a_reconnect_rebaselines_the_feed_client_loss_counter() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 16).await;

        // The old connection shed a lot.
        harness.dxlink_drops.store(500, Ordering::Relaxed);
        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        let _ = begin_of(next(&mut rx).await);
        let _ = bar_time(next(&mut rx).await);

        // Exactly what the supervisor does when the socket drops, ordering
        // included — the mirror is not touched by this test.
        open_generations_for_the_next_connection(
            &harness.progress,
            &harness.routing,
            &harness.history,
            &harness.dxlink_drops,
        )
        .await;
        assert_eq!(begin_of(next(&mut rx).await).generation, 2);

        // The new client sheds far less than the old one ever did.
        harness.dxlink_drops.store(3, Ordering::Relaxed);
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        let _ = bar_time(next(&mut rx).await);

        let end = end_of(next(&mut rx).await);
        assert_eq!(end.generation, 2);
        assert!(
            !end.lossless,
            "three events were shed during this replay; a baseline from the \
             previous connection would have hidden them"
        );
    }

    /// A replay whose `SNAPSHOT_BEGIN` never arrived is still numbered when it
    /// ends, so the bars it lost on the way have to be remembered too.
    #[tokio::test]
    async fn a_replay_without_a_begin_still_reports_what_it_lost() {
        let harness = Harness::start();
        // Capacity one and never read: every bar after the first is dropped.
        let (_rx, lagged) = harness.watch(1, "AAPL{=5m}", 1).await;

        // No begin, just bars — and more of them than the consumer can hold.
        for time in [1_000i64, 2_000, 3_000] {
            harness.send(flagged_candle("AAPL{=5m}", time, 0)).await;
        }
        harness
            .send(flagged_candle(
                "AAPL{=5m}",
                4_000,
                SNAPSHOT_END | REMOVE_EVENT,
            ))
            .await;
        tokio::time::sleep(Duration::from_millis(100)).await;

        assert!(lagged.load(Ordering::Relaxed) >= 1, "bars were dropped");
        let ended = harness
            .resume_of(1, "AAPL{=5m}")
            .expect("the series was seen");
        assert_eq!(ended.phase, SnapshotPhase::Ended);
        assert!(
            !ended.lossless,
            "a replay the venue never announced still lost bars, and saying \
             otherwise reports a history that has holes as complete"
        );
    }

    /// Never dropping a marker has to stop somewhere. A consumer that stops
    /// reading while the connection reconnects would otherwise grow the
    /// backlog for as long as it sulks. The newest phase change is the one
    /// that has to survive.
    #[tokio::test]
    async fn a_marker_backlog_is_bounded_and_keeps_the_newest() {
        let harness = Harness::start();
        // One slot, filled by the first marker and never read.
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 1).await;

        // A series has to exist before a reconnect can open a generation for
        // it. This one fills the single slot and is never read.
        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Far more generations than the backlog can hold.
        let reconnects = MAX_PENDING_MARKERS + 20;
        for _ in 0..reconnects {
            open_generations_for_the_next_connection(
                &harness.progress,
                &harness.routing,
                &harness.history,
                &harness.dxlink_drops,
            )
            .await;
        }

        let parked = harness
            .routing
            .read()
            .await
            .senders
            .get(&1)
            .expect("the consumer is registered")
            .first()
            .expect("one channel")
            .pending
            .lock()
            .expect("not poisoned in tests")
            .len();
        assert!(
            parked <= MAX_PENDING_MARKERS,
            "the backlog grew past its bound: {parked}"
        );

        // The first generation was announced before anything filled up, and
        // what is left behind it is the tail, not the head.
        let mut seen = Vec::new();
        for _ in 0..=MAX_PENDING_MARKERS {
            harness.drained.notify_one();
            match tokio::time::timeout(Duration::from_millis(50), rx.recv()).await {
                Ok(Some(delivery)) => seen.push(begin_of(delivery).generation),
                _ => break,
            }
        }
        let last = *seen.last().expect("something was delivered");
        assert_eq!(
            last,
            (reconnects + 1) as u64,
            "the newest phase change must survive the bound"
        );
    }

    /// The reservation has to survive a cancellation at the point it is most
    /// likely: waiting for room in a full command queue, before the
    /// unsubscribe has been queued at all.
    #[tokio::test]
    async fn a_removal_cancelled_before_it_is_queued_leaves_the_series_intact() {
        let harness = Harness::start();
        let (commands, _command_rx) = mpsc::channel::<DXLinkCommand>(1);
        // Full, so `send` itself is what blocks.
        commands
            .try_send(DXLinkCommand::RemoveEventSender(99))
            .expect("the one slot is free");
        assert!(
            commands
                .try_send(DXLinkCommand::RemoveEventSender(99))
                .is_err(),
            "the queue must actually be full for this test to mean anything"
        );

        let subscription = subscription_for(1, commands, &harness, shared_targets(&["AAPL{=5m}"]));
        let five = CandlePeriod::minutes(5).expect("a period");

        assert!(
            tokio::time::timeout(
                Duration::from_millis(100),
                subscription.remove_candles(&[DxFeedSymbol("AAPL".to_string())], five),
            )
            .await
            .is_err(),
            "the removal must still be waiting to be queued when it is dropped"
        );

        assert_eq!(
            subscription.subscribed(),
            vec![("AAPL{=5m}".to_string(), EventKind::Candle)],
            "a removal that never reached the command queue must leave the \
             series subscribed"
        );
    }

    /// A generation number has to stay unique even when the old replay's
    /// ending is still sitting unread in the consumer's queue.
    #[tokio::test]
    async fn an_unread_ending_cannot_collide_with_a_later_generation() {
        let harness = Harness::start();
        let (mut rx, _) = harness.watch(1, "AAPL{=5m}", 16).await;

        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let loop_handle = spawn_routing_command_loop(command_rx, harness.routing.clone());
        let targets = shared_targets(&["AAPL{=5m}"]);
        let subscription = subscription_for(1, commands, &harness, targets.clone());
        let five = CandlePeriod::minutes(5).expect("a period");

        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        // Deliberately unread: the ending is still queued when the series goes.
        tokio::time::sleep(Duration::from_millis(50)).await;

        subscription
            .remove_candles(&[DxFeedSymbol("AAPL".to_string())], five)
            .await
            .expect("the removal is accepted");

        // Subscribed again, as add_candles would.
        targets_of(&targets).insert(candle_target("AAPL{=5m}"));
        record_routes(
            &harness.routing,
            1,
            &feed_subscriptions(&[candle_target("AAPL{=5m}")]),
        )
        .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 5_000, SNAPSHOT_BEGIN))
            .await;

        // Now read: the old replay, then the new one, and the two generations
        // must not be the same number.
        assert_eq!(begin_of(next(&mut rx).await).generation, 1);
        assert_eq!(bar_time(next(&mut rx).await), 1_000);
        assert_eq!(bar_time(next(&mut rx).await), 2_000);
        let old_end = end_of(next(&mut rx).await).generation;
        let new_begin = begin_of(next(&mut rx).await).generation;
        assert_eq!(old_end, 1);
        assert_eq!(
            new_begin, 2,
            "an ending still in the queue must stay distinguishable from the \
             replay that replaced it"
        );

        loop_handle.abort();
    }

    // ---------------------------------------------------------------------
    // #148: a target is finer-grained than the route that delivers it.
    // ---------------------------------------------------------------------

    /// The interleaving from #148. A `remove_candles` is queued for the
    /// series at one history start, a concurrent `add_candles` re-reserves
    /// the same series at another, and the loop processes the add first. The
    /// remove must then not undo it: the route stays, the venue is not told,
    /// and a bar for the series still reaches the consumer.
    #[tokio::test]
    async fn a_remove_processed_after_a_concurrent_add_leaves_the_new_series_routable() {
        let harness = Harness::start();
        let (mut mine, _) = harness.watch(1, "AAPL{=5m}", 16).await;
        let targets = shared_targets(&["AAPL{=5m}"]);

        // Receives both commands before processing either, then processes
        // the add first, whatever order they were queued in.
        let (commands, mut command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let routing = harness.routing.clone();
        let loop_handle = tokio::spawn(async move {
            let first = command_rx.recv().await.expect("the remove is queued");
            let second = command_rx.recv().await.expect("the add is queued");
            assert!(
                matches!(first, DXLinkCommand::Unsubscribe(..)),
                "the remove reaches the queue first"
            );
            let mut wire = Vec::new();
            for cmd in [second, first] {
                match cmd {
                    DXLinkCommand::Subscribe(requests, _, sub_id, held, ack) => {
                        assert!(
                            withdrawn_request(&held, &requests).is_none(),
                            "the add's target is still reserved"
                        );
                        record_routes(&routing, sub_id, &requests).await;
                        answer(ack, Ok(()));
                    }
                    DXLinkCommand::Unsubscribe(requests, sub_id, still_held, ack) => {
                        let plan =
                            plan_unsubscribe(&routing, sub_id, still_held.as_deref(), &requests)
                                .await;
                        wire.extend(plan.orphaned.into_iter().map(|request| request.symbol));
                        forget_routes(&routing, sub_id, &plan.released).await;
                        answer(ack, Ok(()));
                    }
                    _ => {}
                }
            }
            wire
        });

        let subscription = subscription_for(1, commands, &harness, targets.clone());
        let five = CandlePeriod::minutes(5).expect("a period");
        let later = DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp");

        // `join!` polls in order: the remove takes its target out and queues
        // first, the add then finds the series absent and re-reserves it.
        let symbols = [DxFeedSymbol("AAPL".to_string())];
        let (removed, added) = tokio::join!(
            subscription.remove_candles(&symbols, five),
            subscription.add_candles(&symbols, five, later),
        );
        removed.expect("the removal is accepted");
        added.expect("the re-subscription is accepted");

        let wire = loop_handle.await.expect("the stand-in loop finishes");
        assert!(
            wire.is_empty(),
            "the series the add just subscribed must not be taken off the wire: {wire:?}"
        );
        assert_eq!(
            targets_of(&targets).iter().cloned().collect::<Vec<_>>(),
            vec![FeedTarget {
                kind: EventKind::Candle,
                symbol: "AAPL{=5m}".to_string(),
                from_time: Some(later.timestamp_millis()),
            }],
            "only the newer target survives"
        );
        assert!(
            harness
                .routing
                .read()
                .await
                .routes
                .get(&("AAPL{=5m}".to_string(), EventKind::Candle))
                .is_some_and(|holders| holders.contains(&1)),
            "the surviving target must still be routable"
        );

        harness.send(flagged_candle("AAPL{=5m}", 5_000, 0)).await;
        assert_eq!(
            bar_time(next(&mut mine).await),
            5_000,
            "a bar for the surviving series reaches the consumer"
        );
    }

    /// The other half of the property: when the subscription holds no other
    /// target for the series, the route goes and so does the series on the
    /// wire when nobody else has it. The same symbol at another period is
    /// another series and does not count.
    #[tokio::test]
    async fn removing_the_only_target_of_a_series_still_releases_it() {
        let harness = Harness::start();
        let (mut mine, _) = harness.watch(1, "AAPL{=5m}", 16).await;
        also_watch(&harness, 1, "AAPL{=h}").await;

        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let (loop_handle, unsubscribed) =
            spawn_recording_command_loop(command_rx, harness.routing.clone());
        let targets = shared_targets(&["AAPL{=5m}", "AAPL{=h}"]);
        let subscription = subscription_for(1, commands, &harness, targets.clone());
        let five = CandlePeriod::minutes(5).expect("a period");

        subscription
            .remove_candles(&[DxFeedSymbol("AAPL".to_string())], five)
            .await
            .expect("the removal is accepted");

        assert_eq!(
            unsubscribed.lock().expect("not poisoned in tests").clone(),
            vec!["AAPL{=5m}".to_string()],
            "the last holder's series leaves the wire"
        );
        assert!(
            !harness
                .routing
                .read()
                .await
                .routes
                .contains_key(&("AAPL{=5m}".to_string(), EventKind::Candle)),
            "the route goes with the only target"
        );
        assert_eq!(
            subscription.subscribed(),
            vec![("AAPL{=h}".to_string(), EventKind::Candle)],
            "the hourly series is untouched"
        );

        harness.send(flagged_candle("AAPL{=5m}", 5_000, 0)).await;
        nothing_more(&mut mine).await;

        loop_handle.abort();
    }

    /// The decision on its own, for the shapes the loop can meet.
    #[tokio::test]
    async fn a_plan_releases_only_what_the_subscription_no_longer_holds() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let requested = feed_subscriptions(&[candle_target("AAPL{=5m}")]);
        record_routes(&routing, 1, &requested).await;

        // Re-reserved at another history start: nothing is released.
        let held = Arc::new(Mutex::new(BTreeSet::from([FeedTarget {
            from_time: Some(99),
            ..candle_target("AAPL{=5m}")
        }])));
        let plan = plan_unsubscribe(&routing, 1, Some(&held), &requested).await;
        assert!(plan.released.is_empty(), "the series is still wanted");
        assert!(plan.orphaned.is_empty(), "so the venue hears nothing");

        // Another period of the same underlying is another series.
        let other_period = shared_targets(&["AAPL{=h}"]);
        let plan = plan_unsubscribe(&routing, 1, Some(&other_period), &requested).await;
        assert_eq!(plan.released.len(), 1, "the five-minute series is released");
        assert_eq!(plan.orphaned.len(), 1, "and nobody else holds it");

        // A close releases everything the set says.
        let plan = plan_unsubscribe(&routing, 1, None, &requested).await;
        assert_eq!(plan.released.len(), 1);
        assert_eq!(plan.orphaned.len(), 1);

        // Shared with another subscription: released locally, kept on the wire.
        record_routes(&routing, 2, &requested).await;
        let plan = plan_unsubscribe(&routing, 1, None, &requested).await;
        assert_eq!(plan.released.len(), 1);
        assert!(plan.orphaned.is_empty(), "somebody else is still watching");
    }

    /// A subscribe queued before a close and processed after it must be
    /// refused: `close_sub` removes the sender last, so its absence is the
    /// loop's record that the subscription is gone.
    #[tokio::test]
    async fn a_subscription_without_a_sender_is_closed_to_the_loop() {
        let harness = Harness::start();
        let (_mine, _) = harness.watch(1, "AAPL{=5m}", 4).await;

        assert!(subscription_is_open(&harness.routing, 1).await);
        assert!(!subscription_is_open(&harness.routing, 2).await);

        harness.routing.write().await.senders.remove(&1);
        assert!(
            !subscription_is_open(&harness.routing, 1).await,
            "once the sender is gone, so is the subscription"
        );
    }

    /// The mirror of the remove case: a subscribe whose target was withdrawn
    /// between its reservation and the loop processing it must be refused,
    /// or it records a route for a target the subscription no longer claims
    /// and puts a series on the wire that nothing later takes off.
    #[tokio::test]
    async fn a_subscribe_whose_target_was_withdrawn_is_refused() {
        let harness = Harness::start();
        let (_mine, _) = harness.watch(1, "MSFT{=5m}", 4).await;
        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let (loop_handle, unsubscribed) =
            spawn_recording_command_loop(command_rx, harness.routing.clone());
        let targets = shared_targets(&["MSFT{=5m}"]);
        let subscription = subscription_for(1, commands, &harness, targets.clone());
        let five = CandlePeriod::minutes(5).expect("a period");
        let start = DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp");
        let symbols = [DxFeedSymbol("AAPL".to_string())];

        // `join!` polls in order: the add reserves and queues, then the
        // target is withdrawn as `remove_candles` would before the loop
        // gets to the command.
        let (added, ()) = tokio::join!(subscription.add_candles(&symbols, five, start), async {
            targets_of(&targets).retain(|target| target.symbol != "AAPL{=5m}");
        });
        let error = added.expect_err("a withdrawn target is not subscribed");
        assert!(
            error.to_string().contains("AAPL{=5m}"),
            "the caller is told which series went missing: {error}"
        );

        assert!(
            !harness
                .routing
                .read()
                .await
                .routes
                .contains_key(&("AAPL{=5m}".to_string(), EventKind::Candle)),
            "no route for a target nobody holds"
        );
        assert_eq!(
            subscription.subscribed(),
            vec![("MSFT{=5m}".to_string(), EventKind::Candle)],
            "the reservation was given back and the other series is untouched"
        );
        assert!(
            unsubscribed
                .lock()
                .expect("not poisoned in tests")
                .is_empty(),
            "nothing reached the venue"
        );

        loop_handle.abort();
    }

    /// A series a subscribe never landed under has no route, and a request
    /// for it must not reach the venue as an unsubscribe: the venue was never
    /// asked to add it.
    #[tokio::test]
    async fn a_request_without_a_route_is_not_sent_to_the_venue() {
        let routing: Arc<RwLock<EventRouting>> = Arc::new(RwLock::new(EventRouting::default()));
        let never_landed = feed_subscriptions(&[candle_target("AAPL{=5m}")]);
        assert!(
            orphaned_subscriptions(&routing, 1, &never_landed)
                .await
                .is_empty(),
            "a series with no route is not on the wire for anybody"
        );
    }

    /// Once a close has begun, an `add_candles` on the same subscription is
    /// refused rather than subscribing a series that the sender about to be
    /// removed could never deliver.
    #[tokio::test]
    async fn a_close_refuses_a_target_added_after_it_began() {
        let harness = Harness::start();
        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let (loop_handle, unsubscribed) =
            spawn_recording_command_loop(command_rx, harness.routing.clone());
        let mut streamer = streamer_with(commands, shutdown_tx);
        let subscription = streamer
            .create_sub([EventKind::Candle])
            .await
            .expect("the streamer is open");
        let id = subscription.id;
        let five = CandlePeriod::minutes(5).expect("a period");
        let hourly = CandlePeriod::hours(1).expect("a period");
        let start = DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp");
        let symbols = [DxFeedSymbol("AAPL".to_string())];
        subscription
            .add_candles(&symbols, five, start)
            .await
            .expect("subscribing succeeds");

        // The close drains and flags first; the add then finds the door shut.
        let (closed, added) = tokio::join!(
            streamer.close_sub(id),
            subscription.add_candles(&symbols, hourly, start),
        );
        closed.expect("the close is accepted");
        let error = added.expect_err("a closed subscription accepts nothing");
        assert!(error.to_string().contains("closed"), "{error}");

        assert_eq!(
            unsubscribed.lock().expect("not poisoned in tests").clone(),
            vec!["AAPL{=5m}".to_string()],
            "exactly what was subscribed leaves the wire"
        );
        assert!(subscription.subscribed().is_empty());
        assert!(
            harness.routing.read().await.routes.is_empty(),
            "nothing routes to a closed subscription"
        );
        assert!(streamer.get_sub(id).is_none());

        loop_handle.abort();
    }

    /// The other order: the add reserved and queued before the close began.
    /// Its subscribe reaches the loop after the close drained the set, so it
    /// is refused, and the close takes only what was on the wire off it.
    #[tokio::test]
    async fn a_close_beats_a_subscribe_that_reserved_before_it() {
        let harness = Harness::start();
        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let (loop_handle, unsubscribed) =
            spawn_recording_command_loop(command_rx, harness.routing.clone());
        let mut streamer = streamer_with(commands, shutdown_tx);
        let subscription = streamer
            .create_sub([EventKind::Candle])
            .await
            .expect("the streamer is open");
        let id = subscription.id;
        let five = CandlePeriod::minutes(5).expect("a period");
        let hourly = CandlePeriod::hours(1).expect("a period");
        let start = DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp");
        let symbols = [DxFeedSymbol("AAPL".to_string())];
        subscription
            .add_candles(&symbols, five, start)
            .await
            .expect("subscribing succeeds");

        let (added, closed) = tokio::join!(
            subscription.add_candles(&symbols, hourly, start),
            streamer.close_sub(id),
        );
        closed.expect("the close is accepted");
        let error = added.expect_err("the late subscribe is refused");
        assert!(error.to_string().contains("AAPL{=h}"), "{error}");

        assert_eq!(
            unsubscribed.lock().expect("not poisoned in tests").clone(),
            vec!["AAPL{=5m}".to_string()],
            "the hourly series never landed, so the venue is not told to stop it"
        );
        assert!(subscription.subscribed().is_empty());
        assert!(harness.routing.read().await.routes.is_empty());

        loop_handle.abort();
    }

    /// A close the venue refused leaves the subscription exactly as it was:
    /// its targets recorded, and open to further subscribes, because that is
    /// what a retry needs.
    #[tokio::test]
    async fn a_refused_close_reopens_the_subscription() {
        let (commands, mut command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let (shutdown_tx, _shutdown_rx) = oneshot::channel::<()>();
        let loop_handle = tokio::spawn(async move {
            while let Some(cmd) = command_rx.recv().await {
                match cmd {
                    DXLinkCommand::Subscribe(_, _, _, _, ack) => answer(ack, Ok(())),
                    DXLinkCommand::Unsubscribe(_, _, _, ack) => answer(
                        ack,
                        Err(TastyTradeError::Streaming("the venue refused".to_string())),
                    ),
                    _ => {}
                }
            }
        });
        let mut streamer = streamer_with(commands, shutdown_tx);
        let subscription = streamer
            .create_sub([EventKind::Candle])
            .await
            .expect("the streamer is open");
        let id = subscription.id;
        let five = CandlePeriod::minutes(5).expect("a period");
        let hourly = CandlePeriod::hours(1).expect("a period");
        let start = DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp");
        let symbols = [DxFeedSymbol("AAPL".to_string())];
        subscription
            .add_candles(&symbols, five, start)
            .await
            .expect("subscribing succeeds");

        streamer
            .close_sub(id)
            .await
            .expect_err("the venue refused the unsubscribe");

        assert_eq!(
            subscription.subscribed(),
            vec![("AAPL{=5m}".to_string(), EventKind::Candle)],
            "the record a retry needs is still there"
        );
        subscription
            .add_candles(&symbols, hourly, start)
            .await
            .expect("the subscription is open again");
        assert!(
            streamer.get_sub(id).is_some(),
            "the subscription is still there"
        );

        loop_handle.abort();
    }

    /// Polls `future` exactly once, with a waker that wakes nothing.
    ///
    /// For a future that must be driven to a known point and then left there:
    /// a removal that has queued its command but not read the answer.
    fn poll_once<F: std::future::Future>(
        future: std::pin::Pin<&mut F>,
    ) -> std::task::Poll<F::Output> {
        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
        future.poll(&mut cx)
    }

    /// The owner's finding on #150. The loop released nothing wrong, but the
    /// removal's own cleanup ran when its acknowledgement was finally read,
    /// and by then a replacement replay had finished. Resetting the series'
    /// history there erased a history the consumer had been told was in.
    #[tokio::test]
    async fn a_late_removal_does_not_erase_a_finished_replacement_replay() {
        let harness = Harness::start();
        let (mut mine, _) = harness.watch(1, "AAPL{=5m}", 16).await;
        let (commands, command_rx) = mpsc::channel::<DXLinkCommand>(8);
        let (loop_handle, unsubscribed) =
            spawn_recording_command_loop(command_rx, harness.routing.clone());
        let targets = shared_targets(&["AAPL{=5m}"]);
        let subscription = subscription_for(1, commands, &harness, targets.clone());
        let five = CandlePeriod::minutes(5).expect("a period");
        let later = DateTime::from_timestamp(1_700_000_000, 0).expect("a timestamp");
        let symbols = [DxFeedSymbol("AAPL".to_string())];

        // Driven just far enough to take its target out and queue the
        // command. Its acknowledgement stays unread until the end.
        let removal = subscription.remove_candles(&symbols, five);
        tokio::pin!(removal);
        assert!(poll_once(removal.as_mut()).is_pending());
        let released = async {
            while unsubscribed
                .lock()
                .expect("not poisoned in tests")
                .is_empty()
            {
                tokio::task::yield_now().await;
            }
        };
        tokio::time::timeout(Duration::from_secs(2), released)
            .await
            .expect("the loop processes the removal");

        // The series comes back with another history start, and its replay
        // finishes while the removal is still parked.
        subscription
            .add_candles(&symbols, five, later)
            .await
            .expect("re-subscribing succeeds");
        harness
            .send(flagged_candle("AAPL{=5m}", 1_000, SNAPSHOT_BEGIN))
            .await;
        harness
            .send(flagged_candle("AAPL{=5m}", 2_000, SNAPSHOT_END))
            .await;
        begin_of(next(&mut mine).await);
        assert_eq!(bar_time(next(&mut mine).await), 1_000);
        assert_eq!(bar_time(next(&mut mine).await), 2_000);
        end_of(next(&mut mine).await);
        assert!(subscription.history_loaded("AAPL{=5m}"));

        // Only now does the removal read its answer.
        removal.await.expect("the removal was accepted");

        assert!(
            subscription.history_loaded("AAPL{=5m}"),
            "a finished replacement replay must survive an older removal's cleanup"
        );
        assert_eq!(
            subscription.subscribed(),
            vec![("AAPL{=5m}".to_string(), EventKind::Candle)]
        );

        loop_handle.abort();
    }
}