vitrify-cli 0.6.0

vitrify: Rust CLI suite, orchestrator, and harness integration for spec-driven development (SDD)
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
//! The orchestrator's loop.
//!
//! One scheduler thread owns every decision. Around it: a monitor thread
//! per running worker (lease renewal, the hard budget, the exit), one
//! integrator thread (the integration checkout and its gate), and one
//! lease keeper (work waiting for the gate). They report through a single
//! channel; nothing but the scheduler changes the run's state.
//!
//! Dispatch is continuous: whenever a slot is free, every claimable unit is
//! a candidate — any two are independent by construction (neither depends on
//! the other, each has its dependencies built, and ownership is exclusive) —
//! and each built unit goes to the integrator as it reports, to be gated
//! with whatever else is ready. State is re-derived from the mainline head,
//! never remembered: what a unit's last attempt left is in git, what a
//! change to the spec staled is in the traces.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use vitrify_core::claims::Op;
use vitrify_core::model::{self, Instance, UnitView, UtcTime};
use vitrify_core::trace::{self, TraceMap};
use vitrify_core::writeset::WriteSet;
use vitrify_core::{CoreError, journal, ledger};

use super::events::{EventLog, RunEvent};
use super::integrate::{self, GateMemo, HeadCheck, IntegrateRequest, Integrator, Outcome};
use super::lease::{self, Keeper, MonitorParams, MonitorVerdict};
use super::mainline::Mainline;
use super::progress::GateBoard;
use super::{Ctx, RunState, VERSION, WorkerRec, escalate, retry, tail, worker, write_run_json};
use crate::cmd::{brief, claim};
use crate::gate;
use crate::layout;
use crate::out::{self, J, diag, emit};
use crate::signals;

/// How long the scheduler sleeps when nothing reports.
const TICK: Duration = Duration::from_secs(1);

enum Event {
    /// A worker's process ended (or was ended).
    Exited {
        unit: String,
        verdict: MonitorVerdict,
        expires: UtcTime,
    },
    Integrated {
        unit: String,
        outcome: Outcome,
    },
    /// The integrator hit an error that is not a verdict on any unit.
    IntegratorFailed(String),
    HeadChecked(Box<Result<HeadCheck, CoreError>>),
}

enum Job {
    Integrate(IntegrateRequest),
    CheckHead { want_suite: bool },
    Stop,
}

#[derive(PartialEq)]
enum Phase {
    /// The worker is running.
    Working,
    /// Reported `built`; waiting for, or in, the gate.
    Integrating,
}

/// One claim this run holds.
struct Active {
    holder: String,
    seq: i64,
    branch: String,
    wt: PathBuf,
    pid: u32,
    out_path: PathBuf,
    /// The worker's temp directory: this claim's alone until it is closed.
    _tmp: gate::TmpSlot,
    /// What the unit's inputs hashed to when it was claimed.
    trace: TraceMap,
    phase: Phase,
    /// Index of this attempt in `RunState::workers`.
    rec: usize,
    /// Why the scheduler ended the worker itself, if it did.
    killed: Option<&'static str>,
}

/// A unit the run has stopped dispatching: an open question, or too many
/// failed attempts. It returns to the pool when its reading set changes —
/// the only thing that can make another attempt go differently.
struct Parked {
    reason: String,
    trace: TraceMap,
}

#[derive(Default)]
struct Fails {
    all: u32,
    work: u32,
    escalated: bool,
}

enum End {
    Complete,
    Blocked,
    /// The escalation judge said the job cannot safely continue.
    Aborted(String),
    Interrupted(i32),
}

/// The lease backend: claim transactions on this run's SDD.
struct RealLease {
    ml: Mainline,
    lease_minutes: i64,
}

impl lease::LeaseBackend for RealLease {
    fn renew(&self, unit: &str, holder: &str) -> lease::RenewResult {
        match self.ml.claim(Op::Renew, unit, holder, self.lease_minutes) {
            Ok(outcome) => match outcome.claim_expires.as_deref().and_then(UtcTime::parse) {
                Some(expires) => lease::RenewResult::Renewed { expires },
                None => lease::RenewResult::Failed("unparseable expiry".into()),
            },
            Err(e) if e.exit_code() == 6 => lease::RenewResult::LeaseExpired,
            // exit 4: no longer Claimed by this holder — the lease is gone
            Err(e) if e.exit_code() == 4 => lease::RenewResult::LeaseExpired,
            Err(e) => lease::RenewResult::Failed(e.to_string()),
        }
    }
}

struct Scheduler {
    ctx: Ctx,
    state: RunState,
    started_at: String,
    events_tx: Sender<Event>,
    events: Receiver<Event>,
    jobs: Sender<Job>,
    integrator: Option<JoinHandle<()>>,
    keeper: Arc<Keeper>,
    keeper_thread: Option<JoinHandle<()>>,
    params: MonitorParams,
    /// Held for the life of the run: a later run reads a free lock as
    /// "that run is gone".
    job_lock: Option<std::fs::File>,
    /// A detached checkout of the mainline head, for reading spec state.
    head_wt: PathBuf,
    active: BTreeMap<String, Active>,
    parked: BTreeMap<String, Parked>,
    /// Attempts per unit that did not land, this run: all of them, those
    /// where the work itself failed, and whether the judge has been asked.
    fails: BTreeMap<String, Fails>,
    worker_n: u32,
    escalation_index: u32,
    inst: Option<Instance>,
    views: Vec<UnitView>,
    head: String,
    /// The head up to which staleness has been applied.
    healed_at: String,
    last_derive: Instant,
    dirty: bool,
    abort: Option<String>,
    failure: Option<String>,
    /// Claims lost to a publish race since the last one that succeeded.
    races_lost: u32,
    /// This pass changed mainline without starting a worker (a claim made
    /// and given back): the derived view is no longer current.
    churned: bool,
    /// The head the last termination check ran on (`--wait`).
    checked_head: Option<String>,
    audit_notes: Vec<String>,
    /// The run's `events.jsonl`, appended to as things happen, so a reader
    /// can see the history and not only the latest `run.json`.
    log: EventLog,
}

/// The first line of an attempt's reason, at most 200 chars, for
/// `run.json`; `None` when there is no reason (a verified attempt).
fn first_line_clipped(detail: &str) -> Option<String> {
    let line = detail.lines().next().unwrap_or("");
    if line.is_empty() {
        return None;
    }
    Some(line.chars().take(200).collect())
}

pub(crate) fn orchestrate(ctx: Ctx) -> Result<u8, CoreError> {
    let mut s = Scheduler::start(ctx)?;
    let end = s.drive();
    s.finish(end)
}

// ---------------------------------------------------------- dead runs

/// `run-<stamp>-p<pid>-w<n>` → (`run-<stamp>-p<pid>`, pid).
fn run_holder(holder: &str) -> Option<(String, u32)> {
    let (job, w) = holder.rsplit_once("-w")?;
    if w.is_empty() || !w.chars().all(|c| c.is_ascii_digit()) || !job.starts_with("run-") {
        return None;
    }
    let (_, pid) = job.rsplit_once("-p")?;
    Some((job.to_string(), pid.parse().ok()?))
}

#[derive(PartialEq)]
enum Liveness {
    Alive,
    Dead,
    /// No trace of the job in this checkout: another machine's, or another
    /// checkout's. Its lease is left to lapse.
    Foreign,
}

/// Whether the run `job` (which ran in this checkout, if at all) is gone.
/// A run holds a lock on `.vitrify/jobs/<job>.lock` for its whole life; the
/// operating system frees it however the process ends.
fn liveness(scratch: &Path, job: &str, pid: u32) -> Liveness {
    let lock = scratch.join("jobs").join(format!("{job}.lock"));
    if let Ok(file) = std::fs::OpenOptions::new().write(true).open(&lock) {
        return match file.try_lock() {
            Ok(()) => {
                let _ = file.unlock();
                Liveness::Dead
            }
            Err(_) => Liveness::Alive,
        };
    }
    // a run from before job locks leaves only its log directory
    if scratch.join("logs").join(job).is_dir() {
        return if gate::alive(pid, false) {
            Liveness::Alive
        } else {
            Liveness::Dead
        };
    }
    Liveness::Foreign
}

/// Claims held by runs that are gone: (unit, holder, job, claim_seq).
fn dead_claims(ctx: &Ctx, inst: &Instance) -> Vec<(String, String, String, i64)> {
    let mut out = Vec::new();
    for u in &inst.units {
        if u.meta.get_str_or("status", "Draft") != "Claimed" {
            continue;
        }
        let holder = u.meta.get_str_or("claimed_by", "").to_string();
        let Some((job, pid)) = run_holder(&holder) else {
            continue;
        };
        if job != ctx.job_id && liveness(&ctx.ml.layout.scratch, &job, pid) == Liveness::Dead {
            out.push((u.unit.clone(), holder, job, u.meta.get_int("claim_seq")));
        }
    }
    out
}

