šŸ‘· ci(kavach): a cosmic-ray gate on the Python lines a pull request changes

Merged
AT
6d ago
+1760-0

Slice #558 under #238: a pull request that changes a module of packages/kavach/python/kavach gets a CI job that runs cosmic-ray 8.9.0 on the lines it changed and fails on any survivor. Once it runs, the Python sweep slices under #466 (#507–#556) can start.

Proof done; auto-merge is on.

What it does

.github/workflows/kavach-python-mutation.yml has three jobs:

  • Plan, kavach Python mutation plan: runs on every pull request. It runs the gate's own tests, then fails on any # pragma: no mutate under packages/kavach/python/ and on a malformed or stale equivalents list. It also says whether any module line or list row changed.
  • Mutants, kavach Python mutants: runs only when the plan found work. Its steps are below.
  • Gate, kavach Python mutation: one fixed-name check, which passes only when the plan passed and the mutants job either passed or had nothing to do.

What the mutants job does:

  1. It diffs the checkout, which is the pull request's merge, against HEAD^1. That is the merge base side, so the changed line numbers are the files' own.
  2. It runs the whole suite once under coverage.py, recording each test as its own context. Every Python child is recorded too.
  3. A changed statement that no test runs is refused as uncovered, with no mutant run.
  4. Each mutant on a changed line runs only the tests recorded on its lines, failfast, in its own copy of the package.

How a child's lines reach the right test:

  • A .pth file in the job's venv starts coverage in every Python process, including one started with -I or a bare environment.
  • The test's name reaches a child through a sidecar file that the recorder rewrites at each test's start and end, not through an inherited variable. Some tests build their child's environment when their module is imported (test_rules.ENV, test_refusal_log.GIT_ENV), before any test has run.
  • The hook puts the name in MUTATION_TEST_CONTEXT, which the coveragerc names as its context.

How each test run is judged:

  • Every test run (baselines and mutants) goes through gate.py cap. It sets #360's caps: RLIMIT_AS of 3 GB and RLIMIT_FSIZE of 1 GB, as ulimit -v 3000000 and ulimit -f 1000000 set them.
  • A run is timeout when it outruns 3Ɨ its unmutated time plus 30 s.
  • A run is capped when any of these happens:
    • it dies by a signal or exits 128 or above;
    • its output shows MemoryError or File too large;
    • its largest process came within 1 GB of the memory cap;
    • it left a file at the file cap.
  • Neither outcome is ever a kill.
  • On Linux, a run where the wrapper could not apply both caps is an error, never survived or killed.
  • Each distinct selection of tests first runs unmutated under the same caps. A failure there is a fault of the run, not a kill.

cosmic-ray enumerates the mutants (cosmic-ray init and dump) and applies each one (cosmic_ray.mutating.use_mutation). The gate runs the tests itself, not cosmic-ray exec, for two reasons: exec takes one test command for every mutant, and it scores a timeout as a kill.

The code-owned list

packages/kavach/python/mutation-equivalents.tsv has one row per mutant: file, the line's text, operator, nth, outcome (survived, timeout or capped) and reason.

  • A sixth column, nth, that the slice did not name. It says which of the operator's mutants on that line, counted from the left. #351 lists two Eq_Is mutants on command_guard.py line 91, so file, text and operator alone cannot tell them apart.
  • The first 12 rows are #351's equivalents.
  • When a row is checked: the gate runs a row's mutant whenever its line changes, or when the row itself is added or changed. It fails when the mutant no longer gets the listed outcome, so a row cannot go stale.
    • A row whose line no longer exists, or that names a mutant cosmic-ray does not make, fails too.
    • This pull request adds all 12 rows, so its own run checks every one.
  • Who holds it: niyam's Guard, once .github/niyam.json names it. The slice put the list's path in .github/CODEOWNERS, but #560 deleted that file after #558 was approved. #560 moved the owned files outside .github/, such as agnipariksha.json, to "guard" in .github/niyam.json, so the list goes there too. Guard then passes a change to it only when the approved slice names it under Guards:.
    • That line goes on main after this merges. Guard reads main's copy of .github/niyam.json, and it judges a created file as it does a changed one (scripts/guard.mjs at niyam v0.7.19, lines 719–735). If the line were on main first, Guard would refuse this pull request, which creates the list under a slice that does not name it.
    • Moving the list under .github/ would put it under Guard with no settings edit. But #558's Guards: line does not name such a path, and a name added after approval counts for nothing.

