proxima-lib 0.1.0

A sans-IO core where everything is a Pipe — one async In -> Result<Out, Err> trait worn four ways (transform, source, sink, observe), composed into proxies, gateways, caches, codecs and telemetry pipelines. Config-driven, runtime-agnostic: the same pipe runs on the from-scratch Prime per-core runtime, on tokio, or on no_std bare metal.
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
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex, OnceLock, Weak};
use std::task::{Context, Poll};
use std::time::Duration;

use bon::Builder;
use bytes::Bytes;
use conflaguration::{Settings, Validate, ValidationMessage};
use futures::{FutureExt, Stream, select_biased};
use proxima_primitives::pipe::capabilities::Clock;
use proxima_primitives::pipe::clock::TimeClock;
use proxima_primitives::sync::mpsc;
use proxima_primitives::sync::oneshot;
use proxima_telemetry::error;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::OffsetDateTime;
use ulid::Ulid;

use crate::body::{ChunkStream, RequestStream, ResponseStream};
use crate::capture_surface::CaptureContext;
use crate::error::ProximaError;
use proxima_primitives::pipe::{Pipe, SendPipe};

use crate::pipe::{Handler, PipeHandle, ThreadLocalHandler, ThreadLocalPipeHandle, into_handle};
use crate::pipe_factory::{PipeFactory, PipeFactoryRegistry};
use crate::recording::LiveCaptureContext;
use crate::recording::event::{
    HttpEvent, InteractionId, ProtocolEvent, RecordMeta, RecordingEvent, RequestHeader,
};
use crate::recording::sink::DynRecordingSink;
use crate::recording::{
    AccumulatingSink, DeferredRuntime, FormatKind, LazyFanOut, SinkSpec, deferred_runtime,
};
use crate::request::{Request, Response};
use crate::runtime::{CoreId, Runtime};

// memoized sender to a RecordUpstream's single, long-lived drainer.
type DrainerCell = Arc<OnceLock<mpsc::UnboundedSender<DrainerMessage>>>;

/// A message on the drainer's ordered channel: either a recording event to
/// append, or a barrier asking "is everything sent before me durable yet".
/// Channel-only — never serialized, unlike [`RecordingEvent`] — so a
/// barrier can never leak into the recorded wire format.
// `Event` is the hot-path variant (every request sends several); boxing it
// to shrink `Barrier` would put a heap allocation on every recorded event
// to save stack space on a variant sent at most once per `flush()` call.
#[allow(clippy::large_enum_variant)]
enum DrainerMessage {
    Event(RecordingEvent),
    /// Reports the outcome of the flush the barrier triggered, so a caller
    /// awaiting [`RecordUpstream::flush`] observes a real I/O error rather
    /// than a bare "done".
    Barrier(oneshot::Sender<Result<(), ProximaError>>),
}

/// Proxy that tees every (request, response) interaction into a
/// recording sink. Per-chunk events preserve inter-chunk timing for
/// replay; sink writes drain on a background task so the request hot
/// path doesn't block on I/O.
///
/// Generic over the inner handle: `RecordUpstream<PipeHandle>` impls
/// `Handler`; `RecordUpstream<ThreadLocalPipeHandle>` impls
/// `ThreadLocalHandler`. Dispatch unifies through the
/// `ThreadLocalHandler` blanket so a single body pipes both paths.
pub struct RecordUpstream<Inner = PipeHandle, Clk = TimeClock> {
    label: String,
    inner: Inner,
    sink: DynRecordingSink,
    pipe_label: String,
    protocol: String,
    // armed by the App at serve; once set, the drainer spawns once instead
    // of per call (see `instance_drainer_sender`).
    spigot: DeferredRuntime,
    drainer: DrainerCell,
    // the injected time seam `TimedReplay` already uses on the replay side
    // (`proxima_primitives::pipe::capabilities::Clock`); production defaults
    // to `TimeClock` (the real driver), `with_clock` swaps in a deterministic
    // double so record and replay share one clock instead of record minting
    // its own `Instant`/`OffsetDateTime::now_utc()` reads.
    clock: Clk,
    // a wall-clock reading captured once at construction, paired with the
    // clock's own reading at that same instant. Every request's `ts_start`
    // is `wall_epoch + (clock.now_nanos() - epoch_nanos)` — a pure function
    // of the injected clock (so it can be made deterministic) that still
    // tracks real wall time under the production `TimeClock`, instead of a
    // fresh `now_utc()` read per request.
    wall_epoch: OffsetDateTime,
    epoch_nanos: u64,
    // seeded per-instance ULID randomness feeding `InteractionId`. Production
    // seeds this once from OS entropy (`rand::random`); `with_clock` accepts
    // an explicit seed so a test reproduces the exact same id sequence.
    // Mutex-guarded (never held across an `.await`) rather than lock-free,
    // matching this crate's existing short-critical-section convention (see
    // `pipe/causality.rs`'s `Arc<Vec<std::sync::Mutex<..>>>` slots).
    rng: Arc<Mutex<StdRng>>,
}

impl<Inner> RecordUpstream<Inner, TimeClock> {
    #[must_use]
    pub fn new(
        label: impl Into<String>,
        inner: Inner,
        sink: DynRecordingSink,
        pipe_label: impl Into<String>,
    ) -> Self {
        let clock = TimeClock;
        let epoch_nanos = clock.now_nanos();
        Self {
            label: label.into(),
            inner,
            sink,
            pipe_label: pipe_label.into(),
            protocol: "http".into(),
            spigot: deferred_runtime(),
            drainer: Arc::new(OnceLock::new()),
            clock,
            wall_epoch: OffsetDateTime::now_utc(),
            epoch_nanos,
            rng: Arc::new(Mutex::new(StdRng::seed_from_u64(rand::random()))),
        }
    }
}

