Skip to content

feat(mutation): compile each package's mutants once and run only the tests that reach them - #13

Merged
kamuikatsurgi merged 2 commits into
mainfrom
kamuikatsurgi/fast-mutation-engine
Nov 21, 2026
Merged

kamuikatsurgi merged 2 commits into
mainfrom
kamuikatsurgi/fast-mutation-engine

Conversation

@kamuikatsurgi

Copy link
Copy Markdown
Member

Summary

Go mutation testing ran a fresh go test -overlay ./... for every mutant: a new compile, a new link and the whole suite each time. Cost grew linearly with the diff, and a large PR ran past the CI timeout with no report at all. heimdall-v2#701 (568 mutants) was cancelled at 40 minutes.

This change makes the Go engine build each package once and run only the tests that can kill each mutant. On that PR the mutation run drops from 21m04s to about 1m05s on the same machine, with the same verdicts.

What was slow

Measured on heimdall-v2#701 (head df908bf8) with the original engine:

Finding Evidence
One build, link and full suite per mutant 568 mutants, 222 in app and 196 in bridge/processor
One test dominated a package TestClerkTaskUnderStateSyncBurst is 10.9 s of an ~11 s suite and reran for all 196 bridge/processor mutants
Build and link was about half the cost in app ~4 s of ~8 s per mutant
92 mutants (16%) did not compile and were scored as killed 78 of 171 return_value mutants, because every result was rewritten to nil
22 runs were duplicates two sites on one line both mutated the first match; the second site was never tested
A timeout lost everything no progress output, no partial report, no baseline run

What changed

Go engine (internal/lang/goanalyzer)

  • One build per package. Every mutant of a package is compiled into a single test binary behind a runtime switch (DIFFGUARD_MUTANT). The instrumentation is syntactic: comparisons and arithmetic go through small generic helpers that evaluate operands once in source order, statements are wrapped in guards.
  • Compile-error repair. If the instrumented package does not compile, the mutants the compiler blames are dropped from the shared build and it is rebuilt (up to 6 times).
  • Targeted tests. Each test is run once, unmutated, with tracing on, which records the mutants it reaches. A mutant is then tested by running only those tests, fast ones first, with -test.failfast. Tests that only pass together fall back to one traced run of the whole suite.
  • Subpackage tests still count. A test binary is built for the mutated package and for each package beneath it whose tests link it, matching the old ./... scope.
  • Overlay fallback. A mutant that cannot share the build (for example one in a constant expression), or a package whose shared build or baseline fails, is tested with its own go test -overlay run, after a compile check and an unmutated baseline.
  • Derived timeouts. A mutant's limit is a small multiple of how long the same tests take unmutated, capped by --test-timeout.
  • Exact sites. Sites carry a byte offset, so two sites on one line, or the two operators in a - b - c, are distinct mutants.
  • Typed zero returns. return_value now returns *new(T) for each declared result type, so the mutant compiles for any signature. Returns that are already all zero values (nil, false, 0, "", T{}) are skipped as equivalent.

Orchestrator (internal/mutation), all languages

  • Outcomes are classified as killed, timed out, survived, not covered, does not compile, not applied, or not run. Only mutants that compiled and ran are scored; the summary lists the rest.
  • If a package's tests fail before any mutation, its mutants are reported as not run instead of all "killed".
  • Gating tiers run first (Tier 1, then 2, then 3).
  • New flags: --mutation-budget, --mutation-cache-dir, --mutation-shard i/n, --mutation-confidence, --mutation-seed.
  • Progress lines on stderr, and a report of what finished on SIGINT/SIGTERM.
  • A gating tier with mutants that never ran is at least WARN.

Rust and TypeScript keep their existing appliers and runners. Languages opt in to the new path through two optional interfaces, lang.SessionRunner and lang.Fingerprinter.

Results on heimdall-v2#701

Both engines ran with the heimdall-v2 workflow's flags on a 12-core Mac.

