✅ test(kavach): bash_guard.py lines 46–454 have no cosmic-ray survivor but equivalents

Merged
AT
2d ago
+325-12

Slice 41 of the mutation sweep (#466): cosmic-ray 8.9.0 leaves no survivor on bash_guard.py lines 46–454 but the 13 equivalents listed below.

Measured first

bash_guard.py has not changed on main since 9b535c2. On main (bc3d92e), the gate's own machinery over lines 46–454, every line taken as changed: 722 mutants, 507 killed, 161 survived, 42 on lines no test ran (ansi_c's \c, a backslash inside double quotes, step_dir's relative cd after an unresolved one), and 12 timed out (the comment skip in strip_comments, which loops forever when its condition is turned).

On this branch: 631 mutants, 618 killed, 13 survived, all listed below. No timeout, no capped run, no uncovered line. Proof on Linux, with the gate's memory and file caps: draft veda-hq/veda#748 (never merges), run 46320080202, the gate's own run over lines 46–454 taken as changed (scripts/kavach_mutation/sweep.py there).

Tests

All in the new tests/test_bash_guard.py.

  • Each refusal is the check's own single line. The command guard turns an exit of 1 or 3 into a block too, but adds a BLOCKED line of its own, so each test asserts exit 2 and exactly one BLOCKED line, starting with this check's words: an unreadable command, a program variable, a protected variable, --no-verify.
  • Heredocs: body lines sorting before and after the marker stay unread; the lines after the end marker are read.
  • $'...': \cL and \cI are a form feed and a tab before the =; octal \475 keeps its low byte, =; \U00110000 is no character.
  • Text checks: --no-veri after -m is refused; git commit -n -m "x (unparseable) is refused as -n.
  • Directories: cd -, and a relative cd after cd $X, cannot be resolved; a directory named ,x (sorting before -) is read.
  • -c overrides: a forcing push refspec after another override; another key may start with +.
  • The shell reader, called directly. strip_comments and tokenize are called in a child that loads bash_guard.py as the guard does, under a 30 s timeout: backslash-newline only after a backslash; a trailing backslash at even and odd places, quoted and not, and past 256 characters (Python's shared small integers make is not differ from != only there); escaped quotes inside and outside double quotes, early and late in the text; a backslash inside single quotes; comments only at a word's start, to the newline; file-descriptor numbers only at a word's start; a run of every operator character; words of quoted operators.
  • Called directly because the shell-code check repeats them in the same words: is_dangerous_long (--force-with-lease=main, --mir=x, --f, --), and check_config_overrides (--config-env, $X). git_safety.py refuses the same pushes with the same text, so through the guard a mutant here still shows the expected refusal.

Rewrites that keep behaviour

No line moves, so the line numbers in #508 to #512 still hold.

  • ansi_c: s.startswith("c") for s[0] == "c" (equal to is for one character); s.isdigit() for s[0].isdigit() (by then s is octal digits or one character).
  • The -n text check: "n" in f for "n" in f[1:]; each flag starts with -.
  • TERMINATORS and SEPARATORS are built with * unpacking, not | of disjoint sets (where ^ is the same).
  • HIDE_OPS and SHOW_OPS are str.maketrans of two literal strings, the same seven placeholders –.
  • The comment skip is re.compile(r"[^\n]*").match(text, i).end(), not a while loop: same end, at the newline or the text's end, and no mutant that loops forever.
  • strip_comments steps by len(ch), one character, not 1: i += 0 looped forever appending, and on Linux ran into the memory cap.
  • tokenize: the word test drops tok.translate(SHOW_OPS) == tok or. A word with no placeholder that is made only of operator characters was split by the branch before, so for every word that reaches this test the first clause implies the second.
  • is_dangerous_long: arg.partition("=")[0] for arg.split("=", 1)[0].

Equivalents for the Python gate's list

Rows for packages/kavach/python/mutation-equivalents.tsv, for Alok to add. This pull request does not change the list. Since a row holds on every line that reads the same, the first two also match line 1034, ansi_char's copy of that line, slice #510. That slice checks the two mutants survive there too, or makes the lines differ.

bash_guard.py	return chr(code) if code <= 0x10FFFF else "�"	ReplaceComparisonOperator_LtE_Lt	1	survived	Only code 0x10FFFF changes, to U+FFFD: it and U+10FFFF are each neither a word character nor a blank, the only classes the rules that read ansi_c's decoded text test.
bash_guard.py	return chr(code) if code <= 0x10FFFF else "�"	NumberReplacer	2	survived	The same: only 0x10FFFF changes, to U+FFFD, and neither is a word character or a blank.
bash_guard.py	while i < len(text):	ReplaceComparisonOperator_Lt_NotEq	1	survived	i moves by 1, by 2 only when a character follows, or to a newline or the text's end, so it never passes len(text).
bash_guard.py	if ch == "\\" and quote == '"' and i + 1 < len(text):	ReplaceComparisonOperator_Eq_LtE	2	survived	quote is '"' or "'" here, and "'" sorts after '"'.
bash_guard.py	if ch == "\\" and quote == '"' and i + 1 < len(text):	NumberReplacer	1	survived	i + 2 differs only for a backslash before the last character inside open double quotes: the text strip_comments returns is the same either way, and its quote is left open.
bash_guard.py	if ch == "\\" and quote == '"' and i + 1 < len(text):	ReplaceComparisonOperator_Lt_NotEq	1	survived	i < len(text) here, so i + 1 never passes len(text).
bash_guard.py	if ch == quote:	ReplaceComparisonOperator_Eq_Is	1	survived	quote is '"' or "'", and CPython shares one object for each one-character ASCII string, so ch is quote exactly when ch == quote.
bash_guard.py	elif ch == "\\" and i + 1 < len(text):	ReplaceComparisonOperator_Lt_NotEq	1	survived	i < len(text) here, so i + 1 never passes len(text).
bash_guard.py	elif ch == "#" and (i == 0 or text[i - 1] in " \t\n;&|()"):	ReplaceComparisonOperator_Eq_LtE	2	survived	i is an index, never negative.
bash_guard.py	elif ch.isdigit() and (i == 0 or text[i - 1] in " \t\n;&|()"):	ReplaceComparisonOperator_Eq_LtE	1	survived	i is an index, never negative.
bash_guard.py	while j < len(text) and text[j].isdigit():	ReplaceComparisonOperator_Lt_NotEq	1	survived	j starts at i, below len(text), and moves by 1, so it stops at len(text) at the latest.
bash_guard.py	if j < len(text) and text[j] in "<>":	ReplaceComparisonOperator_Lt_NotEq	1	survived	The loop before leaves j at len(text) at the latest.
bash_guard.py	if opt == "--config-env" or VALUE_UNRESOLVABLE.search(cval):	ReplaceComparisonOperator_Eq_LtE	1	survived	opt is "-c" or "--config-env" (iter_git_calls keeps no other override), and "-c" sorts after "--config-env".

Where each line is judged

The CI gate (kavach Python mutation) judges the rewritten lines. None of them carries a listed mutant.

Cubic's review allowance is used up until 9 December 2026, so its check reports neutral; Alok said to keep merging sweep pull requests without it until then.

Slice: #507

🤖 Generated with Claude Code


Summary by cubic

Rewrites bash_guard.py lines 46–454 in behavior-preserving forms so the cosmic-ray mutation sweep leaves no survivor there, and adds tests/test_bash_guard.py covering the rewritten behavior. 618 of 631 mutants are killed; the 13 survivors are verified equivalents, listed for packages/kavach/python/mutation-equivalents.tsv (unchanged by this PR).

Tests

  • Each refusal exits 2 with exactly one BLOCKED line, the check's own.
  • Heredoc bodies stay unread; $'...' control, octal, and past-Unicode escapes decode as bash does; -n and unparseable commands are refused; cd - and a relative cd after an unresolved one are unresolvable; forcing + push refspecs in -c overrides are refused.
  • strip_comments and tokenize are called directly in a child under a 30 s timeout, so a mutant that loops forever fails the test instead of hanging the suite.

Rewrites

  • s.startswith("c"), s.isdigit(), "n" in f, set unpacking for TERMINATORS/SEPARATORS, literal placeholder strings for HIDE_OPS/SHOW_OPS, a regex for the comment skip, i += len(ch), the redundant tokenize clause dropped, and partition in is_dangerous_long.
  • No line moves, so slices #508–512 line numbers still hold.