tailsurf 0.8.0

Rust SDK for tail.surf live transcript streams
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
//! Bounded REST, SSE, and WebSocket clients for the TSF service.

use std::{
    collections::{HashSet, VecDeque},
    fmt::Display,
    future::Future,
    pin::Pin,
    str::FromStr,
    sync::{Arc, OnceLock},
    task::{Context, Poll},
    time::Duration,
};

use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
use bytes::{Bytes, BytesMut};
use futures_util::{SinkExt, StreamExt};
use rand::{Rng, RngExt};
use reqwest::StatusCode;
use secrecy::ExposeSecret;
use serde::de::DeserializeOwned;
use tokio::{
    net::TcpStream,
    sync::{OwnedSemaphorePermit, Semaphore, mpsc, oneshot},
    task::JoinHandle,
    time::{sleep, timeout},
};
use tokio_tungstenite::{
    MaybeTlsStream, WebSocketStream, connect_async_with_config,
    tungstenite::{
        Error as WebSocketError, Message,
        client::IntoClientRequest,
        error::ProtocolError,
        http::{HeaderValue, header::SEC_WEBSOCKET_PROTOCOL},
    },
};
use url::Url;

use crate::{
    ClientWriterId, LinkId, LinkSecret, StreamId, WriterId,
    ids::{encode_base64url_32, is_canonical_base64url_32},
    protocol::{
        rest::{
            ApiErrorResponse, AppendJsonRecord, AppendRange, AppendRecordsRequest, CreateLinkInput,
            CreateStreamRequest, CreateStreamResponse, ListLinksResponse, MAX_LINK_PAGE_ITEMS,
            MAX_REST_ERROR_RESPONSE_BYTES, MAX_REST_RESPONSE_BYTES, MAX_SSE_EVENT_BYTES,
            MAX_SSE_READ_BATCH_PAYLOAD_BYTES, MAX_SSE_READ_BATCH_RECORDS,
            MAX_SSE_UNTERMINATED_EVENT_BYTES, MAX_STATELESS_APPEND_PAYLOAD_BYTES,
            MAX_STATELESS_APPEND_RECORDS, RecordData, RestRecordPart, SseCaughtUpData,
            SseReadBatchData, SseSnapshotBoundaryData, StreamLinkCredential, StreamMetadata,
            UpdateStreamRequest,
        },
        ws::{
            DEFAULT_READ_TAIL_OFFSET, MAX_PLAYBACK_RATE_PERMILLE, MAX_READ_SELECTOR_VALUE,
            MIN_PLAYBACK_RATE_PERMILLE, ReadStart, ReadStreamOptions, WriteStreamOptions,
            frame::{
                AppendRecord, CaughtUpPosition, ClientFrame, FrameCodecError,
                MAX_APPEND_BATCH_RECORDS, MAX_BATCH_PAYLOAD_BYTES, MAX_RECORD_BYTES, PartHeader,
                ReadBatch, RecordFormat, RecordMeta, ServerFrame, SnapshotBoundary,
                TSF_WEBSOCKET_PROTOCOL,
            },
        },
    },
};

type ClientWebSocket = WebSocketStream<MaybeTlsStream<TcpStream>>;

const API_PREFIX: &str = "/api/v1";
const MAX_CLIENT_DELAY: Duration = Duration::from_millis(2_147_483_647);

/// Timeouts, retry behavior, and API origin for [`TsfClient`].
///
/// Configured durations cannot exceed 2,147,483,647 milliseconds. Required timeouts must be
/// greater than zero.
#[derive(Clone, Debug)]
pub struct TsfClientConfig {
    /// Service origin without the `/api/v1` namespace.
    pub api_origin: Url,
    /// Per-request timeout for REST operations and SSE opening handshakes.
    pub rest_request_timeout: Duration,
    /// Timeout for establishing and upgrading a WebSocket.
    pub websocket_connect_timeout: Duration,
    /// Timeout for authentication, frame sends, and append acknowledgements.
    pub websocket_operation_timeout: Duration,
    /// Optional idle timeout while waiting for a read frame. Protocol heartbeats reset the timer.
    /// `None` waits indefinitely.
    pub websocket_read_idle_timeout: Option<Duration>,
    /// Retry policy for anonymous stream creation, idempotent metadata reads, socket setup, and
    /// consecutive read connection failures.
    pub retry_policy: RetryPolicy,
}

impl TsfClientConfig {
    /// Creates a configuration with bounded defaults for the supplied API origin.
    pub fn new(api_origin: Url) -> Result<Self, TsfClientError> {
        validate_api_origin(&api_origin)?;
        Ok(Self {
            api_origin,
            ..Self::default()
        })
    }
}

impl Default for TsfClientConfig {
    fn default() -> Self {
        Self {
            api_origin: default_api_origin(),
            rest_request_timeout: Duration::from_secs(10),
            websocket_connect_timeout: Duration::from_secs(10),
            websocket_operation_timeout: Duration::from_secs(30),
            websocket_read_idle_timeout: Some(Duration::from_secs(60)),
            retry_policy: RetryPolicy::default(),
        }
    }
}

/// Exponential-backoff policy for idempotent operations.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RetryPolicy {
    /// Total attempts including the initial request.
    pub max_attempts: usize,
    /// Base delay before the first retry. Client-controlled delays are jittered.
    pub initial_backoff: Duration,
    /// Maximum base delay and server retry hint honored by the client.
    pub max_backoff: Duration,
}

impl RetryPolicy {
    /// Returns a policy that performs exactly one attempt.
    pub fn none() -> Self {
        Self {
            max_attempts: 1,
            initial_backoff: Duration::ZERO,
            max_backoff: Duration::ZERO,
        }
    }

    fn next_backoff(self, current: Duration) -> Duration {
        current
            .checked_mul(2)
            .unwrap_or(self.max_backoff)
            .min(self.max_backoff)
    }

    fn reconnect_delay(self, retry: usize) -> Duration {
        let multiplier = 1_u32 << retry.min(30);
        let backoff = self
            .initial_backoff
            .checked_mul(multiplier)
            .unwrap_or(self.max_backoff)
            .min(self.max_backoff);
        jittered_backoff(backoff)
    }
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            initial_backoff: Duration::from_millis(200),
            max_backoff: Duration::from_secs(2),
        }
    }
}

fn jittered_backoff(backoff: Duration) -> Duration {
    if backoff.is_zero() {
        Duration::ZERO
    } else {
        backoff
            .mul_f64(rand::rng().random_range(0.5_f64..=1.5_f64))
            .min(MAX_CLIENT_DELAY)
    }
}

/// Cloneable TSF REST, SSE, and v1 WebSocket client.
///
/// REST operations preserve their retry identity and use [`RetryPolicy`]. Stateless append retries
/// can create physical duplicates, which logical transcript readers suppress. Durable WebSocket
/// writer recovery is owned by [`TsfWriter`].
#[derive(Clone)]
pub struct TsfClient {
    config: TsfClientConfig,
    http: reqwest::Client,
}

/// Pagination controls for one link inventory request.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ListLinksOptions {
    /// Maximum number of links to return. The service accepts values from 1 through 100.
    pub limit: Option<u8>,
    /// Opaque cursor returned by the previous page.
    pub cursor: Option<String>,
}

impl TsfClient {
    /// Creates a client for the default [tail.surf](https://tail.surf) API origin.
    pub fn new() -> Self {
        Self {
            config: TsfClientConfig::default(),
            http: reqwest::Client::new(),
        }
    }

    /// Creates a client for an explicit API origin with default timeouts.
    pub fn with_api_origin(api_origin: Url) -> Result<Self, TsfClientError> {
        Self::with_config(TsfClientConfig::new(api_origin)?)
    }

    /// Creates a client from a complete configuration.
    pub fn with_config(config: TsfClientConfig) -> Result<Self, TsfClientError> {
        validate_client_config(&config)?;
        Ok(Self {
            config,
            http: reqwest::Client::new(),
        })
    }

    /// Returns the configured API origin without the `/api/v1` namespace.
    pub fn api_origin(&self) -> &Url {
        &self.config.api_origin
    }

    /// Returns the complete immutable client configuration.
    pub fn config(&self) -> &TsfClientConfig {
        &self.config
    }

    /// Creates a stream and returns its metadata and initial link credentials.
    ///
    /// Construct the request once so its prepared link secrets remain stable. The client generates
    /// one idempotency key for this logical call and reuses the complete request while retrying
    /// transient failures according to policy.
    pub async fn create_stream(
        &self,
        request: &CreateStreamRequest,
    ) -> Result<CreateStreamResponse, TsfClientError> {
        let idempotency_key = CreateStreamIdempotencyKey::new_random();
        self.create_stream_with_idempotency_key(request, &idempotency_key)
            .await
    }

    /// Creates a logical stream using a caller-owned idempotency key.
    ///
    /// An exact retry requires the same prepared request. The idempotency key alone cannot return
    /// the link credentials.
    pub async fn create_stream_with_idempotency_key(
        &self,
        request: &CreateStreamRequest,
        idempotency_key: &CreateStreamIdempotencyKey,
    ) -> Result<CreateStreamResponse, TsfClientError> {
        if request
            .links
            .iter()
            .any(|link| !is_canonical_base64url_32(link.secret.expose_secret()))
        {
            return Err(TsfClientError::InvalidLinkSecret);
        }
        self.retry_when(
            || {
                self.send_json_with_bearer(
                    self.http
                        .post(self.rest_url("/streams"))
                        .header("Idempotency-Key", idempotency_key.expose_secret())
                        .json(&request),
                    "create stream",
                    None,
                )
            },
            TsfClientError::is_recoverable_create_failure,
        )
        .await
    }

    /// Retrieves current stream metadata, retrying transient failures according to policy.
    ///
    /// Private streams require a read-capable stream link. Public streams may pass `None`.
    pub async fn get_stream(
        &self,
        stream_id: &StreamId,
        link_secret: Option<&LinkSecret>,
    ) -> Result<StreamMetadata, TsfClientError> {
        self.get_json_with_bearer(
            format_args!("/streams/{stream_id}"),
            "get stream",
            link_secret,
        )
        .await
    }

    /// Updates owner-controlled stream settings.
    ///
    /// Transient failures are retried with the same absolute update values.
    pub async fn update_stream(
        &self,
        stream_id: &StreamId,
        request: &UpdateStreamRequest,
        owner_link_secret: &LinkSecret,
    ) -> Result<StreamMetadata, TsfClientError> {
        self.retry_transient(|| {
            self.send_json_with_bearer(
                self.http
                    .patch(self.rest_url(format_args!("/streams/{stream_id}")))
                    .json(request),
                "update stream",
                Some(owner_link_secret),
            )
        })
        .await
    }

    /// Permanently deletes a stream.
    ///
    /// Transient failures are retried. Deletion is idempotent.
    pub async fn delete_stream(
        &self,
        stream_id: &StreamId,
        owner_link_secret: &LinkSecret,
    ) -> Result<(), TsfClientError> {
        self.retry_transient(|| {
            self.send_empty(
                self.http
                    .delete(self.rest_url(format_args!("/streams/{stream_id}"))),
                "delete stream",
                Some(owner_link_secret),
            )
        })
        .await
    }

    /// Creates a stream link idempotently.
    ///
    /// Transient failures are retried with the same client-generated Link ID and secret.
    pub async fn create_link(
        &self,
        stream_id: &StreamId,
        request: &CreateLinkInput,
        owner_link_secret: &LinkSecret,
    ) -> Result<StreamLinkCredential, TsfClientError> {
        if !is_canonical_base64url_32(request.secret.expose_secret()) {
            return Err(TsfClientError::InvalidLinkSecret);
        }
        let link_id = request.link_id.clone();
        self.retry_transient(|| {
            self.send_json_with_bearer(
                self.http
                    .put(self.rest_url(format_args!("/streams/{stream_id}/links/{link_id}")))
                    .json(request),
                "create link",
                Some(owner_link_secret),
            )
        })
        .await
    }

    /// Lists one page of retained, non-secret link metadata.
    pub async fn list_links(
        &self,
        stream_id: &StreamId,
        options: &ListLinksOptions,
        owner_link_secret: &LinkSecret,
    ) -> Result<ListLinksResponse, TsfClientError> {
        if options
            .limit
            .is_some_and(|limit| !(1..=MAX_LINK_PAGE_ITEMS as u8).contains(&limit))
        {
            return Err(TsfClientError::InvalidListLinksOptions(
                "limit must be between 1 and 100",
            ));
        }
        if options.cursor.as_deref() == Some("") {
            return Err(TsfClientError::InvalidListLinksOptions(
                "cursor must not be empty",
            ));
        }
        let mut url = self.rest_url(format_args!("/streams/{stream_id}/links"));
        {
            let mut query = url.query_pairs_mut();
            if let Some(limit) = options.limit {
                query.append_pair("limit", &limit.to_string());
            }
            if let Some(cursor) = &options.cursor {
                query.append_pair("cursor", cursor);
            }
        }
        let page = self
            .retry_transient(|| {
                self.send_json_with_bearer(
                    self.http.get(url.clone()),
                    "list links",
                    Some(owner_link_secret),
                )
            })
            .await?;
        validate_link_page(
            &page,
            options.limit.unwrap_or(MAX_LINK_PAGE_ITEMS as u8) as usize,
        )?;
        Ok(page)
    }

    /// Lists every retained link, following pagination until completion.
    pub async fn list_all_links(
        &self,
        stream_id: &StreamId,
        owner_link_secret: &LinkSecret,
    ) -> Result<ListLinksResponse, TsfClientError> {
        let mut links = Vec::new();
        let mut cursor: Option<String> = None;
        let mut authorizing_link_id = None;
        let mut seen_cursors = HashSet::new();
        let mut seen_link_ids = HashSet::new();
        loop {
            let page = self
                .list_links(
                    stream_id,
                    &ListLinksOptions {
                        limit: Some(MAX_LINK_PAGE_ITEMS as u8),
                        cursor,
                    },
                    owner_link_secret,
                )
                .await?;
            if authorizing_link_id
                .as_ref()
                .is_some_and(|expected| expected != &page.authorizing_link_id)
            {
                return Err(TsfClientError::InvalidLinkPage(
                    "authorizing link changed across pages",
                ));
            }
            authorizing_link_id.get_or_insert(page.authorizing_link_id);
            // validate_link_page rejects duplicates within a page. Keep that invariant across
            // pages.
            for link in &page.links {
                if !seen_link_ids.insert(link.link_id.clone()) {
                    return Err(TsfClientError::InvalidLinkPage(
                        "link ID appears on multiple pages",
                    ));
                }
            }
            links.extend(page.links);
            match page.next_cursor {
                Some(next) if seen_cursors.insert(next.clone()) => cursor = Some(next),
                Some(_) => {
                    return Err(TsfClientError::InvalidLinkPage(
                        "link pagination cursor repeated",
                    ));
                }
                None => break,
            }
        }
        Ok(ListLinksResponse {
            authorizing_link_id: authorizing_link_id
                .expect("link inventory always contains an authorizing link ID"),
            links,
            next_cursor: None,
        })
    }

    /// Revokes a stream link by its non-secret identifier.
    ///
    /// Transient failures are retried. Revocation is idempotent.
    pub async fn revoke_link(
        &self,
        stream_id: &StreamId,
        link_id: &LinkId,
        owner_link_secret: &LinkSecret,
    ) -> Result<(), TsfClientError> {
        self.retry_transient(|| {
            self.send_empty(
                self.http
                    .delete(self.rest_url(format_args!("/streams/{stream_id}/links/{link_id}"))),
                "revoke link",
                Some(owner_link_secret),
            )
        })
        .await
    }

    /// Atomically appends one durable JSON batch without opening a WebSocket.
    ///
    /// A retry keeps client writer identity and writer sequence numbers stable. An ambiguous
    /// response may create physical duplicates. Logical readers suppress those duplicates.
    pub async fn append_records(
        &self,
        stream_id: &StreamId,
        client_writer_id: ClientWriterId,
        records: &[AppendRecord],
        expected_next_seq_num: Option<u64>,
        write_link_secret: &LinkSecret,
    ) -> Result<AppendRange, TsfClientError> {
        if records.is_empty() || records.len() > MAX_STATELESS_APPEND_RECORDS {
            return Err(TsfClientError::InvalidStatelessAppend(
                "record count must be between 1 and 128",
            ));
        }
        let writer_start_seq_num = records[0].writer_seq_num;
        let final_writer_seq_num = writer_start_seq_num
            .checked_add((records.len() - 1) as u64)
            .ok_or(TsfClientError::InvalidStatelessAppend(
                "writer sequence overflow",
            ))?;
        if final_writer_seq_num == u64::MAX {
            return Err(TsfClientError::InvalidStatelessAppend(
                "writer sequence range must end before u64::MAX",
            ));
        }
        if expected_next_seq_num.is_some_and(|value| value > MAX_READ_SELECTOR_VALUE) {
            return Err(TsfClientError::InvalidStatelessAppend(
                "expected next sequence exceeds the data adapter range",
            ));
        }
        let mut json_records = Vec::with_capacity(records.len());
        let mut payload_bytes = 0_usize;
        for (index, record) in records.iter().enumerate() {
            record.validate()?;
            payload_bytes = payload_bytes.checked_add(record.data.len()).ok_or(
                TsfClientError::InvalidStatelessAppend("payload size overflow"),
            )?;
            if record.writer_seq_num
                != writer_start_seq_num.checked_add(index as u64).ok_or(
                    TsfClientError::InvalidStatelessAppend("writer sequence overflow"),
                )?
            {
                return Err(TsfClientError::InvalidStatelessAppend(
                    "writer sequence numbers must be contiguous",
                ));
            }
            let data = compact_record_data(&record.data);
            json_records.push(AppendJsonRecord {
                data,
                format: record.format,
                part: Some(RestRecordPart {
                    index: record.part.index(),
                    is_final: record.part.is_final(),
                }),
            });
        }
        if payload_bytes > MAX_STATELESS_APPEND_PAYLOAD_BYTES {
            return Err(TsfClientError::InvalidStatelessAppend(
                "append payload must not exceed 900 KiB",
            ));
        }
        let request = AppendRecordsRequest {
            client_writer_id: URL_SAFE_NO_PAD.encode(client_writer_id.as_bytes()),
            writer_start_seq_num,
            records: json_records,
            expected_next_seq_num,
        };
        let range: AppendRange = self
            .retry_transient(|| {
                self.send_json_with_bearer(
                    self.http
                        .post(self.rest_url(format_args!("/streams/{stream_id}/records")))
                        .json(&request),
                    "append records",
                    Some(write_link_secret),
                )
            })
            .await?;
        validate_append_range(range, records.len())
    }

    /// Connects the standard bounded, reconnecting durable writer.
    pub async fn connect_writer(
        &self,
        options: WriteStreamOptions,
    ) -> Result<TsfWriter, TsfClientError> {
        self.connect_writer_with_config(options, TsfWriterConfig::default())
            .await
    }

    /// Connects a durable writer with explicit in-flight and reconnect bounds.
    pub async fn connect_writer_with_config(
        &self,
        mut options: WriteStreamOptions,
        config: TsfWriterConfig,
    ) -> Result<TsfWriter, TsfClientError> {
        let session = self.open_write_session(&options).await?;
        options.expected_next_seq_num = None;
        TsfWriter::new(self.clone(), options, session, config)
    }

