lightstreamer-rs 1.0.0

A Rust client for the Lightstreamer TLCP protocol, for real-time data streaming.
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
//! The session lifecycle state machine: create → bind → loop/rebind →
//! recovery, and the terminal paths out of all of them.
//!
//! This is the only layer that combines the two halves of the client: it
//! drives a [`Transport`], feeds every line it yields through
//! [`crate::protocol::response::parse_line`], and keeps the session alive.
//! Everything protocol-visible here is derived from
//! `docs/spec/02-session-lifecycle.md`, whose §2.2 transition table (T1–T9) is
//! implemented literally and cited transition by transition.
//!
//! # The states
//!
//! The specification defines exactly two session states — **Bound: Session is
//! Streaming** and **Unbound: Session is Buffering** — plus the unnamed
//! pre-session and destroyed endpoints of its state diagram
//! [`docs/spec/02-session-lifecycle.md` §2.1, §2.3]. This module keeps that
//! vocabulary. [`StreamState`] refines *Bound* into the moment before `CONOK`
//! has arrived and the moment after, because a client must time out an
//! unanswered handshake and the spec's two states give it nowhere to say so;
//! the unbound state is not a variant here at all but the interval between two
//! turns of the driver's loop, which is where it naturally lives.
//!
//! # What the caller is told, and why
//!
//! `docs/adr/0005-recovery-is-visible-in-the-event-stream.md` is binding: an
//! application holding derived state must be able to tell an interruption that
//! **preserved continuity** from one that **replaced the session**, and both
//! from a session that is **definitively gone**. That is why [`BindKind`],
//! [`RecoveryOutcome`] and [`SessionClosed`] are separate types with separate
//! variants rather than a single "reconnected" signal: the distinction exists
//! in the protocol, and hiding it would force every application either to
//! rebuild state on every hiccup or to be silently wrong after a real one.
//!
//! # Transport independence
//!
//! Every transport-dependent decision here reads a declared
//! [`TransportProperties`] flag. There is no match on a concrete transport type
//! in this module, and adding a fourth transport requires no change to it
//! (`docs/adr/0007-transport-port-shape.md`).
//!
//! # Concurrency and shutdown
//!
//! [`SessionDriver::run`] is the whole runtime of this layer: one future, no
//! spawned tasks, no detached work. Whoever spawns it owns its lifetime, and
//! every exit path — terminal notification, exhausted retries, caller
//! shutdown, or a dropped [`SessionHandle`] — closes the transport before
//! returning. The driver requires [`Transport::next_line`] to be cancel-safe,
//! since it is polled inside a `select!` alongside the command channel and the
//! liveness timer; a line must not be lost when that future is dropped.
//!
//! Both channels are bounded. The event channel applies backpressure rather
//! than dropping, because a dropped data notification would desynchronise the
//! recovery progressive that makes recovery correct
//! [`docs/spec/02-session-lifecycle.md` §5.2].
//!
//! # The stop lane
//!
//! Backpressure has one exception, and it is deliberate: **an ordered stop is
//! never subject to it**. A caller that stops reading its event stream blocks
//! the driver, which is the documented contract — but it must not thereby make
//! the client unstoppable, because that would leak a task and a socket for the
//! life of the process. [`SessionHandle::stop`] therefore raises a signal that
//! every blocking wait in this module races against: a full event channel, a
//! reconnection delay, and the establishment of a stream connection all give it
//! up at once. Any event that had not yet been delivered when a stop is ordered
//! is discarded, since the stream it was going to is about to end anyway.

// Several of this module's `pub(crate)` operations — `reconfigure`,
// `force_rebind`, `shutdown` and some builder setters — are exercised by this
// file's own tests but not yet reached from the public façade, which exposes a
// deliberately smaller surface than the state machine supports. They are
// covered, not unwritten.
#![allow(dead_code)]

pub(crate) mod backoff;
pub(crate) mod liveness;
pub(crate) mod options;

use std::collections::{BTreeMap, HashMap};
use std::num::NonZeroU32;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use tokio::sync::{mpsc, watch};
use tokio::time::Instant;

use crate::config::ClientConfig;
pub(crate) use crate::protocol::ProtocolError;
use crate::protocol::request::{
    BindSession, ConnectionMode, CreateSession, DestroyCause, DestroySession, ForceRebind,
    Heartbeat, MaxFrequencyLimit, MessageSend, ReconfigureSubscription, RequestId, Subscribe,
    TlcpRequest, TransportKind, Unsubscribe,
};
use crate::protocol::response::{Notification, parse_line};
use crate::session::backoff::Backoff;
use crate::session::liveness::{Liveness, LivenessAction};
use crate::session::options::SessionOptions;
use crate::subscription::manager::SubscriptionManager;
use crate::transport::http::{HttpMode, HttpTransport};
use crate::transport::ws::WsTransport;
use crate::transport::{AnyTransport, TransportError};

// ---------------------------------------------------------------------------
// The vocabulary this layer publishes upward
// ---------------------------------------------------------------------------

// The façade above talks to this module and to nothing below it: the documented
// dependency graph is `client -> session -> {protocol, transport port}`, with no
// edge from `client` to `protocol` or `transport`. Some of what the façade needs
// is nonetheless defined in those layers — a subscription mode is a wire value,
// a decoded update is subscription state — so this block is the curated list of
// what a caller of this module may name.
//
// It is a narrowing, not a formality. Everything not re-exported here — the
// parser, the request encoders, `Notification`, `ProtocolError`, the transports
// — is unreachable from `client`, and `tests/architecture.rs` fails if that
// stops being true. Adding a name here is a deliberate widening of the seam.
pub(crate) use crate::protocol::request::{
    DecimalNumber, RequestedBufferSize, RequestedMaxFrequency, SequenceName, Snapshot,
    SubscriptionMode,
};
pub(crate) use crate::protocol::response::{
    Bandwidth, FilteringMode, MaxFrequency, MessageSequence,
};
pub(crate) use crate::subscription::manager::{
    CommandFields, SubscriptionError, SubscriptionEvent,
};
// `connect` is generic over the port, so the port is part of this layer's own
// signature: a test that drives a whole client over a scripted transport must
// be able to name it. What stays unreachable from above is every *adapter*.
pub(crate) use crate::transport::{EncodedRequest, StreamOpen, Transport, TransportProperties};

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// A failure of the session layer itself, as opposed to a failure the server
/// reported (which is carried in [`ServerCause`], with its code preserved).
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub(crate) enum SessionError {
    /// The options and the transport disagree about something that must match.
    #[error("invalid session configuration: {reason}")]
    Configuration {
        /// What is inconsistent, in terms the caller can act on. Never
        /// contains a credential.
        reason: String,
    },

    /// A request could not be encoded.
    #[error(transparent)]
    Protocol(#[from] ProtocolError),

    /// The driver has stopped, so no further command can be delivered.
    #[error("the session driver has stopped")]
    Stopped,

    /// A monotonic identifier space ran out. Identifiers are never reused
    /// while a response to the old one could still arrive, so exhaustion is
    /// reported rather than wrapped around
    /// [`docs/spec/02-session-lifecycle.md` §4.4].
    #[error("the {what} identifier space is exhausted")]
    Exhausted {
        /// Which space ran out.
        what: &'static str,
    },
}

// ---------------------------------------------------------------------------
// Identity types
// ---------------------------------------------------------------------------

/// A server-assigned session identifier
/// [`docs/spec/02-session-lifecycle.md` §3.1].
///
/// It is opaque: the client echoes it and compares it, never parses it. A
/// rebind or a recovery returns the *same* identifier, which is exactly how
/// this module tells a resumed session from a replaced one — the Hands On
/// transcripts show the identical id across a rebind and a recovery, and a
/// different one after a destroy [`docs/spec/02-session-lifecycle.md` §10, F7,
/// F8, F9].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct SessionId(String);

impl SessionId {
    /// Wraps an identifier as the server sent it.
    #[must_use]
    #[inline]
    pub(crate) fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// The identifier as it goes back on the wire.
    #[must_use]
    #[inline]
    pub(crate) fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for SessionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

/// A caller-facing handle on one subscription, stable for as long as the
/// caller keeps it.
///
/// It is deliberately **not** the protocol's `LS_subId`. That number "must be a
/// progressive integer number starting with 1, and must be unique within the
/// session" [`docs/spec/02-session-lifecycle.md` §4.4], so it necessarily
/// restarts at 1 when a lost session is replaced — the spec does not even say
/// whether a client should try to preserve its old numbering
/// [ibid. §9.5, ambiguity A17]. Giving the caller a key that outlives the
/// session, and renumbering the wire ids underneath it, makes that a
/// non-question: no subscription changes identity because the session did.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct SubscriptionKey(u64);

impl SubscriptionKey {
    /// The key's opaque numeric value, for logging and for a façade that needs
    /// a map key.
    #[must_use]
    #[inline]
    pub(crate) const fn get(self) -> u64 {
        self.0
    }

    /// A key with a chosen value, for [`crate::test_util`] to hand a consumer
    /// a `SubscriptionId` its tests can name. No session ever allocates one
    /// this way.
    #[cfg(feature = "test-util")]
    #[must_use]
    pub(crate) const fn from_raw(id: u64) -> Self {
        Self(id)
    }
}

/// What the caller asked to subscribe to.
///
/// This is the *desired* subscription, held for the lifetime of the session
/// driver. The wire request it turns into ([`Subscribe`]) additionally needs a
/// session id, a request id and an `LS_subId`, all of which belong to one
/// particular session and are re-derived every time the subscription is issued
/// [`docs/spec/03-requests.md` §6.1].
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SubscriptionSpec {
    /// `LS_group` — the item-group name, interpreted by the Metadata Adapter.
    pub(crate) group: String,
    /// `LS_schema` — the field-schema name.
    pub(crate) schema: String,
    /// `LS_mode` — the subscription mode of every item in the group.
    pub(crate) mode: SubscriptionMode,
    /// `LS_data_adapter` — which Data Adapter provides the items.
    pub(crate) data_adapter: Option<String>,
    /// `LS_selector` — a Metadata-Adapter-interpreted selector.
    pub(crate) selector: Option<String>,
    /// `LS_requested_buffer_size` — per-item buffer dimension.
    pub(crate) requested_buffer_size: Option<RequestedBufferSize>,
    /// `LS_requested_max_frequency` — per-item update frequency ceiling.
    pub(crate) requested_max_frequency: Option<RequestedMaxFrequency>,
    /// `LS_snapshot` — whether, and how much, snapshot to request.
    ///
    /// This is the field that decides whether a re-established subscription
    /// restarts from a snapshot, which the caller is told about in
    /// [`ResubscribedEntry::snapshot_requested`]
    /// (`docs/adr/0005-recovery-is-visible-in-the-event-stream.md`).
    pub(crate) snapshot: Option<Snapshot>,

    /// The caller's own names for the items, in order, or empty when the
    /// subscription named a server-side group.
    ///
    /// Not a request parameter: TLCP transmits item counts and never names
    /// [`docs/spec/04-notifications.md` §3.1], so these exist only to label the
    /// updates this session hands upward. They travel with the spec because the
    /// [`SubscriptionManager`] that needs them is rebuilt from it whenever a
    /// replacement session re-establishes the subscription.
    pub(crate) declared_items: Vec<String>,
    /// The caller's own names for the fields — see
    /// [`declared_items`](Self::declared_items).
    pub(crate) declared_fields: Vec<String>,
}

impl SubscriptionSpec {
    /// A minimal subscription: a group, a schema and a mode.
    #[must_use]
    pub(crate) fn new(
        group: impl Into<String>,
        schema: impl Into<String>,
        mode: SubscriptionMode,
    ) -> Self {
        Self {
            group: group.into(),
            schema: schema.into(),
            mode,
            data_adapter: None,
            selector: None,
            requested_buffer_size: None,
            requested_max_frequency: None,
            snapshot: None,
            declared_items: Vec::new(),
            declared_fields: Vec::new(),
        }
    }

    /// Whether the caller asked for a snapshot.
    #[must_use]
    #[inline]
    fn wants_snapshot(&self) -> bool {
        matches!(self.snapshot, Some(Snapshot::On | Snapshot::Length(_)))
    }

    /// A fresh [`SubscriptionManager`] for this subscription.
    ///
    /// Built from the spec rather than handed down from the caller, so that a
    /// subscription re-established on a replacement session gets a state
    /// machine with no memory of the old one — the server restarts its flow, so
    /// the baseline must restart with it
    /// [`docs/spec/02-session-lifecycle.md` §9.5].
    #[must_use]
    fn new_manager(&self) -> SubscriptionManager {
        SubscriptionManager::new(
            self.mode,
            self.wants_snapshot(),
            self.declared_items.clone(),
            self.declared_fields.clone(),
        )
    }
}

/// A message the caller wants delivered to the Metadata Adapter
/// [`docs/spec/03-requests.md` §12].
///
/// As with [`SubscriptionSpec`], this is what the caller asked for; the
/// session id and the `LS_reqId` belong to one particular connection and are
/// filled in when the request is issued.
///
/// # The progressive is the caller's, not this client's
///
/// `LS_msg_prog` is "the progressive number of this message, starting at 1",
/// it orders messages within a sequence, and it is what identifies the message
/// in `MSGDONE`/`MSGFAIL` [`docs/spec/03-requests.md` §12.1]. This layer does
/// **not** allocate it. The server tracks the numbering per session — its
/// duplicate and skipped-progressive codes (`32`, `33`, `38`, `39`
/// [`docs/spec/05-error-codes.md` §2]) are session-scoped — so a client-side
/// counter would silently restart whenever a session was replaced, at exactly
/// the moment the caller most needs to know what happened to a message. The
/// caller owns the sequence numbering, and this layer never renumbers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct OutgoingMessage {
    /// `LS_message` — the payload. Its meaning is entirely the Metadata
    /// Adapter's [`docs/spec/03-requests.md` §12.1].
    pub(crate) message: String,
    /// `LS_sequence` — messages sharing a sequence are processed in
    /// `LS_msg_prog` order. `None` lets the server process the message at once,
    /// possibly concurrently with others.
    pub(crate) sequence: Option<SequenceName>,
    /// `LS_msg_prog` — this message's position in its sequence, and its
    /// identity in `MSGDONE`/`MSGFAIL`. Mandatory whenever a sequence is given
    /// or an outcome is requested; [`MessageSend`] rejects the combination that
    /// omits it, so a missing one surfaces as
    /// [`MessageResult::NotSent`] rather than as a server error.
    pub(crate) msg_prog: Option<NonZeroU32>,
    /// `LS_max_wait` — how long the server may wait for earlier messages of the
    /// same sequence before processing this one. Only meaningful alongside a
    /// sequence.
    pub(crate) max_wait_millis: Option<u64>,
    /// `LS_outcome` — `Some(false)` suppresses the `MSGDONE`/`MSGFAIL`
    /// notification, making the send fire-and-forget. `None` leaves the
    /// server's default of `true` [`docs/spec/03-requests.md` §12.1].
    pub(crate) outcome: Option<bool>,
}

impl OutgoingMessage {
    /// A fire-and-forget message with no sequence and no progressive.
    ///
    /// `LS_outcome` is set to `false` because `LS_msg_prog` "is mandatory
    /// whenever `LS_sequence` is specified or `LS_outcome` is set to `true`",
    /// and `LS_outcome` defaults to `true`
    /// [`docs/spec/03-requests.md` §12.1] — so a message with no progressive
    /// can only be sent by explicitly declining the outcome.
    #[must_use]
    pub(crate) fn fire_and_forget(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            sequence: None,
            msg_prog: None,
            max_wait_millis: None,
            outcome: Some(false),
        }
    }

    /// A message identified by a progressive, whose outcome will be reported.
    #[must_use]
    pub(crate) fn numbered(message: impl Into<String>, msg_prog: NonZeroU32) -> Self {
        Self {
            message: message.into(),
            sequence: None,
            msg_prog: Some(msg_prog),
            max_wait_millis: None,
            outcome: None,
        }
    }

    /// Places this message in an ordered sequence.
    #[must_use = "builders do nothing unless the result is used"]
    pub(crate) fn in_sequence(mut self, sequence: SequenceName) -> Self {
        self.sequence = Some(sequence);
        self
    }

    /// How this message identifies itself in `MSGDONE`/`MSGFAIL`.
    #[must_use]
    fn identity(&self) -> (MessageSequence, Option<u64>) {
        let sequence = match &self.sequence {
            Some(name) => MessageSequence::Named(name.as_str().to_owned()),
            // The server echoes the literal `*` when the send named no
            // sequence [`docs/spec/04-notifications.md` §4.1].
            None => MessageSequence::Unspecified,
        };
        (sequence, self.msg_prog.map(|prog| u64::from(prog.get())))
    }
}

// ---------------------------------------------------------------------------
// Outcome types
// ---------------------------------------------------------------------------

/// A cause code and message exactly as the server supplied them.
///
/// The code is preserved as a number and never folded into the message, so a
/// caller can branch on it [`docs/spec/05-error-codes.md` §1]. Codes `<= 0`
/// are Metadata-Adapter-defined and application-specific [ibid. §2].
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ServerCause {
    /// The `<error-code>` or `<cause-code>` from Appendix A.
    pub(crate) code: i64,
    /// The accompanying human-readable text, which may be empty.
    pub(crate) message: String,
}

/// How a session came to be bound, and therefore what the caller may keep.
///
/// This is the ADR-0005 distinction at its sharpest: [`BindKind::Rebound`] and
/// [`BindKind::Recovering`] leave derived state valid, [`BindKind::Recreated`]
/// invalidates it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum BindKind {
    /// T1 — the first session of this driver was created
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T1].
    Created,

    /// T1 again, but after an earlier session was lost: a **new** session with
    /// a new identifier, its progressive restarted at zero and its
    /// subscriptions re-executed from scratch
    /// [`docs/spec/02-session-lifecycle.md` §6.1, §9.5]. Derived state built
    /// on the previous session must be discarded.
    Recreated {
        /// The identifier of the session this one replaces, when there was one.
        previous: Option<SessionId>,
    },

    /// T6 after a clean `LOOP` — the same session rebound with no recovery
    /// request. The server "restarts sending real-time notifications from where
    /// it stopped. In particular, all subscriptions are preserved, with all
    /// their items and fields" [`docs/spec/02-session-lifecycle.md` §4.4].
    Rebound,

    /// T6 with `LS_recovery_from` — the same session, resuming from a stated
    /// progressive. Whether the resumption was exact is reported separately by
    /// [`SessionEvent::Recovered`], because `PROG` arrives after `CONOK`
    /// [`docs/spec/02-session-lifecycle.md` §5.4].
    Recovering {
        /// The progressive the client asked to resume from.
        requested_progressive: u64,
    },
}

/// Where a recovered flow actually resumed, relative to where it was asked to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct RecoveryOutcome {
    /// What `LS_recovery_from` carried.
    pub(crate) requested: u64,
    /// What `PROG` answered.
    pub(crate) resumed_at: u64,
    /// The relationship between the two.
    pub(crate) kind: RecoveryKind,
}

/// The three ways a `PROG` can relate to the requested progressive.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RecoveryKind {
    /// The server resumed exactly where the client asked. Nothing was lost and
    /// nothing is duplicated.
    Exact,

    /// The server resumed **earlier** than asked, which it is explicitly
    /// allowed to do. The duplicated notifications are discarded by this client
    /// before the caller sees them, as the spec's fourth recovery precondition
    /// requires: "the first data notifications received are discarded, until
    /// the desired point is reached"
    /// [`docs/spec/02-session-lifecycle.md` §5.2].
    Duplicated {
        /// How many notifications this client suppressed.
        count: u64,
    },

    /// The server resumed **later** than asked: the notifications in between
    /// are gone.
    ///
    /// SPEC-AMBIGUITY (A7): `PROG` "can be lower" than requested, but the spec
    /// "never states whether it can be *higher* than requested, nor what a
    /// client must do if it is"
    /// [`docs/spec/02-session-lifecycle.md` §5.4]. Reporting the gap is the
    /// defensive choice — silently continuing would tell an application that
    /// continuity was preserved when it was not, which
    /// `docs/adr/0005-recovery-is-visible-in-the-event-stream.md` calls out as
    /// worse than saying nothing.
    Gap {
        /// How many notifications were skipped by the server.
        missing: u64,
    },
}

/// Why the session stopped being bound to a stream connection.
///
/// One variant per edge leaving *Bound* in the specification's own state
/// diagram [`docs/spec/02-session-lifecycle.md` §2.2, §2.3], plus the two
/// cases the diagram has no edge for: a stalled-but-not-broken connection
/// (§8.1) and a rejected bind (§6.2).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum UnbindReason {
    /// T2 — `Force-Unbound by Client`: the client sent `LS_op=force_rebind`
    /// and the server answered with `LOOP`
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T2].
    ForcedByClient {
        /// The delay `LOOP` asked for before rebinding.
        expected_delay: Duration,
    },

    /// T3 — `Content-Length Reached`: the HTTP stream connection exhausted its
    /// byte budget [`docs/spec/02-session-lifecycle.md` §2.2, T3].
    ContentLengthReached {
        /// The delay `LOOP` asked for before rebinding.
        expected_delay: Duration,
    },

    /// T4 — `Poll Cycle Expired`: the polling cycle completed
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T4].
    PollCycleExpired {
        /// The delay `LOOP` asked for before rebinding. On a synchronous
        /// polling session this may be non-zero
        /// [`docs/spec/02-session-lifecycle.md` §4.2].
        expected_delay: Duration,
    },

    /// A `LOOP` arrived on a transport whose declared properties identify none
    /// of T2, T3 or T4. Rebinding is the same either way; only the label
    /// differs.
    Looped {
        /// The delay `LOOP` asked for before rebinding.
        expected_delay: Duration,
    },

    /// T5 — `Connection Failed`: "a stream connection is dropped by any cause"
    /// and no notification is received. The session is *not* closed and a
    /// rebind — specifically, a recovery — is still allowed
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T5].
    ConnectionFailed {
        /// What the transport reported. Never contains a credential.
        detail: String,
    },

    /// The stream went quiet past its keepalive budget without ever failing.
    ///
    /// Not an edge in the p.8 diagram, but the spec's own prescription: "if
    /// after the expected interval plus a configurable timeout no `PROBE` has
    /// been received, the connection is closed and reopened"
    /// [`docs/spec/02-session-lifecycle.md` §8.1], and "if a connection becomes
    /// mute, the client can issue a recovery request" [ibid. §5.1].
    KeepaliveExpired {
        /// The budget that elapsed with no traffic at all.
        budget: Duration,
    },

    /// A `create_session` or `bind_session` was answered with a `CONERR` whose
    /// code permits another attempt [`docs/spec/02-session-lifecycle.md` §6.2].
    Rejected {
        /// The server's cause, code preserved.
        cause: ServerCause,
    },

    /// The server ended the session with a code that tells the client to open a
    /// new one at once — code `48`, "the client should recover by opening a new
    /// session immediately" [`docs/spec/02-session-lifecycle.md` §6.2].
    ServerRefresh {
        /// The server's cause, code preserved.
        cause: ServerCause,
    },
}

impl UnbindReason {
    /// Whether this unbind followed a clean `LOOP`, after which a plain rebind
    /// is safe: "the Server will assume that all updates sent in the previous
    /// connection (up to the `LOOP` notification) have been received"
    /// [`docs/spec/02-session-lifecycle.md` §4.4].
    #[must_use]
    const fn is_clean_loop(&self) -> bool {
        matches!(
            self,
            Self::ForcedByClient { .. }
                | Self::ContentLengthReached { .. }
                | Self::PollCycleExpired { .. }
                | Self::Looped { .. }
        )
    }

    /// The delay the server asked for before rebinding, if it named one.
    #[must_use]
    const fn expected_delay(&self) -> Option<Duration> {
        match self {
            Self::ForcedByClient { expected_delay }
            | Self::ContentLengthReached { expected_delay }
            | Self::PollCycleExpired { expected_delay }
            | Self::Looped { expected_delay } => Some(*expected_delay),
            _ => None,
        }
    }
}

/// Why the session ended, terminally. The driver has stopped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SessionClosed {
    /// T7 or T8 — the client ended it
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T7/T8].
    ByClient {
        /// Whether the server confirmed the destroy with an `END`.
        ///
        /// `false` when the caller merely shut the driver down, or when the
        /// destroy could not be confirmed. Dropping the connection without a
        /// destroy leaves the session buffering server-side until its timeout
        /// [`docs/spec/02-session-lifecycle.md` §6.3], which is the reason the
        /// destroy path exists at all.
        destroy_confirmed: bool,
        /// The cause the server reported in `END`, when it sent one. The
        /// default for a client destroy is code `31`
        /// [`docs/spec/02-session-lifecycle.md` §10, F5].
        cause: Option<ServerCause>,
    },

    /// The server closed the session, or refused to bind it, with a code that
    /// admits no retry [`docs/spec/02-session-lifecycle.md` §6.2].
    ByServer {
        /// The server's cause, code preserved.
        cause: ServerCause,
    },

    /// The reconnect budget was spent without ever binding again. This is T9 in
    /// practice: the session is treated as definitively lost
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T9].
    RetriesExhausted {
        /// How many consecutive attempts failed.
        attempts: u32,
        /// The last thing that went wrong, when there was a last thing.
        last: Option<UnbindReason>,
    },

    /// The driver could not even build the request it needed to send. A bug or
    /// an impossible configuration, never a server condition.
    Internal {
        /// What failed. Never contains a credential.
        reason: String,
    },
}

// ---------------------------------------------------------------------------
// Events
// ---------------------------------------------------------------------------

/// What the caller learns about one bind.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BoundInfo {
    /// The session that is now bound.
    pub(crate) session_id: SessionId,
    /// How it came to be bound, and therefore what derived state survives.
    pub(crate) kind: BindKind,
    /// `CONOK` argument 3: the longest inactivity the server guarantees
    /// [`docs/spec/02-session-lifecycle.md` §8.1]. This, not any configured
    /// value, is what the client's liveness budget is built from.
    pub(crate) keep_alive: Duration,
    /// `CONOK` argument 2: the maximum request length the server accepts, in
    /// bytes [`docs/spec/02-session-lifecycle.md` §3.1].
    pub(crate) request_limit_bytes: u64,
    /// `CONOK` argument 4: the control link, or `None` when the server sent the
    /// literal `*` [`docs/spec/02-session-lifecycle.md` §3.1].
    pub(crate) control_link: Option<String>,
}

/// One subscription re-issued on a newly bound session.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ResubscribedEntry {
    /// The caller's stable key, unchanged across the re-establishment.
    pub(crate) key: SubscriptionKey,
    /// The `LS_subId` allocated for it in the new session.
    pub(crate) subscription_id: NonZeroU32,
    /// Whether this subscription had already been confirmed by the server
    /// before the session was replaced. `false` means it was still pending.
    pub(crate) previously_active: bool,
    /// Whether the subscription asks for a snapshot, and will therefore restart
    /// from one rather than resume
    /// (`docs/adr/0005-recovery-is-visible-in-the-event-stream.md`).
    pub(crate) snapshot_requested: bool,
}

/// Which subscription operation a control request was performing.
///
/// The distinction is not cosmetic: a refused `add` means the subscription
/// does not exist, while a refused `delete` means it very much still does.
/// A caller told only "rejected" would get one of the two backwards.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SubscriptionOperation {
    /// `LS_op=add` — the subscription was never established. It has been
    /// dropped from the desired set and will **not** be re-issued on a later
    /// session.
    Subscribe,
    /// `LS_op=delete` — the subscription is **still active**; the caller's
    /// request to remove it did not take effect.
    Unsubscribe,
    /// `LS_op=reconf` — the subscription is unchanged and still active; only
    /// the frequency change failed.
    Reconfigure,
}