impl<Inner, Clk> RecordUpstream<Inner, Clk>
where
    Clk: Clock,
{
    /// Build over an explicit [`Clock`] and a seeded RNG — the seam a
    /// deterministic test injects so two runs of the same scenario mint
    /// byte-identical `InteractionId`s and `ts_ms`/`ts_start` values.
    /// Mirrors `TimedReplay::with_clock` on the replay side (see
    /// `proxima-recording/src/pipe/replay.rs`).
    #[must_use]
    pub fn with_clock(
        label: impl Into<String>,
        inner: Inner,
        sink: DynRecordingSink,
        pipe_label: impl Into<String>,
        clock: Clk,
        wall_epoch: OffsetDateTime,
        rng_seed: u64,
    ) -> Self {
        let epoch_nanos = clock.now_nanos();
        Self {
            label: label.into(),
            inner,
            sink,
            pipe_label: pipe_label.into(),
            protocol: "http".into(),
            spigot: deferred_runtime(),
            drainer: Arc::new(OnceLock::new()),
            clock,
            wall_epoch,
            epoch_nanos,
            rng: Arc::new(Mutex::new(StdRng::seed_from_u64(rng_seed))),
        }
    }

    /// This upstream's label, set at construction (TARGET 3 — served-Handler
    /// naming now lives at the mount-site label, not the handle).
    #[must_use]
    pub fn label(&self) -> &str {
        &self.label
    }

    #[must_use]
    pub fn with_protocol(mut self, protocol: impl Into<String>) -> Self {
        self.protocol = protocol.into();
        self
    }

    /// Inject the runtime spigot so the drainer spawns once on the runtime
    /// instead of per call. Unarmed spigots (tests, direct construction)
    /// keep the legacy per-call drainer.
    #[must_use]
    pub fn with_runtime(mut self, spigot: DeferredRuntime) -> Self {
        self.spigot = spigot;
        self
    }

    /// Await durability of every interaction enqueued before this call.
    ///
    /// Enqueues a barrier on the SAME ordered channel recording events flow
    /// through, so it resolves only once every event sent strictly before
    /// it has been appended and the sink flushed — never a sleep, never a
    /// filesystem poll. Ordering, not timing, is what makes this
    /// deterministic. Calling it before any request has gone through starts
    /// the drainer exactly as the first real request would (the same
    /// `get_or_init`), so there is still exactly one channel and one
    /// ordering to reason about — no separate "is it running yet" check to
    /// get wrong.
    ///
    /// # Errors
    ///
    /// Returns `Err` rather than resolving if there is no persistent
    /// drainer to barrier against (no runtime armed via
    /// [`RecordUpstream::with_runtime`]) or if the drainer task is gone —
    /// spawn failed, panicked, or was cancelled mid-flight. A barrier that
    /// quietly "succeeds" against a missing or dead drainer would hide
    /// exactly the failure this API exists to surface.
    ///
    /// ```
    /// use bytes::Bytes;
    /// use proxima::pipe::{PipeHandle, into_handle};
    /// use proxima::{
    ///     AccumulatingSink, DynRecordingSink, LazyFanOut, ProximaError, RecordUpstream, Request,
    ///     Response, SendPipe, deferred_runtime,
    /// };
    ///
    /// struct EchoPipe;
    ///
    /// impl SendPipe for EchoPipe {
    ///     type In = Request<Bytes>;
    ///     type Out = Response<Bytes>;
    ///     type Err = ProximaError;
    ///     async fn call(&self, _request: Request<Bytes>) -> Result<Response<Bytes>, ProximaError> {
    ///         Ok(Response::ok("pong"))
    ///     }
    /// }
    ///
    /// # #[proxima::main]
    /// # async fn main() -> Result<(), ProximaError> {
    /// let sink: DynRecordingSink = std::sync::Arc::new(AccumulatingSink::with_defaults(
    ///     std::sync::Arc::new(LazyFanOut::new(Vec::new(), deferred_runtime())),
    /// ));
    /// let inner: PipeHandle = into_handle(EchoPipe);
    /// let upstream = RecordUpstream::new("example", inner, sink, "example");
    ///
    /// // no runtime armed: flush reports the gap instead of hanging forever.
    /// assert!(upstream.flush().await.is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub fn flush(&self) -> impl Future<Output = Result<(), ProximaError>> + Send + 'static {
        let sender = self
            .spigot
            .get()
            .map(|runtime| instance_drainer_sender(&self.drainer, runtime, &self.sink));
        async move {
            let sender = sender.ok_or_else(|| {
                ProximaError::Record(
                    "recording drainer has no armed runtime to flush (call with_runtime first)"
                        .into(),
                )
            })?;
            let (ack_sender, ack_receiver) = oneshot::channel();
            sender
                .send(DrainerMessage::Barrier(ack_sender))
                .map_err(|_| ProximaError::Record("recording drainer is gone".into()))?;
            ack_receiver.await.map_err(|_| {
                ProximaError::Record("recording drainer dropped before flushing".into())
            })?
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum Phase {
    Request,
    Response,
}

// spawns ONE long-lived drainer on the runtime the first time it's called and
// memoizes its sender; every later call reuses it — no spawn, no channel, per
// call.
fn instance_drainer_sender(
    drainer: &DrainerCell,
    runtime: &Arc<dyn Runtime>,
    sink: &DynRecordingSink,
) -> mpsc::UnboundedSender<DrainerMessage> {
    // spawn_on_core, not spawn_on_current_core: the recording pipe is driven
    // from arbitrary call sites, not necessarily a runtime worker thread.
    drainer
        .get_or_init(|| {
            let (sender, receiver) = mpsc::unbounded_channel::<DrainerMessage>();
            if let Err(error) =
                runtime.spawn_on_core(CoreId(0), Box::pin(drain_forever(receiver, sink.clone())))
            {
                error!(error = ?error, "recording drainer spawn failed");
            }
            sender
        })
        .clone()
}

// append each event, drain any burst already queued, then flush once caught
// up — durability amortized across the burst instead of once per call.
async fn drain_forever(
    mut receiver: mpsc::UnboundedReceiver<DrainerMessage>,
    sink: DynRecordingSink,
) {
    while let Some(message) = receiver.recv().await {
        drain_message(&sink, message).await;
        // drain any burst already queued without waiting for more: a
        // `now_or_never` immediate poll stands in for tokio's `try_recv`
        // (proxima's mpsc doesn't shim that non-blocking probe — see its
        // module doc's "Non-coverage" list).
        while let Some(Some(message)) = receiver.recv().now_or_never() {
            drain_message(&sink, message).await;
        }
        if let Err(error) = sink.flush().await {
            error!(error = %error, "recording sink flush failed");
        }
    }
    if let Err(error) = sink.flush().await {
        error!(error = %error, "recording sink flush failed");
    }
}

// appends a durable event, or — for a barrier — flushes right away and
// replies with the real outcome, so an awaiting caller never observes a
// later, unrelated flush's result in place of its own.
async fn drain_message(sink: &DynRecordingSink, message: DrainerMessage) {
    match message {
        DrainerMessage::Event(event) => {
            if let Err(error) = sink.append(event).await {
                error!(error = %error, "recording sink append failed");
            }
        }
        DrainerMessage::Barrier(ack) => {
            let result = sink.flush().await;
            if let Err(ref error) = result {
                error!(error = %error, "recording sink flush failed");
            }
            let _ = ack.send(result);
        }
    }
}

impl<Inner, Clk> SendPipe for RecordUpstream<Inner, Clk>
where
    Inner: Handler + Clone,
    Clk: Clock + Clone + Send + Sync + Unpin + 'static,
{
    type In = Request<Bytes>;
    type Out = Response<Bytes>;
    type Err = ProximaError;

    fn call(
        &self,
        request: Request<Bytes>,
    ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
        record_dispatch(
            self.inner.clone(),
            request,
            self.sink.clone(),
            self.pipe_label.clone(),
            self.protocol.clone(),
            self.spigot.clone(),
            self.drainer.clone(),
            self.clock.clone(),
            self.wall_epoch,
            self.epoch_nanos,
            self.rng.clone(),
        )
    }
}

impl<Clk> Pipe for RecordUpstream<ThreadLocalPipeHandle, Clk>
where
    Clk: Clock + Clone + Send + Unpin + 'static,
{
    type In = Request<Bytes>;
    type Out = Response<Bytes>;
    type Err = ProximaError;

    fn call(
        &self,
        request: Request<Bytes>,
    ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> {
        record_dispatch_local(
            self.inner.clone(),
            request,
            self.sink.clone(),
            self.pipe_label.clone(),
            self.protocol.clone(),
            self.spigot.clone(),
            self.drainer.clone(),
            self.clock.clone(),
            self.wall_epoch,
            self.epoch_nanos,
            self.rng.clone(),
        )
    }
}

// derives the `InteractionId`'s wall-clock-timestamp component and the
// `HttpEvent::Started.ts` field from the injected clock, so both are a pure
// function of `(wall_epoch, epoch_nanos, clock.now_nanos())` instead of a
// fresh `OffsetDateTime::now_utc()` read — the seam that makes two runs of
// the same scenario mint identical timestamps under a deterministic clock,
// while still tracking real wall time under the production `TimeClock`.
fn wall_clock_now(wall_epoch: OffsetDateTime, epoch_nanos: u64, now_nanos: u64) -> OffsetDateTime {
    wall_epoch + Duration::from_nanos(now_nanos.saturating_sub(epoch_nanos))
}

// two `next_u64` draws under one lock acquisition give the 80 bits of
// randomness `Ulid::from_parts` keeps (it masks away the rest) — see
// `InteractionId::new()`'s `ulid::Ulid::from_datetime_with_source`, whose
// same-shaped msb/lsb draw this seeded path replaces.
fn draw_interaction_random(rng: &Mutex<StdRng>) -> u128 {
    let mut guard = rng
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let high = u128::from(guard.next_u64());
    let low = u128::from(guard.next_u64());
    (high << 64) | low
}

fn mint_interaction_id(
    wall_epoch: OffsetDateTime,
    epoch_nanos: u64,
    now_nanos: u64,
    rng: &Mutex<StdRng>,
) -> (InteractionId, OffsetDateTime) {
    let ts_start = wall_clock_now(wall_epoch, epoch_nanos, now_nanos);
    let timestamp_ms = u64::try_from(ts_start.unix_timestamp_nanos() / 1_000_000).unwrap_or(0);
    let random = draw_interaction_random(rng);
    (
        InteractionId::from_ulid(Ulid::from_parts(timestamp_ms, random)),
        ts_start,
    )
}

fn elapsed_ms(now_nanos: u64, started_nanos: u64) -> u64 {
    now_nanos.saturating_sub(started_nanos) / 1_000_000
}

/// Shared body for both Handler and ThreadLocalHandler impls. Dispatches
/// the inner call via `ThreadLocalHandler::call` — the blanket impl makes
/// every `Handler` automatically a `ThreadLocalHandler`, so a Send Inner
/// still produces a Send future here and an Rc-based Inner produces a
/// !Send one.
// four extra params (clock/wall_epoch/epoch_nanos/rng) over the pre-existing
// seven thread the injected time+rng seam through from `RecordUpstream`'s
// fields; a wrapper struct would just relocate the same plumbing.
#[allow(clippy::too_many_arguments)]
async fn record_dispatch<Inner, Clk>(
    inner: Inner,
    request: Request<Bytes>,
    sink: DynRecordingSink,
    pipe_label: String,
    protocol: String,
    spigot: DeferredRuntime,
    drainer: DrainerCell,
    clock: Clk,
    wall_epoch: OffsetDateTime,
    epoch_nanos: u64,
    rng: Arc<Mutex<StdRng>>,
) -> Result<Response<Bytes>, ProximaError>
where
    Inner: Handler + Clone,
    Clk: Clock + Clone + Send + Sync + Unpin + 'static,
{
    let cancel = request.context.cancel.clone();
    let started_nanos = clock.now_nanos();
    let (id, ts_start) = mint_interaction_id(wall_epoch, epoch_nanos, started_nanos, &rng);

    let sender = match spigot.get() {
        // armed: one drainer per RecordUpstream, spawned once on the
        // injected runtime — not a `tokio::spawn` per call.
        Some(runtime) => instance_drainer_sender(&drainer, runtime, &sink),
        // unarmed (tests / direct construction): the legacy per-call
        // drainer on the ambient tokio runtime, cancellable with the request.
        None => {
            let (sender, mut receiver) = mpsc::unbounded_channel::<DrainerMessage>();
            let sink_for_task = sink.clone();
            let drainer_cancel = cancel.clone();
            // no injected runtime to spawn_on_core against: a dedicated OS
            // thread driving `block_on` gives real background progress
            // without requiring any particular async runtime (mirrors
            // `proxima_primitives::sync::task`'s portable `JoinSet`).
            std::thread::spawn(move || {
                futures::executor::block_on(async move {
                    loop {
                        select_biased! {
                            _ = drainer_cancel.fired().fuse() => break,
                            message = receiver.recv().fuse() => match message {
                                Some(message) => drain_message(&sink_for_task, message).await,
                                None => break,
                            },
                        }
                    }
                    if let Err(error) = sink_for_task.flush().await {
                        error!(error = %error, "recording sink flush failed");
                    }
                });
            });
            sender
        }
    };

    let Request {
        method,
        path,
        query,
        metadata,
        payload,
        stream,
        mut context,
        ..
    } = request;
    let req_chunks: ChunkStream = match stream {
        Some(request_stream) => request_stream.into_chunk_stream(),
        None => Box::pin(futures::stream::once(async move { Ok(payload) })),
    };
    let capture = Arc::new(LiveCaptureContext::new());
    context.capture = Some(capture.clone() as Arc<dyn CaptureContext>);

    let req_headers_for_record: std::collections::BTreeMap<String, String> = metadata
        .iter()
        .map(|(name, value)| {
            (
                String::from_utf8_lossy(name).into_owned(),
                String::from_utf8_lossy(value).into_owned(),
            )
        })
        .collect();
    let req_query_for_record: std::collections::BTreeMap<String, String> = query
        .iter()
        .map(|(name, value)| {
            (
                String::from_utf8_lossy(name).into_owned(),
                String::from_utf8_lossy(value).into_owned(),
            )
        })
        .collect();
    // `protocol` is implied by ProtocolEvent::Http; the previous `protocol: String` field is dropped.
    let _ = protocol;
    let _ = sender.send(DrainerMessage::Event(RecordingEvent {
        id,
        ts_ms: 0,
        parent: None,
        event: ProtocolEvent::Http(HttpEvent::Started {
            ts: ts_start,
            pipe: pipe_label,
            request: RequestHeader {
                method: String::from_utf8_lossy(method.as_bytes()).into_owned(),
                path: String::from_utf8_lossy(&path).into_owned(),
                headers: req_headers_for_record,
                query: req_query_for_record,
            },
            meta: None,
        }),
    }));

    let req_body = wrap_chunked(
        req_chunks,
        clock.clone(),
        started_nanos,
        id,
        sender.clone(),
        Phase::Request,
        capture.clone(),
    );

    let inbound = Request {
        method,
        path,
        query,
        metadata,
        payload: Bytes::new(),
        stream: Some(RequestStream::from_chunk_stream(req_body)),
        context,
    };
    let response = SendPipe::call(&inner, inbound).await?;

    let resp_started_ms = elapsed_ms(clock.now_nanos(), started_nanos);
    let header_pairs: Vec<(String, String)> = response
        .metadata
        .iter()
        .map(|(name, value)| {
            (
                String::from_utf8_lossy(name).into_owned(),
                String::from_utf8_lossy(value).into_owned(),
            )
        })
        .collect();
    let _ = sender.send(DrainerMessage::Event(RecordingEvent {
        id,
        ts_ms: resp_started_ms,
        parent: None,
        event: ProtocolEvent::Http(HttpEvent::ResponseStarted {
            status: response.status,
            headers: header_pairs,
        }),
    }));

    let status = response.status;
    let headers = response.metadata.clone();
    let resp_chunks = response.into_chunk_stream();
    let resp_body = wrap_chunked(
        resp_chunks,
        clock,
        started_nanos,
        id,
        sender,
        Phase::Response,
        capture,
    );
    let mut rebuilt =
        Response::new(status).with_stream(ResponseStream::from_chunk_stream(resp_body));
    for (name, value) in headers {
        rebuilt = rebuilt.with_header(name, value);
    }
    Ok(rebuilt)
}

// !Send variant for `impl ThreadLocalHandler for RecordUpstream<ThreadLocalPipeHandle>`.
// Identical body to `record_dispatch` modulo the dispatch trait. Lives separately
// because the previous `impl<T: Handler> ThreadLocalHandler for T` blanket was removed
// during the proxima-pipe extraction (coherence issue with downstream wrappers).
// same shape as `record_dispatch` above — see its comment.
#[allow(clippy::too_many_arguments)]
async fn record_dispatch_local<Inner, Clk>(
    inner: Inner,
    request: Request<Bytes>,
    sink: DynRecordingSink,
    pipe_label: String,
    protocol: String,
    spigot: DeferredRuntime,
    drainer: DrainerCell,
    clock: Clk,
    wall_epoch: OffsetDateTime,
    epoch_nanos: u64,
    rng: Arc<Mutex<StdRng>>,
) -> Result<Response<Bytes>, ProximaError>
where
    Inner: ThreadLocalHandler + Clone,
    Clk: Clock + Clone + Send + Unpin + 'static,
{
    let cancel = request.context.cancel.clone();
    let started_nanos = clock.now_nanos();
    let (id, ts_start) = mint_interaction_id(wall_epoch, epoch_nanos, started_nanos, &rng);

    let sender = match spigot.get() {
        // armed: one drainer per RecordUpstream, spawned once on the
        // injected runtime — not a `tokio::spawn` per call.
        Some(runtime) => instance_drainer_sender(&drainer, runtime, &sink),
        // unarmed (tests / direct construction): the legacy per-call
        // drainer on the ambient tokio runtime, cancellable with the request.
        None => {
            let (sender, mut receiver) = mpsc::unbounded_channel::<DrainerMessage>();
            let sink_for_task = sink.clone();
            let drainer_cancel = cancel.clone();
            // no injected runtime to spawn_on_core against: a dedicated OS
            // thread driving `block_on` gives real background progress
            // without requiring any particular async runtime (mirrors
            // `proxima_primitives::sync::task`'s portable `JoinSet`).
            std::thread::spawn(move || {
                futures::executor::block_on(async move {
                    loop {
                        select_biased! {
                            _ = drainer_cancel.fired().fuse() => break,
                            message = receiver.recv().fuse() => match message {
                                Some(message) => drain_message(&sink_for_task, message).await,
                                None => break,
                            },
                        }
                    }
                    if let Err(error) = sink_for_task.flush().await {
                        error!(error = %error, "recording sink flush failed");
                    }
                });
            });
            sender
        }
    };

    let Request {
        method,
        path,
        query,
        metadata,
        payload,
        stream,
        mut context,
        ..
    } = request;
    let req_chunks: ChunkStream = match stream {
        Some(request_stream) => request_stream.into_chunk_stream(),
        None => Box::pin(futures::stream::once(async move { Ok(payload) })),
    };
    let capture = Arc::new(LiveCaptureContext::new());
    context.capture = Some(capture.clone() as Arc<dyn CaptureContext>);

    let req_headers_for_record: std::collections::BTreeMap<String, String> = metadata
        .iter()
        .map(|(name, value)| {
            (
                String::from_utf8_lossy(name).into_owned(),
                String::from_utf8_lossy(value).into_owned(),
            )
        })
        .collect();
    let req_query_for_record: std::collections::BTreeMap<String, String> = query
        .iter()
        .map(|(name, value)| {
            (
                String::from_utf8_lossy(name).into_owned(),
                String::from_utf8_lossy(value).into_owned(),
            )
        })
        .collect();
    let _ = protocol;
    let _ = sender.send(DrainerMessage::Event(RecordingEvent {
        id,
        ts_ms: 0,
        parent: None,
        event: ProtocolEvent::Http(HttpEvent::Started {
            ts: ts_start,
            pipe: pipe_label,
            request: RequestHeader {
                method: String::from_utf8_lossy(method.as_bytes()).into_owned(),
                path: String::from_utf8_lossy(&path).into_owned(),
                headers: req_headers_for_record,
                query: req_query_for_record,
            },
            meta: None,
        }),
    }));

    let req_body = wrap_chunked(
        req_chunks,
        clock.clone(),
        started_nanos,
        id,
        sender.clone(),
        Phase::Request,
        capture.clone(),
    );

    let inbound = Request {
        method,
        path,
        query,
        metadata,
        payload: Bytes::new(),
        stream: Some(RequestStream::from_chunk_stream(req_body)),
        context,
    };
    let response = Pipe::call(&inner, inbound).await?;

    let resp_started_ms = elapsed_ms(clock.now_nanos(), started_nanos);
    let header_pairs: Vec<(String, String)> = response
        .metadata
        .iter()
        .map(|(name, value)| {
            (
                String::from_utf8_lossy(name).into_owned(),
                String::from_utf8_lossy(value).into_owned(),
            )
        })
        .collect();
    let _ = sender.send(DrainerMessage::Event(RecordingEvent {
        id,
        ts_ms: resp_started_ms,
        parent: None,
        event: ProtocolEvent::Http(HttpEvent::ResponseStarted {
            status: response.status,
            headers: header_pairs,
        }),
    }));

    let status = response.status;
    let headers = response.metadata.clone();
    let resp_chunks = response.into_chunk_stream();
    let resp_body = wrap_chunked(
        resp_chunks,
        clock,
        started_nanos,
        id,
        sender,
        Phase::Response,
        capture,
    );
    let mut rebuilt =
        Response::new(status).with_stream(ResponseStream::from_chunk_stream(resp_body));
    for (name, value) in headers {
        rebuilt = rebuilt.with_header(name, value);
    }
    Ok(rebuilt)
}

fn wrap_chunked<Clk>(
    inner: ChunkStream,
    clock: Clk,
    started_nanos: u64,
    id: InteractionId,
    sender: mpsc::UnboundedSender<DrainerMessage>,
    phase: Phase,
    capture: Arc<LiveCaptureContext>,
) -> ChunkStream
where
    Clk: Clock + Send + Unpin + 'static,
{
    Box::pin(ChunkRecorder {
        inner,
        clock,
        started_nanos,
        id,
        sender: Some(sender),
        phase,
        end_emitted: false,
        capture,
    })
}

struct ChunkRecorder<Clk: Clock> {
    inner: ChunkStream,
    clock: Clk,
    started_nanos: u64,
    id: InteractionId,
    sender: Option<mpsc::UnboundedSender<DrainerMessage>>,
    phase: Phase,
    end_emitted: bool,
    capture: Arc<LiveCaptureContext>,
}

impl<Clk: Clock> ChunkRecorder<Clk> {
    fn elapsed_ms(&self) -> u64 {
        elapsed_ms(self.clock.now_nanos(), self.started_nanos)
    }

    fn emit_chunk(&mut self, chunk: &Bytes) {
        if let Some(sender) = self.sender.as_ref() {
            let metadata = self.capture.drain();
            let ts_ms = self.elapsed_ms();
            let http_event = match self.phase {
                Phase::Request => HttpEvent::RequestChunk {
                    data: chunk.clone(),
                    metadata,
                },
                Phase::Response => HttpEvent::ResponseChunk {
                    data: chunk.clone(),
                    metadata,
                },
            };
            let _ = sender.send(DrainerMessage::Event(RecordingEvent {
                id: self.id,
                ts_ms,
                parent: None,
                event: ProtocolEvent::Http(http_event),
            }));
        }
    }

    fn emit_end(&mut self) {
        if self.end_emitted {
            return;
        }
        self.end_emitted = true;
        if let Some(sender) = self.sender.take() {
            let ts_end = self.elapsed_ms();
            let http_event = match self.phase {
                Phase::Request => HttpEvent::RequestEnded,
                Phase::Response => HttpEvent::Ended {
                    latency_ms: ts_end,
                    meta: RecordMeta::default(),
                },
            };
            let _ = sender.send(DrainerMessage::Event(RecordingEvent {
                id: self.id,
                ts_ms: ts_end,
                parent: None,
                event: ProtocolEvent::Http(http_event),
            }));
        }
    }
}

impl<Clk: Clock> Drop for ChunkRecorder<Clk> {
    fn drop(&mut self) {
        // emit end-of-interaction even if the consumer drops mid-stream.
        self.emit_end();
    }
}

impl<Clk: Clock + Send + Unpin + 'static> Stream for ChunkRecorder<Clk> {
    type Item = Result<Bytes, ProximaError>;

    fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match Pin::new(&mut self.inner).poll_next(ctx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(None) => {
                self.emit_end();
                Poll::Ready(None)
            }
            Poll::Ready(Some(Err(error))) => {
                // upstream stream errored; drop the sender so the drainer exits.
                self.sender = None;
                self.end_emitted = true;
                Poll::Ready(Some(Err(error)))
            }
            Poll::Ready(Some(Ok(chunk))) => {
                self.emit_chunk(&chunk);
                Poll::Ready(Some(Ok(chunk)))
            }
        }
    }
}

/// `Weak<PipeFactoryRegistry>` because `inner` resolves through the same
/// registry that owns this factory — Arc would cycle.
pub struct RecordPipeFactory {
    upstreams: Weak<PipeFactoryRegistry>,
    spigot: DeferredRuntime,
}

impl RecordPipeFactory {
    #[must_use]
    pub fn new(upstreams: Weak<PipeFactoryRegistry>, spigot: DeferredRuntime) -> Self {
        Self { upstreams, spigot }
    }

    // shared by the `PipeFactory` impl (which must return the erased
    // `PipeHandle` for the heterogeneous registry) and tests that need the
    // concrete `RecordUpstream` back — e.g. to await `RecordUpstream::flush`,
    // a capability `PipeHandle`'s object-safe erasure has no room to carry.
    async fn build_typed(&self, spec: &Value) -> Result<RecordUpstream<PipeHandle>, ProximaError> {
        let config: RecordConfig = serde_json::from_value(spec.clone())
            .map_err(|err| ProximaError::Config(format!("record config: {err}")))?;
        config
            .validate()
            .map_err(|err| ProximaError::Config(format!("{err}")))?;
        let label = config.name.clone();
        let pipe_label = config.pipe.clone().unwrap_or_else(|| label.clone());
        let sink_spec = config.sink.into_sink_spec()?;
        let durable = Arc::new(LazyFanOut::new(vec![sink_spec], self.spigot.clone()));
        let sink: DynRecordingSink = Arc::new(AccumulatingSink::with_defaults(durable));
        let inner = resolve_inner(&config.inner, &self.upstreams).await?;
        Ok(RecordUpstream::new(label, inner, sink, pipe_label)
            .with_protocol(config.protocol)
            .with_runtime(self.spigot.clone()))
    }
}

impl PipeFactory for RecordPipeFactory {
    fn name(&self) -> &str {
        "record"
    }

    fn build(
        &self,
        spec: &Value,
        _inner: Option<PipeHandle>,
    ) -> Pin<Box<dyn Future<Output = Result<PipeHandle, ProximaError>> + Send + '_>> {
        let spec = spec.clone();
        Box::pin(async move { Ok(into_handle(self.build_typed(&spec).await?)) })
    }
}

