vitrify-cli 0.7.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
//! A lane: one SDD's part of a run.
//!
//! Each lane has what building one SDD needs for itself: its pinned SDD
//! and the [`Mainline`] handle that names it, its own integration
//! checkout (under its own lock) and the integrator thread that owns it,
//! its lease keeper, its head checkout, and the state the scheduler keeps
//! for it — the claims it holds, the units it has set aside, the failures
//! it has counted, the view it last derived from mainline. Its records are
//! under `.vitrify/logs/<job>/<slug>/`.
//!
//! The lane decides nothing on its own: the scheduler owns every decision,
//! and the lane's threads — its integrator, its workers' monitors, its
//! culprit searches — only report, each message naming the lane it belongs
//! to.
//!
//! A lane reads the whole repository's unit graph at the head it derives
//! from, so a unit that depends on another SDD's units (through a pin) is
//! claimable only once they are built, whichever SDDs the run builds.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::Duration;

use vitrify_core::CoreError;
use vitrify_core::claims::Op;
use vitrify_core::graph::{self as unit_graph, Node, UnitGraph};
use vitrify_core::model::{self, Instance, UnitView, UtcTime};
use vitrify_core::trace::{self, TraceMap};

use super::events::{EventLog, RunEvent};
use super::integrate::{
    self, GateMemo, HeadCheck, IntegrateRequest, Integrator, Outcome, Rechecked, RottenRow,
};
use super::lease::{self, Keeper, MonitorParams, MonitorVerdict};
use super::mainline::Mainline;
use super::progress::GateBoard;
use super::project::{self, LaneState, outside_blocker};
use super::{Ctx, LaneSeed, tail};
use crate::cmd::{self, claim};
use crate::gate;
use crate::incremental::culprit;
use crate::incremental::dirty::{self, SpecFiles};
use crate::out::{diag, emit};

/// A lane's place in the scheduler's list of lanes.
pub(super) type LaneId = usize;

/// What a lane's threads tell the scheduler, each naming its lane.
pub(super) enum Event {
    /// A worker's process ended (or was ended).
    Exited {
        lane: LaneId,
        unit: String,
        verdict: MonitorVerdict,
        expires: UtcTime,
    },
    Integrated {
        lane: LaneId,
        unit: String,
        outcome: Outcome,
    },
    /// The integrator hit an error that is not a verdict on any unit.
    IntegratorFailed { lane: LaneId, error: String },
    HeadChecked {
        lane: LaneId,
        check: Box<Result<HeadCheck, CoreError>>,
    },
    /// The integrator found rows that fail on their gate's base too.
    Rotten { lane: LaneId, rows: Vec<RottenRow> },
    /// The lane's dynamic preflight ended: the failures, or `None` when it
    /// was interrupted.
    Prepared {
        lane: LaneId,
        result: Box<Result<Option<Vec<String>>, CoreError>>,
    },
    /// The lane's checkouts are gone (`run.lane_cap`).
    Released { lane: LaneId },
    /// A re-check batch ended.
    Rechecked {
        lane: LaneId,
        result: Box<Result<Rechecked, CoreError>>,
    },
    /// A culprit search for a rotten unit's row ended.
    Culprit {
        lane: LaneId,
        unit: String,
        report: Result<culprit::Report, String>,
    },
}

/// What the scheduler asks of a lane's integrator.
pub(super) enum Job {
    Integrate(IntegrateRequest),
    /// Check the head; then, with `release`, give the checkouts back.
    CheckHead {
        want_suite: bool,
        release: bool,
    },
    /// Make the lane's checkout and run its §5 Bootstrap, Build and Lint
    /// there: its dynamic preflight, at its first build or re-check.
    Prepare,
    /// Remove the lane's checkouts, so another lane can be admitted.
    Release,
    /// Check the unit's rows at the lane's gates again: it is no longer
    /// held rotten.
    Forget(String),
    /// Re-check these dirty Verified units: one batch, no worker.
    Recheck(Vec<String>),
    Stop,
}

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

