Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(change-risk): split include_paths given as a string and key the cache on it (#3698)
Swati Ahuja9b0e3feb559c
diff --git a/packages/core/src/repowise/core/analysis/change_health/service.py b/packages/core/src/repowise/core/analysis/change_health/service.pyindex 6c711699a..a1a80337c 100644--- a/packages/core/src/repowise/core/analysis/change_health/service.py+++ b/packages/core/src/repowise/core/analysis/change_health/service.py@@ -163,8 +163,10 @@ class ChangeHealthDeltaService:str(HEALTH_ANALYZER_VERSION),self.rules_fingerprint,str(PERFORMANCE_MODEL_VERSION),- ",".join(request.extensions),- ",".join(request.exclude_patterns),+ # repr, not a join: a comma is legal inside a pattern.+ repr(tuple(request.extensions)),+ repr(tuple(request.exclude_patterns)),+ repr(tuple(request.include_paths)),)return hashlib.sha256("\x00".join(parts).encode("utf-8")).hexdigest()diff --git a/packages/core/src/repowise/core/analysis/change_health/sources.py b/packages/core/src/repowise/core/analysis/change_health/sources.pyindex 29f0576e6..eea9ebe32 100644--- a/packages/core/src/repowise/core/analysis/change_health/sources.py+++ b/packages/core/src/repowise/core/analysis/change_health/sources.py@@ -370,7 +370,7 @@ def _counts(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]+ if keep is not None and not keep.match_file(path): # type: ignore[attr-defined]return Falseif spec is not None and spec.match_file(path): # type: ignore[attr-defined]return Falsediff --git a/packages/server/src/repowise/server/mcp_server/tool_change_risk.py b/packages/server/src/repowise/server/mcp_server/tool_change_risk.pyindex 2293f49ef..0aeb1f84e 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@@ -153,6 +153,15 @@ _DIFF_SIZE_LABELS = {}+def _path_patterns(value: list[str] | str | None) -> tuple[str, ...]:+ """A list of patterns, or one comma-separated string, as a pattern tuple.++ ``tuple("src/")`` would be one pattern per character, which matches nothing.+ """+ items = value.split(",") if isinstance(value, str) else value or ()+ return tuple(item.strip() for item in items if item.strip())++async def _score_change(repo_path: Any,revspec: str | None,@@ -377,7 +386,7 @@ async def get_change_risk(revspec,tuple(extensions or ()),tuple(result.riskignore_excludes + result.request_excludes),- tuple(include_paths or ()),+ _path_patterns(include_paths),))try:diff --git a/tests/unit/change_health/test_comparison.py b/tests/unit/change_health/test_comparison.pyindex e6028afd7..c706f2250 100644--- a/tests/unit/change_health/test_comparison.py+++ b/tests/unit/change_health/test_comparison.py@@ -496,6 +496,22 @@ def test_include_paths_keep_only_the_named_paths(make_repo):assert included.scope.changed == 1+def test_include_paths_match_deleted_and_renamed_files_by_their_path(make_repo):+ repo = make_repo()+ repo.commit(+ "seed",+ {"app.py": "x = 1\n", "vendor/old.py": "y = 1\n", "lib/moved.py": "z = 1\n"},+ )+ repo.remove("vendor/old.py")+ repo.move("lib/moved.py", "vendor/moved.py")+ repo.commit("change", {"app.py": "x = 2\n", "vendor/new.py": "w = 1\n"})++ included = compare(repo, "HEAD", include_paths=("vendor/",))++ # A deletion has no head path; it is kept by its base path, a rename by its new path.+ assert included.scope.changed == 3++def test_change_finding_ids_are_deterministic_across_runs(make_repo):repo = make_repo()repo.commit("seed", {"app.py": "x = 1\n"})diff --git a/tests/unit/change_health/test_mcp_contract.py b/tests/unit/change_health/test_mcp_contract.pyindex 2465faf2e..aa13954ea 100644--- a/tests/unit/change_health/test_mcp_contract.py+++ b/tests/unit/change_health/test_mcp_contract.py@@ -2,6 +2,7 @@from __future__ import annotations+import dataclassesimport importlibimport jsonfrom concurrent.futures import ThreadPoolExecutor@@ -264,3 +265,40 @@ def test_a_working_tree_comparison_is_keyed_by_content_not_by_head(make_repo):assert first.introduced_total >= 1assert second.introduced_total == 0+++def test_every_request_filter_is_part_of_the_cache_key(make_repo):+ repo = seeded(make_repo)+ service = ChangeHealthDeltaService(repo_path=str(repo.path))+ pair = service.source.resolve("HEAD")+ plain = DeltaRequest(str(repo.path), "HEAD")+ # repo_path and revspec reach the key through the service and the resolved pair.+ for field in dataclasses.fields(DeltaRequest):+ if field.name in ("repo_path", "revspec"):+ continue+ narrowed = dataclasses.replace(plain, **{field.name: ("vendor/",)})+ assert service._cache_key(narrowed, pair) != service._cache_key(plain, pair), field.name+ # A comma is legal inside a pattern, so one "a,b" pattern is not "a" and "b".+ joined = dataclasses.replace(plain, **{field.name: ("a,b",)})+ split = dataclasses.replace(plain, **{field.name: ("a", "b")})+ assert service._cache_key(joined, pair) != service._cache_key(split, pair), field.name+++async def test_include_paths_as_a_string_filters_like_the_list(tool, 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)},+ )+ module = tool(repo)++ async def changed(**kwargs):+ result = await module.get_change_risk("HEAD", baseline=0, **kwargs)+ return result["health_delta"]["scope"]["changed"]++ assert await changed(include_paths="vendor/") == 1+ assert await changed(include_paths=["vendor/"]) == 1+ assert await changed(include_paths="docs/, vendor/") == 1+ # A filtered result is never served to the unfiltered call.+ assert await changed() == 2