/// Serialisable recording format — the config mirror of [`FormatKind`].
/// Accepts `bin`, `jsonl`, or `json` (the last two both → JSON) matching the
/// historical hand-parser; defaults to `bin`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FormatChoice {
    #[default]
    Bin,
    Jsonl,
    Json,
}

impl From<FormatChoice> for FormatKind {
    fn from(choice: FormatChoice) -> Self {
        match choice {
            FormatChoice::Bin => FormatKind::Bin,
            FormatChoice::Jsonl | FormatChoice::Json => FormatKind::Json,
        }
    }
}

fn default_format() -> FormatChoice {
    FormatChoice::Bin
}

/// Typed config for a recording sink — the destination file + format the
/// interactions are written to. Mirrors [`SinkSpec`].
#[derive(Debug, Clone, Builder, Deserialize, Serialize, Settings)]
#[settings(prefix = "PROXIMA_RECORD_SINK")]
#[builder(derive(Clone, Debug), on(String, into))]
pub struct SinkConfig {
    /// Destination path for the recording.
    pub path: String,

    /// Output format (`bin` | `jsonl` | `json`). Defaults to `bin`. The wire
    /// form also accepts the legacy `format` key as an alias for `type`.
    #[setting(skip)]
    #[serde(default = "default_format", alias = "format", rename = "type")]
    #[builder(default = default_format())]
    pub format: FormatChoice,

