From 8745b2910ccad20718d0ac43de3aac1c4227b82d Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 22:13:11 -0500 Subject: [PATCH 1/3] tools: one mutation runner with two readers [host NOT_RUN] Adds tools/mutation_runner.sh (sed-table reader and marker reader over one copy/edit/build/test engine, edit kinds sed, marker-sed, replace, equivalence class as data, c4 receipt JSON layout) and its self-test. No existing *_mutations.sh is touched; conversion comes in later PRs. Co-Authored-By: Claude Sonnet 5.5 --- tests/fixtures/mutation_runner/expected.json | 9 + tests/test_mutation_runner.sh | 130 ++++++++++++ tools/mutation_runner.sh | 198 +++++++++++++++++++ 3 files changed, 337 insertions(+) create mode 100644 tests/fixtures/mutation_runner/expected.json create mode 100755 tests/test_mutation_runner.sh create mode 100755 tools/mutation_runner.sh diff --git a/tests/fixtures/mutation_runner/expected.json b/tests/fixtures/mutation_runner/expected.json new file mode 100644 index 000000000..02b35ff57 --- /dev/null +++ b/tests/fixtures/mutation_runner/expected.json @@ -0,0 +1,9 @@ +{ + "mutants": [ + {"id": "K1", "file": "add.c", "checks": "addition is computed", "status": "KILLED", "note": "1 failing case(s), first: add"}, + {"id": "S1", "file": "add.c", "checks": "clamp upper bound", "status": "SURVIVED", "note": "harness still PASS, no justification recorded"}, + {"id": "S2", "file": "add.c", "checks": "clamp upper bound again", "status": "SURVIVED_REDUNDANT", "note": "redundant: covered by the caller, UNVERIFIED"}, + {"id": "L1", "file": "add.c", "checks": "legacy gap row", "status": "SURVIVED_GAP", "note": "gap: legacy prose with quotes and back"}, + {"id": "E1", "file": "add.c", "checks": "no-op edit", "status": "ERROR", "note": "edit did not apply"} + ] +} diff --git a/tests/test_mutation_runner.sh b/tests/test_mutation_runner.sh new file mode 100755 index 000000000..4be5b81a1 --- /dev/null +++ b/tests/test_mutation_runner.sh @@ -0,0 +1,130 @@ +#!/bin/sh +# Self-test of tools/mutation_runner.sh. Host only, CPU only, light. Builds a +# tiny C project in a temp dir, runs the runner over fake mutants and checks +# every verdict, the exit code and the JSON bytes. Each check can fail: the +# always-passing test run and the corrupted expected file prove the self-test +# notices a runner that reports the wrong thing. +set -u +root=$(cd "$(dirname "$0")/.." && pwd) +runner=$root/tools/mutation_runner.sh +expected=$root/tests/fixtures/mutation_runner/expected.json +tmp=$(mktemp -d "${TMPDIR:-/tmp}/mutrun-selftest.XXXXXX") || exit 2 +trap 'rm -rf "$tmp"' EXIT INT TERM +proj=$tmp/proj +mkdir -p "$proj" +cat > "$proj/add.c" <<'CEOF' +int add(int a, int b) { return a + b; } /* MUT:ADD_PLUS */ +int clamp(int x) { return x>100?100:x; } +int twice(int x) { return x; } +CEOF +cat > "$proj/check.c" <<'CEOF' +#include +int add(int a, int b); +int main(void) { + if (add(2, 3) != 5) { puts("FAIL add"); return 1; } + puts("PASS"); + return 0; +} +CEOF +BUILD='cc -o check check.c add.c' +TEST=./check +fails=0; checks=0 +ok() { checks=$((checks + 1)); if [ "$2" = 0 ]; then echo "ok $1"; else echo "FAIL $1"; fails=$((fails + 1)); fi; } +has() { grep -q -- "$2" "$1"; } # file pattern + +# run : sets rc, $tmp/err, $tmp/outp, $tmp/j.json +run() { + rows=$1; shift + printf '%s\n' "$rows" > "$tmp/rows" + rm -f "$tmp/j.json" + "$runner" -m "$tmp/rows" -d "$proj" -o "$tmp/j.json" "$@" > "$tmp/outp" 2> "$tmp/err" + rc=$? +} +before=$(find "$proj" -type f | sort | xargs sha256sum | sha256sum) + +# (1) a mutant that breaks the code is KILLED, exit 0 +run 'K~add.c~s/a + b/a - b/~addition' -k table -t "$TEST" -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^K KILLED'; ok "1 breaking mutant is KILLED, exit 0" $? + +# (2) a mutant the test cannot see SURVIVES, exit nonzero +run 'S~add.c~s/x>100/x>101/~clamp' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^S SURVIVED ' && ! has "$tmp/err" 'SURVIVED_'; ok "2 unseen mutant is SURVIVED, exit nonzero" $? + +# (3) same mutant marked redundant (new column form and legacy prose form) +run 'S~add.c~s/x>100/x>101/~clamp~redundant~covered elsewhere' -k table -t "$TEST" -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^S SURVIVED_REDUNDANT' && has "$tmp/j.json" '"note": "redundant: covered elsewhere"'; ok "3a redundant (class column) is SURVIVED_REDUNDANT, exit 0" $? +run 'S~add.c~s/x>100/x>101/~clamp~redundant: covered elsewhere' -k table -t "$TEST" -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^S SURVIVED_REDUNDANT' && has "$tmp/j.json" '"note": "redundant: covered elsewhere"'; ok "3b redundant (legacy prose) is SURVIVED_REDUNDANT, exit 0" $? +run 'S~add.c~s/x>100/x>101/~clamp~gap~no test reaches it' -k table -t "$TEST" -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^S SURVIVED_GAP'; ok "3c gap is SURVIVED_GAP, exit 0" $? +run 'S~add.c~s/x>100/x>101/~clamp~none~' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^S SURVIVED '; ok "3d class none is a plain SURVIVED, exit nonzero" $? + +# (4) a no-op edit is ERROR +run 'N~add.c~s/NO_SUCH_TEXT/y/~noop' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^N ERROR' && has "$tmp/j.json" '"note": "edit did not apply"'; ok "4a unmatched sed is ERROR edit did not apply" $? +run 'N~add.c~R:return a + b;@>@return a + b;~noop replace' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^N ERROR' && has "$tmp/j.json" '"note": "edit did not apply"'; ok "4b replace with identical text is ERROR" $? +run 'N~missing.c~s/a/b/~no file' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^N ERROR'; ok "4c missing file is ERROR" $? +run 'B~add.c~s/int add/int add_/~breaks the build' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^B ERROR' && has "$tmp/j.json" 'build failed'; ok "4d build failure is ERROR" $? + +# (5) replace edit: exactly once works, twice is ERROR, @NL@ is a newline +run 'R1~add.c~R:return a + b;@>@return a - b;~replace once' -k table -t "$TEST" -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^R1 KILLED'; ok "5a replace matching once is applied (KILLED)" $? +run 'R2~add.c~R:return@>@return 0 +~replace twice' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^R2 ERROR' && has "$tmp/j.json" 'not found exactly once'; ok "5b replace matching twice is ERROR" $? +run 'R3~add.c~R:return a + b;@>@return a - b;@NL@int zzz(void) { return 1; }~newline escape' -k table -t "$TEST" -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^R3 KILLED'; ok "5c replace with @NL@ builds and is KILLED" $? + +# (6) marker reader +run 'ADD_PLUS|add.c|s/a + b/a * b/' -k marker -t "$TEST" -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^ADD_PLUS KILLED'; ok "6a marker row is KILLED" $? +run 'NO_MARKER|add.c|s/a + b/a * b/' -k marker -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^NO_MARKER ERROR'; ok "6b marker not on any line is ERROR (edit does not apply)" $? +run 'ADD_PLUS|add.c|s/a + b/a * b/' -k marker -t "$TEST" -b "$BUILD" -B +[ $rc -eq 0 ]; ok "6c marker baseline passing is accepted" $? +run 'ADD_PLUS|add.c|s/a + b/a * b/' -k marker -t 'exit 1' -b "$BUILD" -B +[ $rc -eq 1 ] && has "$tmp/outp" 'baseline does not pass'; ok "6d failing baseline FAILS the sweep" $? + +# (7) sed-table row with two files and @@ separated expressions +cp "$proj/add.c" "$proj/other.c" +run 'T~add.c,other.c~s/a + b/a - b/@@s/return x;/return x + 1;/~two files' -k table -t "$TEST" -b "$BUILD" -f FAIL +[ $rc -eq 0 ] && has "$tmp/err" '^T KILLED' && has "$tmp/j.json" '"file": "add.c,other.c"' && has "$tmp/j.json" 'first: add'; ok "7 two-file sed row is applied per file and KILLED" $? +rm -f "$proj/other.c" + +# (8) JSON byte-compare against the committed expected receipt +cat > "$tmp/rows8" <<'REOF' +K1~add.c~s/a + b/a - b/~addition is computed +S1~add.c~s/x>100/x>101/~clamp upper bound +S2~add.c~s/x>100/x>101/~clamp upper bound again~redundant~covered by the caller, UNVERIFIED +L1~add.c~s/x>100/x>101/~legacy gap row~gap: legacy prose with "quotes" and \back +E1~add.c~s/NO_SUCH_TEXT/y/~no-op edit +REOF +"$runner" -k table -m "$tmp/rows8" -d "$proj" -o "$tmp/j8.json" -t "$TEST" -b "$BUILD" -f FAIL >/dev/null 2>"$tmp/err8" +rc8=$? +[ $rc8 -ne 0 ]; ok "8a sweep with a plain SURVIVED and an ERROR exits nonzero" $? +cmp -s "$tmp/j8.json" "$expected"; ok "8b JSON is byte-identical to tests/fixtures/mutation_runner/expected.json" $? +sed 's/KILLED/SURVIVED/' "$expected" > "$tmp/bad.json" +! cmp -s "$tmp/j8.json" "$tmp/bad.json"; ok "8c the byte compare notices a wrong receipt" $? +ONLY='K1 S2' "$runner" -k table -m "$tmp/rows8" -d "$proj" -o "$tmp/j9.json" -t "$TEST" -b "$BUILD" >/dev/null 2>&1 +[ "$(grep -c '"id"' "$tmp/j9.json")" -eq 2 ]; ok "8d ONLY restricts the sweep" $? + +# (9) deliberately wrong run: a test that always passes must make the breaking +# mutant SURVIVE, never KILLED, and the sweep must fail. +run 'K~add.c~s/a + b/a - b/~addition' -k table -t true -b "$BUILD" +[ $rc -ne 0 ] && has "$tmp/err" '^K SURVIVED' && ! has "$tmp/err" '^K KILLED'; ok "9a always-passing test reports SURVIVED, not KILLED" $? +run 'K~add.c~s/a + b/a - b/~addition' -k table -t false -b "$BUILD" +[ $rc -eq 0 ] && has "$tmp/err" '^K KILLED'; ok "9b always-failing test reports KILLED (the runner trusts the exit code)" $? +run '' -k table -t "$TEST" -b "$BUILD" +[ $rc -ne 0 ]; ok "9c an empty sweep fails instead of passing vacuously" $? + +# (10) the source tree is never edited +after=$(find "$proj" -type f | sort | xargs sha256sum | sha256sum) +[ "$before" = "$after" ]; ok "10 source tree unchanged (no build artifacts, no edits)" $? + +"$runner" >/dev/null 2>&1; [ $? -eq 2 ]; ok "11 missing arguments exit 2" $? + +if [ $fails -ne 0 ]; then echo "mutation runner self-test: FAIL ($fails of $checks)"; exit 1; fi +echo "mutation runner self-test: PASS ($checks checks)" diff --git a/tools/mutation_runner.sh b/tools/mutation_runner.sh new file mode 100755 index 000000000..df95621a1 --- /dev/null +++ b/tools/mutation_runner.sh @@ -0,0 +1,198 @@ +#!/bin/sh +# One mutation runner (POSIX sh, no Python). Deep module, small interface. +# +# A MUTATION is one record: +# { name, file(s), edit, test command, what the check guards, equivalence } +# The runner copies the source tree to a scratch directory (the source tree is +# never edited), applies the edit there, checks that the files really changed +# (otherwise ERROR "edit did not apply"), runs the optional build command +# (failure => ERROR), then runs the test command inside the scratch tree: +# exit nonzero => KILLED exit 0 => SURVIVED +# EQUIVALENCE is a data column: class none|redundant|gap plus rationale text. +# A SURVIVED mutant with class redundant or gap is reported SURVIVED_REDUNDANT +# or SURVIVED_GAP and the JSON note is ": " (the same note +# tools/c4_requal_mutants.sh writes today). Class none (or no class) leaves it +# SURVIVED, which fails the sweep. +# +# EDIT KINDS +# sed files comma-separated, expressions separated by @@ (one per +# file, in order), as tools/c4_requal_mutants.sh does today. +# marker-sed one file, one sed expression applied only to lines containing +# MUT:, as tensor/autodiff/m20 mutation sweeps do today. +# replace edit text "R:@>@[@@@>@...]": must be +# found exactly once in its file (else ERROR), @NL@ in is a +# newline, as numeric_oracle_mutations.sh does today. +# +# READERS (two; the older sweeps become adapters over them) +# table rows id~files~edit~guards[~class~rationale] (kinds sed, replace) +# Legacy rows are accepted: 4 columns (no equivalence), or 5 columns +# whose last column is prose starting "redundant:" or "gap:". +# Lines that are empty or start with # are skipped. +# marker rows name|file|sed-expr (kind marker-sed; the expression may +# contain |). Marker rows have no equivalence column: a survivor is +# always a failure. +# +# USAGE +# tools/mutation_runner.sh -k table|marker -m ROWS|- -t TESTCMD +# [-d SRCDIR] [-b BUILDCMD] [-c "PATH ..."] [-o out.json] [-f PREFIX] [-B] +# -k reader kind. -m rows file, or - for stdin. -t test command (sh -c, run +# in the scratch tree). -d source tree (default .). -b build command run in +# the scratch tree before the test. -c paths (relative to SRCDIR) to copy +# (default: whole tree). -o JSON receipt path. -f line prefix that marks a +# failing case in test output, used only for the KILLED note. -B run build +# and test on the unmutated tree first; if that fails the sweep FAILS. +# Env ONLY="id id" restricts the sweep to those ids. +# +# OUTPUT +# stderr: one line per mutant " ()", STATUS is +# KILLED, SURVIVED, SURVIVED_REDUNDANT, SURVIVED_GAP or ERROR. +# stdout: one summary line. +# JSON (-o), exactly the c4 receipt layout, same whitespace, key order and +# escaping (note has double quotes and backslashes removed with tr -d): +# {"mutants":[{"id","file","checks","status","note"}, ...]} +# Exit 0 only if at least one mutant ran and every mutant is KILLED or +# justified-survived; 2 on bad usage. +set -u + +kind=; rows=; test_cmd=; src=.; build_cmd=; copy=.; out=; fprefix=; baseline=0 +while getopts k:m:t:d:b:c:o:f:B opt; do + case $opt in + k) kind=$OPTARG;; m) rows=$OPTARG;; t) test_cmd=$OPTARG;; d) src=$OPTARG;; + b) build_cmd=$OPTARG;; c) copy=$OPTARG;; o) out=$OPTARG;; f) fprefix=$OPTARG;; + B) baseline=1;; *) kind=;; + esac +done +case $kind in table|marker) ;; *) kind=;; esac +if [ -z "$kind" ] || [ -z "$rows" ] || [ -z "$test_cmd" ]; then + echo "usage: mutation_runner.sh -k table|marker -m ROWS|- -t TESTCMD [-d SRCDIR] [-b BUILDCMD] [-c PATHS] [-o out.json] [-f PREFIX] [-B]" >&2 + exit 2 +fi +src=$(cd "$src" 2>/dev/null && pwd) || { echo "mutation_runner: no such source dir" >&2; exit 2; } +work=$(mktemp -d "${TMPDIR:-/tmp}/mutrun.XXXXXX") || exit 2 +trap 'rm -rf "$work"' EXIT INT TERM +if [ "$rows" = - ]; then cat > "$work/rows"; rows=$work/rows; fi +[ -r "$rows" ] || { echo "mutation_runner: cannot read rows" >&2; exit 2; } +[ -n "$out" ] || out=$work/out.json + +# Fresh scratch copy of the tree in $1. +copy_tree() { + mkdir -p "$1" + for p in $copy; do + if [ "$p" = . ]; then cp -R "$src/." "$1/" + else + mkdir -p "$1/$(dirname "$p")" + cp -R "$src/$p" "$1/$p" 2>/dev/null + fi + done +} + +if [ $baseline -eq 1 ]; then + copy_tree "$work/base" + if { [ -z "$build_cmd" ] || (cd "$work/base" && sh -c "$build_cmd") "$work/base.log" 2>&1; } \ + && (cd "$work/base" && sh -c "$test_cmd") >"$work/base.log" 2>&1; then : + else + head -5 "$work/base.log" >&2 + echo "mutation runner: FAIL (unmutated baseline does not pass)" + exit 1 + fi + rm -rf "$work/base" +fi + +# Edit step. Sets $note on failure and returns nonzero. +apply_edit() { # dir files ekind edit id + a_dir=$1; a_files=$(echo "$2" | tr ',' ' '); a_kind=$3; a_edit=$4; a_id=$5 + for f in $a_files; do + [ -f "$a_dir/$f" ] || { note="edit did not apply (no such file $f)"; return 1; } + done + a_before=$(for f in $a_files; do sha256sum "$a_dir/$f"; done | sha256sum) + i=1 + for f in $a_files; do + e=$(printf '%s\n' "$a_edit" | awk -F'@@' -v n=$i '{print $n}') + case $a_kind in + sed) sed -i "$e" "$a_dir/$f" ;; + marker-sed) sed -i "/MUT:$a_id/ $e" "$a_dir/$f" ;; + replace) + e=${e#R:} + FROM=${e%%@>@*} TO=${e#*@>@} perl -0pi -e '$n += s/\Q$ENV{FROM}\E/$ENV{TO} =~ s{\@NL\@}{\n}gr/ge; END { $? = $n == 1 ? 0 : 3 }' "$a_dir/$f" \ + || { note="replace text not found exactly once in $f"; return 1; } + ;; + esac + i=$((i + 1)) + done + a_after=$(for f in $a_files; do sha256sum "$a_dir/$f"; done | sha256sum) + [ "$a_before" != "$a_after" ] || { note="edit did not apply"; return 1; } +} + +n=0 k=0 r=0 g=0 +first=1 +printf '{\n "mutants": [\n' > "$out" + +# run_one id files ekind edit guards justification (justification is the +# prose "redundant: ..." / "gap: ..." or empty) +run_one() { + id=$1; files=$2; ekind=$3; edit=$4; what=$5; just=$6 + if [ -n "${ONLY:-}" ]; then case " $ONLY " in *" $id "*) ;; *) return 0;; esac; fi + dir=$work/m.$id + copy_tree "$dir" + note= + if ! apply_edit "$dir" "$files" "$ekind" "$edit" "$id"; then status=ERROR + else + log=$work/$id.log + if [ -n "$build_cmd" ] && ! (cd "$dir" && sh -c "$build_cmd") "$log" 2>&1; then + status=ERROR; note="build failed: $(grep -m1 -E "error:|Error " "$log" | cut -c1-120)" + else + (cd "$dir" && sh -c "$test_cmd") "$log.run" 2>&1 + rc=$? + nf=0 + [ -z "$fprefix" ] || nf=$(grep -c "^$fprefix" "$log.run") + if [ $rc -ne 0 ] && [ "$nf" -gt 0 ]; then + cut_from=$((${#fprefix} + 2)) + status=KILLED; note="$nf failing case(s), first: $(grep -m1 "^$fprefix" "$log.run" | cut -c"$cut_from"-$((cut_from + 64)))" + elif [ $rc -ne 0 ]; then status=KILLED; note="harness exit $rc (crash or abort)" + else + case "$just" in + redundant:*) status=SURVIVED_REDUNDANT; note="$just" ;; + gap:*) status=SURVIVED_GAP; note="$just" ;; + *) status=SURVIVED; note="harness still PASS, no justification recorded" ;; + esac + fi + fi + fi + echo "$id $status $what ($note)" >&2 + [ $first -eq 1 ] || printf ',\n' >> "$out" + first=0 + printf ' {"id": "%s", "file": "%s", "checks": "%s", "status": "%s", "note": "%s"}' "$id" "$files" "$what" "$status" "$(echo "$note" | tr -d '"\\')" >> "$out" + n=$((n + 1)) + case $status in KILLED) k=$((k + 1));; SURVIVED_REDUNDANT) r=$((r + 1));; SURVIVED_GAP) g=$((g + 1));; esac + rm -rf "$dir" +} + +# Reader 1: id~files~edit~guards[~class~rationale] +read_table() { + while IFS='~' read -r t_id t_files t_edit t_guards t_c5 t_c6; do + case $t_id in ''|'#'*) continue;; esac + t_kind=sed + case $t_edit in R:*) t_kind=replace;; esac + case $t_c5 in + none|redundant|gap) + if [ "$t_c5" = none ]; then t_just=; else t_just="$t_c5: $t_c6"; fi ;; + *) t_just=$t_c5; [ -z "$t_c6" ] || t_just="$t_c5~$t_c6" ;; + esac + run_one "$t_id" "$t_files" "$t_kind" "$t_edit" "$t_guards" "$t_just" + done < "$rows" +} + +# Reader 2: name|file|sed-expr (expr may contain |) +read_marker() { + while IFS='|' read -r m_name m_file m_expr; do + case $m_name in ''|'#'*) continue;; esac + run_one "$m_name" "$m_file" marker-sed "$m_expr" "MUT:$m_name" "" + done < "$rows" +} + +read_$kind + +printf '\n ]\n}\n' >> "$out" +echo "mutants: $n total, $k killed, $r survived as redundant, $g survived as test gaps (receipt $out)" +# Fail on any unjustified survivor or build/edit error, and on an empty sweep. +[ "$n" -gt 0 ] && [ $((k + r + g)) -eq "$n" ] From 7c0702c4123447eaad8c399fb658c291239a9d8b Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 22:15:28 -0500 Subject: [PATCH 2/3] mutation runner: note escaping via printf so a backslash-letter is not read as an echo escape The hand-written fixture was right: a note containing backslash-b came out with a backspace byte because echo in dash interprets escapes. printf '%s' does not. No real c4 row has a backslash in guards or rationale. Co-Authored-By: Claude Sonnet 5.5 --- tools/mutation_runner.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/mutation_runner.sh b/tools/mutation_runner.sh index df95621a1..b7fb5ca0d 100755 --- a/tools/mutation_runner.sh +++ b/tools/mutation_runner.sh @@ -161,7 +161,7 @@ run_one() { echo "$id $status $what ($note)" >&2 [ $first -eq 1 ] || printf ',\n' >> "$out" first=0 - printf ' {"id": "%s", "file": "%s", "checks": "%s", "status": "%s", "note": "%s"}' "$id" "$files" "$what" "$status" "$(echo "$note" | tr -d '"\\')" >> "$out" + printf ' {"id": "%s", "file": "%s", "checks": "%s", "status": "%s", "note": "%s"}' "$id" "$files" "$what" "$status" "$(printf '%s\n' "$note" | tr -d '"\\')" >> "$out" n=$((n + 1)) case $status in KILLED) k=$((k + 1));; SURVIVED_REDUNDANT) r=$((r + 1));; SURVIVED_GAP) g=$((g + 1));; esac rm -rf "$dir" From c4768c2dfcf3d5ae04a88a731c6c56d3cdb1fbe1 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 22:15:02 -0500 Subject: [PATCH 3/3] tools: m20 receipt mutation sweep runs on the shared runner [host NOT_RUN] Co-Authored-By: Claude Sonnet 5.5 --- ..._m20_receipt_mutations_conversion_equiv.sh | 64 ++++++++++++++++ tools/m20_receipt_mutations.sh | 74 +++++++++++-------- 2 files changed, 108 insertions(+), 30 deletions(-) create mode 100755 tests/test_m20_receipt_mutations_conversion_equiv.sh diff --git a/tests/test_m20_receipt_mutations_conversion_equiv.sh b/tests/test_m20_receipt_mutations_conversion_equiv.sh new file mode 100755 index 000000000..a722b3891 --- /dev/null +++ b/tests/test_m20_receipt_mutations_conversion_equiv.sh @@ -0,0 +1,64 @@ +#!/bin/sh +# Proof that tools/m20_receipt_mutations.sh (now a thin adapter over +# tools/mutation_runner.sh) behaves exactly like the pre-conversion script +# from pinned commit 2dc9dd8: same stdout bytes, same exit code, same +# per-mutant verdicts. Host only, CPU only, light. Run from anywhere. +# +# The old script is extracted into tools/ (it does cd dirname/..) under a +# temporary name and removed by a trap. Both sweeps run on the same tree. +# Nothing is normalised: the sweep prints no temp paths or timings, so the +# stdout is compared byte for byte. This sweep writes no JSON. +# A negative check proves the comparison can fail. +set -u +root=$(cd "$(dirname "$0")/.." && pwd) +cd "$root" || exit 2 +PIN=2dc9dd8 +tmp=$(mktemp -d "${TMPDIR:-/tmp}/m20-equiv.XXXXXX") || exit 2 +old=tools/.m20_receipt_mutations_old.$$.sh +trap 'rm -rf "$tmp" "$root/$old"' EXIT INT TERM + +git show "$PIN:tools/m20_receipt_mutations.sh" > "$old" || { echo "FAIL cannot extract old script from $PIN"; exit 2; } +chmod +x "$old" + +sh "$old" > "$tmp/old.out" 2>&1; echo $? > "$tmp/old.rc" +sh tools/m20_receipt_mutations.sh > "$tmp/new.out" 2>&1; echo $? > "$tmp/new.rc" + +# verdict list: " KILLED|SURVIVED|ERROR" from the MUTATION lines +verdicts() { + sed -n 's/^MUTATION \([A-Z0-9_]*\): caught.*/\1 KILLED/p; + s/^MUTATION \([A-Z0-9_]*\): NOT CAUGHT.*/\1 SURVIVED/p; + s/^MUTATION \([A-Z0-9_]*\): NOT APPLIED.*/\1 ERROR/p' "$1" +} +# compare OLDOUT OLDRC NEWOUT NEWRC: returns 0 only if identical +compare() { + cmp -s "$1" "$3" || return 1 + [ "$(cat "$2")" = "$(cat "$4")" ] || return 1 + [ "$(verdicts "$1")" = "$(verdicts "$3")" ] || return 1 + return 0 +} + +fail=0 +n=$(verdicts "$tmp/old.out" | wc -l) +if [ "$n" -lt 1 ]; then echo "FAIL old sweep produced no mutant verdicts"; fail=1; fi +if compare "$tmp/old.out" "$tmp/old.rc" "$tmp/new.out" "$tmp/new.rc"; then + echo "PASS old and new sweeps identical (stdout, exit code $(cat "$tmp/new.rc"), $n mutants compared)" +else + echo "FAIL old and new sweeps differ"; diff "$tmp/old.out" "$tmp/new.out" | head -10; fail=1 +fi + +# Negative check: one verdict line altered must be reported different. +sed '0,/: caught/s//: NOT CAUGHT/' "$tmp/new.out" > "$tmp/bad.out" +if compare "$tmp/old.out" "$tmp/old.rc" "$tmp/bad.out" "$tmp/new.rc"; then + echo "FAIL negative check: altered verdict was not detected"; fail=1 +else + echo "PASS negative check: altered verdict line is reported different" +fi +# Negative check 2: a different exit code must be reported different. +echo 1 > "$tmp/bad.rc" +if compare "$tmp/old.out" "$tmp/old.rc" "$tmp/new.out" "$tmp/bad.rc" && [ "$(cat "$tmp/old.rc")" != 1 ]; then + echo "FAIL negative check: altered exit code was not detected"; fail=1 +else + echo "PASS negative check: altered exit code is reported different" +fi +[ $fail -eq 0 ] && echo "m20 conversion equivalence: PASS" || echo "m20 conversion equivalence: FAIL" +exit $fail diff --git a/tools/m20_receipt_mutations.sh b/tools/m20_receipt_mutations.sh index 67ec48cc4..46a8dbe77 100755 --- a/tools/m20_receipt_mutations.sh +++ b/tools/m20_receipt_mutations.sh @@ -5,23 +5,33 @@ # rule, run tests/test_m20_receipt.sh against the copy and require it to FAIL. # A mutation that does not apply, or that the tests do not catch, fails the # sweep. Host only, no device. Shell only (no Python). +# +# Thin adapter over tools/mutation_runner.sh (marker reader). The rows below +# are the mutant set; the runner copies the tree, applies each edit and runs +# the test. The sed address stays /MUT:$/ (end of line): the runner adds +# its own unanchored /MUT:/ address, and each row nests the anchored one +# inside a { } block so only the line ENDING in the marker is edited. This +# script maps the runner's per-mutant stderr lines back to the legacy stdout +# lines and keeps the legacy exit codes. set -u cd "$(dirname "$0")/.." || exit 2 SCRATCH=$(mktemp -d "${TMPDIR:-/tmp}/m20-receipt-mut.XXXXXX") || exit 2 trap 'rm -rf "$SCRATCH"' EXIT INT TERM -# name@sed expression (applied only to the line ending in MUT:) -MUTATIONS='DIRTY_REFUSED@s/|| refuse/|| :/ -EVID_OUTSIDE@s/) refuse/) :/ -UNKNOWN_LABEL@s/refuse "row/: "row/ -NOTRUN_FORCES_NQ@s/add_nq/:/ -GB10_NEEDS_EVIDENCE@s/|| refuse/|| :/ -GB10_HASH_MATCH@s/|| refuse/|| :/ -HOST_LOG_PASS@s/|| refuse/|| :/ -GB10_PARITY_REQUIRED@s/|| add_nq/|| :/ -CLEAN_AFTER@s/|| add_nq/|| :/ -COMMIT_UNCHANGED@s/|| add_nq/|| :/ -NO_OVERWRITE@s/set -o noclobber; //' +# Rows: name|file|sed expression (anchored to the end-of-line marker). +cat > "$SCRATCH/rows" <<'ROWS' +DIRTY_REFUSED|tools/m20_receipt.sh|{/MUT:DIRTY_REFUSED$/ s/|| refuse/|| :/} +EVID_OUTSIDE|tools/m20_receipt.sh|{/MUT:EVID_OUTSIDE$/ s/) refuse/) :/} +UNKNOWN_LABEL|tools/m20_receipt.sh|{/MUT:UNKNOWN_LABEL$/ s/refuse "row/: "row/} +NOTRUN_FORCES_NQ|tools/m20_receipt.sh|{/MUT:NOTRUN_FORCES_NQ$/ s/add_nq/:/} +GB10_NEEDS_EVIDENCE|tools/m20_receipt.sh|{/MUT:GB10_NEEDS_EVIDENCE$/ s/|| refuse/|| :/} +GB10_HASH_MATCH|tools/m20_receipt.sh|{/MUT:GB10_HASH_MATCH$/ s/|| refuse/|| :/} +HOST_LOG_PASS|tools/m20_receipt.sh|{/MUT:HOST_LOG_PASS$/ s/|| refuse/|| :/} +GB10_PARITY_REQUIRED|tools/m20_receipt.sh|{/MUT:GB10_PARITY_REQUIRED$/ s/|| add_nq/|| :/} +CLEAN_AFTER|tools/m20_receipt.sh|{/MUT:CLEAN_AFTER$/ s/|| add_nq/|| :/} +COMMIT_UNCHANGED|tools/m20_receipt.sh|{/MUT:COMMIT_UNCHANGED$/ s/|| add_nq/|| :/} +NO_OVERWRITE|tools/m20_receipt.sh|{/MUT:NO_OVERWRITE$/ s/set -o noclobber; //} +ROWS # Baseline: the unmutated writer must pass, otherwise every mutant would # look "caught". @@ -29,26 +39,30 @@ if ! tests/test_m20_receipt.sh > "$SCRATCH/base.log" 2>&1; then grep '^FAIL' "$SCRATCH/base.log" | head -5 echo "m20 receipt mutation sweep: FAIL (unmutated baseline does not pass)"; exit 1 fi + +# The test command runs inside the runner's scratch tree and points the test +# at that tree's (mutated) writer through M20_RECEIPT_WRITER. +tools/mutation_runner.sh -k marker -m "$SCRATCH/rows" \ + -t 'M20_RECEIPT_WRITER=$PWD/tools/m20_receipt.sh tests/test_m20_receipt.sh' \ + -c "tools/m20_receipt.sh tests/test_m20_receipt.sh tests/fixtures" \ + -f FAIL -o "$SCRATCH/out.json" > "$SCRATCH/runner.out" 2> "$SCRATCH/runner.err" +rrc=$? + fail=0 total=0 -old_ifs=$IFS -IFS=' -' -for m in $MUTATIONS; do - IFS=$old_ifs - name=${m%%@*}; expr=${m#*@} +while IFS= read -r line; do + name=${line%% *}; rest=${line#* }; status=${rest%% *} total=$((total + 1)) - cp tools/m20_receipt.sh "$SCRATCH/m20_receipt.sh" - sed -i "/MUT:$name\$/ $expr" "$SCRATCH/m20_receipt.sh" - if cmp -s tools/m20_receipt.sh "$SCRATCH/m20_receipt.sh"; then - echo "MUTATION $name: NOT APPLIED (marker or pattern missing)"; fail=1; continue - fi - if M20_RECEIPT_WRITER="$SCRATCH/m20_receipt.sh" tests/test_m20_receipt.sh > "$SCRATCH/run.log" 2>&1; then - echo "MUTATION $name: NOT CAUGHT (tests still pass)"; fail=1 - else - echo "MUTATION $name: caught ($(grep -c '^FAIL' "$SCRATCH/run.log") failing checks)" - fi -done -IFS=$old_ifs + case $status in + KILLED) + n=$(echo "$line" | sed -n 's/.*(\([0-9][0-9]*\) failing case(s).*/\1/p'); : "${n:=0}" + echo "MUTATION $name: caught ($n failing checks)" ;; + ERROR) echo "MUTATION $name: NOT APPLIED (marker or pattern missing)"; fail=1 ;; + *) echo "MUTATION $name: NOT CAUGHT (tests still pass)"; fail=1 ;; + esac +done < "$SCRATCH/runner.err" +# Runner usage error or a row it never reported must not pass silently. +[ "$total" -eq "$(grep -c '|' "$SCRATCH/rows")" ] || fail=1 +[ "$rrc" -eq 0 ] || fail=1 if [ "$fail" -ne 0 ]; then echo "m20 receipt mutation sweep: FAIL"; exit 1; fi echo "m20 receipt mutation sweep: PASS ($total of $total mutations caught)"