test-impact
Identify which tests need rerunning after a code change — traces static call graph (function-level) or import graph (module-level) to find affected test files, then emits a ready-to-run pytest command. TRIGGER when: user asks which tests are affected by a change; phrases: "which
Install
npx skills add https://github.com/Borda/AI-Rig/tree/main/plugins/codemap-py/claude-skills/test-impact
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install borda-ai-rig@llmmart
git clone https://github.com/Borda/AI-Rig.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole borda/ai-rig collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
Files (ai-rig)
-
SKILL.md 19.7 KB
--- name: test-impact description: 'Identify which tests need rerunning after a code change — traces static call graph (function-level) or import graph (module-level) to find affected test files, then emits a ready-to-run pytest command. TRIGGER when: user asks which tests are affected by a change; phrases: "which tests are affected", "what tests cover this", "test impact of", "what tests to rerun".' argument-hint: <module::symbol | module> [--no-mocks] allowed-tools: Bash, Write, AskUserQuestion effort: low --- <objective> Find minimal tests affected by function/module change via Codemap static analysis; run no tests. Return ready `pytest` command for impacted tests only. Two input modes: - **Function-level** (`module::symbol`) — reverse-call-graph BFS; all direct/transitive test callers + symbol mocks (`mock_patches`). - **Module-level** (bare `module`) — reverse-import-graph BFS; all tests importing through any chain + any module-symbol mocks. `not_covered`: dynamic dispatch, hook callbacks, string-dispatch callers; same blind spot as `fn-blast`. Surface caveat; log gap. NOT for: finding all callers of a function (use `/codemap-py:query-code fn-rdeps <module::symbol> --exclude-tests`); querying module deps or blast radius (use `/codemap-py:query-code`); running/executing tests (identified here, not executed). </objective> <inputs> - **$ARGUMENTS**: `<qname> [--no-mocks]` - `qname` — `module::symbol` (function-level; a dotted `module.symbol` or a bare name defined once also resolves, while a module-name suffix or bare method name exits with candidates) or bare dotted module (module-level; an indexed module name always wins) - `--no-mocks` — exclude mock-only test files (no call/import path) - Omitted → inspect the working-tree change set in Step 1; use its union when multiple Python files changed (including unmapped files), otherwise AskUserQuestion </inputs> <workflow> ## Step 0 — Ensure index **Text substitution guard**: `$ARGUMENTS` and any `AskUserQuestion` answer are pasted into double-quoted Bash words below. If either contains a double quote (`"`), backtick (`` ` ``), backslash (`\`), or `$`, do not run any block that substitutes that text: print `! Unsafe argument text — use a plain module::symbol or dotted module, with single quotes if quoting is needed. Re-invoke.` and stop. Check each answer before substituting it into `QNAME`. ```bash # timeout: 10000 PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || basename "$PWD") # dispatcher, not scan-query/scan-index aliases — aliases lease in-engine too but skip dispatcher's interpreter probe (exit 127), deprecated shims removed no earlier than 1.0.0 # PATH-literal invocation everywhere below — expansion-bearing form matches no bare-name allow prefix; plugin's absolute bin/codemap-py stays interactive fallback command -v codemap-py >/dev/null 2>&1 || { echo "codemap-py not on PATH — install the codemap-py plugin, or invoke its bin/codemap-py launcher as one standalone command"; exit 1; } # git-root-anchored, not CWD-relative — resolve_proj_index.py resolves the same pair the engine uses (index_paths.py); a hand-rolled path here disagreed from a subdirectory python "$PLUGIN_ROOT/bin/resolve_index_env.py" --output-prefix "codemap-${_CM_PROJ}" 2>/dev/null IFS= read -r INDEX < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-resolve-index-${CSID}" 2>/dev/null || INDEX="" [ -n "$INDEX" ] || { echo "! resolve_index_env.py failed — check Python availability and CLAUDE_PLUGIN_ROOT"; exit 1; } [ -f "$INDEX" ] || echo "No index found — build requires SCAN_NO_AUTOBUILD != 1" ``` `SCAN_NO_AUTOBUILD=1` opts out: use index unchanged; no refresh/full build. Echo build wall-time when run, separating build/query cost. If `$INDEX` missing: - With `SCAN_NO_AUTOBUILD=1`: print `! codemap index missing and SCAN_NO_AUTOBUILD=1 — refusing to auto-build. Build it manually first: /codemap-py:scan-codebase`; exit 1. - Otherwise run foreground `codemap-py index`; wait, then continue. Never invoke `codemap-py:scan-codebase`: `disable-model-invocation:true`, user-slash-only. Build through gated `codemap-py index` dispatcher. ```bash # timeout: 400000 [ "${SCAN_NO_AUTOBUILD:-0}" = "1" ] && { echo "! codemap index missing and SCAN_NO_AUTOBUILD=1 — refusing to auto-build. Build it manually first: /codemap-py:scan-codebase"; exit 1; } _CM_BUILD_T0=$(date +%s) # forward only when user already overrode CODEMAP_INDEX_DIR — an unconditional export turns the # engine's own git-root default into a flat-convention override (index_paths.py), same trap fixed # below for the incremental-refresh branch [ -n "${CODEMAP_INDEX_DIR:-}" ] && export CODEMAP_INDEX_DIR codemap-py index --timeout 360 \ && echo "[codemap] index built in $(( $(date +%s) - _CM_BUILD_T0 ))s" \ || { printf "! codemap-py index failed — cannot proceed without an index\n"; exit 1; } ``` If index already exists: ```bash # timeout: 400000 if [ "${SCAN_NO_AUTOBUILD:-0}" = "1" ]; then echo "[codemap] SCAN_NO_AUTOBUILD=1 — using existing index as-is (no refresh)" else _CM_BUILD_T0=$(date +%s) # forward only when user already overrode CODEMAP_INDEX_DIR — an unconditional export turns the # engine's own git-root default into a flat-convention override (index_paths.py), materializing # a second index when this ran from a subdirectory [ -n "${CODEMAP_INDEX_DIR:-}" ] && export CODEMAP_INDEX_DIR codemap-py index --incremental --timeout 360 \ && echo "[codemap] index built in $(( $(date +%s) - _CM_BUILD_T0 ))s" \ || printf "⚠ codemap-py index --incremental failed — index may be stale; continuing\n" fi ``` After build/refresh, re-verify index; the block's last command is Step 1's argument parse: ```bash # timeout: 5000 PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || basename "$PWD") python "$PLUGIN_ROOT/bin/resolve_index_env.py" --output-prefix "codemap-${_CM_PROJ}" 2>/dev/null IFS= read -r INDEX < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-resolve-index-${CSID}" 2>/dev/null || INDEX="" [ -n "$INDEX" ] && [ -f "$INDEX" ] || { printf "! Index not found after refresh at %s — check CODEMAP_INDEX_DIR or re-run /codemap-py:scan-codebase\n" "${INDEX:-<unresolved>}"; exit 1; } # shlex tokens + exact-token flag match: awk/grep split sent a leading --no-mocks as the target python "$PLUGIN_ROOT/bin/parse_test_impact_args.py" --output-prefix "codemap-${_CM_PROJ}" --arguments="$ARGUMENTS" ``` ## Step 1 — Parse arguments Extract `QNAME` + `NO_MOCKS` from `$ARGUMENTS` — the parse ran as the last command of the Step 0 re-verify block above. Exit `1` → index not found (its `!` line printed); stop. Exit `0` → target recorded (`ti-qname`/`ti-mocks` sentinels). Exit `2` → relay its `!` line (unknown flag, unbalanced quote, flag-shaped target); stop. Exit `3` → no target given: First run `codemap-py query diff-impact` to inspect tracked staged and unstaged Python changes against `HEAD` (untracked files are excluded). If more than one Python file changed (`len(changed_modules) + len(unmapped_files) > 1`), use its `test_impact` union (`test_files`, `total`, `pytest_cmd`) and top-level `index` metadata for Step 3; label the scope `working-tree changes`, skip the single-target question and Step 2. Do not invent `via_call`/`via_mock` counts: omit that breakdown for the union. ```bash # timeout: 120000 PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || basename "$PWD") _TI_DIFF="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-ti-diff-${CSID}" _TI_DIFF_ERR="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-ti-diff-stderr-${CSID}" codemap-py query diff-impact >"$_TI_DIFF" 2>"$_TI_DIFF_ERR" _TI_RC=$? python "$PLUGIN_ROOT/bin/summarize_test_impact.py" --diff-impact --rc "$_TI_RC" --stderr-file "$_TI_DIFF_ERR" --output-prefix "codemap-${_CM_PROJ}" <"$_TI_DIFF" || printf 'whole-change analysis unavailable\n' ``` Nonzero exit or unparsable JSON → report the diagnostic plus `whole-change analysis unavailable` and continue with the question route below; never render an empty test set from that failure or reuse the captured file for a union. Surface `unmapped_files` and every `changed_modules[].errors` entry as coverage gaps, including when continuing with the question route. The union can omit failed test-impact subqueries without listing their errors: always label it a static selection, never proof of complete coverage, and retain the dynamic-dispatch/hook/string-dispatch caveat even when `index.not_covered` is absent. For the whole-change route with `--no-mocks`, do not use the default diff union (it includes mocks): the helper queries each changed module and each of its `changed_symbols` targets in one batch, then prints the deduplicated test union and a pytest command quoted for POSIX shells or native Windows PowerShell. Run this fixed block only after a successful parsed diff-impact result; stop on any failed query. Carry the diff's coverage gaps and `diff_index`, the batch `index` and every `target_caveats` entry into Step 3. Never narrow this request to one selected module or silently include mock-only tests. ```bash # timeout: 120000 PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || basename "$PWD") python "$PLUGIN_ROOT/bin/test_impact_union.py" "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-ti-diff-${CSID}" || exit 1 ``` With zero or one changed Python file, or when whole-change analysis is unavailable, continue with the existing question route: - Use `AskUserQuestion`: "Which function or module changed? Type `module::symbol` (function-level) or a bare dotted module (module-level) in the free-text field." Options: up to 3 concrete targets grounded in changed `.py` files (`git diff --name-only HEAD`, as bare dotted modules), plus `Cancel — exit without analysis`. Never offer a format name as an option — choosing one yields no target. - No grounded target → skip the question; print `Usage: /codemap-py:test-impact <module::symbol | module> [--no-mocks]`; stop. After an answer, record the target before Step 2; the text substitution guard applies: ```bash # timeout: 5000 PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || basename "$PWD") QNAME="<answer from AskUserQuestion>" case "$QNAME" in *'<'*'>'*|"") echo "! BLOCKED — target placeholder not substituted; ask again"; exit 1;; esac # answer + original flags: re-parsing the answer alone rewrites ti-mocks empty, dropping --no-mocks python "$PLUGIN_ROOT/bin/parse_test_impact_args.py" --output-prefix "codemap-${_CM_PROJ}" --arguments="$QNAME $ARGUMENTS" ``` **Multi-symbol guard**: `$ARGUMENTS` may contain multiple targets (e.g. `mypackage.auth::validate mypackage.auth::parse`). Helper keeps first non-flag token; when more remain it prints `⚠ test-impact accepts one symbol at a time — using first token only: $QNAME. Run separately for each remaining symbol.` — relay verbatim. ## Step 2 — Run test-impact query ```bash # timeout: 10000 PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || basename "$PWD") IFS= read -r QNAME < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-ti-qname-${CSID}" 2>/dev/null || QNAME="" IFS= read -r MOCKS_FLAG < "${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-ti-mocks-${CSID}" 2>/dev/null || MOCKS_FLAG="" [ -n "$QNAME" ] || { echo "! no test-impact target recorded — finish Step 1 first"; exit 1; } _TI_ERR="${TMPDIR:-/tmp}/codemap-${_CM_PROJ}-ti-stderr-${CSID}" # capture stderr to file, never 2>/dev/null — swallowed diagnostic rendered a broken index as "no affected tests" before RESULT=$(codemap-py query test-impact "$QNAME" $MOCKS_FLAG 2>"$_TI_ERR") _TI_RC=$? [ "$_TI_RC" -eq 0 ] || printf "! test-impact query failed (exit %d) — this is NOT a 'no affected tests' result; do not report an empty test set\n" "$_TI_RC" >&2 # one parse, no per-field `|| echo` defaults (forged total=0); error JSON is on stdout, so routing reads it there printf '%s' "$RESULT" | python "$PLUGIN_ROOT/bin/summarize_test_impact.py" --rc "$_TI_RC" --stderr-file "$_TI_ERR" --output-prefix "codemap-${_CM_PROJ}" || exit 1 ``` On success the helper prints a summary — Step 3 renders from it only: - `test_files` — list of test file paths - `pytest_cmd` — ready-to-run command for POSIX shells on POSIX hosts or PowerShell on native Windows. `test_files` is the authoritative argv list; for another shell, rebuild with its quoting rather than reuse incompatible escaping. - `via_call` / `via_mock` — breakdown of how tests were found - `index.not_covered` — surface as caveat if non-empty - `index.hint` — include as suggestion - `index.query_complete` / `index.stale` — completeness signals, always emitted; `query_complete: false` or `stale: true` means the query ran against an incomplete/stale index (e.g. after a soft-failed `--incremental` refresh, Step 0) — surface as caveat on **both** Step 3 branches, independent of `total` or `not_covered` - `index.completeness_reason` — human-readable reason when present; absent on a compact+complete answer by design, never treated as a gap or defaulted **Loud-failure contract**: query failure ≠ empty result. Shell enforces it three ways: nonzero exits skill `1` before field reads; unparsable payload exits helper `1`; `query_complete`/`stale` carry through the same parse so a soft-failed refresh with a clean exit can still be caveated. None of the three may become an uncaveated "no affected tests". False empty set from broken index is worst possible output. **Failure routing** (nonzero exit; helper reads the engine's stdout error JSON, then stderr head): - `rejected_target` present (typo, ambiguous name, module not indexed) → prints `error` plus each `candidates`/`suggestions` entry; no rebuild advice. Relay the candidates; re-run with one exact candidate. - Exit `2` without it → rejected argument; no rebuild advice. - Anything else (missing/oversized/corrupt index, self-check failure) → rebuild advice. **JSON guard**: output may include log/warning prefix/suffix; helper tolerates it — never assume raw valid JSON. Never add per-field `|| echo "0"` / `|| echo "[]"`: failed parse must not manufacture benign defaults — `query_complete`/`stale` may legitimately read `null` when the engine omits them; treat `null` as "signal absent", never as false/true. ## Step 3 — Output Render every value from Step 2's printed summary, or Step 1's whole-change result — never estimate counts or re-run the query. For the whole-change route read completeness from the top-level `index` object and use `working-tree changes` for `<qname>` and report scope. For a single-target result, prepend `Scope: covers <qname> only; N other changed modules not analysed.` before either output branch and include it in any written report. Run the fixed block below to count tracked staged and unstaged Python changes against `HEAD`, excluding the selected module; if those files cannot be mapped reliably, it prints `other changed modules not analysed; count unknown` instead of inventing a count. Never describe a single-target pytest command as covering the entire change. ```bash # timeout: 15000 export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" _CM_PROJ=$(git rev-parse --show-toplevel 2>/dev/null | xargs basename 2>/dev/null || basename "$PWD") python - "$_CM_PROJ" <<'PY' import os import subprocess import sys import tempfile from pathlib import Path qname = "<unrecorded>" try: state = Path(os.environ.get("TMPDIR") or tempfile.gettempdir()) / f"codemap-{sys.argv[1]}-ti-qname-{os.environ['CSID']}" qname = state.read_text(encoding="utf-8").strip() diff = subprocess.run(["git", "diff", "--name-only", "HEAD", "--", "*.py"], capture_output=True, text=True, timeout=10) if diff.returncode or not qname: raise ValueError("change count unavailable") paths = diff.stdout.splitlines() if any(path.startswith('"') for path in paths): raise ValueError("quoted paths cannot be mapped") module = qname.split("::", 1)[0].replace(".", "/") selected = [path for path in paths if any(path == suffix or path.endswith("/" + suffix) for suffix in (module + ".py", module + "/__init__.py"))] if len(selected) > 1: raise ValueError("selected module is ambiguous") print(f"Scope: covers {qname} only; {len(paths) - len(selected)} other changed modules not analysed.") except (OSError, ValueError, subprocess.TimeoutExpired): print(f"Scope: covers {qname} only; other changed modules not analysed; count unknown") PY ``` **Completeness caveat (both branches below)**: read `query_complete` and `stale` from that summary first. `query_complete` reads `false` or `stale` reads `true` → prepend `⚠ Index completeness: <completeness_reason, or "index incomplete/stale — see Step 0 refresh output above" when reason is empty>` before the branch's own message. This fires independently of `not_covered` — a soft-failed `--incremental` refresh (Step 0) produces exactly this with a clean query exit. **When `total == 0`**: reachable only after Step 2 exit `0` + parsed payload, or a successful parsed Step 1 whole-change result. Retain the selected route's scope, static-selection and coverage-gap caveats, including `unmapped_files` and per-module errors. Genuine empty only when the completeness caveat above is also absent — with the caveat present, "no affected tests" means "the index couldn't prove it", not "there are none". Report "No tests found via static analysis. Try full suite or check with `grep -rn <symbol_name> tests/`." — prefixed with the completeness caveat when present. **When `total > 0`**: ```` ## Test impact: <qname> **Affected tests** (<total> files, <via_call> via call/import graph, <via_mock> via mocks): <test_files as bullet list> **Run:** ``` <pytest_cmd> ``` <if not_covered non-empty> **Caveat:** dynamic-dispatch / hook-callback callers are not in the static graph — <hint>. </if> <if completeness caveat present> **Caveat:** index completeness — <completeness_reason, or generic incomplete/stale message above>. </if> ```` Output routing: if `total >= 5`, print free non-colliding path (`<TI_OUT>` below); write report there: ```bash # timeout: 3000 # never overwrite — same-day re-run on another target would replace unrelated report; helper appends -2, -3, … until free PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name codemap-py) || exit $?; export PLUGIN_ROOT python "$PLUGIN_ROOT/bin/anti_overwrite_path.py" .temp output-test-impact --branch-date ``` Write the report with Write: a YAML `---` block first (`Title: Test impact — <qname>`, `Date`, `Scope: <qname>`, `Focus: static test impact`, `Agents: none`, `Outcome: <total> affected test files`, `Confidence: complete` or `incomplete/stale` per the caveat, `Next steps: run the printed pytest command`, `Path: <TI_OUT>`), then the template above. </workflow>
Comments (0)
Sign in to join the conversation.
Reviews (0)
No reviews yet.
No comments yet.