alkhttp 0.5.0

HTTP interface for the alk stack: serves HTTP/1.1 + HTTP/2 on standard ALPNs (with WebSocket upgrade carrying the channels protocol) and hosts the HTTP-backed call-protocol adapters
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
//! `HttpAdapter` — `ProtocolHandler` for `h2`/`http/1.1` (axum over a
//! `BiStream`).
//!
//! Wires the axum `Router` (gateway endpoints + `/healthz` +
//! `/openapi.json` + MCP + custom routes + decoy fallback) and drives
//! hyper's HTTP/1.1 or HTTP/2 connection driver over a single
//! bidirectional stream yielded by `Connection::accept_bi()`. The WS
//! upgrade route lands with the websocket subsystem; until then the
//! router reserves `/alk/channels` for it.
//!
//! ## Reserved paths (ADR-046 §3)
//!
//! [`RESERVED_PATHS`] is enforced per-method at `build_router` time: a
//! custom route that registers *any* method on a reserved path panics
//! at construction. The default surface already registers its own
//! methods on these paths, so axum's merge would catch the same-method
//! case anyway; the pre-merge probe extends the guarantee to the
//! per-method case (e.g. a custom `POST /search` next to the default
//! `GET /search`), where a silent merge would otherwise serve a custom
//! handler on a reserved path and break the "default surface wins"
//! rule. Same-path-different-method merges outside the reserved set
//! remain legal (axum composes the `MethodRouter`).
//!
//! ## Connection knobs and boundaries
//!
//! The accept-loop side (`serve_io`, crate-private) configures the hyper auto builder with a
//! tokio timer and timeouts (values in the method doc). The **concurrency
//! cap is not a knob of this crate**: the `ProtocolHandler::handle` trait method serves
//! exactly one accepted bidirectional stream per call, and the accept
//! loop — how many streams are handled concurrently, on which tasks —
//! belongs to the consumer that owns the `Connection` (or the
//! assembly-layer listener). Timeout/cap interaction: a request can
//! hold its connection for the whole handler runtime; only the header
//! phase and idle keep-alive windows are bounded here.

use std::sync::Arc;
use std::time::Duration;

use alkcall::core::auth::AuthContext;
use alkcall::core::types::{Connection, HandlerError, StreamError};
use alkcall::registry::registration::OperationRegistry;
use async_trait::async_trait;
use axum::routing::get;
use axum::Router;
use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
use hyper_util::server::conn::auto::Builder as HyperBuilder;
use hyper_util::service::TowerToHyperService;
use parking_lot::Mutex;
use tracing::error;

use super::auth::bearer_auth_middleware;
use super::decoy::{decoy_fallback, decoy_method_not_allowed};
use super::healthz::healthz;
use super::state::{DecoyConfig, RouterState};

/// The HTTP/1.1 ALPN (`http/1.1`) `HttpAdapter` registers on (ADR-001).
pub const ALPN_HTTP1: &[u8] = b"http/1.1";
/// The HTTP/2 ALPN (`h2`) `HttpAdapter` registers on (ADR-001).
pub const ALPN_H2: &[u8] = b"h2";

/// The WS upgrade path (ADR-067). Reserved in the default surface; the
/// handler is wired by the websocket subsystem task.
pub const WS_UPGRADE_PATH: &str = "/alk/channels";

/// Reserved default-surface paths (ADR-046 collision rule). Custom
/// routes must not register any method on these paths — the default
/// surface owns them. Enforced per-method in `build_router` (a
/// same-method collision would already panic in axum's merge; the
/// pre-merge probe also covers different-method registrations, which
/// would otherwise silently merge in).
pub const RESERVED_PATHS: &[&str] = &[
    "/search",
    "/schema",
    "/call",
    "/batch",
    "/subscribe",
    "/publish",
    "/healthz",
    "/openapi.json",
    "/mcp",
    WS_UPGRADE_PATH,
];

/// The HTTP server host (ADR-001, ADR-002, ADR-039): an axum
/// `Router` serving the gateway, `/healthz`, `/openapi.json`, the MCP
/// route, the WS upgrade path, and assembly-registered custom routes,
/// behind the bearer-auth middleware — served over one ALPN depending
/// on the constructor.
pub struct HttpAdapter {
    identity_provider: Arc<dyn alkcall::core::auth::IdentityProvider>,
    registry: Arc<OperationRegistry>,
    decoy: DecoyConfig,
    extra_routes: Option<Router>,
    alpn: &'static [u8],
    router: Router,
    openapi_doc: CachedOpenAPIDoc,
    ws_sessions: Arc<crate::websocket::WsSessions>,
    ws_max_sessions: usize,
    ws_session_slots: Arc<tokio::sync::Semaphore>,
    ws_idle_timeout: Option<Duration>,
    ws_openable_alpns: Option<Arc<[crate::websocket::OpenableAlpn]>>,
    ws_op_register_acl: alkcall::registry::spec::AccessControl,
}

impl HttpAdapter {
    /// An HTTP/1.1 adapter (registers on `http/1.1` ALPN).
    pub fn new(
        identity_provider: Arc<dyn alkcall::core::auth::IdentityProvider>,
        registry: Arc<OperationRegistry>,
    ) -> Self {
        Self::for_alpn(identity_provider, registry, ALPN_HTTP1)
    }

    /// An HTTP/2 adapter (registers on `h2` ALPN).
    pub fn h2(
        identity_provider: Arc<dyn alkcall::core::auth::IdentityProvider>,
        registry: Arc<OperationRegistry>,
    ) -> Self {
        Self::for_alpn(identity_provider, registry, ALPN_H2)
    }

    fn for_alpn(
        identity_provider: Arc<dyn alkcall::core::auth::IdentityProvider>,
        registry: Arc<OperationRegistry>,
        alpn: &'static [u8],
    ) -> Self {
        let decoy = DecoyConfig::default();
        let openapi_doc = CachedOpenAPIDoc::new(&registry);
        let ws_sessions = Arc::new(crate::websocket::WsSessions::new());
        let ws_max_sessions = crate::websocket::DEFAULT_WS_MAX_SESSIONS;
        let ws_session_slots = Arc::new(tokio::sync::Semaphore::new(ws_max_sessions));
        let ws_idle_timeout = Some(crate::websocket::DEFAULT_WS_IDLE_TIMEOUT);
        let state = RouterState {
            registry: Arc::clone(&registry),
            identity_provider: Arc::clone(&identity_provider),
            decoy: decoy.clone(),
            openapi_doc: openapi_doc.clone(),
            ws_sessions: Arc::clone(&ws_sessions),
            ws_session_slots: Arc::clone(&ws_session_slots),
            ws_idle_timeout,
            ws_openable_alpns: None,
            ws_op_register_acl: alkcall::registry::spec::AccessControl::default(),
        };
        let router = build_router(state, None);
        Self {
            identity_provider,
            registry,
            decoy,
            extra_routes: None,
            alpn,
            router,
            openapi_doc,
            ws_sessions,
            ws_max_sessions,
            ws_session_slots,
            ws_idle_timeout,
            ws_openable_alpns: None,
            ws_op_register_acl: alkcall::registry::spec::AccessControl::default(),
        }
    }

