agent-first-http 0.4.2

Persistent HTTP client for AI agents — one request, one JSON line
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
#!/usr/bin/env python3
"""
Comprehensive stress + correctness test for afhttp.
Starts a local test server, pipes JSONL into afhttp, validates output.
"""

import json
import os
import subprocess
import sys
import time
import base64
import signal
import threading
import http.server
import socketserver
import tempfile

# Add tests dir to path for server import
sys.path.insert(0, os.path.dirname(__file__))
from server import start_server

AFHTTP = os.environ.get("AFHTTP_BIN") or os.path.join(os.path.dirname(__file__), "..", "target", "debug", "afhttp")
HTTP_PORT = int(os.environ.get("AFH_TEST_HTTP_PORT", "18080"))
HOST_PORT = f"127.0.0.1:{HTTP_PORT}"
BASE = f"http://{HOST_PORT}"
COVERAGE_MODE = os.environ.get("AFH_COVERAGE_MODE") == "1"
_AFH_VERSION = os.environ.get("AFH_VERSION")

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

class TestFailure(Exception):
    pass


def parse_output_line(line: str) -> dict:
    try:
        return json.loads(line)
    except json.JSONDecodeError:
        return {"_raw": line}


def parse_output_lines(text: str) -> list[dict]:
    lines = []
    for line in text.strip().split("\n"):
        line = line.strip()
        if line:
            lines.append(parse_output_line(line))
    return lines


def temp_path(prefix: str, suffix: str) -> str:
    return os.path.join(
        tempfile.gettempdir(),
        f"{prefix}-{os.getpid()}-{time.time_ns()}{suffix}",
    )


def run_afh(inputs: list[str], timeout_s=30, extra_args: list[str] = None) -> list[dict]:
    """Send JSONL lines to afhttp stdin, collect parsed output lines."""
    payload = "\n".join(inputs) + "\n"
    cmd = [AFHTTP, "--mode", "pipe"] + (extra_args or [])
    proc = subprocess.run(
        cmd,
        input=payload,
        capture_output=True,
        text=True,
        timeout=timeout_s,
    )
    if proc.stderr.strip():
        raise TestFailure(f"afhttp wrote to stderr: {proc.stderr[:800]}")
    return parse_output_lines(proc.stdout)


def run_afh_interactive(inputs_with_delays: list, timeout_s=60) -> list[dict]:
    """
    Send inputs with delays between them.
    inputs_with_delays: list of (delay_seconds, line_string) tuples.
    """
    proc = subprocess.Popen(
        [AFHTTP, "--mode", "pipe"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    for delay, line_str in inputs_with_delays:
        if delay > 0:
            time.sleep(delay)
        try:
            proc.stdin.write((line_str + "\n").encode())
            proc.stdin.flush()
        except (BrokenPipeError, OSError):
            break

    try:
        proc.stdin.close()
    except (BrokenPipeError, OSError):
        pass

    try:
        proc.wait(timeout=timeout_s)
    except subprocess.TimeoutExpired:
        proc.kill()
        proc.wait()

    stdout = proc.stdout.read().decode()
    stderr = proc.stderr.read().decode() if proc.stderr else ""
    if stderr.strip():
        raise TestFailure(f"afhttp wrote to stderr: {stderr[:800]}")
    return parse_output_lines(stdout)


def afh_version() -> str:
    global _AFH_VERSION
    if _AFH_VERSION:
        return _AFH_VERSION
    proc = subprocess.run(
        [AFHTTP, "--version"],
        capture_output=True,
        text=True,
        timeout=5,
    )
    if proc.returncode != 0:
        raise TestFailure(f"afhttp --version failed: {proc.stderr.strip()}")
    tokens = proc.stdout.strip().split()
    if not tokens:
        raise TestFailure("afhttp --version returned empty output")
    _AFH_VERSION = tokens[-1]
    return _AFH_VERSION


def run_afh_until_terminal(request_lines: list[str], expected_terminal: int, timeout_s=60) -> list[dict]:
    """
    Keep stdin open until all request ids have reached terminal outputs, then close.
    This avoids coupling throughput tests to close() cancellation timing.
    """
    proc = subprocess.Popen(
        [AFHTTP, "--mode", "pipe"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        bufsize=1,
    )
    outputs: list[dict] = []
    lock = threading.Lock()

    def reader():
        for raw_line in proc.stdout:
            line = raw_line.strip()
            if line:
                with lock:
                    outputs.append(parse_output_line(line))

    reader_thread = threading.Thread(target=reader, daemon=True)
    reader_thread.start()

    for line_str in request_lines:
        try:
            proc.stdin.write(line_str + "\n")
            proc.stdin.flush()
        except (BrokenPipeError, OSError):
            break

    deadline = time.time() + timeout_s
    reached = False
    while time.time() < deadline:
        with lock:
            terminal = sum(
                1
                for o in outputs
                if o.get("id") and o.get("code") in ("response", "error")
            )
        if terminal >= expected_terminal:
            reached = True
            break
        if proc.poll() is not None:
            break
        time.sleep(0.01)

    try:
        proc.stdin.write(json.dumps({"code": "close"}) + "\n")
        proc.stdin.flush()
    except (BrokenPipeError, OSError):
        pass
    try:
        proc.stdin.close()
    except (BrokenPipeError, OSError):
        pass

    try:
        proc.wait(timeout=max(10, timeout_s // 2))
    except subprocess.TimeoutExpired:
        proc.kill()
        proc.wait()

    reader_thread.join(timeout=1.0)
    stderr = proc.stderr.read() if proc.stderr else ""
    with lock:
        result = list(outputs)
        terminal = sum(
            1
            for o in result
            if o.get("id") and o.get("code") in ("response", "error")
        )

    if stderr.strip():
        raise TestFailure(f"afhttp wrote to stderr: {stderr[:800]}")

    if not reached and terminal < expected_terminal:
        raise TestFailure(
            f"timed out waiting for terminal outputs: expected {expected_terminal}, got {terminal}; "
            f"stderr={stderr[:800]}"
        )

    return result


def find_by_id(outputs, code, req_id):
    """Find output line matching code and id."""
    for o in outputs:
        if o.get("code") == code and o.get("id") == req_id:
            return o
    return None


def find_all_by_id(outputs, code, req_id):
    return [o for o in outputs if o.get("code") == code and o.get("id") == req_id]


def find_by_code(outputs, code):
    return [o for o in outputs if o.get("code") == code]


def find_log_events(outputs, event):
    """Find log events by event type (code=log, event=<event>)."""
    return [o for o in outputs if o.get("code") == "log" and o.get("event") == event]


def get_header_ci(headers_dict, name):
    """Case-insensitive header lookup in a dict (server echoes headers as received)."""
    name_lower = name.lower()
    for k, v in headers_dict.items():
        if k.lower() == name_lower:
            return v
    return None


passed = 0
failed = 0
errors = []


def test(name):
    """Decorator for test functions."""
    def decorator(fn):
        def wrapper():
            global passed, failed
            try:
                fn()
                passed += 1
                print(f"  \033[32mPASS\033[0m {name}")
            except Exception as e:
                failed += 1
                errors.append((name, str(e)))
                print(f"  \033[31mFAIL\033[0m {name}: {e}")
        wrapper.__name__ = name
        return wrapper
    return decorator


# ---------------------------------------------------------------------------
# Correctness tests
# ---------------------------------------------------------------------------

@test("basic GET returns parsed JSON")
def test_basic_get():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/fast"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, f"no response for id=1, got: {[o.get('code') for o in out]}"
    assert r["status"] == 200
    assert r["body"]["ok"] is True
    assert "trace" in r
    assert r["trace"]["duration_ms"] >= 0


@test("POST with JSON body sets Content-Type automatically")
def test_post_json():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST", "url": f"{BASE}/echo",
                     "body": {"key": "value"}}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 200
    echo = r["body"]
    assert "application/json" in echo["content_type"]
    assert json.loads(echo["body"]) == {"key": "value"}


@test("POST with string body sends raw bytes (no implicit Content-Type)")
def test_post_string():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST", "url": f"{BASE}/echo",
                     "body": "hello world"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    echo = r["body"]
    # No implicit Content-Type — caller must specify if needed
    assert echo["content_type"] == "", f"expected no Content-Type for string body, got: {echo['content_type']!r}"
    assert echo["body"] == "hello world"


@test("POST with base64 body sends binary")
def test_post_base64():
    data = bytes(range(256))
    b64 = base64.b64encode(data).decode()
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST", "url": f"{BASE}/echo",
                     "body_base64": b64}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    echo = r["body"]
    assert echo["body_length"] == 256


@test("204 No Content returns no body fields")
def test_204():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/empty"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 204
    assert "body" not in r or r["body"] is None
    assert "body_base64" not in r or r["body_base64"] is None
    assert "body_file" not in r or r["body_file"] is None


@test("HEAD request returns no body")
def test_head():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "HEAD", "url": f"{BASE}/head-test"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 200
    assert "body" not in r or r["body"] is None


@test("binary response returns body_base64")
def test_binary():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/binary/100"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 200
    assert "body_base64" in r and r["body_base64"]
    decoded = base64.b64decode(r["body_base64"])
    assert len(decoded) == 100


@test("text response returns body as string")
def test_text():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/text/500"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["body"] == "A" * 500


@test("4xx/5xx returns response (not error)")
def test_4xx_5xx():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/status/404"}),
        json.dumps({"code": "request", "id": "2", "method": "GET", "url": f"{BASE}/status/500"}),
        json.dumps({"code": "close"}),
    ])
    r1 = find_by_id(out, "response", "1")
    r2 = find_by_id(out, "response", "2")
    assert r1, "no response for 404"
    assert r2, "no response for 500"
    assert r1["status"] == 404
    assert r2["status"] == 500


@test("default User-Agent header is sent")
def test_user_agent():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/headers"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    headers = r["body"]
    ua = get_header_ci(headers, "User-Agent")
    assert ua and ua.startswith("afhttp/"), f"User-Agent not found or wrong: {headers}"


@test("custom header sent, null removes default")
def test_header_merge():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/headers",
                     "headers": {"X-Custom": "test", "User-Agent": None}}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    headers = r["body"]
    assert get_header_ci(headers, "X-Custom") == "test", f"X-Custom not found: {headers}"
    assert get_header_ci(headers, "User-Agent") is None, f"User-Agent should be removed: {headers}"


