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
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
//! `to_openapi`: gateway projection of the local operation registry into a
//! fixed 6-endpoint OpenAPI 3.0 document (ADR-042, as extended by
//! ADR-068).
//!
//! `to_openapi` is a pure projection (ADR-017 §5): it consumes the
//! registry and produces a spec; it does not modify the registry,
//! register operations, or implement `OperationAdapter`. The generated
//! doc describes the 6 fixed gateway endpoints (`/search`, `/schema`,
//! `/call`, `/batch`, `/subscribe`, `/publish`) — the sole HTTP invoke
//! path (ADR-047). The per-caller operation surface is discovered at
//! runtime through AccessControl-filtered `/search`, not preloaded into
//! the doc (ADR-042 §3).
//!
//! `info.version` is a semver constant tracking the **gateway endpoint
//! contract**, not the operation set — per-caller operation changes do
//! not bump the version (ADR-045). `1.0.0` was the 5-endpoint contract;
//! `1.1.0` adds `/publish` (ADR-068) — minor (additive). `1.2.0` aligns
//! the doc with the settled gateway runtime contract (review-001
//! PRJ-01..05, PRJ-14, PRJ-15): envelope responses, 422 client-fault
//! mappings, the `/subscribe` 200+SSE asymmetry, Bearer
//! `securitySchemes`, shared error components. `1.3.0` is the
//! review-002 projection-truthfulness pass (PRJ-16b/17/18/19/20/21/23):
//! runtime-resolvable refs (the in-band `BatchResultEntry.error`
//! component), protocol-status op errors merged oneOf into the shared
//! responses instead of clobbering them, `/call` 401 and the extractor
//! 415/plain-text-422 slots documented, `/publish` 400 narrowed to the
//! framing contract, the unreachable `/batch` 500 removed, and the
//! OAS-invalid `x-operation-error-statuses` pseudo-schema dropped. Per
//! ADR-045's tracking rule this is a minor bump, not major: the
//! corrections remove only documentation of statuses/codes the runtime
//! never emitted (a strict client matching them observed nothing to
//! break), and every added slot documents behavior the runtime already
//! had — the wire contract is unchanged. `1.4.0` is the GW-16 status
//! unification: the runtime's hand-rolled `/publish` framing faults
//! and the `/batch` cap rejection moved from 400 to the documented
//! `INVALID_INPUT → 422` mapping (one error class, one status), so the
//! doc's 400/422 slots moved with the runtime. Clients matching the old
//! 400 slots observed behavior the runtime no longer emits; the 422
//! slots are where `INVALID_INPUT` was already documented, so the
//! client-visible consequence is additive on the documented mapping
//! side and the status drift is gone.
//!
//! # Error fidelity
//!
//! The doc mirrors `gateway::error`'s actual mapping (review-001
//! PRJ-03/PRJ-04 decisions):
//!
//! - `INVALID_INPUT` and identity-resolved `INVALID_OPERATION_TYPE` are
//!   documented at `422` — the status the runtime emits. Axum extractor
//!   rejections are plain-text bodies with none of the `CallError` JSON
//!   shape: malformed framing answers `400`, a missing or non-JSON
//!   `Content-Type` answers `415`, and a syntactically-valid body that
//!   fails deserialization answers `422` (axum's `Json`/`Query`
//!   rejection statuses — PRJ-19). Those slots are documented alongside
//!   the dispatch-path JSON contract per endpoint.
//! - Protocol-level codes are documented at their mapped statuses.
//!   `FORBIDDEN`/`INVALID_OPERATION_TYPE` map to `401` (no token) or
//!   `403`/`422` (token present) per the identity-aware mapper; the doc
//!   places them at the identity-resolved status, so the `401` slots
//!   carry both codes (PRJ-20).
//! - Operation-level codes are projected by registry-declared
//!   `http_status`, **but** the runtime is purely code-driven: a
//!   non-`HTTP_*` code (e.g. `RATE_LIMITED` declared at 429) actually
//!   surfaces as `500 INTERNAL` because `ErrorDefinition.http_status` is
//!   never consulted at dispatch. Non-protocol statuses carry
//!   `x-runtime-behavior: 500` (the PRJ-04 project-honest decision);
//!   protocol-status declarations from `HTTP_<status>`-prefixed codes
//!   are **merged oneOf** into the shared protocol response instead of
//!   overwriting it (PRJ-17) — the runtime genuinely maps those codes
//!   to the declared status, alongside the protocol codes that status
//!   already documents.
//!
//! See `docs/architecture/http-adapters.md` §"to_openapi" and
//! ADR-042/045/068/023.

use std::collections::BTreeMap;

use serde_json::{json, Value};

use alkcall::client::AdapterError;
use alkcall::registry::registration::OperationRegistry;
use alkcall::registry::spec::ErrorDefinition;

use super::openapi_spec::OpenAPISpec;
use crate::gateway::MAX_BATCH_OPERATIONS;

const GATEWAY_VERSION: &str = "1.4.0";
const GATEWAY_TITLE: &str = "alk gateway";
const OPENAPI_VERSION: &str = "3.0.0";

const PATH_SEARCH: &str = "/search";
const PATH_SCHEMA: &str = "/schema";
const PATH_CALL: &str = "/call";
const PATH_BATCH: &str = "/batch";
const PATH_SUBSCRIBE: &str = "/subscribe";
const PATH_PUBLISH: &str = "/publish";

const STATUS_BAD_REQUEST: u16 = 400;
const STATUS_UNAUTHORIZED: u16 = 401;
const STATUS_FORBIDDEN: u16 = 403;
const STATUS_NOT_FOUND: u16 = 404;
const STATUS_UNPROCESSABLE: u16 = 422;
const STATUS_INTERNAL: u16 = 500;
const STATUS_TIMEOUT: u16 = 504;

const HTTP_PREFIX: &str = "HTTP_";

const CODE_INVALID_INPUT: &str = "INVALID_INPUT";
const CODE_INVALID_OPERATION_TYPE: &str = "INVALID_OPERATION_TYPE";
const CODE_FORBIDDEN: &str = "FORBIDDEN";
const CODE_NOT_FOUND: &str = "NOT_FOUND";
const CODE_INTERNAL: &str = "INTERNAL";
const CODE_TIMEOUT: &str = "TIMEOUT";

const SCHEMA_BATCH_ERROR: &str = "BatchError";
const SCHEMA_BATCH_OPERATION_ERROR: &str = "BatchOperationError";

const SCHEME_BEARER: &str = "bearerAuth";

const RESPONSE_REF: &str = "#/components/schemas/";

/// Project the registry into the fixed 6-endpoint gateway doc (ADR-042).
///
/// Returns [`AdapterError::SchemaParse`] if the generated doc does not
/// re-validate against the structural checks in `OpenAPISpec::from_value`
/// — a would-be invariant violation of `build_doc`, not a caller-facing
/// input error. The HTTP surface serves only a generic `500` for this
/// (`/openapi.json` handler caches the serialized doc; SRV-09): no
/// serde/parse internals reach the wire.
pub fn to_openapi(registry: &OperationRegistry) -> Result<OpenAPISpec, AdapterError> {
    let operation_errors = gather_operation_errors(registry);
    let raw = build_doc(&operation_errors);
    OpenAPISpec::from_value(raw)
}

fn build_doc(operation_errors: &BTreeMap<u16, Value>) -> Value {
    let paths = json!({
        PATH_SEARCH: search_path_item(),
        PATH_SCHEMA: schema_path_item(),
        PATH_CALL: call_path_item(operation_errors),
        PATH_BATCH: batch_path_item(),
        PATH_SUBSCRIBE: subscribe_path_item(),
        PATH_PUBLISH: publish_path_item(operation_errors),
    });

    json!({
        "openapi": OPENAPI_VERSION,
        "info": {
            "title": GATEWAY_TITLE,
            "version": GATEWAY_VERSION,
            "description": "alk gateway: 6 fixed endpoints gating access to the operation registry. The per-caller operation surface is discovered via /search (AccessControl-filtered), not preloaded into this doc."
        },
        "paths": paths,
        "components": components(operation_errors),
        "security": [{ SCHEME_BEARER: [] }]
    })
}

fn search_path_item() -> Value {
    json!({
        "get": {
            "operationId": "gatewaySearch",
            "summary": "List the operations this caller may invoke (AccessControl-filtered). Important: the response body carries the envelope wrapper {request_id, result, output}; the operations array is under output.",
            "security": [{ SCHEME_BEARER: [] }],
            "responses": {
                "200": json_response(ref_schema("SearchResponse"),
                    "The AccessControl-filtered operation listing under `output` (GW-02). Items carry name, namespace, and op_type; forbidden ops are omitted from the listing — the request does not fail. Cache-Control: no-store, Vary: Authorization."),
                "400": plain_text_extractor_rejection(),
                "415": plain_text_unsupported_media_type(),
                "422": plain_text_extractor_shape_rejection(),
                "404": json_response(ref_response("NotFound"),
                    "The appended /search and /schema reserved paths are hidden from the listing; a direct dispatch to services/list still resolves normally."),
                "500": json_response(ref_response("Internal"),
                    "Dispatcher failure."),
                "504": json_response(ref_response("Timeout"),
                    "The bounded Once-op dispatch exceeded the 30 s gateway deadline (retryable)."),
            }
        }
    })
}