    /// Set the decoy surface for unregistered paths and rebuild the
    /// router (custom routes are preserved — SRV-05).
    pub fn with_decoy(mut self, decoy: DecoyConfig) -> Self {
        self.decoy = decoy.clone();
        let state = RouterState {
            registry: Arc::clone(&self.registry),
            identity_provider: Arc::clone(&self.identity_provider),
            decoy,
            openapi_doc: self.openapi_doc.clone(),
            ws_sessions: Arc::clone(&self.ws_sessions),
            ws_session_slots: Arc::clone(&self.ws_session_slots),
            ws_idle_timeout: self.ws_idle_timeout,
            ws_openable_alpns: self.ws_openable_alpns.clone(),
            ws_op_register_acl: self.ws_op_register_acl.clone(),
        };
        // `extra_routes` is borrowed, not consumed (SRV-05): a builder
        // call after `with_extra_routes` must keep the custom routes in
        // the rebuild — `with_extra_routes` stores a verified clone, so
        // re-merging it is safe.
        self.router = build_router(state, self.extra_routes.clone());
        self
    }

    /// Mount assembly-provided custom routes under the bearer-auth
    /// middleware (ADR-046) and rebuild the router. Reserved paths
    /// (`RESERVED_PATHS`) are rejected before the merge.
    pub fn with_extra_routes(mut self, routes: Router) -> Self {
        let state = RouterState {
            registry: Arc::clone(&self.registry),
            identity_provider: Arc::clone(&self.identity_provider),
            decoy: self.decoy.clone(),
            openapi_doc: self.openapi_doc.clone(),
            ws_sessions: Arc::clone(&self.ws_sessions),
            ws_session_slots: Arc::clone(&self.ws_session_slots),
            ws_idle_timeout: self.ws_idle_timeout,
            ws_openable_alpns: self.ws_openable_alpns.clone(),
            ws_op_register_acl: self.ws_op_register_acl.clone(),
        };
        self.router = build_router(state, Some(routes.clone()));
        self.extra_routes = Some(routes);
        self
    }

    /// The concurrent WS session cap (WS-09): the upgrade handler
    /// acquires one semaphore permit per upgrade, post-auth and
    /// pre-upgrade; a caller over the configured cap is rejected with
    /// **503 Service Unavailable**, and the permit is held for the
    /// session's lifetime (an ended session frees its slot).
    ///
    /// Default: [`crate::websocket::DEFAULT_WS_MAX_SESSIONS`] (64).
    pub fn with_ws_max_sessions(mut self, max_sessions: usize) -> Self {
        self.ws_max_sessions = max_sessions;
        let state = RouterState {
            registry: Arc::clone(&self.registry),
            identity_provider: Arc::clone(&self.identity_provider),
            decoy: self.decoy.clone(),
            openapi_doc: self.openapi_doc.clone(),
            ws_sessions: Arc::clone(&self.ws_sessions),
            ws_session_slots: Self::rebuild_session_slots(max_sessions),
            ws_idle_timeout: self.ws_idle_timeout,
            ws_openable_alpns: self.ws_openable_alpns.clone(),
            ws_op_register_acl: self.ws_op_register_acl.clone(),
        };
        self.router = build_router(state, self.extra_routes.clone());
        self
    }

    /// The WS idle-read timeout (WS-01, WS-13 semantics): the read
    /// pump closes the connection with a 1001 (GoingAway) close frame
    /// after this long producing **no completed inbound chunk** — the
    /// deadline resets on demux progress (complete chunks forwarded
    /// into the byte stream), not on WS message arrival, so a
    /// dribbling peer (slow message arrivals inside a declared chunk)
    /// is bounded while productive-but-slow peers survive. This is an
    /// intentional no-progress eviction line, not a transport-idle
    /// bound: there is no WS ping/pong keepalive, and app-silence that
    /// outlasts the window (a quiet subscription) is evicted by design
    /// — see `websocket::byte_adapter`'s module doc. `None` disables
    /// the knob (not recommended: the stall window is then unbounded;
    /// long-lived silent subscriptions are the intended `None` case,
    /// leaning on `WsSessions::abort` and the write-side caps).
    ///
    /// Default: [`crate::websocket::DEFAULT_WS_IDLE_TIMEOUT`] (60 s).
    pub fn with_ws_idle_timeout(mut self, idle_timeout: Option<Duration>) -> Self {
        self.ws_idle_timeout = idle_timeout;
        let state = RouterState {
            registry: Arc::clone(&self.registry),
            identity_provider: Arc::clone(&self.identity_provider),
            decoy: self.decoy.clone(),
            openapi_doc: self.openapi_doc.clone(),
            ws_sessions: Arc::clone(&self.ws_sessions),
            ws_session_slots: Arc::clone(&self.ws_session_slots),
            ws_idle_timeout: self.ws_idle_timeout,
            ws_openable_alpns: self.ws_openable_alpns.clone(),
            ws_op_register_acl: self.ws_op_register_acl.clone(),
        };
        self.router = build_router(state, self.extra_routes.clone());
        self
    }

    /// The shared WS session registry (WS-08): live sessions'
    /// [`WsPumps`](crate::websocket::WsPumps) handles, evictable via
    /// `WsSessions::abort`. The upgrade handler registers against this
    /// instance.
    pub fn ws_sessions(&self) -> Arc<crate::websocket::WsSessions> {
        Arc::clone(&self.ws_sessions)
    }

    /// The openable-ALPN set for WS sessions (WS-22, review 006
    /// Unit 2): one [`OpenableAlpn`](crate::websocket::OpenableAlpn)
    /// per openable data-channel ALPN — the open-op spec (with the
    /// `channel_open` marker), the ALPN-specific
    /// [`OpenHandler`](alkcall::channels::operations::OpenHandler),
    /// and the optional establisher + per-registration timeout
    /// (ADR-049, threaded through with `None` defaults). With an
    /// establisher attached, its `Establishment.plan` (alkcall 0.6.0 /
    /// ADR-049 amendment 2) is threaded process-locally to the pump
    /// handler's second parameter. Each WS
    /// session's per-session fork registers the set (plus the generic
    /// channel ops, bootstrap discovery, and `op/register`), so a WS
    /// client can open data channels exactly as any channels consumer
    /// (ADR-067: the browser reaches what a Rust consumer on an
    /// in-line connection would reach).
    ///
    /// The ALPN-specific `OpenHandler` implementations stay in the
    /// ALPN crates (alktty et al.); this adapter only ferries the
    /// registrations onto the session fork. Bare-registry / custom
    /// upgrade routes pass their own set via the
    /// [`OpenableAlpns`](crate::websocket::OpenableAlpns) request
    /// extension instead.
    ///
    /// Default: no openables (channel 0 only — the pre-Unit-2 shape).
    pub fn with_ws_openable_alpns(
        mut self,
        openables: Vec<crate::websocket::OpenableAlpn>,
    ) -> Self {
        self.ws_openable_alpns = Some(openables.into());
        let state = RouterState {
            registry: Arc::clone(&self.registry),
            identity_provider: Arc::clone(&self.identity_provider),
            decoy: self.decoy.clone(),
            openapi_doc: self.openapi_doc.clone(),
            ws_sessions: Arc::clone(&self.ws_sessions),
            ws_session_slots: Arc::clone(&self.ws_session_slots),
            ws_idle_timeout: self.ws_idle_timeout,
            ws_openable_alpns: self.ws_openable_alpns.clone(),
            ws_op_register_acl: self.ws_op_register_acl.clone(),
        };
        self.router = build_router(state, self.extra_routes.clone());
        self
    }