@test("config update merges defaults")
def test_config_update():
    out = run_afh([
        json.dumps({"code": "config", "defaults": {"headers_for_any_hosts": {"Authorization": "Bearer test"},
                                                     "timeout_idle_s": 60}}),
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/headers"}),
        json.dumps({"code": "close"}),
    ])
    cfg = find_by_code(out, "config")
    assert cfg, "no config echo"
    assert cfg[0]["defaults"]["timeout_idle_s"] == 60
    assert cfg[0]["defaults"]["headers_for_any_hosts"]["Authorization"] == "Bearer test"
    assert cfg[0]["defaults"]["headers_for_any_hosts"]["User-Agent"] == f"afhttp/{afh_version()}"  # preserved
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    auth = get_header_ci(r["body"], "Authorization")
    assert auth == "Bearer test", f"Authorization header: {auth}"


@test("redirect chain followed and final response returned")
def test_redirects():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/redirect/3"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 200
    assert r["body"]["redirected"] is True
    assert r["trace"].get("redirects") == 3


@test("max_redirects=0 returns redirect as-is")
def test_redirect_disabled():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/redirect/3",
                     "options": {"response_redirect": 0}}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 302
    assert r["trace"].get("redirects") == 0


@test("too_many_redirects error")
def test_too_many_redirects():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/redirect/20",
                     "options": {"response_redirect": 5}}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, "no error"
    assert e["error_code"] == "too_many_redirects"


@test("duplicate in-flight id returns invalid_request")
def test_duplicate_id_rejected():
    out = run_afh([
        json.dumps({"code": "request", "id": "dup", "method": "GET",
                     "url": f"{BASE}/delay/250"}),
        json.dumps({"code": "request", "id": "dup", "method": "GET",
                     "url": f"{BASE}/delay/250"}),
        json.dumps({"code": "close"}),
    ], timeout_s=15)
    responses = find_all_by_id(out, "response", "dup")
    errors_out = find_all_by_id(out, "error", "dup")
    assert len(responses) == 1, f"expected 1 response, got {len(responses)}: {out}"
    assert len(errors_out) == 1, f"expected 1 error, got {len(errors_out)}: {out}"
    assert errors_out[0]["error_code"] == "invalid_request"


@test("request_concurrency_limit returns overloaded when exceeded")
def test_request_concurrency_limit_overloaded():
    out = run_afh_interactive([
        (0, json.dumps({"code": "config", "request_concurrency_limit": 1})),
        (0, json.dumps({"code": "request", "id": "a", "method": "GET",
                         "url": f"{BASE}/delay/500"})),
        (0.02, json.dumps({"code": "request", "id": "b", "method": "GET",
                            "url": f"{BASE}/fast"})),
        (1, json.dumps({"code": "close"})),
    ], timeout_s=15)

    req_results = [o for o in out if o.get("code") in ("response", "error") and o.get("id") in ("a", "b")]
    overloaded = [o for o in req_results if o.get("code") == "error" and o.get("error_code") == "overloaded"]
    responses = [o for o in req_results if o.get("code") == "response"]

    assert len(overloaded) == 1, f"expected 1 overloaded error, got: {req_results}"
    assert overloaded[0].get("retryable") is True, f"expected overloaded to be retryable: {overloaded[0]}"
    assert len(responses) == 1, f"expected 1 successful response, got: {req_results}"
    assert responses[0]["id"] != overloaded[0]["id"], f"same id cannot be both response and overloaded: {req_results}"


@test("duplicate id rejected while chunked stream is still active")
def test_duplicate_id_rejected_while_chunked_active():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "dup-stream", "method": "GET",
                         "url": f"{BASE}/stream/ndjson/8/120",
                         "options": {"chunked": True}})),
        (0.12, json.dumps({"code": "request", "id": "dup-stream", "method": "GET",
                            "url": f"{BASE}/fast"})),
        (1.4, json.dumps({"code": "close"})),
    ], timeout_s=20)

    dup_errors = [o for o in out if o.get("code") == "error" and o.get("id") == "dup-stream"]
    assert len(dup_errors) == 1, f"expected one duplicate-id error, got: {dup_errors}"
    assert dup_errors[0]["error_code"] == "invalid_request", \
        f"expected invalid_request for duplicate id, got: {dup_errors[0]}"
    ce = find_by_id(out, "chunk_end", "dup-stream")
    assert ce, f"expected original stream to complete, got codes: {[o.get('code') for o in out]}"


@test("request_concurrency_limit applies until chunked stream completes")
def test_request_concurrency_limit_during_chunked_stream():
    out = run_afh_interactive([
        (0, json.dumps({"code": "config", "request_concurrency_limit": 1})),
        (0, json.dumps({"code": "request", "id": "stream-a", "method": "GET",
                         "url": f"{BASE}/stream/ndjson/8/120",
                         "options": {"chunked": True}})),
        (0.12, json.dumps({"code": "request", "id": "req-b", "method": "GET",
                            "url": f"{BASE}/fast"})),
        (1.4, json.dumps({"code": "close"})),
    ], timeout_s=20)

    overloaded = find_by_id(out, "error", "req-b")
    assert overloaded, f"expected overloaded for req-b, got: {out}"
    assert overloaded["error_code"] == "overloaded", f"expected overloaded, got: {overloaded}"
    assert overloaded.get("retryable") is True, f"expected retryable=true, got: {overloaded}"
    ce = find_by_id(out, "chunk_end", "stream-a")
    assert ce, f"expected stream-a chunk_end, got codes: {[o.get('code') for o in out]}"


