omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / mutation_runner.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # One mutation runner (POSIX sh, no Python). Deep module, small interface. |
| 18 | # |
| 19 | # A MUTATION is one record: |
| 20 | # { name, file(s), edit, test command, what the check guards, equivalence } |
| 21 | # The runner copies the source tree to a scratch directory (the source tree is |
| 22 | # never edited), applies the edit there, checks that the files really changed |
| 23 | # (otherwise ERROR "edit did not apply"), runs the optional build command |
| 24 | # (failure => ERROR), then runs the test command inside the scratch tree: |
| 25 | # exit nonzero => KILLED exit 0 => SURVIVED |
| 26 | # EQUIVALENCE is a data column: class none|redundant|gap plus rationale text. |
| 27 | # A SURVIVED mutant with class redundant or gap is reported SURVIVED_REDUNDANT |
| 28 | # or SURVIVED_GAP and the JSON note is "<class>: <rationale>" (the same note |
| 29 | # tools/c4_requal_mutants.sh writes today). Class none (or no class) leaves it |
| 30 | # SURVIVED, which fails the sweep. |
| 31 | # |
| 32 | # EDIT KINDS |
| 33 | # sed files comma-separated, expressions separated by @@ (one per |
| 34 | # file, in order), as tools/c4_requal_mutants.sh does today. |
| 35 | # marker-sed one file, one sed expression applied only to lines containing |
| 36 | # MUT:<name>, as tensor/autodiff/m20 mutation sweeps do today. |
| 37 | # replace edit text "R:<from>@>@<to>[@@<from>@>@<to>...]": <from> must be |
| 38 | # found exactly once in its file (else ERROR), @NL@ in <to> is a |
| 39 | # newline, as numeric_oracle_mutations.sh does today. |
| 40 | # |
| 41 | # READERS (two; the older sweeps become adapters over them) |
| 42 | # table rows id~files~edit~guards[~class~rationale] (kinds sed, replace) |
| 43 | # Legacy rows are accepted: 4 columns (no equivalence), or 5 columns |
| 44 | # whose last column is prose starting "redundant:" or "gap:". |
| 45 | # Lines that are empty or start with # are skipped. |
| 46 | # marker rows name|file|sed-expr (kind marker-sed; the expression may |
| 47 | # contain |). Marker rows have no equivalence column: a survivor is |
| 48 | # always a failure. |
| 49 | # |
| 50 | # USAGE |
| 51 | # tools/mutation_runner.sh -k table|marker -m ROWS|- -t TESTCMD |
| 52 | # [-d SRCDIR] [-b BUILDCMD] [-e TEXT] [-c "PATH ..."] [-o out.json] [-f PREFIX] [-B] |
| 53 | # -k reader kind. -m rows file, or - for stdin. -t test command (sh -c, run |
| 54 | # in the scratch tree). -d source tree (default .). -b build command run in |
| 55 | # the scratch tree before the test. -e TEXT optional: when the build step |
| 56 | # fails and its log is empty (make killed or never started) the row note |
| 57 | # is "build failed: TEXT" instead of "build failed: <first error line>"; |
| 58 | # without -e nothing changes. -c paths (relative to SRCDIR) to copy |
| 59 | # (default: whole tree). -o JSON receipt path. -f line prefix that marks a |
| 60 | # failing case in test output, used only for the KILLED note. -B run build |
| 61 | # and test on the unmutated tree first; if that fails the sweep FAILS. |
| 62 | # Env ONLY="id id" restricts the sweep to those ids. |
| 63 | # |
| 64 | # OUTPUT |
| 65 | # stderr: one line per mutant "<id> <STATUS> <guards> (<note>)", STATUS is |
| 66 | # KILLED, SURVIVED, SURVIVED_REDUNDANT, SURVIVED_GAP or ERROR. |
| 67 | # stdout: one summary line. |
| 68 | # JSON (-o), exactly the c4 receipt layout, same whitespace, key order and |
| 69 | # escaping (note has double quotes and backslashes removed with tr -d): |
| 70 | # {"mutants":[{"id","file","checks","status","note"}, ...]} |
| 71 | # Exit 0 only if at least one mutant ran and every mutant is KILLED or |
| 72 | # justified-survived; 2 on bad usage. |
| 73 | set -u |
| 74 | |
| 75 | kind=; rows=; test_cmd=; src=.; build_cmd=; copy=.; out=; fprefix=; baseline=0; empty_note= |
| 76 | while getopts k:m:t:d:b:e:c:o:f:B opt; do |
| 77 | case $opt in |
| 78 | k) kind=$OPTARG;; m) rows=$OPTARG;; t) test_cmd=$OPTARG;; d) src=$OPTARG;; |
| 79 | b) build_cmd=$OPTARG;; e) empty_note=$OPTARG;; c) copy=$OPTARG;; o) out=$OPTARG;; f) fprefix=$OPTARG;; |
| 80 | B) baseline=1;; *) kind=;; |
| 81 | esac |
| 82 | done |
| 83 | case $kind in table|marker) ;; *) kind=;; esac |
| 84 | if [ -z "$kind" ] || [ -z "$rows" ] || [ -z "$test_cmd" ]; then |
| 85 | echo "usage: mutation_runner.sh -k table|marker -m ROWS|- -t TESTCMD [-d SRCDIR] [-b BUILDCMD] [-e TEXT] [-c PATHS] [-o out.json] [-f PREFIX] [-B]" >&2 |
| 86 | exit 2 |
| 87 | fi |
| 88 | src=$(cd "$src" 2>/dev/null && pwd) || { echo "mutation_runner: no such source dir" >&2; exit 2; } |
| 89 | work=$(mktemp -d "${TMPDIR:-/tmp}/mutrun.XXXXXX") || exit 2 |
| 90 | trap 'rm -rf "$work"' EXIT INT TERM |
| 91 | if [ "$rows" = - ]; then cat > "$work/rows"; rows=$work/rows; fi |
| 92 | [ -r "$rows" ] || { echo "mutation_runner: cannot read rows" >&2; exit 2; } |
| 93 | [ -n "$out" ] || out=$work/out.json |
| 94 | |
| 95 | # Fresh scratch copy of the tree in $1. |
| 96 | copy_tree() { |
| 97 | mkdir -p "$1" |
| 98 | for p in $copy; do |
| 99 | if [ "$p" = . ]; then cp -R "$src/." "$1/" |
| 100 | else |
| 101 | mkdir -p "$1/$(dirname "$p")" |
| 102 | cp -R "$src/$p" "$1/$p" 2>/dev/null |
| 103 | fi |
| 104 | done |
| 105 | } |
| 106 | |
| 107 | if [ $baseline -eq 1 ]; then |
| 108 | copy_tree "$work/base" |
| 109 | if { [ -z "$build_cmd" ] || (cd "$work/base" && sh -c "$build_cmd") </dev/null >"$work/base.log" 2>&1; } \ |
| 110 | && (cd "$work/base" && sh -c "$test_cmd") </dev/null >>"$work/base.log" 2>&1; then : |
| 111 | else |
| 112 | head -5 "$work/base.log" >&2 |
| 113 | echo "mutation runner: FAIL (unmutated baseline does not pass)" |
| 114 | exit 1 |
| 115 | fi |
| 116 | rm -rf "$work/base" |
| 117 | fi |
| 118 | |
| 119 | # Edit step. Sets $note on failure and returns nonzero. |
| 120 | apply_edit() { # dir files ekind edit id |
| 121 | a_dir=$1; a_files=$(echo "$2" | tr ',' ' '); a_kind=$3; a_edit=$4; a_id=$5 |
| 122 | for f in $a_files; do |
| 123 | [ -f "$a_dir/$f" ] || { note="edit did not apply (no such file $f)"; return 1; } |
| 124 | done |
| 125 | a_before=$(for f in $a_files; do sha256sum "$a_dir/$f"; done | sha256sum) |
| 126 | i=1 |
| 127 | for f in $a_files; do |
| 128 | e=$(printf '%s\n' "$a_edit" | awk -F'@@' -v n=$i '{print $n}') |
| 129 | case $a_kind in |
| 130 | sed) sed -i "$e" "$a_dir/$f" ;; |
| 131 | marker-sed) sed -i "/MUT:$a_id/ $e" "$a_dir/$f" ;; |
| 132 | replace) |
| 133 | e=${e#R:} |
| 134 | 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" \ |
| 135 | || { note="replace text not found exactly once in $f"; return 1; } |
| 136 | ;; |
| 137 | esac |
| 138 | i=$((i + 1)) |
| 139 | done |
| 140 | a_after=$(for f in $a_files; do sha256sum "$a_dir/$f"; done | sha256sum) |
| 141 | [ "$a_before" != "$a_after" ] || { note="edit did not apply"; return 1; } |
| 142 | } |
| 143 | |
| 144 | n=0 k=0 r=0 g=0 |
| 145 | first=1 |
| 146 | printf '{\n "mutants": [\n' > "$out" |
| 147 | |
| 148 | # run_one id files ekind edit guards justification (justification is the |
| 149 | # prose "redundant: ..." / "gap: ..." or empty) |
| 150 | run_one() { |
| 151 | id=$1; files=$2; ekind=$3; edit=$4; what=$5; just=$6 |
| 152 | if [ -n "${ONLY:-}" ]; then case " $ONLY " in *" $id "*) ;; *) return 0;; esac; fi |
| 153 | dir=$work/m.$id |
| 154 | copy_tree "$dir" |
| 155 | note= |
| 156 | if ! apply_edit "$dir" "$files" "$ekind" "$edit" "$id"; then status=ERROR |
| 157 | else |
| 158 | log=$work/$id.log |
| 159 | if [ -n "$build_cmd" ] && ! (cd "$dir" && sh -c "$build_cmd") </dev/null >"$log" 2>&1; then |
| 160 | status=ERROR; note="build failed: $(grep -m1 -E "error:|Error " "$log" | cut -c1-120)" |
| 161 | [ -z "$empty_note" ] || [ -s "$log" ] || note="build failed: $empty_note" |
| 162 | else |
| 163 | (cd "$dir" && sh -c "$test_cmd") </dev/null >"$log.run" 2>&1 |
| 164 | rc=$? |
| 165 | nf=0 |
| 166 | [ -z "$fprefix" ] || nf=$(grep -c "^$fprefix" "$log.run") |
| 167 | if [ $rc -ne 0 ] && [ "$nf" -gt 0 ]; then |
| 168 | cut_from=$((${#fprefix} + 2)) |
| 169 | status=KILLED; note="$nf failing case(s), first: $(grep -m1 "^$fprefix" "$log.run" | cut -c"$cut_from"-$((cut_from + 64)))" |
| 170 | elif [ $rc -ne 0 ]; then status=KILLED; note="harness exit $rc (crash or abort)" |
| 171 | else |
| 172 | case "$just" in |
| 173 | redundant:*) status=SURVIVED_REDUNDANT; note="$just" ;; |
| 174 | gap:*) status=SURVIVED_GAP; note="$just" ;; |
| 175 | *) status=SURVIVED; note="harness still PASS, no justification recorded" ;; |
| 176 | esac |
| 177 | fi |
| 178 | fi |
| 179 | fi |
| 180 | echo "$id $status $what ($note)" >&2 |
| 181 | [ $first -eq 1 ] || printf ',\n' >> "$out" |
| 182 | first=0 |
| 183 | printf ' {"id": "%s", "file": "%s", "checks": "%s", "status": "%s", "note": "%s"}' "$id" "$files" "$what" "$status" "$(printf '%s\n' "$note" | tr -d '"\\')" >> "$out" |
| 184 | n=$((n + 1)) |
| 185 | case $status in KILLED) k=$((k + 1));; SURVIVED_REDUNDANT) r=$((r + 1));; SURVIVED_GAP) g=$((g + 1));; esac |
| 186 | rm -rf "$dir" |
| 187 | } |
| 188 | |
| 189 | # Reader 1: id~files~edit~guards[~class~rationale] |
| 190 | read_table() { |
| 191 | while IFS='~' read -r t_id t_files t_edit t_guards t_c5 t_c6; do |
| 192 | case $t_id in ''|'#'*) continue;; esac |
| 193 | t_kind=sed |
| 194 | case $t_edit in R:*) t_kind=replace;; esac |
| 195 | case $t_c5 in |
| 196 | none|redundant|gap) |
| 197 | if [ "$t_c5" = none ]; then t_just=; else t_just="$t_c5: $t_c6"; fi ;; |
| 198 | *) t_just=$t_c5; [ -z "$t_c6" ] || t_just="$t_c5~$t_c6" ;; |
| 199 | esac |
| 200 | run_one "$t_id" "$t_files" "$t_kind" "$t_edit" "$t_guards" "$t_just" |
| 201 | done < "$rows" |
| 202 | } |
| 203 | |
| 204 | # Reader 2: name|file|sed-expr (expr may contain |) |
| 205 | read_marker() { |
| 206 | while IFS='|' read -r m_name m_file m_expr; do |
| 207 | case $m_name in ''|'#'*) continue;; esac |
| 208 | run_one "$m_name" "$m_file" marker-sed "$m_expr" "MUT:$m_name" "" |
| 209 | done < "$rows" |
| 210 | } |
| 211 | |
| 212 | read_$kind |
| 213 | |
| 214 | printf '\n ]\n}\n' >> "$out" |
| 215 | echo "mutants: $n total, $k killed, $r survived as redundant, $g survived as test gaps (receipt $out)" |
| 216 | # Fail on any unjustified survivor or build/edit error, and on an empty sweep. |
| 217 | [ "$n" -gt 0 ] && [ $((k + r + g)) -eq "$n" ] |