unitrun.sh raw

   1  #!/bin/bash
   2  # Unit driver for the test suite.
   3  #
   4  #   tests/unitrun.sh                          every unit of every phase
   5  #   tests/unitrun.sh --phases 0a,0b,1,1b      selected phases
   6  #   tests/unitrun.sh --failed                 re-run only what failed
   7  #   tests/unitrun.sh --phases 0a --only asn1  units matching a pattern
   8  #   tests/unitrun.sh --units a,b,c            exactly these units, whatever
   9  #                                             phase they belong to
  10  #   tests/unitrun.sh --phases 0a -j 6         six units at a time
  11  #   tests/unitrun.sh --cold                   drop the package cache first
  12  #                                             (always serial: a cold run has no
  13  #                                             cached artifacts to share)
  14  #   tests/unitrun.sh --fail-fast              stop launching after the first
  15  #                                             failure (build.sh uses this)
  16  #   tests/unitrun.sh --wait                   wait for a running instance
  17  #                                             instead of refusing to start
  18  #
  19  # Why this exists: the suite used to be one process per phase, so a phase that
  20  # hung or crashed lost every fixture it had already run, and re-running it meant
  21  # re-running all of them. Units are independent: each has its own temp dir, its
  22  # own log, its own timeout and its own exit code, and results are kept under
  23  # tmp/unitrun/, so a red unit is re-run on its own and a completed run is not
  24  # repeated.
  25  #
  26  #   tmp/unitrun/<phase>/<unit>/{rc,start,end,out,err}   per-unit state
  27  #   tmp/unitrun/summary.tsv                             one row per unit
  28  #
  29  # Exit: 0 when every unit passed or skipped, 1 when any unit failed. A unit
  30  # that already passed is not run again unless --force is given.
  31  set -uo pipefail
  32  
  33  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  34  ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
  35  # The compiler's scratch dirs (`./tmp/mxc-build-*`) and the fixtures' relative
  36  # paths both resolve against the cwd, so pin it: a gate started from a
  37  # subdirectory must build exactly what a gate started from the root builds.
  38  cd "$ROOT" || exit 1
  39  export MOXIEROOT="$ROOT"
  40  MOXIE="${MOXIE:-$ROOT/moxie}"
  41  export MOXIE
  42  
  43  RUNDIR="${UNITRUN_DIR:-$ROOT/tmp/unitrun}"
  44  # Fixture trees MUST live outside the checkout. The language restrictions fire
  45  # only for a package outside the toolchain's own module, so a fixture built
  46  # from inside the tree is not user code and the restriction phase silently
  47  # measures nothing: `reject_closure_capture` compiled clean from
  48  # $ROOT/tmp/... and was rejected with the expected error from /tmp/... . The
  49  # driver's own results stay in the tree (they are what a later reader wants);
  50  # only the throwaway build dirs go outside it.
  51  WORKROOT="${UNITRUN_WORKROOT:-${XDG_CACHE_HOME:-$HOME/.cache}/moxie-unitrun}"
  52  JOBS="${UNITRUN_JOBS:-1}"
  53  # The default is per CORE, not a flat wall-clock number: a unit's wall time is
  54  # what it costs divided by how many other units are competing for the CPU with
  55  # it, so the same fixture needs several times as long at -j 4 as at -j 1, and a
  56  # flat 900s turned a slow-but-fine unit into a red TIMEOUT. Measured:
  57  # 0a/http_header_api compiles a ~250-package closure and takes 1129s at -j 1
  58  # against a warm cache; the same unit was killed at 900s when it shared the box.
  59  TIMEOUT="${UNITRUN_TIMEOUT:-$((1800 * $(nproc 2>/dev/null || echo 1)))}"
  60  # A unit whose cost is a known constant rather than a function of parallelism.
  61  # http_header_api is the one: 1129s measured alone, so 1800s is 60% headroom
  62  # over the measurement, not a guess. Applied only while the caller has not set
  63  # a timeout of their own - an explicit --timeout is an instruction, not a
  64  # default to improve on.
  65  declare -A UNIT_TIMEOUT=(
  66  	["0a/http_header_api"]=1800
  67  )
  68  TIMEOUT_EXPLICIT=0
  69  PHASES=""
  70  ONLY=""
  71  UNITS_LIST=""
  72  FAILED_ONLY=0
  73  FORCE=0
  74  VERBOSE=0
  75  COLD=0
  76  FAIL_FAST=0
  77  WAIT_LOCK=0
  78  
  79  # Phases that can subdivide themselves into units. A phase not listed here is
  80  # monolithic: its whole script is one unit, so it still gets its own timeout,
  81  # log and result file. Phase 2 is a bootstrap chain, not a list of fixtures;
  82  # phase 5 reports per-target results internally and is one unit on purpose.
  83  declare -A PHASE_SCRIPT=(
  84  	[0a]="regressions/run.sh"
  85  	[0b]="restrictions/run.sh"
  86  	[1]="phase1_packages.sh"
  87  	[1b]="phase1b_stdlib_sweep.sh"
  88  	[1c]="phase1c_wasm_sweep.sh"
  89  	[5]="phase5_wasm.sh"
  90  )
  91  declare -A PHASE_MONO=(
  92  	[0c]="phase0c_coldcache.sh"
  93  	[2]="phase2_selfhost.sh"
  94  	[7]="phase7_ui.sh"
  95  	[3]="phase3_ir_validate.sh"
  96  	[4]="phase4_binary_smoke.sh"
  97  	[6]="phase6_instrumentation.sh"
  98  	[8]="phase8_netsmoke.sh"
  99  )
 100  ALL_PHASES=(0a 0b 0c 1 1b 1c 2 3 4 5 6 7 8)
 101  
 102  usage() {
 103  	awk 'NR > 1 && /^#/ { sub(/^# ?/, ""); print; next } NR > 1 { exit }' "$0"
 104  	exit "${1:-0}"
 105  }
 106  
 107  while [ $# -gt 0 ]; do
 108  	case "$1" in
 109  	--phases) PHASES="${2:-}"; shift 2 ;;
 110  	--only)
 111  		if [ -n "$ONLY" ]; then
 112  			echo "--only takes one pattern; combine with a glob (e.g. --only 'reject_*')" >&2
 113  			exit 1
 114  		fi
 115  		ONLY="${2:-}"
 116  		shift 2
 117  		;;
 118  	--jobs | -j) JOBS="${2:-}"; shift 2 ;;
 119  	--timeout) TIMEOUT="${2:-}"; TIMEOUT_EXPLICIT=1; shift 2 ;;
 120  	--units)
 121  		UNITS_LIST="${2:-}"
 122  		shift 2
 123  		;;
 124  	--failed) FAILED_ONLY=1; shift ;;
 125  	--force) FORCE=1; shift ;;
 126  	--fail-fast) FAIL_FAST=1; shift ;;
 127  	--wait) WAIT_LOCK=1; shift ;;
 128  	--cold) COLD=1; JOBS=1; shift ;;
 129  	--verbose | -v) VERBOSE=1; shift ;;
 130  	-h | --help) usage 0 ;;
 131  	*) echo "unknown argument: $1" >&2; usage 1 ;;
 132  	esac
 133  done
 134  
 135  case "${PHASES:-all}" in
 136  all | "") PHASE_LIST=("${ALL_PHASES[@]}") ;;
 137  *) IFS=',' read -r -a PHASE_LIST <<<"$PHASES" ;;
 138  esac
 139  for p in "${PHASE_LIST[@]}"; do
 140  	if [ -z "${PHASE_SCRIPT[$p]:-}" ] && [ -z "${PHASE_MONO[$p]:-}" ]; then
 141  		echo "unknown phase: $p (have: ${ALL_PHASES[*]})" >&2
 142  		exit 1
 143  	fi
 144  done
 145  
 146  mkdir -p "$RUNDIR"
 147  
 148  # One run at a time. Two runs share tmp/unitrun: the second one deletes the
 149  # first's per-unit state and work dirs and launches the same units again, so
 150  # each becomes the other's stray writer - measured as three bogus failures
 151  # ("cannot create .../status: No such file or directory") in a run whose units
 152  # had all passed. The lock is the whole fix: refuse, name the holder, and let
 153  # the operator choose between --wait and a different --phases.
 154  exec 9>"$RUNDIR/lock"
 155  if ! flock -n 9; then
 156  	holder="$(cat "$RUNDIR/lock.owner" 2>/dev/null || echo "another run")"
 157  	if [ "$WAIT_LOCK" = 1 ]; then
 158  		echo "unitrun: waiting for $holder to finish"
 159  		flock 9
 160  	else
 161  		echo "unitrun: $holder is already running (results would collide);" >&2
 162  		echo "         wait for it, or re-run with --wait" >&2
 163  		exit 1
 164  	fi
 165  fi
 166  printf '%s (pid %s, started %s)\n' "$(basename "$0")" "$$" "$(date '+%H:%M:%S')" >"$RUNDIR/lock.owner"
 167  trap 'rm -f "$RUNDIR/lock.owner"' EXIT
 168  
 169  state_dir() { echo "$RUNDIR/$1" | tr -c 'A-Za-z0-9_./-' '_'; }
 170  unit_rc() { cat "$(state_dir "$1")/rc" 2>/dev/null || echo ""; }
 171  
 172  # The verdict, not the exit status, decides pass/skip: the phase script writes it
 173  # the moment it knows the outcome, while the exit status is whatever the script
 174  # returned after that. A unit killed between the two would otherwise be reported
 175  # by a status that no longer matches what it concluded.
 176  # A verdict is current only for the commit that produced it. Stale verdicts are
 177  # invalidated (treated as never run) so the driver re-runs them instead of
 178  # reporting pass or fail for a tree that no longer exists.
 179  verdict_current() {
 180  	local d
 181  	d="$(state_dir "$1")"
 182  	[ -f "$d/verdict" ] || return 1
 183  	local want have st
 184  	want="$(git -C "$ROOT" rev-parse HEAD 2>/dev/null || true)"
 185  	st="$(git -C "$ROOT" status --porcelain 2>/dev/null | sha256sum | cut -d' ' -f1)"
 186  	want="$want $st"
 187  	have="$(cat "$d/commit" 2>/dev/null || true)"
 188  	[ -n "$want" ] && [ "$want" = "$have" ]
 189  }
 190  
 191  unit_verdict_of() {
 192  	if [ -f "$(state_dir "$1")/verdict" ] && ! verdict_current "$1"; then
 193  		echo ""
 194  		return
 195  	fi
 196  	local v
 197  	v="$(cat "$(state_dir "$1")/verdict" 2>/dev/null || echo "")"
 198  	case "$v" in
 199  	PASS) echo 0 ;;
 200  	SKIP) echo 2 ;;
 201  	"") echo "" ;;
 202  	*) echo 1 ;;
 203  	esac
 204  }
 205  
 206  if [ ! -x "$MOXIE" ]; then
 207  	echo "FATAL: $MOXIE not found or not executable" >&2
 208  	exit 1
 209  fi
 210  
 211  # A cold run is a different measurement from a warm one: it is the only way to
 212  # see what a fresh checkout costs, and it must not run beside a warm run that is
 213  # repopulating the very cache it just emptied. Hence -j 1 (set with the flag)
 214  # and one explicit place that does the deletion.
 215  if [ "$COLD" = 1 ]; then
 216  	MOXIEPATH="${MOXIEPATH:-$HOME/moxie}"
 217  	echo "cold run: removing $MOXIEPATH/cache"
 218  	rm -rf "$MOXIEPATH/cache"
 219  fi
 220  
 221  # phase_run PHASE ARGS...: run a phase script through the shared environment.
 222  # MOXIE and MOXIEROOT are pinned by the driver; TMPROOT is inside the tree so a
 223  # unit's scratch survives a /tmp wipe long enough to be read after a failure.
 224  phase_run() {
 225  	local p="$1"
 226  	shift
 227  	if [ -n "${PHASE_MONO[$p]:-}" ]; then
 228  		MOXIE="$MOXIE" MOXIEROOT="$ROOT" TMPROOT="$WORKROOT/list-$p-$$" \
 229  			STATUSFILE="$RUNDIR/status-$p-$$" bash "$ROOT/tests/${PHASE_MONO[$p]}" "$@"
 230  	else
 231  		MOXIE="$MOXIE" MOXIEROOT="$ROOT" TMPROOT="$WORKROOT/list-$p-$$" \
 232  			STATUSFILE="$RUNDIR/status-$p-$$" bash "$ROOT/tests/${PHASE_SCRIPT[$p]}" "$@"
 233  	fi
 234  }
 235  
 236  # ------------------------------------------------------------------ unit list
 237  UNITS=()
 238  UNIT_INDEX=()
 239  PHASE_OF=()
 240  for p in "${PHASE_LIST[@]}"; do
 241  	idx=0
 242  	if [ -n "${PHASE_MONO[$p]:-}" ]; then
 243  		UNITS+=("$p/whole-phase")
 244  		UNIT_INDEX+=("")
 245  		PHASE_OF+=("$p")
 246  		continue
 247  	fi
 248  	while IFS= read -r u; do
 249  		[ -n "$u" ] || continue
 250  		if [ "$idx" -eq 0 ] && [ "$u" = "whole-phase" ]; then
 251  			# A phase script may declare itself unsplittable.
 252  			UNITS+=("$p/whole-phase")
 253  			UNIT_INDEX+=("")
 254  			PHASE_OF+=("$p")
 255  			break
 256  		fi
 257  		UNITS+=("$p/$u")
 258  		UNIT_INDEX+=("$idx")
 259  		PHASE_OF+=("$p")
 260  		idx=$((idx + 1))
 261  	done < <(phase_run "$p" --list)
 262  	# A phase that can split itself must actually declare its units. Measured:
 263  	# phase 1b listed nothing, so the driver reported "nothing to do" for it and
 264  	# the 45-package stdlib compile sweep counted as a pass without compiling
 265  	# anything. A phase that names no units is an error, not a skip.
 266  	if [ "$idx" -eq 0 ]; then
 267  		echo "unitrun: phase $p declared no units; it would be reported as passing without running" >&2
 268  		exit 1
 269  	fi
 270  done
 271  rm -f "$RUNDIR"/status-*-$$
 272  
 273  SELECTED=()
 274  for i in "${!UNITS[@]}"; do
 275  	u="${UNITS[$i]}"
 276  	name="${u#*/}"
 277  	if [ -n "$ONLY" ]; then
 278  		case "$name" in
 279  		$ONLY) ;;
 280  		*) continue ;;
 281  		esac
 282  	fi
 283  	if [ -n "$UNITS_LIST" ]; then
 284  		case ",$UNITS_LIST," in
 285  		*",$name,"*) ;;
 286  		*) continue ;;
 287  		esac
 288  	fi
 289  	rc="$(unit_verdict_of "$u")"
 290  	if [ -f "$(state_dir "$u")/verdict" ] && ! verdict_current "$u"; then
 291  		# Stale: never fall back to the old rc file, or a verdict from another
 292  		# tree would decide this run.
 293  		rc=""
 294  	else
 295  		[ -n "$rc" ] || rc="$(unit_rc "$u")"
 296  	fi
 297  	if [ "$FAILED_ONLY" = 1 ]; then
 298  		# A unit never run is work not done, which is what --failed asks to
 299  		# make up, so it counts as failed here.
 300  		if [ -z "$rc" ] || { [ "$rc" != 0 ] && [ "$rc" != 2 ]; }; then
 301  			SELECTED+=("$i")
 302  		fi
 303  		continue
 304  	fi
 305  	if [ "$FORCE" = 0 ] && { [ "$rc" = 0 ] || [ "$rc" = 2 ]; }; then
 306  		continue
 307  	fi
 308  	SELECTED+=("$i")
 309  done
 310  
 311  # Preflight: two traps the 1.0.0 sweep proved cost a whole run. Both are checked
 312  # here, before any unit runs, with a message that names the trap rather than the
 313  # symptom it produces later.
 314  banner="$("$MOXIE" version 2>&1 | head -1)"
 315  case "$banner" in
 316  mxc\ *) ;;
 317  *)
 318  	echo "unitrun: MOXIE ($MOXIE) reports '$banner', which is the legacy bootstrap." >&2
 319  	echo "         ./moxie must be the self-hosted stage4 build (build.sh installs it;" >&2
 320  	echo "         legacy/moxie is only the bootstrap)." >&2
 321  	exit 1
 322  	;;
 323  esac
 324  need_go=0
 325  for i in "${SELECTED[@]}"; do
 326  	case "${UNITS[$i]}" in 6/*) need_go=1 ;; esac
 327  done
 328  if [ "$need_go" = 1 ]; then
 329  	goroot="$(go env GOROOT 2>/dev/null || true)"
 330  	if [ -z "$goroot" ] || [ ! -f "$goroot/src/strings/strings.go" ]; then
 331  		echo "unitrun: phase 6 needs a Go toolchain whose GOROOT has src/strings (got '${goroot:-none}')." >&2
 332  		echo "         export GOROOT=/opt/go1.25.3 PATH=/opt/go1.25.3/bin:\$PATH" >&2
 333  		echo "         Phase 6's regexp reference calls 'go run'; a stale GOROOT reports" >&2
 334  		echo "         'package strings is not in std' and fails a phase the compiler passed." >&2
 335  		exit 1
 336  	fi
 337  fi
 338  
 339  if [ "${#SELECTED[@]}" -eq 0 ]; then
 340  	echo "unitrun: nothing to do (${#UNITS[@]} units, all done; --force to re-run)"
 341  	exit 0
 342  fi
 343  
 344  echo "=== unitrun: ${#SELECTED[@]} of ${#UNITS[@]} units, $JOBS at a time, ${TIMEOUT}s each ==="
 345  echo "binary:  $MOXIE"
 346  echo "results: $RUNDIR"
 347  echo ""
 348  
 349  # ------------------------------------------------------------------ reporting
 350  report_unit() {
 351  	local unit="$1" d rc status reason
 352  	d="$(state_dir "$unit")"
 353  	rc="$(unit_verdict_of "$unit")"
 354  	[ -n "$rc" ] || rc="$(cat "$d/rc" 2>/dev/null || echo 1)"
 355  	case "$rc" in
 356  	0) status=PASS ;;
 357  	2) status=SKIP ;;
 358  	124 | 137) status="TIMEOUT" ;;
 359  	*) status=FAIL ;;
 360  	esac
 361  	if [ -f "$d/verdict" ]; then
 362  		status="$(cat "$d/verdict")"
 363  	fi
 364  	if [ "$rc" = 124 ] || [ "$rc" = 137 ]; then
 365  		# The phase never wrote a verdict: the timeout killed it. Reporting
 366  		# whatever status happens to be there would report a stale result.
 367  		status="TIMEOUT"
 368  	fi
 369  	# Nanosecond stamps: a fast unit is not "0s", and phase 0 is judged by
 370  	# which units are slow, which a whole-second clock cannot show.
 371  	local start end ms
 372  	start="$(cat "$d/start" 2>/dev/null || echo 0)"
 373  	end="$(cat "$d/end" 2>/dev/null || echo 0)"
 374  	ms=$(((end - start) / 1000000))
 375  	reason="$(cat "$d/reason" 2>/dev/null | head -1)"
 376  	if [ -z "$reason" ]; then
 377  		reason="$(grep -m1 -iE 'error|panic|failed|no such|not found|timeout' "$d/err" "$d/out" 2>/dev/null |
 378  			head -1 | cut -c1-200)"
 379  	fi
 380  	[ -n "$reason" ] || reason="(no output)"
 381  	if [ "$status" != PASS ]; then
 382  		# Keep the failing run's own out/err. "Passed on rerun" is a
 383  		# diagnostic that dies with the rerun; the archive keeps the diff
 384  		# that explains a log-order race, and a later rerun cannot overwrite
 385  		# it.
 386  		local arch="$RUNDIR/failures/${unit//\//_}_$(cat "$d/start" 2>/dev/null || echo 0)"
 387  		if [ -d "$d" ] && [ ! -d "$arch" ]; then
 388  			mkdir -p "$RUNDIR/failures"
 389  			cp -a "$d" "$arch"
 390  			printf '  archived: %s\n' "$arch"
 391  		fi
 392  	fi
 393  	if [ "$status" = PASS ]; then
 394  		printf '%-8s %-46s %6sms\n' "$status" "$unit" "$ms"
 395  	else
 396  		printf '%-8s %-46s %6sms  %s\n' "$status" "$unit" "$ms" "$reason"
 397  		sed -n '1,8p' "$d/err" 2>/dev/null | sed 's/^/    | /'
 398  		[ "$VERBOSE" = 1 ] && sed -n '1,20p' "$d/out" 2>/dev/null | sed 's/^/    > /'
 399  	fi
 400  	reason="${reason//$'\t'/ }"
 401  	echo -e "$unit\t$status\t$rc\t$reason" >>"$RUNDIR/summary.tsv.tmp"
 402  }
 403  
 404  # -------------------------------------------------------------------- running
 405  launch_unit() {
 406  	local i="$1" unit="${UNITS[$1]}" phase="${PHASE_OF[$1]}" d rc ut
 407  	d="$(state_dir "$unit")"
 408  	ut="$TIMEOUT"
 409  	if [ "$TIMEOUT_EXPLICIT" = 0 ] && [ -n "${UNIT_TIMEOUT[$unit]:-}" ]; then
 410  		ut="${UNIT_TIMEOUT[$unit]}"
 411  	fi
 412  	# The phase script needs TMPROOT before it can name its unit dir, so the
 413  	# path is derived here once from the unit id and exported.
 414  	UNIT_SAFE="$(echo "$unit" | tr -c 'A-Za-z0-9_.-' '_')"
 415  	# The flock is on fd 9 of the driver. This function runs in a subshell, so
 416  	# it inherits the OPEN file description: without closing it here every unit
 417  	# holds the lock open and a second driver never starts - the run looks like
 418  	# it is working while its state directories are being clobbered.
 419  	exec 9>&-
 420  	rm -rf "$d" "$WORKROOT/$UNIT_SAFE"
 421  	mkdir -p "$d"
 422  	date +%s%N >"$d/start"
 423  	if [ -z "${UNIT_INDEX[$i]}" ]; then
 424  		MOXIE="$MOXIE" MOXIEROOT="$ROOT" TMPROOT="$WORKROOT/$UNIT_SAFE" \
 425  			STATUSFILE="$d/status" \
 426  			timeout -k 30 "$ut" bash "$ROOT/tests/${PHASE_MONO[$phase]}" >"$d/out" 2>"$d/err"
 427  	else
 428  		MOXIE="$MOXIE" MOXIEROOT="$ROOT" TMPROOT="$WORKROOT/$UNIT_SAFE" \
 429  			UNIT_NAME="${unit#*/}" STATUSFILE="$d/status" \
 430  			timeout -k 30 "$ut" bash "$ROOT/tests/${PHASE_SCRIPT[$phase]}" --unit "${UNIT_INDEX[$i]}" \
 431  			>"$d/out" 2>"$d/err"
 432  	fi
 433  	rc=$?
 434  	echo "$rc" >"$d/rc"
 435  	date +%s%N >"$d/end"
 436  	# A unit that failed keeps its work dir: that is where the artifact and the
 437  	# full build log are. A unit that passed keeps only its own small logs, so a
 438  	# full run does not leave a gigabyte of wasm behind.
 439  	if [ "$rc" = 0 ] || [ "$rc" = 2 ]; then
 440  		rm -rf "$WORKROOT/$UNIT_SAFE"
 441  	fi
 442  	touch "$d/done"
 443  }
 444  
 445  rm -f "$RUNDIR/summary.tsv.tmp"
 446  declare -A SLOT_PID=()
 447  declare -A SLOT_SEL=()
 448  next=0
 449  launched=0
 450  
 451  while [ "$next" -lt "${#SELECTED[@]}" ]; do
 452  	if [ "$FAIL_FAST" = 1 ] && [ "$launched" -gt 0 ]; then
 453  		# Fail fast looks at what is still running or has just finished, not
 454  		# at the index order: with -j above 1 the failure is rarely the unit
 455  		# that happens to sit earliest in the list.
 456  		for i in $(seq 0 $((JOBS - 1))); do
 457  			[ -n "${SLOT_PID[$i]:-}" ] || continue
 458  			kill -0 "${SLOT_PID[$i]}" 2>/dev/null && continue
 459  			[ "$(unit_rc "${UNITS[${SLOT_SEL[$i]}]}")" = 1 ] || continue
 460  			echo ""
 461  			echo "fail-fast: ${UNITS[${SLOT_SEL[$i]}]} failed; not launching the remaining $(( ${#SELECTED[@]} - next )) unit(s)"
 462  			break 2
 463  		done
 464  		for sel in "${SELECTED[@]:0:$next}"; do
 465  			[ "$(unit_rc "${UNITS[$sel]}")" = 1 ] || continue
 466  			echo ""
 467  			echo "fail-fast: ${UNITS[$sel]} failed; not launching the remaining $(( ${#SELECTED[@]} - next )) unit(s)"
 468  			break 2
 469  		done
 470  	fi
 471  	for i in $(seq 0 $((JOBS - 1))); do
 472  		[ -n "${SLOT_PID[$i]:-}" ] && continue
 473  		[ "$next" -lt "${#SELECTED[@]}" ] || break
 474  		sel="${SELECTED[$next]}"
 475  		next=$((next + 1))
 476  		launched=$((launched + 1))
 477  		SLOT_SEL[$i]="$sel"
 478  		launch_unit "$sel" &
 479  		SLOT_PID[$i]=$!
 480  	done
 481  	# Block until any unit finishes, then free its slot. Cheaper and far less
 482  	# noisy than polling: a poll loop over 100 units would spend the whole run
 483  	# waking up every 200ms.
 484  	wait -n 2>/dev/null
 485  	for i in $(seq 0 $((JOBS - 1))); do
 486  		[ -n "${SLOT_PID[$i]:-}" ] || continue
 487  		if ! kill -0 "${SLOT_PID[$i]}" 2>/dev/null; then
 488  			wait "${SLOT_PID[$i]}" 2>/dev/null
 489  			unset "SLOT_PID[$i]"
 490  		fi
 491  	done
 492  done
 493  for i in $(seq 0 $((JOBS - 1))); do
 494  	[ -n "${SLOT_PID[$i]:-}" ] || continue
 495  	wait "${SLOT_PID[$i]}" 2>/dev/null
 496  done
 497  
 498  for sel in "${SELECTED[@]}"; do
 499  	report_unit "${UNITS[$sel]}"
 500  done
 501  
 502  mv "$RUNDIR/summary.tsv.tmp" "$RUNDIR/summary.tsv" 2>/dev/null
 503  
 504  # -------------------------------------------------------------------- summary
 505  total=0
 506  passed=0
 507  failed=0
 508  skipped=0
 509  # Only the units this run selected count towards the exit code. A unit filtered
 510  # out by --units or --only keeps whatever verdict it had from an earlier run, and
 511  # counting those made a targeted run report - and fail on - work it never ran
 512  # (a stale FAIL from a killed sweep turned a 13-fixture quickcheck red).
 513  for sel in "${SELECTED[@]}"; do
 514  	unit="${UNITS[$sel]}"
 515  	rc="$(unit_verdict_of "$unit")"
 516  	[ -n "$rc" ] || rc="$(unit_rc "$unit")"
 517  	[ -n "$rc" ] || continue
 518  	total=$((total + 1))
 519  	case "$rc" in
 520  	0) passed=$((passed + 1)) ;;
 521  	2) skipped=$((skipped + 1)) ;;
 522  	*) failed=$((failed + 1)) ;;
 523  	esac
 524  done
 525  
 526  # ── tree cleanliness ────────────────────────────────────────────────────────
 527  # The suite builds into $TMPROOT (outside the tree) and into the tree's own tmp/
 528  # and cache/, which are gitignored. Anything a gate leaves in a TRACKED path is
 529  # drift: a phase that writes into a source tree and gets "restored afterwards"
 530  # is hiding it rather than preventing it. Measure it, do not promise it.
 531  #
 532  # Only the suite's own checkout is judged: the UI app is a SEPARATE repo beside
 533  # it, and its arrangement (checked-in build products under web/static) is its
 534  # own business, not this gate's.
 535  DIRTY="$(git -C "$ROOT" status --porcelain 2>/dev/null | head -20)"
 536  if [ -n "$DIRTY" ]; then
 537  	echo ""
 538  	echo "FAIL      tree-cleanliness"
 539  	printf '  the suite left tracked files modified in %s:\n' "$ROOT"
 540  	printf '%s\n' "$DIRTY" | sed 's/^/    /'
 541  	failed=$((failed + 1))
 542  	total=$((total + 1))
 543  else
 544  	echo "PASS      tree-cleanliness"
 545  	passed=$((passed + 1))
 546  	total=$((total + 1))
 547  fi
 548  
 549  echo ""
 550  echo "=== Summary ==="
 551  printf 'total: %d  passed: %d  failed: %d  skipped: %d\n' "$total" "$passed" "$failed" "$skipped"
 552  if [ "$failed" -gt 0 ]; then
 553  	echo ""
 554  	echo "failed units (re-run with: tests/unitrun.sh --phases ${PHASE_LIST[*]} --failed):"
 555  	for unit in "${UNITS[@]}"; do
 556  		rc="$(unit_verdict_of "$unit")"
 557  		[ -n "$rc" ] || rc="$(unit_rc "$unit")"
 558  		[ "$rc" = 1 ] || [ "$rc" = 124 ] || [ "$rc" = 137 ] || continue
 559  		printf '  %s  %s\n' "$unit" "$(sed -n '1p' "$(state_dir "$unit")/err" 2>/dev/null | cut -c1-120)"
 560  	done
 561  fi
 562  [ "$failed" -gt 0 ] && exit 1
 563  exit 0
 564