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(health): stop an unresolved scope from being answered with the whole repo (#2292)
Raghav Chamadiya1f54d0b861eb
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health.py b/packages/server/src/repowise/server/mcp_server/tool_health.pyindex e7e1cdfaa..b64e3b2c9 100644--- a/packages/server/src/repowise/server/mcp_server/tool_health.py+++ b/packages/server/src/repowise/server/mcp_server/tool_health.py@@ -314,6 +314,12 @@ async def _refactoring_blocks("""page = Noneignored: dict[str, str] = {}+ # A scope that resolved to no file is not the dashboard. The queue below+ # honours it through an ``IN ()``, but the rollup and its facets are read+ # by repository id and have no scope to honour, so they have to be withheld+ # rather than filtered — the same reason ``directive`` is dashboard-only.+ resolved_scope = not (scoped and not file_paths)+ rollup_wanted = included and wants("refactoring_summary") and resolved_scopeif included:emits_queue = wants("refactoring_opportunities")query, ignored = parse_refactoring_query(@@ -321,18 +327,18 @@ async def _refactoring_blocks(lead_type=lead_type,confidence=confidence,effort=effort,- file_paths=list(file_paths) if file_paths else None,+ file_paths=list(file_paths) if file_paths is not None else None,limit=min(max(limit, 0), _REFACTORING_COLLECTION_CAP) if emits_queue else 1,offset=cursor if emits_queue else 0,)page = await service.page(query,steps_per_item=_REFACTORING_STEP_CAP if emits_queue else 0,- with_facets=wants("refactoring_summary"),+ with_facets=rollup_wanted,)return _RefactoringBlocks(page=page,- summary=await service.summary() if included and wants("refactoring_summary") else None,+ summary=await service.summary() if rollup_wanted else None,# The dashboard lead only. A targeted call is already about a file the# caller named, so pointing it at the repository's worst file elsewhere# would be answering a question nobody asked.diff --git a/packages/server/src/repowise/server/services/refactoring_health.py b/packages/server/src/repowise/server/services/refactoring_health.pyindex 51741e9cc..7489feb11 100644--- a/packages/server/src/repowise/server/services/refactoring_health.py+++ b/packages/server/src/repowise/server/services/refactoring_health.py@@ -171,7 +171,11 @@ def parse_query(effort=admit("effort", effort, _EFFORTS),mechanical_only=bool(mechanical),addresses_primary=addresses_primary,- file_paths=tuple(file_paths) if file_paths else None,+ # ``is not None``, not truthiness: an empty sequence is a scope that+ # resolved to no file, and the store turns that into ``IN ()``.+ # Reading it as "unscoped" answers a question about nothing with+ # the whole repository.+ file_paths=tuple(file_paths) if file_paths is not None else None,path_contains=(search or "").strip() or None,view=resolved_view,order=admit("order", order, CANONICAL_ORDERS),@@ -239,7 +243,7 @@ class RefactoringHealthService:lead_types=list(query.lead_types) if query.lead_types else None,confidence=query.confidence,effort=query.effort,- file_paths=list(query.file_paths) if query.file_paths else None,+ file_paths=list(query.file_paths) if query.file_paths is not None else None,path_contains=query.path_contains,mechanical_only=query.mechanical_only,addresses_primary=query.addresses_primary,diff --git a/tests/unit/server/mcp/test_health.py b/tests/unit/server/mcp/test_health.pyindex def81b2e7..299b0bd82 100644--- a/tests/unit/server/mcp/test_health.py+++ b/tests/unit/server/mcp/test_health.py@@ -556,6 +556,62 @@ async def test_get_health_unmatched_module_stays_scoped(setup_mcp, health_data):assert not {"kpis", "worst_files", "gap_analysis", "distribution"} & set(result)+async def test_unmatched_module_does_not_serve_repo_wide_refactoring(+ session, setup_mcp, health_data+):+ """The opportunity queue has to respect a scope that resolved to nothing.++ ``file_paths`` reaches the queue as a tuple, and an empty tuple is falsy, so+ it was read as "no scope" and answered with the repository's worst files.+ The neighbouring queues do not have that hole: plans and coverage are gated+ on ``nothing_resolved``, the dashboard directive on ``not scoped``, and the+ performance queue passes its tuple through to an ``IN ()`` that matches+ nothing.++ The rollup beside the queue cannot be filtered the same way — it is read by+ repository id — so it is withheld instead, along with the facets it carries.+ Both halves are asserted here: a call that resolved nothing must not be+ answered with the repository, by either route.+ """+ from repowise.core.analysis.health.refactoring.identity import (+ REFACTORING_MODEL_VERSION,+ )+ from repowise.core.persistence.models import RefactoringOpportunity+ from repowise.server.mcp_server import get_health++ session.add(+ RefactoringOpportunity(+ repository_id=setup_mcp,+ opportunity_id="refop-elsewhere",+ refactoring_model_version=REFACTORING_MODEL_VERSION,+ status="open",+ file_path="src/db/models.py",+ lead_refactoring_type="extract_method",+ details_json=json.dumps({}),+ )+ )+ await session.flush()++ leaked = await get_health(targets=["module:nope"], include=["refactoring"])++ assert leaked["unresolved"] == [{"target": "module:nope", "reason": "no_such_module"}]+ # Indexed, not ``.get``: a renamed key must fail here rather than pass by+ # defaulting to the value the assertion wants.+ assert leaked["refactoring_opportunities"] == []+ assert leaked["refactoring_opportunities_total"] == 0+ # The rollup carries the repository's worst file as its ``lead``, which is+ # the same answer the queue was giving, by another route.+ assert "refactoring_summary" not in leaked++ # The same seed is reachable when the scope does resolve to that file, so+ # the assertion above is about the scope and not about an empty table.+ scoped = await get_health(targets=["src/db/models.py"], include=["refactoring"])+ assert [o["file_path"] for o in scoped["refactoring_opportunities"]] == [+ "src/db/models.py"+ ]++@pytest.mark.asyncioasync def test_get_health_module_target_resolves_to_its_files(setup_mcp, health_data):from repowise.server.mcp_server import get_health