/// `--dry-run`: what the run would release before dispatching.
pub(crate) fn dead_run_notes(ctx: &Ctx, inst: &Instance) -> Vec<String> {
    dead_claims(ctx, inst)
        .into_iter()
        .map(|(unit, holder, _, seq)| {
            format!(
                "Recovery: would release {unit} (c{seq}), claimed by {holder} — that run is gone"
            )
        })
        .collect()
}

// ---------------------------------------------------------- the scheduler

impl Scheduler {
    fn start(ctx: Ctx) -> Result<Scheduler, CoreError> {
        let io = |path: &Path| {
            let path = path.to_path_buf();
            move |source: std::io::Error| CoreError::Io { path, source }
        };
        ctx.ml.layout.ensure_scratch()?;
        std::fs::create_dir_all(&ctx.logs_dir).map_err(io(&ctx.logs_dir))?;
        let log = EventLog::open(&ctx.logs_dir);
        let jobs_dir = ctx.ml.layout.scratch.join("jobs");
        std::fs::create_dir_all(&jobs_dir).map_err(io(&jobs_dir))?;
        let lock_path = jobs_dir.join(format!("{}.lock", ctx.job_id));
        let job_lock = std::fs::OpenOptions::new()
            .create(true)
            .truncate(false)
            .write(true)
            .open(&lock_path)
            .map_err(io(&lock_path))?;
        // Nobody else can hold this name for long — a recovering run only
        // probes a lock and lets go — so wait for it rather than run without.
        job_lock.lock().map_err(io(&lock_path))?;

        let params = MonitorParams {
            lease_minutes: ctx.cfg.run.lease_minutes,
            renew_fraction: ctx.cfg.run.renew_fraction,
            worker_timeout_minutes: ctx.cfg.run.worker_timeout_minutes,
            tick: TICK,
        };
        let (events_tx, events) = mpsc::channel::<Event>();
        let (jobs, jobs_rx) = mpsc::channel::<Job>();

        let integrator = Integrator::new(ctx.ml.clone(), ctx.cfg.run.rows_at_once())
            .with_board(GateBoard::to(ctx.logs_dir.join("gate.json")).with_events(log.clone()));
        let memo = GateMemo {
            suite: None,
            tables_green_at: ctx.audited_at.clone(),
        };
        let tx = events_tx.clone();
        let integrator = std::thread::spawn(move || integrator_loop(integrator, jobs_rx, tx, memo));

        let keeper = Arc::new(Keeper::default());
        let keeper_thread = {
            let keeper = Arc::clone(&keeper);
            let params = params.clone();
            let backend = RealLease {
                ml: ctx.ml.clone(),
                lease_minutes: ctx.cfg.run.lease_minutes,
            };
            std::thread::spawn(move || keeper.run(&params, &lease::RealClock, &backend))
        };

        let head_wt = ctx
            .ml
            .layout
            .scratch
            .join(format!("tx-{}-head", std::process::id()));
        Ok(Scheduler {
            started_at: UtcTime::now().format(),
            state: RunState::default(),
            events_tx,
            events,
            jobs,
            integrator: Some(integrator),
            keeper,
            keeper_thread: Some(keeper_thread),
            params,
            job_lock: Some(job_lock),
            head_wt,
            active: BTreeMap::new(),
            parked: BTreeMap::new(),
            fails: BTreeMap::new(),
            worker_n: 0,
            escalation_index: 0,
            inst: None,
            views: Vec::new(),
            head: String::new(),
            healed_at: String::new(),
            last_derive: Instant::now(),
            dirty: true,
            abort: None,
            failure: None,
            races_lost: 0,
            churned: false,
            checked_head: None,
            audit_notes: Vec::new(),
            log,
            ctx,
        })
    }

    fn record(&self, terminated: Option<(&str, &str)>) {
        if let Err(e) = write_run_json(&self.ctx, &self.state, &self.started_at, terminated) {
            diag(&format!("warning: run.json not written: {e}"));
        }
    }

    /// Log a `waiting: …` line the run printed while it waits under `--wait`.
    fn waiting(&self, line: &str) {
        self.event(RunEvent::new(UtcTime::now(), "waiting", None, None, line));
    }

    /// Append one event to the run's event log.
    fn event(&self, e: RunEvent) {
        self.log.write(&e);
    }

    fn limit(&self) -> u32 {
        self.ctx.cfg.run.abort_failed_waves.max(1) as u32
    }

    // ------------------------------------------------------------ loop

    fn drive(&mut self) -> Result<End, CoreError> {
        // the run's own record exists before anything is dispatched
        self.record(None);
        let sdd = crate::cmd::sdd_arg(&self.ctx.ml.sdd_rel);
        let harness = self.ctx.harness.clone();
        let cap = self.ctx.cap;
        let detail = if harness.is_empty() {
            format!("{sdd} · cap {cap}")
        } else {
            format!("{sdd} · {harness} · cap {cap}")
        };
        self.event(
            RunEvent::new(UtcTime::now(), "run-started", None, None, detail)
                .with("cap", J::Int(cap as i64))
                .with("harness", J::s(harness))
                .with("sdd", J::s(sdd)),
        );
        self.recover_dead_runs()?;

        loop {
            if let Some(sig) = signals::pending() {
                return Ok(End::Interrupted(sig));
            }
            if let Some(e) = self.failure.take() {
                return Err(CoreError::refusal(1, e));
            }
            if let Some(reason) = self.abort.take() {
                return Ok(End::Aborted(reason));
            }

            // nothing reported: look at mainline again every poll period —
            // spec changes and lapsed leases arrive without an event
            self.integrator_alive()?;
            let poll = Duration::from_secs(self.ctx.cfg.run.poll_seconds.max(1) as u64);
            if self.dirty || self.last_derive.elapsed() >= poll {
                if !self.derive()? {
                    // mainline could not be brought up to date this time:
                    // nothing is decided on a view that may be stale
                    std::thread::sleep(Duration::from_millis(300));
                    self.dirty = true;
                    continue;
                }
                self.churned = false;
                let dispatched = self.fill_slots()?;
                self.dirty = false;

                if self.active.is_empty() && dispatched == 0 && self.abort.is_none() {
                    if self.churned {
                        // a claim was made and given back: derive again
                        // before deciding anything (bounded: each such
                        // attempt counts toward setting the unit aside)
                        self.dirty = true;
                        continue;
                    }
                    if (1..=3).contains(&self.races_lost) {
                        // a claim lost only to a busy mainline: look again
                        self.races_lost += 1;
                        std::thread::sleep(Duration::from_millis(300));
                        self.dirty = true;
                        continue;
                    }
                    if let Some(end) = self.conclude()? {
                        return Ok(end);
                    }
                }
            }
            match self.events.recv_timeout(TICK) {
                Ok(event) => {
                    self.handle(event)?;
                    while let Ok(event) = self.events.try_recv() {
                        self.handle(event)?;
                    }
                    self.dirty = true;
                }
                Err(RecvTimeoutError::Timeout) => {}
                Err(RecvTimeoutError::Disconnected) => {
                    return Err(CoreError::refusal(
                        1,
                        "error: the orchestrator's threads are gone",
                    ));
                }
            }
        }
    }