    /// Connects a low-level write session that sends records and receives ack ranges directly.
    ///
    /// Unlike [`TsfWriter`], this session does not retain or resend unacknowledged records.
    pub async fn connect_write_session(
        &self,
        options: WriteStreamOptions,
    ) -> Result<TsfWriteSession, TsfClientError> {
        self.open_write_session(&options).await
    }

    async fn open_write_session(
        &self,
        options: &WriteStreamOptions,
    ) -> Result<TsfWriteSession, TsfClientError> {
        self.retry_transient(|| self.connect_write_session_once(options))
            .await
    }

    async fn connect_write_session_once(
        &self,
        options: &WriteStreamOptions,
    ) -> Result<TsfWriteSession, TsfClientError> {
        let url = self.websocket_url(format_args!("/streams/{}/write", options.stream_id))?;
        let connect_timeout = self.config.websocket_connect_timeout;
        let operation_timeout = self.config.websocket_operation_timeout;
        let opening_frame = ClientFrame::OpenWrite {
            client_writer_id: options.client_writer_id,
            link_secret: options.link_secret.clone(),
            expected_next_seq_num: options.expected_next_seq_num,
        }
        .encode()?;

        let mut ws =
            connect_websocket(url, connect_timeout, operation_timeout, opening_frame).await?;
        with_timeout(operation_timeout, "writer ready", expect_ready(&mut ws)).await?;

        Ok(TsfWriteSession {
            ws,
            operation_timeout,
        })
    }

    /// Connects a resumable read session at the requested position and bounds.
    pub async fn connect_reader(
        &self,
        mut options: ReadStreamOptions,
    ) -> Result<TsfReadSession, TsfClientError> {
        let ConnectedReadSocket {
            socket,
            stream_metadata,
            snapshot_boundary,
        } = self.connect_read_socket(&options).await?;
        apply_snapshot_boundary(&mut options, snapshot_boundary);
        Ok(TsfReadSession::new(
            self.clone(),
            options,
            socket,
            stream_metadata,
            None,
            snapshot_boundary,
        ))
    }

    /// Connects a resumable SSE reader.
    ///
    /// Private credentials stay in the bearer header. Reconnects reuse the original URL and send
    /// the latest versioned event cursor in `Last-Event-ID`. The REST request timeout bounds each
    /// opening handshake but not the established event body.
    pub async fn connect_sse_reader(
        &self,
        mut options: ReadStreamOptions,
    ) -> Result<TsfSseReadSession, TsfClientError> {
        validate_read_options(&options)?;
        let request_options = options.clone();
        let connection = self
            .open_sse_connection(&request_options, None)
            .await?
            .ok_or(TsfClientError::InvalidSse(
                "initial read completed without stream_metadata",
            ))?;
        if let Some(boundary) = connection.snapshot_boundary {
            apply_snapshot_boundary(&mut options, Some(boundary));
        }
        Ok(TsfSseReadSession {
            client: self.clone(),
            options,
            request_options,
            body: connection.body,
            parser: connection.parser,
            stream_metadata: connection
                .stream_metadata
                .expect("validated stream_metadata event"),
            last_caught_up: None,
            snapshot_boundary: connection.snapshot_boundary,
            reconnect_attempts: 0,
            last_event_id: connection.resume_event_id,
            finished: false,
        })
    }

    async fn open_sse_connection(
        &self,
        options: &ReadStreamOptions,
        last_event_id: Option<&str>,
    ) -> Result<Option<SseConnection>, TsfClientError> {
        let handshake_timeout = self.config.rest_request_timeout;
        self.retry_transient(|| async {
            with_timeout(
                handshake_timeout,
                "SSE handshake",
                self.open_sse_connection_once(options, last_event_id),
            )
            .await
        })
        .await
    }

    async fn open_sse_connection_once(
        &self,
        options: &ReadStreamOptions,
        last_event_id: Option<&str>,
    ) -> Result<Option<SseConnection>, TsfClientError> {
        let mut url = self.rest_url(format_args!("/streams/{}/records", options.stream_id));
        append_sse_query(&mut url, options);
        let mut request = self.apply_rest_auth(
            self.http.get(url).header("Accept", "text/event-stream"),
            options.link_secret.as_ref(),
        )?;
        if let Some(last_event_id) = last_event_id {
            request = request.header("Last-Event-ID", last_event_id);
        }
        let response = request.send().await?;
        if response.status() == StatusCode::NO_CONTENT {
            return Ok(None);
        }
        if !response.status().is_success() {
            return Err(http_status_error(response, "read SSE").await);
        }
        let mut connection = SseConnection {
            body: Box::pin(response.bytes_stream()),
            parser: SseParser::default(),
            stream_metadata: None,
            snapshot_boundary: None,
            resume_event_id: None,
        };
        let event = next_sse_event(&mut connection.body, &mut connection.parser)
            .await?
            .ok_or(TsfClientError::InvalidSse(
                "response ended before stream_metadata",
            ))?;
        if event.event != "stream_metadata" {
            return Err(TsfClientError::InvalidSse(
                "first event is not stream_metadata",
            ));
        }
        connection.stream_metadata = Some(
            serde_json::from_str(&event.data)
                .map_err(|_| TsfClientError::InvalidSse("invalid stream_metadata event"))?,
        );
        if event.id.is_some() {
            connection.resume_event_id = Some(sse_resume_event_id(&event)?.to_owned());
        }
        if options.snapshot {
            let event = next_sse_event(&mut connection.body, &mut connection.parser)
                .await?
                .ok_or(TsfClientError::InvalidSse(
                    "response ended before snapshot_boundary",
                ))?;
            if event.event != "snapshot_boundary" {
                return Err(TsfClientError::InvalidSse(
                    "snapshot_boundary must follow stream_metadata",
                ));
            }
            let (event_id, cursor) = sse_resume_cursor(&event)?;
            let boundary: SseSnapshotBoundaryData = serde_json::from_str(&event.data)
                .map_err(|_| TsfClientError::InvalidSse("invalid snapshot_boundary event"))?;
            let boundary = SnapshotBoundary {
                end_seq_num: boundary.end_seq_num,
                last_timestamp_ms: boundary.last_timestamp_ms,
            };
            let previous = last_event_id.map(parse_sse_resume_cursor).transpose()?;
            validate_sse_snapshot_cursor(boundary, cursor, previous)?;
            connection.resume_event_id = Some(event_id.to_owned());
            connection.snapshot_boundary = Some(boundary);
        }
        Ok(Some(connection))
    }

    async fn connect_read_socket(
        &self,
        options: &ReadStreamOptions,
    ) -> Result<ConnectedReadSocket, TsfClientError> {
        if let Some(start) = options.start {
            let value = match start {
                ReadStart::SeqNum(value)
                | ReadStart::TimestampMs(value)
                | ReadStart::TailOffset(value) => value,
            };
            if value > MAX_READ_SELECTOR_VALUE {
                return Err(TsfClientError::InvalidReadSelector {
                    value,
                    maximum: MAX_READ_SELECTOR_VALUE,
                });
            }
        }
        if let Some(rate) = options.playback_rate_permille {
            if !(MIN_PLAYBACK_RATE_PERMILLE..=MAX_PLAYBACK_RATE_PERMILLE).contains(&rate) {
                return Err(TsfClientError::InvalidPlaybackRate {
                    value: rate,
                    minimum: MIN_PLAYBACK_RATE_PERMILLE,
                    maximum: MAX_PLAYBACK_RATE_PERMILLE,
                });
            }
            if options.end_seq_num.is_none() && !options.snapshot {
                return Err(TsfClientError::PlaybackRequiresEnd);
            }
        }
        if options.snapshot && options.end_seq_num.is_some() {
            return Err(TsfClientError::SnapshotWithEnd);
        }
        let opening_frame = ClientFrame::OpenRead {
            link_secret: options.link_secret.clone(),
            start: options
                .start
                .unwrap_or(ReadStart::TailOffset(DEFAULT_READ_TAIL_OFFSET)),
            limit: options.limit,
            end_seq_num: options.end_seq_num,
            playback_rate_permille: options.playback_rate_permille,
            snapshot: options.snapshot,
        }
        .encode()?;
        let url = self.websocket_url(format_args!("/streams/{}/read", options.stream_id))?;
        let connect_timeout = self.config.websocket_connect_timeout;
        let operation_timeout = self.config.websocket_operation_timeout;
        let read_idle_timeout = self.config.websocket_read_idle_timeout;
        let snapshot = options.snapshot;

        self.retry_transient(|| {
            let url = url.clone();
            let opening_frame = opening_frame.clone();

            async move {
                let mut ws =
                    connect_websocket(url, connect_timeout, operation_timeout, opening_frame)
                        .await?;
                let handshake = with_timeout(
                    operation_timeout,
                    "reader handshake",
                    expect_read_handshake(&mut ws, snapshot),
                )
                .await?;

                Ok(ConnectedReadSocket {
                    socket: ReadSocket {
                        ws,
                        read_idle_timeout,
                    },
                    stream_metadata: handshake.stream_metadata,
                    snapshot_boundary: handshake.snapshot_boundary,
                })
            }
        })
        .await
    }

    fn rest_url(&self, path: impl Display) -> Url {
        let mut url = self.config.api_origin.clone();
        url.set_path(&format!("{API_PREFIX}{path}"));
        url.set_query(None);
        url.set_fragment(None);
        url
    }

    fn apply_rest_auth(
        &self,
        request: reqwest::RequestBuilder,
        link_secret: Option<&LinkSecret>,
    ) -> Result<reqwest::RequestBuilder, TsfClientError> {
        if let Some(secret) = link_secret {
            if !is_canonical_base64url_32(secret.expose_secret()) {
                return Err(TsfClientError::InvalidLinkSecret);
            }
            Ok(request.bearer_auth(secret.expose_secret()))
        } else {
            Ok(request)
        }
    }

    fn websocket_url(&self, path: impl Display) -> Result<Url, TsfClientError> {
        let mut url = self.rest_url(path);
        let scheme = match url.scheme() {
            "http" => "ws",
            "https" => "wss",
            other => return Err(TsfClientError::InvalidWebSocketScheme(other.to_owned())),
        };
        url.set_scheme(scheme)
            .map_err(|_| TsfClientError::InvalidWebSocketScheme(url.scheme().to_owned()))?;
        Ok(url)
    }

    async fn get_json_with_bearer<T: DeserializeOwned>(
        &self,
        path: impl Display,
        operation: &'static str,
        link_secret: Option<&LinkSecret>,
    ) -> Result<T, TsfClientError> {
        let url = self.rest_url(path);
        self.retry_transient(|| {
            self.send_json_with_bearer(self.http.get(url.clone()), operation, link_secret)
        })
        .await
    }

    async fn send_json_with_bearer<T: DeserializeOwned>(
        &self,
        request: reqwest::RequestBuilder,
        operation: &'static str,
        link_secret: Option<&LinkSecret>,
    ) -> Result<T, TsfClientError> {
        let response = self
            .apply_rest_auth(request, link_secret)?
            .timeout(self.config.rest_request_timeout)
            .send()
            .await?;
        json_response(response, operation).await
    }

    async fn send_empty(
        &self,
        request: reqwest::RequestBuilder,
        operation: &'static str,
        link_secret: Option<&LinkSecret>,
    ) -> Result<(), TsfClientError> {
        let response = self
            .apply_rest_auth(request, link_secret)?
            .timeout(self.config.rest_request_timeout)
            .send()
            .await?;
        let status = response.status();
        if status == StatusCode::NO_CONTENT {
            return Ok(());
        }
        Err(http_status_error(response, operation).await)
    }

    async fn retry_transient<T, Fut>(&self, run: impl FnMut() -> Fut) -> Result<T, TsfClientError>
    where
        Fut: Future<Output = Result<T, TsfClientError>>,
    {
        self.retry_when(run, TsfClientError::is_retryable).await
    }

    async fn retry_when<T, Fut>(
        &self,
        mut run: impl FnMut() -> Fut,
        should_retry: impl Fn(&TsfClientError) -> bool,
    ) -> Result<T, TsfClientError>
    where
        Fut: Future<Output = Result<T, TsfClientError>>,
    {
        let retry_policy = self.config.retry_policy;
        let attempts = retry_policy.max_attempts;
        let mut backoff = retry_policy.initial_backoff;

        for attempt in 1..=attempts {
            match run().await {
                Ok(value) => return Ok(value),
                Err(error) if attempt < attempts && should_retry(&error) => {
                    let delay = error
                        .retry_after()
                        .map(|delay| delay.min(retry_policy.max_backoff))
                        .unwrap_or_else(|| jittered_backoff(backoff));
                    if !delay.is_zero() {
                        sleep(delay).await;
                    }
                    backoff = retry_policy.next_backoff(backoff);
                }
                Err(error) => return Err(error),
            }
        }

        unreachable!("retry loop always returns from a non-empty attempt range")
    }
}

impl Default for TsfClient {
    fn default() -> Self {
        Self::new()
    }
}

/// Returns the default `https://tail.surf` API origin.
pub fn default_api_origin() -> Url {
    Url::parse("https://tail.surf").expect("default tsf API base URL is valid")
}

/// Non-authorizing idempotency key for one logical stream-creation request.
#[derive(Clone, Debug)]
pub struct CreateStreamIdempotencyKey(LinkSecret);

impl CreateStreamIdempotencyKey {
    /// Generates a cryptographically random canonical 256-bit key.
    pub fn new_random() -> Self {
        let mut bytes = [0_u8; 32];
        rand::rng().fill_bytes(&mut bytes);
        Self(encode_base64url_32(&bytes).into())
    }
}

impl FromStr for CreateStreamIdempotencyKey {
    type Err = InvalidCreateStreamIdempotencyKey;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if is_canonical_base64url_32(value) {
            Ok(Self(value.into()))
        } else {
            Err(InvalidCreateStreamIdempotencyKey)
        }
    }
}

impl ExposeSecret<str> for CreateStreamIdempotencyKey {
    fn expose_secret(&self) -> &str {
        self.0.expose_secret()
    }
}

/// Error returned for a malformed stream-creation idempotency key.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
#[error("create idempotency key must be canonical 43-character unpadded base64url")]
pub struct InvalidCreateStreamIdempotencyKey;

/// Low-level authenticated write socket without retained-record recovery.
pub struct TsfWriteSession {
    ws: ClientWebSocket,
    operation_timeout: Duration,
}

/// Maximum payload bytes a writer may retain before acknowledgement.
///
/// This matches the TSF writer socket's hard queued-payload bound.
pub const MAX_WRITER_UNACKED_PAYLOAD_BYTES: usize = 5 * 1024 * 1024;
/// Maximum records a writer may retain before acknowledgement.
///
/// This matches the TSF writer socket's hard queued-record bound.
pub const MAX_WRITER_UNACKED_RECORDS: usize = 128;

/// Memory and concurrency bounds for [`TsfWriter`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TsfWriterConfig {
    /// Maximum total payload bytes retained until durability acknowledgement. Must not exceed
    /// [`MAX_WRITER_UNACKED_PAYLOAD_BYTES`].
    pub max_unacked_bytes: usize,
    /// Maximum number of records retained until durability acknowledgement. Must not exceed
    /// [`MAX_WRITER_UNACKED_RECORDS`].
    pub max_unacked_records: usize,
}

impl TsfWriterConfig {
    fn validate(self) -> Result<Self, TsfClientError> {
        if self.max_unacked_bytes == 0 {
            return Err(TsfClientError::InvalidWriterConfig(
                "max_unacked_bytes must be greater than zero".to_owned(),
            ));
        }
        if self.max_unacked_bytes > MAX_WRITER_UNACKED_PAYLOAD_BYTES {
            return Err(TsfClientError::InvalidWriterConfig(format!(
                "max_unacked_bytes must not exceed {}",
                MAX_WRITER_UNACKED_PAYLOAD_BYTES
            )));
        }
        if self.max_unacked_records == 0 {
            return Err(TsfClientError::InvalidWriterConfig(
                "max_unacked_records must be greater than zero".to_owned(),
            ));
        }
        if self.max_unacked_records > MAX_WRITER_UNACKED_RECORDS {
            return Err(TsfClientError::InvalidWriterConfig(format!(
                "max_unacked_records must not exceed {}",
                MAX_WRITER_UNACKED_RECORDS
            )));
        }
        Ok(self)
    }
}

impl Default for TsfWriterConfig {
    fn default() -> Self {
        Self {
            max_unacked_bytes: MAX_WRITER_UNACKED_PAYLOAD_BYTES,
            max_unacked_records: MAX_WRITER_UNACKED_RECORDS,
        }
    }
}

impl AppendRecord {
    /// Creates a physical record without allocating when the input already owns compatible bytes.
    pub fn new(
        writer_seq_num: u64,
        part: PartHeader,
        format: RecordFormat,
        data: impl IntoRecordData,
    ) -> Self {
        Self {
            writer_seq_num,
            part,
            format,
            data: data.into_record_data(),
        }
    }

    fn validate(&self) -> Result<(), TsfClientError> {
        if self.data.len() > MAX_RECORD_BYTES {
            return Err(FrameCodecError::RecordTooLarge {
                actual: self.data.len(),
                max: MAX_RECORD_BYTES,
            }
            .into());
        }
        Ok(())
    }

    fn unacked_bytes(&self) -> usize {
        self.data.len().max(1)
    }
}

/// Server acknowledgement mapping a contiguous writer range to durable sequence numbers.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AppendAck {
    /// First acknowledged writer-local sequence number.
    pub writer_start_seq_num: u64,
    /// Exclusive writer-local sequence after the acknowledged range.
    pub writer_end_seq_num: u64,
    /// Durable sequence number assigned to the first acknowledged record.
    pub start_seq_num: u64,
    /// Exclusive durable sequence after the acknowledged range.
    pub end_seq_num: u64,
}

impl AppendAck {
    /// Returns whether the half-open writer range contains a sequence number.
    pub const fn contains_writer_seq(self, writer_seq_num: u64) -> bool {
        self.writer_start_seq_num <= writer_seq_num && writer_seq_num < self.writer_end_seq_num
    }

    /// Returns the number of records when writer and durable ranges are valid and equal in length.
    pub fn record_count(self) -> Result<u64, TsfClientError> {
        let writer_count = self
            .writer_end_seq_num
            .checked_sub(self.writer_start_seq_num)
            .ok_or(TsfClientError::InvalidAppendAck(self))?;
        let durable_count = self
            .end_seq_num
            .checked_sub(self.start_seq_num)
            .ok_or(TsfClientError::InvalidAppendAck(self))?;
        if writer_count == 0 {
            return Err(TsfClientError::InvalidAppendAck(self));
        }
        if writer_count != durable_count {
            return Err(TsfClientError::InvalidAppendAck(self));
        }
        Ok(writer_count)
    }

    fn validate(self) -> Result<Self, TsfClientError> {
        self.record_count()?;
        Ok(self)
    }
}

/// Durable assignment for one submitted record.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AppendReceipt {
    /// Submitted writer-local sequence number.
    pub writer_seq_num: u64,
    /// Durable sequence number assigned by the service.
    pub seq_num: u64,
    /// Append acknowledgement range that covered this record.
    pub ack: AppendAck,
}

