praxis-proxy-filter 0.7.2

Filter pipeline engine and built-in filters for Praxis
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Praxis Contributors

//! Transport-agnostic HTTP request/response metadata and per-request filter context.

use std::{
    any::Any,
    borrow::Cow,
    collections::{HashMap, VecDeque},
    net::IpAddr,
    sync::Arc,
    time::{Duration, Instant},
};

use http::{HeaderMap, Method, StatusCode, Uri, header::HeaderName};
use praxis_core::{
    connectivity::Upstream, health::HealthRegistry, id::IdGenerator, kv::KvStoreRegistry, time::TimeSource,
};
use praxis_tls::TlsPeerIdentity;

#[cfg(feature = "bound-upstream-request-body")]
use crate::extensions::BoundRequestBodyRewrite;
use crate::{
    FilterError, IterationState,
    body::BodyMode,
    condition::{ConditionError, HeaderSource},
    extensions::{BoundUpstream, RequestExtensions, SelectedClusterApplication},
    pipeline::body::merge_body_mode,
    results::FilterResultSet,
};
#[cfg(feature = "upstream-binding")]
use crate::{extensions::BoundUpstreamFrozen, pipeline::catalog::ClusterApplicationCatalog};

// -----------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------

/// Maximum number of keys per namespace in structured metadata.
///
/// Prevents unbounded accumulation from streaming processors that
/// send unique keys across many response messages. Existing keys
/// can still be overwritten past this limit.
const MAX_STRUCTURED_METADATA_KEYS: usize = 64;

/// Maximum entries allowed in the general `filter_metadata` map.
///
/// Individual keys and values are already size-bounded (64 / 256
/// bytes), but without an entry count cap a filter chain could
/// insert thousands of unique keys per request.
const MAX_METADATA_ENTRIES: usize = 128;

/// Maximum number of distinct `structured_metadata` namespaces per request.
///
/// Each namespace holds its own key-bounded JSON object, but without a
/// cap on the namespace count a processor that derives the namespace from
/// a dynamic/streaming source could accumulate an unbounded number of
/// objects over a single long-lived request. Mirrors the entry cap on
/// `filter_metadata`.
const MAX_STRUCTURED_METADATA_NAMESPACES: usize = 64;

/// Bounded opaque chunks emitted by filters while IRR owns a logical stream.
pub(crate) struct PendingStreamChunks {
    /// FIFO ordering of locally emitted opaque chunks.
    chunks: VecDeque<bytes::Bytes>,
    /// Combined iteration-state and pending-output ceiling.
    max_retained_bytes: usize,
    /// Bytes currently retained in `chunks`.
    retained_bytes: usize,
}

impl PendingStreamChunks {
    /// Create an empty bounded pending-output queue.
    pub(crate) fn new(max_retained_bytes: usize) -> Self {
        Self {
            chunks: VecDeque::new(),
            max_retained_bytes,
            retained_bytes: 0,
        }
    }

    /// Consume the accounting wrapper and return its FIFO queue.
    pub(crate) fn into_chunks(self) -> VecDeque<bytes::Bytes> {
        self.chunks
    }

    /// Drain queued chunks and reset their retained-byte accounting.
    pub(crate) fn drain_chunks(&mut self) -> VecDeque<bytes::Bytes> {
        self.retained_bytes = 0;
        std::mem::take(&mut self.chunks)
    }
}

/// A binding-publish attempt rejected because the logical binding is frozen
/// and the attempted cluster differs from the frozen one.
///
/// The executor freezes the binding right after the first binding router
/// publishes it, whether or not any body participant exists, so a later router
/// cannot silently retarget a request whose body may already have been
/// processed against the binding. Valid configurations never reach this at
/// runtime (pipeline validation rejects any control flow that could publish a
/// second, different binding), so the router treats it as a fail-closed
/// backstop and returns a 500.
#[cfg(feature = "upstream-binding")]
#[derive(Debug)]
pub(crate) struct BindingFrozen {
    /// The frozen logical cluster that remains in effect.
    pub(crate) frozen: Arc<str>,

    /// The different cluster a later router attempted to bind.
    pub(crate) attempted: Arc<str>,
}

/// Trusted header mutation recorded during pre-read body processing.
///
/// Pre-read filters run *before* the request-phase pipeline. Mutations
/// they produce cannot be applied immediately because the request
/// headers have already been captured. Instead, they are stored as an
/// ordered log and replayed when the pipeline runs.
///
/// Downstream-supplied headers are untrusted. Only mutations in this
/// log are considered authoritative by provenance-aware filters such
/// as `endpoint_selector`.
#[derive(Clone, Debug)]
pub enum TrustedHeaderMutation {
    /// Remove the header from the request.
    Remove(HeaderName),

    /// Set (overwrite) the header to a specific value.
    ///
    /// Stores [`http::header::HeaderValue`] to preserve non-text bytes
    /// faithfully.
    Set(HeaderName, http::header::HeaderValue),

    /// Add the header with a string value.
    ///
    /// Uses `String` rather than [`HeaderValue`] because pre-read
    /// `extra_request_headers` are string-typed. Trusted routing
    /// values are always text (e.g. `host:port` addresses).
    ///
    /// [`HeaderValue`]: http::header::HeaderValue
    Add(HeaderName, String),
}

impl TrustedHeaderMutation {
    /// Whether this mutation targets the given header name.
    pub fn matches_header(&self, name: &HeaderName) -> bool {
        match self {
            Self::Remove(n) | Self::Set(n, _) | Self::Add(n, _) => n == name,
        }
    }
}

/// Tri-state result from [`HttpFilterContext::pending_header_value`].
///
/// Distinguishes "not mentioned" from "explicitly removed" so that
/// callers like `endpoint_selector` know whether to fall through to
/// pre-read provenance.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PendingHeaderResult {
    /// The header was not mentioned in any pending mutation list.
    Absent,
    /// The header was explicitly removed by a pending mutation.
    Removed,
    /// The header has a resolved pending value.
    Value(String),
}

/// Tri-state effective value of a header in the trusted mutation log.
///
/// Distinguishes "never mentioned" from "explicitly removed" so the pre-read
/// condition overlay ([`EffectiveHeaders`]) can decide whether to fall through
/// to the original request or mask it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum TrustedHeaderState {
    /// No trusted mutation mentioned the header.
    Absent,
    /// A trusted mutation removed the header; the original is masked.
    Removed,
    /// The header resolved to a single trusted value.
    Value(String),
}

/// Transport mode selected by filters for the next sub-request response.
///
/// This is a provider-agnostic projection of a filter decision. Praxis does
/// not inspect request JSON or expose a YAML switch for this value.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SubRequestResponseMode {
    /// Buffer the complete sub-request response before returning it.
    #[default]
    Buffered,

    /// Return response headers plus a pull-based streaming body.
    Streaming,
}

/// Provider-neutral reason an owned streaming source terminated abnormally.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StreamTerminationCause {
    /// Admission capacity was not acquired in time.
    AdmissionTimeout,
    /// The peer circuit breaker rejected the exchange.
    CircuitOpen,
    /// Connection establishment failed.
    Connect,
    /// The overall or per-step deadline expired.
    DeadlineExceeded,
    /// The upstream produced no bytes within its idle budget.
    IdleTimeout,
    /// Transport I/O failed after connection establishment.
    Io,
    /// A response-body filter failed after commitment.
    Filter,
    /// A configured response byte ceiling was exceeded.
    ResponseTooLarge,
}

/// Typed abnormal termination exposed to streaming completion filters.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StreamTermination {
    /// Framework-level failure classification.
    cause: StreamTerminationCause,
    /// Set by a completion filter that produced a valid terminal sequence.
    handled: bool,
}

impl StreamTermination {
    /// Create an unhandled termination value.
    pub(crate) fn new(cause: StreamTerminationCause) -> Self {
        Self { cause, handled: false }
    }

    /// The provider-neutral termination classification.
    pub fn cause(&self) -> StreamTerminationCause {
        self.cause
    }

    /// Whether a completion filter converted the failure into final bytes.
    pub fn is_handled(&self) -> bool {
        self.handled
    }
}

// -----------------------------------------------------------------------------
// HttpFilterContext
// -----------------------------------------------------------------------------