    /// The `op/register` surface's `AccessControl` for WS sessions
    /// (review 007 WS-29, implementing review-006 UP-02's recorded
    /// posture): the per-session announce op is registered with this
    /// ACL on each session's fork, so a peer whose identity does not
    /// satisfy it gets `FORBIDDEN` on the announce. This is the
    /// assembly layer's override surface for restricting which
    /// authenticated peers may announce ops on WS sessions.
    ///
    /// Default: `AccessControl::default()` — any authenticated peer
    /// may announce (the SRV-10 permissive-crate-default precedent).
    /// Bare-registry / custom upgrade routes pass their own ACL via
    /// the
    /// [`OpRegisterAcl`](crate::websocket::OpRegisterAcl) request
    /// extension instead.
    pub fn with_ws_op_register_acl(mut self, acl: alkcall::registry::spec::AccessControl) -> Self {
        self.ws_op_register_acl = acl;
        let state = RouterState {
            registry: Arc::clone(&self.registry),
            identity_provider: Arc::clone(&self.identity_provider),
            decoy: self.decoy.clone(),
            openapi_doc: self.openapi_doc.clone(),
            ws_sessions: Arc::clone(&self.ws_sessions),
            ws_session_slots: Arc::clone(&self.ws_session_slots),
            ws_idle_timeout: self.ws_idle_timeout,
            ws_openable_alpns: self.ws_openable_alpns.clone(),
            ws_op_register_acl: self.ws_op_register_acl.clone(),
        };
        self.router = build_router(state, self.extra_routes.clone());
        self
    }

    /// A fresh semaphore for the new cap; the retained handles of
    /// already-open sessions are unaffected (they hold their permits).
    fn rebuild_session_slots(max_sessions: usize) -> Arc<tokio::sync::Semaphore> {
        Arc::new(tokio::sync::Semaphore::new(max_sessions))
    }

    /// The configured decoy surface (assembly introspection).
    pub fn decoy(&self) -> &DecoyConfig {
        &self.decoy
    }

    /// The ALPN this adapter registers on (`http/1.1` or `h2`).
    pub fn alpn(&self) -> &'static [u8] {
        self.alpn
    }

    /// The assembled router (for the accept loop the consumer owns).
    pub fn router(&self) -> &Router {
        &self.router
    }
}

fn build_router(state: RouterState, extra_routes: Option<Router>) -> Router {
    let auth_state = Arc::clone(&state.identity_provider);

    #[cfg(feature = "mcp")]
    let mcp_router: Router<RouterState> = {
        let dispatch = crate::gateway::GatewayDispatch::new(Arc::clone(&state.registry));
        Router::new()
            .nest_service(
                "/mcp",
                crate::adapters::to_mcp_service(std::sync::Arc::new(dispatch)),
            )
            .layer(axum::middleware::from_fn(mcp_body_limit))
            .layer(from_fn_with_state(
                auth_state.clone(),
                bearer_auth_middleware,
            ))
    };
    #[cfg(not(feature = "mcp"))]
    let mcp_router: Router<RouterState> = Router::new();

    let default: Router<RouterState> = Router::new()
        .merge(crate::gateway::routes::gateway_router())
        // The openapi handler's state is the pre-serialized projection
        // cache (SRV-09), threaded through `RouterState` below.
        .route("/openapi.json", get(openapi_json_handler))
        .route("/healthz", get(healthz))
        .fallback(decoy_fallback)
        .method_not_allowed_fallback(decoy_method_not_allowed);

    let with_extras = match extra_routes {
        Some(extra) => {
            enforce_reserved_paths(&extra);
            let extra: Router<RouterState> = extra.with_state(());
            default.merge(extra)
        }
        None => default,
    };

    // Re-applied after the extras merge (SRV-12): the call covers only
    // the MethodRouters registered before it, so without this the extra
    // routes keep axum's bare 405 (no decoy body, no `Server: nginx`) —
    // the exact stealth probe SRV-07 neutralized for the default
    // surface. Idempotent for the routers the earlier call covered.
    let with_extras = with_extras.method_not_allowed_fallback(decoy_method_not_allowed);

    // Applied after the merges (ADR-046 §4): the bearer-auth layer wraps
    // every route registered before this call (the gateway endpoints,
    // /openapi.json, /healthz, and the extra routes) — axum 0.8
    // semantics are that `route_layer` wraps the routes registered
    // before it, not the ones after. Routes that must not pass through
    // this layer (the /mcp nest and the WS upgrade route, each with its
    // own auth) are merged/registered after it — SRV-11: before the
    // reorder, a route carrying an inner auth layer was also wrapped by
    // this one and resolved the token twice (the SRV-10 double-resolve,
    // the old comment claiming the opposite).
    let with_auth = with_extras.route_layer(from_fn_with_state(
        Arc::clone(&auth_state),
        bearer_auth_middleware,
    ));

    // Merged after the router-wide route_layer on purpose: the /mcp
    // nest carries its own bearer layer (applied around the nested
    // service, `from_fn_with_state` above), so registering it here keeps
    // exactly one token resolution per request (SRV-11). nest_service
    // registers a plain Route endpoint (no MethodRouter), so the decoy
    // 405 fallback has no interplay with this merge.
    let with_mcp = with_auth.merge(mcp_router);

    // Registered after the router-wide route_layer on purpose: axum's
    // route_layer applies only to earlier-registered routes, so the WS
    // upgrade path resolves the token exactly once through its own
    // `ws_bearer_auth` (401 without a resolvable token — a WS session
    // without an identity cannot run AccessControl::check). The
    // MethodRouter carries the decoy 405 fallback explicitly
    // (MethodRouter::route_layer wraps method endpoints, not the
    // fallback) — a wrong-method probe on /alk/channels keeps the decoy
    // shape instead of axum's bare 405.
    with_mcp
        .route(
            WS_UPGRADE_PATH,
            get(crate::websocket::ws_upgrade_handler)
                .fallback(decoy_method_not_allowed)
                .route_layer(from_fn_with_state(
                    Arc::clone(&auth_state),
                    crate::websocket::ws_bearer_auth,
                )),
        )
        .with_state(state)
}

/// Enforce the per-method reserved-path collision rule (ADR-046 §3):
/// reject extra routes that register any method on a
/// [`RESERVED_PATHS`] path by merging a probe `MethodRouter` occupied
/// on every method. The merge panics on the first collision — the same
/// panic axum raises for a same-method overlap — and is a no-op for a
/// custom router that respects the reserved set.
fn enforce_reserved_paths(extra: &Router) {
    if !extra.has_routes() {
        return;
    }
    let probe = RESERVED_PATHS.iter().fold(Router::new(), |router, path| {
        router.route(
            path,
            get(rejected_reserved_path)
                .post(rejected_reserved_path)
                .put(rejected_reserved_path)
                .patch(rejected_reserved_path)
                .delete(rejected_reserved_path)
                .head(rejected_reserved_path)
                .options(rejected_reserved_path)
                .trace(rejected_reserved_path)
                .connect(rejected_reserved_path),
        )
    });
    let _ = Router::new().merge(probe).merge(extra.clone());
}

async fn rejected_reserved_path() -> axum::response::Response {
    unreachable!("reserved-path probe handler is never called")
}

use axum::middleware::from_fn_with_state;

