stocks / scripts /verify-diff.sh
user
python version
ee446e8
Raw History Blame Contribute Delete
8.33 kB
#!/usr/bin/env bash
# verify-diff.sh β€” token-efficient CI for the agent fast loop (`make verify-diff`).
#
# Scope: only files changed vs HEAD (plus untracked .py/.ts sources). Linters,
# type-checkers and the test suites that map to those files run; nothing else.
#
# Output contract (token-efficient quiet output):
# - success: a single confirmation line.
# - failure: compiler-style `file:line:col error:` diagnostics only β€” the
# failing tool's errors, never its progress banners or colour noise.
#
# Not a substitute for `make ci` β€” `verify-diff` is the fast loop, `ci` is the
# pre-close gate.
set -u
log_timing() {
mkdir -p results
local target="$1" elapsed="$2" status="$3"
local sha
sha=$(git rev-parse --short HEAD 2>/dev/null || echo unknown)
printf '%s\t%s\t%s\t%s\t%s\n' "$(date '+%Y-%m-%dT%H:%M:%S')" "$target" "$elapsed" "$status" "$sha" >> results/ci-timings.log
}
repo_root=$(git rev-parse --show-toplevel) || exit 1
cd "$repo_root" || exit 1
export NO_COLOR=1
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
start=$(date +%s%N)
# Files changed vs HEAD (deleted excluded) plus untracked sources β€” the full set
# the agent touched since the last commit.
changed_py=()
changed_ts=()
mapfile -t changed_py < <({ git diff --name-only --diff-filter=d HEAD -- '*.py'; git ls-files --others --exclude-standard -- '*.py'; } | sort -u)
mapfile -t changed_ts < <({ git diff --name-only --diff-filter=d HEAD -- '*.ts'; git ls-files --others --exclude-standard -- '*.ts'; } | sort -u)
all_changed=$(printf '%s\n' "${changed_py[@]}" "${changed_ts[@]}" | sed '/^$/d' | sort -u)
changed_count=$(printf '%s\n' "$all_changed" | sed '/^$/d' | wc -l | tr -d ' ')
if [ "$changed_count" -eq 0 ]; then
echo "βœ“ verify-diff: no source files changed β€” nothing to verify"
exit 0
fi
errors=0
passed=0
# DIAG_PAT matches compiler-style locations across ruff, pyright, biome, tsgo,
# and pytest: `path.ext(line,col)` (tsc) and `path.ext:line:col` (the rest).
DIAG_PAT='\.[A-Za-z0-9_./-]+\.(py|ts|tsx|js|css)(\(|:)[0-9]+'
fail() {
local tool="$1" log="$2"
errors=$((errors + 1))
echo " $tool: FAIL" >&2
if grep -qE "$DIAG_PAT" "$log"; then
grep -nE "$DIAG_PAT" "$log" | head -n 40 | sed 's/^/ /' >&2
else
tail -n 20 "$log" | sed 's/^/ /' >&2
fi
}
# --- test mapping (changed file -> the tests that cover it) -----------------
# Best-effort heuristic; first hit wins:
# 1. a changed file under tests/ is its own suite;
# 2. parent-prefixed name (core/dsl/grammar.py -> tests/dsl/test_dsl_grammar.py,
# modules/screener/service.py -> tests/modules/screener/test_screener_service.py);
# 3. exact basename at tests root (adapters/llm_service.py -> tests/llm/test_llm_service.py);
# 4. scoped import-dependency grep (core/modules only), capped;
# 5. exact basename anywhere under tests/modules/;
# 6. bare-stem grep β€” core/modules only, distinctive stems (>= 5 chars) so
# broad names like `app`/`config` never flood the run.
#
# TS sources also match the underscore form their tests use
# (chart-prev-day.ts -> test_chart_prev_day.ts, country-flags.ts ->
# test_country_flags.ts).
find_test_dir() {
local name="$1" hit
[ -f "tests/$name" ] && { printf 'tests/%s\n' "$name"; return 0; }
hit=$(find tests -mindepth 2 -maxdepth 3 -name "$name" -not -path "tests/fixtures/*" 2>/dev/null | head -n1)
[ -n "$hit" ] && { printf '%s\n' "$hit"; return 0; }
return 1
}
map_source_to_tests() {
local file="$1" stem stem_alts parent modpath hits
case "$file" in
# conftest.py holds fixtures, not tests. Passing it to pytest makes it a
# second module named `conftest` and collides with tests/<dir>/conftest.py.
tests/conftest.py | tests/*/conftest.py)
return 0
;;
tests/*.py | tests/*.ts)
printf '%s\n' "$file"
return 0
;;
esac
case "$file" in
*.py) stem=$(basename "$file" .py); stem_alts=("$stem") ;;
*.ts) stem=$(basename "$file" .ts); stem_alts=("$stem" "${stem//-/_}") ;;
*) return 0 ;;
esac
parent=$(basename "$(dirname "$file")")
if [ "$parent" != "." ] && [ "$parent" != "features" ]; then
local name
for name in "${stem_alts[@]}"; do
for ext in py ts; do
find_test_dir "test_${parent}_${name}.${ext}" && return 0
done
done
fi
local name
for name in "${stem_alts[@]}"; do
find_test_dir "test_${name}.py" && return 0
find_test_dir "test_${name}.ts" && return 0
done
modpath=""
case "$file" in
core/*) modpath="core.${file#core/}"; modpath="${modpath%.py}" ;;
modules/*) modpath="${file%.py}"; modpath="${modpath//\//.}" ;;
esac
hits=()
if [ -n "$modpath" ]; then
mapfile -t hits < <(rg -l "import ${modpath}\b|from ${modpath} import" tests --glob '*.py' --glob '*.ts' 2>/dev/null)
if [ "${#hits[@]}" -gt 0 ] && [ "${#hits[@]}" -le 4 ]; then
printf '%s\n' "${hits[@]}"
return 0
fi
fi
for name in "${stem_alts[@]}"; do
for ext in py ts; do
find_test_dir "test_${name}.${ext}" && return 0
done
done
if [ -n "$modpath" ] && [ "${#stem}" -ge 5 ]; then
mapfile -t hits < <(rg -l "\b${stem}\b" tests --glob '*.py' --glob '*.ts' 2>/dev/null)
if [ "${#hits[@]}" -gt 0 ] && [ "${#hits[@]}" -le 3 ]; then
printf '%s\n' "${hits[@]}"
fi
fi
return 0
}
test_files=()
while IFS= read -r file; do
[ -z "$file" ] && continue
while IFS= read -r suite; do
[ -n "$suite" ] && test_files+=("$suite")
done < <(map_source_to_tests "$file")
done <<< "$all_changed"
mapfile -t test_files < <(printf '%s\n' "${test_files[@]}" | sed '/^$/d' | sort -u)
# .ts suites run under Node's native runner (that is all `make test-js` is);
# pytest cannot collect them β€” split before invoking the runners.
py_suites=()
ts_suites=()
for suite in "${test_files[@]}"; do
case "$suite" in
*.ts) ts_suites+=("$suite") ;;
*.py) py_suites+=("$suite") ;;
esac
done
if [ "${#changed_py[@]}" -gt 0 ]; then
echo "β€Ί py: ${#changed_py[@]} file(s)" >&2
if ! uv run ruff check "${changed_py[@]}" >"$tmp/ruff.log" 2>&1; then
fail ruff "$tmp/ruff.log"
fi
pyright_files=()
for f in "${changed_py[@]}"; do
case "$f" in
core/*|modules/*|app.py|config.py) pyright_files+=("$f") ;;
esac
done
if [ "${#pyright_files[@]}" -gt 0 ]; then
if ! npx pyright "${pyright_files[@]}" >"$tmp/pyright.log" 2>&1; then
fail pyright "$tmp/pyright.log"
fi
fi
fi
if [ "${#changed_ts[@]}" -gt 0 ]; then
echo "β€Ί ts: ${#changed_ts[@]} file(s)" >&2
if ! npx tsgo --noEmit -p tsconfig.json >"$tmp/tsgo.log" 2>&1; then
fail tsgo "$tmp/tsgo.log"
fi
biome_files=()
for f in "${changed_ts[@]}"; do
# static/types/ is generated by `make api-types` and excluded in
# biome.json; handing biome an excluded path fails the run with
# "no files were processed" rather than linting anything.
case "$f" in
static/types/*) ;;
static/*) biome_files+=("$f") ;;
esac
done
if [ "${#biome_files[@]}" -gt 0 ]; then
if ! npx @biomejs/biome check "${biome_files[@]}" >"$tmp/biome.log" 2>&1; then
fail biome "$tmp/biome.log"
fi
fi
fi
if [ "${#py_suites[@]}" -gt 0 ]; then
echo "β€Ί tests: ${#py_suites[@]} py suite(s): ${py_suites[*]}" >&2
if ! uv run pytest -q --tb=short -m "not e2e and not perf" "${py_suites[@]}" >"$tmp/pytest.log" 2>&1; then
fail pytest "$tmp/pytest.log"
else
passed=$(grep -oE '[0-9]+ passed' "$tmp/pytest.log" | tail -n1 | awk '{print $1}')
passed=${passed:-0}
fi
fi
if [ "${#ts_suites[@]}" -gt 0 ]; then
echo "β€Ί tests: ${#ts_suites[@]} ts suite(s): ${ts_suites[*]}" >&2
if ! node --import ./tests/ts-resolve-hooks.mjs --test "${ts_suites[@]}" >"$tmp/node-test.log" 2>&1; then
fail node-test "$tmp/node-test.log"
else
passed=$((passed + $(grep -oE '# pass [0-9]+' "$tmp/node-test.log" | awk '{s+=$3} END {print s+0}')))
fi
fi
elapsed_ms=$((($(date +%s%N) - start) / 1000000))
if [ "$errors" -eq 0 ]; then
log_timing verify-diff "$elapsed_ms" ok
echo "βœ“ verify-diff: ${changed_count} file(s) checked, 0 errors, ${passed} test(s) passed (${elapsed_ms}ms)"
exit 0
fi
log_timing verify-diff "$elapsed_ms" fail
echo "verify-diff: FAIL β€” ${errors} tool(s) failed (${elapsed_ms}ms); fix the reported errors, then re-run." >&2
exit 1