/// What a control request was acting on.
///
/// Control responses correlate by `LS_reqId`, which is this layer's private
/// sequence — the caller never sees one and so could never map a response back
/// to the thing it asked for. Naming the target closes that gap: a rejection
/// that cannot be routed to the subscription it killed is a silently lost
/// subscription, whatever the event stream says.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ControlTarget {
    /// The request concerned the session as a whole — `force_rebind`,
    /// `destroy`, `msg` — or the response could not be correlated to any
    /// request at all.
    Session,
    /// The request concerned one subscription.
    Subscription {
        /// Which one, in the caller's own stable terms.
        key: SubscriptionKey,
        /// What was being attempted, and therefore what the failure means for
        /// the subscription's state.
        operation: SubscriptionOperation,
    },
}

/// How a control request ended.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ControlOutcome {
    /// `REQOK` — the server accepted the request
    /// [`docs/spec/03-requests.md` §13.1].
    Accepted,
    /// `REQERR` or `ERROR` — the server refused it, with an Appendix B code
    /// [`docs/spec/05-error-codes.md` §1].
    Rejected {
        /// The server's cause, code preserved.
        cause: ServerCause,
    },
    /// The request never reached the server: the transport refused it or the
    /// encoder rejected the parameters. No server code exists for this.
    NotSent {
        /// What went wrong. Never contains a credential.
        reason: String,
    },
}

/// What became of one message sent with `msg`
/// [`docs/spec/03-requests.md` §12].
///
/// A message can fail in three places, and the caller needs to tell them
/// apart: it may never leave this client, the server may refuse to accept it,
/// or the Metadata Adapter may fail while processing it. Only the third is a
/// `MSGFAIL`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum MessageResult {
    /// `MSGDONE` — the Metadata Adapter's `notifyUserMessage` returned without
    /// raising [`docs/spec/04-notifications.md` §4.1].
    Done {
        /// The adapter's response text. Empty when it supplied none.
        response: String,
    },

    /// `MSGFAIL` — the message was not delivered, or its processing failed
    /// [`docs/spec/04-notifications.md` §4.2].
    Failed {
        /// The server's cause, code preserved. Appendix B codes `32`, `33`,
        /// `38` and `39` concern the sequence numbering specifically
        /// [`docs/spec/05-error-codes.md` §2].
        cause: ServerCause,
    },

    /// `REQERR` or `ERROR` — the server refused the request outright, so no
    /// `MSGDONE`/`MSGFAIL` will ever follow
    /// [`docs/spec/03-requests.md` §13.2].
    Refused {
        /// The server's cause, code preserved.
        cause: ServerCause,
    },

    /// The message never reached the server: nothing was bound to send it on,
    /// the transport refused it, or its parameters could not be encoded.
    ///
    /// **Messages are not buffered.** A subscription is a statement of desired
    /// state and is safely re-issued on whatever session comes next; a message
    /// is a one-shot side effect whose ordering and de-duplication the server
    /// tracks *per session* [`docs/spec/03-requests.md` §12.1;
    /// `docs/spec/05-error-codes.md` §2, codes 32/33/38/39]. Re-issuing one
    /// across a session replacement would restart the numbering the server
    /// deduplicates against, so this client fails the send instead of
    /// promising an ordering it cannot keep. Retrying is the caller's
    /// decision, which is the only place the decision can be made correctly.
    NotSent {
        /// What stopped it. Never contains a credential.
        reason: String,
    },
}

/// What one subscription notification meant, or the typed reason it could not
/// be reconciled with what the server already said.
///
/// A line this client cannot decode is reported, never fatal: the subscription
/// stays alive and the caller is told which line was lost
/// (`docs/adr/0003-typed-event-stream-as-delivery-surface.md`).
pub(crate) type SubscriptionOutcome = Result<SubscriptionEvent, SubscriptionError>;

/// Something the server said about itself, rather than about the data
/// [`docs/spec/02-session-lifecycle.md` §5.3].
///
/// A typed alternative to passing the `SERVNAME`, `CLIENTIP`, `SYNC` and `CONS`
/// notifications upward verbatim: the parser's own types stop at this layer, so
/// the façade above cannot come to depend on them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ServerAnnouncement {
    /// `SERVNAME` — the server's configured name
    /// [`docs/spec/04-notifications.md` §5.4].
    Name(String),
    /// `CLIENTIP` — the address the server sees this client at
    /// [`docs/spec/04-notifications.md` §5.5].
    ClientIp(String),
    /// `SYNC` — how long the server believes the session has been running
    /// [`docs/spec/04-notifications.md` §5.3].
    Clock {
        /// Seconds since the session began, as the server counts them.
        elapsed_seconds: u64,
    },
    /// `CONS` — the bandwidth ceiling now in force
    /// [`docs/spec/04-notifications.md` §5.2].
    Bandwidth(Bandwidth),
}

/// Everything the session layer tells the layer above it.
///
/// The variants that carry the ADR-0005 distinctions are [`SessionEvent::Bound`]
/// (through [`BindKind`]), [`SessionEvent::Recovered`] and
/// [`SessionEvent::Closed`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SessionEvent {
    /// The session is bound to a stream connection and streaming
    /// [`docs/spec/02-session-lifecycle.md` §2.1].
    Bound(Box<BoundInfo>),

    /// A recovery bind reported where it resumed
    /// [`docs/spec/02-session-lifecycle.md` §5.4].
    Recovered(RecoveryOutcome),

    /// Subscriptions were re-issued on a newly bound session. Emitted whenever
    /// any `add` is sent at bind time, which after a session replacement is
    /// every one of them [`docs/spec/02-session-lifecycle.md` §6.1].
    Resubscribed(Vec<ResubscribedEntry>),

    /// The session left the bound state. A reconnect is already scheduled
    /// unless `retry_in` is `None`, which means the driver is about to stop.
    Unbound {
        /// Which edge of the state diagram was taken.
        reason: UnbindReason,
        /// How long the driver will wait before its next attempt.
        retry_in: Option<Duration>,
    },

    /// What became of a message the caller sent
    /// [`docs/spec/03-requests.md` §12].
    ///
    /// `MSGDONE` and `MSGFAIL` **are** data notifications and are counted
    /// toward the recovery progressive like any other
    /// [`docs/spec/02-session-lifecycle.md` §5.3] — they simply reach the
    /// caller through this typed variant instead of through
    /// [`SessionEvent::Data`], because it can answer "what happened to my
    /// message" in one match arm rather than three.
    Message {
        /// Its position in the session's data-notification count, or `None`
        /// when the message never left this client and so the server never
        /// counted anything.
        progressive: Option<u64>,
        /// The sequence the message was sent in, or
        /// [`MessageSequence::Unspecified`].
        sequence: MessageSequence,
        /// The `LS_msg_prog` that identifies the message, or `None` when it
        /// carried none — only possible with `LS_outcome=false`, in which case
        /// the sole result that can arrive is [`MessageResult::NotSent`] or
        /// [`MessageResult::Refused`].
        prog: Option<u64>,
        /// What became of it.
        result: MessageResult,
    },

    /// Something happened to one subscription
    /// [`docs/spec/04-notifications.md` §2, §3].
    ///
    /// The notification that caused it has already been applied to that
    /// subscription's item state here, so what travels upward is the meaning,
    /// not the line. Duplicates suppressed after a recovery never appear, and
    /// `MSGDONE`/`MSGFAIL` arrive as [`SessionEvent::Message`] instead.
    Subscription {
        /// This notification's 1-based position in the session's data-
        /// notification count — the number `LS_recovery_from` would carry if
        /// the connection broke right after it.
        progressive: u64,
        /// Which subscription it belongs to.
        key: SubscriptionKey,
        /// What it meant, or why it could not be reconciled with what the
        /// server already said. Boxed only to keep [`SessionEvent`] small.
        outcome: Box<SubscriptionOutcome>,
    },

    /// A control request was answered [`docs/spec/03-requests.md` §13].
    ///
    /// `target` is what makes this routable. A rejection carrying only a
    /// request id would name a number the caller has never seen, leaving it
    /// with a dead subscription and no way to know which one — the silent loss
    /// this crate treats as a defect.
    ControlResponse {
        /// The request being answered, or `None` for an `ERROR`, which carries
        /// no request id because the server could not parse one
        /// [`docs/spec/03-requests.md` §13.2].
        request_id: Option<RequestId>,
        /// What the request was acting on.
        target: ControlTarget,
        /// What the server said.
        outcome: ControlOutcome,
    },

    /// The server said something about itself that does not change the state
    /// machine: `CONS`, `SYNC`, `SERVNAME`, `CLIENTIP`
    /// [`docs/spec/02-session-lifecycle.md` §5.3].
    ServerInfo(ServerAnnouncement),

    /// A line arrived that this client could not parse.
    ///
    /// Never fatal: "a future server version must not crash an old client".
    /// The raw line is preserved so the caller can log or surface it.
    Unparsed {
        /// The line as it arrived, terminator stripped.
        line: String,
        /// Why it could not be parsed.
        error: ProtocolError,
    },

    /// Terminal. The driver has stopped and no further event will follow.
    Closed(SessionClosed),
}

// ---------------------------------------------------------------------------
// Commands
// ---------------------------------------------------------------------------

/// What the layer above asks the session to do.
#[derive(Debug)]
pub(crate) enum SessionCommand {
    /// Add a subscription, now or as soon as a session is bound
    /// [`docs/spec/03-requests.md` §6].
    Subscribe {
        /// The key the caller was handed synchronously.
        key: SubscriptionKey,
        /// What to subscribe to.
        spec: Box<SubscriptionSpec>,
    },

    /// Remove a subscription [`docs/spec/03-requests.md` §7].
    Unsubscribe {
        /// Which one.
        key: SubscriptionKey,
    },

    /// Change a subscription's maximum update frequency
    /// [`docs/spec/03-requests.md` §8].
    Reconfigure {
        /// Which one.
        key: SubscriptionKey,
        /// The new ceiling.
        max_frequency: MaxFrequencyLimit,
    },

    /// Send a message to the Metadata Adapter
    /// [`docs/spec/03-requests.md` §12].
    ///
    /// Unlike a subscription this is never buffered; see
    /// [`MessageResult::NotSent`].
    SendMessage {
        /// What to send.
        message: Box<OutgoingMessage>,
    },

    /// Ask the server to unbind the session so it can be rebound — T2
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T2].
    ForceRebind {
        /// `LS_close_socket`: whether the server should close the stream
        /// connection rather than leave it reusable
        /// [`docs/spec/02-session-lifecycle.md` §4.3].
        close_socket: Option<bool>,
    },

    /// Destroy the session — T7 while bound, T8 while unbound
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T7/T8].
    Destroy {
        /// The cause to report in the resulting `END`, or `None` for the
        /// server's default code `31`.
        cause: Option<DestroyCause>,
    },

    /// Stop the driver without destroying the session. The server keeps the
    /// session buffering until its own timeout
    /// [`docs/spec/02-session-lifecycle.md` §6.3].
    Shutdown,
}

/// The caller's end of the session. Cheap to clone; the driver stops when the
/// last clone is dropped.
#[derive(Debug, Clone)]
pub(crate) struct SessionHandle {
    commands: mpsc::Sender<SessionCommand>,
    next_key: Arc<AtomicU64>,
    /// Raised to order an immediate stop. See the module documentation's *stop
    /// lane*: it is the one signal that is never subject to backpressure.
    stop: Arc<watch::Sender<bool>>,
}

impl SessionHandle {
    /// Allocates a subscription key without asking for anything yet.
    ///
    /// Separate from [`SessionHandle::subscribe`] so that the layer above can
    /// register everything that key names — a stream, a decoder — *before* the
    /// request that could produce a notification for it is queued. Registering
    /// afterwards leaves a window in which an immediate `SUBOK` or `REQERR`
    /// has nowhere to go [`docs/spec/03-requests.md` §13].
    ///
    /// # Errors
    ///
    /// [`SessionError::Exhausted`] if the 64-bit key space ran out. Keys are
    /// never reused, so this is reported rather than wrapped.
    pub(crate) fn allocate_key(&self) -> Result<SubscriptionKey, SessionError> {
        self.next_key
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |key| {
                key.checked_add(1)
            })
            .map(SubscriptionKey)
            .map_err(|_| SessionError::Exhausted {
                what: "subscription key",
            })
    }

    /// Requests a subscription under a key already allocated by
    /// [`SessionHandle::allocate_key`].
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped.
    pub(crate) async fn subscribe_with_key(
        &self,
        key: SubscriptionKey,
        spec: SubscriptionSpec,
    ) -> Result<(), SessionError> {
        self.send(SessionCommand::Subscribe {
            key,
            spec: Box::new(spec),
        })
        .await
    }

    /// Requests a subscription and returns its key immediately.
    ///
    /// The key is allocated here rather than by the driver so that the caller
    /// can record it before the `add` request has even been encoded. The
    /// subscription is issued as soon as a session is bound, and re-issued on
    /// every session that replaces it.
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped, or
    /// [`SessionError::Exhausted`] if the key space ran out.
    pub(crate) async fn subscribe(
        &self,
        spec: SubscriptionSpec,
    ) -> Result<SubscriptionKey, SessionError> {
        let key = self.allocate_key()?;
        self.subscribe_with_key(key, spec).await?;
        Ok(key)
    }

    /// Orders the driver to stop at once, whatever it is waiting for.
    ///
    /// Unlike [`SessionHandle::shutdown`] this does not queue a command, so it
    /// cannot be held up by a full command channel or by a driver blocked
    /// emitting into an event stream nobody is reading. It is how "dropping the
    /// client shuts the session down" stays true under backpressure. The
    /// server is *not* told: use [`SessionHandle::destroy`] for that.
    pub(crate) fn stop(&self) {
        let _ = self.stop.send(true);
    }

    /// A receiver for the stop lane, for the other tasks a client owns.
    pub(crate) fn stop_signal(&self) -> watch::Receiver<bool> {
        self.stop.subscribe()
    }

    /// Removes a subscription.
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped.
    pub(crate) async fn unsubscribe(&self, key: SubscriptionKey) -> Result<(), SessionError> {
        self.send(SessionCommand::Unsubscribe { key }).await
    }

    /// Changes a subscription's maximum update frequency.
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped.
    pub(crate) async fn reconfigure(
        &self,
        key: SubscriptionKey,
        max_frequency: MaxFrequencyLimit,
    ) -> Result<(), SessionError> {
        self.send(SessionCommand::Reconfigure { key, max_frequency })
            .await
    }

    /// Sends a message to the Metadata Adapter
    /// [`docs/spec/03-requests.md` §12].
    ///
    /// Returning `Ok` means the message was **queued for this driver**, not
    /// that it reached the server. Its fate arrives as a
    /// [`SessionEvent::Message`] carrying the sequence and progressive the
    /// message identified itself with — including when it never left this
    /// client, which is what happens if nothing is bound at the time
    /// ([`MessageResult::NotSent`]; messages are deliberately not buffered).
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped.
    pub(crate) async fn send_message(&self, message: OutgoingMessage) -> Result<(), SessionError> {
        self.send(SessionCommand::SendMessage {
            message: Box::new(message),
        })
        .await
    }

    /// Asks the server to unbind the session so it is rebound at once.
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped.
    pub(crate) async fn force_rebind(
        &self,
        close_socket: Option<bool>,
    ) -> Result<(), SessionError> {
        self.send(SessionCommand::ForceRebind { close_socket })
            .await
    }

    /// Destroys the session and stops the driver.
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped.
    pub(crate) async fn destroy(&self, cause: Option<DestroyCause>) -> Result<(), SessionError> {
        self.send(SessionCommand::Destroy { cause }).await
    }

    /// Stops the driver without destroying the session.
    ///
    /// # Errors
    ///
    /// [`SessionError::Stopped`] if the driver has already stopped.
    pub(crate) async fn shutdown(&self) -> Result<(), SessionError> {
        self.send(SessionCommand::Shutdown).await
    }

    async fn send(&self, command: SessionCommand) -> Result<(), SessionError> {
        self.commands
            .send(command)
            .await
            .map_err(|_| SessionError::Stopped)
    }
}

// ---------------------------------------------------------------------------
// Error-code classification
// ---------------------------------------------------------------------------

/// What a session-level error code leaves the client able to do.
///
/// The specification "has **no formal recoverable/lost taxonomy**"
/// [`docs/spec/05-error-codes.md` §1] and states a client action for only a
/// handful of codes. This enum is therefore a client-side policy built on the
/// statements the spec does make; every arm of [`classify`] cites the sentence
/// it rests on, and the arms that rest on nothing say so.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Recovery {
    /// The old session is gone but a new one may be created at once, and all
    /// subscriptions re-executed
    /// [`docs/spec/02-session-lifecycle.md` §6.1].
    RecreateSession,
    /// The condition is stated to be temporary; retry after a backoff.
    RetryLater,
    /// Retrying cannot help. The session is definitively lost.
    Fatal,
}

/// Classifies an Appendix A session code
/// [`docs/spec/05-error-codes.md` §2, §5].
#[must_use]
pub(crate) fn classify(code: i64) -> Recovery {
    match code {
        // "the recovery attempt failed" — and the spec's general rule for that
        // is that "the Client can't but create a new stream connection as if it
        // was the first time it connects" [§5.1].
        4 => Recovery::RecreateSession,

        // "Specified session not found on a `bind_session` request" — the
        // "too much time passed" case, whose prescribed answer is to create a
        // new session and re-execute the subscriptions
        // [`docs/spec/02-session-lifecycle.md` §6.2, §6.1].
        20 => Recovery::RecreateSession,

        // "the client should recover by opening a new session immediately" —
        // the one code for which the spec prescribes the action outright
        // [`docs/spec/05-error-codes.md` §5.4].
        48 => Recovery::RecreateSession,

        // "retry later", stated verbatim for 5 and 6; 10 is "New sessions
        // temporarily blocked" [`docs/spec/05-error-codes.md` §5.5].
        5 | 6 | 10 => Recovery::RetryLater,

        // SPEC-AMBIGUITY: the spec records no recoverability for 7, 8 and 9
        // (licensed / configured session limits, server load on
        // `create_session`) [`docs/spec/05-error-codes.md` §2]. They describe
        // capacity, which is transient by nature, so they are retried under
        // the same backoff as 5/6/10. This is a client-side reading, not a
        // spec statement, and is reversible.
        7..=9 => Recovery::RetryLater,

        // SPEC-AMBIGUITY: 33 and 34 share one description, "An unexpected
        // error occurred on the Server while the session was in activity", and
        // the spec neither distinguishes them nor states recoverability
        // [`docs/spec/05-error-codes.md` §2]. An unexpected server-side error
        // is treated as transient and retried; a fresh session is the most a
        // client can do about it either way.
        33 | 34 => Recovery::RetryLater,

        // SPEC-AMBIGUITY: code 21 is the session id "not compatible with this
        // Server instance… it might pertain to a different instance", which
        // matches the description of the permanent cluster-affinity failure
        // that "will not be overcome by a reconnection but only by fixing the
        // configuration" — but `docs/spec/05-error-codes.md` §5.3 warns that
        // "the document never states the linkage. Do not assume it." Treating
        // 21 as fatal is the defensive reading: retrying a permanent
        // misconfiguration is a reconnect storm against a server that can
        // never answer, and the caller is told the code so it can decide
        // otherwise.
        21 => Recovery::Fatal,

        // Everything else, including authentication and adapter-set failures
        // (1, 2), protocol incompatibility (3), licence restrictions (11),
        // administrative closure (31, 32), the same-user eviction (35), the
        // foreign manual rebind (40), and the malformed-request family
        // (60, 64–71).
        //
        // SPEC-AMBIGUITY: the spec "neither reserves [the gaps in the
        // numbering] nor says what a client should do with a positive code it
        // does not recognise" [`docs/spec/05-error-codes.md` §2]. An unknown
        // code is therefore fatal rather than retried: a client that retries
        // codes it does not understand turns one server-side refusal into a
        // reconnect storm, whereas a client that stops hands the caller a code
        // and a message to act on.
        //
        // Codes `<= 0` are Metadata-Adapter-defined and "application-specific"
        // [`docs/spec/05-error-codes.md` §2]; this client cannot know whether
        // retrying one is safe, so it does not.
        _ => Recovery::Fatal,
    }
}

// ---------------------------------------------------------------------------
// Internal bookkeeping
// ---------------------------------------------------------------------------

/// Where a subscription stands with the server.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SubscriptionState {
    /// Not yet issued on the current session, or issued and not yet confirmed.
    Pending,
    /// Confirmed by `SUBOK` or `SUBCMD` [`docs/spec/04-notifications.md` §3.1].
    Active,
    /// A `delete` has been sent; awaiting `UNSUB`
    /// [`docs/spec/04-notifications.md` §3.4].
    Removing,
}

/// One desired subscription and its binding, if any, in the current session.
#[derive(Debug, Clone)]
struct RegistryEntry {
    spec: SubscriptionSpec,
    /// The `LS_subId` this subscription holds in the current session, or `None`
    /// when it has not been issued on it. Cleared whenever the session is
    /// replaced, because `LS_subId` is unique *within the session*
    /// [`docs/spec/02-session-lifecycle.md` §4.4].
    wire: Option<NonZeroU32>,
    state: SubscriptionState,
    /// Whether the server ever confirmed it, in any session. Reported on
    /// re-subscription so the caller knows what it is getting back.
    was_active: bool,
    /// The item and field state of this subscription: schema, baselines, and
    /// the §2.4 snapshot classification.
    ///
    /// It lives here because the notifications that drive it are routed by this
    /// layer and by nothing else. The façade above never sees a
    /// [`Notification`]; it receives the [`SubscriptionEvent`] this produced.
    manager: SubscriptionManager,
}

/// The desired subscription set, and its mapping onto the current session's
/// `LS_subId` space.
#[derive(Debug, Default)]
struct Registry {
    entries: BTreeMap<SubscriptionKey, RegistryEntry>,
    by_sub_id: BTreeMap<u32, SubscriptionKey>,
    /// The next `LS_subId` to allocate. "A progressive integer number starting
    /// with 1… unique within the session", and, because IDs "survive rebinds
    /// and must not be reused", never decremented within a session
    /// [`docs/spec/02-session-lifecycle.md` §4.4].
    next_sub_id: u32,
}

impl Registry {
    fn new() -> Self {
        Self {
            entries: BTreeMap::new(),
            by_sub_id: BTreeMap::new(),
            next_sub_id: 1,
        }
    }

    /// Drops every wire binding, for use when a **new** session replaces the
    /// old one. Subscription ids restart at 1 in the new session
    /// [`docs/spec/02-session-lifecycle.md` §9.5].
    ///
    /// Entries that were being removed are dropped outright: the new session
    /// never had them, so there is nothing left to delete.
    fn reset_for_new_session(&mut self) {
        self.entries
            .retain(|_, entry| entry.state != SubscriptionState::Removing);
        for entry in self.entries.values_mut() {
            entry.wire = None;
            entry.state = SubscriptionState::Pending;
            // A new session is a new flow: the server restarts the subscription
            // from scratch, so every field baseline, every schema and every
            // snapshot classification held for it is stale
            // [`docs/spec/02-session-lifecycle.md` §9.5]. Keeping one would let
            // an unchanged token or a diff from the new session be applied to a
            // value from the old.
            entry.manager = entry.spec.new_manager();
        }
        self.by_sub_id.clear();
        self.next_sub_id = 1;
    }

    /// Allocates the next `LS_subId`, or `None` if the session has somehow
    /// exhausted the 32-bit space.
    fn allocate(&mut self) -> Option<NonZeroU32> {
        let id = NonZeroU32::new(self.next_sub_id)?;
        self.next_sub_id = self.next_sub_id.checked_add(1)?;
        Some(id)
    }
}

/// What a control request was for, so its response can be acted on.
#[derive(Debug, Clone, PartialEq, Eq)]
enum PendingKind {
    Subscribe(SubscriptionKey),
    Unsubscribe(SubscriptionKey),
    /// A `reconf`, carrying the value it asked for so that an accepted change
    /// can be committed to the desired state and survive a session that has to
    /// be recreated [`docs/spec/03-requests.md` §8].
    Reconfigure {
        key: SubscriptionKey,
        max_frequency: MaxFrequencyLimit,
    },
    /// A `msg`. It carries the message's own identity because a `REQERR`
    /// correlates by `LS_reqId` while the caller thinks in terms of the
    /// sequence and progressive that `MSGDONE`/`MSGFAIL` would have used
    /// [`docs/spec/04-notifications.md` §4.1] — keeping both here is what lets
    /// a refusal be reported on the same surface as a processing failure.
    SendMessage {
        sequence: MessageSequence,
        prog: Option<u64>,
    },
    ForceRebind,
    Destroy,
}

impl PendingKind {
    /// What this request acts on, for the response event that will report it.
    #[must_use]
    fn target(&self) -> ControlTarget {
        match self {
            Self::Subscribe(key) => ControlTarget::Subscription {
                key: *key,
                operation: SubscriptionOperation::Subscribe,
            },
            Self::Unsubscribe(key) => ControlTarget::Subscription {
                key: *key,
                operation: SubscriptionOperation::Unsubscribe,
            },
            Self::Reconfigure { key, .. } => ControlTarget::Subscription {
                key: *key,
                operation: SubscriptionOperation::Reconfigure,
            },
            Self::SendMessage { .. } | Self::ForceRebind | Self::Destroy => ControlTarget::Session,
        }
    }
}

/// One control request awaiting its `REQOK` or `REQERR`, tagged with the
/// session it belongs to.
///
/// The tag exists because §4.5 promises the awkward case outright: "after
/// binding the second session, it is possible that late responses to control
/// requests related with the first session arrive interspersed with
/// notifications for the second session"
/// [`docs/spec/02-session-lifecycle.md` §4.5]. Without a generation, a late
/// `REQERR` naming a subscription that has since been re-established on a new
/// session would drop it from the desired set — a subscription lost to a
/// message about a session that no longer exists.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Pending {
    kind: PendingKind,
    /// Which session generation issued it. See
    /// [`SessionDriver::generation`].
    generation: u64,
}

/// The desired-state form of a value accepted by a `reconf`.
///
/// `reconf` cannot ask for `unfiltered`, so every value it can carry has a
/// counterpart among the subscription request's own
/// [`docs/spec/03-requests.md` §6.1, §8.1].
#[must_use]
fn as_requested_frequency(limit: MaxFrequencyLimit) -> RequestedMaxFrequency {
    match limit {
        MaxFrequencyLimit::Unlimited => RequestedMaxFrequency::Unlimited,
        MaxFrequencyLimit::Limited(number) => RequestedMaxFrequency::Limited(number),
    }
}

/// Which kind of stream-opening request is in flight, and what it means for
/// the `CONOK` that answers it.
#[derive(Debug, Clone, PartialEq, Eq)]
enum OpenIntent {
    /// `create_session` [`docs/spec/02-session-lifecycle.md` §3.1].
    Create,
    /// `bind_session` with no `LS_recovery_from`
    /// [`docs/spec/02-session-lifecycle.md` §4.4].
    Rebind,
    /// `bind_session` with `LS_recovery_from`
    /// [`docs/spec/02-session-lifecycle.md` §5.4].
    Recover {
        /// The progressive the request carried.
        requested: u64,
    },
}

/// The two moments of the specification's *Bound* state that a client must
/// tell apart: before `CONOK`, when an unanswered handshake must time out, and
/// after it, when the negotiated keep-alive governs
/// [`docs/spec/02-session-lifecycle.md` §2.1, §8.1].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StreamState {
    /// A stream-opening request is in flight; no `CONOK` yet.
    Establishing,
    /// `CONOK` arrived: the session is bound and streaming.
    Bound,
}

/// What to open next, and after how long.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Reopen {
    intent: OpenIntent,
    delay: Duration,
}