@test("303 redirect switches method to GET")
def test_redirect_303_switches_to_get():
    class ReuseServer(socketserver.TCPServer):
        allow_reuse_address = True

    class Redirect303Handler(http.server.BaseHTTPRequestHandler):
        def log_message(self, *args):
            pass

        def do_POST(self):
            if self.path == "/start":
                self.send_response(303)
                self.send_header("Location", "/target")
                self.send_header("Content-Length", "0")
                self.end_headers()
                return
            if self.path == "/target":
                body = json.dumps({"method": "POST"}).encode()
                self.send_response(200)
                self.send_header("Content-Type", "application/json")
                self.send_header("Content-Length", str(len(body)))
                self.end_headers()
                self.wfile.write(body)
                return
            self.send_response(404)
            self.send_header("Content-Length", "0")
            self.end_headers()

        def do_GET(self):
            if self.path == "/target":
                body = json.dumps({"method": "GET"}).encode()
                self.send_response(200)
                self.send_header("Content-Type", "application/json")
                self.send_header("Content-Length", str(len(body)))
                self.end_headers()
                self.wfile.write(body)
                return
            self.send_response(404)
            self.send_header("Content-Length", "0")
            self.end_headers()

    server = ReuseServer(("127.0.0.1", 0), Redirect303Handler)
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    port = server.server_address[1]
    try:
        out = run_afh([
            json.dumps({"code": "request", "id": "1", "method": "POST",
                         "url": f"http://127.0.0.1:{port}/start",
                         "body": {"x": 1}}),
            json.dumps({"code": "close"}),
        ])
        r = find_by_id(out, "response", "1")
        assert r, f"no response, got: {out}"
        assert r["status"] == 200
        assert r["body"]["method"] == "GET", f"expected GET after 303, got: {r['body']}"
    finally:
        server.shutdown()
        server.server_close()


@test("redirect strips Authorization across origins")
def test_redirect_strips_auth_cross_origin():
    class ReuseServer(socketserver.TCPServer):
        allow_reuse_address = True

    class SinkHandler(http.server.BaseHTTPRequestHandler):
        def log_message(self, *args):
            pass

        def do_GET(self):
            if self.path != "/sink":
                self.send_response(404)
                self.send_header("Content-Length", "0")
                self.end_headers()
                return
            body = json.dumps({"authorization": self.headers.get("Authorization")}).encode()
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)

    sink = ReuseServer(("127.0.0.1", 0), SinkHandler)
    sink_thread = threading.Thread(target=sink.serve_forever, daemon=True)
    sink_thread.start()
    sink_port = sink.server_address[1]

    class RedirectHandler(http.server.BaseHTTPRequestHandler):
        def log_message(self, *args):
            pass

        def do_GET(self):
            if self.path != "/start":
                self.send_response(404)
                self.send_header("Content-Length", "0")
                self.end_headers()
                return
            self.send_response(302)
            self.send_header("Location", f"http://127.0.0.1:{sink_port}/sink")
            self.send_header("Content-Length", "0")
            self.end_headers()

    redirect = ReuseServer(("127.0.0.1", 0), RedirectHandler)
    redirect_thread = threading.Thread(target=redirect.serve_forever, daemon=True)
    redirect_thread.start()
    redirect_port = redirect.server_address[1]

    try:
        out = run_afh([
            json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"http://127.0.0.1:{redirect_port}/start",
                         "headers": {"Authorization": "Bearer secret-token"}}),
            json.dumps({"code": "close"}),
        ])
        r = find_by_id(out, "response", "1")
        assert r, f"no response, got: {out}"
        assert r["status"] == 200
        assert r["body"]["authorization"] is None, f"Authorization leaked across origin: {r['body']}"
    finally:
        redirect.shutdown()
        redirect.server_close()
        sink.shutdown()
        sink.server_close()


@test("connection refused produces error")
def test_connection_refused():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": "http://127.0.0.1:19999/fail",
                     "options": {"retry": 0}}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, f"no error, got: {[o.get('code') for o in out]}"
    assert e["error_code"] in ("connect_refused", "connect_timeout"), f"error_code: {e['error_code']}"
    assert e["retryable"] is True


@test("invalid URL produces error")
def test_invalid_url():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": "not-a-url"}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, "no error"
    assert e["error_code"] == "invalid_request"
    assert e["retryable"] is False


@test("invalid JSON produces error with no id")
def test_invalid_json():
    out = run_afh([
        "this is not json",
        json.dumps({"code": "close"}),
    ])
    errs = find_by_code(out, "error")
    assert errs, "no error"
    assert errs[0].get("id") is None


@test("ping returns pong with stats")
def test_ping():
    out = run_afh([
        json.dumps({"code": "ping"}),
        json.dumps({"code": "close"}),
    ])
    pongs = find_by_code(out, "pong")
    assert pongs, "no pong"
    assert "trace" in pongs[0]
    assert "uptime_s" in pongs[0]["trace"]
    assert "requests_total" in pongs[0]["trace"]


@test("startup message has correct structure")
def test_startup():
    # startup is a log category — emitted when --log startup is passed at launch
    out = run_afh([json.dumps({"code": "close"})], extra_args=["--log", "startup"])
    startups = find_log_events(out, "startup")
    assert startups, f"no startup log, got: {[o.get('code') for o in out]}"
    s = startups[0]
    assert s["version"] == afh_version()
    assert isinstance(s["argv"], list)
    assert "--mode" in s["argv"] and "pipe" in s["argv"]
    assert s["config"]["response_save_above_bytes"] == 10485760
    assert s["config"]["defaults"]["response_parse_json"] is True
    # No startup without --log startup
    out2 = run_afh([json.dumps({"code": "close"})])
    assert len(find_log_events(out2, "startup")) == 0, "unexpected startup without --log"


@test("multiple Set-Cookie headers returned as array")
def test_multi_header():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/multi-header"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    cookies = r["headers"].get("set-cookie")
    assert isinstance(cookies, list), f"expected array, got {type(cookies)}: {cookies}"
    assert len(cookies) == 2


@test("unicode in response body")
def test_unicode():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/unicode"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert "你好世界" in r["body"]["text"]


@test("parse_json=false returns JSON as string")
def test_parse_json_false():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/fast",
                     "options": {"response_parse_json": False}}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert isinstance(r["body"], str), f"expected string, got {type(r['body'])}"


@test("request timeout produces error")
def test_request_timeout():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/delay/5000",
                     "options": {"timeout_idle_s": 1, "retry": 0}}),
        json.dumps({"code": "close"}),
    ], timeout_s=15)
    e = find_by_id(out, "error", "1")
    assert e, f"no error, got: {[o.get('code') for o in out]}"
    assert e["error_code"] == "request_timeout", f"error_code: {e['error_code']}"
    assert e["retryable"] is False


# ---------------------------------------------------------------------------
# Chunked / streaming tests
# ---------------------------------------------------------------------------

@test("chunked SSE stream with \\n\\n delimiter")
def test_chunked_sse():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/stream/sse/5/10",
                         "options": {"chunked": True, "chunked_delimiter": "\n\n"}})),
        (3, json.dumps({"code": "close"})),
    ])
    cs = find_by_id(out, "chunk_start", "1")
    assert cs, "no chunk_start"
    assert cs["status"] == 200
    chunks = find_all_by_id(out, "chunk_data", "1")
    assert len(chunks) == 5, f"expected 5 chunks, got {len(chunks)}"
    ce = find_by_id(out, "chunk_end", "1")
    assert ce, "no chunk_end"
    assert ce["trace"]["chunks"] == 5


@test("chunked NDJSON stream with \\n delimiter")
def test_chunked_ndjson():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/stream/ndjson/10/5",
                         "options": {"chunked": True}})),
        (3, json.dumps({"code": "close"})),
    ])
    chunks = find_all_by_id(out, "chunk_data", "1")
    assert len(chunks) == 10, f"expected 10 chunks, got {len(chunks)}"
    # Verify each chunk is valid JSON
    for c in chunks:
        parsed = json.loads(c["data"])
        assert "seq" in parsed


# ---------------------------------------------------------------------------
# Large response / auto-download tests
# ---------------------------------------------------------------------------

@test("large response auto-saved to body_file")
def test_large_auto_download():
    # Set response_save_above_bytes low so we trigger auto-download
    out = run_afh([
        json.dumps({"code": "config", "response_save_above_bytes": 1000}),
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/size/5000"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r.get("body_file"), f"expected body_file, got: {r.keys()}"
    # Verify file exists and has correct size
    assert os.path.exists(r["body_file"]), f"body_file does not exist: {r['body_file']}"
    size = os.path.getsize(r["body_file"])
    assert size == 5000, f"expected 5000 bytes, got {size}"
    # Verify sidecar JSON exists
    sidecar = r["body_file"] + ".json"
    assert os.path.exists(sidecar), "sidecar .json missing"


@test("save_to option saves to specified path")
def test_save_to():
    save_path = temp_path("afhttp-test", ".bin")
    try:
        out = run_afh([
            json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/size/2000",
                         "options": {"response_save_file": save_path}}),
            json.dumps({"code": "close"}),
        ])
        ce = find_by_id(out, "chunk_end", "1")
        assert ce, f"no chunk_end, got: {[o.get('code') for o in out]}"
        assert ce["body_file"] == save_path
        assert os.path.exists(save_path)
        assert os.path.getsize(save_path) == 2000
    finally:
        if os.path.exists(save_path):
            os.unlink(save_path)


