Skip to main content

amont_runtime/hooks/
common.rs

1//! Shared plumbing for the linter-orchestration hooks.
2//!
3//! Nine of them do the same four things: collect staged files of some kind,
4//! bail out if there are none, resolve a tool, run it. In shell that was ~65
5//! lines apiece, mostly duplicated; here it is a handful of helpers and each
6//! hook keeps only what is actually specific to it.
7
8use crate::git;
9use crate::ui::{error_sign, valid_sign, warning_sign};
10use std::path::Path;
11use std::process::{Command, Stdio};
12use std::sync::OnceLock;
13
14/// Staged files, deletions excluded, whose name ends with one of `exts`.
15/// The file set every check asks about, when it is not the staged one.
16///
17/// Set at most once, before any check runs, by `amont run --all-files`. A
18/// process-level override rather than a parameter because a check's signature
19/// is `(&[OsString])` — it never sees a `Ctx` — and threading a file set
20/// through twenty of them to serve one mode would be a worse trade than a
21/// value that is written once and read many times.
22///
23/// Same shape as `PushRefs`: read once, lent to every check that asks.
24static OVERRIDE: OnceLock<Vec<String>> = OnceLock::new();
25
26/// Set once the file set stops being the index.
27///
28/// `restage`'s own doc says what makes re-staging safe: the pre-commit stage
29/// holds the unstaged changes aside, so the tree contains the staged content
30/// and nothing else, and anything a formatter touched is by definition part of
31/// this commit. `amont run --all-files` replaces the file set with every
32/// tracked path — which is that precondition being FALSE.
33///
34/// With `amont.fix true`, every fixer's `restage(&files)` would then `git
35/// add` everything in the working tree that differs from the index, turning a
36/// read-only "does my tree pass" query into `git add .`. That is the hazard §2
37/// of docs/index-fidelity-and-run-modes.md names.
38///
39/// The gate hangs off the OVERRIDE rather than off a flag threaded through
40/// twenty check signatures, because the override IS the fact that matters. It
41/// therefore covers built-ins and `manifest::External::run` (which consults
42/// `fixing_enabled` in two places) in one change, and a future check cannot
43/// forget it.
44static NOT_THE_INDEX: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
45
46/// Make every subsequent `staged_files` answer from `files` instead of the
47/// index. Only the first call counts.
48pub fn override_file_set(files: Vec<String>) {
49    // Set unconditionally, even if a set already won the `OnceLock`: the
50    // statement "the file set is not the index" is true from the first call
51    // onwards regardless of which one supplied the paths.
52    NOT_THE_INDEX.store(true, std::sync::atomic::Ordering::SeqCst);
53    let _ = OVERRIDE.set(files);
54}
55
56/// Whether the file set every check sees is something other than the index.
57pub fn not_the_index() -> bool {
58    NOT_THE_INDEX.load(std::sync::atomic::Ordering::SeqCst)
59}
60
61/// An empty `exts` returns them all.
62///
63/// The UNFILTERED list is read from git ONCE per process and lent to every
64/// caller — the per-stage snapshot. Eleven of the pre-commit checks ask this
65/// question, concurrently, and each used to pay its own `git diff` spawn for
66/// an answer that cannot change while the stage runs: the index-fidelity
67/// hold pins the tree, and a fixer's `restage()` re-adds only paths already
68/// on this list. `PushRefs` ("read once and lent") and `Overrides` ("ONE
69/// subprocess for the whole stage") are the same pattern; this was the last
70/// hot question still answered per asker.
71pub fn staged_files(exts: &[&str]) -> Vec<String> {
72    if let Some(all) = OVERRIDE.get() {
73        return all
74            .iter()
75            .filter(|f| exts.is_empty() || exts.iter().any(|e| f.ends_with(e)))
76            .cloned()
77            .collect();
78    }
79    static INDEX: OnceLock<Vec<String>> = OnceLock::new();
80    INDEX
81        .get_or_init(|| {
82            match git::stdout_paths(&["diff", "--diff-filter=d", "--cached", "--name-only"]) {
83                Some(files) => files,
84                // The third member of a bug family (`repo_hooks`, the push
85                // gates): git FAILING is not git answering "empty", and a
86                // stage that judges an empty set on a git failure reports
87                // clean having verified nothing. Say so — once, this cache
88                // being the once — and still fail open: pre-commit's job is
89                // never to block a commit over its own plumbing.
90                None => {
91                    warn(
92                        "git would not list the staged files — the checks are judging \
93                         an EMPTY set, not a verified one",
94                    );
95                    Vec::new()
96                }
97            }
98        })
99        .iter()
100        .filter(|f| exts.is_empty() || exts.iter().any(|e| f.ends_with(e)))
101        .cloned()
102        .collect()
103}
104
105/// Every path the index holds — what the repository CARRIES, as opposed to
106/// what the current change touches.
107///
108/// The one honest source for a [`Scope`](crate::check::Scope) opt-in marker.
109/// [`staged_files`] answers a different question, and answering the opt-in one
110/// with it makes a `+marker` row fire only when the marker itself is in the
111/// change — which is never, in ordinary work.
112///
113/// `ls-files` reads the INDEX, not `HEAD`, so a marker being added by this very
114/// commit already counts. A marker sitting untracked on disk does not, which is
115/// the same rule the manifest itself lives by: commit it, or it is not real.
116///
117/// Fails OPEN, unlike `staged_files`, and the asymmetry is deliberate. There,
118/// an empty list means the checks judge nothing and say so. Here, an empty list
119/// would silently switch every gated check OFF — a check that has quietly never
120/// run is the one failure this design is arranged against — so a git failure
121/// reports the check as opted in and lets the command itself be the judge. A
122/// command that then finds no project fails to spawn, which is `Unavailable`:
123/// a warning, never a block.
124/// `None` when git would not answer — which is NOT the same as an empty
125/// repository, and the caller must not flatten the two. An empty `Vec` opts
126/// every gated check OUT; `None` means "unverified", and the gate opts them IN.
127pub fn tracked_files() -> Option<Vec<String>> {
128    static TRACKED: OnceLock<Option<Vec<String>>> = OnceLock::new();
129    TRACKED
130        .get_or_init(|| match git::stdout_paths(&["ls-files"]) {
131            Some(files) => Some(files),
132            None => {
133                warn(
134                    "git would not list the repository's files — opt-in gated checks \
135                     will run rather than be skipped on an unverified answer",
136                );
137                None
138            }
139        })
140        .clone()
141}
142
143/// Repo root, or "." when git cannot say.
144///
145/// **For CHECK BODIES ONLY.** The fallback is safe there and nowhere else: git
146/// invokes a hook with the working tree as the current directory, so a check
147/// that reaches this line is already standing in the repository, and "." is the
148/// right answer rather than a guess.
149///
150/// Anything a user types — `amont agents-md`, `install`, `trust`, `restore`
151/// — can be typed from any directory on the machine, and there the fallback is
152/// not a fallback but a wrong answer that reads as a right one. Use
153/// [`repo_root_checked`] at every command entry point.
154pub fn repo_root() -> String {
155    // Cached: the answer is a property of the process's repository, and
156    // every check asked it through its own subprocess.
157    static ROOT: OnceLock<String> = OnceLock::new();
158    ROOT.get_or_init(|| {
159        git::stdout(&["rev-parse", "--show-toplevel"]).unwrap_or_else(|| ".".into())
160    })
161    .clone()
162}
163
164/// Repo root, or an error naming the problem.
165///
166/// The same question as [`repo_root`] without the "." — because "." is a
167/// PLAUSIBLE root, and that is what made it dangerous. `amont agents-md`
168/// run outside a repository did not fail; it resolved the root to the current
169/// directory and wrote `./AGENTS.md` into whatever directory the user happened
170/// to be standing in, then printed `wrote ./AGENTS.md` as if that were the
171/// answer. Same shape in `install`'s two prompts, in `trust` (which then
172/// looked for a manifest, and would have recorded trust, under `.`) and in
173/// `restore`.
174///
175/// Every one of those is a command somebody types, and a command somebody
176/// types is a command they can type from `~`. There is no correct behaviour
177/// available to this function when git cannot answer, so it does not invent
178/// one.
179pub fn repo_root_checked() -> Result<String, String> {
180    git::stdout(&["rev-parse", "--show-toplevel"])
181        .filter(|s| !s.is_empty())
182        .ok_or_else(|| "not inside a git repository".to_string())
183}
184
185/// Resolve a tool, preferring the repo's PINNED copy so the hook matches CI.
186///
187///
188/// Order: `<root>/node_modules/.bin/<tool>`, then the MAIN worktree's (a linked
189/// worktree has no node_modules of its own — this is why the shell version
190/// consulted the git common dir), then PATH.
191pub fn resolve_tool(root: &str, tool: &str) -> Option<Vec<String>> {
192    // Same extension problem as `which`: an npm-installed binary is `eslint.cmd`
193    // on Windows, so the bare name misses the repo's PINNED copy and the hook
194    // silently falls through to an ambient one.
195    if let Some(p) = in_bin_dir(&format!("{root}/node_modules/.bin"), tool) {
196        return Some(vec![p]);
197    }
198    if let Some(common) = git::stdout(&["rev-parse", "--path-format=absolute", "--git-common-dir"])
199    {
200        if let Some(main) = Path::new(&common).parent() {
201            if let Some(p) = in_bin_dir(&main.join("node_modules/.bin").to_string_lossy(), tool) {
202                return Some(vec![p]);
203            }
204        }
205    }
206    if let Some(full) = which(tool) {
207        return Some(vec![full]);
208    }
209    // `npx --no-install`: never silently download a random latest version — a
210    // hook that quietly pulls a different linter than CI uses is worse than one
211    // that skips.
212    if which("npx").is_some()
213        && Command::new(program("npx"))
214            .args(["--no-install", tool, "--version"])
215            .current_dir(root)
216            .stdin(Stdio::null())
217            .stdout(Stdio::null())
218            .stderr(Stdio::null())
219            .status()
220            .map(|s| s.success())
221            .unwrap_or(false)
222    {
223        return Some(vec![
224            program("npx"),
225            "--no-install".to_string(),
226            tool.to_string(),
227        ]);
228    }
229    None
230}
231
232/// First match for `tool` on PATH.
233///
234/// Windows executables carry an extension — `git` is `git.exe`, an npm-installed
235/// `eslint` is `eslint.cmd` — so the bare name finds nothing there. PATHEXT is
236/// the OS's own list of what counts as executable; fall back to the usual set
237/// when it is unset. Found by the Windows CI job on its first run, where
238/// `which("git")` returned None on a machine that plainly has git.
239pub fn which(tool: &str) -> Option<String> {
240    which_on(&std::env::var_os("PATH")?, tool)
241}
242
243/// [`which`] against an EXPLICIT path list — the seam its own test needs.
244///
245/// The test that pins the Windows extension order used to `set_var("PATH")`
246/// around the call, which is process-global: for the length of that call
247/// every OTHER test in the binary — 340 of them, running in parallel, many
248/// spawning git — had a PATH containing one fake tool and nothing else. A
249/// git spawned in that window fails with "not found", which is not a
250/// transient `git::retrying` may retry (correctly: it is a hard error), so
251/// the caller reads it as git's ANSWER. In `gate_stamp` that answer is
252/// "nothing is stamped". Passing the path in deletes the shared state
253/// rather than guarding it — a lock only protects the callers who remember
254/// to take it, and every future test here would have to remember.
255pub fn which_on(path: &std::ffi::OsStr, tool: &str) -> Option<String> {
256    let exts: Vec<String> = if cfg!(windows) {
257        std::env::var("PATHEXT")
258            .unwrap_or_else(|_| ".COM;.EXE;.BAT;.CMD".into())
259            .split(';')
260            .filter(|e| !e.is_empty())
261            .map(|e| e.to_lowercase())
262            .collect()
263    } else {
264        Vec::new()
265    };
266    for dir in std::env::split_paths(path) {
267        // On Windows the EXTENSION forms come first. A node install ships both
268        // `npm` (an extensionless shell script, for MSYS) and `npm.cmd` in the
269        // same directory; preferring the bare name hands CreateProcess a shell
270        // script it cannot execute — "%1 is not a valid Win32 application" —
271        // and the hook reports an installed tool as broken.
272        for e in &exts {
273            let c = dir.join(format!("{tool}{e}"));
274            if c.is_file() {
275                return Some(c.to_string_lossy().into_owned());
276            }
277        }
278        let bare = dir.join(tool);
279        if bare.is_file() {
280            return Some(bare.to_string_lossy().into_owned());
281        }
282    }
283    None
284}
285
286/// `<dir>/<tool>`, trying the Windows executable extensions too.
287fn in_bin_dir(dir: &str, tool: &str) -> Option<String> {
288    let bare = Path::new(dir).join(tool);
289    if bare.is_file() {
290        return Some(bare.to_string_lossy().into_owned());
291    }
292    if cfg!(windows) {
293        for e in [".cmd", ".exe", ".bat", ".ps1"] {
294            let c = Path::new(dir).join(format!("{tool}{e}"));
295            if c.is_file() {
296                return Some(c.to_string_lossy().into_owned());
297            }
298        }
299    }
300    None
301}
302
303/// Resolve a tool name to a full path for spawning.
304///
305/// `Command::new("npm")` cannot execute `npm.cmd`: Rust does no PATHEXT
306/// resolution, so on Windows every bare-name spawn fails with "program not
307/// found" and the hook reports the tool as broken rather than absent. Found by
308/// the Windows job on its first FULL-suite run — the smoke never spawned a
309/// tool, so it could not have surfaced this.
310///
311/// Falls back to the name unchanged, so a caller still gets a sensible error.
312pub fn program(name: &str) -> String {
313    which(name).unwrap_or_else(|| name.to_string())
314}
315
316/// The first of `names` that exists at the repo root — how these hooks decide
317/// a repo has opted into a tool.
318pub fn first_existing(root: &str, names: &[&str]) -> Option<String> {
319    names
320        .iter()
321        .find(|n| Path::new(root).join(n).exists())
322        .map(|n| (*n).to_string())
323}
324
325/// Strip git's own environment before handing a Command to another tool.
326///
327/// git exports GIT_DIR, GIT_INDEX_FILE, GIT_WORK_TREE and friends to every
328/// hook. Those OVERRIDE the working directory, so any tool that shells out to
329/// git operates on the hook's repository no matter where it was launched.
330///
331/// That is not hypothetical: `pre-push-cargo-test` runs a project's test suite,
332/// and this repo's own suite creates throwaway repos and commits to them. With
333/// GIT_DIR inherited, `git commit` in a test wrote into the REAL repository —
334/// an actual stray commit, authored by the test fixture, pushed to a branch.
335///
336/// A test suite should behave exactly as it does when run by hand, which means
337/// seeing no git environment at all.
338pub fn strip_git_env(cmd: &mut Command) {
339    for (k, _) in std::env::vars_os() {
340        let key = k.to_string_lossy();
341        if key.starts_with("GIT_") {
342            cmd.env_remove(&k);
343        }
344    }
345}
346
347/// The wall-clock CEILING for one check's spawned command, in seconds.
348///
349/// `amont.timeout`, default 3600. This used to be 600 and to be the only
350/// clock, which made it answer two different questions with one number: "is
351/// this tool stuck?" and "is this suite slow?". A stuck tool is silent, and
352/// [`idle_timeout`] catches it in minutes; what is left for the ceiling is
353/// the tool that keeps printing and never finishes, which is rare enough to
354/// afford an hour. `0` disables. Read once per process: twenty concurrent
355/// checks must not each spawn a `git config` to learn the same number.
356pub fn check_timeout() -> u64 {
357    static TIMEOUT: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
358    *TIMEOUT.get_or_init(|| crate::config::integer_or("amont.timeout", 3600, 0..=86_400) as u64)
359}
360
361/// The SILENCE budget: how long a spawned command may go without writing a
362/// byte before it is judged stuck, in seconds.
363///
364/// `amont.idleTimeout`, default 120. A hang is silent; a slow test suite
365/// talks — `cargo test` prints a line per test. Killing on silence catches
366/// the captive portal, the deadlocked lock file and the tool waiting on a
367/// prompt nobody will answer FASTER than a ten-minute wall clock did, while
368/// letting a chatty twenty-five-minute suite finish. Only applies where the
369/// output is observed (the captured runners); a command inheriting the
370/// terminal directly answers to the ceiling alone. `0` disables.
371pub fn idle_timeout() -> u64 {
372    static IDLE: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
373    *IDLE.get_or_init(|| crate::config::integer_or("amont.idleTimeout", 120, 0..=86_400) as u64)
374}
375
376/// `secs` as people read it: `12s`, `8m12s`, `1h02m`.
377pub fn human_secs(secs: u64) -> String {
378    match secs {
379        s if s < 60 => format!("{s}s"),
380        s if s < 3600 => format!("{}m{:02}s", s / 60, s % 60),
381        s => format!("{}h{:02}m", s / 3600, (s % 3600) / 60),
382    }
383}
384
385/// When a spawned command last wrote a byte, shared between the reader
386/// threads that see the bytes and the wait loop that judges the silence.
387pub struct Activity {
388    last: std::sync::Mutex<std::time::Instant>,
389}
390
391impl Activity {
392    pub fn new() -> std::sync::Arc<Activity> {
393        std::sync::Arc::new(Activity {
394            last: std::sync::Mutex::new(std::time::Instant::now()),
395        })
396    }
397    pub fn touch(&self) {
398        *self.last.lock().unwrap_or_else(|p| p.into_inner()) = std::time::Instant::now();
399    }
400    pub fn quiet_for(&self) -> std::time::Duration {
401        self.last
402            .lock()
403            .unwrap_or_else(|p| p.into_inner())
404            .elapsed()
405    }
406}
407
408/// The deadline for a network PROBE — an `ls-remote` asked before the real
409/// work, not the work itself. Capped at 30s below [`check_timeout`]: a
410/// probe answers in a second or two when the network is there at all, and
411/// a healthy `amont.timeout` of ten minutes is sized for a test suite, not
412/// for deciding whether the remote is reachable. Shrinking `amont.timeout`
413/// below the cap shrinks this too, and `0` keeps meaning no deadline —
414/// somebody who disabled the clock disabled all of it.
415pub fn network_probe_budget() -> u64 {
416    match check_timeout() {
417        0 => 0,
418        t => t.min(30),
419    }
420}
421
422/// Which clock killed a command.
423#[derive(Debug, Clone, Copy, PartialEq, Eq)]
424pub enum Why {
425    /// The wall-clock ceiling, `amont.timeout`, in seconds.
426    Ceiling(u64),
427    /// The silence budget, `amont.idleTimeout`, in seconds.
428    Silence(u64),
429}
430
431/// A command killed by a clock — what happened, said with enough to tell
432/// "slow" from "stuck", which is the whole reason there are two clocks.
433#[derive(Debug, Clone, Copy)]
434pub struct Killed {
435    pub why: Why,
436    /// How long it had been running.
437    pub ran_secs: u64,
438    /// How long since its last output; `None` when the output was not ours
439    /// to observe (inherited stdio).
440    pub quiet_secs: Option<u64>,
441}
442
443/// What became of a command run under the deadline.
444pub enum Ran {
445    Status(std::process::ExitStatus),
446    /// Killed by a clock; see [`Killed`].
447    TimedOut(Killed),
448}
449
450/// `cmd.status()`, bounded by [`check_timeout`].
451///
452/// Without a bound, one hung tool — a linter deadlocked on a lock file, a
453/// plugin doing network I/O — blocked the commit FOREVER, and it hung inside
454/// the index-fidelity hold: the user's unstaged changes parked in `$GIT_DIR`,
455/// their tree showing staged content only, for as long as they were willing
456/// to wait. The learned response to that is `--no-verify`, permanently —
457/// which disarms every check to escape one.
458///
459/// The kill reaches the direct child only. A grandchild that detached
460/// survives, orphaned — but the COMMIT is no longer hostage to it, which is
461/// the property that matters.
462pub fn status_within(cmd: &mut Command) -> std::io::Result<Ran> {
463    status_within_secs(cmd, check_timeout())
464}
465
466/// [`status_within`] with an explicit ceiling — the testable seam. The
467/// output is inherited, so nobody sees the bytes and the silence budget
468/// cannot apply; the ceiling is the only clock.
469pub fn status_within_secs(cmd: &mut Command, budget_secs: u64) -> std::io::Result<Ran> {
470    if budget_secs == 0 {
471        return cmd.status().map(Ran::Status);
472    }
473    let mut child = cmd.spawn()?;
474    wait_within(&mut child, budget_secs, 0, None)
475}
476
477/// Spawn `cmd` with both streams piped, hand every chunk to `on_output` as
478/// it arrives, and wait under BOTH clocks — the reader threads are what
479/// make the silence budget observable. The shared runner behind the
480/// streamed, captured and discarded variants.
481fn run_observed(
482    cmd: &mut Command,
483    on_output: impl Fn(&[u8]) + Send + Sync + 'static,
484) -> std::io::Result<Ran> {
485    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
486    let activity = Activity::new();
487    let on_output = std::sync::Arc::new(on_output);
488    let mut child = cmd.spawn()?;
489    // The silence is the CHILD's, so its clock starts when the child does:
490    // a spawn that itself took a second on a loaded machine is not a
491    // second the tool spent saying nothing.
492    activity.touch();
493    let mut readers = Vec::new();
494    for pipe in [
495        child
496            .stdout
497            .take()
498            .map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
499        child
500            .stderr
501            .take()
502            .map(|p| Box::new(p) as Box<dyn std::io::Read + Send>),
503    ]
504    .into_iter()
505    .flatten()
506    {
507        let activity = std::sync::Arc::clone(&activity);
508        let on_output = std::sync::Arc::clone(&on_output);
509        readers.push(std::thread::spawn(move || {
510            let mut pipe = pipe;
511            let mut chunk = [0u8; 4096];
512            loop {
513                match std::io::Read::read(&mut pipe, &mut chunk) {
514                    Ok(0) | Err(_) => break,
515                    Ok(n) => {
516                        activity.touch();
517                        on_output(&chunk[..n]);
518                    }
519                }
520            }
521        }));
522    }
523    let ran = wait_within(&mut child, check_timeout(), idle_timeout(), Some(&activity));
524    for r in readers {
525        let _ = r.join();
526    }
527    ran
528}
529
530/// [`status_within`], with the child's stdout and stderr CAPTURED into the
531/// calling check's slot instead of inherited — the other half of one-check-
532/// one-block: a linter's twelve lines used to land on the shared terminal
533/// between two other checks' lines. Falls back to plain [`status_within`]
534/// when no slot is installed on this thread (`amont.progress false`, or a
535/// spawn outside a stage), which is byte-for-byte the old behaviour.
536///
537/// stdout and stderr merge in ARRIVAL order inside the block, which is what
538/// the terminal showed before. The readers are threads, not processes, and
539/// they are joined before the status is returned so a block can never grow
540/// after its check finished.
541pub fn status_streamed(cmd: &mut Command) -> std::io::Result<Ran> {
542    let Some((stage, idx)) = crate::live::current_sink() else {
543        return status_within(cmd);
544    };
545    if crate::live::watching() {
546        // The block lands on a real terminal but the tool sees a pipe and
547        // would strip its colors; the big three opt-in knobs put them back.
548        cmd.env("FORCE_COLOR", "1")
549            .env("CLICOLOR_FORCE", "1")
550            .env("CARGO_TERM_COLOR", "always");
551    }
552    run_observed(cmd, move |bytes| stage.append_raw(idx, bytes))
553}
554
555/// Run to completion under the `amont.timeout` deadline with stdout and
556/// stderr CAPTURED into a string the caller can parse — what the audit
557/// checks need: their verdict lives in the tool's output, not its exit
558/// code alone. Arrival-ordered merge of both streams, like
559/// [`status_streamed`]'s blocks. `None` when the child cannot be spawned.
560pub fn capture_within(cmd: &mut Command) -> Option<(Ran, String)> {
561    let text = std::sync::Arc::new(std::sync::Mutex::new(String::new()));
562    let sink = std::sync::Arc::clone(&text);
563    let ran = run_observed(cmd, move |bytes| {
564        sink.lock()
565            .unwrap_or_else(|p| p.into_inner())
566            .push_str(&String::from_utf8_lossy(bytes));
567    })
568    .ok()?;
569    let text = std::sync::Arc::try_unwrap(text)
570        .map(|m| m.into_inner().unwrap_or_else(|p| p.into_inner()))
571        .unwrap_or_default();
572    Some((ran, text))
573}
574
575/// The two-clock wait over an already-spawned child — shared by every
576/// runner. `wall_secs` is the ceiling, `idle_secs` the silence budget; each
577/// `0` means that clock is off, and the silence budget is also off when
578/// there is no [`Activity`] to consult (inherited stdio).
579pub(crate) fn wait_within(
580    child: &mut std::process::Child,
581    wall_secs: u64,
582    idle_secs: u64,
583    activity: Option<&Activity>,
584) -> std::io::Result<Ran> {
585    let started = std::time::Instant::now();
586    let ceiling = (wall_secs > 0).then(|| started + std::time::Duration::from_secs(wall_secs));
587    let silence = match activity {
588        Some(_) if idle_secs > 0 => Some(std::time::Duration::from_secs(idle_secs)),
589        _ => None,
590    };
591    if ceiling.is_none() && silence.is_none() {
592        return child.wait().map(Ran::Status);
593    }
594    loop {
595        if let Some(status) = child.try_wait()? {
596            return Ok(Ran::Status(status));
597        }
598        let now = std::time::Instant::now();
599        let quiet = activity.map(|a| a.quiet_for());
600        let why = judge(
601            now.duration_since(started),
602            quiet,
603            ceiling.map(|_| wall_secs),
604            silence.map(|_| idle_secs),
605        );
606        if let Some(why) = why {
607            let _ = child.kill();
608            let _ = child.wait();
609            return Ok(Ran::TimedOut(Killed {
610                why,
611                ran_secs: now.duration_since(started).as_secs(),
612                quiet_secs: quiet.map(|q| q.as_secs()),
613            }));
614        }
615        std::thread::sleep(std::time::Duration::from_millis(25));
616    }
617}
618
619/// Which clock, if any, has fired — the decision, with no process or
620/// clock of its own so it can be tested to the second.
621///
622/// `ran` is how long the command has been running; `quiet` how long since
623/// it last wrote, `None` when nobody is watching its output. `ceiling` and
624/// `silence` are the two budgets in seconds, `None` when that clock is off.
625/// The ceiling wins when both have fired: it is the larger claim, and the
626/// message for it carries the silence figure anyway.
627pub fn judge(
628    ran: std::time::Duration,
629    quiet: Option<std::time::Duration>,
630    ceiling: Option<u64>,
631    silence: Option<u64>,
632) -> Option<Why> {
633    if let Some(wall) = ceiling {
634        if ran >= std::time::Duration::from_secs(wall) {
635            return Some(Why::Ceiling(wall));
636        }
637    }
638    if let (Some(idle), Some(q)) = (silence, quiet) {
639        if q >= std::time::Duration::from_secs(idle) {
640            return Some(Why::Silence(idle));
641        }
642    }
643    None
644}
645
646/// Say a command was killed, by which clock, and what that tells you.
647///
648/// The two clocks exist to answer two different questions, so the message
649/// answers the one that was asked: silence means stuck — look at the tool;
650/// the ceiling with recent output means slow — raise the ceiling.
651pub fn say_timed_out(what: &str, k: Killed) {
652    match k.why {
653        Why::Silence(budget) => fail(&format!(
654            "{} printed nothing for {} and was killed after {} — a tool this quiet is \
655             usually stuck, not slow. {} raises the silence budget (0 disables)",
656            hl(what),
657            human_secs(budget),
658            human_secs(k.ran_secs),
659            hl("git config amont.idleTimeout <secs>")
660        )),
661        Why::Ceiling(budget) => {
662            let verdict = match k.quiet_secs {
663                Some(q) if q < 30 => format!(
664                    " It was still printing ({} since its last line): slow, not stuck.",
665                    human_secs(q)
666                ),
667                Some(q) => format!(" Its last output was {} ago.", human_secs(q)),
668                None => String::new(),
669            };
670            fail(&format!(
671                "{} timed out: ran for {} and was killed at the ceiling. {} raises it \
672                 (0 disables).{verdict}",
673                hl(what),
674                human_secs(budget),
675                hl("git config amont.timeout <secs>")
676            ))
677        }
678    }
679}
680
681/// [`status_within`], collapsed to "did it exit 0" — the shape the one-shot
682/// tool spawns want. A timeout says so, names `what`, and reads as failure.
683pub fn bounded_success(cmd: &mut Command, what: &str) -> bool {
684    match status_streamed(cmd) {
685        Ok(Ran::Status(s)) => s.success(),
686        Ok(Ran::TimedOut(b)) => {
687            say_timed_out(what, b);
688            false
689        }
690        Err(_) => false,
691    }
692}
693
694/// Run `argv` from `root`, inheriting stdio. True when it exits 0.
695pub fn run(root: &str, argv: &[String], extra: &[String]) -> bool {
696    let Some((program, rest)) = argv.split_first() else {
697        return true;
698    };
699    let mut cmd = Command::new(program);
700    cmd.args(rest)
701        .args(extra)
702        .current_dir(root)
703        .stdin(Stdio::null());
704    strip_git_env(&mut cmd);
705    bounded_success(&mut cmd, program)
706}
707
708/// As [`run`], but with the tool's own output discarded.
709///
710/// For a pass whose only job is to decide something — prettier's `--check`,
711/// ruff's `--fix` sweep — where the offenders are printed once, by the pass
712/// that reports them, rather than twice.
713pub fn run_quiet(root: &str, argv: &[String], extra: &[String]) -> bool {
714    let Some((program, rest)) = argv.split_first() else {
715        return true;
716    };
717    let mut cmd = Command::new(program);
718    cmd.args(rest)
719        .args(extra)
720        .current_dir(root)
721        .stdin(Stdio::null());
722    strip_git_env(&mut cmd);
723    // Deliberately NOT the streamed runner: this helper's contract is that
724    // the output is discarded, and capture would resurrect it into the
725    // block. Observed and dropped instead of `/dev/null`, so the silence
726    // clock still sees whether the tool is alive.
727    match run_observed(&mut cmd, |_| {}) {
728        Ok(Ran::Status(s)) => s.success(),
729        Ok(Ran::TimedOut(b)) => {
730            say_timed_out(program, b);
731            false
732        }
733        Err(_) => false,
734    }
735}
736
737/// Whether the user asked for checks to repair what they find.
738///
739/// OFF by default. `git config amont.fix true` turns it on, per repository,
740/// because a hook that edits your files without being asked is a larger
741/// surprise than one that complains — and because with index fidelity in place
742/// the repair lands in the commit you are making, which is a bigger claim to
743/// make on somebody's behalf than printing an error.
744pub fn fixing_enabled() -> bool {
745    // Never while the file set is not the index — see `NOT_THE_INDEX`.
746    !not_the_index() && fixing_requested()
747}
748
749/// What the CONFIG says, ignoring whether the current run may act on it.
750///
751/// Split out so `run_all` can tell the difference between "fixing is off" and
752/// "you asked for fixing and this mode will not do it", and say the second out
753/// loud instead of silently ignoring the key.
754pub fn fixing_requested() -> bool {
755    crate::config::boolean_or("amont.fix", false)
756}
757
758/// What a re-stage actually did. THREE answers, because the old `bool`
759/// conflated two of them and the conflation shipped unformatted code.
760///
761/// `prettier.rs` read `if run_quiet(write) && restage(&files) { … Fixed }`. When
762/// `git add` FAILED, `restage` returned `false` — indistinguishable from
763/// "nothing needed staging" — so control fell through to a second `--check`
764/// pass, which inspected the NOW-FORMATTED WORKING TREE, passed, printed
765/// "Prettier passed" and returned `Outcome::Passed`. The index still held the
766/// unformatted content, so the commit contained unformatted code and the hook
767/// said it had passed. `manifest.rs` had the same shape.
768#[derive(Debug, Clone, PartialEq, Eq)]
769pub enum Restaged {
770    /// No path differed from the index — nothing to do, and nothing wrong.
771    Nothing,
772    /// `git add` succeeded; the index now holds the repair.
773    Staged,
774    /// `git add` failed, carrying the paths it could not stage. The index
775    /// holds content the fixer has already replaced on disk, so this MUST be
776    /// loud at every call site — and naming the files is the difference
777    /// between a message somebody can act on and one they cannot.
778    Failed(Vec<String>),
779}
780
781/// Serialises this process's own `git add` calls.
782///
783/// pre-commit runs its checks concurrently (`dispatch.rs`), and up to three of
784/// them can re-stage. git takes `$GIT_DIR/index.lock` exclusively, so two
785/// concurrent `git add`s in the same repository make one of them fail — which,
786/// before `Restaged`, was silently read as "nothing moved". Holding this across
787/// the `git add` removes self-contention entirely; the retry below is only for
788/// OTHER processes.
789static INDEX_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
790
791/// Re-stage exactly the paths a fixer rewrote, and say what happened.
792///
793/// Safe ONLY because the pre-commit stage holds unstaged changes aside: the
794/// tree contains the staged content and nothing else, so anything a formatter
795/// touched is by definition part of this commit. Without that, re-staging would
796/// sweep in work the author deliberately kept back.
797pub fn restage(paths: &[String]) -> Restaged {
798    // Belt and braces alongside `fixing_enabled`: a future fixer that forgets
799    // the gate still cannot turn `amont run --all-files` into `git add .`.
800    if not_the_index() {
801        return Restaged::Nothing;
802    }
803    let changed: Vec<String> = paths
804        .iter()
805        .filter(|p| !git::succeeds(&["diff", "--quiet", "--", p]))
806        .cloned()
807        .collect();
808    if changed.is_empty() {
809        return Restaged::Nothing;
810    }
811    let mut args = vec!["add", "--"];
812    args.extend(changed.iter().map(String::as_str));
813
814    let _serialised = INDEX_LOCK.lock().unwrap_or_else(|e| e.into_inner());
815    // Another PROCESS can hold `index.lock` — a `git status` from an editor, a
816    // second hook in a linked worktree. Back off and retry rather than
817    // reporting a transient collision as a failed repair. `git add` of the same
818    // paths is idempotent: it records the paths' current worktree content, so
819    // running it twice records the same thing twice and cannot double-stage.
820    const BACKOFF_MS: [u64; 3] = [50, 150, 400];
821    if git::succeeds(&args) {
822        return Restaged::Staged;
823    }
824    for wait in BACKOFF_MS {
825        std::thread::sleep(std::time::Duration::from_millis(wait));
826        if git::succeeds(&args) {
827            return Restaged::Staged;
828        }
829    }
830    Restaged::Failed(changed)
831}
832
833pub fn ok(msg: &str) {
834    crate::live::say(&format!("{} {msg}", valid_sign()));
835}
836pub fn fail(msg: &str) {
837    crate::live::say(&format!("{} {msg}", error_sign()));
838}
839pub fn warn(msg: &str) {
840    crate::live::say(&format!("{} {msg}", warning_sign()));
841}
842/// A line with no sign of its own — what a check's direct `println!` becomes,
843/// so it lands in the check's block instead of interleaving. See `live::say`.
844pub fn say(msg: &str) {
845    crate::live::say(msg);
846}
847
848/// Orange, for the fragments these hooks highlight.
849pub fn hl(s: &str) -> String {
850    crate::ui::highlight(s)
851}
852
853#[cfg(test)]
854mod tests {
855
856    /// The two clocks, decided to the second. A chatty command outlives any
857    /// silence budget however long it runs; a silent one dies at the budget
858    /// however short; a command nobody watches answers to the ceiling only.
859    #[test]
860    fn the_clocks_judge_silence_and_ceiling_separately() {
861        use super::{judge, Why};
862        use std::time::Duration as D;
863        let s = D::from_secs;
864        // Chatty and long: past a five-second silence budget, still fine.
865        assert_eq!(judge(s(900), Some(s(0)), Some(3600), Some(5)), None);
866        assert_eq!(judge(s(900), Some(s(4)), Some(3600), Some(5)), None);
867        // Silent for the budget: killed, and the silence is blamed.
868        assert_eq!(
869            judge(s(30), Some(s(5)), Some(3600), Some(5)),
870            Some(Why::Silence(5))
871        );
872        // Unobserved output: the silence clock cannot run at all.
873        assert_eq!(judge(s(900), None, Some(3600), Some(5)), None);
874        // The ceiling fires on elapsed time whatever the output is doing.
875        assert_eq!(
876            judge(s(3600), Some(s(0)), Some(3600), Some(120)),
877            Some(Why::Ceiling(3600))
878        );
879        // Both fired at once: the ceiling is the answer.
880        assert_eq!(
881            judge(s(3600), Some(s(600)), Some(3600), Some(120)),
882            Some(Why::Ceiling(3600))
883        );
884        // Both off: nothing ever fires.
885        assert_eq!(judge(s(86_400), Some(s(86_400)), None, None), None);
886        // Only silence on: no ceiling, however long it runs.
887        assert_eq!(judge(s(86_400), Some(s(1)), None, Some(120)), None);
888    }
889
890    /// The deadline kills what outlives it and reports what finished.
891    #[cfg(unix)]
892    #[test]
893    fn the_deadline_kills_a_sleeper_and_spares_a_finisher() {
894        let started = std::time::Instant::now();
895        let mut slow = Command::new(program("sleep"));
896        slow.arg("300").stdin(Stdio::null());
897        match status_within_secs(&mut slow, 1) {
898            Ok(Ran::TimedOut(Killed {
899                why: Why::Ceiling(1),
900                quiet_secs: None,
901                ..
902            })) => {}
903            other => panic!("expected TimedOut(1), got {:?}", other.map(|_| "ran")),
904        }
905        assert!(
906            started.elapsed() < std::time::Duration::from_secs(60),
907            "the kill did not happen at the deadline"
908        );
909
910        let mut quick = Command::new(program("true"));
911        quick.stdin(Stdio::null());
912        match status_within_secs(&mut quick, 60) {
913            Ok(Ran::Status(s)) => assert!(s.success()),
914            other => panic!("expected a clean exit, got {:?}", other.map(|_| "?")),
915        }
916    }
917
918    use super::*;
919
920    #[test]
921    fn which_finds_a_real_binary_and_not_a_fake_one() {
922        assert!(which("git").is_some());
923        assert!(which("definitely-not-a-real-binary-xyz").is_none());
924    }
925
926    /// On Windows a tool can exist BOTH as an extensionless shell script and as
927    /// a .cmd/.exe in the same directory; only the latter is executable by
928    /// CreateProcess, so the extension forms must win.
929    #[test]
930    #[cfg(windows)]
931    fn windows_prefers_an_executable_extension_over_a_bare_file() {
932        let dir = std::env::temp_dir().join("amont-which-order");
933        let _ = std::fs::create_dir_all(&dir);
934        std::fs::write(dir.join("faketool"), "#!/bin/sh\n").unwrap();
935        std::fs::write(dir.join("faketool.cmd"), "@echo off\n").unwrap();
936        // The path is PASSED, never installed into this process: see
937        // `which_on`. The old spelling swapped the real PATH out from under
938        // every other test in this binary for the length of the call.
939        let found = which_on(dir.as_os_str(), "faketool").unwrap();
940        assert!(found.ends_with(".cmd"), "got {found}");
941        let _ = std::fs::remove_dir_all(&dir);
942    }
943
944    /// "Nothing moved" and "`git add` FAILED" are different answers, and the
945    /// old `bool` gave the same one for both.
946    ///
947    /// That conflation is what shipped unformatted code: `prettier.rs` read
948    /// `if wrote && restage(&files)`, so a failed `git add` fell through to a
949    /// second `--check` against the now-formatted WORKING TREE, which passed —
950    /// while the INDEX still held the unformatted content the commit would
951    /// carry.
952    ///
953    /// An absolute path outside any repository is a `git add` git will always
954    /// refuse, which is the only way to reach the failing branch without
955    /// sabotaging a real index.
956    #[test]
957    fn restage_distinguishes_nothing_from_failure() {
958        // `restage` runs `git add` in the PROCESS cwd, so this test depends
959        // on that cwd as surely as one that moves it — see `crate::TEST_CWD`.
960        // Without the lock it ran inside whatever fixture `gate_stamp` had
961        // moved into, and took that repository's index.lock out from under
962        // its own commit.
963        let _cwd = crate::TEST_CWD.lock().unwrap_or_else(|p| p.into_inner());
964        let outside = std::env::temp_dir()
965            .join("amont-restage-outside-any-repo")
966            .to_string_lossy()
967            .into_owned();
968        assert_eq!(
969            restage(std::slice::from_ref(&outside)),
970            Restaged::Failed(vec![outside]),
971            "a `git add` git refuses must report Failed, never Nothing"
972        );
973        assert_eq!(
974            restage(&[]),
975            Restaged::Nothing,
976            "no paths is nothing to do, and nothing wrong"
977        );
978    }
979
980    /// No check may hand `Command` a bare program name.
981    ///
982    /// `Command::new` does NO PATHEXT resolution, so `Command::new("npm")`
983    /// cannot execute `npm.cmd` and `Command::new("uvx")` cannot execute
984    /// `uvx.exe`: the spawn fails with "program not found" and a
985    /// `Severity::Block` check reports an installed tool as broken. That is the
986    /// incident `program()` exists for, and it kept recurring — `yamllint` and
987    /// three sites in `python_tools` were still doing it, THREE OF THEM after
988    /// `which()` had already succeeded and discarded the answer.
989    ///
990    /// A source scan rather than a runtime assertion because the failure only
991    /// reproduces on Windows, and the whole point is to catch the next one on
992    /// every platform. Comment lines are skipped: `program()`'s own doc quotes
993    /// the offending call. The needle is assembled from two pieces so this
994    /// module — which the scan also reads — does not match itself.
995    #[test]
996    fn no_hook_spawns_a_bare_program_name() {
997        let needle = concat!("Command", "::new(");
998        let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/src/hooks");
999        let mut scanned = 0usize;
1000        for entry in std::fs::read_dir(dir).expect("hooks dir").flatten() {
1001            let path = entry.path();
1002            if path.extension().and_then(|e| e.to_str()) != Some("rs") {
1003                continue;
1004            }
1005            scanned += 1;
1006            let src = std::fs::read_to_string(&path).expect("read a hook module");
1007            for (n, line) in src.lines().enumerate() {
1008                if line.trim_start().starts_with("//") {
1009                    continue;
1010                }
1011                let Some(after) = line.split_once(needle) else {
1012                    continue;
1013                };
1014                assert!(
1015                    !after.1.starts_with('"'),
1016                    "{}:{} spawns a bare name — route it through `program()` or \
1017                     the path `which()` already resolved: {}",
1018                    path.display(),
1019                    n + 1,
1020                    line.trim()
1021                );
1022            }
1023        }
1024        assert!(
1025            scanned > 10,
1026            "the scan found almost nothing: {scanned} files"
1027        );
1028    }
1029
1030    #[test]
1031    fn first_existing_picks_the_earliest_present_name() {
1032        let dir = std::env::temp_dir().join("amont-first-existing-test");
1033        let _ = std::fs::create_dir_all(&dir);
1034        let root = dir.to_string_lossy().into_owned();
1035        let _ = std::fs::write(dir.join("second"), "x");
1036        assert_eq!(
1037            first_existing(&root, &["first", "second", "third"]).as_deref(),
1038            Some("second")
1039        );
1040        assert_eq!(first_existing(&root, &["nope"]), None);
1041        let _ = std::fs::remove_dir_all(&dir);
1042    }
1043}