Skip to content

ub check: run each mutant's importing tests first, full suite only for survivors; hide mutant-count warning - #16

Merged
0xjgv merged 7 commits into
mainfrom
ub-cli-fast-mutation
Nov 17, 2026
Merged

0xjgv merged 7 commits into
mainfrom
ub-cli-fast-mutation

Conversation

@0xjgv

@0xjgv 0xjgv commented Nov 17, 2026

Copy link
Copy Markdown
Owner

ub check ran the whole test suite for every mutant. Now each Python mutant first runs the test modules that import its module. Only a mutant those tests leave alive reruns the full suite. The per-file WARNING: N covered mutations selected line now prints only with --verbose.

Before / after

All runs on the same 10-core Mac, rm -rf .metrics target first, TEST='uv run -q --no-project --python 3.11 --with-editable . python -m unittest discover -s tests -q'.

Run Wall Result stderr
main's ub check on main's src 327 s 273 killed, 0 survived, exit 0 196 B (three WARNING lines)
this branch's ub check on main's src 104 s, 91 s 273 killed, 0 survived, exit 0 0 B
this branch on its own src (the done-criteria command) 102 s, 112 s 314 killed, 0 survived, exit 0 0 B

Running the branch on main's src gives the same 273 killed and 0 survived as main. On its own src the count rises to 314 only because the self-gate also mutates the code this PR adds to check.py.

Approaches weighed

  • (a) Per-mutant test selection with a full-suite fallback. Picked. It needs no change to the user's command, and the fallback means selection can only change the speed, never the verdict.
  • (b) A {file}/{module} placeholder in --test-command. Rejected. Every user would have to rewrite their command, and the done-criteria command has no placeholder.
  • (c) Reuse an interpreter or skip uv startup per mutant. Rejected on measurement: uv run ... python -c pass takes 0.09 s, against a 4.5–9 s suite. The cost is in the tests, not the startup.

How focus works (src/ub_cli/check.py)

  • python_imports parses every project .py file with ast (all import statements, including ones inside functions). It skips SKIP_LINK directories, dot directories and node_modules.
  • affected_tests walks that graph out from the mutated module and groups the test*.py importers by import distance.
  • _focuser builds one command per test module: the user's command plus -f -k '*test_x.*'. It runs each command once on the original code and drops any that fail. It orders them nearest distance first, then test_<stem>, then fastest baseline.
  • FocusedRunner runs a mutant through those commands. The first failure or timeout kills the mutant. If every focused command passes, the full command runs. Baseline runs (no timeout) bypass focus.
  • Focus applies only when the command is a unittest discover with no shell operators, no comment and no short flag containing k. Such a flag would OR its pattern with ours and widen the user's selection. Any other command (make test, pytest, python -c ...) behaves exactly as before.
  • Focused runs use the engine's full-suite timeout. A timeout scaled down to the focused baseline could kill a slow mutant that the full suite would not. The cost: two signature.py mutants hang and hold a ~45 s tail, which is most of the remaining time.

Limit of "equals the full-suite result": a focused failure is a kill without a full run. A test that passes in the full suite but fails when run alone under the mutant (an order-dependent test) could turn a full-suite survivor into a kill. The baseline check catches order-dependence on the original code only.

Tests (tests/test_check.py::FocusTests)

  • Module names, relative imports and imports inside functions. Worker, dot-dir and node_modules trees are excluded.
  • Transitive selection grouped by distance.
  • The narrowing rules: -k, -kslow, -qk, quoted -k, # comment, 2>&1, &&, pytest, unbalanced quote.
  • Ordering: nearest distance, then the module named for the file, then the fastest. A module that fails its baseline is dropped.
  • FocusedRunner stops at the first failing focus and skips focus for baselines.
  • A mutant killed by a focused test never runs the full command.
  • Fallback: a mutant killed only by a test that loads the module through importlib (so it is not selected) is still reported killed, after [focused, full] runs.
  • The WARNING prints only with --verbose.

make test: OK. The gate's own mutation run over check.py kills every mutant of the new code (0 survived above).

Not covered

  • pytest commands get no focus and run as before.
  • Imports via import_module or a subprocess are not followed. They cost a full rerun, never a wrong verdict.
  • On Python < 3.12, a -k that matches nothing exits 0, which wastes a run but cannot change a verdict.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T19:59:26.322673Z 0abce42 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@0xjgv
0xjgv merged commit 87c4582 into main Nov 17, 2026
2 checks passed
@0xjgv
0xjgv deleted the ub-cli-fast-mutation branch November 17, 2026 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant