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#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct Explanation {
14 pub overall: Verdict,
15 pub segments: Vec<SegmentReport>,
16 pub parsed: bool,
18 pub stateful: bool,
21}
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct SegmentReport {
25 pub text: String,
27 pub verdict: Verdict,
28 pub culprit: Option<String>,
33}
34
35pub fn explain(input: &str) -> Explanation {
37 explain_inner(input, |_| false)
38}
39
40pub 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 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 let segments: Vec<SegmentReport> = super::check::walk_with_scope(&script, |stmt| segment_report(stmt, &covered));
80 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 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 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
146fn command_label(cmd: &Cmd) -> Option<String> {
163 match cmd {
164 Cmd::Simple(s) => simple_cmd_name(s),
165 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 Cmd::DoubleBracket { .. } => None,
178 }
179}
180
181pub(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 if matches!(cmd, Cmd::Simple(_)) {
198 return None;
199 }
200 first_denied_simple(cmd)
201}
202
203fn 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
227fn 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
247fn 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 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 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.push_str("The command has likely already run. This is feedback, not a request to re-run it.\n");
318 out
319 }
320
321 fn guidance(&self, total: usize, denied: usize) -> Option<&'static str> {
322 if denied == 0 {
323 return None;
324 }
325 if total == 1 {
330 return Some(
331 "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.",
332 );
333 }
334 if denied == total {
335 return Some("This is not a block. These all need manual approval. None of them auto-approve on their own.");
336 }
337 if self.stateful {
338 return Some(
339 "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.",
340 );
341 }
342 Some(
343 "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.",
344 )
345 }
346}
347
348fn header(total: usize, denied: usize) -> String {
349 if denied == 0 {
350 if total == 1 {
351 return "safe-chains: auto-approves.\n".to_string();
352 }
353 return format!("safe-chains: all {total} segments auto-approve.\n");
354 }
355 if total == 1 {
360 return format!("safe-chains: {}\n", crate::refusal::EXPLAIN_SINGLE);
361 }
362 format!("safe-chains: did not auto-approve {denied} of {total} segments. {}\n", crate::refusal::EXPLAIN_MANY)
363}
364
365fn render_line(s: &SegmentReport) -> String {
369 let mark = if s.verdict.is_allowed() { '✓' } else { '✗' };
370 let text = crate::sanitize_display(&s.text);
371 match &s.culprit {
372 Some(culprit) if !s.verdict.is_allowed() => {
373 format!(" {mark} {text} ({})\n", crate::sanitize_display(culprit))
374 }
375 _ => format!(" {mark} {text}\n"),
376 }
377}
378
379#[cfg(test)]
380mod tests {
381 use super::*;
382
383 fn marks(input: &str) -> Vec<bool> {
384 explain(input).segments.iter().map(|s| s.verdict.is_allowed()).collect()
385 }
386
387 #[test]
388 fn single_safe_command_one_allowed_segment() {
389 let e = explain("ls -la");
390 assert!(e.is_allowed());
391 assert_eq!(e.segments.len(), 1);
392 assert!(e.segments[0].verdict.is_allowed());
393 assert_eq!(e.segments[0].culprit, None);
394 }
395
396 #[test]
397 fn single_unsafe_command_is_denied_without_redundant_culprit() {
398 let e = explain("rm -rf /");
399 assert!(!e.is_allowed());
400 assert_eq!(e.segments.len(), 1);
401 assert_eq!(e.segments[0].culprit, None);
402 }
403
404 #[test]
405 fn one_torpedo_marks_only_that_segment() {
406 let e = explain("git status && rm -rf / && echo done");
407 assert!(!e.is_allowed());
408 assert_eq!(marks("git status && rm -rf / && echo done"), vec![true, false, true]);
409 assert!(e.segments.iter().all(|s| s.culprit.is_none()));
410 }
411
412 #[test]
413 fn all_safe_chain_is_allowed() {
414 let e = explain("git status && ls && echo hi");
415 assert!(e.is_allowed());
416 assert_eq!(marks("git status && ls && echo hi"), vec![true, true, true]);
417 }
418
419 #[test]
420 fn semicolons_and_or_split_into_segments() {
421 assert_eq!(explain("ls; pwd; whoami").segments.len(), 3);
422 assert_eq!(explain("ls || rm -rf /").segments.len(), 2);
423 }
424
425 #[test]
437 fn a_denied_compound_names_the_command_inside_it() {
438 for src in [
439 "(cat ~/.ssh/id_rsa)", "{ cat ~/.ssh/id_rsa; }", "if true; then cat ~/.ssh/id_rsa; fi",
440 "for f in a b; do cat ~/.ssh/id_rsa; done", "while true; do cat ~/.ssh/id_rsa; done",
441 "case $x in a) cat ~/.ssh/id_rsa ;; esac",
442 ] {
443 let ex = explain(src);
444 assert_eq!(ex.segments.len(), 1, "{src}: one segment");
445 assert!(!ex.is_allowed(), "{src}: denied");
446 assert_eq!(ex.segments[0].culprit.as_deref(), Some("cat"), "{src}: must name the command inside the construct");
447 }
448
449 assert_eq!(denied_inner_words("(cat ~/.ssh/id_rsa)"), Some(vec!["cat".to_string(), "~/.ssh/id_rsa".to_string()]),);
451 assert_eq!(denied_inner_words("cat ~/.ssh/id_rsa"), None);
453 assert_eq!(denied_inner_words("(ls)"), None);
455 }
456
457 #[test]
458 fn culprit_is_first_denied_in_a_pipeline() {
459 let e = explain("grep foo file | rm -rf /");
460 assert!(!e.is_allowed());
461 assert_eq!(e.segments.len(), 1);
462 assert_eq!(e.segments[0].culprit.as_deref(), Some("rm"));
463 }
464
465 #[test]
466 fn segment_text_round_trips() {
467 let e = explain("git status && echo done");
468 assert_eq!(e.segments[0].text, "git status");
469 assert_eq!(e.segments[1].text, "echo done");
470 }
471
472 #[test]
473 fn unparseable_input_is_a_single_unparsed_segment() {
474 let e = explain("echo 'unterminated");
475 assert!(!e.parsed);
476 assert!(!e.is_allowed());
477 }
478
479 #[test]
482 fn cd_chain_is_marked_stateful() {
483 assert!(explain("cd build && rm -rf x").stateful);
484 assert!(explain("export FOO=bar && rm -rf x").stateful);
485 assert!(explain("FOO=bar && rm -rf x").stateful);
486 assert!(explain("source ./env && rm -rf x").stateful);
487 }
488
489 #[test]
490 fn independent_chain_is_not_stateful() {
491 assert!(!explain("git status && rm -rf x && echo done").stateful);
492 assert!(!explain("ls && pwd").stateful);
493 }
494
495 #[test]
496 fn single_segment_is_never_stateful() {
497 assert!(!explain("cd build").stateful);
498 }
499
500 #[test]
503 fn surfaces_only_the_mixed_bundling_case() {
504 assert!(explain("git status && rm -rf / && echo done").should_surface());
505 assert!(!explain("ls && pwd").should_surface(), "all-safe: nothing to teach");
506 assert!(!explain("rm -rf / && rm -rf /etc").should_surface(), "all-denied: no rescue");
507 assert!(!explain("rm -rf /").should_surface(), "single denied: no chaining lesson");
508 assert!(!explain("echo 'unterminated").should_surface(), "unparseable");
509 }
510
511 #[test]
514 fn stateful_chain_context_names_only_the_refused_segments() {
515 assert_eq!(
516 explain("cd build && cargo publish").render_context(),
517 "safe-chains did not auto-approve:\n ✗ cargo publish\nThe command has likely already run. This is feedback, not a request to re-run it.\n"
518 );
519 }
520
521 proptest::proptest! {
522 #[test]
523 fn hook_context_has_one_line_per_listed_segment(
524 cmd in proptest::sample::select(crate::handler_property_tests::FORGERY_PAYLOADS.to_vec()),
525 ) {
526 let e = explain(&format!("cd x && {cmd}"));
527 let context = e.render_context();
528 let short = e.parsed && e.stateful;
529 let expected = if short { e.segments.iter().filter(|s| !s.verdict.is_allowed()).count() } else { e.segments.len() };
530 let lines = context.lines().filter(|l| l.starts_with(" \u{2713} ") || l.starts_with(" \u{2717} ")).count();
531 proptest::prop_assert_eq!(lines, expected, "{}", context);
532 proptest::prop_assert!(!short || !context.contains('\u{2713}'), "{}", context);
533 }
534 }
535
536 #[test]
537 fn stateless_chain_context_keeps_the_full_breakdown() {
538 let e = explain("git status && rm -rf / && echo done");
539 assert_eq!(e.render_context(), e.render());
540 }
541
542 #[test]
545 fn coverage_overlay_flips_a_user_allowed_segment() {
546 let patterns = Matcher::from_allow_patterns(&["rm *"]);
547 let e = explain_with_coverage("git status && rm -rf / && echo done", &patterns);
548 assert!(e.is_allowed(), "user allowlisted rm, so the chain auto-approves");
549 assert!(e.segments.iter().all(|s| s.verdict.is_allowed()));
550 assert!(!e.should_surface());
551 }
552
553 #[test]
554 fn coverage_overlay_leaves_uncovered_segments_denied() {
555 let patterns = Matcher::from_allow_patterns(&["rm *"]);
556 let e = explain_with_coverage("rm -rf / && cargo publish", &patterns);
557 assert!(!e.is_allowed());
558 assert_eq!(marks_cov("rm -rf / && cargo publish", &patterns), vec![true, false]);
559 }
560
561 fn marks_cov(input: &str, patterns: &Matcher) -> Vec<bool> {
562 explain_with_coverage(input, patterns).segments.iter().map(|s| s.verdict.is_allowed()).collect()
563 }
564
565 #[test]
568 fn render_mixed_chain_lists_marks_and_split_tip() {
569 let out = explain("git status && rm -rf / && echo done").render();
570 assert!(out.contains("✓ git status"));
571 assert!(out.contains("✗ rm -rf /"));
572 assert!(out.contains("✓ echo done"));
573 assert!(out.contains("1 of 3 segments"));
574 assert!(out.contains("not a block"), "must clarify it is not a block: {out}");
575 assert!(out.contains("not a request to re-run"), "must not invite a re-run: {out}");
576 assert!(out.contains("separate tool calls"));
577 }
578
579 #[test]
580 fn render_stateful_chain_says_belongs_in_one_call() {
581 let out = explain("cd build && rm -rf / && echo done").render();
582 assert!(out.contains("belong in one call"), "stateful chain must not advise splitting: {out}");
583 assert!(out.contains("not a request to re-run"));
584 assert!(!out.contains("separate tool calls"));
585 }
586
587 #[test]
588 fn render_pipeline_culprit_disambiguates_failing_stage() {
589 let out = explain("grep foo file | rm -rf /").render();
590 assert!(out.contains("(rm)"), "pipeline should name the failing stage: {out}");
591 }
592
593 #[test]
594 fn render_all_safe_has_no_tip() {
595 let out = explain("ls && pwd").render();
596 assert!(out.contains("all 2 segments auto-approve"));
597 assert!(!out.contains('✗'));
598 assert!(!out.contains("approval"));
599 }
600
601 #[test]
602 fn render_single_denied_keeps_it_alone() {
603 let out = explain("cargo publish").render();
604 assert!(out.contains("did not auto-approve"), "says what happened: {out}");
609 assert!(out.contains("has researched"), "says why, without rating the command: {out}");
610 assert!(out.contains("not a block"));
611 assert!(out.contains("needs manual approval"));
612 }
613
614 #[test]
615 fn render_unparseable_is_explicit() {
616 let out = explain("echo 'unterminated").render();
617 assert!(out.contains("could not parse"));
618 }
619
620 #[test]
621 fn empty_input_renders_no_command() {
622 for input in ["", " "] {
623 let e = explain(input);
624 assert!(e.segments.is_empty(), "{input:?} should have no segments");
625 assert!(e.render().contains("no command to check"));
626 }
627 }
628}