/// What the line pump decided.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Flow {
    /// Keep pumping.
    Continue,
    /// Leave the bound state and reopen.
    Unbind(UnbindReason),
    /// Stop for good.
    Closed(SessionClosed),
}

/// Which future woke the pump.
enum Woke {
    Line(Option<Result<String, TransportError>>),
    Command(Option<SessionCommand>),
    Timer,
    /// The stop lane fired. See the module documentation.
    Stop,
}

/// How an attempt to establish a stream connection ended.
enum Opened {
    /// The transport accepted the request; `CONOK` or `CONERR` will follow on
    /// the line stream.
    Established,
    /// It could not be established, or not within the configured budget.
    Failed {
        /// What went wrong. Never contains a credential.
        detail: String,
    },
    /// A stop was ordered while the connection was being established.
    Stopped,
}

// ---------------------------------------------------------------------------
// The driver
// ---------------------------------------------------------------------------

/// Creates a session from a caller's configuration, choosing and building the
/// transport it asks for.
///
/// This is the **composition root**: the one place in the crate that turns a
/// public [`ClientConfig`] into a concrete transport adapter and the options
/// that drive it. Keeping it here rather than in the façade is what lets a
/// second transport be added without touching a semver-governed module
/// (`docs/adr/0007-transport-port-shape.md`, §3); the façade above never names
/// an adapter, and the adapters below never see a `ClientConfig`.
///
/// # Errors
///
/// - [`Error::Transport`] if the configured address cannot be turned into an
///   endpoint for the chosen transport.
/// - [`Error::Internal`] if the options and the transport contradict each
///   other, which only a bug in this translation can produce.
pub(crate) fn connect_configured(
    config: ClientConfig,
) -> Result<
    (
        SessionDriver<AnyTransport>,
        SessionHandle,
        mpsc::Receiver<SessionEvent>,
    ),
    crate::error::Error,
> {
    let choice = config.transport();
    let address = config.address().as_str().to_owned();
    let transport = match choice {
        crate::config::Transport::WebSocket => {
            AnyTransport::WebSocket(WsTransport::try_new(address)?)
        }
        crate::config::Transport::HttpStreaming => {
            AnyTransport::Http(HttpTransport::try_new(address, HttpMode::Streaming)?)
        }
        crate::config::Transport::HttpPolling => {
            AnyTransport::Http(HttpTransport::try_new(address, HttpMode::Polling)?)
        }
    };

    let mut options = SessionOptions::from_client_config(config);
    if matches!(choice, crate::config::Transport::HttpPolling) {
        // The session's connection mode must agree with the polling transport's
        // declared `is_polling`, and `LS_polling` changes the meaning of the
        // `CONOK` keep-alive while forbidding the streaming liveness parameters
        // [`docs/spec/02-session-lifecycle.md` §7.2, §8.4]. The public
        // `ConnectionOptions` exposes no polling knobs yet, so the cycle timings
        // are this crate's defaults; the keep-alive *hint*, if the caller set
        // one, becomes the poll's idle time, which is what the `CONOK`
        // keep-alive reports back for a polling connection
        // [`docs/spec/02-session-lifecycle.md` §7.2].
        let idle_millis = match options.connection {
            ConnectionMode::Streaming {
                keepalive_millis, ..
            } => keepalive_millis,
            ConnectionMode::Polling { idle_millis, .. } => idle_millis,
        }
        .unwrap_or(DEFAULT_IDLE_MILLIS);
        options = options.with_connection(ConnectionMode::Polling {
            polling_millis: DEFAULT_POLLING_MILLIS,
            idle_millis: Some(idle_millis),
        });
    }

    connect(transport, options).map_err(|error| crate::error::Error::Internal {
        reason: error.to_string(),
    })
}

/// Default `LS_polling_millis` for the HTTP long-polling transport: the
/// session-keep-alive budget the server allows between poll cycles
/// [`docs/spec/02-session-lifecycle.md` §7.2].
///
/// A choice of this crate — the public [`crate::ConnectionOptions`] exposes no
/// polling knob yet, so a single sensible default stands in until an ergonomic
/// surface for it is designed. The server clamps a value it considers too high
/// and reports the one it used.
const DEFAULT_POLLING_MILLIS: u64 = 5_000;

/// Default `LS_idle_millis` for the HTTP long-polling transport: how long the
/// server holds a poll open waiting for a notification when none is ready
/// [`docs/spec/02-session-lifecycle.md` §7.2].
///
/// A choice of this crate, and deliberately **positive**: a synchronous poll
/// (`idle_millis` of zero) returns at once even when idle, which would spin the
/// bind/rebind loop against a quiet session. Fifteen seconds holds the
/// connection open for real-time delivery while a cache-flushing close still
/// happens promptly when data arrives.
const DEFAULT_IDLE_MILLIS: u64 = 15_000;

/// Creates a session over `transport`.
///
/// Returns the driver — a single future that must be polled to completion,
/// typically by `tokio::spawn` — the caller's handle, and the event stream.
///
/// # Errors
///
/// [`SessionError::Configuration`] if the options ask for a polling connection
/// on a transport that does not poll, or the reverse. The two must agree
/// because `LS_polling` changes the meaning of the `CONOK` keep-alive argument
/// and forbids `LS_keepalive_millis` and `LS_inactivity_millis` entirely
/// [`docs/spec/02-session-lifecycle.md` §7.2, §8.4].
pub(crate) fn connect<T: Transport>(
    transport: T,
    options: SessionOptions,
) -> Result<
    (
        SessionDriver<T>,
        SessionHandle,
        mpsc::Receiver<SessionEvent>,
    ),
    SessionError,
> {
    let properties = transport.properties();
    if properties.is_polling != options.is_polling() {
        return Err(SessionError::Configuration {
            reason: format!(
                "transport declares is_polling={} but the connection mode asks for polling={}",
                properties.is_polling,
                options.is_polling()
            ),
        });
    }

    let (command_tx, command_rx) = mpsc::channel(options.command_capacity.get());
    let (event_tx, event_rx) = mpsc::channel(options.event_capacity.get());
    let (stop_tx, stop_rx) = watch::channel(false);

    let handle = SessionHandle {
        commands: command_tx,
        next_key: Arc::new(AtomicU64::new(1)),
        stop: Arc::new(stop_tx),
    };
    let driver = SessionDriver::new(
        transport, properties, options, command_rx, event_tx, stop_rx,
    );
    Ok((driver, handle, event_rx))
}

/// Resolves as soon as an immediate stop has been ordered, or every handle
/// that could order one has been dropped.
///
/// Cancel-safe: [`watch::Receiver::changed`] is, and the flag is re-read on
/// every turn, so dropping this future in a `select!` loses nothing.
async fn stopped(stop: &mut watch::Receiver<bool>) {
    loop {
        if *stop.borrow_and_update() {
            return;
        }
        if stop.changed().await.is_err() {
            // Every `SessionHandle` is gone, which is the other way the layer
            // above says it is finished.
            return;
        }
    }
}

/// How many consecutive input lines the pump processes before it must poll the
/// command channel and the liveness clocks, whatever the socket still holds.
///
/// The pump is biased towards the server so that the state machine cannot act
/// on a command against a session a line already ended. Bias without a bound is
/// starvation: a connection that always has a line ready would postpone a
/// heartbeat, a `destroy` and a shutdown indefinitely. This is the ceiling on
/// that latency — a control action waits at most this many notifications.
const MAX_LINES_PER_TURN: u32 = 32;

/// How many consecutive failures to send the outbound heartbeat are tolerated
/// before the stream connection is treated as lost.
///
/// A failed heartbeat leaves the outbound commitment unmet
/// [`docs/spec/02-session-lifecycle.md` §8.4], and a client that cannot write
/// to a socket has nothing to gain by holding it: after this many attempts the
/// connection is reopened, which is what the retry budget is there to bound.
const MAX_HEARTBEAT_FAILURES: u32 = 3;

/// The session state machine. One future, no spawned tasks.
#[derive(Debug)]
pub(crate) struct SessionDriver<T: Transport> {
    transport: T,
    properties: TransportProperties,
    options: SessionOptions,
    commands: mpsc::Receiver<SessionCommand>,
    events: mpsc::Sender<SessionEvent>,
    /// The stop lane. See the module documentation.
    stop: watch::Receiver<bool>,
    /// Set when a stop was ordered while the driver was blocked, so that the
    /// current turn unwinds instead of carrying on.
    stopping: bool,

    /// The current stream state, meaningful only while a stream connection is
    /// open.
    stream_state: StreamState,
    /// What the in-flight stream-opening request was.
    intent: OpenIntent,
    /// The session the server gave us, while it is still usable.
    session: Option<SessionId>,
    /// The last session that was abandoned — rejected on a bind, or ended by
    /// the server with a code that asks for a fresh one. Kept only so the
    /// replacement can be reported as [`BindKind::Recreated`] naming what it
    /// replaced (`docs/adr/0005-recovery-is-visible-in-the-event-stream.md`);
    /// it is never sent on the wire again.
    previous_session: Option<SessionId>,

    /// "The count of all the notifications received since the start of the
    /// session", which `LS_recovery_from` carries
    /// [`docs/spec/02-session-lifecycle.md` §5.2].
    progressive: u64,
    /// How many duplicated data notifications are still to be discarded after
    /// a `PROG` that resumed earlier than requested
    /// [`docs/spec/02-session-lifecycle.md` §5.2, precondition 4].
    skip_remaining: u64,

    /// Whether a `PROG` has already been honoured on the current stream
    /// connection. `PROG` is one of the notifications sent immediately after
    /// `CONOK` and only in answer to `LS_recovery_from`
    /// [`docs/spec/02-session-lifecycle.md` §5.4], so a second one — or one on
    /// a connection that asked for no recovery — correlates with nothing this
    /// client requested and must not move the recovery baseline.
    prog_honoured: bool,

    registry: Registry,
    pending: HashMap<String, Pending>,
    /// Which session this driver is on: incremented every time a session is
    /// created or recreated, never reset. Pending control requests carry it so
    /// that a late response from a replaced session cannot mutate the
    /// replacement's state [`docs/spec/02-session-lifecycle.md` §4.5].
    generation: u64,
    /// `LS_reqId` is "unique within the connection", where "connection" is
    /// never defined [`docs/spec/02-session-lifecycle.md` §4.4, ambiguity A5].
    ///
    /// SPEC-AMBIGUITY (A5): this counter is monotonic for the **whole life of
    /// the driver** and is never reset — not per socket, not per stream
    /// connection, not per session. That is at least as strict as every reading
    /// of the ambiguity, so it cannot collide under any of them, and it makes
    /// the late responses of §4.5 — "after binding the second session, it is
    /// possible that late responses to control requests related with the first
    /// session arrive interspersed with notifications for the second session" —
    /// harmless: a stale id can never be confused with a live one.
    next_request_id: u64,

    /// Set when `force_rebind` is sent, so the `LOOP` it provokes is attributed
    /// to T2 rather than to the transport's own reason
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T2].
    force_rebind_outstanding: bool,
    /// Set when `destroy` is sent, so the `END` it provokes is attributed to
    /// the client [`docs/spec/02-session-lifecycle.md` §2.2, T7].
    destroy_requested: bool,

    /// Consecutive session refreshes (server code 48) with no productive
    /// session in between. See [`SessionDriver::plan_after`].
    refresh_streak: u32,
    /// Consecutive failures to send the outbound heartbeat.
    heartbeat_failures: u32,

    /// The keep-alive the server last reported in `CONOK`, `None` until the
    /// first one arrives.
    ///
    /// On a **polling** connection this argument is `LS_idle_millis` — how long
    /// the server may hold the poll open waiting for a notification
    /// [`docs/spec/02-session-lifecycle.md` §7.2] — so the whole response,
    /// `CONOK` included, may be withheld for that long. It is kept here so the
    /// establishment budget of the *next* poll cycle can exceed it; see
    /// [`SessionDriver::establishment_budget`].
    negotiated_keep_alive: Option<Duration>,

    liveness: Liveness,
    backoff: Backoff,
}

impl<T: Transport> SessionDriver<T> {
    fn new(
        transport: T,
        properties: TransportProperties,
        options: SessionOptions,
        commands: mpsc::Receiver<SessionCommand>,
        events: mpsc::Sender<SessionEvent>,
        stop: watch::Receiver<bool>,
    ) -> Self {
        let liveness = Liveness::new(
            options.keepalive_slack,
            options.heartbeat_interval(),
            Instant::now(),
        );
        let backoff = Backoff::new(options.backoff);
        Self {
            transport,
            properties,
            options,
            commands,
            events,
            stop,
            stopping: false,
            stream_state: StreamState::Establishing,
            intent: OpenIntent::Create,
            session: None,
            previous_session: None,
            progressive: 0,
            skip_remaining: 0,
            prog_honoured: false,
            registry: Registry::new(),
            pending: HashMap::new(),
            generation: 1,
            next_request_id: 1,
            force_rebind_outstanding: false,
            destroy_requested: false,
            refresh_streak: 0,
            heartbeat_failures: 0,
            negotiated_keep_alive: None,
            liveness,
            backoff,
        }
    }

    /// How long to allow for a stream connection to be established and to
    /// deliver its first line.
    ///
    /// For a **streaming** connection this is the caller's
    /// [`SessionOptions::open_timeout`]: `CONOK` is expected within one round
    /// trip. For a **polling** connection it is that round-trip budget **plus
    /// the idle time**, because the server may legitimately withhold the whole
    /// response — the HTTP head and `CONOK` included — for up to
    /// `LS_idle_millis` while it waits for a notification
    /// [`docs/spec/02-session-lifecycle.md` §7.2]. Killing the connection at the
    /// bare round-trip budget would abort every quiet poll cycle before its
    /// `CONOK` could arrive.
    ///
    /// The idle time used is the one the server **negotiated** in the last
    /// `CONOK` (which it may have clamped above what was requested), falling back
    /// to the requested `LS_idle_millis` before the first `CONOK`. Adding it to
    /// the round-trip budget encodes `budget > idle` structurally, so the two
    /// cannot silently invert the way a fixed budget against a larger negotiated
    /// idle did.
    #[must_use]
    fn establishment_budget(&self) -> Duration {
        let ConnectionMode::Polling { idle_millis, .. } = self.options.connection else {
            return self.options.open_timeout;
        };
        // `LS_idle_millis` absent or zero is a synchronous poll that returns at
        // once, so there is no idle hold to budget for
        // [`docs/spec/02-session-lifecycle.md` §7.2].
        let requested = Duration::from_millis(idle_millis.unwrap_or(0));
        let idle = self
            .negotiated_keep_alive
            .unwrap_or(requested)
            .max(requested);
        self.options
            .open_timeout
            .checked_add(idle)
            .unwrap_or(Duration::MAX)
    }

    /// The session's data-notification count so far — what a recovery bind
    /// would ask to resume from [`docs/spec/02-session-lifecycle.md` §5.2].
    #[must_use]
    #[inline]
    pub(crate) const fn progressive(&self) -> u64 {
        self.progressive
    }

    /// The session identifier, once the server has supplied one.
    #[must_use]
    #[inline]
    pub(crate) const fn session_id(&self) -> Option<&SessionId> {
        self.session.as_ref()
    }

    /// Runs the session to its terminal state.
    ///
    /// Returns why it ended; the same value is also the last event on the event
    /// channel. The transport is closed before this returns on **every** path.
    pub(crate) async fn run(mut self) -> SessionClosed {
        let closed = self.drive().await;
        if let Err(error) = self.transport.close().await {
            tracing::warn!(%error, "transport did not close cleanly");
        }
        // Best effort: a caller that dropped the receiver is not an error, and
        // a caller that stopped reading must not be able to keep this task
        // alive — `emit` gives the terminal event up when a stop is ordered.
        self.emit(SessionEvent::Closed(closed.clone())).await;
        tracing::info!(?closed, "session ended");
        closed
    }

    /// The outer loop: open a stream connection, pump it, decide what to open
    /// next. Each turn is one edge of the specification's state diagram
    /// [`docs/spec/02-session-lifecycle.md` §2.3].
    async fn drive(&mut self) -> SessionClosed {
        let mut next = Reopen {
            intent: OpenIntent::Create,
            delay: Duration::ZERO,
        };
        loop {
            if let Some(closed) = self.wait_before_reopen(next.delay).await {
                return closed;
            }

            let request = match self.build_open(&next.intent) {
                Ok(request) => request,
                Err(error) => {
                    return SessionClosed::Internal {
                        reason: error.to_string(),
                    };
                }
            };
            self.intent = next.intent.clone();
            self.stream_state = StreamState::Establishing;
            self.skip_remaining = 0;
            self.prog_honoured = false;

            let flow = match self.open_stream(request).await {
                Opened::Established => {
                    // The wait for `CONOK` gets the same budget the open did: on
                    // a polling connection the server may hold `CONOK` back for
                    // the idle time even when the head arrived at once
                    // [`SessionDriver::establishment_budget`].
                    self.liveness
                        .on_stream_opened(self.establishment_budget(), Instant::now());
                    self.pump().await
                }
                Opened::Failed { detail } => {
                    Flow::Unbind(UnbindReason::ConnectionFailed { detail })
                }
                Opened::Stopped => {
                    return SessionClosed::ByClient {
                        destroy_confirmed: false,
                        cause: None,
                    };
                }
            };

            match flow {
                Flow::Closed(closed) => return closed,
                Flow::Continue | Flow::Unbind(_) => {}
            }
            let reason = match flow {
                Flow::Unbind(reason) => reason,
                // The pump only ever returns `Unbind` or `Closed`; treating a
                // `Continue` as a connection failure keeps this total without
                // an `unreachable!`.
                _ => UnbindReason::ConnectionFailed {
                    detail: "stream ended without a reason".to_owned(),
                },
            };

            self.liveness.on_stream_closed();
            self.stream_state = StreamState::Establishing;

            // After a clean `LOOP` the connection may legitimately be reused —
            // with WS "the server sends the *Loop* command exactly as in the
            // HTTP case, but it does not close the connection", and the client
            // "is free to choose whether to rebind the session to the same
            // stream connection or open a new one"
            // [`docs/spec/02-session-lifecycle.md` §4.3], a choice the
            // transport is the only layer able to make.
            //
            // After anything else it may not. A stalled connection must be
            // "closed and reopened" [ibid. §8.1], and a recovery request
            // "invalidates any further notifications (of any type) that may be
            // later received on the original connection" [ibid. §5.1] — so the
            // wedged socket is torn down here rather than left to be collected
            // whenever the transport happens to replace it.
            if !reason.is_clean_loop()
                && let Err(error) = self.transport.close().await
            {
                tracing::debug!(%error, "closing the failed stream connection");
            }

            match self.plan_after(&reason) {
                Some(reopen) => {
                    self.emit(SessionEvent::Unbound {
                        reason: reason.clone(),
                        retry_in: Some(reopen.delay),
                    })
                    .await;
                    if self.stopping {
                        return SessionClosed::ByClient {
                            destroy_confirmed: false,
                            cause: None,
                        };
                    }
                    tracing::debug!(?reason, delay = ?reopen.delay, intent = ?reopen.intent, "reopening");
                    next = reopen;
                }
                None => {
                    self.emit(SessionEvent::Unbound {
                        reason: reason.clone(),
                        retry_in: None,
                    })
                    .await;
                    return SessionClosed::RetriesExhausted {
                        attempts: self.backoff.attempts(),
                        last: Some(reason),
                    };
                }
            }
        }
    }

    /// Opens the next stream connection under the establishment budget.
    ///
    /// The budget covers the **whole** establishment — name resolution, the
    /// TCP and TLS handshakes, the protocol handshake, and the first write —
    /// not merely the wait for `CONOK` that follows it. It is
    /// [`SessionDriver::establishment_budget`]: the caller's
    /// [`SessionOptions::open_timeout`] for a streaming connection, and that
    /// plus the idle time for a polling one, whose response the server may
    /// legitimately withhold until a notification is ready. The specification
    /// defines no such limit; without it a connect that hangs below the port
    /// hangs the session for as long as the operating system allows. The stop
    /// lane is honoured throughout, so a client being torn down does not wait
    /// out a dial to an unreachable host.
    async fn open_stream(&mut self, request: StreamOpen) -> Opened {
        let budget = self.establishment_budget();
        let transport = &mut self.transport;
        let stop = &mut self.stop;
        tokio::select! {
            biased;
            result = tokio::time::timeout(budget, transport.open_stream(request)) => match result {
                Ok(Ok(())) => Opened::Established,
                Ok(Err(error)) => Opened::Failed { detail: error.to_string() },
                Err(_elapsed) => {
                    tracing::warn!(?budget, "the stream connection could not be established in time");
                    Opened::Failed {
                        detail: format!("the stream connection was not established within {budget:?}"),
                    }
                }
            },
            () = stopped(stop) => Opened::Stopped,
        }
    }

    /// Waits out a reconnection delay while still serving commands, so a
    /// shutdown or a destroy is not stuck behind a backoff.
    async fn wait_before_reopen(&mut self, delay: Duration) -> Option<SessionClosed> {
        if delay.is_zero() {
            return None;
        }
        let deadline = Instant::now()
            .checked_add(delay)
            .unwrap_or_else(Instant::now);
        loop {
            let woke = tokio::select! {
                biased;
                command = self.commands.recv() => Woke::Command(command),
                () = stopped(&mut self.stop) => Woke::Stop,
                () = tokio::time::sleep_until(deadline) => Woke::Timer,
            };
            match woke {
                Woke::Timer => return None,
                Woke::Stop => {
                    return Some(SessionClosed::ByClient {
                        destroy_confirmed: false,
                        cause: None,
                    });
                }
                Woke::Command(None) => {
                    return Some(SessionClosed::ByClient {
                        destroy_confirmed: false,
                        cause: None,
                    });
                }
                Woke::Command(Some(command)) => match self.on_command(command).await {
                    Flow::Closed(closed) => return Some(closed),
                    Flow::Continue | Flow::Unbind(_) => {
                        if self.stopping {
                            return Some(SessionClosed::ByClient {
                                destroy_confirmed: false,
                                cause: None,
                            });
                        }
                    }
                },
                Woke::Line(_) => {}
            }
        }
    }

    /// Builds the request that opens the next stream connection.
    ///
    /// `LS_reduce_head` is never set. On a `bind_session` it would suppress
    /// `CONOK` itself, and the spec then defines no substitute signal for a
    /// successful bind, nor any way to tell success from failure
    /// [`docs/spec/02-session-lifecycle.md` §3.2, ambiguity A4]. `CONOK` is
    /// this state machine's only evidence of the *Unbound → Bound* transition,
    /// so it is never asked to be withheld.
    fn build_open(&mut self, intent: &OpenIntent) -> Result<StreamOpen, ProtocolError> {
        let session = match (&self.session, intent) {
            (Some(session), OpenIntent::Rebind | OpenIntent::Recover { .. }) => Some(session),
            _ => None,
        };
        match (session, intent) {
            (Some(session), OpenIntent::Recover { requested }) => {
                Ok(StreamOpen::Bind(Box::new(BindSession {
                    // Always explicit, even on WS where it may be defaulted to
                    // "the last session that was bound to the WS connection"
                    // [`docs/spec/02-session-lifecycle.md` §4.4]. Naming it
                    // removes any question of which session a rebind applies to
                    // when late responses from a previous one are still in
                    // flight [ibid. §4.5].
                    session: Some(session.as_str().to_owned()),
                    recovery_from: Some(*requested),
                    content_length: self.options.content_length,
                    connection: self.options.connection,
                    reduce_head: None,
                })))
            }
            (Some(session), OpenIntent::Rebind) => Ok(StreamOpen::Bind(Box::new(BindSession {
                session: Some(session.as_str().to_owned()),
                recovery_from: None,
                content_length: self.options.content_length,
                connection: self.options.connection,
                reduce_head: None,
            }))),
            _ => {
                let credentials = &self.options.credentials;
                Ok(StreamOpen::Create(Box::new(CreateSession {
                    user: credentials.user.clone(),
                    password: credentials.password.clone(),
                    adapter_set: credentials.adapter_set.clone(),
                    requested_max_bandwidth: None,
                    content_length: self.options.content_length,
                    connection: self.options.connection,
                    reduce_head: None,
                    ttl: None,
                })))
            }
        }
    }

    /// Chooses what to open after an unbind, and how long to wait first.
    ///
    /// `None` means the retry budget is spent and the session is definitively
    /// lost.
    fn plan_after(&mut self, reason: &UnbindReason) -> Option<Reopen> {
        // A clean `LOOP` is not a failure: the server told us to rebind and
        // guaranteed continuity, so the backoff schedule is irrelevant and the
        // only delay is the one `LOOP` asked for
        // [`docs/spec/02-session-lifecycle.md` §4.2, §4.4].
        if reason.is_clean_loop() {
            self.backoff.reset();
            self.refresh_streak = 0;
            return Some(Reopen {
                intent: OpenIntent::Rebind,
                delay: reason.expected_delay().unwrap_or(Duration::ZERO),
            });
        }

        if !matches!(reason, UnbindReason::ServerRefresh { .. }) {
            self.refresh_streak = 0;
        }

        match reason {
            // T5, and the mute-connection case of §5.1. The session is not
            // closed, so recovery — not a plain rebind — is the right request:
            // a plain rebind after a drop "silently loses data"
            // [`docs/spec/02-session-lifecycle.md` §4.4].
            UnbindReason::ConnectionFailed { .. } | UnbindReason::KeepaliveExpired { .. } => {
                let delay = self.backoff.next_delay()?;
                let intent = if self.session.is_some() {
                    OpenIntent::Recover {
                        requested: self.progressive,
                    }
                } else {
                    OpenIntent::Create
                };
                Some(Reopen { intent, delay })
            }

            // "the client should create a new stream connection, as if it was
            // the first time it connects to the Server, and re-execute all the
            // subscriptions" [`docs/spec/02-session-lifecycle.md` §6.1].
            UnbindReason::Rejected { .. } => {
                let delay = self.backoff.next_delay()?;
                Some(Reopen {
                    intent: OpenIntent::Create,
                    delay,
                })
            }

            // Code 48: "the client should recover by opening a new session
            // immediately" [`docs/spec/05-error-codes.md` §5.4]. Immediately
            // means no backoff, and the schedule is reset because nothing
            // failed.
            //
            // Only the first one, though. A server that answers every fresh
            // session with another refresh would otherwise get an unthrottled
            // reconnect loop that never exhausts, because the `CONOK` it sends
            // first resets the retry budget every time. Consecutive refreshes
            // with no session productive enough to deliver a single data
            // notification in between are therefore delayed like any other
            // failed attempt, and counted towards a definitive loss. The
            // streak is cleared by the first data notification
            // ([`SessionDriver::on_data`]) and by any other unbind reason, so
            // a deployment that legitimately refreshes sessions periodically
            // never accumulates one.
            UnbindReason::ServerRefresh { .. } => {
                self.refresh_streak = self.refresh_streak.saturating_add(1);
                if self.refresh_streak <= 1 {
                    self.backoff.reset();
                    return Some(Reopen {
                        intent: OpenIntent::Create,
                        delay: Duration::ZERO,
                    });
                }
                if let Some(max) = self.options.backoff.max_attempts
                    && self.refresh_streak > max.get()
                {
                    tracing::warn!(
                        streak = self.refresh_streak,
                        "the server kept asking for a fresh session; giving up"
                    );
                    return None;
                }
                let delay = self.backoff.next_delay()?;
                tracing::warn!(
                    streak = self.refresh_streak,
                    ?delay,
                    "repeated session refresh; delaying the next attempt"
                );
                Some(Reopen {
                    intent: OpenIntent::Create,
                    delay,
                })
            }

            // Handled by `is_clean_loop` above.
            UnbindReason::ForcedByClient { .. }
            | UnbindReason::ContentLengthReached { .. }
            | UnbindReason::PollCycleExpired { .. }
            | UnbindReason::Looped { .. } => Some(Reopen {
                intent: OpenIntent::Rebind,
                delay: Duration::ZERO,
            }),
        }
    }