/// Future that resolves when one submitted record is durable or permanently fails.
pub struct AppendTicket {
    rx: oneshot::Receiver<Result<AppendReceipt, TsfClientError>>,
    terminal_error: Arc<OnceLock<String>>,
}

impl AppendTicket {
    /// Polls for a completed receipt without registering an async wakeup.
    ///
    /// Returns `None` while the record remains pending.
    pub fn try_recv(&mut self) -> Option<Result<AppendReceipt, TsfClientError>> {
        match self.rx.try_recv() {
            Ok(result) => Some(result),
            Err(oneshot::error::TryRecvError::Empty) => {
                retained_terminal_error(&self.terminal_error).map(Err)
            }
            Err(oneshot::error::TryRecvError::Closed) => Some(Err(terminal_writer_error(
                &self.terminal_error,
                TsfClientError::AppendWriterDropped,
            ))),
        }
    }
}

impl Future for AppendTicket {
    type Output = Result<AppendReceipt, TsfClientError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match Pin::new(&mut self.rx).poll(cx) {
            Poll::Ready(Ok(result)) => Poll::Ready(result),
            Poll::Ready(Err(_)) => Poll::Ready(Err(terminal_writer_error(
                &self.terminal_error,
                TsfClientError::AppendWriterDropped,
            ))),
            Poll::Pending => retained_terminal_error(&self.terminal_error)
                .map_or(Poll::Pending, |error| Poll::Ready(Err(error))),
        }
    }
}

/// Bounded durable writer that retains unacknowledged records and resends them across transient
/// interruptions.
pub struct TsfWriter {
    cmd_tx: mpsc::Sender<WriterCommand>,
    byte_permits: Arc<Semaphore>,
    record_permits: Arc<Semaphore>,
    terminal_error: Arc<OnceLock<String>>,
    max_unacked_bytes: usize,
    task: Option<JoinHandle<()>>,
}

impl TsfWriter {
    fn new(
        client: TsfClient,
        options: WriteStreamOptions,
        session: TsfWriteSession,
        config: TsfWriterConfig,
    ) -> Result<Self, TsfClientError> {
        let config = config.validate()?;
        let command_capacity = config.max_unacked_records + 1;
        let (cmd_tx, cmd_rx) = mpsc::channel(command_capacity);
        let terminal_error = Arc::new(OnceLock::new());
        let task = tokio::spawn(run_writer(
            client,
            options,
            session,
            cmd_rx,
            Arc::clone(&terminal_error),
        ));

        Ok(Self {
            cmd_tx,
            byte_permits: Arc::new(Semaphore::new(config.max_unacked_bytes)),
            record_permits: Arc::new(Semaphore::new(config.max_unacked_records)),
            terminal_error,
            max_unacked_bytes: config.max_unacked_bytes,
            task: Some(task),
        })
    }

    /// Waits for window capacity, submits a record, and returns its durability ticket.
    pub async fn submit(&self, record: AppendRecord) -> Result<AppendTicket, TsfClientError> {
        let permit = self.reserve(record.unacked_bytes()).await?;
        permit.submit(record)
    }

    /// Reserves one record slot and at least one byte of the unacknowledged window.
    ///
    /// The returned permit owns capacity until it is dropped or submitted.
    pub async fn reserve(&self, bytes: usize) -> Result<WritePermit, TsfClientError> {
        let bytes = bytes.max(1);
        if bytes > self.max_unacked_bytes {
            return Err(TsfClientError::AppendRecordExceedsWriterWindow {
                bytes,
                max_unacked_bytes: self.max_unacked_bytes,
            });
        }

        let record_permit = self
            .record_permits
            .clone()
            .acquire_owned()
            .await
            .map_err(|_| self.closed_error())?;
        let byte_permit = self
            .byte_permits
            .clone()
            .acquire_many_owned(bytes as u32)
            .await
            .map_err(|_| self.closed_error())?;
        let cmd_tx_permit = self
            .cmd_tx
            .clone()
            .reserve_owned()
            .await
            .map_err(|_| self.closed_error())?;

        Ok(WritePermit {
            cmd_tx_permit,
            byte_permit,
            record_permit,
            terminal_error: Arc::clone(&self.terminal_error),
            reserved_bytes: bytes,
        })
    }

    /// Stops accepting records, waits for every pending durability acknowledgement, and joins the
    /// writer task.
    pub async fn close(mut self) -> Result<(), TsfClientError> {
        let (done_tx, mut done_rx) = oneshot::channel();
        self.cmd_tx
            .send(WriterCommand::Close { done_tx })
            .await
            .map_err(|_| self.closed_error())?;

        if let Some(task) = self.task.take() {
            task.await
                .map_err(|error| TsfClientError::AppendWriterFailed(error.to_string()))?;
        }

        done_rx.try_recv().map_err(|_| self.dropped_error())?
    }

    fn closed_error(&self) -> TsfClientError {
        terminal_writer_error(&self.terminal_error, TsfClientError::AppendWriterClosed)
    }

    fn dropped_error(&self) -> TsfClientError {
        terminal_writer_error(&self.terminal_error, TsfClientError::AppendWriterDropped)
    }
}

fn terminal_writer_error(
    terminal_error: &OnceLock<String>,
    fallback: TsfClientError,
) -> TsfClientError {
    retained_terminal_error(terminal_error).unwrap_or(fallback)
}

fn retained_terminal_error(terminal_error: &OnceLock<String>) -> Option<TsfClientError> {
    terminal_error
        .get()
        .map(|message| TsfClientError::AppendWriterFailed(message.clone()))
}

impl Drop for TsfWriter {
    fn drop(&mut self) {
        if let Some(task) = self.task.take() {
            task.abort();
        }
    }
}

/// Owned capacity in a writer's record and byte windows.
///
/// Dropping an unused permit releases its capacity.
pub struct WritePermit {
    cmd_tx_permit: mpsc::OwnedPermit<WriterCommand>,
    byte_permit: OwnedSemaphorePermit,
    record_permit: OwnedSemaphorePermit,
    terminal_error: Arc<OnceLock<String>>,
    reserved_bytes: usize,
}

impl WritePermit {
    /// Submits a record no larger than the reserved capacity without awaiting another window slot.
    pub fn submit(self, record: AppendRecord) -> Result<AppendTicket, TsfClientError> {
        if let Some(error) = retained_terminal_error(&self.terminal_error) {
            return Err(error);
        }
        record.validate()?;
        let bytes = record.unacked_bytes();
        if bytes > self.reserved_bytes {
            return Err(TsfClientError::AppendRecordExceedsReservedBytes {
                bytes,
                reserved_bytes: self.reserved_bytes,
            });
        }

        let (ack_tx, ack_rx) = oneshot::channel();
        self.cmd_tx_permit.send(WriterCommand::Submit {
            record,
            ack_tx,
            byte_permit: self.byte_permit,
            record_permit: self.record_permit,
        });

        Ok(AppendTicket {
            rx: ack_rx,
            terminal_error: self.terminal_error,
        })
    }
}

/// Conversion into payload bytes accepted by [`AppendRecord::new`].
pub trait IntoRecordData {
    /// Converts this value into reference-counted immutable bytes.
    fn into_record_data(self) -> Bytes;
}
/// Sealed marker for types that own their bytes and implement `Into<Bytes>`.
trait OwnedIntoBytes: Into<Bytes> {}
impl OwnedIntoBytes for Bytes {}
impl OwnedIntoBytes for Vec<u8> {}
impl OwnedIntoBytes for Box<[u8]> {}
impl OwnedIntoBytes for String {}

impl<T: OwnedIntoBytes> IntoRecordData for T {
    fn into_record_data(self) -> Bytes {
        self.into()
    }
}

impl IntoRecordData for &Bytes {
    fn into_record_data(self) -> Bytes {
        self.clone()
    }
}

impl IntoRecordData for &[u8] {
    fn into_record_data(self) -> Bytes {
        Bytes::copy_from_slice(self)
    }
}

impl<const N: usize> IntoRecordData for &[u8; N] {
    fn into_record_data(self) -> Bytes {
        Bytes::copy_from_slice(&self[..])
    }
}

impl IntoRecordData for &str {
    fn into_record_data(self) -> Bytes {
        Bytes::copy_from_slice(self.as_bytes())
    }
}

impl TsfWriteSession {
    /// Sends one physical record under the operation timeout.
    pub async fn send(&mut self, record: AppendRecord) -> Result<(), TsfClientError> {
        let operation_timeout = self.operation_timeout;

        with_timeout(operation_timeout, "send append frame", async move {
            self.buffer_batch(&[&record]).await?;
            self.flush().await
        })
        .await
    }

    /// Encodes one batch into the socket's write buffer, leaving the flush to the caller.
    async fn buffer_batch(&mut self, records: &[&AppendRecord]) -> Result<(), TsfClientError> {
        let frame = ClientFrame::encode_append_batch(records)?;
        self.ws.feed(Message::Binary(frame)).await?;
        Ok(())
    }

    /// Writes every buffered append batch to the transport in one flush.
    async fn flush(&mut self) -> Result<(), TsfClientError> {
        self.ws.flush().await?;
        Ok(())
    }

    /// Waits for and validates the next durability acknowledgement.
    ///
    /// Returns `None` when the service closes the socket normally before another ack.
    pub async fn next_ack(&mut self) -> Result<Option<AppendAck>, TsfClientError> {
        let frame = with_timeout(
            self.operation_timeout,
            "append acknowledgement",
            next_server_frame(&mut self.ws),
        )
        .await
        .map_err(|error| match error {
            TsfClientError::WebSocketClosedWithReason { code: 1008, reason }
                if reason == "sequence_mismatch" =>
            {
                TsfClientError::SequenceMismatch
            }
            other => other,
        })?;
        match frame {
            Some(ServerFrame::AppendAck {
                writer_start_seq_num,
                writer_end_seq_num,
                start_seq_num,
                end_seq_num,
            }) => AppendAck {
                writer_start_seq_num,
                writer_end_seq_num,
                start_seq_num,
                end_seq_num,
            }
            .validate()
            .map(Some),
            Some(frame) => Err(TsfClientError::UnexpectedServerFrame(server_frame_name(
                &frame,
            ))),
            None => Ok(None),
        }
    }
}

enum WriterCommand {
    Submit {
        record: AppendRecord,
        ack_tx: oneshot::Sender<Result<AppendReceipt, TsfClientError>>,
        byte_permit: OwnedSemaphorePermit,
        record_permit: OwnedSemaphorePermit,
    },
    Close {
        done_tx: oneshot::Sender<Result<(), TsfClientError>>,
    },
}

struct PendingAppend {
    record: AppendRecord,
    ack_tx: oneshot::Sender<Result<AppendReceipt, TsfClientError>>,
    _byte_permit: OwnedSemaphorePermit,
    _record_permit: OwnedSemaphorePermit,
}

async fn run_writer(
    client: TsfClient,
    options: WriteStreamOptions,
    mut session: TsfWriteSession,
    mut cmd_rx: mpsc::Receiver<WriterCommand>,
    terminal_error: Arc<OnceLock<String>>,
) {
    let mut pending = VecDeque::new();
    let mut close_tx: Option<oneshot::Sender<Result<(), TsfClientError>>> = None;
    let mut reconnect_attempts = 0;

    loop {
        tokio::select! {
            cmd = cmd_rx.recv(), if close_tx.is_none() => {
                match cmd {
                    Some(command) => {
                        let first_new = pending.len();
                        drain_submissions(&mut pending, &mut cmd_rx, &mut close_tx, command);

                        if let Err(error) = send_retained(&mut session, &pending, first_new).await
                            && let Err(error) = recover_pending_appends(
                                &mut session,
                                &client,
                                &options,
                                &pending,
                                &mut reconnect_attempts,
                                error,
                            )
                            .await
                        {
                            finish_writer_error(
                                &mut pending,
                                &mut close_tx,
                                &terminal_error,
                                error,
                            );
                            return;
                        }
                    }
                    None => {
                        fail_pending(&mut pending, "append writer dropped");
                        return;
                    }
                }
            }

            ack = session.next_ack(), if !pending.is_empty() => {
                match ack {
                    Ok(Some(ack)) => {
                        if let Err(error) = dispatch_ack(ack, &mut pending) {
                            finish_writer_error(
                                &mut pending,
                                &mut close_tx,
                                &terminal_error,
                                error,
                            );
                            return;
                        }
                        reconnect_attempts = 0;
                    }
                    Ok(None) => {
                        if let Err(error) = recover_pending_appends(
                            &mut session,
                            &client,
                            &options,
                            &pending,
                            &mut reconnect_attempts,
                            TsfClientError::WebSocketClosed,
                        )
                        .await
                        {
                            finish_writer_error(
                                &mut pending,
                                &mut close_tx,
                                &terminal_error,
                                error,
                            );
                            return;
                        }
                    }
                    Err(error) => {
                        if let Err(error) = recover_pending_appends(
                            &mut session,
                            &client,
                            &options,
                            &pending,
                            &mut reconnect_attempts,
                            error,
                        )
                        .await
                        {
                            finish_writer_error(
                                &mut pending,
                                &mut close_tx,
                                &terminal_error,
                                error,
                            );
                            return;
                        }
                    }
                }
            }
        }

        if close_tx.is_some() && pending.is_empty() {
            if let Some(close_tx) = close_tx.take() {
                let _ = close_tx.send(Ok(()));
            }
            return;
        }
    }
}

/// Moves the submitted record and every already-queued submission into `pending`.
///
/// This never awaits, so a batch is fully retained before any I/O can fail: a failed or timed-out
/// write leaves every record in `pending` for reconnect resend.
fn drain_submissions(
    pending: &mut VecDeque<PendingAppend>,
    cmd_rx: &mut mpsc::Receiver<WriterCommand>,
    close_tx: &mut Option<oneshot::Sender<Result<(), TsfClientError>>>,
    first: WriterCommand,
) {
    let mut command = Some(first);

    while let Some(WriterCommand::Submit {
        record,
        ack_tx,
        byte_permit,
        record_permit,
    }) = command
    {
        pending.push_back(PendingAppend {
            record,
            ack_tx,
            _byte_permit: byte_permit,
            _record_permit: record_permit,
        });
        command = cmd_rx.try_recv().ok();
    }

    if let Some(WriterCommand::Close { done_tx }) = command {
        *close_tx = Some(done_tx);
    }
}

/// Writes the records from `from` onwards under one operation timeout and one flush.
async fn send_retained(
    session: &mut TsfWriteSession,
    pending: &VecDeque<PendingAppend>,
    from: usize,
) -> Result<(), TsfClientError> {
    if from >= pending.len() {
        return Ok(());
    }
    let operation_timeout = session.operation_timeout;

    with_timeout(operation_timeout, "send append frames", async move {
        let mut records = pending.iter().skip(from).peekable();
        while records.peek().is_some() {
            let mut batch = Vec::with_capacity(MAX_APPEND_BATCH_RECORDS);
            let mut payload_bytes = 0;
            while batch.len() < MAX_APPEND_BATCH_RECORDS {
                let Some(next) = records.peek() else {
                    break;
                };
                if !batch.is_empty()
                    && next.record.data.len() > MAX_BATCH_PAYLOAD_BYTES - payload_bytes
                {
                    break;
                }
                let next = records.next().expect("peeked record");
                payload_bytes += next.record.data.len();
                batch.push(&next.record);
            }
            session.buffer_batch(&batch).await?;
        }
        session.flush().await
    })
    .await
}
async fn recover_pending_appends(
    session: &mut TsfWriteSession,
    client: &TsfClient,
    options: &WriteStreamOptions,
    pending: &VecDeque<PendingAppend>,
    reconnect_attempts: &mut usize,
    mut error: TsfClientError,
) -> Result<(), TsfClientError> {
    if !error.is_retryable() {
        return Err(error);
    }

    let retry_policy = client.config.retry_policy;
    let max_reconnects = retry_policy.max_attempts.saturating_sub(1);
    while *reconnect_attempts < max_reconnects {
        let delay = retry_policy.reconnect_delay(*reconnect_attempts);
        if !delay.is_zero() {
            sleep(delay).await;
        }
        *reconnect_attempts += 1;
        match client.connect_write_session_once(options).await {
            Ok(mut connected) => match send_retained(&mut connected, pending, 0).await {
                Ok(()) => {
                    *session = connected;
                    return Ok(());
                }
                Err(next_error) if next_error.is_retryable() => error = next_error,
                Err(next_error) => return Err(next_error),
            },
            Err(next_error) if next_error.is_retryable() => error = next_error,
            Err(next_error) => return Err(next_error),
        }
    }

    Err(error)
}

fn dispatch_ack(
    ack: AppendAck,
    pending: &mut VecDeque<PendingAppend>,
) -> Result<(), TsfClientError> {
    let record_count =
        usize::try_from(ack.record_count()?).map_err(|_| TsfClientError::InvalidAppendAck(ack))?;
    if record_count > pending.len() {
        return Err(TsfClientError::InvalidAppendAck(ack));
    }

    for (item, writer_seq_num) in pending
        .iter()
        .take(record_count)
        .zip(ack.writer_start_seq_num..ack.writer_end_seq_num)
    {
        if item.record.writer_seq_num < writer_seq_num {
            return Err(TsfClientError::AppendNotAcknowledged {
                writer_seq_num: item.record.writer_seq_num,
                ack,
            });
        }
        if item.record.writer_seq_num > writer_seq_num {
            return Err(TsfClientError::InvalidAppendAck(ack));
        }
    }

    for ((item, writer_seq_num), seq_num) in pending
        .drain(..record_count)
        .zip(ack.writer_start_seq_num..ack.writer_end_seq_num)
        .zip(ack.start_seq_num..ack.end_seq_num)
    {
        let _ = item.ack_tx.send(Ok(AppendReceipt {
            writer_seq_num,
            seq_num,
            ack,
        }));
    }

    Ok(())
}

fn validate_append_range(
    range: AppendRange,
    expected_records: usize,
) -> Result<AppendRange, TsfClientError> {
    if range.end_seq_num.checked_sub(range.start_seq_num) != Some(expected_records as u64) {
        return Err(TsfClientError::InvalidAppendRange(range));
    }
    Ok(range)
}

fn finish_writer_error(
    pending: &mut VecDeque<PendingAppend>,
    close_tx: &mut Option<oneshot::Sender<Result<(), TsfClientError>>>,
    terminal_error: &OnceLock<String>,
    error: TsfClientError,
) {
    let message = error.to_string();
    let _ = terminal_error.set(message.clone());
    fail_pending(pending, message);
    if let Some(close_tx) = close_tx.take() {
        let _ = close_tx.send(Err(error));
    }
}

fn fail_pending(pending: &mut VecDeque<PendingAppend>, message: impl Into<String>) {
    let message = message.into();
    while let Some(pending) = pending.pop_front() {
        let _ = pending
            .ack_tx
            .send(Err(TsfClientError::AppendWriterFailed(message.clone())));
    }
}

/// Streaming body for one SSE connection.
type SseBody = Pin<Box<dyn futures_util::Stream<Item = Result<Bytes, reqwest::Error>> + Send>>;

struct ParsedSseEvent {
    event: String,
    data: String,
    id: Option<String>,
}

struct SseConnection {
    body: SseBody,
    parser: SseParser,
    stream_metadata: Option<StreamMetadata>,
    snapshot_boundary: Option<SnapshotBoundary>,
    resume_event_id: Option<String>,
}