fn schema_path_item() -> Value {
    json!({
        "get": {
            "operationId": "gatewaySchema",
            "summary": "Get an operation's full OperationSpec (output wrapped in the envelope under output; includes op_type, visibility, access_control, channel_open, publish_schema).",
            "security": [{ SCHEME_BEARER: [] }],
            "parameters": [
                {
                    "name": "name",
                    "in": "query",
                    "required": true,
                    "schema": { "type": "string" }
                }
            ],
            "responses": {
                "200": json_response(ref_schema("SchemaResponse"),
                    "The operation's full spec under `output`. Cache-Control: no-store, Vary: Authorization."),
                "400": plain_text_extractor_rejection(),
                "415": plain_text_unsupported_media_type(),
                "401": json_response(ref_response("Unauthorized"),
                    "No bearer token or it did not resolve; AccessControl checks FORBIDDEN (401 without a token)."),
                "403": json_response(ref_response("Forbidden"),
                    "Token resolved, but AccessControl denies the operation."),
                "404": json_response(ref_response("NotFound"),
                    "Unknown operation, or the operation is Internal (hidden from HTTP discovery, GW-02)."),
                "422": json_response(ref_response("InvalidInput"),
                    "Dispatch-path client fault: the services/schema op reported INVALID_INPUT (missing name). A query string that parses but yields no `name` is a plain-text extractor 422 (PRJ-19)."),
                "500": json_response(ref_response("Internal"),
                    "Dispatcher failure."),
                "504": json_response(ref_response("Timeout"),
                    "The bounded Once-op dispatch exceeded the 30 s gateway deadline (retryable)."),
            }
        }
    })
}

fn call_path_item(operation_errors: &BTreeMap<u16, Value>) -> Value {
    json!({
        "post": {
            "operationId": "gatewayCall",
            "summary": "Invoke an operation by name with a flat JSON input. The final ResponseEnvelope is the response.",
            "security": [{ SCHEME_BEARER: [] }],
            "requestBody": {
                "required": true,
                "content": {
                    "application/json": {
                        "schema": ref_schema("CallRequest")
                    }
                }
            },
            "responses": call_responses(operation_errors)
        }
    })
}

/// The operation-declared status projections merged into a target
/// responses map (PRJ-17): the shared protocol statuses document the
/// protocol codes the runtime emits at that status, and the runtime
/// also maps `HTTP_<status>`-prefixed operation codes to their declared
/// status — so instead of overwriting a shared response (which would
/// erase the protocol codes every real call still carries), each
/// operation-declared projection is appended to the shared response's
/// `oneOf` as a per-code envelope component. Non-protocol statuses have
/// nothing shared to merge with; they overwrite as before (they only
/// occur at keys the fixed map has not set).
fn merge_operation_errors(responses: &mut Value, operation_errors: &BTreeMap<u16, Value>) {
    for (status, projection) in operation_errors {
        let key = status.to_string();
        let code_variants: Vec<Value> = projection
            .pointer("/content/application~1json/schema/properties/code/enum")
            .and_then(Value::as_array)
            .map(|codes| {
                codes
                    .iter()
                    .filter_map(|code| code.as_str())
                    .map(error_variant_ref)
                    .collect()
            })
            .unwrap_or_default();
        if code_variants.is_empty() {
            continue;
        }
        let Some(shared) = responses.get_mut(&key) else {
            responses[&key] = projection.clone();
            continue;
        };
        let Some(shared_schema) = shared
            .pointer_mut("/content/application~1json/schema")
            .filter(|schema| schema.is_object())
        else {
            responses[&key] = projection.clone();
            continue;
        };
        let existing = shared_schema.as_object_mut().expect("schema object");
        if !existing.contains_key("oneOf") {
            let shared_schema_value = existing.clone();
            existing.insert("oneOf".to_string(), json!([shared_schema_value]));
        }
        if let Some(variants) = existing.get_mut("oneOf").and_then(Value::as_array_mut) {
            variants.extend(code_variants);
        }
    }
}

/// The in-envelope component name of one operation-declared error code
/// (PRJ-17's merge target): the oneOf variant a merged protocol
/// response references.
fn error_variant_ref(code: &str) -> Value {
    json!({ "$ref": format!("{RESPONSE_REF}CallError_{code}") })
}

/// The fixed status set of a dispatch-path endpoint's documented
/// response map (200 + protocol statuses, plus the operation-declared
/// statuses gathered from the registry; PRJ-12 determinism: the merge
/// order is sorted).
fn call_responses(operation_errors: &BTreeMap<u16, Value>) -> Value {
    let mut responses = json!({
        "200": json_response(ref_schema("CallOk"),
            "The operation's final ResponseEnvelope: request_id, result=ok, output."),
        "400": plain_text_extractor_rejection(),
        "415": plain_text_unsupported_media_type(),
        "401": json_response(one_of_refs(&[
            "CallErrorForbidden".to_string(),
            "CallErrorInvalidOperationType".to_string(),
        ]),
            "No bearer token or it did not resolve: AccessControl denial (FORBIDDEN) or the non-Once-op dispatch-path report (INVALID_OPERATION_TYPE) without identity (PRJ-20). Malformed JSON bodies are rejected earlier by the extractors with a plain-text 400 (not this JSON shape)."),
        "403": json_response(ref_response("Forbidden"),
            "Token resolved, but AccessControl denies the operation."),
        "404": json_response(ref_response("NotFound"),
            "Unknown operation, or the operation is Internal (hidden from the HTTP surface)."),
        "422": json_response(
            {
                let mut schema = one_of_refs(&[
                    "CallErrorInvalidInput".to_string(),
                    "CallErrorInvalidOperationType".to_string(),
                ]);
                schema["x-extractor-variant"] = json!("A syntactically valid JSON body that fails deserialization is also answered 422, with a plain-text extractor body (PRJ-19) — see components.responses.ExtractorShapeRejection.");
                schema
            },
            "Dispatch-path client fault: malformed input (INVALID_INPUT), or the operation's type does not accept a Once invoke (INVALID_OPERATION_TYPE). The extractor's shape rejection (valid JSON, wrong shape) is also a 422 but with a plain-text body, not this envelope (PRJ-19). Once-op invokes are bounded by the 30 s gateway deadline.",
        ),
        "500": json_response(one_of_refs(&[
            "CallErrorInternal".to_string(),
            "CallFailure".to_string(),
        ]),
            "Dispatcher failure, or an operation-level error code without HTTP_ prefix or http_status (the runtime mapper is purely code-driven and such codes surface as 500)."),
        "504": json_response(ref_response("Timeout"),
            "The Once-op dispatch exceeded the 30 s gateway deadline (retryable)."),
    });
    merge_operation_errors(&mut responses, operation_errors);
    responses
}

/// `/publish`'s responses: the shared dispatch-path statuses plus the
/// NDJSON-framing 400 and the identity-split 401 (PRJ-03).
fn publish_path_item(operation_errors: &BTreeMap<u16, Value>) -> Value {
    json!({
        "post": {
            "operationId": "gatewayPublish",
            "summary": "Invoke a Pub operation. The body is NDJSON streamed (never fully buffered): the first line = {\"operation\": \"/service/op\", \"chunk\": {...}}, subsequent lines are chunk values. The final ResponseEnvelope is the response. A terminal error is a plain HTTP status + JSON body (not an NDJSON line).",
            "security": [{ SCHEME_BEARER: [] }],
            "requestBody": {
                "required": true,
                "content": {
                    "application/x-ndjson": {
                        "schema": ref_schema("NdjsonBody")
                    }
                }
            },
            "responses": publish_responses(operation_errors)
        }
    })
}