/// Per-request mutable state shared across all HTTP filters.
///
/// Created by the protocol layer for each incoming request. Filters read
/// and mutate it to select clusters, choose upstreams, and inject headers.
#[expect(
    clippy::struct_excessive_bools,
    reason = "flags map to independent protocol concerns"
)]
pub struct HttpFilterContext<'a> {
    /// Complete request body captured by protocol pre-read, when the
    /// pipeline's effective request mode is [`BodyMode::StreamBuffer`].
    ///
    /// This remains available during `on_request` even when a filter's body
    /// hook was skipped because body-derived header mutations changed its
    /// request conditions between the pre-read and header phases.
    pub buffered_request_body: Option<bytes::Bytes>,

    /// Per-filter body-done tracking. When `true` at index `i`,
    /// filter `i` is skipped for remaining body chunks.
    pub body_done_indices: Vec<bool>,

    /// Iteration counters for re-entrant branches.
    /// Branch name -> current iteration count.
    pub branch_iterations: HashMap<Arc<str>, u32>,

    /// Downstream client IP address (from the TCP connection).
    pub client_addr: Option<IpAddr>,

    /// The cluster name selected by the router filter.
    pub cluster: Option<Arc<str>>,

    /// Stable invocation ID of the filter currently executing.
    ///
    /// Assigned at pipeline build time and unique within the
    /// request's pinned [`FilterPipeline`]. Set by the pipeline
    /// executor before each filter hook call and cleared after.
    /// Filter state accessors use this as the storage key so
    /// that multiple instances of the same filter type — including
    /// filters in branch chains — get independent state.
    ///
    /// [`FilterPipeline`]: crate::FilterPipeline
    pub current_filter_id: Option<usize>,

    /// Whether the downstream connection uses TLS.
    ///
    /// Set by the protocol layer from the connection's SSL
    /// digest. Used by the forwarded headers filter to derive
    /// `X-Forwarded-Proto` from the actual connection state
    /// rather than the request URI scheme (which is absent
    /// in HTTP/1.1).
    pub downstream_tls: bool,

    /// Matched route path pattern for metrics (bounded; not the raw URL).
    pub metrics_route: Option<::metrics::SharedString>,

    /// Verified downstream TLS peer identity, if the connection
    /// is mTLS and the peer presented a valid client certificate.
    ///
    /// `None` for plain-TLS or non-TLS connections, or when the
    /// client did not send a certificate (e.g. `client_cert_mode:
    /// request` with no cert).  Populated once from the SSL digest
    /// before the first filter runs and preserved across all
    /// subsequent `build_filter_context()` calls for the request.
    pub peer_identity: Option<Arc<TlsPeerIdentity>>,

    /// Type-safe request-scoped extension container.
    ///
    /// Filters store and retrieve arbitrary typed values that
    /// persist across all Pingora lifecycle phases (request,
    /// request body, response, response body, logging). Keyed
    /// by [`TypeId`], so only one value per concrete type. Use
    /// private newtypes to avoid collisions between independent
    /// filters.
    ///
    /// [`TypeId`]: std::any::TypeId
    pub extensions: RequestExtensions,

    /// Branch filters whose `on_request` ran during the request
    /// phase, indexed by `filter_id`. The response phase runs
    /// `on_response` only for the branch filters marked here, the
    /// way [`executed_filter_indices`] gates top-level filters.
    ///
    /// [`executed_filter_indices`]: Self::executed_filter_indices
    pub executed_branch_filters: Vec<bool>,

    /// Tracks which pipeline filter indices actually executed
    /// during the request phase. The response phase skips
    /// filters that did not run (e.g. due to `SkipTo`).
    pub executed_filter_indices: Vec<bool>,

    /// Extra headers to inject into the upstream request.
    pub extra_request_headers: Vec<(Cow<'static, str>, String)>,

    /// Headers to remove from the upstream request.
    pub request_headers_to_remove: Vec<HeaderName>,

    /// Headers to set (overwrite) on the upstream request.
    pub request_headers_to_set: Vec<(HeaderName, http::header::HeaderValue)>,

    /// Durable per-request metadata that persists across all
    /// Pingora lifecycle phases (request, request-body, response,
    /// response-body, logging). Unlike [`filter_results`] which
    /// are cleared after branch evaluation, metadata survives
    /// for the entire request lifetime.
    ///
    /// Keys use dot-prefix namespacing by convention
    /// (e.g. `json_rpc.kind`, `classifier.label`).
    ///
    /// [`filter_results`]: Self::filter_results
    pub filter_metadata: HashMap<String, String>,

    /// How the upstream gRPC call ended.
    ///
    /// Read from the response trailers, or from the response header
    /// block of a Trailers-Only response. `None` for a non-gRPC
    /// response, and during the request phase — the trailers have not
    /// arrived yet.
    pub grpc_completion: Option<praxis_core::grpc::GrpcCompletion>,

    /// Trusted header mutations recorded by *earlier* pre-read passes.
    ///
    /// Read-only for filters: the protocol layer seeds it before each
    /// pre-read pass so condition evaluation can see headers a promoter
    /// wrote on a previous chunk. Empty during the request phase.
    pub prior_pre_read_mutations: Vec<TrustedHeaderMutation>,

    /// Ordered log of trusted header mutations written during the current
    /// pass (or, in the request phase, the full accumulated log). Replayed
    /// by the protocol layer after the request-phase pipeline runs.
    pub pre_read_mutations: Vec<TrustedHeaderMutation>,

    /// Structured per-request metadata keyed by namespace.
    ///
    /// Unlike [`filter_metadata`] which stores flat string
    /// key-value pairs, this stores nested JSON values per
    /// namespace. Used by filters that need to pass structured
    /// data (e.g. dynamic metadata from external filters) across lifecycle
    /// phases.
    ///
    /// [`filter_metadata`]: Self::filter_metadata
    pub structured_metadata: HashMap<String, serde_json::Value>,

    /// Filter result map: `filter_name` -> result entries.
    ///
    /// Filters write string key-value pairs here during
    /// `on_request` or `on_response`. The pipeline executor
    /// reads these to evaluate branch conditions. Cleared
    /// after branch evaluation at each filter.
    pub filter_results: HashMap<&'static str, FilterResultSet>,

    /// Typed per-filter state that persists across all lifecycle
    /// phases (request, request-body, response, response-body).
    ///
    /// Keyed by stable filter invocation ID, unique within the
    /// request's pinned [`FilterPipeline`]. Swapped into each
    /// `HttpFilterContext` from the protocol-layer request context
    /// and written back after filter execution, following the same
    /// pattern as [`filter_metadata`].
    ///
    /// [`FilterPipeline`]: crate::FilterPipeline
    /// [`filter_metadata`]: Self::filter_metadata
    pub filter_state: HashMap<usize, Box<dyn Any + Send + Sync>>,

    /// Shared health registry for endpoint health lookups.
    pub health_registry: Option<&'a HealthRegistry>,

    /// Shared request ID generator.
    pub id_generator: &'a IdGenerator,

    /// Named key-value stores for runtime mappings.
    pub kv_stores: Option<&'a KvStoreRegistry>,

    /// Per-cluster session stores for sticky session affinity.
    pub session_stores: Option<&'a Arc<crate::SessionStoreRegistry>>,

    /// Shared sub-request client for iterative sub-requests.
    pub subrequest_client: Option<&'a praxis_core::subrequest::SubRequestClient>,

    /// Filter-selected transport mode for the next sub-request response.
    ///
    /// Every newly constructed context starts in [`Buffered`] mode. A caller
    /// that reuses context state across iterative steps must reset this field
    /// before running the next step pipeline.
    ///
    /// [`Buffered`]: SubRequestResponseMode::Buffered
    pub subrequest_response_mode: SubRequestResponseMode,

    /// Transport-agnostic request headers, URI, and method.
    pub request: &'a Request,

    /// Accumulated request body bytes seen so far.
    pub request_body_bytes: u64,

    /// Per-request body delivery mode for the request direction.
    /// Defaults to [`BodyMode::Stream`]; filters may upgrade it
    /// via [`set_request_body_mode`].
    ///
    /// [`set_request_body_mode`]: Self::set_request_body_mode
    pub request_body_mode: BodyMode,

    /// When the request was received; available in all phases.
    pub request_start: Instant,

    /// Accumulated response body bytes seen so far.
    pub response_body_bytes: u64,

    /// Per-request body delivery mode for the response direction.
    /// Defaults to [`BodyMode::Stream`]; filters may upgrade it
    /// via [`set_response_body_mode`].
    ///
    /// [`set_response_body_mode`]: Self::set_response_body_mode
    pub response_body_mode: BodyMode,

    /// The upstream response headers, available during `on_response`.
    /// `None` during the request phase.
    pub response_header: Option<&'a mut Response>,

    /// Optional hint that a filter modified the response headers during
    /// `on_response`, used by the protocol layer to skip unnecessary work.
    ///
    /// Setting this is never required for correctness: the protocol layer
    /// independently compares the response header name sequence before and
    /// after the pipeline and rebuilds when it changed. Leaving it unset
    /// only forgoes an optimisation, never an edit.
    pub response_headers_modified: bool,

    /// Whether the upstream owns this request's outcome: it was contacted
    /// (Pingora ran `upstream_peer`) and the client did not go away before
    /// the upstream answered. `false` when the request was rejected or
    /// aborted before any upstream connection, or ended by the client
    /// first, so response-phase filters (e.g. the circuit breaker) can
    /// tell a genuine upstream failure from one the upstream never caused.
    pub upstream_reached: bool,

    /// Index of the selected endpoint in the cluster's
    /// endpoint list. Set by the load balancer filter
    /// for use by passive health checking in the
    /// protocol layer.
    pub selected_endpoint_index: Option<usize>,

    /// Endpoints already attempted for this request (alternate-host retry).
    pub attempted_endpoints: Vec<Arc<str>>,

    /// Resolved retry policy snapshot for this request.
    pub retry_policy: Option<Arc<praxis_core::config::RetryPolicy>>,

    /// Optional route-level retry policy override (merged by the load balancer).
    pub route_retry_policy: Option<Arc<praxis_core::config::RetryPolicy>>,

    /// Shared cluster retry state (budget + active-request counter).
    pub cluster_retry_state: Option<Arc<praxis_core::retry::ClusterRetryState>>,

    /// Whether `cluster_retry_state.leave()` has already been called.
    pub cluster_retry_state_released: bool,

    /// Reselector for alternate-host retries after connect/response failure.
    pub endpoint_reselector: Option<Arc<crate::EndpointReselector>>,
    /// Address of an endpoint pinned by session affinity.
    ///
    /// Set by the sticky sessions filter on cache hit. The load
    /// balancer consumes this to build a proper [`Upstream`] with
    /// the cluster's TLS and connection options, then clears it.
    /// This avoids duplicating connection config across filters.
    pub pinned_endpoint_address: Option<Arc<str>>,

    /// Wall-clock time source for timestamp generation.
    pub time_source: &'a dyn TimeSource,

    /// Rewritten URI path for the upstream request.
    ///
    /// Set by the `path_rewrite` or `url_rewrite` filter during
    /// `on_request`. Applied to the upstream `RequestHeader` in the
    /// protocol layer.
    ///
    /// The router checks this field before the original request URI.
    /// If a preceding filter sets `rewritten_path`, the router
    /// matches against it, enabling "rewrite then route" pipelines.
    ///
    /// If both `path_rewrite` and `url_rewrite` appear in the same
    /// pipeline, only the last writer's value takes effect.
    /// Pipeline validation rejects this by default; set
    /// `allow_rewrite_override: true` on the later filter to
    /// permit it. Or, better yet, don't.
    pub rewritten_path: Option<String>,

    /// The upstream peer selected by the load balancer filter.
    pub upstream: Option<Upstream>,
}

/// Leftover per-read timeout a response-body filter asked to apply to
/// the live streaming body.
///
/// Dispatch snapshots `ctx.upstream`'s `read_timeout` into
/// [`praxis_core::subrequest::SubResponseBody`]. Mutating a reconstructed
/// `ctx.upstream` later does not change that snapshot, so filters store
/// leftover budget here and the streaming executor copies it onto the
/// active read timer.
pub(crate) struct StreamReadTimeoutCap(Duration);

impl StreamReadTimeoutCap {
    /// Construct a leftover per-read timeout marker.
    pub(crate) const fn new(timeout: Duration) -> Self {
        Self(timeout)
    }
}

/// Absolute stream deadline requested during a response-body filter pass.
///
/// Stored in [`RequestExtensions`] so the streaming executor can copy the
/// cap onto the live [`SubResponseBody`](praxis_core::subrequest::SubResponseBody).
pub(crate) struct StreamDeadlineCap(Instant);

impl StreamDeadlineCap {
    /// Construct a leftover absolute stream deadline marker.
    pub(crate) const fn new(deadline: Instant) -> Self {
        Self(deadline)
    }
}

