net-mesh 0.34.0

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

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

use parking_lot::Mutex;

use super::org::OrgId;
use crate::adapter::net::identity::EntityId;

/// Provisional ceiling on tracked in-flight+recent admissions
/// (plan §2.5: "constants frozen after measurement"). Sized so a
/// burst of legitimate concurrent callers fits comfortably while a
/// single caller cannot exhaust process memory with novel
/// `call_id`s. Flagged for OA-2 measurement before freeze.
pub const DEFAULT_MAX_REPLAY_ENTRIES: usize = 65_536;

/// Provisional per-caller ceiling (E1.5, verdict §10). Policy runs
/// AFTER replay insertion, so even a policy-vetoed VALID proof
/// consumes a slot; without a per-caller sub-ceiling a single
/// credentialed caller could fill the whole global map and starve
/// every other org fail-closed. Sized to admit a healthy concurrent
/// burst from one caller while leaving ample global headroom for
/// everyone else (16× fits under the global default). Flagged for
/// OA-2 measurement before freeze.
pub const DEFAULT_MAX_REPLAY_ENTRIES_PER_CALLER: usize = 4_096;

/// Entries reserved for the PROVIDER'S OWN owner org (§5).
///
/// The per-caller ceiling is correct in isolation and does not COMPOSE: with
/// `max_entries / max_entries_per_caller == 16`, sixteen identities saturate
/// the global map, after which the provider denies its OWN owner-org callers
/// fail-closed with a retryable `Unavailable`. Minting sixteen identities is
/// one org-admin action for a single grantee org, so the coalition is trivial
/// to assemble.
///
/// Raising `max_entries` does not fix that — it only changes the coalition
/// size. Partitioning does: external callers can never touch this reserve, so
/// no external coalition of any size can deny the provider's own org.
pub const DEFAULT_OWNER_RESERVED_REPLAY_ENTRIES: usize = 16_384;

/// Aggregate ceiling for ONE external acting organization, across ALL of its
/// member identities (§5).
///
/// This is the quota that actually defeats the coalition: sixteen identities
/// from one grantee org share ONE allocation rather than getting sixteen. It
/// is keyed on the VERIFIED acting organization — never the certificate
/// issuer, the peer/session, or any claimed wire field — because those are
/// either attacker-chosen or free to mint.
pub const DEFAULT_MAX_REPLAY_ENTRIES_PER_EXTERNAL_ORG: usize = 4_096;

/// Replay-guard ceilings — a global map cap plus a per-caller
/// sub-ceiling (E1.5) so one caller cannot consume another's
/// allocation.
#[derive(Debug, Clone, Copy)]
pub struct AdmissionReplayConfig {
    /// Maximum simultaneously-retained `(caller, call_id)`
    /// entries across ALL callers. At capacity, a novel admission
    /// denies rather than evicting an unexpired guard.
    pub max_entries: usize,
    /// Maximum simultaneously-retained entries for ONE caller.
    /// Checked before the global cap, so a flooding caller hits its
    /// own ceiling first and never denies other callers.
    pub max_entries_per_caller: usize,
    /// Entries within [`Self::max_entries`] reserved for the provider's OWN
    /// owner org, which external callers can never consume (§5).
    ///
    /// External traffic is therefore bounded by
    /// `max_entries - owner_reserved_entries`, and no external coalition — of
    /// any size, from any number of orgs — can deny an owner-org call.
    pub owner_reserved_entries: usize,
    /// Aggregate ceiling for ONE external acting org across all of its member
    /// identities (§5). The provider's own owner org is deliberately NOT
    /// subject to this: it is bounded per-identity and by the global cap, and
    /// may borrow whatever external capacity is idle.
    pub max_entries_per_external_org: usize,
}

impl Default for AdmissionReplayConfig {
    fn default() -> Self {
        Self {
            max_entries: DEFAULT_MAX_REPLAY_ENTRIES,
            max_entries_per_caller: DEFAULT_MAX_REPLAY_ENTRIES_PER_CALLER,
            owner_reserved_entries: DEFAULT_OWNER_RESERVED_REPLAY_ENTRIES,
            max_entries_per_external_org: DEFAULT_MAX_REPLAY_ENTRIES_PER_EXTERNAL_ORG,
        }
    }
}

impl AdmissionReplayConfig {
    /// Enforce the ceiling invariant (Kyra E1 audit): both bounds are
    /// positive AND the per-caller ceiling is STRICTLY below the
    /// global one. A `max_entries_per_caller >= max_entries` would let
    /// a single caller fill the entire global guard and starve every
    /// other org — the exact starvation the per-caller ceiling exists
    /// to prevent. Validated loudly at construction rather than
    /// silently clamped.
    pub fn validate(&self) -> Result<(), ReplayConfigError> {
        if self.max_entries == 0 {
            return Err(ReplayConfigError::ZeroGlobalCeiling);
        }
        if self.max_entries_per_caller == 0 {
            return Err(ReplayConfigError::ZeroPerCallerCeiling);
        }
        if self.max_entries_per_caller >= self.max_entries {
            return Err(ReplayConfigError::PerCallerNotBelowGlobal {
                per_caller: self.max_entries_per_caller,
                global: self.max_entries,
            });
        }
        if self.max_entries_per_external_org == 0 {
            return Err(ReplayConfigError::ZeroPerExternalOrgCeiling);
        }
        // A reserve at or above the global cap would leave external callers
        // no capacity at all — protected cross-org RPC would be dead on
        // arrival rather than merely bounded.
        if self.owner_reserved_entries >= self.max_entries {
            return Err(ReplayConfigError::OwnerReserveNotBelowGlobal {
                reserved: self.owner_reserved_entries,
                global: self.max_entries,
            });
        }
        // The per-org quota must fit inside the external pool, or the pool
        // bound would be unreachable and the org quota decorative.
        let external_pool = self.max_entries - self.owner_reserved_entries;
        if self.max_entries_per_external_org > external_pool {
            return Err(ReplayConfigError::PerExternalOrgAboveExternalPool {
                per_org: self.max_entries_per_external_org,
                external_pool,
            });
        }
        Ok(())
    }
}

/// An invalid [`AdmissionReplayConfig`] (Kyra E1 audit).
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum ReplayConfigError {
    /// `max_entries == 0` — the global guard could never admit.
    #[error("replay max_entries must be > 0")]
    ZeroGlobalCeiling,
    /// `max_entries_per_caller == 0` — no caller could ever admit.
    #[error("replay max_entries_per_caller must be > 0")]
    ZeroPerCallerCeiling,
    /// `max_entries_per_caller >= max_entries` — one caller could
    /// consume the entire global guard.
    #[error("replay max_entries_per_caller ({per_caller}) must be < max_entries ({global})")]
    PerCallerNotBelowGlobal {
        /// The configured per-caller ceiling.
        per_caller: usize,
        /// The configured global ceiling.
        global: usize,
    },
    /// `max_entries_per_external_org == 0` — no external org could admit.
    #[error("replay max_entries_per_external_org must be > 0")]
    ZeroPerExternalOrgCeiling,
    /// `owner_reserved_entries >= max_entries` — external callers would have
    /// no capacity at all.
    #[error(
        "replay owner_reserved_entries ({reserved}) must be < max_entries ({global}); \
         a reserve at or above the global cap leaves external callers nothing"
    )]
    OwnerReserveNotBelowGlobal {
        /// The configured owner reserve.
        reserved: usize,
        /// The configured global ceiling.
        global: usize,
    },
    /// The per-external-org quota cannot fit inside the external pool.
    #[error(
        "replay max_entries_per_external_org ({per_org}) must be <= the external pool \
         ({external_pool} = max_entries - owner_reserved_entries)"
    )]
    PerExternalOrgAboveExternalPool {
        /// The configured per-external-org ceiling.
        per_org: usize,
        /// The derived external pool size.
        external_pool: usize,
    },
}

