anytype 0.5.0

An ergonomic Anytype API client in rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
//! `HttpClient` middleware used by `AnytypeClient`
//!
//! Responsible for
//!  - handing all HTTP api requests
//!  - logging/tracing
//!  - retries and backoff (for timeouts and connection errors)
//!  - rate limiting

use std::{
    fmt,
    future::Future,
    sync::{
        Arc,
        atomic::{AtomicU32, AtomicU64, Ordering},
    },
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use bytes::Bytes;
use futures::{StreamExt, stream::BoxStream};
use parking_lot::Mutex;
use reqwest::{ClientBuilder, Method, Response, StatusCode, Url, header::HeaderMap};
use serde::{Serialize, de::DeserializeOwned};
use tracing::{debug, error, info, trace, warn};

use crate::{
    Result,
    client::{
        MAX_DOCUMENT_RESPONSE_BYTES, MAX_ERROR_RESPONSE_BYTES, MAX_FILE_RESPONSE_BYTES,
        MAX_JSON_RESPONSE_BYTES, ResponseLimits,
    },
    config::{
        ANYTYPE_API_HEADER, MAX_HTTP_REQUEST_ATTEMPTS, MAX_RETRIES, RATE_LIMIT_WAIT_MAX_SECS,
        RATE_LIMIT_WAIT_WARN_SECS,
    },
    filters::QueryWithFilters,
    http_timeout::{HttpTimeoutClass, HttpTimeoutPolicy, TimeoutOutcome, timeout_outcome},
    prelude::*,
};

/// HTTP metrics tracked using atomic counters for thread-safe access.
/// These counters are cumulative and never reset during the client's lifetime.
#[derive(Debug, Default)]
pub struct HttpMetrics {
    /// Total number of logical HTTP operations entering the request pipeline
    logical_operations: AtomicU64,
    /// Total number of HTTP requests sent to the server (excludes cached responses)
    total_requests: AtomicU64,
    /// Total number of physical HTTP attempts, including automatic replays
    physical_attempts: AtomicU64,
    /// Total number of multipart POST requests dispatched
    multipart_posts: AtomicU64,
    /// Total number of successful responses (2xx status codes)
    successful_responses: AtomicU64,
    /// Total number of error responses (non-2xx status codes, excluding rate limit errors)
    errors: AtomicU64,
    /// Total number of retry attempts (connection failures, timeouts, 5xx errors)
    retries: AtomicU64,
    /// Total bytes sent in request bodies
    bytes_sent: AtomicU64,
    /// Total bytes received in response bodies
    bytes_received: AtomicU64,
    /// Total number of rate limit errors (429 responses)
    rate_limit_errors: AtomicU64,
    /// Total seconds spent waiting for rate limit backoff
    rate_limit_delay_secs: AtomicU64,
    timeout_counts: [AtomicU64; HttpTimeoutClass::COUNT],
    timeout_elapsed_millis: [AtomicU64; HttpTimeoutClass::COUNT],
    transport_timeout_count: AtomicU64,
    transport_timeout_elapsed_millis: AtomicU64,
    timeout_outcomes: [AtomicU64; TimeoutOutcome::COUNT],
}

impl HttpMetrics {
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns a snapshot of current metrics as plain u64 values
    pub fn snapshot(&self) -> HttpMetricsSnapshot {
        HttpMetricsSnapshot {
            logical_operations: self.logical_operations.load(Ordering::Relaxed),
            total_requests: self.total_requests.load(Ordering::Relaxed),
            physical_attempts: self.physical_attempts.load(Ordering::Relaxed),
            multipart_posts: self.multipart_posts.load(Ordering::Relaxed),
            successful_responses: self.successful_responses.load(Ordering::Relaxed),
            errors: self.errors.load(Ordering::Relaxed),
            retries: self.retries.load(Ordering::Relaxed),
            bytes_sent: self.bytes_sent.load(Ordering::Relaxed),
            bytes_received: self.bytes_received.load(Ordering::Relaxed),
            rate_limit_errors: self.rate_limit_errors.load(Ordering::Relaxed),
            rate_limit_delay_secs: self.rate_limit_delay_secs.load(Ordering::Relaxed),
            timeout_counts: std::array::from_fn(|index| {
                self.timeout_counts[index].load(Ordering::Relaxed)
            }),
            timeout_elapsed_millis: std::array::from_fn(|index| {
                self.timeout_elapsed_millis[index].load(Ordering::Relaxed)
            }),
            transport_timeout_count: self.transport_timeout_count.load(Ordering::Relaxed),
            transport_timeout_elapsed_millis: self
                .transport_timeout_elapsed_millis
                .load(Ordering::Relaxed),
            timeout_outcomes: std::array::from_fn(|index| {
                self.timeout_outcomes[index].load(Ordering::Relaxed)
            }),
        }
    }

    fn increment_logical_operations(&self) {
        self.logical_operations.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_requests(&self) {
        self.total_requests.fetch_add(1, Ordering::Relaxed);
        self.physical_attempts.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_multipart_posts(&self) {
        self.multipart_posts.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_success(&self) {
        self.successful_responses.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_errors(&self) {
        self.errors.fetch_add(1, Ordering::Relaxed);
    }

    fn increment_retries(&self) {
        self.retries.fetch_add(1, Ordering::Relaxed);
    }

    fn add_bytes_sent(&self, bytes: u64) {
        self.bytes_sent.fetch_add(bytes, Ordering::Relaxed);
    }

    fn add_bytes_received(&self, bytes: u64) {
        self.bytes_received.fetch_add(bytes, Ordering::Relaxed);
    }

    fn increment_rate_limit_errors(&self) {
        self.rate_limit_errors.fetch_add(1, Ordering::Relaxed);
    }

    fn add_rate_limit_delay(&self, secs: u64) {
        self.rate_limit_delay_secs
            .fetch_add(secs, Ordering::Relaxed);
    }

    fn record_timeout(&self, class: HttpTimeoutClass, outcome: TimeoutOutcome, elapsed: Duration) {
        saturating_increment(&self.timeout_counts[class.index()]);
        saturating_add(
            &self.timeout_elapsed_millis[class.index()],
            duration_millis_saturating(elapsed),
        );
        saturating_increment(&self.timeout_outcomes[outcome.index()]);
    }

    fn record_transport_timeout(&self, outcome: TimeoutOutcome, elapsed: Duration) {
        saturating_increment(&self.transport_timeout_count);
        saturating_add(
            &self.transport_timeout_elapsed_millis,
            duration_millis_saturating(elapsed),
        );
        saturating_increment(&self.timeout_outcomes[outcome.index()]);
    }
}

fn saturating_increment(counter: &AtomicU64) {
    saturating_add(counter, 1);
}

fn saturating_add(counter: &AtomicU64, amount: u64) {
    let _ = counter.try_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
        Some(current.saturating_add(amount))
    });
}

fn duration_millis_saturating(duration: Duration) -> u64 {
    u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
}

/// A point-in-time snapshot of HTTP metrics with plain u64 values.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct HttpMetricsSnapshot {
    /// Total number of logical HTTP operations entering the request pipeline
    pub logical_operations: u64,
    /// Total number of HTTP requests sent to the server
    pub total_requests: u64,
    /// Total number of physical HTTP attempts, including automatic replays
    pub physical_attempts: u64,
    /// Total number of multipart POST requests dispatched
    pub multipart_posts: u64,
    /// Total number of successful responses (2xx status codes)
    pub successful_responses: u64,
    /// Total number of error responses (non-2xx status codes, excluding rate limit errors)
    pub errors: u64,
    /// Total number of retry attempts
    pub retries: u64,
    /// Total bytes sent in request bodies
    pub bytes_sent: u64,
    /// Total bytes received in response bodies
    pub bytes_received: u64,
    /// Total number of rate limit errors (429 responses)
    pub rate_limit_errors: u64,
    /// Total seconds spent waiting for rate limit backoff
    pub rate_limit_delay_secs: u64,
    timeout_counts: [u64; HttpTimeoutClass::COUNT],
    timeout_elapsed_millis: [u64; HttpTimeoutClass::COUNT],
    transport_timeout_count: u64,
    transport_timeout_elapsed_millis: u64,
    timeout_outcomes: [u64; TimeoutOutcome::COUNT],
}

/// Cumulative observations for one timeout class.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct TimeoutMetricSnapshot {
    /// Number of observations.
    pub count: u64,
    /// Saturating cumulative elapsed milliseconds.
    pub elapsed_millis: u64,
}

impl HttpMetricsSnapshot {
    /// Returns observations for one library logical timeout class.
    #[must_use]
    pub const fn timeout(self, class: HttpTimeoutClass) -> TimeoutMetricSnapshot {
        TimeoutMetricSnapshot {
            count: self.timeout_counts[class.index()],
            elapsed_millis: self.timeout_elapsed_millis[class.index()],
        }
    }

    /// Returns caller transport timeout observations.
    #[must_use]
    pub const fn transport_timeouts(self) -> TimeoutMetricSnapshot {
        TimeoutMetricSnapshot {
            count: self.transport_timeout_count,
            elapsed_millis: self.transport_timeout_elapsed_millis,
        }
    }

    /// Returns observations for one timeout outcome.
    #[must_use]
    pub const fn timeout_outcome_count(self, outcome: TimeoutOutcome) -> u64 {
        self.timeout_outcomes[outcome.index()]
    }
}

impl std::fmt::Display for HttpMetricsSnapshot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "logical_operations={} requests={} physical_attempts={} multipart_posts={} success={} errors={} retries={} rate_limit={}/{}s sent={} recv={}",
            self.logical_operations,
            self.total_requests,
            self.physical_attempts,
            self.multipart_posts,
            self.successful_responses,
            self.errors,
            self.retries,
            self.rate_limit_errors,
            self.rate_limit_delay_secs,
            format_bytes(self.bytes_sent),
            format_bytes(self.bytes_received),
        )
    }
}

#[allow(clippy::cast_precision_loss)]
fn format_bytes(bytes: u64) -> String {
    if bytes < 1024 {
        format!("{bytes}B")
    } else if bytes < 1024 * 1024 {
        format!("{:.1}KB", bytes as f64 / 1024.0)
    } else {
        format!("{:.1}MB", (bytes / (1024 * 1024)) as f64)
    }
}

/// status codes where it's ok to retry and backoff
fn retry_for_status(code: StatusCode) -> bool {
    match code {
      StatusCode::TOO_MANY_REQUESTS /* 429 */ |
      StatusCode::GATEWAY_TIMEOUT /* 504 */ |
      StatusCode::REQUEST_TIMEOUT /* 408 */ => true,
      _ => false,
    }
}

fn mutation_status_is_indeterminate(status: StatusCode) -> bool {
    matches!(
        status,
        StatusCode::REQUEST_TIMEOUT | StatusCode::TOO_MANY_REQUESTS
    ) || status.is_server_error()
}

fn log_http_status(
    request: &HttpRequest,
    status: StatusCode,
    variant: &'static str,
    physical_attempt: u32,
) {
    error!(
        target: "anytype::http",
        error_variant = variant,
        http_status = status.as_u16(),
        http_method = %request.method,
        http_path = %diagnostic_path(&request.path),
        physical_attempt,
        "HTTP request failed"
    );
}

fn log_http_transport(request: &HttpRequest, physical_attempt: u32) {
    error!(
        target: "anytype::http",
        error_variant = "transport",
        http_method = %request.method,
        http_path = %diagnostic_path(&request.path),
        physical_attempt,
        "HTTP request failed"
    );
}

#[derive(Clone, Default)]
pub struct HttpRequest {
    pub method: Method,
    pub path: String,
    pub query: Vec<(String, String)>,
    pub body: Option<Bytes>,
}

/// Raw response data for endpoints whose status and headers are part of the
/// public contract, such as ranged and conditional file downloads.
pub(crate) struct RawHttpResponse {
    pub(crate) status: StatusCode,
    pub(crate) headers: HeaderMap,
    pub(crate) body: Bytes,
}

/// Successful HTTP streaming response with established-stream deadlines.
pub(crate) struct StreamingHttpResponse {
    chunks: BoxStream<'static, Result<Bytes>>,
}

impl StreamingHttpResponse {
    pub(crate) fn bytes_stream(self) -> BoxStream<'static, Result<Bytes>> {
        self.chunks
    }
}

struct EstablishedSseState {
    chunks: BoxStream<'static, std::result::Result<Bytes, reqwest::Error>>,
    metrics: Arc<HttpMetrics>,
    path: String,
    started: tokio::time::Instant,
    last_progress: tokio::time::Instant,
    idle: Option<Duration>,
    lifetime: Option<Duration>,
    terminated: bool,
}

impl EstablishedSseState {
    async fn next(mut self) -> Option<(Result<Bytes>, Self)> {
        if self.terminated {
            return None;
        }
        let idle_deadline = self
            .idle
            .and_then(|idle| self.last_progress.checked_add(idle));
        let lifetime_deadline = self
            .lifetime
            .and_then(|lifetime| self.started.checked_add(lifetime));
        let now = tokio::time::Instant::now();
        if lifetime_deadline.is_some_and(|deadline| now >= deadline) {
            return Some(self.terminate_with_timeout(HttpTimeoutClass::SseLifetime));
        }
        if idle_deadline.is_some_and(|deadline| now >= deadline) {
            return Some(self.terminate_with_timeout(HttpTimeoutClass::SseIdle));
        }
        tokio::select! {
            biased;
            () = wait_until(lifetime_deadline) => {
                Some(self.terminate_with_timeout(HttpTimeoutClass::SseLifetime))
            },
            () = wait_until(idle_deadline) => {
                Some(self.terminate_with_timeout(HttpTimeoutClass::SseIdle))
            },
            chunk = self.chunks.next() => match chunk {
                Some(Ok(chunk)) => {
                    if !chunk.is_empty() {
                        self.last_progress = tokio::time::Instant::now();
                    }
                    Some((Ok(chunk), self))
                }
                Some(Err(source)) => {
                    self.terminated = true;
                    self.metrics.increment_errors();
                    let error = if source.is_timeout() {
                        let elapsed = self.started.elapsed();
                        self.metrics.record_transport_timeout(
                            TimeoutOutcome::StreamTerminated,
                            elapsed,
                        );
                        warn!(
                            target: "anytype::http",
                            error_variant = "transport_timeout",
                            timeout_outcome = %TimeoutOutcome::StreamTerminated,
                            elapsed_millis = duration_millis_saturating(elapsed),
                            http_method = "GET",
                            http_path = %diagnostic_path(&self.path),
                            physical_attempt = 1_u32,
                            "HTTP established stream transport timeout"
                        );
                        AnytypeError::Http {
                            method: "GET".to_owned(),
                            url: self.path.clone(),
                            source: reqwest::Error::without_url(source),
                            outcome: Some(TimeoutOutcome::StreamTerminated),
                            elapsed: Some(elapsed),
                            attempts: Some(1),
                        }
                    } else {
                        AnytypeError::ChatSseTransport {
                            path: diagnostic_path(&self.path),
                        }
                    };
                    self.terminate();
                    Some((Err(error), self))
                }
                None => None,
            },
        }
    }

    fn terminate_with_timeout(mut self, class: HttpTimeoutClass) -> (Result<Bytes>, Self) {
        let error = self.logical_timeout(class);
        self.terminate();
        (Err(error), self)
    }

    fn terminate(&mut self) {
        self.terminated = true;
        self.chunks = futures::stream::empty().boxed();
    }

    fn logical_timeout(&self, class: HttpTimeoutClass) -> AnytypeError {
        let elapsed = self.started.elapsed();
        self.metrics.increment_errors();
        self.metrics
            .record_timeout(class, TimeoutOutcome::StreamTerminated, elapsed);
        warn!(
            target: "anytype::http",
            error_variant = "logical_timeout",
            timeout_class = %class,
            timeout_outcome = %TimeoutOutcome::StreamTerminated,
            elapsed_millis = duration_millis_saturating(elapsed),
            http_method = "GET",
            http_path = %diagnostic_path(&self.path),
            physical_attempt = 1_u32,
            "HTTP established stream deadline expired"
        );
        AnytypeError::HttpTimeout {
            class,
            outcome: TimeoutOutcome::StreamTerminated,
            method: "GET".to_owned(),
            path: self.path.clone(),
            elapsed,
            attempts: 1,
        }
    }
}

async fn wait_until(deadline: Option<tokio::time::Instant>) {
    if let Some(deadline) = deadline {
        tokio::time::sleep_until(deadline).await;
    } else {
        std::future::pending::<()>().await;
    }
}

/// Result of a completed mutation whose exact HTTP rejection status is part of
/// the caller's safety decision.
pub(crate) enum PreservedStatusResponse<T> {
    /// The server returned a successful status and a valid response body.
    Success(T),
    /// The server definitively rejected the mutation with this exact status.
    Rejected { status: u16 },
    /// The server returned a status that cannot prove whether the mutation ran.
    Indeterminate { status: u16 },
}

impl fmt::Debug for HttpRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HttpRequest")
            .field("method", &self.method)
            .field("path", &diagnostic_path(&self.path))
            .field("query_fields", &self.query.len())
            .field("body", &self.body.as_ref().map_or(0, Bytes::len))
            .finish()
    }
}

impl HttpRequest {
    /// Create a new request with updated pagination parameters.
    /// This replaces any existing limit/offset query parameters.
    pub(crate) fn with_pagination(&self, offset: u32, limit: u32) -> Self {
        let mut new_query: Vec<(String, String)> = self
            .query
            .iter()
            .filter(|(key, _)| key != "offset" && key != "limit")
            .cloned()
            .collect();

        new_query.push(("limit".to_string(), limit.to_string()));
        new_query.push(("offset".to_string(), offset.to_string()));

        Self {
            method: self.method.clone(),
            path: self.path.clone(),
            query: new_query,
            body: self.body.clone(),
        }
    }
}

#[derive(Clone)]
pub struct HttpClient {
    pub client: reqwest::Client,

    /// Base URL for API requests (e.g., "<http://localhost:31009>")
    pub base_url: String,

    credential_state: Arc<Mutex<HttpCredentialState>>,

    limits: ValidationLimits,

    response_limits: ResponseLimits,

    // Max consecutive 429 retries before failing; 0 disables cap.
    rate_limit_max_retries: u32,

    timeout_policy: HttpTimeoutPolicy,

    /// HTTP request/response metrics
    pub metrics: Arc<HttpMetrics>,
}

#[derive(Clone)]
struct HttpCredentialState {
    credentials: HttpCredentials,
    generation: u64,
}

impl fmt::Debug for HttpClient {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HttpClient")
            .field("base_path", &diagnostic_path(&self.base_url))
            .field("api_key", &String::from("(MASKED)"))
            .field("rate_limit_max_retries", &self.rate_limit_max_retries)
            .field("timeout_policy", &self.timeout_policy)
            .field("metrics", &self.metrics)
            .finish_non_exhaustive()
    }
}

const MAX_DIAGNOSTIC_PATH_CHARS: usize = 512;
const REDACTED_DIAGNOSTIC_PATH: &str = "/[redacted]";

/// Returns bounded path-only context for HTTP diagnostics.
///
/// Valid absolute and scheme-relative HTTP URLs lose their scheme, authority,
/// userinfo, query, and fragment. Valid origin-form request targets lose query
/// and fragment values. Malformed, non-HTTP, control-bearing, and other target
/// forms fail closed to a fixed redaction marker rather than echoing input.
pub(crate) fn diagnostic_path(value: &str) -> String {
    let parsed = parse_diagnostic_target(value);
    let Some(path) = parsed.as_ref().map(Url::path) else {
        return REDACTED_DIAGNOSTIC_PATH.to_owned();
    };
    let mut redacted = String::with_capacity(path.len().min(MAX_DIAGNOSTIC_PATH_CHARS));
    for character in path.chars().take(MAX_DIAGNOSTIC_PATH_CHARS) {
        redacted.push(if character.is_control() {
            '�'
        } else {
            character
        });
    }
    if path.chars().count() > MAX_DIAGNOSTIC_PATH_CHARS {
        redacted.push('…');
    }
    if redacted.is_empty() {
        redacted.push('/');
    }
    redacted
}

fn parse_diagnostic_target(value: &str) -> Option<Url> {
    if value.is_empty()
        || value.chars().any(char::is_control)
        || value.chars().any(char::is_whitespace)
        || value.contains('\\')
        || has_invalid_percent_encoding(value)
    {
        return None;
    }
    let base = Url::parse("http://redacted.invalid/").ok()?;
    if let Some(remainder) = value.strip_prefix("//") {
        if remainder.starts_with('/') {
            return None;
        }
        let parsed = base.join(value).ok()?;
        return parsed.has_host().then_some(parsed);
    }
    if value.starts_with('/') {
        return base.join(value).ok();
    }
    let parsed = Url::parse(value).ok()?;
    let (_, remainder) = value.split_once(':')?;
    (matches!(parsed.scheme(), "http" | "https")
        && remainder.starts_with("//")
        && !remainder[2..].starts_with('/')
        && parsed.has_host())
    .then_some(parsed)
}