impl HttpFilterContext<'_> {
    /// Selected cluster name, if any.
    pub fn cluster_name(&self) -> Option<&str> {
        self.cluster.as_deref()
    }

    /// Upstream peer address, if selected.
    pub fn upstream_addr(&self) -> Option<&str> {
        self.upstream.as_ref().map(|u| &*u.address)
    }

    /// Cap the live streaming body's next per-chunk read at `timeout`.
    ///
    /// Dispatch copies the selected peer's `read_timeout` into the live
    /// [`SubResponseBody`](praxis_core::subrequest::SubResponseBody). A
    /// reconstructed `ctx.upstream` is detached from that snapshot, so
    /// recapping leftover budget on the peer does not change the active
    /// read timer. Response-body filters call this instead; the streaming
    /// executor applies the cap after the body-filter pass.
    ///
    /// A tighter existing cap is left in place.
    pub fn cap_stream_read_timeout(&mut self, timeout: Duration) {
        let next = self
            .extensions
            .get::<StreamReadTimeoutCap>()
            .map_or(timeout, |existing| existing.0.min(timeout));
        self.extensions.insert(StreamReadTimeoutCap::new(next));
    }

    /// Leftover per-read timeout requested during this body-filter pass.
    pub fn stream_read_timeout_cap(&self) -> Option<Duration> {
        self.extensions.get::<StreamReadTimeoutCap>().map(|cap| cap.0)
    }

    /// Take leftover per-read timeout so the streaming executor can apply it.
    pub(crate) fn take_stream_read_timeout_cap(&mut self) -> Option<Duration> {
        self.extensions.remove::<StreamReadTimeoutCap>().map(|cap| cap.0)
    }

    /// Tighten the live streaming body's absolute deadline.
    ///
    /// Unlike [`cap_stream_read_timeout`](Self::cap_stream_read_timeout), this
    /// publishes a monotonic cutoff that the transport checks before each
    /// upstream read. Downstream backpressure can delay the next poll without
    /// extending the deadline.
    ///
    /// Only the filtered sub-request streaming executor reads this cap, so it
    /// does not bound a normally proxied upstream response.
    ///
    /// A tighter existing deadline is left in place.
    pub fn cap_stream_deadline(&mut self, deadline: Instant) {
        let next = self
            .extensions
            .get::<StreamDeadlineCap>()
            .map_or(deadline, |existing| existing.0.min(deadline));
        self.extensions.insert(StreamDeadlineCap::new(next));
    }

    /// Absolute stream deadline requested during this body-filter pass.
    pub fn stream_deadline_cap(&self) -> Option<Instant> {
        self.extensions.get::<StreamDeadlineCap>().map(|cap| cap.0)
    }

    /// Take the absolute stream deadline so the streaming executor can apply it.
    pub(crate) fn take_stream_deadline_cap(&mut self) -> Option<Instant> {
        self.extensions.remove::<StreamDeadlineCap>().map(|cap| cap.0)
    }

    /// Opaque application protocol of the cluster selected for this exchange.
    ///
    /// Published by the load balancer after a successful upstream selection
    /// and stable for the life of the exchange, so every phase (request,
    /// response, response-body, logging) observes the same value. `None`
    /// when no cluster was selected or the selected cluster declared no
    /// `application_protocol`. The value is opaque to Praxis core; consuming
    /// filters interpret it.
    pub fn selected_application_protocol(&self) -> Option<&str> {
        self.extensions
            .get::<SelectedClusterApplication>()
            .and_then(SelectedClusterApplication::protocol)
    }

    /// Opaque application provider of the cluster selected for this exchange.
    ///
    /// Companion to [`selected_application_protocol`]; identical lifecycle
    /// and opacity, reflecting the selected cluster's `application_provider`.
    ///
    /// [`selected_application_protocol`]: Self::selected_application_protocol
    pub fn selected_application_provider(&self) -> Option<&str> {
        self.extensions
            .get::<SelectedClusterApplication>()
            .and_then(SelectedClusterApplication::provider)
    }

    /// Publish the selected cluster's opaque application metadata into the
    /// request-scoped extensions.
    ///
    /// Called by the trusted built-in load balancer after a successful upstream
    /// selection. Publication is authoritative: a tagged cluster replaces any
    /// prior value, and an untagged cluster removes it. This matters because a
    /// single [`RequestExtensions`] is threaded across `iterative_request_router`
    /// steps; without the removal, a later step selecting an untagged cluster
    /// would leave the previous step's protocol/provider visible and a consuming
    /// filter could apply the wrong transformation.
    pub(crate) fn publish_selected_application(&mut self, protocol: Option<Arc<str>>, provider: Option<Arc<str>>) {
        match SelectedClusterApplication::new(protocol, provider) {
            Some(app) => self.extensions.insert(app),
            None => {
                self.extensions.remove::<SelectedClusterApplication>();
            },
        }
    }

    /// Logical cluster bound for the whole downstream request, if any.
    ///
    /// Published by the router when it matches a route in a binding-enabled
    /// pipeline and stable across every IRR iteration (unlike the
    /// exchange-local selected-upstream metadata). `None` in ordinary router
    /// pipelines and before a binding router has matched.
    pub fn bound_cluster(&self) -> Option<&str> {
        self.extensions.get::<BoundUpstream>().map(BoundUpstream::cluster)
    }

    /// Opaque application protocol of the bound cluster, if bound and tagged.
    ///
    /// Companion to [`bound_cluster`]; reflects the bound cluster's
    /// `application_protocol` as resolved from the pipeline cluster catalog.
    /// The value is opaque to Praxis core; consuming filters interpret it.
    ///
    /// [`bound_cluster`]: Self::bound_cluster
    pub fn bound_application_protocol(&self) -> Option<&str> {
        self.extensions
            .get::<BoundUpstream>()
            .and_then(BoundUpstream::application_protocol)
    }

    /// Opaque application provider of the bound cluster, if bound and tagged.
    ///
    /// Companion to [`bound_application_protocol`]; identical lifecycle and
    /// opacity, reflecting the bound cluster's `application_provider`.
    ///
    /// [`bound_application_protocol`]: Self::bound_application_protocol
    pub fn bound_application_provider(&self) -> Option<&str> {
        self.extensions
            .get::<BoundUpstream>()
            .and_then(BoundUpstream::application_provider)
    }

    /// Borrow the bound upstream's application metadata as a condition view.
    ///
    /// Feeds request-phase condition evaluation so a `bound_upstream` predicate
    /// can match on the router-published `application_protocol` /
    /// `application_provider`. Returns an empty view (matching nothing) when no
    /// upstream has been bound.
    #[cfg(feature = "upstream-binding")]
    pub(crate) fn bound_upstream_view(&self) -> crate::condition::BoundUpstreamView<'_> {
        self.extensions
            .get::<BoundUpstream>()
            .map_or_else(crate::condition::BoundUpstreamView::default, |bound| {
                crate::condition::BoundUpstreamView {
                    protocol: bound.application_protocol(),
                    provider: bound.application_provider(),
                }
            })
    }

    /// Without the `upstream-binding` feature nothing publishes a binding, so
    /// the view is always empty and costs no lookup.
    #[cfg(not(feature = "upstream-binding"))]
    #[expect(clippy::unused_self, reason = "keeps the signature of the feature-on version")]
    pub(crate) fn bound_upstream_view(&self) -> crate::condition::BoundUpstreamView<'_> {
        crate::condition::BoundUpstreamView::default()
    }

    /// Publish (or replace) the logical upstream binding for this request.
    ///
    /// Called by the trusted built-in router after it selects a route's
    /// cluster. `protocol`/`provider` come from the pipeline cluster catalog,
    /// keyed by `cluster`.
    ///
    /// Before the executor freezes the binding, a later router replaces the
    /// previous value. Once frozen (see [`bound_upstream_frozen`]), republishing
    /// the same cluster is an idempotent no-op, and
    /// attempting to publish a different cluster fails closed with
    /// [`BindingFrozen`] so exchange-local routing cannot retarget a request
    /// whose body was already processed against the frozen binding.
    ///
    /// [`bound_upstream_frozen`]: Self::bound_upstream_frozen
    #[cfg(feature = "upstream-binding")]
    pub(crate) fn publish_bound_upstream(
        &mut self,
        cluster: Arc<str>,
        protocol: Option<Arc<str>>,
        provider: Option<Arc<str>>,
    ) -> Result<(), BindingFrozen> {
        if self.bound_upstream_frozen() {
            match self.bound_cluster() {
                Some(existing) if existing == cluster.as_ref() => return Ok(()),
                Some(existing) => {
                    let frozen = Arc::from(existing);
                    return Err(BindingFrozen {
                        frozen,
                        attempted: cluster,
                    });
                },
                // The barrier only marks itself once a cluster is bound, so a
                // frozen-but-unbound state cannot arise; fall through and
                // publish defensively rather than panic.
                None => {},
            }
        }
        self.extensions.insert(BoundUpstream::new(cluster, protocol, provider));
        Ok(())
    }

    /// Resolve `cluster` through the pipeline `catalog` and publish the binding.
    ///
    /// The trusted built-in router calls this before it commits its
    /// exchange-local route fields. A cluster absent from the catalog, or one
    /// declared without tags, binds with no metadata rather than failing.
    ///
    /// # Errors
    ///
    /// Returns [`BindingFrozen`] if the binding is frozen and `cluster` differs
    /// from the frozen one.
    #[cfg(feature = "upstream-binding")]
    pub(crate) fn bind_upstream(
        &mut self,
        cluster: Arc<str>,
        catalog: &ClusterApplicationCatalog,
    ) -> Result<(), BindingFrozen> {
        let (protocol, provider) = catalog
            .lookup(&cluster)
            .map_or((None, None), |meta| (meta.protocol_arc(), meta.provider_arc()));
        self.publish_bound_upstream(cluster, protocol, provider)
    }

    /// Whether the request's logical upstream binding is frozen.
    ///
    /// Freezing also serves as the once-per-request marker for the bound-body
    /// phase: a `ReEnter` loop or IRR continuation cannot replay the hooks or
    /// retarget the request after body processing.
    #[cfg(feature = "upstream-binding")]
    pub(crate) fn bound_upstream_frozen(&self) -> bool {
        self.extensions.get::<BoundUpstreamFrozen>().is_some()
    }

    /// Take the request body the bound-upstream body phase rewrote, if a
    /// read-write participant ran for this request.
    ///
    /// The transport calls this once after the request phase and forwards the
    /// returned body, replaying it on retry, in place of the pre-read body.
    /// `None` means the pre-read body stands.
    #[doc(hidden)]
    #[cfg(feature = "bound-upstream-request-body")]
    pub fn take_bound_request_body_rewrite(&mut self) -> Option<bytes::Bytes> {
        self.extensions
            .remove::<BoundRequestBodyRewrite>()
            .map(|rewrite| rewrite.0)
    }

    /// Without the `bound-upstream-request-body` feature no participant can
    /// rewrite the body, so there is never anything to take.
    #[doc(hidden)]
    #[cfg(not(feature = "bound-upstream-request-body"))]
    #[expect(
        clippy::unused_self,
        clippy::needless_pass_by_ref_mut,
        reason = "keeps the signature of the feature-on version"
    )]
    pub fn take_bound_request_body_rewrite(&mut self) -> Option<bytes::Bytes> {
        None
    }

    /// Freeze the request's logical upstream binding.
    ///
    /// Idempotent; called by the executor immediately after the first binding
    /// router publishes [`BoundUpstream`], before bound-body participants and
    /// branch chains run. It is set even when there are no body participants,
    /// keeping the logical binding request-stable for conditions, bound load
    /// balancers, and IRR continuations.
    #[cfg(feature = "upstream-binding")]
    pub(crate) fn freeze_bound_upstream(&mut self) {
        self.extensions.insert(BoundUpstreamFrozen);
    }

    /// Shared sub-request client, if set.
    pub(crate) fn subrequest_client(&self) -> Option<&praxis_core::subrequest::SubRequestClient> {
        self.subrequest_client
    }

    /// Return the response transport mode selected for the next sub-request.
    pub fn subrequest_response_mode(&self) -> SubRequestResponseMode {
        self.subrequest_response_mode
    }

    /// Select the response transport mode for the next sub-request.
    ///
    /// Filters own this decision; the transport layer only executes it.
    pub fn set_subrequest_response_mode(&mut self, mode: SubRequestResponseMode) {
        self.subrequest_response_mode = mode;
    }

    /// Emit an opaque response chunk into the IRR-owned logical stream.
    ///
    /// Emission is available only inside an iterative session. A buffered
    /// intermediate step may retain chunks for a later streaming step; if the
    /// iteration instead terminates with a buffered response, IRR rejects the
    /// pending chunks. Chunks emitted by a streaming response-body callback are
    /// delivered in FIFO order before that callback's body output. Pending
    /// chunks are bounded together with retained [`IterationState`]; exceeding
    /// that bound returns an error and enqueues nothing. Praxis does not inspect
    /// or reinterpret the bytes.
    ///
    /// # Errors
    ///
    /// Returns an error outside an IRR step or when the retained-state limit
    /// would be exceeded.
    pub fn emit_stream_chunk(&mut self, bytes: bytes::Bytes) -> Result<(), FilterError> {
        let state_bytes = self
            .extensions
            .get::<IterationState>()
            .map_or(0, IterationState::retained_bytes);
        let pending = self
            .extensions
            .get_mut::<PendingStreamChunks>()
            .ok_or_else(|| -> FilterError {
                "stream chunk emission is only available inside iterative_request_router"
                    .to_owned()
                    .into()
            })?;
        let retained = state_bytes
            .checked_add(pending.retained_bytes)
            .and_then(|value| value.checked_add(bytes.len()))
            .ok_or_else(|| -> FilterError { "stream chunk retained-state size overflow".to_owned().into() })?;
        if retained > pending.max_retained_bytes {
            return Err(format!(
                "stream chunk emission exceeds retained-state limit ({} > {})",
                retained, pending.max_retained_bytes
            )
            .into());
        }
        pending.retained_bytes += bytes.len();
        pending.chunks.push_back(bytes);
        Ok(())
    }

    /// Abnormal source termination visible during a streaming completion hook.
    pub fn stream_termination(&self) -> Option<&StreamTermination> {
        self.extensions.get::<StreamTermination>()
    }

    /// Mark the current abnormal stream termination as converted to a valid
    /// provider-specific terminal sequence by this filter.
    ///
    /// Returns `false` when the step is completing normally.
    pub fn mark_stream_termination_handled(&mut self) -> bool {
        let Some(termination) = self.extensions.get_mut::<StreamTermination>() else {
            return false;
        };
        termination.handled = true;
        true
    }

    /// Enable bounded local stream emission for an IRR step.
    pub(crate) fn enable_stream_chunk_emission(&mut self, max_retained_bytes: usize) {
        self.extensions.insert(PendingStreamChunks::new(max_retained_bytes));
    }

    /// Read a durable metadata value by key.
    pub fn get_metadata(&self, key: &str) -> Option<&str> {
        self.filter_metadata.get(key).map(String::as_str)
    }

    /// How the upstream gRPC call ended, if this was a gRPC call.
    ///
    /// Only populated once the upstream response trailers have been
    /// seen, so it is always `None` during the request phase.
    pub fn grpc_completion(&self) -> Option<&praxis_core::grpc::GrpcCompletion> {
        self.grpc_completion.as_ref()
    }

    /// Request-scoped [`TraceContext`] id, else inbound `x-request-id`.
    ///
    /// [`TraceContext`]: crate::trace_context::TraceContext
    pub fn request_id(&self) -> Option<&str> {
        if let Some(tc) = self.extensions.get::<crate::trace_context::TraceContext>() {
            return Some(tc.request_id());
        }
        self.request.headers.get("x-request-id").and_then(|v| v.to_str().ok())
    }

    /// Inject hop `x-request-id` and `traceparent` when [`TraceContext`] is present.
    ///
    /// [`TraceContext`]: crate::trace_context::TraceContext
    pub fn apply_trace_propagation(&self, framework_headers: &mut praxis_core::subrequest::FrameworkHeaders) {
        let Some(tc) = self.extensions.get::<crate::trace_context::TraceContext>() else {
            return;
        };
        for (name, value) in &self.extra_request_headers {
            if name.eq_ignore_ascii_case("x-request-id") && value != tc.request_id() {
                tracing::warn!(
                    existing = %value,
                    expected = %tc.request_id(),
                    "competing x-request-id pending alongside TraceContext during sub-request propagation"
                );
            }
        }
        if let Err(error) = tc.inject_into(framework_headers, self.id_generator, self.time_source) {
            tracing::warn!(%error, "failed to inject trace correlation into framework headers");
        }
    }

    /// Execute a buffered sub-request, injecting correlation when present.
    ///
    /// # Errors
    ///
    /// Returns [`SubRequestError`] if the client is missing or the exchange fails.
    ///
    /// [`SubRequestError`]: praxis_core::subrequest::SubRequestError
    #[expect(
        clippy::too_many_arguments,
        reason = "mirrors SubRequestClient::execute including framework headers"
    )]
    pub async fn execute_subrequest(
        &self,
        peer: &pingora_core::upstreams::peer::HttpPeer,
        request: &praxis_core::subrequest::SubRequest,
        max_response_bytes: usize,
        timeout: Duration,
        mut framework_headers: praxis_core::subrequest::FrameworkHeaders,
    ) -> Result<praxis_core::subrequest::SubResponse, praxis_core::subrequest::SubRequestError> {
        let client = self.subrequest_client().ok_or_else(|| {
            praxis_core::subrequest::SubRequestError::InvalidRequest(
                "sub-request client is not available on this filter context".to_owned(),
            )
        })?;
        self.apply_trace_propagation(&mut framework_headers);
        let fw = (!framework_headers.is_empty()).then_some(&framework_headers);
        Box::pin(client.execute(peer, request, max_response_bytes, timeout, fw)).await
    }

    /// Send a streaming sub-request, injecting correlation when present.
    ///
    /// # Errors
    ///
    /// Returns [`SubRequestError`] if the client is missing or the exchange fails.
    ///
    /// [`SubRequestError`]: praxis_core::subrequest::SubRequestError
    #[expect(
        clippy::too_many_arguments,
        reason = "mirrors SubRequestClient::send_streaming including framework headers"
    )]
    pub async fn send_streaming_subrequest(
        &self,
        peer: &pingora_core::upstreams::peer::HttpPeer,
        request: &praxis_core::subrequest::SubRequest,
        timeout: Duration,
        limits: praxis_core::subrequest::StreamLimits,
        mut framework_headers: praxis_core::subrequest::FrameworkHeaders,
    ) -> Result<praxis_core::subrequest::StreamingSubResponse, praxis_core::subrequest::SubRequestError> {
        let client = self.subrequest_client().ok_or_else(|| {
            praxis_core::subrequest::SubRequestError::InvalidRequest(
                "sub-request client is not available on this filter context".to_owned(),
            )
        })?;
        self.apply_trace_propagation(&mut framework_headers);
        let fw = (!framework_headers.is_empty()).then_some(&framework_headers);
        Box::pin(client.send_streaming(peer, request, timeout, limits, fw)).await
    }

    /// Write a durable metadata value that persists across all phases.
    ///
    /// Keys should use dot-prefix namespacing
    /// (e.g. `json_rpc.kind`, `classifier.label`). Keys are limited to
    /// 64 bytes and values to 256 bytes to bound per-request
    /// memory growth.
    pub fn set_metadata(&mut self, key: impl Into<String>, value: impl Into<String>) {
        let key = key.into();
        let value = value.into();
        if key.is_empty() || key.len() > 64 {
            tracing::warn!(key_len = key.len(), "metadata key rejected (must be 1-64 bytes)");
            return;
        }
        if value.len() > 256 {
            tracing::warn!(key = %key, value_len = value.len(), "metadata value rejected (max 256 bytes)");
            return;
        }
        if !self.filter_metadata.contains_key(&key) && self.filter_metadata.len() >= MAX_METADATA_ENTRIES {
            tracing::warn!(
                key = %key,
                entries = self.filter_metadata.len(),
                "metadata entry rejected (max {MAX_METADATA_ENTRIES} entries)"
            );
            return;
        }
        self.filter_metadata.insert(key, value);
    }

    /// Upgrade the request body delivery mode for this request.
    ///
    /// Merges `mode` into the current mode using ratchet-up
    /// semantics: `StreamBuffer > SizeLimit > Stream`. A mode
    /// can only be upgraded, never downgraded.
    pub fn set_request_body_mode(&mut self, mode: BodyMode) {
        merge_body_mode(&mut self.request_body_mode, mode);
    }

    /// Upgrade the response body delivery mode for this request.
    ///
    /// Same ratchet-up semantics as [`set_request_body_mode`].
    ///
    /// [`set_request_body_mode`]: Self::set_request_body_mode
    pub fn set_response_body_mode(&mut self, mode: BodyMode) {
        merge_body_mode(&mut self.response_body_mode, mode);
    }

    /// Store typed per-request state for the currently executing filter.
    ///
    /// Uses [`current_filter_id`] as the storage key, so multiple
    /// instances of the same filter type get independent state.
    ///
    /// No-op if called outside of pipeline execution (when
    /// [`current_filter_id`] is `None`).
    ///
    /// [`current_filter_id`]: Self::current_filter_id
    pub fn insert_filter_state<T: Any + Send + Sync>(&mut self, state: T) {
        let Some(idx) = self.current_filter_id else {
            tracing::warn!("insert_filter_state called outside pipeline execution");
            return;
        };
        self.filter_state.insert(idx, Box::new(state));
    }

    /// Retrieve a shared reference to the typed state stored by the
    /// currently executing filter.
    ///
    /// Returns `None` when no state is stored, when the stored type
    /// does not match `T`, or when called outside pipeline execution.
    pub fn get_filter_state<T: Any + Send + Sync>(&self) -> Option<&T> {
        let idx = self.current_filter_id?;
        self.filter_state.get(&idx)?.downcast_ref()
    }

    /// Retrieve a mutable reference to the typed state stored by the
    /// currently executing filter.
    ///
    /// Returns `None` under the same conditions as
    /// [`get_filter_state`].
    ///
    /// [`get_filter_state`]: Self::get_filter_state
    pub fn get_filter_state_mut<T: Any + Send + Sync>(&mut self) -> Option<&mut T> {
        let idx = self.current_filter_id?;
        self.filter_state.get_mut(&idx)?.downcast_mut()
    }

    /// Remove and return the typed state stored by the currently
    /// executing filter.
    ///
    /// Returns `None` when no state is stored, when the stored type
    /// does not match `T`, or when called outside pipeline execution.
    /// A type mismatch does not destroy the stored entry.
    pub fn remove_filter_state<T: Any + Send + Sync>(&mut self) -> Option<T> {
        let idx = self.current_filter_id?;
        if !self.filter_state.get(&idx)?.as_ref().is::<T>() {
            return None;
        }
        let boxed = self.filter_state.remove(&idx)?;
        Some(*boxed.downcast::<T>().ok()?)
    }

    /// Resolve the effective value of a trusted header from the
    /// pre-read mutation log.
    ///
    /// Walks the ordered mutation log forward, applying each mutation
    /// in sequence. Only trusted sources (pre-read filter mutations)
    /// are considered; the original request headers are intentionally
    /// excluded.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - A `Set` mutation contains a non-text [`HeaderValue`]
    /// - Multiple distinct values remain after all mutations (ambiguous final state)
    ///
    /// [`HeaderValue`]: http::header::HeaderValue
    pub fn resolve_trusted_header(&self, name: &HeaderName) -> Result<Option<String>, String> {
        let (values, _touched) = collect_trusted_values(self.trusted_mutations(), name)?;
        require_unique_value(values, name, "trusted")
    }

    /// Resolve the tri-state effective value of a header from the trusted
    /// mutation log (prior passes followed by the current pass).
    ///
    /// Unlike [`resolve_trusted_header`], this distinguishes a header that was
    /// never mentioned ([`Absent`], fall through to the original request) from
    /// one an explicit `Remove` cleared ([`Removed`], mask the original), so
    /// the pre-read condition overlay agrees with the post-merge request.
    ///
    /// # Errors
    ///
    /// Returns an error under the same conditions as [`resolve_trusted_header`]
    /// (non-text `Set` value, or multiple distinct remaining values).
    ///
    /// [`Absent`]: TrustedHeaderState::Absent
    /// [`Removed`]: TrustedHeaderState::Removed
    /// [`resolve_trusted_header`]: Self::resolve_trusted_header
    pub(crate) fn resolve_trusted_header_state(&self, name: &HeaderName) -> Result<TrustedHeaderState, String> {
        let (values, touched) = collect_trusted_values(self.trusted_mutations(), name)?;
        match require_unique_value(values, name, "trusted")? {
            Some(v) => Ok(TrustedHeaderState::Value(v)),
            None if touched => Ok(TrustedHeaderState::Removed),
            None => Ok(TrustedHeaderState::Absent),
        }
    }

    /// Iterator over trusted mutations in application order: prior passes
    /// first, then the current pass.
    fn trusted_mutations(&self) -> impl Iterator<Item = &TrustedHeaderMutation> {
        self.prior_pre_read_mutations
            .iter()
            .chain(self.pre_read_mutations.iter())
    }

    /// Resolve the effective pending value of a header from the
    /// mutation lists (not the original request).
    ///
    /// Returns a tri-state [`PendingHeaderResult`] so callers can
    /// distinguish "not mentioned" from "explicitly removed."
    /// Applies mutations in HTTP order: remove → set → add.
    ///
    /// Multiple distinct values are rejected as ambiguous. This is
    /// intentionally stricter than the normal pipeline's last-write-wins
    /// semantics because routing-critical headers (used by
    /// `endpoint_selector`) must have a single unambiguous value.
    ///
    /// # Errors
    ///
    /// Returns an error if a pending `Set` value contains
    /// non-text bytes, or if the final state has multiple
    /// distinct values.
    pub fn pending_header_value(&self, name: &HeaderName) -> Result<PendingHeaderResult, String> {
        // The pipeline normalizes pending mutations as remove → set → add:
        // a remove clears any prior value, a set establishes a new one, and
        // adds accumulate. When both remove and set are present for the same
        // header, the set wins because it is applied after the remove.
        let removed = self.request_headers_to_remove.iter().any(|n| n == name);
        let set_value = find_last_set(&self.request_headers_to_set, name)?;
        let extras = collect_extras(&self.extra_request_headers, name);

        let mut all: Vec<String> = Vec::new();
        if let Some(s) = set_value {
            all.push(s);
        }
        all.extend(extras);

        if all.is_empty() {
            return Ok(if removed {
                PendingHeaderResult::Removed
            } else {
                PendingHeaderResult::Absent
            });
        }

        match require_unique_value(all, name, "pending")? {
            Some(v) => Ok(PendingHeaderResult::Value(v)),
            None => Ok(if removed {
                PendingHeaderResult::Removed
            } else {
                PendingHeaderResult::Absent
            }),
        }
    }

    /// Set a structured metadata value under a namespace.
    ///
    /// Each namespace is stored as a JSON object; `key` becomes
    /// a field within that object. If the namespace does not yet
    /// exist, a new empty object is created first.
    ///
    /// A per-namespace key limit of 64
    /// prevents unbounded accumulation from streaming processors.
    /// New keys are silently dropped once the limit is reached;
    /// existing keys can still be overwritten.
    pub fn set_structured_metadata(&mut self, namespace: &str, key: &str, value: serde_json::Value) {
        if !self.structured_metadata.contains_key(namespace)
            && self.structured_metadata.len() >= MAX_STRUCTURED_METADATA_NAMESPACES
        {
            tracing::warn!(
                namespace,
                limit = MAX_STRUCTURED_METADATA_NAMESPACES,
                "structured metadata namespace limit reached; dropping new namespace"
            );
            return;
        }
        let ns = self
            .structured_metadata
            .entry(namespace.to_owned())
            .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
        if let serde_json::Value::Object(map) = ns {
            if map.len() >= MAX_STRUCTURED_METADATA_KEYS && !map.contains_key(key) {
                tracing::warn!(
                    namespace,
                    key,
                    limit = MAX_STRUCTURED_METADATA_KEYS,
                    "structured metadata key limit reached; dropping new key"
                );
                return;
            }
            map.insert(key.to_owned(), value);
        }
    }

    /// Get a structured metadata value from a namespace.
    ///
    /// Returns `None` when the namespace is absent, when it is
    /// not a JSON object, or when `key` is not present within it.
    pub fn get_structured_metadata(&self, namespace: &str, key: &str) -> Option<&serde_json::Value> {
        self.structured_metadata.get(namespace)?.as_object()?.get(key)
    }

    /// Merge a complete namespace object, overwriting existing keys.
    ///
    /// Keys already present in the namespace are overwritten;
    /// keys absent from `values` are left untouched. New keys
    /// that would exceed the per-namespace limit of 64
    /// are silently dropped.
    pub fn merge_structured_metadata(&mut self, namespace: &str, values: serde_json::Map<String, serde_json::Value>) {
        if !self.structured_metadata.contains_key(namespace)
            && self.structured_metadata.len() >= MAX_STRUCTURED_METADATA_NAMESPACES
        {
            tracing::warn!(
                namespace,
                limit = MAX_STRUCTURED_METADATA_NAMESPACES,
                "structured metadata namespace limit reached; dropping new namespace"
            );
            return;
        }
        let ns = self
            .structured_metadata
            .entry(namespace.to_owned())
            .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
        if let serde_json::Value::Object(map) = ns {
            for (key, value) in values {
                if map.len() >= MAX_STRUCTURED_METADATA_KEYS && !map.contains_key(&key) {
                    tracing::warn!(
                        namespace,
                        key,
                        limit = MAX_STRUCTURED_METADATA_KEYS,
                        "structured metadata key limit reached during merge; dropping new key"
                    );
                    continue;
                }
                map.insert(key, value);
            }
        }
    }
}

