zccache 1.11.5

Local-first compiler cache for C/C++/Rust/Emscripten
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
//! Library half of the zccache-ci stop hook.
//!
//! Exposes the timeout/diagnostics/kill machinery so it is unit-testable
//! independently of the `main()` entrypoint.
//!
//! The Stop hook can hang under pathological conditions (see issue #141):
//! a daemon-lock deadlock, a runaway test, or a build script in an infinite
//! loop. The harness gives us 10 minutes wall-clock, but a single agent turn
//! has been observed running for 45+ minutes when the harness misbehaves.
//!
//! This crate enforces:
//!
//! 1. **Wall-clock timeout** ([`StageRunner`]) — every stage runs against a
//!    shared deadline. On timeout the entire child process tree is killed
//!    (`taskkill /T /F` on Windows, process-group SIGKILL on Unix) and a
//!    best-effort diagnostic snapshot is dumped to stderr.
//!
//! 2. **Per-stage progress markers** ([`StageRunner::start_stage`]) — every
//!    stage prints `[<elapsed>] -> <name>` so the user can see which stage is
//!    hanging when the timeout fires. The last printed stage on the timeout
//!    path is the smoking gun.
//!
//! 3. **Orphan daemon reaper** ([`reap_orphan_daemons`]) — kills any
//!    `zccache-daemon` whose parent PID is no longer alive at startup. This
//!    catches the case where a previous agent turn was force-killed before
//!    its daemon could be reaped.

use crate::core::NormalizedPath;
use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::process::{Child, Command};
use std::time::{Duration, Instant};

use wait_timeout::ChildExt;

/// Default wall-clock timeout for the entire stop hook run, in seconds.
///
/// Override via `ZCCACHE_CI_TIMEOUT_SECS`.
pub const DEFAULT_TIMEOUT_SECS: u64 = 300;

/// Resolve the wall-clock timeout from the environment, falling back to
/// [`DEFAULT_TIMEOUT_SECS`].
pub fn resolve_timeout() -> Duration {
    let secs = env::var("ZCCACHE_CI_TIMEOUT_SECS")
        .ok()
        .and_then(|v| v.parse::<u64>().ok())
        .filter(|s| *s > 0)
        .unwrap_or(DEFAULT_TIMEOUT_SECS);
    Duration::from_secs(secs)
}

/// Outcome of running a single stage under [`StageRunner::run`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StageOutcome {
    /// Stage exited normally with the given status code.
    Exited(i32),
    /// Stage was killed because the hard wall-clock deadline elapsed.
    GlobalTimeout,
    /// Stage spawn failed before we could even wait on it.
    SpawnFailed,
}

/// Where progress markers are written. The default is stderr so that test
/// stdout stays clean and so the harness shows progress as the run unfolds.
pub trait ProgressSink: Send {
    fn write_line(&mut self, line: &str);
}

/// A `ProgressSink` that writes directly to stderr.
pub struct StderrProgress;

impl ProgressSink for StderrProgress {
    fn write_line(&mut self, line: &str) {
        let _ = writeln!(std::io::stderr(), "{line}");
    }
}

/// A `ProgressSink` that captures lines into an in-memory `Vec` for tests.
#[derive(Default)]
pub struct CapturingProgress {
    pub lines: Vec<String>,
}

impl ProgressSink for CapturingProgress {
    fn write_line(&mut self, line: &str) {
        self.lines.push(line.to_string());
    }
}

/// Runs a sequence of stages against a shared wall-clock deadline. Each stage
/// gets at most `deadline - now()` to complete. If a stage exhausts the
/// budget, the runner kills the child process tree and returns
/// [`StageOutcome::GlobalTimeout`].
///
/// The runner is intentionally a thin layer around `std::process::Command` so
/// it composes with whatever the caller wants to spawn (cargo, lint, tests).
///
/// Every stage emits a progress marker through the [`ProgressSink`] when it
/// starts. The default sink writes to stderr; tests use [`CapturingProgress`].
pub struct StageRunner<P: ProgressSink = StderrProgress> {
    started: Instant,
    deadline: Instant,
    progress: P,
    /// Last stage label seen — printed on timeout as the smoking-gun stage.
    last_stage: Option<String>,
}