    /// Reads lines until the stream connection ends or the session does.
    async fn pump(&mut self) -> Flow {
        let mut consecutive_lines: u32 = 0;
        loop {
            let deadline = self.liveness.next_deadline();
            // Deliberately biased, server first. The state machine must stay in
            // step with the server: a command issued against a session that has
            // already been unbound by a line sitting unread in the buffer would
            // be sent into a connection that no longer exists. Commands wait at
            // most until the line stream goes quiet, which on any real
            // connection is immediately — and the liveness timer, being last,
            // can only fire when neither of the other two has anything, which
            // is exactly the condition it exists to detect.
            //
            // Every `MAX_LINES_PER_TURN` lines the bias is inverted for one
            // turn. Without that, a connection that always has a line ready —
            // a busy server, or a hostile one — would postpone every command
            // and every liveness deadline for as long as it kept talking. The
            // inversion costs one reordering per 32 notifications and bounds
            // the latency of a `destroy`, a shutdown and a heartbeat.
            //
            // This requires `next_line` to be cancel-safe: the future is
            // dropped whenever another branch wins, and a line must not be lost
            // with it.
            let woke = if consecutive_lines >= MAX_LINES_PER_TURN {
                consecutive_lines = 0;
                tokio::select! {
                    biased;
                    command = self.commands.recv() => Woke::Command(command),
                    () = stopped(&mut self.stop) => Woke::Stop,
                    () = sleep_until_option(deadline) => Woke::Timer,
                    line = self.transport.next_line() => Woke::Line(line),
                }
            } else {
                tokio::select! {
                    biased;
                    line = self.transport.next_line() => Woke::Line(line),
                    command = self.commands.recv() => Woke::Command(command),
                    () = stopped(&mut self.stop) => Woke::Stop,
                    () = sleep_until_option(deadline) => Woke::Timer,
                }
            };
            consecutive_lines = if matches!(woke, Woke::Line(_)) {
                consecutive_lines.saturating_add(1)
            } else {
                0
            };

            let flow = match woke {
                Woke::Line(Some(Ok(line))) => self.on_line(line).await,
                Woke::Line(Some(Err(error))) => Flow::Unbind(UnbindReason::ConnectionFailed {
                    detail: error.to_string(),
                }),
                Woke::Line(None) => self.on_stream_end(),
                Woke::Command(Some(command)) => self.on_command(command).await,
                // Every handle was dropped: nobody can command the session and
                // nobody is left to care about its events.
                Woke::Command(None) | Woke::Stop => Flow::Closed(SessionClosed::ByClient {
                    destroy_confirmed: false,
                    cause: None,
                }),
                Woke::Timer => self.on_timer().await,
            };

            // A stop ordered while an event was waiting for room unwinds here:
            // the event is gone, and so is any reason to keep pumping.
            if self.stopping {
                return Flow::Closed(SessionClosed::ByClient {
                    destroy_confirmed: false,
                    cause: None,
                });
            }

            match flow {
                Flow::Continue => {}
                other => return other,
            }
        }
    }

    /// The transport reported a clean end of stream, with no `LOOP` and no
    /// error.
    fn on_stream_end(&mut self) -> Flow {
        if self.destroy_requested {
            // The destroy was sent; the connection ending without the `END`
            // that §2.2 T7 promises is still an ended session.
            return Flow::Closed(SessionClosed::ByClient {
                destroy_confirmed: false,
                cause: None,
            });
        }
        if self.properties.is_polling {
            // SPEC-AMBIGUITY: §7.1 describes the long-polling cycle as the
            // server unbinding "and closing the stream connection" without
            // mentioning `LOOP`, while §2.2 T4 says a `LOOP` is what marks the
            // poll cycle expiring. A polling transport that ends its stream
            // without a `LOOP` is therefore read as T4 — the benign reading —
            // rather than as T5. Reading it as T5 would ask the server to
            // recover a session that was never interrupted, on every single
            // poll cycle.
            return Flow::Unbind(UnbindReason::PollCycleExpired {
                expected_delay: Duration::ZERO,
            });
        }
        // T5: "a stream connection is dropped by any cause… upon connection
        // drop, the session is not closed, hence the Client is still allowed to
        // issue a rebind" [`docs/spec/02-session-lifecycle.md` §2.2, T5].
        Flow::Unbind(UnbindReason::ConnectionFailed {
            detail: "stream connection closed by the peer".to_owned(),
        })
    }

    /// A liveness deadline fell due.
    async fn on_timer(&mut self) -> Flow {
        match self.liveness.due(Instant::now()) {
            LivenessAction::Idle => Flow::Continue,
            LivenessAction::SendHeartbeat => self.send_heartbeat().await,
            LivenessAction::InboundStalled { budget } => match self.stream_state {
                // Nothing answered the handshake within the client's own limit.
                // The spec sets no such limit; see `SessionOptions::open_timeout`.
                StreamState::Establishing => Flow::Unbind(UnbindReason::ConnectionFailed {
                    detail: format!("no response to the stream-opening request within {budget:?}"),
                }),
                // §8.1: "if after the expected interval plus a configurable
                // timeout no `PROBE` has been received, the connection is closed
                // and reopened".
                StreamState::Bound => Flow::Unbind(UnbindReason::KeepaliveExpired { budget }),
            },
        }
    }

    /// One line from the server.
    async fn on_line(&mut self, line: String) -> Flow {
        // Every line resets the inbound clock, not only `PROBE`: the keep-alive
        // is the longest interval with no traffic of any kind
        // [`docs/spec/02-session-lifecycle.md` §8.1].
        self.liveness.on_inbound(Instant::now());

        match parse_line(&line) {
            Ok(notification) => self.on_notification(notification).await,
            Err(error) => {
                // SPEC-AMBIGUITY: an unparsed line is *not* counted toward the
                // recovery progressive, because this client cannot know whether
                // it was a data notification — and the one group the spec
                // leaves unclassified, MPN, is exactly the group this client
                // does not parse [`docs/spec/02-session-lifecycle.md` §5.3,
                // ambiguity A6]. Under-counting is the safe direction: it makes
                // a later recovery ask to resume from an *earlier* point, which
                // the protocol handles by re-delivering duplicates (§5.2), the
                // very case §5.2's fourth precondition is written for.
                // Over-counting would silently skip data.
                tracing::warn!(%error, "unrecognized line preserved for the caller");
                self.emit(SessionEvent::Unparsed { line, error }).await;
                Flow::Continue
            }
        }
    }

    /// The state machine proper: one arm per notification that means something
    /// to the lifecycle, everything else forwarded or counted.
    async fn on_notification(&mut self, notification: Notification) -> Flow {
        match notification {
            // T1 and T6 — the only evidence of a successful bind
            // [`docs/spec/02-session-lifecycle.md` §2.2, T1/T6].
            Notification::ConnectionOk {
                session_id,
                request_limit_bytes,
                keep_alive_millis,
                control_link,
            } => {
                self.on_conok(
                    SessionId::new(session_id),
                    request_limit_bytes,
                    Duration::from_millis(keep_alive_millis),
                    control_link,
                )
                .await;
                Flow::Continue
            }

            // The session could not be created or bound
            // [`docs/spec/02-session-lifecycle.md` §6.2].
            Notification::ConnectionError { code, message } => {
                let cause = ServerCause { code, message };
                match classify(code) {
                    Recovery::Fatal => {
                        tracing::warn!(code, "session refused with a code that admits no retry");
                        Flow::Closed(SessionClosed::ByServer { cause })
                    }
                    Recovery::RecreateSession | Recovery::RetryLater => {
                        // A rejected bind means the old session is unusable:
                        // it is retired so the next attempt creates a new one
                        // and re-executes every subscription [§6.1], while its
                        // identifier is kept for one more bind so the caller
                        // learns what the new session replaced.
                        self.previous_session = self.session.take();
                        Flow::Unbind(UnbindReason::Rejected { cause })
                    }
                }
            }

            // The server closed the session
            // [`docs/spec/02-session-lifecycle.md` §2.2 note, §6.2].
            Notification::End {
                cause_code,
                cause_message,
            } => self.on_end(cause_code, cause_message),

            // T2, T3 or T4, depending on why
            // [`docs/spec/02-session-lifecycle.md` §2.2, §4.2].
            Notification::Loop {
                expected_delay_millis,
            } => Flow::Unbind(self.loop_reason(Duration::from_millis(expected_delay_millis))),

            // Where a recovered flow resumes
            // [`docs/spec/02-session-lifecycle.md` §5.4].
            Notification::Progressive { progressive } => {
                self.on_prog(progressive).await;
                Flow::Continue
            }

            // A control request was answered [`docs/spec/03-requests.md` §13].
            Notification::RequestOk { request_id } => {
                self.on_request_ok(request_id).await;
                Flow::Continue
            }
            Notification::RequestError {
                request_id,
                code,
                message,
            } => {
                self.on_request_error(request_id, ServerCause { code, message })
                    .await;
                Flow::Continue
            }
            // `ERROR` carries no request id "because the server could not parse
            // one" [`docs/spec/03-requests.md` §13.2], so nothing can be
            // correlated; it is surfaced uncorrelated rather than guessed at.
            Notification::Error { code, message } => {
                tracing::warn!(code, "server rejected a request it could not correlate");
                self.emit(SessionEvent::ControlResponse {
                    request_id: None,
                    // `ERROR` names no request, so there is nothing to route
                    // it to [`docs/spec/03-requests.md` §13.2].
                    target: ControlTarget::Session,
                    outcome: ControlOutcome::Rejected {
                        cause: ServerCause { code, message },
                    },
                })
                .await;
                Flow::Continue
            }

            // Keep-alive: already accounted for by `on_line`, and explicitly
            // not a data notification
            // [`docs/spec/02-session-lifecycle.md` §5.3, §8.1].
            Notification::Probe => {
                tracing::trace!("probe");
                Flow::Continue
            }
            // "The purpose of this notification is to fill the receive buffer";
            // the payload "is to be ignored"
            // [`docs/spec/04-notifications.md` §5.6].
            Notification::NoOp { .. } => Flow::Continue,
            // The WebSocket establishment check's echo; it means the channel
            // carries TLCP end to end [`docs/spec/03-requests.md` §15.2].
            Notification::WsOk => {
                tracing::debug!("websocket establishment check acknowledged");
                Flow::Continue
            }

            // Session-related, not data
            // [`docs/spec/02-session-lifecycle.md` §5.3].
            Notification::Sync { elapsed_seconds } => {
                self.emit(SessionEvent::ServerInfo(ServerAnnouncement::Clock {
                    elapsed_seconds,
                }))
                .await;
                Flow::Continue
            }
            Notification::ClientIp { address } => {
                self.emit(SessionEvent::ServerInfo(ServerAnnouncement::ClientIp(
                    address,
                )))
                .await;
                Flow::Continue
            }
            Notification::ServerName { name } => {
                self.emit(SessionEvent::ServerInfo(ServerAnnouncement::Name(name)))
                    .await;
                Flow::Continue
            }
            Notification::ConstraintsChanged { bandwidth } => {
                self.emit(SessionEvent::ServerInfo(ServerAnnouncement::Bandwidth(
                    bandwidth,
                )))
                .await;
                Flow::Continue
            }

            // Everything left is a data notification
            // [`docs/spec/02-session-lifecycle.md` §5.3].
            data => {
                self.on_data(data).await;
                Flow::Continue
            }
        }
    }