fn publish_responses(operation_errors: &BTreeMap<u16, Value>) -> Value {
    let mut responses = json!({
        "200": json_response(ref_schema("CallOk"),
            "The operation's final ResponseEnvelope: request_id, result=ok, output."),
        "401": json_response(one_of_refs(&[
            "CallErrorForbidden".to_string(),
            "CallErrorInvalidOperationType".to_string(),
        ]),
            "No bearer token or it did not resolve: AccessControl denial (FORBIDDEN) or the non-Pub-op dispatch-path report (INVALID_OPERATION_TYPE) without identity."),
        "403": json_response(ref_response("Forbidden"),
            "Token resolved, but AccessControl denies the operation."),
        "404": json_response(ref_response("NotFound"),
            "Unknown operation, or the operation is Internal (hidden from the HTTP surface)."),
        "422": json_response(one_of_refs(&[
            "CallErrorInvalidInput".to_string(),
            "CallErrorInvalidOperationType".to_string(),
        ]),
            "Dispatch-path client fault: NDJSON stream framing faults — empty body, first line missing 'operation'/'chunk', a line exceeding the 2 MiB per-line cap, or a raw body-read failure (INVALID_INPUT, GW-16 unified with the mid-stream status) — plus a later NDJSON line that was not valid JSON, a chunk that failed the operation's publish_schema validation (INVALID_INPUT with details.chunk), or the operation's type is not Pub (INVALID_OPERATION_TYPE, token present). The gateway's 2 MiB + 64 KiB body-limit layer answers an oversized whole-body upload with a plain-text 413 before the route runs."),
        "500": json_response(one_of_refs(&[
            "CallErrorInternal".to_string(),
            "PublishFailure".to_string(),
        ]),
            "Dispatcher failure, or an operation-level error code without HTTP_ prefix or http_status (the runtime mapper is purely code-driven and such codes surface as 500)."),
        "504": json_response(ref_response("Timeout"),
            "The sink dispatch exceeded the 30 s gateway deadline (retryable) — the same deadline bounds the Once-op invoke (GW-17)."),
    });
    merge_operation_errors(&mut responses, operation_errors);
    responses
}

fn batch_path_item() -> Value {
    json!({
        "post": {
            "operationId": "gatewayBatch",
            "summary": "Invoke multiple operations in one request. The response body is {request_id, result, output: {results: [...] }}. Per-call failures are in-band result:error items; only the request-level cap failure is an HTTP error status.",
            "security": [{ SCHEME_BEARER: [] }],
            "requestBody": {
                "required": true,
                "content": {
                    "application/json": {
                        "schema": {
                            "type": "array",
                            "items": ref_schema("CallRequest"),
                            "maxItems": MAX_BATCH_OPERATIONS,
                        }
                    }
                }
            },
            "responses": {
                "200": json_response(ref_schema("BatchResponse"),
                    "results[] shares entries' order with the request; each entry is an envelope-shaped {request_id, result, output|error} object; entries for Internal ops carry a NOT_FOUND in-band error. Per-call dispatch failures surface only as these in-band entries — there is no HTTP error status for an individual call."),
                "422": json_response(one_of_refs(&[
                    "BatchCapExceeded".to_string(),
                ]),
                    "Request-level failure: the batch exceeds 100 operations (INVALID_INPUT, JSON; GW-16 unified with the INVALID_INPUT → 422 mapping). Malformed JSON bodies are rejected earlier by the extractors with a plain-text 400 (not this JSON shape); a JSON array body whose items fail deserialization is a plain-text extractor 422. Oversized uploads are pre-empted by the gateway's body-limit layer with a plain-text 413."),
            }
        }
    })
}

fn subscribe_path_item() -> Value {
    json!({
        "post": {
            "operationId": "gatewaySubscribe",
            "summary": "Invoke a streaming (Sub) operation. The response is always HTTP 200 text/event-stream; failures arrive as in-band event:error SSE frames (GW-12), not HTTP error statuses. The first event:error frame is terminal: it is followed by stream close (call.completed-to-come).",
            "security": [{ SCHEME_BEARER: [] }],
            "requestBody": {
                "required": true,
                "content": {
                    "application/json": {
                        "schema": ref_schema("CallRequest")
                    }
                }
            },
            "responses": {
                "200": sse_response(
                    "Server-Sent Events. Each Success SSE event carries one output value (with a retry: 15000 reconnect hint); an Error event carries the serialized CallError as its data and ends the stream (an Err is terminal — no events follow it, matching the wire dispatch's call.error semantics). Keep-alive comment frames are sent every 15 s. Pre-dispatch denials (unknown op, Internal op, ACL denial, wrong dispatch path), handler-triggered (not stream) failures and mid-stream handler failures all appear as event:error frames here — standard HTTP monitoring sees no failures on /subscribe; clients must inspect event:error."),
                "400": plain_text_extractor_rejection(),
                "415": plain_text_unsupported_media_type(),
                "422": plain_text_extractor_shape_rejection(),
            }
        }
    })
}

/// The `/subscribe` 200 response: `text/event-stream` content with the
/// full in-band error contract in the description (PRJ-05 / GW-12).
fn sse_response(description: &str) -> Value {
    json!({
        "description": description,
        "content": {
            "text/event-stream": {
                "schema": ref_schema("SseStream")
            }
        }
    })
}

fn json_response(schema: Value, description: &str) -> Value {
    json!({
        "description": description,
        "content": {
            "application/json": {
                "schema": schema
            }
        }
    })
}

fn components(operation_errors: &BTreeMap<u16, Value>) -> Value {
    let mut schemas = json!({
        "CallRequest": call_request_schema(),
        "CallOk": call_ok_schema(),
        "SearchResponse": search_response_schema(),
        "SearchOperation": search_operation_schema(),
        "SchemaResponse": envelope_ref_schema("OperationSpecOutput"),
        "OperationSpecOutput": operation_spec_output_schema(),
        "BatchResponse": batch_response_schema(),
        "BatchResultEntry": batch_result_entry_schema(),
        "BatchError": batch_error_schema(),
        "BatchOperationError": error_variant_schema(&operation_error_codes(operation_errors)),
        "BatchCapExceeded": batch_cap_exceeded_schema(),
        "SseStream": sse_stream_schema(),
        "NdjsonBody": ndjson_body_schema(),
        "CallFailure": {
            "description": "In-band operation-declared failure envelope.",
            "allOf": [failure_envelope_schema(operation_errors)]
        },
        "PublishFailure": {
            "description": "In-band operation-declared failure envelope.",
            "allOf": [failure_envelope_schema(operation_errors)]
        },
        "CallErrorInvalidInput": call_error_schema(CODE_INVALID_INPUT),
        "CallErrorInvalidOperationType": call_error_schema(CODE_INVALID_OPERATION_TYPE),
        "CallErrorForbidden": call_error_schema(CODE_FORBIDDEN),
        "CallErrorNotFound": call_error_schema(CODE_NOT_FOUND),
        "CallErrorInternal": call_error_schema(CODE_INTERNAL),
        "CallErrorTimeout": call_error_schema(CODE_TIMEOUT)
    });
    for code in operation_error_codes(operation_errors) {
        let component = format!("CallError_{code}");
        if schemas.get(&component).is_none() {
            schemas[&component] = call_error_schema(&code);
        }
    }
    json!({
        "securitySchemes": {
            SCHEME_BEARER: {
                "type": "http",
                "scheme": "bearer",
                "description": "Bearer token resolved via IdentityProvider::resolve_from_token (ADR-004). Endpoints marked optional security are callable without a token; AccessControl then decides."
            }
        },
        "responses": {
            "Unauthorized": json_response(ref_schema("CallErrorForbidden"),
                "No bearer token or it did not resolve."),
            "Forbidden": json_response(ref_schema("CallErrorForbidden"),
                "The token resolved, but AccessControl denies the operation."),
            "NotFound": json_response(ref_schema("CallErrorNotFound"),
                "The operation is unknown or Internal."),
            "InvalidInput": one_of_response(),
            "Internal": json_response(ref_schema("CallErrorInternal"),
                "Dispatcher failure."),
            "Timeout": json_response(ref_schema("CallErrorTimeout"),
                "The bounded Once-op dispatch exceeded the 30 s gateway deadline (retryable)."),
        },
        "schemas": schemas
    })
}

/// The operation-declared codes, deduplicated and sorted across every
/// status (BTreeMap-fold, PRJ-12 determinism): the code set the merged
/// `CallError_<code>` components and `BatchOperationError`'s enum cover.
fn operation_error_codes(operation_errors: &BTreeMap<u16, Value>) -> Vec<String> {
    let mut codes = std::collections::BTreeSet::new();
    for projection in operation_errors.values() {
        if let Some(enum_codes) = projection
            .pointer("/content/application~1json/schema/properties/code/enum")
            .and_then(Value::as_array)
        {
            codes.extend(
                enum_codes
                    .iter()
                    .filter_map(|v| v.as_str().map(str::to_string)),
            );
        }
    }
    codes.into_iter().collect()
}

fn one_of_response() -> Value {
    json_response(
        one_of_refs(&[
            "CallErrorInvalidInput".to_string(),
            "CallErrorInvalidOperationType".to_string(),
        ]),
        "Dispatch-path client fault.",
    )
}

fn call_request_schema() -> Value {
    json!({
        "type": "object",
        "required": ["operation"],
        "properties": {
            "operation": {
                "type": "string",
                "description": "The fully-qualified operation name to invoke. A leading slash is accepted and stripped."
            },
            "input": {
                "type": "object",
                "description": "The JSON input object passed to the operation.",
                "default": {}
            }
        }
    })
}