/// One claim this run holds.
pub(super) struct Active {
    pub holder: String,
    pub seq: i64,
    pub branch: String,
    pub wt: PathBuf,
    /// The worker's process group; `None` for an adopted attempt, which
    /// has no worker.
    pub pid: Option<u32>,
    pub out_path: PathBuf,
    /// The worker's temp directory: this claim's alone until it is closed.
    pub _tmp: Option<gate::TmpSlot>,
    /// What the unit's inputs hashed to when it was claimed.
    pub trace: TraceMap,
    pub phase: Phase,
    /// Index of this attempt in `RunState::workers`.
    pub rec: usize,
    /// Why the scheduler ended the worker itself, if it did.
    pub 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.
pub(super) struct Parked {
    pub reason: String,
    pub trace: TraceMap,
}

/// Where a lane's checkouts stand. All but `Absent` count against
/// `run.lane_cap`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Checkout {
    /// None on disk; the lane is not admitted.
    Absent,
    /// Its dynamic preflight is running, in a checkout made for it.
    Preparing,
    /// On disk and usable.
    Ready,
    /// Being removed.
    Releasing,
}

/// A unit whose rows fail on the base of the gate that ran them: something
/// already on mainline broke it, or the rows are flaky. It is reported,
/// never rebuilt; it and the units that depend on it count as not built
/// while it fails.
#[derive(Default)]
pub(super) struct Rotten {
    /// Its failing rows, as `row <n>: \`<command>\``.
    pub rows: Vec<String>,
    /// The culprit searches' report lines, as they ended.
    pub findings: Vec<String>,
}

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

pub(super) struct Lane {
    /// The SDD, pinned: path, slug, and how mainline is reached.
    pub ml: Mainline,
    /// `.vitrify/logs/<job>/<slug>/`: worker logs, prompts, the gate
    /// board, escalations.
    pub logs_dir: PathBuf,
    /// Workers at once in this lane (the run's cap bounds them all).
    pub cap: usize,
    /// Where the lane stands now; at the end, how it ended.
    pub state: LaneState,
    /// A project run: the lane's lines on stdout name it.
    pub project: bool,
    /// Its checkouts, as `run.lane_cap` counts them.
    pub checkout: Checkout,
    /// Its dynamic preflight has run (once per run).
    pub prepared: bool,
    /// Why its preflight failed (a project run): the lane is blocked.
    pub blocked: Vec<String>,
    /// The priority its gate commands wait for the gate pool at: its
    /// highest-priority unfinished unit's.
    pub priority: Arc<AtomicI64>,
    /// A re-check batch is running, of these units.
    pub rechecking: Option<Vec<String>>,
    /// Units already re-checked at a head, so a batch whose passes could
    /// not be recorded is not run again on the same tree.
    pub rechecked_at: BTreeMap<String, String>,
    /// This run: units built by workers and landed; units adopted and
    /// landed; units re-checked.
    pub built: usize,
    pub adopted: usize,
    pub rechecked: usize,
    /// Units this run has checked — re-checked, found rotten, or landed:
    /// what `--recheck` no longer distrusts.
    pub checked: BTreeSet<String>,
    /// The §5 Test command has run in this run (a gate or a re-check).
    pub suite_ran: bool,
    /// To the lane's integrator.
    pub jobs: Sender<Job>,
    pub integrator: Option<JoinHandle<()>>,
    pub keeper: Arc<Keeper>,
    pub keeper_thread: Option<JoinHandle<()>>,
    /// A detached checkout of the mainline head, for reading spec state.
    pub head_wt: PathBuf,
    pub active: BTreeMap<String, Active>,
    pub 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.
    pub fails: BTreeMap<String, Fails>,
    /// (unit, command, output) of each gate that turned a unit away.
    pub gate_failures: Vec<(String, String, String)>,
    /// (unit, final outcome label, detail) of each attempt that ended.
    pub attempts: Vec<(String, String, String)>,
    pub escalation_index: u32,
    pub inst: Option<Instance>,
    pub views: Vec<UnitView>,
    pub head: String,
    /// The head up to which staleness has been applied.
    pub healed_at: String,
    /// The head the last termination check ran on (`--wait`).
    pub checked_head: Option<String>,
    pub audit_notes: Vec<String>,
    /// Every SDD of the repository at `graph_at`, and their unit graph
    /// (`None` when it could not be built).
    pub sdds: Vec<Instance>,
    pub graph: Option<UnitGraph>,
    graph_at: String,
    /// Units held rotten, by id.
    pub rotten: BTreeMap<String, Rotten>,
    /// Culprit searches running for this lane.
    pub searches: usize,
    /// Held by the culprit search probing the lane's probe checkout.
    pub probe_turn: Arc<Mutex<()>>,
    /// Units found not adoptable since the spec last moved.
    pub not_adoptable: BTreeSet<String>,
    /// Rows the preflight re-check found failing, for the scheduler to take
    /// up once the lane has a view.
    pub preflight_rotten: Vec<RottenRow>,
    /// The job's event log.
    log: EventLog,
}