/// The outcome of an admission check. Only [`Self::Admitted`] lets
/// the handler run; the §2.4 engine maps the others to typed
/// `AdmissionDenied` reasons.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplayOutcome {
    /// First sight of this `(caller, call_id)` within its window —
    /// recorded; the handler may run.
    Admitted,
    /// The SAME proof (identical binding digest) re-presented
    /// before expiry — a replay.
    Replay,
    /// The same `(caller, call_id)` with a DIFFERENT binding
    /// digest — a correlation-id collision (caller bug or a
    /// forged reuse of an id).
    CallIdCollision,
    /// The GLOBAL guard is full of still-live entries; admitting
    /// would require evicting an unexpired guard, so this call is
    /// denied fail-closed.
    CapacityExhausted,
    /// THIS caller already holds the maximum simultaneously-retained
    /// entries (E1.5). Denies only this caller — every other
    /// caller's allocation is untouched, so one flooding org cannot
    /// starve the rest.
    PerCallerCapacityExhausted,
    /// THIS external acting ORGANIZATION has consumed its aggregate
    /// allocation, across all of its member identities (§5).
    ///
    /// Distinct from [`Self::PerCallerCapacityExhausted`] and from
    /// [`Self::CapacityExhausted`] on purpose: it is the signal that one
    /// grantee org is behaving abusively, which neither of the others can
    /// express. Per-caller exhaustion names a single identity — and the whole
    /// point of the attack is that identities are free to mint — while global
    /// exhaustion suggests fleet-wide pressure the operator cannot attribute.
    PerOrganizationCapacityExhausted,
    /// The EXTERNAL pool (`max_entries - owner_reserved_entries`) is full of
    /// live entries, with no single org over its own quota (§5).
    ///
    /// Beyond the requested per-org outcome, because conflating it with either
    /// neighbour would mislead: it is not one abusive grantee, and it is NOT
    /// global exhaustion — the owner reserve is by construction still free, so
    /// the provider's own org is unaffected and no operator action against a
    /// particular org is indicated. Reaching it means genuinely many distinct
    /// external orgs are active at once.
    ExternalPoolCapacityExhausted,
}

/// The VERIFIED principal an admission is charged to (§5).
///
/// A struct rather than loose arguments because WHICH identity each quota is
/// keyed on is the whole security property, and a positional `&OrgId, &OrgId`
/// pair would be trivial to transpose at a call site.
#[derive(Debug, Clone, Copy)]
pub struct ReplayPrincipal<'a> {
    /// The caller entity, resolved from the AUTHENTICATED direct session —
    /// never a request-body field.
    pub caller: &'a EntityId,
    /// The org the caller is VERIFIED to be acting for: taken from the
    /// org-signed membership certificate and cross-checked against the
    /// dispatcher grant by `verify_org_admission`.
    ///
    /// Deliberately NOT the certificate ISSUER, the peer/session, or any
    /// claimed wire field. The issuer is attacker-chosen for a self-minted
    /// org; sessions and identities are free to mint, which is exactly what
    /// makes the sixteen-identity coalition cheap. The acting org is the
    /// coarsest thing an attacker cannot fabricate without the provider
    /// already trusting it.
    pub acting_org: &'a OrgId,
    /// The PROVIDER's own owner org — the beneficiary of the reserve.
    pub provider_owner_org: &'a OrgId,
}

impl ReplayPrincipal<'_> {
    /// Whether this admission is charged to the provider's own org, and so
    /// draws on the reserve rather than the external pool.
    fn is_owner_org(&self) -> bool {
        self.acting_org == self.provider_owner_org
    }
}

struct ReplayEntry {
    binding_digest: [u8; 32],
    /// Monotonic instant at/after which this entry is reusable.
    expires_at: Instant,
    /// The acting org this entry is charged to (§5).
    ///
    /// Carried on the ENTRY so reclamation can decrement the org counter and
    /// the external-pool counter without re-deriving anything: an expired
    /// entry must return its slot to exactly the quotas it consumed, and by
    /// the time it expires the request that created it is long gone.
    acting_org: OrgId,
    /// Whether this entry drew on the external pool (i.e. was NOT owner-org).
    ///
    /// Cached rather than recomputed at reclaim time because the provider's
    /// owner org can CHANGE under a re-adopt; recomputing would then return a
    /// slot to the wrong pool and permanently skew the accounting.
    external: bool,
}

/// The mutex-guarded state. Nested `caller → (call_id → entry)` so
/// the per-caller ceiling and per-caller reclamation touch ONLY one
/// caller's entries (E1.5); `total` mirrors the summed inner lengths
/// so the global cap is a field read, not an O(callers) sum.
#[derive(Default)]
struct ReplayState {
    by_caller: HashMap<EntityId, HashMap<u64, ReplayEntry>>,
    total: usize,
    /// Live entries per VERIFIED acting org (§5). The aggregate quota that
    /// makes a coalition of identities share ONE allocation.
    by_org: HashMap<OrgId, usize>,
    /// Live entries drawing on the EXTERNAL pool — i.e. every entry whose
    /// acting org is not the provider's own. Mirrors the summed external
    /// `by_org` values so the pool bound is a field read.
    external_total: usize,
}

impl ReplayState {
    /// Charge one entry to every counter it consumes. The four counters move
    /// together, under the caller's lock, or not at all.
    fn charge(&mut self, entry_external: bool, acting_org: &OrgId) {
        self.total += 1;
        *self.by_org.entry(*acting_org).or_insert(0) += 1;
        if entry_external {
            self.external_total += 1;
        }
    }

    /// Release one reclaimed entry from every counter it consumed.
    ///
    /// The mirror of [`Self::charge`]. `total`, the per-org count and the
    /// external pool must be decremented in step, or a quota drifts upward
    /// forever and eventually denies a legitimate caller with no live entries
    /// to justify it — a leak that only manifests under sustained load, which
    /// is when it is hardest to diagnose.
    fn release(&mut self, entry: &ReplayEntry) {
        self.total -= 1;
        if let Some(count) = self.by_org.get_mut(&entry.acting_org) {
            *count -= 1;
            if *count == 0 {
                self.by_org.remove(&entry.acting_org);
            }
        }
        if entry.external {
            self.external_total -= 1;
        }
    }

    /// Drop `caller`'s expired entries (and the caller bucket if it
    /// empties), releasing each from every counter it held.
    fn reclaim_caller(&mut self, caller: &EntityId, now: Instant) {
        let Some(inner) = self.by_caller.get_mut(caller) else {
            return;
        };
        let expired: Vec<ReplayEntry> = {
            let mut drained = Vec::new();
            inner.retain(|_, e| {
                if e.expires_at > now {
                    true
                } else {
                    drained.push(ReplayEntry {
                        binding_digest: e.binding_digest,
                        expires_at: e.expires_at,
                        acting_org: e.acting_org,
                        external: e.external,
                    });
                    false
                }
            });
            drained
        };
        let empty = inner.is_empty();
        for entry in &expired {
            self.release(entry);
        }
        if empty {
            self.by_caller.remove(caller);
        }
    }

    /// Drop every expired entry across all callers, releasing each from every
    /// counter it held. Returns the number reclaimed.
    fn reclaim_all(&mut self, now: Instant) -> usize {
        let mut released: Vec<ReplayEntry> = Vec::new();
        self.by_caller.retain(|_, inner| {
            inner.retain(|_, e| {
                if e.expires_at > now {
                    true
                } else {
                    released.push(ReplayEntry {
                        binding_digest: e.binding_digest,
                        expires_at: e.expires_at,
                        acting_org: e.acting_org,
                        external: e.external,
                    });
                    false
                }
            });
            !inner.is_empty()
        });
        for entry in &released {
            self.release(entry);
        }
        released.len()
    }

    /// Live entries charged to `org`.
    fn org_live(&self, org: &OrgId) -> usize {
        self.by_org.get(org).copied().unwrap_or(0)
    }
}

/// Burst of FAILED admissions one authenticated peer may cost before it is
/// throttled (§6). See [`AdmissionFailureLimiter`].
pub const DEFAULT_MAX_FAILED_ADMISSIONS_PER_PEER: u32 = 64;

/// Failed-admission budget refilled per second, per peer (§6).
pub const DEFAULT_FAILED_ADMISSION_REFILL_PER_SEC: u32 = 8;

/// Maximum peers tracked at once; the oldest idle entry is reclaimed at
/// capacity (§6). Bounded so the limiter cannot itself become the memory
/// exhaustion it exists to prevent.
pub const DEFAULT_MAX_RATE_LIMITED_PEERS: usize = 4_096;

