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
refactor(health): split the work queue builder, name the recent-regression conditions, cover the chat adapter (#2854)
Raghav Chamadiya481d32cf4a44
diff --git a/packages/core/src/repowise/core/analysis/actions/build.py b/packages/core/src/repowise/core/analysis/actions/build.pyindex 4ac177f05..756d4fd4f 100644--- a/packages/core/src/repowise/core/analysis/actions/build.py+++ b/packages/core/src/repowise/core/analysis/actions/build.py@@ -240,6 +240,20 @@ def build_authors(rows: Rows, *, since: datetime | None) -> dict[str, Any]:return {"author_last_commit": last, "active_authors_90d": len(active)}+def _authored_regression(row: Any) -> bool:+ """The commit's own lines introduced or worsened a critical/high finding."""+ return (+ field(row, "change_kind") in ("introduced", "worsened")+ and field(row, "severity") in ("critical", "high")+ and field(row, "attribution_basis") in AUTHORED_BASES+ )+++def _shown_code_shape(biomarker: Any, hidden: frozenset[str]) -> bool:+ """A code-shape marker, not a history one, and not one the registry hides."""+ return biomarker_category(biomarker) != HISTORY_CATEGORY and biomarker not in hidden++def _recent_candidates(rows: Rows, *, week: datetime | None, files: Mapping[str, FileFacts]) -> list[tuple[Any, datetime]]:@@ -250,17 +264,11 @@ def _recent_candidates(out = []for f in rows:at = _when(field(f, "committed_at"))- biomarker = field(f, "biomarker_type")- if (- at is not None- and at >= week- and field(f, "change_kind") in ("introduced", "worsened")- and field(f, "severity") in ("critical", "high")- and field(f, "attribution_basis") in AUTHORED_BASES- and not _is_test(field(f, "file_path"), files)- and biomarker_category(biomarker) != HISTORY_CATEGORY- and biomarker not in hidden- ):+ if at is None or at < week or not _authored_regression(f):+ continue+ if _is_test(field(f, "file_path"), files):+ continue+ if _shown_code_shape(field(f, "biomarker_type"), hidden):out.append((f, at))return outdiff --git a/packages/server/src/repowise/server/chat_tools.py b/packages/server/src/repowise/server/chat_tools.pyindex 149f2f868..5ae939417 100644--- a/packages/server/src/repowise/server/chat_tools.py+++ b/packages/server/src/repowise/server/chat_tools.py@@ -121,52 +121,40 @@ async def execute_tool(confirmed: bool = False,) -> dict[str, Any]:"""Execute a tool only when it belongs to the configured MCP surface."""- tool = next(- (candidate for candidate in get_tool_catalog(repo_path) if candidate.entry.name == name),- None,- )- if tool is None:+ entry = _catalog_entry(name, repo_path)+ if entry is None:return {"error": f"Tool is not enabled for this repository: {name}","error_code": "tool_not_enabled",}- return await execute_entry(tool.entry, arguments, repo=repo, confirmed=confirmed)+ return await execute_entry(entry, arguments, repo=repo, confirmed=confirmed)-def get_artifact_type(tool_name: str, repo_path: str | None) -> str:- tool = next(+def _catalog_entry(tool_name: str, repo_path: str | None) -> ToolEntry | None:+ """The named entry when it belongs to the repository's configured surface."""+ return next((- candidate+ candidate.entryfor candidate in get_tool_catalog(repo_path)if candidate.entry.name == tool_name),None,)- return tool.entry.artifact_type if tool is not None else "generic"+++def get_artifact_type(tool_name: str, repo_path: str | None) -> str:+ entry = _catalog_entry(tool_name, repo_path)+ return entry.artifact_type if entry is not None else "generic"def get_artifact_presentation(tool_name: str, repo_path: str | None) -> str:- tool = next(- (- candidate- for candidate in get_tool_catalog(repo_path)- if candidate.entry.name == tool_name- ),- None,- )- return tool.entry.presentation if tool is not None else "generic"+ entry = _catalog_entry(tool_name, repo_path)+ return entry.presentation if entry is not None else "generic"def get_artifact_evidence_basis(tool_name: str, repo_path: str | None) -> str:- tool = next(- (- candidate- for candidate in get_tool_catalog(repo_path)- if candidate.entry.name == tool_name- ),- None,- )- return tool.entry.evidence_basis if tool is not None else "unknown"+ entry = _catalog_entry(tool_name, repo_path)+ return entry.evidence_basis if entry is not None else "unknown"def init_tool_state(diff --git a/packages/server/src/repowise/server/routers/code_health/refactoring_routes.py b/packages/server/src/repowise/server/routers/code_health/refactoring_routes.pyindex 89959f10a..97b68be42 100644--- a/packages/server/src/repowise/server/routers/code_health/refactoring_routes.py+++ b/packages/server/src/repowise/server/routers/code_health/refactoring_routes.py@@ -6,6 +6,7 @@ not structured refactoring plans.from __future__ import annotations+from collections.abc import Callablefrom dataclasses import dataclassfrom typing import Any@@ -36,6 +37,9 @@ _EFFORT_BUCKETS: tuple[tuple[int, str], ...] = ((400, "L"),)+# The divisor behind ``impact_per_effort``, and the order ``max_effort`` caps.+_EFFORT_RANK = {"S": 1, "M": 2, "L": 3, "XL": 5}+_SORT_KEYS = {"impact_per_effort": lambda t: (-t["impact_per_effort"], -t["total_impact"]),@@ -116,14 +120,10 @@ def queue_filters()-async def _queue_targets(- session: AsyncSession, repo_id: str, q: QueueFilters, *, history: str-) -> tuple[list[dict], int]:- """Every work item the filters keep, unsorted, and the history-only count."""- repo = await crud.get_repository(session, repo_id)- if repo is None:- raise HTTPException(status_code=404, detail="Repository not found")-+async def _load_queue_rows(+ session: AsyncSession, repo_id: str, q: QueueFilters+) -> tuple[dict[str, Any], list[Any]]:+ """The metrics the file-level filters keep, by path, and the findings read."""metrics = await crud.get_health_metrics(session, repo_id)# As the findings list does, so a row's count and the list behind it agree:# the zero-impact dimensions stay out of the ranking, and naming one thing@@ -150,29 +150,95 @@ async def _queue_targets(only_failing=q.only_failing,hotspots=await hotspot_paths(session, repo_id) if q.only_hotspots else None,)- metric_by_path = {m.file_path: m for m in metrics if keep_metric(m)}+ return {m.file_path: m for m in metrics if keep_metric(m)}, findings++def _finding_filter(q: QueueFilters) -> Callable[[Any], bool]:+ """One marker, then exact severities or else a severity floor."""# An all-empty list (","; " ") means the caller selected nothing, not that# nothing matches — falling through to ``min_severity`` keeps a stray# serialization from silently emptying the queue behind a 200.picked = {v.strip().lower() for v in (q.severity or "").split(",") if v.strip()}- exact_severities = picked or None+ floor = _SEVERITY_ORDER.get(q.min_severity, 0) if q.min_severity else None++ def keep(f: Any) -> bool:+ if q.biomarker and f.biomarker_type != q.biomarker:+ return False+ if picked:+ return (f.severity or "").lower() in picked+ if floor is not None:+ return _SEVERITY_ORDER.get(f.severity, 0) >= floor+ return True+ return keep+++def _group_by_file(findings: list[Any], keep: Callable[[Any], bool]) -> dict[str, list[Any]]:by_file: dict[str, list[Any]] = {}for f in findings:- if q.biomarker and f.biomarker_type != q.biomarker:- continue- if exact_severities is not None:- if (f.severity or "").lower() not in exact_severities:- continue- elif q.min_severity:- order = _SEVERITY_ORDER- if order.get(f.severity, 0) < order.get(q.min_severity, 0):- continue- by_file.setdefault(f.file_path, []).append(f)-- effort_rank = {"S": 1, "M": 2, "L": 3, "XL": 5}- max_effort_rank = effort_rank.get(q.max_effort or "", 99)+ if keep(f):+ by_file.setdefault(f.file_path, []).append(f)+ return by_file+++def _is_history_only(fs: list[Any], q: QueueFilters, history: str) -> bool:+ # Naming a marker reaches it whatever its origin, as with the zero-impact+ # dimensions in ``_load_queue_rows``.+ return history == "exclude" and q.biomarker is None and not split_by_origin(fs)[0]+++def _lead_finding(fs: list[Any]) -> Any:+ primary = primary_finding(fs)+ if primary is not None:+ return primary+ # Every finding here is advisory, so no cause accuses this file and the+ # general queue never reaches this: advisory is excluded from it. A caller+ # who filtered to an advisory marker did reach it, and the marker they+ # asked for is the honest lead for the row.+ return max(fs, key=lambda x: (_SEVERITY_ORDER.get(x.severity, 0), -(x.line_start or 0)))+++def _target_row(file_path: str, fs: list[Any], m: Any, effort_bucket: str) -> dict:+ primary = _lead_finding(fs)+ # Impact, and therefore the ranking, counts only findings still open:+ # ``score`` on this row was computed from open findings, and a file+ # whose findings were all dismissed is not work to rank near the top.+ open_fs = [x for x in fs if (getattr(x, "status", None) or "open") == "open"]+ total_impact = round(sum(x.health_impact for x in open_fs), 3)+ return {+ "file_path": file_path,+ "score": round(m.score, 2),+ "nloc": m.nloc,+ "module": m.module or None,+ "is_test": bool(getattr(m, "is_test", False)),+ "primary_biomarker": primary.biomarker_type,+ "primary_severity": primary.severity,+ "primary_reason": primary.reason,+ "primary_function": primary.function_name,+ "primary_line_start": primary.line_start,+ "primary_line_end": primary.line_end,+ "primary_suggestion": _suggestion_for(primary.biomarker_type),+ "primary_finding_id": primary.id,+ "total_impact": total_impact,+ "finding_count": len(fs),+ "open_finding_count": len(open_fs),+ "biomarkers": sorted({x.biomarker_type for x in fs}),+ "effort_bucket": effort_bucket,+ "impact_per_effort": round(total_impact / _EFFORT_RANK[effort_bucket], 3),+ }+++async def _queue_targets(+ session: AsyncSession, repo_id: str, q: QueueFilters, *, history: str+) -> tuple[list[dict], int]:+ """Every work item the filters keep, unsorted, and the history-only count."""+ repo = await crud.get_repository(session, repo_id)+ if repo is None:+ raise HTTPException(status_code=404, detail="Repository not found")++ metric_by_path, findings = await _load_queue_rows(session, repo_id, q)+ by_file = _group_by_file(findings, _finding_filter(q))+ max_effort_rank = _EFFORT_RANK.get(q.max_effort or "", 99)targets: list[dict] = []history_only = 0@@ -184,55 +250,13 @@ async def _queue_targets(# top of a list ordered by how bad things are.if m is None:continue- # Naming a marker reaches it whatever its origin, as with the- # zero-impact dimensions above.- if history == "exclude" and q.biomarker is None and not split_by_origin(fs)[0]:+ if _is_history_only(fs, q, history):history_only += 1continue- nloc = m.nloc- score = m.score- primary = primary_finding(fs)- if primary is None:- # Every finding here is advisory, so no cause accuses this file and- # the general queue never reaches this: advisory is excluded from- # it. A caller who filtered to an advisory marker did reach it, and- # the marker they asked for is the honest lead for the row.- primary = max(- fs, key=lambda x: (_SEVERITY_ORDER.get(x.severity, 0), -(x.line_start or 0))- )- # Impact, and therefore the ranking, counts only findings still open:- # ``score`` on this row was computed from open findings, and a file- # whose findings were all dismissed is not work to rank near the top.- open_fs = [x for x in fs if (getattr(x, "status", None) or "open") == "open"]- total_impact = round(sum(x.health_impact for x in open_fs), 3)- effort_bucket = _effort_for_nloc(nloc)- if effort_rank[effort_bucket] > max_effort_rank:+ effort_bucket = _effort_for_nloc(m.nloc)+ if _EFFORT_RANK[effort_bucket] > max_effort_rank:continue- weight = effort_rank[effort_bucket]- ratio = round(total_impact / weight, 3)- targets.append(- {- "file_path": file_path,- "score": round(score, 2),- "nloc": nloc,- "module": m.module if (m and m.module) else None,- "is_test": bool(getattr(m, "is_test", False)),- "primary_biomarker": primary.biomarker_type,- "primary_severity": primary.severity,- "primary_reason": primary.reason,- "primary_function": primary.function_name,- "primary_line_start": primary.line_start,- "primary_line_end": primary.line_end,- "primary_suggestion": _suggestion_for(primary.biomarker_type),- "primary_finding_id": primary.id,- "total_impact": total_impact,- "finding_count": len(fs),- "open_finding_count": len(open_fs),- "biomarkers": sorted({x.biomarker_type for x in fs}),- "effort_bucket": effort_bucket,- "impact_per_effort": ratio,- }- )+ targets.append(_target_row(file_path, fs, m, effort_bucket))return targets, history_onlydiff --git a/tests/unit/persistence/test_actions_build.py b/tests/unit/persistence/test_actions_build.pyindex 5c846124f..a03bc1a90 100644--- a/tests/unit/persistence/test_actions_build.py+++ b/tests/unit/persistence/test_actions_build.py@@ -212,3 +212,36 @@ def test_a_store_that_fails_to_build_costs_only_itself() -> None:)assert facts.unavailable == {"doc_drift": "Not here."}assert facts.secrets+++def test_only_a_recent_authored_code_shape_regression_is_a_candidate() -> None:+ """Each condition alone keeps a commit-health row out of the recent findings."""+ from types import SimpleNamespace++ from repowise.core.analysis.actions.build import _recent_candidates++ week = _T - timedelta(days=7)+ kept = {"committed_at": _iso(timedelta(days=2)), "change_kind": "worsened",+ "severity": "critical", "attribution_basis": "changed_symbol",+ "file_path": "src/a.py", "biomarker_type": "complex_method"}+ dropped = {+ "no date": {"committed_at": None},+ "unparseable date": {"committed_at": "not a date"},+ "before the week": {"committed_at": _iso(timedelta(days=8))},+ "resolved": {"change_kind": "resolved"},+ "medium": {"severity": "medium"},+ "touched, not written": {"attribution_basis": "file_change"},+ "a test file": {"file_path": "tests/test_a.py"},+ "a history marker": {"biomarker_type": "change_entropy"},+ "a hidden marker": {"biomarker_type": "dry_violation"},+ }+ files = {"tests/test_a.py": SimpleNamespace(is_test=True)}++ rows = [kept, *({**kept, **change} for change in dropped.values())]+ out = _recent_candidates(rows, week=week, files=files)++ assert [row for row, _at in out] == [kept]+ assert _recent_candidates(rows, week=None, files=files) == []+ # The week's own instant is inside it.+ edge = {**kept, "committed_at": week.isoformat()}+ assert len(_recent_candidates([edge], week=week, files=files)) == 1\ No newline at end of filediff --git a/tests/unit/server/test_chat_tools_execution.py b/tests/unit/server/test_chat_tools_execution.pynew file mode 100644index 000000000..2e0d8c21d--- /dev/null+++ b/tests/unit/server/test_chat_tools_execution.py@@ -0,0 +1,144 @@+"""The chat adapter's own branches: lookups, failures and serialization."""++from __future__ import annotations++from dataclasses import dataclass+from types import SimpleNamespace++import pytest++import repowise.server.mcp_server as mcp_mod+from repowise.core.registry import ToolEntry+from repowise.server import chat_tools+++def _contract(entry: ToolEntry) -> chat_tools.ChatToolContract:+ return chat_tools.ChatToolContract(entry=entry, description="", parameters={})+++def one_tool(monkeypatch):+ """A catalog holding a single read-only tool with distinct metadata."""++ async def probe() -> dict:+ return {"ok": True}++ entry = ToolEntry(+ fn=probe,+ name="probe",+ artifact_type="risk",+ presentation="table",+ evidence_basis="measured",+ )+ monkeypatch.setattr(chat_tools, "get_tool_catalog", lambda _repo_path: [_contract(entry)])+ return entry+++def test_an_entry_without_a_fastmcp_contract_is_left_out(monkeypatch, caplog) -> None:+ entry = ToolEntry(fn=lambda: None, name="orphan")+ monkeypatch.setattr(chat_tools, "selected_tool_entries", lambda _repo_path: [entry])+ monkeypatch.setattr(chat_tools, "get_registered_tool", lambda _name: None)++ assert chat_tools.get_tool_catalog(None) == []+ assert "orphan" in caplog.text+++def test_artifact_metadata_comes_from_the_entry(one_tool) -> None:+ assert chat_tools.get_artifact_type("probe", None) == "risk"+ assert chat_tools.get_artifact_presentation("probe", None) == "table"+ assert chat_tools.get_artifact_evidence_basis("probe", None) == "measured"+++def test_a_tool_outside_the_catalog_gets_the_generic_metadata(one_tool) -> None:+ assert chat_tools.get_artifact_type("missing", None) == "generic"+ assert chat_tools.get_artifact_presentation("missing", None) == "generic"+ assert chat_tools.get_artifact_evidence_basis("missing", None) == "unknown"+++async def test_a_tool_outside_the_catalog_is_refused(one_tool) -> None:+ result = await chat_tools.execute_tool("missing", {})++ assert result["error_code"] == "tool_not_enabled"+ assert "missing" in result["error"]+++async def test_a_tool_in_the_catalog_runs(one_tool) -> None:+ assert await chat_tools.execute_tool("probe", {}) == {"ok": True}+++async def test_a_failing_tool_returns_its_error_instead_of_raising() -> None:+ async def broken() -> dict:+ raise ValueError("bad input")++ result = await chat_tools.execute_entry(ToolEntry(fn=broken, name="broken"), {})++ assert result == {"error": "ValueError: bad input", "error_code": "tool_failed"}+++async def test_a_tool_without_a_repo_parameter_never_gets_one(monkeypatch) -> None:+ """The request's repo only backstops tools that accept a ``repo`` argument."""+ seen: dict = {}++ async def no_repo(**kwargs) -> dict:+ seen.update(kwargs)+ return {}++ registry = SimpleNamespace(get_all_aliases=lambda: ["boot"])+ monkeypatch.setattr(mcp_mod, "_registry", registry)++ await chat_tools.execute_entry(ToolEntry(fn=no_repo, name="no_repo"), {}, repo="boot")++ assert seen == {}+++@dataclass+class _Point:+ x: int+ y: tuple+++class _Opaque:+ __slots__ = ()++ def __str__(self) -> str:+ return "opaque"+++async def test_a_tool_result_is_made_json_serializable() -> None:+ async def rich() -> dict:+ return {+ 1: (_Point(1, (2, 3)), None, True, 1.5),+ "nested": [{"k": _Opaque()}],+ }++ result = await chat_tools.execute_entry(ToolEntry(fn=rich, name="rich"), {})++ assert result == {+ "1": [{"x": 1, "y": [2, 3]}, None, True, 1.5],+ "nested": [{"k": "opaque"}],+ }+++def test_init_tool_state_publishes_and_keeps_omitted_values(monkeypatch) -> None:+ for name in ("_session_factory", "_fts", "_vector_store", "_decision_store", "_repo_path"):+ monkeypatch.setattr(mcp_mod, name, getattr(mcp_mod, name, None))+ mcp_mod._decision_store = "kept-store"+ mcp_mod._repo_path = "/kept"++ chat_tools.init_tool_state("factory", "fts", "vectors")++ assert (mcp_mod._session_factory, mcp_mod._fts, mcp_mod._vector_store) == (+ "factory",+ "fts",+ "vectors",+ )+ assert (mcp_mod._decision_store, mcp_mod._repo_path) == ("kept-store", "/kept")++ chat_tools.init_tool_state("f2", "fts2", "v2", decision_store="store", repo_path="/repo")++ assert (mcp_mod._decision_store, mcp_mod._repo_path) == ("store", "/repo")diff --git a/tests/unit/server/test_health_work_queue_controls.py b/tests/unit/server/test_health_work_queue_controls.pyindex 9b76a412c..d53b22d4a 100644--- a/tests/unit/server/test_health_work_queue_controls.py+++ b/tests/unit/server/test_health_work_queue_controls.py@@ -326,3 +326,88 @@ async def test_history_only_files_are_left_out_and_counted(client, session, tmp_# Naming the marker reaches it, as naming a zero-impact dimension does.named = await _queue(client, repo_id, "biomarker=change_entropy")assert [t["file_path"] for t in named["targets"]] == ["a.py"]+++async def test_a_row_carries_its_lead_impact_and_effort(client, session, tmp_path) -> None:+ """Every field of one row, pinned so a reshaping of the route cannot drift it."""+ repo_id = await _repo(+ client,+ session,+ tmp_path,+ [_metric("a.py", 4.123, nloc=100)],+ [+ _finding("a.py", severity="critical", impact=1.0),+ _finding("a.py", "god_class", severity="low", impact=0.5),+ ],+ )++ row = (await _queue(client, repo_id))["targets"][0]++ assert row["file_path"] == "a.py"+ assert row["score"] == 4.12+ assert row["nloc"] == 100+ assert row["module"] is None+ assert row["is_test"] is False+ assert row["primary_biomarker"] == "complex_method"+ assert row["primary_severity"] == "critical"+ assert row["primary_reason"] == "complex_method in a.py"+ assert (row["primary_line_start"], row["primary_line_end"]) == (12, 20)+ assert row["primary_suggestion"]+ assert row["primary_finding_id"]+ assert row["total_impact"] == 1.5+ assert (row["finding_count"], row["open_finding_count"]) == (2, 2)+ assert row["biomarkers"] == ["complex_method", "god_class"]+ # 100 lines is an M, weighted 2.+ assert (row["effort_bucket"], row["impact_per_effort"]) == ("M", 0.75)+++async def test_min_severity_is_a_floor(client, session, tmp_path) -> None:+ repo_id = await _repo(+ client,+ session,+ tmp_path,+ [_metric("a.py", 4.0), _metric("b.py", 5.0), _metric("c.py", 6.0)],+ [+ _finding("a.py", severity="low"),+ _finding("b.py", severity="high"),+ _finding("c.py", severity="critical"),+ ],+ )++ body = await _queue(client, repo_id, "min_severity=high")++ assert {t["file_path"] for t in body["targets"]} == {"b.py", "c.py"}+++async def test_max_effort_drops_larger_files(client, session, tmp_path) -> None:+ repo_id = await _repo(+ client,+ session,+ tmp_path,+ [+ _metric("s.py", 4.0, nloc=30),+ _metric("m.py", 4.0, nloc=100),+ _metric("x.py", 4.0, nloc=900),+ ],+ [_finding("s.py"), _finding("m.py"), _finding("x.py")],+ )++ small = await _queue(client, repo_id, "max_effort=S")+ every = await _queue(client, repo_id, "max_effort=XL")++ assert [t["file_path"] for t in small["targets"]] == ["s.py"]+ assert {t["effort_bucket"] for t in every["targets"]} == {"S", "M", "XL"}+++async def test_a_named_marker_counts_only_its_own_findings(client, session, tmp_path) -> None:+ repo_id = await _repo(+ client,+ session,+ tmp_path,+ [_metric("a.py", 4.0), _metric("b.py", 5.0)],+ [_finding("a.py"), _finding("a.py", "god_class"), _finding("b.py")],+ )++ body = await _queue(client, repo_id, "biomarker=god_class")++ assert [(t["file_path"], t["finding_count"]) for t in body["targets"]] == [("a.py", 1)]\ No newline at end of file