/// Resumable HTTP event-stream reader.
///
/// Transient transport and service interruptions reconnect from the next sequence number. Normal
/// completion and configured bounds return `None`; protocol and policy failures surface as errors.
pub struct TsfSseReadSession {
    client: TsfClient,
    options: ReadStreamOptions,
    request_options: ReadStreamOptions,
    body: SseBody,
    parser: SseParser,
    stream_metadata: StreamMetadata,
    last_caught_up: Option<CaughtUpPosition>,
    snapshot_boundary: Option<SnapshotBoundary>,
    reconnect_attempts: usize,
    last_event_id: Option<String>,
    finished: bool,
}

impl TsfSseReadSession {
    /// Returns authorized stream metadata from the opening event.
    pub fn stream_metadata(&self) -> &StreamMetadata {
        &self.stream_metadata
    }

    /// Returns the fixed boundary captured for a snapshot read.
    pub fn snapshot_boundary(&self) -> Option<SnapshotBoundary> {
        self.snapshot_boundary
    }

    /// Returns the most recent reconnect-safe caught-up position.
    pub fn last_caught_up(&self) -> Option<CaughtUpPosition> {
        self.last_caught_up
    }

    fn resume_cursors(
        &self,
        event: &ParsedSseEvent,
    ) -> Result<(ParsedSseResumeCursor, Option<ParsedSseResumeCursor>), TsfClientError> {
        Ok((
            sse_resume_cursor(event)?.1,
            self.last_event_id
                .as_deref()
                .map(parse_sse_resume_cursor)
                .transpose()?,
        ))
    }

    /// Returns the next record batch, reconnecting from the last safe absolute cursor when needed.
    ///
    /// The session advances past the whole batch on return: records the caller does not consume
    /// are not redelivered, including after a reconnect. Process or retain every needed record
    /// from each batch.
    pub async fn next_batch(&mut self) -> Result<Option<ReadBatch>, TsfClientError> {
        loop {
            if self.finished || read_options_exhausted(&self.options) {
                return Ok(None);
            }
            let event = match next_sse_event(&mut self.body, &mut self.parser).await {
                Ok(event) => event,
                Err(error) if error.is_resumable_sse_interruption() => None,
                Err(error) => return Err(error),
            };
            let Some(event) = event else {
                let retry_policy = self.client.config.retry_policy;
                let attempts = retry_policy.max_attempts;
                if self.reconnect_attempts + 1 >= attempts {
                    return Err(TsfClientError::ReadReconnectLimitExceeded {
                        max_connection_attempts: attempts,
                    });
                }
                let delay = retry_policy.reconnect_delay(self.reconnect_attempts);
                if !delay.is_zero() {
                    sleep(delay).await;
                }
                self.reconnect_attempts += 1;
                let Some(connection) = self
                    .client
                    .open_sse_connection(&self.request_options, self.last_event_id.as_deref())
                    .await?
                else {
                    self.finished = true;
                    return Ok(None);
                };
                if let Some(boundary) = connection.snapshot_boundary {
                    if self
                        .snapshot_boundary
                        .is_some_and(|previous| previous != boundary)
                    {
                        return Err(TsfClientError::InvalidSse(
                            "snapshot boundary changed during resume",
                        ));
                    }
                    self.snapshot_boundary = Some(boundary);
                }
                if connection.resume_event_id.is_some() {
                    self.last_event_id = connection.resume_event_id;
                }
                self.body = connection.body;
                self.parser = connection.parser;
                self.stream_metadata = connection
                    .stream_metadata
                    .expect("validated stream_metadata event");
                // A handshake alone is not progress. A read_batch or caught_up event resets the
                // counter below.
                continue;
            };
            match event.event.as_str() {
                "read_batch" => {
                    let batch: SseReadBatchData = serde_json::from_str(&event.data)
                        .map_err(|_| TsfClientError::InvalidSse("invalid read_batch event"))?;
                    validate_sse_read_batch_count(batch.records.len())?;
                    let batch = sse_read_batch(batch)?;
                    validate_sse_read_batch(&batch, &self.options)?;
                    let (cursor, previous) = self.resume_cursors(&event)?;
                    validate_sse_read_batch_cursor(
                        &batch,
                        cursor,
                        previous,
                        &self.options,
                        self.snapshot_boundary,
                    )?;
                    self.last_event_id = event.id;
                    self.reconnect_attempts = 0;
                    self.finished = advance_read_options_for_batch(&mut self.options, &batch);
                    return Ok(Some(batch));
                }
                "caught_up" => {
                    let value: SseCaughtUpData = serde_json::from_str(&event.data)
                        .map_err(|_| TsfClientError::InvalidSse("invalid caught_up event"))?;
                    let caught_up = CaughtUpPosition {
                        next_seq_num: value.next_seq_num,
                        last_timestamp_ms: value.last_timestamp_ms,
                    };
                    let (cursor, previous) = self.resume_cursors(&event)?;
                    validate_sse_caught_up_cursor(
                        caught_up,
                        cursor,
                        previous,
                        &self.options,
                        self.snapshot_boundary,
                    )?;
                    self.last_event_id = event.id;
                    self.options.start = Some(ReadStart::SeqNum(caught_up.next_seq_num));
                    self.last_caught_up = Some(caught_up);
                    self.reconnect_attempts = 0;
                }
                "error" => return Err(TsfClientError::SseTerminal(event.data)),
                "stream_metadata" => {
                    self.stream_metadata = serde_json::from_str(&event.data)
                        .map_err(|_| TsfClientError::InvalidSse("invalid stream_metadata event"))?
                }
                _ => {}
            }
        }
    }
}

/// Resumable WebSocket reader.
///
/// Transient transport and service interruptions reconnect from the next sequence number. Normal
/// completion and configured bounds return `None`; protocol and policy failures surface as errors.
pub struct TsfReadSession {
    client: TsfClient,
    options: ReadStreamOptions,
    socket: ReadSocket,
    stream_metadata: StreamMetadata,
    finished: bool,
    last_caught_up: Option<CaughtUpPosition>,
    snapshot_boundary: Option<SnapshotBoundary>,
    no_progress_reconnects: usize,
    reconnect_backoff: Duration,
    pending_reconnect_backoff: Duration,
    reconnect_needed: bool,
}

impl TsfReadSession {
    fn new(
        client: TsfClient,
        options: ReadStreamOptions,
        socket: ReadSocket,
        stream_metadata: StreamMetadata,
        last_caught_up: Option<CaughtUpPosition>,
        snapshot_boundary: Option<SnapshotBoundary>,
    ) -> Self {
        let reconnect_backoff = client.config.retry_policy.initial_backoff;
        Self {
            client,
            options,
            socket,
            stream_metadata,
            finished: false,
            last_caught_up,
            snapshot_boundary,
            no_progress_reconnects: 0,
            reconnect_backoff,
            pending_reconnect_backoff: Duration::ZERO,
            reconnect_needed: false,
        }
    }

    /// Returns the latest reconnect-safe position reported after preceding records were delivered.
    pub const fn last_caught_up(&self) -> Option<CaughtUpPosition> {
        self.last_caught_up
    }

    /// Returns the fixed exclusive end captured for a snapshot read.
    pub const fn snapshot_boundary(&self) -> Option<SnapshotBoundary> {
        self.snapshot_boundary
    }

    /// Returns metadata supplied by the latest successful read handshake.
    pub const fn stream_metadata(&self) -> &StreamMetadata {
        &self.stream_metadata
    }

    /// Waits for the next record batch using the configured idle timeout.
    ///
    /// The session advances past the whole batch on return: records the caller does not consume
    /// are not redelivered, including after a reconnect. Process or retain every needed record
    /// from each batch.
    pub async fn next_batch(&mut self) -> Result<Option<ReadBatch>, TsfClientError> {
        self.next_batch_inner().await
    }

    /// Waits for the next record batch with a caller-supplied timeout for this operation.
    pub async fn next_batch_with_timeout(
        &mut self,
        timeout: Duration,
    ) -> Result<Option<ReadBatch>, TsfClientError> {
        with_timeout(timeout, "read stream batch", self.next_batch_inner()).await
    }

    async fn next_batch_inner(&mut self) -> Result<Option<ReadBatch>, TsfClientError> {
        loop {
            if self.finished || read_options_exhausted(&self.options) {
                self.finished = true;
                return Ok(None);
            }
            if self.reconnect_needed {
                self.reconnect().await?;
            }

            match self.socket.next_outcome().await {
                Ok(ReadSocketOutcome::Records(batch)) => {
                    validate_read_batch_for_request(&batch, &self.options)?;
                    self.batch_delivered(&batch);
                    return Ok(Some(batch));
                }
                Ok(ReadSocketOutcome::CaughtUp(caught_up)) => {
                    validate_caught_up_for_request(caught_up, &self.options)?;
                    self.options.start = Some(ReadStart::SeqNum(caught_up.next_seq_num));
                    self.last_caught_up = Some(caught_up);
                }
                Ok(ReadSocketOutcome::Closed) => {
                    self.finished = true;
                    return Ok(None);
                }
                Err(error) if error.is_resumable_read_interruption() => {
                    self.require_reconnect()?;
                    self.reconnect().await?;
                }
                Err(error) => return Err(error),
            }
        }
    }

    async fn reconnect(&mut self) -> Result<(), TsfClientError> {
        debug_assert!(self.reconnect_needed);
        let delay = jittered_backoff(self.pending_reconnect_backoff);
        if !delay.is_zero() {
            sleep(delay).await;
        }
        let ConnectedReadSocket {
            socket,
            stream_metadata,
            snapshot_boundary,
        } = self.client.connect_read_socket(&self.options).await?;
        self.socket = socket;
        self.stream_metadata = stream_metadata;
        apply_snapshot_boundary(&mut self.options, snapshot_boundary);
        if snapshot_boundary.is_some() {
            self.snapshot_boundary = snapshot_boundary;
        }
        self.no_progress_reconnects = 0;
        self.reconnect_backoff = self.client.config.retry_policy.initial_backoff;
        self.pending_reconnect_backoff = Duration::ZERO;
        self.reconnect_needed = false;
        Ok(())
    }

    fn require_reconnect(&mut self) -> Result<(), TsfClientError> {
        if self.reconnect_needed {
            return Ok(());
        }
        let retry_policy = self.client.config.retry_policy;
        let max_reconnects = retry_policy.max_attempts.saturating_sub(1);
        if self.no_progress_reconnects >= max_reconnects {
            return Err(TsfClientError::ReadReconnectLimitExceeded {
                max_connection_attempts: retry_policy.max_attempts,
            });
        }
        self.no_progress_reconnects += 1;
        self.pending_reconnect_backoff = self.reconnect_backoff;
        self.reconnect_backoff = retry_policy.next_backoff(self.reconnect_backoff);
        self.reconnect_needed = true;
        Ok(())
    }

    fn batch_delivered(&mut self, batch: &ReadBatch) {
        self.no_progress_reconnects = 0;
        self.reconnect_backoff = self.client.config.retry_policy.initial_backoff;
        self.pending_reconnect_backoff = Duration::ZERO;
        self.reconnect_needed = false;
        self.finished = advance_read_options_for_batch(&mut self.options, batch);
    }
}

fn advance_read_options_for_batch(options: &mut ReadStreamOptions, batch: &ReadBatch) -> bool {
    let last = batch.last().expect("validated non-empty batch");
    advance_read_options(options, last.seq_num, batch.len())
}

fn advance_read_options(
    options: &mut ReadStreamOptions,
    last_seq_num: u64,
    record_count: usize,
) -> bool {
    let Some(next_seq_num) = last_seq_num.checked_add(1) else {
        return true;
    };
    options.start = Some(ReadStart::SeqNum(next_seq_num));
    if let Some(remaining) = options.limit.as_mut() {
        *remaining = remaining.saturating_sub(record_count as u64);
    }
    read_options_exhausted(options)
}

fn read_options_exhausted(options: &ReadStreamOptions) -> bool {
    options.limit == Some(0)
        || matches!(
            (options.start, options.end_seq_num),
            (Some(ReadStart::SeqNum(start)), Some(end_seq_num)) if start >= end_seq_num
        )
}

fn validate_read_batch_for_request(
    batch: &ReadBatch,
    options: &ReadStreamOptions,
) -> Result<(), TsfClientError> {
    let Some(first) = batch.first() else {
        return Err(TsfClientError::InvalidReadResponse("ReadBatch is empty"));
    };
    let wrong_start = match options.start {
        Some(ReadStart::SeqNum(start)) => first.seq_num != start,
        Some(ReadStart::TimestampMs(start)) => first.timestamp_ms < start,
        Some(ReadStart::TailOffset(_)) | None => false,
    };
    if wrong_start {
        return Err(TsfClientError::InvalidReadResponse(
            "ReadBatch does not begin at the requested position",
        ));
    }
    if options
        .limit
        .is_some_and(|remaining| batch.len() as u64 > remaining)
    {
        return Err(TsfClientError::InvalidReadResponse(
            "ReadBatch exceeds the remaining record limit",
        ));
    }
    // Decode enforces strictly increasing sequences, so only the last record can cross.
    if options
        .end_seq_num
        .is_some_and(|end_seq_num| batch.last().is_some_and(|last| last.seq_num >= end_seq_num))
    {
        return Err(TsfClientError::InvalidReadResponse(
            "ReadBatch crosses the requested end sequence",
        ));
    }
    Ok(())
}

fn validate_caught_up_for_request(
    caught_up: CaughtUpPosition,
    options: &ReadStreamOptions,
) -> Result<(), TsfClientError> {
    if matches!(options.start, Some(ReadStart::SeqNum(next)) if caught_up.next_seq_num != next) {
        return Err(TsfClientError::InvalidReadResponse(
            "CaughtUp does not match the next requested sequence",
        ));
    }
    Ok(())
}

struct ReadSocket {
    ws: ClientWebSocket,
    read_idle_timeout: Option<Duration>,
}

struct ConnectedReadSocket {
    socket: ReadSocket,
    stream_metadata: StreamMetadata,
    snapshot_boundary: Option<SnapshotBoundary>,
}

fn apply_snapshot_boundary(options: &mut ReadStreamOptions, boundary: Option<SnapshotBoundary>) {
    let Some(boundary) = boundary else {
        return;
    };
    options.snapshot = false;
    options.end_seq_num = Some(boundary.end_seq_num);
}

impl ReadSocket {
    async fn next_outcome(&mut self) -> Result<ReadSocketOutcome, TsfClientError> {
        loop {
            let outcome = if let Some(read_idle_timeout) = self.read_idle_timeout {
                with_timeout(
                    read_idle_timeout,
                    "read stream record",
                    next_read_socket_frame(&mut self.ws),
                )
                .await?
            } else {
                next_read_socket_frame(&mut self.ws).await?
            };
            if let Some(outcome) = outcome {
                return Ok(outcome);
            }
        }
    }
}

enum ReadSocketOutcome {
    Records(ReadBatch),
    CaughtUp(CaughtUpPosition),
    Closed,
}

async fn connect_websocket(
    url: Url,
    connect_timeout: Duration,
    operation_timeout: Duration,
    opening_frame: Bytes,
) -> Result<ClientWebSocket, TsfClientError> {
    // TSF v1 sends each batch in one message, so Nagle could hold a small append back for an ACK.
    const DISABLE_NAGLE: bool = true;

    let mut request = url.as_str().into_client_request()?;
    request.headers_mut().insert(
        SEC_WEBSOCKET_PROTOCOL,
        HeaderValue::from_static(TSF_WEBSOCKET_PROTOCOL),
    );

    let (mut ws, response) = timeout(
        connect_timeout,
        connect_async_with_config(request, None, DISABLE_NAGLE),
    )
    .await
    .map_err(|_| TsfClientError::Timeout {
        operation: "connect websocket",
    })??;
    let selected_protocol = response
        .headers()
        .get(SEC_WEBSOCKET_PROTOCOL)
        .map(|value| {
            value
                .to_str()
                .map_err(|_| TsfClientError::InvalidWebSocketProtocolHeader)
        })
        .transpose()?;

    if selected_protocol != Some(TSF_WEBSOCKET_PROTOCOL) {
        return Err(TsfClientError::UnexpectedWebSocketProtocol(
            selected_protocol.map(str::to_owned),
        ));
    }

    timeout(operation_timeout, ws.send(Message::Binary(opening_frame)))
        .await
        .map_err(|_| TsfClientError::Timeout {
            operation: "send opening frame",
        })??;

    Ok(ws)
}

async fn with_timeout<T>(
    duration: Duration,
    operation: &'static str,
    future: impl Future<Output = Result<T, TsfClientError>>,
) -> Result<T, TsfClientError> {
    timeout(duration, future)
        .await
        .map_err(|_| TsfClientError::Timeout { operation })?
}

fn validate_read_options(options: &ReadStreamOptions) -> Result<(), TsfClientError> {
    if let Some(start) = options.start {
        let value = match start {
            ReadStart::SeqNum(value)
            | ReadStart::TimestampMs(value)
            | ReadStart::TailOffset(value) => value,
        };
        if value > MAX_READ_SELECTOR_VALUE {
            return Err(TsfClientError::InvalidReadSelector {
                value,
                maximum: MAX_READ_SELECTOR_VALUE,
            });
        }
    }
    if let Some(rate) = options.playback_rate_permille {
        if !(MIN_PLAYBACK_RATE_PERMILLE..=MAX_PLAYBACK_RATE_PERMILLE).contains(&rate) {
            return Err(TsfClientError::InvalidPlaybackRate {
                value: rate,
                minimum: MIN_PLAYBACK_RATE_PERMILLE,
                maximum: MAX_PLAYBACK_RATE_PERMILLE,
            });
        }
        if options.end_seq_num.is_none() && !options.snapshot {
            return Err(TsfClientError::PlaybackRequiresEnd);
        }
    }
    if options.snapshot && options.end_seq_num.is_some() {
        return Err(TsfClientError::SnapshotWithEnd);
    }
    Ok(())
}

fn append_sse_query(url: &mut Url, options: &ReadStreamOptions) {
    let mut query = url.query_pairs_mut();
    match options.start {
        Some(ReadStart::SeqNum(value)) => {
            query.append_pair("seq_num", &value.to_string());
        }
        Some(ReadStart::TimestampMs(value)) => {
            query.append_pair("timestamp_ms", &value.to_string());
        }
        Some(ReadStart::TailOffset(value)) => {
            query.append_pair("tail_offset", &value.to_string());
        }
        None => {}
    }
    if let Some(value) = options.limit {
        query.append_pair("limit", &value.to_string());
    }
    if let Some(value) = options.end_seq_num {
        query.append_pair("end_seq_num", &value.to_string());
    }
    if let Some(value) = options.playback_rate_permille {
        query.append_pair("playback_rate_permille", &value.to_string());
    }
    if options.snapshot {
        query.append_pair("snapshot", "true");
    }
}

#[derive(Default)]
struct SseParser {
    buffer: Vec<u8>,
    offset: usize,
    /// Start of the not-yet-terminated event; only newly pushed bytes are validated.
    tail_start: usize,
}

impl SseParser {
    fn push(&mut self, chunk: &[u8]) -> Result<(), TsfClientError> {
        self.compact();
        self.buffer.extend_from_slice(chunk);
        self.validate_new_bytes(chunk.len())
    }