/// Per-peer throttle on the SIGNATURE work a failing caller can compel (§6).
///
/// # The asymmetry this closes
///
/// A peer needs only a TOFU-pinned session and NO org credentials to reach the
/// expensive part of the gate. It self-mints an `OrgKeypair`, issues ITSELF a
/// genuinely valid membership certificate and dispatcher grant under that key,
/// and attaches a garbage capability grant naming the provider's (public)
/// owner org. Every cheap plaintext check passes — none of them verifies a
/// signature — and the gate then performs THREE `ed25519 verify_strict`
/// operations before denying `CapabilityGrantInvalid`.
///
/// Nothing bounded that. Failed admissions deliberately consume no replay
/// slot (the guard records only ADMITTED calls, so a denial cannot evict a
/// legitimate entry), so the replay ceilings — including the §5 trust-domain
/// partition — never see this traffic at all.
///
/// # Why it charges on FAILURE rather than per attempt
///
/// A per-attempt limiter would throttle honest callers, and picking a rate
/// that suits every deployment is exactly the guess that makes such limits
/// wrong. But the attacker's distinguishing property is not its RATE — it is
/// that its admissions always FAIL. A legitimate caller's succeed.
///
/// So a successful admission costs nothing at all: an honest peer can drive
/// the gate as fast as it likes and never touch this. Only failures draw on
/// the bucket, and when it empties the peer is refused BEFORE the signature
/// work rather than after — which is the whole point, since the work is the
/// resource being protected.
///
/// A legitimately misconfigured client (expired proof, wrong capability) fails
/// at a low rate and the refill absorbs it; a client failing faster than the
/// refill is, by construction, either broken or hostile, and throttling it is
/// correct in both cases.
///
/// # These are SAFE defaults, not universal limits
///
/// 64 burst with 8/s refill bounds one peer to ~1.2 ms/s of verification CPU
/// while letting a reconnect storm through untouched. An operator running
/// protected RPC where clients legitimately fail admission in bursts can raise
/// the envelope knowingly via [`MeshNodeConfig`]; the point is that the
/// failure mode is bounded and attributable, not that the numbers suit every
/// workload.
///
/// [`MeshNodeConfig`]: crate::adapter::net::MeshNodeConfig
pub struct AdmissionFailureLimiter {
    buckets: Mutex<HashMap<u64, PeerBucket>>,
    config: AdmissionRateLimitConfig,
    /// Admissions refused because the peer's failure budget was exhausted.
    throttled: AtomicU64,
}

/// Tunables for [`AdmissionFailureLimiter`] (§6).
#[derive(Debug, Clone, Copy)]
pub struct AdmissionRateLimitConfig {
    /// Burst of failed admissions one peer may cost before throttling.
    pub max_failed_per_peer: u32,
    /// Budget refilled per second, per peer.
    pub refill_per_sec: u32,
    /// Maximum peers tracked simultaneously.
    pub max_tracked_peers: usize,
}

impl Default for AdmissionRateLimitConfig {
    fn default() -> Self {
        Self {
            max_failed_per_peer: DEFAULT_MAX_FAILED_ADMISSIONS_PER_PEER,
            refill_per_sec: DEFAULT_FAILED_ADMISSION_REFILL_PER_SEC,
            max_tracked_peers: DEFAULT_MAX_RATE_LIMITED_PEERS,
        }
    }
}

impl AdmissionRateLimitConfig {
    /// Reject a degenerate envelope loudly rather than clamping it.
    pub fn validate(&self) -> Result<(), ReplayConfigError> {
        if self.max_failed_per_peer == 0 {
            return Err(ReplayConfigError::ZeroPerCallerCeiling);
        }
        if self.refill_per_sec == 0 {
            // A zero refill makes the first burst permanent: a peer that
            // exhausts its budget is throttled forever, so a single expired
            // proof at startup would take a client out until restart.
            return Err(ReplayConfigError::ZeroPerCallerCeiling);
        }
        if self.max_tracked_peers == 0 {
            return Err(ReplayConfigError::ZeroGlobalCeiling);
        }
        Ok(())
    }
}

struct PeerBucket {
    /// Remaining failed-admission allowance.
    tokens: u32,
    /// When `tokens` was last refilled.
    last_refill: Instant,
    /// Last touch, for idle reclamation at capacity.
    last_seen: Instant,
}

impl AdmissionFailureLimiter {
    /// A limiter with the given envelope, VALIDATED.
    pub fn try_new(config: AdmissionRateLimitConfig) -> Result<Self, ReplayConfigError> {
        config.validate()?;
        Ok(Self {
            buckets: Mutex::new(HashMap::new()),
            config,
            throttled: AtomicU64::new(0),
        })
    }

    /// A limiter with the default envelope (always valid).
    pub fn with_defaults() -> Self {
        Self {
            buckets: Mutex::new(HashMap::new()),
            config: AdmissionRateLimitConfig::default(),
            throttled: AtomicU64::new(0),
        }
    }

    /// May `from_node` attempt an admission right now?
    ///
    /// Call BEFORE the signature work. `false` means the peer has spent its
    /// failure budget and must be denied cheaply.
    pub fn may_attempt(&self, from_node: u64, now: Instant) -> bool {
        let mut buckets = self.buckets.lock();
        let cfg = self.config;
        match buckets.get_mut(&from_node) {
            None => true, // unseen peer: no failures charged yet
            Some(bucket) => {
                Self::refill(bucket, cfg, now);
                bucket.last_seen = now;
                if bucket.tokens > 0 {
                    true
                } else {
                    self.throttled.fetch_add(1, Ordering::Relaxed);
                    false
                }
            }
        }
    }

    /// Charge one failed admission to `from_node`.
    ///
    /// Called ONLY on a denial. A successful admission costs nothing, which is
    /// what keeps honest traffic entirely unaffected.
    pub fn on_failure(&self, from_node: u64, now: Instant) {
        let mut buckets = self.buckets.lock();
        let cfg = self.config;
        if !buckets.contains_key(&from_node) {
            if buckets.len() >= cfg.max_tracked_peers {
                // Reclaim the least-recently-seen peer. Safe to evict: losing a
                // bucket only restores that peer's full allowance, and the
                // evicted one is by definition the least active.
                if let Some(oldest) = buckets
                    .iter()
                    .min_by_key(|(_, b)| b.last_seen)
                    .map(|(peer, _)| *peer)
                {
                    buckets.remove(&oldest);
                }
            }
            buckets.insert(
                from_node,
                PeerBucket {
                    tokens: cfg.max_failed_per_peer,
                    last_refill: now,
                    last_seen: now,
                },
            );
        }
        if let Some(bucket) = buckets.get_mut(&from_node) {
            Self::refill(bucket, cfg, now);
            bucket.tokens = bucket.tokens.saturating_sub(1);
            bucket.last_seen = now;
        }
    }

    fn refill(bucket: &mut PeerBucket, cfg: AdmissionRateLimitConfig, now: Instant) {
        let elapsed = now.saturating_duration_since(bucket.last_refill);
        let secs = elapsed.as_secs();
        if secs == 0 {
            return;
        }
        let gained = secs.saturating_mul(u64::from(cfg.refill_per_sec));
        let gained = u32::try_from(gained).unwrap_or(u32::MAX);
        bucket.tokens = bucket
            .tokens
            .saturating_add(gained)
            .min(cfg.max_failed_per_peer);
        bucket.last_refill = now;
    }

    /// Admissions refused because the peer's failure budget was exhausted.
    pub fn throttled_denials(&self) -> u64 {
        self.throttled.load(Ordering::Relaxed)
    }

    /// Remaining failure allowance for `from_node` (test/metric surface).
    pub fn tokens_for(&self, from_node: u64) -> u32 {
        self.buckets
            .lock()
            .get(&from_node)
            .map_or(self.config.max_failed_per_peer, |b| b.tokens)
    }

    /// Peers currently tracked (test/metric surface).
    pub fn tracked_peers(&self) -> usize {
        self.buckets.lock().len()
    }
}
/// The volatile admission replay guard. One per provider node.
pub struct AdmissionReplayGuard {
    entries: Mutex<ReplayState>,
    config: AdmissionReplayConfig,
    /// Count of admissions denied for GLOBAL capacity — a metric
    /// surface (§2.5: "deny + metric on exhaustion").
    capacity_denials: AtomicU64,
    /// Count of admissions denied for PER-CALLER capacity (E1.5) —
    /// a separate metric so operators can tell a fleet-wide flood
    /// from a single abusive caller.
    per_caller_denials: AtomicU64,
    /// Count of admissions denied because ONE EXTERNAL ORG exhausted its
    /// aggregate allocation (§5) — the signal that identifies an abusive
    /// grantee, which neither the global nor the per-caller counter can.
    per_org_denials: AtomicU64,
    /// Count of admissions denied because the EXTERNAL POOL was full with no
    /// single org over quota — genuinely many active external orgs, and
    /// notably NOT a state in which the provider's own org is affected.
    external_pool_denials: AtomicU64,
}

impl AdmissionReplayGuard {
    /// A guard with the given ceilings, VALIDATED (Kyra E1 audit) —
    /// see [`AdmissionReplayConfig::validate`]. Prefer this over
    /// [`Self::new`] on any config not known-good at compile time.
    pub fn try_new(config: AdmissionReplayConfig) -> Result<Self, ReplayConfigError> {
        config.validate()?;
        Ok(Self::from_validated(config))
    }

