mutation: parallel jobs in temp copies, per-mutation test files, FFF_MUTATION reduced iterations, trap cleanup, sleep inhibit

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 19:19:58 +01:00
co-authored by Claude Sonnet 5.5
parent dcb8efa422
commit 8d3b77add4
3 changed files with 69 additions and 35 deletions
+52 -27
View File
@@ -8,11 +8,50 @@
# h: capsule reduced to centre path + destination circle (prototype H10) | i: no step cap | j: blocked trigger needs centre-on-wall
# k: wall removal / door open clears blocked unconditionally (no KEEP) | l: GM moves never set/clear blocked
# m: door push goes to the wrong side first | n: door push skips the validity check | o: push ignores the previous-position rule
# Parallel: MUTATION_JOBS (default nproc/4) jobs, each in its own temp copy + nimcache; output collected, printed in fixed order.
# Tests run with FFF_MUTATION=1 (iteration counts ~/10, see tests/testutil.nim scaled()); only test files holding an expected
# test are run per mutation (control runs all). Debug build. Wrapped in systemd-inhibit (sleep:idle) when that works.
# Ctrl-C / TERM / EXIT: kills all jobs and removes the temp dir (fffmut.* under $TMPDIR or /tmp).
# Log: results/mutation.log
cd "$(dirname "$0")/.." || exit 1
job() { # runs in its own session: $1 id $2 title $3 file to mutate $4 sed script ("" = control) $5 expected-failure regex
local id=$1 title=$2 file=$3 sedscr=$4 expect=$5 w=$T/$1/w res=$T/$1/res tfs tf out=$T/$1/out r=0 rr F verdict=ok rc=0
mkdir -p "$w"; cp -r server tests tools nim.cfg "$w"/
find "$w/tests" -maxdepth 1 -type f ! -name '*.nim' -delete
echo "## $id: $title" > "$res"
if [ -n "$sedscr" ]; then
sed -i "$sedscr" "$w/$file"
if cmp -s "$file" "$w/$file"; then
echo "MUTATION $id DID NOT APPLY (sed matched nothing)" >> "$res"; echo "## $id: sed no-op" >> "$res"
echo "$id: sed no-op" > "$T/$id/verdict"; echo 1 > "$T/$id/rc"; return
fi
{ echo "# diff $file -> mutated copy:"; diff "$file" "$w/$file" | sed 's/^/# /'; } >> "$res"
tfs=$(grep -lE "(test|suite) \".*($expect)" tests/t*.nim); [ -n "$tfs" ] || tfs=$(ls tests/t*.nim)
else tfs=$(ls tests/t*.nim); fi
: > "$out"
for tf in $tfs; do
(cd "$w" && nim c -r --hints:off --nimcache:"$T/$id/nc-$(basename "$tf" .nim)" "$tf") >> "$out" 2>&1; rr=$?
[ $rr -eq 0 ] || r=$rr
done
grep -E '\[(OK|FAILED)\]|Success|Failure|Error' "$out" | sed 's/^ *\.* */ /' >> "$res"
F=$(grep '\[FAILED\]' "$out" | grep -E "$expect")
{ echo "# test exit=$r; expected tests failed: $(printf '%s' "$F" | grep -c .)"; [ -n "$F" ] && echo "$F"; echo; } >> "$res"
if [ -z "$sedscr" ]; then [ $r -eq 0 ] || { verdict="CONTROL FAILED"; rc=1; }
else [ -n "$F" ] || { verdict="SURVIVED"; rc=1; }; fi
echo "$id: exit=$r expected_failed=$(printf '%s' "$F" | grep -c .) $verdict" > "$T/$id/verdict"; echo $rc > "$T/$id/rc"
}
if [ "$1" = __job ]; then shift; job "$@"; exit 0; fi
if [ -z "$MUT_INHIBITED" ] && systemd-inhibit --what=sleep:idle --who=mutation --why=test true 2>/dev/null; then
MUT_INHIBITED=1 exec systemd-inhibit --what=sleep:idle --who=mutation --why="mutation run" "$0" "$@"
fi
mkdir -p results
LOG=$PWD/results/mutation.log
T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
JOBS=${MUTATION_JOBS:-$(( $(nproc) / 4 ))}; [ "$JOBS" -ge 1 ] || JOBS=1
export FFF_MUTATION=1
T=$(mktemp -d -t fffmut.XXXXXX)
PIDS=()
cleanup() { trap - EXIT INT TERM; local p; for p in "${PIDS[@]}"; do kill -TERM -- "-$p" 2>/dev/null; done; wait 2>/dev/null; rm -rf "$T"; }
trap cleanup EXIT; trap 'exit 130' INT; trap 'exit 143' TERM
NEW="shared endpoint: no leak at the vertex|'<' apex|every segment endpoint|rays nudged past a corner"
D="shared vertex: both arms required"
E="random visibility polygons|random simple polygons|corpus|prototype scene, all tokens|random wall fields|concave polygon|square polygon"
@@ -26,32 +65,11 @@ L="GM moves a stuck token|GM drops a token|GM manual clear inside|GM: ignores wa
M="Q14: a door closed|push: centre on the door line|push: one side blocked|doorway: token in an open doorway|GM drops a token"
N="push: one side blocked|push: both sides invalid"
O="push: centre on the door line"
rc=0
: > "$LOG"
run() { # $1 id $2 title $3 file to mutate $4 sed script ("" = control) $5 expected-failure regex
local id=$1 title=$2 file=$3 sedscr=$4 expect=$5 w=$T/w
rm -rf "$w"; mkdir -p "$w"; cp -r server tests tools nim.cfg "$w"/
find "$w/tests" -maxdepth 1 -type f ! -name '*.nim' -delete
{ echo "## $id: $title"
if [ -n "$sedscr" ]; then
sed -i "$sedscr" "$w/$file"
if cmp -s "$file" "$w/$file"; then echo "MUTATION $id DID NOT APPLY (sed matched nothing)"; echo "## $id: sed no-op" ; rc=1; return; fi
echo "# diff $file -> mutated copy:"; diff "$file" "$w/$file" | sed 's/^/# /'
fi
} >> "$LOG"
local out="$T/out-$id.txt" r=0 rr tf F
: > "$out"
for tf in tests/t*.nim; do
(cd "$w" && nim c -r --hints:off --nimcache:"$T/nc-$(basename "$tf" .nim)" "$tf") >> "$out" 2>&1; rr=$?
[ $rr -eq 0 ] || r=$rr
done
grep -E '\[(OK|FAILED)\]|Success|Failure|Error' "$out" | sed 's/^ *\.* */ /' >> "$LOG"
F=$(grep '\[FAILED\]' "$out" | grep -E "$expect")
{ echo "# test exit=$r; expected tests failed: $(printf '%s' "$F" | grep -c .)"; [ -n "$F" ] && echo "$F"; echo; } >> "$LOG"
local verdict=ok
if [ -z "$sedscr" ]; then [ $r -eq 0 ] || { verdict="CONTROL FAILED"; rc=1; }
else [ -n "$F" ] || { verdict="SURVIVED"; rc=1; }; fi
echo "$id: exit=$r expected_failed=$(printf '%s' "$F" | grep -c .) $verdict" | tee -a "$LOG"
IDS=()
run() {
while [ "$(jobs -rp | wc -l)" -ge "$JOBS" ]; do wait -n; done
mkdir -p "$T/$1"; IDS+=("$1")
setsid env T="$T" FFF_MUTATION=1 "$0" __job "$@" & PIDS+=($!)
}
run control "unmutated copy (must pass)" server/geometry.nim "" ""
run a "remove the +-RayEps rays" server/geometry.nim '/angles.add ang [-+] RayEps/d' "$NEW"
@@ -70,5 +88,12 @@ run l "GM moves never set or clear blocked" server/moverules.nim '/^ if who.rol
run m "door push: wrong side first" server/moverules.nim 's/for sg in \[(if side > 0: 1.0 else: -1.0), (if side > 0: -1.0 else: 1.0)\]:/for sg in [(if side > 0: -1.0 else: 1.0), (if side > 0: 1.0 else: -1.0)]:/' "$M"
run n "door push: no validity check" server/moverules.nim 's/if inBounds(sc, p) and not overlapsAny(sc, tt): return (true, p)/return (true, p)/' "$N"
run o "door push: previous position ignored on the line" server/moverules.nim 's/side = if t.hasPrev: dot(t.prev - w.a, n) else: 0.0/side = 0.0/' "$O"
wait
rc=0; : > "$LOG"
for id in control a b c d1 d2 e f g h i j k l m n o; do
[ -f "$T/$id/res" ] && cat "$T/$id/res" >> "$LOG"
if [ -f "$T/$id/verdict" ]; then tee -a "$LOG" < "$T/$id/verdict"; [ "$(cat "$T/$id/rc")" = 0 ] || rc=1
else echo "$id: NO RESULT (job killed?)" | tee -a "$LOG"; rc=1; fi
done
echo "exit=$rc" | tee -a "$LOG"
exit $rc