    /// Read the mainline head: apply staleness where the spec moved, load
    /// the SDD, compute the views, return parked units whose reading set
    /// changed to the pool, and stop workers building from text that is no
    /// longer current. `false` = mainline was too busy to be put right;
    /// the view was left as it was.
    fn derive(&mut self) -> Result<bool, CoreError> {
        let mut head = self.ctx.ml.head()?;
        if self.healed_at.is_empty() {
            self.healed_at = head.clone(); // preflight left the audit clean
        }
        if head != self.healed_at {
            if integrate::spec_moved(&self.ctx.ml, &self.healed_at, &head)? {
                let healed = match self.ctx.ml.heal(false, true) {
                    Ok(healed) => healed,
                    Err(e) if e.exit_code() == 5 => {
                        // mainline too busy to publish it now: next pass
                        diag(&format!("note: {e}"));
                        return Ok(false);
                    }
                    Err(e) => return Err(e),
                };
                for (unit, cause) in &healed.stale {
                    emit(&format!("stale {unit}: {cause} — it will be rebuilt"));
                    self.event(RunEvent::new(
                        UtcTime::now(),
                        "stale",
                        Some(unit),
                        None,
                        cause.clone(),
                    ));
                }
                let notes: Vec<String> = healed
                    .other
                    .iter()
                    .filter(|f| f.check != 7)
                    .map(|f| format!("audit [check {}] {}: {}", f.check, f.subject, f.cause))
                    .collect();
                if notes != self.audit_notes {
                    for n in &notes {
                        diag(n);
                    }
                    self.audit_notes = notes;
                }
                if healed.commit.is_some() {
                    head = self.ctx.ml.head()?;
                }
            }
            self.healed_at = head.clone();
        }
        {
            // the head checkout is one more worktree: its turn like any other
            let _turn = claim::tx_lock();
            self.ctx.ml.checkout_at(&self.head_wt, &head)?;
        }
        let inst = self.ctx.ml.sdd_in(&self.head_wt)?;
        let now = UtcTime::now();
        self.views = inst
            .units
            .iter()
            .map(|u| model::unit_view(u, &inst.units, now))
            .collect();
        self.views.sort_by(|a, b| a.unit.cmp(&b.unit));

        // a parked unit rejoins when its reading set is no longer what it
        // was parked on
        let mut back: Vec<String> = Vec::new();
        for (unit, parked) in &self.parked {
            if let Some(u) = inst.unit(unit)
                && trace::compute(&inst, u)?.0 != parked.trace
            {
                back.push(unit.clone());
            }
        }
        for unit in back {
            self.parked.remove(&unit);
            self.fails.remove(&unit);
            emit(&format!(
                "{unit}: its reading set changed — back in the pool"
            ));
            self.event(RunEvent::new(
                UtcTime::now(),
                "back-in-pool",
                Some(&unit),
                None,
                "its reading set changed",
            ));
        }
        // a worker whose unit's inputs changed under it is building the
        // wrong thing: the gate would reject it; stop it now
        let mut stale: Vec<u32> = Vec::new();
        for (unit, a) in self.active.iter_mut() {
            if a.phase == Phase::Working
                && a.killed.is_none()
                && let Some(u) = inst.unit(unit)
                && trace::compute(&inst, u)?.0 != a.trace
            {
                diag(&format!(
                    "{unit}: its inputs changed on {} while it was being built — stopping the worker; the unit is rebuilt from the current text",
                    self.ctx.ml.branch
                ));
                a.killed = Some("stale-inputs");
                stale.push(a.pid);
            }
        }
        for pid in stale {
            gate::kill_group(pid);
        }

        self.inst = Some(inst);
        self.head = head;
        self.last_derive = Instant::now();
        Ok(true)
    }

    /// The integrator thread ends only when told to. If it is gone, units
    /// waiting for the gate would wait forever with their leases renewed.
    fn integrator_alive(&self) -> Result<(), CoreError> {
        if self.integrator.as_ref().is_some_and(|t| t.is_finished()) {
            return Err(CoreError::refusal(
                1,
                "error: the integrator thread ended unexpectedly",
            ));
        }
        Ok(())
    }

    /// Start a worker for each claimable unit while slots are free.
    fn fill_slots(&mut self) -> Result<usize, CoreError> {
        let mut dispatched = 0;
        let candidates: Vec<(String, bool)> = self
            .views
            .iter()
            .filter(|v| {
                v.claimable
                    && !self.active.contains_key(&v.unit)
                    && !self.parked.contains_key(&v.unit)
            })
            .map(|v| (v.unit.clone(), v.status == "Stale"))
            .collect();
        for (unit, was_stale) in candidates {
            // the cap bounds running workers; a unit waiting for the gate
            // holds a lease, not a slot
            let working = self
                .active
                .values()
                .filter(|a| a.phase == Phase::Working)
                .count();
            if working >= self.ctx.cap || signals::interrupted() || self.abort.is_some() {
                break;
            }
            if self.dispatch(&unit, was_stale)? {
                dispatched += 1;
            }
        }
        Ok(dispatched)
    }

    // -------------------------------------------------------- dispatch

    /// Claim `unit`, give it a worktree, start its worker.
    fn dispatch(&mut self, unit: &str, was_stale: bool) -> Result<bool, CoreError> {
        self.worker_n += 1;
        let holder = format!("{}-w{}", self.ctx.job_id, self.worker_n);
        let lease = self.ctx.cfg.run.lease_minutes;
        let outcome = match self.ctx.ml.claim(Op::Claim, unit, &holder, lease) {
            Ok(o) => o,
            Err(e) if matches!(e.exit_code(), 4 | 5) => {
                diag(&format!("claim lost for {unit}: {e}"));
                if e.exit_code() == 5 {
                    self.races_lost += 1; // mainline was busy, not the unit taken
                }
                return Ok(false);
            }
            Err(e) => return Err(e),
        };
        self.races_lost = 0;
        let seq = outcome.claim_seq;
        let branch = layout::unit_branch(&self.ctx.slug, unit, seq);
        let wt = self.ctx.ml.layout.unit_worktree(&self.ctx.slug, unit, seq);
        let primary = &self.ctx.ml.primary;

        // Both names belong to this claim alone. If either is taken, it is
        // by something this run did not make: leave it exactly as it is.
        let taken = if primary.branch_exists(&branch).map_err(CoreError::from)? {
            Some(format!("branch {branch}"))
        } else if wt.exists() {
            Some(format!("worktree {}", wt.display()))
        } else {
            None
        };
        if let Some(what) = taken {
            let why = format!(
                "{what} already exists and is not this run's — left untouched; remove or rename it if it is not wanted"
            );
            diag(&format!("{unit} (c{seq}): {why}"));
            self.release(unit, &holder);
            self.state
                .attempts
                .push((unit.to_string(), "name-taken".to_string(), why));
            self.count_failure(unit, "name-taken", false)?;
            // mainline has moved (a claim and its release): what this pass
            // derived is no longer the state to conclude on
            self.churned = true;
            return Ok(false);
        }

        let expires = outcome
            .claim_expires
            .as_deref()
            .and_then(UtcTime::parse)
            .unwrap_or_else(|| UtcTime::now().add_minutes(self.ctx.cfg.run.lease_minutes));
        match self.launch(unit, &holder, seq, &branch, &wt, &outcome.commit, was_stale) {
            Ok((handle, tmp, trace, retried)) => {
                let pid = handle.pid;
                let out_path = handle.out_path.clone();
                let started = UtcTime::now();
                self.state.workers.push(WorkerRec {
                    holder: holder.clone(),
                    pid,
                    unit: unit.to_string(),
                    claim_seq: seq,
                    branch: branch.clone(),
                    worktree: wt.display().to_string(),
                    started_at: started.format(),
                    ended_at: None,
                    outcome: "running".to_string(),
                    retry: retried,
                    detail: None,
                });
                let rec = self.state.workers.len() - 1;
                self.monitor(handle, unit, &holder, expires);
                self.active.insert(
                    unit.to_string(),
                    Active {
                        holder: holder.clone(),
                        seq,
                        branch,
                        wt,
                        pid,
                        out_path,
                        _tmp: tmp,
                        trace,
                        phase: Phase::Working,
                        rec,
                        killed: None,
                    },
                );
                emit(&format!(
                    "dispatch {unit} (c{seq}) → w{}{}",
                    self.worker_n,
                    if retried { "  [retry]" } else { "" }
                ));
                self.event(
                    RunEvent::new(
                        started,
                        "dispatched",
                        Some(unit),
                        Some(seq),
                        format!(
                            "→ w{}{}",
                            self.worker_n,
                            if retried { " (retry)" } else { "" }
                        ),
                    )
                    .with("holder", J::s(holder.clone()))
                    .with("retry", J::Bool(retried)),
                );
                self.record(None);
                Ok(true)
            }
            Err(e) => {
                // the worktree and branch are this claim's own, made a
                // moment ago and holding nothing: take them back
                if wt.join(".git").exists() {
                    let _ = primary.worktree_remove(&wt);
                }
                let _ = primary.delete_branch(&branch);
                self.release(unit, &holder);
                if e.exit_code() != 4 {
                    return Err(e);
                }
                // The unit cannot be handed to a worker as the spec stands
                // at its claim commit — its reading set does not resolve: a
                // pinned contract whose authority has moved on since
                // preflight, a consumed contract that is gone. No attempt
                // would change that, and it is no reason to stop building
                // the rest: the unit waits for the spec.
                let text = e.to_string();
                let why = text
                    .lines()
                    .map(|l| l.strip_prefix("error: ").unwrap_or(l))
                    .collect::<Vec<_>>()
                    .join("; ");
                diag(&format!(
                    "{unit} (c{seq}): not started — {why}; set aside until the spec changes"
                ));
                self.state.attempts.push((
                    unit.to_string(),
                    "not-started".to_string(),
                    why.clone(),
                ));
                self.park(unit, format!("its reading set does not resolve: {why}"))?;
                // mainline has moved (a claim and its release)
                self.churned = true;
                Ok(false)
            }
        }
    }