fn has_invalid_percent_encoding(value: &str) -> bool {
    let bytes = value.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] == b'%' {
            if index + 2 >= bytes.len()
                || !bytes[index + 1].is_ascii_hexdigit()
                || !bytes[index + 2].is_ascii_hexdigit()
            {
                return true;
            }
            index += 3;
        } else {
            index += 1;
        }
    }
    false
}

struct ParsedRetry {
    header: String,
    duration: Duration,
}

#[derive(Clone)]
struct OperationTiming {
    started: tokio::time::Instant,
    attempts: Arc<AtomicU32>,
}

impl OperationTiming {
    fn start() -> Self {
        Self {
            started: tokio::time::Instant::now(),
            attempts: Arc::new(AtomicU32::new(0)),
        }
    }

    fn record_attempt(&self) -> u32 {
        self.attempts
            .fetch_add(1, Ordering::Relaxed)
            .saturating_add(1)
    }

    fn attempts(&self) -> u32 {
        self.attempts.load(Ordering::Relaxed)
    }

    fn elapsed(&self) -> Duration {
        self.started.elapsed()
    }
}

/// Parse rate limit headers from a 429 response to determine retry duration.
/// Anytype Heart uses github.com/didip/tollbooth/v8 (v8.0.1), which sets
/// RateLimit-Reset and X-Rate-Limit-Duration as seconds to wait.
fn parse_retry_after(headers: &HeaderMap) -> Result<ParsedRetry> {
    for header_name in ["ratelimit-reset", "x-rate-limit-duration"] {
        if let Some(header_value) = headers.get(header_name)
            && let Ok(header) = header_value.to_str()
        {
            if let Ok(secs) = header.parse::<u64>() {
                return Ok(ParsedRetry {
                    duration: Duration::from_secs(secs),
                    header: header.to_string(),
                });
            }
            error!(header_name, "Could not parse HTTP 429 response header");
        }
    }

    // couldn't parse header
    Err(AnytypeError::RateLimitExceeded {
        header: "Received 429 response but couldn't parse rate limit headers. See logs".to_string(),
        duration: Duration::from_secs(0),
    })
}

impl HttpClient {
    pub fn new(
        builder: ClientBuilder,
        base_url: String,
        limits: ValidationLimits,
        response_limits: ResponseLimits,
        rate_limit_max_retries: u32,
        timeout_policy: HttpTimeoutPolicy,
        http_creds: HttpCredentials,
    ) -> Result<Self> {
        // Keep this middleware as the sole retry authority. Reqwest follows
        // redirects and retries protocol NACKs by default, and callers may
        // install an even broader policy on ClientBuilder. Either path sits
        // below send(), where method safety, attempt metrics, and mutation
        // dispatch state cannot be enforced. A redirect is surfaced as its
        // original 3xx response so callers can reject it without forwarding
        // credentials or replaying a body to another endpoint.
        let client = builder
            .redirect(reqwest::redirect::Policy::none())
            .retry(reqwest::retry::never())
            .build()
            .map_err(reqwest::Error::without_url)
            .map_err(|source| AnytypeError::Http {
                method: "client-init".to_owned(),
                url: String::new(),
                source,
                outcome: None,
                elapsed: None,
                attempts: None,
            })?;
        for (name, limit, maximum) in [
            (
                "json_bytes",
                response_limits.json_bytes,
                MAX_JSON_RESPONSE_BYTES,
            ),
            (
                "document_bytes",
                response_limits.document_bytes,
                MAX_DOCUMENT_RESPONSE_BYTES,
            ),
            (
                "error_bytes",
                response_limits.error_bytes,
                MAX_ERROR_RESPONSE_BYTES,
            ),
            (
                "file_bytes",
                response_limits.file_bytes,
                MAX_FILE_RESPONSE_BYTES,
            ),
            (
                "chat_sse_event_bytes",
                response_limits.chat_sse_event_bytes,
                crate::client::MAX_CHAT_SSE_EVENT_BYTES,
            ),
        ] {
            if limit == 0 || limit > maximum || usize::try_from(limit).is_err() {
                return Err(AnytypeError::Validation {
                    message: format!(
                        "response_limits.{name} must be between 1 and {maximum} bytes"
                    ),
                });
            }
        }
        Ok(Self {
            client,
            base_url,
            credential_state: Arc::new(Mutex::new(HttpCredentialState {
                credentials: http_creds,
                generation: 0,
            })),
            limits,
            response_limits,
            rate_limit_max_retries,
            timeout_policy,
            metrics: Arc::new(HttpMetrics::new()),
        })
    }

    /// Returns a snapshot of current HTTP metrics
    pub fn metrics_snapshot(&self) -> HttpMetricsSnapshot {
        self.metrics.snapshot()
    }

    async fn with_deadline<T>(
        &self,
        class: HttpTimeoutClass,
        outcome: TimeoutOutcome,
        method: &Method,
        path: &str,
        timing: &OperationTiming,
        operation: impl Future<Output = Result<T>>,
    ) -> Result<T> {
        // Boxing bounds every helper's composed future at one pointer, so
        // deep call chains (pagination, resolution, discovery) cannot grow
        // caller stack frames with the whole deadline-wrapped request state.
        // One heap allocation per logical request is negligible next to the
        // wire round trip.
        let operation = Box::pin(operation);
        let Some(duration) = self.timeout_policy.duration(class) else {
            return match operation.await {
                Err(AnytypeError::Http { source, .. }) => {
                    Err(self.classify_transport_error(source, method, path, timing))
                }
                result => result,
            };
        };
        let deadline =
            timing
                .started
                .checked_add(duration)
                .ok_or_else(|| AnytypeError::Validation {
                    message: format!("{class} HTTP deadline cannot be represented"),
                })?;
        match tokio::time::timeout_at(deadline, operation).await {
            Ok(Err(AnytypeError::Http { source, .. })) => {
                Err(self.classify_transport_error(source, method, path, timing))
            }
            // A completed result in hand is returned as-is, even when it
            // becomes ready on the same tick the deadline expires: the
            // deadline converts waiting into a timeout, never a completed
            // outcome, so success and typed errors are not discarded and
            // metrics count exactly one outcome per logical operation.
            // `ensure_before_dispatch` still prevents a new physical send at
            // or after the deadline instant.
            Ok(result) => result,
            Err(_) => Err(self.logical_timeout_error(class, outcome, method, path, timing)),
        }
    }

    fn logical_timeout_error(
        &self,
        class: HttpTimeoutClass,
        outcome: TimeoutOutcome,
        method: &Method,
        path: &str,
        timing: &OperationTiming,
    ) -> AnytypeError {
        let elapsed = timing.elapsed();
        let attempts = timing.attempts();
        self.metrics.increment_errors();
        self.metrics.record_timeout(class, outcome, elapsed);
        warn!(
            target: "anytype::http",
            error_variant = "logical_timeout",
            timeout_class = %class,
            timeout_outcome = %outcome,
            elapsed_millis = duration_millis_saturating(elapsed),
            http_method = %method,
            http_path = %diagnostic_path(path),
            physical_attempt = attempts,
            "HTTP logical deadline expired"
        );
        AnytypeError::HttpTimeout {
            class,
            outcome,
            method: method.as_str().to_owned(),
            path: path.to_owned(),
            elapsed,
            attempts,
        }
    }

    fn ensure_before_dispatch(
        &self,
        class: HttpTimeoutClass,
        outcome: TimeoutOutcome,
        method: &Method,
        path: &str,
        timing: &OperationTiming,
    ) -> Result<()> {
        let Some(duration) = self.timeout_policy.duration(class) else {
            return Ok(());
        };
        let deadline =
            timing
                .started
                .checked_add(duration)
                .ok_or_else(|| AnytypeError::Validation {
                    message: format!("{class} HTTP deadline cannot be represented"),
                })?;
        if tokio::time::Instant::now() >= deadline {
            return Err(self.logical_timeout_error(class, outcome, method, path, timing));
        }
        Ok(())
    }

    fn transport_error(
        &self,
        source: reqwest::Error,
        method: &Method,
        path: &str,
        _timing: &OperationTiming,
    ) -> AnytypeError {
        AnytypeError::Http {
            method: method.as_str().to_owned(),
            url: path.to_owned(),
            source,
            outcome: None,
            elapsed: None,
            attempts: None,
        }
    }

    fn classify_transport_error(
        &self,
        source: reqwest::Error,
        method: &Method,
        path: &str,
        timing: &OperationTiming,
    ) -> AnytypeError {
        let outcome = timeout_outcome(method);
        if source.is_timeout() {
            let elapsed = timing.elapsed();
            self.metrics.record_transport_timeout(outcome, elapsed);
            warn!(
                target: "anytype::http",
                error_variant = "transport_timeout",
                timeout_outcome = %outcome,
                elapsed_millis = duration_millis_saturating(elapsed),
                http_method = %method,
                http_path = %diagnostic_path(path),
                physical_attempt = timing.attempts(),
                "HTTP caller transport timeout"
            );
            AnytypeError::Http {
                method: method.as_str().to_owned(),
                url: path.to_owned(),
                source,
                outcome: Some(outcome),
                elapsed: Some(elapsed),
                attempts: Some(timing.attempts()),
            }
        } else if outcome == TimeoutOutcome::MutationIndeterminate
            && timing.attempts() > 0
            && !source.is_connect()
            && !source.is_builder()
        {
            // Connect and builder failures happen before any bytes reach the
            // peer, so the mutation was provably never dispatched and the
            // typed transport error (with its reqwest source) is returned
            // instead of an indeterminate outcome.
            AnytypeError::HttpMutationIndeterminate {
                method: method.as_str().to_owned(),
                path: path.to_owned(),
                attempts: timing.attempts(),
                status: None,
            }
        } else {
            AnytypeError::Http {
                method: method.as_str().to_owned(),
                url: path.to_owned(),
                source,
                outcome: None,
                elapsed: None,
                attempts: None,
            }
        }
    }

    fn mutation_indeterminate(
        method: &Method,
        path: &str,
        attempts: u32,
        status: StatusCode,
    ) -> AnytypeError {
        AnytypeError::HttpMutationIndeterminate {
            method: method.as_str().to_owned(),
            path: path.to_owned(),
            attempts,
            status: Some(status.as_u16()),
        }
    }

    pub(crate) const fn document_response_limit(&self) -> u64 {
        self.response_limits.document_bytes
    }

    pub(crate) const fn file_response_limit(&self) -> u64 {
        self.response_limits.file_bytes
    }

    pub(crate) const fn error_response_limit(&self) -> u64 {
        self.response_limits.error_bytes
    }

    /// Incrementally buffers one response up to `limit` bytes.
    ///
    /// A truthful oversized `Content-Length` is rejected before reading. The
    /// streamed total is still checked with overflow-safe arithmetic because
    /// the header may be absent or misleading. Capacity starts small rather
    /// than trusting an attacker-controlled advertised length.
    async fn read_bounded(
        &self,
        mut response: Response,
        limit: u64,
        method: &str,
        path: &str,
    ) -> Result<Bytes> {
        let declared = response.content_length();
        if declared.is_some_and(|length| length > limit) {
            return Err(AnytypeError::ResponseTooLarge { limit, declared });
        }

        const INITIAL_CAPACITY: u64 = 8 * 1024;
        let initial_capacity = limit
            .min(declared.unwrap_or(INITIAL_CAPACITY))
            .min(INITIAL_CAPACITY);
        let mut body = Vec::with_capacity(initial_capacity as usize);
        while let Some(chunk) = response
            .chunk()
            .await
            .map_err(reqwest::Error::without_url)
            .map_err(|source| AnytypeError::Http {
                method: method.to_owned(),
                url: path.to_owned(),
                source,
                outcome: None,
                elapsed: None,
                attempts: None,
            })?
        {
            self.metrics.add_bytes_received(chunk.len() as u64);
            let next_len = (body.len() as u64)
                .checked_add(chunk.len() as u64)
                .ok_or(AnytypeError::ResponseTooLarge { limit, declared })?;
            if next_len > limit {
                return Err(AnytypeError::ResponseTooLarge { limit, declared });
            }
            body.extend_from_slice(&chunk);
        }
        Ok(Bytes::from(body))
    }

    async fn read_error_body(
        &self,
        response: Response,
        method: &str,
        path: &str,
    ) -> Result<String> {
        let body = self
            .read_bounded(response, self.response_limits.error_bytes, method, path)
            .await?;
        Ok(String::from_utf8_lossy(&body).into_owned())
    }

    /// Returns true if `api_key` has been initialized.
    pub fn has_key(&self) -> bool {
        self.credential_state.lock().credentials.has_creds()
    }

    /// Sets the API key for authenticated requests.
    pub fn set_api_key(&self, api_key: HttpCredentials) {
        let mut state = self.credential_state.lock();
        state.credentials = api_key;
        state.generation = state.generation.saturating_add(1);
    }

    /// Clears the api key if set. (in memory, does not change keystore)
    pub fn clear_api_key(&self) {
        let mut state = self.credential_state.lock();
        state.credentials = HttpCredentials::default();
        state.generation = state.generation.saturating_add(1);
    }

    /// Returns the non-secret generation of the in-memory HTTP credentials.
    pub fn credential_generation(&self) -> u64 {
        self.credential_state.lock().generation
    }

    /// Returns http token from memory (Does not refresh from keystore)
    pub(crate) fn get_api_key(&self) -> HttpCredentials {
        self.credential_state.lock().credentials.clone()
    }