    fn next_event(&mut self) -> Result<Option<ParsedSseEvent>, TsfClientError> {
        loop {
            let Some((index, length)) = sse_boundary(&self.buffer[self.offset..]) else {
                return Ok(None);
            };
            let start = self.offset;
            self.offset += index + length;
            if let Some(event) = parse_sse_block(&self.buffer[start..start + index])? {
                return Ok(Some(event));
            }
        }
    }

    /// Validates only the bytes appended by the last push; earlier bytes were proven on arrival.
    /// The 3-byte overlap catches an event terminator straddling the previous chunk boundary.
    fn validate_new_bytes(&mut self, pushed: usize) -> Result<(), TsfClientError> {
        let new_start = self.buffer.len() - pushed;
        let mut pos = new_start.saturating_sub(3).max(self.tail_start);
        while let Some((index, length)) = sse_boundary(&self.buffer[pos..]) {
            let boundary_end = pos + index + length;
            if boundary_end - self.tail_start > MAX_SSE_EVENT_BYTES {
                return Err(TsfClientError::InvalidSse("event exceeds 2 MiB"));
            }
            self.tail_start = boundary_end;
            pos = boundary_end;
        }
        if self.buffer.len() - self.tail_start > MAX_SSE_UNTERMINATED_EVENT_BYTES {
            return Err(TsfClientError::InvalidSse(
                "unterminated event exceeds 2 MiB",
            ));
        }
        Ok(())
    }

    fn compact(&mut self) {
        if self.offset >= 64 * 1024 && self.offset >= self.buffer.len() / 2 {
            self.buffer.drain(..self.offset);
            self.tail_start -= self.offset;
            self.offset = 0;
        }
    }
}

async fn next_sse_event(
    body: &mut SseBody,
    parser: &mut SseParser,
) -> Result<Option<ParsedSseEvent>, TsfClientError> {
    loop {
        if let Some(event) = parser.next_event()? {
            return Ok(Some(event));
        }
        match body.next().await {
            Some(Ok(chunk)) => {
                parser.push(&chunk)?;
            }
            Some(Err(error)) => return Err(error.into()),
            None => return Ok(None),
        }
    }
}

fn sse_boundary(buffer: &[u8]) -> Option<(usize, usize)> {
    let mut from = 0;
    while let Some(index) = memchr::memchr(b'\n', &buffer[from..]) {
        let at = from + index;
        if at >= 3 && buffer[at - 3..=at] == *b"\r\n\r\n" {
            return Some((at - 3, 4));
        }
        if at >= 1 && buffer[at - 1] == b'\n' {
            return Some((at - 1, 2));
        }
        from = at + 1;
    }
    None
}

fn parse_sse_block(block: &[u8]) -> Result<Option<ParsedSseEvent>, TsfClientError> {
    let text =
        std::str::from_utf8(block).map_err(|_| TsfClientError::InvalidSse("event is not UTF-8"))?;
    // Borrow during the scan; allocate only for blocks that actually carry data.
    let mut event = None;
    let mut id = None;
    let mut data = Vec::new();
    for line in text.lines() {
        if line.is_empty() || line.starts_with(':') {
            continue;
        }
        let (name, value) = line.split_once(':').map_or((line, ""), |(name, value)| {
            (name, value.strip_prefix(' ').unwrap_or(value))
        });
        match name {
            "event" => event = Some(value),
            "id" => id = Some(value),
            "data" => data.push(value),
            _ => {}
        }
    }
    if data.is_empty() {
        return Ok(None);
    }
    // Single data lines dominate; join only multi-line payloads.
    let data = match data.as_slice() {
        [single] => (*single).to_owned(),
        lines => lines.join("\n"),
    };
    Ok(Some(ParsedSseEvent {
        event: event.unwrap_or("message").to_owned(),
        data,
        id: id.map(str::to_owned),
    }))
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct ParsedSseResumeCursor {
    next_seq_num: u64,
    consumed_records: u64,
    snapshot: Option<(u64, u64)>,
}

fn sse_resume_event_id(event: &ParsedSseEvent) -> Result<&str, TsfClientError> {
    Ok(sse_resume_cursor(event)?.0)
}

fn sse_resume_cursor(
    event: &ParsedSseEvent,
) -> Result<(&str, ParsedSseResumeCursor), TsfClientError> {
    let Some(id) = event.id.as_deref() else {
        return Err(invalid_sse_resume_cursor());
    };
    Ok((id, parse_sse_resume_cursor(id)?))
}

fn parse_sse_resume_cursor(id: &str) -> Result<ParsedSseResumeCursor, TsfClientError> {
    let mut fields = id.split(',');
    if fields.next() != Some("v1") {
        return Err(invalid_sse_resume_cursor());
    }
    let Some(next_seq_num) = fields.next().and_then(parse_sse_cursor_u64) else {
        return Err(invalid_sse_resume_cursor());
    };
    let Some(consumed_count) = fields.next().and_then(parse_sse_cursor_u64) else {
        return Err(invalid_sse_resume_cursor());
    };
    let snapshot = match (fields.next(), fields.next()) {
        (None, None) => None,
        (Some(next), Some(timestamp)) => Some((
            parse_sse_cursor_u64(next).ok_or_else(invalid_sse_resume_cursor)?,
            parse_sse_cursor_u64(timestamp).ok_or_else(invalid_sse_resume_cursor)?,
        )),
        _ => return Err(invalid_sse_resume_cursor()),
    };
    if fields.next().is_some()
        || next_seq_num > MAX_READ_SELECTOR_VALUE
        || consumed_count > next_seq_num
        || snapshot.is_some_and(|(snapshot_end_seq_num, snapshot_last_timestamp_ms)| {
            snapshot_end_seq_num > MAX_READ_SELECTOR_VALUE
                || next_seq_num > snapshot_end_seq_num
                || snapshot_last_timestamp_ms > MAX_READ_SELECTOR_VALUE
                || (snapshot_end_seq_num == 0 && snapshot_last_timestamp_ms != 0)
        })
    {
        return Err(invalid_sse_resume_cursor());
    }
    Ok(ParsedSseResumeCursor {
        next_seq_num,
        consumed_records: consumed_count,
        snapshot,
    })
}

fn parse_sse_cursor_u64(value: &str) -> Option<u64> {
    if value.is_empty()
        || (value != "0" && value.starts_with('0'))
        || !value.bytes().all(|byte| byte.is_ascii_digit())
    {
        return None;
    }
    value.parse().ok()
}

fn invalid_sse_resume_cursor() -> TsfClientError {
    TsfClientError::InvalidSse("SSE event does not carry a valid resume cursor")
}

/// Per-byte JSON escaped length matching serde_json's ESCAPE table: '"', '\\', and the
/// five short-form controls take two bytes, the rest of C0 takes six (\u00XX), and every
/// other byte passes through unescaped.
const JSON_ESCAPED_LEN: [u8; 256] = {
    let mut table = [1u8; 256];
    let mut control = 0;
    while control < 0x20 {
        table[control] = 6;
        control += 1;
    }
    table[b'"' as usize] = 2;
    table[b'\\' as usize] = 2;
    table[b'\x08' as usize] = 2; // \b
    table[b'\t' as usize] = 2;
    table[b'\n' as usize] = 2;
    table[b'\x0C' as usize] = 2; // \f
    table[b'\r' as usize] = 2;
    table
};

fn compact_record_data(bytes: &[u8]) -> RecordData {
    let Ok(text) = std::str::from_utf8(bytes) else {
        return RecordData::Base64url(URL_SAFE_NO_PAD.encode(bytes));
    };
    let escaped_len = bytes.iter().fold(0usize, |total, byte| {
        total + JSON_ESCAPED_LEN[*byte as usize] as usize
    });
    let utf8_len = br#"{"encoding":"utf8","value":""}"#.len() + escaped_len;
    let base64url_len =
        br#"{"encoding":"base64url","value":""}"#.len() + bytes.len().saturating_mul(4).div_ceil(3);
    if utf8_len <= base64url_len {
        RecordData::Utf8(text.to_owned())
    } else {
        RecordData::Base64url(URL_SAFE_NO_PAD.encode(bytes))
    }
}

fn sse_read_batch(batch: SseReadBatchData) -> Result<ReadBatch, TsfClientError> {
    // Unpadded base64url packs 3 bytes into 4 chars, so decoded payload sizes are exact here.
    let payload_len: usize = batch
        .records
        .iter()
        .map(|record| match &record.data {
            RecordData::Utf8(value) => value.len(),
            RecordData::Base64url(value) => value.len() * 3 / 4,
        })
        .sum();
    let mut payload = BytesMut::with_capacity(payload_len);
    let mut records = Vec::with_capacity(batch.records.len());
    for record in batch.records {
        let mut writer = [0u8; WriterId::BYTE_LEN];
        let decoded_len = URL_SAFE_NO_PAD
            .decode_slice(record.writer_id, &mut writer)
            .map_err(|_| TsfClientError::InvalidSse("invalid writer_id"))?;
        if decoded_len != WriterId::BYTE_LEN {
            return Err(TsfClientError::InvalidSse("invalid writer_id length"));
        }
        let data = match record.data {
            RecordData::Utf8(value) => value.into_bytes(),
            RecordData::Base64url(value) => URL_SAFE_NO_PAD
                .decode(value)
                .map_err(|_| TsfClientError::InvalidSse("invalid record base64url"))?,
        };
        if data.len() > MAX_RECORD_BYTES {
            return Err(TsfClientError::InvalidSse(
                "read_batch contains an oversized record",
            ));
        }
        let part = PartHeader::new(record.part.index, record.part.is_final)?;
        let data_start = payload.len() as u32;
        payload.extend_from_slice(&data);
        records.push(RecordMeta {
            seq_num: record.seq_num,
            timestamp_ms: record.timestamp_ms,
            writer_id: WriterId::from_bytes(writer),
            writer_seq_num: record.writer_seq_num,
            part,
            format: record.format,
            data_start,
            data_len: data.len() as u32,
        });
    }
    Ok(ReadBatch::from_parts(payload.freeze(), records))
}

fn validate_sse_read_batch(
    batch: &ReadBatch,
    options: &ReadStreamOptions,
) -> Result<(), TsfClientError> {
    let mut payload_bytes = 0_usize;
    let mut previous_seq_num = None;
    for record in batch {
        payload_bytes = payload_bytes.saturating_add(record.data.len());
        if payload_bytes > MAX_SSE_READ_BATCH_PAYLOAD_BYTES {
            return Err(TsfClientError::InvalidSse(
                "read_batch exceeds the decoded payload limit",
            ));
        }
        if previous_seq_num
            .is_some_and(|previous: u64| previous.checked_add(1) != Some(record.seq_num))
        {
            return Err(TsfClientError::InvalidSse(
                "read_batch sequence numbers are not contiguous",
            ));
        }
        if options
            .end_seq_num
            .is_some_and(|end_seq_num| record.seq_num >= end_seq_num)
        {
            return Err(TsfClientError::InvalidSse(
                "read_batch crosses the requested end sequence",
            ));
        }
        previous_seq_num = Some(record.seq_num);
    }
    if options
        .limit
        .is_some_and(|remaining| batch.len() as u64 > remaining)
    {
        return Err(TsfClientError::InvalidSse(
            "read_batch exceeds the remaining record limit",
        ));
    }
    Ok(())
}

fn validate_sse_read_batch_count(record_count: usize) -> Result<(), TsfClientError> {
    if record_count == 0 || record_count > MAX_SSE_READ_BATCH_RECORDS {
        return Err(TsfClientError::InvalidSse(
            "read_batch record count is outside the protocol limit",
        ));
    }
    Ok(())
}

fn validate_sse_read_batch_cursor(
    batch: &ReadBatch,
    cursor: ParsedSseResumeCursor,
    previous: Option<ParsedSseResumeCursor>,
    options: &ReadStreamOptions,
    snapshot_boundary: Option<SnapshotBoundary>,
) -> Result<(), TsfClientError> {
    let Some(first) = batch.first() else {
        return Err(TsfClientError::InvalidSse("read_batch is empty"));
    };
    let Some(expected_next_seq_num) = batch
        .last()
        .and_then(|record| record.seq_num.checked_add(1))
    else {
        return Err(TsfClientError::InvalidSse(
            "read_batch cursor cannot follow its records",
        ));
    };
    if cursor.next_seq_num != expected_next_seq_num {
        return Err(TsfClientError::InvalidSse(
            "read_batch cursor does not follow its records",
        ));
    }
    if previous.is_some_and(|value| first.seq_num != value.next_seq_num) {
        return Err(TsfClientError::InvalidSse(
            "read_batch does not resume at the previous cursor",
        ));
    }
    if previous.is_none()
        && matches!(options.start, Some(ReadStart::SeqNum(start)) if first.seq_num != start)
    {
        return Err(TsfClientError::InvalidSse(
            "read_batch does not begin at the requested sequence",
        ));
    }
    if previous.is_none()
        && matches!(options.start, Some(ReadStart::TimestampMs(start)) if first.timestamp_ms < start)
    {
        return Err(TsfClientError::InvalidSse(
            "read_batch begins before the requested timestamp",
        ));
    }
    let expected_consumed = previous
        .map_or(0, |value| value.consumed_records)
        .checked_add(batch.len() as u64)
        .ok_or(TsfClientError::InvalidSse(
            "read_batch consumed count overflowed",
        ))?;
    if cursor.consumed_records != expected_consumed {
        return Err(TsfClientError::InvalidSse(
            "read_batch cursor has the wrong consumed count",
        ));
    }
    validate_sse_cursor_boundary(cursor, previous, snapshot_boundary)
}

fn validate_sse_caught_up_cursor(
    caught_up: CaughtUpPosition,
    cursor: ParsedSseResumeCursor,
    previous: Option<ParsedSseResumeCursor>,
    options: &ReadStreamOptions,
    snapshot_boundary: Option<SnapshotBoundary>,
) -> Result<(), TsfClientError> {
    if cursor.next_seq_num != caught_up.next_seq_num {
        return Err(TsfClientError::InvalidSse(
            "caught_up cursor does not match its position",
        ));
    }
    if let Some(previous) = previous {
        if cursor.next_seq_num != previous.next_seq_num
            || cursor.consumed_records != previous.consumed_records
        {
            return Err(TsfClientError::InvalidSse(
                "caught_up does not continue the previous cursor",
            ));
        }
    } else if cursor.consumed_records != 0 {
        return Err(TsfClientError::InvalidSse(
            "initial caught_up cursor has a consumed count",
        ));
    }
    if previous.is_none()
        && matches!(options.start, Some(ReadStart::SeqNum(start)) if cursor.next_seq_num != start)
    {
        return Err(TsfClientError::InvalidSse(
            "initial caught_up does not match the requested sequence",
        ));
    }
    validate_sse_cursor_boundary(cursor, previous, snapshot_boundary)
}

fn validate_sse_snapshot_cursor(
    boundary: SnapshotBoundary,
    cursor: ParsedSseResumeCursor,
    previous: Option<ParsedSseResumeCursor>,
) -> Result<(), TsfClientError> {
    if cursor.snapshot != Some((boundary.end_seq_num, boundary.last_timestamp_ms)) {
        return Err(TsfClientError::InvalidSse(
            "snapshot_boundary cursor does not match its boundary",
        ));
    }
    if let Some(previous) = previous {
        if cursor.next_seq_num != previous.next_seq_num
            || cursor.consumed_records != previous.consumed_records
        {
            return Err(TsfClientError::InvalidSse(
                "snapshot_boundary does not continue the previous cursor",
            ));
        }
    } else if cursor.consumed_records != 0 {
        return Err(TsfClientError::InvalidSse(
            "initial snapshot_boundary cursor has a consumed count",
        ));
    }
    validate_sse_cursor_boundary(cursor, previous, Some(boundary))
}

fn validate_sse_cursor_boundary(
    cursor: ParsedSseResumeCursor,
    previous: Option<ParsedSseResumeCursor>,
    boundary: Option<SnapshotBoundary>,
) -> Result<(), TsfClientError> {
    let expected_snapshot = boundary.map(|value| (value.end_seq_num, value.last_timestamp_ms));
    if cursor.snapshot != expected_snapshot
        || previous.is_some_and(|value| cursor.snapshot != value.snapshot)
    {
        return Err(TsfClientError::InvalidSse(
            "SSE resume cursor changed its snapshot boundary",
        ));
    }
    Ok(())
}

async fn json_response<T: DeserializeOwned>(
    response: reqwest::Response,
    operation: &'static str,
) -> Result<T, TsfClientError> {
    let status = response.status();
    if !status.is_success() {
        return Err(http_status_error(response, operation).await);
    }

    let body = bounded_response_body(response, operation, MAX_REST_RESPONSE_BYTES).await?;
    Ok(serde_json::from_slice(&body)?)
}

async fn http_status_error(response: reqwest::Response, operation: &'static str) -> TsfClientError {
    let status = response.status();
    let header_request_id = response
        .headers()
        .get("x-request-id")
        .and_then(|value| value.to_str().ok())
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(str::to_owned);
    let header_retry_after = response
        .headers()
        .get("retry-after")
        .and_then(|value| value.to_str().ok())
        .and_then(parse_retry_after);
    let raw = bounded_response_body(response, operation, MAX_REST_ERROR_RESPONSE_BYTES)
        .await
        .unwrap_or_default();
    let parsed = serde_json::from_slice::<ApiErrorResponse>(&raw).ok();
    let request_id = header_request_id.or_else(|| {
        parsed
            .as_ref()
            .map(|response| response.error.request_id.as_str())
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(str::to_owned)
    });
    let retry_after = header_retry_after.or_else(|| {
        parsed
            .as_ref()
            .and_then(|response| response.error.retry_after_ms)
            .map(Duration::from_millis)
    });
    let actual_next_seq_num = parsed
        .as_ref()
        .and_then(|response| response.error.actual_next_seq_num);
    let api_code = parsed
        .as_ref()
        .map(|response| response.error.code.clone())
        .filter(|value| !value.is_empty());
    let raw = String::from_utf8(raw).unwrap_or_default();
    let body = api_error_message(&raw).unwrap_or(raw);
    TsfClientError::HttpStatus {
        operation,
        status,
        body,
        api_code,
        request_id,
        retry_after,
        actual_next_seq_num,
    }
}

async fn bounded_response_body(
    response: reqwest::Response,
    operation: &'static str,
    maximum_bytes: usize,
) -> Result<Vec<u8>, TsfClientError> {
    if response
        .content_length()
        .is_some_and(|length| length > maximum_bytes as u64)
    {
        return Err(TsfClientError::ResponseTooLarge {
            operation,
            maximum_bytes,
        });
    }

    let mut body = Vec::new();
    let mut chunks = response.bytes_stream();
    while let Some(chunk) = chunks.next().await {
        let chunk = chunk?;
        if chunk.len() > maximum_bytes.saturating_sub(body.len()) {
            return Err(TsfClientError::ResponseTooLarge {
                operation,
                maximum_bytes,
            });
        }
        body.extend_from_slice(&chunk);
    }
    Ok(body)
}

fn parse_retry_after(value: &str) -> Option<Duration> {
    value.trim().parse::<u64>().ok().map(Duration::from_secs)
}

fn api_error_message(body: &str) -> Option<String> {
    let response: serde_json::Value = serde_json::from_str(body).ok()?;
    let code = response["error"]["code"].as_str()?.trim();
    let message = response["error"]["message"].as_str()?.trim();

    match (code.is_empty(), message.is_empty()) {
        (true, true) => None,
        (true, false) => Some(message.to_owned()),
        (false, true) => Some(code.to_owned()),
        (false, false) => Some(format!("{code}: {message}")),
    }
}

async fn next_server_frame(
    ws: &mut ClientWebSocket,
) -> Result<Option<ServerFrame>, TsfClientError> {
    loop {
        let Some(message) = ws.next().await else {
            return Ok(None);
        };

        match message? {
            Message::Binary(bytes) => return Ok(Some(ServerFrame::decode_bytes(bytes)?)),
            Message::Close(Some(close)) if u16::from(close.code) == 1000 => return Ok(None),
            Message::Close(Some(close)) => {
                return Err(TsfClientError::WebSocketClosedWithReason {
                    code: u16::from(close.code),
                    reason: close.reason.to_string(),
                });
            }
            Message::Close(None) => return Ok(None),
            Message::Ping(_) | Message::Pong(_) => {}
            Message::Text(_) => return Err(TsfClientError::UnexpectedTextMessage),
            Message::Frame(_) => {}
        }
    }
}

async fn next_read_socket_frame(
    ws: &mut ClientWebSocket,
) -> Result<Option<ReadSocketOutcome>, TsfClientError> {
    match next_server_frame(ws).await? {
        Some(ServerFrame::ReadBatch(records)) => Ok(Some(ReadSocketOutcome::Records(records))),
        Some(ServerFrame::CaughtUp(caught_up)) => Ok(Some(ReadSocketOutcome::CaughtUp(caught_up))),
        Some(ServerFrame::Heartbeat) => Ok(None),
        Some(frame) => Err(TsfClientError::UnexpectedServerFrame(server_frame_name(
            &frame,
        ))),
        None => Ok(Some(ReadSocketOutcome::Closed)),
    }
}

async fn expect_ready(ws: &mut ClientWebSocket) -> Result<(), TsfClientError> {
    match next_server_frame(ws).await? {
        Some(ServerFrame::Ready) => Ok(()),
        Some(frame) => Err(TsfClientError::UnexpectedServerFrame(server_frame_name(
            &frame,
        ))),
        None => Err(TsfClientError::WebSocketClosed),
    }
}

struct ReadHandshake {
    stream_metadata: StreamMetadata,
    snapshot_boundary: Option<SnapshotBoundary>,
}

async fn expect_read_handshake(
    ws: &mut ClientWebSocket,
    snapshot: bool,
) -> Result<ReadHandshake, TsfClientError> {
    expect_ready(ws).await?;
    let stream_metadata = match next_server_frame(ws).await? {
        Some(ServerFrame::StreamMetadata(stream_metadata)) => stream_metadata,
        Some(frame) => {
            return Err(TsfClientError::UnexpectedServerFrame(server_frame_name(
                &frame,
            )));
        }
        None => return Err(TsfClientError::WebSocketClosed),
    };
    let snapshot_boundary = if snapshot {
        match next_server_frame(ws).await? {
            Some(ServerFrame::SnapshotBoundary(boundary)) => Some(boundary),
            Some(frame) => {
                return Err(TsfClientError::UnexpectedServerFrame(server_frame_name(
                    &frame,
                )));
            }
            None => return Err(TsfClientError::WebSocketClosed),
        }
    } else {
        None
    };
    Ok(ReadHandshake {
        stream_metadata,
        snapshot_boundary,
    })
}

fn server_frame_name(frame: &ServerFrame) -> &'static str {
    match frame {
        ServerFrame::Ready => "ready",
        ServerFrame::AppendAck { .. } => "append_ack",
        ServerFrame::ReadBatch(_) => "read_batch",
        ServerFrame::Heartbeat => "heartbeat",
        ServerFrame::CaughtUp(_) => "caught_up",
        ServerFrame::StreamMetadata(_) => "stream_metadata",
        ServerFrame::SnapshotBoundary(_) => "snapshot_boundary",
    }
}