fn call_ok_schema() -> Value {
    json!({
        "type": "object",
        "required": ["request_id", "result", "output"],
        "properties": {
            "request_id": { "type": "string" },
            "result": { "type": "string", "enum": ["ok"] },
            "output": { "description": "The operation's output value." }
        }
    })
}

fn call_error_shape_schema() -> Value {
    json!({
        "type": "object",
        "required": ["code", "message", "retryable"],
        "properties": {
            "code": { "type": "string" },
            "message": { "type": "string" },
            "retryable": { "type": "boolean" },
            "details": { "type": "object", "description": "Optional structured details (e.g. retry_after for Retry-After statuses)." }
        }
    })
}

fn call_error_schema(code: &str) -> Value {
    let mut schema = call_error_shape_schema();
    schema["properties"]["code"] = json!({ "type": "string", "enum": [code] });
    schema
}

/// The generic in-band error variant: the `CallError` wire shape with
/// `code` carrying the operation-declared enum (PRJ-16b). With no
/// declared operation errors the arm collapses to the unpinned shape
/// (still runtime-true: a foreign code surfaces as `500 INTERNAL`,
/// whose envelope is exactly this shape).
fn error_variant_schema(codes: &[String]) -> Value {
    let mut schema = call_error_shape_schema();
    if !codes.is_empty() {
        schema["properties"]["code"] = json!({
            "type": "string",
            "enum": codes
        });
    }
    schema
}

fn failure_envelope_schema(operation_errors: &BTreeMap<u16, Value>) -> Value {
    let _ = operation_errors;
    json!({
        "type": "object",
        "required": ["code", "message", "retryable"],
        "properties": {
            "code": { "type": "string" },
            "message": { "type": "string" },
            "retryable": { "type": "boolean" },
            "details": { "type": "object", "description": "Optional structured details (e.g. retry_after for Retry-After statuses)." }
        }
    })
}

fn search_operation_schema() -> Value {
    json!({
        "type": "object",
        "required": ["name", "namespace", "op_type"],
        "properties": {
            "name": { "type": "string", "description": "Fully-qualified operation name (e.g. fs/readFile)." },
            "namespace": { "type": "string", "description": "The segment before the first / (empty when the name has none)." },
            "op_type": { "type": "string", "enum": ["query", "mutation", "sub", "pub"] }
        }
    })
}

fn search_response_schema() -> Value {
    envelope_ref_schema("operations_array_schema()")
}

fn operations_array_schema() -> Value {
    json!({
        "type": "object",
        "required": ["operations"],
        "properties": {
            "operations": {
                "type": "array",
                "items": ref_schema("SearchOperation")
            }
        }
    })
}

/// An envelope (`{request_id, result, output}`) wrapping the given
/// output schema value.
fn envelope_ref_schema(output: &str) -> Value {
    let output_schema = if output == "operations_array_schema()" {
        operations_array_schema()
    } else {
        ref_schema(output)
    };
    json!({
        "type": "object",
        "required": ["request_id", "result", "output"],
        "properties": {
            "request_id": { "type": "string" },
            "result": { "type": "string", "enum": ["ok"] },
            "output": output_schema
        }
    })
}

fn operation_spec_output_schema() -> Value {
    json!({
        "type": "object",
        "required": ["name", "namespace", "op_type", "visibility", "input_schema", "output_schema", "error_schemas", "access_control"],
        "properties": {
            "name": { "type": "string" },
            "namespace": { "type": "string" },
            "op_type": { "type": "string", "enum": ["query", "mutation", "sub", "pub"] },
            "visibility": { "type": "string", "enum": ["external", "internal"] },
            "input_schema": { "description": "JSON Schema for the operation's input." },
            "output_schema": { "description": "JSON Schema for the operation's output." },
            "error_schemas": {
                "type": "array",
                "items": { "type": "object" }
            },
            "access_control": {
                "type": "object",
                "description": "The operation's AccessControl requirements (required_scopes, required_scopes_any, resource_type, resource_action)."
            },
            "channel_open": {
                "type": "boolean",
                "description": "Marker (ADR-047): when true, the op's stream is binary and the channels layer allocates a data channel for it. Absent for JSON-stream ops."
            },
            "publish_schema": {
                "description": "Schema for each published chunk's input (Pub ops only, ADR-046 §4). Absent for Query/Mutation/Sub ops and Pub ops with no per-chunk validation."
            }
        }
    })
}

fn batch_result_entry_schema() -> Value {
    json!({
        "type": "object",
        "required": ["request_id", "result"],
        "properties": {
            "request_id": { "type": "string" },
            "result": { "type": "string", "enum": ["ok", "error"] },
            "output": { "description": "The operation's output when result=ok." },
            "error": ref_schema(SCHEMA_BATCH_ERROR)
        }
    })
}

/// The in-band error of a failed batch entry (PRJ-16b): each failed
/// entry carries the serialized `CallError` — a protocol code (the six
/// pinned components) or an operation-declared code (the generic arm's
/// enum). An in-band payload is the raw error, not the 500-remap the
/// HTTP status path applies, so the operation-code arm stays generic.
fn batch_error_schema() -> Value {
    json!({
        "description": "The in-band error of one failed batch entry: the serialized CallError as the runtime emits it — any protocol or operation-declared code lands here.",
        "oneOf": [
            ref_schema("CallErrorInvalidInput"),
            ref_schema("CallErrorInvalidOperationType"),
            ref_schema("CallErrorForbidden"),
            ref_schema("CallErrorNotFound"),
            ref_schema("CallErrorInternal"),
            ref_schema("CallErrorTimeout"),
            ref_schema(SCHEMA_BATCH_OPERATION_ERROR)
        ]
    })
}

fn batch_response_schema() -> Value {
    json!({
        "type": "object",
        "required": ["results"],
        "properties": {
            "results": {
                "type": "array",
                "items": ref_schema("BatchResultEntry")
            }
        }
    })
}

fn batch_cap_exceeded_schema() -> Value {
    json!({
        "type": "object",
        "required": ["code", "message"],
        "properties": {
            "code": { "type": "string", "enum": ["INVALID_INPUT"] },
            "message": { "type": "string" }
        }
    })
}

fn sse_stream_schema() -> Value {
    json!({
        "type": "string",
        "description": "text/event-stream body. Framing: keep-alive comment frames every 15 s; Success events carry `data:` (the JSON-serialized output value) and `retry: 15000`; an Error event carries `event: error`, data: the serialized CallError and `retry: 15000`, and is terminal (followed by stream close)."
    })
}

fn ndjson_body_schema() -> Value {
    json!({
        "type": "string",
        "description": "NDJSON: the first line is {\"operation\": \"/service/op\", \"chunk\": {...}}; subsequent lines are chunk values (one published chunk per line; 2 MiB per-line cap)."
    })
}

/// An inline JSON schema reference into `components.schemas`.
fn ref_schema(name: &str) -> Value {
    json!({ "$ref": format!("{RESPONSE_REF}{name}") })
}

/// A `oneOf` over inline JSON schema references.
fn one_of_refs(names: &[String]) -> Value {
    let variants: Vec<Value> = names
        .iter()
        .map(|n| json!({ "$ref": format!("{RESPONSE_REF}{n}") }))
        .collect();
    json!({ "oneOf": variants })
}

/// A response object reference into the shared `components.responses`
/// error shapes this projection defines.
fn ref_response(name: &str) -> Value {
    json!({ "$ref": format!("#/components/responses/{name}") })
}

/// A `400` response whose body is the web framework's plain-text
/// rejection — NOT the `CallError` JSON envelope. The route extractors
/// (`Json<CallRequest>`, `Json<Vec<CallRequest>>`, `Query<SchemaQuery>`)
/// reject malformed request framing before dispatch; their bodies are
/// plain text (axum default). The sibling slots
/// ([`plain_text_unsupported_media_type`],
/// [`plain_text_extractor_shape_rejection`]) document the 415 and 422
/// rejections the same extractor layer emits (PRJ-19). These are known
/// runtime gaps (PRJ-03 doc-align): the JSON `CallError` shape is only
/// guaranteed on the dispatch-path errors (401/403/404/422/500/504 and
/// operation-declared statuses).
fn plain_text_extractor_rejection() -> Value {
    json!({
        "description": "Malformed request framing (syntactically invalid JSON body / malformed query string). NOTE: the framework's extractor rejects with a PLAIN-TEXT body (not the CallError JSON shape this doc uses elsewhere) — a known runtime gap (review-001 PRJ-03). Dispatch-path client faults use 422 with the JSON envelope instead. A body exceeding the gateway's request-body limit is a plain-text 413.",
        "content": {
            "text/plain": {
                "schema": { "type": "string" }
            }
        }
    })
}