    /// Makes an authenticated DELETE request.
    pub(crate) async fn delete_request<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        let req = HttpRequest {
            method: Method::DELETE,
            path: path.into(),
            query: Vec::default(),
            body: None,
        };
        self.send(req).await
    }

    /// Makes one authenticated JSON DELETE without middleware retries.
    pub(crate) async fn delete_request_once<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        let req = HttpRequest {
            method: Method::DELETE,
            path: path.into(),
            query: Vec::default(),
            body: None,
        };
        self.send_with_limit_and_retries(req, self.response_limits.json_bytes, false)
            .await
    }

    /// Makes an authenticated DELETE whose successful JSON may contain a
    /// complete document body.
    pub(crate) async fn delete_document_request<T: DeserializeOwned>(
        &self,
        path: &str,
    ) -> Result<T> {
        self.delete_document_request_with_retries(path, true).await
    }

    /// Makes one authenticated DELETE whose successful JSON may contain a
    /// complete document body, without replaying the request in middleware.
    pub(crate) async fn delete_document_request_once<T: DeserializeOwned>(
        &self,
        path: &str,
    ) -> Result<T> {
        self.delete_document_request_with_retries(path, false).await
    }

    async fn delete_document_request_with_retries<T: DeserializeOwned>(
        &self,
        path: &str,
        allow_retries: bool,
    ) -> Result<T> {
        let req = HttpRequest {
            method: Method::DELETE,
            path: path.into(),
            query: Vec::default(),
            body: None,
        };
        self.send_with_limit_and_retries(req, self.response_limits.document_bytes, allow_retries)
            .await
    }

    pub(crate) async fn get_request<T: DeserializeOwned>(
        &self,
        path: &str,
        query: QueryWithFilters,
    ) -> Result<T> {
        query.validate().map_err(|err| AnytypeError::Validation {
            message: format!("get_request {} {err}", diagnostic_path(path)),
        })?;
        let req = HttpRequest {
            method: Method::GET,
            path: path.into(),
            query: query.params,
            body: None,
        };
        self.send(req).await
    }

    /// Makes an authenticated GET with an explicit finite response ceiling.
    pub(crate) async fn get_request_with_limit<T: DeserializeOwned>(
        &self,
        path: &str,
        query: QueryWithFilters,
        response_limit: u64,
    ) -> Result<T> {
        query.validate().map_err(|err| AnytypeError::Validation {
            message: format!("get_request_with_limit {} {err}", diagnostic_path(path)),
        })?;
        if response_limit == 0 || response_limit > self.response_limits.document_bytes {
            return Err(AnytypeError::Validation {
                message: format!(
                    "response limit must be between 1 and {} bytes",
                    self.response_limits.document_bytes
                ),
            });
        }
        let req = HttpRequest {
            method: Method::GET,
            path: path.into(),
            query: query.params,
            body: None,
        };
        self.send_with_limit(req, response_limit).await
    }

    /// Opens an authenticated streaming GET request.
    ///
    /// Unlike [`get_request`](Self::get_request), this returns the live
    /// response without buffering its body so callers can incrementally
    /// consume endpoints such as Server-Sent Events.
    pub(crate) async fn get_streaming_request(
        &self,
        path: &str,
        query: QueryWithFilters,
        headers: HeaderMap,
    ) -> Result<StreamingHttpResponse> {
        query.validate().map_err(|err| AnytypeError::Validation {
            message: format!("get_streaming_request {} {err}", diagnostic_path(path)),
        })?;
        self.limits.validate_query(&query.params)?;

        let api_key = self.get_api_key();
        let Some(token) = api_key.token() else {
            return Err(AnytypeError::Auth {
                message: "HTTP credentials missing token. Client is not authenticated.".to_owned(),
            });
        };
        let full_url = format!("{}{}", self.base_url, path);
        debug!(path = %diagnostic_path(path), "get_streaming_request");
        self.metrics.increment_logical_operations();
        let open_timing = OperationTiming::start();
        let response = self
            .with_deadline(
                HttpTimeoutClass::SseOpen,
                TimeoutOutcome::ReadAborted,
                &Method::GET,
                path,
                &open_timing,
                async {
                    self.ensure_before_dispatch(
                        HttpTimeoutClass::SseOpen,
                        TimeoutOutcome::ReadAborted,
                        &Method::GET,
                        path,
                        &open_timing,
                    )?;
                    open_timing.record_attempt();
                    self.metrics.increment_requests();
                    self.client
                        .get(&full_url)
                        .query(&query.params)
                        .header(ANYTYPE_API_HEADER, ANYTYPE_API_VERSION)
                        .bearer_auth(token)
                        .headers(headers)
                        .send()
                        .await
                        .map_err(reqwest::Error::without_url)
                        .map_err(|source| {
                            if source.is_timeout() {
                                self.transport_error(source, &Method::GET, path, &open_timing)
                            } else {
                                AnytypeError::ChatSseTransport {
                                    path: diagnostic_path(path),
                                }
                            }
                        })
                },
            )
            .await?;

        if !response.status().is_success() {
            self.metrics.increment_errors();
            let code = response.status().as_u16();
            let error_timing = OperationTiming::start();
            self.ensure_before_dispatch(
                HttpTimeoutClass::SseErrorBody,
                TimeoutOutcome::ReadAborted,
                &Method::GET,
                path,
                &error_timing,
            )?;
            error_timing.record_attempt();
            let message = self
                .with_deadline(
                    HttpTimeoutClass::SseErrorBody,
                    TimeoutOutcome::ReadAborted,
                    &Method::GET,
                    path,
                    &error_timing,
                    self.read_error_body(response, "get", path),
                )
                .await?;
            return Err(AnytypeError::ApiError {
                code,
                method: "get".to_string(),
                url: path.to_string(),
                message,
            });
        }

        self.metrics.increment_success();
        let established = tokio::time::Instant::now();
        let state = EstablishedSseState {
            chunks: response.bytes_stream().boxed(),
            metrics: self.metrics.clone(),
            path: path.to_owned(),
            started: established,
            last_progress: established,
            idle: self.timeout_policy.sse_idle,
            lifetime: self.timeout_policy.sse_total_lifetime,
            terminated: false,
        };
        Ok(StreamingHttpResponse {
            chunks: futures::stream::unfold(state, EstablishedSseState::next).boxed(),
        })
    }

    /// Makes an authenticated PATCH request with JSON body.
    pub(crate) async fn patch_request<T: DeserializeOwned, B: Serialize + Sync>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, AnytypeError> {
        let req = HttpRequest {
            method: Method::PATCH,
            path: path.into(),
            query: Vec::default(),
            body: Some(Bytes::from(
                serde_json::to_vec(body)
                    .map_err(|source| AnytypeError::Serialization { source })?,
            )),
        };
        self.send(req).await
    }

    /// Makes an authenticated PATCH whose successful JSON may contain a
    /// complete document body.
    pub(crate) async fn patch_document_request<T: DeserializeOwned, B: Serialize + Sync>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, AnytypeError> {
        let req = HttpRequest {
            method: Method::PATCH,
            path: path.into(),
            query: Vec::default(),
            body: Some(Bytes::from(
                serde_json::to_vec(body)
                    .map_err(|source| AnytypeError::Serialization { source })?,
            )),
        };
        self.send_with_limit(req, self.response_limits.document_bytes)
            .await
    }

    pub(crate) async fn post_request<T: DeserializeOwned, B: Serialize + Sync>(
        &self,
        path: &str,
        body: &B,
        query: QueryWithFilters,
    ) -> Result<T> {
        let req = HttpRequest {
            method: Method::POST,
            path: path.into(),
            query: query.params,
            body: Some(Bytes::from(
                serde_json::to_vec(body)
                    .map_err(|source| AnytypeError::Serialization { source })?,
            )),
        };
        self.send(req).await
    }

    /// Makes one authenticated POST attempt while preserving its exact
    /// completed non-success status.
    ///
    /// This path is intentionally narrow: it never follows redirects, retries,
    /// or maps statuses into broader [`AnytypeError`] variants. Transport,
    /// response-read, and deserialization failures remain errors because they
    /// do not prove the mutation outcome.
    pub(crate) async fn post_request_preserve_status<T: DeserializeOwned, B: Serialize + Sync>(
        &self,
        path: &str,
        body: &B,
        query: QueryWithFilters,
    ) -> Result<PreservedStatusResponse<T>> {
        let req = HttpRequest {
            method: Method::POST,
            path: path.into(),
            query: query.params,
            body: Some(Bytes::from(
                serde_json::to_vec(body)
                    .map_err(|source| AnytypeError::Serialization { source })?,
            )),
        };
        self.send_preserving_status(req).await
    }

    /// Makes an authenticated POST whose successful JSON may contain a
    /// complete document body.
    pub(crate) async fn post_document_request<T: DeserializeOwned, B: Serialize + Sync>(
        &self,
        path: &str,
        body: &B,
        query: QueryWithFilters,
    ) -> Result<T> {
        let req = HttpRequest {
            method: Method::POST,
            path: path.into(),
            query: query.params,
            body: Some(Bytes::from(
                serde_json::to_vec(body)
                    .map_err(|source| AnytypeError::Serialization { source })?,
            )),
        };
        self.send_with_limit(req, self.response_limits.document_bytes)
            .await
    }

    /// Makes an unauthenticated POST request (for auth endpoints).
    pub(crate) async fn post_unauthenticated<Resp: DeserializeOwned, Req: Serialize + Sync>(
        &self,
        path: &str,
        body: &Req,
    ) -> Result<Resp> {
        let timing = OperationTiming::start();
        self.with_deadline(
            HttpTimeoutClass::StandardOperation,
            TimeoutOutcome::MutationIndeterminate,
            &Method::POST,
            path,
            &timing,
            self.post_unauthenticated_inner(path, body, timing.clone()),
        )
        .await
    }

    async fn post_unauthenticated_inner<Resp: DeserializeOwned, Req: Serialize + Sync>(
        &self,
        path: &str,
        body: &Req,
        timing: OperationTiming,
    ) -> Result<Resp> {
        let full_url = format!("{}{}", self.base_url, path);
        debug!(path = %diagnostic_path(path), "post_unauthenticated");
        self.metrics.increment_logical_operations();
        self.ensure_before_dispatch(
            HttpTimeoutClass::StandardOperation,
            TimeoutOutcome::MutationIndeterminate,
            &Method::POST,
            path,
            &timing,
        )?;
        timing.record_attempt();
        self.metrics.increment_requests();
        let response = self
            .client
            .post(&full_url)
            .header(ANYTYPE_API_HEADER, ANYTYPE_API_VERSION)
            .json(body)
            .send()
            .await
            .map_err(reqwest::Error::without_url)
            .map_err(|source| self.transport_error(source, &Method::POST, path, &timing))?;
        if !response.status().is_success() {
            self.metrics.increment_errors();
            let status = response.status();
            let code = status.as_u16();
            // The ambiguous status alone fixes the classification, so the
            // error-body read is best-effort diagnostics and must not mask
            // the indeterminate outcome.
            let body = self.read_error_body(response, "post", path).await;
            if mutation_status_is_indeterminate(status) {
                return Err(Self::mutation_indeterminate(
                    &Method::POST,
                    path,
                    timing.attempts(),
                    status,
                ));
            }
            let message = body?;
            return Err(AnytypeError::ApiError {
                code,
                method: "post".to_string(),
                url: path.to_string(),
                message,
            });
        }
        let data = match self
            .read_bounded(response, self.response_limits.json_bytes, "post", path)
            .await
        {
            Ok(data) => data,
            Err(error) => {
                self.metrics.increment_errors();
                return Err(error);
            }
        };
        self.metrics.increment_success();
        deserialize_json(&data)
    }

    /// Makes an authenticated DELETE request that expects an empty
    /// (`204 No Content`) response body.
    ///
    /// The JSON [`delete_request`](Self::delete_request) helper deserializes a
    /// response entity; this variant is for non-file endpoints that return
    /// `204` with no body (currently chat message deletion). REST file
    /// deletion routes through
    /// [`file_request`](Self::file_request) instead, which applies the long
    /// timeout profile that the observed ~154-second permanent delete
    /// requires. Do not route file deletes back through this
    /// standard-profile helper.
    pub(crate) async fn delete_no_content(&self, path: &str) -> Result<()> {
        let timing = OperationTiming::start();
        self.with_deadline(
            HttpTimeoutClass::StandardOperation,
            TimeoutOutcome::MutationIndeterminate,
            &Method::DELETE,
            path,
            &timing,
            self.delete_no_content_inner(path, timing.clone()),
        )
        .await
    }

    async fn delete_no_content_inner(&self, path: &str, timing: OperationTiming) -> Result<()> {
        let api_key = self.get_api_key();
        let Some(token) = api_key.token() else {
            return Err(AnytypeError::Auth {
                message: "HTTP credentials missing token. Client is not authenticated.".to_owned(),
            });
        };
        let full_url = format!("{}{}", self.base_url, path);
        debug!(path = %diagnostic_path(path), "delete_no_content");
        self.metrics.increment_logical_operations();
        self.ensure_before_dispatch(
            HttpTimeoutClass::StandardOperation,
            TimeoutOutcome::MutationIndeterminate,
            &Method::DELETE,
            path,
            &timing,
        )?;
        timing.record_attempt();
        self.metrics.increment_requests();
        let response = self
            .client
            .delete(&full_url)
            .header(ANYTYPE_API_HEADER, ANYTYPE_API_VERSION)
            .bearer_auth(token)
            .send()
            .await
            .map_err(reqwest::Error::without_url)
            .map_err(|source| self.transport_error(source, &Method::DELETE, path, &timing))?;
        if !response.status().is_success() {
            self.metrics.increment_errors();
            let status = response.status();
            let code = status.as_u16();
            // The ambiguous status alone fixes the classification, so the
            // error-body read is best-effort diagnostics and must not mask
            // the indeterminate outcome.
            let body = self.read_error_body(response, "delete", path).await;
            if mutation_status_is_indeterminate(status) {
                return Err(Self::mutation_indeterminate(
                    &Method::DELETE,
                    path,
                    timing.attempts(),
                    status,
                ));
            }
            let message = body?;
            return Err(AnytypeError::ApiError {
                code,
                method: "delete".to_string(),
                url: path.to_string(),
                message,
            });
        }
        self.metrics.increment_success();
        Ok(())
    }

    /// Makes an authenticated file request while preserving response metadata.
    ///
    /// In addition to successful responses, the statuses produced by HTTP
    /// range and precondition handling are returned to the caller. Other
    /// non-success statuses retain the client's usual [`AnytypeError::ApiError`]
    /// behavior.
    pub(crate) async fn file_request(
        &self,
        method: Method,
        path: &str,
        query: &[(String, String)],
        headers: HeaderMap,
    ) -> Result<RawHttpResponse> {
        self.file_request_with_limits(
            method,
            path,
            query,
            headers,
            self.response_limits.file_bytes,
            self.response_limits.error_bytes,
            crate::files::DEFAULT_FILE_HEADER_EVIDENCE_BYTES,
            1,
        )
        .await
    }

    /// Makes an authenticated file request under caller-specific finite limits.
    #[allow(clippy::too_many_arguments, clippy::too_many_lines)]
    pub(crate) async fn file_request_with_limits(
        &self,
        method: Method,
        path: &str,
        query: &[(String, String)],
        headers: HeaderMap,
        success_body_limit: u64,
        error_body_limit: u64,
        header_evidence_limit: u64,
        max_attempts: u32,
    ) -> Result<RawHttpResponse> {
        let timing = OperationTiming::start();
        let diagnostic_method = method.clone();
        self.with_deadline(
            HttpTimeoutClass::LongOperation,
            timeout_outcome(&diagnostic_method),
            &diagnostic_method,
            path,
            &timing,
            self.file_request_with_limits_inner(
                method,
                path,
                query,
                headers,
                success_body_limit,
                error_body_limit,
                header_evidence_limit,
                max_attempts,
                timing.clone(),
            ),
        )
        .await
    }

    #[allow(clippy::too_many_arguments, clippy::too_many_lines)]
    async fn file_request_with_limits_inner(
        &self,
        method: Method,
        path: &str,
        query: &[(String, String)],
        headers: HeaderMap,
        success_body_limit: u64,
        error_body_limit: u64,
        header_evidence_limit: u64,
        max_attempts: u32,
        timing: OperationTiming,
    ) -> Result<RawHttpResponse> {
        if success_body_limit == 0 || success_body_limit > self.response_limits.file_bytes {
            return Err(AnytypeError::Validation {
                message: format!(
                    "file response limit must be between 1 and {} bytes",
                    self.response_limits.file_bytes
                ),
            });
        }
        if error_body_limit == 0 || error_body_limit > self.response_limits.error_bytes {
            return Err(AnytypeError::Validation {
                message: format!(
                    "file error response limit must be between 1 and {} bytes",
                    self.response_limits.error_bytes
                ),
            });
        }
        if header_evidence_limit == 0
            || header_evidence_limit > crate::files::MAX_FILE_HEADER_EVIDENCE_BYTES
        {
            return Err(AnytypeError::Validation {
                message: format!(
                    "file header evidence limit must be between 1 and {} bytes",
                    crate::files::MAX_FILE_HEADER_EVIDENCE_BYTES
                ),
            });
        }
        if max_attempts == 0 || max_attempts > crate::files::MAX_FILE_REQUEST_ATTEMPTS {
            return Err(AnytypeError::Validation {
                message: format!(
                    "file request attempts must be between 1 and {}",
                    crate::files::MAX_FILE_REQUEST_ATTEMPTS
                ),
            });
        }
        let api_key = self.get_api_key();
        let Some(token) = api_key.token() else {
            return Err(AnytypeError::Auth {
                message: "HTTP credentials missing token. Client is not authenticated.".to_owned(),
            });
        };
        let full_url = format!("{}{}", self.base_url, path);
        debug!(method = %method, path = %diagnostic_path(path), "file_request");
        let replay_safe = matches!(method, Method::GET | Method::HEAD);
        self.metrics.increment_logical_operations();
        loop {
            self.ensure_before_dispatch(
                HttpTimeoutClass::LongOperation,
                timeout_outcome(&method),
                &method,
                path,
                &timing,
            )?;
            let attempts = timing.record_attempt();
            self.metrics.increment_requests();
            let sent = self
                .client
                .request(method.clone(), &full_url)
                .query(query)
                .header(ANYTYPE_API_HEADER, ANYTYPE_API_VERSION)
                .bearer_auth(token)
                .headers(headers.clone())
                .send()
                .await
                .map_err(reqwest::Error::without_url);
            let response = match sent {
                Ok(response) => response,
                Err(source) if replay_safe && attempts < max_attempts && source.is_connect() => {
                    self.metrics.increment_retries();
                    log_and_backoff(attempts - 1, "file transport failure").await;
                    continue;
                }
                Err(source) => {
                    self.metrics.increment_errors();
                    return Err(self.transport_error(source, &method, path, &timing));
                }
            };
            let status = response.status();
            // Enforce the allowlisted-header evidence budget before any retry
            // decision or body read. Every physical response is independently
            // bounded, including intermediate 429 and retryable-status frames.
            crate::files::retained_file_header_bytes(
                response.headers(),
                status,
                header_evidence_limit,
            )?;

            if replay_safe && attempts < max_attempts && status == StatusCode::TOO_MANY_REQUESTS {
                let ParsedRetry { header, duration } = parse_retry_after(response.headers())?;
                if duration > Duration::from_secs(RATE_LIMIT_WAIT_MAX_SECS) {
                    self.metrics.increment_errors();
                    return Err(AnytypeError::RateLimitExceeded { header, duration });
                }
                // Bound and discard every retry response before replaying. This
                // keeps per-attempt evidence within the caller's error policy.
                if method != Method::HEAD {
                    self.read_bounded(response, error_body_limit, method.as_str(), path)
                        .await?;
                }
                self.metrics.increment_rate_limit_errors();
                self.metrics.increment_retries();
                self.metrics.add_rate_limit_delay(duration.as_secs());
                tokio::time::sleep(duration).await;
                continue;
            }

            if replay_safe && attempts < max_attempts && retry_for_status(status) {
                if method != Method::HEAD {
                    self.read_bounded(response, error_body_limit, method.as_str(), path)
                        .await?;
                }
                self.metrics.increment_errors();
                self.metrics.increment_retries();
                log_and_backoff(attempts - 1, "retryable file HTTP status").await;
                continue;
            }

            let response_headers = response.headers().clone();
            let allowed_control_status = matches!(
                status,
                StatusCode::NOT_MODIFIED
                    | StatusCode::PRECONDITION_FAILED
                    | StatusCode::RANGE_NOT_SATISFIABLE
            );
            let body = if method == Method::HEAD || status == StatusCode::NOT_MODIFIED {
                // HEAD and 304 legitimately carry representation metadata such as
                // a Content-Length while having no response body to buffer.
                Bytes::new()
            } else {
                let body_limit = if status.is_success() {
                    success_body_limit
                } else {
                    error_body_limit
                };
                match self
                    .read_bounded(response, body_limit, method.as_str(), path)
                    .await
                {
                    Ok(body) => body,
                    Err(error) => {
                        self.metrics.increment_errors();
                        // A failed error-body read must not mask an already
                        // known ambiguous mutation status; the body is
                        // best-effort diagnostics.
                        if !replay_safe
                            && !(status.is_success() || allowed_control_status)
                            && mutation_status_is_indeterminate(status)
                        {
                            return Err(Self::mutation_indeterminate(
                                &method, path, attempts, status,
                            ));
                        }
                        return Err(error);
                    }
                }
            };

            if status.is_success() {
                self.metrics.increment_success();
            } else {
                self.metrics.increment_errors();
            }

            if !(status.is_success() || allowed_control_status) {
                if !replay_safe && mutation_status_is_indeterminate(status) {
                    return Err(Self::mutation_indeterminate(
                        &method, path, attempts, status,
                    ));
                }
                return Err(AnytypeError::ApiError {
                    code: status.as_u16(),
                    method: method.as_str().to_ascii_lowercase(),
                    url: path.to_string(),
                    message: String::from_utf8_lossy(&body).into_owned(),
                });
            }

            return Ok(RawHttpResponse {
                status,
                headers: response_headers,
                body,
            });
        }
    }

    /// Makes one non-replayed multipart POST under caller-specific byte limits.
    #[allow(clippy::too_many_arguments)]
    pub(crate) async fn post_multipart_with_limits<T: DeserializeOwned>(
        &self,
        path: &str,
        form: reqwest::multipart::Form,
        serialized_body_bytes: Option<u64>,
        request_body_limit: Option<u64>,
        response_body_limit: Option<u64>,
        error_body_limit: Option<u64>,
    ) -> Result<T> {
        let timing = OperationTiming::start();
        self.with_deadline(
            HttpTimeoutClass::LongOperation,
            TimeoutOutcome::MutationIndeterminate,
            &Method::POST,
            path,
            &timing,
            self.post_multipart_with_limits_inner(
                path,
                form,
                serialized_body_bytes,
                request_body_limit,
                response_body_limit,
                error_body_limit,
                timing.clone(),
            ),
        )
        .await
    }

    #[allow(clippy::too_many_arguments)]
    async fn post_multipart_with_limits_inner<T: DeserializeOwned>(
        &self,
        path: &str,
        form: reqwest::multipart::Form,
        serialized_body_bytes: Option<u64>,
        request_body_limit: Option<u64>,
        response_body_limit: Option<u64>,
        error_body_limit: Option<u64>,
        timing: OperationTiming,
    ) -> Result<T> {
        if let Some(limit) = request_body_limit {
            if limit == 0 {
                return Err(AnytypeError::Validation {
                    message: "multipart request limit must be nonzero".to_owned(),
                });
            }
            let actual = serialized_body_bytes.ok_or_else(|| AnytypeError::Validation {
                message: "multipart request length is unavailable".to_owned(),
            })?;
            if actual > limit {
                return Err(AnytypeError::Validation {
                    message: format!(
                        "multipart request body exceeds the {limit}-byte request limit"
                    ),
                });
            }
        }
        let response_body_limit = response_body_limit.unwrap_or(self.response_limits.json_bytes);
        if response_body_limit == 0 || response_body_limit > self.response_limits.json_bytes {
            return Err(AnytypeError::Validation {
                message: format!(
                    "multipart response limit must be between 1 and {} bytes",
                    self.response_limits.json_bytes
                ),
            });
        }
        let error_body_limit = error_body_limit.unwrap_or(self.response_limits.error_bytes);
        if error_body_limit == 0 || error_body_limit > self.response_limits.error_bytes {
            return Err(AnytypeError::Validation {
                message: format!(
                    "multipart error response limit must be between 1 and {} bytes",
                    self.response_limits.error_bytes
                ),
            });
        }
        let api_key = self.get_api_key();
        let Some(token) = api_key.token() else {
            return Err(AnytypeError::Auth {
                message: "HTTP credentials missing token. Client is not authenticated.".to_owned(),
            });
        };
        let full_url = format!("{}{}", self.base_url, path);
        debug!(path = %diagnostic_path(path), "post_multipart");
        self.metrics.increment_logical_operations();
        self.ensure_before_dispatch(
            HttpTimeoutClass::LongOperation,
            TimeoutOutcome::MutationIndeterminate,
            &Method::POST,
            path,
            &timing,
        )?;
        timing.record_attempt();
        self.metrics.increment_requests();
        self.metrics.increment_multipart_posts();
        if let Some(actual) = serialized_body_bytes {
            self.metrics.add_bytes_sent(actual);
        }
        let response = self
            .client
            .post(&full_url)
            .header(ANYTYPE_API_HEADER, ANYTYPE_API_VERSION)
            .bearer_auth(token)
            .multipart(form)
            .send()
            .await
            .map_err(reqwest::Error::without_url)
            .map_err(|source| self.transport_error(source, &Method::POST, path, &timing))?;
        if !response.status().is_success() {
            self.metrics.increment_errors();
            let status = response.status();
            let code = status.as_u16();
            // The ambiguous status alone fixes the classification, so the
            // error-body read is best-effort diagnostics and must not mask
            // the indeterminate outcome.
            let body = self
                .read_bounded(response, error_body_limit, "post", path)
                .await;
            if mutation_status_is_indeterminate(status) {
                return Err(Self::mutation_indeterminate(
                    &Method::POST,
                    path,
                    timing.attempts(),
                    status,
                ));
            }
            let message = String::from_utf8_lossy(&body?).into_owned();
            return Err(AnytypeError::ApiError {
                code,
                method: "post".to_string(),
                url: path.to_string(),
                message,
            });
        }
        let data = match self
            .read_bounded(response, response_body_limit, "post", path)
            .await
        {
            Ok(data) => data,
            Err(error) => {
                self.metrics.increment_errors();
                return Err(error);
            }
        };
        self.metrics.increment_success();
        deserialize_json(&data)
    }

    /// This function handles all authenticated anytype rest api requests (http: get,post,patch,delete)
    /// - handles 429 rate limit feedback
    /// - retries up to N(=3) times for connection failures or server timeout
    /// - maps http error codes into `AnytypeErrors`
    /// - deserializes json response body into return type T
    pub(crate) async fn send<T: DeserializeOwned>(&self, req: HttpRequest) -> Result<T> {
        self.send_with_limit(req, self.response_limits.json_bytes)
            .await
    }

    async fn send_preserving_status<T: DeserializeOwned>(
        &self,
        req: HttpRequest,
    ) -> Result<PreservedStatusResponse<T>> {
        let timing = OperationTiming::start();
        let method = req.method.clone();
        let path = req.path.clone();
        self.with_deadline(
            HttpTimeoutClass::StandardOperation,
            timeout_outcome(&method),
            &method,
            &path,
            &timing,
            self.send_preserving_status_inner(req, timing.clone()),
        )
        .await
    }

    async fn send_preserving_status_inner<T: DeserializeOwned>(
        &self,
        req: HttpRequest,
        timing: OperationTiming,
    ) -> Result<PreservedStatusResponse<T>> {
        self.limits.validate_query(&req.query)?;
        if let Some(ref body) = req.body {
            self.limits.validate_body(
                body,
                &format!("http {} {}", req.method, diagnostic_path(&req.path)),
            )?;
        }
        let api_key = self.get_api_key();
        let Some(token) = api_key.token() else {
            return Err(AnytypeError::Auth {
                message: "HTTP credentials missing token. Client is not authenticated.".to_owned(),
            });
        };
        self.metrics.increment_logical_operations();
        let full_url = format!("{}{}", self.base_url, req.path);
        let body = req.body.clone().unwrap_or_default();
        let body_size = body.len() as u64;
        log_request(&req);
        self.ensure_before_dispatch(
            HttpTimeoutClass::StandardOperation,
            timeout_outcome(&req.method),
            &req.method,
            &req.path,
            &timing,
        )?;
        let physical_attempt = timing.record_attempt();
        self.metrics.increment_requests();
        self.metrics.add_bytes_sent(body_size);
        debug!(
            target: "anytype::http",
            http_method = %req.method,
            http_path = %diagnostic_path(&req.path),
            physical_attempt,
            "HTTP physical attempt"
        );
        let response = self
            .client
            .request(req.method.clone(), full_url)
            .query(&req.query)
            .header(ANYTYPE_API_HEADER, ANYTYPE_API_VERSION)
            .bearer_auth(token)
            .body(body)
            .send()
            .await
            .map_err(reqwest::Error::without_url)
            .map_err(|source| {
                self.metrics.increment_errors();
                log_http_transport(&req, physical_attempt);
                self.transport_error(source, &req.method, &req.path, &timing)
            })?;
        let status = response.status();
        if !status.is_success() {
            if status == StatusCode::TOO_MANY_REQUESTS {
                self.metrics.increment_rate_limit_errors();
            } else {
                self.metrics.increment_errors();
            }
            log_http_status(&req, status, "preserved_status", physical_attempt);
            let indeterminate = mutation_status_is_indeterminate(status);
            let status = status.as_u16();
            return Ok(if indeterminate {
                PreservedStatusResponse::Indeterminate { status }
            } else {
                PreservedStatusResponse::Rejected { status }
            });
        }
        let body = match self
            .read_bounded(
                response,
                self.response_limits.json_bytes,
                req.method.as_str(),
                &req.path,
            )
            .await
        {
            Ok(body) => body,
            Err(error) => {
                self.metrics.increment_errors();
                return Err(error);
            }
        };
        log_response(&req.path, &body);
        let value = match deserialize_json(&body) {
            Ok(value) => value,
            Err(error) => {
                self.metrics.increment_errors();
                return Err(error);
            }
        };
        self.metrics.increment_success();
        Ok(PreservedStatusResponse::Success(value))
    }

    #[allow(clippy::too_many_lines)]
    pub(crate) async fn send_with_limit<T: DeserializeOwned>(
        &self,
        req: HttpRequest,
        response_limit: u64,
    ) -> Result<T> {
        self.send_with_limit_and_retries(req, response_limit, true)
            .await
    }

    #[allow(clippy::too_many_lines)]
    async fn send_with_limit_and_retries<T: DeserializeOwned>(
        &self,
        req: HttpRequest,
        response_limit: u64,
        allow_retries: bool,
    ) -> Result<T> {
        let timing = OperationTiming::start();
        let method = req.method.clone();
        let path = req.path.clone();
        let outcome = timeout_outcome(&method);
        self.with_deadline(
            HttpTimeoutClass::StandardOperation,
            outcome,
            &method,
            &path,
            &timing,
            self.send_with_limit_and_retries_inner(
                req,
                response_limit,
                allow_retries,
                timing.clone(),
            ),
        )
        .await
    }

    #[allow(clippy::too_many_lines)]
    async fn send_with_limit_and_retries_inner<T: DeserializeOwned>(
        &self,
        req: HttpRequest,
        response_limit: u64,
        allow_retries: bool,
        timing: OperationTiming,
    ) -> Result<T> {
        // A retry clones and replays the complete request. Restrict every
        // retry path, including rate-limit handling, to methods whose HTTP
        // semantics make that replay safe. In particular, a POST or PATCH
        // response proves only that a response arrived; it does not make the
        // mutation safe to send again.
        let retryable_method = allow_retries && is_idempotent_method(&req.method);
        // Preserve the existing stricter status/transport retry budget while
        // one request-lifetime counter prevents mixed retry classes from
        // exceeding the common physical-attempt ceiling.
        let mut retry_attempt = 0u32;
        let mut rate_limit_retries = 0u32;

        // time to wait on next iteration
        let mut retry_wait: Option<Duration> = None;

        // check for excessive request size or invalid query
        self.limits.validate_query(&req.query)?;
        if let Some(ref body) = req.body {
            self.limits.validate_body(
                body,
                &format!("http {} {}", req.method, diagnostic_path(&req.path)),
            )?;
        }
        let api_key = self.get_api_key();
        let Some(token) = api_key.token() else {
            return Err(AnytypeError::Auth {
                message: "HTTP credentials missing token. Client is not authenticated.".to_owned(),
            });
        };
        self.metrics.increment_logical_operations();
        let full_url = format!("{}{}", self.base_url, req.path);
        let req_builder = self
            .client
            .request(req.method.clone(), &full_url)
            .query(&req.query)
            .header(ANYTYPE_API_HEADER, ANYTYPE_API_VERSION)
            .bearer_auth(token);

        // debug log (if tracing enabled)
        log_request(&req);

        // Track bytes to be sent (body size)
        let body_size = req.body.as_ref().map_or(0, |bytes| bytes.len() as u64);

        loop {
            if let Some(wait_time) = retry_wait {
                info!("RateLimit: pausing for {} sec", wait_time.as_secs());
                tokio::time::sleep(wait_time).await;
                retry_wait = None;
            }
            self.ensure_before_dispatch(
                HttpTimeoutClass::StandardOperation,
                timeout_outcome(&req.method),
                &req.method,
                &req.path,
                &timing,
            )?;
            let request = req_builder
                .try_clone()
                .ok_or_else(|| {
                    // try_clone with no body should never return None
                    AnytypeError::Other {
                        message: "reqwest::RequestBuilder internal error".into(),
                    }
                })?
                .body(req.body.clone().unwrap_or_default());

            // Track request metrics
            let physical_attempt = timing.record_attempt();
            self.metrics.increment_requests();
            self.metrics.add_bytes_sent(body_size);
            debug!(
                target: "anytype::http",
                http_method = %req.method,
                http_path = %diagnostic_path(&req.path),
                physical_attempt,
                "HTTP physical attempt"
            );

            match request.send().await.map_err(reqwest::Error::without_url) {
                Ok(response) => {
                    let code = response.status();
                    if code != StatusCode::TOO_MANY_REQUESTS {
                        rate_limit_retries = 0;
                    }
                    match code {
                        // 2xx
                        // 201 (Object Created)
                        ok if ok.is_success() => {
                            // success - get the response body.
                            // If we fail to fully read the response, don't retry. The server might
                            // believe the request succeeded, and the request may not be idempotent.
                            // Most transient failures where we could have reasonably retried
                            // would have already occurred.
                            let body = match self
                                .read_bounded(
                                    response,
                                    response_limit,
                                    req.method.as_str(),
                                    &req.path,
                                )
                                .await
                            {
                                Ok(body) => body,
                                Err(error) => {
                                    self.metrics.increment_errors();
                                    return Err(error);
                                }
                            };
                            self.metrics.increment_success();

                            log_response(&req.path, &body);

                            // deserialization failure should not be retried
                            let resp_obj = deserialize_json(&body)?;
                            return Ok(resp_obj)
                        },
                        StatusCode::TOO_MANY_REQUESTS /* 429 */ => {
                            self.metrics.increment_rate_limit_errors();
                            if !retryable_method {
                                // The 429 alone fixes the classification;
                                // the error body is best-effort diagnostics
                                // and must not mask the indeterminate
                                // outcome.
                                let _best_effort_body = self.read_error_body(
                                    response,
                                    req.method.as_str(),
                                    &req.path,
                                )
                                .await;
                                return Err(Self::mutation_indeterminate(
                                    &req.method,
                                    &req.path,
                                    physical_attempt,
                                    code,
                                ));
                            }
                            rate_limit_retries = rate_limit_retries.saturating_add(1);
                            let headers = response.headers();
                            match parse_retry_after(headers) {
                                Err(err) => {
                                    error!(
                                        target: "anytype::http",
                                        error_variant = "invalid_rate_limit_header",
                                        http_status = code.as_u16(),
                                        http_method = %req.method,
                                        http_path = %diagnostic_path(&req.path),
                                        "HTTP request failed"
                                    );
                                    // couldn't parse header.
                                    return Err(err)
                                }
                                Ok(ParsedRetry{ header, duration}) => {
                                    self.read_error_body(
                                        response,
                                        req.method.as_str(),
                                        &req.path,
                                    )
                                    .await?;
                                    if self.rate_limit_max_retries > 0
                                        && rate_limit_retries > self.rate_limit_max_retries
                                    {
                                    error!(
                                            target: "anytype::http",
                                            error_variant = "rate_limit_retry_limit",
                                            http_status = code.as_u16(),
                                            http_method = %req.method,
                                            http_path = %diagnostic_path(&req.path),
                                            physical_attempt,
                                            "http 429 Rate-limit retries exceeded max={}",
                                            self.rate_limit_max_retries
                                        );
                                        return Err(AnytypeError::RateLimitExceeded {
                                            header,
                                            duration,
                                        });
                                    }
                                    if duration > Duration::from_secs(RATE_LIMIT_WAIT_MAX_SECS) {
                                        error!(
                                            target: "anytype::http",
                                            error_variant = "rate_limit_backoff_limit",
                                            http_status = code.as_u16(),
                                            http_method = %req.method,
                                            http_path = %diagnostic_path(&req.path),
                                            physical_attempt,
                                            "http 429 Rate-limit backoff={}s exceeds max",
                                            duration.as_secs()
                                        );
                                        return Err(AnytypeError::RateLimitExceeded {
                                            header,
                                            duration,
                                        });
                                    }
                                    if duration > Duration::from_secs(RATE_LIMIT_WAIT_WARN_SECS) {
                                        warn!(
                                            physical_attempt,
                                            "http 429 Rate-limit backoff={}s",
                                            duration.as_secs()
                                        );
                                    }
                                    if physical_attempt >= MAX_HTTP_REQUEST_ATTEMPTS {
                                        error!(
                                            target: "anytype::http",
                                            error_variant = "physical_attempt_limit",
                                            http_status = code.as_u16(),
                                            http_method = %req.method,
                                            http_path = %diagnostic_path(&req.path),
                                            physical_attempt,
                                            "HTTP physical-attempt ceiling reached"
                                        );
                                        return Err(AnytypeError::RateLimitExceeded {
                                            header,
                                            duration,
                                        });
                                    }
                                    self.metrics.increment_retries();
                                    self.metrics.add_rate_limit_delay(duration.as_secs());
                                    retry_wait = Some(duration);
                                    // continue to try again
                                }
                            }
                        }
                        StatusCode::BAD_REQUEST /* 400 */ => {
                            self.metrics.increment_errors();
                            let message = self.read_error_body(response, req.method.as_str(), &req.path).await?;
                            log_http_status(&req, code, "validation", physical_attempt);
                            return Err(AnytypeError::ApiError {
                                code: code.as_u16(),
                                method: req.method.to_string(),
                                url: req.path,
                                message,
                            })
                        }
                        StatusCode::NOT_FOUND /* 404 */ |
                        StatusCode::GONE /* 410 */
                         => {
                            self.metrics.increment_errors();
                            self.read_error_body(response, req.method.as_str(), &req.path).await?;
                            log_http_status(&req, code, "not_found", physical_attempt);
                            return Err(AnytypeError::NotFound{
                                // too generic here - we don't know whether the query
                                // needs to be reported at higher level
                                obj_type: "Object".into(),
                                key: String::default()
                            })
                        },
                        StatusCode::UNAUTHORIZED /* 401 */ => {
                            // client is not authenticated
                            self.metrics.increment_errors();
                            self.read_error_body(response, req.method.as_str(), &req.path).await?;
                            log_http_status(&req, code, "unauthorized", physical_attempt);
                            return Err(AnytypeError::Unauthorized)
                        }
                        StatusCode::FORBIDDEN /* 403 */ => {
                            // client is authenticated, but does not have permission to access the object
                            self.metrics.increment_errors();
                            self.read_error_body(response, req.method.as_str(), &req.path).await?;
                            log_http_status(&req, code, "forbidden", physical_attempt);
                            return Err(AnytypeError::Forbidden)
                        }
                        _ => {
                            self.metrics.increment_errors();
                            // The status alone fixes an indeterminate
                            // mutation classification, so the error-body
                            // read is best-effort diagnostics for that path
                            // and must not mask the outcome.
                            let body = self.read_error_body(response, req.method.as_str(), &req.path).await;
                            log_http_status(&req, code, "api_error", physical_attempt);
                            if !retryable_method
                                && (code.is_server_error() || retry_for_status(code))
                            {
                                return Err(Self::mutation_indeterminate(
                                    &req.method,
                                    &req.path,
                                    physical_attempt,
                                    code,
                                ));
                            }
                            let message = body?;
                            if retry_attempt < MAX_RETRIES
                                && physical_attempt < MAX_HTTP_REQUEST_ATTEMPTS
                                && retry_for_status(code)
                                && retryable_method
                            {
                                self.metrics.increment_retries();
                                log_and_backoff(retry_attempt, "retryable HTTP status").await;
                                retry_attempt += 1;
                                continue;
                            }
                            return Err(AnytypeError::ApiError{
                                code: code.as_u16(),
                                method: req.method.to_string(),
                                url: req.path,
                                message,
                            });
                        },
                    }
                }
                Err(err) => {
                    log_http_transport(&req, physical_attempt);
                    // Check for connection or timeout errors
                    if err.is_connect() && retryable_method {
                        rate_limit_retries = 0;
                        if retry_attempt < MAX_RETRIES
                            && physical_attempt < MAX_HTTP_REQUEST_ATTEMPTS
                        {
                            log_and_backoff(retry_attempt, "transport failure").await;
                            self.metrics.increment_retries();
                            retry_attempt += 1;
                            continue;
                        }
                        self.metrics.increment_errors();
                        return Err(self.transport_error(err, &req.method, &req.path, &timing));
                    }
                    // Other non-recoverable errors (e.g., DNS error, invalid URL, etc.)
                    self.metrics.increment_errors();
                    return Err(self.transport_error(err, &req.method, &req.path, &timing));
                }
            }
        }
    }
}

