runnerd-core 0.0.29

Config, domain logic and API types shared by runnerd and its clients
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
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
//! Measured admission on the fakes: more runners than the caps fit when the history predicts small peaks, nothing
//! while the slice is at the high-water mark, the caps without a recent slice sample or after a kill for memory,
//! and the decision in each runner's record.

use super::*;
use crate::admission::pooled::POOL_AFTER;
use crate::api::{AdmissionPolicy, Basis, JobPeak};
use crate::observe::metrics::Events;
use crate::scheduler::PRIORITY_AFTER;
use crate::store::{RunnerRecord, SampleRecord};
use crate::time;

const G: u64 = 1 << 30;
const M: u64 = 1 << 20;

/// `app` 4 slots of 4G under measured admission: 16G of caps for a 10G slice.
fn measured() -> String {
    CONFIG.replace("slots = 2", "slots = 4").replace(
        "isolation = \"sysbox\"",
        "isolation = \"sysbox\"\nadmission = \"measured\"",
    )
}

/// Ten finished `build` jobs of o/r that peaked at `peak`, the last one with `oom_kills` kills for memory.
async fn history(store: &Store, peak: u64, oom_kills: u64) {
    history_of(store, "build", 1000, peak, oom_kills).await;
}

/// Ten finished `name` jobs of o/r from ID `first`, as `history`.
async fn history_of(store: &Store, name: &str, first: u64, peak: u64, oom_kills: u64) {
    runs_of(store, name, first, 10, peak, oom_kills, Ended::Lost).await;
}

/// How a run in `runs_of` ended.
#[derive(Clone, Copy, PartialEq)]
enum Ended {
    /// Its container was gone before GitHub reported it.
    Lost,
    /// GitHub reported it completed with success.
    Succeeded,
}

/// `count` finished `name` jobs of o/r from ID `first`, as `history`.
async fn runs_of(
    store: &Store,
    name: &str,
    first: u64,
    count: u64,
    peak: u64,
    oom_kills: u64,
    ended: Ended,
) {
    let at = time::now_ms() - 3600 * 1000;
    for i in 0..count {
        let id = first + i;
        let runner = format!("earlier-{id}");
        store.queued(
            "o/r",
            &QueuedJob {
                created_at: at,
                name: name.to_owned(),
                ..job(id)
            },
            time::now_ms(),
        );
        store.runner_started(RunnerRecord {
            purpose: None,
            name: runner.clone(),
            repository: "o/r".to_owned(),
            profile: "app".to_owned(),
            slot: 1,
            container: format!("c-{id}"),
            started_at: at,
            memory_cap: Some(4 * G),
            cpu_cap: Some(2000),
            admission: None,
        });
        store.claimed(id, 1, &runner, at + 1000);
        store.samples(vec![SampleRecord {
            runner: runner.clone(),
            at: at + 2000,
            cpu: 0,
            cpu_usage: 0,
            memory: peak,
            network_received: 0,
            network_sent: 0,
            cpu_held: crate::observe::metrics::CpuHeld::default(),
            events: Events {
                oom: Some(0),
                oom_kill: Some(if i == count - 1 { oom_kills } else { 0 }),
                sock_throttled: None,
                local_oom: None,
            },
        }]);
        match ended {
            Ended::Lost => store.lost(id, at + 60_000),
            // No times: the job keeps the ones it was queued and claimed at.
            Ended::Succeeded => store.details(JobDetails {
                id,
                run_id: 1,
                run_attempt: 1,
                name: name.to_owned(),
                workflow_name: None,
                head_branch: None,
                html_url: None,
                labels: Vec::new(),
                created_at: None,
                status: "completed".to_owned(),
                conclusion: Some("success".to_owned()),
                started_at: None,
                completed_at: None,
                runner_name: Some(runner.clone()),
                steps: Vec::new(),
            }),
        }
        store.runner_removed(&runner, at + 60_000);
    }
    store.flush().await;
}

/// The slice's memory, sampled now.
fn slice_at(store: &Store, memory: u64) {
    store.host_sample(api::HostSample {
        at: time::now_ms() / 1000,
        cpu: 0,
        memory,
    });
}

/// An engine under measured admission with a 10G slice and the history in place, four build jobs queued.
async fn measured_engine(
    github: &FakeGithub,
    docker: &FakeDocker,
    dir: &TestDir,
    peak: u64,
    oom_kills: u64,
) -> (Engine<FakeGithub, FakeDocker>, Handle) {
    let (mut engine, handle) = engine_with(github, docker, dir, &measured());
    engine.memory_max = Some(10 * G);
    history(&handle.store, peak, oom_kills).await;
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    (engine, handle)
}

#[tokio::test]
async fn small_predicted_peaks_start_more_runners_than_the_caps_fit() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    // At 4G caps the 10G slice holds 2; 1G peaks + 15% reserve 1.25G each, so all 4 start.
    assert_eq!(registered(&github.calls()).len(), 4);
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 4 * 1280 * M);
    assert_eq!(
        handle.status.borrow().budget.admission,
        AdmissionPolicy::Measured
    );
}

#[tokio::test]
async fn nothing_starts_while_the_slice_is_at_the_high_water_mark() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    // 9.2G of 10G: past 90%, whatever the predictions leave.
    slice_at(&handle.store, 92 * G / 10);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 0);
    engine.publish();
    assert!(
        handle.status.borrow().budget.held,
        "the budget says admission is held"
    );
    slice_at(&handle.store, 3 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 4);
    engine.publish();
    assert!(!handle.status.borrow().budget.held, "admission resumed");
}

#[tokio::test]
async fn a_reseed_waits_while_the_slice_is_at_the_high_water_mark() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(10 * G);
    engine.recover().await.unwrap();
    engine.request_reseed("app").unwrap();
    // 9.2G of 10G, though nothing runnerd counts holds any of it: the 4G seed container would push it to the cap.
    slice_at(&handle.store, 92 * G / 10);
    engine.tick(Shutdown::No).await;
    assert!(engine.reseeds.is_empty(), "the seed waits");
    slice_at(&handle.store, 3 * G);
    engine.tick(Shutdown::No).await;
    assert!(engine.reseeds.contains_key("app"), "the seed starts");
}

#[tokio::test]
async fn without_a_recent_sample_of_the_slice_runners_are_admitted_at_their_caps() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    handle.store.host_sample(api::HostSample {
        at: time::now_ms() / 1000 - 60,
        cpu: 0,
        memory: 2 * G,
    });
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2, "2 × 4G fit in 10G");
}

#[tokio::test]
async fn peaks_sampled_with_page_cache_do_not_predict_what_a_job_reserves() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(10 * G);
    history(&handle.store, G, 0).await;
    // As a history from before version 8 holds them: those runners were sampled counting active page cache.
    rusqlite::Connection::open(dir.0.join("history.db"))
        .unwrap()
        .execute("UPDATE runners SET memory_measure = NULL", [])
        .unwrap();
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        2,
        "no run measured the new way: 2 × the 4G cap"
    );
}

#[tokio::test]
async fn a_kill_for_memory_from_before_the_upgrade_keeps_its_job_on_its_cap() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(10 * G);
    // An hour ago, before the upgrade, the last build was killed for memory.
    history(&handle.store, G, 1).await;
    rusqlite::Connection::open(dir.0.join("history.db"))
        .unwrap()
        .execute("UPDATE runners SET memory_measure = NULL", [])
        .unwrap();
    // Since then the profile's light lint jobs ran, sampled the new way: enough to predict a new job from.
    history_of(&handle.store, "lint", 2000, G, 0).await;
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        2,
        "builds reserve their 4G cap, not the lints' 1.25G"
    );
    // The start tasks write the runners' rows after the tick returns.
    let mut reserved: Vec<String> = Vec::new();
    for _ in 0..200 {
        handle.store.flush().await;
        reserved = rusqlite::Connection::open(dir.0.join("history.db"))
            .unwrap()
            .prepare("SELECT admission FROM runners WHERE name NOT LIKE 'earlier-%'")
            .unwrap()
            .query_map([], |row| row.get(0))
            .unwrap()
            .collect::<Result<_, _>>()
            .unwrap();
        if reserved.len() == 2 {
            break;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    // Until a week after the kill, as without the upgrade; the history's times are a few ms older than this.
    let expires =
        (time::now_ms() - 3600 * 1000 + 60_000 + crate::admission::UNPREDICTABLE_FOR) / 1000;
    assert_eq!(reserved.len(), 2);
    for admission in reserved {
        let admission: api::Admission = serde_json::from_str(&admission).unwrap();
        assert!(
            matches!(admission.basis, Basis::Unpredictable { until } if until.abs_diff(expires) <= 1),
            "{:?}",
            admission.basis
        );
    }
}

#[tokio::test]
async fn a_job_killed_for_memory_is_admitted_at_its_cap() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 1).await;
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        2,
        "its peaks say nothing for a week"
    );
}