/// Cap the `/mcp` body at 8 MiB (feature `mcp`).
///
/// The nested rmcp `StreamableHttpService` collects the raw request body
/// itself (`expect_json` → `body.collect()`), bypassing axum's
/// extractor-based default limit: `DefaultBodyLimit` works by inserting
/// an extension that `FromRequest` extractors consult, so it has no
/// effect on a service that reads the body directly (rmcp 1.8
/// `server_side_http::expect_json` never checks it). This middleware is
/// both the cap and the status source: it wraps the body in a counting
/// stream that stops at [`MCP_BODY_LIMIT`] with an explicit error and
/// post-checks a exceedance flag to answer `413 Payload Too Large`,
/// replacing whatever the inner service answered (rmcp maps body-read
/// errors to `500`).
///
/// The limit is 8 MiB — headroom over the gateway's 2 MiB whole-body
/// default for JSON-RPC batch payloads on the MCP surface.
#[cfg(feature = "mcp")]
const MCP_BODY_LIMIT: usize = 8 * 1024 * 1024;

#[cfg(feature = "mcp")]
const MCP_BODY_LIMIT_EXCEEDED: &str = "mcp body limit exceeded";

#[cfg(feature = "mcp")]
async fn mcp_body_limit(
    req: axum::extract::Request,
    next: axum::middleware::Next,
) -> axum::response::Response {
    use axum::response::IntoResponse;

    let (parts, body) = req.into_parts();

    if let Some(len) = parts
        .headers
        .get(http::header::CONTENT_LENGTH)
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.parse::<usize>().ok())
    {
        if len > MCP_BODY_LIMIT {
            return (
                axum::http::StatusCode::PAYLOAD_TOO_LARGE,
                "Payload Too Large: /mcp body exceeds the 8 MiB limit",
            )
                .into_response();
        }
    }

    let exceeded = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let counting = CountingBody {
        inner: body.into_data_stream(),
        remaining: MCP_BODY_LIMIT,
        exceeded: Arc::clone(&exceeded),
    };

    let mut limited_req =
        axum::extract::Request::from_parts(parts, axum::body::Body::from_stream(counting));
    limited_req.extensions_mut().insert(exceeded.clone());

    let response = next.run(limited_req).await;

    if exceeded.load(std::sync::atomic::Ordering::Relaxed) {
        return (
            axum::http::StatusCode::PAYLOAD_TOO_LARGE,
            "Payload Too Large: /mcp body exceeds the 8 MiB limit",
        )
            .into_response();
    }
    response
}

#[cfg(feature = "mcp")]
struct CountingBody {
    inner: axum::body::BodyDataStream,
    remaining: usize,
    exceeded: Arc<std::sync::atomic::AtomicBool>,
}

#[cfg(feature = "mcp")]
impl futures::Stream for CountingBody {
    type Item = Result<axum::body::Bytes, std::io::Error>;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        let this = &mut *self;
        match std::pin::Pin::new(&mut this.inner).poll_next(cx) {
            std::task::Poll::Ready(Some(Ok(data))) => {
                let len = data.len();
                if len > this.remaining {
                    this.remaining = 0;
                    this.exceeded
                        .store(true, std::sync::atomic::Ordering::Relaxed);
                    return std::task::Poll::Ready(Some(Err(std::io::Error::other(
                        MCP_BODY_LIMIT_EXCEEDED,
                    ))));
                }
                this.remaining -= len;
                std::task::Poll::Ready(Some(Ok(data)))
            }
            std::task::Poll::Ready(Some(Err(e))) => {
                let _ = e;
                std::task::Poll::Ready(Some(Err(std::io::Error::other(MCP_BODY_LIMIT_EXCEEDED))))
            }
            std::task::Poll::Pending => std::task::Poll::Pending,
            std::task::Poll::Ready(None) => std::task::Poll::Ready(None),
        }
    }
}

#[async_trait]
impl alkcall::core::types::ProtocolHandler for HttpAdapter {
    fn alpn(&self) -> &'static [u8] {
        self.alpn
    }

    async fn handle(&self, connection: Connection, auth: &AuthContext) -> Result<(), HandlerError> {
        if let Some(identity) = auth.identity.clone() {
            let _ = connection.set_identity(identity);
        }

        let stream = connection
            .accept_bi()
            .await
            .map_err(stream_error_to_handler)?;
        self.serve_io(stream).await
    }
}

impl HttpAdapter {
    /// Serve one accepted bidirectional stream as a single HTTP
    /// connection.
    ///
    /// Hyper knobs (SRV-04) — hyper 1.11 *silently ignores* the
    /// header-read timeout unless a timer is set, so the timer is
    /// explicit and the values chosen are:
    ///
    /// - `header_read_timeout` = **10 s** (tighter than hyper's 30 s
    ///   default; bounds the slow-loris window where a client drips
    ///   request header bytes)
    /// - h1 `keep_alive` = **enabled** (default; normal client reuse),
    ///   with hyper's default 30 s idle header-read window applying
    ///   per-request
    /// - h2 `keep_alive_interval` = **30 s**, `keep_alive_timeout` =
    ///   **10 s** — a peer that fails to ack pings for 10 s is dropped,
    ///   so half-open h2 connections do not accumulate
    ///
    /// No concurrency cap is set here — see the module doc: the accept
    /// loop is the consumer's, and the per-connection "cap" is one
    /// stream per `handle` call.
    async fn serve_io<I>(&self, io: I) -> Result<(), HandlerError>
    where
        I: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + Unpin + 'static,
    {
        let io = TokioIo::new(io);
        let service = TowerToHyperService::new(self.router.clone());

        const HEADER_READ_TIMEOUT: Duration = Duration::from_secs(10);
        #[cfg(feature = "h2")]
        const H2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(30);
        #[cfg(feature = "h2")]
        const H2_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(10);

        #[cfg_attr(not(feature = "h2"), allow(unused_mut))]
        let mut builder = HyperBuilder::new(TokioExecutor::new());
        #[cfg(feature = "http1")]
        {
            builder
                .http1()
                .timer(TokioTimer::new())
                .keep_alive(true)
                .header_read_timeout(HEADER_READ_TIMEOUT);
        }
        #[cfg(feature = "h2")]
        {
            builder
                .http2()
                .timer(TokioTimer::new())
                .enable_connect_protocol()
                .keep_alive_interval(H2_KEEP_ALIVE_INTERVAL)
                .keep_alive_timeout(H2_KEEP_ALIVE_TIMEOUT);
        }

        let conn = builder.serve_connection_with_upgrades(io, service);
        tokio::pin!(conn);

        let result = (&mut conn).await;
        if let Err(e) = result {
            error!("http adapter: connection closed with error: {e}");
        }
        Ok(())
    }
}

fn stream_error_to_handler(e: StreamError) -> HandlerError {
    HandlerError::from(e)
}

/// Serialized-to-bytes cache of the `/openapi.json` projection
/// (SRV-09): built once per `HttpAdapter` from the registry at
/// construction instead of re-projecting + re-serializing per request.
/// The registry is defined not to mutate after assembly (the assembly
/// layer registers handlers before serving), so the doc is fixed for
/// the adapter's lifetime; a rebuild via [`HttpAdapter::with_decoy`] /
/// `with_extra_routes` re-derives it from the same registry.
///
/// Serialization happens once, here — so a serde failure surfaces at
/// construction as a tracing error and the handler answers `/openapi.json`
/// with a generic 500 (no serde internals on the wire) rather than
/// echoing an error to unauthenticated callers.
#[derive(Clone)]
pub(crate) struct CachedOpenAPIDoc {
    inner: Arc<Mutex<Option<CachedOpenAPIDocInner>>>,
}