    /// Optional zstd compression level for the `bin` format.
    #[setting(default)]
    #[serde(default)]
    pub zstd_level: Option<i32>,
}

impl SinkConfig {
    /// Lower the wire config to the runtime [`SinkSpec`].
    pub fn into_sink_spec(self) -> Result<SinkSpec, ProximaError> {
        let mut sink_spec = SinkSpec::new(&self.path, self.format.into());
        if let Some(level) = self.zstd_level {
            sink_spec = sink_spec.with_zstd_level(level);
        }
        Ok(sink_spec)
    }
}

fn default_record_label() -> String {
    "record".to_string()
}

fn default_protocol() -> String {
    "http".to_string()
}

/// Typed config surface for the `record` upstream — a tee that records every
/// interaction through `inner` to a `sink`. `inner` stays a recursive pipe
/// spec (resolved via the registry like `load.rs::build_pipe`), so it is held
/// as a neutral [`Value`] rather than flattened.
#[derive(Debug, Clone, Builder, Deserialize, Serialize)]
#[builder(derive(Clone, Debug), on(String, into))]
pub struct RecordConfig {
    /// Recording sink (destination + format).
    pub sink: SinkConfig,

    /// The inner pipe spec to wrap and record (recursive).
    pub inner: Value,

    /// Handler / upstream label.
    #[serde(default = "default_record_label")]
    #[builder(default = default_record_label())]
    pub name: String,