@test("download with progress reporting")
def test_download_progress():
    save_path = temp_path("afhttp-test-progress", ".bin")
    try:
        out = run_afh_interactive([
            (0, json.dumps({"code": "config", "log": ["progress"]})),
            (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                             "url": f"{BASE}/size/10000",
                             "options": {"response_save_file": save_path, "progress_bytes": 2000}})),
            (3, json.dumps({"code": "close"})),
        ])
        # progress is now a log event: code=log, event=progress
        progress = [o for o in out if o.get("code") == "log" and o.get("event") == "progress" and o.get("id") == "1"]
        ce = find_by_id(out, "chunk_end", "1")
        assert ce, "no chunk_end"
        assert ce["trace"]["received_bytes"] == 10000
        # With progress_bytes=2000 and 10000 bytes, expect at least some progress events
        assert len(progress) >= 1, f"no progress events: {[o.get('code') for o in out]}"
    finally:
        if os.path.exists(save_path):
            os.unlink(save_path)


# ---------------------------------------------------------------------------
# Stress tests
# ---------------------------------------------------------------------------

@test("100 concurrent requests to same host")
def test_100_concurrent():
    request_lines = []
    for i in range(100):
        request_lines.append(json.dumps({
            "code": "request", "id": str(i), "method": "GET",
            "url": f"{BASE}/fast",
        }))
    out = run_afh_until_terminal(
        request_lines,
        expected_terminal=100,
        timeout_s=120 if COVERAGE_MODE else 30,
    )
    responses = find_by_code(out, "response")
    assert len(responses) == 100, f"expected 100 responses, got {len(responses)}"
    ids = {r["id"] for r in responses}
    assert len(ids) == 100, "duplicate or missing response ids"
    for r in responses:
        assert r["status"] == 200


@test("200 requests to varied endpoints")
def test_200_varied():
    request_lines = []
    endpoints = ["/fast", "/json/50", "/text/100", "/status/200", "/status/201"]
    for i in range(200):
        ep = endpoints[i % len(endpoints)]
        request_lines.append(json.dumps({
            "code": "request", "id": str(i), "method": "GET",
            "url": f"{BASE}{ep}",
        }))
    out = run_afh_until_terminal(
        request_lines,
        expected_terminal=200,
        timeout_s=120 if COVERAGE_MODE else 30,
    )
    responses = find_by_code(out, "response")
    assert len(responses) == 200, f"expected 200 responses, got {len(responses)}"


@test("100 concurrent requests with mixed delays")
def test_100_mixed_delays():
    request_lines = []
    for i in range(100):
        delay = (i % 5) * 50  # 0, 50, 100, 150, 200ms
        request_lines.append(json.dumps({
            "code": "request", "id": str(i), "method": "GET",
            "url": f"{BASE}/delay/{delay}",
        }))
    out = run_afh_until_terminal(
        request_lines,
        expected_terminal=100,
        timeout_s=120 if COVERAGE_MODE else 30,
    )
    responses = find_by_code(out, "response")
    assert len(responses) == 100, f"expected 100, got {len(responses)}"
    # Verify responses may arrive out-of-order (fast before slow)
    ids_in_order = [r["id"] for r in responses]
    assert ids_in_order != list(map(str, range(100))), \
        "all responses in order — expected some reordering with mixed delays"


@test("500 rapid-fire requests")
def test_500_rapid():
    request_lines = []
    for i in range(500):
        request_lines.append(json.dumps({
            "code": "request", "id": str(i), "method": "GET",
            "url": f"{BASE}/fast",
        }))
    out = run_afh_until_terminal(
        request_lines,
        expected_terminal=500,
        timeout_s=180 if COVERAGE_MODE else 60,
    )
    responses = find_by_code(out, "response")
    errors_out = find_by_code(out, "error")
    total = len(responses) + len([e for e in errors_out if e.get("id")])
    assert total == 500, f"expected 500 responses+errors, got {total} ({len(responses)} ok, {len(errors_out)} err)"
    assert len(responses) >= 450, f"too many failures: {len(responses)} ok out of 500"


@test("50 concurrent POST requests with JSON bodies")
def test_50_posts():
    request_lines = []
    for i in range(50):
        request_lines.append(json.dumps({
            "code": "request", "id": str(i), "method": "POST",
            "url": f"{BASE}/echo",
            "body": {"index": i, "data": "x" * 100},
        }))
    out = run_afh_until_terminal(
        request_lines,
        expected_terminal=50,
        timeout_s=120 if COVERAGE_MODE else 30,
    )
    responses = find_by_code(out, "response")
    assert len(responses) == 50, f"expected 50, got {len(responses)}"


@test("concurrent requests with different body types")
def test_mixed_body_types():
    inputs = [
        json.dumps({"code": "request", "id": "json", "method": "POST",
                     "url": f"{BASE}/echo", "body": {"type": "json"}}),
        json.dumps({"code": "request", "id": "text", "method": "POST",
                     "url": f"{BASE}/echo", "body": "plain text"}),
        json.dumps({"code": "request", "id": "b64", "method": "POST",
                     "url": f"{BASE}/echo",
                     "body_base64": base64.b64encode(b"\x00\x01\x02").decode()}),
        json.dumps({"code": "request", "id": "empty", "method": "GET",
                     "url": f"{BASE}/empty"}),
        json.dumps({"code": "request", "id": "binary", "method": "GET",
                     "url": f"{BASE}/binary/50"}),
        json.dumps({"code": "close"}),
    ]
    out = run_afh(inputs)
    for rid in ["json", "text", "b64", "empty", "binary"]:
        r = find_by_id(out, "response", rid)
        assert r, f"no response for {rid}"


# ---------------------------------------------------------------------------
# Edge case tests
# ---------------------------------------------------------------------------

@test("cancel in-flight request")
def test_cancel():
    # Use /hang endpoint (120s sleep) so request is definitely still in-flight when we cancel
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "slow", "method": "GET",
                         "url": f"{BASE}/hang",
                         "options": {"retry": 0, "timeout_idle_s": 30}})),
        (0.5, json.dumps({"code": "cancel", "id": "slow"})),
        (1, json.dumps({"code": "close"})),
    ], timeout_s=15)
    e = find_by_id(out, "error", "slow")
    assert e, f"no error for cancelled request, got: {[o.get('code') for o in out]}"
    assert e["error_code"] == "cancelled", f"error_code: {e['error_code']}"


@test("config change mid-flight doesn't break running requests")
def test_config_mid_flight():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/delay/500"})),
        (0.1, json.dumps({"code": "config", "defaults": {"timeout_idle_s": 999}})),
        (0, json.dumps({"code": "request", "id": "2", "method": "GET",
                         "url": f"{BASE}/fast"})),
        (2, json.dumps({"code": "close"})),
    ])
    r1 = find_by_id(out, "response", "1")
    r2 = find_by_id(out, "response", "2")
    assert r1, "request 1 failed"
    assert r2, "request 2 failed"
    assert r1["status"] == 200
    assert r2["status"] == 200


@test("rapid ping flood (50 pings)")
def test_ping_flood():
    inputs = [json.dumps({"code": "ping"}) for _ in range(50)]
    inputs.append(json.dumps({"code": "close"}))
    out = run_afh(inputs)
    pongs = find_by_code(out, "pong")
    assert len(pongs) == 50, f"expected 50 pongs, got {len(pongs)}"