    fn from_validated(config: AdmissionReplayConfig) -> Self {
        Self {
            entries: Mutex::new(ReplayState::default()),
            config,
            capacity_denials: AtomicU64::new(0),
            per_caller_denials: AtomicU64::new(0),
            per_org_denials: AtomicU64::new(0),
            external_pool_denials: AtomicU64::new(0),
        }
    }

    /// A guard with the given ceilings. Panics on an invalid config
    /// (loud, not silently clamped) — use [`Self::try_new`] when the
    /// config comes from untrusted/dynamic input.
    pub fn new(config: AdmissionReplayConfig) -> Self {
        match Self::try_new(config) {
            Ok(guard) => guard,
            Err(e) => panic!("invalid AdmissionReplayConfig: {e}"),
        }
    }

    /// A guard with the default ceilings (always valid).
    pub fn with_defaults() -> Self {
        Self::from_validated(AdmissionReplayConfig::default())
    }

    /// Atomic insert-or-deny (the last step of §2.4). `now` is the
    /// monotonic clock (real callers pass `Instant::now()`;
    /// `expires_at` is `now + proof-remaining-ttl + skew`,
    /// precomputed by the caller so the guard never touches a wall
    /// clock).
    ///
    /// One lock acquisition covers eviction, the collision check,
    /// and the insert, so two concurrent presentations of one
    /// proof can never both see "absent" and both admit.
    pub fn admit(
        &self,
        principal: ReplayPrincipal<'_>,
        call_id: u64,
        binding_digest: [u8; 32],
        expires_at: Instant,
        now: Instant,
    ) -> ReplayOutcome {
        let caller = principal.caller;
        let external = !principal.is_owner_org();
        let mut st = self.entries.lock();

        // An existing entry for this exact `(caller, call_id)`:
        // replay vs collision, UNLESS it has expired (then it is
        // reusable — the window closed, so this is a legitimate new
        // call reusing the id). Handled under one `get_mut` so the
        // expired overwrite touches neither `total` nor the
        // per-caller count (the key stays occupied).
        if let Some(inner) = st.by_caller.get_mut(caller) {
            if let Some(existing) = inner.get(&call_id) {
                if existing.expires_at > now {
                    return if existing.binding_digest == binding_digest {
                        ReplayOutcome::Replay
                    } else {
                        ReplayOutcome::CallIdCollision
                    };
                }
                // Expired overwrite REUSES the occupied key, so no counter
                // moves — the entry it replaces was already charged and is
                // charged to the same principal by construction (the key is
                // `(caller, call_id)` and the caller cannot change orgs
                // mid-window without a new membership certificate).
                inner.insert(
                    call_id,
                    ReplayEntry {
                        binding_digest,
                        expires_at,
                        acting_org: *principal.acting_org,
                        external,
                    },
                );
                return ReplayOutcome::Admitted;
            }
        }

        // New key for this caller. Per-caller ceiling FIRST (E1.5) so
        // a flooding caller hits its own limit before it can pressure
        // the global cap. At capacity, reclaim only THIS caller's
        // expired slots; if still full, deny only this caller.
        let caller_live = st.by_caller.get(caller).map_or(0, HashMap::len);
        if caller_live >= self.config.max_entries_per_caller {
            st.reclaim_caller(caller, now);
            let caller_live = st.by_caller.get(caller).map_or(0, HashMap::len);
            if caller_live >= self.config.max_entries_per_caller {
                self.per_caller_denials.fetch_add(1, Ordering::Relaxed);
                return ReplayOutcome::PerCallerCapacityExhausted;
            }
        }

        // §5 — the TRUST-DOMAIN quotas, checked before the global cap so a
        // flooding org exhausts its own allocation first and the owner
        // reserve is never reachable from outside.
        //
        // Both are keyed on the VERIFIED acting org. Sixteen identities from
        // one grantee org therefore share ONE allocation instead of getting
        // sixteen, which is precisely what made the coalition cheap: minting
        // identities is a single org-admin action, minting a trusted ORG is
        // not.
        //
        // Owner-org traffic is deliberately exempt from both: it is bounded
        // per-identity and by the global cap, and may borrow whatever external
        // capacity is idle. It cannot starve external callers, because the
        // external pool is a floor for them in exactly the way the reserve is
        // a floor for the owner — external entries already admitted are never
        // evicted, and a new owner entry can only consume capacity that is
        // free right now.
        if external {
            let org_live = st.org_live(principal.acting_org);
            if org_live >= self.config.max_entries_per_external_org {
                st.reclaim_all(now);
                if st.org_live(principal.acting_org) >= self.config.max_entries_per_external_org {
                    self.per_org_denials.fetch_add(1, Ordering::Relaxed);
                    return ReplayOutcome::PerOrganizationCapacityExhausted;
                }
            }
            let external_pool = self
                .config
                .max_entries
                .saturating_sub(self.config.owner_reserved_entries);
            if st.external_total >= external_pool {
                st.reclaim_all(now);
                if st.external_total >= external_pool {
                    self.external_pool_denials.fetch_add(1, Ordering::Relaxed);
                    return ReplayOutcome::ExternalPoolCapacityExhausted;
                }
            }
        }

        // Global ceiling. Reclaim EXPIRED slots fleet-wide; if none
        // are reclaimable, deny fail-closed rather than evict a live
        // guard.
        //
        // Still reachable for OWNER traffic, which is bounded only here and
        // per-identity — an owner org with enough distinct identities can
        // legitimately fill the map, and denying fail-closed remains correct.
        // External traffic can no longer reach it: the pool bound above is
        // strictly tighter.
        if st.total >= self.config.max_entries {
            st.reclaim_all(now);
            if st.total >= self.config.max_entries {
                self.capacity_denials.fetch_add(1, Ordering::Relaxed);
                return ReplayOutcome::CapacityExhausted;
            }
        }

        st.by_caller.entry(caller.clone()).or_default().insert(
            call_id,
            ReplayEntry {
                binding_digest,
                expires_at,
                acting_org: *principal.acting_org,
                external,
            },
        );
        st.charge(external, principal.acting_org);
        ReplayOutcome::Admitted
    }

    /// Reclaim every entry whose window has closed as of `now`.
    /// Optional maintenance — [`Self::admit`] reclaims lazily at
    /// capacity — but a periodic sweep keeps steady-state memory
    /// low. Returns how many entries were reclaimed.
    pub fn evict_expired(&self, now: Instant) -> usize {
        self.entries.lock().reclaim_all(now)
    }

    /// Current tracked-entry count across all callers (test/metric
    /// surface).
    pub fn len(&self) -> usize {
        self.entries.lock().total
    }

    /// `true` iff no entries are tracked.
    pub fn is_empty(&self) -> bool {
        self.entries.lock().total == 0
    }

    /// Number of entries currently tracked for one caller
    /// (test/metric surface).
    pub fn caller_len(&self, caller: &EntityId) -> usize {
        self.entries
            .lock()
            .by_caller
            .get(caller)
            .map_or(0, HashMap::len)
    }

    /// Total admissions denied for GLOBAL capacity since construction.
    pub fn capacity_denials(&self) -> u64 {
        self.capacity_denials.load(Ordering::Relaxed)
    }

    /// Live entries charged to one acting org, across ALL of its member
    /// identities (§5 test/metric surface).
    pub fn org_len(&self, org: &OrgId) -> usize {
        self.entries.lock().org_live(org)
    }

    /// Live entries drawing on the EXTERNAL pool (§5 test/metric surface).
    pub fn external_len(&self) -> usize {
        self.entries.lock().external_total
    }

    /// Total admissions denied because one EXTERNAL ORG exhausted its
    /// aggregate allocation (§5). Operators should read a rising value here as
    /// "one grantee is misbehaving", distinct from
    /// [`Self::capacity_denials`] ("the whole guard is under pressure").
    pub fn per_org_denials(&self) -> u64 {
        self.per_org_denials.load(Ordering::Relaxed)
    }

    /// Total admissions denied because the EXTERNAL POOL filled with no single
    /// org over quota (§5) — many active external orgs, owner org unaffected.
    pub fn external_pool_denials(&self) -> u64 {
        self.external_pool_denials.load(Ordering::Relaxed)
    }

    /// Total admissions denied for PER-CALLER capacity (E1.5) since
    /// construction.
    pub fn per_caller_denials(&self) -> u64 {
        self.per_caller_denials.load(Ordering::Relaxed)
    }
}