struct CachedOpenAPIDocInner {
    bytes: axum::body::Bytes,
}

impl CachedOpenAPIDoc {
    pub(crate) fn new(registry: &OperationRegistry) -> Self {
        Self {
            inner: Arc::new(Mutex::new(None)),
        }
        .with_registry(registry)
    }

    fn with_registry(self, registry: &OperationRegistry) -> Self {
        let spec = match crate::adapters::to_openapi(registry) {
            Ok(spec) => spec,
            Err(e) => {
                error!("openapi.json projection failed; endpoint will return 500: {e}");
                return self;
            }
        };
        match serde_json::to_vec(&spec.raw) {
            Ok(bytes) => {
                *self.inner.lock() = Some(CachedOpenAPIDocInner {
                    bytes: axum::body::Bytes::from(bytes),
                });
            }
            Err(e) => {
                error!("openapi.json serialization failed; endpoint will return 500: {e}");
            }
        }
        self
    }

    fn bytes(&self) -> Option<axum::body::Bytes> {
        self.inner.lock().as_ref().map(|c| c.bytes.clone())
    }
}

/// `GET /openapi.json` — the `to_openapi` projection of the local
/// operation registry (ADR-042, ADR-045): the fixed 6-endpoint gateway
/// doc. Served under the bearer-auth route layer like every other
/// gateway endpoint. The serialized doc is cached at adapter
/// construction ([`CachedOpenAPIDoc`]); a cache miss (projection or
/// serialization failed at construction) answers with a **generic**
/// `500` body — no serde internals on the wire.
async fn openapi_json_handler(
    axum::extract::State(doc): axum::extract::State<CachedOpenAPIDoc>,
) -> axum::response::Response {
    use axum::response::IntoResponse;
    match doc.bytes() {
        Some(bytes) => ([(http::header::CONTENT_TYPE, "application/json")], bytes).into_response(),
        None => (
            axum::http::StatusCode::INTERNAL_SERVER_ERROR,
            "internal server error",
        )
            .into_response(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::server::auth::ResolvedIdentity;
    use alkcall::core::auth::IdentityProvider;
    use alkcall::core::types::ProtocolHandler;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    struct NoopProvider;
    impl IdentityProvider for NoopProvider {
        fn resolve_from_fingerprint(&self, _: &str) -> Option<alkcall::core::auth::Identity> {
            None
        }
        fn resolve_from_token(
            &self,
            _: &alkcall::core::auth::AuthToken,
        ) -> Option<alkcall::core::auth::Identity> {
            None
        }
    }

    fn empty_registry() -> Arc<OperationRegistry> {
        Arc::new(OperationRegistry::new())
    }

    fn provider() -> Arc<dyn IdentityProvider> {
        Arc::new(NoopProvider)
    }

    #[test]
    fn alpn_returns_http1_for_default_new() {
        let adapter = HttpAdapter::new(provider(), empty_registry());
        assert_eq!(adapter.alpn(), ALPN_HTTP1);
        assert_eq!(adapter.alpn(), b"http/1.1");
    }

    #[test]
    fn alpn_returns_h2_for_h2_constructor() {
        let adapter = HttpAdapter::h2(provider(), empty_registry());
        assert_eq!(adapter.alpn(), ALPN_H2);
        assert_eq!(adapter.alpn(), b"h2");
    }

    #[test]
    fn decoy_config_default_is_not_found() {
        assert!(matches!(DecoyConfig::default(), DecoyConfig::NotFound));
    }

    #[test]
    fn with_decoy_updates_decoy() {
        let adapter = HttpAdapter::new(provider(), empty_registry());
        let adapter = adapter.with_decoy(DecoyConfig::Redirect {
            to: "https://example.com".to_string(),
        });
        assert!(matches!(adapter.decoy(), DecoyConfig::Redirect { .. }));
    }

    #[tokio::test]
    async fn full_http_request_response_cycle_over_duplex() {
        let extra = Router::new().route("/v1/ping", get(|| async { "pong" }));
        let adapter = HttpAdapter::new(provider(), empty_registry()).with_extra_routes(extra);

        let (client, server) = tokio::io::duplex(64 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");

        let server_task =
            tokio::spawn(async move { ProtocolHandler::handle(&adapter, conn, &auth).await });

        let mut client = client;
        client
            .write_all(b"GET /v1/ping HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
            .await
            .unwrap();

        let mut response = Vec::new();
        let _ = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            client.read_to_end(&mut response),
        )
        .await
        .expect("read timed out")
        .unwrap();

        let text = String::from_utf8_lossy(&response);
        assert!(text.starts_with("HTTP/1.1 200 OK"), "got: {text}");
        assert!(text.contains("pong"), "got: {text}");

        let _ = server_task.await;
    }

    /// The accept-path connection-failure arm: a `Connection` whose
    /// single underlying stream is already gone (closed by the dial
    /// side) yields `StreamError::ConnectionClosed` from `accept_bi`,
    /// which `stream_error_to_handler` maps to
    /// `HandlerError::ConnectionClosed` — the error the consumer's
    /// accept loop observes for a peer that died before the first
    /// stream arrived.
    #[tokio::test]
    async fn accept_bi_failure_maps_to_handler_error_connection_closed() {
        let adapter = HttpAdapter::new(provider(), empty_registry());
        let (client, server) = tokio::io::duplex(64 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");

        drop(client);
        conn.close(0, "gone before the first stream");
        let result = ProtocolHandler::handle(&adapter, conn, &auth).await;
        match result {
            Err(HandlerError::ConnectionClosed) => {}
            other => panic!("expected HandlerError::ConnectionClosed, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn healthz_served_by_the_adapter_over_duplex() {
        let adapter = HttpAdapter::new(provider(), empty_registry());
        let (client, server) = tokio::io::duplex(64 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");
        let server_task = tokio::spawn(async move {
            let _ = ProtocolHandler::handle(&adapter, conn, &auth).await;
        });

        let mut client = client;
        client
            .write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
            .await
            .unwrap();

        let mut response = Vec::new();
        let _ = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            client.read_to_end(&mut response),
        )
        .await
        .expect("read timed out")
        .unwrap();

        let text = String::from_utf8_lossy(&response);
        assert!(text.starts_with("HTTP/1.1 200 OK"), "got: {text}");
        assert!(text.contains("ok"), "got: {text}");

        let _ = server_task.await;
    }

    #[tokio::test]
    async fn unknown_path_serves_decoy_404_over_duplex() {
        let adapter = HttpAdapter::new(provider(), empty_registry());
        let (client, server) = tokio::io::duplex(64 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");
        let server_task = tokio::spawn(async move {
            let _ = ProtocolHandler::handle(&adapter, conn, &auth).await;
        });

        let mut client = client;
        client
            .write_all(b"GET /nowhere HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
            .await
            .unwrap();

        let mut response = Vec::new();
        let _ = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            client.read_to_end(&mut response),
        )
        .await
        .expect("read timed out")
        .unwrap();

        let text = String::from_utf8_lossy(&response);
        assert!(text.starts_with("HTTP/1.1 404 Not Found"), "got: {text}");
        assert!(
            text.contains("nginx"),
            "decoy should look like nginx: {text}"
        );

        let _ = server_task.await;
    }
    #[tokio::test]
    async fn openapi_json_serves_the_gateway_projection() {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let registry = OperationRegistry::new();
        let spec = alkcall::registry::spec::OperationSpec::new(
            "echo/run",
            alkcall::registry::spec::OperationType::Query,
            alkcall::registry::spec::Visibility::External,
            serde_json::json!({}),
            serde_json::json!({}),
            vec![],
            alkcall::registry::spec::AccessControl::default(),
            None,
        );
        registry
            .register(alkcall::registry::registration::HandlerRegistration::new(
                spec,
                alkcall::registry::registration::HandlerKind::Once(
                    alkcall::registry::registration::make_handler(|input, ctx| async move {
                        alkcall::protocol::wire::ResponseEnvelope::ok(ctx.request_id, input)
                    }),
                ),
                alkcall::registry::registration::OperationProvenance::Local,
                None,
                None,
                alkcall::core::types::Capabilities::new(),
            ))
            .unwrap();

        let adapter = HttpAdapter::new(provider(), Arc::new(registry));
        let (client, server) = tokio::io::duplex(256 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");
        let server_task = tokio::spawn(async move {
            let _ = ProtocolHandler::handle(&adapter, conn, &auth).await;
        });

        let mut client = client;
        client
            .write_all(
                b"GET /openapi.json HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n",
            )
            .await
            .unwrap();

        let mut response = Vec::new();
        let _ = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            client.read_to_end(&mut response),
        )
        .await
        .expect("read timed out")
        .unwrap();

        let text = String::from_utf8_lossy(&response);
        assert!(text.starts_with("HTTP/1.1 200 OK"), "got: {text}");
        assert!(text.contains("application/json"), "got: {text}");
        // The 6-endpoint gateway doc; the version tracks the projection
        // truthfulness pass.
        assert!(text.contains("\"/publish\""), "publish path in doc");
        assert!(text.contains("1.4.0"), "info.version 1.4.0 in doc");
        assert!(text.contains("gatewayPublish"), "publish operationId");

        let _ = server_task.await;
    }
    #[cfg(feature = "mcp")]
    #[tokio::test]
    async fn mcp_endpoint_serves_four_gateway_tools_bearer_gated() {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let adapter = HttpAdapter::new(provider(), empty_registry());
        let (client, server) = tokio::io::duplex(256 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");
        let server_task = tokio::spawn(async move {
            let _ = ProtocolHandler::handle(&adapter, conn, &auth).await;
        });

        let mut client = client;
        client
            .write_all(
                b"POST /mcp HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nAccept: application/json, text/event-stream\r\nContent-Length: 175\r\nConnection: close\r\n\r\n{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"initialize\", \"params\": {\"protocolVersion\": \"2025-06-18\", \"capabilities\": {}, \"clientInfo\": {\"name\": \"test-client\", \"version\": \"1.0.0\"}}}",
            )
            .await
            .unwrap();

        let mut response = Vec::new();
        let _ = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            client.read_to_end(&mut response),
        )
        .await
        .expect("read timed out")
        .unwrap();

        let text = String::from_utf8_lossy(&response);
        // Bearer middleware is applied around the nested service: no token
        // means no identity stash, but the middleware does not enforce —
        // the MCP initialize response must still come back.
        assert!(
            text.starts_with("HTTP/1.1 200 OK"),
            "initialize over /mcp got: {text}"
        );
        assert!(
            text.contains("alkhttp-to-mcp"),
            "server info in initialize response: {text}"
        );

        let _ = server_task.await;
    }

    #[cfg(feature = "mcp")]
    #[tokio::test]
    async fn mcp_rejects_oversized_body_declared_content_length_with_413() {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let oversized = MCP_BODY_LIMIT + 1;
        let head = format!(
            "POST /mcp HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nAccept: application/json, text/event-stream\r\nContent-Length: {oversized}\r\nConnection: close\r\n\r\n"
        );

        let adapter = HttpAdapter::new(provider(), empty_registry());
        let (client, server) = tokio::io::duplex(64 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");
        let server_task = tokio::spawn(async move {
            let _ = ProtocolHandler::handle(&adapter, conn, &auth).await;
        });

        let mut client = client;
        client.write_all(head.as_bytes()).await.unwrap();

        let mut response = Vec::new();
        let _ = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            client.read_to_end(&mut response),
        )
        .await
        .expect("read timed out")
        .unwrap();

        let text = String::from_utf8_lossy(&response);
        assert!(
            text.starts_with("HTTP/1.1 413 Payload Too Large"),
            "declared oversized body got: {text}"
        );

        let _ = server_task.await;
    }

    #[cfg(feature = "mcp")]
    #[tokio::test]
    async fn mcp_rejects_oversized_chunked_body_with_413() {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let adapter = HttpAdapter::new(provider(), empty_registry());
        let (client, server) = tokio::io::duplex(64 * 1024);
        let conn = Connection::from_bidi(server, b"http/1.1".to_vec(), None);
        let auth = AuthContext::anonymous(b"http/1.1");
        let server_task = tokio::spawn(async move {
            let _ = ProtocolHandler::handle(&adapter, conn, &auth).await;
        });

        let (mut reader_client, mut writer_client) = tokio::io::split(client);
        writer_client
            .write_all(
                b"POST /mcp HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nAccept: application/json, text/event-stream\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n",
            )
            .await
            .unwrap();

        let writer = tokio::spawn(async move {
            let chunk = vec![b'a'; 64 * 1024];
            let chunk_header = format!("{:x}\r\n", chunk.len());
            for _ in 0..(MCP_BODY_LIMIT / chunk.len()) + 1 {
                if writer_client
                    .write_all(chunk_header.as_bytes())
                    .await
                    .is_err()
                {
                    return;
                }
                if writer_client.write_all(&chunk).await.is_err() {
                    return;
                }
                if writer_client.write_all(b"\r\n").await.is_err() {
                    return;
                }
            }
            let _ = writer_client.write_all(b"0\r\n\r\n").await;
        });

        let mut response = Vec::new();
        let read = tokio::time::timeout(
            std::time::Duration::from_secs(10),
            reader_client.read_to_end(&mut response),
        )
        .await;
        writer.abort();
        read.expect("read timed out").unwrap();

        let text = String::from_utf8_lossy(&response);
        assert!(
            text.starts_with("HTTP/1.1 413 Payload Too Large"),
            "chunked oversized body got: {text}"
        );

        let _ = server_task.await;
    }

    struct StaticProvider;
    impl IdentityProvider for StaticProvider {
        fn resolve_from_fingerprint(&self, _: &str) -> Option<alkcall::core::auth::Identity> {
            None
        }
        fn resolve_from_token(
            &self,
            _: &alkcall::core::auth::AuthToken,
        ) -> Option<alkcall::core::auth::Identity> {
            Some(alkcall::core::auth::Identity {
                id: "worker-a".to_string(),
                scopes: vec![],
                resources: std::collections::HashMap::new(),
            })
        }
    }

    fn static_provider() -> Arc<dyn IdentityProvider> {
        Arc::new(StaticProvider)
    }

    struct CountingProvider {
        resolutions: std::sync::Mutex<usize>,
    }

    impl CountingProvider {
        fn new() -> Self {
            Self {
                resolutions: std::sync::Mutex::new(0),
            }
        }

        fn resolutions(&self) -> usize {
            *self.resolutions.lock().unwrap_or_else(|e| e.into_inner())
        }
    }

    impl IdentityProvider for CountingProvider {
        fn resolve_from_fingerprint(&self, _: &str) -> Option<alkcall::core::auth::Identity> {
            None
        }
        fn resolve_from_token(
            &self,
            _: &alkcall::core::auth::AuthToken,
        ) -> Option<alkcall::core::auth::Identity> {
            *self.resolutions.lock().unwrap_or_else(|e| e.into_inner()) += 1;
            Some(alkcall::core::auth::Identity {
                id: "worker-a".to_string(),
                scopes: vec![],
                resources: std::collections::HashMap::new(),
            })
        }
    }

    async fn ws_upgrade_oneshot(
        app: Router,
        authorization: &str,
    ) -> axum::http::Response<axum::body::Body> {
        use tower::ServiceExt;
        let request = axum::http::Request::builder()
            .method(axum::http::Method::GET)
            .uri(WS_UPGRADE_PATH)
            .header(axum::http::header::AUTHORIZATION, authorization)
            .header(axum::http::header::CONNECTION, "upgrade")
            .header(axum::http::header::UPGRADE, "websocket")
            .header(axum::http::header::SEC_WEBSOCKET_VERSION, "13")
            .header(
                axum::http::header::SEC_WEBSOCKET_KEY,
                "dGhlIHNhbXBsZSBub25jZQ==",
            )
            .header(
                axum::http::header::HOST,
                axum::http::HeaderValue::from_static("localhost"),
            );
        let mut request = request.body(axum::body::Body::empty()).unwrap();
        let on_upgrade = hyper::upgrade::on(&mut request);
        request.extensions_mut().insert(on_upgrade);
        app.oneshot(request).await.unwrap()
    }

    fn router_state(idp: Arc<dyn IdentityProvider>) -> RouterState {
        RouterState {
            registry: empty_registry(),
            identity_provider: idp,
            decoy: DecoyConfig::default(),
            openapi_doc: CachedOpenAPIDoc::new(&OperationRegistry::new()),
            ws_sessions: Arc::new(crate::websocket::WsSessions::new()),
            ws_session_slots: Arc::new(tokio::sync::Semaphore::new(
                crate::websocket::DEFAULT_WS_MAX_SESSIONS,
            )),
            ws_idle_timeout: Some(crate::websocket::DEFAULT_WS_IDLE_TIMEOUT),
            ws_openable_alpns: None,
            ws_op_register_acl: alkcall::registry::spec::AccessControl::default(),
        }
    }

    async fn get_with_bearer(
        app: Router,
        path: &str,
        authorization: Option<&str>,
    ) -> axum::http::Response<axum::body::Body> {
        use tower::ServiceExt;
        let mut builder = axum::http::Request::builder().uri(path);
        if let Some(value) = authorization {
            builder = builder.header(axum::http::header::AUTHORIZATION, value);
        }
        app.oneshot(builder.body(axum::body::Body::empty()).unwrap())
            .await
            .unwrap()
    }

    async fn get_with_bearer_with_method(
        app: Router,
        request: axum::http::Request<axum::body::Body>,
    ) -> axum::http::Response<axum::body::Body> {
        use tower::ServiceExt;
        app.oneshot(request).await.unwrap()
    }

    #[tokio::test]
    async fn extra_routes_resolve_bearer_identity_through_the_default_auth() {
        let extra = Router::new().route(
            "/v1/whoami",
            get(|ResolvedIdentity(identity): ResolvedIdentity| async move {
                match identity {
                    Some(id) => id.id,
                    None => "none".to_string(),
                }
            }),
        );
        let app = build_router(router_state(static_provider()), Some(extra));

        let response = get_with_bearer(app.clone(), "/v1/whoami", Some("Bearer alk_test")).await;
        assert_eq!(response.status(), axum::http::StatusCode::OK);
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert_eq!(&body[..], b"worker-a");

        let response = get_with_bearer(app, "/v1/whoami", None).await;
        assert_eq!(response.status(), axum::http::StatusCode::OK);
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert_eq!(&body[..], b"none");
    }

    #[tokio::test]
    async fn extra_route_with_own_layer_can_opt_out_of_default_auth() {
        async fn public_identity(
            mut request: axum::extract::Request,
            next: axum::middleware::Next,
        ) -> axum::response::Response {
            request
                .extensions_mut()
                .insert(Some(alkcall::core::auth::Identity {
                    id: "public-webhook".to_string(),
                    scopes: vec![],
                    resources: std::collections::HashMap::new(),
                }));
            next.run(request).await
        }

        let extra = Router::new().route(
            "/v1/public",
            get(|ResolvedIdentity(identity): ResolvedIdentity| async move {
                match identity {
                    Some(id) => id.id,
                    None => "none".to_string(),
                }
            })
            .route_layer(axum::middleware::from_fn(public_identity)),
        );
        let app = build_router(router_state(static_provider()), Some(extra));

        let response = get_with_bearer(app, "/v1/public", None).await;
        assert_eq!(response.status(), axum::http::StatusCode::OK);
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert_eq!(&body[..], b"public-webhook");
    }

    #[tokio::test]
    #[should_panic(expected = "Overlapping method route")]
    async fn extra_route_on_reserved_path_panics_at_construction() {
        let extra = Router::new().route(
            "/search",
            axum::routing::post(|| async { "shadowing the gateway" }),
        );
        let _ = HttpAdapter::new(static_provider(), empty_registry()).with_extra_routes(extra);
    }

    #[tokio::test]
    async fn extra_route_on_non_reserved_path_merges_cleanly() {
        let extra = Router::new().route(
            "/v1/ping",
            get(|| async { "pong" }).post(|| async { "pong-post" }),
        );
        let adapter =
            HttpAdapter::new(static_provider(), empty_registry()).with_extra_routes(extra);
        assert!(adapter.router().has_routes());
    }

    #[tokio::test]
    async fn second_with_decoy_keeps_extra_routes() {
        let extra = Router::new().route("/v1/ping", get(|| async { "pong" }));
        let adapter = HttpAdapter::new(static_provider(), empty_registry())
            .with_extra_routes(extra)
            .with_decoy(DecoyConfig::Redirect {
                to: "https://example.com".to_string(),
            });

        let request = axum::http::Request::builder()
            .uri("/v1/ping")
            .body(axum::body::Body::empty())
            .unwrap();
        let response = get_with_bearer(adapter.router().clone(), "/v1/ping", None).await;
        drop(request);
        assert_eq!(
            response.status(),
            axum::http::StatusCode::OK,
            "a second builder call must not drop the extra routes (SRV-05)"
        );
    }

    #[tokio::test]
    async fn method_mismatch_on_registered_path_serves_decoy_405() {
        let adapter = HttpAdapter::new(static_provider(), empty_registry());
        let request = axum::http::Request::builder()
            .method(axum::http::Method::OPTIONS)
            .uri("/search")
            .body(axum::body::Body::empty())
            .unwrap();
        let response = get_with_bearer_with_method(adapter.router().clone(), request).await;
        assert_eq!(
            response.status(),
            axum::http::StatusCode::METHOD_NOT_ALLOWED
        );
        let server = response
            .headers()
            .get(axum::http::header::SERVER)
            .map(|v| v.to_str().unwrap().to_string());
        assert_eq!(
            server.as_deref(),
            Some("nginx"),
            "405 must carry the decoy Server header, not axum's bare 405 (SRV-07)"
        );
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        let body = String::from_utf8_lossy(&body);
        assert!(body.contains("405 Not Allowed"), "got: {body}");
        assert!(
            !body.contains("axum") && !body.contains("alk"),
            "got: {body}"
        );
    }

    #[tokio::test]
    async fn method_mismatch_on_extra_route_serves_decoy_405() {
        let extra = Router::new().route("/v1/ping", get(|| async { "pong" }));
        let adapter =
            HttpAdapter::new(static_provider(), empty_registry()).with_extra_routes(extra);

        let request = axum::http::Request::builder()
            .method(axum::http::Method::DELETE)
            .uri("/v1/ping")
            .body(axum::body::Body::empty())
            .unwrap();
        let response = get_with_bearer_with_method(adapter.router().clone(), request).await;
        assert_eq!(
            response.status(),
            axum::http::StatusCode::METHOD_NOT_ALLOWED,
            "wrong-method probe on an extra route"
        );
        let server = response
            .headers()
            .get(axum::http::header::SERVER)
            .map(|v| v.to_str().unwrap().to_string());
        assert_eq!(
            server.as_deref(),
            Some("nginx"),
            "extra-route 405 must carry the decoy Server header, not axum's bare 405 (SRV-12)"
        );
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        let body = String::from_utf8_lossy(&body);
        assert!(body.contains("405 Not Allowed"), "got: {body}");
        assert!(
            !body.contains("axum") && !body.contains("alk"),
            "got: {body}"
        );
    }

    #[tokio::test]
    async fn method_mismatch_on_default_surface_still_serves_decoy_405_after_extras_merge() {
        let extra = Router::new().route("/v1/ping", get(|| async { "pong" }));
        let adapter =
            HttpAdapter::new(static_provider(), empty_registry()).with_extra_routes(extra);

        let request = axum::http::Request::builder()
            .method(axum::http::Method::OPTIONS)
            .uri("/search")
            .body(axum::body::Body::empty())
            .unwrap();
        let response = get_with_bearer_with_method(adapter.router().clone(), request).await;
        assert_eq!(
            response.status(),
            axum::http::StatusCode::METHOD_NOT_ALLOWED
        );
        let server = response
            .headers()
            .get(axum::http::header::SERVER)
            .map(|v| v.to_str().unwrap().to_string());
        assert_eq!(
            server.as_deref(),
            Some("nginx"),
            "the re-applied 405 fallback must not regress the default surface (SRV-07)"
        );
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        let body = String::from_utf8_lossy(&body);
        assert!(body.contains("405 Not Allowed"), "got: {body}");
    }

    #[tokio::test]
    async fn ws_upgrade_resolves_the_token_exactly_once() {
        let provider = Arc::new(CountingProvider::new());
        let app = build_router(
            router_state(provider.clone() as Arc<dyn IdentityProvider>),
            None,
        );

        let response = ws_upgrade_oneshot(app, "Bearer alk_test").await;
        assert_eq!(
            response.status(),
            axum::http::StatusCode::SWITCHING_PROTOCOLS,
            "a valid bearer token upgrades"
        );
        assert_eq!(
            provider.resolutions(),
            1,
            "the WS upgrade path must resolve the token exactly once (SRV-11)"
        );

        let provider = Arc::new(CountingProvider::new());
        let app = build_router(
            router_state(provider.clone() as Arc<dyn IdentityProvider>),
            None,
        );
        let response = ws_upgrade_oneshot(app, "Bearer alk_test").await;
        drop(response);
        assert_eq!(
            provider.resolutions(),
            1,
            "exactly one resolution per upgrade request after any builder order"
        );
    }

    #[tokio::test]
    async fn ws_upgrade_without_token_is_rejected_401() {
        let app = build_router(router_state(static_provider()), None);
        let response = ws_upgrade_oneshot(app, "").await;
        assert_eq!(
            response.status(),
            axum::http::StatusCode::UNAUTHORIZED,
            "the WS route keeps its own enforced layer"
        );
    }

    #[cfg(feature = "mcp")]
    #[tokio::test]
    async fn mcp_request_resolves_the_token_exactly_once() {
        let provider = Arc::new(CountingProvider::new());
        let app = build_router(
            router_state(provider.clone() as Arc<dyn IdentityProvider>),
            None,
        );

        let request = axum::http::Request::builder()
            .method(axum::http::Method::POST)
            .uri("/mcp")
            .header(axum::http::header::HOST, "localhost")
            .header(axum::http::header::AUTHORIZATION, "Bearer alk_test")
            .header(axum::http::header::CONTENT_TYPE, "application/json")
            .header(
                axum::http::header::ACCEPT,
                "application/json, text/event-stream",
            )
            .body(axum::body::Body::from(
                serde_json::to_vec(&serde_json::json!({
                    "jsonrpc": "2.0", "id": 1, "method": "initialize",
                    "params": {
                        "protocolVersion": "2025-06-18",
                        "capabilities": {},
                        "clientInfo": { "name": "test-client", "version": "1.0.0" }
                    }
                }))
                .unwrap(),
            ))
            .unwrap();
        let response = tower::ServiceExt::oneshot(app, request).await.unwrap();
        assert_eq!(
            response.status(),
            axum::http::StatusCode::OK,
            "the /mcp initialize response comes back"
        );
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert!(
            String::from_utf8_lossy(&body).contains("alkhttp-to-mcp"),
            "initialize response body, got: {}",
            String::from_utf8_lossy(&body)
        );
        assert_eq!(
            provider.resolutions(),
            1,
            "the /mcp path must resolve the token exactly once (SRV-11)"
        );
    }

    #[tokio::test]
    async fn method_mismatch_on_ws_upgrade_path_serves_decoy_405() {
        let app = build_router(router_state(static_provider()), None);
        let request = axum::http::Request::builder()
            .method(axum::http::Method::POST)
            .uri(WS_UPGRADE_PATH)
            .body(axum::body::Body::empty())
            .unwrap();
        let response = get_with_bearer_with_method(app, request).await;
        assert_eq!(
            response.status(),
            axum::http::StatusCode::METHOD_NOT_ALLOWED
        );
        let server = response
            .headers()
            .get(axum::http::header::SERVER)
            .map(|v| v.to_str().unwrap().to_string());
        assert_eq!(
            server.as_deref(),
            Some("nginx"),
            "wrong-method probe on the WS path keeps the decoy shape after the reorder"
        );
    }

    #[tokio::test]
    async fn openapi_json_is_cached_and_generic_on_cache_miss() {
        let adapter = HttpAdapter::new(static_provider(), empty_registry());
        let first = openapi_json_handler(axum::extract::State(adapter.openapi_doc.clone())).await;
        let second = openapi_json_handler(axum::extract::State(adapter.openapi_doc.clone())).await;
        let first_bytes = axum::body::to_bytes(first.into_body(), usize::MAX)
            .await
            .unwrap();
        let second_bytes = axum::body::to_bytes(second.into_body(), usize::MAX)
            .await
            .unwrap();
        assert_eq!(
            &first_bytes[..],
            &second_bytes[..],
            "cached doc is byte-stable"
        );

        let miss = CachedOpenAPIDoc {
            inner: Arc::new(Mutex::new(None)),
        };
        let response = openapi_json_handler(axum::extract::State(miss)).await;
        assert_eq!(
            response.status(),
            axum::http::StatusCode::INTERNAL_SERVER_ERROR
        );
        let body = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert_eq!(
            &body[..],
            b"internal server error",
            "the 500 body is the fixed generic string, no serde internals"
        );
    }
}