@test("multiple config updates accumulate correctly")
def test_config_accumulate():
    out = run_afh([
        json.dumps({"code": "config", "defaults": {"headers_for_any_hosts": {"X-A": "1"}}}),
        json.dumps({"code": "config", "defaults": {"headers_for_any_hosts": {"X-B": "2"}}}),
        json.dumps({"code": "config", "defaults": {"headers_for_any_hosts": {"X-A": None}}}),
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/headers"}),
        json.dumps({"code": "close"}),
    ])
    configs = find_by_code(out, "config")
    assert len(configs) == 3
    # After 3 updates: X-A removed, X-B present, User-Agent preserved
    last_config = configs[-1]
    assert "X-A" not in last_config["defaults"]["headers_for_any_hosts"]
    assert last_config["defaults"]["headers_for_any_hosts"]["X-B"] == "2"
    assert "User-Agent" in last_config["defaults"]["headers_for_any_hosts"]
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert get_header_ci(r["body"], "X-A") is None, "X-A should be removed"
    assert get_header_ci(r["body"], "X-B") == "2", f"X-B: {get_header_ci(r['body'], 'X-B')}"


@test("empty lines in stdin are ignored")
def test_empty_lines():
    out = run_afh([
        "",
        "   ",
        json.dumps({"code": "ping"}),
        "",
        json.dumps({"code": "close"}),
    ])
    pongs = find_by_code(out, "pong")
    assert len(pongs) == 1
    errs = find_by_code(out, "error")
    assert len(errs) == 0, f"unexpected errors: {errs}"


@test("retry on connection refused (with retry enabled)")
def test_retry_connect_refused():
    out = run_afh([
        json.dumps({"code": "config", "log": ["retry"]}),
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": "http://127.0.0.1:19999/fail",
                     "options": {"retry": 2, "timeout_idle_s": 10}}),
        json.dumps({"code": "close"}),
    ], timeout_s=20)
    e = find_by_id(out, "error", "1")
    assert e, "no error"
    assert e["error_code"] in ("connect_refused", "connect_timeout"), f"error_code: {e['error_code']}"
    assert e["retryable"] is True
    # Should have retry log events
    logs = find_by_code(out, "log")
    retry_logs = [l for l in logs if l.get("event") == "retry"]
    assert len(retry_logs) >= 1, f"expected retry logs, got {len(retry_logs)}"


@test("large JSON body (1MB) round-trips correctly")
def test_large_json_body():
    big_body = {"data": "x" * (1024 * 1024)}
    expected_len = len(json.dumps(big_body))
    out = run_afh([
        # Ensure the response (which echoes body length) isn't auto-downloaded
        json.dumps({"code": "config", "response_save_above_bytes": 20_000_000}),
        json.dumps({"code": "request", "id": "1", "method": "POST",
                     "url": f"{BASE}/echo",
                     "body": big_body}),
        json.dumps({"code": "close"}),
    ], timeout_s=30)
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    # serde_json and Python json.dumps may differ by a byte (trailing space, etc.)
    actual = r["body"]["body_length"]
    assert abs(actual - expected_len) <= 2, \
        f"expected ~{expected_len}, got {actual} (diff={actual - expected_len})"


@test("redirect with log enabled")
def test_redirect_logging():
    out = run_afh([
        json.dumps({"code": "config", "log": ["redirect"]}),
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/redirect/3"}),
        json.dumps({"code": "close"}),
    ])
    logs = find_by_code(out, "log")
    redirect_logs = [l for l in logs if l.get("event") == "redirect"]
    assert len(redirect_logs) == 3, f"expected 3 redirect logs, got {len(redirect_logs)}"
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 200


@test("graceful shutdown on stdin EOF")
def test_eof_shutdown():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET", "url": f"{BASE}/fast"}),
        # No close — rely on EOF
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    closes = find_by_code(out, "close")
    assert closes, "no close message on EOF"
    assert closes[0]["message"] == "shutdown"


# ---------------------------------------------------------------------------
# NEW: Additional coverage
# ---------------------------------------------------------------------------

@test("multipart upload sends correct content-type")
def test_multipart():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "POST",
                         "url": f"{BASE}/echo-multipart",
                         "body_multipart": [
                             {"name": "field1", "value": "hello"},
                             {"name": "field2", "value": "world"},
                         ]})),
        (3, json.dumps({"code": "close"})),
    ])
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    assert r["status"] == 200
    assert r["body"]["has_multipart"] is True


@test("multipart with file upload")
def test_multipart_file():
    tmp_path = temp_path("afhttp-test-upload", ".txt")
    try:
        with open(tmp_path, "w") as f:
            f.write("file content here")
        out = run_afh_interactive([
            (0, json.dumps({"code": "request", "id": "1", "method": "POST",
                             "url": f"{BASE}/echo-multipart",
                             "body_multipart": [
                                 {"name": "purpose", "value": "test"},
                                 {"name": "file", "file": tmp_path,
                                  "filename": "test.txt", "content_type": "text/plain"},
                             ]})),
            (3, json.dumps({"code": "close"})),
        ])
        r = find_by_id(out, "response", "1")
        assert r, f"no response, got: {[o.get('code') for o in out]}"
        assert r["status"] == 200
        assert r["body"]["has_multipart"] is True
        assert r["body"]["body_length"] > 0
    finally:
        if os.path.exists(tmp_path):
            os.unlink(tmp_path)


@test("multipart with base64 binary part")
def test_multipart_base64():
    data = base64.b64encode(bytes(range(128))).decode()
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "POST",
                         "url": f"{BASE}/echo-multipart",
                         "body_multipart": [
                             {"name": "data", "value_base64": data,
                              "filename": "data.bin", "content_type": "application/octet-stream"},
                         ]})),
        (3, json.dumps({"code": "close"})),
    ])
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    assert r["body"]["has_multipart"] is True


@test("body_urlencoded sends correct content-type and encoded fields")
def test_body_urlencoded():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST",
                    "url": f"{BASE}/echo-urlencoded",
                    "body_urlencoded": [
                        {"name": "grant_type", "value": "authorization_code"},
                        {"name": "code", "value": "abc123"},
                    ]}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    assert r["status"] == 200
    ct = r["body"]["content_type"]
    assert "application/x-www-form-urlencoded" in ct, f"unexpected Content-Type: {ct}"
    fields = r["body"]["fields"]
    assert fields[0] == {"name": "grant_type", "value": "authorization_code"}
    assert fields[1] == {"name": "code", "value": "abc123"}


@test("body_urlencoded percent-encodes special characters")
def test_body_urlencoded_special_chars():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST",
                    "url": f"{BASE}/echo-urlencoded",
                    "body_urlencoded": [
                        {"name": "redirect_uri", "value": "https://app.example.com/cb?x=1&y=2"},
                        {"name": "note", "value": "hello world"},
                    ]}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    fields = r["body"]["fields"]
    assert fields[0] == {"name": "redirect_uri", "value": "https://app.example.com/cb?x=1&y=2"}
    assert fields[1] == {"name": "note", "value": "hello world"}


@test("body_urlencoded supports duplicate keys")
def test_body_urlencoded_duplicate_keys():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST",
                    "url": f"{BASE}/echo-urlencoded",
                    "body_urlencoded": [
                        {"name": "tag", "value": "rust"},
                        {"name": "tag", "value": "async"},
                        {"name": "tag", "value": "web"},
                    ]}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    fields = r["body"]["fields"]
    assert len(fields) == 3
    assert all(f["name"] == "tag" for f in fields)
    assert [f["value"] for f in fields] == ["rust", "async", "web"]


@test("body_urlencoded and body are mutually exclusive")
def test_body_urlencoded_mutual_exclusion():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST",
                    "url": f"{BASE}/echo",
                    "body": {"key": "value"},
                    "body_urlencoded": [{"name": "k", "value": "v"}]}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, f"expected error, got: {[o.get('code') for o in out]}"
    assert e["error_code"] == "invalid_request"


@test("body_file sends file contents as request body")
def test_body_file():
    tmp_path = temp_path("afhttp-test-body", ".json")
    try:
        payload = json.dumps({"from_file": True})
        with open(tmp_path, "w") as f:
            f.write(payload)
        out = run_afh([
            json.dumps({"code": "request", "id": "1", "method": "POST",
                         "url": f"{BASE}/echo",
                         "body_file": tmp_path,
                         "headers": {"Content-Type": "application/json"}}),
            json.dumps({"code": "close"}),
        ])
        r = find_by_id(out, "response", "1")
        assert r, "no response"
        assert r["body"]["body_length"] == len(payload)
        assert json.loads(r["body"]["body"]) == {"from_file": True}
    finally:
        if os.path.exists(tmp_path):
            os.unlink(tmp_path)


