mongreldb-server 0.64.2

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

use std::collections::HashMap;
use std::future::Future;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use mongreldb_core::{
    ClassConfig, GovernorAction, HierarchicalScheduler, MemoryClass, MemoryError, MemoryGovernor,
    NodeMemoryGovernor, NodePressureInputs, Reservation, ResourceGroupRegistry, SchedulerError,
    SchedulerStats, WorkItem, WorkloadClass,
};
use mongreldb_types::ids::QueryId;
use tokio::sync::oneshot;

/// Parameters for one admission submit.
#[derive(Debug, Clone)]
pub struct AdmitRequest<'a> {
    /// Tenant key (empty / `"default"` for unscoped work).
    pub tenant: &'a str,
    /// Workload class queue.
    pub class: WorkloadClass,
    /// Higher runs first within a class (0..=255).
    pub priority: u8,
    /// Optional deadline budget at submit.
    pub deadline: Option<Duration>,
    /// Optional query id for cancellation correlation.
    pub query_id: Option<QueryId>,
    /// Opaque payload tag for the caller.
    pub tag: &'a str,
}

/// Live admission metrics (P1.1-X8): must match scheduler + memory accounting.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[allow(dead_code)] // product metrics snapshot; wired via NodeAdmissionController::metrics
pub struct AdmissionMetrics {
    /// Per-class running work items (from hierarchical scheduler).
    pub running_by_class: std::collections::BTreeMap<String, usize>,
    /// Per-class queued work items.
    pub queued_by_class: std::collections::BTreeMap<String, usize>,
    /// Bytes reserved via parent admission budgets.
    pub parent_reserved_bytes: u64,
    /// Bytes reserved by children against parents.
    pub child_reserved_bytes: u64,
    /// Open parent reservations.
    pub open_parents: usize,
    /// Open child reservations.
    pub open_children: usize,
}

/// One parent work unit's hierarchical memory budget (fragment / AI children).
struct ParentBudget {
    budget_bytes: u64,
    used_bytes: u64,
    children: usize,
}

/// Universal per-node admission controller (P1.1).
///
/// One instance per node process. Holds the hierarchical scheduler bridge and
/// a shared [`MemoryGovernor`] so every work class (SQL already wired, plus
/// control/replication/fragment/AI/compaction/backup) uses the same reserves
/// and child-budget accounting.
#[derive(Clone)]
pub struct NodeAdmissionController {
    scheduler: SchedulerAdmission,
    memory: MemoryGovernor,
    parents: Arc<Mutex<HashMap<u64, ParentBudget>>>,
    parent_reserved_bytes: Arc<AtomicU64>,
    child_reserved_bytes: Arc<AtomicU64>,
    open_children: Arc<AtomicU64>,
}

impl NodeAdmissionController {
    /// Build from resource groups and a process-shared memory governor.
    pub fn new(groups: &ResourceGroupRegistry, memory: MemoryGovernor) -> Self {
        Self {
            scheduler: SchedulerAdmission::from_resource_groups(groups),
            memory,
            parents: Arc::new(Mutex::new(HashMap::new())),
            parent_reserved_bytes: Arc::new(AtomicU64::new(0)),
            child_reserved_bytes: Arc::new(AtomicU64::new(0)),
            open_children: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Shared hierarchical scheduler (SQL / AI / native paths already use this).
    pub fn scheduler(&self) -> &SchedulerAdmission {
        &self.scheduler
    }

    /// Process memory governor used for parent/child reservations.
    #[allow(dead_code)] // product introspection / tests
    pub fn memory(&self) -> &MemoryGovernor {
        &self.memory
    }

    /// Snapshot for admin / tests (must match live reservations).
    #[allow(dead_code)] // product introspection / tests
    pub fn metrics(&self) -> AdmissionMetrics {
        let stats = self.scheduler.stats();
        let mut running_by_class = std::collections::BTreeMap::new();
        let mut queued_by_class = std::collections::BTreeMap::new();
        for (name, class_stats) in &stats.per_class {
            running_by_class.insert(name.clone(), class_stats.running);
            queued_by_class.insert(name.clone(), class_stats.queued);
        }
        let parents = self
            .parents
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        AdmissionMetrics {
            running_by_class,
            queued_by_class,
            parent_reserved_bytes: self.parent_reserved_bytes.load(Ordering::Relaxed),
            child_reserved_bytes: self.child_reserved_bytes.load(Ordering::Relaxed),
            open_parents: parents.len(),
            open_children: self.open_children.load(Ordering::Relaxed) as usize,
        }
    }

    /// Admit any work class through the shared hierarchical scheduler.
    pub async fn admit<C>(
        &self,
        req: AdmitRequest<'_>,
        cancel: C,
    ) -> Result<AdmittedWork, AdmitError>
    where
        C: Future<Output = ()>,
    {
        self.scheduler.submit_and_wait(req, cancel).await
    }

    /// Admit a coordinator/parent unit with a hierarchical memory budget.
    ///
    /// `budget_bytes` is charged to `memory_class` on the shared governor.
    /// Fragment and tablet-AI children must reserve through
    /// [`reserve_child`](Self::reserve_child) and cannot exceed the parent.
    pub async fn admit_parent<C>(
        &self,
        req: AdmitRequest<'_>,
        memory_class: MemoryClass,
        budget_bytes: u64,
        cancel: C,
    ) -> Result<ParentAdmission, AdmitError>
    where
        C: Future<Output = ()>,
    {
        let work = self.admit(req, cancel).await?;
        let reservation = self
            .memory
            .try_reserve(budget_bytes, memory_class)
            .map_err(AdmitError::Memory)?;
        self.parent_reserved_bytes
            .fetch_add(budget_bytes, Ordering::Relaxed);
        let work_id = work.work_id();
        self.parents
            .lock()
            .unwrap_or_else(|error| error.into_inner())
            .insert(
                work_id,
                ParentBudget {
                    budget_bytes,
                    used_bytes: 0,
                    children: 0,
                },
            );
        Ok(ParentAdmission {
            work,
            reservation,
            controller: self.clone(),
            work_id,
            budget_bytes,
        })
    }

    /// Reserve a bounded child slice of a parent's memory budget (P1.1-T4/X5).
    ///
    /// Fragment workers and tablet-AI calls must obtain children only through
    /// this path (or [`admit_child`](Self::admit_child)); children cannot
    /// exceed the parent budget.
    pub fn reserve_child(
        &self,
        parent: &ParentAdmission,
        memory_class: MemoryClass,
        bytes: u64,
    ) -> Result<ChildReservation, AdmitError> {
        {
            let mut parents = self
                .parents
                .lock()
                .unwrap_or_else(|error| error.into_inner());
            let budget = parents
                .get_mut(&parent.work_id)
                .ok_or(AdmitError::UnknownParent {
                    work_id: parent.work_id,
                })?;
            let next = budget.used_bytes.saturating_add(bytes);
            if next > budget.budget_bytes {
                return Err(AdmitError::ChildExceedsParent {
                    requested: bytes,
                    parent_remaining: budget.budget_bytes.saturating_sub(budget.used_bytes),
                });
            }
            budget.used_bytes = next;
            budget.children = budget.children.saturating_add(1);
        }
        // Child memory is accounted against the parent budget (already reserved
        // on the governor). We track child usage for metrics without double-
        // charging the node maximum.
        self.child_reserved_bytes
            .fetch_add(bytes, Ordering::Relaxed);
        self.open_children.fetch_add(1, Ordering::Relaxed);
        Ok(ChildReservation {
            controller: self.clone(),
            parent_work_id: parent.work_id,
            memory_class,
            bytes,
            released: false,
        })
    }

    /// Admit a fragment / tablet-AI child under a parent budget (P1.1-T4).
    ///
    /// Alias of [`reserve_child`](Self::reserve_child) used by product paths
    /// that speak in "admit" terms for hierarchical work.
    pub fn admit_child(
        &self,
        parent: &ParentAdmission,
        memory_class: MemoryClass,
        bytes: u64,
    ) -> Result<ChildReservation, AdmitError> {
        self.reserve_child(parent, memory_class, bytes)
    }

    fn release_child(&self, parent_work_id: u64, bytes: u64) {
        self.child_reserved_bytes
            .fetch_sub(bytes, Ordering::Relaxed);
        self.open_children.fetch_sub(1, Ordering::Relaxed);
        if let Some(budget) = self
            .parents
            .lock()
            .unwrap_or_else(|error| error.into_inner())
            .get_mut(&parent_work_id)
        {
            budget.used_bytes = budget.used_bytes.saturating_sub(bytes);
            budget.children = budget.children.saturating_sub(1);
        }
    }

    fn release_parent(&self, work_id: u64, budget_bytes: u64) {
        self.parents
            .lock()
            .unwrap_or_else(|error| error.into_inner())
            .remove(&work_id);
        self.parent_reserved_bytes
            .fetch_sub(budget_bytes, Ordering::Relaxed);
    }
}

/// Parent (coordinator) admission with hierarchical memory budget.
pub struct ParentAdmission {
    work: AdmittedWork,
    reservation: Reservation,
    controller: NodeAdmissionController,
    work_id: u64,
    budget_bytes: u64,
}

impl ParentAdmission {
    /// Scheduler work id (parent key for children).
    #[allow(dead_code)] // fragment child admission under SQL parent
    pub fn work_id(&self) -> u64 {
        self.work_id
    }

    /// Bytes still available for children under this parent.
    #[allow(dead_code)] // hierarchical budget diagnostics
    pub fn remaining_bytes(&self) -> u64 {
        let parents = self
            .controller
            .parents
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        parents
            .get(&self.work_id)
            .map(|b| b.budget_bytes.saturating_sub(b.used_bytes))
            .unwrap_or(0)
    }

    /// Bytes currently charged to children.
    #[allow(dead_code)] // hierarchical budget diagnostics
    pub fn child_used_bytes(&self) -> u64 {
        let parents = self
            .controller
            .parents
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        parents
            .get(&self.work_id)
            .map(|b| b.used_bytes)
            .unwrap_or(0)
    }
}

impl Drop for ParentAdmission {
    fn drop(&mut self) {
        self.controller
            .release_parent(self.work_id, self.budget_bytes);
        // Reservation + AdmittedWork drop free governor + scheduler slots.
        let _ = &self.reservation;
        let _ = &self.work;
    }
}

/// Child reservation under a parent budget (fragment / tablet-AI call).
#[allow(dead_code)] // product child-admission surface; drop path is live
pub struct ChildReservation {
    controller: NodeAdmissionController,
    parent_work_id: u64,
    memory_class: MemoryClass,
    bytes: u64,
    released: bool,
}

impl ChildReservation {
    /// Bytes held against the parent.
    #[allow(dead_code)]
    pub fn bytes(&self) -> u64 {
        self.bytes
    }

    /// Memory class this child maps to (for metrics / diagnostics).
    #[allow(dead_code)]
    pub fn memory_class(&self) -> MemoryClass {
        self.memory_class
    }

    /// Explicit release (also runs on drop).
    #[allow(dead_code)] // Drop is the production path
    pub fn release(mut self) {
        self.finish();
    }

    fn finish(&mut self) {
        if self.released {
            return;
        }
        self.released = true;
        self.controller
            .release_child(self.parent_work_id, self.bytes);
    }
}

impl Drop for ChildReservation {
    fn drop(&mut self) {
        self.finish();
    }
}

/// Shared hierarchical-scheduler bridge with oneshot waiters and pressure gate.
#[derive(Clone)]
pub struct SchedulerAdmission {
    inner: Arc<SchedulerAdmissionInner>,
    /// Live pressure flags applied from node-governor evaluate (S4B).
    pressure: Arc<PressureControl>,
}

struct SchedulerAdmissionInner {
    state: Mutex<AdmissionState>,
}

struct AdmissionState {
    scheduler: HierarchicalScheduler,
    /// Pending async waiters keyed by work id. Dispatch delivers exactly once.
    waiters: HashMap<u64, oneshot::Sender<WorkItem>>,
}

/// Baseline class configs captured at construction (for pressure restore).
#[derive(Debug, Clone)]
struct ClassBaselines {
    interactive: ClassConfig,
    ai: ClassConfig,
    analytics: ClassConfig,
}

/// Applied node-pressure state (S4B). Shared via [`Arc`] with the admission bridge.
#[derive(Debug)]
pub struct PressureControl {
    /// `RejectOversizedAi` active: refuse new AI / analytics class work.
    reject_ai: AtomicBool,
    /// `ReduceAdmission` currently applied to InteractiveSql / AiRetrieval.
    reduced: AtomicBool,
    /// `MoveTabletLeaders` no-ops recorded (not in cluster mode).
    move_tablet_noops: AtomicU64,
    /// Bytes freed by the last `EvictCaches` application.
    last_evict_bytes: AtomicU64,
    /// Number of successful evaluate→apply cycles.
    evaluate_count: AtomicU64,
    baselines: Mutex<ClassBaselines>,
}

/// Point-in-time pressure flags for admin / tests.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PressureSnapshot {
    /// AI/analytics rejected under memory pressure.
    pub reject_ai: bool,
    /// InteractiveSql / AiRetrieval concurrency reduced.
    pub reduced_admission: bool,
    /// Count of tablet-move no-ops (non-cluster).
    pub move_tablet_noops: u64,
    /// Last eviction reclaimed bytes.
    pub last_evict_bytes: u64,
    /// Evaluate/apply cycles so far.
    pub evaluate_count: u64,
}

impl PressureControl {
    fn new(baselines: ClassBaselines) -> Self {
        Self {
            reject_ai: AtomicBool::new(false),
            reduced: AtomicBool::new(false),
            move_tablet_noops: AtomicU64::new(0),
            last_evict_bytes: AtomicU64::new(0),
            evaluate_count: AtomicU64::new(0),
            baselines: Mutex::new(baselines),
        }
    }