    /// Handles `CONOK`: the *Unbound → Bound* transition
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T1/T6].
    async fn on_conok(
        &mut self,
        session_id: SessionId,
        request_limit_bytes: u64,
        keep_alive: Duration,
        control_link: Option<String>,
    ) {
        self.stream_state = StreamState::Bound;
        self.liveness.on_bound(keep_alive, Instant::now());
        // Remember it: on a polling connection this is `LS_idle_millis`, and the
        // next poll cycle's establishment budget must exceed it
        // [`SessionDriver::establishment_budget`].
        self.negotiated_keep_alive = Some(keep_alive);
        self.backoff.reset();

        // "upon the first time the 'control link' address is used to open a new
        // stream connection to rebind to a session, the streaming activity…
        // will switch to the 'control link'"; the literal `*` means keep using
        // the current address [`docs/spec/02-session-lifecycle.md` §3.1].
        self.transport.set_control_link(control_link.as_deref());

        let previous = self.session.take().or_else(|| self.previous_session.take());
        let same_session = previous.as_ref() == Some(&session_id);
        self.session = Some(session_id.clone());
        self.previous_session = None;

        let kind = match (&self.intent, same_session) {
            (OpenIntent::Rebind, true) => BindKind::Rebound,
            (OpenIntent::Recover { requested }, true) => BindKind::Recovering {
                requested_progressive: *requested,
            },
            // A `bind_session` answered with a *different* session id. The spec
            // does not contemplate it — every transcript shows the id unchanged
            // across a rebind and a recovery
            // [`docs/spec/02-session-lifecycle.md` §10, F7/F8].
            //
            // SPEC-AMBIGUITY: nothing says what a client should do here.
            // Treating it as a replacement is the only safe reading: the
            // progressive and the `LS_subId` space belong to a session, so
            // carrying them into a different one would corrupt both.
            (OpenIntent::Rebind | OpenIntent::Recover { .. }, false) => {
                tracing::warn!(
                    old = %previous.as_ref().map_or("<none>", SessionId::as_str),
                    new = %session_id,
                    "bind returned a different session id; treating it as a new session"
                );
                BindKind::Recreated { previous }
            }
            (OpenIntent::Create, _) => match previous {
                Some(previous) => BindKind::Recreated {
                    previous: Some(previous),
                },
                None => BindKind::Created,
            },
        };

        // A new session restarts the progressive at zero and the `LS_subId`
        // space at 1 [`docs/spec/02-session-lifecycle.md` §9.5].
        let fresh = matches!(kind, BindKind::Created | BindKind::Recreated { .. });
        if fresh {
            self.progressive = 0;
            self.skip_remaining = 0;
            self.registry.reset_for_new_session();
            self.generation = self.generation.saturating_add(1);
            self.retire_stale_pending().await;
        }

        tracing::info!(session = %session_id, ?kind, ?keep_alive, "session bound");
        self.emit(SessionEvent::Bound(Box::new(BoundInfo {
            session_id,
            kind,
            keep_alive,
            request_limit_bytes,
            control_link,
        })))
        .await;

        // On a rebind or a recovery the server preserves every subscription
        // "with all their items and fields", so re-issuing them would create
        // duplicates [`docs/spec/02-session-lifecycle.md` §4.4]. Only
        // subscriptions with no binding in the current session are issued —
        // which, after a replacement, is all of them.
        self.issue_unbound_subscriptions().await;
    }

    /// Retires every control request issued by a session that has been
    /// replaced.
    ///
    /// §4.5 promises the case outright: "after binding the second session, it
    /// is possible that late responses to control requests related with the
    /// first session arrive interspersed with notifications for the second
    /// session" [`docs/spec/02-session-lifecycle.md` §4.5]. Once the session
    /// they named is gone, their responses can say nothing true about the
    /// replacement — an `add` refused on the old session says nothing about the
    /// `add` just re-issued on the new one — so they are closed out here, at
    /// the boundary, and anything that arrives for them afterwards is reported
    /// but acts on nothing.
    ///
    /// Every retired request produces exactly one outcome, which is what keeps
    /// "every message gets exactly one report" true across a session
    /// replacement.
    async fn retire_stale_pending(&mut self) {
        let generation = self.generation;
        let stale: Vec<(String, PendingKind)> = self
            .pending
            .extract_if(|_, pending| pending.generation != generation)
            .map(|(id, pending)| (id, pending.kind))
            .collect();
        if stale.is_empty() {
            return;
        }
        tracing::debug!(
            count = stale.len(),
            "retiring control requests issued by a session that was replaced"
        );
        for (request_id, kind) in stale {
            let target = kind.target();
            // No `LOOP` can now be attributed to a `force_rebind` the previous
            // session never answered.
            if matches!(kind, PendingKind::ForceRebind) {
                self.force_rebind_outstanding = false;
            }
            if let PendingKind::SendMessage { sequence, prog } = kind {
                self.emit(SessionEvent::Message {
                    progressive: None,
                    sequence,
                    prog,
                    result: MessageResult::NotSent {
                        reason: "the session the message was sent on was replaced".to_owned(),
                    },
                })
                .await;
            }
            self.emit(SessionEvent::ControlResponse {
                request_id: RequestId::try_new(request_id).ok(),
                target,
                outcome: ControlOutcome::NotSent {
                    reason: "the session the request was issued on was replaced".to_owned(),
                },
            })
            .await;
        }
    }

    /// Handles `END` [`docs/spec/02-session-lifecycle.md` §6.2].
    fn on_end(&mut self, cause_code: i64, cause_message: String) -> Flow {
        let cause = ServerCause {
            code: cause_code,
            message: cause_message,
        };
        if self.destroy_requested {
            // T7: "If successful, an `END` notification is sent on the stream
            // connection. After that… the session is terminated"
            // [`docs/spec/02-session-lifecycle.md` §2.2, T7]. The default code
            // is 31 [ibid. §10, F5].
            return Flow::Closed(SessionClosed::ByClient {
                destroy_confirmed: true,
                cause: Some(cause),
            });
        }
        // SPEC-AMBIGUITY (A2): "the p.8 diagram has no edge labelled for a
        // server-initiated `END` on a bound session… The spec does not
        // reconcile the diagram with the `END` cause codes of Appendix A"
        // [`docs/spec/02-session-lifecycle.md` §2.2]. It is treated here as an
        // unlabelled edge out of *Bound*, terminal by default, with the single
        // documented exception of code 48.
        match classify(cause_code) {
            Recovery::RecreateSession => {
                tracing::info!(code = cause_code, "server asked for a fresh session");
                self.previous_session = self.session.take();
                Flow::Unbind(UnbindReason::ServerRefresh { cause })
            }
            Recovery::RetryLater => {
                self.previous_session = self.session.take();
                Flow::Unbind(UnbindReason::Rejected { cause })
            }
            Recovery::Fatal => {
                tracing::warn!(code = cause_code, "server ended the session");
                Flow::Closed(SessionClosed::ByServer { cause })
            }
        }
    }

    /// Attributes a `LOOP` to the transition that caused it.
    ///
    /// The attribution reads declared transport properties, never a transport
    /// identity (`docs/adr/0007-transport-port-shape.md`).
    fn loop_reason(&mut self, expected_delay: Duration) -> UnbindReason {
        if self.force_rebind_outstanding {
            self.force_rebind_outstanding = false;
            // T2 — "If successful, a `LOOP` notification is sent on the stream
            // connection" [`docs/spec/02-session-lifecycle.md` §2.2, T2].
            return UnbindReason::ForcedByClient { expected_delay };
        }
        if self.properties.is_polling {
            // T4 — "The polling cycle is complete"
            // [`docs/spec/02-session-lifecycle.md` §2.2, T4].
            return UnbindReason::PollCycleExpired { expected_delay };
        }
        if self.properties.ends_on_content_length {
            // T3 — "The `Content-Length` of an HTTP stream connection has been
            // reached" [`docs/spec/02-session-lifecycle.md` §2.2, T3].
            return UnbindReason::ContentLengthReached { expected_delay };
        }
        UnbindReason::Looped { expected_delay }
    }

    /// Handles `PROG` [`docs/spec/02-session-lifecycle.md` §5.4].
    ///
    /// Only a `PROG` that answers a recovery this client asked for is obeyed.
    /// The notification is "sent only when requested via `LS_recovery_from` on
    /// `bind_session` requests", and it is one of the notifications "sent
    /// immediately after `CONOK`" [ibid. §5.4] — so exactly one is expected per
    /// recovery bind and none at all otherwise. Obeying an unsolicited one
    /// would let a single line move the recovery baseline to any value at all,
    /// and every later decision about continuity and duplicates rests on that
    /// number. An unexpected one is reported and ignored.
    async fn on_prog(&mut self, resumed_at: u64) {
        let requested = match self.intent {
            OpenIntent::Recover { requested } if !self.prog_honoured => requested,
            OpenIntent::Recover { .. } => {
                tracing::warn!(
                    resumed_at,
                    "ignoring a second PROG on one stream connection"
                );
                return;
            }
            _ => {
                tracing::warn!(
                    resumed_at,
                    "ignoring a PROG that answers no recovery request"
                );
                return;
            }
        };
        self.prog_honoured = true;

        let kind = match resumed_at.cmp(&requested) {
            std::cmp::Ordering::Equal => RecoveryKind::Exact,

            // Precondition 4: "the first data notifications received are
            // discarded, until the desired point is reached"
            // [`docs/spec/02-session-lifecycle.md` §5.2].
            std::cmp::Ordering::Less => match requested.checked_sub(resumed_at) {
                Some(count) => {
                    self.skip_remaining = count;
                    RecoveryKind::Duplicated { count }
                }
                // Unreachable: the ordering was established by the match.
                None => RecoveryKind::Exact,
            },

            // SPEC-AMBIGUITY (A7): see `RecoveryKind::Gap`.
            std::cmp::Ordering::Greater => match resumed_at.checked_sub(requested) {
                Some(missing) => {
                    self.progressive = resumed_at;
                    tracing::warn!(
                        requested,
                        resumed_at,
                        missing,
                        "recovery resumed past the requested point"
                    );
                    RecoveryKind::Gap { missing }
                }
                None => RecoveryKind::Exact,
            },
        };

        self.emit(SessionEvent::Recovered(RecoveryOutcome {
            requested,
            resumed_at,
            kind,
        }))
        .await;
    }

    /// Counts, annotates and forwards a data notification
    /// [`docs/spec/02-session-lifecycle.md` §5.3].
    async fn on_data(&mut self, notification: Notification) {
        // A session that delivered data was productive, whatever it does next:
        // it is not part of a refresh loop. See [`SessionDriver::plan_after`].
        self.refresh_streak = 0;

        if self.skip_remaining > 0 {
            // Guarded by the branch above, so the subtraction cannot wrap.
            if let Some(remaining) = self.skip_remaining.checked_sub(1) {
                self.skip_remaining = remaining;
            }
            tracing::trace!(
                remaining = self.skip_remaining,
                "discarding a duplicate re-delivered by recovery"
            );
            return;
        }

        self.progressive = match self.progressive.checked_add(1) {
            Some(next) => next,
            None => {
                tracing::error!(
                    "data-notification counter overflowed; recovery is no longer exact"
                );
                self.progressive
            }
        };

        let progressive = self.progressive;

        // `MSGDONE` and `MSGFAIL` were counted above like every other data
        // notification [`docs/spec/02-session-lifecycle.md` §5.3]; they are
        // merely delivered on the message surface instead of the raw one,
        // because they answer a `msg` the caller sent.
        match notification {
            Notification::MessageDone {
                sequence,
                prog,
                response,
            } => {
                self.emit(SessionEvent::Message {
                    progressive: Some(progressive),
                    sequence,
                    prog: Some(prog),
                    result: MessageResult::Done { response },
                })
                .await;
            }
            Notification::MessageFailed {
                sequence,
                prog,
                code,
                message,
            } => {
                self.emit(SessionEvent::Message {
                    progressive: Some(progressive),
                    sequence,
                    prog: Some(prog),
                    result: MessageResult::Failed {
                        cause: ServerCause { code, message },
                    },
                })
                .await;
            }
            notification => {
                if let Some(event) = self.interpret_for_subscription(progressive, &notification) {
                    self.emit(event).await;
                }
            }
        }
    }

    /// Applies a subscription-bearing notification to the subscription it names
    /// and produces the event that goes upward, if any.
    ///
    /// The wire line stops here. The layer above receives a typed
    /// [`SubscriptionEvent`] — or the typed reason the line could not be
    /// reconciled — and never a [`Notification`], which is what keeps the
    /// public facade independent of the parser.
    fn interpret_for_subscription(
        &mut self,
        progressive: u64,
        notification: &Notification,
    ) -> Option<SessionEvent> {
        let Some((sub_id, key)) = self.resolve_subscription(notification) else {
            // A data notification naming no subscription this client knows:
            // either it carries no `<subscription-ID>` at all, or it names one
            // that has already gone. Logged rather than invented into an event.
            tracing::debug!(?notification, "unattributed data notification");
            return None;
        };
        let Some(entry) = self.registry.entries.get_mut(&key) else {
            tracing::debug!(
                key = key.get(),
                "a notification named a retired subscription"
            );
            return None;
        };

        // The manager sees the line **before** the registry acts on it, so that
        // the `UNSUB` which removes the entry is still delivered as the
        // subscription's own terminal event.
        let outcome = entry.manager.handle(notification);

        match notification {
            // "Real-time updates for the subscription start after this line"
            // [`docs/spec/04-notifications.md` §3.1].
            Notification::SubscriptionOk { .. } | Notification::SubscriptionCommandOk { .. } => {
                entry.state = SubscriptionState::Active;
                entry.was_active = true;
            }
            // "After this line no further update for the subscription will be
            // sent" [`docs/spec/04-notifications.md` §3.4], so the desired set
            // loses it too — the caller asked for that.
            Notification::Unsubscribed { .. } => {
                self.registry.entries.remove(&key);
                self.registry.by_sub_id.remove(&sub_id);
            }
            _ => {}
        }

        match outcome {
            // A notification this subscription does not care about.
            Ok(None) => None,
            Ok(Some(event)) => Some(SessionEvent::Subscription {
                progressive,
                key,
                outcome: Box::new(Ok(event)),
            }),
            Err(error) => Some(SessionEvent::Subscription {
                progressive,
                key,
                outcome: Box::new(Err(error)),
            }),
        }
    }

    /// The `LS_subId` a notification names and the caller-facing key it maps
    /// to, when it names one this session knows.
    fn resolve_subscription(&self, notification: &Notification) -> Option<(u32, SubscriptionKey)> {
        let sub_id = match notification {
            Notification::Update {
                subscription_id, ..
            }
            | Notification::SubscriptionOk {
                subscription_id, ..
            }
            | Notification::SubscriptionCommandOk {
                subscription_id, ..
            }
            | Notification::Unsubscribed { subscription_id }
            | Notification::EndOfSnapshot {
                subscription_id, ..
            }
            | Notification::ClearSnapshot {
                subscription_id, ..
            }
            | Notification::Overflow {
                subscription_id, ..
            }
            | Notification::SubscriptionReconfigured {
                subscription_id, ..
            } => *subscription_id,
            _ => return None,
        };
        let key = self.registry.by_sub_id.get(&sub_id).copied()?;
        Some((sub_id, key))
    }

    /// Takes the pending request a response names, if it still belongs to the
    /// session this driver is on.
    ///
    /// A request issued by a session that has since been replaced is retired at
    /// the boundary ([`SessionDriver::retire_stale_pending`]), so finding one
    /// here with an older generation means the response outran the retirement.
    /// It is dropped rather than acted on: §4.5 warns that these arrive, and
    /// letting one mutate the replacement session's registry is how a live
    /// subscription disappears.
    fn take_pending(&mut self, request_id: &str) -> Option<PendingKind> {
        let pending = self.pending.remove(request_id)?;
        if pending.generation == self.generation {
            return Some(pending.kind);
        }
        tracing::debug!(
            request_id,
            issued_on = pending.generation,
            current = self.generation,
            "ignoring a late response from a session that was replaced"
        );
        None
    }

    /// Correlates a `REQOK` with the request that caused it
    /// [`docs/spec/03-requests.md` §13.1].
    async fn on_request_ok(&mut self, request_id: Option<String>) {
        let Some(request_id) = request_id else {
            // The bare `REQOK` that answers an HTTP `heartbeat`, which carries
            // no `LS_reqId` [`docs/spec/03-requests.md` §14.3].
            tracing::trace!("heartbeat acknowledged");
            return;
        };

        let target = match self.take_pending(&request_id) {
            Some(kind) => {
                tracing::debug!(request_id, ?kind, "control request accepted");
                let target = kind.target();
                // The one acceptance that changes desired state. Everything
                // else the server accepts is reported by the notification that
                // follows it — `SUBOK` for an `add`, `UNSUB` for a `delete` —
                // but `reconf` has no such notification of its own, and the
                // value it granted has to survive a session that is recreated
                // and re-issues every `add` [ibid. §6.1]. Committing it here,
                // and only here, is what stops a reconnection silently putting
                // the caller's frequency ceiling back where it started.
                if let PendingKind::Reconfigure { key, max_frequency } = kind
                    && let Some(entry) = self.registry.entries.get_mut(&key)
                {
                    entry.spec.requested_max_frequency =
                        Some(as_requested_frequency(max_frequency));
                    tracing::debug!(
                        key = key.get(),
                        "frequency reconfiguration accepted and committed to the desired state"
                    );
                }
                target
            }
            // §4.5: "late responses to control requests related with the first
            // session [may] arrive interspersed with notifications for the
            // second session". Request ids are never reused, so an unknown one
            // is a stale response: it is reported, but it names nothing this
            // driver is still tracking.
            None => {
                tracing::debug!(request_id, "response to an unknown or stale request");
                ControlTarget::Session
            }
        };

        let outcome = ControlOutcome::Accepted;
        match RequestId::try_new(request_id.clone()) {
            Ok(id) => {
                self.emit(SessionEvent::ControlResponse {
                    request_id: Some(id),
                    target,
                    outcome,
                })
                .await;
            }
            Err(error) => {
                tracing::warn!(%error, "server echoed a request id this client cannot represent");
                self.emit(SessionEvent::ControlResponse {
                    request_id: None,
                    target,
                    outcome,
                })
                .await;
            }
        }
    }

    /// Correlates a `REQERR` and undoes whatever the request had optimistically
    /// recorded [`docs/spec/03-requests.md` §13.2].
    async fn on_request_error(&mut self, request_id: String, cause: ServerCause) {
        let pending = self.take_pending(&request_id);
        let target = pending
            .as_ref()
            .map_or(ControlTarget::Session, PendingKind::target);

        if let Some(kind) = pending {
            match kind {
                // The server refused the subscription. Dropping it from the
                // desired set is what stops it being re-issued on every future
                // session — and the caller is told, so nothing is lost
                // silently.
                PendingKind::Subscribe(key) => {
                    if let Some(entry) = self.registry.entries.remove(&key)
                        && let Some(sub_id) = entry.wire
                    {
                        self.registry.by_sub_id.remove(&sub_id.get());
                    }
                    tracing::warn!(
                        key = key.get(),
                        code = cause.code,
                        "subscription refused by the server"
                    );
                }
                // The delete failed, so the subscription is still there.
                PendingKind::Unsubscribe(key) => {
                    if let Some(entry) = self.registry.entries.get_mut(&key) {
                        entry.state = if entry.was_active {
                            SubscriptionState::Active
                        } else {
                            SubscriptionState::Pending
                        };
                    }
                }
                // The subscription keeps the ceiling it already had: the new
                // one is only committed on `REQOK`.
                PendingKind::Reconfigure { .. } => {}
                // The server refused the submission, so no `MSGDONE`/`MSGFAIL`
                // will follow [`docs/spec/03-requests.md` §13.2]. Reporting it
                // on the message surface as well is what keeps "every message
                // gets exactly one outcome" true.
                PendingKind::SendMessage { sequence, prog } => {
                    self.emit(SessionEvent::Message {
                        progressive: None,
                        sequence,
                        prog,
                        result: MessageResult::Refused {
                            cause: cause.clone(),
                        },
                    })
                    .await;
                }
                // No `LOOP` will follow, so the next one must not be
                // misattributed to T2.
                PendingKind::ForceRebind => self.force_rebind_outstanding = false,
                // No `END` will follow.
                PendingKind::Destroy => self.destroy_requested = false,
            }
        }

        let outcome = ControlOutcome::Rejected { cause };
        let id = RequestId::try_new(request_id).ok();
        self.emit(SessionEvent::ControlResponse {
            request_id: id,
            target,
            outcome,
        })
        .await;
    }

    // -----------------------------------------------------------------------
    // Commands
    // -----------------------------------------------------------------------

    async fn on_command(&mut self, command: SessionCommand) -> Flow {
        match command {
            SessionCommand::Subscribe { key, spec } => {
                let manager = spec.new_manager();
                self.registry.entries.insert(
                    key,
                    RegistryEntry {
                        spec: *spec,
                        wire: None,
                        state: SubscriptionState::Pending,
                        was_active: false,
                        manager,
                    },
                );
                if self.stream_state == StreamState::Bound {
                    self.issue_subscription(key).await;
                }
                Flow::Continue
            }

            SessionCommand::Unsubscribe { key } => {
                self.unsubscribe(key).await;
                Flow::Continue
            }

            SessionCommand::Reconfigure { key, max_frequency } => {
                self.reconfigure(key, max_frequency).await;
                Flow::Continue
            }

            SessionCommand::SendMessage { message } => {
                self.send_message(*message).await;
                Flow::Continue
            }

            SessionCommand::ForceRebind { close_socket } => {
                if self.stream_state != StreamState::Bound {
                    tracing::debug!("force_rebind ignored: no stream connection is bound");
                    return Flow::Continue;
                }
                let Some(session) = self.session.clone() else {
                    return Flow::Continue;
                };
                let Some(request_id) = self.next_request_id() else {
                    self.report_id_exhaustion(ControlTarget::Session).await;
                    return Flow::Continue;
                };
                // Set before sending: the `LOOP` may well arrive before the
                // `REQOK` [`docs/spec/02-session-lifecycle.md` §4.5].
                self.force_rebind_outstanding = true;
                let request = ForceRebind {
                    session: Some(session.as_str().to_owned()),
                    request_id: request_id.clone(),
                    polling_millis: None,
                    close_socket,
                };
                self.dispatch(&request, request_id, PendingKind::ForceRebind)
                    .await;
                Flow::Continue
            }

            SessionCommand::Destroy { cause } => self.destroy(cause).await,

            SessionCommand::Shutdown => Flow::Closed(SessionClosed::ByClient {
                destroy_confirmed: false,
                cause: None,
            }),
        }
    }

    /// T7 while bound, T8 while unbound
    /// [`docs/spec/02-session-lifecycle.md` §2.2, T7/T8].
    async fn destroy(&mut self, cause: Option<DestroyCause>) -> Flow {
        let Some(session) = self.session.clone() else {
            return Flow::Closed(SessionClosed::ByClient {
                destroy_confirmed: false,
                cause: None,
            });
        };
        let Some(request_id) = self.next_request_id() else {
            self.report_id_exhaustion(ControlTarget::Session).await;
            return Flow::Closed(SessionClosed::Internal {
                reason: "the request-id space is exhausted".to_owned(),
            });
        };
        self.destroy_requested = true;
        let request = DestroySession {
            session: Some(session.as_str().to_owned()),
            request_id: request_id.clone(),
            cause,
            close_socket: None,
        };
        let sent = self
            .dispatch(&request, request_id, PendingKind::Destroy)
            .await;

        if self.stream_state == StreamState::Bound && sent {
            // Stay in the pump and wait for the `END` that T7 promises, so the
            // caller learns the server's cause code.
            return Flow::Continue;
        }

        // SPEC-AMBIGUITY (A1): with no stream connection bound "there is
        // nowhere for the `END` notification… to be delivered; the spec does
        // not say whether `END` is dropped, buffered, or suppressed in this
        // case" [`docs/spec/02-session-lifecycle.md` §2.2, T8]. The driver
        // therefore does not wait for one: the request has been sent, and
        // waiting for a notification the spec does not promise would hang the
        // shutdown.
        Flow::Closed(SessionClosed::ByClient {
            destroy_confirmed: false,
            cause: None,
        })
    }

    /// Sends one message, or reports at once that it could not be sent
    /// [`docs/spec/03-requests.md` §12].
    ///
    /// There is no queue behind this. See [`MessageResult::NotSent`] for why a
    /// message is not held the way a subscription is.
    async fn send_message(&mut self, message: OutgoingMessage) {
        let (sequence, prog) = message.identity();

        // `msg` needs a bound session for the same reason every control
        // request does, and — unlike a subscription — there is nothing
        // meaningful to do with it until one exists.
        let session = match (self.stream_state, self.session.clone()) {
            (StreamState::Bound, Some(session)) => session,
            _ => {
                tracing::debug!(?sequence, ?prog, "message not sent: no session is bound");
                self.emit(SessionEvent::Message {
                    progressive: None,
                    sequence,
                    prog,
                    result: MessageResult::NotSent {
                        reason: "no session is bound to send the message on".to_owned(),
                    },
                })
                .await;
                return;
            }
        };

        let Some(request_id) = self.next_request_id() else {
            self.report_id_exhaustion(ControlTarget::Session).await;
            self.emit(SessionEvent::Message {
                progressive: None,
                sequence,
                prog,
                result: MessageResult::NotSent {
                    reason: "the request-id space is exhausted".to_owned(),
                },
            })
            .await;
            return;
        };
        let request = MessageSend {
            session: Some(session.as_str().to_owned()),
            request_id: request_id.clone(),
            message: message.message,
            sequence: message.sequence,
            msg_prog: message.msg_prog,
            max_wait_millis: message.max_wait_millis,
            // Left at the server's default of `true`: the `REQOK` is what
            // correlates the submission with this request
            // [`docs/spec/03-requests.md` §12.1].
            ack: None,
            outcome: message.outcome,
        };

        let kind = PendingKind::SendMessage {
            sequence: sequence.clone(),
            prog,
        };
        if !self.dispatch(&request, request_id, kind).await {
            // `dispatch` has already reported the transport or encoding
            // failure as a `ControlResponse`; it is repeated here on the
            // message surface so a caller watching only that surface cannot
            // lose a message silently.
            self.emit(SessionEvent::Message {
                progressive: None,
                sequence,
                prog,
                result: MessageResult::NotSent {
                    reason: "the message request could not be sent".to_owned(),
                },
            })
            .await;
        }
    }

    async fn unsubscribe(&mut self, key: SubscriptionKey) {
        let Some(entry) = self.registry.entries.get_mut(&key) else {
            tracing::debug!(key = key.get(), "unsubscribe for an unknown subscription");
            return;
        };
        let Some(sub_id) = entry.wire else {
            // Never issued on this session, so there is nothing to delete: the
            // caller simply stops wanting it.
            self.registry.entries.remove(&key);
            return;
        };
        entry.state = SubscriptionState::Removing;

        let Some(session) = self.session.clone() else {
            return;
        };
        let Some(request_id) = self.next_request_id() else {
            self.report_id_exhaustion(ControlTarget::Subscription {
                key,
                operation: SubscriptionOperation::Unsubscribe,
            })
            .await;
            return;
        };
        let request = Unsubscribe {
            session: Some(session.as_str().to_owned()),
            request_id: request_id.clone(),
            subscription_id: sub_id,
            // Left at the server's default of `true`: the `REQOK` is what
            // correlates the response to this request
            // [`docs/spec/03-requests.md` §7.1].
            ack: None,
        };
        self.dispatch(&request, request_id, PendingKind::Unsubscribe(key))
            .await;
    }

    /// Asks the server to change a subscription's frequency ceiling
    /// [`docs/spec/03-requests.md` §8].
    ///
    /// The new value is **not** written into the desired state here. Until the
    /// server has accepted the `reconf` the subscription still has its old
    /// ceiling, and a session recreated in between must re-issue what the
    /// server actually granted, not what it was last asked for. The commit
    /// happens on `REQOK` [`docs/spec/03-requests.md` §13.1], in
    /// [`SessionDriver::on_request_ok`].
    async fn reconfigure(&mut self, key: SubscriptionKey, max_frequency: MaxFrequencyLimit) {
        let Some(entry) = self.registry.entries.get(&key) else {
            return;
        };
        let Some(sub_id) = entry.wire else {
            return;
        };
        let Some(session) = self.session.clone() else {
            return;
        };
        let Some(request_id) = self.next_request_id() else {
            self.report_id_exhaustion(ControlTarget::Subscription {
                key,
                operation: SubscriptionOperation::Reconfigure,
            })
            .await;
            return;
        };
        let request = ReconfigureSubscription {
            session: Some(session.as_str().to_owned()),
            request_id: request_id.clone(),
            subscription_id: sub_id,
            requested_max_frequency: Some(max_frequency.clone()),
        };
        self.dispatch(
            &request,
            request_id,
            PendingKind::Reconfigure { key, max_frequency },
        )
        .await;
    }

    // -----------------------------------------------------------------------
    // Subscriptions
    // -----------------------------------------------------------------------

    /// Issues every subscription that has no binding in the current session.
    ///
    /// After a rebind or a recovery that is nothing, because the server
    /// preserved them all [`docs/spec/02-session-lifecycle.md` §4.4]. After a
    /// session replacement it is every one of them, which is precisely the
    /// obligation of §6.1: "re-execute all the subscriptions that were active
    /// at the moment the previous stream connection terminated".
    async fn issue_unbound_subscriptions(&mut self) {
        let keys: Vec<SubscriptionKey> = self
            .registry
            .entries
            .iter()
            .filter(|(_, entry)| entry.wire.is_none() && entry.state != SubscriptionState::Removing)
            .map(|(key, _)| *key)
            .collect();
        if keys.is_empty() {
            return;
        }

        let mut issued = Vec::with_capacity(keys.len());
        for key in keys {
            if let Some(entry) = self.issue_subscription(key).await {
                issued.push(entry);
            }
        }
        if !issued.is_empty() {
            tracing::info!(count = issued.len(), "subscriptions re-established");
            self.emit(SessionEvent::Resubscribed(issued)).await;
        }
    }

    /// Sends one `add` for a subscription that has no binding yet.
    async fn issue_subscription(&mut self, key: SubscriptionKey) -> Option<ResubscribedEntry> {
        let session = self.session.clone()?;
        let sub_id = self.registry.allocate()?;
        let entry = self.registry.entries.get_mut(&key)?;
        entry.wire = Some(sub_id);
        entry.state = SubscriptionState::Pending;
        let spec = entry.spec.clone();
        let previously_active = entry.was_active;
        self.registry.by_sub_id.insert(sub_id.get(), key);

        let Some(request_id) = self.next_request_id() else {
            if let Some(entry) = self.registry.entries.get_mut(&key) {
                entry.wire = None;
            }
            self.registry.by_sub_id.remove(&sub_id.get());
            self.report_id_exhaustion(ControlTarget::Subscription {
                key,
                operation: SubscriptionOperation::Subscribe,
            })
            .await;
            return None;
        };
        let request = Subscribe {
            session: Some(session.as_str().to_owned()),
            request_id: request_id.clone(),
            subscription_id: sub_id,
            group: spec.group.clone(),
            schema: spec.schema.clone(),
            mode: spec.mode,
            data_adapter: spec.data_adapter.clone(),
            selector: spec.selector.clone(),
            requested_buffer_size: spec.requested_buffer_size,
            requested_max_frequency: spec.requested_max_frequency.clone(),
            snapshot: spec.snapshot,
            // Left at the server's default of `true`: `REQOK` is what
            // correlates the response to this request
            // [`docs/spec/03-requests.md` §6.1].
            ack: None,
        };
        if !self
            .dispatch(&request, request_id, PendingKind::Subscribe(key))
            .await
        {
            // The `add` never reached the server, so the binding recorded a
            // moment ago is a fiction: the subscription would sit in the
            // registry looking issued, be skipped by every later
            // `issue_unbound_subscriptions`, and never be established again —
            // a subscription lost in silence. Releasing the binding puts it
            // back in the set that is re-issued at the next bind.
            //
            // The `LS_subId` itself is **not** returned to the pool: ids
            // "must not be reused" within a session
            // [`docs/spec/02-session-lifecycle.md` §4.4], so the next attempt
            // allocates a fresh one.
            if let Some(entry) = self.registry.entries.get_mut(&key) {
                entry.wire = None;
            }
            self.registry.by_sub_id.remove(&sub_id.get());
            tracing::warn!(
                key = key.get(),
                "subscription could not be issued; it will be retried on the next bind"
            );
            return None;
        }

        Some(ResubscribedEntry {
            key,
            subscription_id: sub_id,
            previously_active,
            snapshot_requested: spec.wants_snapshot(),
        })
    }

    // -----------------------------------------------------------------------
    // Sending
    // -----------------------------------------------------------------------

    /// The next `LS_reqId`, or `None` once the space is exhausted.
    ///
    /// See the note on the [`SessionDriver::next_request_id`] field for why it
    /// never resets. It also never wraps: an id that came back round would
    /// collide with a request whose late response §4.5 says may still be in
    /// flight, and correlating a response to the wrong request is worse than
    /// refusing to send a new one. Exhausting a 64-bit space takes longer than
    /// any session lives, so the refusal is a proof obligation rather than an
    /// operating condition.
    fn next_request_id(&mut self) -> Option<RequestId> {
        let next = self.next_request_id.checked_add(1)?;
        let id = RequestId::from_progressive(self.next_request_id);
        self.next_request_id = next;
        Some(id)
    }

    /// Reports that a request could not be given an id, on the surface that
    /// request would have been answered on.
    async fn report_id_exhaustion(&mut self, target: ControlTarget) {
        tracing::error!(
            "the request-id space is exhausted; no further control request can be sent"
        );
        self.emit(SessionEvent::ControlResponse {
            request_id: None,
            target,
            outcome: ControlOutcome::NotSent {
                reason: "the request-id space is exhausted".to_owned(),
            },
        })
        .await;
    }

    /// Which encoding rules apply to a control request on this transport.
    ///
    /// A declared property, not a transport identity: `control_shares_stream`
    /// is exactly the WebSocket case, where `LS_ack` is meaningful and
    /// `LS_session` may be defaulted [`docs/spec/03-requests.md` §5.1;
    /// `docs/adr/0007-transport-port-shape.md`].
    #[must_use]
    const fn control_kind(&self) -> TransportKind {
        if self.properties.control_shares_stream {
            TransportKind::WebSocket
        } else {
            TransportKind::Http
        }
    }

    /// Encodes and sends a control request, recording it for correlation.
    ///
    /// Returns whether it reached the transport. A failure is surfaced to the
    /// caller as [`ControlOutcome::NotSent`] rather than swallowed.
    async fn dispatch<R: TlcpRequest>(
        &mut self,
        request: &R,
        request_id: RequestId,
        kind: PendingKind,
    ) -> bool {
        let target = kind.target();
        let parameters = match request.encode_parameters(self.control_kind()) {
            Ok(parameters) => parameters,
            Err(error) => {
                tracing::error!(%error, "cannot encode control request");
                self.emit(SessionEvent::ControlResponse {
                    request_id: Some(request_id),
                    target,
                    outcome: ControlOutcome::NotSent {
                        reason: error.to_string(),
                    },
                })
                .await;
                return false;
            }
        };

        self.pending.insert(
            request_id.as_str().to_owned(),
            Pending {
                kind,
                generation: self.generation,
            },
        );
        let encoded = EncodedRequest {
            name: R::NAME,
            path: R::PATH,
            parameters,
        };
        match self.transport.send_control(encoded).await {
            Ok(()) => {
                // "any Control Request has the same effect of a Heartbeat"
                // [`docs/spec/02-session-lifecycle.md` §8.4].
                self.liveness.on_outbound(Instant::now());
                true
            }
            Err(error) => {
                self.pending.remove(request_id.as_str());
                tracing::warn!(%error, "control request could not be sent");
                self.emit(SessionEvent::ControlResponse {
                    request_id: Some(request_id),
                    target,
                    outcome: ControlOutcome::NotSent {
                        reason: error.to_string(),
                    },
                })
                .await;
                false
            }
        }
    }

    /// Sends the `heartbeat` pseudo-request that keeps the client's
    /// `LS_inactivity_millis` commitment
    /// [`docs/spec/02-session-lifecycle.md` §8.4].
    /// Sends the `heartbeat` pseudo-request, or gives the connection up.
    ///
    /// A failure here is not merely logged. The outbound deadline is already
    /// past when this runs, so leaving it where it is would make the timer fire
    /// again on the very next turn of the pump — a spin that burns a core and
    /// floods the log without ever satisfying the commitment. The deadline is
    /// therefore advanced whatever happens, which spaces the retries one
    /// interval apart, and after [`MAX_HEARTBEAT_FAILURES`] consecutive
    /// failures the connection is treated as lost: a client that cannot write
    /// to a socket cannot keep the `LS_inactivity_millis` commitment on it
    /// [`docs/spec/02-session-lifecycle.md` §8.4], and the server will drop it
    /// regardless.
    async fn send_heartbeat(&mut self) -> Flow {
        let session = self.session.as_ref().map(|s| s.as_str().to_owned());
        // `LS_session` "is only needed for the purpose 3 above, whereby the
        // client declares that it is still listening to the stream connection
        // for the specified session" — which is the purpose of every heartbeat
        // this driver sends [`docs/spec/02-session-lifecycle.md` §8.4].
        let request = Heartbeat { session };
        let failure = match request.encode_parameters(self.control_kind()) {
            Ok(parameters) => {
                let encoded = EncodedRequest {
                    name: Heartbeat::NAME,
                    path: Heartbeat::PATH,
                    parameters,
                };
                match self.transport.send_control(encoded).await {
                    Ok(()) => None,
                    Err(error) => Some(error.to_string()),
                }
            }
            Err(error) => Some(error.to_string()),
        };

        // Either way the clock moves: a heartbeat that failed is not retried
        // sooner than one that succeeded.
        self.liveness.on_outbound(Instant::now());

        let Some(detail) = failure else {
            self.heartbeat_failures = 0;
            return Flow::Continue;
        };
        self.heartbeat_failures = self.heartbeat_failures.saturating_add(1);
        tracing::warn!(
            detail,
            attempts = self.heartbeat_failures,
            "heartbeat could not be sent"
        );
        if self.heartbeat_failures >= MAX_HEARTBEAT_FAILURES {
            self.heartbeat_failures = 0;
            return Flow::Unbind(UnbindReason::ConnectionFailed {
                detail: format!(
                    "the outbound heartbeat failed {MAX_HEARTBEAT_FAILURES} times in a row: {detail}"
                ),
            });
        }
        Flow::Continue
    }

    /// Publishes an event, applying backpressure when the caller is slow.
    ///
    /// Nothing is dropped and nothing is reordered: the driver waits for room
    /// rather than discarding, because a discarded data notification would
    /// desynchronise the recovery progressive that makes recovery correct
    /// [`docs/spec/02-session-lifecycle.md` §5.2].
    ///
    /// The one thing that can end the wait early is the stop lane. A caller
    /// that has stopped reading is entitled to stall the session — that is the
    /// documented contract — but not to make it unstoppable, so an ordered stop
    /// abandons the undelivered event and marks the driver as unwinding. The
    /// stream it was going to is about to end anyway.
    ///
    /// A closed receiver is not an error either: the caller may legitimately
    /// stop listening, and the driver still has a session to shut down in
    /// order.
    async fn emit(&mut self, event: SessionEvent) {
        let events = &self.events;
        let stop = &mut self.stop;
        let interrupted = tokio::select! {
            biased;
            permit = events.reserve() => match permit {
                Ok(permit) => {
                    permit.send(event);
                    false
                }
                Err(_) => {
                    tracing::debug!("event receiver dropped; events are no longer delivered");
                    false
                }
            },
            () = stopped(stop) => true,
        };
        if interrupted {
            tracing::debug!(
                "a stop was ordered while the event stream was full; the event was not delivered"
            );
            self.stopping = true;
        }
    }
}