    /// Logical pipe label stamped into the recording. Defaults to `name`.
    #[serde(default)]
    pub pipe: Option<String>,

    /// Recorded protocol tag. Defaults to `http`.
    #[serde(default = "default_protocol")]
    #[builder(default = default_protocol())]
    pub protocol: String,
}

impl Validate for RecordConfig {
    fn validate(&self) -> conflaguration::Result<()> {
        let mut errors = Vec::new();
        if self.sink.path.is_empty() {
            errors.push(ValidationMessage::new("sink.path", "must not be empty"));
        }
        if self.inner.is_null() {
            errors.push(ValidationMessage::new("inner", "must not be null"));
        }
        if errors.is_empty() {
            Ok(())
        } else {
            Err(conflaguration::Error::Validation { errors })
        }
    }
}

async fn resolve_inner(
    spec: &Value,
    upstreams: &Weak<PipeFactoryRegistry>,
) -> Result<PipeHandle, ProximaError> {
    let registry = upstreams.upgrade().ok_or_else(|| {
        ProximaError::Registry("upstream registry dropped before record build".into())
    })?;
    // mirror the shorthand dispatch in load.rs::build_pipe.
    if let Some(http) = spec.get("http")
        && let Some(url) = http.as_str()
    {
        let factory = registry.get("http")?;
        let inner_spec = serde_json::json!({ "url": url });
        return factory.build(&inner_spec, None).await;
    }
    if let Some(synth) = spec.get("synth") {
        let factory = registry.get("synth")?;
        return factory.build(synth, None).await;
    }
    if let Some(callback) = spec.get("callback") {
        let factory = registry.get("callback")?;
        return factory.build(callback, None).await;
    }
    if let Some(replay) = spec.get("replay") {
        let factory = registry.get("replay")?;
        return factory.build(replay, None).await;
    }
    if let Some(process) = spec.get("process") {
        let factory = registry.get("process")?;
        return factory.build(process, None).await;
    }
    if let Some(rpc) = spec.get("process_rpc") {
        let factory = registry.get("process_rpc")?;
        return factory.build(rpc, None).await;
    }
    if let Some(type_field) = spec.get("type").and_then(Value::as_str) {
        let factory = registry.get(type_field)?;
        return factory.build(spec, None).await;
    }
    Err(ProximaError::Config(
        "record.inner needs http / synth / callback / replay / process / process_rpc / `type`"
            .into(),
    ))
}