impl Lane {
    /// Start lane `id` from what preflight established: its records
    /// directory, its integrator thread, its lease keeper.
    pub fn start(
        id: LaneId,
        seed: LaneSeed,
        ctx: &Ctx,
        events: Sender<Event>,
        log: EventLog,
        params: &MonitorParams,
    ) -> Result<Lane, CoreError> {
        let ml = seed.ml;
        let logs_dir = ctx.layout.lane_logs(&ctx.job_id, &ml.slug);
        std::fs::create_dir_all(&logs_dir).map_err(|source| CoreError::Io {
            path: logs_dir.clone(),
            source,
        })?;
        let (jobs, jobs_rx) = mpsc::channel::<Job>();
        let rotten_events = events.clone();
        let integrator = Integrator::new(ml.clone(), ctx.cfg.run.rows_at_once())
            .with_board(
                GateBoard::to(logs_dir.join("gate.json"))
                    .with_events(log.clone())
                    .for_lane(&ml.slug),
            )
            .with_rotten_sink(move |rows| {
                let _ = rotten_events.send(Event::Rotten { lane: id, rows });
            });
        let memo = GateMemo::default();
        let preflight_rotten = seed.rotten;
        let priority = Arc::new(AtomicI64::new(0));
        let lane_priority = Arc::clone(&priority);
        // rows preflight found failing are not run again while they fail
        for r in &preflight_rotten {
            integrator.hold_rotten(&r.unit);
        }
        let integrator = std::thread::spawn(move || {
            integrator_loop(id, integrator, jobs_rx, events, memo, &lane_priority)
        });

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

        Ok(Lane {
            // a project run's lanes read one head checkout, made once per
            // head: they all derive from the same mainline commit
            head_wt: if ctx.project {
                ctx.layout.head_checkout("project")
            } else {
                ctx.layout.head_checkout(&ml.slug)
            },
            ml,
            logs_dir,
            cap: seed.cap,
            state: if seed.blocked.is_empty() {
                LaneState::Build
            } else {
                LaneState::Blocked
            },
            project: ctx.project,
            // one SDD's run made its checkout at preflight; a project run's
            // lanes make theirs when admitted
            checkout: if ctx.project {
                Checkout::Absent
            } else {
                Checkout::Ready
            },
            prepared: !ctx.project,
            blocked: seed.blocked,
            priority,
            rechecking: None,
            rechecked_at: BTreeMap::new(),
            built: 0,
            adopted: 0,
            rechecked: 0,
            checked: seed.checked.iter().cloned().collect(),
            suite_ran: seed.suite_ran,
            jobs,
            integrator: Some(integrator),
            keeper,
            keeper_thread: Some(keeper_thread),
            active: BTreeMap::new(),
            parked: BTreeMap::new(),
            fails: BTreeMap::new(),
            gate_failures: Vec::new(),
            attempts: Vec::new(),
            escalation_index: 0,
            inst: None,
            views: Vec::new(),
            head: String::new(),
            healed_at: String::new(),
            checked_head: None,
            audit_notes: Vec::new(),
            sdds: Vec::new(),
            graph: None,
            graph_at: String::new(),
            rotten: BTreeMap::new(),
            searches: 0,
            probe_turn: Arc::new(Mutex::new(())),
            not_adoptable: BTreeSet::new(),
            preflight_rotten,
            log,
        })
    }