Before After
Wall time 21m04s 1m05s – 1m12s (3 runs)
CPU time 6,864 s ~330–360 s
Mutants tested 568 557
Non-compiling mutants scored as kills 92 0 (5 excluded and reported)
Re-run with a warm cache n/a 4 s
Overall score 93.0% (528/568) 92.4% (510/552)
Tier 1 96.0% (332/346) 95.8% (321/335)
Survivors 40 42
  • All 40 of the original engine's survivors also survive in the new one, and nothing it left alive is killed by the new one.
  • The 2 new survivors (bridge/processor/event_owner.go:94-95) were counted as kills before only because the mutant failed to compile.
  • Two cold runs and the cached run produce identical survivor sets.
  • The "before" timing is slightly inflated: a few minutes of light test work overlapped with it.
  • Not measured on a CI runner. The CPU time alone implies roughly 1.5–3 minutes on 4 cores.

Other modes on the same PR:

Run Wall time Result
--mutation-workers 4 1m30s identical to the full run
--mutation-budget 20s 22 s 62 Tier 1 mutants tested, 495 reported as not run, WARN
--mutation-shard 1/4 58 s 140 mutants
--mutation-confidence 0.99 1m04s Tier 1 settled after 47 mutants, 288 skipped

Sharding and early stop save little here: most of the minute is per-package preparation (build plus tracing each test), not the mutant runs.

Behaviour changes to know about

  • Scores are slightly stricter. Non-compiling mutants no longer pad the kill count, and typed zero returns turn former compile failures into real mutants.
  • "Not covered" is new. A mutant no test executes is reported as NOT COVERED and counts as a survivor. A branch-removal mutant in a branch no test enters is reported this way.
  • --test-timeout is now a ceiling for Go, not the limit itself.
  • Sampling is seeded (default seed 1), so the same commit selects the same mutants.
  • The JSON stats gain fields: generated, cached, and an outcomes map.
  • The cache fingerprint covers the toolchain, go.mod/go.sum, and every file of the in-repo packages a package's tests are built from, including testdata. It does not capture external services or environment variables.
  • New dependency: golang.org/x/tools (for astutil).

Executed tests

  • make build, make test: all packages pass.
  • ./diffguard --base main . on this change exits 0: complexity max 8, sizes, deps, churn and dead code PASS; mutation 91.6% with Tier 1 at 97.1% (334/344), in 3m09s.
  • New end-to-end tests build throwaway modules and check every outcome class, the whole-suite fallback, a failing baseline, test patterns, subpackage tests, cancellation and the compile-error repair.
  • Per-mutant cross-check against the original engine on heimdall-v2#701, described above.

Rollout notes

Tooling only. Repositories that pin a diffguard commit (heimdall-v2, sequence-store-proto) keep the old engine until the pin is bumped; bor installs @latest. To get the most out of this in CI, add --mutation-cache-dir with an actions/cache step and a --mutation-budget below the job timeout.

This touches collectMutants' neighbours in internal/mutation, so it will conflict with #11.

…tests that reach them

Go mutation testing ran a fresh go test -overlay ./... for every mutant, so
cost grew linearly with the diff: heimdall-v2 PR 701 (568 mutants) took 21m
on a 12-core machine and was cancelled at the 40m CI timeout.

All mutants of a package are now compiled into one test binary behind a
runtime switch. A traced run of each test records which mutants it reaches,
and a mutant is tested by running only those tests. Mutants that cannot share
the build fall back to their own overlay run.

Also:
- classify outcomes (killed, timed out, survived, not covered, does not
  compile, not run) and score only mutants that compiled and ran
- identify sites by offset so two sites on a line are distinct mutants
- zero returned values with typed zero values so the mutant compiles
- run gating tiers first; add a wall-clock budget, progress lines and a
  partial report on SIGINT/SIGTERM
- add a result cache, sharding, early stopping and seeded sampling
@kamuikatsurgi
kamuikatsurgi requested a review from cffls November 20, 2026 08:40
@kamuikatsurgi

Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

…tation-engine

# Conflicts:
#	internal/mutation/mutation.go

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@kamuikatsurgi
kamuikatsurgi merged commit e02ef8e into main Nov 21, 2026
6 checks passed
@kamuikatsurgi
kamuikatsurgi deleted the kamuikatsurgi/fast-mutation-engine branch November 21, 2026 05:15
Copilot stopped work on behalf of kamuikatsurgi due to an error November 21, 2026 05:15
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.

2 participants