File size: 8,327 Bytes
ee446e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/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