    /// The SDD's path from the repository root, as `--sdd` takes it.
    pub fn sdd(&self) -> String {
        crate::cmd::sdd_arg(&self.ml.sdd_rel)
    }

    /// Workers of this lane running now (a unit waiting for the gate holds
    /// a lease, not a slot).
    pub fn working(&self) -> usize {
        self.active
            .values()
            .filter(|a| a.phase == Phase::Working)
            .count()
    }

    fn event(&self, e: RunEvent) {
        self.log
            .write(&e.with("lane", crate::out::J::s(self.ml.slug.clone())));
    }

    /// `unit` is no longer held rotten — it was rebuilt, landed Verified, or
    /// its row was found flaky: its rows run at the lane's gates again, it
    /// may be re-checked again, and what depends on it is no longer held.
    pub fn unrot(&mut self, unit: &str) {
        if self.rotten.remove(unit).is_some() {
            let _ = self.jobs.send(Job::Forget(unit.to_string()));
        }
        self.rechecked_at.remove(unit);
    }

    /// A line of this lane's for stdout: in a project run it names the lane.
    pub fn say(&self, line: &str) -> String {
        if self.project {
            project::event_line(&self.ml.slug, line)
        } else {
            line.to_string()
        }
    }

    /// The SDD's slug as its `sdd:` front matter names it: how the unit
    /// graph names its units.
    pub fn graph_slug(&self) -> String {
        self.inst
            .as_ref()
            .map(|i| unit_graph::slug_of(i).to_string())
            .unwrap_or_else(|| self.ml.slug.clone())
    }

    /// `unit` of this lane, as the unit graph names it.
    pub fn node(&self, unit: &str) -> Node {
        Node::new(self.graph_slug(), unit)
    }

    /// The units `unit` depends on, directly or transitively, in any SDD:
    /// from the unit graph, else from its own SDD's `depends_on`.
    pub fn closure(&self, unit: &str) -> BTreeSet<Node> {
        let mut out: BTreeSet<Node> = BTreeSet::new();
        let mut todo = vec![self.node(unit)];
        while let Some(n) = todo.pop() {
            let deps: Vec<Node> = match &self.graph {
                Some(g) => g.dependencies(&n).into_iter().cloned().collect(),
                None if n.slug == self.graph_slug() => self
                    .views
                    .iter()
                    .find(|v| v.unit == n.unit)
                    .map(|v| {
                        v.depends_on
                            .iter()
                            .map(|(d, _)| Node::new(n.slug.clone(), d.clone()))
                            .collect()
                    })
                    .unwrap_or_default(),
                None => Vec::new(),
            };
            for d in deps {
                if out.insert(d.clone()) {
                    todo.push(d);
                }
            }
        }
        out
    }

    /// The first unit, in any SDD, that `unit` depends on and that is
    /// rotten (`rotten` holds every lane's).
    pub fn rotten_dependency(&self, unit: &str, rotten: &BTreeSet<Node>) -> Option<Node> {
        if rotten.is_empty() {
            return None;
        }
        self.closure(unit).into_iter().find(|n| rotten.contains(n))
    }

