runnerd-core 0.1.0

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
//! Records of failed jobs on the fakes: which runners are kept, the limits that remove the oldest, removal by hand,
//! and shells, which count against the budget like a slot.

use std::collections::HashMap;
use std::time::Duration;

use super::*;
use crate::docker::TtySize;
use crate::record;
use crate::scheduler::engine::RecordSelection;
use crate::store::RunnerRecord;

pub(super) const FAILED: &str = "2026-10-23 01:08:55Z: Running job: build\n\
                      2026-10-23 01:09:27Z: Job build completed with result: Failed\n";

pub(super) fn recording() -> String {
    CONFIG.replace("slots = 2", "slots = 2\nrecord_failures = true")
}

const SIZE: TtySize = TtySize { cols: 80, rows: 24 };

/// Job 1 runs in slot 1 and then fails: its runner prints `log` and exits.
async fn job_ends(
    github: &FakeGithub,
    docker: &FakeDocker,
    engine: &mut Engine<FakeGithub, FakeDocker>,
    handle: &Handle,
    log: &str,
) {
    running_runner(github, docker, "@@job 1 1\n");
    github
        .0
        .lock()
        .unwrap()
        .jobs
        .insert(1, details(1, "completed", Some("failure")));
    engine.recover().await.unwrap();
    until_state(handle, 1, JobState::Running).await;
    let mut state = docker.0.lock().unwrap();
    state.runner_log = Some(log.to_owned());
    state.written = 1 << 30;
    state.containers[0].running = false;
}

/// Ticks until the engine took the finished commits.
pub(super) async fn until_recorded(engine: &mut Engine<FakeGithub, FakeDocker>) {
    for _ in 0..200 {
        engine.tick(Shutdown::No).await;
        if engine.no_recordings() {
            engine.publish();
            return;
        }
        tokio::time::sleep(Duration::from_millis(5)).await;
    }
    panic!("a commit never finished");
}

/// A record image an earlier job left, made `age` seconds ago.
pub(super) fn record_image(job: u64, age: u64) -> docker::Image {
    let created = crate::time::now_ms() / 1000 - age;
    docker::Image {
        reference: record::image(job),
        labels: HashMap::from([
            (record::LABEL.to_owned(), job.to_string()),
            ("runnerd.repository".to_owned(), "o/r".to_owned()),
            ("runnerd.profile".to_owned(), "app".to_owned()),
            ("runnerd.created-at".to_owned(), created.to_string()),
        ]),
        bytes: 1 << 30,
    }
}

pub(super) async fn note(handle: &Handle, job: u64) -> Option<api::RecordNote> {
    handle.store.flush().await;
    handle.store.job(job).await.unwrap().unwrap().record
}

#[tokio::test]
async fn a_failed_job_leaves_a_record_and_its_container_goes_once_committed() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    let mut events = handle.store.subscribe();
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    until_recorded(&mut engine).await;

    let calls = docker.calls();
    let commit = calls
        .iter()
        .position(|call| call == "commit container-1 runnerd-record:1");
    let remove = calls.iter().position(|call| call == "remove container-1");
    assert!(
        commit.is_some() && commit < remove,
        "the container is committed, then removed: {calls:?}"
    );
    let records = handle.records.borrow().clone();
    assert_eq!(records.records.len(), 1);
    let kept = &records.records[0];
    assert_eq!(
        (kept.job, kept.repository.as_str(), kept.bytes),
        (1, "o/r", 1 << 30)
    );
    assert_eq!(kept.workflow.as_deref(), Some("ci"));
    let note = note(&handle, 1).await.unwrap();
    assert_eq!(note.bytes, Some(1 << 30));
    assert!(
        note.created_at.is_some(),
        "the history keeps that it was made"
    );
    let mut created = false;
    while let Ok(event) = events.try_recv() {
        created |= matches!(&event.event, api::Event::RecordCreated { record } if record.job == 1);
    }
    assert!(created, "record.created is announced");
}

#[tokio::test]
async fn an_idle_runner_whose_job_fails_while_it_is_deregistered_is_recorded_not_removed() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record");
    idle_runner(&github, &docker);
    github.0.lock().unwrap().strict = true;
    {
        let mut state = docker.0.lock().unwrap();
        state.captures_wait = Some(github.clone());
        state.capture_end = CaptureEnd::Container;
    }
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    for occupant in engine.occupants_mut() {
        occupant.started_at -= crate::scheduler::IDLE_TIMEOUT;
    }
    // The tick saw it running and idle, so it removes it as expired; meanwhile it takes job 1, which fails, and its
    // runner exits, which ends its registration. Its capture reads the job and its last line a moment later.
    github.hold_deletes();
    let ends = async {
        github.until_waiting(1).await;
        {
            let mut state = github.0.lock().unwrap();
            state.runners.clear();
            state
                .jobs
                .insert(1, details(1, "completed", Some("failure")));
        }
        {
            let mut state = docker.0.lock().unwrap();
            state.runner_log = Some(FAILED.to_owned());
            state.written = 1 << 30;
            state.containers[0].running = false;
            state.container_captures.insert(
                "container-1".to_owned(),
                "@@job 1 1\n@@page 1 0 7\nfailed\n".to_owned(),
            );
        }
        github.release_deletes();
    };
    tokio::join!(engine.tick(Shutdown::No), ends);
    until_recorded(&mut engine).await;

    let calls = docker.calls();
    let commit = calls
        .iter()
        .position(|call| call == "commit container-1 runnerd-record:1");
    let remove = calls.iter().position(|call| call == "remove container-1");
    assert!(
        commit.is_some() && commit < remove,
        "the failed job's container is committed, then removed: {calls:?}"
    );
    let note = note(&handle, 1).await.unwrap();
    assert!(note.created_at.is_some(), "job 1's record was made");
    until_log_lines(&handle, 1, 1).await;
}