#[tokio::test]
async fn a_jobs_record_says_how_its_runner_was_admitted() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    github.0.lock().unwrap().queued = vec![job(1)];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    let runner = registered(&github.calls())[0]
        .trim_start_matches("register ")
        .to_owned();
    handle.store.claimed(1, 1, &runner, time::now_ms());
    handle.store.flush().await;
    let admission = handle.store.job(1).await.unwrap().unwrap().admission;
    assert_eq!(
        admission,
        Some(api::Admission {
            policy: AdmissionPolicy::Measured,
            reserved: 1280 * M,
            basis: Basis::Job {
                runs: 10,
                peak: JobPeak::P95
            },
            free: Some(10 * G),
            slice: Some(2 * G),
            cpu: None,
        })
    );
}

/// A sample of each runner the engine registered, using `memory`.
fn runners_use(github: &FakeGithub, store: &Store, memory: u64) {
    runners_used(github, store, memory, time::now_ms());
}

/// A sample of each runner the engine registered, using `memory` at `at` (ms).
fn runners_used(github: &FakeGithub, store: &Store, memory: u64, at: u64) {
    let samples = registered(&github.calls())
        .iter()
        .map(|call| SampleRecord {
            runner: call.trim_start_matches("register ").to_owned(),
            at,
            cpu: 0,
            cpu_usage: 0,
            memory,
            network_received: 0,
            network_sent: 0,
            cpu_held: crate::observe::metrics::CpuHeld::default(),
            events: Events::default(),
        })
        .collect();
    store.samples(samples);
}

#[tokio::test]
async fn a_runner_using_more_than_it_reserved_counts_at_what_it_uses() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2);
    // Both reserved 1.25G and use 4G: the 10G slice has 2G left, under the 9G mark.
    runners_use(&github, &handle.store, 4 * G);
    slice_at(&handle.store, 8 * G);
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        3,
        "one more 1.25G runner fits in the 2G left, not two"
    );
    engine.publish();
    assert_eq!(
        handle.status.borrow().budget.memory_in_use,
        8 * G + 1280 * M
    );
}

#[tokio::test]
async fn back_under_caps_every_runner_counts_at_its_cap_whatever_it_reserved() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2, "2 × 1.25G reserved");
    // Back to caps with 3 × 3G, which fits the 10G slice; the two running containers keep their 4G caps.
    for runner in &mut github.0.lock().unwrap().runners {
        runner.busy = true;
    }
    let caps = CONFIG
        .replace("slots = 2", "slots = 3")
        .replace("memory = \"4G\"", "memory = \"3G\"");
    engine.reload(Config::parse(&caps).unwrap(), Some(10 * G), None);
    github.0.lock().unwrap().queued = (1..=3).map(job).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        2,
        "8G of caps run: a 3G runner does not fit"
    );
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 8 * G);
}

#[tokio::test]
async fn once_its_job_is_known_a_runner_reserves_that_jobs_prediction() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    // Started for a build, the runner gets job 9, an e2e another runner's build left behind.
    handle.store.queued(
        "o/r",
        &QueuedJob {
            name: "e2e".to_owned(),
            ..job(9)
        },
        time::now_ms(),
    );
    docker
        .0
        .lock()
        .unwrap()
        .captures
        .insert(("1".to_owned(), "0".to_owned()), "@@job 9 1\n".to_owned());
    github.0.lock().unwrap().queued = vec![job(1)];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 1);
    until_state(&handle, 9, JobState::Running).await;
    github.0.lock().unwrap().queued = Vec::new();
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    engine.publish();
    // 3G + 15% is 3.45G, rounded up to 3.5G.
    assert_eq!(handle.status.borrow().budget.memory_in_use, 3584 * M);
    handle.store.flush().await;
    let admission = handle.store.job(9).await.unwrap().unwrap().admission;
    assert_eq!(
        admission.map(|admission| (admission.reserved, admission.basis)),
        Some((
            3584 * M,
            Basis::Job {
                runs: 10,
                peak: JobPeak::P95
            }
        ))
    );
}

#[tokio::test]
async fn a_runner_that_takes_a_job_no_listing_saw_reserves_that_jobs_prediction_once_github_names_it()
 {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, 3 * G, 0).await;
    history_of(&handle.store, "lint", 2000, G, 0).await;
    // Started for a build, the runner gets job 9, a lint no listing of queued jobs showed.
    docker
        .0
        .lock()
        .unwrap()
        .captures
        .insert(("1".to_owned(), "0".to_owned()), "@@job 9 1\n".to_owned());
    github.0.lock().unwrap().queued = vec![job(1)];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 1);
    until_state(&handle, 9, JobState::Running).await;
    github.0.lock().unwrap().queued = Vec::new();
    // GitHub does not answer for the job at first.
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    let mut lint = details(9, "in_progress", None);
    lint.name = "lint".to_owned();
    lint.workflow_name = None;
    github.0.lock().unwrap().jobs.insert(9, lint);
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    engine.publish();
    // 1G + 15%, rounded up, not the 3.5G of a build.
    assert_eq!(handle.status.borrow().budget.memory_in_use, 1280 * M);
}

#[tokio::test]
async fn an_idle_runner_reserves_the_largest_prediction_of_the_jobs_queued_since() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    github.0.lock().unwrap().queued = vec![job(1)];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 1, "1.25G for the build");
    // An e2e queues beside the build; GitHub may give either to the waiting runner.
    github.0.lock().unwrap().queued = vec![
        job(1),
        QueuedJob {
            name: "e2e".to_owned(),
            ..job(2)
        },
    ];
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2);
    engine.publish();
    assert_eq!(
        handle.status.borrow().budget.memory_in_use,
        2 * 3584 * M,
        "both runners reserve the e2e's 3.5G"
    );
}

#[tokio::test]
async fn waiting_runners_raised_past_the_budget_for_a_heavier_job_are_stopped_until_the_rest_fits()
{
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        4,
        "4 × 1.25G for the builds"
    );
    // An e2e queues: any of the four may get it, so each reserves its 3.5G, 14G of the 10G slice.
    github.0.lock().unwrap().queued.push(QueuedJob {
        name: "e2e".to_owned(),
        ..job(5)
    });
    engine.tick(Shutdown::No).await;
    let deleted: BTreeSet<String> = github
        .calls()
        .into_iter()
        .filter(|call| call.starts_with("delete "))
        .collect();
    assert_eq!(deleted.len(), 2, "two stop, so the other two's 7G fits");
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 2 * 3584 * M);
    let starting = handle
        .status
        .borrow()
        .queued
        .iter()
        .filter(|job| job.waiting == Some(api::Waiting::RunnerStarting))
        .count();
    assert_eq!(
        starting, 2,
        "only the two runners left say they start a job"
    );
}

#[tokio::test]
async fn running_runners_count_at_their_caps_once_the_slice_has_no_recent_sample() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    runners_use(&github, &handle.store, G);
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 2 * 1280 * M);
    // The sampler stalled a minute ago: what the two use now is not known.
    handle.store.host_sample(api::HostSample {
        at: time::now_ms() / 1000 - 60,
        cpu: 0,
        memory: 2 * G,
    });
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 2 * 4 * G);
}

#[tokio::test]
async fn a_runner_whose_own_sample_is_old_counts_at_its_cap() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    github.0.lock().unwrap().queued = vec![job(1)];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    // Its container's files could not be read for a minute; the slice is sampled now.
    runners_used(&github, &handle.store, G, time::now_ms() - 60_000);
    slice_at(&handle.store, 2 * G);
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 4 * G);
}

/// A burst of jobs ending left 20 runners counted at 88.8G with 7G in use on 2026-11-05: each stayed in its slot for
/// its log and its removal, past the sample that would have said it holds nothing, and counted at its cap.
#[tokio::test]
async fn a_runner_whose_container_is_gone_counts_nothing_while_it_keeps_its_slot() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    github.0.lock().unwrap().queued = vec![job(1)];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    runners_used(&github, &handle.store, G, time::now_ms() - 60_000);
    slice_at(&handle.store, 2 * G);
    let runner = engine
        .occupied()
        .map(|(_, occupant)| occupant.runner_name.clone())
        .next()
        .unwrap();
    engine.publish();
    let counted = handle.status.borrow().budget.memory_in_use;
    handle.store.containers_gone(vec![runner.clone()]);
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 0);
    // A `docker restart` brings the same cgroup back: a live container's memory must not be spent twice.
    handle.store.containers_read(&[runner]);
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, counted);
}