#[cfg(all(
    test,
    feature = "runtime-prime-executor",
    feature = "runtime-prime-inbox-alloc",
    feature = "runtime-prime-reactor",
    feature = "runtime-prime-bgpool"
))]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::recording::JsonlSource;
    use crate::recording::source::RecordingSource;
    use crate::upstreams::synth::SynthPipeFactory;
    use futures::StreamExt;
    use std::sync::Arc;
    use tempfile::tempdir;

    fn build_pipe_factory_registry() -> Arc<PipeFactoryRegistry> {
        let registry = Arc::new(PipeFactoryRegistry::new());
        registry
            .register(Arc::new(SynthPipeFactory))
            .expect("register synth");
        registry
    }

    // production arms `PrimeRuntime` through `prime::config::Builder`, which
    // defaults `background_pool` to `PoolKind::Rayon` (prime/src/config.rs)
    // — every durable write in the recording pipe routes through
    // `Runtime::spawn_background_blocking`, and a real pool is always
    // attached. Bypassing the builder here (bare `PrimeRuntime::new`) left
    // `background_pool: None`, so `spawn_background_blocking` fell back to
    // spawning a brand-new OS thread per append/flush call (prime/src/os/runtime.rs) —
    // thread-creation latency that balloons under host contention. A fixed
    // retry-budget poll used to race that latency and lose; `RecordUpstream::flush`
    // now awaits a real barrier instead, but a warm pool still keeps these tests
    // fast. `ProximaBackgroundPool` mirrors production's persistent-pool shape
    // without pulling in `rayon`.
    fn armed_spigot() -> crate::recording::DeferredRuntime {
        let spigot = crate::recording::deferred_runtime();
        let background_pool: std::sync::Arc<dyn crate::runtime::BackgroundPool> =
            std::sync::Arc::new(
                crate::runtime::prime::os::background::ProximaBackgroundPool::new()
                    .expect("background pool"),
            );
        let runtime: std::sync::Arc<dyn crate::runtime::Runtime> = std::sync::Arc::new(
            crate::runtime::PrimeRuntime::new(1)
                .expect("prime runtime")
                .with_background_pool(background_pool),
        );
        // block here (setup, off the request/assertion critical path) until
        // core 0's worker thread has actually run one task. A freshly
        // `thread::Builder::spawn`'d worker is kernel-scheduled but not yet
        // CPU-scheduled; under host contention it can sit unscheduled for
        // longer than a bounded polling loop's whole retry budget, which is
        // exactly the race a since-removed filesystem-polling test helper
        // lost (the drainer task never got a first turn before the poll gave
        // up). A real blocking handshake here proves the worker is warm before any recording
        // event is ever sent to it.
        let (ready_sender, ready_receiver) = std::sync::mpsc::channel::<()>();
        runtime
            .spawn_on_core(
                crate::runtime::CoreId(0),
                Box::pin(async move {
                    let _ = ready_sender.send(());
                }),
            )
            .expect("warm up drainer core");
        ready_receiver
            .recv()
            .expect("drainer core never scheduled a task");
        spigot.set(runtime).ok();
        spigot
    }

    // a single, non-looping read: only ever called after `RecordUpstream::flush`
    // has resolved, so the file already holds everything the barrier ordered
    // ahead of it — no retry budget to race, nothing left to poll for.
    async fn read_recorded_events(path: &std::path::Path) -> Vec<RecordingEvent> {
        if !path.exists() {
            return Vec::new();
        }
        let runtime: std::sync::Arc<dyn crate::runtime::Runtime> =
            std::sync::Arc::new(crate::runtime::PrimeRuntime::new(1).expect("prime runtime"));
        let source = JsonlSource::new(path, runtime);
        let mut events = source.events();
        let mut collected: Vec<RecordingEvent> = Vec::new();
        while let Some(event) = events.next().await {
            match event {
                Ok(recording_event) => collected.push(recording_event),
                Err(_) => break,
            }
        }
        collected
    }

    // principle-4 parity: the fluent builder and the config value must lower to
    // identical SinkSpec state (path, format, zstd level).
    #[test]
    fn parity_fluent_builder_and_config_value_match() {
        let from_value: SinkConfig = serde_json::from_value(serde_json::json!({
            "path": "/var/trace.bin",
            "type": "bin",
            "zstd_level": 7,
        }))
        .expect("from_value");
        let from_value = from_value.into_sink_spec().expect("into_sink_spec value");

        let from_builder = SinkConfig::builder()
            .path("/var/trace.bin")
            .format(FormatChoice::Bin)
            .zstd_level(7)
            .build()
            .into_sink_spec()
            .expect("into_sink_spec builder");

        assert_eq!(from_value, from_builder);
    }

    #[test]
    fn sink_format_alias_maps_jsonl_to_json() {
        let via_format: SinkConfig =
            serde_json::from_value(serde_json::json!({"path": "/x", "format": "jsonl"}))
                .expect("from_value");
        assert_eq!(
            via_format.into_sink_spec().expect("spec").format,
            FormatKind::Json
        );
    }

    #[proxima::test]
    async fn record_factory_resolves_inner_via_registry_and_records_round_trip() {
        let dir = tempdir().expect("tempdir");
        let path = dir.path().join("trace.jsonl");
        let upstreams = build_pipe_factory_registry();
        let factory = RecordPipeFactory::new(Arc::downgrade(&upstreams), armed_spigot());
        let spec = serde_json::json!({
            "name": "echo_recorded",
            "pipe": "echo",
            "sink":  { "type": "jsonl", "path": path.to_string_lossy() },
            "inner": { "synth": { "status": 200, "body": "from-inner" } },
        });
        // `build_typed` (not the `PipeFactory::build` trait method) returns
        // the concrete `RecordUpstream` instead of an erased `PipeHandle` —
        // the erased handle has no room to carry `flush`, the capability this
        // test needs to await durability deterministically.
        let upstream = factory.build_typed(&spec).await.expect("build");
        let request = Request::builder()
            .method("POST")
            .path("/v1/chat")
            .body("hello")
            .build()
            .expect("request");
        let response = SendPipe::call(&upstream, request).await.expect("call");
        assert_eq!(response.status, 200);
        let body = response.collect_body().await.expect("collect");
        assert_eq!(&body[..], b"from-inner");

        upstream.flush().await.expect("flush");
        let collected = read_recorded_events(&path).await;
        assert!(matches!(
            collected[0].event,
            ProtocolEvent::Http(HttpEvent::Started { .. })
        ));
        // streaming order: request side may emit RequestChunk(s) +
        // RequestEnded before ResponseStarted. assert the structural shape.
        let mut idx = 1;
        while matches!(
            collected.get(idx).map(|event| &event.event),
            Some(ProtocolEvent::Http(HttpEvent::RequestChunk { .. }))
        ) {
            idx += 1;
        }
        assert!(matches!(
            collected[idx].event,
            ProtocolEvent::Http(HttpEvent::RequestEnded)
        ));
        idx += 1;
        assert!(matches!(
            collected[idx].event,
            ProtocolEvent::Http(HttpEvent::ResponseStarted { .. })
        ));
        idx += 1;
        while matches!(
            collected.get(idx).map(|event| &event.event),
            Some(ProtocolEvent::Http(HttpEvent::ResponseChunk { .. }))
        ) {
            idx += 1;
        }
        assert!(matches!(
            collected[idx].event,
            ProtocolEvent::Http(HttpEvent::Ended { .. })
        ));
    }

    #[proxima::test]
    async fn pipe_attached_metadata_round_trips_to_recorded_response_chunk() {
        use crate::pipe::into_handle;

        // a Handler that stashes a "clock_at_call" entropy fingerprint into
        // the per-call capture context. mirrors what an entropy wrapper
        // elsewhere in the workspace would do at a nondeterministic seam.
        struct ClockCapturingPipe {
            clock_value: u64,
        }
        impl SendPipe for ClockCapturingPipe {
            type In = Request<Bytes>;
            type Out = Response<Bytes>;
            type Err = ProximaError;

            fn call(
                &self,
                request: Request<Bytes>,
            ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> {
                let clock_value = self.clock_value;
                async move {
                    if let Some(capture) = request.context.capture.as_ref() {
                        capture.attach(
                            "clock_at_call",
                            bytes::Bytes::copy_from_slice(&clock_value.to_be_bytes()),
                        );
                    }
                    Ok(Response::ok(bytes::Bytes::from_static(b"recorded-body")))
                }
            }
        }

        let dir = tempdir().expect("tempdir");
        let path = dir.path().join("trace.jsonl");
        // one spigot shared by the sink AND the upstream: `RecordUpstream`
        // needs its own runtime armed (not just the sink's) for `flush` to
        // have a persistent drainer to barrier against.
        let spigot = armed_spigot();
        let durable = Arc::new(LazyFanOut::new(
            vec![SinkSpec::new(path.to_string_lossy(), FormatKind::Json)],
            spigot.clone(),
        ));
        let sink: DynRecordingSink = Arc::new(AccumulatingSink::with_defaults(durable));
        let inner = into_handle(ClockCapturingPipe {
            clock_value: 0x0123_4567_89AB_CDEF,
        });
        let recorder = RecordUpstream::new("recorded", inner, sink, "echo").with_runtime(spigot);
        let request = Request::builder()
            .method("POST")
            .path("/v1/chat")
            .body("ignored")
            .build()
            .expect("request");
        let response = SendPipe::call(&recorder, request).await.expect("call");
        let body = response.collect_body().await.expect("collect");
        assert_eq!(&body[..], b"recorded-body");

        recorder.flush().await.expect("flush");
        let collected = read_recorded_events(&path).await;
        let chunk_metadata = collected
            .iter()
            .find_map(|event| match &event.event {
                ProtocolEvent::Http(HttpEvent::ResponseChunk { metadata, .. })
                    if !metadata.is_empty() =>
                {
                    Some(metadata)
                }
                _ => None,
            })
            .expect("response chunk with metadata must be recorded");
        let recorded = chunk_metadata
            .get("clock_at_call")
            .expect("clock_at_call key present");
        assert_eq!(
            recorded.as_ref(),
            &0x0123_4567_89AB_CDEF_u64.to_be_bytes(),
            "recorded entropy must match what the Handler attached",
        );
    }

    #[proxima::test]
    async fn record_factory_missing_sink_returns_config_error() {
        let upstreams = build_pipe_factory_registry();
        let factory = RecordPipeFactory::new(Arc::downgrade(&upstreams), armed_spigot());
        let spec = serde_json::json!({
            "inner": { "synth": { "status": 200, "body": "x" } },
        });
        let outcome = factory.build(&spec, None).await;
        assert!(matches!(outcome, Err(ProximaError::Config(_))));
    }

    #[proxima::test]
    async fn record_factory_missing_inner_returns_config_error() {
        let upstreams = build_pipe_factory_registry();
        let factory = RecordPipeFactory::new(Arc::downgrade(&upstreams), armed_spigot());
        let spec = serde_json::json!({
            "sink": { "type": "jsonl", "path": "/tmp/x.jsonl" },
        });
        let outcome = factory.build(&spec, None).await;
        assert!(matches!(outcome, Err(ProximaError::Config(_))));
    }

    #[proxima::test]
    async fn record_upstream_directly_constructed_works_without_factory() {
        let dir = tempdir().expect("tempdir");
        let path = dir.path().join("direct.jsonl");
        let durable = Arc::new(LazyFanOut::new(
            vec![SinkSpec::new(path.to_string_lossy(), FormatKind::Json)],
            armed_spigot(),
        ));
        let sink: DynRecordingSink = Arc::new(AccumulatingSink::with_defaults(durable));
        let inner_factory = SynthPipeFactory;
        let inner = inner_factory
            .build(&serde_json::json!({ "status": 200, "body": "ok" }), None)
            .await
            .expect("inner");
        let upstream = RecordUpstream::new("rec", inner, sink.clone(), "rec");
        let request = Request::builder()
            .method("GET")
            .path("/")
            .build()
            .expect("request");
        let response = SendPipe::call(&upstream, request).await.expect("call");
        assert_eq!(response.status, 200);
        // drain response body so chunk events are emitted before flush.
        let _ = response.collect_body().await.expect("collect");
        for _ in 0..16 {
            proxima_primitives::sync::task::yield_now().await;
        }
        sink.flush().await.expect("flush");
    }

    #[proxima::test]
    async fn armed_spigot_spawns_drainer_once_across_calls() {
        let dir = tempdir().expect("tempdir");
        let path = dir.path().join("armed.jsonl");
        let spigot = armed_spigot();
        let durable = Arc::new(LazyFanOut::new(
            vec![SinkSpec::new(path.to_string_lossy(), FormatKind::Json)],
            spigot.clone(),
        ));
        let sink: DynRecordingSink = Arc::new(AccumulatingSink::with_defaults(durable));
        let inner_factory = SynthPipeFactory;
        let inner = inner_factory
            .build(&serde_json::json!({ "status": 200, "body": "ok" }), None)
            .await
            .expect("inner");
        let upstream =
            RecordUpstream::new("rec-armed", inner, sink.clone(), "rec").with_runtime(spigot);

        for _ in 0..3 {
            let request = Request::builder()
                .method("GET")
                .path("/")
                .build()
                .expect("request");
            let response = SendPipe::call(&upstream, request).await.expect("call");
            assert_eq!(response.status, 200);
            let _ = response.collect_body().await.expect("collect");
        }

        upstream.flush().await.expect("flush");
        let collected = read_recorded_events(&path).await;
        let ended_count = collected
            .iter()
            .filter(|event| matches!(event.event, ProtocolEvent::Http(HttpEvent::Ended { .. })))
            .count();
        assert_eq!(
            ended_count, 3,
            "all three calls recorded through the single armed drainer"
        );
    }

    // ── single-connection determinism proof ───────────────────────────────
    //
    // One connection, one request/response, through the FULL listener
    // stack (in-memory duplex socket -> h1 codec -> `RecordUpstream` tee ->
    // handler -> response) on a virtual clock and a seeded RNG. Runs the
    // identical scenario `DETERMINISM_RUNS` times and asserts the recorded
    // JSONL trace is byte-identical every time.
    //
    // Scope: single connection, single request/response — proves neither
    // multi-connection ordering (kernel-determined reactor readiness, a
    // separate months-scale component) nor prime's reactor timers (this
    // scenario never awaits one: the h1 driver runs under a bare
    // `futures::executor::block_on`, no runtime installed, and the only
    // "clock" consulted anywhere on this path is `RecordUpstream`'s
    // injected `RecordingClock`).
    #[cfg(feature = "http1-native")]
    mod determinism {
        use super::*;
        use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
        use proxima_primitives::pipe::clock::testing::RecordingClock;
        use std::collections::VecDeque;
        use std::pin::Pin;
        use std::task::{Context, Poll, Waker};
        use time::macros::datetime;

        const DETERMINISM_RUNS: usize = 500;
        const FIXED_SEED: u64 = 0xC0FF_EE99_DEAD_BEEF;
        const REQUEST_WIRE: &[u8] = b"POST /echo HTTP/1.1\r\nHost: determinism.local\r\nContent-Length: 13\r\nConnection: close\r\n\r\nhello, proxima";

        // fixed wall-clock literal the deterministic `RecordingClock` is
        // anchored to (see `RecordUpstream::with_clock`'s `wall_epoch` /
        // `epoch_nanos` pair) — arbitrary but constant across every run.
        fn fixed_wall_epoch() -> OffsetDateTime {
            datetime!(2024-01-01 00:00:00 UTC)
        }

        // echoes the request body back as the response body — exercises the
        // request-chunk AND response-chunk recording paths, not just a bare
        // status/headers round trip.
        struct EchoPipe;

        impl SendPipe for EchoPipe {
            type In = Request<Bytes>;
            type Out = Response<Bytes>;
            type Err = ProximaError;

            fn call(
                &self,
                request: Request<Bytes>,
            ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
                async move {
                    let (_, body) = request.body_bytes().await?;
                    Ok(Response::new(200)
                        .with_body(body)
                        .with_header("content-type", "text/plain"))
                }
            }
        }

        /// Byte queue shared by one direction of [`test_duplex`]. Mirrors
        /// `proxima-http/src/http1/serve.rs`'s private test-only duplex —
        /// `futures` (unlike `tokio`) ships no `io::duplex`, and that one is
        /// `#[cfg(test)]`-private to a different crate, so this integration
        /// test needs its own copy of the same minimal scaffolding.
        #[derive(Default)]
        struct DuplexBuf {
            bytes: VecDeque<u8>,
            closed: bool,
            waker: Option<Waker>,
        }

        struct DuplexHalf {
            read_buf: Arc<std::sync::Mutex<DuplexBuf>>,
            write_buf: Arc<std::sync::Mutex<DuplexBuf>>,
        }

        fn test_duplex() -> (DuplexHalf, DuplexHalf) {
            let a_to_b = Arc::new(std::sync::Mutex::new(DuplexBuf::default()));
            let b_to_a = Arc::new(std::sync::Mutex::new(DuplexBuf::default()));
            (
                DuplexHalf {
                    read_buf: b_to_a.clone(),
                    write_buf: a_to_b.clone(),
                },
                DuplexHalf {
                    read_buf: a_to_b,
                    write_buf: b_to_a,
                },
            )
        }

        impl AsyncRead for DuplexHalf {
            fn poll_read(
                self: Pin<&mut Self>,
                cx: &mut Context<'_>,
                buf: &mut [u8],
            ) -> Poll<std::io::Result<usize>> {
                let mut guard = self.read_buf.lock().unwrap();
                if guard.bytes.is_empty() {
                    if guard.closed {
                        return Poll::Ready(Ok(0));
                    }
                    guard.waker = Some(cx.waker().clone());
                    return Poll::Pending;
                }
                let take = guard.bytes.len().min(buf.len());
                for slot in buf.iter_mut().take(take) {
                    *slot = guard.bytes.pop_front().expect("checked len above");
                }
                Poll::Ready(Ok(take))
            }
        }

        impl AsyncWrite for DuplexHalf {
            fn poll_write(
                self: Pin<&mut Self>,
                _cx: &mut Context<'_>,
                buf: &[u8],
            ) -> Poll<std::io::Result<usize>> {
                let mut guard = self.write_buf.lock().unwrap();
                guard.bytes.extend(buf.iter().copied());
                if let Some(waker) = guard.waker.take() {
                    waker.wake();
                }
                Poll::Ready(Ok(buf.len()))
            }

            fn poll_flush(
                self: Pin<&mut Self>,
                _cx: &mut Context<'_>,
            ) -> Poll<std::io::Result<()>> {
                Poll::Ready(Ok(()))
            }

            fn poll_close(
                self: Pin<&mut Self>,
                _cx: &mut Context<'_>,
            ) -> Poll<std::io::Result<()>> {
                let mut guard = self.write_buf.lock().unwrap();
                guard.closed = true;
                if let Some(waker) = guard.waker.take() {
                    waker.wake();
                }
                Poll::Ready(Ok(()))
            }
        }

        impl Drop for DuplexHalf {
            fn drop(&mut self) {
                let mut guard = self.write_buf.lock().unwrap();
                guard.closed = true;
                if let Some(waker) = guard.waker.take() {
                    waker.wake();
                }
            }
        }

        // one full pass of the scenario: fresh tempdir, fresh `RecordingClock`
        // pinned to nanos=0, fresh armed spigot (real background pool, no
        // per-call `std::thread::spawn` fallback), one connection, one
        // request/response, then the raw recorded JSONL bytes off disk.
        fn run_once() -> Vec<u8> {
            let dir = tempdir().expect("tempdir");
            let path = dir.path().join("trace.jsonl");
            let spigot = armed_spigot();
            let durable = Arc::new(LazyFanOut::new(
                vec![SinkSpec::new(path.to_string_lossy(), FormatKind::Json)],
                spigot.clone(),
            ));
            let sink: DynRecordingSink = Arc::new(AccumulatingSink::with_defaults(durable));
            let inner: PipeHandle = into_handle(EchoPipe);
            let clock = RecordingClock::new();
            let concrete = Arc::new(
                RecordUpstream::with_clock(
                    "determinism",
                    inner,
                    sink,
                    "determinism",
                    clock,
                    fixed_wall_epoch(),
                    FIXED_SEED,
                )
                .with_runtime(spigot),
            );
            let dispatch: PipeHandle = into_handle(concrete.clone());

            let (server_half, mut client_half) = test_duplex();
            let server = crate::serve_h1_connection(server_half, dispatch, None, None);
            let client = async move {
                client_half
                    .write_all(REQUEST_WIRE)
                    .await
                    .expect("client write");
                let mut response = Vec::new();
                client_half
                    .read_to_end(&mut response)
                    .await
                    .expect("client read");
                response
            };
            let (server_result, response) =
                futures::executor::block_on(futures::future::join(server, client));
            server_result.expect("serve_h1_connection should complete");
            assert!(
                String::from_utf8_lossy(&response).starts_with("HTTP/1.1 200"),
                "sanity: the scenario must actually produce a 200 response"
            );

            futures::executor::block_on(concrete.flush()).expect("flush");
            std::fs::read(&path).expect("read recorded trace")
        }

        // first index at which two byte buffers differ (or the length of the
        // shorter one, if one is a prefix of the other) — the pinpoint this
        // proof needs to name a real diverging leak instead of just failing.
        fn first_divergence(expected: &[u8], actual: &[u8]) -> Option<usize> {
            if expected == actual {
                return None;
            }
            let shared = expected.len().min(actual.len());
            (0..shared)
                .find(|&index| expected[index] != actual[index])
                .or(Some(shared))
        }

        #[test]
        fn single_connection_trace_is_byte_identical_across_n_runs() {
            let baseline = run_once();
            assert!(!baseline.is_empty(), "recorded trace must not be empty");

            for run_index in 1..DETERMINISM_RUNS {
                let candidate = run_once();
                if let Some(offset) = first_divergence(&baseline, &candidate) {
                    let window = 32;
                    let baseline_start = offset.saturating_sub(window);
                    let candidate_start = offset.saturating_sub(window);
                    let baseline_snippet = String::from_utf8_lossy(
                        &baseline[baseline_start..(offset + window).min(baseline.len())],
                    );
                    let candidate_snippet = String::from_utf8_lossy(
                        &candidate[candidate_start..(offset + window).min(candidate.len())],
                    );
                    panic!(
                        "run {run_index} diverged from run 0 at byte {offset}\n  \
                         run 0:  ...{baseline_snippet}...\n  \
                         run {run_index}: ...{candidate_snippet}..."
                    );
                }
            }
        }

        // ── multi-connection interleaving proof ────────────────────────────
        //
        // `DETERMINISM_RUNS` single connections above prove the h1 + record
        // path is deterministic in isolation. This extends the SAME harness
        // to N concurrent connections sharing one `RecordUpstream` (one
        // trace), all driven by a single `futures::executor::block_on` call
        // — no epoll/kqueue, no OS thread hand-off for the connections
        // themselves (the recording drainer's background thread only ever
        // DEQUEUES; every `sender.send` that decides record ORDER happens on
        // this one foreground thread). If the combined trace is still
        // byte-identical across runs and processes, the only ordering input
        // the single-connection proof couldn't exercise — the executor's own
        // ready-queue, deciding which connection's next step runs first — is
        // deterministic too.
        #[cfg(feature = "http-prime-deps")]
        mod multi_connection {
            use super::*;
            use proxima_primitives::stream::{
                ConnectFuture, PeerInfo, StreamConnection, StreamUpstream,
            };

            const MULTI_CONN_RUNS: usize = 300;

            // one-shot transport: hands back the pre-built duplex half
            // exactly once, matching `H1ClientUpstream`'s own keep-alive
            // contract (it only ever calls `connect()` again after an
            // error, which this scenario never produces).
            struct SingleConnUpstream {
                conn: std::sync::Mutex<Option<DuplexHalf>>,
            }

            impl SingleConnUpstream {
                fn new(conn: DuplexHalf) -> Self {
                    Self {
                        conn: std::sync::Mutex::new(Some(conn)),
                    }
                }
            }

            impl StreamConnection for DuplexHalf {
                fn peer(&self) -> Option<PeerInfo> {
                    None
                }
            }

            impl StreamUpstream for SingleConnUpstream {
                type Conn = DuplexHalf;

                fn connect_future(&self) -> ConnectFuture<'_, Self::Conn> {
                    Box::pin(async move {
                        let mut guard = self
                            .conn
                            .lock()
                            .unwrap_or_else(std::sync::PoisonError::into_inner);
                        guard.take().ok_or_else(|| {
                            std::io::Error::other(
                                "single-connection upstream already connected once",
                            )
                        })
                    })
                }
            }

            // deliberately uneven: connection 0 sends 1 request, connection
            // 1 sends 2, ... wrapping every 4 connections, so a run mixes
            // short- and long-lived connections instead of N identical ones.
            fn request_count_for(connection_index: usize) -> usize {
                1 + (connection_index % 4)
            }

            // varies 40..=290 bytes as a function of (connection, request)
            // — no two requests in a run share a size by coincidence.
            fn byte_count_for(connection_index: usize, request_index: usize) -> usize {
                40 + (connection_index * 31 + request_index * 17) % 251
            }

            // a readable, real-sentence payload padded/truncated to
            // `byte_count` — not `b"AAAA"`: the point is to prove ordering,
            // and a legible trace is easier to eyeball if this ever fails.
            fn scripted_body(
                connection_index: usize,
                request_index: usize,
                byte_count: usize,
            ) -> Bytes {
                let marker = format!("conn={connection_index} req={request_index} ");
                let filler = "the quick brown fox jumps over the lazy dog; ";
                let mut body = marker.into_bytes();
                while body.len() < byte_count {
                    body.extend_from_slice(filler.as_bytes());
                }
                body.truncate(byte_count);
                Bytes::from(body)
            }

            // drives one connection's whole request script, in program
            // order, over its own keep-alive client. The interleaving
            // ACROSS connections comes from running N of these concurrently
            // (see `run_multi_once`), never from anything inside this fn.
            async fn drive_client_connection(
                client: crate::H1ClientUpstream<SingleConnUpstream>,
                connection_index: usize,
                request_count: usize,
            ) {
                for request_index in 0..request_count {
                    let byte_count = byte_count_for(connection_index, request_index);
                    let body = scripted_body(connection_index, request_index, byte_count);
                    let request = Request::builder()
                        .method("POST")
                        .path(format!("/echo/conn-{connection_index}/req-{request_index}"))
                        .body(body.clone())
                        .build()
                        .expect("request");
                    let response = SendPipe::call(&client, request).await.expect("client call");
                    assert_eq!(response.status, 200);
                    let echoed = response.collect_body().await.expect("collect body");
                    assert_eq!(
                        echoed, body,
                        "echo pipe must return the exact request body for conn {connection_index} req {request_index}"
                    );
                }
            }

            // one full pass: `connection_count` connections sharing ONE
            // `RecordUpstream` (one trace), driven concurrently by a single
            // `futures::executor::block_on` call. Returns the raw recorded
            // bytes (for the byte-identical comparison) alongside the
            // parsed events (for the interleaving check) — both read back
            // before the tempdir drops.
            fn run_multi_once(connection_count: usize) -> (Vec<u8>, Vec<RecordingEvent>) {
                let dir = tempdir().expect("tempdir");
                let path = dir.path().join("trace.jsonl");
                let spigot = armed_spigot();
                let durable = Arc::new(LazyFanOut::new(
                    vec![SinkSpec::new(path.to_string_lossy(), FormatKind::Json)],
                    spigot.clone(),
                ));
                let sink: DynRecordingSink = Arc::new(AccumulatingSink::with_defaults(durable));
                let inner: PipeHandle = into_handle(EchoPipe);
                let clock = RecordingClock::new();
                let concrete = Arc::new(
                    RecordUpstream::with_clock(
                        "determinism-multi",
                        inner,
                        sink,
                        "determinism-multi",
                        clock,
                        fixed_wall_epoch(),
                        FIXED_SEED,
                    )
                    .with_runtime(spigot),
                );
                let dispatch: PipeHandle = into_handle(concrete.clone());

                let mut servers = Vec::with_capacity(connection_count);
                let mut clients = Vec::with_capacity(connection_count);
                for connection_index in 0..connection_count {
                    let (server_half, client_half) = test_duplex();
                    servers.push(crate::serve_h1_connection(
                        server_half,
                        dispatch.clone(),
                        None,
                        None,
                    ));
                    let client = crate::H1ClientUpstream::new(
                        SingleConnUpstream::new(client_half),
                        "determinism.local",
                        format!("conn-{connection_index}"),
                    );
                    clients.push(drive_client_connection(
                        client,
                        connection_index,
                        request_count_for(connection_index),
                    ));
                }

                let (server_results, _client_results) =
                    futures::executor::block_on(futures::future::join(
                        futures::future::join_all(servers),
                        futures::future::join_all(clients),
                    ));
                for result in server_results {
                    result.expect("serve_h1_connection should complete");
                }

                futures::executor::block_on(concrete.flush()).expect("flush");
                let bytes = std::fs::read(&path).expect("read recorded trace");
                let events = futures::executor::block_on(read_recorded_events(&path));
                (bytes, events)
            }

            fn connection_index_from_path(path: &str) -> usize {
                path.strip_prefix("/echo/conn-")
                    .and_then(|rest| rest.split('/').next())
                    .and_then(|value| value.parse::<usize>().ok())
                    .unwrap_or_else(|| {
                        panic!("recorded path did not encode a connection index: {path}")
                    })
            }

            // the connection each `Started` event belongs to, in the order
            // they were recorded — the sequence genuine interleaving must
            // NOT reduce to N contiguous blocks.
            fn started_connection_sequence(events: &[RecordingEvent]) -> Vec<usize> {
                events
                    .iter()
                    .filter_map(|event| match &event.event {
                        ProtocolEvent::Http(HttpEvent::Started { request, .. }) => {
                            Some(connection_index_from_path(&request.path))
                        }
                        _ => None,
                    })
                    .collect()
            }

            // number of maximal contiguous same-value runs. A fully
            // sequential schedule (connection 0 draining entirely, then
            // connection 1, ...) produces exactly `connection_count` runs;
            // genuine interleaving produces strictly more, smaller ones.
            fn count_runs(sequence: &[usize]) -> usize {
                if sequence.is_empty() {
                    return 0;
                }
                1 + sequence
                    .windows(2)
                    .filter(|pair| pair[0] != pair[1])
                    .count()
            }

            fn assert_multi_connection_determinism(connection_count: usize) {
                let (baseline, baseline_events) = run_multi_once(connection_count);
                assert!(!baseline.is_empty(), "recorded trace must not be empty");

                let sequence = started_connection_sequence(&baseline_events);
                let observed_runs = count_runs(&sequence);
                assert!(
                    observed_runs > connection_count,
                    "connections did not genuinely interleave: {connection_count} connections \
                     produced only {observed_runs} contiguous per-connection run(s) of Started \
                     events (a fully-sequential schedule -- connection 0 draining completely \
                     before connection 1 starts -- would itself produce exactly \
                     {connection_count}); recorded sequence was {sequence:?}"
                );

                for run_index in 1..MULTI_CONN_RUNS {
                    let (candidate, _candidate_events) = run_multi_once(connection_count);
                    if let Some(offset) = first_divergence(&baseline, &candidate) {
                        let window = 32;
                        let baseline_start = offset.saturating_sub(window);
                        let candidate_start = offset.saturating_sub(window);
                        let baseline_snippet = String::from_utf8_lossy(
                            &baseline[baseline_start..(offset + window).min(baseline.len())],
                        );
                        let candidate_snippet = String::from_utf8_lossy(
                            &candidate[candidate_start..(offset + window).min(candidate.len())],
                        );
                        panic!(
                            "run {run_index} diverged from run 0 at byte {offset} ({connection_count} connections)\n  \
                             run 0:  ...{baseline_snippet}...\n  \
                             run {run_index}: ...{candidate_snippet}..."
                        );
                    }
                }
            }

            #[test]
            fn multi_connection_trace_is_byte_identical_across_n_runs_four_connections() {
                assert_multi_connection_determinism(4);
            }

            #[test]
            fn multi_connection_trace_is_byte_identical_across_n_runs_sixteen_connections() {
                assert_multi_connection_determinism(16);
            }
        }
    }
}