#[tokio::test]
async fn an_idle_runner_whose_capture_names_a_failed_job_while_github_still_deregisters_it_is_recorded()
 {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record");
    idle_runner(&github, &docker);
    github.0.lock().unwrap().strict = true;
    {
        let mut state = docker.0.lock().unwrap();
        state.captures_wait = Some(github.clone());
        state.capture_end = CaptureEnd::Container;
    }
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    for occupant in engine.occupants_mut() {
        occupant.started_at -= crate::scheduler::IDLE_TIMEOUT;
    }
    // While its delete waits, it takes job 1, which its capture reports, and the job fails; GitHub has not dropped
    // its registration yet, so the delete answers `Deleted`, not "not found".
    github.hold_deletes();
    let ends = async {
        github.until_waiting(1).await;
        docker.0.lock().unwrap().container_captures.insert(
            "container-1".to_owned(),
            "@@job 1 1\n@@page 1 0 7\nfailed\n".to_owned(),
        );
        until_state(&handle, 1, JobState::Running).await;
        github
            .0
            .lock()
            .unwrap()
            .jobs
            .insert(1, details(1, "completed", Some("failure")));
        {
            let mut state = docker.0.lock().unwrap();
            state.runner_log = Some(FAILED.to_owned());
            state.written = 1 << 30;
            state.containers[0].running = false;
        }
        github.release_deletes();
    };
    tokio::join!(engine.tick(Shutdown::No), ends);
    until_recorded(&mut engine).await;

    let calls = docker.calls();
    let commit = calls
        .iter()
        .position(|call| call == "commit container-1 runnerd-record:1");
    let remove = calls.iter().position(|call| call == "remove container-1");
    assert!(
        commit.is_some() && commit < remove,
        "the failed job's container is committed, then removed: {calls:?}"
    );
    until_log_lines(&handle, 1, 1).await;
}

#[tokio::test]
async fn a_signal_waits_for_the_record_being_made() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    engine.tick(Shutdown::No).await;
    assert!(!engine.no_recordings(), "job 1's record is being made");
    handle.control.shutdown(Shutdown::Detach);
    tokio::time::timeout(Duration::from_secs(10), engine.schedule())
        .await
        .unwrap();
    let note = note(&handle, 1).await.unwrap();
    assert!(note.created_at.is_some(), "the record was made: {note:?}");
}

#[tokio::test]
async fn only_failed_jobs_of_a_profile_that_records_them_are_kept() {
    let succeeded = FAILED.replace("Failed", "Succeeded");
    for (config, log) in [
        (recording(), succeeded.as_str()),
        (CONFIG.to_owned(), FAILED),
    ] {
        let (github, docker) = (FakeGithub::default(), FakeDocker::default());
        let dir = TestDir::new("no-record");
        let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
        job_ends(&github, &docker, &mut engine, &handle, log).await;
        until_recorded(&mut engine).await;
        let calls = docker.calls();
        assert!(
            !calls.iter().any(|call| call.starts_with("commit")),
            "nothing is committed: {calls:?}"
        );
        assert!(
            calls.contains(&"remove container-1".to_owned()),
            "{calls:?}"
        );
        assert_eq!(note(&handle, 1).await, None);
    }
}

#[tokio::test]
async fn a_runner_that_left_its_credentials_is_not_kept_and_the_history_says_why() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("secrets");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    docker.0.lock().unwrap().changed = vec![
        "/home/runner/_work/r/r/src/main.rs".to_owned(),
        "/home/runner/.credentials".to_owned(),
    ];
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    until_recorded(&mut engine).await;

    let calls = docker.calls();
    assert!(
        !calls.iter().any(|call| call.starts_with("commit")),
        "{calls:?}"
    );
    assert!(
        calls.contains(&"remove container-1".to_owned()),
        "{calls:?}"
    );
    let skipped = note(&handle, 1).await.unwrap().skipped.unwrap();
    assert!(skipped.contains("/home/runner/.credentials"), "{skipped}");
    assert!(
        skipped.contains(&format!(
            "the image of runnerd {}",
            env!("CARGO_PKG_VERSION")
        )),
        "says which image deletes them: {skipped}"
    );
}

#[tokio::test]
async fn a_failure_past_max_count_removes_the_oldest_record_and_the_history_says_why() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 300), record_image(11, 200)];
    let dir = TestDir::new("count");
    let config = format!("{}\n[records]\nmax_count = 2\n", recording());
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    until_recorded(&mut engine).await;

    let jobs: Vec<u64> = handle
        .records
        .borrow()
        .records
        .iter()
        .map(|record| record.job)
        .collect();
    assert_eq!(jobs, [11, 1]);
    assert!(
        docker
            .calls()
            .contains(&"remove image runnerd-record:10".to_owned())
    );
    handle.store.flush().await;
    let removals: Vec<api::Event> = handle
        .store
        .events_after(0, 100)
        .await
        .unwrap()
        .into_iter()
        .map(|event| event.event)
        .filter(|event| event.kind() == "record.deleted")
        .collect();
    assert_eq!(
        removals,
        [api::Event::RecordDeleted {
            job: 10,
            removed_by: api::RecordRemoval::Count
        }]
    );
}

#[tokio::test]
async fn a_failure_that_would_leave_docker_too_little_disk_is_not_kept() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().free = Some(11 << 30);
    let dir = TestDir::new("floor");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    until_recorded(&mut engine).await;

    assert!(!docker.calls().iter().any(|call| call.starts_with("commit")));
    let skipped = note(&handle, 1).await.unwrap().skipped.unwrap();
    assert!(skipped.contains("records.min_free"), "{skipped}");
}

#[tokio::test]
async fn records_past_max_age_and_those_removed_by_hand_go_unless_a_shell_is_open() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![
        record_image(10, 8 * 86_400),
        record_image(11, 3 * 86_400),
        record_image(12, 60),
    ];
    let dir = TestDir::new("trash");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    engine.publish();
    let jobs = |handle: &Handle| -> Vec<u64> {
        handle
            .records
            .borrow()
            .records
            .iter()
            .map(|record| record.job)
            .collect()
    };
    assert_eq!(jobs(&handle), [11, 12], "the record past 7 days went");
    // The idle tick trimmed the shared caches, which a shell waits for.
    while engine.trim.as_ref().is_some_and(|trim| !trim.is_finished()) {
        tokio::task::yield_now().await;
    }

    engine.open_shell(12, SIZE).await.unwrap();
    let refused = engine.remove_records(&RecordSelection::Job(12)).await;
    assert!(
        matches!(refused, Err(ControlError::Conflict(_))),
        "{refused:?}"
    );
    let removed = engine
        .remove_records(&RecordSelection::OlderThan(Duration::from_hours(48)))
        .await
        .unwrap();
    assert_eq!(removed.jobs, [11]);
    assert_eq!(removed.bytes, 1 << 30, "the client is told what it freed");
    let removed = engine.remove_records(&RecordSelection::All).await.unwrap();
    assert_eq!(
        removed.jobs,
        Vec::<u64>::new(),
        "the record with a shell open stays"
    );
    engine.publish();
    assert_eq!(jobs(&handle), [12]);

    handle.store.flush().await;
    let events: Vec<(u64, api::RecordRemoval)> = handle
        .store
        .events_after(0, 100)
        .await
        .unwrap()
        .into_iter()
        .filter_map(|event| match event.event {
            api::Event::RecordDeleted { job, removed_by } => Some((job, removed_by)),
            _ => None,
        })
        .collect();
    assert_eq!(
        events,
        [
            (10, api::RecordRemoval::Age),
            (11, api::RecordRemoval::User)
        ]
    );
}