// The purpose of this trait is to define methods for Arc<HttpClient>
pub trait GetPaged {
    async fn get_request_paged<T: DeserializeOwned + Send + 'static>(
        &self,
        path: &str,
        query: QueryWithFilters,
    ) -> Result<super::paged::PagedResult<T>>;

    /// Makes a paginated GET whose every page uses the same response ceiling.
    async fn get_request_paged_with_limit<T: DeserializeOwned + Send + 'static>(
        &self,
        path: &str,
        query: QueryWithFilters,
        response_limit: u64,
    ) -> Result<super::paged::PagedResult<T>>;

    async fn post_request_paged<T: DeserializeOwned + Send + 'static, B: Serialize + Sync>(
        &self,
        path: &str,
        body: &B,
        query: QueryWithFilters,
    ) -> Result<super::paged::PagedResult<T>>;
}

impl GetPaged for Arc<HttpClient> {
    /// Makes an authenticated GET request that returns a `PagedResult` for pagination support.
    async fn get_request_paged<T: DeserializeOwned + Send + 'static>(
        &self,
        path: &str,
        query: QueryWithFilters,
    ) -> Result<super::paged::PagedResult<T>> {
        query.validate().map_err(|err| AnytypeError::Validation {
            message: format!("get_request_paged {} {err}", diagnostic_path(path)),
        })?;
        let req = HttpRequest {
            method: Method::GET,
            path: path.into(),
            query: query.params,
            body: None,
        };
        let response: PaginatedResponse<T> = self.send(req.clone()).await?;
        Ok(super::paged::PagedResult::new(
            response,
            self.clone(),
            req,
            None,
        ))
    }

    async fn get_request_paged_with_limit<T: DeserializeOwned + Send + 'static>(
        &self,
        path: &str,
        query: QueryWithFilters,
        response_limit: u64,
    ) -> Result<super::paged::PagedResult<T>> {
        query.validate().map_err(|err| AnytypeError::Validation {
            message: format!(
                "get_request_paged_with_limit {} {err}",
                diagnostic_path(path)
            ),
        })?;
        if response_limit == 0 || response_limit > self.response_limits.document_bytes {
            return Err(AnytypeError::Validation {
                message: format!(
                    "paged response limit must be between 1 and {} bytes",
                    self.response_limits.document_bytes
                ),
            });
        }
        let req = HttpRequest {
            method: Method::GET,
            path: path.into(),
            query: query.params,
            body: None,
        };
        let response: PaginatedResponse<T> =
            self.send_with_limit(req.clone(), response_limit).await?;
        Ok(super::paged::PagedResult::new(
            response,
            self.clone(),
            req,
            Some(response_limit),
        ))
    }

    /// Makes an authenticated POST request that returns a `PagedResult` for pagination support.
    async fn post_request_paged<T: DeserializeOwned + Send + 'static, B: Serialize + Sync>(
        &self,
        path: &str,
        body: &B,
        query: QueryWithFilters,
    ) -> Result<super::paged::PagedResult<T>> {
        query.validate().map_err(|err| AnytypeError::Validation {
            message: format!("post_request_paged {} {err}", diagnostic_path(path)),
        })?;
        let req = HttpRequest {
            method: Method::POST,
            path: path.into(),
            query: query.params,
            body: Some(Bytes::from(
                serde_json::to_vec(body)
                    .map_err(|source| AnytypeError::Serialization { source })?,
            )),
        };
        let response: PaginatedResponse<T> = self.send(req.clone()).await?;
        Ok(super::paged::PagedResult::new(
            response,
            self.clone(),
            req,
            None,
        ))
    }
}

// Secret-safe request metadata. Payload tracing is intentionally unavailable:
// RUST_LOG may select this target but cannot expose bodies, query values,
// headers, full URLs, or credentials.
fn log_request(request: &HttpRequest) {
    if tracing::enabled!(target: "anytype::http_json", tracing::Level::TRACE) {
        trace!(
            target: "anytype::http_json",
            method = %request.method,
            path = %diagnostic_path(&request.path),
            query_fields = request.query.len(),
            body_bytes = request.body.as_ref().map_or(0, Bytes::len),
            "HTTP request metadata"
        );
    }
}

// Secret-safe response metadata. Anytype JSON may contain private document
// data, so no tracing level or target can include `body` itself.
fn log_response(path: &str, body: &Bytes) {
    if tracing::enabled!(target: "anytype::http_json", tracing::Level::TRACE) {
        trace!(
            target: "anytype::http_json",
            path = %diagnostic_path(path),
            body_bytes = body.len(),
            "HTTP response metadata"
        );
    }
}

// deserialize, reporting errors with 'serde_path_to_error', which provides
// detailed json path to the error
fn deserialize_json<T: DeserializeOwned>(body: &[u8]) -> Result<T> {
    // Successful mutation endpoints in anytype-heart commonly return an empty
    // 200 response. Treat that as JSON null so callers can deserialize it as
    // `()` while response types that require an entity still fail normally.
    let body = if body.is_empty() { b"null" } else { body };
    let mut deserializer = serde_json::Deserializer::from_slice(body);
    match serde_path_to_error::deserialize(&mut deserializer) {
        Ok(value) => Ok(value),
        Err(err) => {
            let source = err.inner();
            error!(
                target: "anytype::http",
                error_variant = "deserialization",
                json_category = ?source.classify(),
                line = source.line(),
                column = source.column(),
                "HTTP response deserialization failed"
            );
            Err(AnytypeError::Deserialization {
                source: err.into_inner(),
            })
        }
    }
}

// log attempt and sleep for exponential backoff
async fn log_and_backoff(attempt: u32, reason: &str) {
    // exponential backoff: 1s, 2s, 4s, with jitter
    #[allow(clippy::cast_precision_loss)]
    let base_delay = 2u64.pow(attempt) as f64;
    let jitter = f64::from(
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .subsec_nanos(),
    ) / 1_000_000_000.0;
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let jittered_delay = (base_delay * (0.5 + jitter)).round() as u64;
    let delay = if jittered_delay == 0 {
        1
    } else {
        jittered_delay
    };
    warn!("Recoverable {reason}. Attempt {attempt}. Waiting {delay}s before retry");
    tokio::time::sleep(Duration::from_secs(delay)).await;
}

fn is_idempotent_method(method: &Method) -> bool {
    matches!(*method, Method::GET | Method::HEAD | Method::OPTIONS)
}

#[cfg(test)]
mod tests {
    use std::{
        io::{self, Write},
        sync::{Arc, Barrier, Mutex, Once, atomic::Ordering},
        time::Duration,
    };

    use bytes::Bytes;
    use futures::StreamExt;
    use reqwest::{
        ClientBuilder, Method, StatusCode,
        header::{HeaderMap, HeaderValue},
    };
    use serde::Deserialize;
    use tokio::{
        io::{AsyncReadExt, AsyncWriteExt},
        net::{TcpListener, TcpStream},
        sync::oneshot,
        task::JoinHandle,
    };
    use tracing::Dispatch;
    use tracing_subscriber::{fmt as tracing_fmt, layer::SubscriberExt};

