Files
reims-vgpu/scripts/visual-gate/visual-gate.sh
T
Anees IqbalandClaude 9e3969b0ef ✨ The probes that could have caught the reset now gate the commit that would cause it
Performance work on this device has been verified against clippy, unit tests,
the feature matrix and device-side counters. None of those can see a rendering
regression. A branch of 59 commits — 21 code-changing, +7687/-820 across 38
files — was reset off wholesale because it had introduced graphical glitches,
and every one of those commits had been verified. The instruments were
performance instruments because the work was performance work, so the defect was
invisible by construction. Nothing lied; the right question was never asked.

Three probes already ask it. Each is a two-observation instrument — the guest
declares what it believes it drew and where, the host measures its own capture
at exactly those places — which is what separates "the guest declined to draw
it" from "this device lost it". None of them gated anything. That is the defect
this fixes.

`scripts/visual-gate/visual-gate.sh` is one entry point:

- It refuses to start on an unsettled guest. sshd answers well before the
  desktop composites, and one probe run in that window read 12.2 fps with the
  device idle at `duty=0.001`. It polls `uptime` for a 1-minute load average
  under SETTLE_LOAD (1.0) up to SETTLE_TIMEOUT (420 s), and aborts if QEMU stops
  matching, so a boot that died early cannot read as a clean run.
- It runs all three probes even when an earlier one fails. Which probe fails is
  the diagnostic.
- It marks the fail log by byte offset before the first probe and applies the
  six silent-loss classes over its own window only, never the accumulated log,
  which spans builds. Byte offset rather than timestamp because the device's
  `t=` clock does not correlate with the shell's — the same bound
  `window-drag-probe` uses.
- One verdict line; exit 0 clean, 1 regression, 2 setup failure.

`--quick` cuts the trials to 5/3/3 for iteration; the README says a green
`--quick` is not a phase sign-off, and that a change whose failure mode needs a
second frame to appear wants the full gate run twice on one boot.

The counter parsing is its own script so it can be tested without a boot. That
split is the point: a parser that matches nothing prints the same six zeros a
clean run does, so a green gate would mean nothing and its output could not say
which. AGENTS.md carries the rule itself, next to the section on what a
screenshot cannot say.

Component: `scripts/`, x86/PCI and arm64 pathways alike; no Rust and no device
behavior.

Tests: `scripts/visual-gate/self-test.sh`, 10 cases over synthetic log text
quoted from the emitters, no guest or GPU needed. Mutation-checked twice:
dropping the `^` anchor on the line families fails exactly the
"matches its own prefix only" case, and matching route fields by prefix instead
of exactly fails exactly the "a longer field name is not this class" case.
`bash -n` clean on all three scripts. The gate's own refusal path was exercised
with no QEMU running and reported it.

Not verified: no live boot yet, so the gate has never run its three probes
end to end. That is the next step and it is what establishes the green baseline
at this HEAD before any Phase A change.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-03 03:08:39 +03:00