impl StageRunner<StderrProgress> {
    /// Construct a runner that writes progress markers to stderr.
    pub fn new(timeout: Duration) -> Self {
        Self::with_progress(timeout, StderrProgress)
    }
}

impl<P: ProgressSink> StageRunner<P> {
    /// Construct a runner with an explicit progress sink (used by tests).
    pub fn with_progress(timeout: Duration, progress: P) -> Self {
        let started = Instant::now();
        Self {
            started,
            deadline: started + timeout,
            progress,
            last_stage: None,
        }
    }

    /// Total wall-clock elapsed since the runner was constructed.
    pub fn elapsed(&self) -> Duration {
        self.started.elapsed()
    }

    /// Time remaining before the global deadline expires. Returns
    /// `Duration::ZERO` if already past the deadline.
    pub fn remaining(&self) -> Duration {
        self.deadline.saturating_duration_since(Instant::now())
    }

    /// Borrow the most recently started stage label, if any. Useful in tests
    /// and timeout banners to identify the smoking-gun stage.
    pub fn last_stage(&self) -> Option<&str> {
        self.last_stage.as_deref()
    }

    /// Borrow the progress sink so callers (mainly tests) can assert on
    /// captured lines without consuming the runner.
    pub fn progress_ref(&self) -> &P {
        &self.progress
    }

    /// Print a progress marker for a stage and remember its name. Format:
    /// `[<elapsed>] -> <stage>`.
    pub fn start_stage(&mut self, stage: &str) {
        let elapsed = self.elapsed();
        self.progress
            .write_line(&format!("[{}] -> {}", format_elapsed(elapsed), stage));
        self.last_stage = Some(stage.to_string());
    }

    /// Print the final "done" marker.
    pub fn finish(&mut self) {
        let elapsed = self.elapsed();
        self.progress
            .write_line(&format!("[{}] done", format_elapsed(elapsed)));
    }

    /// Spawn `cmd` and wait for it to exit, bounded by the global deadline.
    /// On timeout the child's process tree is killed and the diagnostic
    /// snapshot is dumped to stderr.
    ///
    /// `stage` is the human-readable label for the stage; it is printed via
    /// the progress sink, recorded as `last_stage`, and surfaced in the
    /// timeout banner.
    pub fn run(&mut self, stage: &str, cmd: &mut Command) -> StageOutcome {
        self.start_stage(stage);

        // Already over budget — bail without spawning.
        if self.remaining().is_zero() {
            self.report_timeout(stage, None);
            return StageOutcome::GlobalTimeout;
        }

        configure_process_group(cmd);
        let mut child = match cmd.spawn() {
            Ok(c) => c,
            Err(e) => {
                let _ = writeln!(std::io::stderr(), "{stage}: failed to spawn child: {e}");
                return StageOutcome::SpawnFailed;
            }
        };

        match child.wait_timeout(self.remaining()) {
            Ok(Some(status)) => StageOutcome::Exited(status.code().unwrap_or(-1)),
            Ok(None) => {
                self.report_timeout(stage, Some(&child));
                kill_process_tree(&mut child);
                StageOutcome::GlobalTimeout
            }
            Err(e) => {
                let _ = writeln!(std::io::stderr(), "{stage}: wait error: {e}");
                let _ = child.kill();
                let _ = child.wait();
                StageOutcome::SpawnFailed
            }
        }
    }

    /// Print the timeout banner and dump diagnostics. Public so callers can
    /// invoke it on alternate code paths (e.g. precondition failures).
    pub fn report_timeout(&mut self, stage: &str, child: Option<&Child>) {
        let elapsed = self.elapsed();
        let _ = writeln!(
            std::io::stderr(),
            "STOP-HOOK TIMEOUT after {} - capturing state",
            format_elapsed(elapsed)
        );
        let _ = writeln!(std::io::stderr(), "  hung stage: {stage}");
        if let Some(prev) = &self.last_stage {
            if prev != stage {
                let _ = writeln!(std::io::stderr(), "  last stage: {prev}");
            }
        }
        if let Some(c) = child {
            let _ = writeln!(std::io::stderr(), "  child PID: {}", c.id());
        }

        capture_diagnostics();
    }
}

/// Format a Duration as `<seconds>.<tenths>s`, e.g. `12.4s`.
pub fn format_elapsed(d: Duration) -> String {
    let total_ms = d.as_millis();
    let secs = total_ms / 1000;
    let tenths = (total_ms % 1000) / 100;
    format!("{secs}.{tenths}s")
}