@test("cancel non-existent request returns error")
def test_cancel_nonexistent():
    out = run_afh([
        json.dumps({"code": "cancel", "id": "does-not-exist"}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "does-not-exist")
    assert e, "no error"
    assert e["error_code"] == "invalid_request", f"error_code: {e['error_code']}"


@test("server sends truncated body (Content-Length mismatch)")
def test_server_disconnect():
    # Server claims 1000 bytes but only sends 100, then closes cleanly.
    # reqwest blocks waiting for Content-Length bytes, so set short timeout.
    # Either the body read timeout fires, or close cancels the in-flight request.
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/disconnect/100",
                         "options": {"retry": 0, "timeout_idle_s": 2}})),
        (5, json.dumps({"code": "close"})),
    ], timeout_s=10)
    e = find_by_id(out, "error", "1")
    r = find_by_id(out, "response", "1")
    # Either error (detected truncation / timeout) or response (if reqwest accepted partial)
    assert e or r, f"no response or error, got: {[o.get('code') for o in out]}"


@test("chunked stream delivers partial data before server closes")
def test_chunked_disconnect():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/stream/disconnect/3/100",
                         "options": {"chunked": True, "retry": 0}})),
        (3, json.dumps({"code": "close"})),
    ], timeout_s=10)
    # Should get chunk_start + some chunk_data
    cs = find_by_id(out, "chunk_start", "1")
    chunks = find_all_by_id(out, "chunk_data", "1")
    assert cs, "no chunk_start"
    assert len(chunks) >= 1, f"expected chunks, got {len(chunks)}"
    # Stream may end with error (disconnect) or chunk_end (clean close detected)
    # Either is acceptable behavior for a truncated stream
    e = find_by_id(out, "error", "1")
    ce = find_by_id(out, "chunk_end", "1")
    has_termination = e is not None or ce is not None
    # If neither, the stream was interrupted by shutdown — also acceptable
    # The key assertion is that we got partial data without crashing


@test("binary chunked with null delimiter uses data_base64")
def test_binary_chunked():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/binary/500",
                         "options": {"chunked": True, "chunked_delimiter": None}})),
        (2, json.dumps({"code": "close"})),
    ])
    cs = find_by_id(out, "chunk_start", "1")
    assert cs, "no chunk_start"
    chunks = find_all_by_id(out, "chunk_data", "1")
    assert len(chunks) >= 1, "no chunk_data"
    # All chunks should use data_base64, not data
    for c in chunks:
        assert c.get("data_base64"), f"expected data_base64 in raw mode, got: {c}"
        assert c.get("data") is None
    # Verify total decoded size
    total = sum(len(base64.b64decode(c["data_base64"])) for c in chunks)
    assert total == 500, f"expected 500 bytes total, got {total}"


@test("concurrent chunked streams routed correctly by id")
def test_concurrent_chunked():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "stream-a", "method": "GET",
                         "url": f"{BASE}/stream/ndjson/5/20",
                         "options": {"chunked": True}})),
        (0, json.dumps({"code": "request", "id": "stream-b", "method": "GET",
                         "url": f"{BASE}/stream/ndjson/5/20",
                         "options": {"chunked": True}})),
        (3, json.dumps({"code": "close"})),
    ])
    chunks_a = find_all_by_id(out, "chunk_data", "stream-a")
    chunks_b = find_all_by_id(out, "chunk_data", "stream-b")
    assert len(chunks_a) == 5, f"stream-a: expected 5 chunks, got {len(chunks_a)}"
    assert len(chunks_b) == 5, f"stream-b: expected 5 chunks, got {len(chunks_b)}"
    ce_a = find_by_id(out, "chunk_end", "stream-a")
    ce_b = find_by_id(out, "chunk_end", "stream-b")
    assert ce_a, "no chunk_end for stream-a"
    assert ce_b, "no chunk_end for stream-b"


@test("redirect boundary: exactly at max_redirects succeeds")
def test_redirect_exact_boundary():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/redirect/5",
                     "options": {"response_redirect": 5}}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    assert r["status"] == 200
    assert r["body"]["redirected"] is True


@test("redirect boundary: max_redirects+1 fails")
def test_redirect_over_boundary():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/redirect/6",
                     "options": {"response_redirect": 5}}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, "no error"
    assert e["error_code"] == "too_many_redirects"


@test("429 response returned as response (not retried)")
def test_429_not_retried():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/rate-limit/5"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["status"] == 429
    assert r["body"]["error"] == "rate_limited"
    # Verify Retry-After header is passed through
    assert r["headers"].get("retry-after") == "5"


@test("response with 50 headers all preserved")
def test_many_response_headers():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/huge-headers"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    # Check some of the 50 custom headers exist
    assert r["headers"].get("x-header-0") is not None, "missing x-header-0"
    assert r["headers"].get("x-header-49") is not None, "missing x-header-49"


@test("100 concurrent requests with connection refused (error flood)")
def test_100_errors():
    inputs = []
    for i in range(100):
        inputs.append(json.dumps({
            "code": "request", "id": str(i), "method": "GET",
            "url": "http://127.0.0.1:19999/fail",
            "options": {"retry": 0},
        }))
    inputs.append(json.dumps({"code": "close"}))
    out = run_afh(inputs, timeout_s=30)
    errs = [o for o in out if o.get("code") == "error" and o.get("id")]
    assert len(errs) == 100, f"expected 100 errors, got {len(errs)}"
    ids = {e["id"] for e in errs}
    assert len(ids) == 100, "missing error ids"


@test("concurrent downloads to different files")
def test_concurrent_downloads():
    paths = [temp_path(f"afhttp-dl-{i}", ".bin") for i in range(5)]
    try:
        inputs = []
        for i, p in enumerate(paths):
            inputs.append(json.dumps({
                "code": "request", "id": str(i), "method": "GET",
                "url": f"{BASE}/size/{1000 * (i + 1)}",
                "options": {"response_save_file": p},
            }))
        inputs.append(json.dumps({"code": "close"}))
        out = run_afh(inputs, timeout_s=15)
        for i, p in enumerate(paths):
            ce = find_by_id(out, "chunk_end", str(i))
            assert ce, f"no chunk_end for {i}"
            assert os.path.exists(p), f"file not created: {p}"
            expected = 1000 * (i + 1)
            actual = os.path.getsize(p)
            assert actual == expected, f"file {i}: expected {expected}, got {actual}"
    finally:
        for p in paths:
            if os.path.exists(p):
                os.unlink(p)


@test("slow body delivery doesn't break buffered response")
def test_slow_body():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/slow-body/5000/500/50",
                     "options": {"timeout_idle_s": 10}}),
        json.dumps({"code": "close"}),
    ], timeout_s=15)
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    assert r["trace"]["received_bytes"] == 5000


@test("request with unknown code produces error")
def test_unknown_code():
    out = run_afh([
        json.dumps({"code": "foobar"}),
        json.dumps({"code": "close"}),
    ])
    errs = find_by_code(out, "error")
    assert errs, "no error for unknown code"


@test("request missing required fields produces error")
def test_missing_fields():
    out = run_afh([
        json.dumps({"code": "request", "id": "1"}),  # missing method and url
        json.dumps({"code": "close"}),
    ])
    errs = find_by_code(out, "error")
    assert errs, "no error for missing fields"


@test("empty POST body")
def test_empty_post():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "POST",
                     "url": f"{BASE}/echo"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r["body"]["body_length"] == 0


@test("request with many custom headers")
def test_many_request_headers():
    headers = {f"X-H-{i}": f"v{i}" for i in range(50)}
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/headers", "headers": headers}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    for i in range(50):
        v = get_header_ci(r["body"], f"X-H-{i}")
        assert v == f"v{i}", f"header X-H-{i}: expected v{i}, got {v}"


@test("error has structured Agent-First Data format")
def test_error_structured():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": "http://127.0.0.1:19999/fail",
                     "options": {"retry": 0}}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, "no error"
    # Verify all Agent-First Data error fields exist
    assert "error_code" in e, f"missing error_code: {e.keys()}"
    assert "error" in e, f"missing error: {e.keys()}"
    assert "retryable" in e, f"missing retryable: {e.keys()}"
    assert "trace" in e, f"missing trace: {e.keys()}"
    assert isinstance(e["error_code"], str)
    assert isinstance(e["error"], str)
    assert isinstance(e["retryable"], bool)
    assert "message" not in e, "old message field still present"