    /// The worktree, what the last attempt left, the prompt, the process.
    #[allow(clippy::too_many_arguments)]
    fn launch(
        &self,
        unit: &str,
        holder: &str,
        seq: i64,
        branch: &str,
        wt: &Path,
        claim_commit: &str,
        was_stale: bool,
    ) -> Result<(worker::WorkerHandle, gate::TmpSlot, TraceMap, bool), CoreError> {
        let ml = &self.ctx.ml;
        if let Some(parent) = wt.parent() {
            std::fs::create_dir_all(parent).map_err(|source| CoreError::Io {
                path: parent.to_path_buf(),
                source,
            })?;
        }
        ml.primary
            .worktree_add_branch(wt, branch, claim_commit)
            .map_err(CoreError::from)?;

        // everything the worker is told is read from its own worktree, at
        // the claim commit it was forked from
        let inst = ml.sdd_in(wt)?;
        let Some(u) = inst.unit(unit) else {
            return Err(CoreError::refusal(
                4,
                format!("error: unit not found at the claim commit: {unit}"),
            ));
        };
        let (trace_at_claim, _) = trace::compute(&inst, u)?;

        // what the last attempt left: its owned files, and why it failed
        let prev = retry::previous_attempt(&ml.primary, &inst, unit, seq)?;
        let mut seeded: Vec<String> = Vec::new();
        if let Some(prev) = &prev {
            let ws = WriteSet::of(&inst, u, wt)?;
            match retry::seed(&ml.primary.at(wt), &ws, claim_commit, prev) {
                Ok(paths) => seeded = paths,
                Err(e) => diag(&format!(
                    "warning: {unit}: could not carry over the files of {} ({e}); the worker starts without them",
                    prev.branch
                )),
            }
        }
        let stale_cause = if was_stale {
            Some(stale_cause(&inst, unit))
        } else {
            None
        };
        let sdd = crate::cmd::sdd_arg(&ml.sdd_rel);
        let session = retry::section(
            Some(&retry::Orchestrated {
                unit,
                holder,
                seq,
                branch,
                worktree: wt,
                sdd: &sdd,
            }),
            prev.as_ref(),
            &seeded,
            stale_cause.as_deref(),
        );
        let prompt =
            brief::build_worker_prompt_at(wt, unit, holder, &ml.sdd_rel, &ml.layout, &session)?;
        let _ = std::fs::write(
            self.ctx.logs_dir.join(format!("worker-{holder}.prompt.md")),
            &prompt,
        );

        // an attempt after a failed one runs under the retry profile
        let argv = if prev.is_some() {
            &self.ctx.retry_argv
        } else {
            &self.ctx.worker_argv
        };
        // a temp directory no other worker of this repository is using
        let tmp = gate::tmp_slot(&ml.layout.wt_root, "w").map_err(|source| CoreError::Io {
            path: ml.layout.wt_root.clone(),
            source,
        })?;
        let handle = worker::spawn_in(
            argv,
            &prompt,
            wt,
            &self.ctx.logs_dir,
            holder,
            unit,
            Some(&tmp.dir),
        )?;
        Ok((handle, tmp, trace_at_claim, prev.is_some()))
    }

    /// One thread per worker: renew its lease, enforce the hard budget,
    /// report its exit.
    fn monitor(&self, handle: worker::WorkerHandle, unit: &str, holder: &str, expires: UtcTime) {
        let tx = self.events_tx.clone();
        let params = self.params.clone();
        let backend = RealLease {
            ml: self.ctx.ml.clone(),
            lease_minutes: self.ctx.cfg.run.lease_minutes,
        };
        let unit = unit.to_string();
        let holder = holder.to_string();
        std::thread::spawn(move || {
            let mut child = handle.child;
            let pid = handle.pid;
            let (verdict, expires) = lease::monitor_worker_tracked(
                lease::MonitorTarget {
                    child: &mut child,
                    pid,
                    unit: &unit,
                    holder: &holder,
                    expires,
                },
                &params,
                &lease::RealClock,
                &backend,
            );
            let _ = tx.send(Event::Exited {
                unit,
                verdict,
                expires,
            });
        });
    }

    // ---------------------------------------------------------- events

    fn handle(&mut self, event: Event) -> Result<(), CoreError> {
        match event {
            Event::Exited {
                unit,
                verdict,
                expires,
            } => self.on_exit(&unit, verdict, expires),
            Event::Integrated { unit, outcome } => self.on_integrated(&unit, outcome),
            Event::IntegratorFailed(e) => {
                self.failure = Some(e);
                Ok(())
            }
            // only `conclude` asks for one, and it takes the answer itself
            Event::HeadChecked(_) => Ok(()),
        }
    }

    fn on_exit(
        &mut self,
        unit: &str,
        verdict: MonitorVerdict,
        expires: UtcTime,
    ) -> Result<(), CoreError> {
        let Some(a) = self.active.get(unit) else {
            return Ok(());
        };
        let (holder, seq, wt, out_path, killed) = (
            a.holder.clone(),
            a.seq,
            a.wt.clone(),
            a.out_path.clone(),
            a.killed,
        );
        let w = holder.rsplit('-').next().unwrap_or(&holder).to_string();

        if let Some(why) = killed {
            // the scheduler stopped this worker; there is no report to read
            let detail = match why {
                "stale-inputs" => format!(
                    "the unit's inputs changed on {} while it was being built",
                    self.ctx.ml.branch
                ),
                _ => "the run ended before the worker finished".to_string(),
            };
            return self.fail(unit, why, &detail, Counts::No);
        }

        let stdout_text = std::fs::read_to_string(&out_path).unwrap_or_default();
        let (report, parsed) = worker::report_or_released(&stdout_text, unit);
        self.state.reports.push(render_report_line(&report));
        // surface the worker's own account live — this is usually the
        // line that names the failure cause
        if parsed {
            diag(&format!(
                "report [{w}] {unit}: {}{}",
                report.outcome,
                if report.notes.is_empty() {
                    String::new()
                } else {
                    format!(" — {}", report.notes)
                }
            ));
            if report.unit != unit {
                diag(&format!(
                    "warning: [{w}] reported for {} but was dispatched for {unit}; taken as {unit}",
                    report.unit
                ));
            }
        } else {
            diag(&format!(
                "report [{w}] {unit}: no parseable report line (treated as released) — see {}",
                out_path.display()
            ));
        }

        match verdict {
            MonitorVerdict::TimedOut => {
                diag(&format!(
                    "worker {holder} on {unit} exceeded the hard budget — killed; claim released"
                ));
                self.fail(
                    unit,
                    "timeout",
                    &format!(
                        "the worker exceeded the hard budget of {} minutes and was stopped",
                        self.ctx.cfg.run.worker_timeout_minutes
                    ),
                    Counts::Work,
                )
            }
            MonitorVerdict::LeaseLost => {
                diag(&format!(
                    "worker {holder} on {unit} lost its lease — killed; the unit returns to the pool"
                ));
                self.fail(
                    unit,
                    "lease-lost",
                    "the lease could not be renewed and expired",
                    Counts::Quietly,
                )
            }
            MonitorVerdict::Exited => match report.outcome.as_str() {
                "built" if parsed => {
                    commit_handoff_if_dirty(&self.ctx.ml, &wt, unit, seq);
                    // the lease is the orchestrator's to keep until the
                    // gate has decided
                    self.keeper.hold(unit, &holder, expires);
                    if let Some(a) = self.active.get_mut(unit) {
                        a.phase = Phase::Integrating;
                        if let Some(rec) = self.state.workers.get_mut(a.rec) {
                            rec.outcome = "integrating".to_string();
                        }
                    }
                    self.event(RunEvent::new(
                        UtcTime::now(),
                        "integrating",
                        Some(unit),
                        Some(seq),
                        report.notes.clone(),
                    ));
                    self.record(None);
                    let _ = self.jobs.send(Job::Integrate(IntegrateRequest {
                        unit: unit.to_string(),
                        token_seq: seq,
                        builder_holder: holder,
                        integrator_id: format!("{}-integrator", self.ctx.job_id),
                    }));
                    Ok(())
                }
                "blocked" if parsed => {
                    let q = report.q.clone().unwrap_or_else(|| "Q-???".to_string());
                    self.record_question(&q, unit, &holder, &report.notes)?;
                    self.park(unit, format!("open {q}: {}", report.notes))?;
                    self.fail(
                        unit,
                        "blocked",
                        &format!("{q}: {}", report.notes),
                        Counts::No,
                    )
                }
                _ => {
                    let detail = if report.notes.is_empty() {
                        "the worker ended without building".to_string()
                    } else {
                        report.notes.clone()
                    };
                    self.fail(unit, "released", &detail, Counts::Work)
                }
            },
        }
    }