impl std::fmt::Debug for AdmissionReplayGuard {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AdmissionReplayGuard")
            .field("entries", &self.len())
            .field("max_entries", &self.config.max_entries)
            .field(
                "max_entries_per_caller",
                &self.config.max_entries_per_caller,
            )
            .field("capacity_denials", &self.capacity_denials())
            .field("per_caller_denials", &self.per_caller_denials())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    fn caller(byte: u8) -> EntityId {
        EntityId::from_bytes([byte; 32])
    }

    /// The provider's OWN org, for every pre-§5 witness.
    fn owner_org() -> OrgId {
        OrgId([0xAA; 32])
    }

    /// A distinct external org.
    fn external_org(byte: u8) -> OrgId {
        OrgId([byte; 32])
    }

    /// Pre-§5 shim: admit as the provider's OWN org.
    ///
    /// The existing witnesses are about replay / collision / per-caller
    /// ceilings — none is about trust-domain partitioning — so charging them
    /// to the owner org keeps each one testing exactly what it always did,
    /// rather than silently acquiring a second reason to fail.
    fn admit_owner(
        guard: &AdmissionReplayGuard,
        caller: &EntityId,
        call_id: u64,
        digest: [u8; 32],
        expires: Instant,
        now: Instant,
    ) -> ReplayOutcome {
        let owner = owner_org();
        guard.admit(
            ReplayPrincipal {
                caller,
                acting_org: &owner,
                provider_owner_org: &owner,
            },
            call_id,
            digest,
            expires,
            now,
        )
    }

    /// Admit as a member identity of `org`, an EXTERNAL org.
    fn admit_external(
        guard: &AdmissionReplayGuard,
        org: &OrgId,
        caller: &EntityId,
        call_id: u64,
        digest: [u8; 32],
        expires: Instant,
        now: Instant,
    ) -> ReplayOutcome {
        let owner = owner_org();
        guard.admit(
            ReplayPrincipal {
                caller,
                acting_org: org,
                provider_owner_org: &owner,
            },
            call_id,
            digest,
            expires,
            now,
        )
    }

    // ==================================================================
    // §6 — per-peer throttle on compelled signature work.
    // ==================================================================

    fn limiter() -> AdmissionFailureLimiter {
        AdmissionFailureLimiter::try_new(AdmissionRateLimitConfig {
            max_failed_per_peer: 4,
            refill_per_sec: 2,
            max_tracked_peers: 8,
        })
        .expect("valid envelope")
    }

    /// The property that makes this design defensible: an honest caller is
    /// COMPLETELY unaffected, however fast it calls.
    ///
    /// A per-attempt limiter would throttle legitimate traffic, and picking a
    /// rate that suits every deployment is exactly the guess that makes such
    /// limits wrong. Charging on FAILURE instead keys on the attacker's actual
    /// distinguishing property — its admissions fail; a real caller's succeed.
    #[test]
    fn a_peer_whose_admissions_succeed_is_never_throttled() {
        let lim = limiter();
        let now = Instant::now();
        const PEER: u64 = 7;

        // Far more attempts than the burst allowance, none of them failures.
        for _ in 0..1_000 {
            assert!(
                lim.may_attempt(PEER, now),
                "a successful caller must never be throttled — charging per \
                 ATTEMPT would penalise exactly the traffic we want",
            );
        }
        assert_eq!(lim.throttled_denials(), 0);
        assert_eq!(
            lim.tracked_peers(),
            0,
            "a peer with no failures costs nothing to track"
        );
    }

    /// A failing peer spends its burst and is then refused BEFORE the
    /// signature work — which is the resource being protected.
    #[test]
    fn a_failing_peer_exhausts_its_budget_and_is_refused() {
        let lim = limiter();
        let now = Instant::now();
        const PEER: u64 = 9;

        for _ in 0..4 {
            assert!(lim.may_attempt(PEER, now));
            lim.on_failure(PEER, now);
        }
        assert_eq!(lim.tokens_for(PEER), 0);
        assert!(
            !lim.may_attempt(PEER, now),
            "the peer must be refused once its failure budget is spent",
        );
        assert_eq!(lim.throttled_denials(), 1);
    }

    /// Throttling is per PEER: one abusive session must not deny another.
    #[test]
    fn throttling_one_peer_leaves_others_untouched() {
        let lim = limiter();
        let now = Instant::now();
        const NOISY: u64 = 1;
        const QUIET: u64 = 2;

        for _ in 0..4 {
            lim.on_failure(NOISY, now);
        }
        assert!(!lim.may_attempt(NOISY, now));
        assert!(
            lim.may_attempt(QUIET, now),
            "a second peer's allowance must be untouched by the first's abuse",
        );
    }

    /// The budget refills, so a legitimately misconfigured client recovers
    /// without operator intervention. A zero-refill envelope would make the
    /// first burst permanent, which `validate` refuses for this reason.
    #[test]
    fn the_budget_refills_over_time() {
        let lim = limiter();
        let t0 = Instant::now();
        const PEER: u64 = 3;

        for _ in 0..4 {
            lim.on_failure(PEER, t0);
        }
        assert!(!lim.may_attempt(PEER, t0));

        // 2 tokens/sec: one second restores two attempts.
        let later = t0 + Duration::from_secs(1);
        assert!(
            lim.may_attempt(PEER, later),
            "a throttled peer must recover on its own — otherwise one expired \
             proof at startup takes a client out until restart",
        );
        assert_eq!(lim.tokens_for(PEER), 2);

        // Refill saturates at the burst ceiling rather than growing unbounded.
        let much_later = t0 + Duration::from_secs(3_600);
        lim.may_attempt(PEER, much_later);
        assert_eq!(lim.tokens_for(PEER), 4);
    }

    /// The tracking map is bounded, so the limiter cannot become the memory
    /// exhaustion it exists to prevent.
    #[test]
    fn tracked_peers_are_bounded() {
        let lim = limiter();
        let now = Instant::now();
        for peer in 0..64u64 {
            lim.on_failure(peer, now + Duration::from_millis(peer));
        }
        assert!(
            lim.tracked_peers() <= 8,
            "peer tracking must stay within max_tracked_peers, got {}",
            lim.tracked_peers(),
        );
    }

    /// A degenerate envelope is refused loudly. Zero refill in particular
    /// would make the first burst permanent.
    #[test]
    fn a_degenerate_rate_limit_envelope_is_refused() {
        assert!(AdmissionRateLimitConfig {
            max_failed_per_peer: 0,
            refill_per_sec: 1,
            max_tracked_peers: 8,
        }
        .validate()
        .is_err());
        assert!(AdmissionRateLimitConfig {
            max_failed_per_peer: 4,
            refill_per_sec: 0,
            max_tracked_peers: 8,
        }
        .validate()
        .is_err());
        assert!(AdmissionRateLimitConfig::default().validate().is_ok());
    }
    // ==================================================================
    // §5 — trust-domain partitioning of the replay budget.
    //
    // The per-caller ceiling is correct in isolation and does not COMPOSE:
    // `max_entries / max_entries_per_caller == 16`, so sixteen identities
    // saturate the global map and the provider then denies its OWN owner-org
    // callers. Minting sixteen identities is one org-admin action, which is
    // what made the coalition cheap. Raising `max_entries` would only change
    // the coalition size; these tests pin the partition instead.
    // ==================================================================

    /// A tiny envelope with the same SHAPE as the shipped default
    /// (reserve < total, per-org <= external pool), so the properties are
    /// exercised without allocating 65 536 entries per test.
    ///
    /// `max_entries_per_caller` is deliberately BELOW the org quota, so
    /// filling an org's allocation requires a COALITION of identities — the
    /// realistic shape, and the one the attack exploits. With per-caller equal
    /// to per-org a single identity would trip the caller ceiling first and
    /// the org quota would never be the binding constraint under test.
    fn partitioned() -> AdmissionReplayGuard {
        AdmissionReplayGuard::new(AdmissionReplayConfig {
            max_entries: 40,
            owner_reserved_entries: 10, // external pool = 30
            max_entries_per_external_org: 8,
            max_entries_per_caller: 4, // so one org needs 2 identities
        })
    }

    /// Fill `org` with up to `identities * 4` entries, using distinct member
    /// identities — the coalition shape. Returns (admitted, per-org denials).
    fn flood_org(
        guard: &AdmissionReplayGuard,
        org: &OrgId,
        identities: impl IntoIterator<Item = u8>,
        expires: Instant,
        now: Instant,
    ) -> (usize, usize) {
        let (mut admitted, mut denied) = (0usize, 0usize);
        for identity in identities {
            for call in 0..4u64 {
                match admit_external(
                    guard,
                    org,
                    &caller(identity),
                    call + u64::from(identity) * 1_000,
                    [identity; 32],
                    expires,
                    now,
                ) {
                    ReplayOutcome::Admitted => admitted += 1,
                    ReplayOutcome::PerOrganizationCapacityExhausted => denied += 1,
                    ReplayOutcome::ExternalPoolCapacityExhausted => denied += 1,
                    other => panic!("unexpected outcome {other:?}"),
                }
            }
        }
        (admitted, denied)
    }