#[tokio::test]
async fn records_docker_did_not_list_at_the_start_are_listed_by_a_tick_and_then_expire() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    {
        let mut state = docker.0.lock().unwrap();
        state.images = vec![record_image(10, 8 * 86_400), record_image(11, 60)];
        state.fail_images = 1;
    }
    let dir = TestDir::new("listed-late");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    let jobs = |handle: &Handle| -> Vec<u64> {
        let records = handle.records.borrow();
        records.records.iter().map(|record| record.job).collect()
    };
    engine.recover().await.unwrap();
    engine.publish();
    assert_eq!(jobs(&handle), Vec::<u64>::new(), "Docker did not answer");

    engine.tick(Shutdown::No).await;
    engine.publish();
    assert_eq!(jobs(&handle), [10, 11], "the next tick asked again");

    engine.tick(Shutdown::No).await;
    engine.publish();
    assert_eq!(jobs(&handle), [11], "the record past 7 days went");
}

#[tokio::test]
async fn idle_upkeep_waits_for_a_shell_open_with_no_runner() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60)];
    let dir = TestDir::new("shell-upkeep");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    while engine.trim.as_ref().is_some_and(|trim| !trim.is_finished()) {
        tokio::task::yield_now().await;
    }
    engine.tick(Shutdown::No).await;
    assert!(engine.trim.is_none(), "the first idle tick's trim ended");

    let tty = engine.open_shell(10, SIZE).await.unwrap();
    engine.make_trim_due();
    engine.tick(Shutdown::No).await;
    assert!(engine.trim.is_none(), "a shell mounts the shared caches");

    engine.close_shell(&tty.container).await;
    engine.tick(Shutdown::No).await;
    assert!(engine.trim.is_some(), "the trim was only held back");
}

#[tokio::test]
async fn a_shell_holds_a_slots_memory_and_is_refused_while_the_budget_is_full() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60), record_image(11, 60)];
    let dir = TestDir::new("shell");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    engine.memory_max = Some(6 << 30);
    engine.recover().await.unwrap();

    let tty = engine.open_shell(10, SIZE).await.unwrap();
    engine.publish();
    let budget = handle.status.borrow().budget.clone();
    assert_eq!(budget.memory_in_use, 4 << 30);
    assert_eq!(
        budget.shell_memory,
        [api::ShellMemory {
            job: 10,
            memory: 4 << 30
        }]
    );
    let body = docker.0.lock().unwrap().shells[&tty.container].clone();
    assert_eq!(body.image.as_deref(), Some("runnerd-record:10"));
    assert_eq!(body.cmd.unwrap()[1], "/home/runner/_work/r/r");
    let mounts: Vec<(String, Option<String>)> = body
        .host_config
        .unwrap()
        .mounts
        .unwrap()
        .into_iter()
        .map(|mount| (mount.target.unwrap(), mount.source))
        .collect();
    assert!(
        mounts.contains(&("/state".to_owned(), None)),
        "the shell's changes go to a volume of its own, not a slot's: {mounts:?}"
    );
    assert!(
        mounts.contains(&("/base".to_owned(), Some("runnerd-app-base".to_owned()))),
        "{mounts:?}"
    );

    let refused = engine.open_shell(11, SIZE).await.unwrap_err();
    assert!(
        refused.to_string().starts_with("the budget is full"),
        "{refused}"
    );
    // No runner starts in the memory the shell holds.
    github.0.lock().unwrap().queued = vec![job(5)];
    engine.tick(Shutdown::No).await;
    assert_eq!(registered(&github.calls()), Vec::<&String>::new());

    engine.close_shell(&tty.container).await;
    engine.publish();
    assert_eq!(handle.status.borrow().budget.memory_in_use, 0);
    assert!(
        docker
            .calls()
            .contains(&format!("remove {}", tty.container))
    );
}

#[tokio::test]
async fn a_shell_opens_in_room_the_status_shows_though_the_plain_count_has_none() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60)];
    let dir = TestDir::new("shell");
    let config = recording().replace("slots = 2", "slots = 4").replace(
        "isolation = \"sysbox\"",
        "isolation = \"sysbox\"\nadmission = \"measured\"",
    );
    let (mut engine, _) = admission::two_settled_jobs(&github, &docker, &dir, &config).await;
    // The 4G a shell takes beside 7G plain is past 10.5G; the status counts 5.24G of it.
    engine.memory_max = Some(10 * (1 << 30) + (1 << 29));
    assert!(
        engine.open_shell(10, SIZE).await.is_ok(),
        "the shell is told what the status shows"
    );
}

#[tokio::test]
async fn under_measured_admission_no_shell_opens_while_the_slice_is_at_the_high_water_mark() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60)];
    let dir = TestDir::new("shell");
    let config = recording().replace(
        "isolation = \"sysbox\"",
        "isolation = \"sysbox\"\nadmission = \"measured\"",
    );
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
    engine.memory_max = Some(10 << 30);
    engine.recover().await.unwrap();
    let slice_at = |memory| {
        handle.store.host_sample(api::HostSample {
            at: crate::time::now_ms() / 1000,
            cpu: 0,
            memory,
        });
    };

    // 9.2G of 10G: past 90%, though the 4G shell fits beside no runner.
    slice_at((92 << 30) / 10);
    let refused = engine.open_shell(10, SIZE).await.unwrap_err();
    assert!(refused.to_string().contains("90%"), "{refused}");
    slice_at(3 << 30);
    assert!(
        engine.open_shell(10, SIZE).await.is_ok(),
        "opens once under"
    );
}