    fn on_integrated(&mut self, unit: &str, outcome: Outcome) -> Result<(), CoreError> {
        self.keeper.drop_unit(unit);
        let Some(a) = self.active.get(unit) else {
            return Ok(());
        };
        let (seq, branch, holder) = (a.seq, a.branch.clone(), a.holder.clone());
        match outcome {
            Outcome::Verified { .. } => {
                emit(&format!("integrated {unit} (c{seq})"));
                self.fails.remove(unit);
                self.state
                    .attempts
                    .push((unit.to_string(), "verified".to_string(), String::new()));
                self.close(unit, "verified", "");
                Ok(())
            }
            Outcome::RejectedFencing { reason } => {
                diag(&format!("fencing rejection for {unit}: {reason}"));
                self.fail(unit, "rejected-fencing", &reason, Counts::Quietly)
            }
            Outcome::SkippedLeaseExpired => {
                diag(&format!(
                    "{unit}: token current but lease expired — back to the pool"
                ));
                self.fail(
                    unit,
                    "skipped",
                    "the lease had expired when the gate started",
                    Counts::Quietly,
                )
            }
            Outcome::RejectedWriteSet { paths, detail } => {
                diag(&format!(
                    "write-set violation integrating {unit}: {}",
                    paths.join(", ")
                ));
                self.state.gate_failures.push((
                    unit.to_string(),
                    "write-set check".to_string(),
                    detail.clone(),
                ));
                self.fail(
                    unit,
                    "rejected-write-set",
                    &format!("the work changed paths outside the unit's write-set:\n{detail}"),
                    Counts::Work,
                )
            }
            Outcome::RejectedNotBuilt { reason } => {
                diag(&format!("{unit}: {reason}"));
                self.state.gate_failures.push((
                    unit.to_string(),
                    "Built stamp".to_string(),
                    reason.clone(),
                ));
                self.fail(unit, "rejected-not-built", &reason, Counts::Work)
            }
            Outcome::RejectedStaleInputs { keys } => {
                let detail = format!(
                    "built from text that has since changed on {}: {}",
                    self.ctx.ml.branch,
                    keys.join(", ")
                );
                diag(&format!(
                    "{unit}: {detail} — it is rebuilt from the current text"
                ));
                self.fail(unit, "stale-inputs", &detail, Counts::No)
            }
            Outcome::RejectedGate { command, output } => {
                diag(&format!("gate red integrating {unit}: {command}"));
                self.state
                    .gate_failures
                    .push((unit.to_string(), command.clone(), output.clone()));
                self.fail(
                    unit,
                    "rejected-gate",
                    &format!(
                        "failing gate: `{command}`\n{}",
                        retry::clip(&output, 40, 4000)
                    ),
                    Counts::Work,
                )
            }
            Outcome::OwnershipConflict { paths } => {
                let verdict = self.escalate(
                    unit,
                    &branch,
                    "merge conflict on paths this unit may write — mainline also changed them after the branch forked",
                    &paths.join("\n"),
                )?;
                let detail = format!(
                    "merge conflict with {} on: {}",
                    self.ctx.ml.branch,
                    paths.join(", ")
                );
                self.enforce(unit, &holder, verdict)?;
                self.fail(unit, "ownership-conflict", &detail, Counts::Quietly)
            }
            Outcome::LostRace => {
                diag(&format!(
                    "integration of {unit} lost the publish race after retries"
                ));
                self.fail(
                    unit,
                    "lost-race",
                    "mainline kept moving; the integration was not published",
                    Counts::Quietly,
                )
            }
            // the run is ending: `finish` sets the work aside
            Outcome::Interrupted => Ok(()),
        }
    }

    // ------------------------------------------------------- outcomes

    /// End this run's record of an attempt, with why it ended (`""` when
    /// it verified), and log its outcome.
    fn close(&mut self, unit: &str, outcome: &str, detail: &str) {
        let detail = first_line_clipped(detail);
        if let Some(a) = self.active.remove(unit)
            && let Some(rec) = self.state.workers.get_mut(a.rec)
        {
            let ended = UtcTime::now();
            let duration_s = UtcTime::parse(&rec.started_at)
                .map(|s| ended.0 - s.0)
                .unwrap_or(0);
            rec.outcome = outcome.to_string();
            rec.ended_at = Some(ended.format());
            rec.detail = detail.clone();
            let text = match &detail {
                Some(d) => format!("{outcome} — {d}"),
                None => outcome.to_string(),
            };
            let e = RunEvent::new(ended, "outcome", Some(unit), Some(a.seq), text)
                .with("duration_s", J::Int(duration_s))
                .with("outcome", J::s(outcome));
            self.event(e);
        }
        self.record(None);
    }

    /// An attempt that did not land: its work goes to its branch with the
    /// reason, its worktree is removed, its claim released.
    fn fail(
        &mut self,
        unit: &str,
        label: &str,
        detail: &str,
        counts: Counts,
    ) -> Result<(), CoreError> {
        if let Some(a) = self.active.get(unit) {
            retry::abandon(&self.ctx.ml.primary, &a.wt, unit, a.seq, label, detail);
            let holder = a.holder.clone();
            self.release(unit, &holder);
        }
        self.state
            .attempts
            .push((unit.to_string(), label.to_string(), detail.to_string()));
        self.close(unit, label, detail);
        match counts {
            Counts::No => Ok(()),
            Counts::Quietly => self.count_failure(unit, label, false),
            Counts::Work => self.count_failure(unit, label, true),
        }
    }

    /// Count a failed attempt. At the limit, work failures get one
    /// escalation call; at twice the limit the unit is set aside.
    fn count_failure(&mut self, unit: &str, label: &str, work: bool) -> Result<(), CoreError> {
        let limit = self.limit();
        let (all, ask) = {
            let f = self.fails.entry(unit.to_string()).or_default();
            f.all += 1;
            if work {
                f.work += 1;
            }
            // the judge is asked once: when the work itself — not a lost
            // race or a lapsed lease in between — has failed `limit` times
            let ask = work && f.work >= limit && !f.escalated;
            if ask {
                f.escalated = true;
            }
            (f.all, ask)
        };
        if ask {
            let (command, output) = self
                .state
                .gate_failures
                .iter()
                .rev()
                .find(|(u, _, _)| u == unit)
                .map(|(_, c, o)| (c.clone(), o.clone()))
                .unwrap_or_else(|| {
                    let last = self
                        .state
                        .attempts
                        .iter()
                        .rev()
                        .find(|(u, _, _)| u == unit)
                        .map(|(_, l, d)| format!("{l}: {d}"))
                        .unwrap_or_default();
                    (format!("worker attempt ({label})"), last)
                });
            let verdict = self.escalate(unit, "-", &command, &output)?;
            self.enforce(unit, "escalation", verdict)?;
        }
        if all >= limit * 2 && !self.parked.contains_key(unit) && self.abort.is_none() {
            diag(&format!(
                "{unit}: {all} attempts did not land (last: {label}) — set aside for this run"
            ));
            return self.park(
                unit,
                format!("set aside after {all} attempts that did not land; last outcome {label}"),
            );
        }
        Ok(())
    }

    /// Stop dispatching `unit` until its reading set changes.
    fn park(&mut self, unit: &str, reason: String) -> Result<(), CoreError> {
        let trace = match &self.inst {
            Some(inst) => match inst.unit(unit) {
                Some(u) => trace::compute(inst, u)?.0,
                None => Vec::new(),
            },
            None => Vec::new(),
        };
        self.parked
            .insert(unit.to_string(), Parked { reason, trace });
        Ok(())
    }

    /// Land a `Q-###` in the Decision Log. A mainline too busy to take it
    /// does not end the run: the question is in the run's own record, and
    /// the unit is parked either way.
    fn record_question(
        &self,
        q: &str,
        unit: &str,
        reporter: &str,
        notes: &str,
    ) -> Result<(), CoreError> {
        match self.ctx.ml.land_q_entry(q, unit, reporter, notes) {
            Err(e) if e.exit_code() == 5 => {
                diag(&format!(
                    "warning: {q} for {unit} was not recorded in the Decision Log ({e}); record it by hand: {notes}"
                ));
                Ok(())
            }
            other => other,
        }
    }

