Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(mcp): name the tests a change needs from the impacted-tests selection (#3296)

Raghav Chamadiya68e1044ed87f
diff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.md
index c78ab05ce..401bac750 100644
--- a/docs/agent/MCP_TOOLS.md
+++ b/docs/agent/MCP_TOOLS.md
@@ -243,7 +243,7 @@ What history says about touching a file: hotspot score, bug-fix record, owners,
| `include` | list[string] | none | `graph` (typed dependents, consumers, cross-repo links), `churn`, `owners` (owner percentages, bus factor, contributor count), `tests` (typed `test_recommendations` in PR mode), `blast` (`pr_blast_radius` in PR mode), `scales` (units and calibration, identical per call) |
| `repo` | string | default repo | Workspace repo alias. `"all"` is not supported |
-**Key return fields:** per file: `hotspot_score` (0 to 1), `health_score` (0 to 10), `dependents_count`, `co_change_partners` (`file_path`, `support`, `conf_ab`, `has_import_link`), `primary_owner` (detailed owner metrics are opt-in under `include=["owners"]`), test gaps, `security_signals`. In PR mode, `directive` with `may_break`, `missing_cochanges`, `tests_to_run`, `tests_to_run_basis` (`measured`, `inferred` or `none`), `tests_to_update` (test files to edit, each with a `name_pair`, `imports` or `co_change` reason), `coverage`, `recommended_reviewers`, `next_calls`, and `reach` (`localized`, `moderate` or `broad`; `null` when no structural score was computed). `include=["blast"]` adds `pr_blast_radius`; the raw 0 to 10 `structural_impact_score` and its scale appear there only with `include=["blast", "scales"]`. `missing_tests` appears only when coverage can back it. A target naming no indexed file returns `resolved: false` with a reason, never zeroed counts.
+**Key return fields:** per file: `hotspot_score` (0 to 1), `health_score` (0 to 10), `dependents_count`, `co_change_partners` (`file_path`, `support`, `conf_ab`, `has_import_link`), `primary_owner` (detailed owner metrics are opt-in under `include=["owners"]`), test gaps, `security_signals`. In PR mode, `directive` with `may_break`, `missing_cochanges`, `tests_to_run` (the same selection `repowise impacted-tests` makes), `tests_to_run_basis` (`measured`, `inferred` or `none`), `tests_run_all` (with `tests_run_all_reasons` when every test must run), `tests_to_update` (test files to edit, each with a `name_pair`, `imports` or `co_change` reason), `coverage`, `recommended_reviewers`, `next_calls`, and `reach` (`localized`, `moderate` or `broad`; `null` when no structural score was computed). `include=["blast"]` adds `pr_blast_radius`; the raw 0 to 10 `structural_impact_score` and its scale appear there only with `include=["blast", "scales"]`. `missing_tests` appears only when coverage can back it. A target naming no indexed file returns `resolved: false` with a reason, never zeroed counts.
Dependent counts are a floor over the indexed graph, and structural reach is not proof of runtime breakage. `reach` bands an uncalibrated heuristic that never sees the diff; it is not a probability.
diff --git a/docs/layers/CHANGE_RISK.md b/docs/layers/CHANGE_RISK.md
index 7bc436559..ff1e4f5d6 100644
--- a/docs/layers/CHANGE_RISK.md
+++ b/docs/layers/CHANGE_RISK.md
@@ -141,7 +141,7 @@ files reach, and what is missing from it. The response opens with a
|---|---|
| `may_break` | Production files in structural reverse-import reach of the diff. Candidates for review, not proven breakage |
| `missing_cochanges` | Files that historically change with these but are not in the diff |
-| `tests_to_run`, `tests_to_run_basis` | Which tests to run, `measured` (coverage) or `inferred` (graph reach). With coverage the list is the measured one; `include=["tests"]` adds the typed `test_recommendations` rows, including reached tests the measured list lacks (`reason: structural_reach`) |
+| `tests_to_run`, `tests_to_run_basis` | Which tests to run, from the same selection `repowise impacted-tests` makes; `measured` when coverage decided a changed file, else `inferred`. `tests_run_all` says when every test must run, `tests_to_run_why` why each listed test is in. `include=["tests"]` adds the typed `test_recommendations` rows |
| `missing_tests` | Changed files with a test gap; present only when coverage can back it |
| `coverage` | `{status, reason}` when there is no per-test coverage map |
| `tests_to_update` | Up to three test files the change will probably need edited, with why: `name_pair`, `imports` or `co_change`; empty when none qualify |
diff --git a/docs/layers/TEST_INTELLIGENCE.md b/docs/layers/TEST_INTELLIGENCE.md
index 31bf9a902..f881582f4 100644
--- a/docs/layers/TEST_INTELLIGENCE.md
+++ b/docs/layers/TEST_INTELLIGENCE.md
@@ -248,22 +248,25 @@ row keeps its `basis`:
| `measured` | A test node id | The per-test map shows the test covering a changed file |
| `inferred` | A test file path | The graph shows the test reaching the change; a candidate, not proof |
-`tests_to_run`, sent without the include, is the flat list with
-`tests_to_run_basis`. When coverage exists it is the measured list; test files
-in reverse-import reach that it lacks appear only as `inferred` rows with
-`reason: structural_reach` under the include. Without coverage those files
-join the list itself as `inferred`. Without a coverage map the directive carries
+`tests_to_run`, sent without the include, is the run list from the same
+selection `repowise impacted-tests` makes, with `tests_to_run_basis`,
+`tests_to_run_why` and `tests_run_all` (plus `tests_run_all_reasons` when every
+test must run). Without a coverage map the directive carries
`coverage: {status, reason}`; with one, `coverage_analysis` says whether
coverage is available, partial, degraded or stale.
-**`get_change_risk(revspec=...)`** returns `impacted_tests`, computed from the
-changed *lines*, so it is narrower:
+**`get_change_risk(revspec=...)`** returns `impacted_tests`, the same selection
+over the change's diff, so a measured map at either end of it matches by line:
```json
{
- "status": "map_present",
+ "status": "selected",
+ "run_all": false,
+ "reasons": [],
+ "basis": "measured",
"map_present": true,
- "tests": ["tests/test_auth.py::test_login", "..."],
+ "tests_to_run": ["tests/test_auth.py::test_login", "..."],
+ "why": {"tests/test_auth.py": "src/auth.py changed (coverage)"},
"total": 23,
"truncated": true,
"line_coverage": {
@@ -272,14 +275,14 @@ changed *lines*, so it is narrower:
"covered": [...],
"no_coverage_data": [...]
},
- "summary": "23 test(s) cover the changed lines; showing first 10."
+ "summary": "23 test(s) to run for this change; showing first 10; the rest can be skipped."
}
```
`untested_changes` is the strong signal: the file is in the map but nothing
covers the lines you touched. `stale_test_candidates` flags covered lines whose
guarding test is absent from the diff. `no_coverage_data` means the file is not
-in the map. `get_change_risk` leaves out the CLI's filename-pattern guess.
+in the map.
**Self-checking before a push.** `get_change_risk` without a `revspec` measures
`patch_coverage` over everything a push would bring: the working tree
@@ -311,11 +314,15 @@ carries a discriminator beside the list:
| `status` | Meaning |
|---|---|
- | `map_present` | A per-test map exists. An empty `tests` list here is a real finding: nothing in the map covers this change |
- | `inferred` | No map; the graph names candidate test files. Passing them does not clear the change |
- | `no_map` | No map and no graph answer. The summary says to run the full suite |
+ | `selected` | The selection vouches for `tests_to_run`. Empty here means the change needs no test (documentation, a deleted test) |
+ | `run_all` | Every test must run; `reasons` says why, and `tests_to_run` is what to run first |
| `no_index` | Nothing indexed yet |
- | `unknown` | The git read failed |
+ | `unknown` | The git or index read failed |
+ | `config_invalid` | `tests.*` in `.repowise/config.yaml` does not parse |
+ | `timeout` | The selection took over 30 seconds; `repowise impacted-tests` gives the same answer |
+ | `busy` | A selection for this repository is still running |
+
+ Every status but `selected` carries `run_all: true`: nothing vouches for a subset.
| `no_source_line_changes` | No changed source lines to map |
- `get_risk` keeps coverage availability, freshness and map presence explicit
diff --git a/docs/reference/MCP_RESPONSE_FIELDS.md b/docs/reference/MCP_RESPONSE_FIELDS.md
index 8ece49c35..c0c05b2d7 100644
--- a/docs/reference/MCP_RESPONSE_FIELDS.md
+++ b/docs/reference/MCP_RESPONSE_FIELDS.md
@@ -223,8 +223,8 @@ Opt-in blocks: `owners` adds `owner_pct`, `owner_line_pct`, `recent_owner`, `rec
|-------|---------|
| `may_break` | Production files in reverse-import reach of the change: candidates for review, not proven breakage |
| `missing_cochanges` | Historical co-change partners absent from the change |
-| `tests_to_run`, `tests_to_run_basis` | The test ids or files to run, and whether the list is `measured`, `inferred` or `none`. When coverage exists the list is the measured one; test files in reverse-import reach that it lacks come back as `inferred` rows with `reason: structural_reach` under `include=["tests"]`. An unmeasured list takes those files directly, and a `none` list that gains any becomes `inferred`. An empty list with unavailable coverage never means no tests are needed |
-| `tests_to_update` | Up to three test files the change will probably need edited, each `{path, reason}` with `reason` `name_pair` (named for a changed file), `imports` (imports one directly) or `co_change` (changes with one in git history), in that order. Tests to edit, not to run: a file can appear in both lists. Empty when none qualify |
+| `tests_to_run`, `tests_to_run_basis` | The tests the change needs, from the same selection `repowise impacted-tests` makes, capped at ten: a test the change edits first, then those reaching more of the changed files, then tests that run with every subset. `tests_to_run_basis` is `measured` when coverage decided a changed file, `inferred` when only the graph or a rule did, `none` when nothing named a test. `tests_to_run_why` gives the reason for each listed file (the changed file and evidence, or `runs with every subset: ...`). `tests_run_all` true means no subset can be vouched for and `tests_run_all_reasons` says why; the list is then what to run first. A failed selection carries `tests_status` and `tests_status_reason` and names nothing |
+| `tests_to_update` | Up to three test files the change will probably need edited, each `{path, reason}` with `reason` `name_pair` (named for a changed file), `imports` (its own code imports one; a conftest or setup file reaching it does not count) or `co_change` (changes with one in git history), in that order. Only tests the selection runs because of a changed file and that a runner collects. Tests to edit, not to run: a file can appear in both lists. Empty when none qualify |
| `missing_tests`, `files_without_measured_tests` | Changed files with no measured test. `missing_tests` is absent when coverage is unavailable or stale, since an empty list would read as no gaps |
| `coverage` | `{status, reason}` when there is no per-test coverage map. With a map, `coverage_analysis`, `test_analysis` and `test_inference_analysis` say whether each evidence source was available, stale, partial or degraded |
| `test_recommendations` | With `include=["tests"]`: typed rows, each with a `basis`: `measured` (the per-test coverage map found the test) or `inferred` (structural reach, not coverage proof) |
@@ -250,7 +250,7 @@ Every float is rounded to 4 significant digits. Direct rows' `structural_score`
| `diff_shape` | One sentence on size and spread; never a danger verdict |
| `working_tree` | Present only when `revspec` was omitted: whether uncommitted work was scored, or a clean tree fell back to `HEAD` |
| `fix_history` | Recency-weighted bug-fix record of the touched files, with `percentile` and `overlap`; the raw `density` ships with `include=["diagnostics"]` |
-| `impacted_tests` | Tests that execute the changed lines |
+| `impacted_tests` | The tests the change needs, and whether every test must run |
| `patch_coverage` | Share of changed executable lines the stored coverage ran |
| `branch_overlap` | Other branches editing the same files |
| `independent_changes` | When the diff is several unrelated changes |
@@ -260,7 +260,7 @@ Every float is rounded to 4 significant digits. Direct rows' `structural_score`
**health_delta.** Both revisions are analysed from their own content, and a finding at head is reported only when the diff explains it. `scope` counts changed, eligible, analysed, skipped and failed files; `status` is `available`, `partial` or `unavailable`, and `partial` is never a clean bill. Documentation and configuration files (Markdown, JSON, YAML, TOML and the other doc or config extensions) are skipped with reason `not_code` and never make a run `partial`; a change made only of them reads `clear_in_analyzed_scope` with a headline saying health analysis does not cover them. HTML, schema and other files with no health dialect still make a run `partial`. `introduced`, `worsened` and `resolved` are totals; `top_findings` holds the three most actionable, with `findings_total`. Each finding names `dimension`, `biomarker`, `severity`, `path`, `symbol`, head-side `lines`, a `reason`, and an `attribution` (`basis` of `added_lines`, `changed_symbol`, `changed_call_edge`, `new_file`, `file_change`, `context_change` or `unknown`, with a `confidence`). Identity ignores line numbers, so moved code introduces nothing. `inspect` gives the `finding_id` call that expands one; ids are bound to the two revisions. A finding that matches a stored one carries `health_reference` for `get_health(finding_id=...)`. Performance findings carry `opportunity_id` and are ordered by opportunity rank.
-**impacted_tests.** `tests_to_run` names tests the per-test coverage map proves execute the changed lines, capped at ten with `total` and `truncated`. With a coverage map, `map_present` is true and `line_coverage` buckets: `untested_changes`, `stale_test_candidates` (covered lines whose test file is absent from the diff), `covered`, `no_coverage_data`. With no coverage map, `map_present`, `line_coverage` and (unless tests are named) `tests_to_run_kind` are absent, and `status` is `inferred` when the import graph names test files reaching the change, or `no_map` (run the full suite). `basis` is always present: `measured`, `inferred` or `none`; `tests_to_run_kind` is `test_id` or `test_file` whenever `tests_to_run` is filled. Build the measured map with `coverage run --contexts=test` and `repowise coverage add`.
+**impacted_tests.** The same selection `repowise impacted-tests` makes over the whole change (every touched path, whatever the score's filters). `status` is `selected` or `run_all` (or `no_index`, `unknown`, `config_invalid`, `timeout`, `busy`, `no_source_line_changes` when nothing was selected, each with `run_all` true; `timeout` means the selection took over 30 seconds and `busy` that one for this repository is still running, and `repowise impacted-tests` gives the answer); `run_all` true means no subset can be vouched for, with up to three `reasons` (`reasons_total` counts them all). `tests_to_run` (capped at ten, `total` and `truncated`) leads with a test the change edits, then tests reaching more of the changed files, then the `always_run_total` tests that run with every subset; `why` gives each listed file's reason. With `run_all` the list is what to run first. `tests_to_run_kind` is `test_id` when a coverage node id is listed, else `test_file`. `basis` is `measured` when coverage decided a changed file, `inferred` when only the graph or a rule did, `none` when nothing named a test; `basis_by_file` names the evidence per changed file (`coverage`, `changed-test`, `call-graph`, `import-graph`, `conftest`, `full-run`, `no-tests-needed`, ...). With a coverage map, `map_present` is true and `line_coverage` buckets the scored files' changed lines, matched on the side of the diff the map was measured at (by file when it was measured at neither): `untested_changes`, `stale_test_candidates` (covered lines whose test file is absent from the diff), `covered`, `no_coverage_data`. Build the measured map with `coverage run --contexts=test` and `repowise coverage add`.
**patch_coverage.** Present when the index stores coverage; the same computation and JSON shape `repowise coverage check --format json` gates on. `patch_coverage_pct` is null when no changed line is executable; files the report never names read `not_in_report`. `scope.freshness` is `stale` when coverage was measured at another commit than the change's head. `path_gates` judges the path-scoped gates in `coverage.gates`. Each file row carries `risk` (`fix_pressure`, `dependents`, `hotspot`, `bug_magnet`, `risky`, `reasons`, `basis`) and up to eight `hints` per uncovered range: `range`, `symbol`, up to three `tests` to extend, and a `basis` of `per_test` (measured), `call_graph` or `import_graph` (inferred). Without a `revspec`, it covers everything a push would bring, diffed from the merge-base with the default base branch.
diff --git a/packages/claude-mod/mod-test/lens.test.ts b/packages/claude-mod/mod-test/lens.test.ts
index d9e448ad9..2bfcfaf3d 100644
--- a/packages/claude-mod/mod-test/lens.test.ts
+++ b/packages/claude-mod/mod-test/lens.test.ts
@@ -248,7 +248,7 @@ test('no squeeze row for output distill left alone', async ($, on) => {
// complex helper was added (test/fixtures/change-risk/findings.json, the
// blocks the card reads; this runtime has no fs to load the file).
const FINDINGS =
- {"directive": {"status": "review_required", "headline": "2 new findings need review, starting with nested_complexity in src/requests/_internal_utils.py.", "reasons": ["critical defect: nested_complexity in classify_headers (added_lines)", "high defect: complex_method in classify_headers (added_lines)"], "next_actions": ["Inspect src/requests/_internal_utils.py:55 (chf_7c35d63fcc13e52b)", "Inspect src/requests/_internal_utils.py:55 (chf_1d478916e70baee8)", "Run: tests/test_requests.py tests/test_utils.py"]}, "ref": "working tree", "working_tree": true, "risk_percentile": 92.0, "impacted_tests": {"status": "inferred", "basis": "inferred", "map_present": false, "tests_to_run": ["tests/test_requests.py", "tests/test_utils.py"], "tests_to_run_kind": "test_file", "total": 2, "truncated": false, "line_coverage": {"untested_changes": [], "stale_test_candidates": [], "covered": [], "no_coverage_data": []}, "summary": "2 test file(s) reach the changed files in the graph. Inferred from the dependency graph, not measured. For the line-precise answer build the map with `coverage run --contexts=test` then `repowise coverage add`."}, "health_delta": {"status": "available", "explanation": "Compared 1 changed files on both sides.", "basis": "both_sides_analyzed", "introduced": 2, "worsened": 0, "resolved": 0, "scope": {"changed": 1, "eligible": 1, "analyzed": 1, "skipped": 0, "failed": 0}, "top_findings": [{"id": "chf_7c35d63fcc13e52b", "change": "introduced", "dimension": "defect", "biomarker": "nested_complexity", "severity": "critical", "path": "src/requests/_internal_utils.py", "reason": "classify_headers nests 7 levels deep", "attribution": {"basis": "added_lines", "confidence": "high", "why": "Lines 55-85 are added or rewritten by this change."}, "inspect": "get_change_risk(finding_id='chf_7c35d63fcc13e52b')", "symbol": "classify_headers", "lines": [55, 85]}, {"id": "chf_1d478916e70baee8", "change": "introduced", "dimension": "defect", "biomarker": "complex_method", "severity": "high", "path": "src/requests/_internal_utils.py", "reason": "classify_headers has cyclomatic complexity 15", "attribution": {"basis": "added_lines", "confidence": "high", "why": "Lines 55-85 are added or rewritten by this change."}, "inspect": "get_change_risk(finding_id='chf_1d478916e70baee8')", "symbol": "classify_headers", "lines": [55, 85]}], "findings_total": 2, "findings_emitted": 2}}
+ {"directive": {"status": "review_required", "headline": "2 new findings need review, starting with nested_complexity in src/requests/_internal_utils.py.", "reasons": ["critical defect: nested_complexity in classify_headers (added_lines)", "high defect: complex_method in classify_headers (added_lines)"], "next_actions": ["Inspect src/requests/_internal_utils.py:55 (chf_7c35d63fcc13e52b)", "Inspect src/requests/_internal_utils.py:55 (chf_1d478916e70baee8)", "Run: tests/test_requests.py tests/test_utils.py"]}, "ref": "working tree", "working_tree": true, "risk_percentile": 92.0, "impacted_tests": {"status": "selected", "run_all": false, "reasons": [], "reasons_total": 0, "basis": "inferred", "map_present": false, "tests_to_run": ["tests/test_requests.py", "tests/test_utils.py"], "tests_to_run_kind": "test_file", "total": 2, "truncated": false, "always_run_total": 0, "why": {"tests/test_requests.py": "src/requests/_internal_utils.py changed (import-graph)", "tests/test_utils.py": "src/requests/_internal_utils.py changed (import-graph)"}, "basis_by_file": {"src/requests/_internal_utils.py": "import-graph"}, "summary": "2 test(s) to run for this change; the rest can be skipped."}, "health_delta": {"status": "available", "explanation": "Compared 1 changed files on both sides.", "basis": "both_sides_analyzed", "introduced": 2, "worsened": 0, "resolved": 0, "scope": {"changed": 1, "eligible": 1, "analyzed": 1, "skipped": 0, "failed": 0}, "top_findings": [{"id": "chf_7c35d63fcc13e52b", "change": "introduced", "dimension": "defect", "biomarker": "nested_complexity", "severity": "critical", "path": "src/requests/_internal_utils.py", "reason": "classify_headers nests 7 levels deep", "attribution": {"basis": "added_lines", "confidence": "high", "why": "Lines 55-85 are added or rewritten by this change."}, "inspect": "get_change_risk(finding_id='chf_7c35d63fcc13e52b')", "symbol": "classify_headers", "lines": [55, 85]}, {"id": "chf_1d478916e70baee8", "change": "introduced", "dimension": "defect", "biomarker": "complex_method", "severity": "high", "path": "src/requests/_internal_utils.py", "reason": "classify_headers has cyclomatic complexity 15", "attribution": {"basis": "added_lines", "confidence": "high", "why": "Lines 55-85 are added or rewritten by this change."}, "inspect": "get_change_risk(finding_id='chf_1d478916e70baee8')", "symbol": "classify_headers", "lines": [55, 85]}], "findings_total": 2, "findings_emitted": 2}}
/** A git work tree; get_change_risk answers with FINDINGS; records prompts and transcript lines. */
function reviewStubs(on: any, seen: { mcp: string[]; prompts: string[]; logs: string[] }): void {
diff --git a/packages/claude-mod/src/model/review.ts b/packages/claude-mod/src/model/review.ts
index 3cb684342..9a23e4bce 100644
--- a/packages/claude-mod/src/model/review.ts
+++ b/packages/claude-mod/src/model/review.ts
@@ -93,18 +93,29 @@ export function isRetryable(action: { type: string; reason?: string }): boolean
return action.type === "reviewFailed" && action.reason !== "timeout";
}
-/** The tests the review names, with how it knows them; null when it names none. */
-export function testsToRun(
- risk: ChangeRisk,
-): { tests: string[]; total: number; truncated: boolean; files: boolean; measured: boolean } | null {
+/**
+ * The tests the review names, with how it knows them; null when it names none.
+ * `runAll` is the reason every test must run: the named tests are then only
+ * the ones to run first.
+ */
+export function testsToRun(risk: ChangeRisk): {
+ tests: string[];
+ total: number;
+ truncated: boolean;
+ files: boolean;
+ measured: boolean;
+ runAll: string | null;
+} | null {
const block = risk.impacted_tests;
const tests = block?.tests_to_run ?? [];
- if (block === undefined || tests.length === 0) return null;
+ const runAll = block?.run_all === true ? (block.reasons?.[0] ?? block.summary ?? "") : null;
+ if (block === undefined || (tests.length === 0 && runAll === null)) return null;
return {
tests,
total: block.total ?? tests.length,
truncated: block.truncated === true,
files: block.tests_to_run_kind === "test_file",
measured: block.basis === "measured",
+ runAll,
};
}
diff --git a/packages/claude-mod/src/views/copy.ts b/packages/claude-mod/src/views/copy.ts
index 01d546c25..249effd37 100644
--- a/packages/claude-mod/src/views/copy.ts
+++ b/packages/claude-mod/src/views/copy.ts
@@ -154,7 +154,23 @@ export function testsBasis(measured: boolean): string {
return measured ? "measured by stored coverage" : "inferred from the dependency graph, not measured";
}
-export function testsLine(t: { tests: string[]; total: number; truncated: boolean; files: boolean; measured: boolean }): string {
+/** `Run every test: <reason>`, the line a selection that vouches for no subset gets. */
+export function runAllLine(reason: string): string {
+ return reason ? `Run every test: ${reason}` : "Run every test";
+}
+
+export function testsLine(t: {
+ tests: string[];
+ total: number;
+ truncated: boolean;
+ files: boolean;
+ measured: boolean;
+ runAll?: string | null;
+}): string {
+ if (t.runAll != null) {
+ const first = t.tests.length ? `; first: ${t.tests.join(", ")}` : "";
+ return `${runAllLine(t.runAll)}${first}`;
+ }
const count = countOf(t.total, t.files ? "test file" : "test", t.files ? "test files" : "tests");
const shown = t.truncated ? ` (first ${t.tests.length.toLocaleString("en-US")} shown)` : "";
return `Tests to run: ${count}, ${testsBasis(t.measured)}${shown}: ${t.tests.join(", ")}`;
@@ -179,7 +195,17 @@ export function directiveLines(d: { status: string; headline: string; reasons?:
}
/** The prompt `Run tests` submits, visible in the transcript. */
-export function runTestsPrompt(t: { tests: string[]; truncated: boolean; total: number; measured: boolean }): string {
+export function runTestsPrompt(t: {
+ tests: string[];
+ truncated: boolean;
+ total: number;
+ measured: boolean;
+ runAll?: string | null;
+}): string {
+ if (t.runAll != null) {
+ const first = t.tests.length ? `, starting with ${t.tests.join(" ")}` : "";
+ return `Run the whole test suite for this change${first}. Repowise cannot vouch for a smaller set: ${t.runAll}`;
+ }
const rest = t.truncated ? ` (the first ${t.tests.length} of ${t.total})` : "";
return `Run the tests Repowise names for this change${rest}, ${testsBasis(t.measured)}: ${t.tests.join(" ")}`;
}
@@ -405,7 +431,13 @@ export function findingsCounts(resolved: number, total: number): string {
return `${formatNumber(resolved)} resolved · ${countOf(total, "new finding", "new findings")}`;
}
-export function testsToRunCount(t: { total: number; files: boolean; measured: boolean }): string {
+export function testsToRunCount(t: {
+ total: number;
+ files: boolean;
+ measured: boolean;
+ runAll?: string | null;
+}): string {
+ if (t.runAll != null) return "every test";
const count = countOf(t.total, t.files ? "test file" : "test", t.files ? "test files" : "tests");
return `${count}, ${t.measured ? "measured" : "inferred"}`;
}
@@ -442,7 +474,8 @@ export function briefDecision(title: string, reviewed: boolean): string {
return reviewed ? `${title} (standing decision)` : `${title} (found in the code, not yet reviewed)`;
}
-export function briefTests(t: { tests: string[]; measured: boolean }): string {
+export function briefTests(t: { tests: string[]; measured: boolean; runAll?: string | null }): string {
+ if (t.runAll != null) return runAllLine(t.runAll).replace(/^Run/, "run");
return `tests to run, ${testsBasis(t.measured)}: ${t.tests.join(" ")}`;
}
diff --git a/packages/claude-mod/test/fixtures/change-risk/clear.json b/packages/claude-mod/test/fixtures/change-risk/clear.json
index 19dd7d4b8..bf2bf86e9 100644
--- a/packages/claude-mod/test/fixtures/change-risk/clear.json
+++ b/packages/claude-mod/test/fixtures/change-risk/clear.json
@@ -84,7 +84,10 @@
"exclude_patterns": [],
"is_fix": false,
"impacted_tests": {
- "status": "inferred",
+ "status": "selected",
+ "run_all": false,
+ "reasons": [],
+ "reasons_total": 0,
"basis": "inferred",
"map_present": false,
"tests_to_run": [
@@ -94,13 +97,15 @@
"tests_to_run_kind": "test_file",
"total": 2,
"truncated": false,
- "line_coverage": {
- "untested_changes": [],
- "stale_test_candidates": [],
- "covered": [],
- "no_coverage_data": []
+ "always_run_total": 0,
+ "why": {
+ "tests/test_requests.py": "src/requests/_internal_utils.py changed (import-graph)",
+ "tests/test_utils.py": "src/requests/_internal_utils.py changed (import-graph)"
+ },
+ "basis_by_file": {
+ "src/requests/_internal_utils.py": "import-graph"
},
- "summary": "2 test file(s) reach the changed files in the graph. Inferred from the dependency graph, not measured. For the line-precise answer build the map with `coverage run --contexts=test` then `repowise coverage add`."
+ "summary": "2 test(s) to run for this change; the rest can be skipped."
},
"diff_shape": "Diff shape: bigger than 34% of this repo's recent commits. Size and spread, not danger. Mechanics: get_change_risk(include=['diagnostics']).",
"_meta": {
diff --git a/packages/claude-mod/test/fixtures/change-risk/findings.json b/packages/claude-mod/test/fixtures/change-risk/findings.json
index 8625f5c44..ba6ee8893 100644
--- a/packages/claude-mod/test/fixtures/change-risk/findings.json
+++ b/packages/claude-mod/test/fixtures/change-risk/findings.json
@@ -135,7 +135,10 @@
"exclude_patterns": [],
"is_fix": false,
"impacted_tests": {
- "status": "inferred",
+ "status": "selected",
+ "run_all": false,
+ "reasons": [],
+ "reasons_total": 0,
"basis": "inferred",
"map_present": false,
"tests_to_run": [
@@ -145,13 +148,15 @@
"tests_to_run_kind": "test_file",
"total": 2,
"truncated": false,
- "line_coverage": {
- "untested_changes": [],
- "stale_test_candidates": [],
- "covered": [],
- "no_coverage_data": []
+ "always_run_total": 0,
+ "why": {
+ "tests/test_requests.py": "src/requests/_internal_utils.py changed (import-graph)",
+ "tests/test_utils.py": "src/requests/_internal_utils.py changed (import-graph)"
},
- "summary": "2 test file(s) reach the changed files in the graph. Inferred from the dependency graph, not measured. For the line-precise answer build the map with `coverage run --contexts=test` then `repowise coverage add`."
+ "basis_by_file": {
+ "src/requests/_internal_utils.py": "import-graph"
+ },
+ "summary": "2 test(s) to run for this change; the rest can be skipped."
},
"diff_shape": "Diff shape: bigger than 92% of this repo's recent commits. Size and spread, not danger. Mechanics: get_change_risk(include=['diagnostics']).",
"_meta": {
diff --git a/packages/claude-mod/test/fixtures/change-risk/overlap.json b/packages/claude-mod/test/fixtures/change-risk/overlap.json
index f28b9087c..b1eb2486c 100644
--- a/packages/claude-mod/test/fixtures/change-risk/overlap.json
+++ b/packages/claude-mod/test/fixtures/change-risk/overlap.json
@@ -84,7 +84,10 @@
"exclude_patterns": [],
"is_fix": false,
"impacted_tests": {
- "status": "inferred",
+ "status": "selected",
+ "run_all": false,
+ "reasons": [],
+ "reasons_total": 0,
"basis": "inferred",
"map_present": false,
"tests_to_run": [
@@ -94,13 +97,15 @@
"tests_to_run_kind": "test_file",
"total": 2,
"truncated": false,
- "line_coverage": {
- "untested_changes": [],
- "stale_test_candidates": [],
- "covered": [],
- "no_coverage_data": []
+ "always_run_total": 0,
+ "why": {
+ "tests/test_requests.py": "src/requests/_internal_utils.py changed (import-graph)",
+ "tests/test_utils.py": "src/requests/_internal_utils.py changed (import-graph)"
+ },
+ "basis_by_file": {
+ "src/requests/_internal_utils.py": "import-graph"
},
- "summary": "2 test file(s) reach the changed files in the graph. Inferred from the dependency graph, not measured. For the line-precise answer build the map with `coverage run --contexts=test` then `repowise coverage add`."
+ "summary": "2 test(s) to run for this change; the rest can be skipped."
},
"branch_overlap": {
"base": "origin/main",
diff --git a/packages/claude-mod/test/fixtures/change-risk/partial.json b/packages/claude-mod/test/fixtures/change-risk/partial.json
index 8d953b3c5..ec06e435b 100644
--- a/packages/claude-mod/test/fixtures/change-risk/partial.json
+++ b/packages/claude-mod/test/fixtures/change-risk/partial.json
@@ -97,7 +97,10 @@
"exclude_patterns": [],
"is_fix": false,
"impacted_tests": {
- "status": "inferred",
+ "status": "selected",
+ "run_all": false,
+ "reasons": [],
+ "reasons_total": 0,
"basis": "inferred",
"map_present": false,
"tests_to_run": [
@@ -107,13 +110,15 @@
"tests_to_run_kind": "test_file",
"total": 2,
"truncated": false,
- "line_coverage": {
- "untested_changes": [],
- "stale_test_candidates": [],
- "covered": [],
- "no_coverage_data": []
+ "always_run_total": 0,
+ "why": {
+ "tests/test_requests.py": "src/requests/_internal_utils.py changed (import-graph)",
+ "tests/test_utils.py": "src/requests/_internal_utils.py changed (import-graph)"
},
- "summary": "2 test file(s) reach the changed files in the graph. Inferred from the dependency graph, not measured. For the line-precise answer build the map with `coverage run --contexts=test` then `repowise coverage add`."
+ "basis_by_file": {
+ "src/requests/_internal_utils.py": "import-graph"
+ },
+ "summary": "2 test(s) to run for this change; the rest can be skipped."
},
"branch_overlap": {
"base": "origin/main",
diff --git a/packages/claude-mod/test/fixtures/change-risk/resolved.json b/packages/claude-mod/test/fixtures/change-risk/resolved.json
index 33174234f..de7347dfa 100644
--- a/packages/claude-mod/test/fixtures/change-risk/resolved.json
+++ b/packages/claude-mod/test/fixtures/change-risk/resolved.json
@@ -84,7 +84,10 @@
"exclude_patterns": [],
"is_fix": false,
"impacted_tests": {
- "status": "inferred",
+ "status": "selected",
+ "run_all": false,
+ "reasons": [],
+ "reasons_total": 0,
"basis": "inferred",
"map_present": false,
"tests_to_run": [
@@ -94,13 +97,15 @@
"tests_to_run_kind": "test_file",
"total": 2,
"truncated": false,
- "line_coverage": {
- "untested_changes": [],
- "stale_test_candidates": [],
- "covered": [],
- "no_coverage_data": []
+ "always_run_total": 0,
+ "why": {
+ "tests/test_requests.py": "src/requests/_internal_utils.py changed (import-graph)",
+ "tests/test_utils.py": "src/requests/_internal_utils.py changed (import-graph)"
+ },
+ "basis_by_file": {
+ "src/requests/_internal_utils.py": "import-graph"
},
- "summary": "2 test file(s) reach the changed files in the graph. Inferred from the dependency graph, not measured. For the line-precise answer build the map with `coverage run --contexts=test` then `repowise coverage add`."
+ "summary": "2 test(s) to run for this change; the rest can be skipped."
},
"diff_shape": "Diff shape: bigger than 18% of this repo's recent commits. Size and spread, not danger. Mechanics: get_change_risk(include=['diagnostics']).",
"_meta": {
@@ -110,7 +115,7 @@
"indexed_commit": "4ed3d1b3204c",
"live_head": "99025b48982b",
"index_behind": true,
- "stale_warning": "A file this response serves changed after indexing — verify against source or run `repowise update`.",
+ "stale_warning": "A file this response serves changed after indexing \u2014 verify against source or run `repowise update`.",
"index_scope": {
"version": 1,
"projection": "compact",
diff --git a/packages/claude-mod/test/fixtures/change-risk/unavailable.json b/packages/claude-mod/test/fixtures/change-risk/unavailable.json
index f1cd8520a..2bfaf8a27 100644
--- a/packages/claude-mod/test/fixtures/change-risk/unavailable.json
+++ b/packages/claude-mod/test/fixtures/change-risk/unavailable.json
@@ -6,7 +6,7 @@
"1 changed file were not analysed, so this is not a clean bill."
],
"next_actions": [
- "No measured test map; run the suite covering the changed files.",
+ "Run every test: src/requests/_internal_utils.py: no coverage, no test reaching it in the graph, no paired test.",
"Review the skipped files by hand; they were not compared."
]
},
@@ -71,20 +71,24 @@
"exclude_patterns": [],
"is_fix": false,
"impacted_tests": {
- "status": "no_map",
+ "status": "run_all",
+ "run_all": true,
+ "reasons": [
+ "src/requests/_internal_utils.py: no coverage, no test reaching it in the graph, no paired test."
+ ],
+ "reasons_total": 1,
"basis": "none",
"map_present": false,
"tests_to_run": [],
"tests_to_run_kind": null,
"total": 0,
"truncated": false,
- "line_coverage": {
- "untested_changes": [],
- "stale_test_candidates": [],
- "covered": [],
- "no_coverage_data": []
+ "always_run_total": 0,
+ "why": {},
+ "basis_by_file": {
+ "src/requests/_internal_utils.py": "unknown"
},
- "summary": "No per-test coverage map ingested and no test reaches the changed files in the graph; run the full suite. Inferred from the dependency graph, not measured. For the line-precise answer build the map with `coverage run --contexts=test` then `repowise coverage add`."
+ "summary": "Run every test: src/requests/_internal_utils.py: no coverage, no test reaching it in the graph, no paired test."
},
"branch_overlap": {
"base": "origin/main",
diff --git a/packages/claude-mod/test/review.test.ts b/packages/claude-mod/test/review.test.ts
index d38ab4e4c..091fc4c50 100644
--- a/packages/claude-mod/test/review.test.ts
+++ b/packages/claude-mod/test/review.test.ts
@@ -95,11 +95,22 @@ describe("review model", () => {
truncated: false,
files: true,
measured: false,
+ runAll: null,
});
- expect(testsToRun(risk("unavailable"))).toBeNull();
+ // No subset vouched for: every test runs, and that is the answer.
+ expect(testsToRun(risk("unavailable"))?.runAll).toBe(
+ "src/requests/_internal_utils.py: no coverage, no test reaching it in the graph, no paired test.",
+ );
expect(testsToRun(risk("nothing-to-score"))).toBeNull();
const measured = { impacted_tests: { basis: "measured", tests_to_run: ["t::a"], tests_to_run_kind: "test_id" } } as ChangeRisk;
- expect(testsToRun(measured)).toEqual({ tests: ["t::a"], total: 1, truncated: false, files: false, measured: true });
+ expect(testsToRun(measured)).toEqual({
+ tests: ["t::a"],
+ total: 1,
+ truncated: false,
+ files: false,
+ measured: true,
+ runAll: null,
+ });
});
});
@@ -160,6 +171,7 @@ describe("review card beneath the answer", () => {
"Change review (working tree, 1 changed file)",
"Health: not compared: No changed file is health-analyzable, so nothing was compared",
"Diff shape: bigger than 34% of this repo's recent commits; size, not danger",
+ "Run every test: src/requests/_internal_utils.py: no coverage, no test reaching it in the graph, no paired test.",
"Branches: ◦ 2 other branches also edit tox.ini (origin/3.0, origin/proposed/3.0.0)",
]);
});
@@ -267,7 +279,11 @@ describe("review band row", () => {
["review · health: partly compared, no new findings", " · ◦ 2 other branches edit these files", "1: Run tests", "3: Details"],
[],
],
- ["unavailable", ["review · health: not compared", " · ◦ 2 other branches edit these files", "3: Details"], []],
+ [
+ "unavailable",
+ ["review · health: not compared", " · ◦ 2 other branches edit these files", "1: Run tests", "3: Details"],
+ [],
+ ],
])("%s: health word in a health color only, then the buttons", (name, expected, expectedColors) => {
const row = reviewBandRow(done(name), 120);
expect(drawn(row)).toEqual(expected);
@@ -342,7 +358,9 @@ describe("review buttons", () => {
expect(runTestsText({ phase: "done", risk: measured })).toBe(
"Run the tests Repowise names for this change, measured by stored coverage: tests/a.py::t",
);
- expect(runTestsText(done("unavailable"))).toBeNull();
+ expect(runTestsText(done("unavailable"))).toBe(
+ "Run the whole test suite for this change. Repowise cannot vouch for a smaller set: src/requests/_internal_utils.py: no coverage, no test reaching it in the graph, no paired test.",
+ );
expect(runTestsText({ phase: "none" })).toBeNull();
});
diff --git a/packages/core/src/repowise/core/analysis/changed_lines.py b/packages/core/src/repowise/core/analysis/changed_lines.py
index bc599182c..b181a549d 100644
--- a/packages/core/src/repowise/core/analysis/changed_lines.py
+++ b/packages/core/src/repowise/core/analysis/changed_lines.py
@@ -297,12 +297,23 @@ class ChangeSet:
head: str | None
-def change_set(repo_path: str, revspec: str | None = None, *, staged: bool = False) -> ChangeSet:
+def change_set(
+ repo_path: str,
+ revspec: str | None = None,
+ *,
+ staged: bool = False,
+ working_tree: bool = False,
+) -> ChangeSet:
"""The change :func:`changed_lines` reads, with every touched path and its ends.
- Raises ``ValueError`` on an unknown revision, as :func:`changed_lines` does.
+ *working_tree* reads what ``HEAD`` does not have, staged or not (untracked
+ files stay out, as in :func:`changed_lines`); its base is ``HEAD``. Raises
+ ``ValueError`` on an unknown revision, as :func:`changed_lines` does.
"""
- command, revisions, label = _change_command(repo_path, revspec, staged=staged)
+ if working_tree:
+ command, revisions, label = ["diff"], ["HEAD"], working_tree_label()
+ else:
+ command, revisions, label = _change_command(repo_path, revspec, staged=staged)
tail = ["--no-renames", *revisions]
diffs = parse_unified_diff(_diff(repo_path, [*command, "--unified=0", *DIFF_ARGS, *tail]))
listing = _diff(repo_path, [*command, "--name-status", "-z", *tail])
@@ -316,7 +327,7 @@ def change_set(repo_path: str, revspec: str | None = None, *, staged: bool = Fal
deleted.add(path)
else:
files[path] = diffs.get(path) or FileDiff(path=path)
- base, head = _change_ends(repo_path, revspec, staged=staged)
+ base, head = _change_ends(repo_path, revspec, staged=staged or working_tree)
return ChangeSet(files, deleted, label, base, head)
diff --git a/packages/core/src/repowise/core/analysis/health/coverage/__init__.py b/packages/core/src/repowise/core/analysis/health/coverage/__init__.py
index 8a4d64531..12d28f047 100644
--- a/packages/core/src/repowise/core/analysis/health/coverage/__init__.py
+++ b/packages/core/src/repowise/core/analysis/health/coverage/__init__.py
@@ -17,7 +17,7 @@ from .decay import (
decay_since,
measurement_ref,
)
-from .detector import PARSERS, detect_format, is_test_file, paired_test_file, parse
+from .detector import PARSERS, detect_format, is_test_file, parse
from .discovery import (
CoverageConfig,
CoverageProvenance,
@@ -76,7 +76,6 @@ __all__ = [
"is_test_file",
"measurement_ref",
"normalize_report_path",
- "paired_test_file",
"parse",
"parse_clover",
"parse_cobertura",
diff --git a/packages/core/src/repowise/core/analysis/health/coverage/detector.py b/packages/core/src/repowise/core/analysis/health/coverage/detector.py
index ce3950303..86ed87490 100644
--- a/packages/core/src/repowise/core/analysis/health/coverage/detector.py
+++ b/packages/core/src/repowise/core/analysis/health/coverage/detector.py
@@ -29,7 +29,6 @@ from __future__ import annotations
import re
from collections.abc import Callable
-from pathlib import Path
from ....test_paths import is_test_related_path
from .clover import parse_clover
@@ -162,33 +161,3 @@ def is_test_file(rel_path: str, source: str | None = None) -> bool:
if any(token in head for token in _FRAMEWORK_IMPORTS):
return True
return False
-
-
-def paired_test_file(rel_path: str, all_paths: set[str]) -> str | None:
- """Return a candidate test-file path for *rel_path*, or ``None``."""
- p = Path(rel_path.replace("\\", "/"))
- stem = p.stem
- candidates = {
- f"test_{stem}.py",
- f"{stem}_test.py",
- f"{stem}.test.ts",
- f"{stem}.test.tsx",
- f"{stem}.test.js",
- f"{stem}.test.jsx",
- f"{stem}.test.mts",
- f"{stem}.test.cts",
- f"{stem}.spec.ts",
- f"{stem}.spec.js",
- f"{stem}.spec.mts",
- f"{stem}.spec.cts",
- f"{stem}_test.go",
- f"{stem}_spec.rb",
- f"{stem}Test.java",
- f"{stem}Tests.java",
- }
- for other in all_paths:
- norm = other.replace("\\", "/")
- base = norm.rsplit("/", 1)[-1]
- if base in candidates:
- return other
- return None
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
index 0fdb6781a..a880ba224 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
@@ -21,6 +21,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.analysis.health.grading import TARGET_SCORE
from repowise.core.analysis.health.perf.ranking import _percentile_threshold
from repowise.core.analysis.pr_blast import rank_tests_by_reach
+from repowise.core.analysis.test_collection import narrow_scopes
from repowise.core.analysis.test_reachability import (
DEFAULT_CALL_DEPTH,
MAX_TESTS_PER_TARGET,
@@ -33,7 +34,6 @@ from repowise.core.analysis.test_reachability import (
tests_matching_by_name,
tests_reaching_by_tier,
)
-from repowise.core.analysis.test_selection import expand_test_scopes
from repowise.core.code_origin import ship_rank
from repowise.core.test_paths import is_test_support_path, names_test_for, paired_test_names
@@ -1220,8 +1220,10 @@ async def _validation_inputs(
unreached = sorted(unanswered - inferred.keys())
if unreached:
inferred.update(tests_matching_by_name(unreached, test_files))
+ walked = {path: reached.all_tests or tuple(reached.tests) for path, reached in inferred.items()}
+ narrowed = await narrow_scopes(session, repository_id, walked, test_files)
inferred = {
- path: _expand_scopes(path, reached, test_files) for path, reached in inferred.items()
+ path: _expand_scopes(path, reached, narrowed[path]) for path, reached in inferred.items()
}
hub_targets = [path for path in target_files if path in hubs]
evidence = await _validation_evidence(
@@ -1237,8 +1239,12 @@ async def _validation_inputs(
)
-def _expand_scopes(path: str, reached: ReachedBy, test_files: set[str]) -> ReachedBy:
- """*reached* with a conftest or test package it stopped at replaced by the tests under it.
+def _expand_scopes(path: str, reached: ReachedBy, expanded: list[str]) -> ReachedBy:
+ """*reached* with a conftest or test package it stopped at replaced by *expanded*.
+
+ *expanded* is :func:`~repowise.core.analysis.test_collection.narrow_scopes`'
+ answer: a conftest reached only through its imports stands for the tests
+ the selection narrows it to, any other scope for the tests under it.
A validation command must name tests a runner collects; ``pytest
tests/conftest.py`` runs nothing. A scope with no runnable test under it
@@ -1251,7 +1257,6 @@ def _expand_scopes(path: str, reached: ReachedBy, test_files: set[str]) -> Reach
reads as incomplete instead of as the whole answer.
"""
found = reached.all_tests or tuple(reached.tests)
- expanded = expand_test_scopes(found, test_files)
if tuple(expanded) == tuple(found):
return reached
ranked = rank_tests(path, expanded)
diff --git a/packages/core/src/repowise/core/analysis/pr_blast.py b/packages/core/src/repowise/core/analysis/pr_blast.py
index ca3036b85..47e5887e5 100644
--- a/packages/core/src/repowise/core/analysis/pr_blast.py
+++ b/packages/core/src/repowise/core/analysis/pr_blast.py
@@ -15,7 +15,6 @@ co_change_partners_json field stored in git_metadata rows.
from __future__ import annotations
import math
-import os
from collections import Counter, defaultdict
from collections.abc import Iterable, Mapping
from typing import Any
@@ -42,7 +41,7 @@ def rank_tests_by_reach(by_file: Mapping[str, Iterable[str]]) -> list[str]:
return sorted(reach, key=lambda t: (-reach[t], t))
-#: Prefixes that name a module for its slot in a dispatch table rather than for
+#: Prefixes that name a module for its slot in a dispatch table, not for
#: its subject, so its tests are named after the subject alone: ``tool_dead_code``
#: is tested by ``test_dead_code``, not ``test_tool_dead_code``.
_ROLE_PREFIXES = ("tool_", "get_")
@@ -51,34 +50,62 @@ _ROLE_PREFIXES = ("tool_", "get_")
_MIN_STEM_LENGTH = 3
-def test_name_stems(base: str) -> list[tuple[str, bool]]:
- """Stems a test file for ``base`` could be named after, each with whether the
- match has to be exact.
-
- The full stem keeps the historical substring match, which absorbs suffixes
- like ``test_parser_edge_cases``. A stripped stem is shorter and so collides
- far more easily (``tool_repos`` -> ``repos``, a prefix of ``repository``),
- and clearing a gap on a coincidence is the one error that costs a reader.
- """
- stems = [(base, False)]
+def _subject_names(path: str) -> list[str]:
+ """*path*, and *path* with a dispatch-role prefix stripped from its file name."""
+ head, _, name = path.rpartition("/")
+ stem = name.split(".", 1)[0]
+ out = [path]
for prefix in _ROLE_PREFIXES:
- stripped = base[len(prefix) :]
- if base.startswith(prefix) and len(stripped) >= _MIN_STEM_LENGTH:
- stems.append((stripped, True))
- return stems
-
-
-def _names_a_test_for(stem: str, ext: str, test_path: str, exact: bool) -> bool:
- """Whether ``test_path`` follows a naming convention for ``stem``."""
- if exact:
- named = os.path.splitext(os.path.basename(test_path))[0]
- return named in (f"test_{stem}", f"{stem}_test", f"{stem}.spec")
- return (
- f"test_{stem}" in test_path
- or f"{stem}_test" in test_path
- or f"{stem}.spec.{ext}" in test_path
- or f"{stem}.spec." in test_path
+ if stem.startswith(prefix) and len(stem) - len(prefix) >= _MIN_STEM_LENGTH:
+ out.append(f"{head}/{name[len(prefix):]}" if head else name[len(prefix) :])
+ return out
+
+
+async def untested_files(session: AsyncSession, repo_id: str, paths: list[str]) -> list[str]:
+ """The code files among *paths* nothing can be shown to test, in their order.
+
+ Three signals, checked in descending order of what they can prove, and a
+ file only becomes a gap when all three stay silent. Asserting "nothing
+ tests this" is the one claim here that costs a reader something if it is
+ wrong, so the bar for making it is no evidence at all.
+
+ 1. A per-test coverage row (from ``repowise coverage add``) is
+ execution-*proof*: never a gap.
+ 2. A test file reaching it in the dependency graph is evidence, not proof,
+ but a recorded edge, not a guess, and it finds the suites that
+ name their tests for behaviour instead of for the file under test.
+ 3. Otherwise a test named for it by convention (``tests_matching_by_name``,
+ also tried with a dispatch-role prefix stripped), an honest "unknown",
+ never asserted as untested.
+
+ Test files, and files that are not code (a manifest, a workflow), are never
+ gaps: they need no test of their own.
+ """
+ from repowise.core.analysis.test_reachability import (
+ load_test_files,
+ tests_matching_by_name,
+ tests_reaching,
)
+ from repowise.core.analysis.test_selection import is_code_file
+ from repowise.core.persistence.crud import covered_source_files
+
+ if not paths:
+ return []
+ test_files = await load_test_files(session, repo_id)
+ candidates = [p for p in paths if is_code_file(p) and p not in test_files]
+ if not candidates:
+ return []
+ covered = await covered_source_files(session, repo_id, set(candidates))
+ # Degrades to "no signal" and never raises: a failed walk must not
+ # turn into a false accusation.
+ try:
+ reached = set(await tests_reaching(session, repo_id, candidates, test_files=test_files))
+ except Exception:
+ reached = set()
+ rest = [p for p in candidates if p not in covered and p not in reached]
+ subjects = {p: _subject_names(p) for p in rest}
+ named = tests_matching_by_name(sorted({n for ns in subjects.values() for n in ns}), test_files)
+ return [p for p in rest if not any(n in named for n in subjects[p])]
class PRBlastRadiusAnalyzer:
@@ -147,12 +174,8 @@ class PRBlastRadiusAnalyzer:
# 5. Test gaps
test_gaps = await self._find_test_gaps(all_affected_paths)
- # 6. Canonical test impact. MCP and REST consume this same typed
- # population; ``guarding_tests`` is its compatibility projection.
+ # 6. Canonical test impact: the typed population MCP and REST consume.
test_impact = await self._test_impact(changed_files, exclude_spec=exclude_spec)
- from repowise.core.analysis.test_impact import legacy_guarding_tests
-
- guarding_tests = legacy_guarding_tests(test_impact)
# 7. Structural impact heuristic (0-10). The compatibility field is an
# exact alias supplied by the shared public semantics contract.
@@ -165,7 +188,6 @@ class PRBlastRadiusAnalyzer:
"recommended_reviewers": recommended_reviewers,
"test_gaps": test_gaps,
"test_impact": test_impact,
- "guarding_tests": guarding_tests,
**structural_impact_contract(structural_impact_score),
}
@@ -388,83 +410,8 @@ class PRBlastRadiusAnalyzer:
return reviewers[:5]
async def _find_test_gaps(self, affected_files: list[str]) -> list[str]:
- """Return the files nothing can be shown to test.
-
- Three signals, checked in descending order of what they can prove, and a
- file only becomes a gap when all three stay silent. Asserting "nothing
- tests this" is the one claim here that costs a reader something if it is
- wrong, so the bar for making it is no evidence at all.
-
- 1. A per-test coverage row (from ``repowise coverage add``) is
- execution-*proof*: never a gap.
- 2. A test file reaching it in the dependency graph is evidence, not
- proof - control reaching a file is not a run exercising it - but it
- is a recorded edge rather than a guess, and it finds the suites that
- name their tests for behaviour instead of for the file under test.
- Used only as this floor; nothing downstream reads a coverage figure
- off it.
- 3. Otherwise the filename pattern (test_<name>, <name>_test,
- <name>.spec.*), tried against every stem in ``test_name_stems`` - an
- honest "unknown", never asserted as untested.
-
- Test files themselves are excluded; they don't need their own tests.
- """
- if not affected_files:
- return []
-
- from repowise.core.analysis.test_reachability import tests_reaching
- from repowise.core.persistence.crud import covered_source_files
-
- # Coverage-proven-tested files: absent from gaps regardless of naming.
- covered = await covered_source_files(self._session, self._repo_id, set(affected_files))
-
- # Graph-reached files: likewise absent. Degrades to "no signal" rather
- # than raising - a failed walk must not turn into a false accusation.
- try:
- reached = set(await tests_reaching(self._session, self._repo_id, affected_files))
- except Exception:
- reached = set()
-
- node_res = await self._session.execute(
- select(GraphNode.node_id, GraphNode.is_test).where(
- GraphNode.repository_id == self._repo_id,
- GraphNode.node_id.in_(affected_files),
- )
- )
- # Build a set of affected files that are themselves test files
- test_file_set = {row[0] for row in node_res.all() if row[1]}
-
- # Fetch all test paths for the filename fallback (map-no-data files only)
- all_test_res = await self._session.execute(
- select(GraphNode.node_id).where(
- GraphNode.repository_id == self._repo_id,
- GraphNode.is_test == True, # noqa: E712
- )
- )
- test_paths = {row[0] for row in all_test_res.all()}
-
- gaps = []
- for path in affected_files:
- # Skip test files — they don't need their own tests
- if path in test_file_set:
- continue
- # Coverage proves a test exercises this file: not a gap.
- if path in covered:
- continue
- # The graph records a test reaching it: not a gap either.
- if path in reached:
- continue
- base = os.path.splitext(os.path.basename(path))[0]
- ext = os.path.splitext(path)[1].lstrip(".")
- has_test = any(
- _names_a_test_for(stem, ext, tp, exact)
- for stem, exact in test_name_stems(base)
- for tp in test_paths
- )
- if not has_test:
- gaps.append(path)
-
- return gaps
+ """The affected files nothing can be shown to test (:func:`untested_files`)."""
+ return await untested_files(self._session, self._repo_id, affected_files)
async def _test_impact(self, changed_files: list[str], exclude_spec: Any = None) -> dict:
"""Canonical measured + inferred test-impact population."""
@@ -478,40 +425,6 @@ class PRBlastRadiusAnalyzer:
exclude_spec=exclude_spec,
)
- async def _guarding_tests(self, changed_files: list[str]) -> dict:
- """Compatibility projection of :meth:`_test_impact`.
-
- ``basis`` retains its historical measured/inferred/none domain and
- measured-first fallback. The canonical ``test_impact`` block contains
- the additive union and per-recommendation truth.
- """
- from repowise.core.analysis.test_impact import legacy_guarding_tests
-
- return legacy_guarding_tests(await self._test_impact(changed_files))
-
- async def _inferred_guarding_tests(self, changed_files: list[str], empty: dict) -> dict:
- """Compatibility wrapper returning only structurally inferred rows."""
- impact = await self._test_impact(changed_files)
- inferred = [row for row in impact["recommendations"] if row["basis"] == "inferred"]
- by_file = {
- item["source_file"]: item["inferred_tests"]
- for item in impact["files"]
- if item["inferred_tests"]
- }
- return {
- "map_present": empty.get("map_present", impact["coverage"]["map_present"]),
- "basis": "inferred" if inferred else "none",
- "tests_to_run": [row["test_id"] for row in inferred],
- "tests_to_run_with_basis": inferred,
- "tests_to_run_total": len(inferred),
- "tests_to_run_emitted": len(inferred),
- "tests_to_run_truncated": False,
- "by_file": by_file,
- "analysis": impact["analysis"],
- "coverage": impact["coverage"],
- "inference": impact["inference"],
- }
-
@staticmethod
def _compute_overall_risk(
direct_risks: list[dict],
diff --git a/packages/core/src/repowise/core/analysis/review_directive.py b/packages/core/src/repowise/core/analysis/review_directive.py
index 1c212995c..ed4399272 100644
--- a/packages/core/src/repowise/core/analysis/review_directive.py
+++ b/packages/core/src/repowise/core/analysis/review_directive.py
@@ -112,6 +112,8 @@ class CoveringTestEvidence:
#: Uncapped. A surface decides how many to show.
tests_to_run: tuple[str, ...] = ()
basis: str | None = None # measured | inferred | None
+ #: Why every test must run, when nothing narrower can be vouched for.
+ run_all: str | None = None
def _plural(word: str, count: int) -> str:
@@ -198,7 +200,17 @@ def _actions(delta: ChangeHealthDelta, tests: CoveringTestEvidence) -> tuple[Rev
)
)
next_priority = len(actions)
- if tests.tests_to_run:
+ if tests.run_all:
+ actions.append(
+ ReviewAction(
+ kind="run_tests",
+ priority=next_priority,
+ evidence_basis=tests.basis,
+ explanation=f"Run every test: {tests.run_all}",
+ )
+ )
+ next_priority += 1
+ elif tests.tests_to_run:
actions.append(
ReviewAction(
kind="run_tests",
diff --git a/packages/core/src/repowise/core/analysis/test_collection.py b/packages/core/src/repowise/core/analysis/test_collection.py
index 129b0664b..309fa1410 100644
--- a/packages/core/src/repowise/core/analysis/test_collection.py
+++ b/packages/core/src/repowise/core/analysis/test_collection.py
@@ -20,10 +20,11 @@ and the gap as inputs is the upgrade.
from __future__ import annotations
+import asyncio
import sys
-from collections.abc import Callable
+from collections.abc import Callable, Collection, Mapping, Sequence
from dataclasses import dataclass
-from pathlib import Path
+from pathlib import Path, PurePosixPath
from typing import Any, NamedTuple
import structlog
@@ -69,7 +70,7 @@ def read_checkout(repo_path) -> Checkout:
except OSError:
return None
- tracked = git_refs.tracked_paths(str(root))
+ tracked = sorted(git_refs.tracked_paths(str(root)))
texts = [
(p, text)
for p in tracked
@@ -80,6 +81,20 @@ def read_checkout(repo_path) -> Checkout:
return Checkout(tracked, texts, roots, read, lambda p: (root / p).is_file())
+class SelectionCancelledError(Exception):
+ """The caller gave up on this selection; whatever it had found is discarded."""
+
+
+def _never() -> bool:
+ return False
+
+
+def _stop(cancelled: Callable[[], bool]) -> bool:
+ if cancelled():
+ raise SelectionCancelledError
+ return False
+
+
class Plan(NamedTuple):
"""Who names each changed doc or asset, the scope each scoped file runs, and its routes."""
@@ -88,8 +103,14 @@ class Plan(NamedTuple):
routes: list[str]
-def plan_scopes(change, config, checkout: Checkout) -> Plan:
- """The change's scopes, reading the sources once and only when a file needs its namers."""
+def plan_scopes(
+ change, config, checkout: Checkout, cancelled: Callable[[], bool] = _never
+) -> Plan:
+ """The change's scopes, reading the sources once and only when a file needs its namers.
+
+ *cancelled* is polled between file reads, so an abandoned selection stops
+ reading the checkout.
+ """
from .selection_scopes import (
keeps_full_run,
needs_namers,
@@ -107,7 +128,7 @@ def plan_scopes(change, config, checkout: Checkout) -> Plan:
texts = (
(p, known[p]) if p in known else (p, checkout.read(p) or "")
for p in checkout.tracked
- if is_scan_source(p)
+ if is_scan_source(p) and not _stop(cancelled)
)
namers = file_namers(asked, texts)
scopes = trigger_scopes(paths, checkout.tracked, namers, config)
@@ -149,13 +170,18 @@ async def collect(
summary = await get_test_coverage_summary(session, repo_id)
out["map_empty"] = summary.get("pair_count", 0) == 0
out["map_truncated"] = summary.get("pair_count", 0) >= MAX_TEST_COVERAGE_ROWS
- measured = summary.get("ingested_commit_sha")
+ out["measured_commit"] = measured = summary.get("ingested_commit_sha")
out["map_current"] = out["map_empty"] or measured in {change.base, change.head} - {None}
# Repo file keys back the filename-pattern fallback (same source the
# aggregate coverage ingest resolves against).
repo_keys = {m.file_path for m in await get_health_metrics(session, repo_id)}
- routes = [] if config is None else _gap_routes(repo_path, change, config, repo_keys, out)
+ # git runs off the event loop: a server shares it with other requests.
+ routes = (
+ []
+ if config is None
+ else await asyncio.to_thread(_gap_routes, repo_path, change, config, repo_keys, out)
+ )
routes = sorted({*routes, *scope_routes})
await resolve_impacted(
session,
@@ -294,6 +320,7 @@ def empty_result(changed_files: int) -> dict:
"map_empty": False,
"map_current": True,
"map_truncated": False,
+ "measured_commit": None,
"indexed_commit": None,
"index_problem": None,
"index_gap": None,
@@ -354,7 +381,7 @@ async def resolve_impacted(
Nothing said anything. Run the full suite.
"""
from ..persistence.crud import tests_covering, tests_covering_files
- from .health.coverage import paired_test_file
+ from .test_reachability import tests_matching_by_name
covered: dict[str, dict] = out["covered"]
has_rows: set[str] = set()
@@ -395,12 +422,12 @@ async def resolve_impacted(
continue
if source_file in has_rows or source_file not in changed:
continue # coverage answered, or a route; a name-shaped guess adds nothing
- guess = paired_test_file(source_file, repo_keys)
+ guess = tests_matching_by_name([source_file], repo_keys).get(source_file)
if guess:
out["inferred"].append(
{
"source_file": source_file,
- "test_file": guess,
+ "test_file": guess.tests[0],
"via": "filename-pattern",
}
)
@@ -552,7 +579,12 @@ def select(change, result: dict, config, checkout: Checkout, plan: Plan) -> Sele
map_truncated=result["map_truncated"],
index_gap=result["index_gap"],
gap=result["gap"],
- index_problem=result["index_problem"],
+ index_problem=result["index_problem"]
+ or (
+ None
+ if tracked
+ else "git could not list the checkout's files, so its tests are unknown."
+ ),
graph_error=result["graph_error"],
missing={f for f in named if not checkout.exists(f)},
go_test_dirs=go_test_dirs,
@@ -575,9 +607,15 @@ async def select_for_change(
checkout: Checkout,
*,
indexed_commit: str | None = None,
+ cancelled: Callable[[], bool] = _never,
) -> tuple[dict[str, Any], Selection]:
- """:func:`plan_scopes`, :func:`collect` then :func:`select`: what *change* needs, and why."""
- plan = plan_scopes(change, config, checkout)
+ """:func:`plan_scopes`, :func:`collect` then :func:`select`: what *change* needs, and why.
+
+ Raises :class:`SelectionCancelledError` once *cancelled* turns true.
+ """
+ # The namer search reads every source when a doc or asset changed: off the loop.
+ plan = await asyncio.to_thread(plan_scopes, change, config, checkout, cancelled)
+ _stop(cancelled)
result = await collect(
session,
repo_id,
@@ -591,4 +629,51 @@ async def select_for_change(
indexed_commit=indexed_commit,
)
result["diff"] = change.label
- return result, select(change, result, config, checkout, plan)
+ _stop(cancelled)
+ return result, await asyncio.to_thread(select, change, result, config, checkout, plan)
+
+
+async def narrow_scopes(
+ session, repo_id: str, reached: Mapping[str, Sequence[str]], test_files: Collection[str]
+) -> dict[str, list[str]]:
+ """*reached* with each conftest or test package a walk stopped at replaced by its tests.
+
+ For surfaces that walk a few hops and do not select: a conftest reached
+ only through its imports stands for the tests the selection narrows it to
+ (:mod:`repowise.core.analysis.conftest_routes`, the same graph walk), and
+ any other scope for every runnable test under its directory. The walk runs
+ only for targets that reached a scope. The conftests and pytest configs
+ (nested ones included) come from :func:`read_checkout` of the indexed
+ checkout. A target the walk gives nothing for, or a checkout that cannot
+ be read, keeps every test under each scope it reached. Keys may be symbol
+ ids (``path::name``); the walk starts at their file.
+ """
+ from ..persistence.models import Repository
+ from .test_selection import expand_test_scopes, scope_kind
+
+ scoped = {t: list(v) for t, v in reached.items() if any(scope_kind(x) for x in v)}
+ out = {t: list(v) for t, v in reached.items()}
+ if not scoped:
+ return out
+ local = await session.get(Repository, repo_id)
+ texts: dict[str, str] = {}
+ if local is not None and local.local_path and Path(local.local_path).is_dir():
+ try:
+ checkout = await asyncio.to_thread(read_checkout, local.local_path)
+ texts = dict(checkout.pytest_texts)
+ except Exception as exc: # unread conftests keep every test under them
+ log.debug("narrow_scopes_checkout_failed", error=str(exc))
+ files = sorted({t.split("::", 1)[0] for t in scoped})
+ try:
+ candidates, _, _ = await _graph_candidates(session, repo_id, files, pytest_texts=texts)
+ except Exception as exc: # the walk's own answer stands: every test under each scope
+ log.debug("narrow_scopes_failed", error=str(exc))
+ candidates = {}
+ for target, tests in scoped.items():
+ scopes = [x for x in tests if scope_kind(x)]
+ dirs = {str(PurePosixPath(s).parent) for s in scopes}
+ picked = [t for t, _ in candidates.get(target.split("::", 1)[0], ())]
+ under = [t for t in picked if any(d == "." or t.startswith(f"{d}/") for d in dirs)]
+ kept = [x for x in tests if not scope_kind(x)]
+ out[target] = expand_test_scopes([*kept, *(under or scopes)], test_files)
+ return out
diff --git a/packages/core/src/repowise/core/analysis/test_impact.py b/packages/core/src/repowise/core/analysis/test_impact.py
index 9b2b403b6..f9e38f872 100644
--- a/packages/core/src/repowise/core/analysis/test_impact.py
+++ b/packages/core/src/repowise/core/analysis/test_impact.py
@@ -69,37 +69,6 @@ def _recommendation_sort_key(row: dict[str, Any]) -> tuple[int, int, str, str]:
)
-def legacy_guarding_tests(test_impact: dict[str, Any]) -> dict[str, Any]:
- """Compatibility projection for the historical ``guarding_tests`` block.
-
- Compatibility preserves the historical measured-first contract: measured
- rows win when any exist, otherwise inferred rows are the fallback. Consumers
- that need the additive union and per-row truth read ``recommendations``.
- """
- recommendations = test_impact["recommendations"]
- measured = [row for row in recommendations if row["basis"] == "measured"]
- selected = measured or [row for row in recommendations if row["basis"] == "inferred"]
- basis = selected[0]["basis"] if selected else "none"
- by_file = {
- row["source_file"]: row[f"{basis}_tests"]
- for row in test_impact["files"]
- if basis != "none" and row[f"{basis}_tests"]
- }
- return {
- "map_present": test_impact["coverage"]["map_present"],
- "basis": basis,
- "tests_to_run": [row["test_id"] for row in selected],
- "tests_to_run_with_basis": selected,
- "tests_to_run_total": len(selected),
- "tests_to_run_emitted": len(selected),
- "tests_to_run_truncated": False,
- "by_file": by_file,
- "analysis": test_impact["analysis"],
- "coverage": test_impact["coverage"],
- "inference": test_impact["inference"],
- }
-
-
async def analyze_test_impact(
session: AsyncSession,
repository_id: str,
@@ -170,6 +139,13 @@ async def analyze_test_impact(
path: {"tests": list(reached.all_tests or reached.tests), "via": reached.via}
for path, reached in reached_by_file.items()
}
+ if inference_error is None:
+ from repowise.core.analysis.test_collection import narrow_scopes
+
+ walked = {path: row["tests"] for path, row in inferred.items()}
+ for path, tests in (await narrow_scopes(session, repository_id, walked, test_files)).items():
+ if tests != walked[path]:
+ inferred[path] = {**inferred[path], "tests": tests, "narrowed": True}
return assemble_test_impact(
changed,
@@ -217,7 +193,8 @@ def assemble_test_impact(
rows (``test_id``, optional ``test_file`` and ``source_format``), read only
when *coverage_summary* reports pairs. *inferred_by_file* maps a changed path
to ``{"tests": [...], "via": tier}``, both keys required, with ``tests`` the
- uncapped list (``ReachedBy.all_tests``, not the display-capped ``tests``);
+ uncapped list (``ReachedBy.all_tests``, not the display-capped ``tests``), and
+ ``narrowed`` true when a reached conftest was already resolved to its tests;
paths outside *changed_files* are ignored. That list is used as the walk
found it, except where a scope file expanded (below). *coverage_summary* carries
``pair_count``, ``test_count``, ``source_file_count``, ``ingested_at``,
@@ -334,7 +311,7 @@ def assemble_test_impact(
if not (exclude_spec and is_excluded(test_id, exclude_spec))
]
inferred_totals_by_file[path] = len(kept_tests)
- if expanded != list(reached["tests"]):
+ if reached.get("narrowed") or expanded != list(reached["tests"]):
# A root conftest stands for every test in the repository: rank the
# expansion nearest-first and keep the walk's own per-target cap.
kept_tests = rank_tests(path, kept_tests)[:MAX_TESTS_PER_TARGET]
diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.py
index dbe4be62e..eed3d1b8d 100644
--- a/packages/core/src/repowise/core/analysis/test_reachability.py
+++ b/packages/core/src/repowise/core/analysis/test_reachability.py
@@ -203,6 +203,7 @@ __all__ = [
"call_graph_from_graph",
"clear_test_map_cache",
"direct_dependents",
+ "direct_importers",
"files_reached_by_tests",
"files_with_paired_tests",
"imported_names_by_test",
@@ -1009,6 +1010,17 @@ _LEGACY_CONFTEST_FILTER = (
)
+#: Edges a test file writes itself: imports and references, not the wiring a
+#: test runner adds (a conftest or setup file reaches every test under it).
+_AUTHORED_EDGE_TYPES = sorted(FILE_DEPENDENCY_EDGE_TYPES - {"framework"})
+
+
+async def direct_importers(session: AsyncSession, repo_id: str, files: list[str]) -> set[str]:
+ """Files whose own code imports or references one of *files*, one hop."""
+ rows = await _edges_into(session, repo_id, files, _AUTHORED_EDGE_TYPES, frozenset())
+ return {source for source, _ in rows}
+
+
def _in_clause(prefix: str, values: list[str], params: dict[str, Any]) -> str:
"""Bind *values* as ``:prefix0, :prefix1, ...`` and return the clause body."""
params.update({f"{prefix}{i}": v for i, v in enumerate(values)})
diff --git a/packages/core/src/repowise/core/workspace/test_impact.py b/packages/core/src/repowise/core/workspace/test_impact.py
index b735f84b2..ff81731c1 100644
--- a/packages/core/src/repowise/core/workspace/test_impact.py
+++ b/packages/core/src/repowise/core/workspace/test_impact.py
@@ -19,6 +19,7 @@ from dataclasses import dataclass, field, replace
from pathlib import Path
from typing import TYPE_CHECKING, Any
+from repowise.core.analysis.test_collection import narrow_scopes
from repowise.core.analysis.test_reachability import (
DEFAULT_CALL_DEPTH,
DEFAULT_MAX_DEPTH,
@@ -263,6 +264,7 @@ async def _analyze_consumer(
measured: dict[str, list[dict[str, Any]]] = {}
reached: dict[str, Any] = {}
test_files: set[str] = set()
+ narrowed: dict[str, list[str]] = {}
# A pass that fails takes down only its own signal; whatever the other pass
# found, and the links already classified, still reach the caller.
failures: list[str] = []
@@ -315,6 +317,12 @@ async def _analyze_consumer(
scope_kind(t) for hit in reached.values() for t in hit.all_tests or hit.tests
):
test_files = await load_test_files(session, repo_index.repo_id)
+ narrowed = await narrow_scopes(
+ session,
+ repo_index.repo_id,
+ {k: hit.all_tests or hit.tests for k, hit in reached.items()},
+ test_files,
+ )
except Exception as exc:
failures.append(f"inferred: {type(exc).__name__}")
@@ -366,7 +374,12 @@ async def _analyze_consumer(
continue
# The walk trims its own list per target; the join caps per consumer
# and provider pair below and reports the cut, so start from all of them.
- reached_tests = expand_test_scopes(hit.all_tests or hit.tests, test_files)
+ key = link.consumer_symbol_id if link.consumer_symbol_id in reached else path
+ reached_tests = (
+ narrowed[key]
+ if key in narrowed
+ else expand_test_scopes(hit.all_tests or hit.tests, test_files)
+ )
if not reached_tests:
continue
file_tests.update(reached_tests)
diff --git a/packages/server/src/repowise/server/mcp_server/_budget/contracts.py b/packages/server/src/repowise/server/mcp_server/_budget/contracts.py
index 68fa560d0..8db4b1581 100644
--- a/packages/server/src/repowise/server/mcp_server/_budget/contracts.py
+++ b/packages/server/src/repowise/server/mcp_server/_budget/contracts.py
@@ -96,7 +96,6 @@ _CONTRACTS: dict[str, ResponseBudgetContract] = {
"blocks",
(
"global_hotspots",
- "pr_blast_radius.guarding_tests",
"pr_blast_radius",
"directive.test_recommendations[]",
"directive.tests_to_run[]",
diff --git a/packages/server/src/repowise/server/mcp_server/_change_health.py b/packages/server/src/repowise/server/mcp_server/_change_health.py
index 164530ae0..2c6781bb7 100644
--- a/packages/server/src/repowise/server/mcp_server/_change_health.py
+++ b/packages/server/src/repowise/server/mcp_server/_change_health.py
@@ -16,6 +16,7 @@ from repowise.core.analysis.review_directive import (
ReviewDirective,
review_directive,
)
+from repowise.server.mcp_server._test_selection import UNAVAILABLE_REASON
#: Actionable findings carried in the default response. The rest are counted
#: and recoverable by an exact call, never silently dropped.
@@ -52,13 +53,13 @@ def _test_evidence(tests: dict[str, Any] | None) -> CoveringTestEvidence:
# report and nothing to ask for.
return CoveringTestEvidence()
to_run = tuple(tests.get("tests_to_run") or ())
- if to_run:
- state = "available"
- elif tests.get("status") in {"no_map", "no_index"}:
- state = "unavailable"
- else:
- state = "available"
- return CoveringTestEvidence(state=state, tests_to_run=to_run, basis=tests.get("basis"))
+ state = "unavailable" if tests.get("status") == "no_index" else "available"
+ run_all = None
+ if tests.get("run_all"):
+ run_all = (tests.get("reasons") or [UNAVAILABLE_REASON])[0]
+ return CoveringTestEvidence(
+ state=state, tests_to_run=to_run, basis=tests.get("basis"), run_all=run_all
+ )
def _render_actions(decided: ReviewDirective) -> list[str]:
@@ -80,7 +81,7 @@ def _render_action(action: ReviewAction, inspected: int) -> str | None:
if inspected >= _ACTIONABLE_FINDINGS:
return None
return f"{action.explanation} ({action.targets[0]})"
- if action.kind == "run_tests":
+ if action.kind == "run_tests" and action.targets:
return f"Run: {' '.join(action.targets[:_TESTS_SHOWN])}"
return action.explanation
diff --git a/packages/server/src/repowise/server/mcp_server/_test_selection.py b/packages/server/src/repowise/server/mcp_server/_test_selection.py
new file mode 100644
index 000000000..b93451ae9
--- /dev/null
+++ b/packages/server/src/repowise/server/mcp_server/_test_selection.py
@@ -0,0 +1,251 @@
+"""The tests a change needs, for the agent tools: one selection, rendered small.
+
+``get_change_risk`` and ``get_risk`` ask the same question ``repowise
+impacted-tests`` answers, so they run the same collection and the same
+fail-closed :func:`~repowise.core.analysis.test_selection.select_tests`
+(:mod:`repowise.core.analysis.test_collection`) and only shape the answer:
+whether every test must run and why, the head of the run list with the reason
+each test is in it, and which evidence decided each changed file.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import threading
+from pathlib import Path
+from typing import Any
+
+from repowise.core.analysis.test_selection import Selection, selected_by_change
+from repowise.server.mcp_server._budget import OmissionCollector
+
+#: Run-all reasons carried on the wire; the rest go to the omission store.
+_REASONS_LIMIT = 3
+#: Changed files whose deciding evidence is named.
+_BASIS_LIMIT = 10
+#: Past this the agent is told to run the CLI instead of waiting. Ceiling: a
+#: change whose triggers scope a whole workspace walks every route to it (two
+#: minutes on a 2k-test repository); loading the graph once per call is the
+#: upgrade that lifts it.
+SELECTION_TIMEOUT_SECONDS = 30.0
+
+#: What a failed selection tells the reader: it vouches for no subset.
+UNAVAILABLE_REASON = (
+ "The test selection is unavailable, so run every test (or `repowise impacted-tests`)."
+)
+
+#: Repository path -> the cancel flag of a selection still running there. One
+#: at a time per repository: a selection past its budget keeps its threads
+#: until they notice the flag, and a second one would only add to them.
+_IN_FLIGHT: dict[str, threading.Event] = {}
+
+
+class SelectionUnavailableError(Exception):
+ """No selection could be made; ``status`` and the message say why."""
+
+ def __init__(self, status: str, message: str) -> None:
+ super().__init__(message)
+ self.status = status
+
+
+async def select_change_tests(
+ repo_path: Any, session_factory: Any, change: Any
+) -> tuple[dict[str, Any], Selection, Any]:
+ """``(collection result, selection, checkout)`` for *change*, or
+ :class:`SelectionUnavailableError`.
+
+ The checkout read (``git ls-files`` plus pytest's conftests and configs)
+ and every git call run off the event loop.
+ """
+ from repowise.core.analysis.test_selection import TestSelectionConfig
+ from repowise.core.repo_config import RepoConfigError, load_repo_config
+
+ try:
+ config = TestSelectionConfig.from_repo_config(load_repo_config(repo_path))
+ except (RepoConfigError, ValueError) as exc:
+ raise SelectionUnavailableError("config_invalid", f"tests config is invalid: {exc}") from exc
+ key = str(Path(repo_path).resolve())
+ if key in _IN_FLIGHT:
+ raise SelectionUnavailableError(
+ "busy",
+ "A test selection for this repository is still running; run "
+ "`repowise impacted-tests` for this change.",
+ )
+ cancel = _IN_FLIGHT[key] = threading.Event()
+ task = asyncio.ensure_future(_select(repo_path, session_factory, change, config, cancel))
+ # Released when the selection ends, not when the caller stops waiting.
+ task.add_done_callback(lambda done: _release(key, done))
+ try:
+ done, _ = await asyncio.wait({task}, timeout=SELECTION_TIMEOUT_SECONDS)
+ except asyncio.CancelledError:
+ cancel.set()
+ raise
+ if not done:
+ cancel.set()
+ raise SelectionUnavailableError(
+ "timeout",
+ f"Selecting tests took over {SELECTION_TIMEOUT_SECONDS:.0f} s; run "
+ "`repowise impacted-tests` for this change.",
+ )
+ return task.result()
+
+
+def _release(key: str, task: asyncio.Future) -> None:
+ _IN_FLIGHT.pop(key, None)
+ # An abandoned selection ends in SelectionCancelledError; nobody reads it.
+ if not task.cancelled():
+ task.exception()
+
+
+async def _select(
+ repo_path: Any, session_factory: Any, change: Any, config: Any, cancel: threading.Event
+) -> tuple:
+ from repowise.core.analysis.test_collection import read_checkout, select_for_change
+ from repowise.core.persistence.database import get_session
+ from repowise.server.mcp_server._helpers import _get_repo
+
+ checkout = await asyncio.to_thread(read_checkout, repo_path)
+ async with get_session(session_factory) as session:
+ repository = await _get_repo(session)
+ result, selection = await select_for_change(
+ session,
+ repository.id,
+ repo_path,
+ change,
+ config,
+ checkout,
+ indexed_commit=repository.head_commit,
+ cancelled=cancel.is_set,
+ )
+ return result, selection, checkout
+
+
+def files_change(repo_path: Any, paths: list[str]) -> Any:
+ """A change known only by its paths: no lines, based at ``HEAD``.
+
+ A path the checkout lacks reads as deleted. With no lines every file is
+ matched by file, so coverage at any commit names the tests touching it.
+ """
+ from repowise.core import git_refs
+ from repowise.core.analysis.changed_lines import ChangeSet, FileDiff
+
+ root = Path(repo_path)
+ paths = [p.replace("\\", "/").removeprefix("./") for p in paths]
+ present = {p for p in paths if (root / p).is_file()}
+ return ChangeSet(
+ files={p: FileDiff(path=p) for p in sorted(present)},
+ deleted=set(paths) - present,
+ label="the changed files",
+ base=git_refs.resolve(str(root), "HEAD") or None,
+ head=None,
+ )
+
+
+def run_order(result: dict[str, Any], selection: Selection) -> list[str]:
+ """``selection.tests`` ordered for a reader who runs the head first.
+
+ A test the change edits leads, then tests reaching more of the changed
+ files, then the selection's own order; tests that run with every subset
+ come last, since no changed file named them.
+ """
+ changed = set(selection.basis)
+ reach: dict[str, set[str]] = {}
+ for row in result.get("inferred") or ():
+ reach.setdefault(row["test_file"], set()).add(row["source_file"])
+ for info in (result.get("covered") or {}).values():
+ if info.get("test_file"):
+ reach.setdefault(info["test_file"], set()).update(info.get("source_files") or ())
+
+ def key(item: tuple[int, str]) -> tuple[bool, bool, int, int]:
+ index, test = item
+ path = test.split("::", 1)[0]
+ hits = len(reach.get(path, set()) & changed)
+ return path not in changed, not selected_by_change(selection, path), -hits, index
+
+ return [test for _, test in sorted(enumerate(selection.tests), key=key)]
+
+
+def run_kind(tests: list[str]) -> str | None:
+ """``test_id`` when a coverage node id is listed, ``test_file`` otherwise, ``None`` empty."""
+ if not tests:
+ return None
+ return "test_id" if any("::" in t for t in tests) else "test_file"
+
+
+def basis_of(selection: Selection) -> str:
+ """``measured`` when coverage decided any changed file, ``inferred`` when only the
+ graph or a rule did, ``none`` when nothing named a test."""
+ if not selection.tests:
+ return "none"
+ return "measured" if "coverage" in selection.basis.values() else "inferred"
+
+
+def selection_block(
+ result: dict[str, Any],
+ selection: Selection,
+ collector: OmissionCollector,
+ *,
+ limit: int,
+ label: str,
+) -> dict[str, Any]:
+ """The wire view of *selection*: verdict, reasons, the run list's head and why.
+
+ ``tests_to_run`` empty with ``run_all`` false means the change needs no
+ test (documentation, a deleted test); with ``run_all`` true it means nothing
+ narrower than the full suite is known.
+ """
+ ordered = run_order(result, selection)
+ shown = ordered[:limit]
+ if len(ordered) > limit:
+ collector.add(
+ f"{label}.tests_to_run beyond cap={limit} ({len(ordered) - limit} dropped)",
+ ordered[limit:],
+ )
+ reasons = list(selection.reasons)
+ if len(reasons) > _REASONS_LIMIT:
+ collector.add(
+ f"{label}.reasons beyond cap={_REASONS_LIMIT} "
+ f"({len(reasons) - _REASONS_LIMIT} dropped)",
+ reasons[_REASONS_LIMIT:],
+ )
+ basis_rows = sorted(selection.basis.items())
+ always = sum(not selected_by_change(selection, t) for t in selection.test_files)
+ block: dict[str, Any] = {
+ "status": "run_all" if selection.run_all else "selected",
+ "run_all": selection.run_all,
+ "reasons": reasons[:_REASONS_LIMIT],
+ "reasons_total": len(reasons),
+ "basis": basis_of(selection),
+ "map_present": not result.get("map_empty", True),
+ "tests_to_run": shown,
+ "tests_to_run_kind": run_kind(shown),
+ "total": len(ordered),
+ "truncated": len(ordered) > limit,
+ "always_run_total": always,
+ "why": {
+ path: selection.why[path]
+ for path in dict.fromkeys(t.split("::", 1)[0] for t in shown)
+ if path in selection.why
+ },
+ "basis_by_file": dict(basis_rows[:_BASIS_LIMIT]),
+ "summary": _summary(selection, len(ordered), always, limit),
+ }
+ if len(basis_rows) > _BASIS_LIMIT:
+ block["basis_by_file_total"] = len(basis_rows)
+ collector.add(
+ f"{label}.basis_by_file beyond cap={_BASIS_LIMIT} "
+ f"({len(basis_rows) - _BASIS_LIMIT} dropped)",
+ dict(basis_rows[_BASIS_LIMIT:]),
+ )
+ return block
+
+
+def _summary(selection: Selection, total: int, always: int, limit: int) -> str:
+ shown = f"; showing first {limit}" if total > limit else ""
+ if selection.run_all:
+ lead = selection.reasons[0] if selection.reasons else "the selection could not vouch"
+ named = f" Run these {total} first{shown}." if total else ""
+ return f"Run every test: {lead}{named}"
+ if not total:
+ return "No test needs to run for this change."
+ rule = f", {always} of them because they run with every subset" if always else ""
+ return f"{total} test(s) to run for this change{rule}{shown}; the rest can be skipped."
diff --git a/packages/server/src/repowise/server/mcp_server/tool_change_risk.py b/packages/server/src/repowise/server/mcp_server/tool_change_risk.py
index bfbfacefb..3fc48941e 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_change_risk.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_change_risk.py
@@ -26,7 +26,6 @@ from repowise.core.analysis.change_risk import (
normalize_extensions,
score_live_change,
)
-from repowise.core.analysis.pr_blast import rank_tests_by_reach
from repowise.core.analysis.prior_fix_impact import (
FixRecord,
PriorFixFile,
@@ -72,6 +71,12 @@ from repowise.server.mcp_server._test_impact import (
cross_repo_tests,
tests_block_for,
)
+from repowise.server.mcp_server._test_selection import (
+ UNAVAILABLE_REASON,
+ SelectionUnavailableError,
+ select_change_tests,
+ selection_block,
+)
log = structlog.get_logger(__name__)
@@ -170,7 +175,7 @@ async def get_change_risk(
were skipped. ``review_priority``, ``classification`` and ``diff_shape``
measure diff size only, not danger.
- ``impacted_tests`` keeps measured coverage and inferred candidates distinct.
+ ``impacted_tests`` is the ``impacted-tests`` selection; read ``run_all``.
``patch_coverage`` is the share of changed executable lines stored coverage
ran; ``hints`` name tests to extend. ``fix_history`` is the changed files'
bug-fix record, ``overlap`` the past fixes on these exact lines,
@@ -263,7 +268,12 @@ async def get_change_risk(
)
try:
payload["impacted_tests"] = await _impacted_tests_block(
- ctx, changed, changed_error, collector
+ ctx,
+ changed,
+ changed_error,
+ collector,
+ revspec=revspec,
+ working_tree=result.working_tree,
)
patch_changed, patch_error, patch_label, patch_revspec = await _push_change(
str(ctx.path),
@@ -759,16 +769,17 @@ def _cross_repo_block(
def _empty_impacted(status: str, summary: str) -> dict[str, Any]:
- """Uniform impacted-tests block for the degraded (no tests to name) paths.
+ """Uniform impacted-tests block for the paths where no selection was made.
- ``basis`` says which signal named the tests (``none`` here). With nothing
- named there is no ``tests_to_run_kind``, and ``map_present`` and
- ``line_coverage`` are the measured block's alone: ``status`` and ``basis``
- already say there is no map. The inferred path sets the kind itself, a
- ``test_file``; the measured one a coverage-map ``test_id``.
+ Nothing vouches for a subset there, so the block says ``run_all`` with the
+ reason, like a selection that cannot vouch for one. ``basis`` is ``none``
+ and nothing is named, so there is no ``tests_to_run_kind``, ``map_present``
+ or ``line_coverage``.
"""
return {
"status": status,
+ "run_all": True,
+ "reasons": [summary, UNAVAILABLE_REASON],
"basis": "none",
"tests_to_run": [],
"total": 0,
@@ -1111,81 +1122,6 @@ async def _branch_overlap_block(
return block
-def _cap_tests(tests: list[str], collector: OmissionCollector, label: str) -> list[str]:
- """First _IMPACTED_TESTS_LIMIT ids; the tail goes to the omission store."""
- if len(tests) > _IMPACTED_TESTS_LIMIT:
- collector.add(
- f"impacted_tests.tests_to_run ({label}) beyond cap={_IMPACTED_TESTS_LIMIT} "
- f"({len(tests) - _IMPACTED_TESTS_LIMIT} dropped)",
- tests[_IMPACTED_TESTS_LIMIT:],
- )
- return tests[:_IMPACTED_TESTS_LIMIT]
-
-
-async def _inferred_impacted(
- session: Any, repo_id: str, changed_files: list[str], collector: OmissionCollector
-) -> dict[str, Any]:
- """Graph-inferred candidates for a repo with no coverage map.
-
- "Run the full suite" is correct but useless on the repositories that have
- no coverage report, which is most of them. The dependency graph can narrow
- it: a test file that reaches a changed file is worth running first. That is
- a candidate list and is labelled one - ``basis`` is ``"inferred"`` and
- there is no ``map_present``, so nothing here can be read as the line-precise
- measured answer.
-
- Deliberately file-level and line-blind. Reaching carries no line
- attribution, so there is no ``line_coverage`` rather than one filled from
- a signal that cannot speak to lines - the distinction this whole block
- exists to keep.
- """
- from repowise.core.analysis.test_reachability import load_test_files, tests_reaching
- from repowise.core.analysis.test_selection import expand_test_scopes
-
- hint = (
- "Inferred from the dependency graph, not measured. For the line-precise "
- "answer build the map with `coverage run --contexts=test` then "
- "`repowise coverage add`."
- )
- try:
- test_files = await load_test_files(session, repo_id)
- reaching = await tests_reaching(session, repo_id, changed_files, test_files=test_files)
- except Exception:
- test_files, reaching = set(), {}
- # A conftest the walk stopped at stands for the tests under its directory.
- tests = rank_tests_by_reach(
- {path: expand_test_scopes(found, test_files) for path, found in reaching.items()}
- )
- if not tests:
- return _empty_impacted(
- "no_map",
- "No per-test coverage map ingested and no test reaches the changed files "
- "in the graph; run the full suite. " + hint,
- )
- total = len(tests)
- block = _empty_impacted("inferred", "")
- block.update(
- {
- "basis": "inferred",
- "tests_to_run": _cap_tests(tests, collector, "inferred"),
- "tests_to_run_kind": "test_file",
- "total": total,
- "truncated": total > _IMPACTED_TESTS_LIMIT,
- "summary": (
- f"{total} test file(s) reach the changed files in the graph"
- + (
- f"; showing first {_IMPACTED_TESTS_LIMIT}"
- if total > _IMPACTED_TESTS_LIMIT
- else ""
- )
- + ". "
- + hint
- ),
- }
- )
- return block
-
-
async def _push_change(
repo_path: str,
revspec: str | None,
@@ -1369,19 +1305,23 @@ async def _impacted_tests_block(
changed: dict[str, set[int]],
changed_error: tuple[str, str] | None,
collector: OmissionCollector,
+ *,
+ revspec: str | None = None,
+ working_tree: bool = False,
) -> dict[str, Any]:
- """Line-precise impacted tests + honest missing-test buckets for the change.
-
- Built on the same core functions the CLI (``repowise impacted-tests``) and
- get_risk's guarding-tests path use - ``changed_lines`` -> ``tests_covering``
- / ``detect_missing_tests`` - so the answer is coverage-grounded. The CLI's
- filename-pattern guess is deliberately omitted: an agent cannot tell a guess
- from real coverage, and ``no_coverage_data`` already reports those files
- honestly as "unknown, run the suite". Degrades to a ``status`` string rather
- than raising, so it never fails the surrounding score.
+ """The tests the change needs, decided as ``repowise impacted-tests`` decides them.
+
+ The same collection and fail-closed selection (``test_collection`` then
+ ``select_tests``) over the whole change, every touched path included, so a
+ manifest, a conftest or a test that walks the source tree weighs here as it
+ does in CI. ``line_coverage`` classifies the scored files' lines against
+ the per-test map, on the side of the diff the map was measured at.
+ Any failure degrades to a ``status`` string with ``run_all`` true and never
+ raises, so it cannot fail the surrounding score.
"""
+ from repowise.core.analysis.changed_lines import change_set
from repowise.core.analysis.missing_test_signal import detect_missing_tests
- from repowise.core.persistence.crud import tests_covering
+ from repowise.core.analysis.test_collection import query_lines
from repowise.core.persistence.database import get_session
session_factory = getattr(ctx, "session_factory", None)
@@ -1391,38 +1331,37 @@ async def _impacted_tests_block(
)
if changed_error is not None:
return _empty_impacted(*changed_error)
-
try:
- async with get_session(session_factory) as session:
- repo_id = (await _get_repo(session)).id
- report = await detect_missing_tests(session, repo_id, changed)
- if report.map_empty:
- return await _inferred_impacted(session, repo_id, sorted(changed), collector)
- by_file: dict[str, list[str]] = {}
- for source_file, lines in changed.items():
- rows = await tests_covering(session, repo_id, source_file, lines=lines)
- ids = sorted({row["test_id"] for row in rows})
- if ids:
- by_file[source_file] = ids
+ change = await asyncio.to_thread(
+ partial(
+ change_set,
+ str(ctx.path),
+ None if working_tree else revspec or "HEAD",
+ working_tree=working_tree,
+ )
+ )
+ except Exception: # git refused the change, or could not run
+ return _empty_impacted("unknown", "Could not read the change from git.")
+ try:
+ result, selection, _ = await select_change_tests(ctx.path, session_factory, change)
+ block = selection_block(
+ result, selection, collector, limit=_IMPACTED_TESTS_LIMIT, label="impacted_tests"
+ )
+ if not result["map_empty"]:
+ lines = query_lines(change, result["measured_commit"])
+ async with get_session(session_factory) as session:
+ repo_id = (await _get_repo(session)).id
+ report = await detect_missing_tests(
+ session, repo_id, {p: lines[p] for p in changed if p in lines}
+ )
+ block["line_coverage"] = _serialize_missing(report)
+ except SelectionUnavailableError as exc:
+ return _empty_impacted(exc.status, str(exc))
except LookupError:
return _empty_impacted("no_index", "No indexed repository; run `repowise init`.")
except SQLAlchemyError:
- return _empty_impacted("unknown", "Could not read the coverage map.")
-
- tests = rank_tests_by_reach(by_file)
- total = len(tests)
- return {
- "status": "map_present",
- "basis": "measured",
- "map_present": True,
- "tests_to_run": _cap_tests(tests, collector, "measured"),
- "tests_to_run_kind": "test_id",
- "total": total,
- "truncated": total > _IMPACTED_TESTS_LIMIT,
- "line_coverage": _serialize_missing(report),
- "summary": (
- f"{total} test(s) cover the changed lines"
- + (f"; showing first {_IMPACTED_TESTS_LIMIT}" if total > _IMPACTED_TESTS_LIMIT else "")
- + "."
- ),
- }
+ return _empty_impacted("unknown", "Could not read the index.")
+ except Exception as exc: # a selection bug must not take the score with it
+ log.warning("impacted_tests_failed", error=f"{type(exc).__name__}: {exc}")
+ return _empty_impacted("unknown", f"Selecting tests failed ({type(exc).__name__}).")
+ return block
diff --git a/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py b/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py
index 1f0f4728b..942e16bac 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py
@@ -12,12 +12,11 @@ from typing import Any
from sqlalchemy import select, text
from sqlalchemy.ext.asyncio import AsyncSession
-from repowise.core.analysis.health.engine import _has_paired_test_file, _path_basenames
+from repowise.core.analysis.pr_blast import untested_files
from repowise.core.co_change import confidence_ratio, parse_partners
from repowise.core.git_refs import find_path_removal
from repowise.core.persistence.models import (
GitMetadata,
- GraphNode,
Repository,
)
from repowise.server.mcp_server._budget import OmissionCollector, cap_collection
@@ -239,60 +238,8 @@ def _compute_impact_surface(
async def _check_test_gap(session: AsyncSession, repo_id: str, target: str) -> bool:
- """Return True if *target* has no test, coverage-backed where the map has data.
-
- Three signals, in descending order of what they can prove, and the file is a
- gap only when all three stay silent - the same ladder ``pr_blast``
- ``_find_test_gaps`` uses, because the two answered this question differently
- and a reader has no way to tell which one they are looking at.
-
- 1. A per-test coverage row (from ``repowise coverage add``) is
- execution-proof: never a gap.
- 2. A test file reaching it in the dependency graph is evidence, not proof,
- but a recorded edge rather than a guess - it catches the suites whose
- tests are named for behaviour rather than for the file under test.
- 3. Otherwise the filename pattern (test_<name>, <name>_test, <name>.spec.*)
- - an honest "unknown", never asserted as untested.
-
- Test files themselves (is_test=True) are never a gap.
- """
- from repowise.core.persistence.crud import covered_source_files
-
- # Test files don't need tests — skip the check entirely
- node_res = await session.execute(
- select(GraphNode.is_test)
- .where(
- GraphNode.repository_id == repo_id,
- GraphNode.node_id == target,
- )
- .limit(1)
- )
- row = node_res.scalar_one_or_none()
- if row is True:
- return False
-
- # Coverage proves a test exercises this file: not a gap.
- if await covered_source_files(session, repo_id, {target}):
- return False
-
- # The graph records a test reaching it: not a gap either. Degrades to "no
- # signal" rather than raising - a failed walk must not become an accusation.
- from repowise.core.analysis.test_reachability import tests_reaching
-
- try:
- if await tests_reaching(session, repo_id, [target]):
- return False
- except Exception:
- pass
-
- test_nodes = await session.execute(
- select(GraphNode.node_id).where(
- GraphNode.repository_id == repo_id,
- GraphNode.is_test == True, # noqa: E712
- )
- )
- test_basenames = _path_basenames(set(test_nodes.scalars()))
- return not _has_paired_test_file(target, test_basenames)
+ """Whether nothing can be shown to test *target* (``pr_blast.untested_files``)."""
+ return bool(await untested_files(session, repo_id, [target]))
async def _get_security_signals(session: AsyncSession, repo_id: str, target: str) -> list[dict]:
diff --git a/packages/server/src/repowise/server/mcp_server/tool_risk/directives.py b/packages/server/src/repowise/server/mcp_server/tool_risk/directives.py
index f174631aa..2c043b1ca 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_risk/directives.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_risk/directives.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+from dataclasses import dataclass, field
from typing import Any
from sqlalchemy import select
@@ -12,6 +13,11 @@ from repowise.core.analysis.risk_semantics import (
structural_impact_contract,
)
from repowise.core.analysis.test_reachability import tests_matching_by_name
+from repowise.core.analysis.test_selection import (
+ Selection,
+ is_runnable_test,
+ selected_by_change,
+)
from repowise.core.co_change import MIN_CO_CHANGE_SUPPORT
from repowise.core.persistence.crud.authority import decision_currencies
from repowise.core.persistence.database import get_session
@@ -25,6 +31,12 @@ from repowise.server.mcp_server._helpers import (
filter_path_list,
is_excluded,
)
+from repowise.server.mcp_server._test_selection import (
+ UNAVAILABLE_REASON,
+ basis_of,
+ run_kind,
+ run_order,
+)
def _as_path(entry: Any) -> str | None:
@@ -62,7 +74,8 @@ _MAY_BREAK_LIMIT = 5
#: than the may-break lists (it is what you actually run), but stays glanceable;
#: the overflow and full typed rows live in pr_blast_radius.test_impact.
_TESTS_TO_RUN_LIMIT = 10
-_TESTS_TO_RUN_KIND = {"measured": "test_id", "inferred": "test_file"}
+#: Run-all reasons carried on the directive; the rest go to the omission store.
+_RUN_ALL_REASONS_LIMIT = 3
#: Cap on the edit-list: the test files this change will probably need edited.
_TESTS_TO_UPDATE_LIMIT = 3
@@ -489,44 +502,60 @@ def _project_recommendation(row: dict[str, Any]) -> dict[str, Any]:
return out
-def _unlisted_tests(paths: list[str], rows: list[dict[str, Any]]) -> list[str]:
- """*paths* that no recommendation row names as its file or id."""
- named = {
- name.split("::", 1)[0]
- for row in rows
- for name in (row.get("test_id"), row.get("test_file"))
- if isinstance(name, str)
- }
- return [path for path in paths if path not in named]
+@dataclass(frozen=True)
+class ChangeTests:
+ """The selection for the PR's files (``_test_selection``), or why there is none."""
+
+ result: dict[str, Any] = field(default_factory=dict)
+ selection: Selection | None = None
+ #: Files whose own code imports a changed file (no runner wiring).
+ importers: set[str] = field(default_factory=set)
+ #: pytest's collection roots, so a production ``test_*.py`` is not a test.
+ roots: Any = None
+ status: str = "selected"
+ message: str = ""
def _tests_to_update(
changed_files: list[str],
- test_paths: set[str],
+ tests: ChangeTests,
pr_blast_radius: dict,
exclude_spec: Any,
) -> list[dict[str, str]]:
"""Test files the change will probably need edited, strongest reason first.
- A test named for a changed file (``name_pair``), then one that imports it
- (``imports``), then one that changes with it in git history (``co_change``).
- A path keeps its first reason; tests already in the change are left out.
+ Only tests the selection runs because of a changed file (not a rule that
+ runs them with every subset), that a runner collects, and that the change
+ does not already touch. Among those: a test named for a changed file
+ (``name_pair``), then one whose own code imports it (``imports``; a
+ conftest or setup file reaches tests through runner wiring, not an
+ import), then one that changes with it in git history (``co_change``). A
+ path keeps its first reason.
"""
+ selection = tests.selection
+ if selection is None:
+ return []
changed = set(changed_files)
- candidates = filter_path_list(sorted(test_paths - changed), exclude_spec)
- eligible = set(candidates)
- named = tests_matching_by_name(changed_files, candidates)
+ eligible = set(
+ filter_path_list(
+ sorted(
+ t
+ for t in selection.test_files
+ if selected_by_change(selection, t)
+ and is_runnable_test(t, tests.roots)
+ and t not in changed
+ ),
+ exclude_spec,
+ )
+ )
+ named = tests_matching_by_name(changed_files, sorted(eligible))
ordered: list[tuple[str | None, str]] = [
(path, "name_pair")
for source in changed_files
if source in named
for path in (named[source].all_tests or named[source].tests)
]
- ordered += [
- (_as_path(e), "imports")
- for e in pr_blast_radius.get("transitive_affected") or []
- if isinstance(e, dict) and e.get("direct")
- ]
+ ordered += [(path, "imports") for path in sorted(tests.importers)]
# Indexing already drops pairs below the support floor; a row that still
# records less is weak history, not a reason to edit a test.
ordered += [
@@ -541,6 +570,39 @@ def _tests_to_update(
return [{"path": path, "reason": reason} for path, reason in rows.items()]
+def _tests_to_run(
+ tests: ChangeTests, collector: OmissionCollector
+) -> tuple[list[str], str, dict[str, Any]]:
+ """``(run list, basis, extra directive fields)`` from the selection.
+
+ The extra fields say whether every test must run and why, and why each
+ emitted test is in the list; a failed selection says so and names nothing.
+ """
+ selection = tests.selection
+ if selection is None:
+ # Nothing vouches for a subset, so the answer is every test.
+ return [], "none", {
+ "tests_run_all": True,
+ "tests_run_all_reasons": [tests.message, UNAVAILABLE_REASON],
+ "tests_status": tests.status,
+ "tests_status_reason": tests.message,
+ }
+ population = run_order(tests.result, selection)
+ reasons = list(selection.reasons)
+ if len(reasons) > _RUN_ALL_REASONS_LIMIT:
+ collector.add(
+ f"directive.tests_run_all_reasons beyond cap={_RUN_ALL_REASONS_LIMIT} "
+ f"({len(reasons) - _RUN_ALL_REASONS_LIMIT} dropped)",
+ reasons[_RUN_ALL_REASONS_LIMIT:],
+ )
+ shown = dict.fromkeys(t.split("::", 1)[0] for t in population[:_TESTS_TO_RUN_LIMIT])
+ extra: dict[str, Any] = {"tests_run_all": selection.run_all}
+ if selection.run_all:
+ extra["tests_run_all_reasons"] = reasons[:_RUN_ALL_REASONS_LIMIT]
+ extra["tests_to_run_why"] = {p: selection.why[p] for p in shown if p in selection.why}
+ return population, basis_of(selection), extra
+
+
def _build_pr_directive(
response: dict,
pr_blast_radius: dict,
@@ -551,13 +613,16 @@ def _build_pr_directive(
test_paths: set[str],
alias: str,
*,
+ tests: ChangeTests | None = None,
full_scale: bool = False,
include_tests: bool = False,
include_blast: bool = False,
) -> None:
"""Assemble PR-mode output: trim co-change lists + blast radius, then build
- the directive block. Mutates *response* in place. Behavior preserved.
+ the directive block. Mutates *response* in place. *tests* is the
+ selection the run and edit lists come from.
"""
+ tests = tests or ChangeTests(status="unknown", message="No test selection was made.")
# PR mode — drop global_hotspots (irrelevant to a specific diff), trim
# per-target co-change lists, and synthesize a tight directive the
# agent can act on without parsing the whole blast-radius dossier.
@@ -604,7 +669,6 @@ def _build_pr_directive(
# symbol an importer uses actually changed. The diff-backed fields below keep
# "will".
all_may_break = [p for p in affected if p not in test_paths]
- all_may_break_tests = [p for p in affected if p in test_paths]
may_break = all_may_break[:_MAY_BREAK_LIMIT]
all_missing_cochanges = filter_path_list(
@@ -612,31 +676,12 @@ def _build_pr_directive(
exclude_spec,
)
missing_cochanges = all_missing_cochanges[:3]
- all_tests_to_update = _tests_to_update(changed_files, test_paths, pr_blast_radius, exclude_spec)
- # Run-list: consume the analyzer's canonical typed population instead of
- # independently deriving test ids. Every row retains its basis through
- # de-duplication, sorting, exclusions, and the directive cap.
+ all_tests_to_update = _tests_to_update(changed_files, tests, pr_blast_radius, exclude_spec)
+ # Typed rows from the analyzer ride behind include=["tests"]. The run list
+ # is the selection: the answer ``repowise impacted-tests`` gives.
test_impact = pr_blast_radius.get("test_impact") or {}
all_recommendations = list(test_impact.get("recommendations") or [])
-
- # Preserve the measured-first legacy projection and its existing scalar
- # domain. The additive typed rows above are the union of evidence kinds.
- guarding = pr_blast_radius.get("guarding_tests") or {}
- all_tests_to_run = list(guarding.get("tests_to_run") or [])
- tests_to_run_basis = guarding.get("basis") or "none"
- # Tests in reverse-import reach join an unmeasured list. A measured list
- # names test ids, so they ride as typed rows instead of mixing in files.
- if tests_to_run_basis != "measured":
- listed = set(all_tests_to_run)
- reached = [p for p in all_may_break_tests if p not in listed]
- if reached:
- all_tests_to_run += reached
- tests_to_run_basis = "inferred"
- else:
- all_recommendations += [
- {"test_id": path, "basis": "inferred", "reason": "structural_reach"}
- for path in _unlisted_tests(all_may_break_tests, all_recommendations)
- ]
+ all_tests_to_run, tests_to_run_basis, tests_fields = _tests_to_run(tests, collector)
test_recommendations = all_recommendations[:_TESTS_TO_RUN_LIMIT]
test_recommendations_total = len(all_recommendations)
recommendations_capped = test_recommendations_total > _TESTS_TO_RUN_LIMIT
@@ -646,13 +691,12 @@ def _build_pr_directive(
tests_to_run_suffix = (
f" {tests_to_run_total} test(s) to run, {tests_to_run_basis}." if tests_to_run_total else ""
)
+ if tests_fields.get("tests_run_all"):
+ tests_to_run_suffix += " Every test must run (tests_run_all_reasons)."
if include_tests and all_recommendations:
basis_totals = test_impact.get("recommendations_by_primary_basis") or {}
measured_total = int(basis_totals.get("measured", 0))
- # Plus the structural-reach rows added above, which the analyzer never counted.
- inferred_total = int(basis_totals.get("inferred", 0)) + (
- test_recommendations_total - len(test_impact.get("recommendations") or [])
- )
+ inferred_total = int(basis_totals.get("inferred", 0))
tests_to_run_suffix += (
f" {test_recommendations_total} test recommendation(s): {measured_total} measured "
f"and {inferred_total} inferred, not coverage-proven candidate(s); "
@@ -785,12 +829,12 @@ def _build_pr_directive(
"files_without_measured_tests": [],
"tests_to_run": tests_to_run,
"tests_to_run_basis": tests_to_run_basis,
- # A measured row names a coverage-map test id; an inferred one a test file.
- "tests_to_run_kind": _TESTS_TO_RUN_KIND.get(tests_to_run_basis),
+ "tests_to_run_kind": run_kind(tests_to_run),
"tests_to_run_total": tests_to_run_total,
"tests_to_run_emitted": len(tests_to_run),
"tests_to_run_truncated": tests_capped,
"tests_to_run_omitted": tests_to_run_total - len(tests_to_run),
+ **tests_fields,
# Tests to edit, not to run; a file can sit in both lists.
"tests_to_update": all_tests_to_update,
**(
diff --git a/packages/server/src/repowise/server/mcp_server/tool_risk/get_risk.py b/packages/server/src/repowise/server/mcp_server/tool_risk/get_risk.py
index e6aa384e9..fdd88b768 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_risk/get_risk.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_risk/get_risk.py
@@ -9,6 +9,7 @@ from typing import Any, NamedTuple
from sqlalchemy import func, select
from repowise.core.analysis.risk_semantics import file_risk_scales
+from repowise.core.analysis.test_reachability import direct_importers
from repowise.core.ingestion.models import FILE_DEPENDENCY_EDGE_TYPES
from repowise.core.persistence.batches import chunked
from repowise.core.persistence.crud import get_graph_nodes_by_ids, get_test_file_paths
@@ -35,6 +36,11 @@ from repowise.server.mcp_server._helpers import (
resolve_enum_argument,
)
from repowise.server.mcp_server._meta import build_meta as _build_meta
+from repowise.server.mcp_server._test_selection import (
+ SelectionUnavailableError,
+ files_change,
+ select_change_tests,
+)
from .assessment import (
_assess_one_target,
@@ -42,7 +48,7 @@ from .assessment import (
fix_annotation,
normalize_target_path,
)
-from .directives import _build_pr_directive, _governance_directive
+from .directives import ChangeTests, _build_pr_directive, _governance_directive
from .enrichment import _enrich_cross_repo, _enrich_health
#: Fields an agent cannot rank or act on: uncalibrated pagerank floats, and
@@ -329,6 +335,27 @@ async def _enrich_cards(
await asyncio.to_thread(_enrich_episodes, scored, ctx.path)
+async def _change_tests(ctx: Any, paths: list[str]) -> ChangeTests:
+ """The selection for a change known by its paths, and the tests importing them.
+
+ Every path counts, excluded or not: a test can break on any of them.
+ """
+ try:
+ change = await asyncio.to_thread(files_change, ctx.path, paths)
+ result, selection, checkout = await select_change_tests(
+ ctx.path, ctx.session_factory, change
+ )
+ async with get_session(ctx.session_factory) as session:
+ importers = await direct_importers(session, (await _get_repo(session)).id, paths)
+ except SelectionUnavailableError as exc:
+ return ChangeTests(status=exc.status, message=str(exc))
+ except Exception as exc: # the rest of the directive stands without its tests
+ return ChangeTests(status="unknown", message=f"Could not select tests: {exc}")
+ return ChangeTests(
+ result=result, selection=selection, importers=importers, roots=checkout.roots
+ )
+
+
async def _lead_with_pr_directive(
response: dict,
evidence: _RiskEvidence,
@@ -339,6 +366,7 @@ async def _lead_with_pr_directive(
full_scale: bool,
include_tests: bool,
include_blast: bool,
+ touched: list[str] | None = None,
) -> dict:
governance_risk = await _governance_directive(ctx, changed_files)
_build_pr_directive(
@@ -350,6 +378,7 @@ async def _lead_with_pr_directive(
governance_risk,
evidence.test_paths,
ctx.alias,
+ tests=await _change_tests(ctx, touched or changed_files),
full_scale=full_scale,
include_tests=include_tests,
include_blast=include_blast,
@@ -426,6 +455,7 @@ async def get_risk(
ctx = await _resolve_repo_context(repo)
collector = OmissionCollector("get_risk", repo_root=ctx.path)
exclude_spec = _get_exclude_spec(ctx.path)
+ touched = list(dict.fromkeys(changed_files or ()))
targets = filter_path_list(targets or changed_files, exclude_spec)
changed_files = filter_path_list(changed_files, exclude_spec)
include_graph = "graph" in include_set
@@ -453,6 +483,7 @@ async def get_risk(
full_scale="scales" in include_set,
include_tests="tests" in include_set,
include_blast="blast" in include_set,
+ touched=touched,
)
elif len(targets) > 1:
# Ambient hotspots orient a multi-file request; beside one named file they are noise.
diff --git a/packages/server/src/repowise/server/routers/code_health/coverage_routes.py b/packages/server/src/repowise/server/routers/code_health/coverage_routes.py
index cd74ac791..beaf18a80 100644
--- a/packages/server/src/repowise/server/routers/code_health/coverage_routes.py
+++ b/packages/server/src/repowise/server/routers/code_health/coverage_routes.py
@@ -214,7 +214,7 @@ async def _inferred_coverage(
Degrades to ``basis: "none"`` rather than raising. An unindexed graph is the
honest unknown, and failing the tab to withhold a secondary signal is the
- wrong trade - the same call ``pr_blast._inferred_guarding_tests`` makes.
+ wrong trade.
"""
try:
test_files = await load_test_files(session, repo_id)
diff --git a/packages/types/src/chat.ts b/packages/types/src/chat.ts
index 82af8372c..0cad839bd 100644
--- a/packages/types/src/chat.ts
+++ b/packages/types/src/chat.ts
@@ -288,7 +288,11 @@ export interface RiskReportArtifactData {
};
impacted_tests?: {
tests_to_run?: string[];
+ /** `selected`, `run_all`, or why nothing was selected (`no_index`, `timeout`, ...). */
status?: string;
+ /** No subset can be vouched for: every test must run (`reasons` says why). */
+ run_all?: boolean;
+ reasons?: string[];
summary?: string;
/** `measured` from stored coverage, `inferred` from the dependency graph. */
basis?: "measured" | "inferred" | "none";
diff --git a/packages/ui/__tests__/chat/change-risk-artifact.test.tsx b/packages/ui/__tests__/chat/change-risk-artifact.test.tsx
index 7ed8ef8a0..e86b72343 100644
--- a/packages/ui/__tests__/chat/change-risk-artifact.test.tsx
+++ b/packages/ui/__tests__/chat/change-risk-artifact.test.tsx
@@ -45,7 +45,7 @@ const data: RiskReportArtifactData = {
findings_emitted: 1,
scope: { changed: 3, eligible: 3, analyzed: 3, skipped: 0, failed: 0 },
},
- impacted_tests: { tests_to_run: ["tests/test_app.py"], status: "map_present" },
+ impacted_tests: { tests_to_run: ["tests/test_app.py"], status: "selected", run_all: false },
};
describe("change risk artifact", () => {
@@ -137,6 +137,28 @@ describe("change risk artifact", () => {
expect(screen.queryByText("What this change made worse")).toBeNull();
});
+ it("says every test must run, and why, when no subset is vouched for", () => {
+ const reason = "package.json changed: build or test configuration can change any test.";
+ render(
+ <RiskReportRenderer
+ data={{
+ ...data,
+ impacted_tests: {
+ status: "run_all",
+ run_all: true,
+ reasons: [reason],
+ tests_to_run: ["tests/test_app.py"],
+ },
+ }}
+ />,
+ );
+
+ expect(screen.getByText("Run every test")).toBeInTheDocument();
+ expect(screen.getByText(`Run every test: ${reason}`)).toBeInTheDocument();
+ expect(screen.getByText("Run these first")).toBeInTheDocument();
+ expect(screen.queryByText("1 to run")).not.toBeInTheDocument();
+ });
+
it("reports an error with an alert role", () => {
render(<RiskReportRenderer data={{ error: "Could not read change 'nope'." }} />);
diff --git a/packages/ui/src/chat/artifacts.tsx b/packages/ui/src/chat/artifacts.tsx
index 20321e1ab..a31dab4ff 100644
--- a/packages/ui/src/chat/artifacts.tsx
+++ b/packages/ui/src/chat/artifacts.tsx
@@ -342,6 +342,11 @@ function ChangeRiskCard({ data }: { data: RiskReportArtifactData }) {
: null;
const findings = delta?.top_findings ?? [];
const tests = data.impacted_tests?.tests_to_run ?? [];
+ // No subset can be vouched for: the list is only what to run first.
+ const runAll =
+ data.impacted_tests?.run_all === true
+ ? (data.impacted_tests.reasons?.[0] ?? data.impacted_tests.summary ?? "")
+ : null;
const fragile = data.fix_history?.files ?? [];
const partial = delta ? delta.status !== "available" : false;
@@ -402,14 +407,19 @@ function ChangeRiskCard({ data }: { data: RiskReportArtifactData }) {
{fragile.length > 0 && (
<ContextItem label="Fragile" value={`${fragile.length} files`} />
)}
- {tests.length > 0 && (
- <ContextItem label="Tests" value={`${tests.length} to run`} />
+ {runAll !== null ? (
+ <ContextItem label="Tests" value="Run every test" />
+ ) : (
+ tests.length > 0 && (
+ <ContextItem label="Tests" value={`${tests.length} to run`} />
+ )
)}
</dl>
{(directive?.next_actions?.length ||
delta ||
tests.length > 0 ||
+ runAll !== null ||
fragile.length > 0 ||
data.classification) && (
<details>
@@ -427,9 +437,19 @@ function ChangeRiskCard({ data }: { data: RiskReportArtifactData }) {
</ul>
</div>
) : null}
+ {runAll !== null && (
+ <p
+ role="status"
+ className="text-[10px] text-[var(--color-warning)]"
+ >
+ Run every test{runAll ? `: ${runAll}` : ""}
+ </p>
+ )}
{tests.length > 0 && (
<div>
- <SectionTitle icon={CheckCircle2}>Tests to run</SectionTitle>
+ <SectionTitle icon={CheckCircle2}>
+ {runAll !== null ? "Run these first" : "Tests to run"}
+ </SectionTitle>
<ul className="space-y-0.5 font-mono text-[10px] text-[var(--color-text-secondary)]">
{tests.slice(0, 10).map((test) => (
<li key={test} className="truncate" title={test}>
diff --git a/plugins/claude-code/hooks/lens/lens.js b/plugins/claude-code/hooks/lens/lens.js
index 7378b9021..a0cd01ca1 100644
--- a/plugins/claude-code/hooks/lens/lens.js
+++ b/plugins/claude-code/hooks/lens/lens.js
@@ -2072,7 +2072,14 @@ function diffShape(percentile) {
function testsBasis(measured) {
return measured ? "measured by stored coverage" : "inferred from the dependency graph, not measured";
}
+function runAllLine(reason) {
+ return reason ? `Run every test: ${reason}` : "Run every test";
+}
function testsLine(t) {
+ if (t.runAll != null) {
+ const first = t.tests.length ? `; first: ${t.tests.join(", ")}` : "";
+ return `${runAllLine(t.runAll)}${first}`;
+ }
const count3 = countOf(t.total, t.files ? "test file" : "test", t.files ? "test files" : "tests");
const shown = t.truncated ? ` (first ${t.tests.length.toLocaleString("en-US")} shown)` : "";
return `Tests to run: ${count3}, ${testsBasis(t.measured)}${shown}: ${t.tests.join(", ")}`;
@@ -2093,6 +2100,10 @@ function directiveLines(d) {
return out;
}
function runTestsPrompt(t) {
+ if (t.runAll != null) {
+ const first = t.tests.length ? `, starting with ${t.tests.join(" ")}` : "";
+ return `Run the whole test suite for this change${first}. Repowise cannot vouch for a smaller set: ${t.runAll}`;
+ }
const rest = t.truncated ? ` (the first ${t.tests.length} of ${t.total})` : "";
return `Run the tests Repowise names for this change${rest}, ${testsBasis(t.measured)}: ${t.tests.join(" ")}`;
}
@@ -2247,6 +2258,7 @@ function findingsCounts(resolved, total2) {
return `${formatNumber(resolved)} resolved · ${countOf(total2, "new finding", "new findings")}`;
}
function testsToRunCount(t) {
+ if (t.runAll != null) return "every test";
const count3 = countOf(t.total, t.files ? "test file" : "test", t.files ? "test files" : "tests");
return `${count3}, ${t.measured ? "measured" : "inferred"}`;
}
@@ -2271,6 +2283,7 @@ function briefDecision(title, reviewed) {
return reviewed ? `${title} (standing decision)` : `${title} (found in the code, not yet reviewed)`;
}
function briefTests(t) {
+ if (t.runAll != null) return runAllLine(t.runAll).replace(/^Run/, "run");
return `tests to run, ${testsBasis(t.measured)}: ${t.tests.join(" ")}`;
}
var FLOW_COPY = {
@@ -3751,13 +3764,15 @@ function isRetryable(action) {
function testsToRun(risk) {
const block = risk.impacted_tests;
const tests = block?.tests_to_run ?? [];
- if (block === void 0 || tests.length === 0) return null;
+ const runAll = block?.run_all === true ? block.reasons?.[0] ?? block.summary ?? "" : null;
+ if (block === void 0 || tests.length === 0 && runAll === null) return null;
return {
tests,
total: block.total ?? tests.length,
truncated: block.truncated === true,
files: block.tests_to_run_kind === "test_file",
- measured: block.basis === "measured"
+ measured: block.basis === "measured",
+ runAll
};
}
diff --git a/plugins/claude-code/skills/change-review/SKILL.md b/plugins/claude-code/skills/change-review/SKILL.md
index 3fcd30aa2..3f0b97bff 100644
--- a/plugins/claude-code/skills/change-review/SKILL.md
+++ b/plugins/claude-code/skills/change-review/SKILL.md
@@ -66,16 +66,22 @@ The response carries its own `directive` block, per file rather than per change
- **`missing_cochanges`** — files that historically change together with the
changed files but were left untouched. Often a forgotten update.
- **`missing_tests`** — changed code with a test gap. Flag for new/updated tests.
-- **`tests_to_run`**: the positive complement of `missing_tests`, the tests that
- exercise the changed files. Recommend running these to validate the change.
- Read **`tests_to_run_basis`** with it: `measured` means a coverage map proves
- those tests execute the changed files (pytest-runnable ids); `inferred` means
- the call graph shows those test *files* reaching the change, with the import
- graph filling in where it is silent, which needs no coverage ingest but is a
- candidate list, so say so rather than presenting it as proof; `none` with an empty list is "unknown", never "no tests exist".
-
-`include=["blast"]` adds `pr_blast_radius`, the fuller dossier behind those lists (including the
-per-changed-file `guarding_tests` breakdown behind `tests_to_run`).
+- **`tests_to_run`**: the tests the change needs, from the same selection
+ `repowise impacted-tests` makes, with **`tests_to_run_why`** giving each one's
+ reason (the changed file and its evidence, or "runs with every subset").
+ Check **`tests_run_all`** first: when it is true no smaller set can be
+ vouched for, **`tests_run_all_reasons`** says why, and the list is only what
+ to run first; recommend the whole suite. When it is false the list is the
+ set to run. **`tests_to_run_basis`** is `measured` when stored per-test
+ coverage decided a changed file and `inferred` when only the dependency graph
+ or a rule did; say which. **`tests_status`** appears only when no selection
+ could be made (`timeout`, `config_invalid`, `unknown`, ...), and then every
+ test must run.
+
+`get_change_risk` carries the same answer as **`impacted_tests`**: `status`
+(`selected` or `run_all`, or why nothing was selected), `run_all`, `reasons`,
+`tests_to_run` and `why`. `include=["blast"]` on `get_risk` adds
+`pr_blast_radius`, the fuller dossier behind the directive's lists.
For the line-precise version from a terminal, `repowise impacted-tests <revspec>`
maps each changed line to the tests whose recorded coverage touches it, then
diff --git a/plugins/codex/skills/change-review/SKILL.md b/plugins/codex/skills/change-review/SKILL.md
index 3f6a82998..51d2036b5 100644
--- a/plugins/codex/skills/change-review/SKILL.md
+++ b/plugins/codex/skills/change-review/SKILL.md
@@ -61,16 +61,22 @@ The response carries its own `directive` block, per file rather than per change
- **`missing_cochanges`** — files that historically change together with the
changed files but were left untouched. Often a forgotten update.
- **`missing_tests`** — changed code with a test gap. Flag for new/updated tests.
-- **`tests_to_run`**: the positive complement of `missing_tests`, the tests that
- exercise the changed files. Recommend running these to validate the change.
- Read **`tests_to_run_basis`** with it: `measured` means a coverage map proves
- those tests execute the changed files (pytest-runnable ids); `inferred` means
- the call graph shows those test *files* reaching the change, with the import
- graph filling in where it is silent, which needs no coverage ingest but is a
- candidate list, so say so rather than presenting it as proof; `none` with an empty list is "unknown", never "no tests exist".
-
-`include=["blast"]` adds `pr_blast_radius`, the fuller dossier behind those lists (including the
-per-changed-file `guarding_tests` breakdown behind `tests_to_run`).
+- **`tests_to_run`**: the tests the change needs, from the same selection
+ `repowise impacted-tests` makes, with **`tests_to_run_why`** giving each one's
+ reason (the changed file and its evidence, or "runs with every subset").
+ Check **`tests_run_all`** first: when it is true no smaller set can be
+ vouched for, **`tests_run_all_reasons`** says why, and the list is only what
+ to run first; recommend the whole suite. When it is false the list is the
+ set to run. **`tests_to_run_basis`** is `measured` when stored per-test
+ coverage decided a changed file and `inferred` when only the dependency graph
+ or a rule did; say which. **`tests_status`** appears only when no selection
+ could be made (`timeout`, `config_invalid`, `unknown`, ...), and then every
+ test must run.
+
+`get_change_risk` carries the same answer as **`impacted_tests`**: `status`
+(`selected` or `run_all`, or why nothing was selected), `run_all`, `reasons`,
+`tests_to_run` and `why`. `include=["blast"]` on `get_risk` adds
+`pr_blast_radius`, the fuller dossier behind the directive's lists.
For the line-precise version from a terminal, `repowise impacted-tests <revspec>`
maps each changed line to the tests whose recorded coverage touches it, then
diff --git a/plugins/shared/skills/change-review.md b/plugins/shared/skills/change-review.md
index cfcaee09c..c23a565e6 100644
--- a/plugins/shared/skills/change-review.md
+++ b/plugins/shared/skills/change-review.md
@@ -74,16 +74,22 @@ The response carries its own `directive` block, per file rather than per change
- **`missing_cochanges`** — files that historically change together with the
changed files but were left untouched. Often a forgotten update.
- **`missing_tests`** — changed code with a test gap. Flag for new/updated tests.
-- **`tests_to_run`**: the positive complement of `missing_tests`, the tests that
- exercise the changed files. Recommend running these to validate the change.
- Read **`tests_to_run_basis`** with it: `measured` means a coverage map proves
- those tests execute the changed files (pytest-runnable ids); `inferred` means
- the call graph shows those test *files* reaching the change, with the import
- graph filling in where it is silent, which needs no coverage ingest but is a
- candidate list, so say so rather than presenting it as proof; `none` with an empty list is "unknown", never "no tests exist".
-
-`include=["blast"]` adds `pr_blast_radius`, the fuller dossier behind those lists (including the
-per-changed-file `guarding_tests` breakdown behind `tests_to_run`).
+- **`tests_to_run`**: the tests the change needs, from the same selection
+ `repowise impacted-tests` makes, with **`tests_to_run_why`** giving each one's
+ reason (the changed file and its evidence, or "runs with every subset").
+ Check **`tests_run_all`** first: when it is true no smaller set can be
+ vouched for, **`tests_run_all_reasons`** says why, and the list is only what
+ to run first; recommend the whole suite. When it is false the list is the
+ set to run. **`tests_to_run_basis`** is `measured` when stored per-test
+ coverage decided a changed file and `inferred` when only the dependency graph
+ or a rule did; say which. **`tests_status`** appears only when no selection
+ could be made (`timeout`, `config_invalid`, `unknown`, ...), and then every
+ test must run.
+
+`get_change_risk` carries the same answer as **`impacted_tests`**: `status`
+(`selected` or `run_all`, or why nothing was selected), `run_all`, `reasons`,
+`tests_to_run` and `why`. `include=["blast"]` on `get_risk` adds
+`pr_blast_radius`, the fuller dossier behind the directive's lists.
For the line-precise version from a terminal, `repowise impacted-tests <revspec>`
maps each changed line to the tests whose recorded coverage touches it, then
diff --git a/tests/unit/change_health/test_supplied_source_parity.py b/tests/unit/change_health/test_supplied_source_parity.py
index e00312474..89e9f7ebf 100644
--- a/tests/unit/change_health/test_supplied_source_parity.py
+++ b/tests/unit/change_health/test_supplied_source_parity.py
@@ -276,13 +276,20 @@ def test_directive_without_a_test_block_asks_for_nothing():
assert directive(_clean_delta(), absent)["next_actions"] == []
-def test_directive_still_names_a_missing_coverage_map():
+def test_directive_still_names_a_missing_index():
from repowise.server.mcp_server._change_health import directive
- actions = directive(_clean_delta(), {"status": "no_map"})["next_actions"]
+ actions = directive(_clean_delta(), {"status": "no_index"})["next_actions"]
assert actions == ["No measured test map; run the suite covering the changed files."]
+def test_directive_says_to_run_every_test_and_why():
+ from repowise.server.mcp_server._change_health import directive
+
+ tests = {"run_all": True, "reasons": ["uv.lock changed."], "tests_to_run": ["t0"]}
+ assert directive(_clean_delta(), tests)["next_actions"] == ["Run every test: uv.lock changed."]
+
+
def test_directive_renders_at_most_three_tests_in_one_run_command():
from repowise.server.mcp_server._change_health import directive
diff --git a/tests/unit/health/test_coverage_parsers.py b/tests/unit/health/test_coverage_parsers.py
index 5adcdb195..0fe15c0e4 100644
--- a/tests/unit/health/test_coverage_parsers.py
+++ b/tests/unit/health/test_coverage_parsers.py
@@ -10,7 +10,6 @@ import pytest
from repowise.core.analysis.health.coverage import (
detect_format,
is_test_file,
- paired_test_file,
parse,
parse_clover,
parse_cobertura,
@@ -304,7 +303,3 @@ def test_is_test_file_uses_source_imports() -> None:
assert is_test_file("weird/path/name.py", src) is True
-def test_paired_test_file_finds_partner() -> None:
- all_paths = {"src/foo.py", "tests/test_foo.py", "src/bar.py"}
- assert paired_test_file("src/foo.py", all_paths) == "tests/test_foo.py"
- assert paired_test_file("src/bar.py", all_paths) is None
diff --git a/tests/unit/health/test_mts_cts_test_classification.py b/tests/unit/health/test_mts_cts_test_classification.py
index af6eb2105..e28219df6 100644
--- a/tests/unit/health/test_mts_cts_test_classification.py
+++ b/tests/unit/health/test_mts_cts_test_classification.py
@@ -9,13 +9,15 @@ carry is gone with the implementations it named (#1103).
from __future__ import annotations
-from repowise.core.analysis.health.coverage import paired_test_file
from repowise.core.analysis.health.engine import _has_paired_test_file, _path_basenames
+from repowise.core.analysis.test_reachability import tests_matching_by_name as name_match
-def test_paired_test_file_finds_mts_cts() -> None:
- assert paired_test_file("src/foo.ts", {"src/foo.ts", "src/foo.test.mts"}) == "src/foo.test.mts"
- assert paired_test_file("src/bar.ts", {"src/bar.ts", "src/bar.spec.cts"}) == "src/bar.spec.cts"
+def test_name_match_finds_mts_cts() -> None:
+ foo = name_match(["src/foo.ts"], {"src/foo.ts", "src/foo.test.mts"})
+ bar = name_match(["src/bar.ts"], {"src/bar.ts", "src/bar.spec.cts"})
+ assert foo["src/foo.ts"].tests == ["src/foo.test.mts"]
+ assert bar["src/bar.ts"].tests == ["src/bar.spec.cts"]
def test_engine_has_paired_test_file_for_mts_source() -> None:
diff --git a/tests/unit/health/test_recommendations.py b/tests/unit/health/test_recommendations.py
index 9b7422420..9c7cfa7eb 100644
--- a/tests/unit/health/test_recommendations.py
+++ b/tests/unit/health/test_recommendations.py
@@ -210,13 +210,14 @@ def test_the_test_named_for_the_file_survives_the_cap() -> None:
def test_a_reached_conftest_validates_with_the_tests_under_it() -> None:
"""``pytest tests/unit/conftest.py`` collects nothing; the tests below it run."""
from repowise.core.analysis.health.refactoring.recommendations import _expand_scopes
+ from repowise.core.analysis.test_selection import expand_test_scopes
test_files = {"tests/unit/conftest.py", "tests/unit/test_core.py", "tests/other/test_x.py"}
reached = ReachedBy(
["tests/unit/conftest.py"], "call-graph", 1, ("tests/unit/conftest.py",)
)
validation = build_validation_plan(
- _plan("fixture"), {}, {"src/core.py": _expand_scopes("src/core.py", reached, test_files)}
+ _plan("fixture"), {}, {"src/core.py": _expand_scopes("src/core.py", reached, expand_test_scopes(reached.all_tests, test_files))}
)
assert validation.basis == "inferred"
assert validation.tests == ["tests/unit/test_core.py"]
@@ -227,12 +228,15 @@ def test_a_root_conftest_expansion_is_ranked_before_it_is_capped() -> None:
"""A root conftest stands for every test; the cut keeps the nearest, not the first."""
from repowise.core.analysis.health.refactoring.recommendations import _expand_scopes
from repowise.core.analysis.test_reachability import MAX_TESTS_PER_TARGET
+ from repowise.core.analysis.test_selection import expand_test_scopes
many = {f"tests/aaa/test_{i:03}.py" for i in range(MAX_TESTS_PER_TARGET + 20)}
test_files = {"conftest.py", "tests/unit/test_core.py", *many}
reached = ReachedBy(["conftest.py"], "call-graph", 1, ("conftest.py",))
- expanded = _expand_scopes("src/core.py", reached, test_files)
+ expanded = _expand_scopes(
+ "src/core.py", reached, expand_test_scopes(reached.all_tests, test_files)
+ )
assert expanded.total == MAX_TESTS_PER_TARGET + 21
assert len(expanded.tests) == MAX_TESTS_PER_TARGET
diff --git a/tests/unit/persistence/golden/test_impact.json b/tests/unit/persistence/golden/test_impact.json
index dfc516631..a9acad32b 100644
--- a/tests/unit/persistence/golden/test_impact.json
+++ b/tests/unit/persistence/golden/test_impact.json
@@ -510,529 +510,6 @@
"src/no_match.py"
]
},
- "legacy_current": {
- "analysis": {
- "basis_categories": [
- "measured",
- "inferred"
- ],
- "degraded": false,
- "partial": true,
- "stale": false,
- "status": "partial"
- },
- "basis": "measured",
- "by_file": {
- "src/both.py": [
- "tests/test_both.py::test_measured"
- ],
- "src/measured.py": [
- "tests/test_measured.py::test_measured"
- ],
- "src/seed.py": [
- "tests/test_seed.py::test_seed"
- ],
- "src/truncate.py": [
- "tests/test_truncate.py::test_00",
- "tests/test_truncate.py::test_01",
- "tests/test_truncate.py::test_02",
- "tests/test_truncate.py::test_03",
- "tests/test_truncate.py::test_04",
- "tests/test_truncate.py::test_05",
- "tests/test_truncate.py::test_06",
- "tests/test_truncate.py::test_07",
- "tests/test_truncate.py::test_08",
- "tests/test_truncate.py::test_09",
- "tests/test_truncate.py::test_10",
- "tests/test_truncate.py::test_11"
- ]
- },
- "coverage": {
- "changed_files_total": 7,
- "changed_files_with_measured_tests": 4,
- "changed_files_without_measured_tests": 3,
- "freshness": {
- "indexed_commit": "fixture-head",
- "ingested_commit": "fixture-head",
- "reason": null,
- "status": "current"
- },
- "ingested_at": "<ingested_at>",
- "map_present": true,
- "pair_count": 15,
- "reason": "coverage_map_matches_only_part_of_change",
- "source_file_count": 4,
- "source_format": "coverage.py",
- "status": "partial",
- "test_count": 15
- },
- "inference": {
- "candidates_before_dedup": 2,
- "changed_files_total": 7,
- "changed_files_with_candidates": 2,
- "reason": null,
- "status": "available"
- },
- "map_present": true,
- "tests_to_run": [
- "tests/test_both.py::test_measured",
- "tests/test_measured.py::test_measured",
- "tests/test_seed.py::test_seed",
- "tests/test_truncate.py::test_00",
- "tests/test_truncate.py::test_01",
- "tests/test_truncate.py::test_02",
- "tests/test_truncate.py::test_03",
- "tests/test_truncate.py::test_04",
- "tests/test_truncate.py::test_05",
- "tests/test_truncate.py::test_06",
- "tests/test_truncate.py::test_07",
- "tests/test_truncate.py::test_08",
- "tests/test_truncate.py::test_09",
- "tests/test_truncate.py::test_10",
- "tests/test_truncate.py::test_11"
- ],
- "tests_to_run_emitted": 15,
- "tests_to_run_total": 15,
- "tests_to_run_truncated": false,
- "tests_to_run_with_basis": [
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/both.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/both.py"
- ],
- "test_file": "tests/test_both.py",
- "test_id": "tests/test_both.py::test_measured"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/measured.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/measured.py"
- ],
- "test_file": "tests/test_measured.py",
- "test_id": "tests/test_measured.py::test_measured"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/seed.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/seed.py"
- ],
- "test_file": "tests/test_seed.py",
- "test_id": "tests/test_seed.py::test_seed"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_00"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_01"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_02"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_03"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_04"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_05"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_06"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_07"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_08"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_09"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_10"
- },
- {
- "bases": [
- "measured"
- ],
- "basis": "measured",
- "evidence": [
- {
- "basis": "measured",
- "source_file": "src/truncate.py",
- "source_format": "coverage.py",
- "via": "coverage-map"
- }
- ],
- "repository": "alpha",
- "repository_id": "<current>",
- "source_files": [
- "src/truncate.py"
- ],
- "test_file": "tests/test_truncate.py",
- "test_id": "tests/test_truncate.py::test_11"
- }
- ]
- },
- "legacy_no_coverage": {
- "analysis": {
- "basis_categories": [
- "inferred"
- ],
- "degraded": false,
- "partial": true,
- "stale": false,
- "status": "partial"
- },
- "basis": "inferred",
- "by_file": {
- "src/both.py": [
- "tests/test_both_inferred.py"
- ],
- "src/inferred.py": [
- "tests/excluded/test_hidden.py",
- "tests/test_inferred.py"
- ]
- },
- "coverage": {
- "changed_files_total": 8,
- "changed_files_with_measured_tests": 0,
- "changed_files_without_measured_tests": 8,
- "freshness": {
- "indexed_commit": "fixture-head",
- "ingested_commit": null,
- "reason": "coverage_or_index_commit_unavailable",
- "status": "unknown"
- },
- "ingested_at": null,
- "map_present": false,
- "pair_count": 0,
- "reason": "no_per_test_coverage_map",
- "source_file_count": 0,
- "source_format": null,
- "status": "unavailable",
- "test_count": 0
- },
- "inference": {
- "candidates_before_dedup": 3,
- "changed_files_total": 8,
- "changed_files_with_candidates": 2,
- "reason": null,
- "status": "available"
- },
- "map_present": false,
- "tests_to_run": [
- "tests/excluded/test_hidden.py",
- "tests/test_both_inferred.py",
- "tests/test_inferred.py"
- ],
- "tests_to_run_emitted": 3,
- "tests_to_run_total": 3,
- "tests_to_run_truncated": false,
- "tests_to_run_with_basis": [
- {
- "bases": [
- "inferred"
- ],
- "basis": "inferred",
- "evidence": [
- {
- "basis": "inferred",
- "source_file": "src/inferred.py",
- "source_format": null,
- "via": "import-graph"
- }
- ],
- "repository": "<bare>",
- "repository_id": "<bare>",
- "source_files": [
- "src/inferred.py"
- ],
- "test_file": "tests/excluded/test_hidden.py",
- "test_id": "tests/excluded/test_hidden.py"
- },
- {
- "bases": [
- "inferred"
- ],
- "basis": "inferred",
- "evidence": [
- {
- "basis": "inferred",
- "source_file": "src/both.py",
- "source_format": null,
- "via": "import-graph"
- }
- ],
- "repository": "<bare>",
- "repository_id": "<bare>",
- "source_files": [
- "src/both.py"
- ],
- "test_file": "tests/test_both_inferred.py",
- "test_id": "tests/test_both_inferred.py"
- },
- {
- "bases": [
- "inferred"
- ],
- "basis": "inferred",
- "evidence": [
- {
- "basis": "inferred",
- "source_file": "src/inferred.py",
- "source_format": null,
- "via": "import-graph"
- }
- ],
- "repository": "<bare>",
- "repository_id": "<bare>",
- "source_files": [
- "src/inferred.py"
- ],
- "test_file": "tests/test_inferred.py",
- "test_id": "tests/test_inferred.py"
- }
- ]
- },
"no_coverage": {
"analysis": {
"basis_categories": [
diff --git a/tests/unit/persistence/test_impacted_tests_resolution.py b/tests/unit/persistence/test_impacted_tests_resolution.py
index de53c8328..14363fdfe 100644
--- a/tests/unit/persistence/test_impacted_tests_resolution.py
+++ b/tests/unit/persistence/test_impacted_tests_resolution.py
@@ -528,3 +528,62 @@ async def test_a_reaching_fixture_nobody_requests_selects_only_the_import_check(
out = await _narrowed(async_session, repo.id)
assert _pairs(out, "src/app/util.py") == {"tests/test_a.py": "conftest-import-check"}
+
+
+async def test_narrow_scopes_resolves_a_reached_conftest_as_the_selection_does(
+ async_session, tmp_path
+) -> None:
+ """A few-hop walk that stopped at the conftest gets the test that can break."""
+ import subprocess
+
+ from repowise.core.analysis.test_collection import narrow_scopes
+
+ (tmp_path / "tests").mkdir()
+ (tmp_path / "tests/conftest.py").write_text(_CONFTEST, encoding="utf-8")
+ # The conftests come from the checkout's tracked files.
+ for args in (["init", "-q"], ["add", "tests/conftest.py"]):
+ subprocess.run(["git", *args], cwd=tmp_path, check=True, capture_output=True)
+ repo = await insert_repo(async_session, local_path=str(tmp_path))
+ await _conftest_graph(async_session, repo.id, stamped=True)
+ test_files = {"tests/conftest.py", "tests/test_a.py", "tests/test_b.py", "tests/test_c.py"}
+
+ out = await narrow_scopes(
+ async_session,
+ repo.id,
+ {"src/app/util.py": ["tests/conftest.py"], "src/other.py": ["tests/test_c.py"]},
+ test_files,
+ )
+
+ assert out == {"src/app/util.py": ["tests/test_a.py"], "src/other.py": ["tests/test_c.py"]}
+
+
+async def test_narrow_scopes_keeps_every_test_under_a_conftest_it_cannot_read(
+ async_session, tmp_path
+) -> None:
+ from repowise.core.analysis.test_collection import narrow_scopes
+
+ repo = await insert_repo(async_session, local_path=str(tmp_path))
+ await _conftest_graph(async_session, repo.id, stamped=True)
+ test_files = {"tests/conftest.py", "tests/test_a.py", "tests/test_b.py", "tests/test_c.py"}
+
+ out = await narrow_scopes(
+ async_session, repo.id, {"src/app/util.py": ["tests/conftest.py"]}, test_files
+ )
+
+ assert out == {"src/app/util.py": ["tests/test_a.py", "tests/test_b.py", "tests/test_c.py"]}
+
+
+async def test_narrow_scopes_keeps_the_whole_scope_for_a_target_the_walk_does_not_answer(
+ async_session, tmp_path
+) -> None:
+ from repowise.core.analysis.test_collection import narrow_scopes
+
+ repo = await insert_repo(async_session, local_path=str(tmp_path))
+ await _conftest_graph(async_session, repo.id, stamped=True)
+ test_files = {"tests/conftest.py", "tests/test_a.py", "tests/test_b.py", "tests/test_c.py"}
+
+ out = await narrow_scopes(
+ async_session, repo.id, {"src/unindexed.py": ["tests/conftest.py"]}, test_files
+ )
+
+ assert out == {"src/unindexed.py": ["tests/test_a.py", "tests/test_b.py", "tests/test_c.py"]}
diff --git a/tests/unit/persistence/test_pr_blast_guarding_tests.py b/tests/unit/persistence/test_pr_blast_guarding_tests.py
deleted file mode 100644
index 826a0dc2b..000000000
--- a/tests/unit/persistence/test_pr_blast_guarding_tests.py
+++ /dev/null
@@ -1,245 +0,0 @@
-"""Coverage-backed guarding tests in PRBlastRadiusAnalyzer._guarding_tests.
-
-The positive complement of ``_find_test_gaps``: given the changed files, return
-the tests the per-test coverage map proves execute them (the "run these to
-validate" list). Seeds a ``test_coverage`` table and pins the map-present,
-map-empty, and no-rows-for-file paths.
-"""
-
-from __future__ import annotations
-
-from repowise.core.analysis.health.coverage import TestCoverage
-from repowise.core.analysis.pr_blast import PRBlastRadiusAnalyzer
-from repowise.core.persistence.crud import save_test_coverage
-from tests.unit.persistence.helpers import insert_repo
-
-
-async def _seed_map(session, repo_id):
- await save_test_coverage(
- session,
- repo_id,
- [
- TestCoverage(
- test_id="tests/test_math.py::test_add",
- file_path="src/math.py",
- covered_lines=[1, 2, 3],
- source_format="coverage.py",
- test_file="tests/test_math.py",
- ),
- TestCoverage(
- test_id="tests/test_math.py::test_sub",
- file_path="src/math.py",
- covered_lines=[5, 6],
- source_format="coverage.py",
- test_file="tests/test_math.py",
- ),
- TestCoverage(
- test_id="tests/test_util.py::test_helper",
- file_path="src/util.py",
- covered_lines=[10],
- source_format="coverage.py",
- test_file="tests/test_util.py",
- ),
- ],
- source_format="coverage.py",
- )
-
-
-async def test_guarding_tests_returns_covering_tests_per_file(async_session):
- repo = await insert_repo(async_session)
- await _seed_map(async_session, repo.id)
- await async_session.commit()
-
- analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)
- result = await analyzer._guarding_tests(["src/math.py"])
-
- assert result["map_present"] is True
- # Both math tests guard math.py; util's test does not leak in.
- assert result["tests_to_run"] == [
- "tests/test_math.py::test_add",
- "tests/test_math.py::test_sub",
- ]
- assert result["by_file"]["src/math.py"] == [
- "tests/test_math.py::test_add",
- "tests/test_math.py::test_sub",
- ]
-
-
-async def test_guarding_tests_unions_and_dedups_across_changed_files(async_session):
- repo = await insert_repo(async_session)
- # test_shared guards BOTH files - must appear once in the union.
- await save_test_coverage(
- async_session,
- repo.id,
- [
- TestCoverage(
- test_id="tests/test_shared.py::test_both",
- file_path="src/a.py",
- covered_lines=[1],
- source_format="coverage.py",
- test_file="tests/test_shared.py",
- ),
- TestCoverage(
- test_id="tests/test_shared.py::test_both",
- file_path="src/b.py",
- covered_lines=[2],
- source_format="coverage.py",
- test_file="tests/test_shared.py",
- ),
- ],
- source_format="coverage.py",
- )
- await async_session.commit()
-
- analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)
- result = await analyzer._guarding_tests(["src/a.py", "src/b.py"])
-
- assert result["tests_to_run"] == ["tests/test_shared.py::test_both"]
- assert set(result["by_file"]) == {"src/a.py", "src/b.py"}
-
-
-async def test_guarding_tests_map_empty_is_honest_unknown(async_session):
- repo = await insert_repo(async_session)
- await async_session.commit() # no coverage rows at all
-
- analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)
- result = await analyzer._guarding_tests(["src/math.py"])
-
- # No map -> "unknown", never an empty-because-untested claim.
- assert result["map_present"] is False
- assert result["basis"] == "none"
- assert result["tests_to_run"] == []
- assert result["by_file"] == {}
- assert result["coverage"]["status"] == "unavailable"
- assert result["analysis"]["partial"] is True
-
-
-async def test_guarding_tests_file_with_no_rows_omitted_but_map_present(async_session):
- repo = await insert_repo(async_session)
- await _seed_map(async_session, repo.id)
- await async_session.commit()
-
- analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)
- # new.py has no coverage rows; math.py does. Map IS present.
- result = await analyzer._guarding_tests(["src/new.py", "src/math.py"])
-
- assert result["map_present"] is True
- assert "src/new.py" not in result["by_file"]
- assert result["by_file"]["src/math.py"] == [
- "tests/test_math.py::test_add",
- "tests/test_math.py::test_sub",
- ]
-
-
-async def test_guarding_tests_empty_changed_set(async_session):
- repo = await insert_repo(async_session)
- await _seed_map(async_session, repo.id)
- await async_session.commit()
-
- analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)
- result = await analyzer._guarding_tests([])
-
- assert result["map_present"] is False
- assert result["basis"] == "none"
- assert result["tests_to_run"] == []
- assert result["by_file"] == {}
- assert result["coverage"]["reason"] == "no_changed_files"
-
-
-# ---------------------------------------------------------------------------
-# The graph fallback: what happens on the repos with no coverage report at all
-# ---------------------------------------------------------------------------
-
-
-def _graph(session, repo_id, nodes, edges):
- from repowise.core.persistence.models import GraphEdge, GraphNode
-
- for path, is_test in nodes:
- session.add(
- GraphNode(repository_id=repo_id, node_id=path, node_type="file", is_test=is_test)
- )
- for src, dst in edges:
- session.add(
- GraphEdge(
- repository_id=repo_id,
- source_node_id=src,
- target_node_id=dst,
- edge_type="imports",
- )
- )
-
-
-async def test_no_map_falls_back_to_the_graph_and_says_so(async_session):
- repo = await insert_repo(async_session)
- _graph(
- async_session,
- repo.id,
- [("tests/test_math.py", True), ("src/math.py", False)],
- [("tests/test_math.py", "src/math.py")],
- )
- await async_session.commit()
-
- result = await PRBlastRadiusAnalyzer(async_session, repo.id)._guarding_tests(["src/math.py"])
-
- # map_present stays the measured-map flag it always was; basis is what tells
- # a caller which of the two answered, and the two are never merged.
- assert result["map_present"] is False
- assert result["basis"] == "inferred"
- assert result["tests_to_run"] == ["tests/test_math.py"]
- assert result["by_file"] == {"src/math.py": ["tests/test_math.py"]}
-
-
-async def test_measured_and_inferred_rows_remain_distinguishable(async_session):
- """Both sources may answer, but the inferred row is never called measured."""
- repo = await insert_repo(async_session)
- await _seed_map(async_session, repo.id)
- _graph(
- async_session,
- repo.id,
- [("tests/test_unrelated.py", True), ("src/math.py", False)],
- [("tests/test_unrelated.py", "src/math.py")],
- )
- await async_session.commit()
-
- analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)
- result = await analyzer._guarding_tests(["src/math.py"])
- typed = await analyzer._test_impact(["src/math.py"])
-
- assert result["basis"] == "measured"
- rows = {row["test_id"]: row for row in result["tests_to_run_with_basis"]}
- assert rows["tests/test_math.py::test_add"]["basis"] == "measured"
- assert rows["tests/test_math.py::test_sub"]["basis"] == "measured"
- assert "tests/test_unrelated.py" not in rows
- typed_rows = {row["test_id"]: row for row in typed["recommendations"]}
- assert typed_rows["tests/test_unrelated.py"]["basis"] == "inferred"
- assert typed_rows["tests/test_unrelated.py"]["bases"] == ["inferred"]
-
-
-async def test_a_map_that_is_silent_about_this_file_still_asks_the_graph(async_session):
- """The map exists but knows nothing here - better than an empty list."""
- repo = await insert_repo(async_session)
- await _seed_map(async_session, repo.id)
- _graph(
- async_session,
- repo.id,
- [("tests/test_new.py", True), ("src/new.py", False)],
- [("tests/test_new.py", "src/new.py")],
- )
- await async_session.commit()
-
- result = await PRBlastRadiusAnalyzer(async_session, repo.id)._guarding_tests(["src/new.py"])
-
- assert result["map_present"] is True
- assert result["basis"] == "inferred"
- assert result["tests_to_run"] == ["tests/test_new.py"]
-
-
-async def test_nothing_anywhere_stays_an_honest_unknown(async_session):
- repo = await insert_repo(async_session)
- _graph(async_session, repo.id, [("src/lonely.py", False)], [])
- await async_session.commit()
-
- result = await PRBlastRadiusAnalyzer(async_session, repo.id)._guarding_tests(["src/lonely.py"])
-
- assert result["basis"] == "none"
- assert result["tests_to_run"] == []
diff --git a/tests/unit/persistence/test_test_gap_graph_floor.py b/tests/unit/persistence/test_test_gap_graph_floor.py
index 458b2c426..7373804a6 100644
--- a/tests/unit/persistence/test_test_gap_graph_floor.py
+++ b/tests/unit/persistence/test_test_gap_graph_floor.py
@@ -1,10 +1,10 @@
"""A test reaching a file in the graph stops it being called a test gap.
Issue #1740: two filename heuristics answered "does this file have a test" and
-disagreed - ``pr_blast._find_test_gaps`` (substring match over every test path)
-and ``assessment._check_test_gap`` (SQL LIKE over the same). Both now consult
-the graph before falling back to a name, so the answer they share comes from a
-recorded edge rather than from whichever pattern each happened to implement.
+disagreed - ``pr_blast._find_test_gaps`` and ``assessment._check_test_gap``.
+Both now call ``pr_blast.untested_files``, which consults the graph before
+falling back to a name, so the answer comes from a recorded edge, not
+from whichever pattern each happened to implement.
Pinned here for both call sites at once, because the defect was precisely that
they were tested and maintained apart.
@@ -131,13 +131,13 @@ async def test_stripping_the_prefix_does_not_clear_an_unrelated_file(async_sessi
assert await analyzer._find_test_gaps(["src/mcp/tool_search.py"]) == ["src/mcp/tool_search.py"]
-def test_a_stripped_stem_too_short_to_mean_anything_is_not_offered():
- """``get_id.py`` must not be cleared by every ``test_identity.py`` around."""
- from repowise.core.analysis.pr_blast import test_name_stems
-
- assert test_name_stems("tool_dead_code") == [("tool_dead_code", False), ("dead_code", True)]
- assert test_name_stems("get_id") == [("get_id", False)]
- assert test_name_stems("service") == [("service", False)]
+async def test_a_stripped_stem_too_short_to_mean_anything_is_not_offered(async_session):
+ """``get_id.py`` must not be cleared by a ``test_id.py`` named for something else."""
+ repo = await _seed(
+ async_session, [], [("tests/test_id.py", True), ("src/mcp/get_id.py", False)]
+ )
+ analyzer = PRBlastRadiusAnalyzer(async_session, repo.id)
+ assert await analyzer._find_test_gaps(["src/mcp/get_id.py"]) == ["src/mcp/get_id.py"]
async def test_a_stripped_stem_does_not_match_a_longer_word_it_prefixes(async_session):
diff --git a/tests/unit/persistence/test_test_impact_golden.py b/tests/unit/persistence/test_test_impact_golden.py
index 1a956c0dd..dd40b0992 100644
--- a/tests/unit/persistence/test_test_impact_golden.py
+++ b/tests/unit/persistence/test_test_impact_golden.py
@@ -9,7 +9,7 @@ import json
import os
from pathlib import Path
-from repowise.core.analysis.test_impact import analyze_test_impact, legacy_guarding_tests
+from repowise.core.analysis.test_impact import analyze_test_impact
from repowise.core.exclusion import build_exclude_spec
from tests.unit.persistence.helpers import insert_repo
from tests.unit.persistence.test_pr_test_impact_semantics import _fixture, _seed
@@ -57,8 +57,6 @@ async def _scenarios(session, tmp_path) -> dict:
"no_coverage": await analyze_test_impact(session, bare.id, changed),
"nothing_changed": await analyze_test_impact(session, current.id, []),
}
- out["legacy_current"] = legacy_guarding_tests(out["current"])
- out["legacy_no_coverage"] = legacy_guarding_tests(out["no_coverage"])
text = json.dumps(out, indent=2, sort_keys=True, default=str)
for repo, name in ((current, "current"), (stale, "stale"), (bare, "bare")):
text = text.replace(repo.id, f"<{name}>")
diff --git a/tests/unit/server/mcp/conftest.py b/tests/unit/server/mcp/conftest.py
index 3c644468c..b449e8241 100644
--- a/tests/unit/server/mcp/conftest.py
+++ b/tests/unit/server/mcp/conftest.py
@@ -589,6 +589,40 @@ async def setup_mcp(factory, fts, vector_store, populated_db, tmp_path):
mcp_mod._release_announced = None
+async def pr_checkout(setup_mcp, session, tmp_path):
+ """A git checkout of the fixture's files, indexed at its HEAD.
+
+ PR mode selects tests the way ``repowise impacted-tests`` does, which reads
+ the checkout (its tracked files) and the commit the index describes.
+ """
+ import subprocess
+
+ from sqlalchemy import update
+
+ def git(*args: str) -> str:
+ return subprocess.run(
+ ["git", *args], cwd=tmp_path, check=True, capture_output=True, text=True
+ ).stdout.strip()
+
+ git("init", "-q")
+ for path in ("src/auth/service.py", "src/auth/middleware.py", "src/db/models.py"):
+ (tmp_path / path).parent.mkdir(parents=True, exist_ok=True)
+ (tmp_path / path).write_text("x = 1\n", encoding="utf-8")
+ (tmp_path / "tests").mkdir(exist_ok=True)
+ (tmp_path / "tests/test_service.py").write_text("def test_x():\n pass\n", encoding="utf-8")
+ git("add", "src", "tests")
+ git("-c", "user.name=Dev", "-c", "[email protected]", "commit", "-qm", "seed")
+ head = git("rev-parse", "HEAD")
+ await session.execute(update(Repository).where(Repository.id == "repo1").values(head_commit=head))
+ # Stamped by the indexer: an ordinary test that walks nothing.
+ await session.execute(
+ update(GraphNode).where(GraphNode.is_test == True).values(always_run_reason="") # noqa: E712
+ )
+ await session.flush()
+ return setup_mcp
+
+
@pytest.fixture
async def health_data(session: AsyncSession, populated_db: str) -> str:
"""Seed health_findings + health_file_metrics for the existing repo."""
diff --git a/tests/unit/server/mcp/test_change_risk.py b/tests/unit/server/mcp/test_change_risk.py
index 1cecf97c7..1883f0174 100644
--- a/tests/unit/server/mcp/test_change_risk.py
+++ b/tests/unit/server/mcp/test_change_risk.py
@@ -191,8 +191,17 @@ async def test_get_change_risk_rejects_repo_all() -> None:
# ---------------------------------------------------------------------------
-async def _factory_with_repo(coverage_records: list | None):
- """Build an in-memory session factory seeded with one repo + coverage map."""
+async def _factory_with_repo(
+ coverage_records: list | None,
+ *,
+ head_commit: str | None = None,
+ measured_at: str | None = None,
+):
+ """Build an in-memory session factory seeded with one repo + coverage map.
+
+ *head_commit* is the commit the index describes, *measured_at* the one the
+ coverage map was measured at.
+ """
from datetime import UTC, datetime
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
@@ -219,17 +228,30 @@ async def _factory_with_repo(coverage_records: list | None):
local_path="/tmp/repo",
default_branch="main",
settings_json="{}",
+ head_commit=head_commit,
created_at=now,
updated_at=now,
)
)
await s.flush()
if coverage_records:
- await save_test_coverage(s, "repo1", coverage_records, source_format="coverage.py")
+ await save_test_coverage(
+ s,
+ "repo1",
+ coverage_records,
+ source_format="coverage.py",
+ ingested_commit_sha=measured_at,
+ )
await s.commit()
return factory
+def _sha(repo: Path, rev: str = "HEAD") -> str:
+ return subprocess.run(
+ ["git", "rev-parse", rev], cwd=repo, check=True, capture_output=True, text=True
+ ).stdout.strip()
+
+
def _tc(test_id: str, source_file: str, covered_lines: list[int], test_file: str):
from repowise.core.analysis.health.coverage import TestCoverage
@@ -263,7 +285,9 @@ async def test_impacted_tests_line_precise_hit_and_miss(tmp_path, monkeypatch) -
[
_tc("tests/test_app.py::test_app", "src/app.py", [3], "tests/test_app.py"),
_tc("tests/test_other.py::test_other", "src/other.py", [9], "tests/test_other.py"),
- ]
+ ],
+ head_commit=_sha(repo, "HEAD~1"),
+ measured_at=_sha(repo),
)
module = importlib.import_module("repowise.server.mcp_server.tool_change_risk")
@@ -272,17 +296,22 @@ async def test_impacted_tests_line_precise_hit_and_miss(tmp_path, monkeypatch) -
return SimpleNamespace(path=str(repo), session_factory=factory)
monkeypatch.setattr(module, "_resolve_repo_context", _context)
- result = await module.get_change_risk(baseline=0)
+ # The covering tests exist but are not tracked, so none runs as a test the
+ # graph cannot see into; an explicit revspec keeps them out of the change.
+ (repo / "tests").mkdir()
+ for name in ("test_app", "test_other"):
+ (repo / f"tests/{name}.py").write_text("x = 1\n", encoding="utf-8")
+ result = await module.get_change_risk("HEAD", baseline=0)
it = result["impacted_tests"]
- assert it["status"] == "map_present"
assert it["basis"] == "measured"
assert it["tests_to_run_kind"] == "test_id"
assert it["map_present"] is True
- # app.py line 3 is covered -> its test is named; other/new are not covering.
- assert it["tests_to_run"] == ["tests/test_app.py::test_app"]
- assert it["total"] == 1
- assert it["truncated"] is False
+ # app.py line 3 is covered -> its test leads; other/new are not covering.
+ assert it["tests_to_run"][0] == "tests/test_app.py::test_app"
+ # No test is known for the new file, so the subset cannot be vouched for.
+ assert it["run_all"] is True
+ assert any(r.startswith("src/new.py:") for r in it["reasons"])
mt = it["line_coverage"]
# other.py is in the map but its changed lines (1,2,3) are uncovered.
@@ -315,12 +344,12 @@ async def test_impacted_tests_no_map_is_unknown_not_untested(tmp_path, monkeypat
it = result["impacted_tests"]
# Nothing is seeded in the graph either, so neither tier can speak.
- assert it["status"] == "no_map"
- assert "map_present" not in it
+ assert it["status"] == "run_all"
+ assert it["map_present"] is False
assert it["tests_to_run"] == []
- # Honest degradation: no untested claim, a "run the suite" summary instead.
+ # Honest degradation: no untested claim, a "run every test" summary instead.
assert "line_coverage" not in it
- assert "run the full suite" in it["summary"]
+ assert it["summary"].startswith("Run every test:")
@pytest.mark.asyncio
@@ -337,13 +366,25 @@ async def test_impacted_tests_falls_back_to_the_graph_without_a_map(tmp_path, mo
repo = tmp_path / "repo"
repo.mkdir()
_git(["init", "-q"], repo)
- _commit(repo, {"src/app.py": "a\nb\n"}, "chore: seed")
+ _commit(
+ repo,
+ {"src/app.py": "a\nb\n", "tests/test_round_trips.py": "from src import app\n"},
+ "chore: seed",
+ )
_commit(repo, {"src/app.py": "a\nb\nc\n"}, "feat: add line")
- factory = await _factory_with_repo(None)
+ factory = await _factory_with_repo(None, head_commit=_sha(repo, "HEAD~1"))
async with factory() as s:
for path, is_test in (("tests/test_round_trips.py", True), ("src/app.py", False)):
- s.add(GraphNode(repository_id="repo1", node_id=path, node_type="file", is_test=is_test))
+ s.add(
+ GraphNode(
+ repository_id="repo1",
+ node_id=path,
+ node_type="file",
+ is_test=is_test,
+ always_run_reason="" if is_test else None,
+ )
+ )
s.add(
GraphEdge(
repository_id="repo1",
@@ -362,16 +403,16 @@ async def test_impacted_tests_falls_back_to_the_graph_without_a_map(tmp_path, mo
monkeypatch.setattr(module, "_resolve_repo_context", _context)
it = (await module.get_change_risk(baseline=0))["impacted_tests"]
- assert it["status"] == "inferred"
+ assert it["status"] == "selected"
assert it["basis"] == "inferred"
assert it["tests_to_run_kind"] == "test_file"
- assert "map_present" not in it
+ assert it["map_present"] is False
assert it["tests_to_run"] == ["tests/test_round_trips.py"]
- assert "line_coverage" not in it
# Answered by the import tier: there is no call edge here, which is exactly
# when the weaker tier is allowed to speak.
- assert "reach the changed files in the graph" in it["summary"]
- assert "not measured" in it["summary"]
+ assert it["why"] == {"tests/test_round_trips.py": "src/app.py changed (import-graph)"}
+ assert "line_coverage" not in it
+ assert it["summary"].startswith("1 test(s) to run for this change")
@pytest.mark.asyncio
@@ -387,6 +428,9 @@ async def test_impacted_tests_overflow_cap_is_honest(tmp_path, monkeypatch) -> N
_tc(f"tests/test_{i}.py::test_{i}", "src/hot.py", [1], f"tests/test_{i}.py")
for i in range(12)
]
+ (repo / "tests").mkdir()
+ for i in range(12):
+ (repo / f"tests/test_{i}.py").write_text("x = 1\n", encoding="utf-8")
factory = await _factory_with_repo(records)
module = importlib.import_module("repowise.server.mcp_server.tool_change_risk")
@@ -395,7 +439,7 @@ async def test_impacted_tests_overflow_cap_is_honest(tmp_path, monkeypatch) -> N
return SimpleNamespace(path=str(repo), session_factory=factory)
monkeypatch.setattr(module, "_resolve_repo_context", _context)
- result = await module.get_change_risk(baseline=0)
+ result = await module.get_change_risk("HEAD", baseline=0)
it = result["impacted_tests"]
assert it["total"] == 12
diff --git a/tests/unit/server/mcp/test_change_test_selection.py b/tests/unit/server/mcp/test_change_test_selection.py
new file mode 100644
index 000000000..d88d3a168
--- /dev/null
+++ b/tests/unit/server/mcp/test_change_test_selection.py
@@ -0,0 +1,416 @@
+"""get_change_risk and get_risk name the tests ``repowise impacted-tests`` selects.
+
+Each case is a change the agent tools used to answer from their own shallow
+walk, and got wrong: tests that run with every subset were missing, a conftest
+named two tests instead of every test under it, a manifest read as "no map",
+a test-package ``__init__.py`` was listed as a test, runner wiring read as an
+import in ``tests_to_update``, and a map measured at the base was matched on
+the head's line numbers.
+"""
+
+from __future__ import annotations
+
+import importlib
+import subprocess
+from datetime import UTC, datetime
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+_NOW = datetime(2026, 7, 18, tzinfo=UTC)
+
+
+def _git(repo: Path, *args: str) -> str:
+ return subprocess.run(
+ ["git", *args], cwd=repo, check=True, capture_output=True, text=True
+ ).stdout.strip()
+
+
+def _commit(repo: Path, files: dict[str, str], message: str) -> str:
+ for relative_path, content in files.items():
+ path = repo / relative_path
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(content, encoding="utf-8")
+ _git(repo, "add", relative_path)
+ _git(repo, "-c", "user.name=Dev", "-c", "[email protected]", "commit", "-qm", message)
+ return _git(repo, "rev-parse", "HEAD")
+
+
+def _repo(tmp_path: Path, seed: dict[str, str]) -> tuple[Path, str]:
+ repo = tmp_path / "repo"
+ repo.mkdir()
+ _git(repo, "init", "-q")
+ return repo, _commit(repo, seed, "chore: seed")
+
+
+async def _index(
+ base: str,
+ nodes: dict[str, bool | str],
+ edges: list[tuple[str, str, str]] = (),
+ coverage: list | None = None,
+ measured_at: str | None = None,
+):
+ """An index built at *base*: file nodes (``True`` test, ``str`` always-run reason), edges."""
+ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
+ from sqlalchemy.pool import StaticPool
+
+ from repowise.core.persistence.crud import save_test_coverage
+ from repowise.core.persistence.database import init_db
+ from repowise.core.persistence.models import GraphEdge, GraphNode, Repository
+
+ engine = create_async_engine(
+ "sqlite+aiosqlite:///:memory:",
+ connect_args={"check_same_thread": False},
+ poolclass=StaticPool,
+ )
+ await init_db(engine)
+ factory = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
+ async with factory() as s:
+ s.add(
+ Repository(
+ id="repo1",
+ name="repo",
+ url="https://example.com/repo",
+ local_path="/tmp/repo",
+ default_branch="main",
+ settings_json="{}",
+ head_commit=base,
+ created_at=_NOW,
+ updated_at=_NOW,
+ )
+ )
+ for path, kind in nodes.items():
+ s.add(
+ GraphNode(
+ repository_id="repo1",
+ node_id=path,
+ node_type="file",
+ is_test=kind is not False,
+ # Stamped by the indexer: "" for an ordinary test.
+ always_run_reason=kind if isinstance(kind, str) else ("" if kind else None),
+ )
+ )
+ for source, target, edge_type in edges:
+ s.add(
+ GraphEdge(
+ repository_id="repo1",
+ source_node_id=source,
+ target_node_id=target,
+ edge_type=edge_type,
+ )
+ )
+ await s.flush()
+ if coverage:
+ await save_test_coverage(
+ s, "repo1", coverage, source_format="coverage.py", ingested_commit_sha=measured_at
+ )
+ await s.commit()
+ return factory
+
+
+def _tc(test_id: str, source_file: str, covered_lines: list[int]):
+ from repowise.core.analysis.health.coverage import TestCoverage
+
+ return TestCoverage(
+ test_id=test_id,
+ file_path=source_file,
+ covered_lines=covered_lines,
+ source_format="coverage.py",
+ test_file=test_id.split("::", 1)[0],
+ )
+
+
+async def _change_risk(monkeypatch, repo: Path, factory) -> dict:
+ module = importlib.import_module("repowise.server.mcp_server.tool_change_risk")
+
+ async def _context(_: str | None) -> SimpleNamespace:
+ return SimpleNamespace(path=str(repo), session_factory=factory)
+
+ monkeypatch.setattr(module, "_resolve_repo_context", _context)
+ return await module.get_change_risk("HEAD", baseline=0)
+
+
+async def _risk_directive(monkeypatch, repo: Path, factory, changed: list[str]) -> dict:
+ import repowise.server.mcp_server as mcp_mod
+ from repowise.server.mcp_server.tool_risk.get_risk import get_risk
+
+ monkeypatch.setattr(mcp_mod, "_session_factory", factory)
+ monkeypatch.setattr(mcp_mod, "_repo_path", str(repo))
+ return (await get_risk(changed_files=changed))["directive"]
+
+
+_STATUS = {
+ "src/status.py": "def status():\n return 1\n",
+ "tests/test_status.py": "from src.status import status\n",
+ "tests/test_walks.py": "import pathlib\n",
+}
+
+
+async def test_a_test_that_runs_with_every_subset_is_named(tmp_path, monkeypatch) -> None:
+ repo, base = _repo(tmp_path, _STATUS)
+ _commit(repo, {"src/status.py": "def status():\n return 2\n"}, "fix: status")
+ factory = await _index(
+ base,
+ {
+ "src/status.py": False,
+ "tests/test_status.py": True,
+ "tests/test_walks.py": "it lists and reads files under a source directory",
+ },
+ [("tests/test_status.py", "src/status.py", "imports")],
+ )
+
+ it = (await _change_risk(monkeypatch, repo, factory))["impacted_tests"]
+
+ assert it["status"] == "selected"
+ assert it["run_all"] is False
+ assert it["tests_to_run"] == ["tests/test_status.py", "tests/test_walks.py"]
+ assert it["why"]["tests/test_walks.py"].startswith("runs with every subset: it lists")
+ assert it["always_run_total"] == 1
+ assert it["basis_by_file"] == {"src/status.py": "import-graph"}
+
+
+async def test_a_conftest_change_runs_every_test_under_it(tmp_path, monkeypatch) -> None:
+ files = {
+ "tests/conftest.py": "import pytest\n",
+ "tests/test_a.py": "def test_a():\n pass\n",
+ "tests/sub/test_b.py": "def test_b():\n pass\n",
+ "src/a.py": "A = 1\n",
+ }
+ repo, base = _repo(tmp_path, files)
+ _commit(repo, {"tests/conftest.py": "import pytest\n\nX = 1\n"}, "test: fixture")
+ factory = await _index(
+ base,
+ {
+ "tests/conftest.py": True,
+ "tests/test_a.py": True,
+ "tests/sub/test_b.py": True,
+ "src/a.py": False,
+ },
+ [("tests/test_a.py", "src/a.py", "imports"), ("tests/sub/test_b.py", "src/a.py", "imports")],
+ )
+
+ it = (await _change_risk(monkeypatch, repo, factory))["impacted_tests"]
+
+ assert it["run_all"] is False
+ assert sorted(it["tests_to_run"]) == ["tests/sub/test_b.py", "tests/test_a.py"]
+ assert it["basis_by_file"] == {"tests/conftest.py": "conftest"}
+
+
+async def test_a_manifest_change_runs_everything_on_both_tools(tmp_path, monkeypatch) -> None:
+ repo, base = _repo(tmp_path, {**_STATUS, "package.json": "{}\n"})
+ _commit(repo, {"package.json": '{"name": "x"}\n'}, "chore: manifest")
+ nodes = {"src/status.py": False, "tests/test_status.py": True, "tests/test_walks.py": True}
+ edges = [
+ ("tests/test_status.py", "src/status.py", "imports"),
+ ("tests/test_walks.py", "src/status.py", "imports"),
+ ]
+ factory = await _index(base, nodes, edges)
+
+ result = await _change_risk(monkeypatch, repo, factory)
+ it = result["impacted_tests"]
+ assert it["status"] == "run_all"
+ assert it["reasons"][0].startswith("package.json changed:")
+ assert it["tests_to_run"] == []
+ assert result["directive"]["next_actions"][-1].startswith("Run every test: package.json")
+
+ directive = await _risk_directive(monkeypatch, repo, factory, ["package.json"])
+ assert directive["tests_run_all"] is True
+ assert directive["tests_run_all_reasons"][0].startswith("package.json changed:")
+
+
+async def test_get_risk_never_lists_a_test_package_init_as_a_test(tmp_path, monkeypatch) -> None:
+ files = {
+ "src/a.py": "A = 1\n",
+ "tests/__init__.py": "from src import a\n",
+ "tests/test_a.py": "def test_a():\n pass\n",
+ }
+ repo, base = _repo(tmp_path, files)
+ factory = await _index(
+ base,
+ {"src/a.py": False, "tests/__init__.py": True, "tests/test_a.py": True},
+ [("tests/__init__.py", "src/a.py", "imports"), ("tests/test_a.py", "src/a.py", "imports")],
+ )
+
+ directive = await _risk_directive(monkeypatch, repo, factory, ["src/a.py"])
+
+ assert directive["tests_to_run"] == ["tests/test_a.py"]
+ assert directive["tests_run_all"] is False
+ assert directive["tests_to_run_why"] == {"tests/test_a.py": "src/a.py changed (import-graph)"}
+
+
+async def test_tests_to_update_reads_imports_not_runner_wiring(tmp_path, monkeypatch) -> None:
+ files = {
+ "src/core.py": "C = 1\n",
+ "tests/test_direct.py": "from src import core\n",
+ "tests/test_wired.py": "def test_w():\n pass\n",
+ }
+ repo, base = _repo(tmp_path, files)
+ factory = await _index(
+ base,
+ {"src/core.py": False, "tests/test_direct.py": True, "tests/test_wired.py": True},
+ [
+ ("tests/test_direct.py", "src/core.py", "imports"),
+ # A setup file's wiring: every test it runs for, no import of its own.
+ ("tests/test_wired.py", "src/core.py", "framework"),
+ ],
+ )
+
+ directive = await _risk_directive(monkeypatch, repo, factory, ["src/core.py"])
+
+ assert sorted(directive["tests_to_run"]) == ["tests/test_direct.py", "tests/test_wired.py"]
+ assert directive["tests_to_update"] == [{"path": "tests/test_direct.py", "reason": "imports"}]
+
+
+async def test_a_map_measured_at_the_base_matches_the_old_lines(tmp_path, monkeypatch) -> None:
+ files = {
+ "src/app.py": "a\nb\nc\nd\ne\nf\n",
+ "tests/test_line2.py": "def test_2():\n pass\n",
+ "tests/test_line6.py": "def test_6():\n pass\n",
+ "tests/test_graph.py": "from src import app\n",
+ "src/other.py": "O = 1\n",
+ }
+ repo, base = _repo(tmp_path, files)
+ # Two lines inserted at the top and the old line 6 replaced: the head's
+ # numbers (1, 2, 8) would hit the test of the untouched old line 2.
+ _commit(repo, {"src/app.py": "n1\nn2\na\nb\nc\nd\ne\nF\n"}, "feat: app")
+ factory = await _index(
+ base,
+ {
+ "src/app.py": False,
+ "tests/test_line2.py": True,
+ "tests/test_line6.py": True,
+ "tests/test_graph.py": True,
+ "src/other.py": False,
+ },
+ [
+ # The covering tests reach the app only at run time.
+ ("tests/test_line2.py", "src/other.py", "imports"),
+ ("tests/test_line6.py", "src/other.py", "imports"),
+ ("tests/test_graph.py", "src/app.py", "imports"),
+ ],
+ coverage=[
+ _tc("tests/test_line2.py::test_2", "src/app.py", [2]),
+ _tc("tests/test_line6.py::test_6", "src/app.py", [6]),
+ ],
+ measured_at=base,
+ )
+
+ it = (await _change_risk(monkeypatch, repo, factory))["impacted_tests"]
+
+ assert it["basis"] == "measured"
+ assert it["map_present"] is True
+ assert "tests/test_line6.py::test_6" in it["tests_to_run"]
+ assert "tests/test_line2.py::test_2" not in it["tests_to_run"]
+ # Coverage adds to the graph's answer; it never replaces it.
+ assert "tests/test_graph.py" in it["tests_to_run"]
+ assert it["line_coverage"]["untested_changes"] == []
+ assert [c["source_file"] for c in it["line_coverage"]["stale_test_candidates"]] == [
+ "src/app.py"
+ ]
+
+
+async def test_a_selection_over_its_time_budget_says_so(tmp_path, monkeypatch) -> None:
+ import asyncio
+
+ from repowise.server.mcp_server import _test_selection
+
+ async def _slow(*_a, **_k):
+ await asyncio.sleep(1)
+
+ monkeypatch.setattr(_test_selection, "SELECTION_TIMEOUT_SECONDS", 0.01)
+ monkeypatch.setattr(_test_selection, "_select", _slow)
+ repo, base = _repo(tmp_path, _STATUS)
+ factory = await _index(base, {"src/status.py": False})
+
+ directive = await _risk_directive(monkeypatch, repo, factory, ["src/status.py"])
+
+ assert directive["tests_to_run"] == []
+ assert directive["tests_status"] == "timeout"
+ assert "repowise impacted-tests" in directive["tests_status_reason"]
+
+
+async def test_a_path_the_checkout_lacks_fails_closed_and_backslashes_are_paths(
+ tmp_path, monkeypatch
+) -> None:
+ repo, base = _repo(tmp_path, _STATUS)
+ factory = await _index(
+ base,
+ {"src/status.py": False, "tests/test_status.py": True, "tests/test_walks.py": True},
+ [
+ ("tests/test_status.py", "src/status.py", "imports"),
+ ("tests/test_walks.py", "src/status.py", "imports"),
+ ],
+ )
+
+ missing = await _risk_directive(monkeypatch, repo, factory, ["src/nope.py"])
+ assert missing["tests_run_all"] is True
+ assert missing["tests_run_all_reasons"][0].startswith("src/nope.py was deleted")
+
+ windows = await _risk_directive(monkeypatch, repo, factory, ["src\status.py"])
+ assert windows["tests_run_all"] is False
+ assert sorted(windows["tests_to_run"]) == ["tests/test_status.py", "tests/test_walks.py"]
+
+
+def test_a_checkout_git_could_not_list_does_not_read_as_no_tests() -> None:
+ from repowise.core.analysis.changed_lines import ChangeSet, FileDiff
+ from repowise.core.analysis.test_collection import Checkout, Plan, empty_result, select
+ from repowise.core.analysis.test_selection import TestSelectionConfig
+ from repowise.core.pytest_roots import PytestRoots
+
+ change = ChangeSet({"src/a.py": FileDiff(path="src/a.py")}, set(), "HEAD", None, None)
+ result = empty_result(1)
+ result.update(
+ index_gap=[],
+ placed_tests=set(),
+ inferred=[{"source_file": "src/a.py", "test_file": "tests/test_a.py", "via": "import-graph"}],
+ )
+ empty = Checkout([], [], PytestRoots(), lambda _p: None, lambda _p: True)
+
+ selection = select(change, result, TestSelectionConfig(), empty, Plan({}, {}, []))
+
+ assert selection.run_all is True
+ assert any("git could not list" in r for r in selection.reasons)
+
+
+async def test_workspace_get_risk_selects_in_the_member_repository(tmp_path, monkeypatch) -> None:
+ """In a workspace the selection reads the aliased member's checkout and index."""
+ import repowise.server.mcp_server as mcp_mod
+
+ get_risk_mod = importlib.import_module("repowise.server.mcp_server.tool_risk.get_risk")
+
+ repo, base = _repo(tmp_path, _STATUS)
+ factory = await _index(
+ base,
+ {"src/status.py": False, "tests/test_status.py": True},
+ [("tests/test_status.py", "src/status.py", "imports")],
+ )
+ member = SimpleNamespace(alias="backend", path=repo, session_factory=factory)
+ seen: list[str | None] = []
+
+ async def _context(alias: str | None = None) -> SimpleNamespace:
+ seen.append(alias)
+ return member
+
+ other = tmp_path / "elsewhere"
+ other.mkdir()
+ monkeypatch.setattr(mcp_mod, "_repo_path", str(other))
+ monkeypatch.setattr(get_risk_mod, "_resolve_repo_context", _context)
+
+ directive = (await get_risk_mod.get_risk(changed_files=["src/status.py"], repo="backend"))[
+ "directive"
+ ]
+
+ assert seen == ["backend"]
+ # test_walks.py is tracked there but not indexed, so it runs with every subset.
+ assert directive["tests_to_run"] == ["tests/test_status.py", "tests/test_walks.py"]
+ assert directive["tests_run_all"] is False
diff --git a/tests/unit/server/mcp/test_pr_test_impact_semantics.py b/tests/unit/server/mcp/test_pr_test_impact_semantics.py
index 7df56a286..743ad5806 100644
--- a/tests/unit/server/mcp/test_pr_test_impact_semantics.py
+++ b/tests/unit/server/mcp/test_pr_test_impact_semantics.py
@@ -103,11 +103,8 @@ async def test_sealed_pr_payload_is_directive_first_typed_and_count_exact(setup_
assert directive["test_recommendations_emitted"] == len(directive["test_recommendations"]) == 10
assert directive["test_recommendations_truncated"] is True
assert directive["test_recommendations_omitted"] == 6
- assert directive["tests_to_run"] == [
- row["test_id"] for row in directive["test_recommendations"]
- ]
- assert directive["tests_to_run_total"] == 14
- assert directive["tests_to_run_basis"] == "measured"
+ # tests_to_run is the selection's run list (test_change_test_selection.py);
+ # the typed rows here are the analyzer's own population.
assert directive["missing_tests"] == ["src/no_match.py"]
assert directive["missing_tests_total"] == directive["missing_tests_emitted"] == 1
assert directive["missing_tests_truncated"] is False
diff --git a/tests/unit/server/mcp/test_recoverable_caps_wire_contracts.py b/tests/unit/server/mcp/test_recoverable_caps_wire_contracts.py
index ee6bbd181..4394d2287 100644
--- a/tests/unit/server/mcp/test_recoverable_caps_wire_contracts.py
+++ b/tests/unit/server/mcp/test_recoverable_caps_wire_contracts.py
@@ -348,7 +348,13 @@ async def test_risk_multi_target_graph_cards_recover_relationship_tails(
async def test_risk_real_adversarial_wire_recovers_each_directive_lane(
setup_mcp: str, session: Any, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
+ import importlib
+
from repowise.core.analysis.pr_blast import PRBlastRadiusAnalyzer
+ from repowise.core.analysis.test_selection import Selection
+
+ get_risk_mod = importlib.import_module("repowise.server.mcp_server.tool_risk.get_risk")
+ from repowise.server.mcp_server.tool_risk.directives import ChangeTests
_configure_omissions(tmp_path)
for index in range(8):
@@ -373,10 +379,6 @@ async def test_risk_real_adversarial_wire_recovers_each_directive_lane(
*[f"tests/sealed_{i}.py" for i in range(8)],
]
payload["cochange_warnings"] = [f"src/cochange_{i}.py" for i in range(8)]
- payload["guarding_tests"] = {
- "tests_to_run": [f"tests/GUARD_{i}.py::test_it" for i in range(14)],
- "basis": "measured",
- }
recommendations = [
{
"test_id": f"tests/REC_{i}.py::test_it",
@@ -401,6 +403,20 @@ async def test_risk_real_adversarial_wire_recovers_each_directive_lane(
return payload
monkeypatch.setattr(PRBlastRadiusAnalyzer, "analyze_files", sealed_analyze)
+ guards = tuple(f"tests/GUARD_{i}.py::test_it" for i in range(14))
+ selection = Selection(
+ run_all=False,
+ reasons=(),
+ tests=guards,
+ test_files=tuple(t.split("::")[0] for t in guards),
+ basis={"src/gap_0.py": "coverage"},
+ why={t.split("::")[0]: "src/gap_0.py changed (coverage)" for t in guards},
+ )
+
+ async def sealed_tests(*_a: Any) -> ChangeTests:
+ return ChangeTests(selection=selection)
+
+ monkeypatch.setattr(get_risk_mod, "_change_tests", sealed_tests)
changed = [f"src/gap_{i}.py" for i in range(6)]
result = await tool_middleware(get_risk)(
["src/auth/service.py"], changed_files=changed, include=["tests", "blast"]
@@ -413,8 +429,7 @@ async def test_risk_real_adversarial_wire_recovers_each_directive_lane(
"missing_cochanges": 8,
"missing_tests": 6,
"tests_to_run": 14,
- # 15 analyzer rows plus the 8 reached tests the measured list lacks.
- "test_recommendations": 23,
+ "test_recommendations": 15,
"files_without_measured_tests": 12,
"test_unknown_files": 12,
}
diff --git a/tests/unit/server/mcp/test_risk.py b/tests/unit/server/mcp/test_risk.py
index c44cd27b9..a8a573df2 100644
--- a/tests/unit/server/mcp/test_risk.py
+++ b/tests/unit/server/mcp/test_risk.py
@@ -156,7 +156,7 @@ async def test_get_risk_stable_file(setup_mcp):
@pytest.mark.asyncio
-async def test_get_risk_pr_directive_splits_test_breakage(setup_mcp):
+async def test_get_risk_pr_directive_splits_test_breakage(pr_checkout):
"""PR mode keeps test files out of may_break; they join tests_to_run (#672)."""
from repowise.server.mcp_server import get_risk
@@ -235,7 +235,7 @@ async def test_get_risk_without_targets_or_changed_files_is_an_error(setup_mcp):
@pytest.mark.asyncio
-async def test_get_risk_pr_directive_surfaces_coverage_backed_tests_to_run(setup_mcp, session):
+async def test_get_risk_pr_directive_surfaces_coverage_backed_tests_to_run(pr_checkout, session, tmp_path):
"""PR directive carries coverage-backed tests_to_run from the per-test map."""
from repowise.core.analysis.health.coverage import TestCoverage
from repowise.core.persistence.crud import save_test_coverage
@@ -268,14 +268,14 @@ async def test_get_risk_pr_directive_surfaces_coverage_backed_tests_to_run(setup
result = await get_risk(["src/auth/service.py"], changed_files=["src/auth/service.py"])
directive = result["directive"]
- assert directive["tests_to_run"] == [
- "tests/test_service.py::test_login",
- "tests/test_service.py::test_logout",
- ]
- # The graph also reaches this file, and must not dilute a measured answer.
+ # The graph selects the whole file, which runs both covering tests.
+ assert directive["tests_to_run"] == ["tests/test_service.py"]
assert directive["tests_to_run_basis"] == "measured"
- assert directive["tests_to_run_kind"] == "test_id"
- assert "2 test(s) to run, measured." in directive["summary"]
+ assert directive["tests_to_run_kind"] == "test_file"
+ assert directive["tests_to_run_why"] == {
+ "tests/test_service.py": "src/auth/service.py changed (coverage)"
+ }
+ assert "1 test(s) to run, measured." in directive["summary"]
directive = (
await get_risk(
["src/auth/service.py"], changed_files=["src/auth/service.py"], include=["tests"]
@@ -294,7 +294,7 @@ async def test_get_risk_pr_directive_surfaces_coverage_backed_tests_to_run(setup
@pytest.mark.asyncio
-async def test_get_risk_tests_to_run_ranks_by_files_reached(setup_mcp, session):
+async def test_get_risk_tests_to_run_ranks_by_files_reached(pr_checkout, session, tmp_path):
"""The test covering both changed files leads, despite sorting last."""
from repowise.core.analysis.health.coverage import TestCoverage
from repowise.core.persistence.crud import save_test_coverage
@@ -309,6 +309,8 @@ async def test_get_risk_tests_to_run_ranks_by_files_reached(setup_mcp, session):
test_file=test_id.split("::")[0],
)
+ for path in ("tests/test_zeta.py", "tests/test_alpha.py", "src/auth/token.py"):
+ (tmp_path / path).write_text("x = 1\n", encoding="utf-8")
await save_test_coverage(
session,
"repo1",
@@ -332,6 +334,7 @@ async def test_get_risk_tests_to_run_ranks_by_files_reached(setup_mcp, session):
assert result["directive"]["tests_to_run"] == [
"tests/test_zeta.py::test_both",
"tests/test_alpha.py::test_one",
+ "tests/test_service.py",
]
assert "tests/test_service.py" in {
row["test_id"] for row in result["directive"]["test_recommendations"]
@@ -359,7 +362,7 @@ class TestRankTestsByReach:
@pytest.mark.asyncio
-async def test_get_risk_pr_directive_falls_back_to_the_graph_without_a_map(setup_mcp):
+async def test_get_risk_pr_directive_falls_back_to_the_graph_without_a_map(pr_checkout):
"""No per-test map -> the import graph answers, labelled as inferred.
The fixture records ``tests/test_service.py`` importing ``service.py``. That
@@ -380,7 +383,7 @@ async def test_get_risk_pr_directive_falls_back_to_the_graph_without_a_map(setup
@pytest.mark.asyncio
-async def test_get_risk_pr_directive_runs_tests_in_import_reach(setup_mcp):
+async def test_get_risk_pr_directive_runs_tests_in_import_reach(pr_checkout):
"""No map and no call-graph test -> the tests in reverse-import reach, inferred.
``tests/test_service.py`` reaches ``models.py`` through ``service.py``. With
@@ -400,7 +403,7 @@ async def test_get_risk_pr_directive_runs_tests_in_import_reach(setup_mcp):
@pytest.mark.asyncio
-async def test_get_risk_pr_directive_lists_tests_to_update(setup_mcp):
+async def test_get_risk_pr_directive_lists_tests_to_update(pr_checkout):
"""The test named for the change leads the edit-list, beside the run-list."""
from repowise.server.mcp_server import get_risk
@@ -418,10 +421,27 @@ async def test_get_risk_pr_directive_lists_tests_to_update(setup_mcp):
assert untested["tests_to_update"] == []
-def _update_list(changed, tests, blast, exclude_spec=None):
+def _selected(tests, importers=(), changed="src/core.py"):
+ """The selection for *changed*, with *tests* selected by it."""
+ from repowise.core.analysis.test_selection import Selection
+ from repowise.server.mcp_server.tool_risk.directives import ChangeTests
+
+ tests = sorted(tests)
+ selection = Selection(
+ run_all=False,
+ reasons=(),
+ tests=tuple(tests),
+ test_files=tuple(tests),
+ basis={changed: "import-graph"},
+ why={t: f"{changed} changed (import-graph)" for t in tests},
+ )
+ return ChangeTests(selection=selection, importers=set(importers))
+
+
+def _update_list(changed, tests, blast, exclude_spec=None, importers=()):
from repowise.server.mcp_server.tool_risk.directives import _tests_to_update
- rows = _tests_to_update(changed, set(tests), blast, exclude_spec)
+ rows = _tests_to_update(changed, _selected(tests, importers), blast, exclude_spec)
return [(r["path"], r["reason"]) for r in rows]
@@ -432,18 +452,31 @@ def test_tests_to_update_name_pair():
def test_tests_to_update_imports_only_direct_importers():
- blast = {
- "transitive_affected": [
- {"path": "tests/test_api.py", "direct": True},
- {"path": "tests/test_far.py", "direct": False},
- {"path": "src/api.py", "direct": True},
- ]
- }
- assert _update_list(["src/core.py"], {"tests/test_api.py", "tests/test_far.py"}, blast) == [
+ tests = {"tests/test_api.py", "tests/test_far.py"}
+ importers = {"tests/test_api.py", "src/api.py"}
+ assert _update_list(["src/core.py"], tests, {}, importers=importers) == [
("tests/test_api.py", "imports")
]
+def test_tests_to_update_skips_tests_no_changed_file_selected():
+ """A test that runs with every subset, or is not collected, is not one to edit."""
+ from repowise.server.mcp_server.tool_risk.directives import _tests_to_update
+
+ tests = _selected(
+ {"tests/test_api.py", "tests/test_walks.py"},
+ importers={"tests/test_walks.py", "tests/__init__.py"},
+ )
+ tests.selection.why["tests/test_walks.py"] = "runs with every subset: it lists files"
+ rows = _tests_to_update(
+ ["src/core.py"],
+ tests,
+ {"cochange_warnings": [{"missing_partner": "tests/__init__.py"}]},
+ None,
+ )
+ assert rows == []
+
+
def test_tests_to_update_co_change_skips_production_partners():
blast = {
"cochange_warnings": [
@@ -472,11 +505,9 @@ def test_tests_to_update_honours_exclude_spec():
spec = pathspec.PathSpec.from_lines("gitwildmatch", ["tests/legacy/"])
tests = {"tests/legacy/test_core.py", "tests/legacy/test_api.py", "tests/test_misc.py"}
- blast = {
- "transitive_affected": [{"path": "tests/legacy/test_api.py", "direct": True}],
- "cochange_warnings": [{"missing_partner": "tests/test_misc.py"}],
- }
- assert _update_list(["src/core.py"], tests, blast, spec) == [
+ blast = {"cochange_warnings": [{"missing_partner": "tests/test_misc.py"}]}
+ importers = {"tests/legacy/test_api.py"}
+ assert _update_list(["src/core.py"], tests, blast, spec, importers) == [
("tests/test_misc.py", "co_change")
]
@@ -484,16 +515,13 @@ def test_tests_to_update_honours_exclude_spec():
def test_tests_to_update_orders_by_rule_and_keeps_the_first_reason():
tests = {"tests/test_core.py", "tests/test_api.py", "tests/test_misc.py"}
blast = {
- "transitive_affected": [
- {"path": "tests/test_api.py", "direct": True},
- {"path": "tests/test_core.py", "direct": True},
- ],
"cochange_warnings": [
{"missing_partner": "tests/test_misc.py"},
{"missing_partner": "tests/test_api.py"},
],
}
- assert _update_list(["src/core.py"], tests, blast) == [
+ importers = {"tests/test_api.py", "tests/test_core.py"}
+ assert _update_list(["src/core.py"], tests, blast, importers=importers) == [
("tests/test_core.py", "name_pair"),
("tests/test_api.py", "imports"),
("tests/test_misc.py", "co_change"),
@@ -512,7 +540,15 @@ def test_build_pr_directive_caps_tests_to_update_at_three():
blast = {"cochange_warnings": [{"missing_partner": t} for t in sorted(tests)]}
response: dict = {"targets": {}}
directives._build_pr_directive(
- response, blast, ["src/core.py"], None, OmissionCollector("get_risk"), [], tests, "repo"
+ response,
+ blast,
+ ["src/core.py"],
+ None,
+ OmissionCollector("get_risk"),
+ [],
+ tests,
+ "repo",
+ tests=_selected(tests),
)
directive = response["directive"]
assert [r["path"] for r in directive["tests_to_update"]] == sorted(tests)[:3]
@@ -520,68 +556,68 @@ def test_build_pr_directive_caps_tests_to_update_at_three():
assert directive["tests_to_update_omitted"] == 2
-def _directive(blast, tests=(), **kwargs):
+def _directive(blast, test_paths=(), **kwargs):
from repowise.server.mcp_server._budget import OmissionCollector
from repowise.server.mcp_server.tool_risk import directives
response: dict = {"targets": {}}
directives._build_pr_directive(
response, blast, ["src/core.py"], None, OmissionCollector("get_risk"), [],
- set(tests), "repo", **kwargs,
+ set(test_paths), "repo", **kwargs,
)
return response["directive"]
-def test_pr_directive_folds_reached_tests_into_tests_to_run():
- blast = {
- "transitive_affected": [{"path": "tests/test_api.py"}, {"path": "src/api.py"}],
- "guarding_tests": {"tests_to_run": ["tests/test_core.py"], "basis": "inferred"},
- }
- directive = _directive(blast, {"tests/test_api.py", "tests/test_core.py"})
- assert directive["tests_to_run"] == ["tests/test_core.py", "tests/test_api.py"]
+def test_pr_directive_runs_what_the_selection_selects():
+ blast = {"transitive_affected": [{"path": "tests/test_api.py"}, {"path": "src/api.py"}]}
+ selected = _selected({"tests/test_core.py"})
+ directive = _directive(blast, {"tests/test_api.py", "tests/test_core.py"}, tests=selected)
+ # A test in reverse-import reach the selection did not pick is not added.
+ assert directive["tests_to_run"] == ["tests/test_core.py"]
assert directive["tests_to_run_basis"] == "inferred"
+ assert directive["tests_run_all"] is False
+ assert directive["tests_to_run_why"] == {
+ "tests/test_core.py": "src/core.py changed (import-graph)"
+ }
assert directive["may_break"] == ["src/api.py"]
- assert "may_break_tests" not in directive
-def test_pr_directive_keeps_a_measured_run_list_unmixed():
- blast = {
- "transitive_affected": [{"path": "tests/test_api.py"}],
- "guarding_tests": {"tests_to_run": ["tests/test_core.py::test_a"], "basis": "measured"},
- }
- directive = _directive(blast, {"tests/test_api.py"})
- assert directive["tests_to_run"] == ["tests/test_core.py::test_a"]
- assert directive["tests_to_run_basis"] == "measured"
- # The reached test is not dropped: it rides as a typed row on request.
- typed = _directive(blast, {"tests/test_api.py"}, include_tests=True)
- assert typed["tests_to_run"] == ["tests/test_core.py::test_a"]
- assert typed["test_recommendations"] == [
- {"test_id": "tests/test_api.py", "basis": "inferred", "reason": "structural_reach"}
- ]
+def test_pr_directive_says_when_every_test_must_run():
+ import dataclasses
+
+ selected = _selected({"tests/test_core.py"})
+ reasons = ("pyproject.toml changed: build or test configuration can change any test.",)
+ selected = dataclasses.replace(
+ selected,
+ selection=dataclasses.replace(selected.selection, run_all=True, reasons=reasons),
+ )
+ directive = _directive({}, tests=selected)
+ assert directive["tests_run_all"] is True
+ assert directive["tests_run_all_reasons"] == list(reasons)
+ assert directive["tests_to_run"] == ["tests/test_core.py"]
+ assert "Every test must run" in directive["summary"]
+
+
+def test_pr_directive_without_a_selection_names_nothing_and_says_why():
+ from repowise.server.mcp_server.tool_risk.directives import ChangeTests
+
+ directive = _directive({}, tests=ChangeTests(status="config_invalid", message="bad tests"))
+ assert directive["tests_to_run"] == []
+ assert directive["tests_to_run_basis"] == "none"
+ assert directive["tests_status"] == "config_invalid"
+ assert directive["tests_status_reason"] == "bad tests"
def test_pr_directive_measured_rows_do_not_repeat_a_reached_test():
row = {"test_id": "tests/test_api.py::test_a", "basis": "measured", "evidence": []}
blast = {
"transitive_affected": [{"path": "tests/test_api.py"}],
- "guarding_tests": {"tests_to_run": ["tests/test_api.py::test_a"], "basis": "measured"},
"test_impact": {"recommendations": [row]},
}
typed = _directive(blast, {"tests/test_api.py"}, include_tests=True)
assert [r["test_id"] for r in typed["test_recommendations"]] == ["tests/test_api.py::test_a"]
-def test_pr_directive_none_basis_with_reached_tests_becomes_inferred():
- blast = {
- "transitive_affected": [{"path": "tests/test_api.py"}],
- "guarding_tests": {"tests_to_run": [], "basis": "none"},
- }
- directive = _directive(blast, {"tests/test_api.py"})
- assert directive["tests_to_run"] == ["tests/test_api.py"]
- assert directive["tests_to_run_basis"] == "inferred"
- assert directive["tests_to_run_kind"] == "test_file"
-
-
def test_pr_directive_serves_test_recommendations_on_request():
row = {"test_id": "tests/test_core.py", "basis": "inferred", "evidence": []}
blast = {"test_impact": {"recommendations": [row]}}
@@ -652,7 +688,7 @@ async def test_get_risk_pr_payload_serializes_directive_first(setup_mcp):
@pytest.mark.asyncio
-async def test_get_risk_test_compatibility_projection_cannot_contradict_typed_rows(setup_mcp):
+async def test_get_risk_test_compatibility_projection_cannot_contradict_typed_rows(pr_checkout):
from repowise.server.mcp_server import get_risk
directive = (
@@ -662,9 +698,10 @@ async def test_get_risk_test_compatibility_projection_cannot_contradict_typed_ro
)["directive"]
recommendations = directive["test_recommendations"]
- assert directive["tests_to_run"] == [row["test_id"] for row in recommendations]
- assert directive["tests_to_run_total"] == directive["test_recommendations_total"]
- assert directive["tests_to_run_emitted"] == len(recommendations)
+ # The run list is the selection; the typed rows name the same test file.
+ assert {row["test_id"].split("::")[0] for row in recommendations} == set(
+ directive["tests_to_run"]
+ )
assert directive["test_recommendations_emitted"] == len(recommendations)
assert all(row["basis"] in {"measured", "inferred"} for row in recommendations)
# ``bases`` rides only when it says something ``basis`` does not; absent
@@ -889,7 +926,7 @@ async def test_get_risk_reach_is_null_when_the_analyzer_gave_no_score(setup_mcp,
@pytest.mark.asyncio
-async def test_get_risk_directive_points_at_the_full_run_list_when_capped(setup_mcp, session):
+async def test_get_risk_directive_points_at_the_full_run_list_when_capped(pr_checkout, session, tmp_path):
"""A capped tests_to_run says where the uncapped copy is."""
from repowise.core.analysis.health.coverage import TestCoverage
from repowise.core.persistence.crud import save_test_coverage
@@ -897,6 +934,8 @@ async def test_get_risk_directive_points_at_the_full_run_list_when_capped(setup_
from repowise.server.mcp_server.tool_risk.directives import _TESTS_TO_RUN_LIMIT
over_cap = _TESTS_TO_RUN_LIMIT + 3
+ for i in range(over_cap):
+ (tmp_path / f"tests/test_{i}.py").write_text("x = 1\n", encoding="utf-8")
await save_test_coverage(
session,
"repo1",
@@ -921,14 +960,14 @@ async def test_get_risk_directive_points_at_the_full_run_list_when_capped(setup_
assert len(directive["tests_to_run"]) == _TESTS_TO_RUN_LIMIT
assert f"Showing {_TESTS_TO_RUN_LIMIT} of {over_cap + 1}" in directive["summary"]
- assert directive["tests_to_run_total"] == over_cap
+ # The covering tests and the test file the graph reaches.
+ assert directive["tests_to_run_total"] == over_cap + 1
assert directive["tests_to_run_emitted"] == _TESTS_TO_RUN_LIMIT
assert directive["tests_to_run_reduced_reason"] == "construction_cap"
assert directive["tests_to_run_truncated"] is True
assert directive["test_recommendations_total"] == over_cap + 1
assert directive["test_recommendations_reduced_reason"] == "construction_cap"
assert "response omission marker" in directive["summary"]
- assert len(result["pr_blast_radius"]["guarding_tests"]["tests_to_run"]) == over_cap
@pytest.mark.asyncio
diff --git a/tests/unit/server/mcp/test_tool_change_risk.py b/tests/unit/server/mcp/test_tool_change_risk.py
index 77e00daf0..73601a2d5 100644
--- a/tests/unit/server/mcp/test_tool_change_risk.py
+++ b/tests/unit/server/mcp/test_tool_change_risk.py
@@ -262,9 +262,16 @@ async def test_impacted_tests_pass_a_changed_lines_error_through(factory, tmp_pa
async def test_impacted_tests_degrade_on_an_unreadable_index(
monkeypatch, factory, tmp_path, exc, status
):
- _get_repo_raising(monkeypatch, exc)
+ from repowise.core.analysis import changed_lines
+
+ async def _raise(*_a, **_k):
+ raise exc
+
+ change = changed_lines.ChangeSet({}, set(), "HEAD", None, None)
+ monkeypatch.setattr(changed_lines, "change_set", lambda *_a, **_k: change)
+ monkeypatch.setattr(tool, "select_change_tests", _raise)
block = await tool._impacted_tests_block(
- SimpleNamespace(session_factory=factory),
+ SimpleNamespace(session_factory=factory, path=str(tmp_path)),
{"a.py": {1}},
None,
OmissionCollector("get_change_risk", repo_root=tmp_path),
@@ -273,57 +280,6 @@ async def test_impacted_tests_degrade_on_an_unreadable_index(
assert block["total"] == 0
-async def test_inferred_tests_treat_a_failed_graph_walk_as_no_reach(
- monkeypatch, session, repo_id, tmp_path
-):
- """A graph walk that throws falls back to "run the full suite", never an error."""
- import repowise.core.analysis.test_reachability as reach
-
- async def _raise(*_a, **_k):
- raise RuntimeError("graph unreadable")
-
- monkeypatch.setattr(reach, "tests_reaching", _raise)
- block = await tool._inferred_impacted(
- session, repo_id, ["a.py"], OmissionCollector("get_change_risk", repo_root=tmp_path)
- )
- assert block["status"] == "no_map"
- assert "run the full suite" in block["summary"]
-
-
-async def test_inferred_tests_run_the_tests_under_a_conftest_not_the_conftest(
- monkeypatch, session, repo_id, tmp_path
-):
- """The walk reports a conftest it stopped at; pytest collects nothing from one.
-
- It stands for the tests under its directory, so ``b.py``, reached only
- through it, still gets tests rather than "run the full suite".
- """
- import repowise.core.analysis.test_reachability as reach
-
- async def _test_files(*_a, **_k):
- return {
- "tests/unit/conftest.py",
- "tests/unit/test_a.py",
- "tests/unit/sub/test_b.py",
- "tests/unit/helpers.py",
- "tests/other/test_c.py",
- }
-
- async def _reaching(*_a, **_k):
- return {"a.py": ["tests/unit/test_a.py"], "b.py": ["tests/unit/conftest.py"]}
-
- monkeypatch.setattr(reach, "load_test_files", _test_files)
- monkeypatch.setattr(reach, "tests_reaching", _reaching)
- block = await tool._inferred_impacted(
- session, repo_id, ["a.py", "b.py"], OmissionCollector("get_change_risk", repo_root=tmp_path)
- )
- # test_a reaches both files, so it leads.
- assert block["tests_to_run"] == ["tests/unit/test_a.py", "tests/unit/sub/test_b.py"]
- assert block["total"] == 2
-
-
@pytest.mark.asyncio
async def test_independent_changes_are_silent_for_one_file_or_an_unreadable_index(
monkeypatch, factory, tmp_path