#[tokio::test]
async fn two_light_runs_then_a_burst_of_heavy_copies_with_warm_up_never_overlap_past_memory_max() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(10 * G);
    // The profile's p95 is 3G; the build's two runs hit a warm cache and peaked at 200M.
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    runs_of(
        &handle.store,
        "build",
        1000,
        2,
        200 * M,
        0,
        Ended::Succeeded,
    )
    .await;
    engine.recover().await.unwrap();
    // The runners of slots 1 and 2 take builds 1 and 2.
    docker.0.lock().unwrap().slot_captures =
        HashMap::from([(1, "@@job 1 1\n".to_owned()), (2, "@@job 2 1\n".to_owned())]);
    // The cache is gone: four builds queue at once, each needing 3G once warmed up.
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    slice_at(&handle.store, G);
    engine.tick(Shutdown::No).await;
    // Copies of a job queued together reserve 3G + 15%, rounded up to 3.5G: two fit the 10G slice, not four at
    // 200M + 15%, rounded up to 256M.
    assert_eq!(registered(&github.calls()).len(), 2);
    // Their captures report their jobs.
    until_state(&handle, 1, JobState::Running).await;
    until_state(&handle, 2, JobState::Running).await;
    github.0.lock().unwrap().queued = Vec::new();
    slice_at(&handle.store, G);
    engine.tick(Shutdown::No).await;
    // Still warming up at 300M, both busy with known jobs. The first copy reserves its own 256M, the second 3.5G,
    // and the copies still queued 3.5G: one more fits, not two.
    {
        let mut state = github.0.lock().unwrap();
        for runner in &mut state.runners {
            (runner.status, runner.busy) = ("online".to_owned(), true);
        }
        state.queued = (3..=4).map(job).collect();
    }
    runners_use(&github, &handle.store, 300 * M);
    slice_at(&handle.store, G);
    engine.tick(Shutdown::No).await;
    let started = registered(&github.calls()).len() as u64;
    assert_eq!(started, 3);
    engine.publish();
    assert_eq!(
        handle.status.borrow().budget.memory_in_use,
        300 * M + 2 * 3584 * M
    );
    // Warmed up, the three copies' 3G peaks fit the 10G slice together; four would not.
    assert!(
        started * 3 * G <= 10 * G,
        "{started} copies at 3G fit the 10G slice"
    );
}

#[tokio::test]
async fn copies_arriving_one_by_one_beside_busy_runners_whose_job_is_unknown_take_their_profiles_p95()
 {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(10 * G);
    // The profile's p95 is 3G; the build's two runs hit a warm cache and peaked at 200M.
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    runs_of(
        &handle.store,
        "build",
        1000,
        2,
        200 * M,
        0,
        Ended::Succeeded,
    )
    .await;
    engine.recover().await.unwrap();
    // No log capture tells which job a runner took.
    docker.0.lock().unwrap().fail_execs = u32::MAX;
    let mut started = Vec::new();
    for copy in 1..=4 {
        // The runners before took theirs, which a poll saw; they warm up at 300M, sampled just now. A runner first
        // listed busy may have taken a job the same poll still lists queued, so the copy queues after.
        {
            let mut state = github.0.lock().unwrap();
            for runner in &mut state.runners {
                (runner.status, runner.busy) = ("online".to_owned(), true);
            }
            state.queued.clear();
        }
        engine.tick(Shutdown::No).await;
        github.0.lock().unwrap().queued = vec![job(copy)];
        runners_use(&github, &handle.store, 300 * M);
        slice_at(&handle.store, G);
        engine.tick(Shutdown::No).await;
        started.push(registered(&github.calls()).len());
    }
    // The first reserves 256M, alone. The second may be a copy of the busy runner's build, so it reserves
    // 3G + 15%, rounded up to 3.5G, and so does the first once a build queues beside it: the 10G slice holds no
    // third, where 256M each would let all four start.
    assert_eq!(started, [1, 2, 2, 2]);
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 2 * 3584 * M);
}

#[tokio::test]
async fn jobs_wait_for_cpu_the_budget_lacks_and_their_records_say_what_they_reserved_and_waited() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let config = measured().replace(
        "admission = \"measured\"",
        "admission = \"measured\"\ncpu_overcommit = 1.0",
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
    engine.memory_max = Some(10 * G);
    engine.cpus = Some(4000);
    history(&handle.store, G, 0).await;
    // Each build used 1.5 CPUs over its run.
    rusqlite::Connection::open(dir.0.join("history.db"))
        .unwrap()
        .execute("UPDATE jobs SET cpu_average = 1500", [])
        .unwrap();
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    slice_at(&handle.store, 2 * G);

    engine.tick(Shutdown::No).await;
    // Memory fits all 4 at 1.25G each; 4 CPUs fit 2 builds at 1.5.
    let first = registered(&github.calls())
        .iter()
        .map(|call| call.trim_start_matches("register ").to_owned())
        .collect::<Vec<_>>();
    assert_eq!(first.len(), 2);
    engine.publish();
    let cpu = handle.status.borrow().budget.cpu;
    assert_eq!(
        cpu,
        Some(api::CpuBudget {
            cpus: 4000,
            budget: 4000,
            in_use: 3000,
            held: true,
        })
    );

    handle.store.claimed(1, 1, &first[0], time::now_ms());
    handle.store.flush().await;
    let admission = handle.store.job(1).await.unwrap().unwrap().admission;
    assert_eq!(
        admission.and_then(|admission| admission.cpu),
        Some(api::CpuAdmission {
            reserved: 1500,
            basis: Basis::Job {
                runs: 10,
                peak: JobPeak::P95,
            },
            free: 4000,
            waited: None,
        })
    );

    // The slice gets more CPUs: the other two start, and say they waited for CPU.
    engine.cpus = Some(8000);
    engine.tick(Shutdown::No).await;
    let all = registered(&github.calls())
        .iter()
        .map(|call| call.trim_start_matches("register ").to_owned())
        .collect::<Vec<_>>();
    assert_eq!(all.len(), 4);
    handle.store.claimed(3, 1, &all[2], time::now_ms());
    handle.store.flush().await;
    let admission = handle.store.job(3).await.unwrap().unwrap().admission;
    assert!(
        admission
            .and_then(|admission| admission.cpu)
            .is_some_and(|cpu| cpu.waited.is_some()),
        "the third build's record says it waited for CPU: {admission:?}"
    );
    engine.publish();
    assert!(
        handle
            .status
            .borrow()
            .budget
            .cpu
            .is_some_and(|cpu| !cpu.held),
        "nothing waits for CPU any more"
    );
}

#[tokio::test]
async fn a_job_with_only_runs_from_before_the_upgrade_reserves_its_profiles_largest_peak_with_its_cpu_recorded()
 {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    // No cpu_overcommit: admission is by memory alone.
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(10 * G);
    history(&handle.store, G, 0).await;
    // `legacy` ran only before the upgrade: its peaks counted page cache, its CPU was recorded as ever.
    history_of(&handle.store, "legacy", 2000, 3 * G, 0).await;
    let db = rusqlite::Connection::open(dir.0.join("history.db")).unwrap();
    db.execute(
        "UPDATE runners SET memory_measure = NULL WHERE name LIKE 'earlier-2%'",
        [],
    )
    .unwrap();
    db.execute("UPDATE jobs SET cpu_average = 1500", [])
        .unwrap();
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = vec![QueuedJob {
        name: "legacy".to_owned(),
        ..job(1)
    }];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    let runner = registered(&github.calls())[0]
        .trim_start_matches("register ")
        .to_owned();
    handle.store.claimed(1, 1, &runner, time::now_ms());
    handle.store.flush().await;
    let admission = handle
        .store
        .job(1)
        .await
        .unwrap()
        .unwrap()
        .admission
        .unwrap();
    // The builds' 1G largest peak + 15%, as a job never seen: not its 4G cap.
    assert_eq!(
        (admission.reserved, admission.basis, admission.cpu),
        (1280 * M, Basis::ProfileMax { runs: 10 }, None)
    );
}

/// `measured()` with 6 slots and a CPU budget of the slice's CPUs.
fn cpu_measured() -> String {
    measured().replace("slots = 4", "slots = 6").replace(
        "admission = \"measured\"",
        "admission = \"measured\"\ncpu_overcommit = 1.0",
    )
}

#[tokio::test]
async fn after_a_reload_to_caps_running_runners_count_at_their_cpu_limits() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &cpu_measured());
    engine.cpus = Some(4000);
    history(&handle.store, G, 0).await;
    // Each build used half a CPU over its run.
    rusqlite::Connection::open(dir.0.join("history.db"))
        .unwrap()
        .execute("UPDATE jobs SET cpu_average = 500", [])
        .unwrap();
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 4, "4 × 0.5 CPU fit 4");

    let caps = cpu_measured().replace("admission = \"measured\"\n", "");
    engine.reload(Config::parse(&caps).unwrap(), None, Some(4000));
    engine.publish();
    // Each container's limit is the profile's 2 CPUs: 8 of the 4 the budget has.
    assert_eq!(
        handle.status.borrow().budget.cpu.map(|cpu| cpu.in_use),
        Some(8000)
    );
    github.0.lock().unwrap().queued = (1..=6).map(job).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        4,
        "no 2-CPU runner fits a budget the running ones fill"
    );
}