    fn release(&self, unit: &str, holder: &str) {
        let lease = self.ctx.cfg.run.lease_minutes;
        // a release that only lost the race for the ref is tried again: a
        // claim left standing keeps the unit out of the pool for its lease
        for attempt in 0..3 {
            match self.ctx.ml.claim(Op::Release, unit, holder, lease) {
                Ok(_) => return,
                Err(e) if e.exit_code() == 4 => return, // not ours any more
                Err(e) if e.exit_code() == 5 && attempt < 2 => {
                    std::thread::sleep(Duration::from_millis(200));
                }
                Err(e) => {
                    diag(&format!("release of {unit} failed: {e}"));
                    return;
                }
            }
        }
    }

    // ------------------------------------------------------ escalation

    fn escalate(
        &mut self,
        unit: &str,
        branch: &str,
        command: &str,
        output: &str,
    ) -> Result<escalate::Verdict, CoreError> {
        let (journal_tail, fencing) = match &self.inst {
            Some(inst) => {
                let (events, _) = journal::read(&inst.handle())?;
                let tail: Vec<String> = events
                    .iter()
                    .rev()
                    .take(20)
                    .rev()
                    .map(journal::render_line)
                    .collect();
                let fencing = inst
                    .unit(unit)
                    .map(|u| {
                        format!(
                            "status={}, claim_seq={}, lease to {}",
                            u.meta.get_str_or("status", "Draft"),
                            u.meta.get_int("claim_seq"),
                            u.meta.get_str_or("claim_expires", "—")
                        )
                    })
                    .unwrap_or_else(|| "-".to_string());
                (tail.join("\n"), fencing)
            }
            None => (String::new(), "-".to_string()),
        };
        let evidence = escalate::Evidence {
            unit: unit.to_string(),
            branch: branch.to_string(),
            fencing,
            command: command.to_string(),
            output_tail: output.to_string(),
            journal_tail,
        };
        // the escalation role's model/effort from ~/.vitrify/config.toml,
        // appended to the selected harness's recorded spelling
        let (verdict, evidence_path) = escalate::escalate(
            &self.ctx.home,
            VERSION,
            &self.ctx.escalation_argv,
            &evidence,
            &self.ctx.logs_dir,
            self.escalation_index,
        )?;
        self.escalation_index += 1;
        self.state.escalations.push((
            evidence_path.display().to_string(),
            verdict.verdict.as_str().to_string(),
        ));
        emit(&format!(
            "escalation verdict: {} ({})",
            verdict.verdict.as_str(),
            verdict.reason
        ));
        self.event(
            RunEvent::new(
                UtcTime::now(),
                "escalation",
                Some(unit),
                None,
                verdict.reason.clone(),
            )
            .with("verdict", J::s(verdict.verdict.as_str())),
        );
        Ok(verdict)
    }

    /// The orchestrator, not the judge, acts on a verdict. `retry` and
    /// `release` leave the unit in the pool (its claim is already
    /// released); `block` records the question and parks the unit; `abort`
    /// ends the run.
    fn enforce(
        &mut self,
        unit: &str,
        reporter: &str,
        verdict: escalate::Verdict,
    ) -> Result<(), CoreError> {
        match verdict.verdict {
            escalate::VerdictKind::Retry | escalate::VerdictKind::Release => Ok(()),
            escalate::VerdictKind::Block => {
                let q = verdict.q.clone().unwrap_or_else(|| "Q-???".into());
                let target = verdict.unit.clone().unwrap_or_else(|| unit.to_string());
                self.record_question(&q, &target, reporter, &verdict.reason)?;
                self.park(&target, format!("open {q}: {}", verdict.reason))
            }
            escalate::VerdictKind::Abort => {
                self.abort = Some(verdict.reason.clone());
                Ok(())
            }
        }
    }

    // -------------------------------------------------------- recovery

    /// Release the claims of runs that died in this checkout, and save what
    /// their workers had written.
    fn recover_dead_runs(&mut self) -> Result<(), CoreError> {
        self.derive()?;
        let Some(inst) = &self.inst else {
            return Ok(());
        };
        let dead = dead_claims(&self.ctx, inst);
        let mut jobs: Vec<String> = Vec::new();
        for (unit, holder, job, seq) in dead {
            emit(&format!(
                "recovered {unit} (c{seq}): its holder {holder} is gone — claim released"
            ));
            self.event(
                RunEvent::new(
                    UtcTime::now(),
                    "recovered",
                    Some(&unit),
                    Some(seq),
                    format!("its holder {holder} is gone — claim released"),
                )
                .with("holder", J::s(holder.clone())),
            );
            // whatever its worker wrote becomes the next attempt's draft
            let wt = self.ctx.ml.layout.unit_worktree(&self.ctx.slug, &unit, seq);
            retry::abandon(
                &self.ctx.ml.primary,
                &wt,
                &unit,
                seq,
                "interrupted",
                &format!("the run that held this claim ({job}) ended without releasing it"),
            );
            self.release(&unit, &holder);
            if !jobs.contains(&job) {
                jobs.push(job);
            }
        }
        for job in &jobs {
            self.sweep_dead_job(job);
        }
        if !jobs.is_empty() {
            self.dirty = true;
        }
        Ok(())
    }