// ---------------------------------------------------------------------------
// Process-tree kill (Windows: taskkill /T /F, Unix: process-group SIGKILL)
// ---------------------------------------------------------------------------

/// Configure `cmd` so its children form a new process group / job that we can
/// kill atomically. On Unix this calls `setsid` via `pre_exec`; on Windows
/// this sets the `CREATE_NEW_PROCESS_GROUP` creation flag.
pub fn configure_process_group(cmd: &mut Command) {
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        // SAFETY: setsid is async-signal-safe and only mutates the child's
        // own process-group state.
        unsafe {
            cmd.pre_exec(|| {
                if libc::setsid() == -1 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
    }
    #[cfg(windows)]
    {
        use std::os::windows::process::CommandExt;
        const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
        cmd.creation_flags(CREATE_NEW_PROCESS_GROUP);
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = cmd;
    }
}

/// Kill `child` and every descendant. Best-effort: errors are swallowed
/// because we are already on the failure path.
pub fn kill_process_tree(child: &mut Child) {
    let pid = child.id();

    #[cfg(windows)]
    {
        use std::process::Stdio;
        // /T = recursive (kill children), /F = force.
        let _ = Command::new("taskkill")
            .args(["/T", "/F", "/PID", &pid.to_string()])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
    }

    #[cfg(unix)]
    {
        // The child started its own session via setsid, so its PGID == its
        // PID. Negative pid kills the whole process group.
        let pid_i = pid as i32;
        unsafe {
            libc::kill(-pid_i, libc::SIGKILL);
        }
    }

    // Reap the direct child to avoid a zombie even on platforms where the
    // group-kill above did the heavy lifting.
    let _ = child.kill();
    let _ = child.wait();
}

// ---------------------------------------------------------------------------
// Diagnostics snapshot
// ---------------------------------------------------------------------------

/// Best-effort diagnostics dumped on timeout. Each section is independent and
/// failure to read one section never aborts the others.
pub fn capture_diagnostics() {
    eprintln!("--- diagnostics ---");
    dump_relevant_processes();
    dump_daemon_lock();
    dump_zccache_logs();
    dump_compile_journal();
    eprintln!("--- end diagnostics ---");
}

fn dump_relevant_processes() {
    let mut sys = sysinfo::System::new();
    sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
    eprintln!("processes (zccache/cargo/rustc/soldr):");
    let mut count = 0usize;
    for (pid, p) in sys.processes() {
        let name = p.name().to_string_lossy();
        let lower = name.to_ascii_lowercase();
        if lower.contains("zccache")
            || lower.contains("cargo")
            || lower.contains("rustc")
            || lower.contains("soldr")
        {
            eprintln!(
                "  PID={pid} name={} status={:?} cpu={:.1}% mem={}KB",
                name,
                p.status(),
                p.cpu_usage(),
                p.memory() / 1024,
            );
            count += 1;
        }
    }
    if count == 0 {
        eprintln!("  (none found)");
    }
}

fn home_dir() -> Option<NormalizedPath> {
    let key = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
    env::var_os(key).map(|os| NormalizedPath::new(Path::new(&os)))
}

fn dump_daemon_lock() {
    let Some(home) = home_dir() else { return };
    let lock = home.join(".zccache").join("daemon.lock");
    eprintln!("daemon.lock ({}):", lock.display());
    match fs::read_to_string(&lock) {
        Ok(s) => {
            for line in s.lines() {
                eprintln!("  {line}");
            }
        }
        Err(e) => eprintln!("  (unreadable: {e})"),
    }
}

fn dump_tail(path: &Path, lines: usize) {
    match fs::read_to_string(path) {
        Ok(s) => {
            let collected: Vec<&str> = s.lines().collect();
            let start = collected.len().saturating_sub(lines);
            for line in &collected[start..] {
                eprintln!("  {line}");
            }
        }
        Err(e) => eprintln!("  (unreadable {}: {})", path.display(), e),
    }
}

fn dump_zccache_logs() {
    let Some(home) = home_dir() else { return };
    let log_dir = home.join(".zccache").join("logs");
    eprintln!("zccache logs ({}):", log_dir.display());
    let entries = match fs::read_dir(&log_dir) {
        Ok(it) => it,
        Err(e) => {
            eprintln!("  (no log dir: {e})");
            return;
        }
    };
    let mut found = false;
    for entry in entries.flatten() {
        let p = entry.path();
        if p.extension().and_then(|s| s.to_str()) == Some("log") {
            found = true;
            eprintln!("  --- tail of {} ---", p.display());
            dump_tail(&p, 50);
        }
    }
    if !found {
        eprintln!("  (no .log files)");
    }
}

fn dump_compile_journal() {
    let Some(home) = home_dir() else { return };
    let journal = home
        .join(".soldr")
        .join("cache")
        .join("zccache")
        .join("logs")
        .join("compile_journal.jsonl");
    eprintln!("soldr compile_journal ({}):", journal.display());
    if !journal.exists() {
        eprintln!("  (not present)");
        return;
    }
    dump_tail(&journal, 50);
}

// ---------------------------------------------------------------------------
// Daemon kill (with wait + lock cleanup)
// ---------------------------------------------------------------------------

/// How long [`kill_pids_and_wait`] will poll for the killed processes to
/// actually exit before giving up. `process.kill()` is asynchronous on Windows
/// (`TerminateProcess` returns before the process is reaped), so we have to
/// confirm exit before returning — otherwise `cargo check` races a dying
/// daemon whose named pipe has already vanished. See issue #152.
pub const KILL_DAEMON_WAIT: Duration = Duration::from_secs(2);

/// Find every running `zccache-daemon[.exe]` PID by walking the process table.
fn find_daemon_pids() -> Vec<u32> {
    use sysinfo::ProcessesToUpdate;

    let mut sys = sysinfo::System::new();
    sys.refresh_processes(ProcessesToUpdate::All, true);
    sys.processes()
        .iter()
        .filter_map(|(pid, process)| {
            let name = process.name().to_string_lossy();
            (name == "zccache-daemon" || name == "zccache-daemon.exe").then_some(pid.as_u32())
        })
        .collect()
}

/// Send a kill to each PID and poll until every PID is confirmed dead, or
/// `timeout` elapses. Returns once every PID is gone (or once we time out).
///
/// Uses [`crate::ipc::force_kill_process`] (TerminateProcess on Windows,
/// SIGKILL on Unix) rather than sysinfo's `Process::kill`, which has been
/// observed to silently fail to terminate Windows console children spawned via
/// `cmd /C` in tests — the kill bool returned `true` but the process kept
/// running. Going through the OS API directly is more reliable.
///
/// Exposed for tests; production code should call [`kill_daemon`].
pub fn kill_pids_and_wait(pids: &[u32], timeout: Duration) {
    if pids.is_empty() {
        return;
    }

    for pid in pids {
        if let Err(e) = crate::ipc::force_kill_process(*pid) {
            eprintln!("force_kill_process({pid}) failed: {e}");
        }
    }

    let deadline = Instant::now() + timeout;
    let poll = Duration::from_millis(25);
    loop {
        let any_alive = pids.iter().any(|pid| crate::ipc::is_process_alive(*pid));
        if !any_alive {
            return;
        }
        if Instant::now() >= deadline {
            let still_alive: Vec<u32> = pids
                .iter()
                .copied()
                .filter(|pid| crate::ipc::is_process_alive(*pid))
                .collect();
            eprintln!(
                "Warning: daemon PIDs still alive after {}ms: {:?}",
                timeout.as_millis(),
                still_alive
            );
            return;
        }
        std::thread::sleep(poll);
    }
}

/// Kill every running `zccache-daemon`, wait for them to actually exit, and
/// remove the stale `daemon.lock` file. Intended for callers that need to
/// replace the daemon binary on Windows without racing the dying process.
///
/// See issue #152: prior to this, `kill_daemon` returned immediately after
/// `process.kill()` and left the lock file pointing at the just-killed PID,
/// causing `cargo check` to fail with "cannot connect to daemon at \\.\\pipe\\..."
/// because parallel rustc workers raced the half-dead daemon.
///
/// WARNING: Do not call from the stop hook unless your stage actually
/// rebuilds `zccache-daemon.exe` — see issue #167 for why blanket calls
/// broke soldr session continuity (and triggered the #166 "unknown session"
/// failures downstream).
pub fn kill_daemon() {
    let pids = find_daemon_pids();
    if pids.is_empty() {
        // No daemon running. The lock file may still be stale from a prior
        // crash, so remove it defensively.
        crate::ipc::remove_lock_file();
        return;
    }
    for pid in &pids {
        eprintln!("Killing running daemon (PID {pid}) to unlock target binaries");
    }
    kill_pids_and_wait(&pids, KILL_DAEMON_WAIT);
    crate::ipc::remove_lock_file();
}

// ---------------------------------------------------------------------------
// Orphan daemon reaper
// ---------------------------------------------------------------------------

/// Kill any `zccache-daemon[.exe]` process whose parent PID is no longer
/// alive — i.e. a true orphan. Returns the PIDs that were killed.
///
/// This is conservative: we only reap when we can confirm the parent is
/// gone. Daemons spawned by a still-running supervisor are left alone.
pub fn reap_orphan_daemons() -> Vec<u32> {
    use sysinfo::{Pid, ProcessesToUpdate};

    let mut sys = sysinfo::System::new();
    sys.refresh_processes(ProcessesToUpdate::All, true);

    let alive: std::collections::HashSet<Pid> = sys.processes().keys().copied().collect();

    let mut killed = Vec::new();
    for (pid, process) in sys.processes() {
        let name = process.name().to_string_lossy();
        if name != "zccache-daemon" && name != "zccache-daemon.exe" {
            continue;
        }
        let parent = process.parent();
        let is_orphan = match parent {
            None => true,
            Some(ppid) => !alive.contains(&ppid),
        };
        if is_orphan {
            eprintln!("Reaping orphan zccache-daemon PID={pid} (parent {parent:?} gone)");
            if process.kill() {
                killed.push(pid.as_u32());
            }
        }
    }
    killed
}

// ---------------------------------------------------------------------------
// Tests (kept here so the runner is exercisable without external fixtures)
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Stdio;

    fn sleep_forever_cmd() -> Command {
        // A child that will never exit on its own. We use the host's interpreter
        // so this works on Windows (where `sleep` is not a binary).
        if cfg!(windows) {
            let mut c = Command::new("cmd");
            c.args(["/C", "ping -n 600 127.0.0.1 > NUL"]);
            c.stdout(Stdio::null()).stderr(Stdio::null());
            c
        } else {
            let mut c = Command::new("sh");
            c.args(["-c", "sleep 600"]);
            c.stdout(Stdio::null()).stderr(Stdio::null());
            c
        }
    }

    fn quick_exit_cmd() -> Command {
        if cfg!(windows) {
            let mut c = Command::new("cmd");
            c.args(["/C", "exit 0"]);
            c.stdout(Stdio::null()).stderr(Stdio::null());
            c
        } else {
            let mut c = Command::new("true");
            c.stdout(Stdio::null()).stderr(Stdio::null());
            c
        }
    }

    #[test]
    fn format_elapsed_examples() {
        assert_eq!(format_elapsed(Duration::from_millis(0)), "0.0s");
        assert_eq!(format_elapsed(Duration::from_millis(300)), "0.3s");
        assert_eq!(format_elapsed(Duration::from_millis(12_400)), "12.4s");
        assert_eq!(format_elapsed(Duration::from_secs(34)), "34.0s");
    }

    #[test]
    fn resolve_timeout_uses_default_when_unset() {
        // The env var is process-global; we don't mutate it here. Just check
        // the parser shape on a parser miss.
        let parsed = "0".parse::<u64>().ok().filter(|s| *s > 0);
        assert!(parsed.is_none(), "0 should be filtered out as invalid");

        let parsed = "abc".parse::<u64>().ok();
        assert!(parsed.is_none());
    }

    #[test]
    fn run_returns_exit_code_when_child_exits_normally() {
        let mut runner = StageRunner::new(Duration::from_secs(5));
        let outcome = runner.run("quick", &mut quick_exit_cmd());
        assert_eq!(outcome, StageOutcome::Exited(0));
    }

    #[test]
    fn run_times_out_and_kills_child_when_deadline_elapses() {
        // Deliberately tight timeout so the test runs in well under a second.
        let mut runner = StageRunner::new(Duration::from_millis(200));

        let outcome = runner.run("hang", &mut sleep_forever_cmd());
        assert_eq!(outcome, StageOutcome::GlobalTimeout);

        // last_stage should be set to the hung stage, so the timeout banner
        // can name it.
        assert_eq!(runner.last_stage(), Some("hang"));
    }

    #[test]
    fn run_skips_when_already_over_budget() {
        // Budget = 0 -> first run() call should immediately bail without
        // spawning anything.
        let mut runner = StageRunner::new(Duration::from_millis(0));
        let outcome = runner.run("noop", &mut quick_exit_cmd());
        assert_eq!(outcome, StageOutcome::GlobalTimeout);
    }

    #[test]
    fn progress_markers_capture_each_stage() {
        let mut runner =
            StageRunner::with_progress(Duration::from_secs(5), CapturingProgress::default());
        runner.start_stage("fmt-check");
        runner.start_stage("clippy");
        runner.start_stage("test");
        runner.finish();

        let progress = runner.progress_ref();
        assert_eq!(progress.lines.len(), 4);
        assert!(progress.lines[0].ends_with("-> fmt-check"));
        assert!(progress.lines[1].ends_with("-> clippy"));
        assert!(progress.lines[2].ends_with("-> test"));
        assert!(progress.lines[3].ends_with("done"));

        // Each marker starts with `[N.Ns]`.
        for line in &progress.lines {
            assert!(
                line.starts_with('[') && line.contains("s]"),
                "expected elapsed prefix in {line:?}"
            );
        }
    }

    #[test]
    fn reap_orphan_daemons_returns_a_vec_without_panic() {
        // We can't reliably create an orphan zccache-daemon in a unit test
        // environment, so we just exercise the happy path: the function must
        // walk the process table and return a Vec<u32> (possibly empty)
        // without panicking. The Drop test on a real machine catches
        // regressions where the function tries to kill the wrong process.
        let killed = reap_orphan_daemons();
        // No assertion on length: developer machines may have running
        // daemons whose parent is or isn't alive depending on workflow.
        let _ = killed.len();
    }

    #[test]
    fn run_emits_progress_marker_for_hung_stage() {
        let mut runner =
            StageRunner::with_progress(Duration::from_millis(200), CapturingProgress::default());
        let outcome = runner.run("hang", &mut sleep_forever_cmd());
        assert_eq!(outcome, StageOutcome::GlobalTimeout);

        let progress = runner.progress_ref();
        assert!(
            progress.lines.iter().any(|l| l.contains("-> hang")),
            "expected progress marker, got {:?}",
            progress.lines
        );
    }

    /// Issue #152 regression: `kill_pids_and_wait` must not return until the
    /// killed process is actually dead. The previous `kill_daemon`
    /// implementation called `process.kill()` and returned immediately, letting
    /// `cargo check` race a daemon that was still partially alive.
    ///
    /// In production the daemon is detached (init/launchd is its parent), so
    /// once it dies it is reaped immediately and `is_process_alive` flips
    /// false. In this test we are the parent, so we need a reaper thread to
    /// emulate the production lifecycle:
    ///
    /// * Unix: an unwait()-ed killed child becomes a zombie; `kill(pid, 0)`
    ///   keeps returning success until the parent calls `wait`.
    /// * Windows: an open process handle keeps the kernel object alive after
    ///   `TerminateProcess`, so `OpenProcess` keeps succeeding until the
    ///   handle is closed (which `Child::wait` does).
    #[test]
    fn kill_pids_and_wait_returns_only_after_child_is_dead() {
        let child = sleep_forever_cmd().spawn().expect("failed to spawn child");
        let pid = child.id();

        assert!(
            crate::ipc::is_process_alive(pid),
            "spawned child PID {pid} should be alive"
        );

        let waiter = std::thread::spawn(move || {
            let mut child = child;
            let _ = child.wait();
        });

        kill_pids_and_wait(&[pid], Duration::from_secs(5));

        waiter.join().expect("reaper thread panicked");

        assert!(
            !crate::ipc::is_process_alive(pid),
            "PID {pid} still alive after kill_pids_and_wait returned"
        );
    }

    #[test]
    fn kill_pids_and_wait_is_a_noop_for_empty_input() {
        // Should return immediately without polling or panicking.
        let start = Instant::now();
        kill_pids_and_wait(&[], Duration::from_secs(60));
        assert!(
            start.elapsed() < Duration::from_millis(500),
            "empty-input kill should return promptly, took {:?}",
            start.elapsed()
        );
    }
}