#[tokio::test]
async fn a_reload_that_sets_cpu_overcommit_admits_by_the_slices_cpus_it_read() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    // Started without a CPU budget, so the slice's CPUs were never read.
    let (mut engine, handle) = engine_with(&github, &docker, &dir, CONFIG);
    engine.recover().await.unwrap();
    engine.publish();
    assert_eq!(handle.status.borrow().budget.cpu, None);
    let with_budget = CONFIG.replace(
        "isolation = \"sysbox\"",
        "isolation = \"sysbox\"\ncpu_overcommit = 1.0",
    );
    engine.reload(Config::parse(&with_budget).unwrap(), None, Some(2000));
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    engine.tick(Shutdown::No).await;
    // Under caps each runner reserves the profile's 2 CPUs: one fits.
    assert_eq!(registered(&github.calls()).len(), 1);
    engine.publish();
    assert_eq!(
        handle.status.borrow().budget.cpu,
        Some(api::CpuBudget {
            cpus: 2000,
            budget: 2000,
            in_use: 2000,
            held: true,
        })
    );
}

/// How many runners of `repository` hold a slot.
fn runners_of(
    engine: &mut Engine<FakeGithub, FakeDocker>,
    handle: &Handle,
    repository: &str,
) -> usize {
    engine.publish();
    handle
        .status
        .borrow()
        .profiles
        .iter()
        .flat_map(|profile| &profile.slots)
        .filter(|slot| {
            slot.runner
                .as_ref()
                .is_some_and(|runner| runner.repository == repository)
        })
        .count()
}

/// A queued `lint` job of o/b.
fn lint(id: u64) -> QueuedJob {
    QueuedJob {
        name: "lint".to_owned(),
        labels: vec!["self-hosted".to_owned(), "light".to_owned()],
        ..job(id)
    }
}

#[tokio::test]
async fn a_heavy_job_keeps_its_place_for_cpu_through_high_water_ticks_and_starts_once_the_cpu_drains()
 {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    // o/r's build uses 4 CPUs over its run, o/b's lint 1, on a slice of 4 CPUs.
    let config = format!(
        "{}\n[[repositories]]\nname = \"o/b\"\nprofile = \"app\"\nlabels = [\"self-hosted\", \"light\"]\nwarm = 0\n",
        cpu_measured()
            .replace("cpus = 2", "cpus = 4")
            .replace("slots = 6", "slots = 8")
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
    engine.memory_max = Some(10 * G);
    engine.cpus = Some(4000);
    history(&handle.store, G, 0).await;
    history_of(&handle.store, "lint", 2000, G, 0).await;
    let db = rusqlite::Connection::open(dir.0.join("history.db")).unwrap();
    for statement in [
        "UPDATE jobs SET cpu_average = 4000 WHERE name = 'build'",
        "UPDATE jobs SET cpu_average = 1000, repository = 'o/b' WHERE name = 'lint'",
        "UPDATE runners SET repository = 'o/b' WHERE name LIKE 'earlier-2%'",
    ] {
        db.execute(statement, []).unwrap();
    }
    engine.recover().await.unwrap();
    slice_at(&handle.store, 2 * G);

    // Three lints start and take their jobs: 3 of the 4 CPUs.
    github.0.lock().unwrap().queued = (10..13).map(lint).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(runners_of(&mut engine, &handle, "o/b"), 3);
    {
        let mut state = github.0.lock().unwrap();
        state.queued.clear();
        for runner in &mut state.runners {
            runner.busy = true;
        }
    }
    // The build queues; it needs all 4 CPUs.
    github.0.lock().unwrap().queued = vec![job(20)];
    engine.tick(Shutdown::No).await;
    engine.publish();
    let budget = handle.status.borrow().budget.clone();
    assert!(
        budget.cpu.is_some_and(|cpu| cpu.held),
        "the build waits for CPU: {budget:?}"
    );
    // As if it had waited past the limit already.
    engine
        .repositories
        .get_mut("o/r")
        .and_then(|state| state.waits.get_mut("app"))
        .and_then(|waits| waits.cpu.as_mut())
        .expect("the build's wait is kept")
        .since -= PRIORITY_AFTER;

    // Every other tick the slice is at its high-water mark, and a lint queues every tick; one lint runner ends every
    // second round, so CPU keeps freeing up.
    let mut next = 30;
    for round in 0..6 {
        slice_at(&handle.store, 95 * G / 10);
        github.0.lock().unwrap().queued.push(lint(next));
        next += 1;
        engine.tick(Shutdown::No).await;
        slice_at(&handle.store, 2 * G);
        github.0.lock().unwrap().queued.push(lint(next));
        next += 1;
        if round % 2 == 1 {
            let mut state = docker.0.lock().unwrap();
            if let Some(at) = state.containers.iter().position(|container| {
                container
                    .labels
                    .get("runnerd.repository")
                    .map(String::as_str)
                    == Some("o/b")
            }) {
                state.containers.remove(at);
            }
        }
        engine.tick(Shutdown::No).await;
        if runners_of(&mut engine, &handle, "o/r") == 1 {
            break;
        }
        assert!(
            runners_of(&mut engine, &handle, "o/b") <= 3,
            "no younger lint takes the CPU the build waits for (round {round})"
        );
    }
    assert_eq!(
        runners_of(&mut engine, &handle, "o/r"),
        1,
        "the build started once the lints it waited for drained"
    );
    assert_eq!(runners_of(&mut engine, &handle, "o/b"), 0);
    assert_eq!(
        engine.repositories["o/r"].waits["app"],
        crate::scheduler::Waits {
            served: true,
            ..Default::default()
        },
        "started, the build no longer waits, and the barrier served its pool"
    );
}

#[tokio::test]
async fn a_job_held_for_cpu_that_leaves_the_queue_no_longer_waits() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let config = CONFIG.replace(
        "isolation = \"sysbox\"",
        "isolation = \"sysbox\"\ncpu_overcommit = 1.0",
    );
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &config);
    // Room for one runner at the profile's 2 CPUs.
    engine.cpus = Some(2000);
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(
        engine.repositories["o/r"]
            .waits
            .get("app")
            .and_then(|waits| waits.cpu)
            .map(|wait| wait.job),
        Some(2)
    );
    // Its run is cancelled: GitHub lists it no more.
    github.0.lock().unwrap().queued = vec![job(1)];
    engine.tick(Shutdown::No).await;
    assert!(
        engine.repositories["o/r"].waits.is_empty(),
        "a job gone from the queue no longer waits"
    );
}

/// The host's memory read now: 64G with `available` of it, the slice's runners holding `slice` of it without page
/// cache, and its tasks stalled `stalled` hundredths of a percent of the last 10 s; a Gradle daemon and an emulator
/// hold the most of it outside the slice.
fn host_at(store: &Store, slice: u64, available: u64, stalled: u32) {
    store.host_memory(api::HostMemory {
        at: time::now_ms() / 1000,
        total: 64 * G,
        available,
        slice,
        some: Some(api::Pressure::default()),
        full: Some(api::Pressure {
            avg10: stalled,
            avg60: 0,
        }),
        holders: vec![
            api::Holder {
                name: "java".to_owned(),
                memory: 9 * G,
            },
            api::Holder {
                name: "qemu-system-x86".to_owned(),
                memory: 6 * G,
            },
        ],
        ceiling: None,
        swap: None,
    });
}

/// How long each of the jobs 1 to 4 waited, in ms, by the history's `column`: `host_waited` or `slot_waited`.
fn waited(dir: &TestDir, column: &str) -> Vec<u64> {
    let db = rusqlite::Connection::open(dir.0.join("history.db")).unwrap();
    let mut statement = db
        .prepare(&format!(
            "SELECT {column} FROM jobs WHERE id <= 4 ORDER BY id"
        ))
        .unwrap();
    statement
        .query_map([], |row| row.get(0))
        .unwrap()
        .collect::<Result<_, _>>()
        .unwrap()
}

/// What each queued job waits for, oldest first.
fn waits(handle: &Handle) -> Vec<Option<api::Waiting>> {
    handle
        .status
        .borrow()
        .queued
        .iter()
        .map(|job| job.waiting.clone())
        .collect()
}