@test("tag echoed in response")
def test_tag_response():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "tag": "batch-42",
                     "method": "GET", "url": f"{BASE}/fast"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert r.get("tag") == "batch-42", f"tag: {r.get('tag')}"


@test("tag echoed in error")
def test_tag_error():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "tag": "err-tag",
                     "method": "GET", "url": "http://127.0.0.1:19999/fail",
                     "options": {"retry": 0}}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, "no error"
    assert e.get("tag") == "err-tag", f"tag: {e.get('tag')}"


@test("tag echoed in chunk_start and chunk_end")
def test_tag_chunked():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "tag": "stream-tag",
                         "method": "GET", "url": f"{BASE}/stream/ndjson/3/5",
                         "options": {"chunked": True}})),
        (2, json.dumps({"code": "close"})),
    ])
    cs = find_by_id(out, "chunk_start", "1")
    assert cs, "no chunk_start"
    assert cs.get("tag") == "stream-tag", f"chunk_start tag: {cs.get('tag')}"
    ce = find_by_id(out, "chunk_end", "1")
    assert ce, "no chunk_end"
    assert ce.get("tag") == "stream-tag", f"chunk_end tag: {ce.get('tag')}"


@test("tag absent when not provided")
def test_tag_absent():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/fast"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert "tag" not in r, f"tag should be absent: {r.get('tag')}"


@test("host_defaults headers merged for matching host")
def test_host_defaults():
    out = run_afh([
        json.dumps({"code": "config", "host_defaults": {
            HOST_PORT: {"headers": {"Authorization": "Bearer host-token"}}
        }}),
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/headers"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    auth = get_header_ci(r["body"], "Authorization")
    assert auth == "Bearer host-token", f"Authorization: {auth}"
    # config echo should show host_defaults
    configs = find_by_code(out, "config")
    assert configs, "no config"
    assert HOST_PORT in configs[0].get("host_defaults", {})


@test("host_defaults don't leak to other hosts")
def test_host_defaults_no_leak():
    out = run_afh([
        json.dumps({"code": "config", "host_defaults": {
            "other-host.example.com": {"headers": {"X-Secret": "leaked"}}
        }}),
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/headers"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    secret = get_header_ci(r["body"], "X-Secret")
    assert secret is None, f"X-Secret leaked to wrong host: {secret}"


@test("retry_on_status retries 429 and succeeds")
def test_retry_on_status():
    # Use unique key for this test to avoid counter collisions
    key = f"test-{os.getpid()}-{time.time()}"
    out = run_afh([
        json.dumps({"code": "config", "log": ["retry"]}),
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/retry-succeed/{key}/2",
                     "options": {"retry_on_status": [429], "retry": 3,
                                 "timeout_idle_s": 10}}),
        json.dumps({"code": "close"}),
    ], timeout_s=20)
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    assert r["status"] == 200, f"expected 200 after retries, got {r['status']}"
    assert r["body"]["attempts"] == 3  # 2 failures + 1 success
    # Should have retry log events
    logs = find_by_code(out, "log")
    retry_logs = [l for l in logs if l.get("event") == "retry"]
    assert len(retry_logs) >= 1, f"expected retry logs, got {len(retry_logs)}"


@test("retry_on_status exhausted returns final response")
def test_retry_on_status_exhausted():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/rate-limit/0",
                     "options": {"retry_on_status": [429], "retry": 1,
                                 "timeout_idle_s": 10}}),
        json.dumps({"code": "close"}),
    ], timeout_s=15)
    r = find_by_id(out, "response", "1")
    assert r, f"no response, got: {[o.get('code') for o in out]}"
    assert r["status"] == 429, f"expected 429 after exhausted retries, got {r['status']}"


@test("max_response_bytes truncates large buffered response")
def test_max_response_bytes():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/size/10000",
                     "options": {"response_max_bytes": 5000, "retry": 0}}),
        json.dumps({"code": "close"}),
    ])
    e = find_by_id(out, "error", "1")
    assert e, f"no error, got: {[o.get('code') for o in out]}"
    assert e["error_code"] == "response_too_large", f"error_code: {e['error_code']}"
    assert e["retryable"] is False


@test("max_response_bytes on chunked stream")
def test_max_response_bytes_chunked():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/stream/ndjson/100/5",
                         "options": {"chunked": True, "response_max_bytes": 100,
                                     "retry": 0}})),
        (3, json.dumps({"code": "close"})),
    ])
    e = find_by_id(out, "error", "1")
    assert e, f"no error, got: {[o.get('code') for o in out]}"
    assert e["error_code"] == "response_too_large", f"error_code: {e['error_code']}"


@test("content_length_bytes in chunk_start")
def test_content_length_bytes():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/stream/ndjson/5/5",
                         "options": {"chunked": True}})),
        (2, json.dumps({"code": "close"})),
    ])
    cs = find_by_id(out, "chunk_start", "1")
    assert cs, "no chunk_start"
    # Server sends Content-Length, so content_length_bytes should be present
    assert "content_length_bytes" in cs, f"missing content_length_bytes: {cs.keys()}"
    assert cs["content_length_bytes"] > 0, f"content_length_bytes: {cs['content_length_bytes']}"


@test("content_length_bytes in download chunk_start")
def test_content_length_bytes_download():
    save_path = temp_path("afhttp-test-cl", ".bin")
    try:
        out = run_afh([
            json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/size/5000",
                         "options": {"response_save_file": save_path}}),
            json.dumps({"code": "close"}),
        ])
        cs = find_by_id(out, "chunk_start", "1")
        assert cs, "no chunk_start"
        assert cs["content_length_bytes"] == 5000, f"content_length_bytes: {cs.get('content_length_bytes')}"
    finally:
        if os.path.exists(save_path):
            os.unlink(save_path)


@test("1000 rapid-fire requests (throughput)")
def test_1000_rapid():
    request_lines = []
    for i in range(1000):
        request_lines.append(json.dumps({
            "code": "request", "id": str(i), "method": "GET",
            "url": f"{BASE}/fast",
        }))
    t0 = time.time()
    out = run_afh_until_terminal(
        request_lines,
        expected_terminal=1000,
        timeout_s=240 if COVERAGE_MODE else 120,
    )
    elapsed = time.time() - t0
    responses = find_by_code(out, "response")
    errs = [o for o in find_by_code(out, "error") if o.get("id")]
    total = len(responses) + len(errs)
    assert total == 1000, f"expected 1000, got {total} ({len(responses)} ok, {len(errs)} err)"
    assert len(responses) >= 950, f"too many failures: {len(responses)}/1000"
    # Print throughput info
    print(f" [{len(responses)}/1000 ok in {elapsed:.1f}s = {1000/elapsed:.0f} req/s]", end="")


@test("response_save_resume without response_save_file returns error")
def test_response_save_resume_no_file():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/fast",
                     "options": {"response_save_resume": True}}),
        json.dumps({"code": "close"}),
    ])
    err = find_by_id(out, "error", "1")
    assert err, f"expected error, got: {[o.get('code') for o in out]}"
    assert "response_save_resume requires response_save_file" in err["error"]


@test("response_save_resume first download (file absent) fetches full file, no Range")
def test_response_save_resume_new_file():
    import tempfile, os
    tmp = tempfile.mktemp(suffix=".bin")
    try:
        out = run_afh([
            json.dumps({"code": "config", "log": ["request"]}),
            json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/range-file/100",
                         "options": {"response_save_file": tmp, "response_save_resume": True}}),
            json.dumps({"code": "close"}),
        ])
        req_logs = find_log_events(out, "request")
        if req_logs:
            ih = req_logs[0].get("implicit_headers", {})
            assert "Range" not in ih, f"unexpected Range on first download: {ih}"
        with open(tmp, "rb") as f:
            data = f.read()
        assert len(data) == 100, f"expected 100 bytes, got {len(data)}"
    finally:
        if os.path.exists(tmp):
            os.unlink(tmp)


