Skip to content

tools/mutants: sandboxed mutation testing with exact manifests (#1114) - #1171

Draft
Smaug123 wants to merge 4 commits into
mainfrom
claude/mutation-testing-infra
Draft

Smaug123 wants to merge 4 commits into
mainfrom
claude/mutation-testing-infra

Conversation

@Smaug123

Copy link
Copy Markdown
Collaborator

Claim. tools/mutants/ runs cargo-mutants over a named crate and file set only inside a Linux sandbox, and pins each set's verdicts in an exact keyed manifest that a dedicated CI job checks.

  • On a Mac the sandbox is Apple's container: the Linux Nix store is read-only, there is no network, the user is unprivileged, memory and time are bounded, and no host path is writable by mutant code.
  • In CI it is a job of its own on a GitHub-hosted runner.
  • A canary refuses every other place before any mutant is compiled or run.

Domain. The infrastructure, plus one pilot set that witnesses it end to end: nuget-regular-file, 7 mutants, all caught. The first real measurement, #1114's nuget sample and #1118's harness census and filter logic, is the follow-up PR. Part of #1114 and #1118.

Vacuity. The run's exit code enforces it; the manifest header does not. A missing or failed baseline (exit 94), a set naming no mutant (95), a graded set that is not the listed set, and a run that catches nothing all fail rather than write a manifest. The header holds nothing that moves, so the keyed merge driver never conflicts on it.

The CI job

.github/workflows can't be pushed from here. The patch is mutants-ci.patch, and the user commits it onto this branch. It adds:

  • mutants-sets, which lists the sets;
  • test-mutants, one matrix entry per set, which runs tools/ci/mutants.sh SET with BORZOI_MUTANTS_CI_JOB=true scoped to that job, TMPDIR on /mnt, timeout-minutes: 330, and the run uploaded as an artifact;
  • both jobs as needs of all-required-checks-complete.

It passes detect-changes.test.sh, including the rule that the required aggregate includes every test-* job, and the other workflow-shape tests. test-mutants is ungated by the change filters, as corpus-diff is. The heavy sets in the follow-up PR will need a per-set filter or sharding (#1170).

How it works

tools/mutants/run.sh SET (macOS, outside nix develop)

  • It refuses a dirty tree and exports HEAD with git archive. It writes the manifest only if the checkout is still that commit, clean, when the run ends.
  • It clears Git's repository-local variables first, so an inherited GIT_DIR can't redirect it.
  • Lock. It holds a lock in the store volume's own namespace: the container volume borzoi-mutants-nix-lock.
    • It takes the lock only while no borzoi-mutants-* container remains, because a killed run's VM might still mount the ext4 store.
    • It releases the lock only once container list confirms its own containers are gone.
    • If the listing fails, it keeps the lock.
  • Containers. All run on nixos/nix, pinned by digest:
    • seed: fills the Linux store volume from the image once, with a completion marker.
    • provision: the only networked step and the only one with the store writable. It realises the devshell and writes nix print-dev-env.
    • prepare: sets up a fresh 64G scratch volume.
    • run:
      • --network none;
      • the store volume read-only, with no daemon and no socket (the host's Darwin store is never mounted);
      • the tree and the dev env read-only;
      • uid 1000;
      • --memory/--cpus;
      • writes only to the scratch volume.
    • export: runs no mutant. It first confirms the run's VM is gone, then copies three artifacts out. Each must be a regular file reached through no link, within the size limit.
  • Deadline. One deadline bounds every container. The run container also enforces it inside the guest with timeout, so it ends on time even if the host script is killed.
  • On failure it keeps the scratch volume, with cargo-mutants' logs, and prints how to read it and how to delete it.

tools/ci/mutants.sh SET

It checks the canary before touching earlier output, then runs the same inner.sh under nix develop. --list gives the matrix.

tools/mutants/inner.sh

It runs with --tests, a fixed PROPTEST_RNG_SEED, and BORZOI_UPDATE_MANIFESTS cleared, so a regeneration grades the same suite a check does. It records the --list output that the checker matches against.

The canary (tools/mutants/canary.sh)

.cargo/mutants.toml installs it twice:

  • as the workspace's rustc wrapper, so it refuses before any workspace crate compiles, whatever the target;
  • as the runner for each target triple the workspace builds for, plus cfg(all()). A CLI --config outranks CI's CARGO_TARGET_<TRIPLE>_RUNNER.

It allows a run only when all of these hold:

  • the kernel is Linux;
  • /nix/store is not writable;
  • either the container's nonce file matches the environment nonce, or the process is CI's mutation job (GITHUB_ACTIONS, CI, RUNNER_ENVIRONMENT=github-hosted, BORZOI_MUTANTS_CI_JOB=true).

It prevents accidental runs, not malicious ones: spoofing those variables gets past it, as its header says. Doctests never go through a runner, so they are left out.

tools/mutants/canary.test.sh

It runs under cargo test, in oracle-harness's suite, and the change filter routes tools/mutants and .cargo to that suite. It checks:

  • the rule against all 24 fact combinations;
  • each CI variable's necessity, and that a self-hosted runner is refused;
  • the end-to-end refusal of the runner, of inner.sh and of tools/ci/mutants.sh, with a cargo shim recording that nothing ran and an earlier run's output surviving;
  • Cargo's actual selection. It builds a probe crate under mutants.toml's arguments, against a competing CARGO_TARGET_<host>_RUNNER. The canary must refuse as the wrapper, with nothing compiled. With the wrapper overridden by the one variable that outranks it, the canary must refuse as the runner. I mutation-checked both layers.

The manifest (borzoi_oracle_harness::mutants, written by the mutants-manifest bin)

  • Lines: one line per mutant, <key>\t<verdict>.
  • Keys: cargo-mutants' name without line and column, with #n for repeats in source order. A proptest follows each mutant through line shifts and report-order permutations.
  • Scope entries: keyed scope: package, scope: files and scope: skip <filter> entries, so adding a skip moves the manifest even when no verdict moves.
  • Verdicts: caught, missed, timeout, unviable, or equivalent: <reason>. Each tools/mutants/equivalent.tsv row names its mutant by key and by site, the source line, collapsed. A row whose key now has another site, whose site repeats, whose mutant wasn't missed, whose key or set doesn't exist, or that is a duplicate, fails the run.
  • What the checker requires. It reads the run only from the launcher-owned directory, never through a link, with sizes bounded, and reads sources the same way. It refuses unless:
    • the checkout is the graded commit and clean;
    • the run's scope is the one sets.tsv declares now;
    • the set name is one plain path component.
  • Timeouts: minimum_test_timeout = 300 keeps a verdict independent of machine speed. regular_file's FIFO test catches its mutant only at its own 120 s deadline.

Fixes no test can catch

These are exercised only by a run under Apple's container, which neither CI nor cargo test has. Each was exercised by hand: a container run passed on every head from round 1 to round 9.

  • The lock (volume as lock, the leftover-container check, keeping the lock when listing fails, release only on confirmed removal). It needs container, and a test would need two concurrent VMs.
  • The deadline across provisioning, the in-guest timeout, and the five-minute export allowance. A test would need a real VM that stalls.
  • The export. A test would need a mutant that plants links or oversized files in the volume:
    • it confirms the VM is gone before mounting the volume;
    • it copies with realpath and size checks.
  • Keeping the scratch volume on failure. A test needs a VM.
  • The unprivileged run user (--uid 1000). It is witnessed by the root-only baseline failure it fixed (list_committed_package_versions_errors_when_commit_marker_stat_fails), not by a test.

A CI job could exercise none of these: CI runs tools/ci/mutants.sh, not run.sh.

Codex rounds

Rounds 1–5 were at xhigh; rounds 6–13 were at high, the user's setting from 2026-11-25 22:12 UTC. Labels: R1 changes a result on realistic input, R2 needs contrived input, R3 is a test or oracle that checks less than it claims, R4 is hygiene.

Round Effort Findings Breakdown
1 xhigh 11 R1×3, R2×3, R3×3, R4×2
2 xhigh 11 R3×3, R2×7, R4×1
3 xhigh 9 R2×5, R3×4
4 xhigh 7 R2×4, R3×2, R4×1
5 xhigh 4 R2, R3×2, R4
6 high 4 R3 (CI wiring), R2×3
7 high 4 R3 (CI wiring), R2×3
8 high 4 R3 (CI wiring), R2×2, R4
9 high 4 R3 (CI wiring), R2×2, R4
10 high 2 R3 (CI wiring), R2
11 high 2 R3 (CI wiring), R2
12 high 2 R3 (CI wiring), R2
13 high 1 R3 (CI wiring)

Round-by-round detail.

  • Round 1, R1s:
    • a target-triple runner outranked the cfg() canary, and CI sets one;
    • the regeneration env leaked into the graded suite;
    • a FIFO test made verdicts depend on machine speed.
  • Round 2:
    • graded set matched only by count, not identity;
    • unbounded host reads;
    • unchecked skip changes.
  • Rounds 3–5: lock namespace and stale-lock recovery, the deadline, export containment, equivalence rows bound to ordinals.
  • Rounds 6–12: source paths from sandbox output, list failure, VM-still-running before export, set names as paths, ambiguous sites, deadline inside the guest, Git env vars, unreadable table, aggregate site work, staging cleanup, relative home, build-time checkout, live-owner lock advice, umask.

Each fix that a test can catch has one: the shell tests, or borzoi_oracle_harness::mutants's and the bin's unit tests. The CI-wiring finding, in every round, is closed by mutants-ci.patch. The final round runs once CI has run that job green on this branch.

Verification

  • End to end in container on every head: provision, run (--network none, read-only store, uid 1000), export, manifest check.
    • The baseline passes offline, nuget-oracle differentials included.
    • 7/7 caught.
    • Flipping one verdict in the checked-in manifest fails with a line diff (exit 1).
  • Gate on the final head: cargo fmt --check, cargo clippy --all-targets --all-features -D warnings, cargo doc (-D warnings, private items), cargo test --no-fail-fast, plus both shell tests.

🤖 Generated with Claude Code

Smaug123 and others added 4 commits November 26, 2026 03:11
cargo-mutants (from the flake's devshell) runs over named sets of files
(`tools/mutants/sets.tsv`) only in a Linux sandbox: on a Mac,
`tools/mutants/run.sh SET` runs it in Apple's `container` with the Linux
Nix store read-only, no network, an unprivileged user, and bounded memory
and time; in CI, `tools/ci/mutants.sh SET` runs it in a job of its own.

Every test binary runs through `tools/mutants/canary-runner.sh`
(`.cargo/mutants.toml`), which refuses unless the kernel is Linux, the
store is not writable, and either the container's nonce canary or a CI
runner is present. So a bare `cargo mutants` elsewhere fails its baseline
and runs no mutant. `tools/mutants/canary.test.sh` holds the rule to its
table and checks the refusal end to end.

Each set's verdicts are an exact keyed manifest
(`tools/mutants/manifests/<set>.txt`, `borzoi_oracle_harness::mutants`):
one line per mutant, keyed without line numbers. Equivalent mutants are
recorded, with their argument, in `tools/mutants/equivalent.tsv`. A failed
baseline, a run that lists or catches no mutant, or a stale equivalent
fails rather than writing a manifest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sandbox's scripts to the workspace filter

The canary test now runs a bare `cargo mutants` in a crate carrying the
repository's own `.cargo/mutants.toml`, outside any sandbox, and asserts
that cargo-mutants' unmutated baseline is refused by the canary (exit 4)
before the crate's test starts. That covers what the precedence probe
cannot: that cargo-mutants itself reads the configuration.

The refusal cases take a set from `sets.tsv` rather than naming one.

A set's manifest or `equivalent.tsv` is read by no test, and
`test-mutants` checks them on every change. So only the sandbox's
scripts, its set table and `.cargo/mutants.toml` route to the
`workspace` filter, and regenerating a manifest runs no crate job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tifacts, report lock errors

- An `equivalent.tsv` row's site is now a fingerprint (FNV-1a over the
  exact text) of every line the mutation replaces, not just its first
  line. An edit anywhere in the code the argument covers, a literal on a
  later line included, fails the row until it is reviewed.
- `tools/ci/mutants.sh` runs the set in a work directory of its own and
  stages out of it, through `tools/mutants/stage.sh`, only regular files
  whose real path lies inside it: the three artifacts whole, up to 64 MiB,
  and each log's first MiB. The job uploads, and the manifest step reads,
  only that staged directory. `stage.test.sh` holds the guard to links out
  of the run, linked directories, FIFOs and oversized files.
- `--list` fails when `sets.tsv` declares no set.
- `run.sh` reports why it could not take the lock when there is no lock
  to inspect, for example because the container service is down, and
  prints cargo-mutants' output when the bare-run canary case fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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