#[tokio::test]
async fn jobs_wait_while_the_host_is_short_of_memory_and_start_once_it_has_room_again() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    slice_at(&handle.store, 0);
    // A 64G host keeps 4G free; past that it has room for two runners of 1.25G, which the slice would hold four of.
    host_at(&handle.store, 0, 4 * G + 2 * 1280 * M + 100 * M, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2);
    engine.publish();
    // Once short with runners running, the host keeps half the margin more free: nothing is to spare until 6G.
    let short = Some(api::Waiting::HostMemory {
        needs: 1280 * M,
        free: 0,
    });
    assert_eq!(
        waits(&handle)[2..],
        [short.clone(), short],
        "the two left wait for the host's memory"
    );
    // Insights are told the same two jobs, with the reading that says who holds the memory.
    handle.store.flush().await;
    let hold = handle.store.insights(0).await.unwrap().host.unwrap();
    assert_eq!(hold.jobs.len(), 2, "the two left wait: {hold:?}");
    assert_eq!(hold.memory.holders[0].name, "java");
    let reason = {
        let status = handle.status.borrow();
        let job = &status.queued[2];
        job.waiting.as_ref().unwrap().reason(job, &status, 0)
    };
    assert!(
        reason.starts_with("The host is short of memory"),
        "{reason}"
    );
    // The runners hold nothing yet, so all that is not available is held by the host's other programs, the
    // largest of which it names.
    let outside = crate::config::ByteSize(64 * G - (4 * G + 2 * 1280 * M + 100 * M));
    assert!(
        reason.ends_with(&format!(
            "programs outside CI hold {outside}, most java 9G and qemu-system-x86 6G"
        )),
        "{reason}"
    );

    // Room for one more by the 4G margin, none by the larger one: this plan holds it, and as it fits the plain margin
    // the next plan has that (ADR 0102).
    tokio::time::sleep(Duration::from_millis(20)).await;
    host_at(&handle.store, 0, 4 * G + 3 * 1280 * M + 100 * M, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2, "the larger margin");
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        3,
        "one fits the plain one"
    );
    // The last fits neither, so the larger margin stands again, until the host has room for it.
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 3, "no room is left");
    assert!(
        engine.measured.host.short,
        "no waiter fits the plain margin"
    );
    tokio::time::sleep(Duration::from_millis(20)).await;
    host_at(&handle.store, 0, 6 * G + 4 * 1280 * M + 100 * M, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 4);
    handle.store.flush().await;
    assert_eq!(
        handle.store.insights(0).await.unwrap().host,
        None,
        "once they start, no job waits for the host's memory"
    );
    let waited = waited(&dir, "host_waited");
    assert_eq!(waited[..2], [0, 0], "the jobs that started waited not");
    assert!(
        waited[2] >= 20 && waited[3] >= 40,
        "each that waited counts the ticks it waited: {waited:?}"
    );
    assert!(removed(&docker.calls()).is_empty(), "no runner was stopped");
}

#[tokio::test]
async fn jobs_count_a_wait_for_a_slot_only_while_memory_had_room_for_their_runners() {
    // Catches auto sizing adding slots for jobs memory would have held back anyway (ADR 0090). Two slots, four jobs
    // of 1.25G, and a slice with room for one more runner beside the two that start: job 3 waits for a slot alone,
    // job 4 for memory too.
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(
        &github,
        &docker,
        &dir,
        &measured().replace("slots = 4", "slots = 2"),
    );
    engine.memory_max = Some(3 * 1280 * M + 100 * M);
    history(&handle.store, G, 0).await;
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    slice_at(&handle.store, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2);
    tokio::time::sleep(Duration::from_millis(20)).await;
    engine.tick(Shutdown::No).await;
    handle.store.flush().await;
    let waited = waited(&dir, "slot_waited");
    assert_eq!(
        waited.iter().map(|&ms| ms >= 20).collect::<Vec<_>>(),
        [false, false, true, false],
        "only job 3 waited for a slot alone: {waited:?}"
    );
}

#[tokio::test]
async fn the_first_runner_into_an_empty_slice_needs_only_the_margin_after_a_wait() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    slice_at(&handle.store, 0);
    host_at(&handle.store, 0, 4 * G + 1000 * M, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 0, "1.25G does not fit");
    // The jobs waited, but nothing runs that a start would follow: the 4G margin alone holds.
    host_at(&handle.store, 0, 4 * G + 1280 * M + 100 * M, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 1);
}

#[tokio::test]
async fn a_runner_using_more_than_it_reserved_takes_what_it_uses_from_the_host() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    github.0.lock().unwrap().queued = vec![job(1)];
    slice_at(&handle.store, 0);
    host_at(&handle.store, 0, 40 * G, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 1);
    // It reserved 1.25G and holds 3G, which the host no longer has available: what is left beside it is
    // MemAvailable less the 4G margin, room for one more 1.25G runner, not two.
    runners_use(&github, &handle.store, 3 * G);
    slice_at(&handle.store, 3 * G);
    host_at(&handle.store, 3 * G, 4 * G + 1280 * M + 100 * M, 0);
    github.0.lock().unwrap().queued = (1..=3).map(job).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2);
}

#[tokio::test]
async fn under_caps_a_runner_starts_only_if_its_cap_fits_what_the_host_leaves() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, CONFIG);
    engine.memory_max = Some(20 * G);
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    // The slice fits both 4G caps; the host past its 4G margin fits one.
    host_at(&handle.store, 0, 8 * G + 100 * M, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 1);
    engine.publish();
    assert_eq!(
        waits(&handle)[1],
        Some(api::Waiting::HostMemory {
            needs: 4 * G,
            free: 0
        })
    );
}

/// A VM's memory read now, as Hyper-V leaves it while idle: 2.12G of which 1.48G is available, the slice holding
/// nothing, its tasks stalled `stalled` hundredths of a percent of the last 10 s.
fn idle_vm_at(store: &Store, stalled: u32) {
    store.host_memory(api::HostMemory {
        at: time::now_ms() / 1000,
        total: 2 * G + 120 * M,
        available: G + 490 * M,
        slice: 0,
        some: Some(api::Pressure::default()),
        full: Some(api::Pressure {
            avg10: stalled,
            avg60: 0,
        }),
        holders: Vec::new(),
        ceiling: None,
        swap: None,
    });
}

/// An engine under caps with a 20G slice, its config's `[host]` extended by `host`, two jobs queued.
async fn vm_engine(
    github: &FakeGithub,
    docker: &FakeDocker,
    dir: &TestDir,
    host: &str,
) -> (Engine<FakeGithub, FakeDocker>, Handle) {
    let config = CONFIG.replace(
        "isolation = \"sysbox\"",
        &format!("isolation = \"sysbox\"\n{host}"),
    );
    let (mut engine, handle) = engine_with(github, docker, dir, &config);
    engine.memory_max = Some(20 * G);
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    (engine, handle)
}

#[tokio::test]
async fn an_idle_vm_without_a_memory_ceiling_starts_nothing() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = vm_engine(&github, &docker, &dir, "").await;
    idle_vm_at(&handle.store, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        0,
        "1.48G is under the 2G margin"
    );
    engine.publish();
    assert_eq!(
        waits(&handle)[0],
        Some(api::Waiting::HostMemory {
            needs: 4 * G,
            free: 0
        })
    );
}

#[tokio::test]
async fn a_vm_that_can_grow_to_its_memory_ceiling_starts_runners_in_what_it_can_grow_by() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = vm_engine(&github, &docker, &dir, "memory_ceiling = \"24G\"").await;
    idle_vm_at(&handle.store, 0);
    engine.tick(Shutdown::No).await;
    // 1.48G available and 21.88G to grow by, less the 2G margin of 24G, fits both 4G caps.
    assert_eq!(registered(&github.calls()).len(), 2);
    engine.publish();
    let line = handle.status.borrow().budget.host_line().unwrap();
    assert!(
        line.starts_with("host: 1.48G of 2.12G available, growing to 24G; leaves the slice 21.36G"),
        "{line}"
    );
}

#[tokio::test]
async fn a_vm_that_stalls_short_of_memory_starts_nothing_whatever_its_memory_ceiling() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = vm_engine(&github, &docker, &dir, "memory_ceiling = \"24G\"").await;
    // The hypervisor does not give what the ceiling promises: every runnable task waits for memory 6% of the time.
    idle_vm_at(&handle.store, 600);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 0, "nothing starts");
    engine.publish();
    let status = handle.status.borrow();
    assert!(
        status.budget.host.as_ref().is_some_and(|host| host.stalled),
        "the budget says the host stalls"
    );
    let job = &status.queued[0];
    let reason = job.waiting.as_ref().unwrap().reason(job, &status, 0);
    assert!(reason.contains("stall"), "{reason}");
}

#[tokio::test]
async fn a_runner_whose_room_the_host_lost_since_the_plan_does_not_start_in_the_same_tick() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, CONFIG);
    engine.memory_max = Some(20 * G);
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=2).map(job).collect();
    // The plan has room for both 4G caps; while the first runner starts, other programs take the host's memory
    // down to what it alone fits in.
    host_at(&handle.store, 0, 20 * G, 0);
    let store = handle.store.clone();
    docker.0.lock().unwrap().on_start = Some(Box::new(move |_| {
        host_at(&store, 0, 8 * G + 100 * M, 0);
    }));
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        1,
        "the second start waits for room"
    );
    engine.publish();
    assert_eq!(
        waits(&handle)[1],
        Some(api::Waiting::HostMemory {
            needs: 4 * G,
            free: 0
        })
    );
}

#[tokio::test]
async fn nothing_starts_while_the_host_stalls_short_of_memory() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    slice_at(&handle.store, 0);
    // 7.5G available is under twice the 4G margin, and every runnable task waited for memory 6% of the last 10 s:
    // nothing starts, though even the larger margin leaves room for one. The runners hold 10G of the rest.
    host_at(&handle.store, 10 * G, 7 * G + 512 * M, 600);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 0, "nothing starts");
    engine.publish();
    {
        let status = handle.status.borrow();
        assert!(
            status.budget.host.as_ref().is_some_and(|host| host.stalled),
            "the budget says the host stalls"
        );
        let job = status.queued.last().unwrap();
        let reason = job.waiting.as_ref().unwrap().reason(job, &status, 0);
        assert!(reason.contains("stall"), "{reason}");
        // 64G less the 7.5G available and the runners' 10G: the runners' memory is not the other programs'.
        let outside = crate::config::ByteSize(64 * G - 7 * G - 512 * M - 10 * G);
        assert!(
            reason.ends_with(&format!(
                "programs outside CI hold {outside}, most java 9G and qemu-system-x86 6G"
            )),
            "{reason}"
        );
    }
    // 3%: under where starts stop, over where they resume.
    host_at(&handle.store, 0, 7 * G + 512 * M, 300);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 0, "still held at 3%");
    host_at(&handle.store, 0, 7 * G + 512 * M, 50);
    engine.tick(Shutdown::No).await;
    // 3.5G past the margin: two 1.25G runners.
    assert_eq!(registered(&github.calls()).len(), 2, "resumed under 1%");
}

