From c6694d3d2e22ffbb3d04c30a15e182f96f6cb133 Mon Sep 17 00:00:00 2001 From: Paul Otto <219596+potto007@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:00:11 -0700 Subject: [PATCH 1/5] ci: add advisory diff-mutation check on PRs Signed-off-by: Paul Otto <219596+potto007@users.noreply.github.com> Github-Issue: #246 --- .github/scripts/mutants-scope.py | 10 ++++ .github/scripts/mutants-summary.sh | 33 +++++++++-- .github/workflows/mutants-diff.yml | 90 ++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 .github/scripts/mutants-scope.py create mode 100644 .github/workflows/mutants-diff.yml diff --git a/.github/scripts/mutants-scope.py b/.github/scripts/mutants-scope.py new file mode 100644 index 0000000..cd2ea4b --- /dev/null +++ b/.github/scripts/mutants-scope.py @@ -0,0 +1,10 @@ +import fnmatch +import sys +import tomllib + +with open(sys.argv[1], "rb") as f: + globs = tomllib.load(f).get("examine_globs", []) + +for path in sys.stdin.read().splitlines(): + if any(fnmatch.fnmatchcase(path, glob) for glob in globs): + print(path) diff --git a/.github/scripts/mutants-summary.sh b/.github/scripts/mutants-summary.sh index 1d8b4a7..4a511fa 100755 --- a/.github/scripts/mutants-summary.sh +++ b/.github/scripts/mutants-summary.sh @@ -3,18 +3,25 @@ set -euo pipefail shards_dir=$1 out=$2 +mode=${3:-nightly} mkdir -p "$out" for kind in caught missed timeout unviable; do - find "$shards_dir" -name "$kind.txt" -exec cat {} + 2>/dev/null | sort -u >"$out/$kind.txt" + { find "$shards_dir" -name "$kind.txt" -exec cat {} + 2>/dev/null || true; } | sort -u >"$out/$kind.txt" done count() { grep -c . "$1" || true; } { - echo "## Mutation grading" - echo - echo "cargo-mutants over the files in \`.cargo/mutants.toml\`, graded by the \`pr_chaos\` tests." + if [[ $mode == diff ]]; then + echo "## Diff mutation grading" + echo + echo "cargo-mutants over the lines this PR changes in the files in \`.cargo/mutants.toml\`, graded by the \`pr_chaos\` tests. Advisory: a missed mutant does not block the merge." + else + echo "## Mutation grading" + echo + echo "cargo-mutants over the files in \`.cargo/mutants.toml\`, graded by the \`pr_chaos\` tests." + fi echo echo "| file | caught | missed | timeout | unviable |" echo "| --- | ---: | ---: | ---: | ---: |" @@ -28,5 +35,21 @@ count() { grep -c . "$1" || true; } done echo "| **total** | $(count "$out/caught.txt") | $(count "$out/missed.txt") | $(count "$out/timeout.txt") | $(count "$out/unviable.txt") |" echo - echo "Survivors are listed in \`missed.txt\` and \`timeout.txt\` in the \`mutants-survivors\` artifact." + if [[ $mode == diff ]]; then + if ! grep -q . "$out"/{caught,missed,timeout,unviable}.txt; then + echo "The changed lines hold no mutants." + echo + fi + for kind in missed timeout; do + [[ -s $out/$kind.txt ]] || continue + echo "### ${kind^}" + echo + echo '```text' + cat "$out/$kind.txt" + echo '```' + echo + done + else + echo "Survivors are listed in \`missed.txt\` and \`timeout.txt\` in the \`mutants-survivors\` artifact." + fi } >"$out/summary.md" diff --git a/.github/workflows/mutants-diff.yml b/.github/workflows/mutants-diff.yml new file mode 100644 index 0000000..c68b28c --- /dev/null +++ b/.github/workflows/mutants-diff.yml @@ -0,0 +1,90 @@ +name: mutants-diff + +on: + pull_request: + paths: + - "src/**.rs" + - .cargo/mutants.toml + - .github/workflows/mutants-diff.yml + - .github/scripts/mutants-scope.py + - .github/scripts/mutants-summary.sh + +permissions: + contents: read + +concurrency: + group: mutants-diff-${{ github.event.pull_request.number }} + cancel-in-progress: true + +defaults: + run: + shell: bash + +env: + CARGO_TERM_COLOR: always + RUSTFLAGS: -C link-arg=-fuse-ld=mold + +jobs: + mutants: + name: mutants (diff, advisory) + runs-on: ubuntu-latest + timeout-minutes: 120 + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + fetch-depth: 2 + - name: Diff against the PR base + id: scope + run: | + git diff --no-ext-diff HEAD^1 HEAD >pr.diff + files=$(git diff --name-only --no-renames HEAD^1 HEAD | python3 .github/scripts/mutants-scope.py .cargo/mutants.toml) + if [ -z "$files" ]; then + echo "run=false" >>"$GITHUB_OUTPUT" + { + echo "## Diff mutation grading" + echo + echo "No file in \`.cargo/mutants.toml\` changed, so no mutants ran." + } >>"$GITHUB_STEP_SUMMARY" + exit 0 + fi + printf '%s\n' "$files" + echo "run=true" >>"$GITHUB_OUTPUT" + - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # stable as of 2026-10-16 + if: steps.scope.outputs.run == 'true' + with: + toolchain: stable + - name: Install mold + if: steps.scope.outputs.run == 'true' + run: sudo apt-get update && sudo apt-get install -y mold + - uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2 + if: steps.scope.outputs.run == 'true' + with: + tool: cargo-mutants@27.1.0,cargo-nextest@0.9.151 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + if: steps.scope.outputs.run == 'true' + with: + key: chaos-mutants-diff + - name: Grade the mutants in the diff + if: steps.scope.outputs.run == 'true' + run: | + set +e + cargo mutants --in-place --in-diff pr.diff --output mutants-diff + code=$? + set -e + case $code in + 0|2|3) ;; + *) echo "cargo-mutants exited $code"; exit "$code" ;; + esac + - name: Job summary + if: always() && steps.scope.outputs.run == 'true' + run: | + .github/scripts/mutants-summary.sh mutants-diff mutants-diff-summary diff + cat mutants-diff-summary/summary.md >>"$GITHUB_STEP_SUMMARY" + - uses: actions/upload-artifact@v7 + if: always() && steps.scope.outputs.run == 'true' + with: + name: mutants-diff + path: mutants-diff-summary/ + if-no-files-found: ignore + retention-days: 14 From c7bfc8745f639db8f5077c687b007f2652c3e4ec Mon Sep 17 00:00:00 2001 From: Paul Otto <219596+potto007@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:00:11 -0700 Subject: [PATCH 2/5] docs: describe the diff mutation check Signed-off-by: Paul Otto <219596+potto007@users.noreply.github.com> Github-Issue: #246 --- docs/src/contributing.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/src/contributing.md b/docs/src/contributing.md index f9e389a..3de2759 100644 --- a/docs/src/contributing.md +++ b/docs/src/contributing.md @@ -114,6 +114,28 @@ The workflow does not file issues. A later job could read the hashes from the `chaos-failures` artifact and open or comment on one tracking issue per hash, with `issues: write` granted to that job alone. +### Diff mutation check + +`.github/workflows/mutants-diff.yml` runs the same mutation grading on a pull +request, limited to the lines the PR changes. It starts only when the PR +touches a Rust file under `src/`, `.cargo/mutants.toml`, or the workflow and +its scripts, and its first step skips the rest when none of the changed files +is in `.cargo/mutants.toml`'s `examine_globs`. Otherwise it diffs the merge +commit against the base it was merged onto, runs +`cargo mutants --in-diff` with the `mutants` profile and the `pr_chaos` tests, +and writes the caught, missed, timeout and unviable counts per file to the job +summary, with each missed and timed-out mutant listed below the table. The +`mutants-diff` artifact holds the same files. + +The check is advisory. It runs in its own workflow, outside the required +`check` job in `ci.yml`, and passes when mutants survive: it fails only when +cargo-mutants itself fails, such as when the unmutated tree does not build. +Expect survivors in `src/board.rs` rendering code, which the chaos tests do not +reach. A survivor in code your PR adds is worth a look: either a `pr_chaos` +invariant should notice the change, or the line is outside what the chaos +oracle checks. It needs no secrets and runs with `contents: read`, so it works +the same on pull requests from forks. + ## This site `docs/build.sh` generates the [API reference](api.md) page from `t3cm types`, From 460ccdc97da38d17435292353ea95ba0cebaee23 Mon Sep 17 00:00:00 2001 From: Paul Otto <219596+potto007@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:00:49 -0700 Subject: [PATCH 3/5] ci: pin diff prefixes for cargo-mutants Signed-off-by: Paul Otto <219596+potto007@users.noreply.github.com> Github-Issue: #246 --- .github/workflows/mutants-diff.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mutants-diff.yml b/.github/workflows/mutants-diff.yml index c68b28c..8094b07 100644 --- a/.github/workflows/mutants-diff.yml +++ b/.github/workflows/mutants-diff.yml @@ -37,7 +37,7 @@ jobs: - name: Diff against the PR base id: scope run: | - git diff --no-ext-diff HEAD^1 HEAD >pr.diff + git diff --no-ext-diff --no-color --src-prefix=a/ --dst-prefix=b/ HEAD^1 HEAD >pr.diff files=$(git diff --name-only --no-renames HEAD^1 HEAD | python3 .github/scripts/mutants-scope.py .cargo/mutants.toml) if [ -z "$files" ]; then echo "run=false" >>"$GITHUB_OUTPUT" From 43b738c757178bb870a4575c5f88acb9f479e985 Mon Sep 17 00:00:00 2001 From: Paul Otto <219596+potto007@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:05:00 -0700 Subject: [PATCH 4/5] ci: summarize diff mutants only after grading Signed-off-by: Paul Otto <219596+potto007@users.noreply.github.com> Github-Issue: #246 --- .github/scripts/mutants-summary.sh | 2 +- .github/workflows/mutants-diff.yml | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/scripts/mutants-summary.sh b/.github/scripts/mutants-summary.sh index 4a511fa..c183cd7 100755 --- a/.github/scripts/mutants-summary.sh +++ b/.github/scripts/mutants-summary.sh @@ -7,7 +7,7 @@ mode=${3:-nightly} mkdir -p "$out" for kind in caught missed timeout unviable; do - { find "$shards_dir" -name "$kind.txt" -exec cat {} + 2>/dev/null || true; } | sort -u >"$out/$kind.txt" + find "$shards_dir" -name "$kind.txt" -exec cat {} + 2>/dev/null | sort -u >"$out/$kind.txt" done count() { grep -c . "$1" || true; } diff --git a/.github/workflows/mutants-diff.yml b/.github/workflows/mutants-diff.yml index 8094b07..d956478 100644 --- a/.github/workflows/mutants-diff.yml +++ b/.github/workflows/mutants-diff.yml @@ -66,8 +66,10 @@ jobs: with: key: chaos-mutants-diff - name: Grade the mutants in the diff + id: grade if: steps.scope.outputs.run == 'true' run: | + mkdir -p mutants-diff set +e cargo mutants --in-place --in-diff pr.diff --output mutants-diff code=$? @@ -77,12 +79,12 @@ jobs: *) echo "cargo-mutants exited $code"; exit "$code" ;; esac - name: Job summary - if: always() && steps.scope.outputs.run == 'true' + if: steps.grade.outcome == 'success' run: | .github/scripts/mutants-summary.sh mutants-diff mutants-diff-summary diff cat mutants-diff-summary/summary.md >>"$GITHUB_STEP_SUMMARY" - uses: actions/upload-artifact@v7 - if: always() && steps.scope.outputs.run == 'true' + if: steps.grade.outcome == 'success' with: name: mutants-diff path: mutants-diff-summary/ From 53ed8619497729684648573483661a40e291dfa8 Mon Sep 17 00:00:00 2001 From: Paul Otto <219596+potto007@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:09:11 -0700 Subject: [PATCH 5/5] docs: split a colon-joined sentence Signed-off-by: Paul Otto <219596+potto007@users.noreply.github.com> Github-Issue: #246 --- docs/src/contributing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/contributing.md b/docs/src/contributing.md index 3de2759..8a7d755 100644 --- a/docs/src/contributing.md +++ b/docs/src/contributing.md @@ -128,7 +128,7 @@ summary, with each missed and timed-out mutant listed below the table. The `mutants-diff` artifact holds the same files. The check is advisory. It runs in its own workflow, outside the required -`check` job in `ci.yml`, and passes when mutants survive: it fails only when +`check` job in `ci.yml`, and it passes when mutants survive. It fails only when cargo-mutants itself fails, such as when the unmutated tree does not build. Expect survivors in `src/board.rs` rendering code, which the chaos tests do not reach. A survivor in code your PR adds is worth a look: either a `pr_chaos`