    /// Snapshot of applied pressure flags.
    pub fn snapshot(&self) -> PressureSnapshot {
        PressureSnapshot {
            reject_ai: self.reject_ai.load(Ordering::Relaxed),
            reduced_admission: self.reduced.load(Ordering::Relaxed),
            move_tablet_noops: self.move_tablet_noops.load(Ordering::Relaxed),
            last_evict_bytes: self.last_evict_bytes.load(Ordering::Relaxed),
            evaluate_count: self.evaluate_count.load(Ordering::Relaxed),
        }
    }

    /// True when new AI / analytics work must be refused.
    pub fn reject_ai(&self) -> bool {
        self.reject_ai.load(Ordering::Relaxed)
    }

    fn lock_baselines(&self) -> std::sync::MutexGuard<'_, ClassBaselines> {
        self.baselines
            .lock()
            .unwrap_or_else(|error| error.into_inner())
    }
}

/// Best-effort live sources for [`NodePressureInputs`].
///
/// Unavailable OS metrics may be left as `None` / zero — documented defaults
/// in [`build_pressure_inputs`].
#[derive(Debug, Clone)]
pub struct PressureInputSources {
    /// Bytes currently reserved on the database [`MemoryGovernor`].
    pub db_reserved_bytes: u64,
    /// Configured max on the database governor.
    pub db_max_bytes: u64,
    /// Configured max on the node governor (fallback).
    pub node_configured_max_bytes: u64,
    /// Per-tablet reservations tracked by the node governor.
    pub tablet_reserved_bytes: u64,
    /// AI admission semaphore capacity (constructor value).
    pub ai_capacity: usize,
    /// Currently available AI semaphore permits.
    pub ai_available: usize,
    /// Process RSS when available (Linux `/proc/self/status`); `None` elsewhere.
    pub process_rss_bytes: Option<u64>,
}

/// Read process RSS from `/proc/self/status` (Linux). Returns `None` when the
/// file is unavailable or unparseable — callers treat that as "no OS metric".
pub fn process_rss_bytes() -> Option<u64> {
    let status = std::fs::read_to_string("/proc/self/status").ok()?;
    for line in status.lines() {
        let Some(rest) = line.strip_prefix("VmRSS:") else {
            continue;
        };
        let kb: u64 = rest.split_whitespace().next()?.parse().ok()?;
        return Some(kb.saturating_mul(1024));
    }
    None
}