    /// Witness 1 — sixteen identities from ONE external org collectively stop
    /// at the org quota, not at sixteen times the per-caller quota.
    ///
    /// This is the reported attack, scaled down: the coalition shares ONE
    /// allocation because the quota is keyed on the verified acting org.
    #[test]
    fn many_identities_from_one_external_org_share_one_allocation() {
        let guard = partitioned();
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);
        let org = external_org(0xB1);

        let (admitted, denied) = flood_org(&guard, &org, 0..16u8, expires, now);

        assert_eq!(
            admitted, 8,
            "sixteen identities from one org must share the SINGLE 8-entry org \
             allocation — if each got its own, the coalition wins",
        );
        assert!(denied > 0);
        assert_eq!(guard.org_len(&org), 8);
        assert_eq!(
            guard.per_org_denials(),
            denied as u64,
            "the per-ORG denial metric must fire, so an operator can attribute \
             this to one grantee rather than to fleet-wide pressure",
        );
        assert_eq!(
            guard.per_caller_denials(),
            0,
            "not a per-caller denial: naming a single identity would point the \
             operator at the wrong subject, since identities are free to mint",
        );
    }

    /// Witness 2 — filling the ENTIRE external pool cannot deny a fresh
    /// owner-org call. The headline property.
    #[test]
    fn a_full_external_pool_never_denies_the_owner_org() {
        let guard = partitioned();
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);

        // Enough distinct orgs (2 identities each) that the POOL, not any one
        // org quota, is what stops them.
        let mut external_admitted = 0usize;
        for org_byte in 0..8u8 {
            let org = external_org(0xC0 + org_byte);
            let (a, _) = flood_org(
                &guard,
                &org,
                [org_byte * 2 + 100, org_byte * 2 + 101],
                expires,
                now,
            );
            external_admitted += a;
        }
        assert_eq!(
            external_admitted, 30,
            "external traffic must be capped at max_entries - owner_reserved",
        );
        assert_eq!(guard.external_len(), 30);

        assert_eq!(
            admit_external(
                &guard,
                &external_org(0xFE),
                &caller(99),
                1,
                [9u8; 32],
                expires,
                now
            ),
            ReplayOutcome::ExternalPoolCapacityExhausted,
        );

        assert_eq!(
            admit_owner(&guard, &caller(200), 1, [7u8; 32], expires, now),
            ReplayOutcome::Admitted,
            "an external coalition of ANY size must never deny the provider's \
             own org — this is the entire point of the reserve",
        );
    }

    /// Witness 3 — owner traffic may BORROW unused external capacity, so the
    /// partition costs nothing when there is no external load.
    #[test]
    fn owner_traffic_borrows_idle_external_capacity() {
        let guard = partitioned();
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);

        // No external traffic at all: the owner should reach the GLOBAL cap
        // (40), not stop at its 10-entry reserve. 10 identities x 4 each.
        let mut admitted = 0usize;
        for identity in 0..10u8 {
            for call in 0..4u64 {
                if admit_owner(
                    &guard,
                    &caller(identity),
                    call,
                    [identity; 32],
                    expires,
                    now,
                ) == ReplayOutcome::Admitted
                {
                    admitted += 1;
                }
            }
        }
        assert_eq!(
            admitted, 40,
            "owner traffic must borrow idle external capacity up to the global \
             cap; stopping at the 10-entry reserve would make the partition a \
             throughput regression rather than a safety property",
        );
        assert_eq!(guard.len(), 40);
    }

    /// Witness 4 — distinct external orgs have INDEPENDENT allocations. One
    /// abusive grantee must not spend another grantee's quota.
    #[test]
    fn distinct_external_orgs_have_independent_allocations() {
        let guard = partitioned();
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);
        let noisy = external_org(0xB1);
        let quiet = external_org(0xB2);

        let (admitted, _) = flood_org(&guard, &noisy, [1u8, 11u8], expires, now);
        assert_eq!(admitted, 8);
        assert_eq!(
            admit_external(&guard, &noisy, &caller(21), 0, [21u8; 32], expires, now),
            ReplayOutcome::PerOrganizationCapacityExhausted,
            "a THIRD fresh identity must still be refused — the quota is the \
             org's, not the identity's",
        );

        let (quiet_admitted, quiet_denied) = flood_org(&guard, &quiet, [2u8, 12u8], expires, now);
        assert_eq!(
            quiet_admitted, 8,
            "a second org's quota must be untouched by the first's abuse",
        );
        assert_eq!(quiet_denied, 0);
        assert_eq!(guard.org_len(&noisy), 8);
        assert_eq!(guard.org_len(&quiet), 8);
    }

    /// Witness 5 — expiry reclamation decrements caller, ORG, external-pool
    /// and global counts in step.
    ///
    /// A counter that fails to decrement drifts upward forever and eventually
    /// denies a legitimate caller with no live entries to justify it — a leak
    /// that only shows up under sustained load, which is when it is hardest to
    /// diagnose. All four are asserted, before and after.
    #[test]
    fn reclamation_releases_every_counter_in_step() {
        let guard = partitioned();
        let t0 = Instant::now();
        let short = t0 + Duration::from_secs(5);
        let org = external_org(0xB1);

        let (admitted, _) = flood_org(&guard, &org, [1u8, 11u8], short, t0);
        assert_eq!(admitted, 8);
        assert_eq!(guard.len(), 8, "global");
        assert_eq!(guard.org_len(&org), 8, "per-org");
        assert_eq!(guard.external_len(), 8, "external pool");
        assert_eq!(guard.caller_len(&caller(1)), 4, "per-caller");

        let later = t0 + Duration::from_secs(6);
        assert_eq!(guard.evict_expired(later), 8);

        assert_eq!(guard.len(), 0, "global count leaked");
        assert_eq!(guard.org_len(&org), 0, "per-org count leaked");
        assert_eq!(guard.external_len(), 0, "external-pool count leaked");
        assert_eq!(guard.caller_len(&caller(1)), 0, "per-caller count leaked");

        assert_eq!(
            admit_external(
                &guard,
                &org,
                &caller(1),
                100,
                [1u8; 32],
                later + Duration::from_secs(30),
                later
            ),
            ReplayOutcome::Admitted,
            "a reclaimed org slot must be usable again, or the quota is a \
             one-way ratchet",
        );
    }

    /// Witness 6 — the three capacity outcomes are DISTINCT, so an operator
    /// can tell an abusive grantee from external saturation from true global
    /// exhaustion. Conflating them is what makes this class of incident
    /// unattributable.
    #[test]
    fn the_three_capacity_outcomes_are_distinguishable() {
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);

        // (a) one org over ITS quota; pool and global both fine.
        let guard = partitioned();
        let org = external_org(0xB1);
        flood_org(&guard, &org, [1u8, 11u8], expires, now);
        assert_eq!(
            admit_external(&guard, &org, &caller(21), 0, [21u8; 32], expires, now),
            ReplayOutcome::PerOrganizationCapacityExhausted,
        );
        assert_eq!(guard.per_org_denials(), 1);
        assert_eq!(guard.external_pool_denials(), 0);
        assert_eq!(guard.capacity_denials(), 0);

        // (b) external pool full, no single org over quota.
        let guard = partitioned();
        for org_byte in 0..8u8 {
            let org = external_org(0xC0 + org_byte);
            flood_org(
                &guard,
                &org,
                [org_byte * 2 + 100, org_byte * 2 + 101],
                expires,
                now,
            );
        }
        assert_eq!(
            admit_external(
                &guard,
                &external_org(0xFE),
                &caller(99),
                1,
                [9u8; 32],
                expires,
                now
            ),
            ReplayOutcome::ExternalPoolCapacityExhausted,
        );
        assert!(guard.external_pool_denials() >= 1);
        assert_eq!(
            guard.capacity_denials(),
            0,
            "external saturation is NOT global exhaustion — the owner reserve \
             is free by construction, so reporting it as global would send an \
             operator looking for fleet-wide pressure that does not exist",
        );

        // (c) true global exhaustion, reachable only by OWNER traffic.
        let guard = partitioned();
        for identity in 0..10u8 {
            for call in 0..4u64 {
                admit_owner(
                    &guard,
                    &caller(identity),
                    call,
                    [identity; 32],
                    expires,
                    now,
                );
            }
        }
        assert_eq!(guard.len(), 40);
        assert_eq!(
            admit_owner(&guard, &caller(50), 1, [50u8; 32], expires, now),
            ReplayOutcome::CapacityExhausted,
        );
        assert_eq!(guard.capacity_denials(), 1);
        assert_eq!(guard.per_org_denials(), 0);
        assert_eq!(guard.external_pool_denials(), 0);
    }

    /// The envelope invariants are refused loudly, not clamped: a reserve at
    /// or above the total would leave external callers nothing, and a per-org
    /// quota above the external pool would make the pool bound unreachable.
    #[test]
    fn an_inconsistent_envelope_is_refused() {
        let reserve_too_big = AdmissionReplayConfig {
            max_entries: 100,
            owner_reserved_entries: 100,
            max_entries_per_external_org: 10,
            max_entries_per_caller: 10,
        };
        assert!(matches!(
            reserve_too_big.validate(),
            Err(ReplayConfigError::OwnerReserveNotBelowGlobal { .. })
        ));

        let org_quota_too_big = AdmissionReplayConfig {
            max_entries: 100,
            owner_reserved_entries: 95, // external pool = 5
            max_entries_per_external_org: 10,
            max_entries_per_caller: 10,
        };
        assert!(matches!(
            org_quota_too_big.validate(),
            Err(ReplayConfigError::PerExternalOrgAboveExternalPool { .. })
        ));

        // The shipped default must itself be valid.
        assert!(AdmissionReplayConfig::default().validate().is_ok());
    }

    #[test]
    fn first_admit_records_and_replay_is_denied() {
        let guard = AdmissionReplayGuard::with_defaults();
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);
        let digest = [1u8; 32];

        assert_eq!(
            admit_owner(&guard, &caller(1), 7, digest, expires, now),
            ReplayOutcome::Admitted
        );
        // Same proof re-presented within the window: replay.
        assert_eq!(
            admit_owner(&guard, &caller(1), 7, digest, expires, now),
            ReplayOutcome::Replay
        );
        assert_eq!(guard.len(), 1);
    }

    #[test]
    fn same_call_id_different_binding_is_a_collision() {
        let guard = AdmissionReplayGuard::with_defaults();
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);

        assert_eq!(
            admit_owner(&guard, &caller(1), 7, [1u8; 32], expires, now),
            ReplayOutcome::Admitted
        );
        assert_eq!(
            admit_owner(&guard, &caller(1), 7, [2u8; 32], expires, now),
            ReplayOutcome::CallIdCollision
        );
    }

    #[test]
    fn distinct_callers_and_call_ids_are_independent() {
        let guard = AdmissionReplayGuard::with_defaults();
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);
        let digest = [1u8; 32];

        assert_eq!(
            admit_owner(&guard, &caller(1), 7, digest, expires, now),
            ReplayOutcome::Admitted
        );
        // Different call_id, same caller: independent.
        assert_eq!(
            admit_owner(&guard, &caller(1), 8, digest, expires, now),
            ReplayOutcome::Admitted
        );
        // Different caller, same call_id: independent.
        assert_eq!(
            admit_owner(&guard, &caller(2), 7, digest, expires, now),
            ReplayOutcome::Admitted
        );
        assert_eq!(guard.len(), 3);
    }

    #[test]
    fn expired_entry_permits_legitimate_call_id_reuse() {
        let guard = AdmissionReplayGuard::with_defaults();
        let t0 = Instant::now();
        let expires = t0 + Duration::from_secs(30);
        let digest = [1u8; 32];

        assert_eq!(
            admit_owner(&guard, &caller(1), 7, digest, expires, t0),
            ReplayOutcome::Admitted
        );
        // Same key AFTER the window closes is a fresh, legitimate
        // call — not a replay.
        let later = t0 + Duration::from_secs(31);
        let new_expires = later + Duration::from_secs(30);
        assert_eq!(
            admit_owner(&guard, &caller(1), 7, digest, new_expires, later),
            ReplayOutcome::Admitted
        );
        // And the SAME proof within the NEW window is a replay again.
        assert_eq!(
            admit_owner(&guard, &caller(1), 7, digest, new_expires, later),
            ReplayOutcome::Replay
        );
    }

    #[test]
    fn capacity_denies_without_evicting_a_live_guard() {
        let guard = AdmissionReplayGuard::new(AdmissionReplayConfig {
            max_entries: 2,
            max_entries_per_caller: 1,
            // Pre-§5 shape: no owner reserve and a non-binding org quota, so
            // these witnesses keep testing ONLY the global and per-caller
            // ceilings they were written for rather than acquiring a second,
            // unrelated reason to deny.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 2,
        });
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);

        assert_eq!(
            admit_owner(&guard, &caller(1), 1, [1u8; 32], expires, now),
            ReplayOutcome::Admitted
        );
        assert_eq!(
            admit_owner(&guard, &caller(2), 2, [2u8; 32], expires, now),
            ReplayOutcome::Admitted
        );
        // Full of LIVE entries: a novel admission is denied, and
        // the metric ticks — no live guard is dropped.
        assert_eq!(
            admit_owner(&guard, &caller(3), 3, [3u8; 32], expires, now),
            ReplayOutcome::CapacityExhausted
        );
        assert_eq!(guard.capacity_denials(), 1);
        assert_eq!(guard.len(), 2);
        // The still-live originals remain protected.
        assert_eq!(
            admit_owner(&guard, &caller(1), 1, [1u8; 32], expires, now),
            ReplayOutcome::Replay
        );
    }

    #[test]
    fn capacity_reclaims_expired_slots_before_denying() {
        let guard = AdmissionReplayGuard::new(AdmissionReplayConfig {
            max_entries: 2,
            max_entries_per_caller: 1,
            // Pre-§5 shape: no owner reserve and a non-binding org quota, so
            // these witnesses keep testing ONLY the global and per-caller
            // ceilings they were written for rather than acquiring a second,
            // unrelated reason to deny.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 2,
        });
        let t0 = Instant::now();
        let short = t0 + Duration::from_secs(10);
        let long = t0 + Duration::from_secs(60);

        assert_eq!(
            admit_owner(&guard, &caller(1), 1, [1u8; 32], short, t0),
            ReplayOutcome::Admitted
        );
        assert_eq!(
            admit_owner(&guard, &caller(2), 2, [2u8; 32], long, t0),
            ReplayOutcome::Admitted
        );
        // After caller(1)'s window closes, a novel admission at
        // capacity reclaims the expired slot instead of denying.
        let later = t0 + Duration::from_secs(11);
        assert_eq!(
            admit_owner(
                &guard,
                &caller(3),
                3,
                [3u8; 32],
                later + Duration::from_secs(30),
                later
            ),
            ReplayOutcome::Admitted
        );
        assert_eq!(guard.capacity_denials(), 0);
        // caller(2) (long window) survived; caller(1) was reclaimed.
        assert_eq!(guard.len(), 2);
    }

    #[test]
    fn evict_expired_reclaims_only_closed_windows() {
        let guard = AdmissionReplayGuard::with_defaults();
        let t0 = Instant::now();
        admit_owner(
            &guard,
            &caller(1),
            1,
            [1u8; 32],
            t0 + Duration::from_secs(10),
            t0,
        );
        admit_owner(
            &guard,
            &caller(2),
            2,
            [2u8; 32],
            t0 + Duration::from_secs(60),
            t0,
        );

        let reclaimed = guard.evict_expired(t0 + Duration::from_secs(11));
        assert_eq!(reclaimed, 1);
        assert_eq!(guard.len(), 1);
        // The unexpired one is untouched — still a replay.
        assert_eq!(
            admit_owner(
                &guard,
                &caller(2),
                2,
                [2u8; 32],
                t0 + Duration::from_secs(60),
                t0 + Duration::from_secs(11)
            ),
            ReplayOutcome::Replay
        );
    }

    #[test]
    fn concurrent_admissions_admit_exactly_once() {
        use std::sync::Arc;
        let guard = Arc::new(AdmissionReplayGuard::with_defaults());
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);
        let digest = [7u8; 32];

        let admitted = Arc::new(AtomicU64::new(0));
        let replayed = Arc::new(AtomicU64::new(0));
        let mut handles = Vec::new();
        for _ in 0..16 {
            let guard = guard.clone();
            let admitted = admitted.clone();
            let replayed = replayed.clone();
            handles.push(std::thread::spawn(move || {
                match admit_owner(&guard, &caller(1), 42, digest, expires, now) {
                    ReplayOutcome::Admitted => {
                        admitted.fetch_add(1, Ordering::Relaxed);
                    }
                    ReplayOutcome::Replay => {
                        replayed.fetch_add(1, Ordering::Relaxed);
                    }
                    other => panic!("unexpected outcome {other:?}"),
                }
            }));
        }
        for h in handles {
            h.join().expect("join");
        }
        assert_eq!(admitted.load(Ordering::Relaxed), 1, "exactly one admit");
        assert_eq!(replayed.load(Ordering::Relaxed), 15, "the rest replay");
    }

    /// E1.5 witness 21 — one caller cannot consume another's replay
    /// allocation. Caller(1) fills its per-caller ceiling; a further
    /// NOVEL call from caller(1) is denied `PerCallerCapacityExhausted`,
    /// yet caller(2) admits freely and the GLOBAL capacity denial
    /// metric never ticks.
    #[test]
    fn per_caller_ceiling_isolates_a_flooding_caller() {
        let guard = AdmissionReplayGuard::new(AdmissionReplayConfig {
            max_entries: 1_000,
            max_entries_per_caller: 3,
            // Pre-§5 shape: no owner reserve, non-binding org quota.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 1_000,
        });
        let now = Instant::now();
        let expires = now + Duration::from_secs(30);

        // caller(1) fills its per-caller allocation with 3 novel calls.
        for call_id in 0..3u64 {
            assert_eq!(
                admit_owner(
                    &guard,
                    &caller(1),
                    call_id,
                    [call_id as u8; 32],
                    expires,
                    now
                ),
                ReplayOutcome::Admitted
            );
        }
        assert_eq!(guard.caller_len(&caller(1)), 3);

        // The 4th novel call from caller(1) is denied — only caller(1).
        assert_eq!(
            admit_owner(&guard, &caller(1), 99, [9u8; 32], expires, now),
            ReplayOutcome::PerCallerCapacityExhausted
        );
        assert_eq!(guard.per_caller_denials(), 1);
        assert_eq!(guard.capacity_denials(), 0, "global cap never fired");

        // caller(2) is entirely unaffected — its allocation is its own.
        for call_id in 0..3u64 {
            assert_eq!(
                admit_owner(
                    &guard,
                    &caller(2),
                    call_id,
                    [call_id as u8; 32],
                    expires,
                    now
                ),
                ReplayOutcome::Admitted
            );
        }
        assert_eq!(guard.caller_len(&caller(2)), 3);
        // A still-live replay from caller(1) is unchanged behavior.
        assert_eq!(
            admit_owner(&guard, &caller(1), 0, [0u8; 32], expires, now),
            ReplayOutcome::Replay
        );
    }

    /// The per-caller ceiling reclaims that caller's EXPIRED slots
    /// before denying, so a caller whose earlier calls have aged out
    /// can keep making new ones without ever touching other callers.
    #[test]
    fn per_caller_ceiling_reclaims_expired_before_denying() {
        let guard = AdmissionReplayGuard::new(AdmissionReplayConfig {
            max_entries: 1_000,
            max_entries_per_caller: 2,
            // Pre-§5 shape: no owner reserve, non-binding org quota.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 1_000,
        });
        let t0 = Instant::now();
        let short = t0 + Duration::from_secs(10);

        admit_owner(&guard, &caller(1), 1, [1u8; 32], short, t0);
        admit_owner(&guard, &caller(1), 2, [2u8; 32], short, t0);
        assert_eq!(guard.caller_len(&caller(1)), 2);

        // After caller(1)'s window closes, a novel call at the
        // per-caller cap reclaims the expired slots instead of denying.
        let later = t0 + Duration::from_secs(11);
        assert_eq!(
            admit_owner(
                &guard,
                &caller(1),
                3,
                [3u8; 32],
                later + Duration::from_secs(30),
                later
            ),
            ReplayOutcome::Admitted
        );
        assert_eq!(guard.per_caller_denials(), 0);
        assert_eq!(guard.caller_len(&caller(1)), 1, "expired slots reclaimed");
    }

    /// KC8 — config validation boundaries (Kyra E1 audit). The
    /// invariant is `0 < max_entries_per_caller < max_entries`, loud
    /// via `try_new`, so no config can let one caller consume the
    /// whole global guard.
    #[test]
    fn replay_config_validation_boundaries() {
        // Valid: strictly below.
        assert!(AdmissionReplayConfig {
            max_entries: 10,
            max_entries_per_caller: 9,
            // Pre-§5 shape: no owner reserve and a non-binding org quota, so
            // these witnesses keep testing ONLY the global and per-caller
            // ceilings they were written for rather than acquiring a second,
            // unrelated reason to deny.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 10,
        }
        .validate()
        .is_ok());
        assert!(AdmissionReplayGuard::try_new(AdmissionReplayConfig {
            max_entries: 10,
            max_entries_per_caller: 9,
            // Pre-§5 shape: no owner reserve and a non-binding org quota, so
            // these witnesses keep testing ONLY the global and per-caller
            // ceilings they were written for rather than acquiring a second,
            // unrelated reason to deny.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 10,
        })
        .is_ok());
        // Defaults are valid.
        assert!(AdmissionReplayConfig::default().validate().is_ok());

        // per_caller == max_entries → rejected.
        assert_eq!(
            AdmissionReplayConfig {
                max_entries: 8,
                max_entries_per_caller: 8,
                // Pre-§5 shape: no owner reserve and a non-binding org quota, so
                // these witnesses keep testing ONLY the global and per-caller
                // ceilings they were written for rather than acquiring a second,
                // unrelated reason to deny.
                owner_reserved_entries: 0,
                max_entries_per_external_org: 8,
            }
            .validate(),
            Err(ReplayConfigError::PerCallerNotBelowGlobal {
                per_caller: 8,
                global: 8,
            }),
        );
        // per_caller > max_entries → rejected.
        assert!(matches!(
            AdmissionReplayConfig {
                max_entries: 8,
                max_entries_per_caller: 9,
                // Pre-§5 shape: no owner reserve and a non-binding org quota, so
                // these witnesses keep testing ONLY the global and per-caller
                // ceilings they were written for rather than acquiring a second,
                // unrelated reason to deny.
                owner_reserved_entries: 0,
                max_entries_per_external_org: 8,
            }
            .validate(),
            Err(ReplayConfigError::PerCallerNotBelowGlobal { .. }),
        ));
        // Zero ceilings → rejected.
        assert_eq!(
            AdmissionReplayConfig {
                max_entries: 0,
                max_entries_per_caller: 0,
                // Pre-§5 shape: no owner reserve and a non-binding org quota, so
                // these witnesses keep testing ONLY the global and per-caller
                // ceilings they were written for rather than acquiring a second,
                // unrelated reason to deny.
                owner_reserved_entries: 0,
                max_entries_per_external_org: 0,
            }
            .validate(),
            Err(ReplayConfigError::ZeroGlobalCeiling),
        );
        assert_eq!(
            AdmissionReplayConfig {
                max_entries: 4,
                max_entries_per_caller: 0,
                // Pre-§5 shape: no owner reserve and a non-binding org quota, so
                // these witnesses keep testing ONLY the global and per-caller
                // ceilings they were written for rather than acquiring a second,
                // unrelated reason to deny.
                owner_reserved_entries: 0,
                max_entries_per_external_org: 4,
            }
            .validate(),
            Err(ReplayConfigError::ZeroPerCallerCeiling),
        );
        // try_new surfaces the error rather than clamping.
        assert!(AdmissionReplayGuard::try_new(AdmissionReplayConfig {
            max_entries: 8,
            max_entries_per_caller: 8,
            // Pre-§5 shape: no owner reserve and a non-binding org quota, so
            // these witnesses keep testing ONLY the global and per-caller
            // ceilings they were written for rather than acquiring a second,
            // unrelated reason to deny.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 8,
        })
        .is_err());
    }

    /// The loud `new` panics on an invalid config (never clamps).
    #[test]
    #[should_panic(expected = "invalid AdmissionReplayConfig")]
    fn replay_new_panics_on_invalid_config() {
        let _ = AdmissionReplayGuard::new(AdmissionReplayConfig {
            max_entries: 4,
            max_entries_per_caller: 4,
            // Pre-§5 shape: no owner reserve and a non-binding org quota, so
            // these witnesses keep testing ONLY the global and per-caller
            // ceilings they were written for rather than acquiring a second,
            // unrelated reason to deny.
            owner_reserved_entries: 0,
            max_entries_per_external_org: 4,
        });
    }
}