#[tokio::test]
async fn a_stall_with_memory_to_spare_holds_no_start_and_stops_no_runner() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    slice_at(&handle.store, 0);
    github.0.lock().unwrap().queued = vec![job(1)];
    host_at(&handle.store, 0, 40 * G, 0);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 1);
    // 6% with 40G available: runners reclaiming at their own caps, not a host short of memory.
    github.0.lock().unwrap().queued = (1..=3).map(job).collect();
    host_at(&handle.store, 0, 40 * G, 600);
    engine.tick(Shutdown::No).await;
    assert_eq!(
        registered(&github.calls()).len(),
        3,
        "the stall holds nothing"
    );
    engine.publish();
    assert!(
        handle
            .status
            .borrow()
            .budget
            .host
            .as_ref()
            .is_some_and(|host| !host.stalled),
        "the budget says the host does not stall"
    );
    assert!(removed(&docker.calls()).is_empty(), "no runner was stopped");
}

#[tokio::test]
async fn without_a_recent_reading_of_the_host_only_the_slice_bounds_starts() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    slice_at(&handle.store, 0);
    handle.store.host_memory(api::HostMemory {
        at: time::now_ms() / 1000 - 60,
        total: 64 * G,
        available: 0,
        slice: 0,
        some: None,
        full: None,
        holders: Vec::new(),
        ceiling: None,
        swap: None,
    });
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 4);
}

#[tokio::test]
async fn runners_a_restart_adopts_keep_the_cpu_they_reserved_not_their_limits() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &cpu_measured());
    engine.cpus = Some(4000);
    history(&handle.store, G, 0).await;
    // Each build used half a CPU over its run.
    rusqlite::Connection::open(dir.0.join("history.db"))
        .unwrap()
        .execute("UPDATE jobs SET cpu_average = 500", [])
        .unwrap();
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = (1..=4).map(job).collect();
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 4, "4 × 0.5 CPU fit 4");
    handle.store.flush().await;
    drop((engine, handle));

    // The daemon restarts while the four run: each still reserves half a CPU, not its 2-CPU limit.
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &cpu_measured());
    engine.cpus = Some(4000);
    engine.recover().await.unwrap();
    engine.publish();
    assert_eq!(
        handle.status.borrow().budget.cpu.map(|cpu| cpu.in_use),
        Some(2000)
    );
}

#[tokio::test]
async fn a_start_that_no_longer_fits_does_not_hold_back_the_plans_later_starts() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let config = sized("0G", "4G", "512M").replacen("slots = 1", "slots = 2", 1);
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
    engine.memory_max = Some(20 * G);
    engine.recover().await.unwrap();
    {
        let mut state = github.0.lock().unwrap();
        state.queued = vec![job(1), job(2), small_job(3)];
        state
            .queued_on
            .extend([(1, "o/r".to_owned()), (2, "o/r".to_owned())]);
        state.queued_on.insert(3, "o/s".to_owned());
    }
    // The plan has room for all three; while the first runner starts, other programs take the host's memory down to
    // what it and a 512M runner fit in, not a second 4G one.
    host_at(&handle.store, 0, 20 * G, 0);
    let store = handle.store.clone();
    docker.0.lock().unwrap().on_start = Some(Box::new(move |_| {
        host_at(&store, 0, 9 * G + 100 * M, 0);
    }));
    engine.tick(Shutdown::No).await;
    // Skipping every start after the one that no longer fit left o/s's job to the next poll, though it fit.
    let registered: Vec<String> = registered(&github.calls()).into_iter().cloned().collect();
    assert_eq!(registered.len(), 2, "{registered:?}");
    assert!(
        registered[1].contains("-small-"),
        "o/s's runner starts in the same tick: {registered:?}"
    );
    engine.publish();
    assert_eq!(
        waits(&handle)[1],
        Some(api::Waiting::HostMemory {
            needs: 4 * G,
            free: 0
        })
    );
}

#[tokio::test]
async fn a_stop_asked_for_while_a_runner_starts_starts_no_more_of_the_tick() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let config = sized("0G", "4G", "512M").replacen("slots = 1", "slots = 2", 1);
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
    engine.memory_max = Some(20 * G);
    engine.recover().await.unwrap();
    {
        let mut state = github.0.lock().unwrap();
        state.queued = vec![job(1), job(2), small_job(3)];
        state
            .queued_on
            .extend([(1, "o/r".to_owned()), (2, "o/r".to_owned())]);
        state.queued_on.insert(3, "o/s".to_owned());
    }
    host_at(&handle.store, 0, 20 * G, 0);
    // While the first runner starts, the host's memory goes down, which defers the second, and a stop is asked for.
    let (store, control) = (handle.store.clone(), handle.control.clone());
    docker.0.lock().unwrap().on_start = Some(Box::new(move |_| {
        host_at(&store, 0, 9 * G + 100 * M, 0);
        control.shutdown(Shutdown::Detach);
    }));
    engine.tick(Shutdown::No).await;
    // A runner started after the stop could take a job the detach then leaves running.
    assert_eq!(registered(&github.calls()).len(), 1, "{:?}", github.calls());
    let started = docker
        .calls()
        .iter()
        .filter(|call| call.starts_with("start "))
        .count();
    assert_eq!(started, 1, "{:?}", docker.calls());
}

/// A pass that runs while a tick follows its jobs plans after the tick took a new listing but before it raised
/// what waiting runners reserve for it: it must raise them first. The bug it catches: a pass admitting runners
/// beside a waiting runner still counted at its old, smaller reservation, past the slice once it is raised.
#[tokio::test]
async fn a_pass_raises_what_waiting_runners_reserve_before_it_admits() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    github.0.lock().unwrap().queued = vec![job(1), job(2)];
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2, "1.25G for each build");
    // One runner ran build 2, which ended; the tick that follows has listed two e2e jobs beside build 1 and not yet
    // raised the waiting runner's reservation for them when Docker says the other's container stopped.
    let ended = {
        let occupant = engine
            .slots
            .values_mut()
            .flatten()
            .flatten()
            .last()
            .unwrap();
        occupant.job = Some(2);
        occupant.busy = true;
        occupant.container.clone()
    };
    for container in &mut docker.0.lock().unwrap().containers {
        if container.id == ended {
            container.running = false;
        }
    }
    let e2e = |id| QueuedJob {
        name: "e2e".to_owned(),
        ..job(id)
    };
    engine.repositories.get_mut("o/r").unwrap().queued = vec![job(1), e2e(5), e2e(6)];
    engine.exits.stopped(ended);
    engine.turnover().await;

    engine.rereserve().await;
    engine.publish();
    let in_use = handle.status.borrow().budget.memory_in_use;
    assert!(
        in_use <= 10 * G,
        "the runners reserve {in_use} bytes of the 10G slice"
    );
}

/// Four builds run at 1.25G each. One ends while the tick that listed an e2e beside the other three has not raised
/// their reservations yet: the pass that releases it raises them to 3.5G each, 10.5G of the 10G slice, and stops
/// none, as passes stop no runner. The bug it catches: the next tick finding nothing left to raise and forgetting
/// that the three were raised, so none stopped and they stayed past the budget.
#[tokio::test]
async fn waiting_runners_a_pass_raised_past_the_budget_are_stopped_by_the_next_tick() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = measured_engine(&github, &docker, &dir, G, 0).await;
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    slice_at(&handle.store, 2 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 4, "4 × 1.25G");
    let ended = {
        let occupant = engine
            .slots
            .values_mut()
            .flatten()
            .flatten()
            .last()
            .unwrap();
        occupant.job = Some(4);
        occupant.busy = true;
        occupant.container.clone()
    };
    for container in &mut docker.0.lock().unwrap().containers {
        if container.id == ended {
            container.running = false;
        }
    }
    let e2e = QueuedJob {
        name: "e2e".to_owned(),
        ..job(5)
    };
    let queued = vec![job(1), job(2), job(3), e2e];
    engine.repositories.get_mut("o/r").unwrap().queued = queued.clone();
    github.0.lock().unwrap().queued = queued;
    engine.exits.stopped(ended);
    engine.turnover().await;
    engine.publish();
    assert_eq!(
        handle.status.borrow().budget.memory_in_use,
        3 * 3584 * M,
        "the pass raised the three left"
    );

    engine.tick(Shutdown::No).await;
    engine.publish();
    let in_use = handle.status.borrow().budget.memory_in_use;
    assert!(
        in_use <= 10 * G,
        "the runners reserve {in_use} bytes of the 10G slice"
    );
}