// -----------------------------------------------------------------------------
// Header Resolution Utilities
// -----------------------------------------------------------------------------

/// Walk the trusted mutation log forward and collect the effective values.
///
/// Returns the surviving values plus whether any mutation *mentioned* `name`.
/// The latter distinguishes "never mentioned" from "removed": both leave the
/// value list empty (a `Remove` clears it), so the flag is the only way to tell
/// them apart for [`HttpFilterContext::resolve_trusted_header_state`].
fn collect_trusted_values<'m>(
    mutations: impl Iterator<Item = &'m TrustedHeaderMutation> + 'm,
    name: &HeaderName,
) -> Result<(Vec<String>, bool), String> {
    let mut values: Vec<String> = Vec::new();
    let mut touched = false;
    for mutation in mutations {
        match mutation {
            TrustedHeaderMutation::Remove(n) if n == name => {
                values.clear();
                touched = true;
            },
            TrustedHeaderMutation::Set(n, v) if n == name => {
                let s = v
                    .to_str()
                    .map_err(|_err| format!("trusted header '{name}' contains non-text bytes"))?;
                values.clear();
                values.push(s.to_owned());
                touched = true;
            },
            TrustedHeaderMutation::Add(n, v) if n == name => {
                values.push(v.clone());
                touched = true;
            },
            _ => {},
        }
    }
    Ok((values, touched))
}