#[tokio::test]
async fn a_shell_whose_process_exited_is_removed_and_one_left_by_an_earlier_run_is_not_taken_for_a_runner()
 {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60)];
    let dir = TestDir::new("shell-gone");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    let tty = engine.open_shell(10, SIZE).await.unwrap();
    docker
        .0
        .lock()
        .unwrap()
        .containers
        .iter_mut()
        .for_each(|container| container.running = false);
    engine.tick(Shutdown::No).await;
    engine.publish();
    assert!(engine.no_shells(), "the exited shell is forgotten");
    assert!(
        docker
            .calls()
            .contains(&format!("remove {}", tty.container))
    );
    drop((engine, handle));

    // A shell carries its record's runner labels, as a commit keeps the container's.
    let mut left = runner_container(1, 7, true);
    left.id = "runnerd-shell-10-1".to_owned();
    left.name = left.id.clone();
    left.labels
        .insert(record::SHELL_LABEL.to_owned(), "10".to_owned());
    docker.0.lock().unwrap().containers = vec![left];
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    engine.publish();
    assert_eq!(runner_in(&handle, 1), None);
    assert!(
        docker
            .calls()
            .contains(&"remove runnerd-shell-10-1".to_owned())
    );
}

#[tokio::test]
async fn a_shell_left_by_an_earlier_run_that_docker_fails_to_remove_is_removed_on_a_later_tick() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let mut left = runner_container(1, 7, true);
    left.id = "runnerd-shell-10-1".to_owned();
    left.name = left.id.clone();
    left.labels
        .insert(record::SHELL_LABEL.to_owned(), "10".to_owned());
    {
        let mut state = docker.0.lock().unwrap();
        state.containers = vec![left];
        state.fail_removes = 1;
    }
    let dir = TestDir::new("shell-left");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    assert_eq!(docker.0.lock().unwrap().containers.len(), 1);
    engine.tick(Shutdown::No).await;
    assert_eq!(docker.0.lock().unwrap().containers.len(), 0);
}

#[tokio::test]
async fn failures_committed_at_once_count_together_against_min_free() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    // Room for one record of 2G (twice the 1G its layer holds) above the 10G floor, not for two.
    docker.0.lock().unwrap().free = Some(13 << 30);
    let dir = TestDir::new("floor-together");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    {
        let mut state = docker.0.lock().unwrap();
        state.containers = vec![runner_container(1, 7, true), runner_container(2, 8, true)];
        for (container, job) in [("container-1", 1), ("container-2", 2)] {
            state
                .container_captures
                .insert(container.to_owned(), format!("@@job {job} 1\n"));
        }
    }
    {
        let mut state = github.0.lock().unwrap();
        state.runners = vec![
            runner(7, &identity("ci", ID_A).runner_name("app", 1, 1), true),
            runner(8, &identity("ci", ID_A).runner_name("app", 2, 1), true),
        ];
        for job in [1, 2] {
            state
                .jobs
                .insert(job, details(job, "completed", Some("failure")));
        }
    }
    engine.recover().await.unwrap();
    until_state(&handle, 2, JobState::Running).await;
    {
        let mut state = docker.0.lock().unwrap();
        state.runner_log = Some(FAILED.to_owned());
        state.written = 1 << 30;
        state
            .containers
            .iter_mut()
            .for_each(|container| container.running = false);
    }
    until_recorded(&mut engine).await;

    let commits = docker
        .calls()
        .iter()
        .filter(|call| call.starts_with("commit"))
        .count();
    assert_eq!(
        commits, 1,
        "the second failure finds the first's disk taken"
    );
    let skipped: Vec<String> = [note(&handle, 1).await, note(&handle, 2).await]
        .into_iter()
        .filter_map(|note| note?.skipped)
        .collect();
    assert_eq!(skipped.len(), 1, "{skipped:?}");
    assert!(skipped[0].contains("records.min_free"), "{skipped:?}");
}

#[tokio::test(start_paused = true)]
async fn shells_get_containers_of_their_own_read_only_caches_and_close_after_eight_hours() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60)];
    let dir = TestDir::new("shell-caches");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();

    let first = engine.open_shell(10, SIZE).await.unwrap();
    let second = engine.open_shell(10, SIZE).await.unwrap();
    assert_ne!(first.container, second.container);
    let body = docker.0.lock().unwrap().shells[&first.container].clone();
    for mount in body.host_config.unwrap().mounts.unwrap() {
        let target = mount.target.unwrap();
        assert_eq!(
            mount.read_only == Some(true),
            target != "/state",
            "{target}: only the shell's own /state is writable"
        );
    }
    let pnpm = engine.names.pnpm();
    let refused = engine.clean_cache(&pnpm).await.unwrap_err();
    assert!(
        refused.to_string().contains("a shell"),
        "a shared cache a shell mounts stays: {refused}"
    );
    let refused = engine.request_reseed("app").unwrap_err();
    assert!(
        matches!(refused, ControlError::Conflict(_)),
        "a reseed waits for the shell, and says so: {refused}"
    );

    tokio::time::advance(Duration::from_hours(8) + Duration::from_secs(1)).await;
    engine.tick(Shutdown::No).await;
    assert!(engine.no_shells(), "forgotten shells are closed");
    let calls = docker.calls();
    for tty in [&first, &second] {
        assert!(
            calls.contains(&format!("remove {}", tty.container)),
            "{calls:?}"
        );
    }
}

#[tokio::test]
async fn a_failed_jobs_runner_a_stopped_daemon_did_not_keep_becomes_its_record_at_the_next_start() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("crash");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    drop((engine, handle));

    // The earlier daemon stopped before it collected or committed job 1's runner, and after it committed job 9's.
    docker.0.lock().unwrap().images = vec![record_image(9, 60)];
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    let store = handle.store.clone();
    store.queued(
        "o/r",
        &QueuedJob {
            id: 9,
            run_id: 9,
            name: "build".to_owned(),
            ..QueuedJob::default()
        },
        crate::time::now_ms(),
    );
    store.flush().await;
    engine.recover().await.unwrap();
    until_recorded(&mut engine).await;

    let kept = note(&handle, 1).await.unwrap();
    assert!(
        kept.created_at.is_some(),
        "job 1's record is made: {kept:?}"
    );
    let made = note(&handle, 9).await.unwrap();
    assert_eq!(made.bytes, Some(1 << 30), "the record found is noted");
    let calls = docker.calls();
    let commit = calls
        .iter()
        .position(|call| call == "commit container-1 runnerd-record:1");
    let remove = calls.iter().position(|call| call == "remove container-1");
    assert!(
        commit.is_some() && commit < remove,
        "the stopped runner is committed, then goes: {calls:?}"
    );
}

#[tokio::test]
async fn no_shell_opens_while_its_profile_reseeds_the_snapshot_it_mounts() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60)];
    let dir = TestDir::new("shell-reseed");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();

    engine.seed_runs("app", 1 << 30);
    let refused = engine.open_shell(10, SIZE).await.unwrap_err();
    assert!(
        matches!(refused, ControlError::Conflict(_)),
        "a shell waits for the reseed: {refused}"
    );
    assert!(engine.no_shells(), "no shell is open");
}

