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.

feat(change-risk): scope the health delta to the paths a caller names (#2621)

Raghav Chamadiyada64d24723ab
diff --git a/packages/core/src/repowise/core/analysis/change_health/service.py b/packages/core/src/repowise/core/analysis/change_health/service.py
index aba2dbe3b..4e2a94f2c 100644
--- a/packages/core/src/repowise/core/analysis/change_health/service.py
+++ b/packages/core/src/repowise/core/analysis/change_health/service.py
@@ -65,6 +65,8 @@ class DeltaRequest:
revspec: str | None
extensions: tuple[str, ...] = ()
exclude_patterns: tuple[str, ...] = ()
+ #: Gitignore-style paths to keep; empty keeps every path.
+ include_paths: tuple[str, ...] = ()
class ChangeHealthDeltaService:
@@ -373,11 +375,12 @@ class ChangeHealthDeltaService:
def _filter(changes: list[FileChange], request: DeltaRequest) -> list[FileChange]:
- """Apply the caller's extension and exclusion filters to the change set."""
+ """Apply the caller's extension, inclusion and exclusion filters to the change set."""
return filter_changes(
changes,
extensions=request.extensions,
exclude_patterns=request.exclude_patterns,
+ include_paths=request.include_paths,
)
diff --git a/packages/core/src/repowise/core/analysis/change_health/sources.py b/packages/core/src/repowise/core/analysis/change_health/sources.py
index ab73c7564..d164cd9eb 100644
--- a/packages/core/src/repowise/core/analysis/change_health/sources.py
+++ b/packages/core/src/repowise/core/analysis/change_health/sources.py
@@ -339,8 +339,9 @@ def filter_changes(
*,
extensions: tuple[str, ...] = (),
exclude_patterns: tuple[str, ...] = (),
+ include_paths: tuple[str, ...] = (),
) -> list[FileChange]:
- """Drop the changes a caller's extension and exclusion filters exclude.
+ """Drop the changes a caller's extension, inclusion and exclusion filters exclude.
One implementation, because a change the health comparison counted and one
the change manifest counted have to be the same change; two copies of this
@@ -353,15 +354,24 @@ def filter_changes(
if exclude_patterns
else None
)
+ keep = (
+ pathspec.PathSpec.from_lines("gitwildmatch", include_paths)
+ if include_paths
+ else None
+ )
exts = {e if e.startswith(".") else f".{e}" for e in extensions}
- return [change for change in changes if _counts(change, spec, exts)]
+ return [change for change in changes if _counts(change, spec, exts, keep)]
-def _counts(change: FileChange, spec: object | None, exts: set[str]) -> bool:
+def _counts(
+ change: FileChange, spec: object | None, exts: set[str], keep: object | None = None
+) -> bool:
"""Whether *change* survives the caller's filters. One path, one decision."""
path = change.head_path or change.base_path or ""
if not path:
return False
+ if keep is not None and not keep.match_file(change.head_path): # type: ignore[attr-defined]
+ return False
if spec is not None and spec.match_file(path): # type: ignore[attr-defined]
return False
return not exts or any(path.endswith(ext) for ext in exts)
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 1719ec49c..2e6e32b17 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
@@ -141,6 +141,7 @@ async def get_change_risk(
repo: str | None = None,
extensions: list[str] | None = None,
exclude_patterns: list[str] | None = None,
+ include_paths: list[str] | str | None = None,
baseline: int = 200,
include: list[str] | None = None,
finding_id: str | None = None,
@@ -169,6 +170,8 @@ async def get_change_risk(
repo: Repository alias in workspace mode; omit for the default.
extensions: File suffixes to count, e.g. ``[".py", ".ts"]``.
exclude_patterns: Gitignore-style paths to omit, e.g. ``["tests/"]``.
+ include_paths: Gitignore-style paths to keep, as a list or one
+ comma-separated string, e.g. ``"src/api/,src/db/"``. Omit for all.
baseline: Recent commits sampled for percentile ranking; 0 disables it.
include: ``"findings"`` for every change finding, ``"diagnostics"`` for
raw score mechanics, ``"scales"`` for units. All identical on
@@ -232,6 +235,7 @@ async def get_change_risk(
revspec,
tuple(extensions or ()),
tuple(result.riskignore_excludes + result.request_excludes),
+ tuple(include_paths or ()),
)
)
try:
@@ -336,13 +340,14 @@ def _compare_health(
revspec: str | None,
extensions: tuple[str, ...],
exclude_patterns: tuple[str, ...],
+ include_paths: tuple[str, ...] = (),
) -> Any:
"""Run the comparison, degrading to an explicit unavailable state."""
from repowise.core.analysis.change_health.models import ChangeHealthDelta
try:
return _delta_service(repo_path).compare(
- DeltaRequest(repo_path, revspec, extensions, exclude_patterns)
+ DeltaRequest(repo_path, revspec, extensions, exclude_patterns, include_paths)
)
except Exception as exc:
log.warning("change_health_comparison_failed", revspec=revspec, error=str(exc))
diff --git a/tests/unit/change_health/test_comparison.py b/tests/unit/change_health/test_comparison.py
index a5d30a218..548c285c9 100644
--- a/tests/unit/change_health/test_comparison.py
+++ b/tests/unit/change_health/test_comparison.py
@@ -432,6 +432,20 @@ def test_exclusions_and_extensions_narrow_the_compared_scope(make_repo):
assert excluded.scope.changed == 1
+def test_include_paths_keep_only_the_named_paths(make_repo):
+ repo = make_repo()
+ repo.commit("seed", {"app.py": "x = 1\n"})
+ repo.commit(
+ "add",
+ {"app.py": python_complex("a", 16), "vendor/lib.py": python_complex("b", 16)},
+ )
+
+ included = compare(repo, "HEAD", include_paths=("vendor/",))
+
+ assert all(f.path.startswith("vendor/") for f in included.findings)
+ assert included.scope.changed == 1
+
+
def test_change_finding_ids_are_deterministic_across_runs(make_repo):
repo = make_repo()
repo.commit("seed", {"app.py": "x = 1\n"})