    /// Each unit's view of `inst`, claimability through the unit graph.
    fn compute_views(&mut self, inst: &Instance) {
        let now = UtcTime::now();
        let slug = unit_graph::slug_of(inst).to_string();
        self.views = inst
            .units
            .iter()
            .map(|u| match &self.graph {
                Some(g) => {
                    let node = Node::new(slug.clone(), u.unit.clone());
                    model::unit_view_with(u, &inst.units, now, &g.resolver(&node))
                }
                None => model::unit_view(u, &inst.units, now),
            })
            .collect();
        self.views.sort_by(|a, b| a.unit.cmp(&b.unit));
    }

    /// Take the repository's SDDs and unit graph from `other`, read at a
    /// head this lane has not read yet: one read per head serves every
    /// lane.
    pub fn share_graph(&mut self, other: &Lane) {
        if !other.graph_at.is_empty() && other.graph_at != self.graph_at {
            self.sdds = other.sdds.clone();
            self.graph = other.graph.clone();
            self.graph_at = other.graph_at.clone();
        }
    }

    /// Every SDD of the repository at `head`, as the head checkout holds
    /// it, and their unit graph: built again only when the head moved.
    fn read_graph(&mut self, head: &str) {
        if self.graph_at == head {
            return;
        }
        // claims, renewals and releases move the head and nothing the graph
        // is built from: no need to read it all again
        if !self.graph_at.is_empty()
            && self.graph.is_some()
            && dirty::benign(
                &self.ml.primary,
                &self.graph_at,
                head,
                &SpecFiles::of(&self.head_wt, &self.sdds),
            )
            .unwrap_or(false)
        {
            self.graph_at = head.to_string();
            return;
        }
        self.sdds = cmd::discover(&self.head_wt)
            .into_iter()
            .filter_map(|h| Instance::load(h).ok())
            .collect();
        self.graph = match UnitGraph::build(&self.sdds) {
            Ok(g) => Some(g),
            Err(e) => {
                diag(&format!(
                    "note: the repository's unit graph could not be read ({e}); units that depend on other SDDs wait"
                ));
                None
            }
        };
        self.graph_at = head.to_string();
    }