#[tokio::test]
async fn no_shell_opens_while_the_shared_caches_it_mounts_are_trimmed() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    docker.0.lock().unwrap().images = vec![record_image(10, 60)];
    let dir = TestDir::new("shell-trim");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();

    engine.trim = Some(tokio::spawn(std::future::pending()));
    let refused = engine.open_shell(10, SIZE).await.unwrap_err();
    assert!(
        matches!(refused, ControlError::Conflict(_)),
        "a shell waits for the trim: {refused}"
    );
    assert!(engine.no_shells(), "no shell is open");
}

#[tokio::test(start_paused = true)]
async fn a_left_runner_docker_cannot_read_is_asked_about_less_and_less_and_never_removed() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("unread");
    {
        let mut state = docker.0.lock().unwrap();
        state.containers = vec![runner_container(2, 8, false)];
        state
            .container_logs
            .insert("container-2".to_owned(), FAILED.to_owned());
        state
            .diag
            .insert("container-2".to_owned(), docker::tests::diag_archive(5));
        // Docker never manages to read its diagnostic files.
        state.fail_files = u32::MAX;
    }
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    let poll = engine.config.host.poll_interval;
    let start = tokio::time::Instant::now();
    let mut reads = Vec::new();
    let mut read = |docker: &FakeDocker| {
        let count = docker
            .calls()
            .iter()
            .filter(|call| call.starts_with("files container-2 "))
            .count();
        if count > reads.len() {
            reads.push(start.elapsed());
        }
    };
    engine.recover().await.unwrap();
    read(&docker);
    // A tick each poll interval for an hour.
    while start.elapsed() < Duration::from_secs(3600) {
        tokio::time::advance(poll).await;
        engine.tick(Shutdown::No).await;
        read(&docker);
    }
    let gaps: Vec<u64> = reads
        .windows(2)
        .map(|pair| pair[1].saturating_sub(pair[0]).as_secs())
        .collect();
    assert_eq!(
        gaps[..5],
        [15, 30, 60, 120, 240],
        "it is read again a poll interval later, then twice as long each time: {reads:?}"
    );
    assert!(
        gaps[5..].iter().all(|gap| (480..=615).contains(gap)),
        "never more than 10 minutes apart: {gaps:?}"
    );
    assert!(
        !docker.calls().contains(&"remove container-2".to_owned()),
        "the runner is never removed"
    );
}

/// A stopped runner an earlier daemon left, of a failed job the worker log names, whose diagnostic files Docker cannot
/// read.
fn left_runner_docker_cannot_read(docker: &FakeDocker) {
    let mut state = docker.0.lock().unwrap();
    state.containers = vec![runner_container(2, 8, false)];
    state
        .container_logs
        .insert("container-2".to_owned(), FAILED.to_owned());
    state
        .diag
        .insert("container-2".to_owned(), docker::tests::diag_archive(5));
    state.fail_files = u32::MAX;
}

fn has_container(docker: &FakeDocker, id: &str) -> bool {
    let state = docker.0.lock().unwrap();
    state.containers.iter().any(|container| container.id == id)
}

fn removals_of(docker: &FakeDocker, container: &str) -> usize {
    let call = format!("remove {container}");
    docker.calls().iter().filter(|made| **made == call).count()
}

#[tokio::test(start_paused = true)]
async fn a_left_runner_read_at_last_whose_removal_fails_is_tried_again_within_that_tick() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    left_runner_docker_cannot_read(&docker);
    let dir = TestDir::new("unread-retried");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    assert_eq!(engine.state_counts().unread, 1, "it waits to be read");
    {
        // It turns out to have nothing to keep, and Docker refuses the first removal.
        let mut state = docker.0.lock().unwrap();
        state.fail_files = 0;
        state.container_logs.remove("container-2");
        state.fail_removes = 1;
    }
    engine.make_unread_due();
    engine.tick(Shutdown::No).await;
    assert_eq!(removals_of(&docker, "container-2"), 2, "asked twice");
    assert!(
        docker.0.lock().unwrap().containers.is_empty(),
        "the second removal took it"
    );
    assert_eq!(engine.state_counts().unread, 0, "it no longer waits");
    assert!(engine.no_leftovers(), "and nothing is left to remove");
}

#[tokio::test(start_paused = true)]
async fn a_left_runner_whose_profile_a_reload_removed_is_removed_at_its_next_ask() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    left_runner_docker_cannot_read(&docker);
    let dir = TestDir::new("unread-reloaded");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    assert_eq!(removals_of(&docker, "container-2"), 0, "not removed yet");
    let config = CONFIG
        .replace("[profiles.app]", "[profiles.big]")
        .replace("profile = \"app\"", "profile = \"big\"");
    engine.reload(Config::parse(&config).unwrap(), None, None);
    assert_eq!(
        engine.state_counts().unread,
        1,
        "a reload keeps it waiting to be read"
    );
    engine.make_unread_due();
    engine.tick(Shutdown::No).await;
    assert_eq!(removals_of(&docker, "container-2"), 1, "nothing keeps it");
    assert!(engine.no_recordings(), "no profile records its failure");
}

#[tokio::test]
async fn a_reload_keeps_a_container_left_to_remove() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let mut left = runner_container(1, 7, true);
    left.id = "runnerd-shell-10-1".to_owned();
    left.name = left.id.clone();
    left.labels
        .insert(record::SHELL_LABEL.to_owned(), "10".to_owned());
    {
        let mut state = docker.0.lock().unwrap();
        state.containers = vec![left];
        state.fail_removes = 1;
    }
    let dir = TestDir::new("left-reloaded");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    engine.reload(Config::parse(&recording()).unwrap(), None, None);
    assert!(
        engine.is_leftover("runnerd-shell-10-1"),
        "it stays to remove"
    );
    engine.tick(Shutdown::No).await;
    assert!(engine.no_leftovers(), "and the next tick removes it");
}

#[tokio::test(start_paused = true)]
async fn a_restart_finds_again_what_the_last_daemon_could_not_read_or_remove() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    left_runner_docker_cannot_read(&docker);
    let dir = TestDir::new("left-restarted");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    assert_eq!(engine.state_counts().unread, 1, "it could not be read");
    drop(engine);

    // A new daemon asks at once, with no backoff carried over; Docker refuses the removal that follows.
    {
        let mut state = docker.0.lock().unwrap();
        state.fail_files = 0;
        state.container_logs.remove("container-2");
        state.fail_removes = 1;
    }
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    assert_eq!(engine.state_counts().unread, 0, "it was read");
    assert!(engine.is_leftover("container-2"), "and is left to remove");
    engine.tick(Shutdown::No).await;
    assert!(
        docker.0.lock().unwrap().containers.is_empty(),
        "the next tick removed it"
    );
}