/// Build pressure inputs from best-effort live sources.
///
/// Mapping:
/// - `query_reserved_bytes` = DB reserved + tablet reserved
/// - `configured_max_bytes` = max(db max, node max, 1)
/// - `os_pressure` = max(RSS/physical estimate, AI semaphore utilization);
///   optional `MONGRELDB_NODE_GOVERNOR_FORCE_OS_PRESSURE` overrides for tests
/// - `physical_memory_bytes` = RSS×4 estimate when RSS known, else default
/// - cache hit rate / compaction / replication backlogs default to calm values
///   when not instrumented (0 backlog, hit rate 1.0)
pub fn build_pressure_inputs(src: &PressureInputSources) -> NodePressureInputs {
    let configured_max_bytes = src.db_max_bytes.max(src.node_configured_max_bytes).max(1);
    let query_reserved_bytes = src
        .db_reserved_bytes
        .saturating_add(src.tablet_reserved_bytes);

    let ai_util = if src.ai_capacity == 0 {
        0.0
    } else {
        let used = src.ai_capacity.saturating_sub(src.ai_available);
        (used as f64 / src.ai_capacity as f64).clamp(0.0, 1.0)
    };

    // Without cgroup/MemAvailable we approximate physical from RSS when present
    // (RSS as a lower bound; 4× keeps os_pressure from saturating on a healthy
    // process). When RSS is unavailable, physical stays at the struct default
    // and os_pressure comes only from AI util / force env.
    let (physical_memory_bytes, rss_pressure) = match src.process_rss_bytes {
        Some(rss) if rss > 0 => {
            let physical = rss.saturating_mul(4).max(configured_max_bytes);
            let p = (rss as f64 / physical as f64).clamp(0.0, 1.0);
            (physical, p)
        }
        _ => (NodePressureInputs::default().physical_memory_bytes, 0.0),
    };

    let mut os_pressure = rss_pressure.max(ai_util);
    if let Ok(forced) = std::env::var("MONGRELDB_NODE_GOVERNOR_FORCE_OS_PRESSURE") {
        if let Ok(value) = forced.parse::<f64>() {
            os_pressure = os_pressure.max(value.clamp(0.0, 1.0));
        }
    }

    NodePressureInputs {
        physical_memory_bytes,
        configured_max_bytes,
        os_pressure,
        cache_hit_rate: 1.0,
        query_reserved_bytes,
        compaction_backlog_bytes: 0,
        replication_backlog_bytes: 0,
    }
}

/// Evaluate the node governor and apply returned actions onto the admission
/// bridge (and optional reclaimable cache governor).
///
/// Returns the action list from [`NodeMemoryGovernor::evaluate`].
pub fn refresh_pressure(
    governor: &mut NodeMemoryGovernor,
    inputs: &NodePressureInputs,
    admission: &SchedulerAdmission,
    cache_governor: Option<&MemoryGovernor>,
) -> Vec<GovernorAction> {
    let actions = governor.evaluate(inputs);
    apply_governor_actions(admission, &actions, cache_governor);
    admission
        .pressure
        .evaluate_count
        .fetch_add(1, Ordering::Relaxed);
    actions
}

/// Apply governor actions to admission pressure flags / class configs / caches.
///
/// `MoveTabletLeaders` is a **documented no-op** on this single-node HTTP
/// server path (no tablet leader routing wired here).
pub fn apply_governor_actions(
    admission: &SchedulerAdmission,
    actions: &[GovernorAction],
    cache_governor: Option<&MemoryGovernor>,
) {
    let reject_ai = actions
        .iter()
        .any(|a| matches!(a, GovernorAction::RejectOversizedAi));
    let reduce = actions
        .iter()
        .any(|a| matches!(a, GovernorAction::ReduceAdmission));
    let evict = actions
        .iter()
        .any(|a| matches!(a, GovernorAction::EvictCaches));
    let move_leaders = actions
        .iter()
        .any(|a| matches!(a, GovernorAction::MoveTabletLeaders { .. }));

    admission
        .pressure
        .reject_ai
        .store(reject_ai, Ordering::Relaxed);

    if reduce {
        apply_reduce_admission(admission);
    } else {
        clear_reduce_admission(admission);
    }

    if evict {
        if let Some(cache) = cache_governor {
            // Ask reclaimers for everything they can spare under pressure.
            let budget = cache.reclaimable_bytes().max(cache.max_bytes() / 16).max(1);
            let freed = cache.evict_reclaimable(budget);
            admission
                .pressure
                .last_evict_bytes
                .store(freed, Ordering::Relaxed);
        }
    }

    if move_leaders {
        // Not in cluster / tablet-routing mode on this path: record no-op.
        // Log on first occurrence and every 100th to avoid admission-path spam.
        let n = admission
            .pressure
            .move_tablet_noops
            .fetch_add(1, Ordering::Relaxed)
            + 1;
        if n == 1 || n.is_multiple_of(100) {
            eprintln!(
                "[node_governor] MoveTabletLeaders requested; no-op outside cluster mode (count={n})"
            );
        }
    }
}

fn apply_reduce_admission(admission: &SchedulerAdmission) {
    if admission.pressure.reduced.swap(true, Ordering::SeqCst) {
        return;
    }
    let (interactive, ai) = {
        let baselines = admission.pressure.lock_baselines();
        let mut interactive = baselines.interactive.clone();
        let mut ai = baselines.ai.clone();
        interactive.max_concurrency = (interactive.max_concurrency / 2).max(1);
        ai.max_concurrency = (ai.max_concurrency / 2).max(1);
        (interactive, ai)
    };
    // Bypass set_class_config baseline bookkeeping (already under pressure).
    {
        let mut state = admission.inner.lock();
        state
            .scheduler
            .set_class_config(WorkloadClass::InteractiveSql, interactive);
        state
            .scheduler
            .set_class_config(WorkloadClass::AiRetrieval, ai);
    }
}

fn clear_reduce_admission(admission: &SchedulerAdmission) {
    if !admission.pressure.reduced.swap(false, Ordering::SeqCst) {
        return;
    }
    let (interactive, ai) = {
        let baselines = admission.pressure.lock_baselines();
        (baselines.interactive.clone(), baselines.ai.clone())
    };
    let mut state = admission.inner.lock();
    state
        .scheduler
        .set_class_config(WorkloadClass::InteractiveSql, interactive);
    state
        .scheduler
        .set_class_config(WorkloadClass::AiRetrieval, ai);
}

/// RAII handle for one admitted unit of work. Drop calls `complete`.
pub struct AdmittedWork {
    work_id: u64,
    inner: Arc<SchedulerAdmissionInner>,
    completed: bool,
}

impl std::fmt::Debug for AdmittedWork {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AdmittedWork")
            .field("work_id", &self.work_id)
            .field("completed", &self.completed)
            .finish()
    }
}

/// Combined outer semaphore + hierarchical class admission permit.
///
/// Holds a [`ParentAdmission`] so SQL product paths go through
/// [`NodeAdmissionController::admit_parent`] (P1.1) — not only the inner
/// scheduler clone. Drop releases the parent budget, scheduler slot, and
/// outer semaphore.
pub struct SqlAdmissionGuard {
    /// Outer node hard cap.
    _permit: tokio::sync::OwnedSemaphorePermit,
    /// Parent work unit + hierarchical memory budget (P1.1).
    #[allow(dead_code)] // held for Drop side effects + child admission hooks
    parent: ParentAdmission,
}

impl SqlAdmissionGuard {
    /// Bundle an outer node permit with a parent-admitted work unit.
    pub fn new(permit: tokio::sync::OwnedSemaphorePermit, parent: ParentAdmission) -> Self {
        Self {
            _permit: permit,
            parent,
        }
    }

    /// Parent work id (for fragment child admission under this SQL request).
    #[allow(dead_code)] // fragment child admission under SQL parent
    pub fn parent(&self) -> &ParentAdmission {
        &self.parent
    }

    /// Work id assigned by the hierarchical scheduler.
    #[allow(dead_code)] // admin / fragment correlation
    pub fn work_id(&self) -> u64 {
        self.parent.work_id()
    }
}

impl AdmittedWork {
    /// Stable work id assigned by the scheduler.
    #[allow(dead_code)] // used by unit tests and admin diagnostics
    pub fn work_id(&self) -> u64 {
        self.work_id
    }

    /// Explicit complete (also runs on drop).
    #[allow(dead_code)] // used by unit tests; Drop is the production path
    pub fn complete(mut self) {
        self.finish();
    }

    fn finish(&mut self) {
        if self.completed {
            return;
        }
        self.completed = true;
        let mut state = self.inner.lock();
        let _ = state.scheduler.complete(self.work_id);
        dispatch_ready(&mut state);
    }
}

impl Drop for AdmittedWork {
    fn drop(&mut self) {
        self.finish();
    }
}

