Skip to main content

greengate_runtime/
live.rs

1//! One check, one block — and while it runs, one line: per-check output
2//! capture plus a live progress region for the concurrent stage.
3//!
4//! Twenty checks used to print straight to inherited stdio from their own
5//! threads, so two failing linters shuffled their lines together and the
6//! reader un-shuffled them by hand — the dispatcher's roll-up existed partly
7//! to apologise for it. Now every check writes into its own slot, and a
8//! completed check's output reaches stdout as ONE locked write: contiguous,
9//! whatever the other nineteen were doing.
10//!
11//! Three writers feed a slot:
12//!
13//! 1. The check's own thread, through [`say`] — which is what
14//!    `common::ok/fail/warn` call. A thread with no slot installed (commit-msg,
15//!    `amont install`, the dispatcher itself) prints directly, exactly as
16//!    before; nothing outside a stage changes.
17//! 2. A captured child's reader threads, through [`Stage::append_raw`] —
18//!    they are not the check's thread, so the thread-local cannot carry the
19//!    routing; the `Arc` is captured before the spawn instead.
20//! 3. Nobody else. The dispatcher's own lines (skips, pins, the roll-up)
21//!    happen strictly before or after the fan-out and stay direct.
22//!
23//! Order across checks is COMPLETION order — deterministic per block, not
24//! per stage, which is the same nondeterminism the interleaved version had
25//! without the shuffling. `amont.progress false` switches the whole
26//! mechanism off and restores raw streaming for anyone who wants to watch a
27//! tool write in real time.
28//!
29//! # The region
30//!
31//! When stderr is a real terminal ([`watching`]) the stage also paints a
32//! live region UNDER the finished blocks: one line per running check —
33//! braille spinner, name, elapsed — repainted every 80ms by a ticker
34//! thread, shrinking as checks finish, gone without a trace when the stage
35//! ends. Blocks go to stdout, the region to stderr; both feed one tty, and
36//! every write to either happens under the same [`Stage::out`] lock, so a
37//! block never tears a repaint in half. Piped, redirected, `TERM=dumb`, or
38//! CI: [`watching`] is false, no ticker starts, and the region costs
39//! nothing — which is also why the test suite (piped stdio throughout)
40//! exercises capture but never the paint.
41
42use std::cell::RefCell;
43use std::io::{IsTerminal, Write};
44use std::sync::atomic::{AtomicBool, Ordering};
45use std::sync::{Arc, Mutex, Weak};
46use std::time::Instant;
47
48/// The fleet spinner's frames (progress.rs) — cycled by elapsed time, so a
49/// frame needs no state beyond the clock.
50const FRAMES: [char; 10] = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
51
52/// The region never grows past this many check lines; the rest fold into
53/// one `… and N more`. Twelve is the whole default fleet on one screen.
54const MAX_LINES: usize = 12;
55
56/// One check's place in the stage.
57struct Slot {
58    /// Sanitised at [`Stage::begin`]: a manifest-declared name is
59    /// repo-derived text and the region writes it to a live terminal.
60    name: String,
61    /// Restamped by [`Stage::enter`], so a serial stage (pre-push) times
62    /// each check from its own start, not the stage's.
63    started: Instant,
64    /// The last byte or line that landed in `buf` — what the region's
65    /// `quiet` figure and the heartbeat's `last output` read.
66    last_output: Instant,
67    /// Elapsed seconds at which the non-tty heartbeat next speaks.
68    next_beat: u64,
69    buf: Vec<u8>,
70    /// Entered and not yet finished — the region shows exactly these.
71    running: bool,
72    done: bool,
73    /// The command this check is waiting on, while it runs: its output
74    /// clock and what its CPU is doing — the same object the kill decision
75    /// reads, so the displays can never disagree with it (ADR-0008).
76    activity: Option<Arc<crate::hooks::common::Activity>>,
77    /// Waiting for a host slot since, and how many slots there are
78    /// (ADR-0009).
79    queued: Option<(Instant, u64)>,
80    /// Under quiet: where the tool's output goes instead of `buf` (the whole
81    /// of it to a log, a bounded excerpt in memory). `buf` keeps the check's
82    /// own messages. See [`crate::capture`].
83    capture: Option<crate::capture::Capture>,
84    /// The exit code of the last tool the check ran, for the failure line.
85    runner_exit: Option<i32>,
86    /// The `running <check>, log <path>` line has been printed.
87    announced: bool,
88}
89
90/// A quiet run says where its log is only once it has taken this long, so a
91/// quick pass is exactly one line even under `2>&1`.
92const ANNOUNCE_SECS: u64 = 5;
93
94/// A running stage: the slots, and the one lock every terminal write inside
95/// the stage goes through.
96pub struct Stage {
97    slots: Mutex<Vec<Slot>>,
98    /// Serialises block emission and region repaints; the value is how many
99    /// region lines are currently painted (what an erase must remove).
100    out: Mutex<usize>,
101    /// Painting at all? [`enabled`] && [`watching`], decided once at begin.
102    live: bool,
103    /// Is this the PUSH stage? Read by the heartbeat, which has something to
104    /// say about a long gate there and nothing to say about one at commit
105    /// time — see [`beat_line`]. Derived from the names, which already
106    /// carry the trigger.
107    on_push: bool,
108    /// [`quiet`], decided once at begin: tool output is captured to a log and
109    /// a passing check prints one line.
110    quiet: bool,
111    log_dir: Option<std::path::PathBuf>,
112    /// A check run by name (`amont run <check>`): its pass is one `ok` line.
113    /// Inside a stage a pass prints nothing, like every other success line
114    /// under quiet — the stage's tally speaks for it — and its log stays.
115    named: AtomicBool,
116    stop: AtomicBool,
117}
118
119thread_local! {
120    /// Where [`say`] routes on THIS thread: a stage and a slot index.
121    static SINK: RefCell<Option<(Arc<Stage>, usize)>> = const { RefCell::new(None) };
122}
123
124impl Stage {
125    /// A stage over `names`, in dispatch order. Does nothing visible until
126    /// checks start entering (the region) or finishing (the blocks).
127    pub fn begin(settings: &crate::config::Settings, names: &[&str]) -> Arc<Stage> {
128        let now = Instant::now();
129        let stage = Arc::new(Stage {
130            slots: Mutex::new(
131                names
132                    .iter()
133                    .map(|n| Slot {
134                        // Every name in a stage carries the stage's own
135                        // prefix ("pre-commit-clippy"); the region drops it
136                        // — twelve identical prefixes say nothing.
137                        name: crate::ui::sanitize(
138                            n.strip_prefix("pre-commit-")
139                                .or_else(|| n.strip_prefix("pre-push-"))
140                                .unwrap_or(n),
141                        ),
142                        started: now,
143                        last_output: now,
144                        next_beat: HEARTBEAT_SECS,
145                        buf: Vec::new(),
146                        running: false,
147                        done: false,
148                        activity: None,
149                        queued: None,
150                        capture: None,
151                        runner_exit: None,
152                        announced: false,
153                    })
154                    .collect(),
155            ),
156            out: Mutex::new(0),
157            live: enabled(settings) && watching(),
158            // The names arrive fully qualified and the loop above has
159            // already had to strip the trigger to display them, so the
160            // stage can answer this without dispatch passing anything in.
161            on_push: holds_a_push_connection(names, crate::rehearsal::in_snapshot()),
162            quiet: quiet(settings),
163            named: AtomicBool::new(false),
164            log_dir: if quiet(settings) {
165                crate::capture::log_dir()
166            } else {
167                None
168            },
169            stop: AtomicBool::new(false),
170        });
171        if stage.live {
172            // The ticker holds a Weak: the stage dropping is what ends it,
173            // so a paint can never outlive the region's owner.
174            let weak = Arc::downgrade(&stage);
175            let own = settings.for_thread();
176            let _ = std::thread::Builder::new()
177                .name("amont-live".into())
178                .spawn(move || tick(own, weak));
179        } else if enabled(settings) || stage.quiet {
180            // Nobody is watching a terminal — an agent, CI, a pipe — and a
181            // captured check shows nothing until it finishes. The heartbeat
182            // is the one line a minute that says it is alive, which is the
183            // difference between "wait" and "kill it" for whoever is on the
184            // other end of the pipe.
185            let weak = Arc::downgrade(&stage);
186            let own = settings.for_thread();
187            let _ = std::thread::Builder::new()
188                .name("amont-heartbeat".into())
189                .spawn(move || heartbeat(own, weak));
190        }
191        stage
192    }
193
194    /// Route this thread's [`say`] calls into slot `idx` until the guard
195    /// drops. Installed by the dispatcher around each `check.run`. Also
196    /// starts the slot's clock and puts it in the region.
197    pub fn enter(self: &Arc<Stage>, idx: usize) -> SinkGuard {
198        {
199            let mut slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
200            if let Some(slot) = slots.get_mut(idx) {
201                slot.running = true;
202                slot.started = Instant::now();
203                slot.last_output = slot.started;
204                slot.next_beat = HEARTBEAT_SECS;
205                if self.quiet {
206                    slot.capture = Some(crate::capture::Capture::open(
207                        self.log_dir.clone(),
208                        &slot.name,
209                    ));
210                }
211            }
212        }
213        SINK.with(|s| *s.borrow_mut() = Some((Arc::clone(self), idx)));
214        SinkGuard
215    }
216
217    /// Slot `idx` is waiting for a host slot (`Some`), or no longer is.
218    pub fn queue(&self, idx: usize, since: Option<(Instant, u64)>) {
219        let mut slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
220        if let Some(slot) = slots.get_mut(idx) {
221            slot.queued = since;
222        }
223    }
224
225    /// Show slot `idx`'s spawned command in the displays while the returned
226    /// guard lives. A check that runs several commands in turn attaches each
227    /// one; between them the slot falls back to its own output clock.
228    pub fn attach(
229        self: &Arc<Stage>,
230        idx: usize,
231        activity: Arc<crate::hooks::common::Activity>,
232    ) -> AttachGuard {
233        let mut slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
234        if let Some(slot) = slots.get_mut(idx) {
235            slot.activity = Some(activity);
236        }
237        AttachGuard {
238            stage: Arc::clone(self),
239            idx,
240        }
241    }
242
243    /// Append raw bytes (a captured child's output) to slot `idx`.
244    pub fn append_raw(&self, idx: usize, bytes: &[u8]) {
245        let mut slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
246        if let Some(slot) = slots.get_mut(idx) {
247            if !slot.done {
248                match slot.capture.as_mut() {
249                    Some(capture) => capture.take(bytes),
250                    None => slot.buf.extend_from_slice(bytes),
251                }
252                slot.last_output = Instant::now();
253            }
254        }
255    }
256
257    /// This stage is one check run by name: its pass prints an `ok` line.
258    pub fn named(&self) {
259        self.named.store(true, Ordering::Relaxed);
260    }
261
262    /// The exit code of the tool slot `idx` just ran (`None`: killed).
263    pub fn runner_exit(&self, idx: usize, code: Option<i32>) {
264        let mut slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
265        if let Some(slot) = slots.get_mut(idx) {
266            slot.runner_exit = code;
267        }
268    }
269
270    fn append_line(&self, idx: usize, line: &str) {
271        let mut slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
272        if let Some(slot) = slots.get_mut(idx) {
273            if !slot.done {
274                slot.buf.extend_from_slice(line.as_bytes());
275                slot.buf.push(b'\n');
276                slot.last_output = Instant::now();
277            }
278        }
279    }
280
281    /// The check is over: emit everything it said as ONE contiguous write,
282    /// with the region lifted out of the way first and repainted after —
283    /// blocks pile up above, spinners stay below.
284    ///
285    /// Called by the dispatcher after `check.run` returns (still on the
286    /// check's thread, so a torn-down thread cannot strand a buffer — the
287    /// same `catch_unwind` that feeds the dead-check outcome runs first).
288    ///
289    /// `outcome` is the check's, when it returned one; `None` is a check
290    /// whose thread died, which has not passed. Only a quiet slot reads it.
291    pub fn finish(
292        &self,
293        settings: &crate::config::Settings,
294        idx: usize,
295        outcome: Option<crate::check::Outcome>,
296    ) {
297        let block = {
298            let mut slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
299            let Some(slot) = slots.get_mut(idx) else {
300                return;
301            };
302            slot.done = true;
303            slot.running = false;
304            let said = std::mem::take(&mut slot.buf);
305            match slot.capture.take() {
306                None => QuietOut::plain(said),
307                Some(capture) => quiet_block(QuietEnd {
308                    name: &slot.name,
309                    secs: slot.started.elapsed().as_secs_f64(),
310                    outcome,
311                    said,
312                    capture,
313                    runner_exit: slot.runner_exit,
314                    ci: in_ci(),
315                    ok_line: self.named.load(Ordering::Relaxed),
316                }),
317            }
318        };
319        if block.is_empty() && !self.live {
320            return;
321        }
322        let mut drawn = self.out.lock().unwrap_or_else(|p| p.into_inner());
323        if !block.is_empty() {
324            if *drawn > 0 {
325                let mut err = std::io::stderr().lock();
326                let _ = write!(err, "\x1b[{}A\x1b[J", *drawn);
327                let _ = err.flush();
328                *drawn = 0;
329            }
330            let stdout = std::io::stdout();
331            let mut handle = stdout.lock();
332            block.write(&mut handle);
333            let _ = handle.flush();
334        }
335        self.repaint(settings, &mut drawn);
336    }
337
338    /// Erase and redraw the region in one stderr write. Lock order is
339    /// `out` → `slots`, everywhere — never the reverse.
340    fn repaint(&self, settings: &crate::config::Settings, drawn: &mut usize) {
341        if !self.live {
342            return;
343        }
344        let entries: Vec<Row> = {
345            let slots = self.slots.lock().unwrap_or_else(|p| p.into_inner());
346            let now = Instant::now();
347            slots
348                .iter()
349                .filter(|s| s.running && !s.done)
350                .map(|s| row_of(s, now, now.duration_since(s.started).as_secs_f64()))
351                .collect()
352        };
353        let text = region(&entries, term_width(), budgets(settings));
354        let mut paint = String::new();
355        if *drawn > 0 {
356            paint.push_str(&format!("\x1b[{}A\x1b[J", *drawn));
357        }
358        paint.push_str(&text);
359        if paint.is_empty() {
360            return;
361        }
362        let mut err = std::io::stderr().lock();
363        let _ = err.write_all(paint.as_bytes());
364        let _ = err.flush();
365        *drawn = text.matches('\n').count();
366    }
367}
368
369impl Drop for Stage {
370    /// The stage's end erases whatever the region still shows — a Block
371    /// verdict, a panic on the dispatcher path, anything: no spinner junk
372    /// above the roll-up. (`get_mut`: dropping proves no other thread holds
373    /// the stage, so the locks are free.)
374    fn drop(&mut self) {
375        self.stop.store(true, Ordering::Relaxed);
376        if !self.live {
377            return;
378        }
379        let drawn = self.out.get_mut().unwrap_or_else(|p| p.into_inner());
380        if *drawn > 0 {
381            let mut err = std::io::stderr().lock();
382            let _ = write!(err, "\x1b[{}A\x1b[J", *drawn);
383            let _ = err.flush();
384            *drawn = 0;
385        }
386    }
387}
388
389/// The ticker: repaint every 80ms until the stage drops or tells it to
390/// stop. Holds only a `Weak`, so it can never keep a finished stage alive.
391/// Owns its `Settings` (see [`crate::config::Settings::for_thread`]): a
392/// spawned thread is `'static`, and the budgets must be read lazily, not
393/// pre-resolved at the spawn.
394fn tick(settings: crate::config::Settings, weak: Weak<Stage>) {
395    loop {
396        std::thread::sleep(std::time::Duration::from_millis(80));
397        let Some(stage) = weak.upgrade() else { return };
398        if stage.stop.load(Ordering::Relaxed) {
399            return;
400        }
401        let mut drawn = stage.out.lock().unwrap_or_else(|p| p.into_inner());
402        stage.repaint(&settings, &mut drawn);
403    }
404}
405
406/// One running check, as the region and the heartbeat see it.
407#[derive(Debug, Clone)]
408pub struct Row {
409    pub name: String,
410    /// Seconds since the check entered.
411    pub elapsed: f64,
412    /// Seconds since it last wrote anything.
413    pub quiet: f64,
414    /// Seconds it has been silent AND idle on CPU — what the silence budget
415    /// is judged against. Equal to `quiet` when CPU is not sampled.
416    pub still: f64,
417    pub cpu: RowCpu,
418    /// The declared lock wait it is in, and for how many seconds
419    /// (ADR-0009): shown instead of the silence countdown, which does not
420    /// run meanwhile.
421    pub wait: Option<(crate::hooks::wait::WaitKind, f64)>,
422    /// The silence budget the kill decision is applying, stretched by the
423    /// host's load, with the load and the factor; `None` while it is the
424    /// configured one (ADR-0009).
425    pub load: Option<(u64, crate::load::Load, u32)>,
426    /// Waiting for a host slot: for how many seconds, out of how many
427    /// slots.
428    pub queued: Option<(f64, u64)>,
429}
430
431/// What a running check's CPU is doing, as far as the displays may say.
432#[derive(Debug, Clone, Copy, PartialEq, Eq)]
433pub enum RowCpu {
434    /// No spawned command is attached (an in-process check, or between two
435    /// commands): nothing to say.
436    None,
437    /// A command is attached but its CPU is not sampled here
438    /// (`amont.idleCpuCredit false`, no silence budget, or the platform):
439    /// silence alone counts.
440    NotSampled,
441    /// Sampled, but nothing fresh to report (not quiet long enough yet, or
442    /// the last snapshot was incomplete).
443    Unmeasured,
444    /// Measurably working, at this many thousandths of a core.
445    Busy(u32),
446    /// Measured under the busy threshold.
447    Idle,
448}
449
450/// A slot as a [`Row`], reading its attached command's clocks when there is
451/// one. The quiet figure is the more recent of the slot's own lines and the
452/// command's bytes (a captured command writes to one and not the other).
453fn row_of(s: &Slot, now: Instant, elapsed: f64) -> Row {
454    use crate::hooks::common::{CpuState, BUSY_MILLI_CORES};
455    let slot_quiet = now.duration_since(s.last_output).as_secs_f64();
456    let (quiet, still, cpu, wait, load) = match &s.activity {
457        None => (slot_quiet, slot_quiet, RowCpu::None, None, None),
458        Some(a) => {
459            let quiet = slot_quiet.min(a.quiet_for().as_secs_f64());
460            let still = quiet.min(a.still_for().as_secs_f64());
461            let cpu = match (a.cpu_state(), a.fresh_rate()) {
462                (CpuState::Off, _) => RowCpu::NotSampled,
463                (_, Some(r)) if r >= BUSY_MILLI_CORES => RowCpu::Busy(r),
464                (_, Some(_)) => RowCpu::Idle,
465                (_, None) => RowCpu::Unmeasured,
466            };
467            let wait = a.waiting().map(|(k, d)| (k, d.as_secs_f64()));
468            (quiet, still, cpu, wait, a.load_scale())
469        }
470    };
471    Row {
472        name: s.name.clone(),
473        elapsed,
474        quiet,
475        still,
476        cpu,
477        wait,
478        load,
479        queued: s
480            .queued
481            .map(|(since, n)| (now.duration_since(since).as_secs_f64(), n)),
482    }
483}
484
485impl Row {
486    /// The silence budget this row counts toward: the load-stretched one
487    /// when the host stretched it, else the configured one.
488    fn applied_idle(&self, budgets: Budgets) -> u64 {
489        match self.load {
490            Some((applied, _, _)) if applied > 0 => applied,
491            _ => budgets.idle,
492        }
493    }
494}
495
496/// The clocks, as the region annotates them, in seconds: `idle` and
497/// `ceiling` with `0` for off; `lock_wait` with `0` for "until the
498/// ceiling".
499#[derive(Debug, Clone, Copy)]
500pub struct Budgets {
501    pub idle: u64,
502    pub ceiling: u64,
503    pub lock_wait: u64,
504    /// The extended silence budget, `idle × amont.idleLoadScale`: what a
505    /// check answers to while its CPU cannot be measured.
506    pub extended: u64,
507}
508
509fn budgets(settings: &crate::config::Settings) -> Budgets {
510    let idle = crate::hooks::common::idle_timeout(settings);
511    Budgets {
512        idle,
513        extended: idle.saturating_mul(crate::hooks::common::idle_load_scale(settings)),
514        ceiling: crate::hooks::common::check_timeout(settings),
515        lock_wait: match crate::hooks::common::lock_wait(settings) {
516            crate::hooks::common::LockWait::Secs(s) => s,
517            crate::hooks::common::LockWait::UntilCeiling => 0,
518        },
519    }
520}
521
522/// How long a check must be quiet before the region says so. A test suite
523/// pauses this long between crates without anything being wrong; past it,
524/// the reader wants to know the silence is being counted.
525const QUIET_NOTE_SECS: f64 = 30.0;
526
527/// The non-tty heartbeat's period: one line a minute per running check.
528const HEARTBEAT_SECS: u64 = 60;
529
530/// Elapsed time in a fixed six-column figure: `  3.2s` under a minute,
531/// `8m12s` and `1h02m` above, so the column stays aligned as the suite
532/// crosses the minute.
533fn elapsed_column(secs: f64) -> String {
534    if secs < 60.0 {
535        format!("{secs:>5.1}s")
536    } else {
537        format!("{:>6}", crate::hooks::common::human_secs(secs as u64))
538    }
539}
540
541/// The region's text: one `⠹ name  12.3s` line per running check, capped at
542/// [`MAX_LINES`] plus a `… and N more` overflow line. Pure — the ticker is
543/// a thin shell around this, and the tests drive it directly.
544///
545/// Two annotations, each only when it carries news: `· quiet 45s/2m` once
546/// a check has been silent past [`QUIET_NOTE_SECS`] (with the silence
547/// budget it is counting toward, when there is one), and `· 48m/60m` once
548/// elapsed passes 80% of the ceiling — the cliff, shown before the fall.
549fn region(entries: &[Row], width: usize, budgets: Budgets) -> String {
550    if entries.is_empty() {
551        return String::new();
552    }
553    let pad = entries
554        .iter()
555        .take(MAX_LINES)
556        .map(|r| r.name.chars().count())
557        .max()
558        .unwrap_or(0);
559    let mut out = String::new();
560    for row in entries.iter().take(MAX_LINES) {
561        let frame = FRAMES[((row.elapsed * 10.0) as usize) % FRAMES.len()];
562        let name = &row.name;
563        let mut line = format!("{frame} {name:<pad$} {}", elapsed_column(row.elapsed));
564        if let Some((secs, n)) = row.queued {
565            line.push_str(&format!(
566                " · queued {} (slots {n}/{n})",
567                crate::hooks::common::human_secs(secs as u64)
568            ));
569        } else if let Some((kind, waited)) = row.wait {
570            // A declared wait: no silence countdown runs, so the row says
571            // what is being waited for and against which budget.
572            line.push_str(&format!(
573                " · {} {}",
574                kind.short(),
575                crate::hooks::common::human_secs(waited as u64)
576            ));
577            if budgets.lock_wait > 0 {
578                line.push_str(&format!(
579                    "/{}",
580                    crate::hooks::common::human_secs(budgets.lock_wait)
581                ));
582            }
583        } else if row.quiet >= QUIET_NOTE_SECS {
584            let quiet = crate::hooks::common::human_secs(row.quiet as u64);
585            match row.cpu {
586                // Working: no countdown — no kill is coming — just how hard.
587                RowCpu::Busy(m) => line.push_str(&format!(
588                    " · quiet {quiet} · {}",
589                    crate::hooks::common::cores(m)
590                )),
591                _ if budgets.idle == 0 => line.push_str(&format!(" · quiet {quiet}")),
592                // The countdown counts what the kill decision counts: the
593                // still-time, which only differs from the silence once CPU
594                // work has pushed it back.
595                // Unmeasured: the extended budget is the one that counts,
596                // and the row says why the figure is not the usual one.
597                RowCpu::Unmeasured => line.push_str(&format!(
598                    " · quiet {quiet}/{} (CPU unmeasured)",
599                    crate::hooks::common::human_secs(budgets.extended.max(budgets.idle))
600                )),
601                RowCpu::Idle if row.quiet - row.still >= 1.0 => line.push_str(&format!(
602                    " · quiet {quiet} · idle {}/{}",
603                    crate::hooks::common::human_secs(row.still as u64),
604                    crate::hooks::common::human_secs(row.applied_idle(budgets))
605                )),
606                _ => line.push_str(&format!(
607                    " · quiet {quiet}/{}",
608                    crate::hooks::common::human_secs(row.applied_idle(budgets))
609                )),
610            }
611            if let (Some((_, _, factor)), false) = (row.load, row.cpu == RowCpu::Unmeasured) {
612                line.push_str(&format!(" (load {})", crate::load::factor_text(factor)));
613            }
614        }
615        if budgets.ceiling > 0 && row.elapsed >= 0.8 * budgets.ceiling as f64 {
616            line.push_str(&format!(
617                " · {}/{}",
618                crate::hooks::common::human_secs(row.elapsed as u64),
619                crate::hooks::common::human_secs(budgets.ceiling)
620            ));
621        }
622        if line.chars().count() > width {
623            out.extend(line.chars().take(width));
624        } else {
625            out.push_str(&line);
626        }
627        out.push('\n');
628    }
629    if entries.len() > MAX_LINES {
630        out.push_str(&format!("… and {} more\n", entries.len() - MAX_LINES));
631    }
632    out
633}
634
635/// The heartbeat: once a minute, for each check still running, one plain
636/// line on stderr — elapsed, and how long since it last said anything.
637/// Not a region: nothing is erased or repainted, because nobody is looking
638/// at a cursor; whoever reads this reads a log.
639///
640/// The first beat for a check also names the two budgets, once, so the
641/// reader can tell how far it is from being killed without opening the
642/// docs. Written under the same `out` lock as the blocks, so a beat never
643/// lands inside one.
644/// Owns its `Settings` for the same reason [`tick`] does.
645/// What a quiet slot knows when its check ends.
646struct QuietEnd<'a> {
647    name: &'a str,
648    secs: f64,
649    outcome: Option<crate::check::Outcome>,
650    /// The check's own messages, as today.
651    said: Vec<u8>,
652    capture: crate::capture::Capture,
653    runner_exit: Option<i32>,
654    ci: bool,
655    /// Print `ok <check> …` on a pass (a check run by name).
656    ok_line: bool,
657}
658
659/// `CI` set to anything but empty, `false` or `0`. Under CI a failure prints
660/// its whole log: the log file goes away with the runner.
661fn in_ci() -> bool {
662    std::env::var("CI").is_ok_and(|v| !v.is_empty() && v != "false" && v != "0")
663}
664
665/// A quiet slot's block: one line for a pass; for anything else the check's
666/// own messages, then each failure block of the tool's output in full and
667/// the log's path. Pure but for the log file it closes and renames.
668fn quiet_block(mut end: QuietEnd) -> QuietOut {
669    let stored = end.capture.finish();
670    let failed = is_failure(end.outcome);
671    // The active-run entry carries the check's result, not the tool's: a
672    // failure with a runner exit of 0 is still a failure in relais's pane.
673    let exit = match (end.outcome, failed) {
674        (None, _) => None,
675        (Some(_), true) => Some(end.runner_exit.filter(|c| *c != 0).unwrap_or(1)),
676        (Some(_), false) => Some(0),
677    };
678    end.capture.end_entry(exit, stored.path());
679    let passed = end.outcome == Some(crate::check::Outcome::Passed);
680    if end.capture.seen == 0 {
681        // No tool output: a check that runs no tool (ban-terms, secrets), or
682        // one the push stage skipped before it ran. Exactly what it printed
683        // before quiet captured anything — under quiet, nothing on a pass —
684        // and no empty log left behind. A tool that ran and printed nothing
685        // on a failure says so.
686        if let Some(p) = stored.path() {
687            let _ = std::fs::remove_file(p);
688        }
689        let mut head = end.said;
690        if failed && end.runner_exit.is_some() {
691            head.extend_from_slice(b"(no output)\n");
692            if let Some(code) = end.runner_exit.filter(|c| *c != 0) {
693                head.extend_from_slice(
694                    format!("{} failed, runner exit {code}\n", end.name).as_bytes(),
695                );
696            }
697        }
698        return QuietOut::plain(head);
699    }
700    if passed && !end.ok_line {
701        // In a stage: silent, like every passing check under quiet; the log
702        // stays on disk for whoever wants it.
703        return QuietOut::plain(end.said);
704    }
705    let path = stored.path().map(|p| p.display().to_string());
706    if passed {
707        let mut out = format!("ok {} {:.1}s", end.name, end.secs);
708        if let (crate::capture::Stored::Saved(_), Some(p)) = (&stored, &path) {
709            out.push_str(&format!(" log={p}"));
710        }
711        out.push('\n');
712        if let Some(note) = stored.note() {
713            out.push_str(&note);
714            out.push('\n');
715        }
716        return QuietOut::plain(out.into_bytes());
717    }
718    let excerpt = end.capture.acc.finish(path.as_deref().unwrap_or("-"));
719    let mut tail = String::new();
720    if failed {
721        if let Some(code) = end.runner_exit.filter(|c| *c != 0) {
722            tail.push_str(&format!("{} failed, runner exit {code}\n", end.name));
723        }
724    }
725    if let Some(p) = &path {
726        tail.push_str(&format!("full log: {p}\n"));
727    }
728    if let Some(note) = stored.note() {
729        tail.push_str(&note);
730        tail.push('\n');
731    }
732    // Under CI a failure prints its whole log, copied from the file when the
733    // block is written: the file goes away with the runner. The excerpt is
734    // what prints if the copy cannot be made.
735    match (&stored, end.ci && failed) {
736        (crate::capture::Stored::Saved(p), true) => QuietOut {
737            head: end.said,
738            stream: Some((p.clone(), excerpt)),
739            tail: tail.into_bytes(),
740        },
741        _ => {
742            let mut head = end.said;
743            head.extend_from_slice(excerpt.as_bytes());
744            QuietOut {
745                head,
746                stream: None,
747                tail: tail.into_bytes(),
748            }
749        }
750    }
751}
752
753/// A finished slot's block: `head`, then a log file copied out whole (with
754/// the excerpt to print if the copy fails), then `tail`.
755struct QuietOut {
756    head: Vec<u8>,
757    stream: Option<(std::path::PathBuf, String)>,
758    tail: Vec<u8>,
759}
760
761impl QuietOut {
762    fn plain(head: Vec<u8>) -> QuietOut {
763        QuietOut {
764            head,
765            stream: None,
766            tail: Vec::new(),
767        }
768    }
769
770    fn is_empty(&self) -> bool {
771        self.head.is_empty() && self.stream.is_none() && self.tail.is_empty()
772    }
773
774    fn write(self, to: &mut impl Write) {
775        let _ = to.write_all(&self.head);
776        if let Some((path, excerpt)) = self.stream {
777            let copied = std::fs::File::open(&path).and_then(|mut f| std::io::copy(&mut f, to));
778            if let Err(e) = copied {
779                let _ = to.write_all(excerpt.as_bytes());
780                let _ = writeln!(to, "log not read: {e}");
781            }
782        }
783        let _ = to.write_all(&self.tail);
784    }
785}
786
787/// Whether an outcome is a failure for the quiet block: the failure line
788/// and, under CI, the whole log. A check whose thread died (`None`) has not
789/// passed. Every variant named, so a new one is a compile error here.
790fn is_failure(outcome: Option<crate::check::Outcome>) -> bool {
791    use crate::check::Outcome;
792    match outcome {
793        None => true,
794        Some(Outcome::Failed) => true,
795        Some(
796            Outcome::Passed
797            | Outcome::Warned
798            | Outcome::Fixed
799            | Outcome::Unavailable
800            | Outcome::Inert
801            | Outcome::Deferred
802            | Outcome::Skipped,
803        ) => false,
804    }
805}
806
807fn heartbeat(settings: crate::config::Settings, weak: Weak<Stage>) {
808    loop {
809        std::thread::sleep(crate::clock::scaled(std::time::Duration::from_secs(1)));
810        let Some(stage) = weak.upgrade() else { return };
811        if stage.stop.load(Ordering::Relaxed) {
812            return;
813        }
814        let mut announce: Vec<String> = Vec::new();
815        let due: Vec<(Row, bool)> = {
816            let mut slots = stage.slots.lock().unwrap_or_else(|p| p.into_inner());
817            let now = Instant::now();
818            let mut due = Vec::new();
819            for s in slots.iter_mut().filter(|s| s.running && !s.done) {
820                let ran = now.duration_since(s.started);
821                let elapsed = ran.as_secs();
822                // Only a log the tool has written to: a check that prints
823                // nothing keeps no log, and the line would point at nothing.
824                if !s.announced
825                    && ran >= crate::clock::scaled(std::time::Duration::from_secs(ANNOUNCE_SECS))
826                {
827                    if let Some(c) = s.capture.as_ref().filter(|c| c.writing() && c.seen > 0) {
828                        s.announced = true;
829                        announce.push(format!("running {}, log {}\n", s.name, c.tmp.display()));
830                    }
831                }
832                if elapsed >= s.next_beat {
833                    let first = s.next_beat == HEARTBEAT_SECS;
834                    s.next_beat += HEARTBEAT_SECS;
835                    due.push((row_of(s, now, elapsed as f64), first));
836                }
837            }
838            due
839        };
840        if due.is_empty() && announce.is_empty() {
841            continue;
842        }
843        let text: String = announce.concat()
844            + &due
845                .iter()
846                .map(|(row, first)| beat_line(row, *first, budgets(&settings), stage.on_push))
847                .collect::<String>();
848        let _guard = stage.out.lock().unwrap_or_else(|p| p.into_inner());
849        let mut err = std::io::stderr().lock();
850        let _ = err.write_all(text.as_bytes());
851        let _ = err.flush();
852    }
853}
854
855/// One heartbeat line. Pure, for the tests.
856///
857/// On the FIRST beat of a PUSH gate it also names something no other part of
858/// the system is placed to explain. `git push` opens its connection to the
859/// remote, reads the remote refs — which is where the `pre-push` hook's own
860/// stdin comes from — and only then calls the hook. The connection is
861/// therefore already open and goes idle for exactly as long as the gate
862/// runs, and a remote may close it before the gate finishes. git then
863/// reports `Connection reset by peer`, which reads as a network fault and
864/// says nothing about the seven minutes that caused it.
865///
866/// The note does NOT recommend ssh keepalive, and that omission is
867/// deliberate: `ServerAliveInterval 60` was already in force on the machine
868/// where this was diagnosed, and GitHub reset the connection anyway.
869/// Whatever the remote is measuring, it is not packets. Recommending it
870/// would be a confident instruction to change a setting that is probably
871/// already on and cannot help, so the note says so and points at the thing
872/// that does work.
873///
874/// Only on a first beat, so it is said once; only on a push, so a commit
875/// gate never hears it. A first beat is a check that has already run a full
876/// minute, which is the population at risk — no threshold to invent.
877fn beat_line(row: &Row, first: bool, budgets: Budgets, on_push: bool) -> String {
878    use crate::hooks::common::human_secs;
879    // The prefix is byte-for-byte what it always was: log readers grep it.
880    // What CPU sampling adds goes after it.
881    let mut line = format!(
882        "  … {} still running: {}, last output {} ago",
883        row.name,
884        human_secs(row.elapsed as u64),
885        human_secs(row.quiet as u64)
886    );
887    if let Some((secs, n)) = row.queued {
888        line.push_str(&format!(
889            ", queued {} for a host slot (amont.hostSlots {n})",
890            human_secs(secs as u64)
891        ));
892    } else if let Some((kind, waited)) = row.wait {
893        line.push_str(&format!(
894            ", waiting for {} {}",
895            kind.describe(),
896            human_secs(waited as u64)
897        ));
898        match budgets.lock_wait {
899            0 => line.push_str(" (amont.lockWait 0: until the ceiling)"),
900            s => line.push_str(&format!(" (amont.lockWait {})", human_secs(s))),
901        }
902    } else {
903        match row.cpu {
904            RowCpu::Busy(m) => line.push_str(&format!(", busy {}", crate::hooks::common::cores(m))),
905            RowCpu::Idle => line.push_str(&format!(", CPU idle {}", human_secs(row.still as u64))),
906            RowCpu::Unmeasured if budgets.idle > 0 => line.push_str(&format!(
907                ", CPU unmeasured — extended budget {}",
908                human_secs(budgets.extended.max(budgets.idle))
909            )),
910            RowCpu::Unmeasured => line.push_str(", CPU unmeasured"),
911            RowCpu::None | RowCpu::NotSampled => {}
912        }
913        if let (Some((applied, load, factor)), false) = (row.load, row.cpu == RowCpu::Unmeasured) {
914            line.push_str(&format!(
915                ", budget {} (load avg {} on {} cores, {} — amont.idleLoadScale)",
916                human_secs(applied),
917                load.avg1_text(),
918                load.cores,
919                crate::load::factor_text(factor)
920            ));
921        }
922    }
923    if first {
924        let idle = match row.applied_idle(budgets) {
925            0 => "off".to_string(),
926            s => human_secs(s),
927        };
928        let ceiling = match budgets.ceiling {
929            0 => "off".to_string(),
930            s => human_secs(s),
931        };
932        match row.cpu {
933            // In a declared wait the silence clock is not running: the rule
934            // in force is the wait's own budget.
935            _ if row.wait.is_some() => line.push_str(&format!(
936                " (killed after {} in the wait, or {ceiling} in total — amont.lockWait / \
937                 amont.timeout)",
938                match budgets.lock_wait {
939                    0 => ceiling.clone(),
940                    s => human_secs(s),
941                }
942            )),
943            // The budget that applies to THIS check, not the configured
944            // one: while CPU is unmeasured that is the extended budget.
945            RowCpu::Unmeasured if budgets.idle > 0 => line.push_str(&format!(
946                " (killed after {} with no output while its CPU is unmeasured, or {ceiling} \
947                 in total — amont.idleTimeout × amont.idleLoadScale / amont.timeout)",
948                human_secs(budgets.extended.max(budgets.idle))
949            )),
950            RowCpu::Busy(_) | RowCpu::Idle if budgets.idle > 0 => line.push_str(&format!(
951                " (killed after {idle} with no output and under 0.1 core of CPU, or \
952                 {ceiling} in total — amont.idleTimeout / amont.timeout)"
953            )),
954            _ => line.push_str(&format!(
955                " (killed after {idle} of silence or {ceiling} in total — amont.idleTimeout / amont.timeout)"
956            )),
957        }
958        if row.cpu == RowCpu::NotSampled && budgets.idle > 0 {
959            line.push_str("; CPU not sampled here, silence alone counts");
960        }
961        if on_push {
962            // `concat!`, not a `\`-continued literal: a continuation keeps
963            // the next line's indentation, which turns the message into runs
964            // of spaces. Each line is its own literal and the newlines are
965            // written down, so what is here is what a reader sees.
966            line.push_str(concat!(
967                "\n    git opened its connection to the remote before calling this",
968                "\n    gate, and it stays idle until the gate finishes. A remote may",
969                "\n    close it first — GitHub does — and the push then fails with",
970                "\n    \"Connection reset by peer\", naming the network rather than the",
971                "\n    wait. ssh keepalive does not prevent this.",
972                "\n    Declaring this check at pre-commit moves it off the push path —",
973                "\n    see \"Moving a gate entry earlier\" in the docs.",
974            ));
975        }
976    }
977    line.push('\n');
978    line
979}
980
981/// Whether a stage of these checks runs while git holds a connection to the
982/// remote open: a push stage, unless it runs inside a rehearsal snapshot
983/// (land's pre-stage included), where nothing is connected, so the beat's
984/// connection note would be false. The names arrive fully qualified.
985fn holds_a_push_connection(names: &[&str], in_snapshot: bool) -> bool {
986    names.iter().any(|n| n.starts_with("pre-push-")) && !in_snapshot
987}
988
989/// `$COLUMNS` when it is exported and sane, else a conservative 80 — the
990/// region's lines are short and an ioctl is not worth its portability. 80,
991/// not wider: shells rarely export `COLUMNS`, and a region line longer than
992/// the real terminal wraps, which breaks the erase arithmetic.
993///
994/// `pub` is now wider than it needs to be — the out-of-crate caller that
995/// justified it, `amont-agent`, is its own project and carries its own copy.
996/// Left public rather than narrowed in the same change that removed it.
997pub fn term_width() -> usize {
998    std::env::var("COLUMNS")
999        .ok()
1000        .and_then(|c| c.parse::<usize>().ok())
1001        .filter(|w| *w >= 20)
1002        .unwrap_or(80)
1003}
1004
1005/// Emits slot `idx`'s block when dropped — however the check's closure
1006/// exits, a panic included: the partial output of a check that died still
1007/// reaches the reader, above the dead-check verdict the runner fills in.
1008pub struct FinishOnDrop<'a> {
1009    stage: &'a Stage,
1010    idx: usize,
1011    /// The check's outcome once it has one; `None` when its thread died.
1012    outcome: std::cell::Cell<Option<crate::check::Outcome>>,
1013    /// Carried, because `Drop` takes no arguments and the finish paint
1014    /// needs the budgets. Same lifetime as the stage it belongs to.
1015    settings: &'a crate::config::Settings,
1016}
1017
1018impl<'a> FinishOnDrop<'a> {
1019    pub fn new(
1020        settings: &'a crate::config::Settings,
1021        stage: &'a Stage,
1022        idx: usize,
1023    ) -> FinishOnDrop<'a> {
1024        FinishOnDrop {
1025            stage,
1026            idx,
1027            outcome: std::cell::Cell::new(None),
1028            settings,
1029        }
1030    }
1031
1032    /// Record what the check returned, for the quiet block.
1033    pub fn outcome(&self, outcome: crate::check::Outcome) {
1034        self.outcome.set(Some(outcome));
1035    }
1036}
1037
1038impl Drop for FinishOnDrop<'_> {
1039    fn drop(&mut self) {
1040        self.stage
1041            .finish(self.settings, self.idx, self.outcome.get());
1042    }
1043}
1044
1045/// Uninstalls the thread's sink on drop, whatever path the check took out.
1046pub struct SinkGuard;
1047
1048impl Drop for SinkGuard {
1049    fn drop(&mut self) {
1050        SINK.with(|s| *s.borrow_mut() = None);
1051    }
1052}
1053
1054/// Detaches a command from its slot's displays when dropped. See
1055/// [`Stage::attach`].
1056pub struct AttachGuard {
1057    stage: Arc<Stage>,
1058    idx: usize,
1059}
1060
1061impl Drop for AttachGuard {
1062    fn drop(&mut self) {
1063        let mut slots = self.stage.slots.lock().unwrap_or_else(|p| p.into_inner());
1064        if let Some(slot) = slots.get_mut(self.idx) {
1065            slot.activity = None;
1066        }
1067    }
1068}
1069
1070/// The sink installed on THIS thread, if any — how a child-capture helper on
1071/// the check's own thread learns where the reader threads should append.
1072pub fn current_sink() -> Option<(Arc<Stage>, usize)> {
1073    SINK.with(|s| s.borrow().clone())
1074}
1075
1076/// One line of check output, wherever it should go.
1077///
1078/// THE funnel: `common::ok/fail/warn` call this, so a check's helper prints
1079/// land in its slot during a stage and on stdout everywhere else. `line` is
1080/// taken without a trailing newline, exactly like `println!`.
1081pub fn say(line: &str) {
1082    let routed = SINK.with(|s| {
1083        s.borrow().as_ref().map(|(stage, idx)| {
1084            stage.append_line(*idx, line);
1085        })
1086    });
1087    if routed.is_none() {
1088        println!("{line}");
1089    }
1090}
1091
1092/// `println!`, stage-aware: formats and routes through [`say`]. What every
1093/// direct print inside a CHECK BODY becomes — a line printed raw from a
1094/// check thread bypasses the slot and interleaves, which is the bug this
1095/// module exists to close.
1096#[macro_export]
1097macro_rules! say {
1098    ($($arg:tt)*) => {
1099        $crate::live::say(&format!($($arg)*))
1100    };
1101}
1102
1103/// Should a check's SUCCESS line be swallowed?
1104///
1105/// A hook that passes says one line per check, and on a clean run that is the
1106/// entire output: fourteen lines to say nothing happened. At a terminal those
1107/// lines are the reassurance that the gate ran. Captured — an agent's tool
1108/// result, a CI log — they are re-read on every later turn of the session and
1109/// say no more the tenth time than the first.
1110///
1111/// So the setting names WHO is reading, not how loud to be:
1112///
1113/// - `auto` (default) — quiet when nobody is watching, verbose at a terminal.
1114/// - `never` — every check says it passed, whoever is reading.
1115/// - `always` — quiet everywhere.
1116///
1117/// `auto` is the default because the reader it costs nothing is the one at a
1118/// terminal: `watching()` is true there, so a person sees exactly what they
1119/// saw before. The reader it saves is the one who cannot skim — a captured
1120/// log, an agent's tool result — and that reader was paying for fourteen
1121/// lines of nothing on every turn of a session. A default that is free for
1122/// one audience and compounding for the other is not a neutral default.
1123///
1124/// Only the success lines go. A failure, a warning, a check that could not
1125/// run, a repaired file, and the blocked summary are printed under every
1126/// setting: quiet is about the uneventful path, and nothing else.
1127pub fn quiet(settings: &crate::config::Settings) -> bool {
1128    *settings.quiet.get_or_init(|| {
1129        decide(
1130            crate::config::enumerated_or(settings, "amont.quiet", QUIET_VALUES, "auto"),
1131            watching(),
1132        )
1133    })
1134}
1135
1136pub const QUIET_VALUES: &[&str] = &["never", "auto", "always"];
1137
1138/// Pure, so the three-way decision is testable without a terminal or a config.
1139fn decide(setting: &str, watching: bool) -> bool {
1140    match setting {
1141        "always" => true,
1142        "auto" => !watching,
1143        // `never`. A value `enumerated_or` rejected never reaches here — it
1144        // complains and hands back the default, which is now `auto`.
1145        _ => false,
1146    }
1147}
1148
1149/// Whether the capture mechanism is on at all. `amont.progress false` is the
1150/// escape hatch back to raw streaming — one knob, read once.
1151pub fn enabled(settings: &crate::config::Settings) -> bool {
1152    *settings
1153        .progress
1154        .get_or_init(|| crate::config::boolean_or(settings, "amont.progress", true))
1155}
1156
1157/// Is anyone watching? True only when stderr is a real terminal that speaks
1158/// VT: not piped, not redirected, not `TERM=dumb` — and on Windows only
1159/// with `TERM` actually set, because bare conhost may not interpret the
1160/// cursor codes the region depends on. This is the paint gate; capture
1161/// ([`enabled`]) does not consult it.
1162pub fn watching() -> bool {
1163    static WATCHING: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
1164    *WATCHING.get_or_init(|| {
1165        if !std::io::stderr().is_terminal() {
1166            return false;
1167        }
1168        match std::env::var("TERM") {
1169            Ok(term) => term != "dumb",
1170            Err(_) => !cfg!(windows),
1171        }
1172    })
1173}
1174
1175#[cfg(test)]
1176mod tests {
1177    use super::*;
1178
1179    fn test_settings() -> crate::config::Settings {
1180        crate::config::Settings::default()
1181    }
1182
1183    #[test]
1184    fn quiet_asks_who_is_reading() {
1185        assert!(!decide("never", true));
1186        assert!(!decide("never", false));
1187        assert!(always_and_auto_agree_at_a_terminal());
1188        assert!(decide("auto", false), "captured: nobody is watching");
1189        assert!(decide("always", true));
1190        assert!(decide("always", false));
1191        // An unreadable value has already been reported by `enumerated_or`,
1192        // which hands back the default; silence is never assumed.
1193        assert!(!decide("shhh", false));
1194    }
1195
1196    fn always_and_auto_agree_at_a_terminal() -> bool {
1197        !decide("auto", true) && decide("always", true)
1198    }
1199
1200    /// The atomicity contract at the unit level: two threads writing
1201    /// interleaved lines into their own slots come out as two contiguous
1202    /// buffers, whatever the scheduler did.
1203    #[test]
1204    fn slots_do_not_share_a_buffer() {
1205        let stage = Stage::begin(&test_settings(), &["a", "b"]);
1206        std::thread::scope(|scope| {
1207            for idx in 0..2 {
1208                let stage = Arc::clone(&stage);
1209                scope.spawn(move || {
1210                    let _guard = stage.enter(idx);
1211                    for i in 0..50 {
1212                        say(&format!("check-{idx} line-{i}"));
1213                        std::thread::yield_now();
1214                    }
1215                });
1216            }
1217        });
1218        let slots = stage.slots.lock().unwrap();
1219        for idx in 0..2 {
1220            let text = String::from_utf8(slots[idx].buf.clone()).unwrap();
1221            assert_eq!(text.lines().count(), 50);
1222            assert!(
1223                text.lines()
1224                    .all(|l| l.starts_with(&format!("check-{idx} "))),
1225                "a foreign line landed in slot {idx}"
1226            );
1227        }
1228    }
1229
1230    /// A thread with no sink prints; its lines never land in anyone's slot.
1231    #[test]
1232    fn no_sink_means_no_capture() {
1233        let stage = Stage::begin(&test_settings(), &["a"]);
1234        say("goes to stdout, not to a slot");
1235        let slots = stage.slots.lock().unwrap();
1236        assert!(slots[0].buf.is_empty());
1237    }
1238
1239    /// After finish, late writes are dropped rather than stranded — a child
1240    /// reader thread that outlives its check must not corrupt a later block.
1241    #[test]
1242    fn a_finished_slot_takes_no_more_writes() {
1243        let stage = Stage::begin(&test_settings(), &["a"]);
1244        stage.append_raw(0, b"before\n");
1245        stage.finish(&test_settings(), 0, None);
1246        stage.append_raw(0, b"after\n");
1247        let slots = stage.slots.lock().unwrap();
1248        assert!(slots[0].buf.is_empty(), "a write landed after finish");
1249    }
1250
1251    /// A repo-derived check name cannot smuggle control bytes onto a live
1252    /// terminal: sanitised at begin, once, for every later paint.
1253    #[test]
1254    fn a_slot_name_is_sanitised_at_begin() {
1255        let stage = Stage::begin(&test_settings(), &["evil\u{1b}[2Jname\rhere"]);
1256        let slots = stage.slots.lock().unwrap();
1257        assert!(!slots[0].name.contains('\u{1b}'), "{:?}", slots[0].name);
1258        assert!(!slots[0].name.contains('\r'), "{:?}", slots[0].name);
1259    }
1260
1261    /// Region names drop the stage's own prefix — it is the same twelve
1262    /// characters on every line.
1263    #[test]
1264    fn a_slot_name_drops_the_stage_prefix() {
1265        let stage = Stage::begin(
1266            &test_settings(),
1267            &["pre-commit-clippy", "pre-push-run-tests", "bare"],
1268        );
1269        let slots = stage.slots.lock().unwrap();
1270        assert_eq!(slots[0].name, "clippy");
1271        assert_eq!(slots[1].name, "run-tests");
1272        assert_eq!(slots[2].name, "bare");
1273    }
1274
1275    fn row(name: &str, elapsed: f64) -> Row {
1276        Row {
1277            name: name.into(),
1278            elapsed,
1279            quiet: 0.0,
1280            still: 0.0,
1281            cpu: RowCpu::None,
1282            wait: None,
1283            load: None,
1284            queued: None,
1285        }
1286    }
1287
1288    /// A check waiting for a host slot says so in the region, within 80
1289    /// columns, and in the heartbeat after its unchanged prefix.
1290    #[test]
1291    fn a_queued_check_says_it_waits_for_a_host_slot() {
1292        let mut r = row("pre-push-run-tests-js", 42.0);
1293        r.queued = Some((42.0, 2));
1294        let text = region(&[r.clone()], 80, B);
1295        assert!(text.contains("· queued 42s (slots 2/2)"), "{text:?}");
1296        assert!(text.lines().all(|l| l.chars().count() <= 80), "{text:?}");
1297        r.elapsed = 60.0;
1298        r.quiet = 60.0;
1299        r.queued = Some((60.0, 2));
1300        let beat = beat_line(&r, false, B, false);
1301        assert_eq!(
1302            beat,
1303            "  … pre-push-run-tests-js still running: 1m00s, last output 1m00s ago, \
1304             queued 1m00s for a host slot (amont.hostSlots 2)\n"
1305        );
1306    }
1307
1308    /// Under load the region counts toward the stretched budget and says
1309    /// so; the heartbeat names the budget, the load and the key; the first
1310    /// beat states the stretched figure.
1311    #[test]
1312    fn a_load_stretched_budget_is_shown_with_its_load() {
1313        let mut r = row("pre-push-run-tests-js", 240.0);
1314        r.quiet = 130.0;
1315        r.still = 130.0;
1316        r.cpu = RowCpu::Idle;
1317        r.load = Some((
1318            468,
1319            crate::load::Load {
1320                avg1_milli: 31_200,
1321                cores: 8,
1322            },
1323            3900,
1324        ));
1325        let text = region(&[r.clone()], 80, B);
1326        assert!(text.contains("· quiet 2m10s/7m48s (load ×3.9)"), "{text:?}");
1327        assert!(text.lines().all(|l| l.chars().count() <= 80), "{text:?}");
1328        let beat = beat_line(&r, false, B, false);
1329        assert!(
1330            beat.ends_with(
1331                ", CPU idle 2m10s, budget 7m48s (load avg 31.2 on 8 cores, ×3.9 — amont.idleLoadScale)\n"
1332            ),
1333            "{beat:?}"
1334        );
1335        let first = flat(&beat_line(&r, true, B, false));
1336        assert!(
1337            first.contains("killed after 7m48s with no output and under 0.1 core of CPU"),
1338            "{first:?}"
1339        );
1340    }
1341
1342    const B: Budgets = Budgets {
1343        idle: 120,
1344        ceiling: 3600,
1345        lock_wait: 600,
1346        extended: 480,
1347    };
1348
1349    /// While CPU is unmeasured the extended budget is the one that counts:
1350    /// the region counts toward it and says why, the heartbeat names it,
1351    /// and the first beat states that rule rather than the configured one.
1352    #[test]
1353    fn an_unmeasured_check_is_shown_against_the_extended_budget() {
1354        let mut r = row("pre-push-run-tests-js", 240.0);
1355        r.quiet = 130.0;
1356        r.still = 130.0;
1357        r.cpu = RowCpu::Unmeasured;
1358        let text = region(&[r.clone()], 80, B);
1359        assert!(
1360            text.contains("· quiet 2m10s/8m00s (CPU unmeasured)"),
1361            "{text:?}"
1362        );
1363        assert!(text.lines().all(|l| l.chars().count() <= 80), "{text:?}");
1364        let beat = beat_line(&r, false, B, false);
1365        assert!(
1366            beat.ends_with(", CPU unmeasured — extended budget 8m00s\n"),
1367            "{beat:?}"
1368        );
1369        let first = flat(&beat_line(&r, true, B, false));
1370        assert!(
1371            first.contains(
1372                "killed after 8m00s with no output while its CPU is unmeasured, or 1h00m in total"
1373            ),
1374            "{first:?}"
1375        );
1376        assert!(first.contains("amont.idleLoadScale"), "{first:?}");
1377    }
1378
1379    /// A declared wait replaces the silence countdown in the region and
1380    /// rides after the heartbeat's unchanged prefix, naming its budget; both
1381    /// fit 80 columns with the longest built-in name.
1382    #[test]
1383    fn a_declared_wait_is_shown_against_its_own_budget() {
1384        use crate::hooks::wait::{CargoLockWhat, WaitKind};
1385        let mut r = row("pre-push-run-tests-js", 240.0);
1386        r.quiet = 130.0;
1387        r.still = 0.0;
1388        r.cpu = RowCpu::Unmeasured;
1389        r.wait = Some((WaitKind::CargoLock(CargoLockWhat::BuildDirectory), 90.0));
1390        let text = region(&[r.clone()], 80, B);
1391        assert!(text.contains("· cargo lock 1m30s/10m00s"), "{text:?}");
1392        assert!(!text.contains("quiet"), "no silence countdown: {text:?}");
1393        assert!(text.lines().all(|l| l.chars().count() <= 80), "{text:?}");
1394        let until = Budgets { lock_wait: 0, ..B };
1395        let text = region(&[r.clone()], 80, until);
1396        assert!(
1397            text.contains("· cargo lock 1m30s") && !text.contains("1m30s/"),
1398            "{text:?}"
1399        );
1400
1401        let beat = beat_line(&r, false, B, false);
1402        assert_eq!(
1403            beat,
1404            "  … pre-push-run-tests-js still running: 4m00s, last output 2m10s ago, \
1405             waiting for the cargo lock on the build directory 1m30s (amont.lockWait 10m00s)\n"
1406        );
1407        let beat = beat_line(&r, false, until, false);
1408        assert!(
1409            beat.contains("(amont.lockWait 0: until the ceiling)"),
1410            "{beat:?}"
1411        );
1412        // The first beat states the wait's rule, not the CPU one: the
1413        // silence clock is not running.
1414        let first = flat(&beat_line(&r, true, B, false));
1415        assert!(
1416            first.contains("(killed after 10m00s in the wait, or 1h00m in total — amont.lockWait"),
1417            "{first:?}"
1418        );
1419        assert!(!first.contains("unmeasured"), "{first:?}");
1420    }
1421
1422    /// The spinner frame comes from the clock: different elapsed, different
1423    /// frame; same elapsed, same frame.
1424    #[test]
1425    fn frames_advance_with_time() {
1426        let a = region(&[row("clippy", 0.0)], 80, B);
1427        let b = region(&[row("clippy", 0.1)], 80, B);
1428        let c = region(&[row("clippy", 1.0)], 80, B);
1429        assert_ne!(a.chars().next(), b.chars().next());
1430        assert_eq!(a.chars().next(), c.chars().next(), "10 frames per second");
1431    }
1432
1433    /// Names pad to a column so the elapsed figures align — across the
1434    /// minute mark too, where the figure changes shape.
1435    #[test]
1436    fn region_lines_align() {
1437        let text = region(&[row("a", 0.0), row("longer-name", 0.0)], 80, B);
1438        let widths: Vec<usize> = text.lines().map(|l| l.chars().count()).collect();
1439        assert_eq!(widths[0], widths[1], "{text:?}");
1440        let text = region(&[row("a", 3.2), row("b", 492.0)], 80, B);
1441        let widths: Vec<usize> = text.lines().map(|l| l.chars().count()).collect();
1442        assert_eq!(widths[0], widths[1], "{text:?}");
1443        assert!(text.contains("8m12s"), "{text:?}");
1444    }
1445
1446    /// Thirteen running checks paint as twelve lines and one overflow.
1447    #[test]
1448    fn region_caps_and_counts_the_rest() {
1449        let entries: Vec<Row> = (0..13).map(|i| row(&format!("check-{i}"), 0.0)).collect();
1450        let text = region(&entries, 80, B);
1451        assert_eq!(text.lines().count(), MAX_LINES + 1);
1452        assert!(text.ends_with("… and 1 more\n"), "{text:?}");
1453    }
1454
1455    /// A narrow terminal truncates rather than wraps — a wrapped region
1456    /// line would break the erase arithmetic.
1457    #[test]
1458    fn region_respects_width() {
1459        let text = region(&[row("a-name-much-longer-than-the-terminal", 0.0)], 20, B);
1460        assert!(text.lines().all(|l| l.chars().count() <= 20), "{text:?}");
1461    }
1462
1463    /// No running checks, no region — not even a blank line.
1464    #[test]
1465    fn an_empty_region_is_empty() {
1466        assert_eq!(region(&[], 80, B), "");
1467    }
1468
1469    /// Silence is annotated only once it is news, and names the budget it
1470    /// counts toward — a check that just paused between crates says
1471    /// nothing extra.
1472    #[test]
1473    fn a_quiet_check_shows_its_silence_against_the_budget() {
1474        let mut r = row("cargo-test", 300.0);
1475        r.quiet = 5.0;
1476        assert!(!region(&[r.clone()], 80, B).contains("quiet"));
1477        r.quiet = 45.0;
1478        let text = region(&[r.clone()], 80, B);
1479        assert!(text.contains("quiet 45s/2m00s"), "{text:?}");
1480        let off = Budgets { idle: 0, ..B };
1481        let text = region(&[r], 80, off);
1482        assert!(
1483            text.contains("quiet 45s") && !text.contains('/'),
1484            "{text:?}"
1485        );
1486    }
1487
1488    /// A silent check that is working shows how hard, with no countdown —
1489    /// no kill is coming; one whose CPU work pushed the still-time back
1490    /// counts down the still-time, which is what the kill decision uses.
1491    /// Both fit an 80-column terminal with a longish name.
1492    #[test]
1493    fn a_quiet_busy_check_shows_cores_and_an_idle_one_counts_down_the_still_time() {
1494        let mut r = row("vitest-workspace", 240.0);
1495        r.quiet = 130.0;
1496        r.still = 130.0;
1497        r.cpu = RowCpu::Busy(3900);
1498        let busy = region(&[r.clone()], 80, B);
1499        assert!(busy.contains("· quiet 2m10s · ~3.9 cores"), "{busy:?}");
1500        assert!(
1501            !busy.contains("/2m00s"),
1502            "no countdown while busy: {busy:?}"
1503        );
1504
1505        r.cpu = RowCpu::Idle;
1506        r.still = 40.0;
1507        let idle = region(&[r.clone()], 80, B);
1508        assert!(idle.contains("· quiet 2m10s · idle 40s/2m00s"), "{idle:?}");
1509
1510        r.cpu = RowCpu::Unmeasured;
1511        r.still = 130.0;
1512        let plain = region(&[r], 80, B);
1513        assert!(
1514            plain.contains("· quiet 2m10s/8m00s (CPU unmeasured)"),
1515            "{plain:?}"
1516        );
1517
1518        for text in [busy, idle, plain] {
1519            assert!(text.lines().all(|l| l.chars().count() <= 80), "{text:?}");
1520        }
1521    }
1522
1523    /// The heartbeat's prefix is unchanged — log readers grep it — and the
1524    /// CPU state rides after it.
1525    #[test]
1526    fn a_heartbeat_appends_the_cpu_state_after_an_unchanged_prefix() {
1527        let mut r = row("vitest", 240.0);
1528        r.quiet = 130.0;
1529        r.still = 40.0;
1530        let prefix = "  … vitest still running: 4m00s, last output 2m10s ago";
1531        for (cpu, suffix) in [
1532            (RowCpu::Busy(3900), ", busy ~3.9 cores\n"),
1533            (RowCpu::Idle, ", CPU idle 40s\n"),
1534            (
1535                RowCpu::Unmeasured,
1536                ", CPU unmeasured — extended budget 8m00s\n",
1537            ),
1538            (RowCpu::None, "\n"),
1539            (RowCpu::NotSampled, "\n"),
1540        ] {
1541            r.cpu = cpu;
1542            assert_eq!(beat_line(&r, false, B, false), format!("{prefix}{suffix}"));
1543        }
1544    }
1545
1546    /// The first beat states the rule that actually applies to this check.
1547    #[test]
1548    fn the_first_beat_states_the_rule_in_force() {
1549        let mut r = row("vitest", 60.0);
1550        r.cpu = RowCpu::Idle;
1551        let sampled = beat_line(&r, true, B, false);
1552        assert!(
1553            flat(&sampled).contains(
1554                "killed after 2m00s with no output and under 0.1 core of CPU, or 1h00m in total"
1555            ),
1556            "{sampled:?}"
1557        );
1558        r.cpu = RowCpu::NotSampled;
1559        let not = beat_line(&r, true, B, false);
1560        assert!(
1561            not.contains("2m00s of silence or 1h00m in total"),
1562            "{not:?}"
1563        );
1564        assert!(
1565            not.contains("CPU not sampled here, silence alone counts"),
1566            "{not:?}"
1567        );
1568    }
1569
1570    /// The ceiling appears once a check is 80% of the way to it — the cliff,
1571    /// shown before the fall — and never for a disabled ceiling.
1572    #[test]
1573    fn the_ceiling_shows_only_when_it_is_near() {
1574        assert!(!region(&[row("cargo-test", 1000.0)], 80, B).contains("/1h00m"));
1575        let text = region(&[row("cargo-test", 3000.0)], 80, B);
1576        assert!(text.contains("50m00s/1h00m"), "{text:?}");
1577        let off = Budgets { ceiling: 0, ..B };
1578        assert!(!region(&[row("cargo-test", 3000.0)], 80, off).contains("/"));
1579    }
1580
1581    /// The heartbeat says how long, how quiet, and — the first time — the
1582    /// budgets, so a reader at the far end of a pipe can tell "wait" from
1583    /// "kill it" without the docs.
1584    #[test]
1585    fn a_heartbeat_names_the_budgets_once() {
1586        let mut r = row("cargo-test", 60.0);
1587        r.quiet = 2.0;
1588        let first = beat_line(&r, true, B, false);
1589        assert!(
1590            first.contains("cargo-test still running: 1m00s"),
1591            "{first:?}"
1592        );
1593        assert!(first.contains("last output 2s ago"), "{first:?}");
1594        assert!(
1595            first.contains("2m00s of silence or 1h00m in total"),
1596            "{first:?}"
1597        );
1598        assert!(first.contains("amont.idleTimeout"), "{first:?}");
1599        let later = beat_line(&r, false, B, false);
1600        assert!(!later.contains("amont.idleTimeout"), "{later:?}");
1601        let off = beat_line(
1602            &r,
1603            true,
1604            Budgets {
1605                idle: 0,
1606                ceiling: 0,
1607                lock_wait: 0,
1608                extended: 0,
1609            },
1610            false,
1611        );
1612        assert!(off.contains("off of silence or off in total"), "{off:?}");
1613    }
1614
1615    /// The message, with newlines and indentation flattened.
1616    ///
1617    /// The note is wrapped for a terminal, so a literal substring can fall
1618    /// across a line break — asserting on `"may close it first"` failed for
1619    /// no better reason than that `may` ended a line. These tests are about
1620    /// what the message SAYS; re-wrapping it should not break them.
1621    fn flat(line: &str) -> String {
1622        line.split_whitespace().collect::<Vec<_>>().join(" ")
1623    }
1624
1625    /// A long PUSH gate is told what it is sitting on; a commit gate is not.
1626    ///
1627    /// The three negatives matter as much as the positive. Said on every
1628    /// beat it would be nagging; said at commit time it would be false —
1629    /// there is no connection open — and a future refactor that wires
1630    /// `on_push` to a constant would show up here and nowhere else.
1631    #[test]
1632    fn a_long_push_gate_is_told_what_it_is_sitting_on() {
1633        let r = row("cargo-test", 60.0);
1634
1635        let pushing = flat(&beat_line(&r, true, B, true));
1636        assert!(
1637            pushing.contains("A remote may close it first"),
1638            "{pushing:?}"
1639        );
1640        assert!(pushing.contains("Connection reset by peer"), "{pushing:?}");
1641        assert!(
1642            pushing.contains("Moving a gate entry earlier"),
1643            "{pushing:?}"
1644        );
1645
1646        // Once, not every minute.
1647        let later = flat(&beat_line(&r, false, B, true));
1648        assert!(!later.contains("close it first"), "{later:?}");
1649
1650        // Never at commit time: nothing is waiting on a socket there.
1651        let committing = flat(&beat_line(&r, true, B, false));
1652        assert!(!committing.contains("close it first"), "{committing:?}");
1653    }
1654
1655    /// amont #431: inside a rehearsal snapshot a push gate may now queue past
1656    /// its first beat, and the stage must not believe git holds a
1657    /// connection there (none is open), else the beat warns of a reset that
1658    /// cannot happen.
1659    #[test]
1660    fn a_push_stage_in_a_snapshot_holds_no_connection() {
1661        let push = ["pre-push-adr-lint-cli-probe"];
1662        assert!(holds_a_push_connection(&push, false));
1663        assert!(!holds_a_push_connection(&push, true));
1664        assert!(!holds_a_push_connection(&["pre-commit-clippy"], false));
1665    }
1666
1667    /// The advice that does NOT appear, and must not come back.
1668    ///
1669    /// `ServerAliveInterval 60` is the obvious suggestion and it is wrong:
1670    /// it was already in force on the machine where this failure was
1671    /// diagnosed, and the remote reset the connection regardless. Telling
1672    /// every amont user to set it would be confident, actionable and
1673    /// useless. This test exists so that a future reader who has the same
1674    /// obvious idea meets an argument instead of a blank.
1675    #[test]
1676    fn the_push_note_does_not_recommend_ssh_keepalive() {
1677        let r = row("cargo-test", 60.0);
1678        let pushing = flat(&beat_line(&r, true, B, true));
1679        assert!(
1680            !pushing.contains("ServerAlive"),
1681            "keepalive was already on when this failed; recommending it \
1682             would be useless advice: {pushing:?}"
1683        );
1684        assert!(
1685            pushing.contains("ssh keepalive does not prevent this"),
1686            "say so, rather than leaving the reader to try it: {pushing:?}"
1687        );
1688    }
1689}