/// A `415` response: the plain-text rejection emitted when the request
/// carries no `Content-Type` (or a non-JSON one on a JSON-body
/// endpoint) (PRJ-19). Same extractor layer as the 400/422 slots.
fn plain_text_unsupported_media_type() -> Value {
    json!({
        "description": "Missing Content-Type (or a type the endpoint does not consume). The framework's extractor rejects with a PLAIN-TEXT body — not the CallError JSON shape (PRJ-19).",
        "content": {
            "text/plain": {
                "schema": { "type": "string" }
            }
        }
    })
}

/// A `422` response: the plain-text rejection emitted when the body is
/// syntactically valid JSON but fails deserialization into the
/// endpoint's request type (e.g. a JSON array where the endpoint reads
/// one object, or a per-item shape mismatch on `/batch`) (PRJ-19).
/// Dispatch-path 422s (invalid *input data*) use the JSON envelope
/// instead.
fn plain_text_extractor_shape_rejection() -> Value {
    json!({
        "description": "The body is valid JSON but does not deserialize into the endpoint's request shape. The framework's extractor rejects with a PLAIN-TEXT body — not the CallError JSON shape (PRJ-19).",
        "content": {
            "text/plain": {
                "schema": { "type": "string" }
            }
        }
    })
}

/// The operation-level error projections, keyed by the registry-declared
/// `http_status`. Deterministic: registry iteration order is folded into
/// BTreeMaps before anything is emitted (PRJ-12).
fn gather_operation_errors(registry: &OperationRegistry) -> BTreeMap<u16, Value> {
    let mut by_status: BTreeMap<u16, BTreeMap<String, ErrorDefinition>> = BTreeMap::new();
    for spec in registry.list_operations() {
        for error in &spec.error_schemas {
            let Some(status) = error.http_status else {
                continue;
            };
            if is_protocol_status(status) && !is_http_prefixed_code(&error.code) {
                continue;
            }
            by_status
                .entry(status)
                .or_default()
                .entry(error.code.clone())
                .or_insert_with(|| error.clone());
        }
    }
    let mut out: BTreeMap<u16, Value> = BTreeMap::new();
    for (status, codes) in by_status {
        let runtime_diverges = codes.keys().any(|code| !is_http_prefixed_code(code));
        out.insert(
            status,
            json!({
                "description": "The registry declares operation-level error codes at this status. Runtime note: the gateway error mapper is purely code-driven; a code without an HTTP_<status> prefix surfaces as 500 instead of this status (x-runtime-behavior: 500).",
                "x-runtime-behavior": if runtime_diverges { json!(500) } else { Value::Null },
                "content": {
                    "application/json": {
                        "schema": {
                            "type": "object",
                            "properties": {
                                "code": {
                                    "type": "string",
                                    "enum": codes.keys().cloned().collect::<Vec<String>>(),
                                },
                                "message": { "type": "string" },
                                "retryable": { "type": "boolean" },
                                "details": { "type": "object", "description": "Optional structured details (e.g. retry_after for Retry-After statuses)." }
                            },
                            "required": ["code", "message", "retryable"]
                        }
                    }
                }
            }),
        );
    }
    out
}

fn is_protocol_status(status: u16) -> bool {
    matches!(
        status,
        STATUS_BAD_REQUEST
            | STATUS_UNAUTHORIZED
            | STATUS_FORBIDDEN
            | STATUS_NOT_FOUND
            | STATUS_UNPROCESSABLE
            | STATUS_INTERNAL
            | STATUS_TIMEOUT
    )
}

fn is_http_prefixed_code(code: &str) -> bool {
    code.starts_with(HTTP_PREFIX) && code[HTTP_PREFIX.len()..].parse::<u16>().is_ok()
}

#[cfg(test)]
mod tests {
    use super::*;
    use alkcall::core::types::Capabilities;
    use alkcall::protocol::wire::ResponseEnvelope;
    use alkcall::registry::registration::{
        make_handler, HandlerKind, HandlerRegistration, OperationProvenance,
    };
    use alkcall::registry::spec::{
        AccessControl, ErrorDefinition, OperationSpec, OperationType, Visibility,
    };
    use serde_json::{json, Map};

    fn noop_handler() -> alkcall::registry::registration::Handler {
        make_handler(|_input, ctx| async move { ResponseEnvelope::ok(ctx.request_id, Value::Null) })
    }

    fn register(registry: &mut OperationRegistry, spec: OperationSpec) {
        registry
            .register(HandlerRegistration::new(
                spec,
                HandlerKind::Once(noop_handler()),
                OperationProvenance::Local,
                None,
                None,
                Capabilities::new(),
            ))
            .unwrap();
    }

    fn external_spec(name: &str, errors: Vec<ErrorDefinition>) -> OperationSpec {
        OperationSpec::new(
            name,
            OperationType::Query,
            Visibility::External,
            json!({}),
            json!({}),
            errors,
            AccessControl::default(),
            None,
        )
    }

    fn error(code: &str, http_status: Option<u16>) -> ErrorDefinition {
        ErrorDefinition {
            code: code.to_string(),
            description: format!("error {code}"),
            schema: json!({ "type": "object" }),
            http_status,
        }
    }

    fn paths_object(spec: &OpenAPISpec) -> &Map<String, Value> {
        spec.raw
            .get("paths")
            .and_then(Value::as_object)
            .unwrap_or_else(|| panic!("paths object present"))
    }

    fn path(spec: &OpenAPISpec, name: &str) -> Map<String, Value> {
        paths_object(spec)
            .get(name)
            .and_then(Value::as_object)
            .unwrap_or_else(|| panic!("path {name} present"))
            .clone()
    }

    fn responses(spec: &OpenAPISpec, name: &str, method: &str) -> Map<String, Value> {
        path(spec, name)
            .get(method)
            .and_then(Value::as_object)
            .unwrap_or_else(|| panic!("operation {method} {name} present"))
            .get("responses")
            .and_then(Value::as_object)
            .unwrap_or_else(|| panic!("responses present"))
            .clone()
    }

    fn response_schema(response: &Value) -> &Value {
        response
            .get("content")
            .and_then(|c: &Value| c.get("application/json"))
            .and_then(|c: &Value| c.get("schema"))
            .unwrap_or_else(|| panic!("application/json schema present"))
    }