#[tokio::test(start_paused = true)]
async fn a_left_runners_job_only_the_history_names_is_still_recorded_when_docker_reads_it_late() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let runner = runner_container(2, 8, false);
    let name = runner.name.clone();
    {
        // No diagnostic files: its worker log does not name the job.
        let mut state = docker.0.lock().unwrap();
        state.containers = vec![runner];
        state
            .container_logs
            .insert("container-2".to_owned(), FAILED.to_owned());
        state.written = 1 << 30;
        state.fail_logs = 1;
    }
    let dir = TestDir::new("unread-history");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    handle.store.queued(
        "o/r",
        &QueuedJob {
            id: 5,
            run_id: 5,
            name: "build".to_owned(),
            ..QueuedJob::default()
        },
        crate::time::now_ms(),
    );
    handle.store.runner_started(RunnerRecord {
        name: name.clone(),
        repository: "o/r".to_owned(),
        profile: "app".to_owned(),
        slot: 2,
        container: "container-2".to_owned(),
        started_at: crate::time::now_ms(),
        memory_cap: None,
        cpu_cap: None,
        admission: None,
        purpose: None,
    });
    handle.store.claimed(5, 5, &name, crate::time::now_ms());
    handle.store.flush().await;
    engine.recover().await.unwrap();
    assert_eq!(engine.state_counts().unread, 1, "its log was not read");

    engine.make_unread_due();
    tokio::task::yield_now().await;
    for _ in 0..200 {
        engine.tick(Shutdown::No).await;
        if engine.state_counts().unread == 0 && engine.no_recordings() {
            break;
        }
        tokio::task::yield_now().await;
    }
    engine.publish();
    let jobs: Vec<u64> = handle
        .records
        .borrow()
        .records
        .iter()
        .map(|record| record.job)
        .collect();
    assert_eq!(jobs, [5], "the retry still knows which job it ran");
}

#[tokio::test(start_paused = true)]
async fn a_left_runner_read_late_has_its_job_read_from_github_under_its_repository() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    left_runner_docker_cannot_read(&docker);
    github
        .0
        .lock()
        .unwrap()
        .jobs
        .insert(5, details(5, "completed", Some("failure")));
    let dir = TestDir::new("unread-repository");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    handle.store.queued(
        "o/r",
        &QueuedJob {
            id: 5,
            run_id: 5,
            name: "build".to_owned(),
            ..QueuedJob::default()
        },
        crate::time::now_ms(),
    );
    handle.store.flush().await;
    engine.recover().await.unwrap();
    assert_eq!(engine.state_counts().unread, 1, "its files were not read");

    docker.0.lock().unwrap().fail_files = 0;
    engine.make_unread_due();
    let poll = engine.config.host.poll_interval;
    for _ in 0..8 {
        engine.tick(Shutdown::No).await;
        tokio::time::advance(poll).await;
    }
    let asked = github.0.lock().unwrap().job_repositories.clone();
    assert_eq!(
        asked,
        BTreeSet::from(["o/r".to_owned()]),
        "the only one asked"
    );
}

#[tokio::test]
async fn a_left_runner_still_running_with_no_slot_is_removed_without_reading_it_for_a_record() {
    // Read, its log of a failed job would make a record of a container that is still running.
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let adopted = runner_container(1, 7, true);
    let mut left = runner_container(2, 8, true);
    left.labels
        .insert("runnerd.slot".to_owned(), "1".to_owned());
    {
        let mut state = docker.0.lock().unwrap();
        state.containers = vec![adopted, left];
        state
            .container_logs
            .insert("container-2".to_owned(), FAILED.to_owned());
        state
            .diag
            .insert("container-2".to_owned(), docker::tests::diag_archive(5));
    }
    let dir = TestDir::new("left-running");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    assert_eq!(removals_of(&docker, "container-2"), 1, "it is removed");
    assert!(engine.no_recordings(), "and no record is made of it");
}

#[tokio::test]
async fn a_left_runner_docker_is_already_removing_stays_followed_until_it_is_gone() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    {
        let mut state = docker.0.lock().unwrap();
        state.containers = vec![runner_container(2, 8, false)];
        state.being_removed.insert("container-2".to_owned());
    }
    let dir = TestDir::new("left-being-removed");
    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    assert!(
        engine.is_leftover("container-2"),
        "Docker's removal can fail"
    );
    docker.0.lock().unwrap().being_removed.clear();
    engine.tick(Shutdown::No).await;
    assert!(engine.no_leftovers(), "the next tick saw it through");
    assert!(
        docker.0.lock().unwrap().containers.is_empty(),
        "and it is gone"
    );
}

#[tokio::test(start_paused = true)]
async fn docker_going_away_while_a_left_runner_is_read_during_a_stop_ends_the_stops_wait() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    left_runner_docker_cannot_read(&docker);
    let dir = TestDir::new("unread-outage");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    handle.control.shutdown(Shutdown::Graceful);
    docker.set_outage(Outage::Refused);
    engine.make_unread_due();
    engine.collect_finished().await;
    assert!(engine.control.docker_gone(), "the stop stops asking Docker");
    assert_eq!(engine.state_counts().unread, 1, "it stays to read");
}

#[tokio::test]
async fn a_record_container_docker_would_not_remove_after_its_commit_goes_without_a_restart() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record-retried");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    docker
        .0
        .lock()
        .unwrap()
        .fail_remove_of
        .insert("container-1".to_owned());
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    until_recorded(&mut engine).await;
    assert_eq!(
        handle.records.borrow().records.len(),
        1,
        "the record is made whatever becomes of its container"
    );
    assert_eq!(removals_of(&docker, "container-1"), 2, "asked again");
    assert!(
        !has_container(&docker, "container-1"),
        "the second removal took it"
    );
    assert!(engine.no_leftovers(), "and nothing is left to remove");
}

#[tokio::test]
async fn a_container_docker_would_neither_commit_nor_remove_goes_without_a_restart() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("commit-failed");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    {
        let mut state = docker.0.lock().unwrap();
        state.fail_commits = 1;
        state.fail_remove_of.insert("container-1".to_owned());
    }
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    until_recorded(&mut engine).await;
    assert!(
        handle.records.borrow().records.is_empty(),
        "no record came of it"
    );
    assert_eq!(removals_of(&docker, "container-1"), 2, "asked again");
    assert!(
        !has_container(&docker, "container-1"),
        "the second removal took it"
    );
}