    /// What a dead run left in `.vitrify/`: its lock, its transaction
    /// worktrees — and possibly workers that outlived it.
    fn sweep_dead_job(&self, job: &str) {
        let scratch = &self.ctx.ml.layout.scratch;
        let _ = std::fs::remove_file(scratch.join("jobs").join(format!("{job}.lock")));
        if let Some((_, pid)) = job.rsplit_once("-p")
            && let Ok(entries) = std::fs::read_dir(scratch)
        {
            let prefix = format!("tx-{pid}-");
            for entry in entries.flatten() {
                if entry.file_name().to_string_lossy().starts_with(&prefix) {
                    let _ = self.ctx.ml.primary.worktree_remove(&entry.path());
                }
            }
        }
        // a run killed outright cannot stop its workers; say so
        let record = scratch.join("logs").join(job).join("run.json");
        let Ok(text) = std::fs::read_to_string(record) else {
            return;
        };
        let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) else {
            return;
        };
        for w in v["workers"].as_array().into_iter().flatten() {
            if w["outcome"] == "running"
                && let Some(pid) = w["pid"].as_u64()
                && gate::alive(pid as u32, true)
            {
                diag(&format!(
                    "note: a worker of {job} (process group {pid}, unit {}) is still running — stop it with: kill -9 -- -{pid}",
                    w["unit"].as_str().unwrap_or("?")
                ));
            }
        }
    }

    // ----------------------------------------------------- termination

    /// Nothing is running and nothing can be started. Decide whether that
    /// is the end, and which one.
    fn conclude(&mut self) -> Result<Option<End>, CoreError> {
        let Some(inst) = self.inst.clone() else {
            return Ok(None);
        };
        // Verified is a status, not a stamp: a change entry bumps the SDD's
        // version and stales only the units whose reading set it touched
        // (schema §11, §12); the others stay Verified with the stamp of the
        // version they were last built at. Whether each one's trace still
        // matches the current content is the head audit's check 5, below.
        let all_verified = inst.units.iter().all(|u| {
            let status = u.meta.get_str_or("status", "Draft");
            status == "Retired" || status == "Verified"
        });
        if self.ctx.wait {
            if self.checked_head.as_deref() == Some(self.head.as_str()) {
                return Ok(None); // already reported on this head
            }
            if !all_verified {
                self.checked_head = Some(self.head.clone());
                self.report(&inst, None, false, true);
                emit("waiting: the run resumes when the spec changes (interrupt to stop)");
                self.waiting("waiting: the run resumes when the spec changes (interrupt to stop)");
                return Ok(None);
            }
        }

        // the head itself: audit, and — when everything is Verified — the
        // whole suite. The integrator owns the checkout; ask it.
        let _ = self.jobs.send(Job::CheckHead {
            want_suite: all_verified,
        });
        let mut asked_at = Instant::now();
        let check = loop {
            match self.events.recv_timeout(TICK) {
                Ok(Event::HeadChecked(result)) => break (*result)?,
                Ok(other) => self.handle(other)?,
                Err(RecvTimeoutError::Timeout) => {
                    self.integrator_alive()?;
                    // an interrupt stops the commands the check runs, and
                    // the answer follows at once; if it does not, do not
                    // wait for it
                    if signals::pending().is_none() {
                        asked_at = Instant::now();
                    } else if asked_at.elapsed() > Duration::from_secs(10) {
                        break HeadCheck {
                            head: String::new(),
                            findings: Vec::new(),
                            broken: None,
                            suite: None,
                            interrupted: true,
                        };
                    }
                }
                Err(RecvTimeoutError::Disconnected) => {
                    return Err(CoreError::refusal(
                        1,
                        "error: the integrator thread is gone",
                    ));
                }
            }
        };
        if let Some(sig) = signals::pending() {
            return Ok(Some(End::Interrupted(sig)));
        }
        if check.interrupted {
            return Ok(None);
        }
        let (complete, lines) = self.report(&inst, Some(&check), all_verified, false);
        if complete {
            self.keep_report(lines);
            return Ok(Some(End::Complete));
        }
        if self.ctx.wait {
            self.checked_head = Some(self.head.clone());
            emit("waiting: the run resumes when the repository changes (interrupt to stop)");
            self.waiting(
                "waiting: the run resumes when the repository changes (interrupt to stop)",
            );
            return Ok(None);
        }
        self.keep_report(lines);
        Ok(Some(End::Blocked))
    }

    /// Print the termination report; returns whether the done-condition
    /// holds, and the lines printed from the status line on. `check` is the
    /// state of the head (absent when the run is ending without one:
    /// interrupted, aborted, or still waiting).
    fn report(
        &self,
        inst: &Instance,
        check: Option<&HeadCheck>,
        all_verified: bool,
        interim: bool,
    ) -> (bool, Vec<String>) {
        let mut lines: Vec<String> = Vec::new();
        let mut emit = |line: &str| lines.push(line.to_string());
        let (events, _) = journal::read(&inst.handle()).unwrap_or_default();
        let proj = journal::replay(&events);
        // consistent = the journal projects what front matter says (with no
        // events at all, that nothing is beyond Ready)
        let replay_clean =
            proj.order_findings.is_empty() && journal::verify(&proj, &inst.units).is_empty();
        let progress = ledger::progress_line(&inst.units);
        let suite_ok = check
            .and_then(|c| c.suite.as_ref())
            .is_none_or(|(_, passed, _)| *passed);
        let complete = all_verified
            && replay_clean
            && !events.is_empty()
            && suite_ok
            && check.is_some_and(|c| c.findings.is_empty() && c.broken.is_none());

        if complete {
            emit("Complete.");
            emit(&format!("  {progress}"));
            emit("  audit: exit 0 (clean)");
            emit("  replay: exit 0 (consistent)");
            if let Some((cmd, _, _)) = check.and_then(|c| c.suite.as_ref()) {
                emit(&format!("  §5 Test: exit 0 ({cmd})"));
            }
        } else {
            emit(if interim {
                "Blocked (waiting)."
            } else {
                "Blocked."
            });
            emit(&format!("  {progress}"));
            if let Some(c) = check {
                if let Some((cmd, output)) = &c.broken {
                    emit(&format!(
                        "  §5 command failing at the {} head: `{cmd}`: {}",
                        self.ctx.ml.branch,
                        tail(output, 3)
                    ));
                }
                if !c.findings.is_empty() {
                    emit(&format!("  audit: {} finding(s)", c.findings.len()));
                    for f in &c.findings {
                        emit(&format!(
                            "    [check {}] {}: {}",
                            f.check, f.subject, f.cause
                        ));
                    }
                }
                if let Some((cmd, false, output)) = &c.suite {
                    emit(&format!(
                        "  §5 Test failing at the {} head: `{cmd}`: {}",
                        self.ctx.ml.branch,
                        tail(output, 3)
                    ));
                }
            }
            if !replay_clean {
                emit("  replay: inconsistent (see `vitrify journal replay`)");
            }
            for v in &self.views {
                if v.status == "Verified" || v.status == "Retired" {
                    continue;
                }
                emit(&format!("  {}: {}", v.unit, self.blocker(v)));
            }
        }
        // the report ends with the journal tail
        emit("  journal tail:");
        for e in events.iter().rev().take(5).rev() {
            emit(&format!("    {}", journal::render_line(e)));
        }
        out::emit("");
        for line in &lines {
            out::emit(line);
        }
        (complete, lines)
    }

    /// Keep the final report: `report.txt` beside the job's logs, and the
    /// run record's `report` (written with the record's next update).
    fn keep_report(&mut self, lines: Vec<String>) {
        let path = self.ctx.logs_dir.join("report.txt");
        let mut text = lines.join("\n");
        text.push('\n');
        if let Err(e) =
            std::fs::create_dir_all(&self.ctx.logs_dir).and_then(|_| std::fs::write(&path, text))
        {
            diag(&format!("warning: {} not written: {e}", path.display()));
        }
        self.state.report = Some(lines);
    }

    /// Why a unit is not Verified, from the most specific cause down.
    fn blocker(&self, v: &UnitView) -> String {
        let attempts = || {
            self.state
                .attempts
                .iter()
                .filter(|(u, _, _)| u == &v.unit)
                .count()
        };
        if let Some(parked) = self.parked.get(&v.unit) {
            return parked.reason.clone();
        }
        if let Some((_, command, output)) = self
            .state
            .gate_failures
            .iter()
            .rev()
            .find(|(u, _, _)| u == &v.unit)
        {
            return format!("failing gate `{command}`: {}", tail(output, 3));
        }
        // the workers' own accounts: last attempt's outcome + notes
        if let Some((_, outcome, notes)) = self
            .state
            .attempts
            .iter()
            .rev()
            .find(|(u, _, _)| u == &v.unit)
        {
            let n = attempts();
            let first = notes.lines().next().unwrap_or("");
            return if first.is_empty() {
                format!("{n} attempt(s); last outcome {outcome}")
            } else {
                format!("{n} attempt(s); last outcome {outcome} — {first}")
            };
        }
        let unmet = |x: &UnitView| {
            x.depends_on
                .iter()
                .find(|(_, s)| s != "Built" && s != "Verified")
                .cloned()
        };
        if let Some((dep, status)) = unmet(v) {
            let mut chain = vec![format!("{dep} ({status})")];
            let mut cur = dep;
            while let Some((next, status)) =
                self.views.iter().find(|x| x.unit == cur).and_then(unmet)
            {
                if chain.len() > self.views.len() {
                    break; // a cycle lint would have refused; never loop on one
                }
                chain.push(format!("{next} ({status})"));
                cur = next;
            }
            return format!("dependency chain blocked: {}", chain.join(" ← "));
        }
        format!("not attempted this run (status {})", v.status)
    }

    // ---------------------------------------------------------- finish

    /// Stop whatever is still running, hand back every claim, close the
    /// record. Every way out of the loop comes through here.
    fn finish(mut self, end: Result<End, CoreError>) -> Result<u8, CoreError> {
        // The terminal's Ctrl-C reaches git too: a command cut down by the
        // signal fails, and that failure is the interrupt, not an error.
        let end = match (end, signals::pending()) {
            (Err(_), Some(sig)) => Ok(End::Interrupted(sig)),
            (end, _) => end,
        };
        let (label, detail): (&'static str, String) = match &end {
            Ok(End::Complete) => ("complete", "done-condition met".to_string()),
            Ok(End::Blocked) => {
                // the report's progress line, and where the blockers are
                let progress = self
                    .state
                    .report
                    .as_ref()
                    .and_then(|lines| lines.get(1))
                    .map(|l| l.trim())
                    .filter(|l| l.starts_with("Progress:"));
                let detail = match progress {
                    Some(p) => format!("{p} — the report names each blocker"),
                    None => "the report names each blocker".to_string(),
                };
                ("blocked", detail)
            }
            Ok(End::Aborted(reason)) => ("blocked", format!("escalation verdict abort: {reason}")),
            Ok(End::Interrupted(sig)) => ("interrupted", signals::name(*sig).to_string()),
            Err(e) => ("error", e.to_string()),
        };
        let unfinished = !matches!(&end, Ok(End::Complete) | Ok(End::Blocked));
        // What an attempt the run cuts short is recorded as. A run the
        // judge aborted ends "blocked", but its workers were not blocked on
        // anything: they were stopped.
        let cut_short = match &end {
            Ok(End::Aborted(_)) => "aborted",
            _ => label,
        };
        if unfinished {
            self.stop_work(cut_short);
        }
        // the threads end with the run
        let _ = self.jobs.send(Job::Stop);
        self.keeper.stop();
        if let Some(t) = self.integrator.take() {
            let _ = t.join();
        }
        if let Some(t) = self.keeper_thread.take() {
            let _ = t.join();
        }
        // whatever the integrator finished before it stopped
        while let Ok(event) = self.events.try_recv() {
            self.absorb(event);
        }
        if unfinished {
            self.hand_back(cut_short);
        }

        if let Ok(End::Aborted(reason)) = &end {
            emit(&format!("escalation verdict abort — stopping: {reason}"));
            if self.derive_quietly()
                && let Some(inst) = self.inst.clone()
            {
                let (_, lines) = self.report(&inst, None, false, false);
                self.keep_report(lines);
            }
        }
        self.record(Some((label, &detail)));
        self.event(
            RunEvent::new(
                UtcTime::now(),
                "run-ended",
                None,
                None,
                format!("{label}: {detail}"),
            )
            .with("state", J::s(label)),
        );

        // a primary checkout that could not follow gets one more try, and —
        // if it still cannot — the last word on how to bring it up to date
        let behind = {
            let _turn = claim::tx_lock();
            claim::catch_up_primary(&self.ctx.ml.primary, &self.ctx.ml.branch)
        };
        if let Some(warning) = behind {
            diag(&warning);
        }

        // this run's own scratch
        if self.head_wt.exists() {
            let _ = self.ctx.ml.primary.worktree_remove(&self.head_wt);
        }
        drop(self.job_lock.take());
        let _ = std::fs::remove_file(
            self.ctx
                .ml
                .layout
                .scratch
                .join("jobs")
                .join(format!("{}.lock", self.ctx.job_id)),
        );
        match end {
            Ok(End::Complete) => Ok(0),
            Ok(End::Blocked) | Ok(End::Aborted(_)) => Ok(2),
            Ok(End::Interrupted(sig)) => {
                emit("");
                emit(&format!(
                    "Interrupted ({}): workers stopped, claims released, work in progress kept on its branches. Run again to continue.",
                    signals::name(sig)
                ));
                signals::die_by(sig)
            }
            Err(e) => Err(e),
        }
    }

    /// Stop every worker and the gate, and wait for them to say so.
    fn stop_work(&mut self, why: &'static str) {
        signals::request_stop();
        let mut waiting = 0;
        for a in self.active.values_mut() {
            if a.phase == Phase::Working {
                if a.killed.is_none() {
                    a.killed = Some(why);
                }
                gate::kill_group(a.pid);
                waiting += 1;
            }
        }
        // each monitor reports its worker's end
        let deadline = Instant::now() + Duration::from_secs(30);
        while waiting > 0 && Instant::now() < deadline {
            match self.events.recv_timeout(Duration::from_millis(200)) {
                Ok(event) => {
                    if matches!(event, Event::Exited { .. }) {
                        waiting -= 1;
                    }
                    self.absorb(event);
                }
                Err(RecvTimeoutError::Timeout) => {}
                Err(RecvTimeoutError::Disconnected) => break,
            }
        }
    }

    /// An event that arrives while the run is ending. A unit the integrator
    /// did publish is Verified, whatever else is going on; everything else
    /// is left for [`Scheduler::hand_back`].
    fn absorb(&mut self, event: Event) {
        match event {
            // its monitor has seen the worker end; `hand_back` does the rest
            Event::Exited { .. } => {}
            Event::Integrated {
                unit,
                outcome: Outcome::Verified { .. },
            } => {
                self.keeper.drop_unit(&unit);
                if let Some(a) = self.active.get(&unit) {
                    emit(&format!("integrated {unit} (c{})", a.seq));
                    self.state
                        .attempts
                        .push((unit.clone(), "verified".to_string(), String::new()));
                    self.close(&unit, "verified", "");
                }
            }
            _ => {}
        }
    }

    /// Every claim still held goes back: the work to its branch, the unit
    /// to the pool.
    fn hand_back(&mut self, why: &'static str) {
        let units: Vec<String> = self.active.keys().cloned().collect();
        for unit in units {
            let Some(a) = self.active.get(&unit) else {
                continue;
            };
            self.keeper.drop_unit(&unit);
            let detail = match a.phase {
                Phase::Working => "the run ended before the worker finished",
                Phase::Integrating => {
                    "the work was reported built; the run ended before the gate had decided"
                }
            };
            retry::abandon(&self.ctx.ml.primary, &a.wt, &unit, a.seq, why, detail);
            let holder = a.holder.clone();
            self.release(&unit, &holder);
            self.state
                .attempts
                .push((unit.clone(), why.to_string(), detail.to_string()));
            self.close(&unit, why, detail);
        }
    }

    /// A last look at the head for the final report; never fails the exit.
    fn derive_quietly(&mut self) -> bool {
        match self.derive() {
            Ok(_) => true,
            Err(e) => {
                diag(&format!("warning: final state not derived: {e}"));
                self.inst.is_some()
            }
        }
    }
}

