From 8745b2910ccad20718d0ac43de3aac1c4227b82d Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 22:13:11 -0500 Subject: [PATCH 1/4] 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/4] 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 3b074226ae7663f6ddd5bbd4400b3be219ee5d30 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 22:15:22 -0500 Subject: [PATCH 3/4] tools: numeric oracle mutation sweep runs on the shared runner [host NOT_RUN] Converts tools/numeric_oracle_mutations.sh to a thin adapter over tools/mutation_runner.sh (same 13 mutants, same stdout and exit code) and adds tests/test_numeric_oracle_conversion_equiv.sh comparing against 2dc9dd8. Co-Authored-By: Claude Sonnet 5.5 --- tests/test_numeric_oracle_conversion_equiv.sh | 49 ++++++++++ tools/numeric_oracle_mutations.sh | 92 +++++++++++-------- 2 files changed, 105 insertions(+), 36 deletions(-) create mode 100755 tests/test_numeric_oracle_conversion_equiv.sh diff --git a/tests/test_numeric_oracle_conversion_equiv.sh b/tests/test_numeric_oracle_conversion_equiv.sh new file mode 100755 index 000000000..53af24afa --- /dev/null +++ b/tests/test_numeric_oracle_conversion_equiv.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# Proof that tools/numeric_oracle_mutations.sh, now an adapter over +# tools/mutation_runner.sh, behaves exactly like the old standalone sweep from +# pinned commit 2dc9dd8: same stdout, same exit code, same per-mutant verdicts. +# Host only, CPU only. Runs both full sweeps (13 mutants each) on the same tree. +# The old script is extracted into tools/ (it cd's to dirname/..) and removed by +# a trap. Normalised, and only this: temp directory paths such as /tmp/tmp.AbC123 +# (replaced by TMPDIR). No timings appear in either script's output. +# The sweep is not a JSON writer, so there is no JSON to compare. +set -u +ROOT=$(cd -P "$(dirname "$0")/.." && pwd) +PIN=2dc9dd8 +OLD=$ROOT/tools/.old_numeric_oracle_mutations.$$.sh +W=$(mktemp -d) +trap 'rm -f "$OLD"; rm -rf "$W"' EXIT INT TERM +git -C "$ROOT" show "$PIN:tools/numeric_oracle_mutations.sh" > "$OLD" || { echo "FAIL: cannot extract old script from $PIN"; exit 1; } +chmod +x "$OLD" + +norm() { sed -E 's#/[^ :]*/tmp\.[A-Za-z0-9]+#TMPDIR#g' "$1"; } +verdicts() { norm "$1" | sed -n -E 's/^ \[(KILLED|SURVIVED|BROKEN)\] +([A-Za-z0-9_]+).*/\2 \1/p' | sed 's/BROKEN$/ERROR/'; } +# differs : 0 if any difference in stdout, exit code or verdict list. +differs() { + ! { diff <(norm "$1") <(norm "$3") >/dev/null && [ "$2" = "$4" ] && diff <(verdicts "$1") <(verdicts "$3") >/dev/null; } +} + +bash "$OLD" > "$W/old.out" 2>&1; old_rc=$? +bash "$ROOT/tools/numeric_oracle_mutations.sh" > "$W/new.out" 2>&1; new_rc=$? + +fails=0 +n=$(verdicts "$W/old.out" | wc -l) +if [ "$n" -lt 1 ]; then echo "FAIL: old sweep produced no verdicts"; fails=$((fails + 1)); fi +if differs "$W/old.out" "$old_rc" "$W/new.out" "$new_rc"; then + echo "FAIL: old and new sweeps differ"; diff <(norm "$W/old.out") <(norm "$W/new.out") | head -20 + echo "exit old=$old_rc new=$new_rc"; fails=$((fails + 1)) +else + echo "ok stdout, exit code ($new_rc) and verdict list identical" +fi +if [ "$(verdicts "$W/old.out")" != "$(verdicts "$W/new.out")" ]; then echo "FAIL: verdict lists differ"; fails=$((fails + 1)); fi + +# Negative checks: the comparison must be able to fail. +sed '0,/\[KILLED\]/s//[SURVIVED]/' "$W/new.out" > "$W/alt.out" +if differs "$W/old.out" "$old_rc" "$W/alt.out" "$new_rc"; then echo "ok altered verdict line is reported different" +else echo "FAIL: comparison missed an altered verdict line"; fails=$((fails + 1)); fi +if differs "$W/old.out" "$old_rc" "$W/new.out" "$((new_rc + 1))"; then echo "ok altered exit code is reported different" +else echo "FAIL: comparison missed an altered exit code"; fails=$((fails + 1)); fi + +echo "mutants compared: $n" +if [ "$fails" -eq 0 ]; then echo "numeric oracle conversion equivalence: PASS ($n mutants compared)"; exit 0; fi +echo "numeric oracle conversion equivalence: FAIL"; exit 1 diff --git a/tools/numeric_oracle_mutations.sh b/tools/numeric_oracle_mutations.sh index 8f5cbc5d9..5f0440841 100755 --- a/tools/numeric_oracle_mutations.sh +++ b/tools/numeric_oracle_mutations.sh @@ -7,6 +7,15 @@ # passed every test because reference and CPU tier shared the same code. # Host only: opens no device, writes only under a temporary directory. # Run: make test-numeric-sweep (runs this after the CHECK sweep), or this file. +# +# This file is a thin adapter over tools/mutation_runner.sh. The mutant set is +# the table below (id~file~edit~test ID that must FAIL, edit kind "replace": +# R:@>@, @NL@ = newline). The runner takes ONE test +# command per call, and this sweep needs a different named FAIL id per mutant, +# so the adapter calls the runner once per row (ONLY=) and maps the runner's +# stderr line back to the legacy [KILLED]/[SURVIVED]/[BROKEN] lines. The judge +# script exits nonzero (KILLED) only if the test binary exited nonzero AND the +# named id is reported; it records the binary's exit status for the legacy line. set -u HERE=$(cd -P "$(dirname "$0")/.." && pwd) TMP=$(mktemp -d) @@ -15,45 +24,56 @@ SRCS="tests/test_omega_numeric.c src/omega_numeric.c src/omega_numeric_provenanc src/omega_blackwell_encoder.c src/omega_blackwell_codegen.c src/omega_blackwell_matmul.c src/omega_blackwell_qmd.c src/sha256.c" -# name | file | exact text | replacement | test ID that must FAIL (or UNDECLARED_SKIP) -MUTATIONS=( -"exp_coefficient|src/omega_numeric.c|const float c5 = 0.0083333338f;|const float c5 = 0.0093333338f;|CPU_TIER_INDEPENDENT_ORACLE" -"exp_drop_ln2_lo|src/omega_numeric.c|float r = (x - fk * LN2_HI) - fk * LN2_LO;|float r = (x - fk * LN2_HI) - fk * 0.0f * LN2_LO;|CPU_TIER_INDEPENDENT_ORACLE" -"log_coefficient|src/omega_numeric.c|const float c1 = 0.333333343f;|const float c1 = 0.343333343f;|CPU_TIER_INDEPENDENT_ORACLE" -"cpu_fadd_is_fsub|src/omega_numeric.c|case OMEGA_NOP_FADD: __asm__ volatile(\"fadd|case OMEGA_NOP_FADD: __asm__ volatile(\"fsub|CPU_TIER_INDEPENDENT_ORACLE" -"ref_fmul_rounds_twice|src/omega_numeric.c|float omega_ref_fmul(float a, float b) { return a * b; }|float omega_ref_fmul(float a, float b) { return (float)((double)a * (double)b * (1.0 + 0x1p-40)); }|CPU_TIER_INDEPENDENT_ORACLE" -"ref_ffma_is_fmsub|src/omega_numeric.c|__asm__(\"fmadd %s0|__asm__(\"fmsub %s0|CPU_TIER_INDEPENDENT_ORACLE" -"cpu_i2f_unsigned|src/omega_numeric.c|\"scvtf %s0, %w1\"|\"ucvtf %s0, %w1\"|CPU_TIER_INDEPENDENT_ORACLE" -"cpu_rcp_is_fmul|src/omega_numeric.c|case OMEGA_NOP_MUFU_RCP: __asm__ volatile(\"fdiv|case OMEGA_NOP_MUFU_RCP: __asm__ volatile(\"fmul|CPU_TIER_INDEPENDENT_ORACLE" -"ref_lds_identity|src/omega_numeric.c|r = a[i ^ (OMEGA_NUMERIC_CTA_THREADS - 1u)];|r = a[i];|CPU_TIER_INDEPENDENT_ORACLE" -"ref_rni_ties_away|src/omega_numeric.c|default: if (frac == 3|default: if (frac >= 2) mag++; else if (frac == 3|E1_SCALAR_INDEPENDENT_ORACLE" -"cpu_gtu_is_gt|src/omega_numeric.c|CPU_SEL(\"hi\")|CPU_SEL(\"gt\")|E1_SCALAR_INDEPENDENT_ORACLE" -"ref_ffma_int_no_sticky|src/omega_numeric.c|(lost ? 1U : 0U)|(lost ? 0U : 0U)|E1_SCALAR_BOUNDARY_VALUES" -"undeclared_skip|tests/test_omega_numeric.c| printf(\"\\nGate 5 Results:| skip(\"NOT_A_CHIP_ONLY_ID\", \"mutation\");@NL@ printf(\"\\nGate 5 Results:|UNDECLARED_SKIP" -) +# id ~ file ~ edit ~ test ID that must FAIL (or UNDECLARED_SKIP) +cat > "$TMP/rows" <<'ROWS_EOF' +exp_coefficient~src/omega_numeric.c~R:const float c5 = 0.0083333338f;@>@const float c5 = 0.0093333338f;~CPU_TIER_INDEPENDENT_ORACLE +exp_drop_ln2_lo~src/omega_numeric.c~R:float r = (x - fk * LN2_HI) - fk * LN2_LO;@>@float r = (x - fk * LN2_HI) - fk * 0.0f * LN2_LO;~CPU_TIER_INDEPENDENT_ORACLE +log_coefficient~src/omega_numeric.c~R:const float c1 = 0.333333343f;@>@const float c1 = 0.343333343f;~CPU_TIER_INDEPENDENT_ORACLE +cpu_fadd_is_fsub~src/omega_numeric.c~R:case OMEGA_NOP_FADD: __asm__ volatile("fadd@>@case OMEGA_NOP_FADD: __asm__ volatile("fsub~CPU_TIER_INDEPENDENT_ORACLE +ref_fmul_rounds_twice~src/omega_numeric.c~R:float omega_ref_fmul(float a, float b) { return a * b; }@>@float omega_ref_fmul(float a, float b) { return (float)((double)a * (double)b * (1.0 + 0x1p-40)); }~CPU_TIER_INDEPENDENT_ORACLE +ref_ffma_is_fmsub~src/omega_numeric.c~R:__asm__("fmadd %s0@>@__asm__("fmsub %s0~CPU_TIER_INDEPENDENT_ORACLE +cpu_i2f_unsigned~src/omega_numeric.c~R:"scvtf %s0, %w1"@>@"ucvtf %s0, %w1"~CPU_TIER_INDEPENDENT_ORACLE +cpu_rcp_is_fmul~src/omega_numeric.c~R:case OMEGA_NOP_MUFU_RCP: __asm__ volatile("fdiv@>@case OMEGA_NOP_MUFU_RCP: __asm__ volatile("fmul~CPU_TIER_INDEPENDENT_ORACLE +ref_lds_identity~src/omega_numeric.c~R:r = a[i ^ (OMEGA_NUMERIC_CTA_THREADS - 1u)];@>@r = a[i];~CPU_TIER_INDEPENDENT_ORACLE +ref_rni_ties_away~src/omega_numeric.c~R:default: if (frac == 3@>@default: if (frac >= 2) mag++; else if (frac == 3~E1_SCALAR_INDEPENDENT_ORACLE +cpu_gtu_is_gt~src/omega_numeric.c~R:CPU_SEL("hi")@>@CPU_SEL("gt")~E1_SCALAR_INDEPENDENT_ORACLE +ref_ffma_int_no_sticky~src/omega_numeric.c~R:(lost ? 1U : 0U)@>@(lost ? 0U : 0U)~E1_SCALAR_BOUNDARY_VALUES +undeclared_skip~tests/test_omega_numeric.c~R: printf("\nGate 5 Results:@>@ skip("NOT_A_CHIP_ONLY_ID", "mutation");@NL@ printf("\nGate 5 Results:~UNDECLARED_SKIP +ROWS_EOF + +cat > "$TMP/judge.sh" <<'JUDGE_EOF' +#!/bin/sh +# usage: judge.sh +"$1/t" > "$1/run.log" 2>&1; rc=$? +echo "$rc" > "$1/rc" +if [ "$rc" -ne 0 ] && { grep -qx "\[FAIL\] $2" "$1/run.log" || grep -q "^Gate 5 Verdict: $2 " "$1/run.log"; }; then exit 1; fi +exit 0 +JUDGE_EOF total=0 killed=0 survived=0 broken=0 -for m in "${MUTATIONS[@]}"; do - IFS='|' read -r name file from to want <<< "$m" +while IFS='~' read -r name file edit want; do + [ -n "$name" ] || continue total=$((total + 1)) - rm -rf "$TMP/tree"; mkdir -p "$TMP/tree/src" "$TMP/tree/tests" - cp "$HERE"/src/*.c "$HERE"/src/*.h "$TMP/tree/src/" - cp "$HERE"/tests/test_omega_numeric.c "$HERE"/tests/numeric_oracle.h "$TMP/tree/tests/" - FROM=$from TO=$to perl -0pi -e '$n += s/\Q$ENV{FROM}\E/$ENV{TO} =~ s{\@NL\@}{\n}gr/ge; END { $? = $n == 1 ? 0 : 3 }' "$TMP/tree/$file" - if [ $? -ne 0 ]; then - echo " [BROKEN] $name: text not found exactly once in $file"; broken=$((broken + 1)); continue - fi + rm -f "$TMP/rc" "$TMP/build.log" + printf '%s~%s~%s~%s\n' "$name" "$file" "$edit" "$want" > "$TMP/row" # shellcheck disable=SC2086 - if ! (cd "$TMP/tree" && gcc -std=gnu11 -O2 -Wall -Wextra -Werror -ffp-contract=off -Isrc \ - -DOMEGA_NUMERIC_CPU_ONLY -o "$TMP/t" $SRCS) > "$TMP/build.log" 2>&1; then - echo " [BROKEN] $name: scratch copy does not build"; head -5 "$TMP/build.log"; broken=$((broken + 1)); continue - fi - "$TMP/t" > "$TMP/run.log" 2>&1; rc=$? - if [ "$rc" -ne 0 ] && { grep -qx "\[FAIL\] $want" "$TMP/run.log" || grep -q "^Gate 5 Verdict: $want " "$TMP/run.log"; }; then - echo " [KILLED] $name: exit $rc, $want"; killed=$((killed + 1)) - else - echo " [SURVIVED] $name: exit $rc, $want not reported"; survived=$((survived + 1)) - fi -done + ONLY=$name "$HERE/tools/mutation_runner.sh" -k table -m "$TMP/row" \ + -d "$HERE" -c "src tests/test_omega_numeric.c tests/numeric_oracle.h" \ + -b "gcc -std=gnu11 -O2 -Wall -Wextra -Werror -ffp-contract=off -Isrc -DOMEGA_NUMERIC_CPU_ONLY -o $TMP/t $SRCS > $TMP/build.log 2>&1" \ + -t "sh $TMP/judge.sh $TMP $want" -o "$TMP/out.json" > /dev/null 2> "$TMP/runner.err" < /dev/null + status=$(sed -n "s/^$name \([A-Z_]*\) .*/\1/p" "$TMP/runner.err" | head -1) + rc=$(cat "$TMP/rc" 2>/dev/null) + case $status in + KILLED) echo " [KILLED] $name: exit $rc, $want"; killed=$((killed + 1));; + SURVIVED) echo " [SURVIVED] $name: exit $rc, $want not reported"; survived=$((survived + 1));; + *) + if grep -q "^$name ERROR .*(build failed" "$TMP/runner.err"; then + echo " [BROKEN] $name: scratch copy does not build"; head -5 "$TMP/build.log" + else + echo " [BROKEN] $name: text not found exactly once in $file" + fi + broken=$((broken + 1));; + esac +done < "$TMP/rows" echo "oracle mutations: $total, $killed caught, $survived survived, $broken broken" [ "$total" -gt 0 ] && [ "$killed" -eq "$total" ] From da08815b2830ef5a1217ba1111ef5caf81724dab Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 23:09:41 -0500 Subject: [PATCH 4/4] tools: oracle adapter passes the source list on one line so the runner build is one command [host NOT_RUN] The forge run at 3b07422 failed: the adapter reported BROKEN "scratch copy does not build" with "sh: 3: src/omega_blackwell_qmd.c: Permission denied". Cause: SRCS spanned three lines and is interpolated into the -b string, which tools/mutation_runner.sh runs with sh -c (lines 91 and 141). The embedded newlines split the one gcc command into three shell commands, so the later lines tried to execute source files. The old script used SRCS unquoted as gcc arguments, where newlines are harmless word separators. Fix: SRCS is now a single line, same files, same order. The runner, the 13 mutant rows, the expected FAIL ids and the equivalence test are unchanged. Not built or run here; the forge decides. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01DsjWa6csUe4NxndCTmugHu --- tools/numeric_oracle_mutations.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/numeric_oracle_mutations.sh b/tools/numeric_oracle_mutations.sh index 5f0440841..802270d06 100755 --- a/tools/numeric_oracle_mutations.sh +++ b/tools/numeric_oracle_mutations.sh @@ -20,9 +20,9 @@ set -u HERE=$(cd -P "$(dirname "$0")/.." && pwd) TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT -SRCS="tests/test_omega_numeric.c src/omega_numeric.c src/omega_numeric_provenance.c src/omega_numeric_divsqrt_gb10.c - src/omega_blackwell_encoder.c src/omega_blackwell_codegen.c src/omega_blackwell_matmul.c - src/omega_blackwell_qmd.c src/sha256.c" +# One line on purpose: $SRCS is interpolated into the runner's -b string, which +# the runner executes with sh -c, so a newline here would split the gcc command. +SRCS="tests/test_omega_numeric.c src/omega_numeric.c src/omega_numeric_provenance.c src/omega_numeric_divsqrt_gb10.c src/omega_blackwell_encoder.c src/omega_blackwell_codegen.c src/omega_blackwell_matmul.c src/omega_blackwell_qmd.c src/sha256.c" # id ~ file ~ edit ~ test ID that must FAIL (or UNDECLARED_SKIP) cat > "$TMP/rows" <<'ROWS_EOF'