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(change-risk): keep repowise's own output out of test selection (#3770)

Swati Ahuja9c31e9ab936d
diff --git a/docs/agent/MCP_TOOLS.md b/docs/agent/MCP_TOOLS.md
index d6c5f738f..764e6c912 100644
--- a/docs/agent/MCP_TOOLS.md
+++ b/docs/agent/MCP_TOOLS.md
@@ -263,7 +263,7 @@ Reviews one commit, a `base..head` range, or uncommitted work by comparing the t
| `revspec` | string | uncommitted work | A commit, `base..head`, or `base...head` (from the merge-base). Pass `HEAD` when the tree is clean |
| `extensions` | list[string] | all | File suffixes to count, e.g. `[".py", ".ts"]` |
| `exclude_patterns` | list[string] | none | Gitignore-style paths to omit; combined with a root `.riskignore` |
-| `include_paths` | list[string] or string | all | Gitignore-style paths to keep, as a list or one comma-separated string. Like `exclude_patterns`, it scopes the score, the health delta and every per-file block; `impacted_tests` still selects over the whole change |
+| `include_paths` | list[string] or string | all | Gitignore-style paths to keep, as a list or one comma-separated string. Like `exclude_patterns`, it scopes the score, the health delta and every per-file block; `impacted_tests` still selects over the whole change, less only repowise's own output |
| `baseline` | int | `200` | Recent commits sampled for percentile ranking; `0` disables percentiles |
| `include` | list[string] | none | `findings` (every finding), `diagnostics` (raw score mechanics, plus the delta's analyzer, revisions and limits), `scales` (units) |
| `finding_id` | string | none | Expand one `health_delta` finding |
diff --git a/docs/reference/MCP_RESPONSE_FIELDS.md b/docs/reference/MCP_RESPONSE_FIELDS.md
index e5665332b..49e02f3b1 100644
--- a/docs/reference/MCP_RESPONSE_FIELDS.md
+++ b/docs/reference/MCP_RESPONSE_FIELDS.md
@@ -262,7 +262,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. Tree-sitter query files (`.scm` under a `queries` directory) and text templates (`.j2`, `.jinja`, `.jinja2`, `.tmpl`, `.tpl`, `.hbs`, `.mustache`, `.ejs`, `.njk`) are skipped with reason `not_analyzable_data`, which also never makes a run `partial`; a change made only of them reads `clear_in_analyzed_scope` with its own headline, since they can still change behaviour. 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.** 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`) is in `repowise impacted-tests`' [run order](CLI_REFERENCE.md#repowise-impacted-tests-revspec), the `always_run_total` tests that run with every subset last; `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`.
+**impacted_tests.** The selection `repowise impacted-tests` makes, over every path the change touched whatever the score's filters, less the repowise output the score also leaves out (the index, and a managed file whose only change is repowise's block or entry), which the CLI still reads when tracked. `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`) is in `repowise impacted-tests`' [run order](CLI_REFERENCE.md#repowise-impacted-tests-revspec), the `always_run_total` tests that run with every subset last; `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; then a file git shows changed since that commit reads `stale_file`, with no figures, and only untouched files keep theirs. `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/core/src/repowise/core/analysis/change_risk/service.py b/packages/core/src/repowise/core/analysis/change_risk/service.py
index 26a0d767b..96692a8d5 100644
--- a/packages/core/src/repowise/core/analysis/change_risk/service.py
+++ b/packages/core/src/repowise/core/analysis/change_risk/service.py
@@ -46,6 +46,10 @@ class ChangeRiskResult:
riskignore_excludes: tuple[str, ...]
request_excludes: tuple[str, ...]
working_tree: bool = False # scored the uncommitted change, not a commit
+ # The part of ``riskignore_excludes`` that is repowise's own output rather
+ # than the user's ``.riskignore``: not the change at all, so test selection
+ # drops it too, while the user's filters leave selection whole.
+ owned_excludes: tuple[str, ...] = ()
# Bug-fix history of the ground this change stands on. ``density`` is the
# churn-weighted mean fix pressure of the touched files, ``percentile`` ranks
# it against the same measure over the repo's own recent commits, and
@@ -189,10 +193,12 @@ def score_live_change(
baseline_excludes = from_riskignore + exclude_patterns
target = revspec or "HEAD"
uncommitted = None
+ generated: tuple[str, ...] = ()
if revspec is None and working_tree_is_dirty(repo_path):
# A managed file (CLAUDE.md, .mcp.json) whose edit is only repowise's
# block or entry is repowise's output too, while the user's edits count.
- worktree_ignores = from_riskignore + generated_worktree_paths(repo_path)
+ generated = generated_worktree_paths(repo_path)
+ worktree_ignores = from_riskignore + generated
uncommitted = extract_worktree_features(
repo_path,
extensions=extensions,
@@ -259,7 +265,9 @@ def score_live_change(
riskignore_excludes=from_riskignore,
request_excludes=exclude_patterns,
)
- return replace(result, history_ref=history)
+ owned = tuple(p for p in from_riskignore if p in OWNED_PATTERNS)
+ owned += generated if working_tree else ()
+ return replace(result, history_ref=history, owned_excludes=owned)
def assess_change(
diff --git a/packages/core/src/repowise/core/analysis/changed_lines.py b/packages/core/src/repowise/core/analysis/changed_lines.py
index 41145b261..df00ba0c8 100644
--- a/packages/core/src/repowise/core/analysis/changed_lines.py
+++ b/packages/core/src/repowise/core/analysis/changed_lines.py
@@ -26,7 +26,7 @@ from collections.abc import Iterable
from dataclasses import dataclass, field
from pathlib import Path
-from .git_cli import _git, split_revspec, untracked_paths
+from .git_cli import _git, path_filter, split_revspec, untracked_paths
# ``@@ -a,b +c,d @@`` - both sides. ``b``/``d`` default to 1 when omitted; a
# count of 0 means "nothing on that side" (pure insertion / pure deletion).
@@ -305,12 +305,14 @@ def change_set(
*,
staged: bool = False,
working_tree: bool = False,
+ exclude_patterns: tuple[str, ...] = (),
) -> ChangeSet:
"""The change :func:`changed_lines` reads, with every touched path and its ends.
*working_tree* reads what ``HEAD`` does not have, staged or not, untracked
- files included; its base is ``HEAD``. Raises ``ValueError`` on an unknown
- revision, as :func:`changed_lines` does.
+ files included; its base is ``HEAD``. *exclude_patterns* (gitignore-style)
+ drops paths that are not part of the change at all, such as repowise's own
+ output. Raises ``ValueError`` on an unknown revision, as :func:`changed_lines` does.
"""
if working_tree:
command, revisions, label = ["diff"], ["HEAD"], working_tree_label()
@@ -322,15 +324,16 @@ def change_set(
fields = [f.strip("\n") for f in listing.split("\0")]
files: dict[str, FileDiff] = {}
deleted: set[str] = set()
+ counts = path_filter(exclude_patterns=exclude_patterns)
for status, path in zip(fields[0::2], fields[1::2], strict=False):
- if not path:
+ if not path or not counts(path):
continue
if status.startswith("D"):
deleted.add(path)
else:
files[path] = diffs.get(path) or FileDiff(path=path)
if working_tree:
- files.update(untracked_diffs(repo_path))
+ files.update(untracked_diffs(repo_path, exclude_patterns))
base, head = _change_ends(repo_path, revspec, staged=staged or working_tree)
return ChangeSet(files, deleted, label, base, head)
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 b10fc761f..2a35b7ba5 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
@@ -218,6 +218,7 @@ async def _attach_change_blocks(
collector,
revspec=revspec,
working_tree=result.working_tree,
+ owned_excludes=result.owned_excludes,
)
patch_changed, patch_error, patch_label, patch_revspec = await _push_change(
str(ctx.path),
@@ -1441,13 +1442,16 @@ async def _impacted_tests_block(
*,
revspec: str | None = None,
working_tree: bool = False,
+ owned_excludes: tuple[str, ...] = (),
) -> dict[str, Any]:
"""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
+ does in CI. The caller's suffix, include and exclude filters do not narrow
+ it; only *owned_excludes*, repowise's own output, leaves it, as it leaves
+ every other block. ``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.
@@ -1471,6 +1475,7 @@ async def _impacted_tests_block(
str(ctx.path),
None if working_tree else revspec or "HEAD",
working_tree=working_tree,
+ exclude_patterns=owned_excludes,
)
)
except Exception: # git refused the change, or could not run
diff --git a/tests/unit/analysis/test_change_risk.py b/tests/unit/analysis/test_change_risk.py
index fb2ed19cb..2bfd05510 100644
--- a/tests/unit/analysis/test_change_risk.py
+++ b/tests/unit/analysis/test_change_risk.py
@@ -592,6 +592,21 @@ def test_a_tracked_edit_and_an_untracked_file_both_count(git_repo: Path) -> None
assert _working_tree_file_sets(git_repo, result) == ({"src/a.py", "src/new.py"},) * 2
+def test_owned_excludes_name_repowise_output_not_the_users_filters(git_repo: Path) -> None:
+ """Test selection drops only repowise's output; .riskignore and request excludes stay."""
+ _commit(git_repo, {"src/a.py": "x = 1\n", ".riskignore": "vendor/\n"}, "feat: a", "Dev")
+ (git_repo / "src" / "a.py").write_text("x = 1\ny = 2\n", encoding="utf-8")
+ (git_repo / "AGENTS.md").write_text(_BLOCK.format(body="hi"), encoding="utf-8")
+ (git_repo / ".repowise").mkdir()
+ (git_repo / ".repowise" / "config.yaml").write_text("provider: x\n", encoding="utf-8")
+
+ result = score_live_change(str(git_repo), baseline=0, exclude_patterns=("docs/",))
+
+ assert result.working_tree is True
+ assert result.owned_excludes == (".repowise/", "/AGENTS.md")
+ assert {"vendor/", "docs/"} <= set(result.riskignore_excludes + result.request_excludes)
+
+
def test_explicit_head_scores_the_commit_even_when_dirty(git_repo: Path) -> None:
"""An explicit revspec always means committed refs."""
_commit(git_repo, {"src/a.py": "x = 1\n"}, "feat: a", author="Dev")
diff --git a/tests/unit/analysis/test_changed_lines.py b/tests/unit/analysis/test_changed_lines.py
index c9087ce1a..c39876d91 100644
--- a/tests/unit/analysis/test_changed_lines.py
+++ b/tests/unit/analysis/test_changed_lines.py
@@ -311,6 +311,23 @@ def test_change_set_keeps_what_changed_lines_drops(git_repo) -> None:
assert (change.base, change.head) == (_sha(git_repo, "HEAD~1"), _sha(git_repo, "HEAD"))
+def test_change_set_drops_excluded_paths_in_every_listing(git_repo) -> None:
+ from repowise.core.analysis.changed_lines import change_set
+
+ (git_repo / "gen.md").write_text("g\n", encoding="utf-8")
+ _git(git_repo, "add", "-A")
+ _git(git_repo, "commit", "-qm", "gen")
+ (git_repo / "mod.py").write_text("a = 1\n", encoding="utf-8")
+ (git_repo / "gen.md").unlink()
+ (git_repo / "out").mkdir()
+ (git_repo / "out" / "x.md").write_text("x\n", encoding="utf-8")
+ (git_repo / "new.py").write_text("n = 1\n", encoding="utf-8")
+
+ change = change_set(str(git_repo), working_tree=True, exclude_patterns=("/gen.md", "out/"))
+ assert set(change.files) == {"mod.py", "new.py"}
+ assert change.deleted == set()
+
+
def test_change_set_ends_for_a_commit_a_fork_and_the_index(git_repo) -> None:
from repowise.core.analysis.changed_lines import change_set
diff --git a/tests/unit/server/mcp/test_change_risk.py b/tests/unit/server/mcp/test_change_risk.py
index 7452ee96c..145c83d57 100644
--- a/tests/unit/server/mcp/test_change_risk.py
+++ b/tests/unit/server/mcp/test_change_risk.py
@@ -632,6 +632,48 @@ async def test_get_change_risk_untracked_only_tree_is_one_change(tmp_path, monke
assert result["exclude_patterns"] == ["/.github/copilot-instructions.md"]
[email protected](("user_text", "selected"), [("# Mine\n", False), ("# Mine, edited\n", True)])
+async def test_impacted_tests_skip_a_managed_file_only_repowise_changed(
+ tmp_path, monkeypatch, user_text, selected
+) -> None:
+ """A refreshed managed block leaves test selection; the user's edit beside it does not."""
+ from repowise.cli.agent_targets.instructions import DISTILL_MARKER_END, DISTILL_MARKER_START
+ from repowise.core.analysis import changed_lines
+
+ def block(body: str) -> str:
+ return f"\n{DISTILL_MARKER_START}\n{body}\n{DISTILL_MARKER_END}\n"
+
+ repo = tmp_path / "repo"
+ repo.mkdir()
+ _git(["init", "-q"], repo)
+ _commit(repo, {"src/app.py": "a = 1\n", "AGENTS.md": "# Mine\n" + block("v1")}, "chore: seed")
+ (repo / "src" / "app.py").write_text("a = 1\nb = 2\n", encoding="utf-8")
+ (repo / "AGENTS.md").write_text(user_text + block("v2"), encoding="utf-8")
+
+ seen: dict = {}
+ real = changed_lines.change_set
+
+ def _spy(*args, **kwargs):
+ change = real(*args, **kwargs)
+ seen["files"] = set(change.files)
+ return change
+
+ monkeypatch.setattr(changed_lines, "change_set", _spy)
+ factory = await _factory_with_repo(None)
+ 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)
+ result = await module.get_change_risk(baseline=0)
+
+ assert result["working_tree"] is True
+ assert "src/app.py" in seen["files"]
+ assert ("AGENTS.md" in seen["files"]) is selected
+
+
@pytest.mark.asyncio
async def test_get_change_risk_keeps_is_fix_when_true(tmp_path, monkeypatch) -> None:
repo = tmp_path / "repo"
diff --git a/tests/unit/server/mcp/test_tool_change_risk.py b/tests/unit/server/mcp/test_tool_change_risk.py
index 1de3a921f..88c3f9fc5 100644
--- a/tests/unit/server/mcp/test_tool_change_risk.py
+++ b/tests/unit/server/mcp/test_tool_change_risk.py
@@ -296,6 +296,29 @@ async def test_impacted_tests_degrade_on_an_unreadable_index(
assert block["total"] == 0
+async def test_impacted_tests_read_the_change_less_repowise_output(monkeypatch, factory, tmp_path):
+ """Selection drops repowise's own output, the one exclusion every block shares."""
+ from repowise.core.analysis import changed_lines
+
+ seen: dict = {}
+
+ def _change_set(*_a, **kwargs):
+ seen.update(kwargs)
+ raise ValueError("stop here")
+
+ monkeypatch.setattr(changed_lines, "change_set", _change_set)
+ await tool._impacted_tests_block(
+ SimpleNamespace(session_factory=factory, path=str(tmp_path)),
+ {"a.py": {1}},
+ None,
+ OmissionCollector("get_change_risk", repo_root=tmp_path),
+ working_tree=True,
+ owned_excludes=("/AGENTS.md",),
+ )
+ assert seen["exclude_patterns"] == ("/AGENTS.md",)
+
+
@pytest.mark.asyncio
async def test_independent_changes_are_silent_for_one_file_or_an_unreadable_index(
monkeypatch, factory, tmp_path