    use super::{
        HttpClient, HttpMetrics, HttpRequest, MAX_DIAGNOSTIC_PATH_CHARS, REDACTED_DIAGNOSTIC_PATH,
        deserialize_json, diagnostic_path, log_http_status, log_request, log_response,
        parse_retry_after,
    };
    use crate::filters::QueryWithFilters;
    use crate::prelude::{
        AnytypeClient, AnytypeError, ClientConfig, HttpCredentials, HttpTimeoutPolicy,
        MAX_JSON_RESPONSE_BYTES, ResponseLimits, ValidationLimits,
    };

    const TEST_SPACE_ID: &str =
        "bafyreid5fvqlnsobih2keakcxjrrlpmly6kf37klzjzen4ibfdgalcdp4y.2tq5w93cr6oe7";
    const TEST_OBJECT_ID: &str = "bafyreie6n5l5nkbjal37su54cha4coy7qzuhrnajluzv5qd5jvtsrxkequ";

    static TRACE_TEST_INTEREST: Once = Once::new();

    fn ensure_trace_interest() {
        TRACE_TEST_INTEREST.call_once(|| {
            let subscriber =
                tracing_subscriber::registry().with(tracing_subscriber::filter::LevelFilter::TRACE);
            let _ = tracing::subscriber::set_global_default(subscriber);
        });
    }

    #[derive(Clone, Default)]
    struct Capture(Arc<Mutex<Vec<u8>>>);

    impl Capture {
        fn contents(&self) -> String {
            String::from_utf8(self.0.lock().expect("capture lock").clone())
                .expect("diagnostics are UTF-8")
        }
    }

    impl Write for Capture {
        fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
            self.0
                .lock()
                .expect("capture lock")
                .extend_from_slice(bytes);
            Ok(bytes.len())
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    impl<'writer> tracing_subscriber::fmt::writer::MakeWriter<'writer> for Capture {
        type Writer = Self;

        fn make_writer(&'writer self) -> Self::Writer {
            self.clone()
        }
    }

    fn capture() -> (Dispatch, Capture) {
        ensure_trace_interest();
        let output = Capture::default();
        let layer = tracing_fmt::layer()
            .with_writer(output.clone())
            .with_target(true)
            .with_ansi(false);
        let subscriber = tracing_subscriber::registry()
            .with(tracing_subscriber::filter::LevelFilter::TRACE)
            .with(layer);
        (Dispatch::new(subscriber), output)
    }

    fn test_limits(json_bytes: u64, document_bytes: u64, error_bytes: u64) -> ResponseLimits {
        ResponseLimits {
            json_bytes,
            document_bytes,
            error_bytes,
            file_bytes: 1024,
            chat_sse_event_bytes: 1024,
        }
    }

    fn credential_test_client() -> Arc<HttpClient> {
        Arc::new(
            HttpClient::new(
                ClientBuilder::new().no_proxy(),
                "http://127.0.0.1:1".to_owned(),
                ValidationLimits::default(),
                test_limits(4, 8, 4),
                1,
                HttpTimeoutPolicy::default(),
                HttpCredentials::new("old-token"),
            )
            .expect("credential test client"),
        )
    }

    #[test]
    fn credential_replacement_and_generation_are_one_atomic_state_transition() {
        let client = credential_test_client();
        let set_barrier = Arc::new(Barrier::new(3));
        let writer = {
            let client = client.clone();
            let barrier = set_barrier.clone();
            std::thread::spawn(move || {
                barrier.wait();
                client.set_api_key(HttpCredentials::new("new-token"));
            })
        };
        let reader = {
            let client = client.clone();
            let barrier = set_barrier.clone();
            std::thread::spawn(move || {
                barrier.wait();
                let state = client.credential_state.lock();
                (
                    state.credentials.token().map(str::to_owned),
                    state.generation,
                )
            })
        };
        set_barrier.wait();
        writer.join().expect("set writer");
        let set_observation = reader.join().expect("set reader");
        assert!(
            matches!(
                set_observation,
                (Some(ref token), 0) if token == "old-token"
            ) || matches!(
                set_observation,
                (Some(ref token), 1) if token == "new-token"
            )
        );

        let clear_barrier = Arc::new(Barrier::new(3));
        let writer = {
            let client = client.clone();
            let barrier = clear_barrier.clone();
            std::thread::spawn(move || {
                barrier.wait();
                client.clear_api_key();
            })
        };
        let reader = {
            let client = client.clone();
            let barrier = clear_barrier.clone();
            std::thread::spawn(move || {
                barrier.wait();
                let state = client.credential_state.lock();
                (
                    state.credentials.token().map(str::to_owned),
                    state.generation,
                )
            })
        };
        clear_barrier.wait();
        writer.join().expect("clear writer");
        let clear_observation = reader.join().expect("clear reader");
        assert!(
            matches!(
                clear_observation,
                (Some(ref token), 1) if token == "new-token"
            ) || matches!(clear_observation, (None, 2))
        );
        assert_eq!(client.credential_generation(), 2);
        assert!(!client.has_key());
    }

    async fn serve_once(response: Vec<u8>) -> (Arc<HttpClient>, JoinHandle<()>) {
        serve_once_with_limits(response, test_limits(4, 8, 4)).await
    }

