From 100b0c9663b4c92264d8472a0573fb644c439262 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Tue, 17 Nov 2026 22:47:11 +0100 Subject: [PATCH] Add selective push gate and wire it Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WzxdWZB8QEaFVQoa8CjuAw --- scripts/check_gate_stamp.sh | 12 ++++ scripts/gate_changed_coverage.py | 53 ++++++++++++++++++ scripts/push_gate.sh | 96 ++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 scripts/gate_changed_coverage.py create mode 100755 scripts/push_gate.sh diff --git a/scripts/check_gate_stamp.sh b/scripts/check_gate_stamp.sh index b2e7c34..2b9fe26 100755 --- a/scripts/check_gate_stamp.sh +++ b/scripts/check_gate_stamp.sh @@ -24,6 +24,18 @@ main() { exit 1 fi + # A stamped tree (full gate green) passes in ~1 s. An unstamped tree gets + # the selective push gate: pre-commit + only the tests the change can + # affect, <=60 s, and it says which ran and why (scripts/push_gate.sh). + if [[ "${1:-}" == check ]]; then + local here + here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + if bash "$SHARED_GATE" check 2>/dev/null; then + exit 0 + fi + echo "gate-stamp: tree not fully stamped; running the selective push gate" >&2 + exec bash "$here/push_gate.sh" + fi exec bash "$SHARED_GATE" "$@" } diff --git a/scripts/gate_changed_coverage.py b/scripts/gate_changed_coverage.py new file mode 100644 index 0000000..7dc40f4 --- /dev/null +++ b/scripts/gate_changed_coverage.py @@ -0,0 +1,53 @@ +"""100% line bar for CHANGED dart files, from the lcov of the selected tests. + +Usage: gate_changed_coverage.py LCOV CHANGED_FILE... +Paths in CHANGED_FILE are repo-relative; only lib/ files of the app count. +A changed lib file missing from the lcov fails (a file no selected test +loads would otherwise score 100% by absence). +""" + +from __future__ import annotations + +from pathlib import Path +import sys + +APP_LIB = "stronglift_replacement/workout_app/lib/" + + +def uncovered(lcov: str, wanted: set[str]) -> dict[str, list[int] | None]: + """Map wanted lib file -> uncovered line numbers (None = absent from lcov).""" + result: dict[str, list[int] | None] = dict.fromkeys(wanted) + current: str | None = None + for line in lcov.splitlines(): + if line.startswith("SF:"): + current = f"{APP_LIB}{line[3:].removeprefix('lib/')}" + if current in wanted: + result[current] = result[current] or [] + else: + current = None + elif line.startswith("DA:") and current is not None: + num, hits = line[3:].split(",")[:2] + if int(hits) == 0: + (result[current] or []).append(int(num)) + return result + + +def main(argv: list[str]) -> int: + """Exit 1 listing every changed lib file that is not fully covered.""" + wanted = {f for f in argv[2:] if f.startswith(APP_LIB) and f.endswith(".dart")} + bad = 0 + for path, lines in sorted( + uncovered(Path(argv[1]).read_text(encoding="utf-8"), wanted).items() + ): + if lines is None: + print(f"NOT COVERED (no selected test loads it): {path}", file=sys.stderr) + bad += 1 + elif lines: + print(f"UNCOVERED LINES {path}: {lines}", file=sys.stderr) + bad += 1 + print(f"changed-file coverage: {len(wanted)} files, {bad} below 100%") + return 1 if bad else 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv)) diff --git a/scripts/push_gate.sh b/scripts/push_gate.sh new file mode 100755 index 0000000..5820d6c --- /dev/null +++ b/scripts/push_gate.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# ============================================================================ +# pre-push gate for an UN-stamped tree: pre-commit plus ONLY the tests the +# change since the last fully green run can affect (scripts/gate_select.py), +# with the 100% line bar on the changed dart files. Budget: 60 s wall-clock. +# +# Never narrows silently: it prints which tests ran and why, and refuses +# (exit 1) when the selection is the full suite or exceeds the budget -- those +# belong to scripts/full_gate.sh, which stamps the tree. +# +# push_gate.sh [BASE] BASE defaults to the last full-green commit +# ($GIT_DIR/gate-full-green), else origin/main +# ============================================================================ + +set -euo pipefail + +readonly APP="stronglift_replacement/workout_app" +readonly MAX_DART_TESTS="${GATE_MAX_DART_TESTS:-30}" +readonly FLUTTER_CONCURRENCY="${FLUTTER_TEST_CONCURRENCY:-2}" + +die() { + echo "push-gate: $*" >&2 + exit 1 +} + +cd "$(git rev-parse --show-toplevel)" +gitdir="$(git rev-parse --git-dir)" + +# A red nightly/full run refuses every push until a full run is green again. +if [[ "$(cat "$gitdir/gate-full-status" 2>/dev/null || true)" == red* ]]; then + die "RED FULL STAMP ($(cat "$gitdir/gate-full-status")). The last full suite failed; fix it and rerun scripts/full_gate.sh -- no push until it is green." +fi + +base="${1:-}" +[[ -n "$base" ]] || base="$(cat "$gitdir/gate-full-green" 2>/dev/null || true)" +[[ -n "$base" ]] || base="$(git merge-base origin/main HEAD 2>/dev/null || true)" +[[ -n "$base" ]] || die "no base (no full-green commit, no origin/main): run scripts/full_gate.sh" +git rev-parse --verify --quiet "$base^{commit}" >/dev/null || die "base $base is unknown: run scripts/full_gate.sh" + +sel="$(python3 scripts/gate_select.py "$base" HEAD)" +field() { jq -r "$1" <<<"$sel"; } +echo "push-gate: base ${base:0:10}..HEAD $(git rev-parse --short HEAD)" +field '.why[] | "push-gate: " + .' + +if [[ "$(field .full_dart)" == true || "$(field .full_python)" == true ]]; then + die "the change needs the FULL suite (see above). Run scripts/full_gate.sh, then push." +fi +mapfile -t dart < <(field '.dart[]') +mapfile -t py < <(field '.python[]') +(( ${#dart[@]} <= MAX_DART_TESTS )) \ + || die "${#dart[@]} Dart tests selected, over the ${MAX_DART_TESTS}-test push budget. Run scripts/full_gate.sh." + +changed=() +mapfile -t changed < <(git diff --name-only "$base"..HEAD) +pids=() names=() +run_bg() { # name, command... + local name="$1" log + shift + log="$(mktemp)" + ( "$@" >"$log" 2>&1 ) & + pids+=("$!") names+=("$name:$log") +} + +run_bg pre-commit pre-commit run --from-ref "$base" --to-ref HEAD +if (( ${#dart[@]} > 0 )); then + rel=("${dart[@]#"$APP"/}") + echo "push-gate: Dart ran: ${#dart[@]} tests" + run_bg dart bash -c "cd '$APP' && env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILE \ + flutter test --coverage --concurrency '$FLUTTER_CONCURRENCY' ${rel[*]} \ + && cd - >/dev/null && python3 scripts/gate_changed_coverage.py '$APP/coverage/lcov.info' ${changed[*]}" +fi +if (( ${#py[@]} > 0 )); then + echo "push-gate: Python ran: ${#py[@]} test files" + run_bg python python3 -m pytest -o addopts="--strict-markers --strict-config -p screen_locker.tests.test_sync_fixtures" \ + -p no:sugar --no-cov -q --tb=short "${py[@]}" +fi +if [[ "$(field .web)" == true ]]; then + echo "push-gate: web ran: vitest coverage" + run_bg web pnpm --dir web run coverage +fi + +fail=0 +for i in "${!pids[@]}"; do + name="${names[$i]%%:*}" log="${names[$i]#*:}" + if wait "${pids[$i]}"; then + echo "push-gate: $name OK" + else + echo "push-gate: $name FAILED" >&2 + tail -25 "$log" >&2 + fail=1 + fi + rm -f "$log" +done +(( fail == 0 )) || die "failed (selection above); fix it, or run scripts/full_gate.sh" +echo "push-gate: OK (selective; the full suite is the nightly's job)"