Choices and limits

  • Import-time lines: a changed line that runs when a module is imported (def, a decorator, a constant) also runs every test module that imports it. Each test module's import is recorded alone, in a fresh process.
  • Fixtures: what runs between two tests is the first one's class and module teardown and the next one's setup, so it is recorded against both. After the last test, it goes to that test.
  • kavach/__init__.py is not recorded. Coverage maps files to modules by name, because tests copy guards into temp folders, and __init__.py cannot be told from tests/__init__.py. A changed line there would be refused as uncovered. Today it holds 11 lines and no logic.
  • Timeout and capped on a real mutant: no proof run had one. run_tests is tested on a real copy (a sleeping test gives timeout), classify covers each capped sign, and on Linux a planted 8 GB allocation and a 1.1 GB write are both stopped and scored capped.
  • Known miss in cap detection: a non-Python child (git, bash) killed by the file cap inside a test makes that test fail, so it scores as a kill.
  • Python version: CI runs the runner's own python3, which is 3.14 today, not a pinned one.
  • Coverage core: the recording sets coverage.py's core = ctrace. On 3.14 the default core is sysmon, which records a line only the first time it runs, so a line many tests run would be tied to the first of them alone. The first CI run warned about exactly this, and a test in test_gate.py fails under sysmon.
  • Not judged: a pull request that weakens a test without touching a source line (from the slice).

Proof

niyam allows one pull request per slice, and a proof branch carries this branch's Slice: #558 commits, so the cases ran on this pull request, one push each. The two proof comments are reverted before merge; the killing test stays.

CasePushRunMutants jobResult
1. A one-line change to rules.py:630 (a comment on if len(line) > 100 and REFERENCE.search(line):)8041c474539353591921 min 54 sRefused, exactly for the line's 3 survivors: >= 100, > 99, > 101. Its other 6 mutants were killed. Each ran the 3 tests recorded on the line, in under 0.5 s.
2. The same change, with test_the_limit_is_100_characters (100 characters passes, 101 fires)914e7f74540353231624 min 15 sPassed: all 9 of line 630's mutants killed, now by 4 tests, the new one among them, in 0.3 to 0.4 s each. The 12 rows held.
3. A line only a subprocess runs: a comment on command_guard.py:241, sys.exit(run())8041c4745393535919same run as case 1Not reported uncovered: 542 tests are recorded on it, every one through a child process. The line has no mutant.

| Clean: the proof comments reverted, Cubic's fixes in, no module line changed | a441b60 | 45411194937 | 33 min 0 s | Passed: the 12 rows, all survived as listed. No fault, nothing uncovered. |

Every run here also checks the 12 list rows, because this pull request adds them.

What a pull request pays. The suite is recorded once on every pull request that changes a module line: 896 s in the clean run, 593 to 656 s in the earlier ones. On top of that come the mutants on its changed lines. Line 630's nine mutants took 3 to 4 s in all, after a baseline of their 3 or 4 tests. The rest of each run's time is the 12 rows. A pull request pays for those only when it changes a row or a listed line, as this one does.

The clean run is slower than the proof runs. Its teardown attribution, fixed after Cubic's review, ties more tests to the hot lines the rows sit on: baselines took 516 s and mutants 533 s, against 335 s and 352 s in case 1. In run 45393535919 all 12 got their listed outcome, survived.

Where case 1's time went (the Time line):

  • recording the suite under coverage, 593 s;
  • unmutated baselines, 335 s;
  • mutants, 352 s.

Almost all of the baselines and mutants time is the rows on hot lines. The two refusal_log.py rows select 162 and 667 tests and took 327 s and 287 s. Line 630's own nine mutants took 3.4 s in all.

The first run, 45389994375, failed in its recording. A test asserts the package's folder is named kavach, and the gate's copies were not. It also ran on coverage.py's sysmon core. Both are fixed.

For Alok

  • Required check: making kavach Python mutation required on main's ruleset is your step, after this merges.

  • Two entries in "guard" in .github/niyam.json, after this merges:

    • "packages/kavach/python/mutation-equivalents.tsv", the list;
    • "scripts/kavach_mutation/", the gate's script. The workflow runs the pull request's own copy of it, as mutation.yml runs scripts/mutation-lanes.mjs, which is on that list for the same reason (Cubic's P1).

    The file is yours to edit, so this pull request leaves it alone. Until the entries are there, a pull request can add a row, or weaken the gate, with no slice naming either. Adding them before the sweep slices under #466 start closes that gap.

  • What that line means for the sweep:

    • The Python sweep slices (#507–#556) send any equivalent they cannot remove to this list, but none names the list under Guards:. With the line on main, Guard refuses a sweep pull request that adds a row.
    • That is the review the slice asked for, but each such row then needs a slice that names the list when you approve it. That could be the sweep slice itself, re-approved with the list in its Guards: line, or a small slice of its own for the rows.

Slice: #558

šŸ¤– Generated with Claude Code