    async fn serve_once_with_limits(
        response: Vec<u8>,
        response_limits: ResponseLimits,
    ) -> (Arc<HttpClient>, JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let address = listener.local_addr().expect("test server address");
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept request");
            let mut request = vec![0_u8; 4096];
            let _ = socket.read(&mut request).await.expect("read request");
            socket.write_all(&response).await.expect("write response");
        });
        let client = HttpClient::new(
            ClientBuilder::new().no_proxy(),
            format!("http://{address}"),
            ValidationLimits::default(),
            response_limits,
            1,
            HttpTimeoutPolicy::default(),
            HttpCredentials::new("test-token"),
        )
        .expect("test client");
        (Arc::new(client), server)
    }

    async fn sse_chunk_fixture(
        policy: HttpTimeoutPolicy,
        schedule: Vec<(Duration, &'static [u8])>,
        finish: bool,
    ) -> (Arc<HttpClient>, JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind SSE server");
        let address = listener.local_addr().expect("SSE server address");
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept SSE request");
            let _request = read_fixture_request(&mut socket).await;
            socket
                .write_all(
                    b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n",
                )
                .await
                .expect("write SSE headers");
            socket.flush().await.expect("flush SSE headers");
            for (delay, chunk) in schedule {
                tokio::time::sleep(delay).await;
                let prefix = format!("{:x}\r\n", chunk.len());
                socket
                    .write_all(prefix.as_bytes())
                    .await
                    .expect("write SSE chunk prefix");
                socket.write_all(chunk).await.expect("write SSE chunk");
                socket
                    .write_all(b"\r\n")
                    .await
                    .expect("write SSE chunk suffix");
                socket.flush().await.expect("flush SSE chunk");
            }
            if finish {
                socket
                    .write_all(b"0\r\n\r\n")
                    .await
                    .expect("finish SSE body");
            } else {
                let mut peer_data = [0_u8; 1];
                let read = socket
                    .read(&mut peer_data)
                    .await
                    .expect("observe SSE peer close");
                assert_eq!(read, 0, "SSE peer sent unexpected data");
            }
        });
        let client = HttpClient::new(
            ClientBuilder::new().no_proxy(),
            format!("http://{address}"),
            ValidationLimits::default(),
            test_limits(4096, 4096, 4096),
            1,
            policy,
            HttpCredentials::new("test-token"),
        )
        .expect("SSE client");
        (Arc::new(client), server)
    }

    fn get_request() -> HttpRequest {
        HttpRequest {
            method: reqwest::Method::GET,
            path: "/test".to_string(),
            query: Vec::new(),
            body: None,
        }
    }

    fn fixture_response(status: &str, body: &str, extra_headers: &str) -> Vec<u8> {
        format!(
            "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\n{extra_headers}Connection: close\r\n\r\n{body}",
            body.len()
        )
        .into_bytes()
    }

    async fn deadline_fixture(
        policy: HttpTimeoutPolicy,
        prefix: Option<&'static [u8]>,
        builder: ClientBuilder,
    ) -> (Arc<HttpClient>, oneshot::Receiver<()>, JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind deadline fixture");
        let address = listener.local_addr().expect("deadline fixture address");
        let (ready_tx, ready_rx) = oneshot::channel();
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept deadline request");
            let _request = read_fixture_request(&mut socket).await;
            if let Some(prefix) = prefix {
                socket
                    .write_all(prefix)
                    .await
                    .expect("write deadline prefix");
                socket.flush().await.expect("flush deadline prefix");
            }
            let _ = ready_tx.send(());
            std::future::pending::<()>().await;
        });
        let client = HttpClient::new(
            builder,
            format!("http://{address}"),
            ValidationLimits::default(),
            test_limits(1024, 2048, 1024),
            5,
            policy,
            HttpCredentials::new("test-token"),
        )
        .expect("deadline client");
        (Arc::new(client), ready_rx, server)
    }

    fn one_second_standard_policy() -> HttpTimeoutPolicy {
        HttpTimeoutPolicy {
            standard_operation: Some(Duration::from_secs(1)),
            ..HttpTimeoutPolicy::default()
        }
    }

    // The deadline tests run in real time until the fixture has accepted the
    // request, then freeze the clock to advance past the deadline exactly.
    // Under `start_paused`, auto-advance can virtually expire the deadline
    // while the real TCP connect is still in flight, after which the fixture
    // never accepts and the test parks forever.
    #[tokio::test]
    async fn standard_deadline_bounds_stalled_headers_and_records_one_timeout() {
        let (client, accepted, server) = deadline_fixture(
            one_second_standard_policy(),
            None,
            ClientBuilder::new().no_proxy(),
        )
        .await;
        let request_client = client.clone();
        let request = tokio::spawn(async move { request_client.send::<()>(get_request()).await });
        accepted.await.expect("request accepted");
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(1)).await;
        let error = request.await.expect("request task").expect_err("deadline");
        let diagnostic = error.diagnostic().to_string();
        assert!(diagnostic.contains("method=GET"));
        assert!(diagnostic.contains("path=/test"));
        assert!(diagnostic.contains("timeout_class=standard_operation"));
        assert!(diagnostic.contains("outcome=read_aborted"));
        assert!(diagnostic.contains("attempts=1"));
        assert!(matches!(
            error,
            AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::StandardOperation,
                outcome: crate::http_timeout::TimeoutOutcome::ReadAborted,
                attempts: 1,
                ..
            }
        ));
        let metrics = client.metrics_snapshot();
        assert_eq!(
            metrics
                .timeout(crate::http_timeout::HttpTimeoutClass::StandardOperation)
                .count,
            1
        );
        assert_eq!(
            metrics.timeout_outcome_count(crate::http_timeout::TimeoutOutcome::ReadAborted),
            1
        );
        server.abort();
    }

    #[tokio::test]
    async fn standard_deadline_includes_success_and_error_bodies() {
        for prefix in [
            b"HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\n{".as_slice(),
            b"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 8\r\n\r\n{".as_slice(),
        ] {
            let (client, prefix_written, server) = deadline_fixture(
                one_second_standard_policy(),
                Some(prefix),
                ClientBuilder::new().no_proxy(),
            )
            .await;
            let request = tokio::spawn(async move { client.send::<()>(get_request()).await });
            prefix_written.await.expect("response prefix written");
            tokio::time::pause();
            tokio::time::advance(Duration::from_secs(1)).await;
            assert!(matches!(
                request.await.expect("request task"),
                Err(AnytypeError::HttpTimeout {
                    class: crate::http_timeout::HttpTimeoutClass::StandardOperation,
                    ..
                })
            ));
            server.abort();
            tokio::time::resume();
        }
    }

    #[tokio::test]
    async fn mutation_deadline_is_indeterminate_and_dispatches_once() {
        let (client, accepted, server) = deadline_fixture(
            one_second_standard_policy(),
            None,
            ClientBuilder::new().no_proxy(),
        )
        .await;
        let request_client = client.clone();
        let request = tokio::spawn(async move {
            request_client
                .send::<()>(HttpRequest {
                    method: Method::POST,
                    path: "/mutation?secret=redacted".to_owned(),
                    query: Vec::new(),
                    body: Some(Bytes::from_static(b"secret body")),
                })
                .await
        });
        accepted.await.expect("mutation accepted");
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(1)).await;
        let error = request.await.expect("request task").expect_err("deadline");
        let diagnostic = error.diagnostic().to_string();
        assert!(diagnostic.contains("method=POST"));
        assert!(diagnostic.contains("path=/mutation"));
        assert!(diagnostic.contains("timeout_class=standard_operation"));
        assert!(diagnostic.contains("outcome=mutation_indeterminate"));
        assert!(diagnostic.contains("attempts=1"));
        assert!(!diagnostic.contains("secret"));
        assert!(!diagnostic.contains("redacted"));
        assert!(matches!(
            error,
            AnytypeError::HttpTimeout {
                outcome: crate::http_timeout::TimeoutOutcome::MutationIndeterminate,
                attempts: 1,
                ..
            }
        ));
        assert_eq!(client.metrics_snapshot().physical_attempts, 1);
        server.abort();
    }

    #[tokio::test]
    async fn shorter_caller_transport_timeout_wins_and_is_measured_once() {
        let policy = HttpTimeoutPolicy {
            standard_operation: Some(Duration::from_secs(10)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, accepted, server) = deadline_fixture(
            policy,
            None,
            ClientBuilder::new()
                .no_proxy()
                .timeout(Duration::from_secs(1)),
        )
        .await;
        let request_client = client.clone();
        let request = tokio::spawn(async move { request_client.send::<()>(get_request()).await });
        accepted.await.expect("request accepted");
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(1)).await;
        let error = request
            .await
            .expect("request task")
            .expect_err("transport timeout");
        let diagnostic = error.diagnostic().to_string();
        assert!(diagnostic.contains("timeout_class=transport"));
        assert!(diagnostic.contains("outcome=read_aborted"));
        assert!(diagnostic.contains("elapsed_ms="));
        assert!(diagnostic.contains("attempts=1"));
        assert!(matches!(error, AnytypeError::Http { source, .. } if source.is_timeout()));
        let metrics = client.metrics_snapshot();
        assert_eq!(metrics.transport_timeouts().count, 1);
        assert_eq!(
            metrics.timeout_outcome_count(crate::http_timeout::TimeoutOutcome::ReadAborted),
            1
        );
        server.abort();
    }

    #[tokio::test]
    async fn mutation_caller_transport_timeout_reports_indeterminate_outcome() {
        let policy = HttpTimeoutPolicy {
            standard_operation: Some(Duration::from_secs(10)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, accepted, server) = deadline_fixture(
            policy,
            None,
            ClientBuilder::new()
                .no_proxy()
                .timeout(Duration::from_secs(1)),
        )
        .await;
        let request_client = client.clone();
        let request = tokio::spawn(async move {
            request_client
                .send::<()>(HttpRequest {
                    method: Method::POST,
                    path: "/mutation?secret=redacted".to_owned(),
                    query: Vec::new(),
                    body: Some(Bytes::from_static(b"secret body")),
                })
                .await
        });
        accepted.await.expect("mutation accepted");
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(1)).await;
        let error = request
            .await
            .expect("request task")
            .expect_err("transport timeout");
        let diagnostic = error.diagnostic().to_string();
        assert!(diagnostic.contains("timeout_class=transport"));
        assert!(diagnostic.contains("outcome=mutation_indeterminate"));
        assert!(diagnostic.contains("elapsed_ms="));
        assert!(diagnostic.contains("attempts=1"));
        assert!(!diagnostic.contains("secret"));
        assert!(matches!(&error, AnytypeError::Http { source, .. } if source.is_timeout()));
        let metrics = client.metrics_snapshot();
        assert_eq!(metrics.transport_timeouts().count, 1);
        assert_eq!(
            metrics
                .timeout_outcome_count(crate::http_timeout::TimeoutOutcome::MutationIndeterminate),
            1
        );
        server.abort();
    }

    #[tokio::test]
    async fn connect_refused_mutation_keeps_typed_transport_error() {
        // Reserve a local port and release it so the connection is refused.
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind refused-port probe");
        let address = listener.local_addr().expect("refused-port address");
        drop(listener);
        let client = Arc::new(
            HttpClient::new(
                ClientBuilder::new().no_proxy(),
                format!("http://{address}"),
                ValidationLimits::default(),
                test_limits(1024, 2048, 1024),
                5,
                HttpTimeoutPolicy::default(),
                HttpCredentials::new("test-token"),
            )
            .expect("refused client"),
        );
        let error = client
            .send::<()>(HttpRequest {
                method: Method::POST,
                path: "/mutation".to_owned(),
                query: Vec::new(),
                body: Some(Bytes::from_static(b"body")),
            })
            .await
            .expect_err("connection refused");
        // The mutation was provably never dispatched, so the typed transport
        // error and its reqwest source survive instead of an indeterminate
        // outcome.
        assert!(
            matches!(&error, AnytypeError::Http { source, .. } if source.is_connect()),
            "unexpected connect-refused error: {error:?}"
        );
        let metrics = client.metrics_snapshot();
        assert_eq!(
            metrics
                .timeout_outcome_count(crate::http_timeout::TimeoutOutcome::MutationIndeterminate),
            0
        );
    }

    #[test]
    fn timeout_metrics_use_saturating_arithmetic() {
        let metrics = HttpMetrics::default();
        let class = crate::http_timeout::HttpTimeoutClass::StandardOperation;
        let outcome = crate::http_timeout::TimeoutOutcome::ReadAborted;
        metrics.timeout_counts[class.index()].store(u64::MAX, Ordering::Relaxed);
        metrics.timeout_elapsed_millis[class.index()].store(u64::MAX - 1, Ordering::Relaxed);
        metrics.timeout_outcomes[outcome.index()].store(u64::MAX, Ordering::Relaxed);
        metrics.record_timeout(class, outcome, Duration::from_millis(10));
        let snapshot = metrics.snapshot();
        assert_eq!(snapshot.timeout(class).count, u64::MAX);
        assert_eq!(snapshot.timeout(class).elapsed_millis, u64::MAX);
        assert_eq!(snapshot.timeout_outcome_count(outcome), u64::MAX);
    }

    #[tokio::test]
    async fn rate_limit_wait_cannot_reset_the_standard_deadline() {
        let response = fixture_response(
            "429 Too Many Requests",
            "rate limited",
            "RateLimit-Reset: 2\r\n",
        );
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind rate-limit deadline fixture");
        let address = listener.local_addr().expect("rate-limit fixture address");
        let (sent_tx, sent_rx) = oneshot::channel();
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept rate-limit request");
            let _request = read_fixture_request(&mut socket).await;
            socket
                .write_all(&response)
                .await
                .expect("write rate-limit response");
            let _ = sent_tx.send(());
        });
        let client = Arc::new(
            HttpClient::new(
                ClientBuilder::new().no_proxy(),
                format!("http://{address}"),
                ValidationLimits::default(),
                test_limits(4096, 4096, 4096),
                5,
                one_second_standard_policy(),
                HttpCredentials::new("test-token"),
            )
            .expect("rate-limit deadline client"),
        );
        let request_client = client.clone();
        let request = tokio::spawn(async move { request_client.send::<()>(get_request()).await });
        sent_rx.await.expect("rate-limit response sent");
        for _ in 0..32 {
            if client.metrics_snapshot().rate_limit_errors == 1 {
                break;
            }
            tokio::task::yield_now().await;
        }
        assert_eq!(client.metrics_snapshot().rate_limit_errors, 1);
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(1)).await;
        let error = request
            .await
            .expect("rate-limit request task")
            .expect_err("shared deadline must expire during rate-limit wait");
        assert!(matches!(
            error,
            AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::StandardOperation,
                attempts: 1,
                ..
            }
        ));
        assert_eq!(client.metrics_snapshot().physical_attempts, 1);
        tokio::time::resume();
        server.await.expect("rate-limit server");
    }

    #[tokio::test]
    async fn retry_backoff_cannot_reset_the_standard_deadline() {
        let response = fixture_response("504 Gateway Timeout", "retry later", "");
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind retry deadline fixture");
        let address = listener.local_addr().expect("retry fixture address");
        let (sent_tx, sent_rx) = oneshot::channel();
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept retry request");
            let _request = read_fixture_request(&mut socket).await;
            socket
                .write_all(&response)
                .await
                .expect("write retry response");
            let _ = sent_tx.send(());
        });
        let client = Arc::new(
            HttpClient::new(
                ClientBuilder::new().no_proxy(),
                format!("http://{address}"),
                ValidationLimits::default(),
                test_limits(4096, 4096, 4096),
                5,
                one_second_standard_policy(),
                HttpCredentials::new("test-token"),
            )
            .expect("retry deadline client"),
        );
        let request_client = client.clone();
        let request = tokio::spawn(async move { request_client.send::<()>(get_request()).await });
        sent_rx.await.expect("retry response sent");
        for _ in 0..32 {
            if client.metrics_snapshot().retries == 1 {
                break;
            }
            tokio::task::yield_now().await;
        }
        assert_eq!(client.metrics_snapshot().retries, 1);
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(1)).await;
        let error = request
            .await
            .expect("retry request task")
            .expect_err("shared deadline must expire during retry backoff");
        assert!(matches!(
            error,
            AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::StandardOperation,
                attempts: 1,
                ..
            }
        ));
        assert_eq!(client.metrics_snapshot().physical_attempts, 1);
        let metrics = client.metrics_snapshot();
        assert_eq!(
            metrics
                .timeout(crate::http_timeout::HttpTimeoutClass::StandardOperation)
                .count,
            1
        );
        assert_eq!(
            metrics.timeout_outcome_count(crate::http_timeout::TimeoutOutcome::ReadAborted),
            1
        );
        tokio::time::resume();
        server.await.expect("retry server");
    }

    #[tokio::test]
    async fn delayed_file_response_uses_long_profile() {
        let policy = HttpTimeoutPolicy {
            standard_operation: Some(Duration::from_secs(120)),
            long_operation: Some(Duration::from_secs(600)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, accepted, server) =
            deadline_fixture(policy, None, ClientBuilder::new().no_proxy()).await;
        let file_client = client.clone();
        let file = tokio::spawn(async move {
            file_client
                .file_request(Method::GET, "/v1/files/id", &[], HeaderMap::new())
                .await
        });
        accepted.await.expect("file request accepted");
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(600)).await;
        assert!(matches!(
            file.await.expect("file request task"),
            Err(AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::LongOperation,
                ..
            })
        ));
        server.abort();

        let timing = super::OperationTiming::start();
        client
            .with_deadline(
                crate::http_timeout::HttpTimeoutClass::LongOperation,
                crate::http_timeout::TimeoutOutcome::ReadAborted,
                &Method::GET,
                "/v1/files/id",
                &timing,
                async {
                    tokio::time::sleep(Duration::from_secs(154)).await;
                    Ok::<_, AnytypeError>(())
                },
            )
            .await
            .expect("154-second operation fits the long profile");
    }

    #[tokio::test]
    async fn scripted_delayed_file_response_completes_within_long_profile() {
        // A scripted HTTP fixture delivers the complete file response only
        // after the standard deadline has passed, proving the real wire path
        // for file requests runs under the long profile. Paused time cannot
        // complete loopback responses deterministically (auto-advance fires
        // pending deadline timers before IO readiness is surfaced), so this
        // runs on the real clock with a two-second scripted delay; the
        // 154-second policy literal stays covered by the paused
        // `with_deadline` leg of `delayed_file_response_uses_long_profile`.
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind delayed file fixture");
        let address = listener.local_addr().expect("delayed file fixture address");
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener
                .accept()
                .await
                .expect("accept delayed file request");
            let _request = read_fixture_request(&mut socket).await;
            tokio::time::sleep(Duration::from_secs(2)).await;
            socket
                .write_all(&fixture_response("200 OK", "file body", ""))
                .await
                .expect("write delayed file response");
            std::future::pending::<()>().await;
        });
        let policy = HttpTimeoutPolicy {
            standard_operation: Some(Duration::from_secs(1)),
            long_operation: Some(Duration::from_secs(8)),
            ..HttpTimeoutPolicy::default()
        };
        let client = Arc::new(
            HttpClient::new(
                ClientBuilder::new().no_proxy(),
                format!("http://{address}"),
                ValidationLimits::default(),
                test_limits(1024, 2048, 1024),
                5,
                policy,
                HttpCredentials::new("test-token"),
            )
            .expect("delayed file client"),
        );
        let response = client
            .file_request(Method::GET, "/v1/files/id", &[], HeaderMap::new())
            .await
            .expect("delayed scripted response fits the long profile");
        assert_eq!(response.status, StatusCode::OK);
        let metrics = client.metrics_snapshot();
        assert_eq!(metrics.physical_attempts, 1);
        assert_eq!(
            metrics
                .timeout(crate::http_timeout::HttpTimeoutClass::LongOperation)
                .count,
            0
        );
        assert_eq!(
            metrics
                .timeout(crate::http_timeout::HttpTimeoutClass::StandardOperation)
                .count,
            0
        );
        server.abort();
    }

    #[tokio::test]
    async fn each_paginated_page_receives_a_fresh_standard_deadline() {
        use super::GetPaged;

        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind paged fixture");
        let address = listener.local_addr().expect("paged fixture address");
        let (page_two_tx, mut page_two_rx) = oneshot::channel();
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept page one");
            let _request = read_fixture_request(&mut socket).await;
            let body =
                r#"{"data":[1],"pagination":{"has_more":true,"limit":1,"offset":1,"total":2}}"#;
            socket
                .write_all(&fixture_response("200 OK", body, ""))
                .await
                .expect("write page one");
            let (mut stalled, _) = listener.accept().await.expect("accept page two");
            let _request = read_fixture_request(&mut stalled).await;
            let _ = page_two_tx.send(());
            std::future::pending::<()>().await;
        });
        let client = Arc::new(
            HttpClient::new(
                ClientBuilder::new().no_proxy(),
                format!("http://{address}"),
                ValidationLimits::default(),
                test_limits(1024, 2048, 1024),
                5,
                one_second_standard_policy(),
                HttpCredentials::new("test-token"),
            )
            .expect("paged client"),
        );
        let paged_client = client.clone();
        let (first_item_tx, first_item_rx) = oneshot::channel();
        let (resume_tx, resume_rx) = oneshot::channel();
        let mut task = tokio::spawn(async move {
            let paged = paged_client
                .get_request_paged::<u32>("/v1/paged", QueryWithFilters::default())
                .await?;
            let mut stream = paged.into_stream();
            let first = stream.next().await.expect("first page item")?;
            assert_eq!(first, 1);
            let _ = first_item_tx.send(());
            resume_rx.await.expect("resume signal");
            // The next poll triggers the page-two refill request.
            stream
                .next()
                .await
                .expect("page-two outcome")
                .map(|_| unreachable!("page two never completes"))
        });
        if first_item_rx.await.is_err() {
            let finished = task.await;
            panic!("paged task failed before first item: {finished:?}");
        }
        // Wait out more than the whole one-second standard deadline between
        // pages. A budget shared across pages would already be expired when
        // page two dispatches, so its request would never reach the wire.
        tokio::time::sleep(Duration::from_millis(1_500)).await;
        resume_tx.send(()).expect("send resume");
        tokio::select! {
            dispatched = &mut page_two_rx => {
                dispatched.expect("page two dispatched");
            }
            finished = &mut task => {
                panic!("paged task finished before page-two dispatch: {finished:?}");
            }
        }
        let error = task
            .await
            .expect("paged task")
            .expect_err("page-two deadline");
        // A fresh page-two deadline expires after roughly its own second of
        // waiting, not the 1.5-second inter-page wait already elapsed.
        assert!(
            matches!(
                &error,
                AnytypeError::HttpTimeout {
                    class: crate::http_timeout::HttpTimeoutClass::StandardOperation,
                    elapsed,
                    ..
                } if *elapsed >= Duration::from_secs(1) && *elapsed < Duration::from_millis(1_400)
            ),
            "unexpected page-two error: {error:?}"
        );
        server.abort();
    }

    #[tokio::test]
    async fn multipart_uses_long_profile_instead_of_standard_profile() {
        let policy = HttpTimeoutPolicy {
            standard_operation: Some(Duration::from_secs(1)),
            long_operation: Some(Duration::from_secs(10)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, accepted, server) =
            deadline_fixture(policy, None, ClientBuilder::new().no_proxy()).await;
        let form = reqwest::multipart::Form::new().text("field", "value");
        let multipart = tokio::spawn(async move {
            client
                .post_multipart_with_limits::<serde_json::Value>(
                    "/v1/upload",
                    form,
                    None,
                    None,
                    None,
                    None,
                )
                .await
        });
        accepted.await.expect("multipart accepted");
        tokio::time::pause();
        tokio::time::advance(Duration::from_secs(10)).await;
        assert!(matches!(
            multipart.await.expect("multipart task"),
            Err(AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::LongOperation,
                ..
            })
        ));
        server.abort();
    }

    #[tokio::test]
    async fn sse_open_and_error_body_use_separate_timeout_classes() {
        let policy = HttpTimeoutPolicy {
            sse_open: Some(Duration::from_secs(1)),
            sse_error_body: Some(Duration::from_secs(1)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, accepted, server) =
            deadline_fixture(policy, None, ClientBuilder::new().no_proxy()).await;
        let open_client = client.clone();
        let open = tokio::spawn(async move {
            open_client
                .get_streaming_request("/events", QueryWithFilters::default(), HeaderMap::new())
                .await
        });
        accepted.await.expect("SSE open accepted");
        assert!(matches!(
            open.await.expect("SSE open task"),
            Err(AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::SseOpen,
                ..
            })
        ));
        server.abort();

        let prefix = b"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 8\r\n\r\n{";
        let error_policy = HttpTimeoutPolicy {
            sse_open: Some(Duration::from_secs(10)),
            sse_error_body: Some(Duration::from_secs(1)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, prefix_written, server) =
            deadline_fixture(error_policy, Some(prefix), ClientBuilder::new().no_proxy()).await;
        let error_body = tokio::spawn(async move {
            client
                .get_streaming_request("/events", QueryWithFilters::default(), HeaderMap::new())
                .await
        });
        prefix_written.await.expect("SSE error prefix");
        let error = error_body
            .await
            .expect("SSE error task")
            .err()
            .expect("SSE error response must fail");
        assert!(
            matches!(
                error,
                AnytypeError::HttpTimeout {
                    class: crate::http_timeout::HttpTimeoutClass::SseErrorBody,
                    ..
                }
            ),
            "unexpected SSE error: {error:?}"
        );
        server.abort();
    }

    #[tokio::test]
    async fn established_sse_default_outlives_buffered_deadline() {
        let policy = HttpTimeoutPolicy {
            standard_operation: Some(Duration::from_secs(1)),
            sse_open: Some(Duration::from_secs(120)),
            sse_idle: None,
            sse_total_lifetime: None,
            ..HttpTimeoutPolicy::default()
        };
        let (client, server) = sse_chunk_fixture(
            policy,
            vec![(Duration::from_secs(2), b"data: healthy\n\n")],
            true,
        )
        .await;
        let response = client
            .get_streaming_request("/events", QueryWithFilters::default(), HeaderMap::new())
            .await
            .expect("SSE headers open promptly");
        let mut chunks = response.bytes_stream();
        assert_eq!(
            chunks
                .next()
                .await
                .expect("SSE chunk")
                .expect("healthy SSE"),
            Bytes::from_static(b"data: healthy\n\n")
        );
        server.await.expect("SSE server");
    }

    #[tokio::test]
    async fn established_sse_idle_and_lifetime_are_independent() {
        let idle_policy = HttpTimeoutPolicy {
            sse_idle: Some(Duration::from_secs(1)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, server) = sse_chunk_fixture(idle_policy, Vec::new(), false).await;
        let response = client
            .get_streaming_request("/events", QueryWithFilters::default(), HeaderMap::new())
            .await
            .expect("SSE opens");
        let mut chunks = response.bytes_stream();
        assert!(matches!(
            chunks.next().await,
            Some(Err(AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::SseIdle,
                outcome: crate::http_timeout::TimeoutOutcome::StreamTerminated,
                ..
            }))
        ));
        tokio::time::timeout(Duration::from_secs(1), server)
            .await
            .expect("timed-out SSE response closes promptly")
            .expect("SSE server");
        assert!(
            chunks.next().await.is_none(),
            "stream emits one terminal error"
        );

        let lifetime_policy = HttpTimeoutPolicy {
            sse_idle: Some(Duration::from_secs(10)),
            sse_total_lifetime: Some(Duration::from_secs(2)),
            ..HttpTimeoutPolicy::default()
        };
        let (client, server) = sse_chunk_fixture(
            lifetime_policy,
            vec![
                (Duration::from_secs(1), b": heartbeat\n\n"),
                (Duration::from_secs(1), b": heartbeat\n\n"),
                (Duration::from_secs(1), b"data: late\n\n"),
            ],
            false,
        )
        .await;
        let response = client
            .get_streaming_request("/events", QueryWithFilters::default(), HeaderMap::new())
            .await
            .expect("SSE opens");
        let mut chunks = response.bytes_stream();
        assert!(
            chunks.next().await.is_some(),
            "heartbeat counts as progress"
        );
        let terminal = chunks.next().await;
        assert!(matches!(
            terminal,
            Some(Err(AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::SseLifetime,
                ..
            }))
        ));
        server.abort();
    }

    #[tokio::test(start_paused = true)]
    async fn queued_sse_chunks_cannot_cross_exact_deadline_boundaries() {
        let started = tokio::time::Instant::now();
        let idle_state = super::EstablishedSseState {
            chunks: futures::stream::iter([Ok(Bytes::from_static(b"data: queued\n\n"))]).boxed(),
            metrics: Arc::new(HttpMetrics::new()),
            path: "/events".to_owned(),
            started,
            last_progress: started,
            idle: Some(Duration::from_secs(1)),
            lifetime: Some(Duration::from_secs(10)),
            terminated: false,
        };
        tokio::time::advance(Duration::from_secs(1)).await;
        let (result, idle_state) = idle_state.next().await.expect("idle terminal error");
        assert!(matches!(
            result,
            Err(AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::SseIdle,
                ..
            })
        ));
        assert!(idle_state.next().await.is_none());

        let started = tokio::time::Instant::now();
        let lifetime_state = super::EstablishedSseState {
            chunks: futures::stream::iter([Ok(Bytes::from_static(b"data: queued\n\n"))]).boxed(),
            metrics: Arc::new(HttpMetrics::new()),
            path: "/events".to_owned(),
            started,
            last_progress: started,
            idle: Some(Duration::from_secs(1)),
            lifetime: Some(Duration::from_secs(1)),
            terminated: false,
        };
        tokio::time::advance(Duration::from_secs(1)).await;
        let (result, lifetime_state) = lifetime_state
            .next()
            .await
            .expect("lifetime terminal error");
        assert!(matches!(
            result,
            Err(AnytypeError::HttpTimeout {
                class: crate::http_timeout::HttpTimeoutClass::SseLifetime,
                ..
            })
        ));
        assert!(lifetime_state.next().await.is_none());
    }

    async fn read_fixture_request(socket: &mut TcpStream) -> String {
        let mut request = Vec::new();
        let mut expected_len = None;
        let mut buffer = [0_u8; 1024];
        loop {
            let read = socket.read(&mut buffer).await.expect("read public request");
            if read == 0 {
                break;
            }
            request.extend_from_slice(&buffer[..read]);
            if expected_len.is_none()
                && let Some(header_end) =
                    request.windows(4).position(|window| window == b"\r\n\r\n")
            {
                let headers = String::from_utf8_lossy(&request[..header_end]);
                let body_len = headers
                    .lines()
                    .find_map(|line| {
                        let (name, value) = line.split_once(':')?;
                        name.eq_ignore_ascii_case("content-length")
                            .then(|| value.trim().parse::<usize>().ok())
                            .flatten()
                    })
                    .unwrap_or_default();
                expected_len = Some(header_end + 4 + body_len);
            }
            if expected_len.is_some_and(|length| request.len() >= length) {
                break;
            }
        }
        String::from_utf8(request).expect("request is UTF-8")
    }

    async fn public_fixture_client(
        responses: Vec<Vec<u8>>,
        rate_limit_max_retries: u32,
    ) -> (AnytypeClient, JoinHandle<Vec<String>>) {
        public_fixture_client_with_builder(
            responses,
            rate_limit_max_retries,
            ClientBuilder::new().no_proxy(),
        )
        .await
    }

    async fn public_fixture_client_with_builder(
        responses: Vec<Vec<u8>>,
        rate_limit_max_retries: u32,
        builder: ClientBuilder,
    ) -> (AnytypeClient, JoinHandle<Vec<String>>) {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind public-path fixture");
        let address = listener.local_addr().expect("public fixture address");
        let server = tokio::spawn(async move {
            let mut requests = Vec::with_capacity(responses.len());
            for response in responses {
                let (mut socket, _) = listener.accept().await.expect("accept public request");
                let request = read_fixture_request(&mut socket).await;
                socket
                    .write_all(&response)
                    .await
                    .expect("write public response");
                requests.push(request);
            }
            requests
        });

        let client =
            public_client_for(format!("http://{address}"), rate_limit_max_retries, builder);
        (client, server)
    }

    fn public_client_for(
        base_url: String,
        rate_limit_max_retries: u32,
        builder: ClientBuilder,
    ) -> AnytypeClient {
        let mut config = ClientConfig::default().app_name("retry-safety-http-fixture");
        config.base_url = Some(base_url);
        config.keystore = Some("env".to_string());
        config.disable_cache = true;
        config.rate_limit_max_retries = rate_limit_max_retries;
        let client =
            AnytypeClient::with_client(builder, config).expect("create public fixture client");
        client.set_api_key(HttpCredentials::new("fixture-secret-token"));
        client
    }

    async fn assert_public_mutation_sent_once(
        method: Method,
        status: &'static str,
        code: u16,
        rate_limit_max_retries: u32,
    ) {
        let retry_header = if code == 429 {
            "Retry-After: 0\r\nRateLimit-Reset: 0\r\n"
        } else {
            ""
        };
        let response = fixture_response(status, "fixture rejection", retry_header);
        let (client, server) = public_fixture_client(vec![response], rate_limit_max_retries).await;

        let result = public_mutation(&client, &method).await;
        let error = result.expect_err("mutation fixture must reject");
        assert!(
            matches!(
                &error,
                AnytypeError::HttpMutationIndeterminate {
                    status: Some(actual),
                    attempts: 1,
                    ..
                } if *actual == code
            ),
            "unexpected mutation error: {error:?}"
        );

        let requests = server.await.expect("public fixture task");
        assert_eq!(requests.len(), 1);
        assert!(requests[0].starts_with(method.as_str()));
        let metrics = client.http_metrics();
        assert_eq!(metrics.total_requests, 1);
        assert_eq!(metrics.physical_attempts, 1);
        assert_eq!(metrics.retries, 0);
    }

    async fn public_mutation(
        client: &AnytypeClient,
        method: &Method,
    ) -> crate::Result<crate::objects::Object> {
        if *method == Method::POST {
            client
                .new_object(TEST_SPACE_ID, "page")
                .name("retry safety")
                .no_verify()
                .create()
                .await
        } else if *method == Method::DELETE {
            client.object(TEST_SPACE_ID, TEST_OBJECT_ID).delete().await
        } else {
            client
                .update_object(TEST_SPACE_ID, TEST_OBJECT_ID)
                .name("retry safety")
                .no_verify()
                .update()
                .await
        }
    }

    #[tokio::test]
    async fn public_post_and_patch_429_and_500_are_each_sent_exactly_once() {
        // Exercise default, unlimited, and high custom retry settings. None may
        // broaden replay permission for a non-idempotent method.
        assert_public_mutation_sent_once(Method::POST, "429 Too Many Requests", 429, 0).await;
        assert_public_mutation_sent_once(
            Method::PATCH,
            "429 Too Many Requests",
            429,
            crate::config::RATE_LIMIT_MAX_RETRIES_DEFAULT,
        )
        .await;
        assert_public_mutation_sent_once(Method::POST, "500 Internal Server Error", 500, 99).await;
        assert_public_mutation_sent_once(Method::PATCH, "500 Internal Server Error", 500, 0).await;
    }

    #[tokio::test]
    async fn public_post_and_patch_408_and_504_are_each_sent_exactly_once() {
        // These statuses enter the explicit retry_for_status branch and must
        // still stop after the first non-idempotent send.
        assert_public_mutation_sent_once(Method::POST, "408 Request Timeout", 408, 0).await;
        assert_public_mutation_sent_once(
            Method::PATCH,
            "408 Request Timeout",
            408,
            crate::config::RATE_LIMIT_MAX_RETRIES_DEFAULT,
        )
        .await;
        assert_public_mutation_sent_once(Method::POST, "504 Gateway Timeout", 504, 99).await;
        assert_public_mutation_sent_once(Method::PATCH, "504 Gateway Timeout", 504, 0).await;
    }

    #[tokio::test]
    async fn public_delete_is_sent_exactly_once_for_ambiguous_statuses() {
        // DELETE was auto-replayed by the previous release's method-semantics
        // rule; this wire fixture pins the at-most-once regression directly.
        assert_public_mutation_sent_once(Method::DELETE, "429 Too Many Requests", 429, 0).await;
        assert_public_mutation_sent_once(
            Method::DELETE,
            "500 Internal Server Error",
            500,
            crate::config::RATE_LIMIT_MAX_RETRIES_DEFAULT,
        )
        .await;
        assert_public_mutation_sent_once(Method::DELETE, "408 Request Timeout", 408, 99).await;
        assert_public_mutation_sent_once(Method::DELETE, "504 Gateway Timeout", 504, 0).await;
    }

    #[tokio::test]
    async fn put_has_no_replay_permission_and_reports_indeterminate() {
        // No PUT endpoint exists today; pin the exclusion so a future PUT
        // endpoint cannot silently inherit replay permission.
        assert!(!super::is_idempotent_method(&Method::PUT));
        assert!(!super::is_idempotent_method(&Method::POST));
        assert!(!super::is_idempotent_method(&Method::PATCH));
        assert!(!super::is_idempotent_method(&Method::DELETE));
        assert!(super::is_idempotent_method(&Method::GET));
        assert!(super::is_idempotent_method(&Method::HEAD));
        assert!(super::is_idempotent_method(&Method::OPTIONS));

        const PUT_REJECTION: &[u8] = b"HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nContent-Length: 13\r\nConnection: close\r\n\r\nput rejection";
        let (client, response_written, server) = deadline_fixture(
            one_second_standard_policy(),
            Some(PUT_REJECTION),
            ClientBuilder::new().no_proxy(),
        )
        .await;
        let request_client = client.clone();
        let request = tokio::spawn(async move {
            request_client
                .send::<()>(HttpRequest {
                    method: Method::PUT,
                    path: "/put".to_owned(),
                    query: Vec::new(),
                    body: Some(Bytes::from_static(b"body")),
                })
                .await
        });
        response_written.await.expect("response written");
        let error = request
            .await
            .expect("request task")
            .expect_err("server rejection");
        assert!(
            matches!(
                &error,
                AnytypeError::HttpMutationIndeterminate {
                    status: Some(500),
                    attempts: 1,
                    ..
                }
            ),
            "unexpected PUT error: {error:?}"
        );
        let metrics = client.metrics_snapshot();
        assert_eq!(metrics.physical_attempts, 1);
        assert_eq!(metrics.retries, 0);
        server.abort();
    }

    async fn assert_public_redirect_is_not_followed(
        method: Method,
        status: &'static str,
        code: u16,
    ) {
        let redirect_target = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind redirect target");
        let target_address = redirect_target
            .local_addr()
            .expect("redirect target address");
        let target = tokio::spawn(async move {
            let accepted =
                tokio::time::timeout(Duration::from_millis(150), redirect_target.accept()).await;
            let Ok(Ok((mut socket, _))) = accepted else {
                return None;
            };
            let request = read_fixture_request(&mut socket).await;
            socket
                .write_all(&fixture_response(
                    "500 Internal Server Error",
                    "redirect replay reached target",
                    "",
                ))
                .await
                .expect("write redirect target response");
            Some(request)
        });

        let origin = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind redirect origin");
        let origin_address = origin.local_addr().expect("redirect origin address");
        let location = format!("Location: http://{target_address}/redirected\r\n");
        let redirect = fixture_response(status, "redirect response", &location);
        let source = tokio::spawn(async move {
            let (mut socket, _) = origin
                .accept()
                .await
                .expect("accept redirect source request");
            let request = read_fixture_request(&mut socket).await;
            socket
                .write_all(&redirect)
                .await
                .expect("write redirect response");
            request
        });

        // A caller-supplied follow policy is intentionally replaced.
        let builder = ClientBuilder::new()
            .no_proxy()
            .redirect(reqwest::redirect::Policy::limited(20));
        let client = public_client_for(format!("http://{origin_address}"), 5, builder);
        let error = public_mutation(&client, &method)
            .await
            .expect_err("redirect must be surfaced without replay");
        assert!(
            matches!(&error, AnytypeError::ApiError { code: actual, .. } if *actual == code),
            "unexpected redirect error: {error:?}"
        );

        let source_request = source.await.expect("redirect source task");
        assert!(source_request.starts_with(method.as_str()));
        assert!(source_request.contains("fixture-secret-token"));
        assert!(source_request.contains("retry safety"));
        let redirected_request = target.await.expect("redirect target task");
        assert!(
            redirected_request.is_none(),
            "redirect target received a replayed request body or credentials"
        );
        let metrics = client.http_metrics();
        assert_eq!(metrics.total_requests, 1);
        assert_eq!(metrics.physical_attempts, 1);
        assert_eq!(metrics.retries, 0);
    }

    #[tokio::test]
    async fn public_post_307_and_patch_308_never_follow_or_replay_cross_origin() {
        assert_public_redirect_is_not_followed(Method::POST, "307 Temporary Redirect", 307).await;
        assert_public_redirect_is_not_followed(Method::PATCH, "308 Permanent Redirect", 308).await;
    }

    #[tokio::test]
    async fn caller_supplied_reqwest_retry_policy_cannot_replay_a_mutation() {
        let response = fixture_response("408 Request Timeout", "retry policy probe", "");
        let policy = reqwest::retry::for_host("127.0.0.1")
            .no_budget()
            .max_retries_per_request(10)
            .classify_fn(|request| {
                if request.status() == Some(StatusCode::REQUEST_TIMEOUT) {
                    request.retryable()
                } else {
                    request.success()
                }
            });
        let builder = ClientBuilder::new().no_proxy().retry(policy);
        let (client, server) = public_fixture_client_with_builder(vec![response], 0, builder).await;

        let error = public_mutation(&client, &Method::POST)
            .await
            .expect_err("caller retry policy must be overridden");
        assert!(matches!(
            error,
            AnytypeError::HttpMutationIndeterminate {
                status: Some(408),
                attempts: 1,
                ..
            }
        ));
        let requests = server.await.expect("custom retry fixture task");
        assert_eq!(requests.len(), 1);
        let metrics = client.http_metrics();
        assert_eq!(metrics.total_requests, 1);
        assert_eq!(metrics.physical_attempts, 1);
        assert_eq!(metrics.retries, 0);
    }

    #[tokio::test]
    async fn public_post_and_patch_disconnects_are_each_sent_exactly_once() {
        for (method, retry_limit) in [
            (Method::POST, 0),
            (Method::PATCH, crate::config::RATE_LIMIT_MAX_RETRIES_DEFAULT),
            (Method::DELETE, 0),
        ] {
            // An empty fixture response closes the connection after the full
            // request has arrived but before an HTTP status is available.
            let (client, server) = public_fixture_client(vec![Vec::new()], retry_limit).await;
            let error = public_mutation(&client, &method)
                .await
                .expect_err("disconnect must fail");
            assert!(
                matches!(&error, AnytypeError::HttpMutationIndeterminate { .. }),
                "unexpected disconnect error: {error:?}"
            );
            let requests = server.await.expect("disconnect fixture task");
            assert_eq!(requests.len(), 1);
            assert!(requests[0].starts_with(method.as_str()));
            let metrics = client.http_metrics();
            assert_eq!(metrics.total_requests, 1);
            assert_eq!(metrics.physical_attempts, 1);
            assert_eq!(metrics.retries, 0);
        }
    }

    #[tokio::test]
    async fn public_get_still_retries_429_after_retry_after_without_wall_delay() {
        let rejected = fixture_response(
            "429 Too Many Requests",
            "rate limited",
            "Retry-After: 0\r\nRateLimit-Reset: 0\r\n",
        );
        let body = r#"{"items":[],"pagination":{"has_more":false,"limit":1,"offset":0,"total":0}}"#;
        let success = fixture_response("200 OK", body, "");
        let (client, server) = public_fixture_client(vec![rejected, success], 1).await;

        let page = client
            .spaces()
            .limit(1)
            .list()
            .await
            .expect("GET retries after rate limiting");
        assert!(page.is_empty());
        let requests = server.await.expect("GET rate-limit fixture");
        assert_eq!(requests.len(), 2);
        assert!(requests.iter().all(|request| request.starts_with("GET ")));
        let metrics = client.http_metrics();
        assert_eq!(metrics.total_requests, 2);
        assert_eq!(metrics.physical_attempts, 2);
        assert_eq!(metrics.retries, 1);
        assert_eq!(metrics.rate_limit_errors, 1);
    }

    #[tokio::test]
    async fn public_get_status_backoff_retry_remains_enabled() {
        let rejected = fixture_response("504 Gateway Timeout", "gateway timeout", "");
        let body = r#"{"items":[],"pagination":{"has_more":false,"limit":1,"offset":0,"total":0}}"#;
        let success = fixture_response("200 OK", body, "");
        let (client, server) = public_fixture_client(vec![rejected, success], 1).await;

        let page = client
            .spaces()
            .limit(1)
            .list()
            .await
            .expect("GET retries after replay-safe status");
        assert!(page.is_empty());
        let requests = server.await.expect("GET status fixture");
        assert_eq!(requests.len(), 2);
        assert!(requests.iter().all(|request| request.starts_with("GET ")));
        let metrics = client.http_metrics();
        assert_eq!(metrics.total_requests, 2);
        assert_eq!(metrics.physical_attempts, 2);
        assert_eq!(metrics.retries, 1);
    }

    #[tokio::test]
    async fn caller_transport_timeout_stops_mixed_retry_sequence() {
        enum Reply {
            Response(Vec<u8>),
            Timeout,
        }

        let rate_limited = fixture_response(
            "429 Too Many Requests",
            "rate limited",
            "RateLimit-Reset: 0\r\n",
        );
        let timed_out = fixture_response("504 Gateway Timeout", "gateway timeout", "");
        let replies = vec![
            Reply::Response(rate_limited.clone()),
            Reply::Response(timed_out.clone()),
            Reply::Timeout,
        ];
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind alternating retry fixture");
        let address = listener.local_addr().expect("alternating fixture address");
        let server = tokio::spawn(async move {
            let mut requests = Vec::with_capacity(replies.len());
            for reply in replies {
                let (mut socket, _) = listener.accept().await.expect("accept alternating request");
                requests.push(read_fixture_request(&mut socket).await);
                match reply {
                    Reply::Response(response) => socket
                        .write_all(&response)
                        .await
                        .expect("write alternating response"),
                    Reply::Timeout => {
                        // Hold this connection open beyond the client timeout,
                        // while continuing to accept the next replay.
                        std::mem::drop(tokio::spawn(async move {
                            tokio::time::sleep(Duration::from_millis(100)).await;
                            drop(socket);
                        }));
                    }
                }
            }
            requests
        });
        let client = public_client_for(
            format!("http://{address}"),
            5,
            ClientBuilder::new()
                .no_proxy()
                .timeout(Duration::from_millis(20)),
        );

        let error = client
            .spaces()
            .limit(1)
            .list()
            .await
            .expect_err("caller transport timeout must stop the logical request");
        assert!(
            matches!(&error, AnytypeError::Http { .. }),
            "unexpected terminal error: {error:?}"
        );
        let metrics = client.http_metrics();
        assert_eq!(metrics.total_requests, 3, "terminal error: {error:?}");
        assert_eq!(metrics.logical_operations, 1);
        assert_eq!(metrics.physical_attempts, 3);
        assert_eq!(metrics.retries, 2);
        assert_eq!(metrics.rate_limit_errors, 1);
        let requests = tokio::time::timeout(Duration::from_secs(1), server)
            .await
            .expect("alternating fixture must receive all three attempts")
            .expect("alternating retry fixture");
        assert_eq!(requests.len(), 3);
        assert!(requests.iter().all(|request| request.starts_with("GET ")));
    }

    #[tokio::test]
    async fn rate_limit_specific_unbounded_setting_still_stops_at_six_attempts() {
        let rate_limited = fixture_response(
            "429 Too Many Requests",
            "rate limited",
            "RateLimit-Reset: 0\r\n",
        );
        let (client, server) = public_fixture_client(vec![rate_limited; 6], 0).await;

        let error = client
            .spaces()
            .limit(1)
            .list()
            .await
            .expect_err("the request-lifetime ceiling overrides an unbounded 429 setting");
        assert!(
            matches!(error, AnytypeError::RateLimitExceeded { .. }),
            "unexpected terminal error: {error:?}"
        );
        assert_eq!(server.await.expect("rate-limit ceiling fixture").len(), 6);
        let metrics = client.http_metrics();
        assert_eq!(metrics.total_requests, 6);
        assert_eq!(metrics.logical_operations, 1);
        assert_eq!(metrics.physical_attempts, 6);
        assert_eq!(metrics.retries, 5);
        assert_eq!(metrics.rate_limit_errors, 6);
    }

    #[test]
    fn response_limit_configuration_rejects_zero_and_hard_maximum_bypass() {
        for json_bytes in [0, MAX_JSON_RESPONSE_BYTES + 1] {
            let error = HttpClient::new(
                ClientBuilder::new().no_proxy(),
                "http://127.0.0.1:1".to_string(),
                ValidationLimits::default(),
                ResponseLimits {
                    json_bytes,
                    ..ResponseLimits::default()
                },
                1,
                HttpTimeoutPolicy::default(),
                HttpCredentials::new("test-token"),
            )
            .expect_err("invalid response limit");
            assert!(matches!(
                error,
                crate::error::AnytypeError::Validation { .. }
            ));
        }

        for chat_sse_event_bytes in [0, crate::client::MAX_CHAT_SSE_EVENT_BYTES + 1] {
            let error = HttpClient::new(
                ClientBuilder::new().no_proxy(),
                "http://127.0.0.1:1".to_string(),
                ValidationLimits::default(),
                ResponseLimits {
                    chat_sse_event_bytes,
                    ..ResponseLimits::default()
                },
                1,
                HttpTimeoutPolicy::default(),
                HttpCredentials::new("test-token"),
            )
            .expect_err("invalid chat SSE event limit");
            assert!(matches!(
                error,
                crate::error::AnytypeError::Validation { .. }
            ));
        }
    }

    #[tokio::test]
    async fn content_length_exact_limit_succeeds() {
        let response =
            b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\nnull".to_vec();
        let (client, server) = serve_once(response).await;

        client.send::<()>(get_request()).await.expect("exact limit");
        server.await.expect("server task");
        assert_eq!(client.metrics_snapshot().bytes_received, 4);
    }

    #[tokio::test]
    async fn one_byte_declared_response_succeeds_with_one_byte_cap() {
        let response =
            b"HTTP/1.1 200 OK\r\nContent-Length: 1\r\nConnection: close\r\n\r\n0".to_vec();
        let (client, server) = serve_once_with_limits(response, test_limits(1, 1, 1)).await;

        let value = client
            .send::<u8>(get_request())
            .await
            .expect("one-byte declared response");
        server.await.expect("server task");
        assert_eq!(value, 0);
        assert_eq!(client.metrics_snapshot().bytes_received, 1);
    }

    #[tokio::test]
    async fn one_byte_chunked_response_succeeds_with_one_byte_cap() {
        let response = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n1\r\n0\r\n0\r\n\r\n"
            .to_vec();
        let (client, server) = serve_once_with_limits(response, test_limits(1, 1, 1)).await;

        let value = client
            .send::<u8>(get_request())
            .await
            .expect("one-byte chunked response");
        server.await.expect("server task");
        assert_eq!(value, 0);
        assert_eq!(client.metrics_snapshot().bytes_received, 1);
    }

    #[tokio::test]
    async fn oversized_content_length_fails_before_body_is_buffered() {
        let response =
            b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nnull ".to_vec();
        let (client, server) = serve_once(response).await;

        let error = client
            .send::<()>(get_request())
            .await
            .expect_err("declared over limit");
        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge {
                limit: 4,
                declared: Some(5)
            }
        ));
        server.await.expect("server task");
        assert_eq!(client.metrics_snapshot().bytes_received, 0);
        assert_eq!(client.metrics_snapshot().errors, 1);
    }

    #[tokio::test]
    async fn chunked_exact_limit_succeeds_without_content_length() {
        let response = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n2\r\nnu\r\n2\r\nll\r\n0\r\n\r\n"
            .to_vec();
        let (client, server) = serve_once(response).await;

        client
            .send::<()>(get_request())
            .await
            .expect("exact chunks");
        server.await.expect("server task");
        assert_eq!(client.metrics_snapshot().bytes_received, 4);
    }

    #[tokio::test]
    async fn first_streamed_byte_over_limit_fails() {
        let response = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n4\r\nnull\r\n1\r\n \r\n0\r\n\r\n"
            .to_vec();
        let (client, server) = serve_once(response).await;

        let error = client
            .send::<()>(get_request())
            .await
            .expect_err("streamed byte over limit");
        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge {
                limit: 4,
                declared: None
            }
        ));
        server.await.expect("server task");
        assert_eq!(client.metrics_snapshot().bytes_received, 5);
        assert_eq!(client.metrics_snapshot().errors, 1);
    }

    #[tokio::test]
    async fn chunked_framing_cannot_bypass_limit_with_a_low_length_header() {
        let response = b"HTTP/1.1 200 OK\r\nContent-Length: 1\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n5\r\nnull \r\n0\r\n\r\n"
            .to_vec();
        let (client, server) = serve_once(response).await;

        let error = client
            .send::<()>(get_request())
            .await
            .expect_err("transfer framing must not bypass streamed total");
        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge { limit: 4, .. }
        ));
        server.await.expect("server task");
        assert_eq!(client.metrics_snapshot().bytes_received, 5);
    }

    #[tokio::test]
    async fn per_request_override_is_bounded_by_document_policy() {
        let response =
            b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nnull ".to_vec();
        let (client, server) = serve_once(response).await;
        client
            .get_request_with_limit::<()>("/test", crate::filters::QueryWithFilters::default(), 8)
            .await
            .expect("document override");
        server.await.expect("server task");

        let error = client
            .get_request_with_limit::<()>("/test", crate::filters::QueryWithFilters::default(), 9)
            .await
            .expect_err("override above configured document ceiling");
        assert!(matches!(
            error,
            crate::error::AnytypeError::Validation { .. }
        ));
    }

    #[tokio::test]
    async fn object_get_routes_complete_document_to_document_limit() {
        const OBJECT_ID: &str = "bafyreie6n5l5nkbjal37su54cha4coy7qzuhrnajluzv5qd5jvtsrxkequ";
        const SPACE_ID: &str =
            "bafyreid5fvqlnsobih2keakcxjrrlpmly6kf37klzjzen4ibfdgalcdp4y.2tq5w93cr6oe7";
        let body = format!(
            r#"{{"object":{{"archived":false,"id":"{OBJECT_ID}","space_id":"{SPACE_ID}","type":null}}}}"#
        );
        let response = format!(
            "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            body.len(),
            body
        )
        .into_bytes();
        let limits = test_limits(4, body.len() as u64, 4);
        let (client, server) = serve_once_with_limits(response, limits).await;

        let object = crate::objects::ObjectRequest::new(
            client,
            ValidationLimits::default(),
            SPACE_ID,
            OBJECT_ID,
        )
        .get()
        .await
        .expect("single-object reads use the document limit");
        server.await.expect("server task");
        assert_eq!(object.id, OBJECT_ID);
    }

    #[tokio::test]
    async fn oversized_error_body_uses_typed_limit_error() {
        let response = b"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 5\r\nConnection: close\r\n\r\nerror"
            .to_vec();
        let (client, server) = serve_once(response).await;
        let error = client
            .send::<()>(get_request())
            .await
            .expect_err("error body over limit");

        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge {
                limit: 4,
                declared: Some(5)
            }
        ));
        server.await.expect("server task");
        assert_eq!(client.metrics_snapshot().errors, 1);
        assert_eq!(client.metrics_snapshot().bytes_received, 0);
    }

    #[tokio::test]
    async fn unauthenticated_json_success_uses_generic_limit() {
        let response =
            b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nnull ".to_vec();
        let (client, server) = serve_once(response).await;
        let error = client
            .post_unauthenticated::<(), _>("/test", &serde_json::json!({}))
            .await
            .expect_err("unauthenticated JSON must be bounded");

        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge { limit: 4, .. }
        ));
        server.await.expect("server task");
    }

    #[tokio::test]
    async fn multipart_json_success_uses_generic_limit() {
        let response =
            b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nnull ".to_vec();
        let (client, server) = serve_once(response).await;
        let form = reqwest::multipart::Form::new().text("file", "content");
        let error = client
            .post_multipart_with_limits::<()>("/test", form, Some(123), None, None, None)
            .await
            .expect_err("multipart JSON response must be bounded");

        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge { limit: 4, .. }
        ));
        server.await.expect("server task");
        let metrics = client.metrics_snapshot();
        assert_eq!(metrics.logical_operations, 1);
        assert_eq!(metrics.total_requests, 1);
        assert_eq!(metrics.physical_attempts, 1);
        assert_eq!(metrics.multipart_posts, 1);
        assert_eq!(metrics.bytes_sent, 123);
    }

    #[tokio::test]
    async fn raw_file_success_uses_separate_file_limit() {
        let response =
            b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nbytes".to_vec();
        let limits = ResponseLimits {
            file_bytes: 4,
            ..test_limits(2, 8, 2)
        };
        let (client, server) = serve_once_with_limits(response, limits).await;
        let error = match client
            .file_request(reqwest::Method::GET, "/test", &[], HeaderMap::new())
            .await
        {
            Ok(_) => panic!("raw file response must use its own limit"),
            Err(error) => error,
        };

        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge { limit: 4, .. }
        ));
        server.await.expect("server task");
    }

    #[tokio::test]
    async fn streamed_over_limit_stops_before_full_response_arrives() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind streaming server");
        let address = listener.local_addr().expect("streaming server address");
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.expect("accept request");
            let mut request = vec![0_u8; 4096];
            let _ = socket.read(&mut request).await.expect("read request");
            socket
                .write_all(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n")
                .await
                .expect("write headers");
            let chunk = vec![b'x'; 1024];
            let mut sent = 0;
            for _ in 0..100 {
                if socket.write_all(b"400\r\n").await.is_err()
                    || socket.write_all(&chunk).await.is_err()
                    || socket.write_all(b"\r\n").await.is_err()
                    || socket.flush().await.is_err()
                {
                    break;
                }
                sent += 1;
                tokio::time::sleep(std::time::Duration::from_millis(2)).await;
            }
            sent
        });
        let client = HttpClient::new(
            ClientBuilder::new().no_proxy(),
            format!("http://{address}"),
            ValidationLimits::default(),
            test_limits(1024, 2048, 1024),
            1,
            HttpTimeoutPolicy::default(),
            HttpCredentials::new("test-token"),
        )
        .expect("streaming test client");

        let error = client
            .send::<()>(get_request())
            .await
            .expect_err("second chunk exceeds limit");
        assert!(matches!(
            error,
            crate::error::AnytypeError::ResponseTooLarge { limit: 1024, .. }
        ));
        let sent = server.await.expect("streaming server task");
        assert!(sent < 100, "client should close before all chunks are sent");
        assert!(client.metrics_snapshot().bytes_received < 100 * 1024);
    }

    #[test]
    fn empty_success_body_deserializes_as_unit() {
        deserialize_json::<()>(b"").expect("empty mutation response");
    }

    #[test]
    fn diagnostic_path_keeps_only_bounded_non_control_path_context() {
        let secret = "URL_PASSWORD_SENTINEL";
        let path = diagnostic_path(&format!(
            "https://user:{secret}@example.invalid/v1/objects?token=QUERY_SECRET#fragment"
        ));
        assert_eq!(path, "/v1/objects");
        assert!(!path.contains(secret));
        assert!(!path.contains("QUERY_SECRET"));
        assert_eq!(
            diagnostic_path("//user:SCHEME_PASSWORD@example.invalid/v1/scheme?token=SECRET"),
            "/v1/scheme"
        );

        assert_eq!(
            diagnostic_path("/v1/spaces\nforged?authorization=HEADER_SECRET"),
            REDACTED_DIAGNOSTIC_PATH
        );

        let bounded = diagnostic_path(&format!("/{}?token=QUERY_SECRET", "x".repeat(700)));
        assert_eq!(bounded.chars().count(), MAX_DIAGNOSTIC_PATH_CHARS + 1);
        assert!(bounded.ends_with('…'));
        assert!(!bounded.contains("QUERY_SECRET"));
    }

    #[tokio::test]
    async fn malformed_targets_fail_closed_across_standard_http_diagnostics() {
        let malformed_absolute =
            "https://user:MALFORMED_PASSWORD@[invalid-host/v1?token=MALFORMED_QUERY";
        let malformed_scheme_relative =
            "//user:SCHEME_RELATIVE_PASSWORD@[invalid-host/v1?token=SCHEME_QUERY";
        let unsupported_target =
            "credential:UNSUPPORTED_PASSWORD@example.invalid/v1?token=UNSUPPORTED_QUERY";
        let control_target = "/v1/CONTROL_PATH\nCONTROL_SECRET?token=CONTROL_QUERY";
        for target in [
            malformed_absolute,
            malformed_scheme_relative,
            unsupported_target,
            control_target,
            "relative/path?token=RELATIVE_QUERY",
            "///user:TRIPLE_SLASH_PASSWORD@example.invalid/v1?token=TRIPLE_QUERY",
            "https:////user:EXCESS_SLASH_PASSWORD@example.invalid/v1?token=EXCESS_QUERY",
            "https:\\user:BACKSLASH_PASSWORD@example.invalid/v1?token=BACKSLASH_QUERY",
            "/v1/%ZZ/PERCENT_PASSWORD?token=PERCENT_QUERY",
            "/v1/SPACE PASSWORD?token=SPACE_QUERY",
        ] {
            assert_eq!(diagnostic_path(target), REDACTED_DIAGNOSTIC_PATH);
        }

        let config = ClientConfig {
            base_url: Some(malformed_absolute.to_owned()),
            app_name: "diagnostic-constructor".to_owned(),
            keystore: Some("env".to_owned()),
            keystore_service: Some("diagnostic-constructor".to_owned()),
            grpc_endpoint: Some(malformed_scheme_relative.to_owned()),
            ..ClientConfig::default()
        };
        let config_debug = format!("{config:?}");
        let (dispatch, output) = capture();
        let client = tracing::dispatcher::with_default(&dispatch, || {
            let client = AnytypeClient::with_config(config).expect("diagnostic fixture client");
            let request = HttpRequest {
                method: Method::POST,
                path: control_target.to_owned(),
                query: vec![("authorization".to_owned(), "TRACE_QUERY_SECRET".to_owned())],
                body: Some(bytes::Bytes::from_static(b"TRACE_DOCUMENT_SECRET")),
            };
            log_request(&request);
            log_response(
                malformed_scheme_relative,
                &bytes::Bytes::from_static(b"TRACE_RESPONSE_SECRET"),
            );
            log_http_status(&request, StatusCode::BAD_GATEWAY, "api_error", 1);
            client
        });
        let api_error = AnytypeError::ApiError {
            code: 502,
            method: "GET".to_owned(),
            url: malformed_absolute.to_owned(),
            message: "MALFORMED_RESPONSE_SECRET".to_owned(),
        };
        let source = reqwest::Client::new()
            .get(malformed_absolute)
            .send()
            .await
            .expect_err("malformed URL must fail before transport")
            .without_url();
        let transport_error = AnytypeError::Http {
            method: "GET".to_owned(),
            url: malformed_absolute.to_owned(),
            source,
            outcome: None,
            elapsed: None,
            attempts: None,
        };

        let mut diagnostics = format!(
            "{} {config_debug} {client:?} {:?} {api_error} {api_error:?} {} {transport_error} {transport_error:?} {}",
            output.contents(),
            client.client,
            api_error.diagnostic(),
            transport_error.diagnostic()
        );
        assert!(std::error::Error::source(&transport_error).is_none());
        let mut source = std::error::Error::source(&transport_error);
        while let Some(current) = source {
            diagnostics.push_str(&format!(" {current} {current:?}"));
            source = current.source();
        }

        assert!(diagnostics.contains(REDACTED_DIAGNOSTIC_PATH));
        for secret in [
            "MALFORMED_PASSWORD",
            "MALFORMED_QUERY",
            "SCHEME_RELATIVE_PASSWORD",
            "SCHEME_QUERY",
            "UNSUPPORTED_PASSWORD",
            "UNSUPPORTED_QUERY",
            "CONTROL_SECRET",
            "CONTROL_QUERY",
            "RELATIVE_QUERY",
            "TRIPLE_SLASH_PASSWORD",
            "TRIPLE_QUERY",
            "EXCESS_SLASH_PASSWORD",
            "EXCESS_QUERY",
            "BACKSLASH_PASSWORD",
            "BACKSLASH_QUERY",
            "PERCENT_PASSWORD",
            "PERCENT_QUERY",
            "SPACE PASSWORD",
            "SPACE_QUERY",
            "TRACE_QUERY_SECRET",
            "TRACE_DOCUMENT_SECRET",
            "TRACE_RESPONSE_SECRET",
            "MALFORMED_RESPONSE_SECRET",
        ] {
            assert!(
                !diagnostics.contains(secret),
                "standard diagnostics exposed {secret}: {diagnostics}"
            );
        }
    }

    #[tokio::test]
    async fn non_whitespace_controls_fail_closed_across_aggregated_http_surfaces() {
        let controls = [
            ('\0', "NUL"),
            ('\u{1}', "SOH"),
            ('\u{7}', "BEL"),
            ('\u{7f}', "DEL"),
        ];
        let mut target_sets = Vec::new();
        let mut secrets = Vec::new();
        for (control, label) in controls {
            let password = format!("{label}_PASSWORD_SECRET");
            let query = format!("{label}_QUERY_SECRET");
            target_sets.push([
                format!(
                    "https://user:{password}@example.invalid/v1/{control}absolute?token={query}"
                ),
                format!("//user:{password}@example.invalid/v1/{control}scheme?token={query}"),
                format!("/v1/{control}origin?token={query}"),
            ]);
            secrets.push(password);
            secrets.push(query);
        }

        for target in target_sets.iter().flatten() {
            assert_eq!(
                diagnostic_path(target),
                REDACTED_DIAGNOSTIC_PATH,
                "control-bearing target must fail closed: {target:?}"
            );
        }

        let config = ClientConfig {
            base_url: Some(target_sets[0][0].clone()),
            app_name: "control-diagnostic-constructor".to_owned(),
            keystore: Some("env".to_owned()),
            keystore_service: Some("control-diagnostic-constructor".to_owned()),
            grpc_endpoint: Some(target_sets[1][1].clone()),
            ..ClientConfig::default()
        };
        let config_debug = format!("{config:?}");
        let (dispatch, output) = capture();
        let client = tracing::dispatcher::with_default(&dispatch, || {
            let client = AnytypeClient::with_config(config).expect("control diagnostic client");
            for target in target_sets.iter().flatten() {
                let request = HttpRequest {
                    method: Method::POST,
                    path: target.clone(),
                    query: vec![(
                        "authorization".to_owned(),
                        "CONTROL_TRACE_QUERY_SECRET".to_owned(),
                    )],
                    body: Some(bytes::Bytes::from_static(b"CONTROL_TRACE_DOCUMENT_SECRET")),
                };
                log_request(&request);
                log_response(
                    target,
                    &bytes::Bytes::from_static(b"CONTROL_TRACE_RESPONSE_SECRET"),
                );
                log_http_status(&request, StatusCode::BAD_GATEWAY, "api_error", 1);
            }
            client
        });

        let mut diagnostics = format!(
            "{} {config_debug} {client:?} {:?}",
            output.contents(),
            client.client
        );
        for target in target_sets.iter().flatten() {
            let api_error = AnytypeError::ApiError {
                code: 502,
                method: "GET".to_owned(),
                url: target.clone(),
                message: "CONTROL_API_RESPONSE_SECRET".to_owned(),
            };
            diagnostics.push_str(&format!(
                " {api_error} {api_error:?} {}",
                api_error.diagnostic()
            ));

            let source = reqwest::Client::new()
                .get("relative-invalid-source")
                .send()
                .await
                .expect_err("relative source URL must fail")
                .without_url();
            let transport_error = AnytypeError::Http {
                method: "GET".to_owned(),
                url: target.clone(),
                source,
                outcome: None,
                elapsed: None,
                attempts: None,
            };
            diagnostics.push_str(&format!(
                " {transport_error} {transport_error:?} {}",
                transport_error.diagnostic()
            ));
            assert!(std::error::Error::source(&transport_error).is_none());
        }

        assert!(diagnostics.contains(REDACTED_DIAGNOSTIC_PATH));
        for (control, label) in controls {
            assert!(
                !diagnostics.contains(control),
                "diagnostics retained {label} control: {diagnostics:?}"
            );
        }
        for secret in secrets.iter().map(String::as_str).chain([
            "CONTROL_TRACE_QUERY_SECRET",
            "CONTROL_TRACE_DOCUMENT_SECRET",
            "CONTROL_TRACE_RESPONSE_SECRET",
            "CONTROL_API_RESPONSE_SECRET",
        ]) {
            assert!(
                !diagnostics.contains(secret),
                "standard diagnostics exposed {secret}: {diagnostics:?}"
            );
        }
    }

    #[test]
    fn all_http_trace_levels_remain_metadata_only() {
        let request = HttpRequest {
            method: Method::PATCH,
            path: "https://user:URL_PASSWORD@example.invalid/v1/objects?token=URL_TOKEN".to_owned(),
            query: vec![("authorization".to_owned(), "QUERY_TOKEN".to_owned())],
            body: Some(bytes::Bytes::from_static(b"DOCUMENT_BODY_SECRET")),
        };
        let (dispatch, output) = capture();
        tracing::dispatcher::with_default(&dispatch, || {
            log_request(&request);
            log_response(
                &request.path,
                &bytes::Bytes::from_static(b"RESPONSE_BODY_SECRET"),
            );
            log_http_status(&request, StatusCode::INTERNAL_SERVER_ERROR, "api_error", 2);
        });

        let diagnostics = output.contents();
        assert!(diagnostics.contains("anytype::http_json"));
        assert!(diagnostics.contains("anytype::http"));
        assert!(diagnostics.contains("/v1/objects"));
        assert!(diagnostics.contains("body_bytes=20"));
        assert!(diagnostics.contains("http_status=500"));
        for secret in [
            "URL_PASSWORD",
            "URL_TOKEN",
            "QUERY_TOKEN",
            "DOCUMENT_BODY_SECRET",
            "RESPONSE_BODY_SECRET",
            "authorization",
        ] {
            assert!(
                !diagnostics.contains(secret),
                "diagnostics exposed {secret}: {diagnostics}"
            );
        }
    }

    #[test]
    fn standard_error_and_config_diagnostics_redact_adversarial_http_values() {
        let error = AnytypeError::ApiError {
            code: 502,
            method: "get\nFORGED_METHOD".to_owned(),
            url: "https://user:URL_PASSWORD@example.invalid/v1/objects?token=URL_TOKEN".to_owned(),
            message: "UPSTREAM_RESPONSE_BODY_SECRET".to_owned(),
        };
        let safe = format!("{error} {error:?} {}", error.diagnostic());
        assert!(safe.contains("status=502"));
        assert!(safe.contains("path=/v1/objects"));
        assert!(safe.contains("method=unknown"));
        for secret in [
            "FORGED_METHOD",
            "URL_PASSWORD",
            "URL_TOKEN",
            "UPSTREAM_RESPONSE_BODY_SECRET",
        ] {
            assert!(
                !safe.contains(secret),
                "error diagnostics exposed {secret}: {safe}"
            );
        }

        let rate_limit = AnytypeError::RateLimitExceeded {
            header: "RATE_LIMIT_HEADER_SECRET".to_owned(),
            duration: Duration::from_secs(3),
        };
        let rate_limit_diagnostics =
            format!("{rate_limit} {rate_limit:?} {}", rate_limit.diagnostic());
        assert!(!rate_limit_diagnostics.contains("RATE_LIMIT_HEADER_SECRET"));

        let config = ClientConfig {
            base_url: Some(
                "https://user:CONFIG_PASSWORD@example.invalid/private?token=CONFIG_TOKEN"
                    .to_owned(),
            ),
            app_name: "APP_NAME_SECRET".to_owned(),
            keystore: Some("file:path=/KEYSTORE_PATH_SECRET".to_owned()),
            keystore_service: Some("KEYSTORE_SERVICE_SECRET".to_owned()),
            grpc_endpoint: Some(
                "https://user:GRPC_PASSWORD@example.invalid/grpc?token=GRPC_TOKEN".to_owned(),
            ),
            ..ClientConfig::default()
        };
        let config_diagnostics = format!("{config:?}");
        assert!(config_diagnostics.contains("base_path: Some(\"/private\")"));
        assert!(config_diagnostics.contains("grpc_path: Some(\"/grpc\")"));
        for secret in [
            "CONFIG_PASSWORD",
            "CONFIG_TOKEN",
            "APP_NAME_SECRET",
            "KEYSTORE_PATH_SECRET",
            "KEYSTORE_SERVICE_SECRET",
            "GRPC_PASSWORD",
            "GRPC_TOKEN",
        ] {
            assert!(
                !config_diagnostics.contains(secret),
                "config Debug exposed {secret}: {config_diagnostics}"
            );
        }
    }

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "snake_case")]
    enum DiagnosticChoice {
        Allowed,
    }

    #[derive(Debug, Deserialize)]
    #[expect(dead_code, reason = "deserialization diagnostic fixture")]
    struct DiagnosticEnvelope {
        choice: DiagnosticChoice,
    }

    #[test]
    fn deserialization_diagnostic_omits_rejected_payload_value_and_source() {
        let (dispatch, output) = capture();
        let error = tracing::dispatcher::with_default(&dispatch, || {
            deserialize_json::<DiagnosticEnvelope>(br#"{"choice":"DOCUMENT_VALUE_SECRET"}"#)
                .expect_err("unknown enum value must fail")
        });

        let diagnostics = format!(
            "{} {error} {error:?} {}",
            output.contents(),
            error.diagnostic()
        );
        assert!(diagnostics.contains("error_variant=\"deserialization\""));
        assert!(diagnostics.contains("json_category=Data"));
        assert!(!diagnostics.contains("DOCUMENT_VALUE_SECRET"));
        assert!(std::error::Error::source(&error).is_none());
    }

    #[tokio::test]
    async fn transport_error_source_chain_drops_credential_bearing_url() {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("reserve closed test address");
        let address = listener.local_addr().expect("closed test address");
        drop(listener);

        let client = HttpClient::new(
            ClientBuilder::new().no_proxy(),
            format!("http://user:TRANSPORT_PASSWORD@{address}"),
            ValidationLimits::default(),
            test_limits(1024, 2048, 1024),
            1,
            HttpTimeoutPolicy::default(),
            HttpCredentials::new("AUTHORIZATION_TOKEN_SECRET"),
        )
        .expect("transport test client");
        let error = client
            .send::<()>(HttpRequest {
                method: Method::POST,
                path: "/v1/objects?token=TRANSPORT_QUERY_SECRET".to_owned(),
                query: Vec::new(),
                body: None,
            })
            .await
            .expect_err("closed test address must reject the connection");

        let mut diagnostics = format!("{error} {error:?} {}", error.diagnostic());
        assert!(std::error::Error::source(&error).is_none());
        let mut source = std::error::Error::source(&error);
        while let Some(current) = source {
            diagnostics.push_str(&format!(" {current} {current:?}"));
            source = current.source();
        }

        assert!(diagnostics.contains("path=/v1/objects"));
        for secret in [
            "TRANSPORT_PASSWORD",
            "TRANSPORT_QUERY_SECRET",
            "AUTHORIZATION_TOKEN_SECRET",
        ] {
            assert!(
                !diagnostics.contains(secret),
                "transport diagnostics exposed {secret}: {diagnostics}"
            );
        }
    }

    #[test]
    fn test_retry_for_status() {
        assert!(super::retry_for_status(StatusCode::TOO_MANY_REQUESTS));
        assert!(super::retry_for_status(StatusCode::REQUEST_TIMEOUT));
        assert!(super::retry_for_status(StatusCode::GATEWAY_TIMEOUT));
        assert!(!super::retry_for_status(StatusCode::INTERNAL_SERVER_ERROR));
        for status in [
            StatusCode::REQUEST_TIMEOUT,
            StatusCode::TOO_MANY_REQUESTS,
            StatusCode::INTERNAL_SERVER_ERROR,
            StatusCode::GATEWAY_TIMEOUT,
        ] {
            assert!(super::mutation_status_is_indeterminate(status));
        }
        for status in [StatusCode::BAD_REQUEST, StatusCode::CONFLICT] {
            assert!(!super::mutation_status_is_indeterminate(status));
        }
    }

    #[tokio::test]
    async fn preserved_mutation_status_distinguishes_ambiguous_failures() {
        for (wire_status, expected_status, indeterminate) in [
            ("400 Bad Request", 400, false),
            ("409 Conflict", 409, false),
            ("408 Request Timeout", 408, true),
            ("429 Too Many Requests", 429, true),
            ("500 Internal Server Error", 500, true),
            ("504 Gateway Timeout", 504, true),
        ] {
            let (client, server) = serve_once(fixture_response(wire_status, "", "")).await;
            let response = client
                .post_request_preserve_status::<String, _>(
                    "/test",
                    &(),
                    QueryWithFilters::default(),
                )
                .await
                .expect("preserved mutation status");
            if indeterminate {
                assert!(matches!(
                    response,
                    super::PreservedStatusResponse::Indeterminate { status }
                        if status == expected_status
                ));
            } else {
                assert!(matches!(
                    response,
                    super::PreservedStatusResponse::Rejected { status }
                        if status == expected_status
                ));
            }
            server.await.expect("preserved-status server");
        }
    }

    #[test]
    fn test_parse_retry_after_ratelimit_reset() {
        let mut headers = HeaderMap::new();
        headers.insert("ratelimit-reset", HeaderValue::from_static("3"));
        let parsed = parse_retry_after(&headers).expect("parse retry header");
        assert_eq!(parsed.duration.as_secs(), 3);
        assert_eq!(parsed.header, "3");
    }

    #[test]
    fn test_parse_retry_after_x_rate_limit_duration() {
        let mut headers = HeaderMap::new();
        headers.insert("x-rate-limit-duration", HeaderValue::from_static("10"));
        let parsed = parse_retry_after(&headers).expect("parse retry header");
        assert_eq!(parsed.duration.as_secs(), 10);
        assert_eq!(parsed.header, "10");
    }
}