188 lines
7.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# visual-gate.sh — did this build lose any pixels?
#
# Performance work on this device has been verified against clippy, unit tests,
# the feature matrix and device-side counters. None of those can see a rendering
# regression, so a branch of performance commits was once reset off wholesale
# after the glitches showed up later: every commit had been verified, and the
# question was never asked.
#
# Three probes already answer it, each of them a two-observation instrument —
# the guest declares what it believes it drew, the host measures the frame at
# exactly those places. That is what separates "the guest declined to draw it"
# from "this device lost it", which is the distinction a screenshot cannot make.
# What was missing is that none of them gated anything.
#
# This is the one entry point. It runs all three, applies a counter budget over
# the six silent-loss classes for its own window of the fail log, and exits
# non-zero if any part failed.
#
# scripts/visual-gate/visual-gate.sh [--quick] [--keep DIR] [--host GUEST]
#
# Exits 0 when every probe passed and every silent-loss counter read zero, 1 on
# any regression, 2 on a setup failure — a guest that never settled, a probe
# that could not run, or a QEMU that died under it.
#
# A green `--quick` is not a phase sign-off. See the README.
set -euo pipefail
export LC_ALL=C
QUICK=0
KEEP=""
GUEST="${GUEST:-macos-vm}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
FAILLOG="${REIMS_VGPU_FAIL_LOG:-/tmp/reims-vgpu-fail.log}"
# A probe on an unsettled guest measures the guest's own startup work. One such
# run read 12.2 fps with the device idle at `duty=0.001` — the desktop was not
# compositing yet. sshd answers well before that, so the port being open is not
# the signal; the 1-minute load average is.
SETTLE_LOAD="${SETTLE_LOAD:-1.0}"
SETTLE_TIMEOUT="${SETTLE_TIMEOUT:-420}"
while [ $# -gt 0 ]; do
case "$1" in
--quick) QUICK=1; shift ;;
--keep) KEEP="$2"; shift 2 ;;
--host) GUEST="$2"; shift 2 ;;
-h|--help) sed -n '2,25p' "${BASH_SOURCE[0]}" | sed 's/^# \?//'; exit 0 ;;
*) echo "visual-gate: unknown argument $1" >&2; exit 2 ;;
esac
done
export GUEST
WORK="${KEEP:-$(mktemp -d)}"
mkdir -p "$WORK"
[ -n "$KEEP" ] || trap 'rm -rf "$WORK"' EXIT
say() { echo "visual-gate: $*"; }
if [ "$QUICK" = 1 ]; then
WEB_N=5; WALL_N=3; MODAL_N=3; MODE=quick
else
WEB_N=20; WALL_N=10; MODAL_N=10; MODE=full
fi
# ---------------------------------------------------------------- settle wait
qemu_alive() { pgrep -f 'qemu-system-x86_6[4]' >/dev/null 2>&1; }
qemu_alive || { say "no QEMU running — boot the pathway first" >&2; exit 2; }
say "waiting for the guest to settle (1-minute load under $SETTLE_LOAD, up to ${SETTLE_TIMEOUT}s)"
settled=0
deadline=$((SECONDS + SETTLE_TIMEOUT))
while [ "$SECONDS" -lt "$deadline" ]; do
qemu_alive || { say "QEMU exited while waiting for the guest" >&2; exit 2; }
# macOS `uptime` ends with "load averages: 1.23 1.45 1.67"; the 1-minute
# figure is the first of the three, which is $(NF-2).
load=$(ssh -o ConnectTimeout=8 -o BatchMode=yes "$GUEST" uptime 2>/dev/null \
| awk '{print $(NF-2)}' | tr -d ',')
if [ -n "$load" ] && awk -v l="$load" -v t="$SETTLE_LOAD" 'BEGIN{exit !(l+0 < t+0)}'; then
say "guest settled at load $load"
settled=1
break
fi
sleep 10
done
[ "$settled" = 1 ] || { say "guest never settled (last load '${load:-unreachable}')" >&2; exit 2; }
# The power state is not a verdict input — the gate answers correct-or-not, and
# frame rate is bimodal here regardless. It is recorded because a log read later
# beside a performance number needs to say which state it was taken in.
if command -v nvidia-smi >/dev/null 2>&1; then
say "host GPU $(nvidia-smi --query-gpu=clocks.sm,clocks.max.sm,utilization.gpu,pstate \
--format=csv,noheader 2>/dev/null | head -1)"
fi
# ------------------------------------------------------------------- probes
# The fail log is bounded by byte offset rather than by timestamp: the device's
# own `t=` clock does not correlate with this shell's, and the accumulated log
# spans builds. Marked before the first probe so the window is exactly the
# frames this gate drove.
OFF=0
[ -f "$FAILLOG" ] && OFF=$(stat -c %s "$FAILLOG")
# Every probe runs even when an earlier one fails: which probe fails is the
# diagnostic. `set -e` is off for exactly these three lines.
run_probe() {
local name="$1" log="$WORK/$name.log"; shift
say "running $name"
set +e
"$@" >"$log" 2>&1
local rc=$?
set -e
# The probes' own verdict is their last non-empty line.
local verdict
verdict=$(grep -v '^[[:space:]]*$' "$log" | tail -1)
printf '%s\t%s\t%s\n' "$name" "$rc" "$verdict" >>"$WORK/verdicts"
say " $name exit=$rc: $verdict"
}
: >"$WORK/verdicts"
# --keep is passed down only when this gate was asked to keep, so a failing run
# leaves every probe's frames and decode records beside its own log.
keep_arg() { [ -n "$KEEP" ] && printf -- '--keep\n%s\n' "$WORK/$1"; }
mapfile -t k < <(keep_arg web-content)
run_probe web-content "$REPO_ROOT/scripts/web-content-probe/web-content-probe.sh" \
-n "$WEB_N" "${k[@]}"
mapfile -t k < <(keep_arg wallpaper)
run_probe wallpaper "$REPO_ROOT/scripts/wallpaper-probe/wallpaper-probe.sh" \
-n "$WALL_N" "${k[@]}"
mapfile -t k < <(keep_arg modal-button)
run_probe modal-button "$REPO_ROOT/scripts/modal-button-probe/modal-button-probe.sh" \
-n "$MODAL_N" --host "$GUEST" "${k[@]}"
qemu_alive || { say "QEMU exited during the probes — no verdict" >&2; exit 2; }
# ------------------------------------------------------------ counter budget
WINDOW="$WORK/window.log"
if [ -f "$FAILLOG" ]; then
tail -c "+$((OFF + 1))" "$FAILLOG" >"$WINDOW"
else
: >"$WINDOW"
fi
# The six silent-loss classes. Any non-zero reading over the gate's own window
# is a gate failure, not a note. Two of them are standing alarms that read zero
# across the whole accumulated log to date — a zero is the working state, and a
# first occurrence is the result.
#
# The parsing lives in its own script so `self-test.sh` can exercise it against
# synthetic log text without a boot: a parser that matches nothing prints the
# same six zeros a clean run does.
set +e
"$SCRIPT_DIR/counter-budget.sh" "$WINDOW" >"$WORK/counters"
budget_rc=$?
set -e
[ "$budget_rc" -le 1 ] || { say "counter budget could not read its window" >&2; exit 2; }
# --------------------------------------------------------------- the verdict
probe_fails=$(awk -F'\t' '$2 == 1' "$WORK/verdicts" | wc -l)
probe_setup=$(awk -F'\t' '$2 != 0 && $2 != 1' "$WORK/verdicts" | wc -l)
hot=$(awk -F'\t' '$2 > 0 {printf "%s=%s ", $1, $2}' "$WORK/counters")
hot=${hot% }
bytes=$(stat -c %s "$WINDOW")
summary="$MODE mode, $(awk -F'\t' '$2 == 0' "$WORK/verdicts" | wc -l)/3 probes green"
summary="$summary, counters over ${bytes} B of fail log: ${hot:-all zero}"
if [ "$probe_setup" -gt 0 ]; then
say "SETUP-FAILED — $summary"
awk -F'\t' '$2 != 0 && $2 != 1 {printf "visual-gate: %s exit=%s: %s\n", $1, $2, $3}' "$WORK/verdicts"
exit 2
fi
if [ "$probe_fails" -gt 0 ] || [ -n "$hot" ]; then
say "FAIL — $summary"
awk -F'\t' '$2 == 1 {printf "visual-gate: %s: %s\n", $1, $3}' "$WORK/verdicts"
exit 1
fi
say "PASS — $summary"
exit 0