|
| 1 | +#!/usr/bin/env bash |
| 2 | +# usage: scripts/agent/gates.sh [--dry-run] <worktree> <outdir> |
| 3 | +# |
| 4 | +# Runs the local gates that Rust CI runs, from <worktree>/src-rust: |
| 5 | +# fmt cargo fmt --all -- --check |
| 6 | +# check cargo check --workspace --locked |
| 7 | +# clippy cargo clippy --workspace --all-targets --locked -- -D warnings |
| 8 | +# clippy-computer-use the same for claurst-tools with --features computer-use |
| 9 | +# (macOS only, as in CI; its dependencies need extra |
| 10 | +# libraries elsewhere) |
| 11 | +# test cargo test --workspace --locked --no-fail-fast |
| 12 | +# alloc the next_frame_perf allocation gate, serial |
| 13 | +# Each gate's output goes to <outdir>/<gate>.log. <outdir>/summary.txt gets one |
| 14 | +# line per gate ("<gate> rc=<n> <seconds>s", or "<gate> skipped") and the |
| 15 | +# workspace test counts; |
| 16 | +# <outdir>/identity.txt records the head, tree, a fingerprint of the changes |
| 17 | +# and untracked files, and the dirty files tested. |
| 18 | +# --dry-run prints the commands and writes nothing. |
| 19 | +# <outdir> must be new, empty, or an earlier gates outdir (one holding |
| 20 | +# identity.txt), since a run replaces the logs it names. |
| 21 | +# |
| 22 | +# Environment (default): |
| 23 | +# COVEN_TOOLCHAIN toolchain for `rustup run` (the dtolnay/rust-toolchain pin |
| 24 | +# in <worktree>/.github/workflows/rust-ci.yml) |
| 25 | +# CARGO_TARGET_DIR build directory (<worktree>/src-rust/target). Never share |
| 26 | +# one between worktrees. |
| 27 | +# CARGO_BUILD_JOBS passed to cargo when set; set it on a shared host |
| 28 | +# RUSTUP_HOME, CARGO_HOME ($HOME/.rustup, $HOME/.cargo) kept for the test |
| 29 | +# gates, which run with a scratch HOME under <outdir> |
| 30 | +# RUSTUP rustup binary (rustup on PATH, else $CARGO_HOME/bin/rustup) |
| 31 | +# GATES_ONLY_TESTS=1 run only the test and alloc gates; the result line |
| 32 | +# then names the gates skipped |
| 33 | +# GATES_COMPUTER_USE 1 or 0 to force the computer-use clippy gate on or off |
| 34 | +# (on when uname -s is Darwin) |
| 35 | +# |
| 36 | +# Not run here: the headless-schemas job (release version preflight and the |
| 37 | +# headless JSON Schema fixtures), the installer-checksums jobs, the macOS |
| 38 | +# job's computer_use::tests and workspace_policy steps, the Windows job, the |
| 39 | +# contract fixtures by name, and the Coven engine contract. Hosted CI runs |
| 40 | +# those, so a local pass covers only the gates listed above. |
| 41 | +# |
| 42 | +# Exit status: 0 when every gate passed, 1 when any failed, 2 on usage errors, |
| 43 | +# 3 when the gates passed but the worktree's head or dirty files changed |
| 44 | +# during the run, so the pass does not describe one tree. |
| 45 | +set -u |
| 46 | + |
| 47 | +usage() { echo "usage: $0 [--dry-run] <worktree> <outdir>" >&2; exit 2; } |
| 48 | + |
| 49 | +DRY_RUN=0 |
| 50 | +if [ "${1:-}" = "--dry-run" ]; then DRY_RUN=1; shift; fi |
| 51 | +[ $# -eq 2 ] || usage |
| 52 | +for arg in "$1" "$2"; do case "$arg" in -*) usage ;; esac; done |
| 53 | + |
| 54 | +WORKTREE="$(cd "$1" 2>/dev/null && pwd -P)" || { echo "gates: no such worktree: $1" >&2; exit 2; } |
| 55 | +[ -d "$WORKTREE/src-rust" ] || { echo "gates: $WORKTREE has no src-rust directory" >&2; exit 2; } |
| 56 | +case "$2" in /*) OUT="$2" ;; *) OUT="$PWD/$2" ;; esac |
| 57 | +# Resolve symlinks and .. in the existing part of the path. |
| 58 | +OUT_PARENT="$(dirname "$OUT")" |
| 59 | +if [ -d "$OUT_PARENT" ]; then |
| 60 | + OUT="$(cd "$OUT_PARENT" && pwd -P)/$(basename "$OUT")" |
| 61 | +else |
| 62 | + case "/$OUT/" in */../*|*/./*) echo "gates: give <outdir> without . or .. when its parent does not exist" >&2; exit 2 ;; esac |
| 63 | +fi |
| 64 | +# Inside the worktree the logs would show up in identity.txt's git status. |
| 65 | +case "$OUT/" in "$WORKTREE"/*) echo "gates: put <outdir> outside the worktree" >&2; exit 2 ;; esac |
| 66 | +# A run removes and rewrites the logs it names; never do that to an unrelated |
| 67 | +# directory that happens to hold a test.log. |
| 68 | +if [ -e "$OUT" ] && [ ! -e "$OUT/identity.txt" ] && [ -n "$(ls -A "$OUT" 2>/dev/null || echo x)" ]; then |
| 69 | + echo "gates: <outdir> exists and is not empty or an earlier gates outdir; give a new one" >&2 |
| 70 | + exit 2 |
| 71 | +fi |
| 72 | + |
| 73 | +TOOLCHAIN="${COVEN_TOOLCHAIN:-}" |
| 74 | +if [ -z "$TOOLCHAIN" ]; then |
| 75 | + TOOLCHAIN="$(grep -m1 -oE 'dtolnay/rust-toolchain@[0-9][0-9.]*' \ |
| 76 | + "$WORKTREE/.github/workflows/rust-ci.yml" 2>/dev/null | cut -d@ -f2)" |
| 77 | +fi |
| 78 | +[ -n "$TOOLCHAIN" ] || { echo "gates: no toolchain pin found in rust-ci.yml; set COVEN_TOOLCHAIN" >&2; exit 2; } |
| 79 | + |
| 80 | +export RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}" |
| 81 | +export CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}" |
| 82 | +export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$WORKTREE/src-rust/target}" |
| 83 | +# CI sets none of these. A caller's RUSTFLAGS=--cap-lints=allow or |
| 84 | +# CARGO_PROFILE_TEST_DEBUG_ASSERTIONS=false would let a gate pass that fails |
| 85 | +# in CI, and a Coven agent shell's COVEN_HOME would point the tests at a real |
| 86 | +# home instead of the scratch one. A CARGO_TARGET_<triple>_RUNNER of `true` |
| 87 | +# would skip every test, and XDG_* would send test caches to the caller's real |
| 88 | +# directories on Linux. RUSTC_BOOTSTRAP=1 would let #![feature] code build on |
| 89 | +# the stable pin, and a workspace wrapper could rewrite every rustc call. |
| 90 | +# Settings in $CARGO_HOME/config.toml, and in a .cargo/config.toml in any |
| 91 | +# directory above the worktree, still apply. (COVEN_TOOLCHAIN was read above.) |
| 92 | +unset RUSTFLAGS CARGO_ENCODED_RUSTFLAGS RUSTDOCFLAGS CARGO_ENCODED_RUSTDOCFLAGS \ |
| 93 | + CARGO_BUILD_RUSTFLAGS CARGO_BUILD_RUSTDOCFLAGS CARGO_BUILD_TARGET RUSTC CLIPPY_CONF_DIR \ |
| 94 | + RUSTC_BOOTSTRAP RUSTC_WORKSPACE_WRAPPER CARGO_BUILD_RUSTC_WORKSPACE_WRAPPER |
| 95 | +for var in $(compgen -e); do |
| 96 | + case "$var" in |
| 97 | + CARGO_TARGET_*_RUSTFLAGS | CARGO_TARGET_*_RUNNER | CARGO_PROFILE_* | XDG_* | \ |
| 98 | + COVEN_* | CLAURST_* | ANTHROPIC_* | OPENAI_* | CODEX_*) |
| 99 | + unset "$var" ;; |
| 100 | + esac |
| 101 | +done |
| 102 | +# Colour codes would break the test-count summary. |
| 103 | +export CARGO_TERM_COLOR=never |
| 104 | +COMPUTER_USE="${GATES_COMPUTER_USE:-}" |
| 105 | +case "$COMPUTER_USE" in ""|0|1) ;; *) echo "gates: GATES_COMPUTER_USE must be 0 or 1" >&2; exit 2 ;; esac |
| 106 | +if [ -z "$COMPUTER_USE" ]; then |
| 107 | + if [ "$(uname -s)" = Darwin ]; then COMPUTER_USE=1; else COMPUTER_USE=0; fi |
| 108 | +fi |
| 109 | +RUSTUP="${RUSTUP:-$(command -v rustup || echo "$CARGO_HOME/bin/rustup")}" |
| 110 | +CARGO=("$RUSTUP" run "$TOOLCHAIN" cargo) |
| 111 | + |
| 112 | +# The head, the tracked changes and the untracked files' contents, so an edit |
| 113 | +# to an already dirty file during the run is noticed too. Writes no objects. |
| 114 | +fingerprint() { |
| 115 | + { |
| 116 | + git -C "$WORKTREE" rev-parse HEAD |
| 117 | + git -C "$WORKTREE" diff HEAD --binary --no-ext-diff --no-textconv |
| 118 | + # One file at a time: a path git cannot hash (a nested repository shows |
| 119 | + # up as "dir/") must not hide the files listed after it. |
| 120 | + git -C "$WORKTREE" ls-files -z -o --exclude-standard | while IFS= read -r -d '' path; do |
| 121 | + printf '%s ' "$path" |
| 122 | + git -C "$WORKTREE" hash-object -- "$path" 2>/dev/null || echo unhashable |
| 123 | + done |
| 124 | + } 2>&1 | cksum |
| 125 | +} |
| 126 | + |
| 127 | +if [ "$DRY_RUN" = 1 ]; then |
| 128 | + SCRATCH_HOME="$OUT/home.XXXXXX" |
| 129 | +else |
| 130 | + # Without git the change check below would compare two identical errors. |
| 131 | + git -C "$WORKTREE" rev-parse --verify -q HEAD >/dev/null || { |
| 132 | + echo "gates: $WORKTREE is not a git worktree with a commit" >&2 |
| 133 | + exit 2 |
| 134 | + } |
| 135 | + "$RUSTUP" run "$TOOLCHAIN" cargo --version >/dev/null 2>&1 || { |
| 136 | + echo "gates: '$RUSTUP run $TOOLCHAIN cargo' does not run; install the toolchain or set RUSTUP" >&2 |
| 137 | + exit 2 |
| 138 | + } |
| 139 | + mkdir -p "$OUT" || exit 2 |
| 140 | + SCRATCH_HOME="$(mktemp -d "$OUT/home.XXXXXX")" || exit 2 |
| 141 | + trap 'rm -rf "$SCRATCH_HOME"' EXIT |
| 142 | + # Logs from an earlier run into the same outdir would look current. |
| 143 | + rm -f "$OUT"/fmt.log "$OUT"/check.log "$OUT"/clippy.log "$OUT"/clippy-computer-use.log \ |
| 144 | + "$OUT"/test.log "$OUT"/alloc.log |
| 145 | + : >"$OUT/summary.txt" |
| 146 | + { |
| 147 | + echo "head $(git -C "$WORKTREE" rev-parse HEAD) tree $(git -C "$WORKTREE" rev-parse 'HEAD^{tree}')" |
| 148 | + echo "toolchain $TOOLCHAIN target $CARGO_TARGET_DIR jobs ${CARGO_BUILD_JOBS:-default}" |
| 149 | + IDENTITY="$(fingerprint)" |
| 150 | + echo "fingerprint $IDENTITY" |
| 151 | + git -C "$WORKTREE" status --porcelain --untracked-files=all |
| 152 | + } >"$OUT/identity.txt" |
| 153 | +fi |
| 154 | +TEST_ENV=(env HOME="$SCRATCH_HOME") |
| 155 | + |
| 156 | +cd "$WORKTREE/src-rust" || exit 2 |
| 157 | +FAILED=0 |
| 158 | +run() { |
| 159 | + local name="$1" start rc |
| 160 | + shift |
| 161 | + if [ "$DRY_RUN" = 1 ]; then |
| 162 | + printf '%s:' "$name" |
| 163 | + printf ' %q' "$@" |
| 164 | + printf '\n' |
| 165 | + return |
| 166 | + fi |
| 167 | + start=$(date +%s) |
| 168 | + "$@" >"$OUT/$name.log" 2>&1 |
| 169 | + rc=$? |
| 170 | + [ "$rc" -eq 0 ] || FAILED=1 |
| 171 | + echo "$name rc=$rc $(($(date +%s) - start))s" | tee -a "$OUT/summary.txt" |
| 172 | +} |
| 173 | + |
| 174 | +if [ "$DRY_RUN" = 1 ]; then |
| 175 | + echo "cd $(printf '%q' "$PWD")" |
| 176 | + echo "toolchain=$TOOLCHAIN CARGO_TARGET_DIR=$CARGO_TARGET_DIR CARGO_BUILD_JOBS=${CARGO_BUILD_JOBS:-}" |
| 177 | +fi |
| 178 | +SKIPPED="" |
| 179 | +if [ "${GATES_ONLY_TESTS:-}" = 1 ]; then |
| 180 | + SKIPPED="fmt check clippy clippy-computer-use" |
| 181 | + for gate in fmt check clippy clippy-computer-use; do |
| 182 | + if [ "$DRY_RUN" = 1 ]; then |
| 183 | + echo "$gate: skipped (GATES_ONLY_TESTS=1)" |
| 184 | + else |
| 185 | + echo "$gate skipped (GATES_ONLY_TESTS=1)" | tee -a "$OUT/summary.txt" |
| 186 | + fi |
| 187 | + done |
| 188 | +else |
| 189 | + run fmt "${CARGO[@]}" fmt --all -- --check |
| 190 | + run check "${CARGO[@]}" check --workspace --locked |
| 191 | + run clippy "${CARGO[@]}" clippy --workspace --all-targets --locked -- -D warnings |
| 192 | + if [ "$COMPUTER_USE" = 1 ]; then |
| 193 | + run clippy-computer-use "${CARGO[@]}" clippy --package claurst-tools --all-targets \ |
| 194 | + --features computer-use --locked -- -D warnings |
| 195 | + elif [ "$DRY_RUN" = 1 ]; then |
| 196 | + echo "clippy-computer-use: skipped (not macOS; set GATES_COMPUTER_USE=1 to run it)" |
| 197 | + else |
| 198 | + echo "clippy-computer-use skipped" | tee -a "$OUT/summary.txt" |
| 199 | + SKIPPED="clippy-computer-use" |
| 200 | + fi |
| 201 | +fi |
| 202 | +run test "${TEST_ENV[@]}" "${CARGO[@]}" test --workspace --locked --no-fail-fast |
| 203 | +run alloc "${TEST_ENV[@]}" "${CARGO[@]}" test -p claurst-tui --locked --test next_frame_perf \ |
| 204 | + -- --ignored --skip measure_active_frames --test-threads=1 |
| 205 | +[ "$DRY_RUN" = 1 ] && exit 0 |
| 206 | + |
| 207 | +# A workspace test gate that passes without running a single test has not |
| 208 | +# tested anything (a cargo runner or config override); count it as failed. |
| 209 | +python3 -I - "$OUT/test.log" >>"$OUT/summary.txt" <<'PY' || FAILED=1 |
| 210 | +import re |
| 211 | +import sys |
| 212 | +
|
| 213 | +passed = failed = ignored = 0 |
| 214 | +failures = [] |
| 215 | +with open(sys.argv[1], errors="replace") as log: |
| 216 | + for line in log: |
| 217 | + m = re.search(r"test result: \w+\. (\d+) passed; (\d+) failed; (\d+) ignored;", line) |
| 218 | + if m: |
| 219 | + passed += int(m[1]) |
| 220 | + failed += int(m[2]) |
| 221 | + ignored += int(m[3]) |
| 222 | + if re.match(r"^test .* FAILED$", line.strip()): |
| 223 | + failures.append(line.strip()) |
| 224 | +print(f"tests passed={passed} failed={failed} ignored={ignored}") |
| 225 | +for failure in failures: |
| 226 | + print(" ", failure) |
| 227 | +if passed == 0: |
| 228 | + print("tests: no test passed, so the test gate is counted as failed") |
| 229 | + sys.exit(1) |
| 230 | +PY |
| 231 | +if [ "$FAILED" != 0 ]; then |
| 232 | + echo "result FAIL" | tee -a "$OUT/summary.txt" |
| 233 | + exit 1 |
| 234 | +fi |
| 235 | +if [ "$(fingerprint)" != "$IDENTITY" ]; then |
| 236 | + echo "result CHANGED (the worktree's head or files changed during the run)" | tee -a "$OUT/summary.txt" |
| 237 | + exit 3 |
| 238 | +fi |
| 239 | +echo "result pass${SKIPPED:+ (skipped: $SKIPPED)}" | tee -a "$OUT/summary.txt" |
| 240 | +exit 0 |
0 commit comments