impl Drop for Scheduler {
    /// A scheduler that goes away with workers still running — a panic
    /// unwinding through it; `finish` leaves none — stops them. Their
    /// claims are released by the next run, which finds this run's job
    /// lock free.
    fn drop(&mut self) {
        for a in self.active.values() {
            if a.phase == Phase::Working {
                gate::kill_group(a.pid);
            }
        }
    }
}

/// Whether a failed attempt counts toward the unit's limit, and whether
/// reaching the limit calls the escalation judge.
enum Counts {
    /// Not the attempt's doing (the spec changed, the run is ending, a
    /// question is open).
    No,
    /// Counted, but nothing a judge could rule on (a lost race, a lapsed
    /// lease, a taken name).
    Quietly,
    /// The work itself failed.
    Work,
}

/// The integrator thread: every built unit that is waiting, as one batch.
fn integrator_loop(
    integrator: Integrator,
    jobs: Receiver<Job>,
    events: Sender<Event>,
    memo: GateMemo,
) {
    let mut memo = memo;
    let mut carried: Option<Job> = None;
    loop {
        let job = match carried.take() {
            Some(job) => job,
            None => match jobs.recv() {
                Ok(job) => job,
                Err(_) => break,
            },
        };
        match job {
            Job::Stop => break,
            Job::CheckHead { want_suite } => {
                let result = integrator.check_head(&mut memo, want_suite);
                let _ = events.send(Event::HeadChecked(Box::new(result)));
            }
            Job::Integrate(first) => {
                let mut batch = vec![first];
                loop {
                    match jobs.try_recv() {
                        Ok(Job::Integrate(req)) => batch.push(req),
                        Ok(other) => {
                            carried = Some(other);
                            break;
                        }
                        Err(_) => break,
                    }
                }
                match integrator.integrate_batch(&batch, &mut memo, None) {
                    Ok(outcomes) => {
                        for (req, outcome) in batch.iter().zip(outcomes) {
                            let _ = events.send(Event::Integrated {
                                unit: req.unit.clone(),
                                outcome,
                            });
                        }
                    }
                    Err(e) => {
                        let _ = events.send(Event::IntegratorFailed(e.to_string()));
                    }
                }
            }
        }
    }
}

/// Why a unit is Stale, from the journal: the declaring event's cause.
fn stale_cause(inst: &Instance, unit: &str) -> String {
    let (events, _) = journal::read(&inst.handle()).unwrap_or_default();
    for e in events.iter().rev() {
        if e.event == "stale" && e.unit.as_deref() == Some(unit) {
            return e
                .cause
                .clone()
                .unwrap_or_else(|| "one of its inputs changed".to_string());
        }
        if e.event == "change" && e.stale.iter().flatten().any(|u| u == unit) {
            return match &e.entry {
                Some(entry) => format!("change entry {entry}"),
                None => "a change entry".to_string(),
            };
        }
    }
    "one of its inputs changed".to_string()
}

fn render_report_line(r: &worker::Report) -> String {
    format!(
        "{{\"unit\":\"{}\",\"outcome\":\"{}\",\"branch\":\"{}\",\"claim_seq\":{},\"q\":{},\"notes\":\"{}\"}}",
        r.unit,
        r.outcome,
        r.branch,
        r.claim_seq,
        r.q.as_deref()
            .map(|q| format!("\"{q}\""))
            .unwrap_or_else(|| "null".into()),
        r.notes.replace('"', "'")
    )
}

/// Commit the handoff worktree with the fixed build message. Workers never
/// run `git commit`: protocol git operations belong to vitrify (RA-6), and a
/// worker is a harness session whose permission policy may forbid commits
/// anyway — a green build is reported `built` with the work left in place.
/// A clean tree (nothing new) is a no-op; a commit failure is diagnosed and
/// left for the integration gate to reject (the branch tip stays at the
/// claim commit).
fn commit_handoff_if_dirty(ml: &Mainline, wt: &Path, unit: &str, seq: i64) {
    if !wt.is_dir() {
        return;
    }
    let git = ml.primary.at(wt);
    match git.is_dirty() {
        Ok(true) => {}
        _ => return,
    }
    let committed = git
        .add_all()
        .and_then(|()| git.commit(&format!("sdd: build {unit} (c{seq})")));
    match committed {
        Ok(()) => diag(&format!("committed handoff worktree for {unit} (c{seq})")),
        Err(e) => diag(&format!("handoff commit for {unit} (c{seq}) failed: {e}")),
    }
}