🧪 test(kavach): no cosmic-ray survivor left in the guards, config and refusal log

Merged
AT
10d ago
+243-9

Slice #328 under #238. No cosmic-ray survivor is left in kavach's tool_guard.py, command_guard.py, config.py and refusal_log.py, except the 12 equivalents listed below.

  • 49 mutants killed by new tests: the slice's 42 real survivors, the 6 in refusal_log.log_dir that need a mocked git, and the mkdir(parents=False) the slice listed as equivalent. A log directory whose parent is missing shows that one up.
  • 55 annotation mutants removed: the six X | None annotations become Optional[X], leaving cosmic-ray no | to swap.
  • 12 equivalents listed, down from 13 because of mkdir.

No source line moves, so the line numbers in #328 and the proof still match.

The tests

Line numbers are on main.

MutantsTest
isolate, tool_guard.py L26–29 and command_guard.py L73–76 (10)test_rules.Guards: both guards run through python3 -c with modules planted in the cwd (the guard's own names and json, shlex, dataclasses, subprocess, pathlib), and through -P with the guard's names planted on PYTHONPATH. Each planted module raises as it loads. Each call must allow and print nothing.
fired swallows a logging error, tool_guard.py L46 and command_guard.py L155 (2)A copy whose refusal_log.label raises. A refusing rule must still print one BLOCKED line, its own.
__name__ == "__main__" → >=, tool_guard.py L90 and command_guard.py L240 (2)test_command_guard.Importing: importing either guard in a child with stdin closed gives (0, "", "").
verdict, command_guard.py L86 (0, 3) and L91 >= (2)A check that exits 2 says nothing more. import z_absent_dependency is named as the check's own failure, whole line asserted.
lesson_rules, command_guard.py L204 (2)A refusing lesson rule prints exactly one BLOCKED line.
answer, command_guard.py L217 and L219 (5)A stub git_safety prints not json, {"other": 1} and [1, 2]. Each time the guard allows, exit 0, and passes the text through.
cannot_name_git, command_guard.py L133 and L134 (2)In a child process: a payload that is not JSON gives False, and ls gives True.
run's refusal_log message, command_guard.py L234 (1)The whole BLOCKED line is asserted.
config.py dataclasses frozen, L33–104 (7)Assigning a field of each raises FrozenInstanceError.
config.py L321 and L387 (2)refusesFrom: 5, and protectedPathsReason: 5 with no paths, are ConfigErrors, not TypeErrors.
refusal_log.py L30 EXCERPT (2)An excerpt and a reason are cut at exactly 200 characters.
refusal_log.py L136 ensure_ascii (1)A row keeps café as UTF-8, with no \u escape.
refusal_log.py L183 and L188 guarded (4)Exits of 0, 1, 3, None and "2", and returns of 0, 1, 3 and "2", write no row. An exit and a return of 2 each write one.
refusal_log.py L59, L60, L65 log_dir (6)A mocked subprocess.run: called once with timeout=5 and check=False. Return codes 1, -1 and 128 with a path on stdout give None, and so does an empty stdout.
refusal_log.py L134 mkdir(parents=True) (1)log_dir patched to a folder whose parent is missing: the row is still written.

Each of the 49 was applied by hand and fails a test (the four test modules, failfast).

Equivalents for the Python gate's code-owned list

No test can tell these apart from the code. The gate's slices create the list; this pull request only names them.

FileLineOperatorWhy no test can kill it
tool_guard.py42 if mode == "refuse":Eq_LtEThe only modes are "warn" and "refuse", and "warn" <= "refuse" is false.
command_guard.py149 if mode == "refuse":Eq_LtEThe same.
tool_guard.py90 if __name__ == "__main__":Eq_LtEImported, the name is tool_guard or kavach.tool_guard, both after "__main__".
command_guard.py240 if __name__ == "__main__":Eq_LtEThe same.
command_guard.py172 verdict(...) == 2Eq_GtEverdict returns only 0 or 2.
command_guard.py175 verdict(...) == 2Eq_GtEThe same.
command_guard.py187 if code == 2:Eq_GtEcode is verdict's.
command_guard.py91 getattr(error, "name", None) == moduleEq_IsEqual only because CPython interns both names.
command_guard.py91 getattr(error, "filename", None) == moduleEq_IsThe same.
command_guard.py86 type(end.code) is intIs_Eqtype(x) == int and type(x) is int agree for every x.
refusal_log.py183 type(end.code) is intIs_EqThe same.
refusal_log.py188 type(result) is intIs_EqThe same.

Proof

cosmic-ray 8.9.0 on this branch's tests, every mutant of the four modules, in draft veda-hq/veda#333 (never merges): run 44788243401, all four jobs green.

ModuleMutantsKilledSurvived, all on the listTimed out
tool_guard.py12512320
command_guard.py27827080
config.py12912900
refusal_log.py11811620

Each job also fails on a listed equivalent that is killed, and on a | mutant left on a rewritten annotation line; neither happened. Each mutant ran its module's main killing test files first, then the whole suite, both failfast. The first run, 44747469132, is not evidence: with every core busy the suite outran cosmic-ray's 600 s timeout, and cosmic-ray scores a timeout as a kill.

Also

  • Optional goes on each module's existing typing import line. kavach has no Python lint or version floor that prefers X | None.
  • For Alok: the first hand run of the guarded mutants wrote a few stray rows to the main checkout's .git/kavach/refusals.jsonl. My first version of the test gave guarded no event, so under a mutant the row went to the test process's own repository. The rows look like BLOCKED: 0, BLOCKED: 1, BLOCKED: 3 and BLOCKED: None. kavach keeps me out of that folder, so I couldn't remove them. The test now passes the fixture's event.

Slice: #328

🤖 Generated with Claude Code


Summary by cubic

Kills every surviving cosmic-ray mutant in command_guard.py, tool_guard.py, config.py, and refusal_log.py: 49 mutants now die under new tests, and 12 are confirmed equivalent with no test able to kill them.

Tests

  • Both guards: planted modules in the cwd or on PYTHONPATH cannot stand in for the guard's own modules, a raising refusal_log.label leaves a refusing rule's own BLOCKED line, importing a guard runs nothing, and checks that exit 2 or print non-answer output pass through as written.
  • config.py: every dataclass is frozen, and non-string refusesFrom and protectedPathsReason values are ConfigErrors, not TypeErrors.
  • refusal_log.py: excerpts cut at 200 characters, rows keep UTF-8, the log folder is made with its parents, git is called once with timeout=5, and only an exit or return of 2 writes a row.

Refactor

  • The six X | None annotations become Optional[X], so cosmic-ray has no | to swap. No source line moves, so the line numbers in slice #328 still match.