fn validate_api_origin(origin: &Url) -> Result<(), TsfClientError> {
    if !matches!(origin.scheme(), "http" | "https")
        || origin.host_str().is_none()
        || !origin.username().is_empty()
        || origin.password().is_some()
        || origin.path() != "/"
        || origin.query().is_some()
        || origin.fragment().is_some()
    {
        return Err(TsfClientError::InvalidApiOrigin(origin.clone()));
    }
    Ok(())
}

fn validate_link_page(
    page: &ListLinksResponse,
    maximum_links: usize,
) -> Result<(), TsfClientError> {
    if page.links.len() > maximum_links {
        return Err(TsfClientError::InvalidLinkPage(
            "page contains more links than requested",
        ));
    }
    if page.next_cursor.is_some() && page.links.is_empty() {
        return Err(TsfClientError::InvalidLinkPage(
            "empty page carries a next cursor",
        ));
    }
    let mut link_ids = HashSet::with_capacity(page.links.len());
    if page
        .links
        .iter()
        .any(|link| !link_ids.insert(&link.link_id))
    {
        return Err(TsfClientError::InvalidLinkPage(
            "page contains duplicate link IDs",
        ));
    }
    Ok(())
}

fn validate_client_config(config: &TsfClientConfig) -> Result<(), TsfClientError> {
    validate_api_origin(&config.api_origin)?;
    for (name, value) in [
        ("rest_request_timeout", config.rest_request_timeout),
        (
            "websocket_connect_timeout",
            config.websocket_connect_timeout,
        ),
        (
            "websocket_operation_timeout",
            config.websocket_operation_timeout,
        ),
    ] {
        if value.is_zero() || value > MAX_CLIENT_DELAY {
            return Err(TsfClientError::InvalidClientConfig(format!(
                "{name} must be greater than zero and at most {} milliseconds",
                MAX_CLIENT_DELAY.as_millis()
            )));
        }
    }
    if config
        .websocket_read_idle_timeout
        .is_some_and(|timeout| timeout.is_zero() || timeout > MAX_CLIENT_DELAY)
    {
        return Err(TsfClientError::InvalidClientConfig(format!(
            "websocket_read_idle_timeout must be greater than zero and at most {} milliseconds when set",
            MAX_CLIENT_DELAY.as_millis()
        )));
    }
    if config.retry_policy.max_attempts == 0 {
        return Err(TsfClientError::InvalidClientConfig(
            "retry_policy.max_attempts must be at least one".to_owned(),
        ));
    }
    if config.retry_policy.initial_backoff > config.retry_policy.max_backoff {
        return Err(TsfClientError::InvalidClientConfig(
            "retry_policy.initial_backoff must not exceed retry_policy.max_backoff".to_owned(),
        ));
    }
    if config.retry_policy.max_backoff > MAX_CLIENT_DELAY {
        return Err(TsfClientError::InvalidClientConfig(format!(
            "retry_policy delays must not exceed {} milliseconds",
            MAX_CLIENT_DELAY.as_millis()
        )));
    }
    Ok(())
}

