Skip to main content

safe_chains/cst/
explain.rs

1use super::check::{cmd_verdict, pipeline_verdict};
2use super::*;
3use crate::allowlist::{Matcher, is_cmd_covered};
4use crate::parse::Token;
5use crate::verdict::{SafetyLevel, Verdict};
6
7/// A per-segment breakdown of why a command would or would not auto-approve.
8///
9/// "Segment" means a top-level list element — the pieces a user separates with
10/// `&&`, `||`, `;`, or `&`. This is the granularity that matters for the common
11/// failure mode: one un-allowlisted command torpedoing an otherwise-safe chain.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct Explanation {
14    pub overall: Verdict,
15    pub segments: Vec<SegmentReport>,
16    /// False when the input could not be parsed at all.
17    pub parsed: bool,
18    /// True when segments share shell state (a `cd`, `export`, assignment, or
19    /// `source`) so that splitting them into separate calls would break them.
20    pub stateful: bool,
21}
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct SegmentReport {
25    /// The segment rendered back to source (whitespace/operators normalized).
26    pub text: String,
27    pub verdict: Verdict,
28    /// For a denied *pipeline* segment (`a | b | c`), the name of the first
29    /// stage that is not auto-approved — disambiguating which stage to drop.
30    /// `None` for a single-command segment (its text already names it) or when
31    /// the culprit isn't a plain command (e.g. a subshell or redirect target).
32    pub culprit: Option<String>,
33}
34
35/// Explain against the built-in classification only.
36pub fn explain(input: &str) -> Explanation {
37    explain_inner(input, |_| false)
38}
39
40/// Explain with the user's allowlist patterns overlaid, so a command the user
41/// has allowed isn't reported as not-auto-approved. This mirrors the hook's own
42/// coverage check (`main.rs`): a segment counts as allowed when it is built-in
43/// safe *or* every command in it is covered by the user's patterns.
44pub fn explain_with_coverage(input: &str, patterns: &Matcher) -> Explanation {
45    explain_inner(input, |cmd| is_cmd_covered(cmd, patterns))
46}
47
48fn explain_inner(input: &str, covered: impl Fn(&Cmd) -> bool) -> Explanation {
49    // ONE work budget for the whole explanation, taken the same way `command_verdict` takes it.
50    //
51    // Without this, explaining had no budget of its own: brace-expansion fan-out charged the shared
52    // counter while the per-segment classifications inside reset it whenever one bottomed out at
53    // depth 0. The result depended on how much the CALLER had already spent and on where the resets
54    // fell, so `explain` was neither order-independent (it disagreed with the verdict enforced just
55    // before it) nor deterministic (two consecutive calls on one dense input rendered different
56    // answers). Entering here resets once, at the top, and keeps every nested classification at
57    // depth >= 1, which is what makes explaining and enforcing spend from the same pool.
58    let Some(_guard) = super::check::ClassifyGuard::enter(input.len()) else {
59        return Explanation {
60            overall: Verdict::Denied,
61            segments: vec![SegmentReport { text: input.trim().to_string(), verdict: Verdict::Denied, culprit: None }],
62            parsed: false,
63            stateful: false,
64        };
65    };
66    let Some(script) = parse(input) else {
67        return Explanation {
68            overall: Verdict::Denied,
69            segments: vec![SegmentReport { text: input.trim().to_string(), verdict: Verdict::Denied, culprit: None }],
70            parsed: false,
71            stateful: false,
72        };
73    };
74
75    // Walk with the SAME accumulated scope as `script_verdict` (cwd + `VAR=` bindings + function
76    // definitions), so each segment is judged in the context of the ones before it. Without this the
77    // per-segment view — and the hook's coverage fallback built on it — would re-allow a call whose
78    // definition shadows a builtin (`ls(){ rm; }; ls`) that the whole-command verdict denies.
79    let segments: Vec<SegmentReport> = super::check::walk_with_scope(&script, |stmt| segment_report(stmt, &covered));
80    // The scope walk exceeded its uncertain-binding budget: too much shell state to track, so the
81    // command is refused — the same fail-closed call `command_verdict` makes, so the explanation
82    // can never describe a more permissive verdict than the one enforced (the `explain_render`
83    // target's first property). One denied segment for the whole input, as for an unparseable one:
84    // per-segment marks would claim a granularity the walk no longer earned. NOT liftable by the
85    // coverage overlay above — a user rule cannot cover a refusal that exists because what the
86    // command does to later segments cannot be bounded.
87    if super::check::scope_budget_spent() {
88        return Explanation {
89            overall: Verdict::Denied,
90            segments: vec![SegmentReport { text: input.trim().to_string(), verdict: Verdict::Denied, culprit: None }],
91            parsed: true,
92            stateful: false,
93        };
94    }
95    let overall = segments.iter().map(|s| s.verdict).fold(Verdict::Allowed(SafetyLevel::Inert), Verdict::combine);
96    let stateful = segments.len() >= 2 && script.0.iter().any(establishes_shell_state);
97
98    Explanation { overall, segments, parsed: true, stateful }
99}
100
101fn segment_report(stmt: &Stmt, covered: &impl Fn(&Cmd) -> bool) -> SegmentReport {
102    let verdict = effective_verdict(&stmt.pipeline, covered);
103    // A culprit is suppressed when it would only repeat the segment's own name: for a lone SIMPLE
104    // command the segment text already IS `cat ~/.ssh/id_rsa`, so labelling it `cat` says nothing.
105    //
106    // That used to be spelled `commands.len() <= 1`, which caught compounds as well — and there the
107    // label is the only actionable information there is. The segment text of a denied `for` loop is
108    // the whole loop; what the caller has to change is the command inside it, and suppressing that
109    // is how a third of the author's decision-log denials came to read "no reason recorded".
110    let redundant_with_segment_text = matches!(stmt.pipeline.commands.as_slice(), [Cmd::Simple(_)]);
111    let culprit = if verdict.is_allowed() || redundant_with_segment_text { None } else { first_denied_label(&stmt.pipeline, covered) };
112    SegmentReport { text: stmt.pipeline.to_string(), verdict, culprit }
113}
114
115fn effective_verdict(pipeline: &Pipeline, covered: &impl Fn(&Cmd) -> bool) -> Verdict {
116    let base = pipeline_verdict(pipeline);
117    if base.is_allowed() {
118        return base;
119    }
120    if !pipeline.commands.is_empty() && pipeline.commands.iter().all(covered) {
121        // `SafeWrite`, the TOP of the auto-approve band — not `Inert`.
122        //
123        // A `permissions.allow` rule says the user accepts this command. It does NOT say the command
124        // is inert, and claiming so was a lie with teeth: `Inert` is the bottom of the ordering, so it
125        // cleared every threshold and a `Bash(rm:*)` rule out-ranked even `--level paranoid`. A
126        // ceiling a per-command rule can lift is not a ceiling.
127        //
128        // Granting at the band's top keeps the rule honoured wherever the band is (the default
129        // threshold IS `SafeWrite`, so ordinary use is unchanged) while letting a stricter level
130        // clamp it: `paranoid` and `reader` now refuse a covered command, which is what someone
131        // asking for a read-only plan meant. The grant widens what is allowed; it no longer escapes
132        // the ceiling the user stated.
133        return Verdict::Allowed(SafetyLevel::SafeWrite);
134    }
135    base
136}
137
138fn first_denied_label(pipeline: &Pipeline, covered: &impl Fn(&Cmd) -> bool) -> Option<String> {
139    pipeline
140        .commands
141        .iter()
142        .find(|c| !cmd_verdict(c).is_allowed() && !covered(c))
143        .and_then(command_label)
144}
145
146/// The name to report as the culprit for a denied command.
147///
148/// A compound is not itself a command anyone can act on: the thing the caller has to change lives
149/// INSIDE it. So this descends into the body and names the first inner command that is denied on
150/// its own — `(cat ~/.ssh/id_rsa)` reports `cat`, not nothing.
151///
152/// It used to return `None` for everything but `Simple`, which is why a denied `for`/`while`/`case`
153/// left `culprit: null` and `facets: null` in the decision log — roughly a third of the denials in
154/// the author's own log read "no reason recorded". `--explain` had the same hole, and it is the
155/// worse place for it: the hook renders that text back to the agent, so a refusal with no reason is
156/// one the agent cannot act on except by guessing.
157///
158/// Every body is descended, not just the one that will run. Which `if` branch or `case` arm
159/// executes is a runtime value, so the classifier already treats such a command as only as safe as
160/// its worst body; reporting has to look in the same places or it would name nothing for exactly
161/// the constructs that were denied because of what is buried in them.
162fn command_label(cmd: &Cmd) -> Option<String> {
163    match cmd {
164        Cmd::Simple(s) => simple_cmd_name(s),
165        // A function DEFINITION is inert — its body only matters when called, and naming the body's
166        // commands here would report a culprit for a command that did nothing.
167        Cmd::FunctionDef { .. } => None,
168        Cmd::Subshell { body, .. } | Cmd::BraceGroup { body, .. } => denied_label_in(body),
169        Cmd::For { body, .. } => denied_label_in(body),
170        Cmd::While { cond, body, .. } | Cmd::Until { cond, body, .. } => denied_label_in(cond).or_else(|| denied_label_in(body)),
171        Cmd::If { branches, else_body, .. } => branches
172            .iter()
173            .find_map(|b| denied_label_in(&b.cond).or_else(|| denied_label_in(&b.body)))
174            .or_else(|| else_body.as_ref().and_then(denied_label_in)),
175        Cmd::Case { arms, .. } => arms.iter().find_map(|arm| denied_label_in(&arm.body)),
176        // `[[ … ]]` is a test expression, not a command that could be the culprit.
177        Cmd::DoubleBracket { .. } => None,
178    }
179}
180
181/// The WORDS of the first denied command inside a compound, for the facet breakdown.
182///
183/// `--explain`'s profile section tokenises the raw string flatly, which cannot see into a compound:
184/// `(cat ~/.ssh/id_rsa)` splits to `["(cat", "~/.ssh/id_rsa)"]`, no resolver recognises `(cat`, and
185/// the refusal renders with no reason at all. Roughly a third of the denials in the author's
186/// decision log read "no reason recorded" for this shape.
187///
188/// Returns `None` for a plain simple command, so the caller keeps its existing path and this is
189/// only consulted where that path has nothing to say.
190pub(crate) fn denied_inner_words(input: &str) -> Option<Vec<String>> {
191    let _guard = super::check::ClassifyGuard::enter(input.len())?;
192    let script = parse(input)?;
193    let [stmt] = &script.0[..] else { return None };
194    let [cmd] = &stmt.pipeline.commands[..] else { return None };
195    // A simple command is already handled by the flat path, and going through the CST for it would
196    // change what that path reports on inputs it handles correctly today.
197    if matches!(cmd, Cmd::Simple(_)) {
198        return None;
199    }
200    first_denied_simple(cmd)
201}
202
203/// The first simple command at or below `cmd` that is denied on its own.
204fn first_denied_simple(cmd: &Cmd) -> Option<Vec<String>> {
205    match cmd {
206        Cmd::Simple(s) => Some(s.words.iter().map(Word::eval).collect()),
207        Cmd::FunctionDef { .. } | Cmd::DoubleBracket { .. } => None,
208        Cmd::Subshell { body, .. } | Cmd::BraceGroup { body, .. } | Cmd::For { body, .. } => first_denied_simple_in(body),
209        Cmd::While { cond, body, .. } | Cmd::Until { cond, body, .. } => {
210            first_denied_simple_in(cond).or_else(|| first_denied_simple_in(body))
211        }
212        Cmd::If { branches, else_body, .. } => branches
213            .iter()
214            .find_map(|b| first_denied_simple_in(&b.cond).or_else(|| first_denied_simple_in(&b.body)))
215            .or_else(|| else_body.as_ref().and_then(first_denied_simple_in)),
216        Cmd::Case { arms, .. } => arms.iter().find_map(|arm| first_denied_simple_in(&arm.body)),
217    }
218}
219
220fn first_denied_simple_in(script: &Script) -> Option<Vec<String>> {
221    script
222        .0
223        .iter()
224        .find_map(|stmt| stmt.pipeline.commands.iter().find(|c| !cmd_verdict(c).is_allowed()).and_then(first_denied_simple))
225}
226
227/// The first command inside `script` that is denied on its own, by name.
228///
229/// Recurses through `command_label`, so a culprit nested several constructs deep is still found.
230/// Termination rests on the CST being finite and acyclic — a body is always a strictly smaller
231/// subtree than the command containing it — which is the same property the classifier's own walk
232/// relies on.
233fn denied_label_in(script: &Script) -> Option<String> {
234    script
235        .0
236        .iter()
237        .find_map(|stmt| stmt.pipeline.commands.iter().find(|c| !cmd_verdict(c).is_allowed()).and_then(command_label))
238}
239
240fn simple_cmd_name(s: &SimpleCmd) -> Option<String> {
241    s.words
242        .first()
243        .map(|w| Token::from_raw(w.eval()).command_name().to_string())
244        .filter(|name| !name.is_empty())
245}
246
247/// Whether a segment establishes shell state that later segments would rely on:
248/// a directory change, an environment change, or a sourced script. Splitting
249/// such a chain into separate calls would silently lose that state.
250fn establishes_shell_state(stmt: &Stmt) -> bool {
251    stmt.pipeline.commands.iter().any(|cmd| match cmd {
252        Cmd::Simple(s) => {
253            if s.words.is_empty() && !s.env.is_empty() {
254                return true;
255            }
256            matches!(simple_cmd_name(s).as_deref(), Some("cd" | "pushd" | "popd" | "export" | "source" | "." | "set" | "alias" | "umask"))
257        }
258        _ => false,
259    })
260}
261
262impl Explanation {
263    pub fn is_allowed(&self) -> bool {
264        self.overall.is_allowed()
265    }
266
267    fn counts(&self) -> (usize, usize) {
268        let total = self.segments.len();
269        let denied = self.segments.iter().filter(|s| !s.verdict.is_allowed()).count();
270        (total, denied)
271    }
272
273    /// Whether this explanation is worth injecting into an agent's context
274    /// automatically. The teachable case is a *mix*: an otherwise-auto-approving
275    /// chain dragged into a manual prompt by one un-allowlisted segment. A single
276    /// denied command, or an all-denied chain, carries no chaining lesson — so we
277    /// stay quiet and let the normal approval flow handle it.
278    pub fn should_surface(&self) -> bool {
279        if !self.parsed || self.segments.len() < 2 {
280            return false;
281        }
282        let (total, denied) = self.counts();
283        denied > 0 && denied < total
284    }
285
286    /// A model- and human-readable breakdown: which segments auto-approve, which
287    /// don't, and what to actually do about it.
288    pub fn render(&self) -> String {
289        if !self.parsed {
290            return "safe-chains: could not parse this command, so it will not be auto-approved.\n".to_string();
291        }
292        if self.segments.is_empty() {
293            return "safe-chains: no command to check.\n".to_string();
294        }
295
296        let (total, denied) = self.counts();
297        let mut out = String::new();
298        out.push_str(&header(total, denied));
299        for s in &self.segments {
300            out.push_str(&render_line(s));
301        }
302        if let Some(tip) = self.guidance(total, denied) {
303            out.push_str(tip);
304            out.push('\n');
305        }
306        out
307    }
308
309    pub fn render_context(&self) -> String {
310        if !(self.parsed && self.stateful) {
311            return self.render();
312        }
313        let mut out = String::from("safe-chains did not auto-approve:\n");
314        for s in self.segments.iter().filter(|s| !s.verdict.is_allowed()) {
315            out.push_str(&render_line(s));
316        }
317        out
318    }
319
320    fn guidance(&self, total: usize, denied: usize) -> Option<&'static str> {
321        if denied == 0 {
322            return None;
323        }
324        // The auto-injected case is always the mixed chain (see should_surface).
325        // By the time an agent reads this, the command has gone through the
326        // normal approval flow and most likely already run — so the guidance is
327        // feedback for next time, never an instruction to re-run.
328        if total == 1 {
329            return Some(
330                "This is not a block. It just needs manual approval. Next time send a command that needs approval on its own, not in the same call as commands that auto-approve.",
331            );
332        }
333        if denied == total {
334            return Some("This is not a block. These all need manual approval. None of them auto-approve on their own.");
335        }
336        if self.stateful {
337            return Some(
338                "This is not a block. The command has likely already run, so this is feedback and not a request to re-run it. These segments share shell state, such as a cd, a variable, or a source, so they belong in one call. Bundling them was correct. Nothing to change.",
339            );
340        }
341        Some(
342            "This is not a block. The command has likely already run, so this is feedback and not a request to re-run it. Next time send independent commands as separate tool calls instead of chaining them. The ✓ segments auto-approve on their own, so only a ✗ segment needs approval.",
343        )
344    }
345}
346
347fn header(total: usize, denied: usize) -> String {
348    if denied == 0 {
349        if total == 1 {
350            return "safe-chains: auto-approves.\n".to_string();
351        }
352        return format!("safe-chains: all {total} segments auto-approve.\n");
353    }
354    // The THIRD producer of refusal copy, and the one that kept "not on the allowlist" alive after
355    // it was removed from the others. It routes through the same builder now, with
356    // `Outcome::Unknown`: `--explain` is run against no harness, so what follows is not ours to
357    // claim. See docs/design/refusal-copy.md.
358    if total == 1 {
359        return format!("safe-chains: {}\n", crate::refusal::EXPLAIN_SINGLE);
360    }
361    format!("safe-chains: did not auto-approve {denied} of {total} segments. {}\n", crate::refusal::EXPLAIN_MANY)
362}
363
364/// One `✓`/`✗` line. The echoed text is command-derived, so it is neutralized first: a raw newline
365/// in it let a command forge an entire extra line carrying our own `✓` marker (see
366/// [`crate::sanitize_display`]).
367fn render_line(s: &SegmentReport) -> String {
368    let mark = if s.verdict.is_allowed() { '✓' } else { '✗' };
369    let text = crate::sanitize_display(&s.text);
370    match &s.culprit {
371        Some(culprit) if !s.verdict.is_allowed() => {
372            format!("  {mark}  {text}   ({})\n", crate::sanitize_display(culprit))
373        }
374        _ => format!("  {mark}  {text}\n"),
375    }
376}
377
378#[cfg(test)]
379mod tests {
380    use super::*;
381
382    fn marks(input: &str) -> Vec<bool> {
383        explain(input).segments.iter().map(|s| s.verdict.is_allowed()).collect()
384    }
385
386    #[test]
387    fn single_safe_command_one_allowed_segment() {
388        let e = explain("ls -la");
389        assert!(e.is_allowed());
390        assert_eq!(e.segments.len(), 1);
391        assert!(e.segments[0].verdict.is_allowed());
392        assert_eq!(e.segments[0].culprit, None);
393    }
394
395    #[test]
396    fn single_unsafe_command_is_denied_without_redundant_culprit() {
397        let e = explain("rm -rf /");
398        assert!(!e.is_allowed());
399        assert_eq!(e.segments.len(), 1);
400        assert_eq!(e.segments[0].culprit, None);
401    }
402
403    #[test]
404    fn one_torpedo_marks_only_that_segment() {
405        let e = explain("git status && rm -rf / && echo done");
406        assert!(!e.is_allowed());
407        assert_eq!(marks("git status && rm -rf / && echo done"), vec![true, false, true]);
408        assert!(e.segments.iter().all(|s| s.culprit.is_none()));
409    }
410
411    #[test]
412    fn all_safe_chain_is_allowed() {
413        let e = explain("git status && ls && echo hi");
414        assert!(e.is_allowed());
415        assert_eq!(marks("git status && ls && echo hi"), vec![true, true, true]);
416    }
417
418    #[test]
419    fn semicolons_and_or_split_into_segments() {
420        assert_eq!(explain("ls; pwd; whoami").segments.len(), 3);
421        assert_eq!(explain("ls || rm -rf /").segments.len(), 2);
422    }
423
424    /// A denied COMPOUND names the command inside it, rather than nothing.
425    ///
426    /// `command_label` returned `None` for every non-simple command, so a denied `for`/`while`/
427    /// `case`/subshell left `culprit: null` in the decision log and no reason in `--explain`.
428    /// Roughly a third of the denials in the author's own log read "no reason recorded".
429    ///
430    /// The construct itself is never the actionable answer — the caller cannot change "a subshell",
431    /// only the command in it — so every body is descended, including the branches and arms that
432    /// may not run. The classifier already treats such a command as only as safe as its worst body;
433    /// reporting looks in the same places, or it names nothing for precisely the constructs whose
434    /// denial came from something buried in them.
435    #[test]
436    fn a_denied_compound_names_the_command_inside_it() {
437        for src in [
438            "(cat ~/.ssh/id_rsa)", "{ cat ~/.ssh/id_rsa; }", "if true; then cat ~/.ssh/id_rsa; fi",
439            "for f in a b; do cat ~/.ssh/id_rsa; done", "while true; do cat ~/.ssh/id_rsa; done",
440            "case $x in a) cat ~/.ssh/id_rsa ;; esac",
441        ] {
442            let ex = explain(src);
443            assert_eq!(ex.segments.len(), 1, "{src}: one segment");
444            assert!(!ex.is_allowed(), "{src}: denied");
445            assert_eq!(ex.segments[0].culprit.as_deref(), Some("cat"), "{src}: must name the command inside the construct");
446        }
447
448        // And the words reach the facet breakdown, which is what puts a REASON on the refusal.
449        assert_eq!(denied_inner_words("(cat ~/.ssh/id_rsa)"), Some(vec!["cat".to_string(), "~/.ssh/id_rsa".to_string()]),);
450        // A plain simple command keeps the existing path — this is only for what it cannot see.
451        assert_eq!(denied_inner_words("cat ~/.ssh/id_rsa"), None);
452        // A construct whose body is fine has no culprit to name.
453        assert_eq!(denied_inner_words("(ls)"), None);
454    }
455
456    #[test]
457    fn culprit_is_first_denied_in_a_pipeline() {
458        let e = explain("grep foo file | rm -rf /");
459        assert!(!e.is_allowed());
460        assert_eq!(e.segments.len(), 1);
461        assert_eq!(e.segments[0].culprit.as_deref(), Some("rm"));
462    }
463
464    #[test]
465    fn segment_text_round_trips() {
466        let e = explain("git status && echo done");
467        assert_eq!(e.segments[0].text, "git status");
468        assert_eq!(e.segments[1].text, "echo done");
469    }
470
471    #[test]
472    fn unparseable_input_is_a_single_unparsed_segment() {
473        let e = explain("echo 'unterminated");
474        assert!(!e.parsed);
475        assert!(!e.is_allowed());
476    }
477
478    // ---- stateful detection ----
479
480    #[test]
481    fn cd_chain_is_marked_stateful() {
482        assert!(explain("cd build && rm -rf x").stateful);
483        assert!(explain("export FOO=bar && rm -rf x").stateful);
484        assert!(explain("FOO=bar && rm -rf x").stateful);
485        assert!(explain("source ./env && rm -rf x").stateful);
486    }
487
488    #[test]
489    fn independent_chain_is_not_stateful() {
490        assert!(!explain("git status && rm -rf x && echo done").stateful);
491        assert!(!explain("ls && pwd").stateful);
492    }
493
494    #[test]
495    fn single_segment_is_never_stateful() {
496        assert!(!explain("cd build").stateful);
497    }
498
499    // ---- should_surface (auto-injection gate) ----
500
501    #[test]
502    fn surfaces_only_the_mixed_bundling_case() {
503        assert!(explain("git status && rm -rf / && echo done").should_surface());
504        assert!(!explain("ls && pwd").should_surface(), "all-safe: nothing to teach");
505        assert!(!explain("rm -rf / && rm -rf /etc").should_surface(), "all-denied: no rescue");
506        assert!(!explain("rm -rf /").should_surface(), "single denied: no chaining lesson");
507        assert!(!explain("echo 'unterminated").should_surface(), "unparseable");
508    }
509
510    // ---- render_context (what the hook injects) ----
511
512    #[test]
513    fn stateful_chain_context_names_only_the_refused_segments() {
514        assert_eq!(explain("cd build && cargo publish").render_context(), "safe-chains did not auto-approve:\n  ✗  cargo publish\n");
515    }
516
517    proptest::proptest! {
518        #[test]
519        fn hook_context_has_one_line_per_listed_segment(
520            cmd in proptest::sample::select(crate::handler_property_tests::FORGERY_PAYLOADS.to_vec()),
521        ) {
522            let e = explain(&format!("cd x && {cmd}"));
523            let context = e.render_context();
524            let short = e.parsed && e.stateful;
525            let expected = if short { e.segments.iter().filter(|s| !s.verdict.is_allowed()).count() } else { e.segments.len() };
526            let lines = context.lines().filter(|l| l.starts_with("  \u{2713}  ") || l.starts_with("  \u{2717}  ")).count();
527            proptest::prop_assert_eq!(lines, expected, "{}", context);
528            proptest::prop_assert!(!short || !context.contains('\u{2713}'), "{}", context);
529        }
530    }
531
532    #[test]
533    fn stateless_chain_context_keeps_the_full_breakdown() {
534        let e = explain("git status && rm -rf / && echo done");
535        assert_eq!(e.render_context(), e.render());
536    }
537
538    // ---- coverage overlay ----
539
540    #[test]
541    fn coverage_overlay_flips_a_user_allowed_segment() {
542        let patterns = Matcher::from_allow_patterns(&["rm *"]);
543        let e = explain_with_coverage("git status && rm -rf / && echo done", &patterns);
544        assert!(e.is_allowed(), "user allowlisted rm, so the chain auto-approves");
545        assert!(e.segments.iter().all(|s| s.verdict.is_allowed()));
546        assert!(!e.should_surface());
547    }
548
549    #[test]
550    fn coverage_overlay_leaves_uncovered_segments_denied() {
551        let patterns = Matcher::from_allow_patterns(&["rm *"]);
552        let e = explain_with_coverage("rm -rf / && cargo publish", &patterns);
553        assert!(!e.is_allowed());
554        assert_eq!(marks_cov("rm -rf / && cargo publish", &patterns), vec![true, false]);
555    }
556
557    fn marks_cov(input: &str, patterns: &Matcher) -> Vec<bool> {
558        explain_with_coverage(input, patterns).segments.iter().map(|s| s.verdict.is_allowed()).collect()
559    }
560
561    // ---- rendering ----
562
563    #[test]
564    fn render_mixed_chain_lists_marks_and_split_tip() {
565        let out = explain("git status && rm -rf / && echo done").render();
566        assert!(out.contains("✓  git status"));
567        assert!(out.contains("✗  rm -rf /"));
568        assert!(out.contains("✓  echo done"));
569        assert!(out.contains("1 of 3 segments"));
570        assert!(out.contains("not a block"), "must clarify it is not a block: {out}");
571        assert!(out.contains("not a request to re-run"), "must not invite a re-run: {out}");
572        assert!(out.contains("separate tool calls"));
573    }
574
575    #[test]
576    fn render_stateful_chain_says_belongs_in_one_call() {
577        let out = explain("cd build && rm -rf / && echo done").render();
578        assert!(out.contains("belong in one call"), "stateful chain must not advise splitting: {out}");
579        assert!(out.contains("not a request to re-run"));
580        assert!(!out.contains("separate tool calls"));
581    }
582
583    #[test]
584    fn render_pipeline_culprit_disambiguates_failing_stage() {
585        let out = explain("grep foo file | rm -rf /").render();
586        assert!(out.contains("(rm)"), "pipeline should name the failing stage: {out}");
587    }
588
589    #[test]
590    fn render_all_safe_has_no_tip() {
591        let out = explain("ls && pwd").render();
592        assert!(out.contains("all 2 segments auto-approve"));
593        assert!(!out.contains('✗'));
594        assert!(!out.contains("approval"));
595    }
596
597    #[test]
598    fn render_single_denied_keeps_it_alone() {
599        let out = explain("cargo publish").render();
600        // The header moved to the shared builder (`refusal::EXPLAIN_SINGLE`), so this asserts the
601        // FACTS it has to carry rather than the exact sentence — the spec's own note that target
602        // tests pinning literal copy "keep passing while the real copy changes, which is worse than
603        // no test". Wording is the copy guards' job (`refusal::tests`).
604        assert!(out.contains("did not auto-approve"), "says what happened: {out}");
605        assert!(out.contains("has researched"), "says why, without rating the command: {out}");
606        assert!(out.contains("not a block"));
607        assert!(out.contains("needs manual approval"));
608    }
609
610    #[test]
611    fn render_unparseable_is_explicit() {
612        let out = explain("echo 'unterminated").render();
613        assert!(out.contains("could not parse"));
614    }
615
616    #[test]
617    fn empty_input_renders_no_command() {
618        for input in ["", "   "] {
619            let e = explain(input);
620            assert!(e.segments.is_empty(), "{input:?} should have no segments");
621            assert!(e.render().contains("no command to check"));
622        }
623    }
624}