#!/usr/bin/env bash # Hand-mutant kill table: run a list of mutants against a revision on # Yggdrasil, in ONE verify job, and get one table back. # # tools/mutation-table.sh [workdir] # workdir directory inside the repo where the commands run (e.g. # packages/cultcache-rs; CultLib has no Cargo.toml at its root). # env TEST default test command for mutants with no `test:` line # BUILD optional compile-only command, run on the UNMUTATED # tree first (a red or missing build aborts like # BASELINE RED); when it fails on a mutant the mutant is # UNVIABLE. Without it a mutant that does not compile # has no failing test and reads ERROR, never CAUGHT. # MUT_TIMEOUT seconds per build or test run (default 600) # TIMEOUT seconds for the WHOLE job (ygg-verify's own cap; # default 3600 here, stated in the START line). Size it # to mutants x (build + test) plus the baselines. # everything else ygg-verify.sh reads (SUMMARY=, MEM, ...) # Typical, from an agent: launch as ONE background shell with # SUMMARY=/mut1.log TEST='cargo test' tools/mutation-table.sh ... # then `tools/stopgap/ygg-verify.sh wait /mut1.log` in the foreground, # with a Bash tool timeout of at least 280000 ms. # # Mutants file (plain text; the text before the first `===` is ignored): # === label-unique-in-the-file (any text; `|` is allowed) # file: src/lib.rs (relative to workdir) # test: cargo test add_ (optional; else $TEST) # --- before # a + b (exact text, may span lines; must occur ONCE; # LF text also matches a CRLF file, a file # with mixed endings may read NOAPPLY) # --- after # a - b # # Each mutant is applied in the container's throwaway clone. After every mutant # the WHOLE tree is reset to HEAD and any untracked entry the run created is # removed (entries that existed after the baselines, such as target/, stay); if # the tree cannot be restored the job stops. A run that changed other files says # so in its row. One row prints as each mutant finishes, so a job timeout keeps # the finished rows. Row (a `YGG-SUMMARY/$YGG_NONCE: ` line, plain `YGG-SUMMARY: ` when no nonce is set): [i/N] label | outcome | killing test or detail. The # outcome is stored as its own field and tallied from fields, never read back # from the rendered row. Outcomes: # CAUGHT the test command failed AND a failing test name was parsed from it # SURVIVED the test command passed with the mutation applied # UNVIABLE BUILD failed, or a test file failed to load (vitest `FAIL # [ ]` with no test name): nothing ran, not a kill # TIMEOUT a run hit MUT_TIMEOUT (not a kill) # NOAPPLY before text absent, ambiguous, or equal to after (no mutation made) # ERROR the test command failed but no failing test was parsed (a build # error without BUILD, a crash): not a kill # Before any mutant, every distinct test command and BUILD run UNMUTATED. If one # fails or a test command runs zero tests the job aborts with `BASELINE RED` or # `BASELINE RAN ZERO TESTS`, because a red or empty baseline would make every # mutant look killed or every one survive. # Exit 0 means every mutant was CAUGHT or UNVIABLE and nothing else happened. # # DELETION LINE: this runner replaces the per-agent runmut/mutrun/strykrun # scripts. It dies with tools/stopgap/ygg-verify.sh when `idunn verify` ships a # typed mutation step. It reads tools/testparse.sh for failure names, the same # parser the summary uses. set -euo pipefail if [ "${1:-}" != --in-container ]; then here=$(cd "$(dirname "$0")" && pwd) repo=${1:?usage: mutation-table.sh [workdir]} rev=${2:?revision}; image=${3:?image}; mfile=${4:?mutants file}; workdir=${5:-.} [ -r "$mfile" ] || { echo "mutation-table: cannot read $mfile" >&2; exit 2; } stage=$(mktemp -d) trap 'rm -rf "$stage"' EXIT cp "$here/testparse.sh" "$here/mutation-table.sh" "$stage/" tr -d '\r' <"$mfile" >"$stage/mutants.txt" bundle=$(tar -C "$stage" -cz testparse.sh mutation-table.sh mutants.txt | base64 | tr -d '\n') cmd=$(printf 'mkdir -p /tmp/mt && echo %s | base64 -d | tar -xz -C /tmp/mt && WORKDIR=%q BUILD=%q TEST=%q MUT_TIMEOUT=%q JOB_TIMEOUT=%q bash /tmp/mt/mutation-table.sh --in-container' \ "$bundle" "$workdir" "${BUILD:-}" "${TEST:-}" "${MUT_TIMEOUT:-600}" "${TIMEOUT:-3600}") TIMEOUT=${TIMEOUT:-3600} "$here/stopgap/ygg-verify.sh" "$repo" "$rev" "$image" "$cmd" exit $? fi # ---- container side --------------------------------------------------------- set +e SRC=${MT_SRC:-/src}; MT_DIR=${MT_DIR:-/tmp/mt} # overridable only so tools/test-tools.sh can run this half locally against a mock # ygg-verify.sh mints a nonce per run and carries only lines tagged with it; unset (a local run), the plain tag is used SUMTAG="YGG-SUMMARY${YGG_NONCE:+/$YGG_NONCE}" # shellcheck source=testparse.sh . "$MT_DIR/testparse.sh" cd "$SRC/${WORKDIR:-.}" || { echo "$SUMTAG: BAD WORKDIR ${WORKDIR:-.}"; exit 2; } M="$MT_DIR/m"; rm -rf "$M"; mkdir -p "$M" tmo=${MUT_TIMEOUT:-600} if ! perl -e ' my ($mf, $out) = @ARGV; my ($n, $sec, %c, %seen) = (0, ""); open my $f, "<", $mf or die "cannot open $mf\n"; sub flush { return unless exists $c{label}; for my $k (qw(file before after)) { die "mutant $c{label}: no $k\n" unless defined $c{$k} } my $d = "$out/$n"; mkdir $d; for my $k (qw(label file test before after)) { my $v = join("\n", ref $c{$k} ? @{$c{$k}} : ($c{$k} // "")); open my $o, ">:raw", "$d/$k" or die; print $o $v; close $o; } } while (<$f>) { chomp; if (/^=== (.+?)\s*$/) { flush(); die "duplicate label $1\n" if $seen{$1}++; %c = (label => $1); $n++; $sec = ""; next } next unless exists $c{label}; if ($sec eq "" && /^file: (.+?)\s*$/) { $c{file} = $1; next } if ($sec eq "" && /^test: (.+?)\s*$/) { $c{test} = $1; next } if (/^--- (before|after)\s*$/) { $sec = $1; $c{$sec} = []; next } push @{$c{$sec}}, $_ if $sec; } flush(); die "no mutants in $mf\n" unless $n; print "$n\n";' "$MT_DIR/mutants.txt" "$M" >"$M/count" 2>"$M/err"; then echo "$SUMTAG: BAD MUTANT FILE: $(cat "$M/err")"; exit 2 fi count=$(cat "$M/count") echo "$SUMTAG: START $count mutants; per-run timeout ${tmo}s; whole-job timeout ${JOB_TIMEOUT:-unknown}s; one row per mutant follows as it finishes, so a timeout keeps the finished rows" # run : prints the exit status; 124 means timeout run() { timeout "$tmo" bash -o pipefail -c "$1" >"$2" 2>&1; echo $?; } # The tree is the whole repository, not only the mutated file. A test or build # that rewrites a tracked file would otherwise carry its pollution into the next # mutant (a false CAUGHT, 2026-10-01). After every run, tracked state is reset # to HEAD and any untracked entry that was not there after the baselines is # removed. Entries present after the baselines (build output such as target/) # are the baseline and stay, so builds stay incremental. tree_state() { git -C "$SRC" status --porcelain 2>/dev/null; } base_state='' # restore: leaves the tree equal to HEAD plus the baseline entries; non-zero if it cannot. restore() { git -C "$SRC" reset -q --hard 2>/dev/null tree_state | while IFS= read -r line; do grep -qxF -- "$line" <<<"$base_state" || rm -rf -- "$SRC/${line:3}" done [ "$(tree_state)" = "$base_state" ] } # dirtied : what a run changed beyond the mutated file and the baseline dirtied() { local rel; rel=$(git ls-files --full-name -- "$1" 2>/dev/null | head -n 1) [ -n "$rel" ] || return 0 # cannot name the mutated file: report nothing rather than everything tree_state | grep -vxF -- "$base_state" | grep -vF -- " $rel" | sed "s/^...//" | head -n 3 | paste -sd, - } declare -A based for n in $(seq 1 "$count"); do t=$(cat "$M/$n/test"); t=${t:-$TEST} [ -n "$t" ] || { echo "$SUMTAG: mutant $(cat "$M/$n/label") has no test: line and TEST is unset"; exit 2; } [ -z "${based[$t]:-}" ] || continue based[$t]=1 rc=$(run "$t" "$M/base.log") scan=$(tp_scan "$M/base.log") tests=$(printf '%s\n' "$scan" | awk '/^FRAMEWORK/{for(i=1;i<=NF;i++){split($i,a,"=");if(a[1]=="passed"||a[1]=="failed")s+=a[2]}}END{print s+0}') if [ "$rc" != 0 ]; then echo "$SUMTAG: BASELINE RED (exit $rc) for: $t"; tail -n 15 "$M/base.log" | sed "s|^|$SUMTAG: |"; exit 2 fi if [ "$tests" -eq 0 ]; then echo "$SUMTAG: BASELINE RAN ZERO TESTS (or printed counts no parser reads) for: $t"; exit 2 fi done # BUILD runs unmutated too. A mistyped or missing build command fails every # mutant, and a table of all-UNVIABLE rows reads as a fully killed suite. if [ -n "${BUILD:-}" ]; then rc=$(run "$BUILD" "$M/base.log") if [ "$rc" != 0 ]; then echo "$SUMTAG: BASELINE RED: BUILD failed on the unmutated tree (exit $rc; 124 is a timeout): $BUILD" tail -n 15 "$M/base.log" | sed "s|^|$SUMTAG: |"; exit 2 fi fi # Baseline runs may dirty tracked files; settle that before the first mutant. git -C "$SRC" reset -q --hard 2>/dev/null base_state=$(tree_state) for n in $(seq 1 "$count"); do label=$(cat "$M/$n/label"); file=$(cat "$M/$n/file"); t=$(cat "$M/$n/test"); t=${t:-$TEST} out=''; detail='' if ! detail=$(perl -e ' local $/; my ($file, $bf, $af) = @ARGV; open my $h, "<:raw", $file or do { print "cannot read $file\n"; exit 3 }; my $c = <$h>; close $h; open my $b, "<:raw", $bf; my $before = <$b> // ""; open my $a, "<:raw", $af; my $after = <$a> // ""; if (length($before) == 0) { print "empty before text\n"; exit 3 } if ($before eq $after) { print "after equals before\n"; exit 3 } # a CRLF file matches multi-line before text written with LF if ($c =~ /\r\n/ && $before !~ /\r/) { s/\n/\r\n/g for $before, $after } my ($cnt, $p) = (0, 0); while (($p = index($c, $before, $p)) >= 0) { $cnt++; $p += length($before) } if ($cnt == 0) { print "before text not found\n"; exit 3 } if ($cnt > 1) { print "before text occurs $cnt times; make it unique\n"; exit 3 } substr($c, index($c, $before), length($before)) = $after; open my $o, ">:raw", $file or exit 3; print $o $c; close $o;' "$file" "$M/$n/before" "$M/$n/after" 2>&1); then out=NOAPPLY elif git diff --quiet -- "$file"; then out=NOAPPLY; detail="applied, but git sees no change in $file" else out=''; detail='' if [ -n "${BUILD:-}" ]; then rc=$(run "$BUILD" "$M/run.log") if [ "$rc" = 124 ]; then out=TIMEOUT; detail="build exceeded ${tmo}s" elif [ "$rc" != 0 ]; then out=UNVIABLE; detail="build exit $rc"; fi fi if [ -z "$out" ]; then rc=$(run "$t" "$M/run.log") if [ "$rc" = 0 ]; then out=SURVIVED; detail="tests passed with the mutation applied: $t" elif [ "$rc" = 124 ]; then out=TIMEOUT; detail="test exceeded ${tmo}s: $t" else names=$(tp_scan "$M/run.log" | grep '^FAILED ' | awk '!s[$0]++' | sed 's/^FAILED //') # A file-level failure (vitest: "FAIL [ ]", no test name) is a # module that did not load: a compile error, never a kill. named=$(printf '%s\n' "$names" | grep -v -E '^[^ >]+ \[ .* \]$') if [ -n "$named" ]; then out=CAUGHT; detail=$(printf '%s\n' "$named" | head -n 1) k=$(printf '%s\n' "$named" | wc -l); [ "$k" -le 1 ] || detail="$detail (+$((k - 1)) more)" elif [ -n "$names" ]; then out=UNVIABLE; detail="a test file failed to load, no test ran: $(printf '%s\n' "$names" | head -n 1)" else out=ERROR; detail="exit $rc, no failing test parsed: $(tail -n 1 "$M/run.log" | cut -c1-120)"; fi fi fi extra=$(dirtied "$file"); [ -z "$extra" ] || detail="$detail [run also changed: $extra; restored]" fi # The outcome is its own field. It is never recovered from rendered text, so a # label or a test name containing " | CAUGHT | " cannot forge a kill. printf '%s' "$out" >"$M/$n/outcome" detail=${detail//$'\n'/ } printf '%s: [%s/%s] %s | %s | %s\n' "$SUMTAG" "$n" "$count" "${label//|/\\|}" "$out" "$detail" if ! restore; then echo "$SUMTAG: RESTORE FAILED after $label: the tree differs from the baseline; stopping. Status: $(tree_state | head -n 5 | paste -sd';' -)"; exit 2 fi done tally() { local c=0 f; for f in "$M"/*/outcome; do [ "$(cat "$f")" = "$1" ] && c=$((c + 1)); done; echo "$c"; } caught=$(tally CAUGHT); unviable=$(tally UNVIABLE) echo "$SUMTAG: MUTATION $count mutants: caught=$caught survived=$(tally SURVIVED) unviable=$unviable timeout=$(tally TIMEOUT) noapply=$(tally NOAPPLY) error=$(tally ERROR) (baseline green, tests ran)" [ $((caught + unviable)) -eq "$count" ]