/// Find the last matching set value for a header name.
fn find_last_set(
    headers_to_set: &[(HeaderName, http::header::HeaderValue)],
    name: &HeaderName,
) -> Result<Option<String>, String> {
    for (n, v) in headers_to_set.iter().rev() {
        if n == name {
            let s = v
                .to_str()
                .map_err(|_err| format!("pending header '{name}' contains non-text bytes"))?;
            return Ok(Some(s.to_owned()));
        }
    }
    Ok(None)
}

/// Collect all matching extra header values for a header name.
fn collect_extras(extras: &[(Cow<'_, str>, String)], name: &HeaderName) -> Vec<String> {
    let name_str = name.as_str();
    extras
        .iter()
        .filter(|(n, _)| n.eq_ignore_ascii_case(name_str))
        .map(|(_, v)| v.clone())
        .collect()
}

/// Require that all values in a list are identical, returning the unique value.
fn require_unique_value(values: Vec<String>, name: &HeaderName, source: &str) -> Result<Option<String>, String> {
    let mut iter = values.into_iter();
    let Some(first) = iter.next() else {
        return Ok(None);
    };
    for v in iter {
        if v != first {
            return Err(format!(
                "{source} header '{name}' has ambiguous values: '{first}' vs '{v}'"
            ));
        }
    }
    Ok(Some(first))
}

// -----------------------------------------------------------------------------
// Effective Headers Overlay
// -----------------------------------------------------------------------------

/// Header view for pre-read body filters: the original request overlaid with
/// trusted header mutations.
///
/// Lookups resolve in last-writer-wins order: the current pass's grouped
/// pending queues (via [`pending_header_value`]) first, then the ordered
/// trusted log (prior passes and this pass, via
/// [`resolve_trusted_header_state`]), and finally the original request. This
/// mirrors what the request phase sees after the protocol layer merges the log
/// into the request, so a body filter can be gated on a header an earlier
/// pre-read filter promoted from the body.
///
/// [`pending_header_value`]: HttpFilterContext::pending_header_value
/// [`resolve_trusted_header_state`]: HttpFilterContext::resolve_trusted_header_state
pub(crate) struct EffectiveHeaders<'c, 'r>(pub(crate) &'c HttpFilterContext<'r>);

impl HeaderSource for EffectiveHeaders<'_, '_> {
    type Error = ConditionError;

    fn header(&self, name: &HeaderName) -> Result<Option<Cow<'_, str>>, ConditionError> {
        let ctx = self.0;
        // This pass's grouped queues are the last writer this pass.
        match ctx.pending_header_value(name).map_err(|_e| ambiguous(name))? {
            PendingHeaderResult::Removed => return Ok(None),
            PendingHeaderResult::Value(v) => return Ok(Some(Cow::Owned(v))),
            PendingHeaderResult::Absent => {},
        }
        match ctx.resolve_trusted_header_state(name).map_err(|_e| ambiguous(name))? {
            TrustedHeaderState::Removed => Ok(None),
            TrustedHeaderState::Value(v) => Ok(Some(Cow::Owned(v))),
            // Fall through to the original request. The `Request` source is
            // infallible, so the error arm is unreachable.
            TrustedHeaderState::Absent => ctx.request.header(name).map_err(|e| match e {}),
        }
    }
}

/// Build an [`ConditionError::AmbiguousHeader`] for `name`.
fn ambiguous(name: &HeaderName) -> ConditionError {
    ConditionError::AmbiguousHeader { header: name.clone() }
}

// -----------------------------------------------------------------------------
// Request
// -----------------------------------------------------------------------------

/// HTTP request metadata.
///
/// ```
/// use http::{HeaderMap, Method, Uri};
/// use praxis_filter::Request;
///
/// let req = Request {
///     method: Method::GET,
///     uri: Uri::from_static("/api/users"),
///     headers: HeaderMap::new(),
/// };
/// assert_eq!(req.uri.path(), "/api/users");
/// ```
#[derive(Clone, Debug)]
pub struct Request {
    /// HTTP header map.
    pub headers: HeaderMap,

    /// HTTP method.
    pub method: Method,

    /// Request URI.
    pub uri: Uri,
}

// -----------------------------------------------------------------------------
// Response
// -----------------------------------------------------------------------------

/// HTTP response metadata.
///
/// ```
/// use http::{HeaderMap, StatusCode};
/// use praxis_filter::Response;
///
/// let mut resp = Response {
///     status: StatusCode::OK,
///     headers: HeaderMap::new(),
/// };
/// resp.headers.insert("x-custom", "value".parse().unwrap());
/// assert_eq!(resp.status, StatusCode::OK);
/// ```
#[derive(Debug)]
pub struct Response {
    /// HTTP header map.
    pub headers: HeaderMap,

    /// HTTP status code.
    pub status: StatusCode,
}

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

#[cfg(test)]
#[expect(clippy::allow_attributes, reason = "blanket test suppressions")]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::panic,
    reason = "tests"
)]
mod tests {
    use super::*;

    #[test]
    fn request_fields_are_accessible() {
        let req = Request {
            method: Method::POST,
            uri: "/submit".parse().unwrap(),
            headers: HeaderMap::new(),
        };
        assert_eq!(req.method, Method::POST);
        assert_eq!(req.uri.path(), "/submit");
        assert!(req.headers.is_empty(), "new request should have no headers");
    }

    #[test]
    fn response_header_mutation() {
        let mut resp = Response {
            status: StatusCode::OK,
            headers: HeaderMap::new(),
        };
        resp.headers.insert("x-powered-by", "praxis".parse().unwrap());
        assert_eq!(resp.headers["x-powered-by"], "praxis");
    }

    #[test]
    fn response_status_codes() {
        for code in [200_u16, 404, 500] {
            let resp = Response {
                status: StatusCode::from_u16(code).unwrap(),
                headers: HeaderMap::new(),
            };
            assert_eq!(resp.status.as_u16(), code);
        }
    }

