All writing

Reusing mutation-testing verdicts across reruns

These are notes on adding a kind of resume to a slow check, the mutation-testing runner I use on an iOS app I’m building, so that a rerun after a crash or a reboot doesn’t start from zero. It looked like plain caching at first, keyed on “the same code”, and most of the work was in two places, deciding what “the same” has to cover, and making sure a reused result still goes through everything a fresh one would.

How a check works

The runner and the check are the same ones from the post about the machine-wide build lock, which has the longer description (and the post on TDD and mutation testing has why I bother with mutation testing at all). What this post needs from it is that before any mutant the runner does a start run of the selected tests on clean code, so we know which tests pass to begin with, and after the last mutant it does one final run on the restored code. If that final run shows a red that the start didn’t already have, every kill in the check is voided, because a test that fails without any mutant applied can’t be trusted to have failed because of one. The runner also has a few ways to apply a mutant (switching it on in one shared build, editing the file and rebuilding, or running a Rust crate’s tests with cargo), and each takes its own start runs, which are kept in a list per place the tests run (the iOS simulator, the Mac, or one crate’s tests) that the code calls a lane.

The check from that post had 74 mutants and about 160 test runs, since some mutants need follow-up runs to confirm what they saw, which comes to roughly two hours and forty minutes when nothing gets in the way. The runner kept every verdict in memory and wrote the report at the very end, so a signal, a reboot or a crash two hours in left the result bundles on disk and nothing that said what they meant, and the rerun did every run again.

Storing the runs instead of the verdicts

The obvious fix is to write each mutant’s verdict to disk as it settles and read it back on the next run, but it pays to go a bit further and keep the evidence instead of the verdict, so each record holds the mutant, its claims (which test and which assertion should catch it), the mutant’s own run, the start run it was judged against, and every follow-up run with the question it answered. A rerun reads those result bundles again and hands them to the current verdict code, which judges them from scratch, and the old verdict is never copied.

The reason is that the verdict half of the runner is a pure function of the recorded runs, so it can run again on old evidence for the cost of re-reading the bundles (one xcresulttool export per run, which I expect to take seconds but haven’t timed yet), while the half that made the runs (applying the edit, building, running on a simulator) can’t. Re-judging also means a fix in the verdict code applies to old runs too, and a bundle that no longer reads sends the mutant back to run instead of carrying an unreadable result forward. The reader had a bug of a related kind around the same time, where xcresulttool describes a skipped test with a “Skip Message” node the reader didn’t know, so it rejected the whole bundle and every claim in a run with one skipped test got no verdict at all.

A record is only written for a mutant whose claims settled as killed, survived or unreached. A run that never executed anything (the test runner refused to start, for example) settles as “no verdict” in the runner, and reusing that would carry a refusal forward as if it were a result.

What “the same tree” has to cover

The runner already computed an id for the tree it ran against, a hash of the HEAD commit plus the uncommitted diff, so the first idea was to reuse a record when that id matches, which stops working as soon as you look at the usual order. I run the check and then commit, and committing the exact same bytes gives a new HEAD and so a new id, which means a rerun after a commit reuses nothing. My first recommendation was a content id that ignores HEAD, and I’d even checked in a throwaway repository that the git tree hash of the working tree stays the same across a commit of the same bytes.

Checking whether anything in the build could see HEAD refuted that, because the app has a build phase that runs git rev-parse --short=12 HEAD, adds -dirty when the tree has uncommitted changes, and writes the result to a text file that gets copied into the app bundle, and there’s a test that reads that value from the real test host. So the same bytes under a different commit build a different app, and a test can observe the difference. No test asserts the exact value today, but nothing stops one from doing it tomorrow, and a cache that’s only correct until somebody writes that test isn’t worth having. The key stayed the tree id with HEAD in it, and reuse after a commit is given up.

If you’re keying a cache on “the same source”, grep the build for anything that reads git. Version stamps, build numbers and “built from” strings make HEAD an input whether the source changed or not.

Besides the tree, a run rests on the runner’s own code, which made the runs, on the toolchain the Rust part of the app is built with, and on the Xcode version, the simulator runtime and (for runs on the Mac) the macOS version the tests ran on, so all of those went into the key as well. Trimmed a bit, it looks like this:

def key(snapshot, m, claims, route, code, ffi, environment) -> dict:
    return {"snapshot": snapshot,      # tree id, HEAD and the dirty diff included
            "mutant": {"id": m.id, "file": m.file, "old": m.old, "new": m.new,
                       "select": list(m.select), ...},
            "claims": sorted(([c.test, list(c.assertion), ...] for c in claims),
                             key=json.dumps),
            "route": list(route),      # which way the mutant is applied, and why
            "code": code,              # hash of the runner's own modules
            "ffi": ffi,                # the Rust inputs and toolchain stamp
            "environment": environment}  # Xcode, simulator runtime, macOS for Mac runs

The record is JSON, and comparing a freshly built dict against one read back from disk only works if the dict holds what JSON gives back, so tuples become lists before they go in, and the claims are sorted so the comparison doesn’t depend on the order they were listed in. The code hash covers the runner’s modules (its tests aside) and the shared modules it loads, rather than only the ones that make runs, which costs a full rerun after any edit to the runner, but it means there’s no list of “modules that matter” that someone has to keep current.

Confirming a reused result at the end of the check

Most of the time went into the end of the check, since a reused kill has to be confirmed by this check’s final run the same way a fresh one is, and every way that confirmation could be skipped turned out to be a bug. The first one showed up while reading the code before anything was built, and the other three came up in review.

The final run picks what to check by looking up each claim’s test in the start runs, and only checks a claim if some start ran its test, while a check’s start only covers the mutants it’s going to run. A reused mutant isn’t run, so its test wasn’t in any start, and the final run would have skipped it. The fix was to carry the old check’s start run in with the reused record, ahead of this check’s own starts in the lane, so this check’s start counts as the latest one.

The second was that the final run compared each claim against the latest start only, so if this check’s start saw a test red, and the reused kill had been judged under an older start where it was green, the red final run was excused because “it was already red at the start”. Now a claim’s test that passed in any start of its lane has to pass in the final run, and the latest start only decides which reds in other tests were already there. This could happen before reuse existed too, between a check’s switched and rebuilt mutants, it was just much harder to reach.

The third and fourth were voided kills coming back, since a check whose final run voids its kills had already written records for every mutant that settled before that point, so the next rerun reused them and its own green final run brought the voided kills back as if nothing happened. The fix was to mark a check’s records void when its final run finds a red, and then a second look at that fix found that it only touched the current check’s records, while a kill it had reused still sat in the earlier check’s record, so a third run fell back to that one, and voiding now reaches every check the run reused from as well.

All of these are covered by tests (more than twenty, each one seen to fail with the statement it covers broken), but no real check has reused anything yet, so I haven’t seen it save a rerun outside the tests.

Summary

When we reuse results from a slow check, the key has to hold everything the build reads, and HEAD is easy to miss when a build step turns it into a file, so it’s worth grepping the build for git before deciding that a commit of the same bytes is the same input. The bigger part of the work was making sure a reused result goes through the same final confirmation a fresh one does, with the start it was judged against carried in, and that anything which voided a result once (in this check or in any check it reused from) keeps it voided, which is where every one of the four bugs was.

Email address copied [email protected]