/// `measured()` with the pooled count off.
fn unpooled() -> String {
    measured().replace(
        "admission = \"measured\"",
        "admission = \"measured\"\npooled_count = false",
    )
}

/// A recent sample of every runner using `used`, and of a slice and a host with room to spare.
fn roomy_readings(github: &FakeGithub, store: &Store, used: u64) {
    runners_use(github, store, used);
    slice_at(store, G);
    host_at(store, G, 40 * G, 0);
}

/// Every running job's first minute went by.
fn first_minute_passes(engine: &mut Engine<FakeGithub, FakeDocker>) {
    for occupant in engine.slots.values_mut().flatten().flatten() {
        occupant.job_started = occupant.job_started.map(|at| at - POOL_AFTER);
    }
}

/// An 8G slice running `build` and `e2e`, each predicted at 3.5G and using 512M, 1G left of the plain count: their
/// runners' room above use is 3G each, which comes to 1.76G of credit.
pub(super) async fn two_settled_jobs(
    github: &FakeGithub,
    docker: &FakeDocker,
    dir: &TestDir,
    config: &str,
) -> (Engine<FakeGithub, FakeDocker>, Handle) {
    let (mut engine, handle) = engine_with(github, docker, dir, config);
    engine.memory_max = Some(8 * G);
    history_of(&handle.store, "build", 1000, 3 * G, 0).await;
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    history_of(&handle.store, "lint", 3000, G, 0).await;
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = vec![
        job(1),
        QueuedJob {
            name: "e2e".to_owned(),
            ..job(2)
        },
    ];
    for slot in 1..=2 {
        let capture = format!("@@job {slot} {slot}\n");
        docker.0.lock().unwrap().slot_captures.insert(slot, capture);
    }
    roomy_readings(github, &handle.store, 512 * M);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2);
    until_state(&handle, 1, JobState::Running).await;
    until_state(&handle, 2, JobState::Running).await;
    github.0.lock().unwrap().queued = Vec::new();
    roomy_readings(github, &handle.store, 512 * M);
    engine.tick(Shutdown::No).await;
    first_minute_passes(&mut engine);
    roomy_readings(github, &handle.store, 512 * M);
    (engine, handle)
}

/// A queued job of o/r predicted at 1.25G.
fn small(id: u64) -> QueuedJob {
    QueuedJob {
        name: "lint".to_owned(),
        ..job(id)
    }
}

/// A 16G host with `available` of it, the slice's runners holding 1G: it keeps a 2G margin, so at 8G it leaves the
/// slice 7G and a credit can stand.
fn small_host_at(store: &Store, available: u64) {
    store.host_memory(api::HostMemory {
        at: time::now_ms() / 1000,
        total: 16 * G,
        available,
        slice: G,
        some: Some(api::Pressure::default()),
        full: Some(api::Pressure::default()),
        holders: Vec::new(),
        ceiling: None,
        swap: None,
    });
}

/// What the only queued job's wait says, as every client shows it.
fn only_reason(handle: &Handle) -> String {
    let status = handle.status.borrow();
    let job = &status.queued[0];
    job.waiting.as_ref().unwrap().reason(job, &status, 0)
}

/// The kinds of insight the history gives now.
async fn host_held_insights(engine: &Engine<FakeGithub, FakeDocker>, handle: &Handle) -> usize {
    handle.store.flush().await;
    let history = handle.store.insights(0).await.unwrap();
    crate::insights::insights(&history, &engine.config, 7, time::now_ms())
        .insights
        .iter()
        .filter(|insight| matches!(insight.kind, api::InsightKind::HostHeld { .. }))
        .count()
}

#[tokio::test]
async fn a_job_the_host_limit_holds_with_room_free_asks_nobody_to_close_programs() {
    // Catches the owner being told to close programs while the host has the room, because what runs reserves more
    // than it holds and the credit opened the slice to one more job than the host limit lets in.
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    github.0.lock().unwrap().queued = vec![small(3)];
    runners_use(&github, &handle.store, 512 * M);
    slice_at(&handle.store, G);
    small_host_at(&handle.store, 8 * G);
    engine.tick(Shutdown::No).await;
    tokio::time::sleep(Duration::from_millis(20)).await;
    runners_use(&github, &handle.store, 512 * M);
    slice_at(&handle.store, G);
    small_host_at(&handle.store, 8 * G);
    engine.tick(Shutdown::No).await;
    engine.publish();

    assert!(
        handle.status.borrow().budget.pooled.is_some(),
        "a credit opens the slice"
    );
    assert_eq!(
        waits(&handle),
        [Some(api::Waiting::HostMemory {
            needs: 1280 * M,
            free: 0
        })],
        "the host limit reads the plain count, so it holds the job"
    );
    assert_eq!(
        only_reason(&handle),
        "The host has 8G of 16G available, but what runs may still grow into it: needs 1.25G; 0B of the 6G it leaves \
         the slice is free"
    );
    // Sizing takes this wait off the one it reads as a wait for the budget, so it has to be counted.
    handle.store.flush().await;
    assert!(
        waited(&dir, "host_waited")[2] >= 20,
        "the job waited for the host's limit"
    );
    handle.store.host_waited(vec![3], 60 * 60_000);
    assert_eq!(
        host_held_insights(&engine, &handle).await,
        0,
        "6G of room past the margin, and the job needs 1.25G"
    );

    // Programs take the room: the same job now waits for a host that lacks it. That takes the credit back, and a job
    // short of both budgets reads the slice's, so the slice gets room of its own.
    engine.memory_max = Some(9 * G);
    runners_use(&github, &handle.store, 512 * M);
    slice_at(&handle.store, G);
    small_host_at(&handle.store, 3 * G);
    engine.tick(Shutdown::No).await;
    engine.publish();
    assert!(
        only_reason(&handle).starts_with("The host is short of memory: needs 1.25G"),
        "{}",
        only_reason(&handle)
    );
    assert_eq!(
        host_held_insights(&engine, &handle).await,
        1,
        "1G past the margin, and the job needs 1.25G"
    );

    // The job would fit, but only by taking what the host is about to run out of: closing programs is what starts it.
    runners_use(&github, &handle.store, 512 * M);
    slice_at(&handle.store, G);
    small_host_at(&handle.store, 3 * G + 512 * M);
    engine.tick(Shutdown::No).await;
    engine.publish();
    assert!(
        only_reason(&handle).starts_with("The host is short of memory: needs 1.25G"),
        "{}",
        only_reason(&handle)
    );
    assert_eq!(
        host_held_insights(&engine, &handle).await,
        1,
        "1.5G past the margin, under one more margin of 2G"
    );

    // Room comes back: the next plan takes the job off what the owner is told.
    runners_use(&github, &handle.store, 512 * M);
    slice_at(&handle.store, G);
    small_host_at(&handle.store, 8 * G);
    engine.tick(Shutdown::No).await;
    assert_eq!(host_held_insights(&engine, &handle).await, 0);
}

#[tokio::test]
async fn settled_jobs_of_two_kinds_make_room_a_start_goes_by_and_every_reader_agrees() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    engine.publish();
    let budget = handle.status.borrow().budget.clone();
    let pooled = budget.pooled.expect("two settled jobs, room above use");
    assert_eq!(budget.memory_in_use, 7 * G, "the plain count stays a fact");
    assert_eq!(pooled.counted, 7 * G - pooled.credit);
    assert_eq!(pooled.runners, 2);
    // 6G less the root of 2 × 3G².
    assert_eq!(pooled.credit / M, 1799);
    let remembered = engine.host_remembered();
    assert_eq!(engine.count(remembered).pooled(), pooled.counted);
    let plan = engine.plan_budget(&engine.admitting(&mut [], None));
    assert_eq!(
        plan.memory,
        Some(crate::scheduler::left(8 * G, pooled.counted)),
        "the plan starts from what the status shows"
    );
    assert_eq!(
        budget.host.map(|host| host.spare),
        Some(G + 40 * G - 4 * G - budget.memory_in_use),
        "the host's limit reads the plain count: the overcount is its margin"
    );
    assert_eq!(
        plan.host,
        Some(crate::scheduler::left(
            G + 40 * G - 4 * G,
            budget.memory_in_use
        )),
        "and so does a plan's"
    );

    // 1G is left of the plain count: lint's 1.25G fits only in the pooled one.
    github.0.lock().unwrap().queued = vec![small(3)];
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 3, "lint starts");
    engine.publish();
    let budget = handle.status.borrow().budget.clone();
    assert_eq!(budget.memory_in_use, 8 * G + 256 * M, "past 8G, in full");
    assert_eq!(
        budget.pooled.map(|after| after.credit),
        Some(pooled.credit),
        "a runner just started changes no credit"
    );

    // The next tick does not stop the runner the last one started.
    roomy_readings(&github, &handle.store, 512 * M);
    engine.tick(Shutdown::No).await;
    let stopped = github
        .calls()
        .iter()
        .filter(|call| call.starts_with("delete "))
        .count();
    assert_eq!(stopped, 0, "what fits the pooled count stays");
}