    /// 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.
    pub fn derive(&mut self) -> Result<bool, CoreError> {
        let mut head = self.ml.head()?;
        // the same head: only the clock has moved (a lease may have lapsed)
        if head == self.head
            && let Some(inst) = self.inst.take()
        {
            self.compute_views(&inst);
            self.inst = Some(inst);
            return Ok(true);
        }
        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.ml, &self.healed_at, &head)? {
                // what was not adoptable may be now, and the other way round
                self.not_adoptable.clear();
                let healed = match self.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 {
                    self.unrot(unit);
                    emit(&self.say(&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.ml.head()?;
                }
            }
            self.healed_at = head.clone();
        }
        {
            // adding the head checkout is one more worktree: its turn like
            // any other; resetting it touches nothing another writer uses
            let made = self.head_wt.join(".git").exists();
            // another lane of the run brought it to this head already
            if !made || !head_checkout_at(&self.head_wt, &head) {
                let _turn = (!made).then(claim::tx_lock);
                self.ml.pristine(&self.head_wt, &head)?;
                head_checkout_made(&self.head_wt, &head);
            }
        }
        let inst = self.ml.sdd_in(&self.head_wt)?;
        self.read_graph(&head);
        self.compute_views(&inst);

        // 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(&self.say(&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(pid) = a.pid
                && 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.ml.branch
                ));
                a.killed = Some("stale-inputs");
                stale.push(pid);
            }
        }
        for pid in stale {
            gate::kill_group(pid);
        }

        self.inst = Some(inst);
        self.head = head;
        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.
    pub 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(())
    }

    /// Stop dispatching `unit` until its reading set changes.
    pub 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.
    pub fn record_question(
        &self,
        q: &str,
        unit: &str,
        reporter: &str,
        notes: &str,
    ) -> Result<(), CoreError> {
        match self.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,
        }
    }

    /// Give `holder`'s claim on `unit` back.
    pub fn release(&self, unit: &str, holder: &str, lease_minutes: i64) {
        // 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.ml.claim(Op::Release, unit, holder, lease_minutes) {
                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;
                }
            }
        }
    }

    /// Why a unit is not Verified, from the most specific cause down.
    /// `rotten` holds every lane's rotten units; `lanes` the slugs the run
    /// builds.
    pub fn blocker(&self, v: &UnitView, rotten: &BTreeSet<Node>, lanes: &[String]) -> String {
        let attempts = || {
            self.attempts
                .iter()
                .filter(|(u, _, _)| u == &v.unit)
                .count()
        };
        if let Some(parked) = self.parked.get(&v.unit) {
            return parked.reason.clone();
        }
        if let Some(r) = self.rotten_dependency(&v.unit, rotten) {
            let own = r.slug == self.graph_slug();
            return format!(
                "waits on {} (rotten)",
                if own { r.unit.clone() } else { r.to_string() }
            );
        }
        if let Some((_, command, output)) = self
            .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.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()
        };
        // waiting on units of other SDDs, through a pin
        let waits_outside = v.through.iter().any(|t| {
            v.depends_on
                .iter()
                .any(|(d, s)| d == &t.entry && s != "Built" && s != "Verified")
        });
        if waits_outside && let Some(g) = &self.graph {
            let node = self.node(&v.unit);
            let status = |n: &Node| g.status(n).map(str::to_string);
            if let Some(line) = outside_blocker(g, lanes, &node, &status) {
                let line = line
                    .strip_prefix(&format!("{} ", v.unit))
                    .unwrap_or(&line)
                    .to_string();
                return line;
            }
            let pending: Vec<String> = v
                .through
                .iter()
                .flat_map(|t| t.units.iter())
                .filter(|(_, s)| s != "Built" && s != "Verified")
                .map(|(u, s)| format!("{u} ({s})"))
                .collect();
            return format!("waits on {}", pending.join(", "));
        }
        // a link of the chain: a unit with its status, or a pin entry as
        // what it resolved to — the chain ends there
        let link = |x: &UnitView, dep: &str, status: &str| -> (String, bool) {
            let Some(t) = x.through.iter().find(|t| t.entry == dep) else {
                return (format!("{dep} ({status})"), false);
            };
            if let Some(g) = &self.graph {
                let node = self.node(&x.unit);
                let status = |n: &Node| g.status(n).map(str::to_string);
                if let Some(line) = outside_blocker(g, lanes, &node, &status)
                    && let Some(rest) = line.strip_prefix(&format!("{} waits on ", x.unit))
                {
                    return (format!("{dep} → {rest}"), true);
                }
            }
            let pending: Vec<String> = t
                .units
                .iter()
                .filter(|(_, s)| s != "Built" && s != "Verified")
                .map(|(u, s)| format!("{u} ({s})"))
                .collect();
            (format!("{dep} → {}", pending.join(", ")), true)
        };
        if let Some((dep, status)) = unmet(v) {
            let (first, mut ended) = link(v, &dep, &status);
            let mut chain = vec![first];
            let mut cur = dep;
            while !ended
                && let Some(x) = self.views.iter().find(|x| x.unit == cur)
                && let Some((next, status)) = unmet(x)
            {
                if chain.len() > self.views.len() {
                    break; // a cycle lint would have refused; never loop on one
                }
                let (text, pin) = link(x, &next, &status);
                chain.push(text);
                ended = pin;
                cur = next;
            }
            return format!("dependency chain blocked: {}", chain.join(" ← "));
        }
        format!("not attempted this run (status {})", v.status)
    }

    /// End the lane's threads: the integrator after whatever it is doing,
    /// the keeper at once.
    pub fn stop_threads(&mut self) {
        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();
        }
    }

    /// Remove the lane's head checkout: this run's own scratch.
    pub fn remove_head_checkout(&self) {
        if self.head_wt.exists() {
            let _ = self.ml.primary.worktree_remove(&self.head_wt);
        }
    }
}