    fn code_enum(spec: &OpenAPISpec, schema: &Value) -> Vec<String> {
        let schema = match schema.get("$ref").and_then(Value::as_str) {
            Some(reference) => spec.raw.pointer(&reference[1..]).expect("ref target"),
            None => schema,
        };
        schema
            .pointer("/properties/code/enum")
            .and_then(Value::as_array)
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(str::to_string))
                    .collect()
            })
            .unwrap_or_default()
    }

    fn one_of_refs_of(schema: &Value) -> Vec<String> {
        schema
            .get("oneOf")
            .and_then(Value::as_array)
            .map(|a| {
                a.iter()
                    .filter_map(|v| v.get("$ref").and_then(Value::as_str))
                    .map(str::to_string)
                    .collect()
            })
            .unwrap_or_default()
    }

    // --- structural basics ------------------------------------------------

    #[test]
    fn empty_registry_produces_six_gateway_paths() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let paths = paths_object(&spec);
        assert_eq!(paths.len(), 6);
        for name in [
            PATH_SEARCH,
            PATH_SCHEMA,
            PATH_CALL,
            PATH_BATCH,
            PATH_SUBSCRIBE,
            PATH_PUBLISH,
        ] {
            assert!(paths.contains_key(name), "{name} present");
        }
    }

    #[test]
    fn registry_with_operations_does_not_add_per_operation_paths() {
        let mut registry = OperationRegistry::new();
        register(&mut registry, external_spec("fs/readFile", vec![]));
        register(&mut registry, external_spec("agent/chat", vec![]));
        let spec = to_openapi(&registry).unwrap();
        let paths = paths_object(&spec);
        assert_eq!(paths.len(), 6);
        assert!(!paths.contains_key("/fs/readFile"));
        assert!(!paths.contains_key("/agent/chat"));
    }

    #[test]
    fn info_version_is_1_4_0_after_gw16_status_unification() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let version = spec
            .raw
            .pointer("/info/version")
            .and_then(Value::as_str)
            .unwrap();
        assert_eq!(version, GATEWAY_VERSION);
        assert_eq!(
            version, "1.4.0",
            "minor bump: status-mapping truthfulness corrections — the 422 slots are where INVALID_INPUT was already documented (ADR-045)"
        );
    }

    #[test]
    fn openapi_field_is_3_0_0_and_title_present() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        assert_eq!(
            spec.raw.get("openapi").and_then(Value::as_str),
            Some(OPENAPI_VERSION)
        );
        assert_eq!(
            spec.raw.pointer("/info/title").and_then(Value::as_str),
            Some(GATEWAY_TITLE)
        );
    }

    #[test]
    fn doc_validates_against_openapiv3_parsing() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let text = serde_json::to_string(&spec.raw).unwrap();
        let parsed: openapiv3::OpenAPI =
            serde_json::from_str(&text).expect("gateway doc parses as OpenAPI 3.0");
        assert_eq!(parsed.openapi, OPENAPI_VERSION);
        assert_eq!(spec.paths.len(), 6);
    }

    #[test]
    fn doc_with_operation_errors_validates_against_openapiv3() {
        let registry = sample_registry();
        let spec = to_openapi(&registry).unwrap();
        let text = serde_json::to_string(&spec.raw).unwrap();
        let parsed: openapiv3::OpenAPI =
            serde_json::from_str(&text).expect("populated doc parses as OpenAPI 3.0");
        let components = parsed.components.expect("components present");
        assert!(
            components
                .schemas
                .keys()
                .any(|name| name.starts_with("CallError_")),
            "merged operation-error components present: {:?}",
            components.schemas.keys().collect::<Vec<_>>()
        );
        assert_eq!(spec.paths.len(), 6);
    }

    // --- PRJ-15: securitySchemes / security -------------------------------

    #[test]
    fn components_declare_the_bearer_security_scheme() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let scheme = spec
            .raw
            .pointer("/components/securitySchemes/bearerAuth")
            .expect("bearerAuth scheme declared (ADR-004)");
        assert_eq!(scheme.get("type"), Some(&json!("http")));
        assert_eq!(scheme.get("scheme"), Some(&json!("bearer")));
        assert_eq!(
            spec.raw
                .get("security")
                .and_then(Value::as_array)
                .map(|a| a.len()),
            Some(1)
        );
    }

    // --- PRJ-01: /search envelope + item fields ----------------------------

    #[test]
    fn search_200_documents_the_envelope_and_real_item_fields() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let ok = responses(&spec, PATH_SEARCH, "get")
            .get("200")
            .unwrap()
            .clone();
        assert_eq!(
            ok.get("description").and_then(Value::as_str),
            Some("The AccessControl-filtered operation listing under `output` (GW-02). Items carry name, namespace, and op_type; forbidden ops are omitted from the listing — the request does not fail. Cache-Control: no-store, Vary: Authorization.")
        );
        let schema = response_schema(&ok);
        assert_eq!(
            schema.get("$ref").and_then(Value::as_str),
            Some("#/components/schemas/SearchResponse")
        );
        let resolved = spec
            .raw
            .pointer("/components/schemas/SearchResponse")
            .unwrap();
        assert_eq!(
            resolved.pointer("/properties/result/enum"),
            Some(&json!(["ok"]))
        );
        assert!(resolved.pointer("/properties/request_id").is_some());
        let operations = resolved.pointer("/properties/output/properties/operations");
        assert!(operations.is_some(), "operations array under output");
        let item = resolved
            .pointer("/properties/output/properties/operations/items/$ref")
            .and_then(Value::as_str)
            .unwrap();
        assert_eq!(item, "#/components/schemas/SearchOperation");
        let item_schema = &spec.raw["components"]["schemas"]["SearchOperation"];
        assert!(item_schema.pointer("/properties/name").is_some());
        assert!(item_schema.pointer("/properties/namespace").is_some());
        assert!(item_schema.pointer("/properties/op_type/enum").is_some());
        assert!(
            item_schema.pointer("/properties/description").is_none(),
            "services/list items carry no description field (PRJ-01)"
        );
        let summary = spec.raw["paths"][PATH_SEARCH]["get"]["summary"]
            .as_str()
            .unwrap();
        assert!(
            !summary.contains("description"),
            "PRJ-01: summary must not claim descriptions"
        );
        assert_eq!(
            ok.pointer("/description").and_then(Value::as_str),
            Some("The AccessControl-filtered operation listing under `output` (GW-02). Items carry name, namespace, and op_type; forbidden ops are omitted from the listing — the request does not fail. Cache-Control: no-store, Vary: Authorization.")
        );
    }

    #[test]
    fn search_does_not_document_401_403_but_documents_404() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_SEARCH, "get");
        assert!(
            !responses.contains_key("401"),
            "PRJ-15: /search 401 cannot occur"
        );
        assert!(
            !responses.contains_key("403"),
            "PRJ-15: /search 403 cannot occur"
        );
        assert!(
            responses.contains_key("404"),
            "PRJ-15: /search 404 can occur"
        );
    }

    // --- PRJ-02: /schema envelope + full spec ------------------------------

    #[test]
    fn schema_200_documents_the_envelope_and_full_spec() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let ok = responses(&spec, PATH_SCHEMA, "get")
            .get("200")
            .unwrap()
            .clone();
        let schema = response_schema(&ok);
        assert_eq!(
            schema.get("$ref").and_then(Value::as_str),
            Some("#/components/schemas/SchemaResponse")
        );
        let resolved = spec
            .raw
            .pointer("/components/schemas/SchemaResponse")
            .unwrap();
        assert_eq!(
            resolved.pointer("/properties/result/enum"),
            Some(&json!(["ok"]))
        );
        let output_ref = resolved
            .pointer("/properties/output/$ref")
            .and_then(Value::as_str)
            .unwrap();
        assert_eq!(output_ref, "#/components/schemas/OperationSpecOutput");
        let output = &spec.raw["components"]["schemas"]["OperationSpecOutput"];
        for field in [
            "name",
            "namespace",
            "op_type",
            "visibility",
            "input_schema",
            "output_schema",
            "error_schemas",
            "access_control",
            "channel_open",
            "publish_schema",
        ] {
            assert!(
                output.pointer(&format!("/properties/{field}")).is_some(),
                "OperationSpecOutput carries {field} (PRJ-02)"
            );
        }
    }

    #[test]
    fn schema_documents_404_and_401_403() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_SCHEMA, "get");
        for status in [
            "200", "400", "401", "403", "404", "415", "422", "500", "504",
        ] {
            assert!(
                responses.contains_key(status),
                "/schema {status} documented"
            );
        }
    }

    // --- PRJ-03: 422 vs 400, extractor-rejection gap -----------------------

    #[test]
    fn call_documents_422_not_400_invalid_input() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_CALL, "post");
        assert!(
            responses.contains_key("400"),
            "extractor rejection documented"
        );
        let content = &responses["400"]["content"];
        assert!(content.get("text/plain").is_some());
        assert!(content.get("application/json").is_none());
        let r422 = responses.get("422").unwrap();
        let schema = response_schema(r422);
        let refs = one_of_refs_of(schema);
        assert!(refs.contains(&"#/components/schemas/CallErrorInvalidInput".to_string()));
        assert!(refs.contains(&"#/components/schemas/CallErrorInvalidOperationType".to_string()));
        assert_eq!(
            spec.raw["components"]["schemas"]["CallErrorInvalidInput"]
                .pointer("/properties/code/enum/0"),
            Some(&json!("INVALID_INPUT")),
            "422 schema carries an INVALID_INPUT code enum"
        );
    }

    #[test]
    fn call_400_documents_the_plain_text_extractor_gap() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_CALL, "post");
        let r400 = responses_map.get("400").unwrap();
        let text = r400["content"]["text/plain"]["schema"]["type"].as_str();
        assert_eq!(text, Some("string"));
        let description = r400.get("description").and_then(Value::as_str).unwrap();
        assert!(
            description.contains("PLAIN-TEXT body"),
            "the extractor-rejection gap is documented on the 400 itself: {description}"
        );
        assert!(r400["content"].get("application/json").is_none());
    }

    #[test]
    fn batch_documents_422_json_shape_for_the_cap_reject() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_BATCH, "post");
        assert!(
            !responses.contains_key("400"),
            "GW-16: the batch cap reject moved to the INVALID_INPUT → 422 mapping"
        );
        let schema = response_schema(responses.get("422").unwrap());
        let refs = one_of_refs_of(schema);
        assert!(refs.contains(&"#/components/schemas/BatchCapExceeded".to_string()));
    }

    #[test]
    fn call_and_search_document_the_extractor_415_and_422_slots() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        for (path_name, method, statuses) in [
            (PATH_CALL, "post", vec!["400", "415"]),
            (PATH_SEARCH, "get", vec!["400", "415", "422"]),
            (PATH_SCHEMA, "get", vec!["400", "415"]),
            (PATH_SUBSCRIBE, "post", vec!["400", "415", "422"]),
        ] {
            let responses_map = responses(&spec, path_name, method);
            for status in statuses {
                let slot = responses_map
                    .get(status)
                    .unwrap_or_else(|| panic!("{path_name} {status} documented (PRJ-19)"));
                let text_type = slot
                    .get("content")
                    .and_then(|c| c.get("text/plain"))
                    .and_then(|c| c.get("schema"))
                    .and_then(|s| s.get("type"))
                    .and_then(Value::as_str);
                assert!(
                    text_type == Some("string"),
                    "{path_name} {status} is a plain-text extractor slot (PRJ-19)"
                );
            }
        }
        let call_responses_map = responses(&spec, PATH_CALL, "post");
        let call_422 = call_responses_map.get("422").unwrap();
        assert!(
            call_422
                .get("description")
                .and_then(Value::as_str)
                .unwrap()
                .contains("plain-text body"),
            "the /call 422 description distinguishes the extractor variant"
        );
    }

    #[test]
    fn search_drift_check_after_prj19_additions() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_SEARCH, "get");
        assert!(
            !responses.contains_key("401") && !responses.contains_key("403"),
            "PRJ-15 unaffected by PRJ-19 additions: /search still carries no 401/403"
        );
        assert!(responses.contains_key("404"));
    }

    // --- PRJ-04: operation-error projection honesty ------------------------

    #[test]
    fn operation_errors_projected_onto_call_with_runtime_behavior_annotated() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec("fs/readFile", vec![error("RATE_LIMITED", Some(429))]),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_CALL, "post");
        let r429 = responses.get("429").unwrap();
        let schema = response_schema(r429);
        let codes = code_enum(&spec, schema);
        assert!(codes.contains(&"RATE_LIMITED".to_string()), "{codes:?}");
        assert_eq!(
            r429.get("x-runtime-behavior"),
            Some(&json!(500)),
            "PRJ-04: non-HTTP_* code documented at its declared status but annotated 500-at-runtime"
        );
        assert!(
            spec.raw["paths"][PATH_CALL]["post"]["responses"]["500"]
                .to_string()
                .contains("operation-level error code without HTTP_ prefix"),
            "the 500 response description explains the runtime landing zone"
        );
    }

    #[test]
    fn http_prefixed_error_code_projects_to_status_without_annotation() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec("svc/op", vec![error("HTTP_429", Some(429))]),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_CALL, "post");
        let r429 = responses_map.get("429").unwrap();
        let codes = code_enum(&spec, response_schema(r429));
        assert!(codes.contains(&"HTTP_429".to_string()));
        assert_eq!(
            r429.get("x-runtime-behavior"),
            Some(&Value::Null),
            "HTTP_-prefixed codes surface at their declared status at runtime: no divergence"
        );
    }

    #[test]
    fn protocol_status_declared_by_non_http_prefixed_code_is_dropped() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec("svc/op", vec![error("RATE_LIMITED", Some(404))]),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_CALL, "post");
        let r404 = responses_map.get("404").unwrap();
        assert_eq!(
            response_schema(r404).get("$ref").and_then(Value::as_str),
            Some("#/components/responses/NotFound"),
            "no operation-error merge: the shared protocol 404 response stands"
        );
    }

    #[test]
    fn http_prefixed_protocol_status_merges_into_shared_response() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec("svc/op", vec![error("HTTP_404", Some(404))]),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_CALL, "post");
        let r404 = responses_map.get("404").unwrap();
        let schema = response_schema(r404);
        assert_eq!(
            schema.get("$ref").and_then(Value::as_str),
            Some("#/components/responses/NotFound"),
            "PRJ-17: the shared NOT_FOUND response survives the merge"
        );
        let variant_refs: Vec<String> = schema
            .get("oneOf")
            .and_then(Value::as_array)
            .expect("PRJ-17: op-declared variants appended to the shared response's oneOf")
            .iter()
            .filter_map(|v| v.get("$ref").and_then(Value::as_str).map(str::to_string))
            .collect();
        assert!(
            variant_refs.contains(&"#/components/schemas/CallError_HTTP_404".to_string()),
            "the HTTP_404 variant is listed alongside the shared response: {variant_refs:?}"
        );
        assert!(
            spec.raw["components"]["schemas"]["CallError_HTTP_404"]
                .pointer("/properties/code/enum/0")
                .is_some(),
            "the merged variant component is defined"
        );
    }

    #[test]
    fn http_prefixed_unauthorized_status_appends_to_401_one_of() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec("svc/op", vec![error("HTTP_401", Some(401))]),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_CALL, "post");
        let refs = one_of_refs_of(response_schema(responses_map.get("401").unwrap()));
        assert!(refs.contains(&"#/components/schemas/CallErrorForbidden".to_string()));
        assert!(refs.contains(&"#/components/schemas/CallError_HTTP_401".to_string()));
    }

    #[test]
    fn operation_error_without_http_status_not_projected() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec("svc/op", vec![error("SOME_ERROR", None)]),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_CALL, "post");
        assert!(
            responses.len() < 10,
            "no status-less error projected: {responses:?}"
        );
    }

    #[test]
    fn operation_errors_from_multiple_ops_dedupe_and_merge_deterministically() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec("svc/a", vec![error("RATE_LIMITED", Some(429))]),
        );
        register(
            &mut registry,
            external_spec("svc/b", vec![error("TOO_MANY_REQUESTS", Some(429))]),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_CALL, "post");
        let r429 = responses_map.get("429").unwrap();
        let codes = code_enum(&spec, response_schema(r429));
        assert_eq!(
            codes,
            vec!["RATE_LIMITED", "TOO_MANY_REQUESTS"],
            "sorted, deduped"
        );
    }

    #[test]
    fn internal_operations_excluded_from_error_projection() {
        let registry = OperationRegistry::new();
        registry
            .register(HandlerRegistration::new(
                OperationSpec::new(
                    "internal/op",
                    OperationType::Query,
                    Visibility::Internal,
                    json!({}),
                    json!({}),
                    vec![error("INTERNAL_ERROR", Some(418))],
                    AccessControl::default(),
                    None,
                ),
                HandlerKind::Once(noop_handler()),
                OperationProvenance::Local,
                None,
                None,
                Capabilities::new(),
            ))
            .unwrap();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_CALL, "post");
        assert!(
            !responses.contains_key("418"),
            "internal op errors not projected"
        );
    }

    // --- PRJ-05: /subscribe 200 + in-band error contract -------------------

    #[test]
    fn subscribe_documents_200_and_extractor_slots() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_SUBSCRIBE, "post");
        assert_eq!(
            responses.len(),
            4,
            "200 + extractor 400/415/422 only: {responses:?}"
        );
        assert!(responses.contains_key("200"));
        assert!(responses.contains_key("400"));
        assert!(responses.contains_key("415"));
        assert!(responses.contains_key("422"));
        let content = &responses["200"]["content"];
        assert!(content.get("text/event-stream").is_some());
        assert!(content.get("application/json").is_none());
    }

    #[test]
    fn subscribe_200_documents_the_event_error_contract() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_SUBSCRIBE, "post");
        let ok = responses_map.get("200").unwrap();
        let description = ok.get("description").and_then(Value::as_str).unwrap();
        for needle in [
            "event:error",
            "terminal",
            "unknown op",
            "Internal op",
            "ACL denial",
            "15 s",
        ] {
            assert!(
                description.contains(needle),
                "200 description documents {needle}: {description}"
            );
        }
    }

    // --- PRJ-14: $refs, no duplicate inlining ------------------------------

    #[test]
    fn request_bodies_use_refs_not_inlined_schemas() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        for path_name in [PATH_CALL, PATH_SUBSCRIBE] {
            let schema = &spec.raw["paths"][path_name]["post"]["requestBody"]["content"]
                ["application/json"]["schema"];
            assert_eq!(
                schema.get("$ref"),
                Some(&json!("#/components/schemas/CallRequest")),
                "{path_name} requestBody uses $ref (PRJ-14)"
            );
        }
        let batch_items = &spec.raw["paths"][PATH_BATCH]["post"]["requestBody"]["content"]
            ["application/json"]["schema"]["items"];
        assert_eq!(
            batch_items.get("$ref"),
            Some(&json!("#/components/schemas/CallRequest"))
        );
    }

    #[test]
    fn call_request_component_shape_matches_the_wire_struct() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let schema = &spec.raw["components"]["schemas"]["CallRequest"];
        assert_eq!(schema.get("type"), Some(&json!("object")));
        assert!(schema.pointer("/properties/operation").is_some());
        assert!(schema.pointer("/properties/input").is_some());
        assert!(schema.pointer("/required/0").is_some());
        assert!(schema.as_object().unwrap().contains_key("required"));
    }

    // --- golden: byte-identical regeneration (determinism, PRJ-12) --------

    fn sample_registry() -> OperationRegistry {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec(
                "svc/alpha",
                vec![
                    error("RATE_LIMITED", Some(429)),
                    error("HTTP_404", Some(429)),
                ],
            ),
        );
        register(
            &mut registry,
            external_spec("svc/beta", vec![error("TOO_MANY_REQUESTS", Some(429))]),
        );
        register(&mut registry, external_spec("svc/bare", vec![]));
        registry
    }

    #[test]
    fn same_registry_yields_byte_identical_docs() {
        let raw_1 = to_openapi(&sample_registry()).unwrap().raw;
        let raw_2 = to_openapi(&sample_registry()).unwrap().raw;
        let a = serde_json::to_string_pretty(&raw_1).unwrap();
        let b = serde_json::to_string_pretty(&raw_2).unwrap();
        assert_eq!(a, b, "identical registry state → byte-identical doc");
    }

    #[test]
    fn error_codes_are_sorted_within_each_status_key() {
        let mut registry = OperationRegistry::new();
        register(
            &mut registry,
            external_spec(
                "svc/multi",
                vec![
                    error("ZULU", Some(418)),
                    error("ALPHA", Some(418)),
                    error("MIKE", Some(418)),
                ],
            ),
        );
        let spec = to_openapi(&registry).unwrap();
        let responses_map = responses(&spec, PATH_CALL, "post");
        let r418 = responses_map.get("418").unwrap();
        assert_eq!(
            code_enum(&spec, response_schema(r418)),
            vec!["ALPHA", "MIKE", "ZULU"]
        );
    }

    // --- /publish contract --------------------------------------------------

    #[test]
    fn publish_has_post_method_with_ndjson_request_body() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let request_schema = spec.raw["paths"][PATH_PUBLISH]["post"]["requestBody"]["content"]
            ["application/x-ndjson"]["schema"]
            .clone();
        assert_eq!(
            request_schema.get("$ref"),
            Some(&json!("#/components/schemas/NdjsonBody")),
            "publish request body is x-ndjson via $ref"
        );
        let description = spec
            .raw
            .pointer("/components/schemas/NdjsonBody/description")
            .and_then(Value::as_str)
            .unwrap();
        assert!(description.contains("operation"), "{description}");
        assert!(description.contains("chunk"), "{description}");
    }

    #[test]
    fn publish_documents_the_full_status_set() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_PUBLISH, "post");
        for status in ["200", "401", "403", "404", "422", "500", "504"] {
            assert!(
                responses.contains_key(status),
                "/publish {status} documented"
            );
        }
        assert!(
            !responses.contains_key("400"),
            "GW-16: /publish framing faults are INVALID_INPUT → 422; no 400 slot remains"
        );
        assert!(
            !responses.contains_key("429") && !responses.contains_key("503"),
            "no operation-declared statuses on an empty registry"
        );
    }

    #[test]
    fn publish_401_and_422_carry_the_identity_split_codes() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_PUBLISH, "post");
        let refs_422 = one_of_refs_of(response_schema(&responses["422"]));
        assert!(refs_422.contains(&"#/components/schemas/CallErrorInvalidInput".to_string()));
        let refs_401 = one_of_refs_of(response_schema(&responses["401"]));
        assert!(
            refs_401.contains(&"#/components/schemas/CallErrorInvalidOperationType".to_string())
        );
    }

    #[test]
    fn publish_422_documents_the_unified_framing_and_chunk_contract() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_PUBLISH, "post");
        let refs_422 = one_of_refs_of(response_schema(&responses["422"]));
        assert_eq!(
            refs_422,
            vec![
                "#/components/schemas/CallErrorInvalidInput".to_string(),
                "#/components/schemas/CallErrorInvalidOperationType".to_string(),
            ],
            "GW-16: /publish 422 oneOf carries the framing INVALID_INPUT plus INVALID_OPERATION_TYPE"
        );
        let description = responses["422"]
            .get("description")
            .and_then(Value::as_str)
            .unwrap();
        assert!(
            description.contains("first line missing"),
            "GW-16: the 422 description names the framing faults now mapped 422: {description}"
        );
        assert!(
            !description.contains("reported INVALID_OPERATION_TYPE"),
            "the 422 description must not claim the 401-reported condition as a 422 outcome: {description}"
        );
    }

    // --- /call protocol statuses --------------------------------------------

    #[test]
    fn call_includes_all_protocol_level_error_statuses() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_CALL, "post");
        for status in [
            "200", "400", "401", "403", "404", "415", "422", "500", "504",
        ] {
            assert!(responses.contains_key(status), "/call {status} documented");
        }
        assert!(
            !responses.contains_key("429"),
            "empty registry: no 429 projected"
        );
        assert!(
            !responses.contains_key("503"),
            "empty registry: no 503 projected"
        );
    }

    #[test]
    fn call_401_covers_the_identity_split_codes() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_CALL, "post");
        let refs_401 = one_of_refs_of(response_schema(&responses["401"]));
        assert!(
            refs_401.contains(&"#/components/schemas/CallErrorForbidden".to_string()),
            "PRJ-20: FORBIDDEN lands at 401 without identity: {refs_401:?}"
        );
        assert!(
            refs_401.contains(&"#/components/schemas/CallErrorInvalidOperationType".to_string()),
            "PRJ-20: unauthenticated Sub/Pub call reports INVALID_OPERATION_TYPE at 401 (error.rs): {refs_401:?}"
        );
        let description = responses["401"]
            .get("description")
            .and_then(Value::as_str)
            .unwrap();
        assert!(
            description.contains("INVALID_OPERATION_TYPE"),
            "the 401 description names both codes: {description}"
        );
    }

    #[test]
    fn batch_documents_no_unreachable_500() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let responses = responses(&spec, PATH_BATCH, "post");
        assert!(
            !responses.contains_key("500"),
            "PRJ-21: batch dispatch failures are in-band entries; no HTTP 500 exists to document"
        );
    }

    #[test]
    fn batch_result_entry_error_refs_a_defined_component() {
        let registry = sample_registry();
        let spec = to_openapi(&registry).unwrap();
        let entry = &spec.raw["components"]["schemas"]["BatchResultEntry"];
        let error_ref = entry
            .pointer("/properties/error/$ref")
            .and_then(Value::as_str)
            .expect("BatchResultEntry.error is a $ref");
        let target = error_ref.trim_start_matches("#/components/schemas/");
        assert!(
            spec.raw["components"]["schemas"].get(target).is_some(),
            "PRJ-16b: {error_ref} resolves inside components.schemas"
        );
    }

    #[test]
    fn batch_error_component_covers_protocol_and_operation_codes() {
        let registry = sample_registry();
        let spec = to_openapi(&registry).unwrap();
        let batch_error = &spec.raw["components"]["schemas"]["BatchError"];
        let arms: Vec<&str> = batch_error
            .get("oneOf")
            .and_then(Value::as_array)
            .map(|a| {
                a.iter()
                    .filter_map(|v| v.get("$ref").and_then(Value::as_str))
                    .collect()
            })
            .unwrap_or_default();
        for expected in [
            "#/components/schemas/CallErrorNotFound",
            "#/components/schemas/CallErrorInternal",
            "#/components/schemas/BatchOperationError",
        ] {
            assert!(arms.contains(&expected), "PRJ-16b: {expected} in {arms:?}");
        }
    }

    #[test]
    fn no_extension_key_inside_components_schemas() {
        let registry = sample_registry();
        let spec = to_openapi(&registry).unwrap();
        let schemas = spec.raw["components"]["schemas"]
            .as_object()
            .expect("schemas object");
        for name in schemas.keys() {
            assert!(
                !name.starts_with("x-"),
                "PRJ-23: {name} is not a schema name — extension keys are illegal inside components.schemas"
            );
        }
    }

    #[test]
    fn protocol_error_component_schemas_pin_codes() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        let components = &spec.raw["components"]["schemas"];
        assert_eq!(
            components["CallErrorInvalidInput"].pointer("/properties/code/enum/0"),
            Some(&json!("INVALID_INPUT"))
        );
        assert_eq!(
            components["CallErrorForbidden"].pointer("/properties/code/enum/0"),
            Some(&json!("FORBIDDEN"))
        );
        assert_eq!(
            components["CallErrorTimeout"].pointer("/properties/code/enum/0"),
            Some(&json!("TIMEOUT"))
        );
    }

    #[test]
    fn call_and_publish_500s_include_the_operation_landing_zone() {
        let registry = OperationRegistry::new();
        let spec = to_openapi(&registry).unwrap();
        for path_name in [PATH_CALL, PATH_PUBLISH] {
            let responses_map = responses(&spec, path_name, "post");
            let r500 = responses_map.get("500").unwrap();
            let refs: Vec<&str> = response_schema(r500)
                .get("oneOf")
                .and_then(Value::as_array)
                .map(|a| {
                    a.iter()
                        .filter_map(|v| v.get("$ref").and_then(Value::as_str))
                        .collect()
                })
                .unwrap_or_default();
            let failure = if path_name == PATH_PUBLISH {
                "#/components/schemas/PublishFailure"
            } else {
                "#/components/schemas/CallFailure"
            };
            assert!(
                refs.contains(&"#/components/schemas/CallErrorInternal"),
                "{refs:?}"
            );
            assert!(refs.contains(&failure), "{refs:?}");
            assert!(r500
                .get("description")
                .and_then(Value::as_str)
                .unwrap()
                .contains("HTTP_"));
        }
        assert!(
            spec.raw["components"]["schemas"]
                .get("PublishFailure")
                .is_some(),
            "publish 500 oneOf references the publish-side failure component"
        );
    }
}