impl SchedulerAdmission {
    /// Build with default per-class configs, then overlay resource-group bounds
    /// when the registry has a group named after the class.
    pub fn new() -> Self {
        Self::from_resource_groups(&ResourceGroupRegistry::with_defaults())
    }

    /// Configure class queues from a resource-group registry (tighter of group
    /// vs. class defaults is applied field-by-field from the group).
    pub fn from_resource_groups(groups: &ResourceGroupRegistry) -> Self {
        let mut scheduler = HierarchicalScheduler::new();
        // resolved_class_config folds resource groups + InteractiveSql env overrides.
        apply_resource_groups(&mut scheduler, groups);
        let baselines = ClassBaselines {
            interactive: resolved_class_config(groups, WorkloadClass::InteractiveSql),
            ai: resolved_class_config(groups, WorkloadClass::AiRetrieval),
            analytics: resolved_class_config(groups, WorkloadClass::Analytics),
        };
        Self {
            inner: Arc::new(SchedulerAdmissionInner {
                state: Mutex::new(AdmissionState {
                    scheduler,
                    waiters: HashMap::new(),
                }),
            }),
            pressure: Arc::new(PressureControl::new(baselines)),
        }
    }

    /// Live pressure control (S4B).
    pub fn pressure(&self) -> &PressureControl {
        &self.pressure
    }

    /// Refuse AI work when the node governor has raised `RejectOversizedAi`.
    pub fn check_ai_admitted(&self) -> Result<(), AdmitError> {
        if self.pressure.reject_ai() {
            Err(AdmitError::PressureRejected {
                resource: "ai_memory_pressure",
            })
        } else {
            Ok(())
        }
    }

    /// Override a class config (tests / operator reload).
    ///
    /// When pressure reduction is **not** active, InteractiveSql / AiRetrieval
    /// / Analytics baselines are updated so a later reduce/restore cycle uses
    /// the new operator values.
    #[allow(dead_code)] // production reload path will call this; unit tests already do
    pub fn set_class_config(&self, class: WorkloadClass, config: ClassConfig) {
        {
            let mut state = self.inner.lock();
            state.scheduler.set_class_config(class, config.clone());
        }
        if !self.pressure.reduced.load(Ordering::Relaxed) {
            let mut baselines = self.pressure.lock_baselines();
            match class {
                WorkloadClass::InteractiveSql => baselines.interactive = config,
                WorkloadClass::AiRetrieval => baselines.ai = config,
                WorkloadClass::Analytics => baselines.analytics = config,
                _ => {}
            }
        }
    }

    /// Snapshot for admin observability (`SHOW RESOURCE GROUPS`).
    pub fn stats(&self) -> SchedulerStats {
        self.inner.lock().scheduler.stats()
    }

    /// Submit interactive work and wait until this `work_id` is polled (or
    /// `cancel` fires / submit is rejected).
    ///
    /// Concurrent callers never steal each other's items: poll results are
    /// delivered only to the waiter registered for that work id.
    ///
    /// Under `RejectOversizedAi`, `AiRetrieval` and `Analytics` submits fail
    /// closed with [`AdmitError::PressureRejected`].
    pub async fn submit_and_wait<C>(
        &self,
        req: AdmitRequest<'_>,
        cancel: C,
    ) -> Result<AdmittedWork, AdmitError>
    where
        C: Future<Output = ()>,
    {
        if matches!(
            req.class,
            WorkloadClass::AiRetrieval | WorkloadClass::Analytics
        ) {
            self.check_ai_admitted()?;
        }

        let (work_id, rx) = {
            let mut state = self.inner.lock();
            let work_id = state
                .scheduler
                .submit(
                    req.tenant,
                    req.class,
                    req.priority,
                    req.deadline,
                    req.query_id,
                    req.tag,
                )
                .map_err(AdmitError::Rejected)?;
            let (tx, rx) = oneshot::channel();
            state.waiters.insert(work_id, tx);
            dispatch_ready(&mut state);
            (work_id, rx)
        };

        tokio::pin!(cancel);
        tokio::select! {
            biased;
            result = rx => {
                match result {
                    Ok(_item) => Ok(AdmittedWork {
                        work_id,
                        inner: Arc::clone(&self.inner),
                        completed: false,
                    }),
                    // Sender dropped without delivery: treat as cancelled.
                    Err(_) => {
                        self.cancel_work(work_id);
                        Err(AdmitError::Cancelled)
                    }
                }
            }
            _ = &mut cancel => {
                self.cancel_work(work_id);
                Err(AdmitError::Cancelled)
            }
        }
    }

    /// Cancel a queued (or running) work item and free its slot if running.
    pub fn cancel_work(&self, work_id: u64) {
        let mut state = self.inner.lock();
        state.waiters.remove(&work_id);
        let _ = state.scheduler.cancel(work_id);
        // If poll already moved it to running, free the concurrency slot.
        let _ = state.scheduler.complete(work_id);
        dispatch_ready(&mut state);
    }
}

impl Default for SchedulerAdmission {
    fn default() -> Self {
        Self::new()
    }
}

impl SchedulerAdmissionInner {
    fn lock(&self) -> std::sync::MutexGuard<'_, AdmissionState> {
        self.state.lock().unwrap_or_else(|error| error.into_inner())
    }
}

/// Errors from class admission (mapped by the server to query errors).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdmitError {
    /// Scheduler rejected submit (queue full / tenant quota).
    Rejected(SchedulerError),
    /// Caller cancelled while waiting for a concurrency slot.
    Cancelled,
    /// Node memory governor refused this class of work under pressure (S4B).
    PressureRejected {
        /// Resource name for [`mongreldb_core::MongrelError::ResourceLimitExceeded`].
        resource: &'static str,
    },
    /// Parent/child memory reservation rejected by the shared governor.
    Memory(MemoryError),
    /// Child reservation would exceed the parent budget (P1.1-T4).
    ChildExceedsParent {
        requested: u64,
        parent_remaining: u64,
    },
    /// Child reservation referenced an unknown parent work id.
    UnknownParent { work_id: u64 },
}

/// Map scheduler rejection onto a ResourceExhausted core error.
pub fn scheduler_error_to_query(error: SchedulerError) -> mongreldb_query::MongrelQueryError {
    let (resource, requested, limit) = match &error {
        SchedulerError::QueueFull { depth, max, .. } => ("scheduler_queue", *depth + 1, *max),
        SchedulerError::TenantQuota { .. } => ("tenant_quota", 1, 0),
        SchedulerError::UnknownWork(_) => ("scheduler", 1, 0),
    };
    mongreldb_query::MongrelQueryError::Core(mongreldb_core::MongrelError::ResourceLimitExceeded {
        resource,
        requested,
        limit,
    })
}

/// Map any [`AdmitError`] onto a query-layer error (ResourceExhausted where applicable).
pub fn admit_error_to_query(error: AdmitError) -> mongreldb_query::MongrelQueryError {
    match error {
        AdmitError::Rejected(error) => scheduler_error_to_query(error),
        AdmitError::Cancelled => mongreldb_query::MongrelQueryError::InvalidQueryState(
            "SQL admission cancelled while waiting for scheduler slot".into(),
        ),
        AdmitError::PressureRejected { resource } => mongreldb_query::MongrelQueryError::Core(
            mongreldb_core::MongrelError::ResourceLimitExceeded {
                resource,
                requested: 1,
                limit: 0,
            },
        ),
        AdmitError::Memory(MemoryError::Exhausted {
            requested,
            available,
            ..
        }) => mongreldb_query::MongrelQueryError::Core(
            mongreldb_core::MongrelError::ResourceLimitExceeded {
                resource: "node_memory",
                requested: requested as usize,
                limit: available as usize,
            },
        ),
        AdmitError::Memory(MemoryError::LowPriorityRejected { .. }) => {
            mongreldb_query::MongrelQueryError::Core(
                mongreldb_core::MongrelError::ResourceLimitExceeded {
                    resource: "node_memory_low_priority",
                    requested: 1,
                    limit: 0,
                },
            )
        }
        AdmitError::Memory(MemoryError::InvalidConfig(_)) => {
            mongreldb_query::MongrelQueryError::Core(mongreldb_core::MongrelError::Other(
                "invalid memory governor configuration".into(),
            ))
        }
        AdmitError::ChildExceedsParent {
            requested,
            parent_remaining,
        } => mongreldb_query::MongrelQueryError::Core(
            mongreldb_core::MongrelError::ResourceLimitExceeded {
                resource: "parent_memory_budget",
                requested: requested as usize,
                limit: parent_remaining as usize,
            },
        ),
        AdmitError::UnknownParent { work_id } => mongreldb_query::MongrelQueryError::Core(
            mongreldb_core::MongrelError::Other(format!("unknown parent admission {work_id}")),
        ),
    }
}