#[tokio::test]
async fn a_container_refused_a_record_that_docker_would_not_remove_goes_without_a_restart() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("refused-retried");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    {
        let mut state = docker.0.lock().unwrap();
        state.changed = vec!["/home/runner/.credentials".to_owned()];
        state.fail_remove_of.insert("container-1".to_owned());
    }
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    until_recorded(&mut engine).await;
    assert_eq!(removals_of(&docker, "container-1"), 2, "asked again");
    assert!(
        !has_container(&docker, "container-1"),
        "the second removal took it"
    );
    assert!(engine.no_leftovers(), "and nothing is left to remove");
}

#[tokio::test]
async fn a_record_container_docker_is_already_removing_stays_followed_until_it_is_gone() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record-under-way");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    docker.0.lock().unwrap().commits_held.send_replace(true);
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    let commit = "commit container-1 runnerd-record:1".to_owned();
    for _ in 0..200 {
        engine.tick(Shutdown::No).await;
        if docker.calls().contains(&commit) {
            break;
        }
        tokio::time::sleep(Duration::from_millis(5)).await;
    }
    {
        // Docker takes it over only now: its logs were still to read.
        let mut state = docker.0.lock().unwrap();
        state.being_removed.insert("container-1".to_owned());
        state.commits_held.send_replace(false);
    }
    until_recorded(&mut engine).await;
    assert!(
        engine.is_leftover("container-1"),
        "Docker's removal can fail"
    );
    docker.0.lock().unwrap().being_removed.clear();
    engine.tick(Shutdown::No).await;
    assert!(engine.no_leftovers(), "the next tick saw it through");
    assert!(!has_container(&docker, "container-1"), "and it is gone");
}

#[tokio::test]
async fn a_restart_removes_a_committed_records_container_and_does_not_record_it_again() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record-restarted");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
    docker
        .0
        .lock()
        .unwrap()
        .fail_remove_of
        .insert("container-1".to_owned());
    job_ends(&github, &docker, &mut engine, &handle, FAILED).await;
    // The daemon stops before a tick retries the removal: the commit's task is done, the recording not yet collected.
    let removal = "remove container-1".to_owned();
    for _ in 0..200 {
        engine.tick(Shutdown::No).await;
        if docker.calls().contains(&removal) {
            break;
        }
        tokio::time::sleep(Duration::from_millis(5)).await;
    }
    handle.store.flush().await;
    drop(engine);
    assert!(has_container(&docker, "container-1"), "Docker kept it");

    let (mut engine, _handle) = engine_with(&github, &docker, &dir, &recording());
    engine.recover().await.unwrap();
    let commits = docker
        .calls()
        .iter()
        .filter(|call| call.starts_with("commit container-1"))
        .count();
    assert_eq!(commits, 1, "its record exists");
    assert!(
        !has_container(&docker, "container-1"),
        "and the new daemon removed its container"
    );
    assert!(engine.no_recordings(), "without starting another record");
}

/// A job queued in the history whose runner stopped while no daemon ran, as an idle runner that took it during a
/// detaching stop. The restart claims it from the worker log, so nothing else closes it: its runner is in no slot. A
/// runner the history never recorded, as Docker failed to inspect it, leaves no removal to wait for.
#[tokio::test(start_paused = true)]
async fn a_left_runners_claimed_job_ends_whether_docker_read_its_log_at_once_or_later() {
    for (failed_reads, recorded) in [(0, true), (1, true), (0, false)] {
        let (github, docker) = (FakeGithub::default(), FakeDocker::default());
        let dir = TestDir::new("left-claim");
        {
            let mut state = docker.0.lock().unwrap();
            state.containers = vec![runner_container(2, 8, false)];
            state
                .container_logs
                .insert("container-2".to_owned(), FAILED.to_owned());
            state
                .diag
                .insert("container-2".to_owned(), docker::tests::diag_archive(5));
            state.fail_files = failed_reads;
        }
        github
            .0
            .lock()
            .unwrap()
            .jobs
            .insert(5, details(5, "completed", Some("failure")));
        let (mut engine, handle) = engine_with(&github, &docker, &dir, &recording());
        let name = runner_container(2, 8, false).name;
        if recorded {
            handle.store.runner_started(RunnerRecord {
                name,
                repository: "o/r".to_owned(),
                profile: "app".to_owned(),
                slot: 2,
                container: "container-2".to_owned(),
                started_at: crate::time::now_ms(),
                memory_cap: None,
                cpu_cap: None,
                admission: None,
                purpose: None,
            });
        }
        handle.store.queued(
            "o/r",
            &QueuedJob {
                id: 5,
                run_id: 5,
                name: "build".to_owned(),
                ..QueuedJob::default()
            },
            crate::time::now_ms(),
        );
        handle.store.flush().await;
        engine.recover().await.unwrap();
        let poll = engine.config.host.poll_interval;
        for _ in 0..240 {
            tokio::time::advance(poll).await;
            engine.tick(Shutdown::No).await;
        }
        handle.store.flush().await;
        let job = handle.store.job(5).await.unwrap().unwrap().summary;
        assert_eq!(
            (job.state, job.conclusion.as_deref()),
            (JobState::Completed, Some("failure")),
            "it ends, {failed_reads} failed reads of its log, runner recorded: {recorded}"
        );
        assert_eq!(job.runner.is_some(), recorded, "{job:?}");
    }
}

/// A capture that broke since the last tick reaches no `recapture` when a pass reaps its runner first, as for a job
/// shorter than one poll: the stopped container's worker log still names the job.
#[tokio::test]
async fn a_pass_that_reaps_a_runner_whose_capture_just_broke_keeps_its_job_in_the_history() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("drain-claim");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, CONFIG);
    engine.recover().await.unwrap();
    docker.0.lock().unwrap().break_captures = 1;
    github.0.lock().unwrap().queued = vec![job(5)];
    engine.tick(Shutdown::No).await;
    until_captures_end(&engine).await;
    {
        let mut github = github.0.lock().unwrap();
        github.queued = Vec::new();
        github
            .jobs
            .insert(5, details(5, "completed", Some("success")));
    }
    {
        let mut state = docker.0.lock().unwrap();
        state.containers[0].running = false;
        let container = state.containers[0].id.clone();
        state
            .container_logs
            .insert(container.clone(), "Running job: build\n".to_owned());
        state.diag.insert(container, docker::tests::diag_archive(5));
    }
    engine.exits.any();
    engine.turnover().await;
    handle.store.flush().await;
    let job = handle.store.job(5).await.unwrap().unwrap().summary;
    assert!(job.runner.is_some(), "its runner is named: {job:?}");
}