#[tokio::test]
async fn a_raised_waiting_runner_stops_only_once_the_pooled_count_is_past_the_budget() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    github.0.lock().unwrap().queued = vec![small(3)];
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 3, "lint's runner waits");
    github.0.lock().unwrap().queued = Vec::new();
    let waiting = engine
        .occupied()
        .find(|(_, occupant)| occupant.job.is_none())
        .map(|(_, occupant)| occupant.runner_name.clone())
        .unwrap();
    engine.measured.raised.push(waiting);

    // 8.25G plain of the 8G budget: past it, but the count is not.
    roomy_readings(&github, &handle.store, 512 * M);
    assert_eq!(engine.over_budget(&[]), [], "the pooled count fits");

    // The host's reading goes old, the credit with it: the same runner is now past the budget.
    handle.store.host_memory(api::HostMemory {
        at: time::now_ms() / 1000 - 60,
        total: 64 * G,
        available: 40 * G,
        slice: G,
        some: None,
        full: None,
        holders: Vec::new(),
        ceiling: None,
        swap: None,
    });
    assert_eq!(engine.over_budget(&[]).len(), 1, "the plain count does not");
}

#[tokio::test]
async fn the_last_check_before_a_start_keeps_the_margin_its_plan_chose() {
    // The runners a plan starts into an empty slice need only the first runner's margin, and the credit's host guard
    // goes by that margin: read afresh, the larger one takes the credit back from a start the plan allowed.
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    // 21G available: 1G past the 20G kept by the margin, under the 22G kept by the larger one.
    host_at(&handle.store, G, 21 * G, 0);
    engine.measured.host.short = true;
    let lint = 1280 * M;
    let mut plan = engine.admitting(&mut [], None);
    plan.remembered.short = false;
    assert!(
        engine.still_fits(&plan, "app", lint, 0),
        "1G left and 1G of credit, by the plan's margin"
    );
    plan.remembered.short = true;
    assert!(
        !engine.still_fits(&plan, "app", lint, 0),
        "no credit by the larger margin"
    );
}

#[tokio::test]
async fn a_reseed_reads_the_room_a_plan_reads() {
    // A plan fills what the pooled count leaves; a reseed reading the plain count would wait on room plans keep taking.
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let second_profile = |config: String| {
        config.replace(
            "[[repositories]]",
            "[profiles.spare]\nimage_stage = \"spare\"\ncpus = 2\nmemory = \"2G\"\nslots = 1\n\n\
             [[repositories]]\nname = \"o/s\"\nprofile = \"spare\"\nlabels = [\"spare\"]\nwarm = 0\n\n\
             [[repositories]]",
        )
    };
    let (engine, _) = two_settled_jobs(&github, &docker, &dir, &second_profile(measured())).await;
    assert!(
        engine.fits_seed("spare"),
        "2G of the 2.76G the count leaves"
    );

    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (engine, _) = two_settled_jobs(&github, &docker, &dir, &second_profile(unpooled())).await;
    assert!(!engine.fits_seed("spare"), "1G left of the plain count");
}

#[tokio::test]
async fn with_the_pooled_count_off_the_same_jobs_count_and_plan_as_they_add_up() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &unpooled()).await;
    engine.publish();
    let budget = handle.status.borrow().budget.clone();
    assert_eq!(budget.pooled, None);
    assert_eq!(budget.memory_in_use, 7 * G);
    let plan = engine.plan_budget(&engine.admitting(&mut [], None));
    assert_eq!(plan.memory, Some(crate::scheduler::left(8 * G, 7 * G)));
    assert_eq!(plan.credit_room, 0, "nothing for a barrier to expect");
    github.0.lock().unwrap().queued = vec![small(3)];
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2, "lint's 1.25G waits");
}

#[tokio::test]
async fn caps_admission_pools_nothing() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    engine.config.host.admission = crate::config::Admission::Caps;
    engine.publish();
    assert_eq!(handle.status.borrow().budget.pooled, None);
    assert_eq!(handle.status.borrow().budget.memory_in_use, 8 * G);
}

#[tokio::test]
async fn any_stale_reading_takes_the_credit_back() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    let old = time::now_ms() - 60_000;
    let credit = |engine: &mut Engine<FakeGithub, FakeDocker>| {
        engine.publish();
        let budget = handle.status.borrow().budget.clone();
        budget.pooled.map(|pooled| pooled.credit)
    };
    assert!(credit(&mut engine).is_some(), "the readings are fresh");

    // The host's reading stops: the sampler reads /proc separately from the slice.
    handle.store.host_memory(api::HostMemory {
        at: old / 1000,
        total: 64 * G,
        available: 40 * G,
        slice: G,
        some: Some(api::Pressure::default()),
        full: Some(api::Pressure::default()),
        holders: Vec::new(),
        ceiling: None,
        swap: None,
    });
    assert_eq!(credit(&mut engine), None, "without a recent host reading");

    // The slice's sample goes old: every runner counts at its cap too.
    host_at(&handle.store, G, 40 * G, 0);
    handle.store.host_sample(api::HostSample {
        at: old / 1000,
        cpu: 0,
        memory: G,
    });
    assert_eq!(credit(&mut engine), None, "without a recent slice sample");
    assert_eq!(handle.status.borrow().budget.memory_in_use, 8 * G);

    // A runner's own sample goes old: it counts at its cap, and the other alone pools with nothing.
    slice_at(&handle.store, G);
    let stale = engine
        .occupied()
        .map(|(_, occupant)| occupant.runner_name.clone())
        .next()
        .unwrap();
    runners_use(&github, &handle.store, 512 * M);
    handle.store.samples(vec![SampleRecord {
        runner: stale.clone(),
        at: old,
        cpu: 0,
        cpu_usage: 0,
        memory: 512 * M,
        network_received: 0,
        network_sent: 0,
        cpu_held: crate::observe::metrics::CpuHeld::default(),
        events: Events::default(),
    }]);
    assert_eq!(credit(&mut engine), None, "its one job left to pool");
}

#[tokio::test]
async fn a_runner_a_restart_adopts_counts_in_full_for_the_rest_of_its_job() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    handle.store.flush().await;
    drop((engine, handle));

    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(8 * G);
    engine.recover().await.unwrap();
    roomy_readings(&github, &handle.store, 512 * M);
    engine.tick(Shutdown::No).await;
    first_minute_passes(&mut engine);
    roomy_readings(&github, &handle.store, 512 * M);
    engine.publish();
    let budget = handle.status.borrow().budget.clone();
    assert_eq!(budget.memory_in_use, 7 * G, "both keep what they reserved");
    assert_eq!(budget.pooled, None, "this daemon did not see them start");
}

#[tokio::test]
async fn a_waiting_runner_a_restart_adopts_counts_in_full_once_it_takes_a_job() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(8 * G);
    history_of(&handle.store, "build", 1000, 3 * G, 0).await;
    history_of(&handle.store, "e2e", 2000, 3 * G, 0).await;
    engine.recover().await.unwrap();
    github.0.lock().unwrap().queued = vec![
        job(1),
        QueuedJob {
            name: "e2e".to_owned(),
            ..job(2)
        },
    ];
    roomy_readings(&github, &handle.store, 512 * M);
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2);
    handle.store.flush().await;
    // The last capture started may not have run yet, and would read the job as this daemon's.
    until_captures_end(&engine).await;
    drop((engine, handle));

    // Each takes its job while no daemon runs.
    for slot in 1..=2 {
        let capture = format!("@@job {slot} {slot}\n");
        docker.0.lock().unwrap().slot_captures.insert(slot, capture);
    }
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &measured());
    engine.memory_max = Some(8 * G);
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    until_state(&handle, 1, JobState::Running).await;
    until_state(&handle, 2, JobState::Running).await;
    roomy_readings(&github, &handle.store, 512 * M);
    engine.tick(Shutdown::No).await;
    first_minute_passes(&mut engine);
    roomy_readings(&github, &handle.store, 512 * M);
    let found = engine
        .occupied()
        .filter(|(_, occupant)| occupant.job_started.is_some() && occupant.key.is_some())
        .count();
    assert_eq!(found, 2, "this daemon found both jobs");
    engine.publish();
    assert_eq!(
        handle.status.borrow().budget.pooled,
        None,
        "their runners are not known to have started as predicted"
    );
}

#[tokio::test]
async fn a_host_that_stalls_or_runs_short_takes_the_credit_back_and_holds_the_start() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("engine"),
    );
    let (mut engine, handle) = two_settled_jobs(&github, &docker, &dir, &measured()).await;
    // Under the margin and a quarter of the host: 20G.
    host_at(&handle.store, G, 19 * G, 0);
    engine.publish();
    assert_eq!(handle.status.borrow().budget.pooled, None);
    github.0.lock().unwrap().queued = vec![small(3)];
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()).len(), 2, "lint waits again");
}