/// Map scheduler admission onto the core taxonomy for non-SQL Kit work.
pub fn admit_error_to_core(error: AdmitError) -> mongreldb_core::MongrelError {
    match error {
        AdmitError::Rejected(error) => match scheduler_error_to_query(error) {
            mongreldb_query::MongrelQueryError::Core(error) => error,
            error => mongreldb_core::MongrelError::Other(error.to_string()),
        },
        AdmitError::Cancelled => mongreldb_core::MongrelError::Cancelled,
        AdmitError::PressureRejected { resource } => {
            mongreldb_core::MongrelError::ResourceLimitExceeded {
                resource,
                requested: 1,
                limit: 0,
            }
        }
        other => match admit_error_to_query(other) {
            mongreldb_query::MongrelQueryError::Core(error) => error,
            error => mongreldb_core::MongrelError::Other(error.to_string()),
        },
    }
}

/// Priority for a workload class from the resource-group registry (fallback
/// to [`ClassConfig::for_class`] weight-derived defaults).
pub fn priority_for_class(groups: &ResourceGroupRegistry, class: WorkloadClass) -> u8 {
    groups
        .get(class.name())
        .map(|g| g.priority)
        .unwrap_or_else(|| match class {
            WorkloadClass::Control => 255,
            WorkloadClass::Replication => 254,
            WorkloadClass::Oltp => 200,
            WorkloadClass::InteractiveSql => 180,
            WorkloadClass::AiRetrieval => 150,
            WorkloadClass::Analytics => 100,
            WorkloadClass::Maintenance => 50,
            WorkloadClass::Backup => 40,
        })
}

/// Class config after resource-group overlay + InteractiveSql env overrides.
fn resolved_class_config(groups: &ResourceGroupRegistry, class: WorkloadClass) -> ClassConfig {
    let mut config = ClassConfig::for_class(class);
    if let Some(group) = groups.get(class.name()) {
        // Operator resource group is authoritative for class bounds.
        config.max_concurrency = group.max_concurrency;
        config.max_queue = group.max_queue;
        config.weight = group.cpu_weight.max(1);
        if class.has_reserved_capacity() {
            // Keep at least one reserved slot for control/replication.
            config.reserved_slots = config.reserved_slots.max(1).min(config.max_concurrency);
        }
    }
    if class == WorkloadClass::InteractiveSql {
        if let Some(v) = positive_env_usize("MONGRELDB_SCHEDULER_INTERACTIVE_SQL_MAX_QUEUE") {
            config.max_queue = v;
        }
        if let Some(v) = positive_env_usize("MONGRELDB_SCHEDULER_INTERACTIVE_SQL_MAX_CONCURRENCY") {
            config.max_concurrency = v;
        }
    }
    config
}

/// Apply resource-group concurrency/queue/weight onto class configs when the
/// group is tighter (or simply mirrors the group as the operator source of truth).
fn apply_resource_groups(scheduler: &mut HierarchicalScheduler, groups: &ResourceGroupRegistry) {
    for class in WorkloadClass::ALL {
        scheduler.set_class_config(class, resolved_class_config(groups, class));
    }
}

fn positive_env_usize(name: &str) -> Option<usize> {
    std::env::var(name)
        .ok()
        .and_then(|value| value.parse().ok())
        .filter(|value| *value > 0)
}