/// A capture that broke twice waits before it starts again; a pass that reaps its runner meanwhile, with no capture
/// left to drain, still reads the job from the stopped container's worker log.
#[tokio::test]
async fn a_pass_that_reaps_a_runner_waiting_to_capture_again_keeps_its_job_in_the_history() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("waiting-claim");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, CONFIG);
    engine.recover().await.unwrap();
    // Its first capture fails, and so does the one the next tick starts: the third waits 5 seconds.
    docker.0.lock().unwrap().fail_execs = 2;
    github.0.lock().unwrap().queued = vec![job(5)];
    for _ in 0..3 {
        engine.tick(Shutdown::No).await;
        until_captures_end(&engine).await;
    }
    assert_eq!(
        docker.0.lock().unwrap().fail_execs,
        0,
        "both captures failed"
    );
    {
        let mut github = github.0.lock().unwrap();
        github.queued = Vec::new();
        github
            .jobs
            .insert(5, details(5, "completed", Some("success")));
    }
    {
        let mut state = docker.0.lock().unwrap();
        state.containers[0].running = false;
        let container = state.containers[0].id.clone();
        state
            .container_logs
            .insert(container.clone(), "Running job: build\n".to_owned());
        state.diag.insert(container, docker::tests::diag_archive(5));
    }
    engine.exits.any();
    engine.turnover().await;
    handle.store.flush().await;
    let job = handle.store.job(5).await.unwrap().unwrap().summary;
    assert!(job.runner.is_some(), "its runner is named: {job:?}");
}

/// A capture started again after a break whose output ends before it names a job leaves the job to the worker log:
/// the runner's release reads it from the stopped container.
#[tokio::test]
async fn a_runner_whose_capture_broke_and_then_ended_with_no_job_is_named_from_its_worker_log() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("spent-claim");
    let (mut engine, handle) = engine_with(&github, &docker, &dir, CONFIG);
    engine.recover().await.unwrap();
    // Its first capture fails; the next tick's prints nothing and ends.
    docker.0.lock().unwrap().fail_execs = 1;
    github.0.lock().unwrap().queued = vec![job(5)];
    for _ in 0..3 {
        engine.tick(Shutdown::No).await;
        until_captures_end(&engine).await;
    }
    assert!(
        !docker.0.lock().unwrap().captured.is_empty(),
        "a capture started again after the failed one"
    );
    {
        let mut github = github.0.lock().unwrap();
        github.queued = Vec::new();
        github
            .jobs
            .insert(5, details(5, "completed", Some("success")));
    }
    {
        let mut state = docker.0.lock().unwrap();
        state.containers[0].running = false;
        let container = state.containers[0].id.clone();
        state
            .container_logs
            .insert(container.clone(), "Running job: build\n".to_owned());
        state.diag.insert(container, docker::tests::diag_archive(5));
    }
    engine.exits.any();
    engine.turnover().await;
    handle.store.flush().await;
    let job = handle.store.job(5).await.unwrap().unwrap().summary;
    assert!(job.runner.is_some(), "its runner is named: {job:?}");
}

/// A runner whose broken capture was due again when its container stopped or went has its worker log read at most
/// once, by `recapture`, not again by its release.
#[tokio::test]
async fn a_runner_whose_capture_broke_before_its_container_ended_has_its_worker_log_read_at_most_once()
 {
    for gone in [false, true] {
        let (github, docker) = (FakeGithub::default(), FakeDocker::default());
        let dir = TestDir::new("ended-claim");
        let (mut engine, _handle) = engine_with(&github, &docker, &dir, CONFIG);
        engine.recover().await.unwrap();
        docker.0.lock().unwrap().break_captures = 1;
        github.0.lock().unwrap().queued = vec![job(5)];
        engine.tick(Shutdown::No).await;
        until_captures_end(&engine).await;
        github.0.lock().unwrap().queued = Vec::new();
        let container = {
            let mut state = docker.0.lock().unwrap();
            let container = state.containers[0].id.clone();
            if gone {
                state.containers.clear();
            } else {
                state.containers[0].running = false;
            }
            container
        };
        for _ in 0..3 {
            engine.tick(Shutdown::No).await;
        }
        let reads = docker
            .calls()
            .iter()
            .filter(|call| call.starts_with(&format!("files {container} ")))
            .count();
        // A stopped container's log is read once; a gone one has none to read.
        assert_eq!(reads, usize::from(!gone), "gone: {gone}");
    }
}

/// A failed job whose runner ends between polls is recorded without GitHub being listed sooner than
/// `poll_interval`. The bug it catches: the end of each recording's check and commit waking a full tick, so failed
/// jobs released by a pass (ADR 0095) multiplied the polls.
#[tokio::test(start_paused = true)]
async fn recording_a_failed_job_between_polls_lists_github_no_sooner() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let dir = TestDir::new("record");
    running_runner(&github, &docker, "@@job 1 1\n");
    github
        .0
        .lock()
        .unwrap()
        .jobs
        .insert(1, details(1, "completed", Some("failure")));
    let config = recording().replace("[host]\n", "[host]\npoll_interval = \"1h\"\n");
    let (engine, handle) = engine_with(&github, &docker, &dir, &config);
    let run = tokio::spawn(engine.run());
    until_state(&handle, 1, JobState::Running).await;
    tokio::time::sleep(Duration::from_secs(5)).await;
    let listings = || {
        github
            .calls()
            .iter()
            .filter(|call| call.starts_with("runners "))
            .count()
    };
    let before = listings();
    {
        let mut state = docker.0.lock().unwrap();
        state.runner_log = Some(FAILED.to_owned());
        state.written = 1 << 30;
        state.containers[0].running = false;
        let id = state.containers[0].id.clone();
        state
            .events
            .retain(|events| events.send(Ok(id.clone())).is_ok());
    }
    tokio::time::sleep(Duration::from_secs(60)).await;

    assert!(
        docker
            .calls()
            .contains(&"commit container-1 runnerd-record:1".to_owned()),
        "the failed job was recorded: {:?}",
        docker.calls()
    );
    assert_eq!(listings(), before, "GitHub was listed before the next poll");
    handle.shutdown(Shutdown::Force);
    run.await.unwrap();
}