@test("response_save_resume sends Range header and appends to file")
def test_response_save_resume():
    import tempfile, os
    with tempfile.NamedTemporaryFile(delete=False, suffix=".bin") as f:
        f.write(b"X" * 50)
        tmp = f.name
    try:
        out = run_afh([
            json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/range-file/100",
                         "options": {"response_save_file": tmp, "response_save_resume": True}}),
            json.dumps({"code": "close"}),
        ])
        end = find_by_id(out, "chunk_end", "1")
        assert end, f"no chunk_end: {[o.get('code') for o in out]}"
        with open(tmp, "rb") as f:
            data = f.read()
        assert len(data) == 100, f"expected 100 bytes after resume, got {len(data)}"
        assert data[:50] == b"X" * 50
        assert data[50:] == b"X" * 50
    finally:
        os.unlink(tmp)


@test("response_save_resume on empty file fetches full file (no Range)")
def test_response_save_resume_empty_file():
    import tempfile, os
    with tempfile.NamedTemporaryFile(delete=False, suffix=".bin") as f:
        tmp = f.name  # empty
    try:
        out = run_afh([
            json.dumps({"code": "config", "log": ["request"]}),
            json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/range-file/100",
                         "options": {"response_save_file": tmp, "response_save_resume": True}}),
            json.dumps({"code": "close"}),
        ])
        req_logs = find_log_events(out, "request")
        if req_logs:
            ih = req_logs[0].get("implicit_headers", {})
            assert "Range" not in ih, f"unexpected Range on empty file: {ih}"
        with open(tmp, "rb") as f:
            data = f.read()
        assert len(data) == 100, f"expected 100 bytes, got {len(data)}"
    finally:
        os.unlink(tmp)


@test("invalid UTF-8 text response returns body_base64 (not corrupted string)")
def test_invalid_utf8_text():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/invalid-utf8/text"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert "body" not in r or r["body"] is None, \
        f"expected no body string for invalid UTF-8, got: {r.get('body')!r}"
    assert r.get("body_base64"), f"expected body_base64 for invalid UTF-8, keys: {list(r.keys())}"
    decoded = base64.b64decode(r["body_base64"])
    assert decoded == b"caf\xe9 r\xe9sum\xe9", f"decoded bytes mismatch: {decoded!r}"


@test("invalid UTF-8 JSON response returns body_base64 (not corrupted string)")
def test_invalid_utf8_json():
    out = run_afh([
        json.dumps({"code": "request", "id": "1", "method": "GET",
                     "url": f"{BASE}/invalid-utf8/json"}),
        json.dumps({"code": "close"}),
    ])
    r = find_by_id(out, "response", "1")
    assert r, "no response"
    assert "body" not in r or r["body"] is None, \
        f"expected no body for invalid UTF-8 JSON, got: {r.get('body')!r}"
    assert r.get("body_base64"), f"expected body_base64, keys: {list(r.keys())}"
    decoded = base64.b64decode(r["body_base64"])
    assert decoded.startswith(b"\xff\xfe"), f"original bytes not preserved: {decoded[:4]!r}"


@test("chunked delimiter mode with invalid UTF-8 returns data_base64")
def test_chunked_invalid_utf8():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/stream/invalid-utf8",
                         "options": {"chunked": True, "chunked_delimiter": "\n"}})),
        (2, json.dumps({"code": "close"})),
    ])
    chunks = find_all_by_id(out, "chunk_data", "1")
    assert len(chunks) >= 1, f"no chunk_data, codes: {[o.get('code') for o in out]}"
    for c in chunks:
        assert c.get("data_base64") is not None, \
            f"expected data_base64 for invalid UTF-8 chunk, got data: {c.get('data')!r}"
        assert c.get("data") is None, \
            f"expected no data string for invalid UTF-8 chunk, got: {c.get('data')!r}"
        decoded = base64.b64decode(c["data_base64"])
        assert any(b in decoded for b in [b"\xff", b"\xfe", b"\xfd", b"\xe9"]), \
            f"decoded bytes don't contain expected invalid UTF-8: {decoded!r}"


@test("close reports final request count")
def test_close_count():
    out = run_afh_interactive([
        (0, json.dumps({"code": "request", "id": "1", "method": "GET",
                         "url": f"{BASE}/fast"})),
        (0, json.dumps({"code": "request", "id": "2", "method": "GET",
                         "url": f"{BASE}/fast"})),
        (0, json.dumps({"code": "request", "id": "3", "method": "GET",
                         "url": f"{BASE}/fast"})),
        (2, json.dumps({"code": "close"})),
    ])
    closes = find_by_code(out, "close")
    assert closes, "no close"
    assert closes[0]["trace"]["requests_total"] == 3


# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------

def main():
    global passed, failed

    # Start test server
    print(f"Starting test server on :{HTTP_PORT}...")
    server = start_server(HTTP_PORT)
    time.sleep(0.3)

    # Verify server is running
    import urllib.request
    try:
        urllib.request.urlopen(f"{BASE}/fast", timeout=2)
    except Exception as e:
        print(f"FATAL: test server not responding: {e}")
        sys.exit(1)
    print("Test server ready.\n")

    # Verify afhttp binary exists
    if not os.path.exists(AFHTTP):
        print(f"FATAL: afhttp binary not found at {AFHTTP}")
        print("Run: cargo build")
        sys.exit(1)

    tests = [
        # Correctness
        test_basic_get,
        test_post_json,
        test_post_string,
        test_post_base64,
        test_204,
        test_head,
        test_binary,
        test_text,
        test_4xx_5xx,
        test_user_agent,
        test_header_merge,
        test_config_update,
        test_redirects,
        test_redirect_disabled,
        test_too_many_redirects,
        test_duplicate_id_rejected,
        test_request_concurrency_limit_overloaded,
        test_duplicate_id_rejected_while_chunked_active,
        test_request_concurrency_limit_during_chunked_stream,
        test_redirect_303_switches_to_get,
        test_redirect_strips_auth_cross_origin,
        test_connection_refused,
        test_invalid_url,
        test_invalid_json,
        test_ping,
        test_startup,
        test_multi_header,
        test_unicode,
        test_parse_json_false,
        test_request_timeout,
        # Chunked
        test_chunked_sse,
        test_chunked_ndjson,
        # Download
        test_large_auto_download,
        test_save_to,
        test_download_progress,
        # Stress
        test_100_concurrent,
        test_200_varied,
        test_100_mixed_delays,
        test_500_rapid,
        test_50_posts,
        test_mixed_body_types,
        # Edge cases
        test_cancel,
        test_config_mid_flight,
        test_ping_flood,
        test_config_accumulate,
        test_empty_lines,
        test_retry_connect_refused,
        test_large_json_body,
        test_redirect_logging,
        test_eof_shutdown,
        test_close_count,
        # New: additional coverage
        test_multipart,
        test_multipart_file,
        test_multipart_base64,
        test_body_urlencoded,
        test_body_urlencoded_special_chars,
        test_body_urlencoded_duplicate_keys,
        test_body_urlencoded_mutual_exclusion,
        test_body_file,
        test_cancel_nonexistent,
        test_server_disconnect,
        test_chunked_disconnect,
        test_binary_chunked,
        test_concurrent_chunked,
        test_redirect_exact_boundary,
        test_redirect_over_boundary,
        test_429_not_retried,
        test_many_response_headers,
        test_100_errors,
        test_concurrent_downloads,
        test_slow_body,
        test_unknown_code,
        test_missing_fields,
        test_empty_post,
        test_many_request_headers,
        # New: Agent-First Data error format + features
        test_error_structured,
        test_tag_response,
        test_tag_error,
        test_tag_chunked,
        test_tag_absent,
        test_host_defaults,
        test_host_defaults_no_leak,
        test_retry_on_status,
        test_retry_on_status_exhausted,
        test_max_response_bytes,
        test_max_response_bytes_chunked,
        test_content_length_bytes,
        test_content_length_bytes_download,
        test_1000_rapid,
        # Resume download
        test_response_save_resume_no_file,
        test_response_save_resume_new_file,
        test_response_save_resume,
        test_response_save_resume_empty_file,
        # Invalid UTF-8 safety
        test_invalid_utf8_text,
        test_invalid_utf8_json,
        test_chunked_invalid_utf8,
    ]

    for t in tests:
        t()

    print(f"\n{'='*60}")
    print(f"  {passed} passed, {failed} failed, {passed+failed} total")
    if errors:
        print(f"\n  Failures:")
        for name, msg in errors:
            print(f"    {name}: {msg}")
    print(f"{'='*60}")

    server.shutdown()
    sys.exit(1 if failed else 0)


if __name__ == "__main__":
    main()