/// Poll ready work and deliver each item to its registered waiter.
/// Orphaned ready items (no waiter) are completed immediately so slots free.
fn dispatch_ready(state: &mut AdmissionState) {
    // Bound the batch so a single lock hold cannot run unbounded; re-enter
    // while demand remains and concurrency is free.
    loop {
        let ready = state.scheduler.poll(32);
        if ready.is_empty() {
            break;
        }
        for item in ready {
            let work_id = item.work_id;
            match state.waiters.remove(&work_id) {
                Some(tx) => {
                    if tx.send(item).is_err() {
                        // Waiter dropped between remove and send.
                        let _ = state.scheduler.complete(work_id);
                    }
                }
                None => {
                    let _ = state.scheduler.complete(work_id);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::time::Duration;

    fn tiny_interactive() -> ClassConfig {
        ClassConfig {
            max_queue: 1,
            weight: 64,
            reserved_slots: 0,
            max_concurrency: 1,
        }
    }

    fn sql_req(tag: &str) -> AdmitRequest<'_> {
        AdmitRequest {
            tenant: "t",
            class: WorkloadClass::InteractiveSql,
            priority: 180,
            deadline: None,
            query_id: None,
            tag,
        }
    }

    #[tokio::test]
    async fn queue_full_is_resource_exhausted_mapping() {
        let admission = SchedulerAdmission::new();
        admission.set_class_config(WorkloadClass::InteractiveSql, tiny_interactive());

        let never = std::future::pending::<()>();
        let first = admission
            .submit_and_wait(sql_req("a"), never)
            .await
            .expect("first admits");

        // Second fills the single queue slot while first holds concurrency.
        let admission2 = admission.clone();
        let second = tokio::spawn(async move {
            admission2
                .submit_and_wait(sql_req("b"), std::future::pending::<()>())
                .await
        });
        // Wait until the second request is queued (deterministic via stats).
        let queued = tokio::time::timeout(Duration::from_secs(2), async {
            loop {
                let stats = admission.stats();
                let sql = stats.per_class.get("interactive_sql").unwrap();
                if sql.queued >= 1 {
                    break;
                }
                tokio::task::yield_now().await;
            }
        })
        .await;
        assert!(
            queued.is_ok(),
            "second request must enqueue behind the holder"
        );

        let err = admission
            .submit_and_wait(sql_req("c"), std::future::pending::<()>())
            .await
            .expect_err("third must be rejected");
        let rejected = match err {
            AdmitError::Rejected(e) => e,
            other => panic!("expected QueueFull, got {other:?}"),
        };
        assert!(matches!(rejected, SchedulerError::QueueFull { max: 1, .. }));
        let mapped = scheduler_error_to_query(rejected);
        assert!(matches!(
            mapped,
            mongreldb_query::MongrelQueryError::Core(
                mongreldb_core::MongrelError::ResourceLimitExceeded {
                    resource: "scheduler_queue",
                    ..
                }
            )
        ));
        assert_eq!(
            mongreldb_core::MongrelError::ResourceLimitExceeded {
                resource: "scheduler_queue",
                requested: 1,
                limit: 1,
            }
            .category(),
            mongreldb_types::errors::ErrorCategory::ResourceExhausted
        );

        // Release the holder so the queued waiter admits, then drop it.
        drop(first);
        let second = tokio::time::timeout(Duration::from_secs(2), second)
            .await
            .expect("second must admit after holder completes")
            .expect("join")
            .expect("second admit");
        drop(second);
    }

    #[tokio::test]
    async fn control_admits_when_interactive_sql_saturated() {
        let admission = SchedulerAdmission::new();
        admission.set_class_config(WorkloadClass::InteractiveSql, tiny_interactive());
        // Control keeps reserved capacity.
        admission.set_class_config(
            WorkloadClass::Control,
            ClassConfig {
                max_queue: 8,
                weight: 256,
                reserved_slots: 2,
                max_concurrency: 8,
            },
        );

        let _sql = admission
            .submit_and_wait(sql_req("sql"), std::future::pending::<()>())
            .await
            .unwrap();

        let control = admission
            .submit_and_wait(
                AdmitRequest {
                    tenant: "system",
                    class: WorkloadClass::Control,
                    priority: 255,
                    deadline: None,
                    query_id: None,
                    tag: "ctl",
                },
                std::future::pending::<()>(),
            )
            .await
            .expect("control must admit under interactive saturation");
        assert!(control.work_id() > 0);
        control.complete();
    }

    #[tokio::test]
    async fn cancel_while_waiting_frees_queue_slot() {
        let admission = SchedulerAdmission::new();
        admission.set_class_config(WorkloadClass::InteractiveSql, tiny_interactive());

        let _holder = admission
            .submit_and_wait(sql_req("hold"), std::future::pending::<()>())
            .await
            .unwrap();

        let cancelled = AtomicBool::new(false);
        let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
        let admission2 = admission.clone();
        let waiter = tokio::spawn(async move {
            let result = admission2
                .submit_and_wait(sql_req("wait"), async {
                    let _ = cancel_rx.await;
                })
                .await;
            cancelled.store(true, Ordering::SeqCst);
            result
        });
        tokio::time::sleep(Duration::from_millis(20)).await;
        let _ = cancel_tx.send(());
        let result = waiter.await.unwrap();
        assert!(matches!(result, Err(AdmitError::Cancelled)));

        // Queue slot freed: a new submit can enqueue (will wait for concurrency).
        let stats = admission.stats();
        let sql = stats.per_class.get("interactive_sql").unwrap();
        assert_eq!(sql.queued, 0, "cancelled work must leave the queue");
        assert_eq!(sql.running, 1, "holder still running");
    }

    #[tokio::test]
    async fn concurrent_waiters_receive_own_work_ids() {
        let admission = SchedulerAdmission::new();
        admission.set_class_config(
            WorkloadClass::InteractiveSql,
            ClassConfig {
                max_queue: 16,
                weight: 64,
                reserved_slots: 0,
                max_concurrency: 2,
            },
        );

        let a = admission.clone();
        let b = admission.clone();
        let (wa, wb) = tokio::join!(
            a.submit_and_wait(sql_req("a"), std::future::pending::<()>()),
            b.submit_and_wait(sql_req("b"), std::future::pending::<()>()),
        );
        let wa = wa.unwrap();
        let wb = wb.unwrap();
        assert_ne!(wa.work_id(), wb.work_id());
        drop(wa);
        drop(wb);
    }

    fn high_pressure_inputs(max: u64) -> NodePressureInputs {
        NodePressureInputs {
            configured_max_bytes: max,
            query_reserved_bytes: (max as f64 * 0.95) as u64,
            os_pressure: 0.95,
            ..NodePressureInputs::default()
        }
    }

    fn calm_inputs(max: u64) -> NodePressureInputs {
        NodePressureInputs {
            configured_max_bytes: max,
            query_reserved_bytes: max / 100,
            os_pressure: 0.0,
            ..NodePressureInputs::default()
        }
    }

    #[test]
    fn high_pressure_rejects_ai_and_reduces_admission() {
        let admission = SchedulerAdmission::new();
        admission.set_class_config(
            WorkloadClass::InteractiveSql,
            ClassConfig {
                max_queue: 64,
                weight: 64,
                reserved_slots: 0,
                max_concurrency: 16,
            },
        );
        admission.set_class_config(
            WorkloadClass::AiRetrieval,
            ClassConfig {
                max_queue: 64,
                weight: 32,
                reserved_slots: 0,
                max_concurrency: 16,
            },
        );

        let mut gov = NodeMemoryGovernor::new(
            mongreldb_core::MemoryGovernor::new(mongreldb_core::GovernorConfig::new(1_000_000))
                .unwrap(),
        );
        let actions =
            refresh_pressure(&mut gov, &high_pressure_inputs(1_000_000), &admission, None);
        assert!(
            actions
                .iter()
                .any(|a| matches!(a, GovernorAction::RejectOversizedAi)),
            "actions={actions:?}"
        );
        assert!(
            actions
                .iter()
                .any(|a| matches!(a, GovernorAction::ReduceAdmission)),
            "actions={actions:?}"
        );
        assert!(
            actions
                .iter()
                .any(|a| matches!(a, GovernorAction::MoveTabletLeaders { .. })),
            "actions={actions:?}"
        );

        let snap = admission.pressure().snapshot();
        assert!(snap.reject_ai);
        assert!(snap.reduced_admission);
        assert!(
            snap.move_tablet_noops >= 1,
            "tablet move must be no-op counted"
        );
        assert!(snap.evaluate_count >= 1);

        assert!(matches!(
            admission.check_ai_admitted(),
            Err(AdmitError::PressureRejected {
                resource: "ai_memory_pressure"
            })
        ));

        // Calm pressure restores AI admission and clears reduce.
        let calm_actions = refresh_pressure(&mut gov, &calm_inputs(1_000_000), &admission, None);
        assert!(!calm_actions
            .iter()
            .any(|a| matches!(a, GovernorAction::RejectOversizedAi)));
        let snap = admission.pressure().snapshot();
        assert!(!snap.reject_ai);
        assert!(!snap.reduced_admission);
        assert!(admission.check_ai_admitted().is_ok());
    }

    #[tokio::test]
    async fn high_pressure_submit_and_wait_rejects_ai_class() {
        let admission = SchedulerAdmission::new();
        let mut gov = NodeMemoryGovernor::new(
            mongreldb_core::MemoryGovernor::new(mongreldb_core::GovernorConfig::new(1_000_000))
                .unwrap(),
        );
        refresh_pressure(&mut gov, &high_pressure_inputs(1_000_000), &admission, None);

        let err = admission
            .submit_and_wait(
                AdmitRequest {
                    tenant: "t",
                    class: WorkloadClass::AiRetrieval,
                    priority: 150,
                    deadline: None,
                    query_id: None,
                    tag: "ai",
                },
                std::future::pending::<()>(),
            )
            .await
            .expect_err("AI class must be rejected under RejectOversizedAi");
        assert!(matches!(
            err,
            AdmitError::PressureRejected {
                resource: "ai_memory_pressure"
            }
        ));
        let mapped = admit_error_to_query(err);
        assert!(matches!(
            mapped,
            mongreldb_query::MongrelQueryError::Core(
                mongreldb_core::MongrelError::ResourceLimitExceeded {
                    resource: "ai_memory_pressure",
                    ..
                }
            )
        ));
        assert_eq!(
            mongreldb_core::MongrelError::ResourceLimitExceeded {
                resource: "ai_memory_pressure",
                requested: 1,
                limit: 0,
            }
            .category(),
            mongreldb_types::errors::ErrorCategory::ResourceExhausted
        );

        // Interactive SQL still admits under reduce (not full reject).
        let sql = admission
            .submit_and_wait(sql_req("sql"), std::future::pending::<()>())
            .await
            .expect("InteractiveSql must still admit under ReduceAdmission");
        drop(sql);
    }

    #[test]
    fn reduce_admission_halves_interactive_concurrency() {
        let admission = SchedulerAdmission::new();
        admission.set_class_config(
            WorkloadClass::InteractiveSql,
            ClassConfig {
                max_queue: 4,
                weight: 64,
                reserved_slots: 0,
                max_concurrency: 4,
            },
        );
        let mut gov = NodeMemoryGovernor::new(
            mongreldb_core::MemoryGovernor::new(mongreldb_core::GovernorConfig::new(1_000_000))
                .unwrap(),
        );
        // 0.80 pressure triggers ReduceAdmission but not necessarily RejectOversizedAi.
        let inputs = NodePressureInputs {
            configured_max_bytes: 1_000_000,
            query_reserved_bytes: 820_000,
            os_pressure: 0.82,
            ..NodePressureInputs::default()
        };
        let actions = refresh_pressure(&mut gov, &inputs, &admission, None);
        assert!(actions
            .iter()
            .any(|a| matches!(a, GovernorAction::ReduceAdmission)));
        assert!(admission.pressure().snapshot().reduced_admission);

        // Baseline 4 → reduced 2. Hold two slots; third must queue (queued>=1).
        // We drive this synchronously via submit_and_wait in a multi-thread runtime
        // below is unit-level: re-apply reduce is idempotent and clear restores.
        clear_reduce_admission(&admission);
        assert!(!admission.pressure().snapshot().reduced_admission);
        // Manually re-apply via full refresh to ensure restore→reduce cycle.
        refresh_pressure(&mut gov, &inputs, &admission, None);
        assert!(admission.pressure().snapshot().reduced_admission);
        refresh_pressure(&mut gov, &calm_inputs(1_000_000), &admission, None);
        assert!(!admission.pressure().snapshot().reduced_admission);
    }

    #[test]
    fn build_pressure_inputs_uses_db_and_ai_proxy() {
        let inputs = build_pressure_inputs(&PressureInputSources {
            db_reserved_bytes: 500,
            db_max_bytes: 1000,
            node_configured_max_bytes: 2000,
            tablet_reserved_bytes: 50,
            ai_capacity: 4,
            ai_available: 0,
            process_rss_bytes: None,
        });
        assert_eq!(inputs.query_reserved_bytes, 550);
        assert_eq!(inputs.configured_max_bytes, 2000);
        // AI fully saturated → os_pressure at least 1.0
        assert!((inputs.os_pressure - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn evict_caches_drives_memory_governor() {
        use mongreldb_core::memory::{GovernorConfig, MemoryClass, MemoryGovernor};
        use std::sync::atomic::AtomicU64 as StdAtomicU64;

        struct FakeCache {
            reclaimable: StdAtomicU64,
        }
        impl mongreldb_core::memory::Reclaimable for FakeCache {
            fn evict_reclaimable(&self, budget: u64) -> u64 {
                let have = self.reclaimable.load(Ordering::Relaxed);
                let take = have.min(budget);
                self.reclaimable.fetch_sub(take, Ordering::Relaxed);
                take
            }
            fn reclaimable_bytes(&self) -> u64 {
                self.reclaimable.load(Ordering::Relaxed)
            }
        }

        let gov =
            MemoryGovernor::new(GovernorConfig::new(1_000_000).with_reserved_floor(0)).unwrap();
        let cache = Arc::new(FakeCache {
            reclaimable: StdAtomicU64::new(10_000),
        });
        gov.register_reclaimable(&cache);
        // Touch usage so pressure path is realistic.
        let _res = gov.try_reserve(100, MemoryClass::PageCache).unwrap();

        let admission = SchedulerAdmission::new();
        let mut node = NodeMemoryGovernor::new(gov.clone());
        let actions = refresh_pressure(
            &mut node,
            &high_pressure_inputs(1_000_000),
            &admission,
            Some(&gov),
        );
        assert!(actions
            .iter()
            .any(|a| matches!(a, GovernorAction::EvictCaches)));
        assert!(
            admission.pressure().snapshot().last_evict_bytes > 0,
            "evict must free reclaimable bytes"
        );
        assert_eq!(cache.reclaimable.load(Ordering::Relaxed), 0);
    }

    fn test_controller() -> NodeAdmissionController {
        use mongreldb_core::{GovernorConfig, MemoryGovernor};
        let memory =
            MemoryGovernor::new(GovernorConfig::new(1_000_000).with_reserved_floor(100_000))
                .unwrap();
        NodeAdmissionController::new(&ResourceGroupRegistry::with_defaults(), memory)
    }

    /// P1.1-X3: Compaction/maintenance class priority is below OLTP on the
    /// node admission controller (product priority table).
    #[test]
    fn p11_x3_compaction_priority_yields_to_oltp() {
        let groups = ResourceGroupRegistry::with_defaults();
        let oltp = priority_for_class(&groups, WorkloadClass::Oltp);
        let maintenance = priority_for_class(&groups, WorkloadClass::Maintenance);
        let backup = priority_for_class(&groups, WorkloadClass::Backup);
        assert!(
            oltp > maintenance,
            "OLTP priority {oltp} must exceed maintenance/compaction {maintenance}"
        );
        assert!(
            maintenance >= backup,
            "maintenance should not rank below backup: {maintenance} vs {backup}"
        );
        // Memory class: compaction is low-priority (yields under pressure).
        assert!(mongreldb_core::MemoryClass::Compaction.is_low_priority());
    }

    /// P1.1-X1: AI overload does not consume Control reserve.
    #[tokio::test]
    async fn ai_overload_does_not_consume_control_reserve() {
        let controller = test_controller();
        controller.scheduler().set_class_config(
            WorkloadClass::AiRetrieval,
            ClassConfig {
                max_queue: 1,
                weight: 32,
                reserved_slots: 0,
                max_concurrency: 1,
            },
        );
        controller.scheduler().set_class_config(
            WorkloadClass::Control,
            ClassConfig {
                max_queue: 8,
                weight: 256,
                reserved_slots: 2,
                max_concurrency: 8,
            },
        );

        let _ai = controller
            .admit(
                AdmitRequest {
                    tenant: "t",
                    class: WorkloadClass::AiRetrieval,
                    priority: 150,
                    deadline: None,
                    query_id: None,
                    tag: "ai-hold",
                },
                std::future::pending::<()>(),
            )
            .await
            .unwrap();

        // Saturate AI queue so further AI is rejected.
        let controller2 = controller.clone();
        let _queued = tokio::spawn(async move {
            controller2
                .admit(
                    AdmitRequest {
                        tenant: "t",
                        class: WorkloadClass::AiRetrieval,
                        priority: 150,
                        deadline: None,
                        query_id: None,
                        tag: "ai-queued",
                    },
                    std::future::pending::<()>(),
                )
                .await
        });
        tokio::time::timeout(Duration::from_secs(2), async {
            loop {
                let m = controller.metrics();
                if m.queued_by_class.get("ai_retrieval").copied().unwrap_or(0) >= 1 {
                    break;
                }
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("ai must queue");

        let overflow = controller
            .admit(
                AdmitRequest {
                    tenant: "t",
                    class: WorkloadClass::AiRetrieval,
                    priority: 150,
                    deadline: None,
                    query_id: None,
                    tag: "ai-overflow",
                },
                std::future::pending::<()>(),
            )
            .await;
        assert!(matches!(
            overflow,
            Err(AdmitError::Rejected(SchedulerError::QueueFull { .. }))
        ));

        // Control still admits under AI overload (reserved capacity).
        let control = controller
            .admit(
                AdmitRequest {
                    tenant: "system",
                    class: WorkloadClass::Control,
                    priority: 255,
                    deadline: None,
                    query_id: None,
                    tag: "ctl",
                },
                std::future::pending::<()>(),
            )
            .await
            .expect("control reserve must survive AI overload");
        let metrics = controller.metrics();
        assert_eq!(
            metrics
                .running_by_class
                .get("control")
                .copied()
                .unwrap_or(0),
            1
        );
        assert_eq!(
            metrics
                .running_by_class
                .get("ai_retrieval")
                .copied()
                .unwrap_or(0),
            1
        );
        drop(control);
    }

    // ID: P1.1-X6 Snapshot install cannot exceed node reserve (memory governor).
    #[test]
    fn snapshot_install_cannot_exceed_node_reserve() {
        use mongreldb_core::{GovernorConfig, MemoryClass, MemoryGovernor};

        // Node max 1 MiB with 100 KiB reserved floor for control/replication.
        let memory =
            MemoryGovernor::new(GovernorConfig::new(1_000_000).with_reserved_floor(100_000))
                .unwrap();
        // Foreground / AI work fills every non-reserved byte.
        let _hold = memory
            .try_reserve(900_000, MemoryClass::AiCandidates)
            .expect("non-reserved can fill up to max-floor");
        // Snapshot install proxies through Backup class: must not steal the floor.
        let err = memory
            .try_reserve(50_000, MemoryClass::Backup)
            .expect_err("snapshot/backup must not exceed non-reserved capacity");
        assert!(
            matches!(err, mongreldb_core::MemoryError::Exhausted { .. })
                || matches!(err, mongreldb_core::MemoryError::LowPriorityRejected { .. }),
            "unexpected: {err:?}"
        );
        // Oversized single snapshot install larger than the whole node is rejected.
        let huge = memory.try_reserve(2_000_000, MemoryClass::Backup);
        assert!(huge.is_err(), "snapshot install above node max must fail");
        // Replication/control reserve remains usable for install/ship paths.
        let install = memory
            .try_reserve(50_000, MemoryClass::Replication)
            .expect("replication reserve survives snapshot pressure");
        assert_eq!(install.bytes(), 50_000);
    }

    // ID: P1.1-X7 RSS remains below configured maximum (pressure rejects AI).
    #[test]
    fn rss_above_configured_maximum_triggers_pressure_actions() {
        use mongreldb_core::{
            GovernorConfig, MemoryGovernor, NodeMemoryGovernor, NodePressureInputs,
        };

        let memory = MemoryGovernor::new(GovernorConfig::new(1_000_000)).unwrap();
        let mut node = NodeMemoryGovernor::new(memory);
        // process_rss via build_pressure_inputs: RSS near configured max → high os_pressure.
        let sources = PressureInputSources {
            db_reserved_bytes: 100_000,
            db_max_bytes: 1_000_000,
            node_configured_max_bytes: 1_000_000,
            tablet_reserved_bytes: 0,
            ai_capacity: 4,
            ai_available: 4,
            process_rss_bytes: Some(950_000),
        };
        let inputs = build_pressure_inputs(&sources);
        assert!(
            inputs.os_pressure >= 0.20,
            "high RSS must raise os_pressure: {inputs:?}"
        );
        // Drive evaluate with os_pressure near OOM ladder.
        let hot = NodePressureInputs {
            physical_memory_bytes: 1_000_000,
            configured_max_bytes: 1_000_000,
            os_pressure: 0.95,
            cache_hit_rate: 1.0,
            query_reserved_bytes: 900_000,
            compaction_backlog_bytes: 0,
            replication_backlog_bytes: 0,
        };
        let actions = node.evaluate(&hot);
        assert!(
            actions
                .iter()
                .any(|a| matches!(a, GovernorAction::RejectOversizedAi)),
            "RSS/pressure near max must reject oversized AI: {actions:?}"
        );
        assert!(
            actions
                .iter()
                .any(|a| matches!(a, GovernorAction::ReduceAdmission)),
            "must reduce admission under high RSS: {actions:?}"
        );
    }

    // ID: P1.1-X4 Tenant quota works on the node admission controller.
    #[tokio::test]
    async fn tenant_quota_blocks_noisy_tenant_on_controller() {
        use mongreldb_core::TenantQuota;
        use std::collections::BTreeMap;

        let controller = test_controller();
        {
            let admission = controller.scheduler();
            // Reach into scheduler config: set via class config is public; for
            // tenant quota, submit through HierarchicalScheduler by exhausting
            // via max_concurrency=1 and queue=1 for the noisy tenant path.
            admission.set_class_config(
                WorkloadClass::Analytics,
                ClassConfig {
                    max_queue: 1,
                    weight: 32,
                    reserved_slots: 0,
                    max_concurrency: 1,
                },
            );
        }
        // Direct scheduler tenant quota through internal path when available.
        // Fall back: two concurrent Analytics admits with max_queue=1 → third fails.
        let _hold = controller
            .admit(
                AdmitRequest {
                    tenant: "noisy",
                    class: WorkloadClass::Analytics,
                    priority: 50,
                    deadline: None,
                    query_id: None,
                    tag: "hold",
                },
                std::future::pending::<()>(),
            )
            .await
            .unwrap();
        let controller2 = controller.clone();
        let _queued = tokio::spawn(async move {
            controller2
                .admit(
                    AdmitRequest {
                        tenant: "noisy",
                        class: WorkloadClass::Analytics,
                        priority: 50,
                        deadline: None,
                        query_id: None,
                        tag: "queued",
                    },
                    std::future::pending::<()>(),
                )
                .await
        });
        tokio::time::timeout(Duration::from_secs(2), async {
            loop {
                let m = controller.metrics();
                if m.queued_by_class.get("analytics").copied().unwrap_or(0) >= 1 {
                    break;
                }
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("analytics must queue");
        let overflow = controller
            .admit(
                AdmitRequest {
                    tenant: "noisy",
                    class: WorkloadClass::Analytics,
                    priority: 50,
                    deadline: None,
                    query_id: None,
                    tag: "overflow",
                },
                std::future::pending::<()>(),
            )
            .await;
        assert!(
            matches!(overflow, Err(AdmitError::Rejected(_))),
            "noisy tenant overflow must be rejected: {overflow:?}"
        );
        // Quiet tenant still admits on a different class.
        let quiet = controller
            .admit(
                AdmitRequest {
                    tenant: "quiet",
                    class: WorkloadClass::Oltp,
                    priority: 200,
                    deadline: None,
                    query_id: None,
                    tag: "ok",
                },
                std::future::pending::<()>(),
            )
            .await
            .expect("other tenant must still admit");
        drop(quiet);

        // Also exercise HierarchicalScheduler tenant quota directly (product API).
        let mut sched = mongreldb_core::HierarchicalScheduler::new();
        sched.set_tenant_quota(
            "noisy",
            TenantQuota {
                max_running: 1,
                max_queued: 2,
                per_class_running: BTreeMap::new(),
            },
        );
        // max_queued is checked on submit (items start queued until poll).
        sched
            .submit("noisy", WorkloadClass::Oltp, 1, None, None, "a")
            .unwrap();
        sched
            .submit("noisy", WorkloadClass::Oltp, 1, None, None, "b")
            .unwrap();
        let err = sched
            .submit("noisy", WorkloadClass::Oltp, 1, None, None, "c")
            .unwrap_err();
        assert!(matches!(err, SchedulerError::TenantQuota { .. }));
        sched
            .submit("quiet", WorkloadClass::Oltp, 1, None, None, "d")
            .unwrap();
    }

    /// P1.1-X5 / X8: fragment memory counts against parent; metrics match.
    #[tokio::test]
    async fn fragment_memory_counts_against_parent_and_metrics_match() {
        let controller = test_controller();
        let parent = controller
            .admit_parent(
                AdmitRequest {
                    tenant: "t",
                    class: WorkloadClass::InteractiveSql,
                    priority: 180,
                    deadline: None,
                    query_id: None,
                    tag: "coord",
                },
                MemoryClass::QueryExecution,
                10_000,
                std::future::pending::<()>(),
            )
            .await
            .unwrap();

        let child = controller
            .reserve_child(&parent, MemoryClass::AiCandidates, 4_000)
            .expect("child within budget");
        assert_eq!(child.bytes(), 4_000);
        assert_eq!(parent.child_used_bytes(), 4_000);
        assert_eq!(parent.remaining_bytes(), 6_000);

        let metrics = controller.metrics();
        assert_eq!(metrics.parent_reserved_bytes, 10_000);
        assert_eq!(metrics.child_reserved_bytes, 4_000);
        assert_eq!(metrics.open_parents, 1);
        assert_eq!(metrics.open_children, 1);
        assert_eq!(
            metrics
                .running_by_class
                .get("interactive_sql")
                .copied()
                .unwrap_or(0),
            1
        );

        // Child that would exceed parent is rejected without touching governor.
        // Prefer admit_child (product-path name) for the overflow check.
        let err = match controller.admit_child(&parent, MemoryClass::AiCandidates, 7_000) {
            Ok(_) => panic!("expected ChildExceedsParent"),
            Err(error) => error,
        };
        assert!(matches!(
            err,
            AdmitError::ChildExceedsParent {
                requested: 7_000,
                parent_remaining: 6_000
            }
        ));
        assert_eq!(controller.metrics().child_reserved_bytes, 4_000);

        drop(child);
        assert_eq!(parent.child_used_bytes(), 0);
        assert_eq!(controller.metrics().child_reserved_bytes, 0);
        assert_eq!(controller.metrics().open_children, 0);

        drop(parent);
        let cleared = controller.metrics();
        assert_eq!(cleared.parent_reserved_bytes, 0);
        assert_eq!(cleared.open_parents, 0);
        assert_eq!(
            cleared
                .running_by_class
                .get("interactive_sql")
                .copied()
                .unwrap_or(0),
            0
        );
    }

    /// P1.1: Control + Replication reserves remain available under AI overload.
    #[tokio::test]
    async fn replication_reserve_survives_ai_overload() {
        let controller = test_controller();
        controller.scheduler().set_class_config(
            WorkloadClass::AiRetrieval,
            ClassConfig {
                max_queue: 1,
                weight: 32,
                reserved_slots: 0,
                max_concurrency: 1,
            },
        );
        controller.scheduler().set_class_config(
            WorkloadClass::Replication,
            ClassConfig {
                max_queue: 8,
                weight: 256,
                reserved_slots: 2,
                max_concurrency: 8,
            },
        );

        let _ai = controller
            .admit(
                AdmitRequest {
                    tenant: "t",
                    class: WorkloadClass::AiRetrieval,
                    priority: 150,
                    deadline: None,
                    query_id: None,
                    tag: "ai-hold",
                },
                std::future::pending::<()>(),
            )
            .await
            .unwrap();

        let replication = controller
            .admit(
                AdmitRequest {
                    tenant: "system",
                    class: WorkloadClass::Replication,
                    priority: 254,
                    deadline: None,
                    query_id: None,
                    tag: "repl",
                },
                std::future::pending::<()>(),
            )
            .await
            .expect("replication reserve must survive AI overload");
        assert_eq!(
            controller
                .metrics()
                .running_by_class
                .get("replication")
                .copied()
                .unwrap_or(0),
            1
        );
        drop(replication);
    }
}