    #[test]
    fn cluster_name_returns_none_when_unset() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(ctx.cluster_name().is_none(), "cluster name should be None when unset");
    }

    #[test]
    fn cluster_name_returns_value_when_set() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.cluster = Some(Arc::from("backend"));
        assert_eq!(
            ctx.cluster_name(),
            Some("backend"),
            "cluster name should return set value"
        );
    }

    #[test]
    fn upstream_addr_returns_none_when_unset() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(ctx.upstream_addr().is_none(), "upstream addr should be None when unset");
    }

    #[test]
    fn cap_stream_read_timeout_tightens_leftover_budget() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.cap_stream_read_timeout(Duration::from_secs(30));
        ctx.cap_stream_read_timeout(Duration::from_millis(250));
        assert_eq!(
            ctx.stream_read_timeout_cap(),
            Some(Duration::from_millis(250)),
            "leftover budget must recap the live timer, not a detached peer copy"
        );
        assert_eq!(
            ctx.take_stream_read_timeout_cap(),
            Some(Duration::from_millis(250)),
            "the streaming executor must be able to take the leftover cap"
        );
        assert!(
            ctx.stream_read_timeout_cap().is_none(),
            "taking the cap must not leave it in request extensions"
        );
    }

    #[test]
    fn cap_stream_read_timeout_keeps_a_tighter_existing_cap() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.cap_stream_read_timeout(Duration::from_millis(100));
        ctx.cap_stream_read_timeout(Duration::from_secs(1));
        assert_eq!(
            ctx.stream_read_timeout_cap(),
            Some(Duration::from_millis(100)),
            "a tighter existing leftover cap must not be relaxed"
        );
    }

    #[test]
    fn cap_stream_deadline_tightens_absolute_cutoff() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        let later = Instant::now() + Duration::from_secs(30);
        let sooner = Instant::now() + Duration::from_secs(1);
        ctx.cap_stream_deadline(later);
        ctx.cap_stream_deadline(sooner);
        assert_eq!(
            ctx.stream_deadline_cap(),
            Some(sooner),
            "leftover deadline must recap the live body, not a detached peer copy"
        );
        assert_eq!(
            ctx.take_stream_deadline_cap(),
            Some(sooner),
            "the streaming executor must be able to take the deadline cap"
        );
        assert!(
            ctx.stream_deadline_cap().is_none(),
            "taking the deadline cap must not leave it in request extensions"
        );
    }

    #[test]
    fn cap_stream_deadline_keeps_a_tighter_existing_cutoff() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        let sooner = Instant::now() + Duration::from_millis(100);
        let later = Instant::now() + Duration::from_secs(1);
        ctx.cap_stream_deadline(sooner);
        ctx.cap_stream_deadline(later);
        assert_eq!(
            ctx.stream_deadline_cap(),
            Some(sooner),
            "a tighter existing deadline must not be relaxed"
        );
    }

    #[test]
    fn upstream_addr_returns_value_when_set() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.upstream = Some(Upstream {
            address: Arc::from("10.0.0.1:8080"),
            authority: None,
            tls: None,
            connection: Arc::new(praxis_core::connectivity::ConnectionOptions::default()),
        });
        assert_eq!(
            ctx.upstream_addr(),
            Some("10.0.0.1:8080"),
            "upstream addr should return set address"
        );
    }

    #[test]
    fn request_id_returns_none_when_absent() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(
            ctx.request_id().is_none(),
            "request ID should be None when header absent"
        );
    }

    #[test]
    fn request_id_returns_value_when_present() {
        let mut req = crate::test_utils::make_request(Method::GET, "/");
        req.headers.insert("x-request-id", "abc-123".parse().unwrap());
        let ctx = crate::test_utils::make_filter_context(&req);
        assert_eq!(
            ctx.request_id(),
            Some("abc-123"),
            "request ID should return header value"
        );
    }

    #[test]
    fn request_id_prefers_trace_context_over_inbound_header() {
        let mut req = crate::test_utils::make_request(Method::GET, "/");
        req.headers.insert("x-request-id", "inbound-rid".parse().unwrap());
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.extensions.insert(crate::trace_context::TraceContext::new(
            "trace-rid".into(),
            "4bf92f3577b34da6a3ce929d0e0e4736".into(),
            "01".into(),
        ));
        assert_eq!(ctx.request_id(), Some("trace-rid"));
    }

    #[tokio::test]
    async fn execute_subrequest_returns_error_without_client() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        let peer = pingora_core::upstreams::peer::HttpPeer::new("127.0.0.1:9".to_owned(), false, String::new());
        let subrequest = praxis_core::subrequest::SubRequest {
            method: Method::GET,
            uri: "/sub".parse().unwrap(),
            headers: HeaderMap::new(),
            body: bytes::Bytes::new(),
        };

        let result = ctx
            .execute_subrequest(
                &peer,
                &subrequest,
                1024,
                Duration::from_secs(1),
                praxis_core::subrequest::FrameworkHeaders::new(),
            )
            .await;

        assert!(
            matches!(result, Err(praxis_core::subrequest::SubRequestError::InvalidRequest(message)) if message.contains("not available")),
            "missing subrequest client should return InvalidRequest"
        );
    }

    #[tokio::test]
    #[allow(
        clippy::significant_drop_tightening,
        reason = "asserting error result without polling stream"
    )]
    async fn send_streaming_subrequest_returns_error_without_client() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        let peer = pingora_core::upstreams::peer::HttpPeer::new("127.0.0.1:9".to_owned(), false, String::new());
        let subrequest = praxis_core::subrequest::SubRequest {
            method: Method::GET,
            uri: "/sub".parse().unwrap(),
            headers: HeaderMap::new(),
            body: bytes::Bytes::new(),
        };
        let limits = praxis_core::subrequest::StreamLimits {
            idle_timeout: Duration::from_secs(1),
            max_stream_duration: None,
            max_total_bytes: None,
        };

        let result = ctx
            .send_streaming_subrequest(
                &peer,
                &subrequest,
                Duration::from_secs(1),
                limits,
                praxis_core::subrequest::FrameworkHeaders::new(),
            )
            .await;

        assert!(
            matches!(result, Err(praxis_core::subrequest::SubRequestError::InvalidRequest(message)) if message.contains("not available")),
            "missing subrequest client should return InvalidRequest"
        );
    }

    async fn capture_subrequest_headers() -> String {
        capture_subrequest_headers_for(
            get_subrequest(),
            crate::trace_context::TraceContext::new(
                "req-from-context".into(),
                "4bf92f3577b34da6a3ce929d0e0e4736".into(),
                "01".into(),
            ),
        )
        .await
    }

    async fn capture_subrequest_headers_for(
        subrequest: praxis_core::subrequest::SubRequest,
        tc: crate::trace_context::TraceContext,
    ) -> String {
        use praxis_core::subrequest::{FrameworkHeaders, SubRequestClient};

        let (addr, server) = start_header_capture_server().await;
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.extensions.insert(tc);
        // The helper installs the crypto provider the connector's TLS config needs.
        let client = SubRequestClient::new(crate::test_support::connector(1, None));
        ctx.subrequest_client = Some(&client);

        let peer = pingora_core::upstreams::peer::HttpPeer::new(addr, false, "localhost".into());
        let response = ctx
            .execute_subrequest(
                &peer,
                &subrequest,
                1024,
                Duration::from_secs(5),
                FrameworkHeaders::new(),
            )
            .await
            .unwrap();
        assert_eq!(response.status, 200);
        server.await.unwrap()
    }

    async fn start_header_capture_server() -> (std::net::SocketAddr, tokio::task::JoinHandle<String>) {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let server = tokio::spawn(async move { read_observed_request(listener).await });
        (addr, server)
    }

    async fn read_observed_request(listener: tokio::net::TcpListener) -> String {
        use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};

        let (mut stream, _) = listener.accept().await.unwrap();
        let mut buf = vec![0; 4096];
        let n = stream.read(&mut buf).await.unwrap();
        let observed = String::from_utf8_lossy(&buf[..n]).to_string();
        stream
            .write_all(
                b"HTTP/1.1 200 OK
content-length: 0

",
            )
            .await
            .unwrap();
        observed
    }

    fn get_subrequest() -> praxis_core::subrequest::SubRequest {
        praxis_core::subrequest::SubRequest {
            method: Method::GET,
            uri: "/sub".parse().unwrap(),
            headers: HeaderMap::new(),
            body: bytes::Bytes::new(),
        }
    }

    #[tokio::test]
    async fn execute_subrequest_propagates_request_id() {
        let observed = capture_subrequest_headers().await;
        assert!(observed.contains("x-request-id: req-from-context"), "{observed}");
    }

    #[tokio::test]
    async fn execute_subrequest_propagates_traceparent() {
        let observed = capture_subrequest_headers().await;
        assert!(observed.contains("traceparent: 00-"), "{observed}");
        assert!(observed.contains("4bf92f3577b34da6a3ce929d0e0e4736"), "{observed}");
    }

    #[tokio::test]
    async fn execute_subrequest_strips_untrusted_tracestate() {
        let mut headers = HeaderMap::new();
        headers.insert("traceparent", "garbage".parse().unwrap());
        headers.insert("tracestate", "congo=t61rcWkgMzE".parse().unwrap());
        let observed = capture_subrequest_headers_for(
            praxis_core::subrequest::SubRequest {
                method: Method::GET,
                uri: "/sub".parse().unwrap(),
                headers,
                body: bytes::Bytes::new(),
            },
            crate::trace_context::TraceContext::new(
                "req-from-context".into(),
                "4bf92f3577b34da6a3ce929d0e0e4736".into(),
                "01".into(),
            ),
        )
        .await;
        assert!(
            observed.contains("traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-"),
            "{observed}"
        );
        assert!(
            !observed.to_ascii_lowercase().contains("tracestate:"),
            "untrusted tracestate must not leak onto the subrequest: {observed}"
        );
        assert!(!observed.contains("garbage"), "{observed}");
    }

    #[test]
    fn set_request_body_mode_upgrades_stream_to_stream_buffer() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        assert_eq!(ctx.request_body_mode, BodyMode::Stream, "should start as Stream");
        ctx.set_request_body_mode(BodyMode::StreamBuffer { max_bytes: Some(4096) });
        assert_eq!(
            ctx.request_body_mode,
            BodyMode::StreamBuffer { max_bytes: Some(4096) },
            "Stream should upgrade to StreamBuffer"
        );
    }

    #[test]
    fn set_request_body_mode_cannot_downgrade() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_request_body_mode(BodyMode::StreamBuffer { max_bytes: Some(2048) });
        ctx.set_request_body_mode(BodyMode::Stream);
        assert_eq!(
            ctx.request_body_mode,
            BodyMode::StreamBuffer { max_bytes: Some(2048) },
            "StreamBuffer should not downgrade to Stream"
        );
    }

    #[test]
    fn set_response_body_mode_upgrades_stream_to_stream_buffer() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        assert_eq!(ctx.response_body_mode, BodyMode::Stream, "should start as Stream");
        ctx.set_response_body_mode(BodyMode::StreamBuffer { max_bytes: Some(8192) });
        assert_eq!(
            ctx.response_body_mode,
            BodyMode::StreamBuffer { max_bytes: Some(8192) },
            "Stream should upgrade to StreamBuffer"
        );
    }

    #[test]
    fn set_request_body_mode_stream_buffer_then_stream_buffer_merges_limits() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_request_body_mode(BodyMode::StreamBuffer { max_bytes: Some(2048) });
        ctx.set_request_body_mode(BodyMode::StreamBuffer { max_bytes: Some(1024) });
        assert_eq!(
            ctx.request_body_mode,
            BodyMode::StreamBuffer { max_bytes: Some(2048) },
            "larger StreamBuffer limit should win when merging"
        );
    }

    #[test]
    fn get_metadata_returns_none_when_empty() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(
            ctx.get_metadata("json_rpc.method").is_none(),
            "get_metadata should return None for absent key"
        );
    }

    #[test]
    fn set_metadata_then_get_returns_value() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_metadata("json_rpc.method", "service/invoke");
        assert_eq!(
            ctx.get_metadata("json_rpc.method"),
            Some("service/invoke"),
            "get_metadata should return the set value"
        );
    }

    #[test]
    fn set_metadata_overwrites_existing() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_metadata("classifier.label", "ProcessRequest");
        ctx.set_metadata("classifier.label", "GetTask");
        assert_eq!(
            ctx.get_metadata("classifier.label"),
            Some("GetTask"),
            "set_metadata should overwrite previous value"
        );
    }

    #[test]
    fn metadata_independent_of_filter_results() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_metadata("request.session_id", "gw-123");
        ctx.filter_results.clear();
        assert_eq!(
            ctx.get_metadata("request.session_id"),
            Some("gw-123"),
            "clearing filter_results should not affect metadata"
        );
    }

    #[test]
    fn set_metadata_accepts_owned_strings() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        let key = "request.task_id".to_owned();
        let value = "task-456".to_owned();
        ctx.set_metadata(key, value);
        assert_eq!(
            ctx.get_metadata("request.task_id"),
            Some("task-456"),
            "set_metadata should accept owned Strings"
        );
    }

    #[test]
    fn kv_stores_returns_none_when_unset() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(ctx.kv_stores.is_none(), "kv_stores should be None when unset");
    }

    #[test]
    fn kv_stores_returns_registry_when_set() {
        let registry = KvStoreRegistry::new();
        let store = registry.get_or_create("routing");
        store.set("model", Arc::from("model-gamma-1"));

        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.kv_stores = Some(&registry);

        let store = ctx.kv_stores.unwrap().get("routing").unwrap();
        assert_eq!(
            store.get("model").as_deref(),
            Some("model-gamma-1"),
            "filter should read KV store via context"
        );
    }

    #[test]
    fn kv_stores_write_from_context_is_visible() {
        let registry = KvStoreRegistry::new();
        let store = registry.get_or_create("flags");

        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.kv_stores = Some(&registry);

        ctx.kv_stores
            .unwrap()
            .get("flags")
            .unwrap()
            .set("dark_mode", Arc::from("true"));
        assert_eq!(
            store.get("dark_mode").as_deref(),
            Some("true"),
            "write through context should be visible on the original store"
        );
    }

    #[test]
    fn kv_stores_missing_store_returns_none() {
        let registry = KvStoreRegistry::new();

        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.kv_stores = Some(&registry);

        assert!(
            ctx.kv_stores.unwrap().get("nonexistent").is_none(),
            "missing store name should return None"
        );
    }

    #[test]
    fn set_metadata_rejects_empty_key() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_metadata("", "val");
        assert!(ctx.get_metadata("").is_none(), "empty key should be silently rejected");
    }

    #[test]
    fn set_metadata_rejects_long_key() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        let long_key = "k".repeat(65);
        ctx.set_metadata(long_key.as_str(), "val");
        assert!(
            ctx.get_metadata(long_key.as_str()).is_none(),
            "65-byte key should be rejected"
        );
    }

    #[test]
    fn set_metadata_accepts_max_length_key() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        let max_key = "k".repeat(64);
        ctx.set_metadata(max_key.as_str(), "val");
        assert_eq!(
            ctx.get_metadata(max_key.as_str()),
            Some("val"),
            "64-byte key should be accepted"
        );
    }

    #[test]
    fn set_metadata_rejects_long_value() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        let long_value = "v".repeat(257);
        ctx.set_metadata("key", long_value.as_str());
        assert!(ctx.get_metadata("key").is_none(), "257-byte value should be rejected");
    }

    #[test]
    fn set_metadata_rejects_when_entry_limit_reached() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        for i in 0..MAX_METADATA_ENTRIES {
            ctx.set_metadata(format!("key.{i}"), "value");
        }
        assert_eq!(
            ctx.filter_metadata.len(),
            MAX_METADATA_ENTRIES,
            "should accept exactly {MAX_METADATA_ENTRIES} entries"
        );

        ctx.set_metadata("overflow", "value");
        assert!(
            ctx.get_metadata("overflow").is_none(),
            "entry beyond limit should be rejected"
        );
    }

    #[test]
    fn set_metadata_allows_overwrite_at_limit() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        for i in 0..MAX_METADATA_ENTRIES {
            ctx.set_metadata(format!("key.{i}"), "old");
        }

        ctx.set_metadata("key.0", "new");
        assert_eq!(
            ctx.get_metadata("key.0"),
            Some("new"),
            "overwriting existing key at limit should succeed"
        );
        assert_eq!(
            ctx.filter_metadata.len(),
            MAX_METADATA_ENTRIES,
            "overwrite should not increase entry count"
        );
    }

    #[test]
    fn kv_stores_lookup_with_match_types() {
        use praxis_core::kv::MatchType;

        let registry = KvStoreRegistry::new();
        let store = registry.get_or_create("routes");
        store.set("route.api.v1", Arc::from("api_cluster"));
        store.set("route.web.main", Arc::from("web_cluster"));

        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.kv_stores = Some(&registry);

        let store = ctx.kv_stores.unwrap().get("routes").unwrap();
        assert!(
            store.lookup("route.api", MatchType::Prefix).unwrap().is_some(),
            "prefix lookup should match route.api.v1"
        );
        assert!(
            store.lookup(".main", MatchType::Suffix).unwrap().is_some(),
            "suffix lookup should match route.web.main"
        );
    }

    // -------------------------------------------------------------------------
    // Filter State Tests
    // -------------------------------------------------------------------------

    #[test]
    fn insert_and_get_filter_state_returns_typed_value() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.current_filter_id = Some(0);
        ctx.insert_filter_state(42_u64);
        assert_eq!(
            ctx.get_filter_state::<u64>(),
            Some(&42_u64),
            "should return the inserted value"
        );
    }

    #[test]
    fn get_filter_state_returns_none_when_empty() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.current_filter_id = Some(0);
        assert!(
            ctx.get_filter_state::<u64>().is_none(),
            "should return None when no state stored"
        );
    }

    #[test]
    fn get_filter_state_returns_none_for_wrong_type() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.current_filter_id = Some(0);
        ctx.insert_filter_state(42_u64);
        assert!(
            ctx.get_filter_state::<String>().is_none(),
            "should return None for type mismatch"
        );
    }

    #[test]
    fn get_filter_state_returns_none_when_no_index() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.filter_state.insert(0, Box::new(42_u64));
        assert!(
            ctx.get_filter_state::<u64>().is_none(),
            "should return None when current_filter_id is None"
        );
    }

    #[test]
    fn get_filter_state_mut_allows_mutation() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.current_filter_id = Some(0);
        ctx.insert_filter_state(10_u64);
        *ctx.get_filter_state_mut::<u64>().unwrap() += 5;
        assert_eq!(
            ctx.get_filter_state::<u64>(),
            Some(&15_u64),
            "mutation through get_mut should be visible"
        );
    }

    #[test]
    fn remove_filter_state_takes_ownership() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.current_filter_id = Some(0);
        ctx.insert_filter_state("hello".to_owned());
        let removed = ctx.remove_filter_state::<String>();
        assert_eq!(removed.as_deref(), Some("hello"), "should return the stored value");
        assert!(
            ctx.get_filter_state::<String>().is_none(),
            "state should be gone after remove"
        );
    }

    #[test]
    fn remove_filter_state_returns_none_for_wrong_type() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.current_filter_id = Some(0);
        ctx.insert_filter_state(42_u64);
        assert!(
            ctx.remove_filter_state::<String>().is_none(),
            "type mismatch should return None"
        );
        assert!(
            ctx.get_filter_state::<u64>().is_some(),
            "type mismatch remove should not destroy the entry"
        );
    }

    #[test]
    fn different_indices_do_not_collide() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.current_filter_id = Some(0);
        ctx.insert_filter_state(100_u64);
        ctx.current_filter_id = Some(1);
        ctx.insert_filter_state(200_u64);

        ctx.current_filter_id = Some(0);
        assert_eq!(ctx.get_filter_state::<u64>(), Some(&100_u64), "index 0 state");

        ctx.current_filter_id = Some(1);
        assert_eq!(ctx.get_filter_state::<u64>(), Some(&200_u64), "index 1 state");
    }

    #[test]
    fn insert_filter_state_is_noop_without_index() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.insert_filter_state(42_u64);
        assert!(ctx.filter_state.is_empty(), "state map should remain empty");
    }

    // -------------------------------------------------------------------------
    // TrustedHeaderMutation Tests
    // -------------------------------------------------------------------------

    #[test]
    fn matches_header_remove() {
        let mutation = TrustedHeaderMutation::Remove("x-dest".parse().unwrap());
        assert!(mutation.matches_header(&"x-dest".parse().unwrap()));
        assert!(!mutation.matches_header(&"x-other".parse().unwrap()));
    }

    #[test]
    fn matches_header_set() {
        let mutation = TrustedHeaderMutation::Set("x-dest".parse().unwrap(), "val".parse().unwrap());
        assert!(mutation.matches_header(&"x-dest".parse().unwrap()));
        assert!(!mutation.matches_header(&"x-other".parse().unwrap()));
    }

    #[test]
    fn matches_header_add() {
        let mutation = TrustedHeaderMutation::Add("x-dest".parse().unwrap(), "val".to_owned());
        assert!(mutation.matches_header(&"x-dest".parse().unwrap()));
        assert!(!mutation.matches_header(&"x-other".parse().unwrap()));
    }

    // -------------------------------------------------------------------------
    // resolve_trusted_header Tests
    // -------------------------------------------------------------------------

    #[test]
    fn resolve_trusted_header_empty_log() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            None,
            "empty mutation log should resolve to None"
        );
    }

    #[test]
    fn resolve_trusted_header_add() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host:8080".to_owned(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("host:8080".to_owned()),
        );
    }

    #[test]
    fn resolve_trusted_header_set() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Set(
            "x-dest".parse().unwrap(),
            "host:9090".parse().unwrap(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("host:9090".to_owned()),
        );
    }

    #[test]
    fn resolve_trusted_header_remove_hides_earlier_add() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host:8080".to_owned(),
        ));
        ctx.pre_read_mutations
            .push(TrustedHeaderMutation::Remove("x-dest".parse().unwrap()));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            None,
            "remove after add should resolve to None"
        );
    }

    #[test]
    fn resolve_trusted_header_set_overrides_add() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "first:8080".to_owned(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Set(
            "x-dest".parse().unwrap(),
            "second:9090".parse().unwrap(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("second:9090".to_owned()),
            "set after add should override"
        );
    }

    #[test]
    fn resolve_trusted_header_duplicate_add_same_value_ok() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host:8080".to_owned(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host:8080".to_owned(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("host:8080".to_owned()),
            "duplicate identical adds should be allowed"
        );
    }

    #[test]
    fn resolve_trusted_header_ambiguous_add_errors() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host-a:8080".to_owned(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host-b:9090".to_owned(),
        ));
        let err = ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap_err();
        assert!(
            err.contains("ambiguous"),
            "distinct adds should produce ambiguity error: {err}"
        );
    }

    #[test]
    fn resolve_trusted_header_set_then_add_same_value_ok() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Set(
            "x-dest".parse().unwrap(),
            "host:8080".parse().unwrap(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host:8080".to_owned(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("host:8080".to_owned()),
            "set then identical add should succeed"
        );
    }

    #[test]
    fn resolve_trusted_header_set_then_distinct_add_errors() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Set(
            "x-dest".parse().unwrap(),
            "host-a:8080".parse().unwrap(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host-b:9090".to_owned(),
        ));
        let err = ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap_err();
        assert!(
            err.contains("ambiguous"),
            "set then distinct add should produce ambiguity error: {err}"
        );
    }

    #[test]
    fn resolve_trusted_header_temporary_ambiguity_resolved_by_remove() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host-a:8080".to_owned(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host-b:9090".to_owned(),
        ));
        ctx.pre_read_mutations
            .push(TrustedHeaderMutation::Remove("x-dest".parse().unwrap()));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            None,
            "Add(a) -> Add(b) -> Remove should resolve to None"
        );
    }

    #[test]
    fn resolve_trusted_header_temporary_ambiguity_resolved_by_set() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host-a:8080".to_owned(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host-b:9090".to_owned(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Set(
            "x-dest".parse().unwrap(),
            "final:7070".parse().unwrap(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("final:7070".to_owned()),
            "Add(a) -> Add(b) -> Set(c) should resolve to c"
        );
    }

    #[test]
    fn resolve_trusted_header_remove_then_set_produces_set() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "old:8080".to_owned(),
        ));
        ctx.pre_read_mutations
            .push(TrustedHeaderMutation::Remove("x-dest".parse().unwrap()));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Set(
            "x-dest".parse().unwrap(),
            "new:9090".parse().unwrap(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("new:9090".to_owned()),
            "remove then set should produce the set value"
        );
    }

    // -------------------------------------------------------------------------
    // pending_header_value Tests
    // -------------------------------------------------------------------------

    #[test]
    fn pending_header_value_empty() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert_eq!(
            ctx.pending_header_value(&"x-dest".parse().unwrap()).unwrap(),
            PendingHeaderResult::Absent,
            "no pending mutations should resolve to Absent"
        );
    }

    #[test]
    fn pending_header_value_from_set() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.request_headers_to_set
            .push(("x-dest".parse().unwrap(), "set-val:9090".parse().unwrap()));
        assert_eq!(
            ctx.pending_header_value(&"x-dest".parse().unwrap()).unwrap(),
            PendingHeaderResult::Value("set-val:9090".to_owned()),
        );
    }

    #[test]
    fn pending_header_value_from_extra() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.extra_request_headers
            .push((Cow::Borrowed("x-dest"), "extra-val:7070".to_owned()));
        assert_eq!(
            ctx.pending_header_value(&"x-dest".parse().unwrap()).unwrap(),
            PendingHeaderResult::Value("extra-val:7070".to_owned()),
        );
    }

    #[test]
    fn pending_header_value_set_after_remove_produces_set_value() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.request_headers_to_remove.push("x-dest".parse().unwrap());
        ctx.request_headers_to_set
            .push(("x-dest".parse().unwrap(), "set-val:9090".parse().unwrap()));
        assert_eq!(
            ctx.pending_header_value(&"x-dest".parse().unwrap()).unwrap(),
            PendingHeaderResult::Value("set-val:9090".to_owned()),
            "set after remove should produce the set value"
        );
    }

    #[test]
    fn pending_header_value_remove_without_set_is_removed() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.request_headers_to_remove.push("x-dest".parse().unwrap());
        assert_eq!(
            ctx.pending_header_value(&"x-dest".parse().unwrap()).unwrap(),
            PendingHeaderResult::Removed,
            "remove without subsequent set should resolve to Removed"
        );
    }

    #[test]
    fn pending_header_value_distinct_extras_error() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.extra_request_headers
            .push((Cow::Borrowed("x-dest"), "val-a:7070".to_owned()));
        ctx.extra_request_headers
            .push((Cow::Borrowed("x-dest"), "val-b:8080".to_owned()));
        let err = ctx.pending_header_value(&"x-dest".parse().unwrap()).unwrap_err();
        assert!(err.contains("ambiguous"), "distinct extras should error: {err}");
    }

    // -------------------------------------------------------------------------
    // resolve_trusted_header_state / EffectiveHeaders Tests
    // -------------------------------------------------------------------------

    /// Resolve `name` through the pre-read overlay, returning an owned value.
    fn effective_value(ctx: &HttpFilterContext<'_>, name: &str) -> Result<Option<String>, ConditionError> {
        use crate::condition::HeaderSource as _;
        let hname = HeaderName::from_bytes(name.as_bytes()).unwrap();
        EffectiveHeaders(ctx).header(&hname).map(|opt| opt.map(Cow::into_owned))
    }

    #[test]
    fn effective_headers_original_only() {
        let mut req = crate::test_utils::make_request(Method::GET, "/");
        req.headers.insert("x-gate", "on".parse().unwrap());
        let ctx = crate::test_utils::make_filter_context(&req);
        assert_eq!(
            effective_value(&ctx, "x-gate").unwrap(),
            Some("on".to_owned()),
            "with no mutations the overlay should return the original header"
        );
    }

    #[test]
    fn effective_headers_prior_add_visible() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.prior_pre_read_mutations
            .push(TrustedHeaderMutation::Add("x-gate".parse().unwrap(), "on".to_owned()));
        assert_eq!(
            effective_value(&ctx, "x-gate").unwrap(),
            Some("on".to_owned()),
            "a header promoted on a prior pass should be visible"
        );
    }

    #[test]
    fn effective_headers_this_pass_ordered_add_visible() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations
            .push(TrustedHeaderMutation::Add("x-gate".parse().unwrap(), "on".to_owned()));
        assert_eq!(
            effective_value(&ctx, "x-gate").unwrap(),
            Some("on".to_owned()),
            "a header promoted this pass via the ordered log should be visible"
        );
    }

    #[test]
    fn effective_headers_pending_set_wins_over_prior() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.prior_pre_read_mutations
            .push(TrustedHeaderMutation::Add("x-gate".parse().unwrap(), "old".to_owned()));
        ctx.request_headers_to_set
            .push(("x-gate".parse().unwrap(), "new".parse().unwrap()));
        assert_eq!(
            effective_value(&ctx, "x-gate").unwrap(),
            Some("new".to_owned()),
            "this pass's grouped queue should win over a prior-pass value"
        );
    }

    #[test]
    fn effective_headers_prior_remove_masks_present_original() {
        let mut req = crate::test_utils::make_request(Method::GET, "/");
        req.headers.insert("x-gate", "on".parse().unwrap());
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.prior_pre_read_mutations
            .push(TrustedHeaderMutation::Remove("x-gate".parse().unwrap()));
        assert_eq!(
            effective_value(&ctx, "x-gate").unwrap(),
            None,
            "a trusted Remove should mask the original header"
        );
    }

    #[test]
    fn effective_headers_ambiguous_errors() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.prior_pre_read_mutations
            .push(TrustedHeaderMutation::Add("x-gate".parse().unwrap(), "a".to_owned()));
        ctx.prior_pre_read_mutations
            .push(TrustedHeaderMutation::Add("x-gate".parse().unwrap(), "b".to_owned()));
        assert!(
            effective_value(&ctx, "x-gate").is_err(),
            "two distinct promoted values should be an error"
        );
    }

    #[test]
    fn resolve_trusted_header_state_add_then_remove_is_removed() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host:8080".to_owned(),
        ));
        ctx.pre_read_mutations
            .push(TrustedHeaderMutation::Remove("x-dest".parse().unwrap()));
        assert_eq!(
            ctx.resolve_trusted_header_state(&"x-dest".parse().unwrap()).unwrap(),
            TrustedHeaderState::Removed,
            "Add then Remove should resolve to Removed, not Absent"
        );
    }

    #[test]
    fn resolve_trusted_header_state_absent_when_never_mentioned() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert_eq!(
            ctx.resolve_trusted_header_state(&"x-dest".parse().unwrap()).unwrap(),
            TrustedHeaderState::Absent,
            "an unmentioned header should resolve to Absent"
        );
    }

    #[test]
    fn resolve_trusted_header_state_walks_prior_then_current() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.prior_pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "old:8080".to_owned(),
        ));
        ctx.pre_read_mutations.push(TrustedHeaderMutation::Set(
            "x-dest".parse().unwrap(),
            "new:9090".parse().unwrap(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header_state(&"x-dest".parse().unwrap()).unwrap(),
            TrustedHeaderState::Value("new:9090".to_owned()),
            "current-pass Set should override a prior-pass Add"
        );
    }

    #[test]
    fn resolve_trusted_header_walks_prior_then_current() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.prior_pre_read_mutations.push(TrustedHeaderMutation::Add(
            "x-dest".parse().unwrap(),
            "host:8080".to_owned(),
        ));
        assert_eq!(
            ctx.resolve_trusted_header(&"x-dest".parse().unwrap()).unwrap(),
            Some("host:8080".to_owned()),
            "resolve_trusted_header should see prior-pass mutations"
        );
    }

    // -------------------------------------------------------------------------
    // Structured Metadata Tests
    // -------------------------------------------------------------------------

    #[test]
    fn structured_metadata_absent_by_default() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(
            ctx.get_structured_metadata("ns", "key").is_none(),
            "structured_metadata should be empty by default"
        );
    }

    #[test]
    fn set_and_get_structured_metadata() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_structured_metadata("test_filter", "model", serde_json::json!("gpt-4"));
        assert_eq!(
            ctx.get_structured_metadata("test_filter", "model"),
            Some(&serde_json::json!("gpt-4")),
            "get should return the value set by set_structured_metadata"
        );
    }

    #[test]
    fn structured_metadata_namespace_count_is_bounded() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        for i in 0..MAX_STRUCTURED_METADATA_NAMESPACES {
            ctx.set_structured_metadata(&format!("ns{i}"), "k", serde_json::json!(i));
        }
        ctx.set_structured_metadata("overflow", "k", serde_json::json!(1));
        assert!(
            ctx.get_structured_metadata("overflow", "k").is_none(),
            "a namespace beyond the cap must be dropped"
        );
        ctx.set_structured_metadata("ns0", "k2", serde_json::json!(2));
        assert_eq!(
            ctx.get_structured_metadata("ns0", "k2"),
            Some(&serde_json::json!(2)),
            "existing namespaces stay writable at the cap"
        );
    }

    #[test]
    fn merge_structured_metadata_namespace_count_is_bounded() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        for i in 0..MAX_STRUCTURED_METADATA_NAMESPACES {
            ctx.set_structured_metadata(&format!("ns{i}"), "k", serde_json::json!(i));
        }
        let mut merge = serde_json::Map::new();
        merge.insert("k".to_owned(), serde_json::json!(1));
        ctx.merge_structured_metadata("overflow", merge);
        assert!(
            ctx.get_structured_metadata("overflow", "k").is_none(),
            "merge into a namespace beyond the cap must be dropped"
        );
    }

    #[test]
    fn merge_structured_metadata_overwrites_existing() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.set_structured_metadata("ns", "key", serde_json::json!("old"));
        let mut merge = serde_json::Map::new();
        merge.insert("key".to_owned(), serde_json::json!("new"));
        merge.insert("extra".to_owned(), serde_json::json!(42));
        ctx.merge_structured_metadata("ns", merge);
        assert_eq!(
            ctx.get_structured_metadata("ns", "key"),
            Some(&serde_json::json!("new")),
            "merge should overwrite existing key"
        );
        assert_eq!(
            ctx.get_structured_metadata("ns", "extra"),
            Some(&serde_json::json!(42)),
            "merge should add new key"
        );
    }

    #[test]
    fn structured_metadata_key_limit_enforced() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        for i in 0..MAX_STRUCTURED_METADATA_KEYS {
            ctx.set_structured_metadata("ns", &format!("key-{i}"), serde_json::json!(i));
        }
        assert_eq!(
            ctx.get_structured_metadata("ns", "key-0"),
            Some(&serde_json::json!(0)),
            "first key should exist"
        );

        ctx.set_structured_metadata("ns", "overflow", serde_json::json!("dropped"));
        assert!(
            ctx.get_structured_metadata("ns", "overflow").is_none(),
            "key beyond limit should be dropped"
        );

        ctx.set_structured_metadata("ns", "key-0", serde_json::json!("updated"));
        assert_eq!(
            ctx.get_structured_metadata("ns", "key-0"),
            Some(&serde_json::json!("updated")),
            "existing key can still be overwritten past limit"
        );
    }

    #[test]
    fn merge_structured_metadata_respects_key_limit() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        for i in 0..MAX_STRUCTURED_METADATA_KEYS {
            ctx.set_structured_metadata("ns", &format!("key-{i}"), serde_json::json!(i));
        }

        let mut merge = serde_json::Map::new();
        merge.insert("key-0".to_owned(), serde_json::json!("overwritten"));
        merge.insert("new-key".to_owned(), serde_json::json!("dropped"));
        ctx.merge_structured_metadata("ns", merge);

        assert_eq!(
            ctx.get_structured_metadata("ns", "key-0"),
            Some(&serde_json::json!("overwritten")),
            "merge should overwrite existing key past limit"
        );
        assert!(
            ctx.get_structured_metadata("ns", "new-key").is_none(),
            "merge should drop new key past limit"
        );
    }

    #[test]
    fn stream_chunk_emission_is_bounded() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.enable_stream_chunk_emission(5);
        ctx.emit_stream_chunk(bytes::Bytes::from_static(b"12345")).unwrap();
        let error = ctx.emit_stream_chunk(bytes::Bytes::from_static(b"6")).unwrap_err();
        assert!(
            error.to_string().contains("retained-state limit"),
            "overflow should report the retained-state limit: {error}"
        );
    }

    #[test]
    fn stream_chunk_emission_requires_irr_session() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        let error = ctx.emit_stream_chunk(bytes::Bytes::from_static(b"event")).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("only available inside iterative_request_router"),
            "out-of-session emission should be rejected: {error}"
        );
    }

    #[test]
    fn stream_termination_requires_explicit_handling() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.extensions
            .insert(StreamTermination::new(StreamTerminationCause::IdleTimeout));
        assert_eq!(
            ctx.stream_termination().map(StreamTermination::cause),
            Some(StreamTerminationCause::IdleTimeout),
            "completion filters should see the typed cause"
        );
        assert!(
            ctx.mark_stream_termination_handled(),
            "an abnormal completion should be markable as handled"
        );
        assert!(
            ctx.stream_termination().is_some_and(StreamTermination::is_handled),
            "handled state should persist for the session"
        );
    }

    // -------------------------------------------------------------------------
    // Selected Cluster Application Tests
    // -------------------------------------------------------------------------

    #[test]
    fn selected_application_absent_by_default() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(
            ctx.selected_application_protocol().is_none(),
            "protocol should be absent before any selection"
        );
        assert!(
            ctx.selected_application_provider().is_none(),
            "provider should be absent before any selection"
        );
    }

    #[test]
    fn publish_selected_application_exposes_both_fields() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_selected_application(Some(Arc::from("openai_chat_completions")), Some(Arc::from("vllm")));
        assert_eq!(
            ctx.selected_application_protocol(),
            Some("openai_chat_completions"),
            "published protocol should be readable"
        );
        assert_eq!(
            ctx.selected_application_provider(),
            Some("vllm"),
            "published provider should be readable"
        );
    }

    #[test]
    fn publish_selected_application_protocol_only() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_selected_application(Some(Arc::from("openai_responses")), None);
        assert_eq!(
            ctx.selected_application_protocol(),
            Some("openai_responses"),
            "published protocol should be readable"
        );
        assert!(
            ctx.selected_application_provider().is_none(),
            "an unpublished provider should stay absent"
        );
    }

    #[test]
    fn publish_selected_application_provider_only() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_selected_application(None, Some(Arc::from("openai")));
        assert!(
            ctx.selected_application_protocol().is_none(),
            "an unpublished protocol should stay absent"
        );
        assert_eq!(
            ctx.selected_application_provider(),
            Some("openai"),
            "published provider should be readable"
        );
    }

    #[test]
    fn publish_selected_application_is_noop_when_both_absent() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_selected_application(None, None);
        assert!(
            ctx.selected_application_protocol().is_none(),
            "publishing nothing must leave the protocol absent"
        );
        assert!(
            ctx.selected_application_provider().is_none(),
            "publishing nothing must leave the provider absent"
        );
        assert!(
            ctx.extensions.get::<SelectedClusterApplication>().is_none(),
            "an untagged cluster must not insert an extension value"
        );
    }

    #[test]
    fn publish_selected_application_untagged_clears_prior_selection() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_selected_application(Some(Arc::from("openai_chat_completions")), Some(Arc::from("vllm")));
        ctx.publish_selected_application(None, None);
        assert!(
            ctx.selected_application_protocol().is_none(),
            "a later untagged selection must clear the prior protocol so a reused context cannot leak stale metadata"
        );
        assert!(
            ctx.selected_application_provider().is_none(),
            "a later untagged selection must clear the prior provider so a reused context cannot leak stale metadata"
        );
        assert!(
            ctx.extensions.get::<SelectedClusterApplication>().is_none(),
            "an untagged re-selection must remove the extension value entirely"
        );
    }

    #[test]
    fn publish_selected_application_replaces_prior_selection() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_selected_application(Some(Arc::from("openai_chat_completions")), Some(Arc::from("vllm")));
        ctx.publish_selected_application(Some(Arc::from("anthropic_messages")), Some(Arc::from("bedrock")));
        assert_eq!(
            ctx.selected_application_protocol(),
            Some("anthropic_messages"),
            "a later tagged selection must overwrite the prior protocol"
        );
        assert_eq!(
            ctx.selected_application_provider(),
            Some("bedrock"),
            "a later tagged selection must overwrite the prior provider"
        );
    }

    // -------------------------------------------------------------------------
    // Bound Upstream Tests
    // -------------------------------------------------------------------------

    #[test]
    fn bound_upstream_absent_by_default() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let ctx = crate::test_utils::make_filter_context(&req);
        assert!(ctx.bound_cluster().is_none(), "no cluster is bound before routing");
        assert!(
            ctx.bound_application_protocol().is_none(),
            "protocol should be absent before binding"
        );
        assert!(
            ctx.bound_application_provider().is_none(),
            "provider should be absent before binding"
        );
    }

    #[cfg(feature = "upstream-binding")]
    #[test]
    fn publish_bound_upstream_exposes_cluster_and_metadata() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_bound_upstream(
            Arc::from("inference-backend"),
            Some(Arc::from("openai_responses")),
            Some(Arc::from("openai")),
        )
        .expect("publish before freeze succeeds");
        assert_eq!(
            ctx.bound_cluster(),
            Some("inference-backend"),
            "bound cluster name should be readable"
        );
        assert_eq!(
            ctx.bound_application_protocol(),
            Some("openai_responses"),
            "bound protocol should be readable"
        );
        assert_eq!(
            ctx.bound_application_provider(),
            Some("openai"),
            "bound provider should be readable"
        );
    }

    #[cfg(feature = "upstream-binding")]
    #[test]
    fn publish_bound_upstream_untagged_cluster_still_binds() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_bound_upstream(Arc::from("backend"), None, None)
            .expect("publish before freeze succeeds");
        assert_eq!(
            ctx.bound_cluster(),
            Some("backend"),
            "an untagged cluster still binds (the cluster name is always present)"
        );
        assert!(
            ctx.bound_application_protocol().is_none(),
            "an untagged binding should carry no application protocol"
        );
        assert!(
            ctx.bound_application_provider().is_none(),
            "an untagged binding should carry no application provider"
        );
    }

    #[cfg(feature = "upstream-binding")]
    #[test]
    fn publish_bound_upstream_replaces_previous_binding() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_bound_upstream(Arc::from("first"), Some(Arc::from("p1")), None)
            .expect("publish before freeze succeeds");
        ctx.publish_bound_upstream(Arc::from("second"), Some(Arc::from("p2")), Some(Arc::from("prov")))
            .expect("replacing before freeze succeeds");
        assert_eq!(
            ctx.bound_cluster(),
            Some("second"),
            "the later binding replaces the previous one before the barrier freezes it"
        );
        assert_eq!(
            ctx.bound_application_protocol(),
            Some("p2"),
            "the later binding's protocol replaces the previous one"
        );
        assert_eq!(
            ctx.bound_application_provider(),
            Some("prov"),
            "the later binding's provider replaces the previous (absent) one"
        );
    }

    #[cfg(feature = "upstream-binding")]
    #[test]
    fn frozen_binding_rejects_a_different_cluster() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_bound_upstream(Arc::from("inference"), Some(Arc::from("p1")), None)
            .expect("publish before freeze succeeds");
        ctx.freeze_bound_upstream();

        let err = ctx
            .publish_bound_upstream(Arc::from("other"), Some(Arc::from("p2")), None)
            .expect_err("a different cluster after freeze must fail closed");
        assert_eq!(&*err.frozen, "inference", "the frozen cluster is reported");
        assert_eq!(&*err.attempted, "other", "the attempted cluster is reported");
        assert_eq!(
            ctx.bound_cluster(),
            Some("inference"),
            "the frozen binding survives a rejected retarget"
        );
        assert_eq!(
            ctx.bound_application_protocol(),
            Some("p1"),
            "the frozen metadata is not overwritten"
        );
    }

    #[cfg(feature = "upstream-binding")]
    #[test]
    fn frozen_binding_allows_idempotent_republish_of_same_cluster() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.publish_bound_upstream(Arc::from("inference"), Some(Arc::from("p1")), Some(Arc::from("vllm")))
            .expect("publish before freeze succeeds");
        ctx.freeze_bound_upstream();

        ctx.publish_bound_upstream(Arc::from("inference"), None, None)
            .expect("republishing the same cluster after freeze is a no-op");
        assert_eq!(ctx.bound_cluster(), Some("inference"), "the binding is unchanged");
        assert_eq!(
            ctx.bound_application_protocol(),
            Some("p1"),
            "an idempotent republish must not clear the frozen metadata"
        );
        assert_eq!(
            ctx.bound_application_provider(),
            Some("vllm"),
            "an idempotent republish must not clear the frozen provider"
        );
    }

    #[cfg(feature = "upstream-binding")]
    #[test]
    fn freeze_before_any_binding_still_lets_the_first_publish_through() {
        let req = crate::test_utils::make_request(Method::GET, "/");
        let mut ctx = crate::test_utils::make_filter_context(&req);
        ctx.freeze_bound_upstream();

        ctx.publish_bound_upstream(Arc::from("a"), None, None)
            .expect("a frozen but unbound context publishes defensively instead of failing");

        assert_eq!(
            ctx.bound_cluster(),
            Some("a"),
            "the first binding lands even after an early freeze"
        );
        assert!(
            ctx.bound_upstream_frozen(),
            "the freeze marker survives the defensive publish"
        );
    }
}