/// Sleeps until an instant, or forever when there is none.
async fn sleep_until_option(deadline: Option<Instant>) {
    match deadline {
        Some(instant) => tokio::time::sleep_until(instant).await,
        None => std::future::pending().await,
    }
}

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

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]

    use std::collections::VecDeque;
    use std::sync::Mutex;

    use super::*;
    use crate::protocol::request::ConnectionMode;
    use crate::session::backoff::BackoffPolicy;
    use crate::session::options::{Credentials, SessionOptions};

    // -----------------------------------------------------------------------
    // A transport that replays a script
    // -----------------------------------------------------------------------
    //
    // This is the whole point of `docs/adr/0007-transport-port-shape.md`: the
    // port yields one ordered line stream, so the state machine can be driven
    // through every transition with no socket, no network and no real timer.
    // Several scripts below are the verbatim transcripts of
    // `docs/spec/02-session-lifecycle.md` §10.

    /// One thing the mock does when the driver asks for a line.
    #[derive(Debug, Clone, PartialEq, Eq)]
    enum Step {
        /// Yield a line.
        Line(String),
        /// Yield a transport failure — transition T5.
        Fail(&'static str),
        /// Yield `None`: the stream ended cleanly.
        End,
        /// Never yield anything again. Models a wedged connection, which is
        /// what the keepalive budget exists to detect.
        Silence,
        /// Yield nothing until at least this many control requests have been
        /// sent. Lets a script react to what the driver does.
        AwaitControls(usize),
        /// Yield this line, always immediately ready, until at least this many
        /// control requests have been sent. Models the busy server the pump's
        /// fairness bound exists for: the line stream is never *not* ready, so
        /// a purely biased pump would never look at anything else.
        FloodUntilControls(&'static str, usize),
    }

    fn line(text: &str) -> Step {
        Step::Line(text.to_owned())
    }

    /// Lines split from a transcript block, blank lines and elisions removed.
    fn transcript(text: &str) -> Vec<Step> {
        text.lines()
            .map(str::trim)
            .filter(|l| !l.is_empty() && *l != "[…]")
            .map(line)
            .collect()
    }

    /// What the driver asked the transport to open.
    #[derive(Debug, Clone, PartialEq, Eq)]
    enum OpenRecord {
        Create,
        Bind {
            session: Option<String>,
            recovery_from: Option<u64>,
        },
    }

    #[derive(Debug, Default)]
    struct MockLog {
        opens: Vec<OpenRecord>,
        controls: Vec<EncodedRequest>,
        control_links: Vec<Option<String>>,
        closes: usize,
    }

    impl MockLog {
        fn control_parameters(&self) -> Vec<String> {
            self.controls
                .iter()
                .map(|request| request.parameters.clone())
                .collect()
        }
    }

    struct MockTransport {
        properties: TransportProperties,
        scripts: VecDeque<Vec<Step>>,
        current: VecDeque<Step>,
        /// How many of the next `send_control` calls fail. Models a control
        /// request that never reaches the server.
        control_failures: usize,
        /// Whether `open_stream` never completes. Models a dial that hangs
        /// below the port — in DNS, in TCP, in TLS, or in the handshake.
        hangs_on_open: bool,
        /// How long `open_stream` withholds its completion. Models a polling
        /// server that holds the whole response — the HTTP head and `CONOK`
        /// included — until a notification is ready or the idle time expires
        /// [`docs/spec/02-session-lifecycle.md` §7.2].
        open_delay: Duration,
        log: Arc<Mutex<MockLog>>,
    }

    impl MockTransport {
        fn new(properties: TransportProperties, scripts: Vec<Vec<Step>>) -> Self {
            Self {
                properties,
                scripts: scripts.into(),
                current: VecDeque::new(),
                control_failures: 0,
                hangs_on_open: false,
                open_delay: Duration::ZERO,
                log: Arc::new(Mutex::new(MockLog::default())),
            }
        }

        fn failing_controls(mut self, count: usize) -> Self {
            self.control_failures = count;
            self
        }

        const fn hanging_open(mut self) -> Self {
            self.hangs_on_open = true;
            self
        }

        const fn with_open_delay(mut self, delay: Duration) -> Self {
            self.open_delay = delay;
            self
        }
    }

    impl Transport for MockTransport {
        fn properties(&self) -> TransportProperties {
            self.properties
        }

        fn set_control_link(&mut self, host: Option<&str>) {
            self.log
                .lock()
                .expect("mock log poisoned")
                .control_links
                .push(host.map(str::to_owned));
        }

        async fn open_stream(&mut self, request: StreamOpen) -> Result<(), TransportError> {
            let record = match request {
                StreamOpen::Create(_) => OpenRecord::Create,
                StreamOpen::Bind(bind) => OpenRecord::Bind {
                    session: bind.session.clone(),
                    recovery_from: bind.recovery_from,
                },
            };
            self.log
                .lock()
                .expect("mock log poisoned")
                .opens
                .push(record);
            if self.hangs_on_open {
                return std::future::pending().await;
            }
            if !self.open_delay.is_zero() {
                tokio::time::sleep(self.open_delay).await;
            }
            self.current = self.scripts.pop_front().unwrap_or_default().into();
            Ok(())
        }

        async fn next_line(&mut self) -> Option<Result<String, TransportError>> {
            loop {
                let Some(step) = self.current.front().cloned() else {
                    return std::future::pending().await;
                };
                match step {
                    Step::Line(text) => {
                        self.current.pop_front();
                        return Some(Ok(text));
                    }
                    Step::Fail(reason) => {
                        self.current.pop_front();
                        return Some(Err(TransportError::ConnectionLost {
                            reason: reason.to_owned(),
                        }));
                    }
                    Step::End => {
                        self.current.pop_front();
                        return None;
                    }
                    Step::Silence => return std::future::pending().await,
                    Step::FloodUntilControls(text, wanted) => {
                        let sent = self.log.lock().expect("mock log poisoned").controls.len();
                        if sent >= wanted {
                            self.current.pop_front();
                            continue;
                        }
                        return Some(Ok(text.to_owned()));
                    }
                    Step::AwaitControls(wanted) => {
                        let sent = self.log.lock().expect("mock log poisoned").controls.len();
                        if sent >= wanted {
                            self.current.pop_front();
                            continue;
                        }
                        return std::future::pending().await;
                    }
                }
            }
        }

        async fn send_control(&mut self, request: EncodedRequest) -> Result<(), TransportError> {
            let name = request.name;
            // The attempt is logged either way, so an `AwaitControls` gate
            // still fires for a request that failed to go out.
            self.log
                .lock()
                .expect("mock log poisoned")
                .controls
                .push(request);
            if self.control_failures > 0
                && let Some(remaining) = self.control_failures.checked_sub(1)
            {
                self.control_failures = remaining;
                return Err(TransportError::Send {
                    name,
                    reason: "control connection refused".to_owned(),
                });
            }
            Ok(())
        }

        async fn close(&mut self) -> Result<(), TransportError> {
            self.log.lock().expect("mock log poisoned").closes += 1;
            Ok(())
        }
    }

    // -----------------------------------------------------------------------
    // Harness
    // -----------------------------------------------------------------------

    /// WebSocket-shaped properties: control travels on the stream connection,
    /// the stream has no content-length end, and it does not poll
    /// (`docs/adr/0007-transport-port-shape.md`).
    const fn websocket() -> TransportProperties {
        TransportProperties {
            control_shares_stream: true,
            ends_on_content_length: false,
            is_polling: false,
        }
    }

    /// HTTP-streaming-shaped properties: control needs its own connection and
    /// the stream ends when its content length is reached — transition T3.
    const fn http_streaming() -> TransportProperties {
        TransportProperties {
            control_shares_stream: false,
            ends_on_content_length: true,
            is_polling: false,
        }
    }

    /// HTTP long-polling properties — transition T4.
    const fn http_polling() -> TransportProperties {
        TransportProperties {
            control_shares_stream: false,
            ends_on_content_length: false,
            is_polling: true,
        }
    }

    fn options() -> SessionOptions {
        SessionOptions::default()
            .with_credentials(Credentials {
                user: None,
                password: None,
                adapter_set: Some("WELCOME".to_owned()),
            })
            .with_backoff(BackoffPolicy {
                initial: Duration::from_millis(10),
                max: Duration::from_millis(40),
                max_attempts: NonZeroU32::new(4),
            })
    }

    struct Outcome {
        closed: SessionClosed,
        events: Vec<SessionEvent>,
        log: Arc<Mutex<MockLog>>,
    }

    impl Outcome {
        fn opens(&self) -> Vec<OpenRecord> {
            self.log.lock().expect("mock log poisoned").opens.clone()
        }

        fn controls(&self) -> Vec<String> {
            self.log
                .lock()
                .expect("mock log poisoned")
                .control_parameters()
        }

        fn closes(&self) -> usize {
            self.log.lock().expect("mock log poisoned").closes
        }

        fn bound(&self) -> Vec<&BoundInfo> {
            self.events
                .iter()
                .filter_map(|event| match event {
                    SessionEvent::Bound(info) => Some(info.as_ref()),
                    _ => None,
                })
                .collect()
        }

        fn unbinds(&self) -> Vec<&UnbindReason> {
            self.events
                .iter()
                .filter_map(|event| match event {
                    SessionEvent::Unbound { reason, .. } => Some(reason),
                    _ => None,
                })
                .collect()
        }

        /// Every subscription event, with the progressive that carried it.
        fn data(&self) -> Vec<(u64, SubscriptionKey, &SubscriptionOutcome)> {
            self.events
                .iter()
                .filter_map(|event| match event {
                    SessionEvent::Subscription {
                        progressive,
                        key,
                        outcome,
                    } => Some((*progressive, *key, outcome.as_ref())),
                    _ => None,
                })
                .collect()
        }

        fn recoveries(&self) -> Vec<RecoveryOutcome> {
            self.events
                .iter()
                .filter_map(|event| match event {
                    SessionEvent::Recovered(outcome) => Some(*outcome),
                    _ => None,
                })
                .collect()
        }

        fn resubscribes(&self) -> Vec<&Vec<ResubscribedEntry>> {
            self.events
                .iter()
                .filter_map(|event| match event {
                    SessionEvent::Resubscribed(entries) => Some(entries),
                    _ => None,
                })
                .collect()
        }
    }

    /// Runs a driver over a mock transport to its terminal state, with every
    /// command pre-queued before the first line is read.
    async fn drive(
        properties: TransportProperties,
        options: SessionOptions,
        scripts: Vec<Vec<Step>>,
        commands: Vec<SessionCommand>,
    ) -> Outcome {
        drive_transport(MockTransport::new(properties, scripts), options, commands).await
    }

    /// As [`drive`], but over a transport the test built itself.
    async fn drive_transport(
        transport: MockTransport,
        options: SessionOptions,
        commands: Vec<SessionCommand>,
    ) -> Outcome {
        let log = Arc::clone(&transport.log);
        let (driver, handle, mut events) =
            connect(transport, options).expect("options must match the transport");
        for command in commands {
            handle
                .commands
                .send(command)
                .await
                .expect("the driver has not started yet");
        }
        let closed = driver.run().await;
        drop(handle);

        let mut collected = Vec::new();
        while let Ok(event) = events.try_recv() {
            collected.push(event);
        }
        Outcome {
            closed,
            events: collected,
            log,
        }
    }

    /// The `END` this suite uses to stop a driver whose scenario does not end
    /// on its own: "The session was closed on the Server side, via software or
    /// by the administrator" [`docs/spec/05-error-codes.md` §2, code 32].
    const SERVER_END: &str = "END,32,session closed on the Server side";

    /// The `CONOK` of the session-creation transcript
    /// [`docs/spec/02-session-lifecycle.md` §10, F1].
    const F1_CONOK: &str = "CONOK,S1d7c802482843a26T5626355,50000,5000,*";
    const F1_SESSION: &str = "S1d7c802482843a26T5626355";

    /// The `CONOK` of the rebinding transcripts
    /// [`docs/spec/02-session-lifecycle.md` §10, F6/F7].
    const F6_CONOK: &str = "CONOK,Se939a67a9be2d336T3823582,50000,5000,*";
    const F6_SESSION: &str = "Se939a67a9be2d336T3823582";

    /// The `CONOK` of the recovery transcript
    /// [`docs/spec/02-session-lifecycle.md` §10, F8].
    const F8_CONOK: &str = "CONOK,S22dee113e3f71b1fT4327493,50000,5000,*";
    const F8_SESSION: &str = "S22dee113e3f71b1fT4327493";

    /// The `add` that the transcripts' `SUBOK,1,…` answers.
    ///
    /// The published transcripts omit the control request that created the
    /// subscription; a driver that never received one has nothing to route a
    /// `U` to, so the suite issues it.
    fn subscribe() -> SessionCommand {
        SessionCommand::Subscribe {
            key: SubscriptionKey(1),
            spec: Box::new(spec()),
        }
    }

    /// A transcript with a wait for the `add` inserted after its `CONOK`, so
    /// the subscription is bound to `LS_subId` 1 before the server answers it.
    fn subscribed_transcript(text: &str) -> Vec<Step> {
        let mut steps = transcript(text);
        steps.insert(1, Step::AwaitControls(1));
        steps
    }

    fn spec() -> SubscriptionSpec {
        // The Hands On subscription: `LS_group=item2`, the three-field stock
        // schema, `MERGE` [`docs/spec/02-session-lifecycle.md` §10, F8].
        let mut spec = SubscriptionSpec::new(
            "item2",
            "stock_name time last_price",
            SubscriptionMode::Merge,
        );
        spec.data_adapter = Some("STOCKS".to_owned());
        spec
    }

    // -----------------------------------------------------------------------
    // T1 — initial → Bound, `Session Created`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t1_create_session_binds_and_reports_a_new_session() {
        // The whole of fixture F1, verbatim
        // [`docs/spec/02-session-lifecycle.md` §10, F1].
        let mut script = transcript(
            "
            CONOK,S1d7c802482843a26T5626355,50000,5000,*
            SERVNAME,Lightstreamer HTTP Server
            CLIENTIP,0:0:0:0:0:0:0:1
            NOOP,sending placeholder data
            CONS,unlimited
            PROBE
            PROBE
            PROBE
            ",
        );
        script.push(line(SERVER_END));

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        let bound = outcome.bound();
        assert_eq!(bound.len(), 1);
        let info = bound.first().expect("one bind");
        // T1: "Record session ID, request limit, keep-alive time and control
        // link" [`docs/spec/02-session-lifecycle.md` §2.2, T1].
        assert_eq!(info.session_id.as_str(), F1_SESSION);
        assert_eq!(info.request_limit_bytes, 50_000);
        assert_eq!(info.keep_alive, Duration::from_millis(5000));
        // The literal `*` means "no control link configured" [§3.1].
        assert_eq!(info.control_link, None);
        assert_eq!(info.kind, BindKind::Created);
        assert_eq!(outcome.opens(), vec![OpenRecord::Create]);
        // The transport is closed on the way out, always.
        assert_eq!(outcome.closes(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn test_t1_head_notifications_are_surfaced_but_not_counted() {
        // `SERVNAME`, `CLIENTIP` and `CONS` are session-related, not data
        // notifications [`docs/spec/02-session-lifecycle.md` §5.3]; `NOOP` and
        // `PROBE` carry nothing at all.
        let mut script = transcript(
            "
            CONOK,S1d7c802482843a26T5626355,50000,5000,*
            SERVNAME,Lightstreamer HTTP Server
            CLIENTIP,0:0:0:0:0:0:0:1
            NOOP,sending placeholder data
            CONS,unlimited
            PROBE
            ",
        );
        script.push(line(SERVER_END));

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        let infos = outcome
            .events
            .iter()
            .filter(|event| matches!(event, SessionEvent::ServerInfo(_)))
            .count();
        assert_eq!(infos, 3, "SERVNAME, CLIENTIP and CONS reach the caller");
        assert!(
            outcome.data().is_empty(),
            "none of them is a data notification"
        );
    }

    // -----------------------------------------------------------------------
    // T2 — Bound → Unbound, `Force-Unbound by Client`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t2_force_rebind_unbinds_and_rebinds_the_same_session() {
        let first = vec![
            line(F6_CONOK),
            // Wait for the `force_rebind` control request, then answer as T2
            // says the server does: "If successful, a `LOOP` notification is
            // sent on the stream connection"
            // [`docs/spec/02-session-lifecycle.md` §2.2, T2].
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("LOOP,0"),
            Step::End,
        ];
        let second = vec![line(F6_CONOK), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![SessionCommand::ForceRebind {
                close_socket: Some(true),
            }],
        )
        .await;

        assert_eq!(
            outcome.unbinds(),
            vec![&UnbindReason::ForcedByClient {
                expected_delay: Duration::ZERO
            }]
        );
        let controls = outcome.controls();
        assert!(
            controls
                .iter()
                .any(|parameters| parameters.contains("LS_op=force_rebind")
                    && parameters.contains("LS_close_socket=true")),
            "{controls:?}"
        );
        // T6: the rebind is a plain `bind_session`, with no recovery, because
        // a `LOOP` is a clean unbind [§4.4].
        assert_eq!(
            outcome.opens().get(1),
            Some(&OpenRecord::Bind {
                session: Some(F6_SESSION.to_owned()),
                recovery_from: None,
            })
        );
    }

    // -----------------------------------------------------------------------
    // T3 — Bound → Unbound, `Content-Length Reached`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t3_content_length_reached_rebinds_on_a_new_connection() {
        // The tail of F6 and the whole of F7, verbatim
        // [`docs/spec/02-session-lifecycle.md` §10, F6, F7].
        let mut first = transcript(
            "
            CONOK,Se939a67a9be2d336T3823582,50000,5000,*
            SUBOK,1,1,3
            U,1,1,stock|15:57:51|16.27
            U,1,1,||16.24
            U,1,1,||16.11
            U,1,1,|15:57:52|15.99
            U,1,1,||15.93
            LOOP,0
            ",
        );
        first.insert(1, Step::AwaitControls(1));
        first.push(Step::End);

        let mut second = transcript(
            "
            CONOK,Se939a67a9be2d336T3823582,50000,5000,*
            NOOP,sending placeholder data
            CONS,unlimited
            U,1,1,|15:57:53|17.7
            U,1,1,||17.81
            ",
        );
        second.push(line(SERVER_END));

        let outcome = drive(
            http_streaming(),
            options(),
            vec![first, second],
            vec![subscribe()],
        )
        .await;

        assert_eq!(
            outcome.unbinds(),
            vec![&UnbindReason::ContentLengthReached {
                expected_delay: Duration::ZERO
            }]
        );
        // F7's three test-relevant observations: the session id is identical,
        // the rebind carries no recovery, and nothing is re-subscribed.
        let bound = outcome.bound();
        assert_eq!(bound.len(), 2);
        assert_eq!(
            bound.first().map(|info| info.session_id.clone()),
            bound.get(1).map(|info| info.session_id.clone())
        );
        assert_eq!(
            bound.get(1).map(|info| info.kind.clone()),
            Some(BindKind::Rebound)
        );
        assert!(outcome.resubscribes().is_empty());
        // The progressive keeps counting across the rebind: `SUBOK` and five
        // `U` before, two `U` after.
        assert_eq!(
            outcome
                .data()
                .iter()
                .map(|(p, _, _)| *p)
                .collect::<Vec<_>>(),
            vec![1, 2, 3, 4, 5, 6, 7, 8]
        );
    }

    // -----------------------------------------------------------------------
    // T4 — Bound → Unbound, `Poll Cycle Expired`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t4_poll_cycle_expired_rebinds_after_the_expected_delay() {
        // "A value greater than 0 is actually used only on polling sessions"
        // [`docs/spec/02-session-lifecycle.md` §4.2].
        let first = vec![line(F1_CONOK), line("LOOP,2000"), Step::End];
        let second = vec![line(F1_CONOK), line(SERVER_END)];

        let polling = options().with_connection(ConnectionMode::Polling {
            polling_millis: 2000,
            idle_millis: Some(10_000),
        });
        let outcome = drive(http_polling(), polling, vec![first, second], Vec::new()).await;

        assert_eq!(
            outcome.unbinds(),
            vec![&UnbindReason::PollCycleExpired {
                expected_delay: Duration::from_millis(2000)
            }]
        );
        // The delay `LOOP` asked for is the delay the driver waits, not a
        // backoff [§4.2].
        let retry_in = outcome.events.iter().find_map(|event| match event {
            SessionEvent::Unbound { retry_in, .. } => *retry_in,
            _ => None,
        });
        assert_eq!(retry_in, Some(Duration::from_millis(2000)));
    }

    #[tokio::test(start_paused = true)]
    async fn test_t4_polling_stream_ending_without_a_loop_is_still_a_poll_cycle() {
        // SPEC-AMBIGUITY exercised: §7.1 describes the cycle without naming
        // `LOOP`, so a clean end on a polling transport is read as T4.
        let first = vec![line(F1_CONOK), Step::End];
        let second = vec![line(F1_CONOK), line(SERVER_END)];

        let polling = options().with_connection(ConnectionMode::Polling {
            polling_millis: 1000,
            idle_millis: None,
        });
        let outcome = drive(http_polling(), polling, vec![first, second], Vec::new()).await;

        assert_eq!(
            outcome.unbinds(),
            vec![&UnbindReason::PollCycleExpired {
                expected_delay: Duration::ZERO
            }]
        );
        // A poll cycle is not a failure, so the rebind carries no recovery.
        assert_eq!(
            outcome.opens().get(1),
            Some(&OpenRecord::Bind {
                session: Some(F1_SESSION.to_owned()),
                recovery_from: None,
            })
        );
    }

    /// Regression: a polling connection whose response is withheld for the idle
    /// time — longer than the bare open timeout — must still bind. The
    /// establishment budget on a polling transport is the open timeout **plus**
    /// the idle time [`SessionDriver::establishment_budget`;
    /// `docs/spec/02-session-lifecycle.md` §7.2], so a quiet poll cycle is not
    /// aborted before its `CONOK` can arrive.
    ///
    /// Before the fix this looped forever with `ConnectionFailed { "…not
    /// established within 10s" }` and delivered nothing, which the hermetic T4
    /// test above could not catch because it modelled no server-side delay.
    #[tokio::test(start_paused = true)]
    async fn test_polling_establishment_budget_covers_a_withheld_response() {
        // Idle 15s, open timeout the default 10s: a bare-open-timeout budget
        // would kill the connection at 10s. The server withholds every response
        // for 13s (10s < 13s < 10s + 15s), so it binds only if the idle time is
        // in the budget. The `CONOK` reports the negotiated idle as 15000 ms.
        let conok = "CONOK,S1,50000,15000,*";
        let first = vec![line(conok), Step::End];
        let second = vec![line(conok), line(SERVER_END)];

        let polling = options().with_connection(ConnectionMode::Polling {
            polling_millis: 5000,
            idle_millis: Some(15_000),
        });
        assert_eq!(
            polling.open_timeout,
            Duration::from_secs(10),
            "the fixture relies on the default 10s open timeout"
        );

        let transport = MockTransport::new(http_polling(), vec![first, second])
            .with_open_delay(Duration::from_secs(13));
        let outcome = drive_transport(transport, polling, Vec::new()).await;

        // It bound on the create poll and again on the rebind poll — the
        // withheld response was waited out both times, not aborted.
        assert_eq!(outcome.bound().len(), 2, "both poll cycles bound");
        assert!(
            !outcome
                .unbinds()
                .iter()
                .any(|reason| matches!(reason, UnbindReason::ConnectionFailed { .. })),
            "no cycle was aborted as a failed establishment: {:?}",
            outcome.unbinds()
        );
        // The only unbind between the two binds is the poll cycle itself.
        assert_eq!(
            outcome.unbinds(),
            vec![&UnbindReason::PollCycleExpired {
                expected_delay: Duration::ZERO
            }]
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_t3_and_t4_are_told_apart_by_declared_properties_only() {
        // The same `LOOP,0` on two transports whose only difference is a
        // declared property (`docs/adr/0007-transport-port-shape.md`).
        let script = || {
            vec![
                vec![line(F1_CONOK), line("LOOP,0"), Step::End],
                vec![line(F1_CONOK), line(SERVER_END)],
            ]
        };

        let a = drive(http_streaming(), options(), script(), Vec::new()).await;
        assert!(matches!(
            a.unbinds().first(),
            Some(UnbindReason::ContentLengthReached { .. })
        ));

        let b = drive(websocket(), options(), script(), Vec::new()).await;
        assert!(matches!(
            b.unbinds().first(),
            Some(UnbindReason::Looped { .. })
        ));
    }

    // -----------------------------------------------------------------------
    // T5 — Bound → Unbound, `Connection Failed`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t5_connection_failed_recovers_from_the_counted_progressive() {
        // The stream of F8 up to the interruption, verbatim, whose data
        // notifications the spec counts as 15
        // [`docs/spec/02-session-lifecycle.md` §10, F8].
        let mut first = subscribed_transcript(F8_STREAM);
        first.push(Step::Fail("connection reset"));

        let second = vec![line(F8_CONOK), line("PROG,15"), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![subscribe()],
        )
        .await;

        assert!(matches!(
            outcome.unbinds().first(),
            Some(UnbindReason::ConnectionFailed { .. })
        ));
        // T5's client action: "Attempt **recovery** (`bind_session` +
        // `LS_recovery_from`), not a plain rebind"
        // [`docs/spec/02-session-lifecycle.md` §2.2, T5].
        assert_eq!(
            outcome.opens().get(1),
            Some(&OpenRecord::Bind {
                session: Some(F8_SESSION.to_owned()),
                recovery_from: Some(15),
            })
        );
        assert_eq!(
            outcome.recoveries(),
            vec![RecoveryOutcome {
                requested: 15,
                resumed_at: 15,
                kind: RecoveryKind::Exact,
            }]
        );
        assert_eq!(
            outcome.bound().get(1).map(|info| info.kind.clone()),
            Some(BindKind::Recovering {
                requested_progressive: 15
            })
        );
    }

    /// The stream of F8 up to the manual interruption, verbatim
    /// [`docs/spec/02-session-lifecycle.md` §10, F8, p.77].
    const F8_STREAM: &str = "
        CONOK,S22dee113e3f71b1fT4327493,50000,5000,*
        SERVNAME,Lightstreamer HTTP Server
        CLIENTIP,0:0:0:0:0:0:0:1
        NOOP,sending placeholder data
        CONS,unlimited
        PROBE
        SUBOK,1,1,3
        CONF,1,unlimited,filtered
        PROBE
        U,1,1,Ations Europe|15:55:08|14.81
        U,1,1,||14.66
        U,1,1,|15:55:09|14.62
        U,1,1,||14.71
        U,1,1,|15:55:10|14.63
        U,1,1,||14.77
        U,1,1,|15:55:11|
        U,1,1,||14.61
        U,1,1,|15:55:12|14.5
        U,1,1,|15:55:13|14.64
        U,1,1,|15:55:14|
        SYNC,25
        U,1,1,||14.74
        U,1,1,||14.66
        ";

    #[tokio::test(start_paused = true)]
    async fn test_data_notification_count_matches_the_spec_worked_example() {
        // "The count should start with the `SUBOK` notification and include the
        // `CONF` and all the `U` notifications. In our example, the count
        // yields 15." [`docs/spec/02-session-lifecycle.md` §5.3, §10 F8].
        let mut script = subscribed_transcript(F8_STREAM);
        script.push(line(SERVER_END));

        let outcome = drive(websocket(), options(), vec![script], vec![subscribe()]).await;

        let data = outcome.data();
        assert_eq!(data.len(), 15, "1 SUBOK + 1 CONF + 13 U");
        assert_eq!(data.last().map(|(p, _, _)| *p), Some(15));
        assert!(
            data.iter().all(|(_, _, outcome)| outcome.is_ok()),
            "PROBE and SYNC are not data notifications, and every counted line decoded"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_recovery_resuming_earlier_discards_the_duplicates() {
        // The recovery response of F8, verbatim: the server answers `PROG,11`
        // to a request that asked for a later point, "in order to simulate
        // notifications already sent by the Server but not received. As a
        // consequence, a few notifications will be duplicated."
        // [`docs/spec/02-session-lifecycle.md` §10, F8].
        let mut first = subscribed_transcript(F8_STREAM);
        first.push(Step::Fail("connection reset"));

        let mut second = transcript(
            "
            CONOK,S22dee113e3f71b1fT4327493,50000,5000,*
            NOOP,sending placeholder data
            CONS,unlimited
            PROG,11
            U,1,1,|15:55:13|14.64
            U,1,1,|15:55:14|
            U,1,1,||14.74
            U,1,1,||14.66
            U,1,1,|15:55:15|17.7
            U,1,1,||17.81
            SYNC,5
            ",
        );
        second.push(line(SERVER_END));

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![subscribe()],
        )
        .await;

        assert_eq!(
            outcome.recoveries(),
            vec![RecoveryOutcome {
                requested: 15,
                resumed_at: 11,
                kind: RecoveryKind::Duplicated { count: 4 },
            }]
        );
        // Precondition 4: the four duplicates are discarded before the caller
        // sees them, and counting resumes at 16
        // [`docs/spec/02-session-lifecycle.md` §5.2].
        let progressives: Vec<u64> = outcome.data().iter().map(|(p, _, _)| *p).collect();
        assert_eq!(progressives.len(), 15 + 2);
        assert_eq!(progressives.get(15), Some(&16));
        assert_eq!(progressives.last(), Some(&17));
    }

    #[tokio::test(start_paused = true)]
    async fn test_recovery_resuming_later_is_reported_as_a_gap() {
        // SPEC-AMBIGUITY A7: `PROG` higher than requested is undefined. It is
        // reported rather than hidden, because telling an application that
        // continuity held when it did not is worse than saying nothing
        // (`docs/adr/0005-recovery-is-visible-in-the-event-stream.md`).
        let first = vec![
            line(F8_CONOK),
            Step::AwaitControls(1),
            line("SUBOK,1,1,3"),
            line("U,1,1,a|b|c"),
            Step::Fail("connection reset"),
        ];
        let second = vec![
            line(F8_CONOK),
            line("PROG,9"),
            line("U,1,1,d|e|f"),
            line(SERVER_END),
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![subscribe()],
        )
        .await;

        assert_eq!(
            outcome.recoveries(),
            vec![RecoveryOutcome {
                requested: 2,
                resumed_at: 9,
                kind: RecoveryKind::Gap { missing: 7 },
            }]
        );
        // The counter follows the server, otherwise every later recovery would
        // ask from the wrong point.
        assert_eq!(outcome.data().last().map(|(p, _, _)| *p), Some(10));
    }

    // -----------------------------------------------------------------------
    // T6 — Unbound → Bound, `Session Rebound`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t6_rebind_preserves_the_session_and_its_subscriptions() {
        // "all subscriptions are preserved, with all their items and fields"
        // [`docs/spec/02-session-lifecycle.md` §4.4], which is why the client
        // must **not** re-subscribe after a rebind.
        let first = vec![
            line(F6_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            line("LOOP,0"),
            Step::End,
        ];
        let second = vec![line(F6_CONOK), line("U,1,1,x|y|z"), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![SessionCommand::Subscribe {
                key: SubscriptionKey(1),
                spec: Box::new(spec()),
            }],
        )
        .await;

        assert_eq!(
            outcome.bound().get(1).map(|info| info.kind.clone()),
            Some(BindKind::Rebound)
        );
        let adds = outcome
            .controls()
            .iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .count();
        assert_eq!(adds, 1, "the subscription must not be issued twice");
        assert!(outcome.resubscribes().is_empty());
    }

    // -----------------------------------------------------------------------
    // T7 / T8 — `Destroyed by Client`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t7_destroy_while_bound_ends_with_the_servers_cause() {
        // Fixture F5: `REQOK,3` then `END,31,Destroy invoked by client`
        // [`docs/spec/02-session-lifecycle.md` §10, F5].
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("END,31,Destroy invoked by client"),
            Step::End,
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::Destroy { cause: None }],
        )
        .await;

        assert_eq!(
            outcome.closed,
            SessionClosed::ByClient {
                destroy_confirmed: true,
                cause: Some(ServerCause {
                    code: 31,
                    message: "Destroy invoked by client".to_owned(),
                }),
            }
        );
        assert!(
            outcome
                .controls()
                .iter()
                .any(|parameters| parameters.contains("LS_op=destroy")),
            "{:?}",
            outcome.controls()
        );
        assert_eq!(outcome.closes(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn test_t8_destroy_while_unbound_ends_without_waiting_for_an_end() {
        // SPEC-AMBIGUITY A1: with nothing bound there is nowhere for the `END`
        // of T7 to be delivered, and the spec does not say whether it is
        // dropped, buffered or suppressed. The driver does not wait for one.
        let first = vec![line(F1_CONOK), line("LOOP,60000"), Step::End];

        let outcome = drive(
            websocket(),
            options(),
            vec![first],
            vec![SessionCommand::Destroy { cause: None }],
        )
        .await;

        assert_eq!(
            outcome.closed,
            SessionClosed::ByClient {
                destroy_confirmed: false,
                cause: None,
            }
        );
        // Exactly one stream connection was ever opened: the destroy landed
        // during the rebind delay.
        assert_eq!(outcome.opens(), vec![OpenRecord::Create]);
    }

    // -----------------------------------------------------------------------
    // T9 — Unbound → final, `Timed Out`
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_t9_timed_out_session_is_recreated_and_subscriptions_re_executed() {
        // T9: the unbound session is discarded server-side, "a later
        // `bind_session` then fails with `CONERR,20`", and the client must
        // "`create_session` and re-execute all subscriptions"
        // [`docs/spec/02-session-lifecycle.md` §2.2, T9; §6.1].
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(2),
            line("REQOK,1"),
            line("REQOK,2"),
            line("SUBOK,1,1,3"),
            line("SUBOK,2,1,3"),
            Step::Fail("connection reset"),
        ];
        let second = vec![line("CONERR,20,Specified session not found"), Step::End];
        let third = vec![line(F6_CONOK), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second, third],
            vec![
                SessionCommand::Subscribe {
                    key: SubscriptionKey(1),
                    spec: Box::new(spec()),
                },
                SessionCommand::Subscribe {
                    key: SubscriptionKey(2),
                    spec: Box::new(SubscriptionSpec::new(
                        "item1",
                        "last_price",
                        SubscriptionMode::Merge,
                    )),
                },
            ],
        )
        .await;

        // The failed bind is retryable, so it produced an unbind, not a close.
        assert!(matches!(
            outcome.unbinds().get(1),
            Some(UnbindReason::Rejected {
                cause: ServerCause { code: 20, .. }
            })
        ));
        // A brand-new session: continuity is gone and the caller is told so.
        assert_eq!(
            outcome.bound().get(1).map(|info| info.kind.clone()),
            Some(BindKind::Recreated {
                previous: Some(SessionId::new(F1_SESSION)),
            })
        );
        // Losing a subscription silently is the defect this asserts against.
        let resubscribed = outcome.resubscribes();
        let entries = resubscribed.first().expect("a resubscribe batch");
        assert_eq!(entries.len(), 2);
        assert_eq!(
            entries.iter().map(|e| e.key).collect::<Vec<_>>(),
            vec![SubscriptionKey(1), SubscriptionKey(2)]
        );
        // `LS_subId` restarts at 1 in the new session
        // [`docs/spec/02-session-lifecycle.md` §9.5], while the caller's keys
        // do not change.
        assert_eq!(
            entries
                .iter()
                .map(|e| e.subscription_id.get())
                .collect::<Vec<_>>(),
            vec![1, 2]
        );
        assert!(entries.iter().all(|e| e.previously_active));
        // Four `add` requests in total: two per session.
        let adds = outcome
            .controls()
            .iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .count();
        assert_eq!(adds, 4);
    }

    // -----------------------------------------------------------------------
    // Server-initiated END on a bound session (§2.2 note, ambiguity A2)
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_server_initiated_end_on_a_bound_session_is_definitive_loss() {
        // Code 35: "the Metadata Adapter… requested the closure of the current
        // session upon opening of a new session for the same user"
        // [`docs/spec/02-session-lifecycle.md` §6.2]. Reconnecting would fight
        // the other client, so it is fatal.
        let script = vec![
            line(F1_CONOK),
            line("END,35,Another session was opened for this user"),
            Step::End,
        ];

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        assert_eq!(
            outcome.closed,
            SessionClosed::ByServer {
                cause: ServerCause {
                    code: 35,
                    message: "Another session was opened for this user".to_owned(),
                },
            }
        );
        assert_eq!(outcome.opens().len(), 1, "no retry after a fatal cause");
        assert!(matches!(
            outcome.events.last(),
            Some(SessionEvent::Closed(SessionClosed::ByServer { .. }))
        ));
    }

    #[tokio::test(start_paused = true)]
    async fn test_server_end_code_48_recreates_the_session_immediately() {
        // "the client should recover by opening a new session immediately"
        // [`docs/spec/02-session-lifecycle.md` §6.2, code 48].
        let first = vec![
            line(F1_CONOK),
            line("END,48,Maximum session duration reached"),
            Step::End,
        ];
        let second = vec![line(F6_CONOK), line(SERVER_END)];

        let outcome = drive(websocket(), options(), vec![first, second], Vec::new()).await;

        assert!(matches!(
            outcome.unbinds().first(),
            Some(UnbindReason::ServerRefresh {
                cause: ServerCause { code: 48, .. }
            })
        ));
        // Immediately: no backoff delay at all.
        let retry_in = outcome.events.iter().find_map(|event| match event {
            SessionEvent::Unbound { retry_in, .. } => *retry_in,
            _ => None,
        });
        assert_eq!(retry_in, Some(Duration::ZERO));
        assert_eq!(outcome.opens().get(1), Some(&OpenRecord::Create));
    }

    // -----------------------------------------------------------------------
    // CONERR: definitive loss versus permitted retry (§6.2)
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_conerr_authentication_failure_is_definitive_loss() {
        // Code 1, "User/password check failed" — retrying cannot help
        // [`docs/spec/05-error-codes.md` §2].
        let script = vec![line("CONERR,1,User/password check failed"), Step::End];

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        assert_eq!(
            outcome.closed,
            SessionClosed::ByServer {
                cause: ServerCause {
                    code: 1,
                    message: "User/password check failed".to_owned(),
                },
            }
        );
        assert_eq!(outcome.opens().len(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn test_conerr_retry_later_code_is_retried_and_succeeds() {
        // Codes 5 and 6 both end with "retry later"
        // [`docs/spec/05-error-codes.md` §5.5].
        let first = vec![
            line("CONERR,5,The Server is temporarily overloaded: retry later"),
            Step::End,
        ];
        let second = vec![line(F1_CONOK), line(SERVER_END)];

        let outcome = drive(websocket(), options(), vec![first, second], Vec::new()).await;

        assert!(matches!(
            outcome.unbinds().first(),
            Some(UnbindReason::Rejected {
                cause: ServerCause { code: 5, .. }
            })
        ));
        assert_eq!(outcome.bound().len(), 1);
        assert_eq!(outcome.opens().len(), 2);
    }

    #[tokio::test(start_paused = true)]
    async fn test_conerr_cluster_affinity_code_is_treated_as_permanent() {
        // SPEC-AMBIGUITY: code 21 matches the description of the permanent
        // clustering failure, but `docs/spec/05-error-codes.md` §5.3 warns
        // "the document never states the linkage. Do not assume it."
        // Retrying a permanent misconfiguration is a reconnect storm, so the
        // defensive reading stops and hands the caller the code.
        let script = vec![
            line("CONERR,21,Session ID not compatible with this Server instance"),
            Step::End,
        ];

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        assert!(matches!(
            outcome.closed,
            SessionClosed::ByServer {
                cause: ServerCause { code: 21, .. }
            }
        ));
    }

    #[test]
    fn test_classify_covers_every_code_the_spec_prescribes_an_action_for() {
        // The only codes for which the spec states a client action
        // [`docs/spec/05-error-codes.md` §5].
        assert_eq!(classify(4), Recovery::RecreateSession);
        assert_eq!(classify(20), Recovery::RecreateSession);
        assert_eq!(classify(48), Recovery::RecreateSession);
        assert_eq!(classify(5), Recovery::RetryLater);
        assert_eq!(classify(6), Recovery::RetryLater);
        assert_eq!(classify(10), Recovery::RetryLater);
        // Fatal by policy, each documented at its arm.
        assert_eq!(classify(1), Recovery::Fatal);
        assert_eq!(classify(21), Recovery::Fatal);
        assert_eq!(classify(31), Recovery::Fatal);
        // An unknown positive code, and an adapter-supplied one, are not
        // retried: the spec says nothing about either.
        assert_eq!(classify(9999), Recovery::Fatal);
        assert_eq!(classify(0), Recovery::Fatal);
        assert_eq!(classify(-7), Recovery::Fatal);
    }

    // -----------------------------------------------------------------------
    // Liveness (§8)
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_keepalive_expiry_forces_a_rebind_on_a_wedged_connection() {
        // The failure mode this client exists to prevent: a connection that
        // never errors and never delivers. `CONOK` negotiates 5000 ms and the
        // configured slack is 3000 ms [`docs/spec/02-session-lifecycle.md`
        // §8.1]. Time is paused, so this test advances the virtual clock only.
        let first = vec![line(F1_CONOK), Step::Silence];
        let second = vec![line(F1_CONOK), line(SERVER_END)];

        let outcome = drive(websocket(), options(), vec![first, second], Vec::new()).await;

        assert_eq!(
            outcome.unbinds(),
            vec![&UnbindReason::KeepaliveExpired {
                budget: Duration::from_millis(8000)
            }]
        );
        // §5.1: "if a connection becomes mute, the client can issue a recovery
        // request" — so it is a recovery, not a plain rebind.
        assert_eq!(
            outcome.opens().get(1),
            Some(&OpenRecord::Bind {
                session: Some(F1_SESSION.to_owned()),
                recovery_from: Some(0),
            })
        );
        // §8.1: the wedged connection is "closed and reopened", not merely
        // abandoned — once for the stall, once on the way out.
        assert_eq!(outcome.closes(), 2);
    }

    #[tokio::test(start_paused = true)]
    async fn test_open_timeout_abandons_an_unanswered_handshake() {
        // Nothing at all arrives, not even a `CONOK`. The keepalive budget
        // cannot apply because it has not been negotiated yet, so the client's
        // own open timeout is what ends the attempt.
        let first = vec![Step::Silence];
        let second = vec![line(F1_CONOK), line(SERVER_END)];

        let outcome = drive(websocket(), options(), vec![first, second], Vec::new()).await;

        assert!(matches!(
            outcome.unbinds().first(),
            Some(UnbindReason::ConnectionFailed { .. })
        ));
        assert_eq!(outcome.bound().len(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn test_reverse_heartbeat_is_sent_before_the_inactivity_commitment_expires() {
        // "If no request is needed, a heartbeat pseudo-request can be sent. If
        // no request is received for more than this time, the Server can assume
        // that the client is stuck" [`docs/spec/02-session-lifecycle.md` §8.4].
        // The commitment is 8000 ms, so a heartbeat is due at 4000 ms — before
        // the 8000 ms keepalive budget ends the connection.
        let first = vec![line(F1_CONOK), Step::Silence];
        let second = vec![line(F1_CONOK), line(SERVER_END)];

        let committed = options().with_connection(ConnectionMode::Streaming {
            inactivity_millis: Some(8000),
            keepalive_millis: None,
            send_sync: None,
        });
        let outcome = drive(websocket(), committed, vec![first, second], Vec::new()).await;

        let controls = outcome.controls();
        assert!(
            controls
                .iter()
                .any(|parameters| parameters.contains("LS_session=")),
            "the heartbeat declares the session it is listening to: {controls:?}"
        );
        assert!(!controls.is_empty(), "a heartbeat must have been sent");
        // And the stall still fires afterwards, because a heartbeat is an
        // outbound obligation and says nothing about the inbound one.
        assert!(matches!(
            outcome.unbinds().first(),
            Some(UnbindReason::KeepaliveExpired { .. })
        ));
    }

    #[tokio::test(start_paused = true)]
    async fn test_a_quiet_but_probing_connection_is_never_declared_stalled() {
        // `PROBE` is what the server sends "when no other activity has been
        // sent on the stream connection" [§8.1]; receiving them means the
        // connection is healthy however little data flows.
        let mut script = vec![line(F1_CONOK)];
        for _ in 0..20 {
            script.push(line("PROBE"));
        }
        script.push(line(SERVER_END));

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        assert!(outcome.unbinds().is_empty());
        assert!(matches!(outcome.closed, SessionClosed::ByServer { .. }));
    }

    // -----------------------------------------------------------------------
    // Control-response correlation (§4.5, ADR-0007)
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_control_responses_are_correlated_when_interleaved_with_data() {
        // "responses may always arrive interspersed with notifications, when
        // control requests are sent on the stream connection"
        // [`docs/spec/02-session-lifecycle.md` §4.5]. The request id is the
        // only thing that correlates them
        // (`docs/adr/0007-transport-port-shape.md`).
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(2),
            line("U,1,1,a|b|c"),
            // Out of order on purpose: "responses may arrive out of order"
            // [`docs/spec/02-session-lifecycle.md` §11.3].
            line("REQOK,2"),
            line("U,1,1,d|e|f"),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            line(SERVER_END),
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![
                SessionCommand::Subscribe {
                    key: SubscriptionKey(1),
                    spec: Box::new(spec()),
                },
                SessionCommand::Subscribe {
                    key: SubscriptionKey(2),
                    spec: Box::new(SubscriptionSpec::new(
                        "item1",
                        "last_price",
                        SubscriptionMode::Merge,
                    )),
                },
            ],
        )
        .await;

        let responses: Vec<String> = outcome
            .events
            .iter()
            .filter_map(|event| match event {
                SessionEvent::ControlResponse {
                    request_id: Some(id),
                    outcome: ControlOutcome::Accepted,
                    ..
                } => Some(id.as_str().to_owned()),
                _ => None,
            })
            .collect();
        assert_eq!(responses, vec!["2".to_owned(), "1".to_owned()]);

        // The `LS_reqId` on the wire is what the response echoed, and the two
        // subscriptions carry different ids.
        let controls = outcome.controls();
        assert_eq!(
            controls
                .iter()
                .filter(|parameters| parameters.contains("LS_reqId=1"))
                .count(),
            1,
            "{controls:?}"
        );
        assert_eq!(
            controls
                .iter()
                .filter(|parameters| parameters.contains("LS_reqId=2"))
                .count(),
            1,
            "{controls:?}"
        );
        // And the data around them still reached the caller, in order.
        assert_eq!(outcome.data().len(), 3);
    }

    #[tokio::test(start_paused = true)]
    async fn test_request_ids_are_never_reused_across_sessions() {
        // SPEC-AMBIGUITY A5: "unique within the connection", where
        // "connection" is undefined. A single monotonic sequence for the life
        // of the driver satisfies every reading, and makes the late responses
        // of §4.5 impossible to misattribute.
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            Step::Fail("connection reset"),
        ];
        let second = vec![line("CONERR,20,Specified session not found"), Step::End];
        let third = vec![line(F6_CONOK), Step::AwaitControls(2), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second, third],
            vec![SessionCommand::Subscribe {
                key: SubscriptionKey(1),
                spec: Box::new(spec()),
            }],
        )
        .await;

        let controls = outcome.controls();
        assert_eq!(controls.len(), 2);
        assert_eq!(
            controls
                .iter()
                .filter(|parameters| parameters.contains("LS_reqId=1"))
                .count(),
            1,
            "{controls:?}"
        );
        assert_eq!(
            controls
                .iter()
                .filter(|parameters| parameters.contains("LS_reqId=2"))
                .count(),
            1,
            "{controls:?}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_rejected_subscription_is_reported_and_not_re_issued() {
        // A subscription the server refuses must not be retried on every future
        // session — but the caller must be told, because losing one silently is
        // the defect this guards against.
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQERR,1,23,Bad Field schema name"),
            Step::Fail("connection reset"),
        ];
        let second = vec![line("CONERR,20,Specified session not found"), Step::End];
        let third = vec![line(F6_CONOK), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second, third],
            vec![SessionCommand::Subscribe {
                key: SubscriptionKey(1),
                spec: Box::new(spec()),
            }],
        )
        .await;

        let rejected = outcome.events.iter().any(|event| {
            matches!(
                event,
                SessionEvent::ControlResponse {
                    outcome: ControlOutcome::Rejected {
                        cause: ServerCause { code: 23, .. }
                    },
                    ..
                }
            )
        });
        assert!(rejected, "the refusal must reach the caller");
        let adds = outcome
            .controls()
            .iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .count();
        assert_eq!(adds, 1, "a refused subscription is not re-issued");
    }

    #[tokio::test(start_paused = true)]
    async fn test_uncorrelatable_error_is_surfaced_without_a_request_id() {
        // `ERROR` "carries no request ID, because the server could not parse
        // one" [`docs/spec/03-requests.md` §13.2].
        let script = vec![
            line(F1_CONOK),
            line("ERROR,67,Malformed request"),
            line(SERVER_END),
        ];

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        assert!(outcome.events.iter().any(|event| matches!(
            event,
            SessionEvent::ControlResponse {
                request_id: None,
                target: ControlTarget::Session,
                outcome: ControlOutcome::Rejected {
                    cause: ServerCause { code: 67, .. }
                },
            }
        )));
    }

    // -----------------------------------------------------------------------
    // Routing a control failure back to the subscription it concerns
    // -----------------------------------------------------------------------

    fn control_targets(outcome: &Outcome) -> Vec<(ControlTarget, ControlOutcome)> {
        outcome
            .events
            .iter()
            .filter_map(|event| match event {
                SessionEvent::ControlResponse {
                    target, outcome, ..
                } => Some((target.clone(), outcome.clone())),
                _ => None,
            })
            .collect()
    }

    #[tokio::test(start_paused = true)]
    async fn test_rejected_subscription_names_the_subscription_it_killed() {
        // A rejection the caller cannot route is a subscription lost in
        // silence: the caller's stream simply goes quiet forever. The response
        // therefore names the key **and** the operation, because "add refused"
        // means the subscription does not exist while "delete refused" means it
        // still does.
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQERR,1,23,Bad Field schema name"),
            line(SERVER_END),
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::Subscribe {
                key: SubscriptionKey(7),
                spec: Box::new(spec()),
            }],
        )
        .await;

        assert_eq!(
            control_targets(&outcome),
            vec![(
                ControlTarget::Subscription {
                    key: SubscriptionKey(7),
                    operation: SubscriptionOperation::Subscribe,
                },
                ControlOutcome::Rejected {
                    cause: ServerCause {
                        code: 23,
                        message: "Bad Field schema name".to_owned(),
                    },
                },
            )]
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_rejected_unsubscribe_names_the_subscription_that_is_still_alive() {
        // The delete failed, so the subscription is still there and still
        // delivering. A caller told only "rejected" would have the state
        // exactly backwards.
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            Step::AwaitControls(2),
            line("REQERR,2,19,Specified subscription not found"),
            line("U,1,1,still|coming|through"),
            line(SERVER_END),
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![
                SessionCommand::Subscribe {
                    key: SubscriptionKey(1),
                    spec: Box::new(spec()),
                },
                SessionCommand::Unsubscribe {
                    key: SubscriptionKey(1),
                },
            ],
        )
        .await;

        let rejection = control_targets(&outcome)
            .into_iter()
            .find(|(_, outcome)| matches!(outcome, ControlOutcome::Rejected { .. }));
        assert_eq!(
            rejection.map(|(target, _)| target),
            Some(ControlTarget::Subscription {
                key: SubscriptionKey(1),
                operation: SubscriptionOperation::Unsubscribe,
            })
        );
        // Still alive, and its updates still route to the same key.
        let routed = outcome.data().iter().any(|(_, key, outcome)| {
            *key == SubscriptionKey(1) && matches!(outcome, Ok(SubscriptionEvent::Update(_)))
        });
        assert!(
            routed,
            "the surviving subscription still routes its updates"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_rejected_reconfigure_names_the_subscription_and_the_operation() {
        // Code 13 is "Subscription frequency reconfiguration not allowed
        // because the subscription is configured for unfiltered dispatching"
        // [`docs/spec/05-error-codes.md` §2].
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            Step::AwaitControls(2),
            line("REQERR,2,13,Reconfiguration not allowed"),
            line(SERVER_END),
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![
                SessionCommand::Subscribe {
                    key: SubscriptionKey(1),
                    spec: Box::new(spec()),
                },
                SessionCommand::Reconfigure {
                    key: SubscriptionKey(1),
                    max_frequency: MaxFrequencyLimit::Unlimited,
                },
            ],
        )
        .await;

        let rejection = control_targets(&outcome)
            .into_iter()
            .find(|(_, outcome)| matches!(outcome, ControlOutcome::Rejected { .. }));
        assert_eq!(
            rejection.map(|(target, _)| target),
            Some(ControlTarget::Subscription {
                key: SubscriptionKey(1),
                operation: SubscriptionOperation::Reconfigure,
            })
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_session_level_control_responses_target_the_session() {
        // `force_rebind` and `destroy` concern no subscription, so there is
        // nothing to route them to.
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("END,31,Destroy invoked by client"),
            Step::End,
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::Destroy { cause: None }],
        )
        .await;

        assert_eq!(
            control_targets(&outcome)
                .into_iter()
                .map(|(target, _)| target)
                .collect::<Vec<_>>(),
            vec![ControlTarget::Session]
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_subscription_whose_add_could_not_be_sent_is_re_issued_on_the_next_bind() {
        // The other silent-loss path: the `add` never leaves the client, so no
        // `REQERR` will ever arrive to clean up after it. Without releasing the
        // wire binding the subscription would look issued forever and be
        // skipped by every later re-establishment.
        let first = vec![line(F1_CONOK), Step::AwaitControls(1), Step::Fail("reset")];
        let second = vec![line(F1_CONOK), Step::AwaitControls(2), line(SERVER_END)];

        let transport = MockTransport::new(websocket(), vec![first, second]).failing_controls(1);

        let outcome = drive_transport(
            transport,
            options(),
            vec![SessionCommand::Subscribe {
                key: SubscriptionKey(1),
                spec: Box::new(spec()),
            }],
        )
        .await;

        // The failure is reported, named, and not silent.
        assert_eq!(
            control_targets(&outcome)
                .into_iter()
                .filter(|(_, outcome)| matches!(outcome, ControlOutcome::NotSent { .. }))
                .map(|(target, _)| target)
                .collect::<Vec<_>>(),
            vec![ControlTarget::Subscription {
                key: SubscriptionKey(1),
                operation: SubscriptionOperation::Subscribe,
            }]
        );
        // And it is issued again on the recovered session, with a fresh
        // `LS_subId` — ids "must not be reused" within a session (§4.4).
        let adds: Vec<String> = outcome
            .controls()
            .into_iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .collect();
        assert_eq!(adds.len(), 2, "{adds:?}");
        assert!(
            adds.last().is_some_and(|last| last.contains("LS_subId=2")),
            "{adds:?}"
        );
    }

    // -----------------------------------------------------------------------
    // Message send (§12)
    // -----------------------------------------------------------------------

    fn messages(
        outcome: &Outcome,
    ) -> Vec<(Option<u64>, MessageSequence, Option<u64>, MessageResult)> {
        outcome
            .events
            .iter()
            .filter_map(|event| match event {
                SessionEvent::Message {
                    progressive,
                    sequence,
                    prog,
                    result,
                } => Some((*progressive, sequence.clone(), *prog, result.clone())),
                _ => None,
            })
            .collect()
    }

    #[tokio::test(start_paused = true)]
    async fn test_message_send_is_dispatched_and_its_outcome_reported() {
        // The spec's own `msg` example orders the parameters LS_sequence,
        // LS_msg_prog, then LS_message [`docs/spec/03-requests.md` §12.4], and
        // the outcome arrives as `MSGDONE`
        // [`docs/spec/04-notifications.md` §4.1].
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("MSGDONE,Orders_Sequence,3,Processed with ID 32652506"),
            line(SERVER_END),
        ];

        let sequence = SequenceName::try_new("Orders_Sequence").expect("a valid sequence name");
        let message = OutgoingMessage::numbered("buy 100", NonZeroU32::new(3).expect("non-zero"))
            .in_sequence(sequence);

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::SendMessage {
                message: Box::new(message),
            }],
        )
        .await;

        let controls = outcome.controls();
        assert_eq!(controls.len(), 1);
        let sent = controls.first().expect("one control request");
        assert!(sent.contains("LS_reqId=1"), "{sent}");
        assert!(sent.contains("LS_sequence=Orders_Sequence"), "{sent}");
        assert!(sent.contains("LS_msg_prog=3"), "{sent}");
        assert!(sent.contains("LS_message=buy%20100"), "{sent}");

        assert_eq!(
            messages(&outcome),
            vec![(
                Some(1),
                MessageSequence::Named("Orders_Sequence".to_owned()),
                Some(3),
                MessageResult::Done {
                    response: "Processed with ID 32652506".to_owned(),
                },
            )]
        );
        // The submission is still reported on the control surface: `REQOK`
        // means accepted, `MSGDONE` means processed
        // [`docs/spec/03-requests.md` §13.1; §4.1].
        assert!(outcome.events.iter().any(|event| matches!(
            event,
            SessionEvent::ControlResponse {
                outcome: ControlOutcome::Accepted,
                ..
            }
        )));
    }

    #[tokio::test(start_paused = true)]
    async fn test_message_outcomes_are_counted_toward_the_recovery_progressive() {
        // `MSGDONE` and `MSGFAIL` are data notifications
        // [`docs/spec/02-session-lifecycle.md` §5.3], so they must advance the
        // count even though they leave on the message surface.
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            line("MSGDONE,*,1,"),
            line("U,1,1,a|b|c"),
            Step::Fail("connection reset"),
        ];
        let second = vec![line(F1_CONOK), line("PROG,3"), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![script, second],
            vec![SessionCommand::SendMessage {
                message: Box::new(OutgoingMessage::numbered(
                    "hello",
                    NonZeroU32::new(1).expect("non-zero"),
                )),
            }],
        )
        .await;

        assert_eq!(
            messages(&outcome)
                .first()
                .map(|(progressive, ..)| *progressive),
            Some(Some(2)),
            "the MSGDONE sits between the SUBOK and the U"
        );
        // SUBOK (1), MSGDONE (2), U (3) — so the recovery asks from 3.
        assert_eq!(
            outcome.opens().get(1),
            Some(&OpenRecord::Bind {
                session: Some(F1_SESSION.to_owned()),
                recovery_from: Some(3),
            })
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_message_failure_is_reported_with_the_servers_cause() {
        // `MSGFAIL` [`docs/spec/04-notifications.md` §4.2]; code 38 is "The
        // specified progressive number has been skipped by timeout"
        // [`docs/spec/05-error-codes.md` §2].
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line(
                "MSGFAIL,Orders_Sequence,4,38,The specified progressive number has been skipped by timeout",
            ),
            line(SERVER_END),
        ];

        let sequence = SequenceName::try_new("Orders_Sequence").expect("a valid sequence name");
        let message = OutgoingMessage::numbered("sell 50", NonZeroU32::new(4).expect("non-zero"))
            .in_sequence(sequence);

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::SendMessage {
                message: Box::new(message),
            }],
        )
        .await;

        assert_eq!(
            messages(&outcome)
                .first()
                .map(|(_, _, _, result)| result.clone()),
            Some(MessageResult::Failed {
                cause: ServerCause {
                    code: 38,
                    message: "The specified progressive number has been skipped by timeout"
                        .to_owned(),
                },
            })
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_message_refused_at_submission_gets_exactly_one_outcome() {
        // A `REQERR` means no `MSGDONE`/`MSGFAIL` will follow
        // [`docs/spec/03-requests.md` §13.2], so the refusal is the message's
        // one and only outcome.
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQERR,1,33,A message with this number has already been enqueued"),
            line(SERVER_END),
        ];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::SendMessage {
                message: Box::new(OutgoingMessage::numbered(
                    "duplicate",
                    NonZeroU32::new(2).expect("non-zero"),
                )),
            }],
        )
        .await;

        let reported = messages(&outcome);
        assert_eq!(reported.len(), 1, "exactly one outcome per message");
        assert_eq!(
            reported.first().map(|(_, _, _, result)| result.clone()),
            Some(MessageResult::Refused {
                cause: ServerCause {
                    code: 33,
                    message: "A message with this number has already been enqueued".to_owned(),
                },
            })
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_message_sent_while_unbound_fails_fast_and_is_never_buffered() {
        // A subscription is desired state and is re-issued on the next session;
        // a message is a one-shot side effect whose numbering the server
        // deduplicates per session, so it is failed instead of held.
        let first = vec![line(F1_CONOK), line("LOOP,60000"), Step::End];

        let outcome = drive(
            websocket(),
            options(),
            vec![first],
            vec![
                SessionCommand::SendMessage {
                    message: Box::new(OutgoingMessage::numbered(
                        "too late",
                        NonZeroU32::new(1).expect("non-zero"),
                    )),
                },
                SessionCommand::Shutdown,
            ],
        )
        .await;

        assert!(matches!(
            messages(&outcome)
                .first()
                .map(|(_, _, _, result)| result.clone()),
            Some(MessageResult::NotSent { .. })
        ));
        // Nothing was sent then, and nothing is sent later either.
        assert!(outcome.controls().is_empty());
    }

    #[tokio::test(start_paused = true)]
    async fn test_fire_and_forget_message_declines_its_outcome() {
        // `LS_msg_prog` "is mandatory whenever `LS_sequence` is specified or
        // `LS_outcome` is set to `true`", and `LS_outcome` defaults to `true`
        // [`docs/spec/03-requests.md` §12.1] — so a message with no progressive
        // must decline the outcome explicitly, or the encoder refuses it.
        let script = vec![line(F1_CONOK), Step::AwaitControls(1), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::SendMessage {
                message: Box::new(OutgoingMessage::fire_and_forget("ping")),
            }],
        )
        .await;

        let controls = outcome.controls();
        let sent = controls.first().expect("one control request");
        assert!(sent.contains("LS_outcome=false"), "{sent}");
        assert!(!sent.contains("LS_msg_prog"), "{sent}");
        // No outcome was requested, so none is reported.
        assert!(messages(&outcome).is_empty());
    }

    // -----------------------------------------------------------------------
    // ADR-0005: the caller can tell the outcomes apart
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_adr0005_recovery_and_reestablishment_are_distinguishable() {
        // Same interruption, two server answers. An application holding derived
        // state must be able to keep it in one case and discard it in the other
        // (`docs/adr/0005-recovery-is-visible-in-the-event-stream.md`).
        let interrupted = || {
            vec![
                line(F8_CONOK),
                line("SUBOK,1,1,3"),
                Step::Fail("connection reset"),
            ]
        };

        let recovered = drive(
            websocket(),
            options(),
            vec![
                interrupted(),
                vec![line(F8_CONOK), line("PROG,1"), line(SERVER_END)],
            ],
            Vec::new(),
        )
        .await;
        assert_eq!(
            recovered.bound().get(1).map(|info| info.kind.clone()),
            Some(BindKind::Recovering {
                requested_progressive: 1
            })
        );
        assert_eq!(
            recovered.recoveries().first().map(|outcome| outcome.kind),
            Some(RecoveryKind::Exact)
        );

        let replaced = drive(
            websocket(),
            options(),
            vec![
                interrupted(),
                vec![line("CONERR,4,Recovery not possible"), Step::End],
                vec![line(F1_CONOK), line(SERVER_END)],
            ],
            Vec::new(),
        )
        .await;
        assert_eq!(
            replaced.bound().get(1).map(|info| info.kind.clone()),
            Some(BindKind::Recreated {
                previous: Some(SessionId::new(F8_SESSION)),
            })
        );
        assert!(replaced.recoveries().is_empty());
    }

    #[tokio::test(start_paused = true)]
    async fn test_adr0005_snapshot_re_delivery_is_visible_on_resubscribe() {
        // "a subscription's data restarted from a snapshot, versus resumed
        // without one" (ADR-0005). The spec says a snapshot is sent only if the
        // subscription asked for it [`docs/spec/03-requests.md` §6.1], so the
        // request is what the caller is told about.
        let mut with_snapshot = spec();
        with_snapshot.snapshot = Some(Snapshot::On);

        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            Step::Fail("connection reset"),
        ];
        let second = vec![line("CONERR,20,Specified session not found"), Step::End];
        let third = vec![line(F6_CONOK), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second, third],
            vec![SessionCommand::Subscribe {
                key: SubscriptionKey(1),
                spec: Box::new(with_snapshot),
            }],
        )
        .await;

        let resubscribed = outcome.resubscribes();
        let entry = resubscribed
            .first()
            .and_then(|entries| entries.first())
            .expect("one re-subscribed entry");
        assert!(entry.snapshot_requested);
        assert!(entry.previously_active);
    }

    // -----------------------------------------------------------------------
    // Reconnection policy and shutdown
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_retries_are_bounded_and_reported_as_definitive_loss() {
        // An unbounded retry that never surfaces a typed outcome is exactly
        // what the backoff policy exists to prevent.
        // The stream never binds: a `CONOK` would reset the budget, which is
        // what `test_a_successful_bind_resets_the_retry_budget` covers.
        let failing = || vec![Step::Fail("connection refused")];
        let options = options().with_backoff(BackoffPolicy {
            initial: Duration::from_millis(10),
            max: Duration::from_millis(20),
            max_attempts: NonZeroU32::new(2),
        });

        let outcome = drive(
            websocket(),
            options,
            vec![failing(), failing(), failing(), failing()],
            Vec::new(),
        )
        .await;

        assert!(matches!(
            outcome.closed,
            SessionClosed::RetriesExhausted { .. }
        ));
        // The initial attempt plus two retries.
        assert_eq!(outcome.opens().len(), 3);
        // Every failed connection is torn down, and so is the last one.
        assert_eq!(outcome.closes(), 4);
    }

    #[tokio::test(start_paused = true)]
    async fn test_a_successful_bind_resets_the_retry_budget() {
        // Otherwise a long-lived session would eventually exhaust its budget on
        // unrelated hiccups spread over days.
        let options = options().with_backoff(BackoffPolicy {
            initial: Duration::from_millis(10),
            max: Duration::from_millis(20),
            max_attempts: NonZeroU32::new(1),
        });
        let outcome = drive(
            websocket(),
            options,
            vec![
                vec![line(F1_CONOK), Step::Fail("blip")],
                vec![line(F1_CONOK), Step::Fail("blip")],
                vec![line(F1_CONOK), line(SERVER_END)],
            ],
            Vec::new(),
        )
        .await;

        assert!(matches!(outcome.closed, SessionClosed::ByServer { .. }));
        assert_eq!(outcome.bound().len(), 3);
    }

    #[tokio::test(start_paused = true)]
    async fn test_shutdown_stops_the_driver_and_closes_the_transport() {
        let script = vec![line(F1_CONOK), Step::Silence];

        let outcome = drive(
            websocket(),
            options(),
            vec![script],
            vec![SessionCommand::Shutdown],
        )
        .await;

        assert_eq!(
            outcome.closed,
            SessionClosed::ByClient {
                destroy_confirmed: false,
                cause: None,
            }
        );
        assert_eq!(outcome.closes(), 1);
        assert_eq!(outcome.opens().len(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn test_dropping_every_handle_stops_the_driver() {
        let transport = MockTransport::new(websocket(), vec![vec![line(F1_CONOK), Step::Silence]]);
        let log = Arc::clone(&transport.log);
        let (driver, handle, _events) =
            connect(transport, options()).expect("options must match the transport");
        drop(handle);

        let closed = driver.run().await;

        assert_eq!(
            closed,
            SessionClosed::ByClient {
                destroy_confirmed: false,
                cause: None,
            }
        );
        assert_eq!(log.lock().expect("mock log poisoned").closes, 1);
    }

    // -----------------------------------------------------------------------
    // Configuration and forward compatibility
    // -----------------------------------------------------------------------

    #[tokio::test(start_paused = true)]
    async fn test_connect_rejects_a_polling_mismatch_between_options_and_transport() {
        // `LS_polling` changes the meaning of the `CONOK` keep-alive argument
        // and forbids `LS_keepalive_millis` [§7.2, §8.4], so the two must agree.
        let transport = MockTransport::new(http_polling(), Vec::new());
        let result = connect(transport, options());
        assert!(matches!(result, Err(SessionError::Configuration { .. })));
    }

    #[tokio::test(start_paused = true)]
    async fn test_unknown_tag_is_surfaced_and_never_fatal() {
        // "a future server version must not crash an old client". The MPN tags
        // are the concrete case in v1 [`docs/spec/06-mpn.md`].
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("SUBOK,1,1,3"),
            line("MPNREG,devid,adapter"),
            line("U,1,1,a|b|c"),
            line(SERVER_END),
        ];

        let outcome = drive(websocket(), options(), vec![script], vec![subscribe()]).await;

        assert!(
            outcome
                .events
                .iter()
                .any(|event| matches!(event, SessionEvent::Unparsed { .. }))
        );
        // SPEC-AMBIGUITY A6: an unparsed line is not counted, so the `U` after
        // it is progressive 2 rather than 3. Under-counting makes a later
        // recovery ask from an earlier point, which the protocol handles by
        // re-delivering duplicates; over-counting would skip data.
        assert_eq!(
            outcome
                .data()
                .iter()
                .map(|(p, _, _)| *p)
                .collect::<Vec<_>>(),
            vec![1, 2]
        );
        assert!(matches!(outcome.closed, SessionClosed::ByServer { .. }));
    }

    #[tokio::test(start_paused = true)]
    async fn test_control_link_is_handed_to_the_transport() {
        // "the address… to which every following session rebind and control
        // request must be sent to" [`docs/spec/02-session-lifecycle.md` §3.1].
        let script = vec![
            line("CONOK,S1,50000,5000,push2.example.com:8080"),
            line(SERVER_END),
        ];

        let outcome = drive(websocket(), options(), vec![script], Vec::new()).await;

        assert_eq!(
            outcome.log.lock().expect("mock log poisoned").control_links,
            vec![Some("push2.example.com:8080".to_owned())]
        );
        assert_eq!(
            outcome
                .bound()
                .first()
                .and_then(|info| info.control_link.clone()),
            Some("push2.example.com:8080".to_owned())
        );
    }

    #[tokio::test(start_paused = true)]
    async fn test_unsubscribe_removes_the_subscription_from_the_desired_set() {
        // After `UNSUB` "no further update for the subscription will be sent"
        // [`docs/spec/04-notifications.md` §3.4], so it must not come back on
        // the next session either.
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            Step::AwaitControls(2),
            line("REQOK,2"),
            line("UNSUB,1"),
            Step::Fail("connection reset"),
        ];
        let second = vec![line("CONERR,20,Specified session not found"), Step::End];
        let third = vec![line(F6_CONOK), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second, third],
            vec![
                SessionCommand::Subscribe {
                    key: SubscriptionKey(1),
                    spec: Box::new(spec()),
                },
                SessionCommand::Unsubscribe {
                    key: SubscriptionKey(1),
                },
            ],
        )
        .await;

        let adds = outcome
            .controls()
            .iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .count();
        assert_eq!(adds, 1, "an unsubscribed item is not re-established");
        assert!(outcome.resubscribes().is_empty());
    }

    // -----------------------------------------------------------------------
    // Concurrency, liveness and shutdown
    // -----------------------------------------------------------------------

    /// How many `LS_op=add` requests a run produced.
    fn adds(outcome: &Outcome) -> usize {
        outcome
            .controls()
            .iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .count()
    }

    /// C-03. The pump is biased towards the server, and a server that always
    /// has a line ready would otherwise postpone every command for ever.
    ///
    /// Deliberately **not** on a paused clock: the condition this guards
    /// against is a driver that never yields, under which a paused clock never
    /// advances. Note that a regression here shows up as a hung test rather
    /// than a failing one, and unavoidably so — a future that never yields
    /// cannot be interrupted from inside the runtime it is starving. That is
    /// the defect, stated exactly.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_a_flooding_server_cannot_starve_a_destroy() {
        // `PROBE` is chosen deliberately: it produces no event, so nothing here
        // depends on the event channel having room. What is being measured is
        // the scheduling, not the backpressure.
        let script = vec![
            line(F1_CONOK),
            Step::FloodUntilControls("PROBE", 1),
            line("END,31,session destroyed"),
        ];

        let transport = MockTransport::new(websocket(), vec![script]);
        let log = Arc::clone(&transport.log);
        let (driver, handle, events) = connect(transport, options()).expect("valid options");
        handle
            .destroy(None)
            .await
            .expect("the driver has not started yet");

        let running = tokio::spawn(driver.run());
        tokio::time::timeout(Duration::from_secs(10), running)
            .await
            .expect("an unbounded line stream must not postpone a command for ever")
            .expect("the driver task did not panic");

        let controls = log.lock().expect("mock log poisoned").control_parameters();
        assert!(
            controls
                .iter()
                .any(|parameters| parameters.contains("LS_op=destroy")),
            "the destroy reached the wire: {controls:?}"
        );
        drop((handle, events));
    }

    /// C-02. A caller that stops reading its event stream stalls the session —
    /// that is the documented contract — but must not be able to make it
    /// unstoppable.
    #[tokio::test(start_paused = true)]
    async fn test_a_stop_is_honoured_while_the_event_stream_is_full() {
        // One event of capacity and nobody reading it: the driver blocks on the
        // second one, inside `emit`, where it is polling neither the socket nor
        // the command channel.
        let mut options = options();
        options.event_capacity = std::num::NonZeroUsize::MIN;
        // A flood that never stops: no control request is ever sent here, so
        // the gate never opens and the line stream stays permanently ready.
        let script = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("SUBOK,1,1,1"),
            Step::FloodUntilControls("U,1,1,20.4", usize::MAX),
        ];

        let transport = MockTransport::new(websocket(), vec![script]);
        let log = Arc::clone(&transport.log);
        let (driver, handle, events) = connect(transport, options).expect("valid options");
        handle
            .commands
            .send(subscribe())
            .await
            .expect("the driver has not started yet");

        let running = tokio::spawn(driver.run());
        // Let the driver bind, fill the single slot, and block on the next one.
        tokio::time::sleep(Duration::from_millis(50)).await;

        handle.stop();
        let closed = tokio::time::timeout(Duration::from_secs(5), running)
            .await
            .expect("a stop must not be subject to backpressure")
            .expect("the driver task did not panic");

        assert!(matches!(closed, SessionClosed::ByClient { .. }));
        assert!(
            log.lock().expect("mock log poisoned").closes >= 1,
            "the transport is closed on every exit path"
        );
        drop(events);
    }

    /// C-10. The budget covers the whole establishment, not just the wait for
    /// `CONOK` that follows it.
    #[tokio::test(start_paused = true)]
    async fn test_an_establishment_that_hangs_is_abandoned_within_the_budget() {
        let mut options = options();
        options.open_timeout = Duration::from_secs(3);
        let transport = MockTransport::new(websocket(), Vec::new()).hanging_open();

        let outcome = tokio::time::timeout(
            Duration::from_secs(120),
            drive_transport(transport, options, Vec::new()),
        )
        .await
        .expect("a hanging dial must not hang the session");

        assert!(matches!(
            outcome.closed,
            SessionClosed::RetriesExhausted { .. }
        ));
        assert!(
            outcome
                .unbinds()
                .iter()
                .all(|reason| matches!(reason, UnbindReason::ConnectionFailed { .. })),
            "every attempt timed out: {:?}",
            outcome.unbinds()
        );
    }

    /// C-10. A stop is honoured while a dial is still hanging.
    #[tokio::test(start_paused = true)]
    async fn test_a_stop_is_honoured_while_a_dial_is_hanging() {
        let mut options = options();
        // Long enough that the timeout cannot be what ends this.
        options.open_timeout = Duration::from_secs(3600);
        let transport = MockTransport::new(websocket(), Vec::new()).hanging_open();
        let (driver, handle, events) = connect(transport, options).expect("valid options");

        let running = tokio::spawn(driver.run());
        tokio::time::sleep(Duration::from_millis(50)).await;
        handle.stop();

        let closed = tokio::time::timeout(Duration::from_secs(5), running)
            .await
            .expect("a stop must interrupt an establishment")
            .expect("the driver task did not panic");
        assert!(matches!(closed, SessionClosed::ByClient { .. }));
        drop(events);
    }

    /// C-11. A heartbeat that cannot be written leaves an already-expired
    /// deadline in place; retrying it immediately is a spin.
    #[tokio::test(start_paused = true)]
    async fn test_repeated_heartbeat_failures_give_the_connection_up() {
        let options = options().with_connection(ConnectionMode::Streaming {
            inactivity_millis: Some(2000),
            keepalive_millis: None,
            send_sync: None,
        });
        // Silence after the bind: nothing but the heartbeat clock can fire.
        let first = vec![line(F1_CONOK), Step::Silence];
        let second = vec![line("CONERR,1,User/password check failed")];
        let transport = MockTransport::new(websocket(), vec![first, second])
            // Every heartbeat, and every retry of it, fails.
            .failing_controls(usize::MAX);

        let outcome = tokio::time::timeout(
            Duration::from_secs(600),
            drive_transport(transport, options, Vec::new()),
        )
        .await
        .expect("a failing heartbeat must not spin for ever");

        // Bounded: the connection was given up rather than retried without end.
        assert!(
            outcome
                .unbinds()
                .iter()
                .any(|reason| matches!(reason, UnbindReason::ConnectionFailed { .. })),
            "{:?}",
            outcome.unbinds()
        );
        let heartbeats = outcome
            .controls()
            .iter()
            .filter(|parameters| parameters.contains("LS_session"))
            .count();
        assert!(
            heartbeats <= usize::try_from(MAX_HEARTBEAT_FAILURES).unwrap_or(usize::MAX),
            "at most {MAX_HEARTBEAT_FAILURES} attempts before giving up, got {heartbeats}"
        );
    }

    /// C-12. An identifier is never handed out twice, and exhaustion is
    /// reported rather than wrapped.
    #[tokio::test]
    async fn test_identifier_spaces_are_exhausted_rather_than_reused() {
        let (mut driver, handle, events) =
            connect(MockTransport::new(websocket(), Vec::new()), options()).expect("valid options");

        driver.next_request_id = u64::MAX;
        assert!(
            driver.next_request_id().is_none(),
            "the last id is never handed out, because it cannot be advanced past"
        );

        handle.next_key.store(u64::MAX, Ordering::Relaxed);
        assert!(matches!(
            handle.allocate_key(),
            Err(SessionError::Exhausted { .. })
        ));
        drop((driver, events));
    }

    /// C-17. `PROG` is "sent only when requested via `LS_recovery_from`"
    /// [`docs/spec/02-session-lifecycle.md` §5.4]. One that answers no request
    /// of ours must not move the baseline every recovery decision rests on.
    #[tokio::test(start_paused = true)]
    async fn test_an_unsolicited_prog_cannot_move_the_recovery_baseline() {
        // A plain creation, one data notification, and a `PROG` claiming the
        // flow is a thousand notifications further on than it is.
        let first = vec![
            line(F1_CONOK),
            line("U,1,1,20.4"),
            line("PROG,1000"),
            Step::Fail("connection reset"),
        ];
        let second = vec![line(F1_CONOK), line(SERVER_END)];

        let outcome = drive(websocket(), options(), vec![first, second], Vec::new()).await;

        assert_eq!(
            outcome.opens().get(1),
            Some(&OpenRecord::Bind {
                session: Some(F1_SESSION.to_owned()),
                recovery_from: Some(1),
            }),
            "the recovery asks to resume from what was actually counted"
        );
        assert!(
            outcome.recoveries().is_empty(),
            "an unsolicited PROG reports no recovery outcome"
        );
    }

    /// C-18. Code 48 asks for a new session "immediately"
    /// [`docs/spec/05-error-codes.md` §5.4]. A server that answers every new
    /// session the same way must not get an unthrottled loop.
    #[tokio::test(start_paused = true)]
    async fn test_repeated_session_refreshes_are_delayed_and_eventually_given_up() {
        let refresh = || vec![line(F1_CONOK), line("END,48,please reconnect")];
        let scripts = (0..12).map(|_| refresh()).collect();

        let outcome = tokio::time::timeout(
            Duration::from_secs(600),
            drive(websocket(), options(), scripts, Vec::new()),
        )
        .await
        .expect("a refresh loop must terminate");

        let delays: Vec<Option<Duration>> = outcome
            .events
            .iter()
            .filter_map(|event| match event {
                SessionEvent::Unbound { retry_in, .. } => Some(*retry_in),
                _ => None,
            })
            .collect();
        assert_eq!(
            delays.first(),
            Some(&Some(Duration::ZERO)),
            "the first refresh is immediate, as the spec prescribes"
        );
        assert!(
            delays
                .iter()
                .skip(1)
                .any(|delay| matches!(delay, Some(delay) if !delay.is_zero())),
            "later refreshes are delayed: {delays:?}"
        );
        assert!(
            matches!(outcome.closed, SessionClosed::RetriesExhausted { .. }),
            "a server that only ever refreshes is a definitive loss, got {:?}",
            outcome.closed
        );
    }

    /// C-18. A session that delivered data is not part of a refresh loop, so a
    /// deployment that legitimately refreshes never accumulates a streak.
    #[tokio::test(start_paused = true)]
    async fn test_a_productive_session_clears_the_refresh_streak() {
        let productive = || {
            vec![
                line(F1_CONOK),
                line("U,1,1,20.4"),
                line("END,48,please reconnect"),
            ]
        };
        let scripts = vec![
            productive(),
            productive(),
            productive(),
            vec![line(F1_CONOK), line(SERVER_END)],
        ];

        let outcome = drive(websocket(), options(), scripts, Vec::new()).await;

        let delays: Vec<Option<Duration>> = outcome
            .events
            .iter()
            .filter_map(|event| match event {
                SessionEvent::Unbound { retry_in, .. } => Some(*retry_in),
                _ => None,
            })
            .collect();
        assert!(
            delays.iter().all(|delay| *delay == Some(Duration::ZERO)),
            "every refresh follows a productive session: {delays:?}"
        );
        assert!(matches!(outcome.closed, SessionClosed::ByServer { .. }));
    }

    /// C-07. §4.5 promises that "late responses to control requests related
    /// with the first session [may] arrive interspersed with notifications for
    /// the second session". One of them must not take a live subscription with
    /// it.
    #[tokio::test(start_paused = true)]
    async fn test_a_late_rejection_from_a_replaced_session_changes_nothing() {
        // Session A issues the `add` as request 1 and is then refreshed.
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("END,48,please reconnect"),
        ];
        // Session B re-issues it as request 2 — and only then does session A's
        // refusal of request 1 arrive.
        let second = vec![
            line(F6_CONOK),
            Step::AwaitControls(2),
            line("REQERR,1,19,Specified subscription not found"),
            line("U,1,1,20.4"),
            line("END,48,please reconnect"),
        ];
        // If the late refusal had been obeyed, there would be no third `add`.
        let third = vec![line(F8_CONOK), Step::AwaitControls(3), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second, third],
            vec![SessionCommand::Subscribe {
                key: SubscriptionKey(1),
                spec: Box::new(spec()),
            }],
        )
        .await;

        assert_eq!(
            adds(&outcome),
            3,
            "the subscription survived a refusal aimed at a session that no longer exists"
        );
        // The request that never got its answer is closed out at the boundary,
        // rather than left pending for ever.
        assert!(
            control_targets(&outcome).iter().any(|(target, outcome)| {
                matches!(
                    target,
                    ControlTarget::Subscription {
                        operation: SubscriptionOperation::Subscribe,
                        ..
                    }
                ) && matches!(outcome, ControlOutcome::NotSent { .. })
            }),
            "a retired request reports exactly one outcome: {:?}",
            control_targets(&outcome)
        );
    }

    /// C-07. The same guarantee for a message: one report per message, even
    /// when the session it was sent on is replaced before the answer arrives.
    #[tokio::test(start_paused = true)]
    async fn test_a_message_pending_across_a_replacement_still_gets_one_outcome() {
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("END,48,please reconnect"),
        ];
        let second = vec![line(F6_CONOK), line(SERVER_END)];

        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![SessionCommand::SendMessage {
                message: Box::new(OutgoingMessage::numbered("BUY 100", NonZeroU32::MIN)),
            }],
        )
        .await;

        let reported = messages(&outcome);
        assert_eq!(reported.len(), 1, "exactly one report: {reported:?}");
        assert!(matches!(
            reported.first().map(|(_, _, _, result)| result),
            Some(MessageResult::NotSent { .. })
        ));
    }

    /// C-08. An accepted `reconf` changes what the subscription *is*, so a
    /// session that has to be recreated must re-issue the new value.
    #[tokio::test(start_paused = true)]
    async fn test_an_accepted_reconfiguration_survives_a_recreated_session() {
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            Step::AwaitControls(2),
            line("REQOK,2"),
            line("END,48,please reconnect"),
        ];
        let second = vec![line(F6_CONOK), Step::AwaitControls(3), line(SERVER_END)];

        let limit = MaxFrequencyLimit::Limited(
            crate::protocol::request::DecimalNumber::try_new("2.5").expect("a decimal number"),
        );
        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![
                SessionCommand::Subscribe {
                    key: SubscriptionKey(1),
                    spec: Box::new(spec()),
                },
                SessionCommand::Reconfigure {
                    key: SubscriptionKey(1),
                    max_frequency: limit,
                },
            ],
        )
        .await;

        let controls = outcome.controls();
        let reissued = controls
            .iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .nth(1)
            .expect("the subscription was re-issued on the new session");
        assert!(
            reissued.contains("LS_requested_max_frequency=2.5"),
            "the recreated subscription keeps the frequency the server granted: {reissued}"
        );
    }

    /// C-08, the other half: a `reconf` the server refused must **not** change
    /// the desired state.
    #[tokio::test(start_paused = true)]
    async fn test_a_refused_reconfiguration_leaves_the_desired_state_alone() {
        let first = vec![
            line(F1_CONOK),
            Step::AwaitControls(1),
            line("REQOK,1"),
            line("SUBOK,1,1,3"),
            Step::AwaitControls(2),
            line("REQERR,2,25,Subscription is not unfiltered"),
            line("END,48,please reconnect"),
        ];
        let second = vec![line(F6_CONOK), Step::AwaitControls(3), line(SERVER_END)];

        let limit = MaxFrequencyLimit::Limited(
            crate::protocol::request::DecimalNumber::try_new("2.5").expect("a decimal number"),
        );
        let outcome = drive(
            websocket(),
            options(),
            vec![first, second],
            vec![
                SessionCommand::Subscribe {
                    key: SubscriptionKey(1),
                    spec: Box::new(spec()),
                },
                SessionCommand::Reconfigure {
                    key: SubscriptionKey(1),
                    max_frequency: limit,
                },
            ],
        )
        .await;

        let controls = outcome.controls();
        let reissued = controls
            .iter()
            .filter(|parameters| parameters.contains("LS_op=add"))
            .nth(1)
            .expect("the subscription was re-issued on the new session");
        assert!(
            !reissued.contains("LS_requested_max_frequency"),
            "a refused change is not desired state: {reissued}"
        );
    }
}