/// The head each head checkout was last made pristine at, in this process.
static HEAD_CHECKOUTS: std::sync::Mutex<Vec<(PathBuf, String)>> = std::sync::Mutex::new(Vec::new());

/// Whether the head checkout at `path` was made pristine at `head` last.
fn head_checkout_at(path: &Path, head: &str) -> bool {
    HEAD_CHECKOUTS
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .iter()
        .any(|(p, h)| p == path && h == head)
}

fn head_checkout_made(path: &Path, head: &str) {
    let mut made = HEAD_CHECKOUTS.lock().unwrap_or_else(|e| e.into_inner());
    made.retain(|(p, _)| p != path);
    made.push((path.to_path_buf(), head.to_string()));
}

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

/// `run-<stamp>-p<pid>-w<n>` (a worker's claim) or `…-a<n>` (an adopted
/// attempt's) → (`run-<stamp>-p<pid>`, pid).
fn run_holder(holder: &str) -> Option<(String, u32)> {
    let (job, w) = holder
        .rsplit_once("-w")
        .or_else(|| holder.rsplit_once("-a"))?;
    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 on `inst`'s units held by runs that are gone: (unit, holder,
/// job, claim_seq).
pub(super) 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.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(super) 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()
}

// ---------------------------------------------------------- threads

/// The lease backend: claim transactions on one lane's SDD.
pub(super) struct RealLease {
    pub ml: Mainline,
    pub 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()),
        }
    }
}

/// The integrator thread of lane `lane`: every built unit that is waiting,
/// as one batch.
fn integrator_loop(
    lane: LaneId,
    integrator: Integrator,
    jobs: Receiver<Job>,
    events: Sender<Event>,
    memo: GateMemo,
    priority: &AtomicI64,
) {
    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::Forget(unit) => integrator.forget_rotten(&unit),
            Job::Prepare => {
                let mut failures = Vec::new();
                let result = gate::at_priority(priority.load(Ordering::Relaxed), || {
                    super::dynamic_checks(&integrator.ml, &mut failures, &|| {
                        diag(
                            "note: another vitrify process is integrating this SDD — waiting for its integration checkout",
                        )
                    })
                })
                .map(|done| done.map(|_| failures));
                let _ = events.send(Event::Prepared {
                    lane,
                    result: Box::new(result),
                });
            }
            Job::Release => {
                integrator.ml.remove_checkouts();
                let _ = events.send(Event::Released { lane });
            }
            Job::Recheck(units) => {
                let result = gate::at_priority(priority.load(Ordering::Relaxed), || {
                    integrator.recheck(&units)
                });
                let _ = events.send(Event::Rechecked {
                    lane,
                    result: Box::new(result),
                });
            }
            Job::CheckHead {
                want_suite,
                release,
            } => {
                let check = gate::at_priority(priority.load(Ordering::Relaxed), || {
                    integrator.check_head(&mut memo, want_suite)
                });
                if release {
                    integrator.ml.remove_checkouts();
                }
                let _ = events.send(Event::HeadChecked {
                    lane,
                    check: Box::new(check),
                });
            }
            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,
                    }
                }
                let done = gate::at_priority(priority.load(Ordering::Relaxed), || {
                    integrator.integrate_batch(&batch, &mut memo, None)
                });
                match done {
                    Ok(outcomes) => {
                        // what the gate found rotten, before what it decided
                        let rows = integrator.take_rotten();
                        if !rows.is_empty() {
                            let _ = events.send(Event::Rotten { lane, rows });
                        }
                        for (req, outcome) in batch.iter().zip(outcomes) {
                            let _ = events.send(Event::Integrated {
                                lane,
                                unit: req.unit.clone(),
                                outcome,
                            });
                        }
                    }
                    Err(e) => {
                        let _ = events.send(Event::IntegratorFailed {
                            lane,
                            error: e.to_string(),
                        });
                    }
                }
            }
        }
    }
}