/// Error surfaced by REST operations, socket setup, reads, and durable writers.
#[derive(Debug, thiserror::Error)]
pub enum TsfClientError {
    /// The configured API origin is not a bare HTTP or HTTPS origin.
    #[error("API origin must be HTTP(S) without credentials, path, query, or fragment: {0}")]
    InvalidApiOrigin(Url),
    /// Client timeout or retry settings are incoherent.
    #[error("invalid client config: {0}")]
    InvalidClientConfig(String),
    /// HTTP transport or response-decoding failure.
    #[error("HTTP client error: {0}")]
    Http(#[from] reqwest::Error),
    /// A REST response contained malformed JSON.
    #[error("invalid JSON in REST response: {0}")]
    Json(#[from] serde_json::Error),
    /// A REST response exceeded the SDK memory-safety bound.
    #[error("{operation} response exceeds {maximum_bytes} bytes")]
    ResponseTooLarge {
        /// Stable operation label.
        operation: &'static str,
        /// Maximum bytes buffered by the SDK.
        maximum_bytes: usize,
    },
    /// Non-success HTTP response.
    #[error("HTTP {operation} failed with {status}: {body}")]
    HttpStatus {
        /// Stable operation label.
        operation: &'static str,
        /// Returned HTTP status.
        status: StatusCode,
        /// Parsed API error or fallback response body.
        body: String,
        /// Stable API error code when the response was JSON.
        api_code: Option<String>,
        /// Server request ID used for support and tracing.
        request_id: Option<String>,
        /// Server-requested retry delay.
        retry_after: Option<Duration>,
        /// Actual stream next sequence for a failed sequence precondition.
        actual_next_seq_num: Option<u64>,
    },
    /// Stateless append input violates the local protocol contract.
    #[error("invalid stateless append: {0}")]
    InvalidStatelessAppend(&'static str),
    /// A link secret is not a canonical 32-byte unpadded base64url value.
    #[error("link secret must be canonical 43-character unpadded base64url")]
    InvalidLinkSecret,
    /// SSE response violated the public event contract.
    #[error("invalid SSE response: {0}")]
    InvalidSse(&'static str),
    /// WebSocket read response violated the requested stream contract.
    #[error("invalid WebSocket read response: {0}")]
    InvalidReadResponse(&'static str),
    /// The server ended an SSE session with a stable terminal error event.
    #[error("SSE terminal error: {0}")]
    SseTerminal(String),
    /// A bounded client operation exceeded its timeout.
    #[error("{operation} timed out")]
    Timeout {
        /// Stable operation label.
        operation: &'static str,
    },
    /// WebSocket transport, TLS, handshake, or protocol failure.
    #[error("WebSocket error: {0}")]
    WebSocket(#[from] tokio_tungstenite::tungstenite::Error),
    /// TSF binary frame encoding or decoding failure.
    #[error("frame codec error: {0}")]
    Frame(#[from] FrameCodecError),
    /// The configured API URL cannot map to `ws` or `wss`.
    #[error("cannot derive WebSocket URL from scheme {0:?}")]
    InvalidWebSocketScheme(String),
    /// The server returned a non-text WebSocket protocol header.
    #[error("server selected invalid WebSocket protocol header")]
    InvalidWebSocketProtocolHeader,
    /// The server did not select `tsf.v1` during upgrade.
    #[error("server selected unsupported WebSocket protocol {0:?}")]
    UnexpectedWebSocketProtocol(Option<String>),
    /// The server closed without a non-normal close reason.
    #[error("server closed the WebSocket")]
    WebSocketClosed,
    /// The server sent a non-normal close code and reason.
    #[error("server closed the WebSocket with code {code}: {reason}")]
    WebSocketClosedWithReason {
        /// WebSocket close code.
        code: u16,
        /// Stable server close reason when available.
        reason: String,
    },
    /// The stream did not start the writer session at its requested sequence.
    #[error("stream next sequence did not match the writer session precondition")]
    SequenceMismatch,
    /// Link-list pagination controls are outside the supported range.
    #[error("invalid list links options: {0}")]
    InvalidListLinksOptions(&'static str),
    /// A link inventory page violated pagination invariants.
    #[error("invalid link page: {0}")]
    InvalidLinkPage(&'static str),
    /// The server returned an invalid or mismatched ack range.
    #[error("server sent invalid append acknowledgement {0:?}")]
    InvalidAppendAck(AppendAck),
    /// The server returned a stateless append range with the wrong length.
    #[error("server sent invalid stateless append range {0:?}")]
    InvalidAppendRange(AppendRange),
    /// An ack skipped a pending writer-local sequence number.
    #[error("server acknowledgement advanced past writer seq {writer_seq_num}: {ack:?}")]
    AppendNotAcknowledged {
        /// Pending writer-local sequence number omitted by the ack.
        writer_seq_num: u64,
        /// Invalid ack that advanced past the pending record.
        ack: AppendAck,
    },
    /// Writer bounds are zero or not representable by the semaphore implementation.
    #[error("invalid append writer config: {0}")]
    InvalidWriterConfig(String),
    /// A requested reservation is larger than the entire writer byte window.
    #[error("append record reserves {bytes} bytes, above writer window {max_unacked_bytes}")]
    AppendRecordExceedsWriterWindow {
        /// Requested reservation size.
        bytes: usize,
        /// Configured writer byte window.
        max_unacked_bytes: usize,
    },
    /// A record is larger than its previously acquired reservation.
    #[error("append record uses {bytes} bytes, above reserved capacity {reserved_bytes}")]
    AppendRecordExceedsReservedBytes {
        /// Actual record accounting size.
        bytes: usize,
        /// Capacity owned by the permit.
        reserved_bytes: usize,
    },
    /// The writer command channel is closed.
    #[error("append writer is closed")]
    AppendWriterClosed,
    /// The writer task ended before resolving a pending ticket.
    #[error("append writer dropped with unacknowledged records")]
    AppendWriterDropped,
    /// The writer background task failed or could not be joined.
    #[error("append writer failed: {0}")]
    AppendWriterFailed(String),
    /// Consecutive read connections ended without delivering a record batch or caught-up event.
    #[error(
        "read stream delivered no record batch or caught-up event across {max_connection_attempts} consecutive connection attempts"
    )]
    ReadReconnectLimitExceeded {
        /// Configured maximum consecutive connection attempts, including the initial connection.
        max_connection_attempts: usize,
    },
    /// A selector exceeds the range supported by the active data adapter.
    #[error("read selector {value} exceeds the supported maximum {maximum}")]
    InvalidReadSelector {
        /// Requested selector value.
        value: u64,
        /// Largest supported selector value.
        maximum: u64,
    },
    /// A timestamp playback rate is outside the protocol range.
    #[error("playback rate {value} must be between {minimum} and {maximum} permille")]
    InvalidPlaybackRate {
        /// Requested playback rate.
        value: u64,
        /// Slowest accepted playback rate.
        minimum: u64,
        /// Fastest accepted playback rate.
        maximum: u64,
    },
    /// Timestamp playback needs a stable exclusive ending sequence.
    #[error("playback rate requires an exclusive end_seq_num sequence")]
    PlaybackRequiresEnd,
    /// A snapshot request also supplied an explicit ending sequence.
    #[error("snapshot and end_seq_num are mutually exclusive")]
    SnapshotWithEnd,
    /// The service sent a valid TSF frame that is not allowed at this protocol state.
    #[error("server sent unexpected {0} frame")]
    UnexpectedServerFrame(&'static str),
    /// The server sent a text WebSocket message instead of one binary TSF frame.
    #[error("server sent an unexpected text WebSocket message")]
    UnexpectedTextMessage,
}

impl TsfClientError {
    /// Returns the request ID attached to an HTTP failure.
    pub fn request_id(&self) -> Option<&str> {
        match self {
            Self::HttpStatus { request_id, .. } => request_id.as_deref(),
            _ => None,
        }
    }

    /// Returns the stable API code attached to an HTTP failure.
    pub fn api_code(&self) -> Option<&str> {
        match self {
            Self::HttpStatus { api_code, .. } => api_code.as_deref(),
            _ => None,
        }
    }

    /// Returns the server-requested retry delay.
    pub fn retry_after(&self) -> Option<Duration> {
        match self {
            Self::HttpStatus { retry_after, .. } => *retry_after,
            _ => None,
        }
    }

    /// Returns the actual stream next sequence attached to a failed sequence precondition.
    pub fn actual_next_seq_num(&self) -> Option<u64> {
        match self {
            Self::HttpStatus {
                actual_next_seq_num,
                ..
            } => *actual_next_seq_num,
            _ => None,
        }
    }
    /// Returns whether retrying a failed create with the same idempotency key and request is safe
    /// and may succeed.
    pub fn is_recoverable_create_failure(&self) -> bool {
        match self {
            Self::Http(error) => {
                error.is_timeout() || error.is_connect() || error.is_body() || error.is_decode()
            }
            Self::Json(_) => true,
            Self::HttpStatus { status, .. } => is_retryable_http_status(status.as_u16()),
            _ => false,
        }
    }

    fn is_retryable(&self) -> bool {
        if self.is_resumable_read_interruption() {
            return true;
        }
        match self {
            Self::Http(error) => error.is_timeout() || error.is_connect(),
            Self::HttpStatus { status, .. } => is_retryable_http_status(status.as_u16()),
            _ => false,
        }
    }

    fn is_resumable_read_interruption(&self) -> bool {
        match self {
            Self::Timeout { .. } => true,
            Self::WebSocket(error) => is_retryable_websocket_error(error),
            Self::WebSocketClosed => true,
            Self::WebSocketClosedWithReason { code, .. } => is_retryable_close_code(*code),
            _ => false,
        }
    }

    fn is_resumable_sse_interruption(&self) -> bool {
        match self {
            Self::Http(error) => {
                error.is_timeout() || error.is_connect() || error.is_body() || error.is_decode()
            }
            Self::HttpStatus { status, .. } => is_retryable_http_status(status.as_u16()),
            Self::Timeout { .. } => true,
            _ => false,
        }
    }
}

fn is_retryable_close_code(code: u16) -> bool {
    matches!(code, 1000 | 1001 | 1005 | 1006 | 1011..=1015)
}

fn is_retryable_http_status(status: u16) -> bool {
    matches!(status, 408 | 425 | 429 | 500 | 502 | 503 | 504)
}

fn is_retryable_websocket_error(error: &WebSocketError) -> bool {
    match error {
        WebSocketError::ConnectionClosed
        | WebSocketError::Io(_)
        | WebSocketError::Tls(_)
        | WebSocketError::WriteBufferFull(_) => true,
        WebSocketError::Protocol(ProtocolError::ResetWithoutClosingHandshake) => true,
        WebSocketError::Http(response) => is_retryable_http_status(response.status().as_u16()),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio_tungstenite::connect_async;

    use super::*;
    use crate::protocol::{rest::SseReadRecord, ws::frame::OwnedReadRecord};

    #[test]
    fn parses_structured_http_error_details() {
        let body = r#"{"error":{"code":"sequence_mismatch","message":"position changed","request_id":"request-42","retry_after_ms":125,"actual_next_seq_num":"42","future_field":true}}"#;
        let parsed: ApiErrorResponse = serde_json::from_str(body).expect("structured API error");

        assert_eq!(
            api_error_message(body).as_deref(),
            Some("sequence_mismatch: position changed")
        );
        assert_eq!(parsed.error.request_id, "request-42");
        assert_eq!(parsed.error.retry_after_ms, Some(125));
        assert_eq!(parsed.error.actual_next_seq_num, Some(42));
        for invalid in ["", "00", "01", "-1", "18446744073709551616"] {
            let body = format!(
                r#"{{"error":{{"code":"sequence_mismatch","message":"position changed","request_id":"request-42","actual_next_seq_num":"{invalid}"}}}}"#,
            );
            assert!(serde_json::from_str::<ApiErrorResponse>(&body).is_err());
        }
        assert_eq!(api_error_message("plain failure"), None);
    }

    #[tokio::test]
    async fn sse_handshake_uses_the_rest_request_timeout() {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("bind SSE listener");
        let address = listener.local_addr().expect("SSE listener address");
        let server = tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.expect("accept SSE request");
            let mut request = [0_u8; 4096];
            let _ = stream.read(&mut request).await.expect("read SSE request");
            stream
                .write_all(
                    b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n",
                )
                .await
                .expect("write SSE headers");
            sleep(Duration::from_secs(1)).await;
        });
        let mut config =
            TsfClientConfig::new(Url::parse(&format!("http://{address}")).expect("SSE API origin"))
                .expect("valid client config");
        config.rest_request_timeout = Duration::from_millis(20);
        config.retry_policy = RetryPolicy::none();
        let client = TsfClient::with_config(config).expect("SSE client");
        let stream_id = "00000000000000000000000000000000"
            .parse()
            .expect("stream ID");

        let started_at = std::time::Instant::now();
        let result = client
            .connect_sse_reader(ReadStreamOptions::new(stream_id))
            .await;

        assert!(matches!(
            result,
            Err(TsfClientError::Timeout {
                operation: "SSE handshake"
            })
        ));
        assert!(started_at.elapsed() < Duration::from_millis(200));
        server.abort();
        let _ = server.await;
    }

    #[tokio::test]
    async fn rest_response_rejects_declared_body_above_memory_bound() {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("bind REST listener");
        let address = listener.local_addr().expect("REST listener address");
        let server = tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.expect("accept REST request");
            let mut request = [0_u8; 4096];
            let _ = stream.read(&mut request).await.expect("read REST request");
            let response = format!(
                "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                MAX_REST_RESPONSE_BYTES + 1
            );
            stream
                .write_all(response.as_bytes())
                .await
                .expect("write REST headers");
        });
        let mut config = TsfClientConfig::new(
            Url::parse(&format!("http://{address}")).expect("REST API origin"),
        )
        .expect("valid client config");
        config.retry_policy = RetryPolicy::none();
        let client = TsfClient::with_config(config).expect("REST client");
        let stream_id = "00000000000000000000000000000000"
            .parse()
            .expect("stream ID");

        let result = client.get_stream(&stream_id, None).await;

        assert!(matches!(
            result,
            Err(TsfClientError::ResponseTooLarge {
                operation: "get stream",
                maximum_bytes: MAX_REST_RESPONSE_BYTES,
            })
        ));
        server.await.expect("join REST server");
    }

    async fn connected_websockets() -> (ClientWebSocket, WebSocketStream<TcpStream>) {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("bind WebSocket listener");
        let address = listener.local_addr().expect("WebSocket listener address");
        let server = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("accept WebSocket client");
            tokio_tungstenite::accept_async(stream)
                .await
                .expect("accept WebSocket handshake")
        });
        let (client, _) = connect_async(format!("ws://{address}"))
            .await
            .expect("connect WebSocket client");

        (client, server.await.expect("join WebSocket server"))
    }

    #[test]
    fn create_idempotency_keys_validate_and_redact_debug_output() {
        let key = CreateStreamIdempotencyKey::new_random();
        let exposed = key.expose_secret().to_owned();

        assert!(is_canonical_base64url_32(&exposed));
        assert_eq!(
            exposed
                .parse::<CreateStreamIdempotencyKey>()
                .expect("canonical key")
                .expose_secret(),
            exposed
        );
        assert!(!format!("{key:?}").contains(&exposed));
        assert!(matches!(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".parse::<CreateStreamIdempotencyKey>(),
            Err(InvalidCreateStreamIdempotencyKey)
        ));
    }

    #[tokio::test]
    async fn read_handshake_returns_metadata() {
        let (mut client, mut server) = connected_websockets().await;
        let stream_metadata = StreamMetadata {
            stream_id: "00000000000000000000000000000000"
                .parse()
                .expect("stream ID"),
            title: None,
            visibility: crate::protocol::rest::Visibility::Private,
            created_at: "2026-08-13T00:00:00Z".to_owned(),
            expires_at: "2026-08-23T00:00:00Z".to_owned(),
        };
        let expected_stream_metadata = stream_metadata.clone();
        let sender = tokio::spawn(async move {
            for frame in [
                ServerFrame::Ready,
                ServerFrame::StreamMetadata(stream_metadata),
            ] {
                server
                    .send(Message::Binary(
                        frame.encode().expect("encode handshake frame"),
                    ))
                    .await
                    .expect("send handshake frame");
            }
        });

        let handshake = expect_read_handshake(&mut client, false)
            .await
            .expect("read handshake");

        assert_eq!(handshake.stream_metadata, expected_stream_metadata);
        assert_eq!(handshake.snapshot_boundary, None);
        sender.await.expect("join handshake sender");
    }

    #[tokio::test]
    async fn read_idle_timeout_resets_on_protocol_heartbeat() {
        let (client, mut server) = connected_websockets().await;
        let sender = tokio::spawn(async move {
            for _ in 0..12 {
                sleep(Duration::from_millis(20)).await;
                server
                    .send(Message::Binary(
                        ServerFrame::Heartbeat.encode().expect("encode heartbeat"),
                    ))
                    .await
                    .expect("send heartbeat");
            }
            server
                .send(Message::Binary(
                    ServerFrame::CaughtUp(CaughtUpPosition {
                        next_seq_num: 42,
                        last_timestamp_ms: 1_786_377_600_000,
                    })
                    .encode()
                    .expect("encode caught up"),
                ))
                .await
                .expect("send caught up");
        });
        let mut socket = ReadSocket {
            ws: client,
            read_idle_timeout: Some(Duration::from_millis(100)),
        };

        let outcome = socket.next_outcome().await.expect("caught-up outcome");

        assert!(matches!(
            outcome,
            ReadSocketOutcome::CaughtUp(CaughtUpPosition {
                next_seq_num: 42,
                last_timestamp_ms: 1_786_377_600_000,
            })
        ));
        sender.await.expect("join heartbeat sender");
    }

    #[tokio::test]
    async fn explicit_read_timeout_does_not_reset_on_protocol_heartbeat() {
        let (client, mut server) = connected_websockets().await;
        let sender = tokio::spawn(async move {
            loop {
                sleep(Duration::from_millis(20)).await;
                server
                    .send(Message::Binary(
                        ServerFrame::Heartbeat.encode().expect("encode heartbeat"),
                    ))
                    .await
                    .expect("send heartbeat");
            }
        });
        let mut socket = ReadSocket {
            ws: client,
            read_idle_timeout: Some(Duration::from_secs(1)),
        };

        let result = with_timeout(
            Duration::from_millis(100),
            "read stream record",
            socket.next_outcome(),
        )
        .await;

        assert!(matches!(
            result,
            Err(TsfClientError::Timeout {
                operation: "read stream record"
            })
        ));
        sender.abort();
    }

    #[tokio::test]
    async fn read_idle_timeout_still_rejects_a_silent_connection() {
        let (client, server) = connected_websockets().await;
        let server = tokio::spawn(async move {
            let _server = server;
            sleep(Duration::from_secs(1)).await;
        });
        let mut socket = ReadSocket {
            ws: client,
            read_idle_timeout: Some(Duration::from_millis(50)),
        };

        let result = socket.next_outcome().await;

        assert!(matches!(
            result,
            Err(TsfClientError::Timeout {
                operation: "read stream record"
            })
        ));
        server.abort();
    }

    #[test]
    fn rejects_incoherent_client_config() {
        let mut config = TsfClientConfig::default();
        config.retry_policy.max_attempts = 0;
        assert!(matches!(
            TsfClient::with_config(config),
            Err(TsfClientError::InvalidClientConfig(_))
        ));

        let mut config = TsfClientConfig::default();
        config.retry_policy.initial_backoff = Duration::from_secs(2);
        config.retry_policy.max_backoff = Duration::from_secs(1);
        assert!(matches!(
            TsfClient::with_config(config),
            Err(TsfClientError::InvalidClientConfig(_))
        ));

        let config = TsfClientConfig {
            rest_request_timeout: Duration::ZERO,
            ..TsfClientConfig::default()
        };
        assert!(matches!(
            TsfClient::with_config(config),
            Err(TsfClientError::InvalidClientConfig(_))
        ));

        let config = TsfClientConfig {
            websocket_connect_timeout: MAX_CLIENT_DELAY + Duration::from_millis(1),
            ..TsfClientConfig::default()
        };
        assert!(matches!(
            TsfClient::with_config(config),
            Err(TsfClientError::InvalidClientConfig(_))
        ));

        let mut config = TsfClientConfig::default();
        config.retry_policy.max_backoff = MAX_CLIENT_DELAY + Duration::from_millis(1);
        assert!(matches!(
            TsfClient::with_config(config),
            Err(TsfClientError::InvalidClientConfig(_))
        ));
    }

    #[test]
    fn rejects_invalid_link_page_invariants() {
        let link = serde_json::json!({
            "link_id": "reader",
            "permissions": "r",
            "status": "active",
            "created_at": "2026-08-13T00:00:00Z",
            "expires_at": null,
            "revoked_at": null
        });
        let duplicate: ListLinksResponse = serde_json::from_value(serde_json::json!({
            "authorizing_link_id": "owner",
            "links": [link.clone(), link],
            "next_cursor": null
        }))
        .expect("decodable duplicate page");
        assert!(matches!(
            validate_link_page(&duplicate, 100),
            Err(TsfClientError::InvalidLinkPage(_))
        ));

        let empty_with_cursor: ListLinksResponse = serde_json::from_value(serde_json::json!({
            "authorizing_link_id": "owner",
            "links": [],
            "next_cursor": "next"
        }))
        .expect("decodable empty page");
        assert!(matches!(
            validate_link_page(&empty_with_cursor, 100),
            Err(TsfClientError::InvalidLinkPage(_))
        ));
    }

    #[test]
    fn writer_window_cannot_exceed_server_queue_contract() {
        let default = TsfWriterConfig::default();
        assert_eq!(default.max_unacked_bytes, MAX_WRITER_UNACKED_PAYLOAD_BYTES);
        assert_eq!(default.max_unacked_records, MAX_WRITER_UNACKED_RECORDS);
        assert!(default.validate().is_ok());

        for config in [
            TsfWriterConfig {
                max_unacked_bytes: MAX_WRITER_UNACKED_PAYLOAD_BYTES + 1,
                ..TsfWriterConfig::default()
            },
            TsfWriterConfig {
                max_unacked_records: MAX_WRITER_UNACKED_RECORDS + 1,
                ..TsfWriterConfig::default()
            },
        ] {
            assert!(matches!(
                config.validate(),
                Err(TsfClientError::InvalidWriterConfig(_))
            ));
        }
    }

    #[test]
    fn builds_versioned_rest_and_path_only_websocket_urls() {
        let client =
            TsfClient::with_api_origin(Url::parse("https://example.com").expect("API origin"))
                .expect("valid API origin");

        assert_eq!(
            client.rest_url("/streams").as_str(),
            "https://example.com/api/v1/streams"
        );
        assert_eq!(
            client
                .websocket_url("/streams/0123456789abcdefghjkmnpqrstvwxyz/read")
                .expect("WebSocket URL")
                .as_str(),
            "wss://example.com/api/v1/streams/0123456789abcdefghjkmnpqrstvwxyz/read"
        );
    }

    #[test]
    fn rejects_non_origin_api_urls() {
        for value in [
            "https://user@example.com",
            "https://example.com/api",
            "https://example.com?region=west",
            "https://example.com#api",
            "wss://example.com",
        ] {
            assert!(matches!(
                TsfClient::with_api_origin(Url::parse(value).expect("URL")),
                Err(TsfClientError::InvalidApiOrigin(_))
            ));
        }
    }

    #[test]
    fn sse_query_keeps_the_original_absolute_selector_and_limit() {
        let stream_id = "0123456789abcdefghjkmnpqrstvwxyz"
            .parse()
            .expect("stream ID");
        let mut options = ReadStreamOptions::new(stream_id);
        options.start = Some(ReadStart::SeqNum(42));
        options.limit = Some(7);
        options.snapshot = true;
        let mut url = Url::parse("https://tail.surf/api/v1/streams/id/records").expect("SSE URL");

        append_sse_query(&mut url, &options);

        assert_eq!(url.query(), Some("seq_num=42&limit=7&snapshot=true"));
    }

    #[test]
    fn sse_parser_retains_only_strict_versioned_resume_ids() {
        let cursor = "v1,4,0";
        let block = format!(
            "id: {cursor}\nevent: caught_up\ndata: {{\"next_seq_num\":\"4\",\"last_timestamp_ms\":\"0\"}}"
        );
        let event = parse_sse_block(block.as_bytes())
            .expect("parse SSE event")
            .expect("data event");

        assert_eq!(sse_resume_event_id(&event).expect("resume cursor"), cursor);

        let snapshot_cursor = "v1,4,0,5,0";
        let snapshot_event = ParsedSseEvent {
            event: "read_batch".to_owned(),
            data: "{}".to_owned(),
            id: Some(snapshot_cursor.to_owned()),
        };
        assert_eq!(
            sse_resume_event_id(&snapshot_event).expect("snapshot resume cursor"),
            snapshot_cursor
        );

        for invalid in [
            "v2,4,0",
            "v1,04,0",
            "v1,4,5",
            "v1,4,0,5",
            "v1,4,0,3,6",
            "v1,4,0,5,6,7",
            "v1,0,0,0,1",
            "v1,1,0,1,9007199254740992",
            "v1,4, 0",
        ] {
            let event = ParsedSseEvent {
                event: "caught_up".to_owned(),
                data: "{}".to_owned(),
                id: Some(invalid.to_owned()),
            };
            assert!(matches!(
                sse_resume_event_id(&event),
                Err(TsfClientError::InvalidSse(_))
            ));
        }
    }

    #[tokio::test]
    async fn rejects_read_selectors_outside_the_adapter_range() {
        let client =
            TsfClient::with_api_origin(Url::parse("http://localhost").expect("API origin"))
                .expect("valid API origin");
        let mut options = ReadStreamOptions::new(
            "0123456789abcdefghjkmnpqrstvwxyz"
                .parse()
                .expect("stream ID"),
        );
        options.start = Some(ReadStart::TailOffset(MAX_READ_SELECTOR_VALUE + 1));

        assert!(matches!(
            client.connect_reader(options).await,
            Err(TsfClientError::InvalidReadSelector {
                value,
                maximum: MAX_READ_SELECTOR_VALUE,
            }) if value == MAX_READ_SELECTOR_VALUE + 1
        ));
    }

    #[test]
    fn append_ack_counts_half_open_matching_ranges() {
        let ack = AppendAck {
            writer_start_seq_num: 7,
            writer_end_seq_num: 10,
            start_seq_num: 42,
            end_seq_num: 45,
        };

        assert_eq!(ack.record_count().expect("record count"), 3);
        assert_eq!(ack.validate().expect("valid ack"), ack);
    }

    #[test]
    fn append_ack_rejects_mismatched_range_lengths() {
        let ack = AppendAck {
            writer_start_seq_num: 7,
            writer_end_seq_num: 9,
            start_seq_num: 42,
            end_seq_num: 43,
        };

        assert!(matches!(
            ack.record_count(),
            Err(TsfClientError::InvalidAppendAck(error_ack)) if error_ack == ack
        ));
    }

    #[tokio::test]
    async fn close_preserves_terminal_error_when_its_command_is_dropped() {
        let (cmd_tx, mut cmd_rx) = mpsc::channel::<WriterCommand>(1);
        let terminal_error = Arc::new(OnceLock::new());
        let task_terminal_error = Arc::clone(&terminal_error);
        let task = tokio::spawn(async move {
            let command = cmd_rx.recv().await.expect("close command");
            task_terminal_error
                .set("stream next sequence did not match".to_owned())
                .expect("set terminal error");
            drop(command);
        });
        let writer = TsfWriter {
            cmd_tx,
            byte_permits: Arc::new(Semaphore::new(1)),
            record_permits: Arc::new(Semaphore::new(1)),
            terminal_error,
            max_unacked_bytes: 1,
            task: Some(task),
        };

        let error = writer.close().await.expect_err("close must fail");
        assert!(
            matches!(&error, TsfClientError::AppendWriterFailed(message) if message.contains("stream next sequence did not match")),
            "error={error}"
        );
    }

    #[test]
    fn read_and_stateless_append_responses_match_the_requested_ranges() {
        assert!(matches!(
            validate_append_range(
                AppendRange {
                    start_seq_num: 4,
                    end_seq_num: 6,
                },
                1,
            ),
            Err(TsfClientError::InvalidAppendRange(_))
        ));

        let mut options = ReadStreamOptions::new(
            "00000000000000000000000000000000"
                .parse()
                .expect("stream ID"),
        );
        options.start = Some(ReadStart::SeqNum(2));
        assert!(
            validate_read_batch_for_request(
                &ReadBatch::try_from_records(vec![sse_test_record(1, 0)]).expect("valid batch"),
                &options
            )
            .is_err()
        );
        assert!(
            validate_caught_up_for_request(
                CaughtUpPosition {
                    next_seq_num: 1,
                    last_timestamp_ms: 0,
                },
                &options,
            )
            .is_err()
        );
    }

    #[test]
    fn dispatch_ack_rejects_more_records_than_are_pending() {
        let permits = Arc::new(Semaphore::new(2));
        let (ack_tx, _ack_rx) = oneshot::channel();
        let record = AppendRecord::new(7, PartHeader::unsplit(), RecordFormat::Bytes, Bytes::new());
        let mut pending = VecDeque::from([PendingAppend {
            record,
            ack_tx,
            _byte_permit: permits.clone().try_acquire_owned().expect("byte permit"),
            _record_permit: permits.try_acquire_owned().expect("record permit"),
        }]);
        let ack = AppendAck {
            writer_start_seq_num: 7,
            writer_end_seq_num: 9,
            start_seq_num: 42,
            end_seq_num: 44,
        };

        assert!(matches!(
            dispatch_ack(ack, &mut pending),
            Err(TsfClientError::InvalidAppendAck(error_ack)) if error_ack == ack
        ));
        assert_eq!(pending.len(), 1);
    }

    #[test]
    fn dispatch_ack_validates_the_full_range_before_draining() {
        let permits = Arc::new(Semaphore::new(4));
        let mut pending = VecDeque::new();
        for writer_seq_num in [7, 9] {
            let (ack_tx, _ack_rx) = oneshot::channel();
            pending.push_back(PendingAppend {
                record: AppendRecord::new(
                    writer_seq_num,
                    PartHeader::unsplit(),
                    RecordFormat::Bytes,
                    Bytes::new(),
                ),
                ack_tx,
                _byte_permit: permits.clone().try_acquire_owned().expect("byte permit"),
                _record_permit: permits.clone().try_acquire_owned().expect("record permit"),
            });
        }
        let ack = AppendAck {
            writer_start_seq_num: 7,
            writer_end_seq_num: 9,
            start_seq_num: 42,
            end_seq_num: 44,
        };

        assert!(matches!(
            dispatch_ack(ack, &mut pending),
            Err(TsfClientError::InvalidAppendAck(error_ack)) if error_ack == ack
        ));
        assert_eq!(
            pending
                .iter()
                .map(|item| item.record.writer_seq_num)
                .collect::<Vec<_>>(),
            [7, 9]
        );
    }

    #[tokio::test]
    async fn sse_parser_accepts_multiple_complete_events_in_one_large_chunk() {
        let payload = format!(
            "event: first\ndata: {}\n\nevent: second\ndata: {}\n\n",
            "a".repeat(1_100_000),
            "b".repeat(1_100_000),
        );
        let mut body: SseBody =
            Box::pin(futures_util::stream::iter(vec![Ok::<_, reqwest::Error>(
                Bytes::from(payload),
            )]));
        let mut parser = SseParser::default();

        assert_eq!(
            next_sse_event(&mut body, &mut parser)
                .await
                .expect("first event")
                .expect("first event value")
                .event,
            "first"
        );
        assert_eq!(
            next_sse_event(&mut body, &mut parser)
                .await
                .expect("second event")
                .expect("second event value")
                .event,
            "second"
        );
    }

    #[tokio::test]
    async fn sse_parser_accepts_an_event_fragmented_across_chunks() {
        let payload = "event: read_batch\ndata: split 😀 payload\n\n".as_bytes();
        let chunks = payload
            .chunks(7)
            .map(|chunk| Ok::<_, reqwest::Error>(Bytes::copy_from_slice(chunk)))
            .collect::<Vec<_>>();
        let mut body: SseBody = Box::pin(futures_util::stream::iter(chunks));
        let mut parser = SseParser::default();

        let event = next_sse_event(&mut body, &mut parser)
            .await
            .expect("fragmented event")
            .expect("fragmented event value");
        assert_eq!(event.event, "read_batch");
        assert_eq!(event.data, "split 😀 payload");
    }

    #[tokio::test]
    async fn sse_parser_rejects_one_oversized_completed_event() {
        let payload = format!(
            "event: read_batch\ndata: {}\n\n",
            "a".repeat(MAX_SSE_EVENT_BYTES)
        );
        let mut body: SseBody =
            Box::pin(futures_util::stream::iter(vec![Ok::<_, reqwest::Error>(
                Bytes::from(payload),
            )]));
        let mut parser = SseParser::default();

        assert!(matches!(
            next_sse_event(&mut body, &mut parser).await,
            Err(TsfClientError::InvalidSse("event exceeds 2 MiB"))
        ));
    }

    #[tokio::test]
    async fn sse_parser_rejects_an_oversized_fragmented_event() {
        let first = format!(
            "event: read_batch\ndata: {}",
            "a".repeat(MAX_SSE_UNTERMINATED_EVENT_BYTES / 2)
        );
        let second = "a".repeat(MAX_SSE_UNTERMINATED_EVENT_BYTES / 2 + 1);
        let mut body: SseBody = Box::pin(futures_util::stream::iter(vec![
            Ok::<_, reqwest::Error>(Bytes::from(first)),
            Ok::<_, reqwest::Error>(Bytes::from(second)),
        ]));
        let mut parser = SseParser::default();

        assert!(matches!(
            next_sse_event(&mut body, &mut parser).await,
            Err(TsfClientError::InvalidSse(
                "unterminated event exceeds 2 MiB"
            ))
        ));
    }

    #[tokio::test]
    async fn sse_parser_rejects_an_event_oversized_across_fragments() {
        let first = format!("event: read_batch\ndata: {}", "a".repeat(1_500_000));
        let second = format!("{}\n\n", "b".repeat(700_000));
        let mut body: SseBody = Box::pin(futures_util::stream::iter(vec![
            Ok::<_, reqwest::Error>(Bytes::from(first)),
            Ok::<_, reqwest::Error>(Bytes::from(second)),
        ]));
        let mut parser = SseParser::default();

        assert!(matches!(
            next_sse_event(&mut body, &mut parser).await,
            Err(TsfClientError::InvalidSse("event exceeds 2 MiB"))
        ));
    }

    #[tokio::test]
    async fn sse_parser_accepts_a_terminator_split_across_chunks() {
        let chunks = ["data: hi\r\n\r", "\n"]
            .into_iter()
            .map(|chunk| Ok::<_, reqwest::Error>(Bytes::copy_from_slice(chunk.as_bytes())))
            .collect::<Vec<_>>();
        let mut body: SseBody = Box::pin(futures_util::stream::iter(chunks));
        let mut parser = SseParser::default();

        let event = next_sse_event(&mut body, &mut parser)
            .await
            .expect("straddled event")
            .expect("straddled event value");
        assert_eq!(event.data, "hi");
    }

    #[tokio::test]
    async fn sse_reader_bounds_consecutive_reconnects_without_delivered_records() {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("bind SSE listener");
        let address = listener.local_addr().expect("SSE listener address");
        let server = tokio::spawn(async move {
            // Every handshake completes with valid metadata; no body ever delivers a record.
            for _ in 0..8 {
                let Ok((mut stream, _)) = listener.accept().await else {
                    break;
                };
                let mut request = [0_u8; 4096];
                let _ = stream.read(&mut request).await;
                let response = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n\
                    event: stream_metadata\n\
                    data: {\"stream_id\":\"00000000000000000000000000000000\",\"visibility\":\"private\",\"created_at\":\"2026-08-13T00:00:00Z\",\"expires_at\":\"2026-08-23T00:00:00Z\"}\n\n";
                let _ = stream.write_all(response.as_bytes()).await;
            }
        });
        let mut config =
            TsfClientConfig::new(Url::parse(&format!("http://{address}")).expect("SSE API origin"))
                .expect("valid client config");
        config.retry_policy = RetryPolicy {
            max_attempts: 3,
            initial_backoff: Duration::from_millis(1),
            max_backoff: Duration::from_millis(2),
        };
        let client = TsfClient::with_config(config).expect("SSE client");
        let stream_id = "00000000000000000000000000000000"
            .parse()
            .expect("stream ID");
        let mut reader = client
            .connect_sse_reader(ReadStreamOptions::new(stream_id))
            .await
            .expect("initial SSE connect");

        let result = tokio::time::timeout(Duration::from_secs(5), reader.next_batch()).await;

        assert!(matches!(
            result,
            Ok(Err(TsfClientError::ReadReconnectLimitExceeded {
                max_connection_attempts: 3,
            }))
        ));
        server.abort();
        let _ = server.await;
    }

    #[tokio::test]
    async fn list_all_links_rejects_link_ids_repeated_across_pages() {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("bind REST listener");
        let address = listener.local_addr().expect("REST listener address");
        let server = tokio::spawn(async move {
            let link = r#"{"link_id":"reader","permissions":"r","status":"active","created_at":"2026-08-13T00:00:00Z","expires_at":null,"revoked_at":null}"#;
            for page in [
                format!(
                    r#"{{"authorizing_link_id":"owner","links":[{link}],"next_cursor":"next"}}"#
                ),
                format!(r#"{{"authorizing_link_id":"owner","links":[{link}],"next_cursor":null}}"#),
            ] {
                let (mut stream, _) = listener.accept().await.expect("accept REST request");
                let mut request = [0_u8; 4096];
                let _ = stream.read(&mut request).await;
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{page}",
                    page.len()
                );
                let _ = stream.write_all(response.as_bytes()).await;
            }
        });
        let mut config = TsfClientConfig::new(
            Url::parse(&format!("http://{address}")).expect("REST API origin"),
        )
        .expect("valid client config");
        config.retry_policy = RetryPolicy::none();
        let client = TsfClient::with_config(config).expect("REST client");
        let stream_id = "00000000000000000000000000000000"
            .parse()
            .expect("stream ID");
        let owner = LinkSecret::from("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");

        let result = client.list_all_links(&stream_id, &owner).await;

        assert!(matches!(
            result,
            Err(TsfClientError::InvalidLinkPage(
                "link ID appears on multiple pages"
            ))
        ));
        server.await.expect("join REST server");
    }

    #[test]
    fn compact_record_data_choice_matches_serialized_lengths() {
        let cases: Vec<Vec<u8>> = vec![
            b"plain text".to_vec(),
            "unicode 😀 text".as_bytes().to_vec(),
            b"\"\\escape\theavy\n".to_vec(),
            vec![0x00; 64],
            vec![0x7f; 128],
            "😀".repeat(1024).into_bytes(),
            vec![0xff; 32],
        ];
        for bytes in cases {
            let chosen = compact_record_data(&bytes);
            if let Ok(text) = std::str::from_utf8(&bytes) {
                let utf8_len = serde_json::to_vec(&RecordData::Utf8(text.to_owned()))
                    .expect("measure utf8")
                    .len();
                let base64url_len = br#"{"encoding":"base64url","value":""}"#.len()
                    + bytes.len().saturating_mul(4).div_ceil(3);
                assert_eq!(
                    matches!(chosen, RecordData::Utf8(_)),
                    utf8_len <= base64url_len,
                    "bytes={bytes:?}"
                );
            } else {
                assert!(matches!(chosen, RecordData::Base64url(_)));
            }
            let round_tripped = match &chosen {
                RecordData::Utf8(value) => value.as_bytes().to_vec(),
                RecordData::Base64url(value) => URL_SAFE_NO_PAD.decode(value).expect("decode"),
            };
            assert_eq!(round_tripped, bytes);
        }
    }

    #[test]
    fn sse_batch_validation_enforces_decoded_bounds_and_read_limits() {
        assert!(validate_sse_read_batch_count(0).is_err());
        assert!(validate_sse_read_batch_count(MAX_SSE_READ_BATCH_RECORDS + 1).is_err());

        let mut options = ReadStreamOptions::new(
            "00000000000000000000000000000000"
                .parse()
                .expect("stream ID"),
        );
        // Build through sse_read_batch so batch validation, not batch construction, is what
        // rejects these.
        let aggregate = sse_read_batch(SseReadBatchData {
            records: [0, 1, 2]
                .map(|seq_num| sse_wire_record(seq_num, 400 * 1024))
                .to_vec(),
        })
        .expect("sse_read_batch leaves aggregate bounds to validation");
        assert!(validate_sse_read_batch(&aggregate, &options).is_err());

        options.limit = Some(1);
        let two = ReadBatch::try_from_records(vec![sse_test_record(0, 0), sse_test_record(1, 0)])
            .expect("valid batch");
        assert!(validate_sse_read_batch(&two, &options).is_err());

        options.limit = None;
        options.end_seq_num = Some(1);
        assert!(validate_sse_read_batch(&two, &options).is_err());

        let non_contiguous = sse_read_batch(SseReadBatchData {
            records: vec![sse_wire_record(0, 0), sse_wire_record(2, 0)],
        })
        .expect("sse_read_batch leaves continuity to validation");
        options.end_seq_num = None;
        assert!(validate_sse_read_batch(&non_contiguous, &options).is_err());

        let mut wire_record = SseReadRecord {
            seq_num: 0,
            timestamp_ms: 0,
            writer_id: URL_SAFE_NO_PAD.encode([0_u8; WriterId::BYTE_LEN - 1]),
            writer_seq_num: 0,
            part: RestRecordPart {
                index: 0,
                is_final: true,
            },
            format: RecordFormat::Bytes,
            data: RecordData::Utf8(String::new()),
        };
        assert!(matches!(
            sse_read_batch(SseReadBatchData {
                records: vec![wire_record.clone()],
            }),
            Err(TsfClientError::InvalidSse("invalid writer_id length"))
        ));

        wire_record.writer_id = URL_SAFE_NO_PAD.encode([0_u8; WriterId::BYTE_LEN]);
        wire_record.data =
            RecordData::Base64url(URL_SAFE_NO_PAD.encode(vec![0_u8; MAX_RECORD_BYTES + 1]));
        assert!(
            sse_read_batch(SseReadBatchData {
                records: vec![wire_record],
            })
            .is_err()
        );
    }

    #[test]
    fn sse_cursor_validation_binds_positions_counts_and_snapshot_timestamps() {
        let mut options = ReadStreamOptions::new(
            "00000000000000000000000000000000"
                .parse()
                .expect("stream ID"),
        );
        options.start = Some(ReadStart::SeqNum(0));
        let records =
            ReadBatch::try_from_records(vec![sse_test_record(0, 0)]).expect("valid batch");
        assert!(
            validate_sse_read_batch_cursor(
                &records,
                ParsedSseResumeCursor {
                    next_seq_num: 2,
                    consumed_records: 1,
                    snapshot: None,
                },
                None,
                &options,
                None,
            )
            .is_err()
        );
        let previous = ParsedSseResumeCursor {
            next_seq_num: 1,
            consumed_records: 1,
            snapshot: None,
        };
        assert!(
            validate_sse_caught_up_cursor(
                CaughtUpPosition {
                    next_seq_num: 2,
                    last_timestamp_ms: 0,
                },
                ParsedSseResumeCursor {
                    next_seq_num: 2,
                    consumed_records: 1,
                    snapshot: None,
                },
                Some(previous),
                &options,
                None,
            )
            .is_err()
        );

        let boundary = SnapshotBoundary {
            end_seq_num: 2,
            last_timestamp_ms: 10,
        };
        assert!(
            validate_sse_snapshot_cursor(
                boundary,
                ParsedSseResumeCursor {
                    next_seq_num: 0,
                    consumed_records: 0,
                    snapshot: Some((2, 11)),
                },
                None,
            )
            .is_err()
        );
    }

    fn sse_test_record(seq_num: u64, payload_bytes: usize) -> OwnedReadRecord {
        OwnedReadRecord {
            seq_num,
            timestamp_ms: seq_num,
            writer_id: WriterId::from_bytes([0_u8; 16]),
            writer_seq_num: seq_num,
            part: PartHeader::unsplit(),
            format: RecordFormat::Bytes,
            data: Bytes::from(vec![0_u8; payload_bytes]),
        }
    }

    fn sse_wire_record(seq_num: u64, payload_bytes: usize) -> SseReadRecord {
        SseReadRecord {
            seq_num,
            timestamp_ms: seq_num,
            writer_id: URL_SAFE_NO_PAD.encode([0_u8; WriterId::BYTE_LEN]),
            writer_seq_num: seq_num,
            part: RestRecordPart {
                index: 0,
                is_final: true,
            },
            format: RecordFormat::Bytes,
            data: RecordData::Base64url(URL_SAFE_NO_PAD.encode(vec![0_u8; payload_bytes])),
        }
    }

    #[test]
    fn sse_read_batch_decodes_mixed_utf8_and_base64url_payloads() {
        let text = SseReadRecord {
            seq_num: 3,
            timestamp_ms: 300,
            writer_id: URL_SAFE_NO_PAD.encode([7_u8; WriterId::BYTE_LEN]),
            writer_seq_num: 30,
            part: RestRecordPart {
                index: 0,
                is_final: true,
            },
            format: RecordFormat::Transcript,
            data: RecordData::Utf8("héllo".to_owned()),
        };
        let binary = SseReadRecord {
            seq_num: 4,
            timestamp_ms: 301,
            writer_id: URL_SAFE_NO_PAD.encode([8_u8; WriterId::BYTE_LEN]),
            writer_seq_num: 40,
            part: RestRecordPart {
                index: 0,
                is_final: true,
            },
            format: RecordFormat::Bytes,
            data: RecordData::Base64url(URL_SAFE_NO_PAD.encode([0_u8, 159, 146, 150])),
        };
        let batch = sse_read_batch(SseReadBatchData {
            records: vec![text, binary],
        })
        .expect("valid read_batch event");

        assert_eq!(batch.len(), 2);
        let first = batch.first().expect("first");
        assert_eq!(first.data, "héllo".as_bytes());
        assert_eq!(first.format, RecordFormat::Transcript);
        assert_eq!(first.writer_id, WriterId::from_bytes([7_u8; 16]));
        assert_eq!(first.writer_seq_num, 30);
        let last = batch.last().expect("last");
        assert_eq!(last.data, &[0_u8, 159, 146, 150]);
        assert_eq!(last.seq_num, 4);

        let options = ReadStreamOptions::new(
            "00000000000000000000000000000000"
                .parse()
                .expect("stream ID"),
        );
        assert!(validate_sse_read_batch(&batch, &options).is_ok());
    }

    #[test]
    fn stateless_append_compacts_an_escape_heavy_maximum_record() {
        let data = vec![0_u8; MAX_RECORD_BYTES];
        let encoded = compact_record_data(&data);
        assert!(matches!(encoded, RecordData::Base64url(_)));
        let request = AppendRecordsRequest {
            client_writer_id: URL_SAFE_NO_PAD.encode([0_u8; 16]),
            writer_start_seq_num: 0,
            records: vec![AppendJsonRecord {
                part: None,
                format: RecordFormat::Transcript,
                data: encoded,
            }],
            expected_next_seq_num: None,
        };
        let json = serde_json::to_vec(&request).expect("append request JSON");
        assert!(json.len() <= crate::protocol::rest::MAX_STATELESS_APPEND_JSON_BYTES);
    }

    #[tokio::test]
    async fn stateless_append_rejects_aggregate_payload_and_max_writer_sequence() {
        let client = TsfClient::new();
        let stream_id = "00000000000000000000000000000000"
            .parse()
            .expect("stream ID");
        let secret = LinkSecret::from("A".repeat(43));
        let large = vec![
            AppendRecord::new(
                0,
                PartHeader::unsplit(),
                RecordFormat::Bytes,
                Bytes::from(vec![0; 500 * 1024]),
            ),
            AppendRecord::new(
                1,
                PartHeader::unsplit(),
                RecordFormat::Bytes,
                Bytes::from(vec![0; 500 * 1024]),
            ),
        ];
        assert!(matches!(
            client
                .append_records(
                    &stream_id,
                    ClientWriterId::from_bytes([0; 16]),
                    &large,
                    None,
                    &secret
                )
                .await,
            Err(TsfClientError::InvalidStatelessAppend(
                "append payload must not exceed 900 KiB"
            ))
        ));

        let endpoint = [AppendRecord::new(
            u64::MAX,
            PartHeader::unsplit(),
            RecordFormat::Bytes,
            Bytes::new(),
        )];
        assert!(matches!(
            client
                .append_records(
                    &stream_id,
                    ClientWriterId::from_bytes([0; 16]),
                    &endpoint,
                    None,
                    &secret,
                )
                .await,
            Err(TsfClientError::InvalidStatelessAppend(
                "writer sequence range must end before u64::MAX"
            ))
        ));

        let valid = [AppendRecord::new(
            0,
            PartHeader::unsplit(),
            RecordFormat::Bytes,
            Bytes::new(),
        )];
        assert!(matches!(
            client
                .append_records(
                    &stream_id,
                    ClientWriterId::from_bytes([0; 16]),
                    &valid,
                    Some(MAX_READ_SELECTOR_VALUE + 1),
                    &secret,
                )
                .await,
            Err(TsfClientError::InvalidStatelessAppend(
                "expected next sequence exceeds the data adapter range"
            ))
        ));
    }

    #[test]
    fn websocket_retry_policy_distinguishes_transient_and_permanent_failures() {
        for code in [1000, 1001, 1005, 1006, 1011, 1012, 1013, 1014, 1015] {
            let error = TsfClientError::WebSocketClosedWithReason {
                code,
                reason: "transient".to_owned(),
            };
            assert!(error.is_retryable(), "close {code}");
            assert!(error.is_resumable_read_interruption(), "close {code}");
        }

        for code in [1002, 1003, 1007, 1008, 1009, 1010, 4000] {
            let error = TsfClientError::WebSocketClosedWithReason {
                code,
                reason: "permanent".to_owned(),
            };
            assert!(!error.is_retryable(), "close {code}");
            assert!(!error.is_resumable_read_interruption(), "close {code}");
        }

        assert!(
            TsfClientError::WebSocket(WebSocketError::Protocol(
                ProtocolError::ResetWithoutClosingHandshake,
            ))
            .is_retryable()
        );
        assert!(
            !TsfClientError::WebSocket(WebSocketError::Protocol(ProtocolError::InvalidOpcode(15),))
                .is_retryable()
        );
    }
}