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.

perf(health): serve get_health lists and the overview without whole-repo work (#3423)

Raghav Chamadiya588079edaa0a
diff --git a/packages/core/src/repowise/core/analysis/health/rows.py b/packages/core/src/repowise/core/analysis/health/rows.py
index dae3cd777..9040632b5 100644
--- a/packages/core/src/repowise/core/analysis/health/rows.py
+++ b/packages/core/src/repowise/core/analysis/health/rows.py
@@ -11,6 +11,11 @@ import json
from collections.abc import Iterable, Mapping
from typing import Any
+#: Types already found to be read by attribute, so a row of one skips both
+#: ``isinstance`` checks on every later read. Bounded by the distinct row
+#: classes a process reads.
+_ATTRIBUTE_TYPES: set[type] = set()
+
def field(row: Any, name: str, default: Any = None) -> Any:
"""Read one attribute from a dataclass, an ORM row, or a mapping."""
@@ -19,10 +24,11 @@ def field(row: Any, name: str, default: Any = None) -> Any:
kind = type(row)
if kind is dict:
return row.get(name, default)
- if isinstance(row, tuple):
+ if kind in _ATTRIBUTE_TYPES:
return getattr(row, name, default)
- if isinstance(row, Mapping):
+ if isinstance(row, Mapping) and not isinstance(row, tuple):
return row.get(name, default)
+ _ATTRIBUTE_TYPES.add(kind)
return getattr(row, name, default)
diff --git a/packages/core/src/repowise/core/analysis/health/worth.py b/packages/core/src/repowise/core/analysis/health/worth.py
index cad621b2c..ecfbe8cfe 100644
--- a/packages/core/src/repowise/core/analysis/health/worth.py
+++ b/packages/core/src/repowise/core/analysis/health/worth.py
@@ -12,8 +12,9 @@ can wait and never reads as the next thing to do.
from __future__ import annotations
+from bisect import bisect_right
from collections import defaultdict
-from collections.abc import Iterable, Mapping, Sequence
+from collections.abc import Callable, Iterable, Mapping, Sequence
from typing import Any, Literal
from .complexity.dispatch import DISPATCH_SHARE
@@ -114,7 +115,9 @@ def dormant(facts: Mapping[str, Any]) -> bool:
return bool(facts.get("gated_off"))
-def measure(findings: Iterable[Any]) -> dict[str, int]:
+def measure(
+ findings: Iterable[Any], details_of: Callable[[Any], Mapping[str, Any]] = detail_map
+) -> dict[str, int]:
"""A function's largest measured CCN, size and nesting across its findings.
``severe`` is set when any of them is critical or a brain method,
@@ -124,12 +127,13 @@ def measure(findings: Iterable[Any]) -> dict[str, int]:
``terminal_pct`` the largest stored terminal share (``complexity/terminal.py``),
``start`` / ``end`` span the function as its size findings place it, and
``deep_start`` / ``deep_end`` are its deepest nested block's lines.
+ ``details_of`` reads a finding's details, for a caller that parsed them once.
"""
shape: dict[str, int] = {}
for f in findings:
if field(f, "severity") == "critical" or field(f, "biomarker_type") == "brain_method":
shape["severe"] = 1
- details = detail_map(f)
+ details = details_of(f)
if details.get("deprecated"):
shape["deprecated"] = 1
if dormant(details):
@@ -156,10 +160,11 @@ def measure(findings: Iterable[Any]) -> dict[str, int]:
def magnitude(shape: Mapping[str, int]) -> int:
"""How far the measured CCN, size or nesting sits past its bar, 0 to 4."""
+ # Each bar tuple is ascending, so the bars a value reaches are a bisection.
return max(
- sum(shape.get("ccn", 0) >= c for c in SIZE_CCN),
- sum(shape.get("nloc", 0) >= c for c in SIZE_NLOC),
- sum(shape.get("max_nesting", 0) >= c for c in SIZE_NESTING),
+ bisect_right(SIZE_CCN, shape.get("ccn", 0)),
+ bisect_right(SIZE_NLOC, shape.get("nloc", 0)),
+ bisect_right(SIZE_NESTING, shape.get("max_nesting", 0)),
)
@@ -274,18 +279,26 @@ def finding_priorities(findings: Sequence[Any]) -> list[LowPriority | None]:
for f in findings:
if field(f, "function_name"):
by_function[(field(f, "file_path"), field(f, "function_name"))].append(f)
+ # Measured once per function, not once per finding on it, and each row's
+ # details parsed once: every list tiers a repository's whole open set.
+ parsed: dict[int, Mapping[str, Any]] = {}
+
+ def details_of(f: Any) -> Mapping[str, Any]:
+ found = parsed.get(id(f))
+ if found is None:
+ found = parsed[id(f)] = detail_map(f)
+ return found
+
+ shapes = {key: measure(rows, details_of) for key, rows in by_function.items()}
out: list[LowPriority | None] = []
for f in findings:
if id(f) in history:
out.append("history")
continue
key = (field(f, "file_path"), field(f, "function_name"))
+ shape = shapes[key] if key in shapes else measure([f], details_of)
out.append(
- low_priority(
- field(f, "biomarker_type"),
- measure(by_function.get(key, [f])),
- error_kind=detail_map(f).get("kind"),
- )
+ low_priority(field(f, "biomarker_type"), shape, error_kind=details_of(f).get("kind"))
)
return out
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py
index 957461ef0..79b608ed9 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py
@@ -71,6 +71,7 @@ from ...models import (
)
from ...read_snapshots import decode_or_none, read_snapshot, refresh_snapshot, snapshot_key
from ...sql import json_text
+from .plain_rows import plain_rows, row_shape
from .refactoring_recommendations import validation_inputs
#: Files read for plan-less finding items, by open code-shape deduction.
@@ -79,22 +80,12 @@ from .refactoring_recommendations import validation_inputs
FINDING_FILES = 100
-def _plain(result: Any) -> list[Any]:
- """Rows as named tuples: the builder reads each field many times, and a
- tuple attribute is several times cheaper than a SQL row's."""
- rows = result.all()
- if not rows:
- return []
- shape = namedtuple("Row", rows[0]._fields) # type: ignore[misc]
- return [shape(*r) for r in rows]
-
-
async def _metrics(session: AsyncSession, repo_id: str, paths: set[str] | None) -> list[Any]:
"""The named files' rows; ``None`` reads every file."""
if paths is not None and not paths:
return []
scoped = HealthFileMetric.file_path.in_(paths) if paths is not None else true()
- return _plain(
+ return plain_rows(
await session.execute(
select(
HealthFileMetric.file_path,
@@ -226,7 +217,7 @@ async def _findings(
return []
f = HealthFinding
history = history_biomarkers()
- return _plain(
+ return plain_rows(
await session.execute(
select(
f.file_path,
@@ -258,7 +249,7 @@ async def _findings(
async def _refactoring(session: AsyncSession, repo_id: str) -> list[Any]:
o = RefactoringOpportunity
- return _plain(
+ return plain_rows(
await session.execute(
select(
o.opportunity_id,
@@ -291,7 +282,7 @@ async def _performance(session: AsyncSession, repo_id: str) -> list[Any]:
# The reason is no column: read for ``expected`` rows only, in SQL, so
# the default queue counts each expected reason with no decode.
reason = json_text(p.details_json, "actionability_reason")
- return _plain(
+ return plain_rows(
await session.execute(
select(
p.opportunity_id,
@@ -334,7 +325,7 @@ def _sure_dead_code(repo_id: str) -> Any:
async def _dead_code(session: AsyncSession, repo_id: str) -> list[Any]:
"""Open dead-code findings sure enough to make what they cover ``unreachable``."""
d = DeadCodeFinding
- return _plain(
+ return plain_rows(
await session.execute(
select(
d.kind,
@@ -355,7 +346,7 @@ def _decoded(rows: list[Any]) -> list[Any]:
if not rows:
return rows
fields = [("details" if f == "details_json" else f) for f in rows[0]._fields]
- shape = namedtuple("Row", fields) # type: ignore[misc]
+ shape = row_shape(tuple(fields))
out = []
for r in rows:
values = list(r)
@@ -390,7 +381,7 @@ async def _plans(
wanted = [c for c in (named, extractions, helpers) if c is not None]
if not wanted:
return []
- return _plain(
+ return plain_rows(
await session.execute(
select(
s.public_id,
@@ -663,7 +654,7 @@ async def _write_changed(
async def _judged_findings(session: AsyncSession, repo_id: str) -> list[Any]:
"""Every finding the queue judges, with what judging reads and its stored judgement."""
f = HealthFinding
- return _plain(
+ return plain_rows(
await session.execute(
select(
f.id,
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/health.py b/packages/core/src/repowise/core/persistence/crud/analysis/health.py
index cf0eaadb5..e689a3eee 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/health.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/health.py
@@ -47,6 +47,7 @@ from ...models import (
_now_utc,
)
from .._shared import _BATCH_SIZE
+from .plain_rows import plain_rows
def _health_finding_row_kwargs(finding: Any, repository_id: str) -> dict:
@@ -715,6 +716,30 @@ def _rounded(value: float | None) -> float | None:
return round(value, 2) if value is not None else None
+async def _open_finding_totals_rows(session: AsyncSession, repository_id: str) -> list[Any]:
+ """The open findings :func:`get_health_findings` returns, narrowed to the
+ columns the summary totals read.
+
+ The summary sums and counts these; it never reads their tier, so it skips
+ the details column and the tiering pass over every open row. Impact order
+ is deliberate: per-file deductions are summed in the order the MCP
+ dashboard sums them, so both name the same worst file; ``id`` makes the
+ order total.
+ """
+ rows = await session.execute(
+ select(HealthFinding.file_path, HealthFinding.health_impact, HealthFinding.dimension)
+ .where(
+ HealthFinding.repository_id == repository_id,
+ HealthFinding.status == "open",
+ HealthFinding.biomarker_type.not_in(excluded_types()),
+ )
+ .order_by(HealthFinding.health_impact.desc(), HealthFinding.id)
+ )
+ return _filter_excluded_paths(
+ plain_rows(rows), await _health_exclude_spec(session, repository_id)
+ )
+
+
async def get_health_summary(
session: AsyncSession,
repository_id: str,
@@ -787,7 +812,7 @@ async def get_health_summary(
worst_performance_score = round(perf_worst.performance_score, 2)
if findings is None:
- findings = await get_health_findings(session, repository_id)
+ findings = await _open_finding_totals_rows(session, repository_id)
# Ranked with the same key as ``get_health_metrics``, off the findings this
# function already holds. Previously a plain ``min`` on the score, which
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/plain_rows.py b/packages/core/src/repowise/core/persistence/crud/analysis/plain_rows.py
new file mode 100644
index 000000000..5254e23fb
--- /dev/null
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/plain_rows.py
@@ -0,0 +1,27 @@
+"""Column-select results as named tuples, for folds over every row of a table.
+
+A tuple's attributes read in C, while a SQLAlchemy ``Row`` or entity resolves
+each one in Python: over a large repository's rows that difference is most of
+a read's cost.
+"""
+
+from __future__ import annotations
+
+from collections import namedtuple
+from functools import cache
+from typing import Any
+
+
+@cache
+def row_shape(names: tuple[str, ...]) -> type:
+ """One named-tuple class per column list, built once."""
+ return namedtuple("Row", names) # type: ignore[misc]
+
+
+def plain_rows(result: Any) -> list[Any]:
+ """Every row of a column select's *result* as a named tuple."""
+ make = row_shape(tuple(result.keys()))._make
+ return [make(row) for row in result]
+
+
+__all__ = ["plain_rows", "row_shape"]
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py b/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py
index 7b54f6838..bf08cd137 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+from functools import cache
from typing import Any
from repowise.core.analysis.health.aggregation import module_rollups as _module_rollups
@@ -24,22 +25,46 @@ def build_dashboard(
The rollup rides along so the overview page needs no second round-trip.
``by_leverage`` is built in ``loading``, before the leads.
"""
+ # A block is a thunk, built only when the projection keeps its key: a list
+ # call would otherwise pay for the whole dashboard and then drop it.
+ result: dict[str, Any] = {
+ key: value() if callable(value) else value
+ for key, value in _dashboard_blocks(data, req, pager).items()
+ if not callable(value) or _kept(req, key)
+ }
+ modules_total = result.get("modules_total", 0)
+ if not req.only:
+ _defer_to_secondary_rankings(result, req, data, modules_total)
+ if "churn_complexity" in req.include_set:
+ result["churn_complexity"] = pager.bound(data.churn_points, "churn_complexity")
+ if "accuracy" in req.include_set:
+ # Does the score rank the buggy files first? Scored over the full open
+ # set, not the capped head, which would be circular.
+ result["defect_accuracy"] = compute_defect_accuracy(
+ data.pop.all_metrics,
+ [_serialize_finding(f, data.reference_repository) for f in data.accuracy_rows],
+ # Same map ``all_metrics`` was ranked with, so it scores these ``worst_files``.
+ deductions=data.deductions,
+ )
+ return result, ModeTotals(metrics=None, trends=None, modules=modules_total)
+
+
+def _kept(req: HealthRequest, key: str) -> bool:
+ """Whether the projection keeps *key*; a ``*_total`` rides with its list."""
+ return req.wants(key) or (key.endswith("_total") and req.wants(key.removesuffix("_total")))
+
+
+def _dashboard_blocks(data: HealthData, req: HealthRequest, pager: Pager) -> dict[str, Any]:
+ """Every dashboard key in payload order: a value, or a thunk that builds it.
+
+ A thunk two blocks share is memoised, so it is built once or not at all.
+ """
pop = data.pop
all_metrics = pop.all_metrics
findings = data.findings
- all_modules = _module_rollups(all_metrics, data.deductions)
- gap = _gap_analysis(all_metrics)
- # KPIs keep test files unless ``scope`` narrows them. Tests score better
- # than production code, so narrowing lowers the number with no new defect
- # found: a caller should ask for it knowingly.
- kpis = _compute_kpis(
- all_metrics,
- hotspot_paths=data.hotspot_paths,
- performance_findings=data.perf_findings_count,
- coverage=data.perf_coverage,
- unanalysed=len(pop.unanalysed_paths),
- )
- result: dict[str, Any] = {
+ modules = cache(lambda: _module_rollups(all_metrics, data.deductions))
+ gap = cache(lambda: _gap_analysis(all_metrics))
+ return {
# The one lead; every block below ranks and describes.
**({"fix_first": _fix_first_block(data, req, pager)} if data.fix_first is not None else {}),
"mode": "dashboard",
@@ -49,60 +74,43 @@ def build_dashboard(
# Files in a language health has no dialect for: no score, so in no
# figure here. Only said when there are some.
**({"unanalysed_file_count": len(pop.unanalysed_paths)} if pop.unanalysed_paths else {}),
- "kpis": kpis,
- "distribution": health_distribution(all_metrics),
+ # KPIs keep test files unless ``scope`` narrows them. Tests score better
+ # than production code, so narrowing lowers the number with no new defect
+ # found: a caller should ask for it knowingly.
+ "kpis": lambda: _compute_kpis(
+ all_metrics,
+ hotspot_paths=data.hotspot_paths,
+ performance_findings=data.perf_findings_count,
+ coverage=data.perf_coverage,
+ unanalysed=len(pop.unanalysed_paths),
+ ),
+ "distribution": lambda: health_distribution(all_metrics),
# Where the gap to the target concentrates: a short list of files.
"gap_analysis": gap,
# Production files only: a test is never the worst file to work on.
# Test files rank in their own list, as findings do.
- "worst_files": pager.bound([_metric_row(data, m) for m in data.metric_rows], "worst_files"),
+ "worst_files": lambda: pager.bound(
+ [_metric_row(data, m) for m in data.metric_rows], "worst_files"
+ ),
"worst_files_total": len(data.metric_rows),
- "test_worst_files": pager.bound(
+ "test_worst_files": lambda: pager.bound(
[_metric_row(data, m) for m in data.test_metric_rows], "test_worst_files"
),
"test_worst_files_total": len(data.test_metric_rows),
- "high_leverage_files": pager.bound(_high_leverage_rows(data, gap), "high_leverage_files"),
- "high_leverage_files_total": len(data.by_leverage),
- "top_findings": pager.bound(
- [
- _serialize_finding(
- f, data.reference_repository, findings.lower_priority.get(f.id)
- )
- for f in findings.finding_rows
- ],
- "top_findings",
+ "high_leverage_files": lambda: pager.bound(
+ _high_leverage_rows(data, gap()), "high_leverage_files"
),
+ "high_leverage_files_total": len(data.by_leverage),
+ "top_findings": lambda: pager.bound(_finding_rows(data, findings.finding_rows), "top_findings"),
"top_findings_total": findings.findings_total,
**({"top_findings_counts": data.finding_counts} if data.finding_counts else {}),
# The test half of the same ranked set, kept out of the production list.
- "test_findings": pager.bound(
- [
- _serialize_finding(
- f, data.reference_repository, findings.lower_priority.get(f.id)
- )
- for f in findings.test_finding_rows
- ],
- "test_findings",
- ),
+ "test_findings": lambda: pager.bound(_finding_rows(data, findings.test_finding_rows), "test_findings"),
"test_findings_total": findings.test_findings_total,
# Worst-first, so the cap keeps the modules worth looking at.
- "modules": pager.bound(all_modules, "modules"),
- "modules_total": len(all_modules),
+ "modules": lambda: pager.bound(modules(), "modules"),
+ "modules_total": lambda: len(modules()),
}
- if not req.only:
- _defer_to_secondary_rankings(result, req, data, len(all_modules))
- if "churn_complexity" in req.include_set:
- result["churn_complexity"] = pager.bound(data.churn_points, "churn_complexity")
- if "accuracy" in req.include_set:
- # Does the score rank the buggy files first? Scored over the full open
- # set, not the capped head, which would be circular.
- result["defect_accuracy"] = compute_defect_accuracy(
- all_metrics,
- [_serialize_finding(f, data.reference_repository) for f in data.accuracy_rows],
- # Same map ``all_metrics`` was ranked with, so it scores these ``worst_files``.
- deductions=data.deductions,
- )
- return result, ModeTotals(metrics=None, trends=None, modules=len(all_modules))
def _fix_first_block(data: HealthData, req: HealthRequest, pager: Pager) -> dict[str, Any]:
@@ -145,6 +153,11 @@ def _fix_first_block(data: HealthData, req: HealthRequest, pager: Pager) -> dict
return block
+def _finding_rows(data: HealthData, rows: list[Any]) -> list[dict[str, Any]]:
+ labels = data.findings.lower_priority
+ return [_serialize_finding(f, data.reference_repository, labels.get(f.id)) for f in rows]
+
+
def _metric_row(data: HealthData, m: Any) -> dict[str, Any]:
return _serialize_metric(m, data.leads.get(m.file_path), is_test=m.file_path in data.test_paths)
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/findings.py b/packages/server/src/repowise/server/mcp_server/tool_health/findings.py
index ba3e05b79..3e2e9831a 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/findings.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/findings.py
@@ -8,6 +8,7 @@ from typing import Any
from sqlalchemy import select
from repowise.core.analysis.health.worth import LOW_PRIORITY_LABEL, finding_priorities
+from repowise.core.persistence.crud.analysis.plain_rows import plain_rows
from repowise.core.persistence.models import HealthFinding
from repowise.server.mcp_server.tool_health.population import Population
from repowise.server.mcp_server.tool_health.request import HealthRequest
@@ -56,6 +57,15 @@ class FindingSets:
test_findings_total: int = 0
#: Row id -> why the finding can wait, for each lower-priority row.
lower_priority: dict[Any, str] = field(default_factory=dict)
+ #: Set when the call prints no finding: ``emitted`` is then unranked and
+ #: this is the set :meth:`head` measures shapes over, on demand.
+ unranked_over: list[Any] | None = None
+
+ def head(self) -> Any | None:
+ """The first ranked finding, ranking now if the call skipped it."""
+ if self.unranked_over is None:
+ return next(iter(self.emitted), None)
+ return next(iter(_tiered(self.emitted, self.unranked_over)[0]), None)
def _tiered(rows: list[Any], measured_over: list[Any]) -> tuple[list[Any], dict[Any, str]]:
@@ -121,9 +131,10 @@ async def load_dashboard_findings(
)
# ``lead_rows`` stays unfiltered: the leads and performance KPI must not
# change because the caller asked to see one dimension.
- emitted, labels = _tiered(
- [r for r in lead_rows if _in_dimensions(r, req.ranked_dimensions)], lead_rows
- )
+ dimensioned = [r for r in lead_rows if _in_dimensions(r, req.ranked_dimensions)]
+ ranked = req.ranks_findings
+ # Tiering measures every open finding; a call that only counts them skips it.
+ emitted, labels = _tiered(dimensioned, lead_rows) if ranked else (dimensioned, {})
# Test findings get their own bucket instead of crowding the headline
# list. Split before the cap, so each list is the top ``limit`` of its
# own population.
@@ -132,8 +143,10 @@ async def load_dashboard_findings(
limit = req.limit
# Fetch the head by id: exactly ``limit`` rows whatever the exclusions,
# where an over-fetch margin on the ranked query could approach a full read.
- head_ids = [r.id for r in prod_emitted[:limit]] if req.wants_findings else []
- test_head_ids = [r.id for r in test_emitted[:limit]] if req.wants_test_findings else []
+ head_ids = [r.id for r in prod_emitted[:limit]] if ranked and req.wants_findings else []
+ test_head_ids = (
+ [r.id for r in test_emitted[:limit]] if ranked and req.wants_test_findings else []
+ )
by_id = await _read_findings_by_id(session, head_ids + test_head_ids)
return FindingSets(
# Re-imposed from the id lists; ``IN`` does not preserve order.
@@ -147,40 +160,42 @@ async def load_dashboard_findings(
test_finding_rows=[by_id[i] for i in test_head_ids if i in by_id],
test_findings_total=len(test_emitted),
lower_priority=labels,
+ unranked_over=None if ranked else lead_rows,
)
+_LITE_COLUMNS = (
+ HealthFinding.id,
+ HealthFinding.file_path,
+ HealthFinding.health_impact,
+ HealthFinding.biomarker_type,
+ HealthFinding.reason,
+ HealthFinding.dimension,
+ HealthFinding.severity,
+ HealthFinding.details_json,
+ HealthFinding.function_name,
+ HealthFinding.line_start,
+ HealthFinding.line_end,
+)
+
+
async def _read_lite_findings(session: Any, repository: Any, req: HealthRequest) -> list[Any]:
"""Narrow read over every open finding.
The columns ``_leads_by_file`` reads, plus ``dimension`` and ``id``, and
what the worth tier measures a function by (``function_name``, the span,
- ``severity``, ``details_json``). A SQLAlchemy ``Row`` exposes them as
- attributes, so the reduction and the exclude filter run against it
+ ``severity``, ``details_json``). The tuple each row is copied into exposes
+ them as attributes, so the reduction and the exclude filter run against it
unchanged. Ceiling: ``details_json`` is read for every open finding, for
the tier; storing the tier on the row would make this read narrow again.
"""
- lite_cols = [
- HealthFinding.id,
- HealthFinding.file_path,
- HealthFinding.health_impact,
- HealthFinding.biomarker_type,
- HealthFinding.reason,
- HealthFinding.dimension,
- HealthFinding.severity,
- HealthFinding.details_json,
- HealthFinding.function_name,
- HealthFinding.line_start,
- HealthFinding.line_end,
- ]
- return list(
- (
- await session.execute(
- select(*lite_cols)
- .where(*_open_findings(repository, req))
- .order_by(HealthFinding.health_impact.desc())
- )
- ).all()
+ # Plain tuples: the tier reads every row's fields several times.
+ return plain_rows(
+ await session.execute(
+ select(*_LITE_COLUMNS)
+ .where(*_open_findings(repository, req))
+ .order_by(HealthFinding.health_impact.desc())
+ )
)
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/plans.py b/packages/server/src/repowise/server/mcp_server/tool_health/plans.py
index b8b2bee10..d76410d65 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/plans.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/plans.py
@@ -44,12 +44,14 @@ def _render_plans(
_merge_ignored(result, data.refactoring_plans_ignored)
if req.wants("refactoring_plans"):
scoped = data.pop.scoped
+ eligible = bool(data.metric_rows if scoped else data.pop.all_metrics)
result["refactoring_plans_status"] = _refactoring_plans_status(
available_plans_total=total,
plans_emitted=len(rows),
scoped=scoped,
- has_eligible_metrics=bool(data.metric_rows if scoped else data.pop.all_metrics),
- finding=next(iter(data.findings.emitted), None),
+ has_eligible_metrics=eligible,
+ # Read only when no plan exists: ranking the open set costs a full pass.
+ finding=data.findings.head() if eligible and not total else None,
repo=req.repo,
)
# Prose suggestions for biomarkers without a structured plan ship once per
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/population.py b/packages/server/src/repowise/server/mcp_server/tool_health/population.py
index b11855548..02293fe1d 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/population.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/population.py
@@ -12,6 +12,7 @@ from repowise.core.analysis.health.counts import project as project_counts
from repowise.core.analysis.health.models import split_by_origin
from repowise.core.analysis.health.rows import scored_rows
from repowise.core.analysis.health.scope import parse_scope
+from repowise.core.persistence.crud.analysis.plain_rows import plain_rows
from repowise.core.persistence.models import HealthFileMetric
from repowise.server.mcp_server._helpers import _get_exclude_spec, filter_rows_by_attr
from repowise.server.mcp_server.tool_health.request import HealthRequest
@@ -67,11 +68,13 @@ async def load_population(
session: Any, repository: Any, repo_path: Any, req: HealthRequest
) -> Population:
"""Read the metric rows and narrow them the way every block will see them."""
- all_metrics_q = select(HealthFileMetric).where(
+ # Plain rows, not entities: every block only reads them, and an entity per
+ # file costs most of a call's fixed time on a large repository.
+ all_metrics_q = select(*HealthFileMetric.__table__.columns).where(
HealthFileMetric.repository_id == repository.id
)
exclude_spec = _get_exclude_spec(repo_path)
- indexed_rows = list((await session.execute(all_metrics_q)).scalars().all())
+ indexed_rows = plain_rows(await session.execute(all_metrics_q))
all_metrics = filter_rows_by_attr(indexed_rows, "file_path", exclude_spec)
# Lets an unresolved target report "excluded" rather than "no_such_path".
# Computed before ``scope`` narrows, or test files would count as excluded.
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/request.py b/packages/server/src/repowise/server/mcp_server/tool_health/request.py
index e3c18111f..855ee6a7b 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/request.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/request.py
@@ -203,6 +203,21 @@ class HealthRequest:
or ("refactoring" in self.include_set and self.wants("suggestion_legend"))
)
+ @property
+ def ranks_findings(self) -> bool:
+ """Whether the dashboard prints findings, so their tier order matters.
+
+ The bare dashboard turns its finding lists into calls, so there only
+ ``biomarkers`` and the suggestion legend print one. Every other block
+ reads totals, which need no order.
+ """
+ listed = bool(self.only) and (self.wants("top_findings") or self.wants("test_findings"))
+ biomarkers = "biomarkers" in self.include_set and (
+ self.wants("findings") or self.wants("test_findings")
+ )
+ legend = "refactoring" in self.include_set and self.wants("suggestion_legend")
+ return listed or biomarkers or legend
+
@property
def plans_requested(self) -> bool:
"""Structured refactoring plans (Extract Class, ...), loaded only when asked for.
diff --git a/packages/server/src/repowise/server/mcp_server/tool_overview/tool.py b/packages/server/src/repowise/server/mcp_server/tool_overview/tool.py
index ef0e21cfc..c9580155c 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_overview/tool.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_overview/tool.py
@@ -10,6 +10,7 @@ from sqlalchemy import select
from sqlalchemy.pool import StaticPool
from repowise.core.persistence.crud import code_file_rows
+from repowise.core.persistence.crud.analysis.plain_rows import plain_rows
from repowise.core.persistence.database import get_session
from repowise.core.persistence.models import GitMetadata
from repowise.core.registry import mcp_tool_registry as mcp
@@ -246,12 +247,24 @@ _READING_ORDER_HINT = (
)
+#: What git health and the ownership map read off each row. Every code file
+#: has one, so hydrating whole rows (their JSON history columns included) cost
+#: most of a warm overview on a large repository.
+_GIT_ROW_COLUMNS = (
+ GitMetadata.file_path,
+ GitMetadata.bus_factor,
+ GitMetadata.is_hotspot,
+ GitMetadata.commit_count_30d,
+ GitMetadata.commit_count_90d,
+ GitMetadata.primary_owner_email,
+ GitMetadata.primary_owner_name,
+)
+
+
async def _load_git_rows(session: Any, repository: Any, exclude_spec: Any) -> list[Any]:
"""Git metadata rows outside the exclude rules, the source of git health and ownership."""
- git_res = await session.execute(
- select(GitMetadata).where(code_file_rows(repository.id))
- )
- return filter_rows_by_attr(list(git_res.scalars().all()), "file_path", exclude_spec)
+ git_res = await session.execute(select(*_GIT_ROW_COLUMNS).where(code_file_rows(repository.id)))
+ return filter_rows_by_attr(plain_rows(git_res), "file_path", exclude_spec)
async def _requested_blocks(
diff --git a/tests/unit/health/test_rows_field.py b/tests/unit/health/test_rows_field.py
new file mode 100644
index 000000000..0a849c49a
--- /dev/null
+++ b/tests/unit/health/test_rows_field.py
@@ -0,0 +1,44 @@
+"""``rows.field`` reads every row shape the same way, however often it is asked."""
+
+from __future__ import annotations
+
+from collections import UserDict, namedtuple
+from dataclasses import dataclass
+
+from repowise.core.analysis.health.rows import field
+
+
+@dataclass
+class _Metric:
+ file_path: str
+ score: float
+
+
+class _Settings(dict):
+ """A ``dict`` subclass: read as a mapping, not by attribute."""
+
+ file_path = "attribute, not key"
+
+
+class _Pairs(tuple):
+ """A tuple subclass that also has a ``get``: read by attribute, as a tuple."""
+
+ def get(self, name, default=None): # pragma: no cover - must never be called
+ raise AssertionError("a tuple is read by attribute")
+
+
+def test_each_shape_reads_the_same_on_every_call() -> None:
+ Row = namedtuple("Row", "file_path score")
+ rows = [
+ {"file_path": "a.py", "score": 1.0},
+ UserDict({"file_path": "a.py", "score": 1.0}),
+ _Settings({"file_path": "a.py", "score": 1.0}),
+ Row("a.py", 1.0),
+ _Metric("a.py", 1.0),
+ ]
+ for _ in range(3): # the first read of a type decides how later ones go
+ for row in rows:
+ assert field(row, "file_path") == "a.py"
+ assert field(row, "score") == 1.0
+ assert field(row, "missing", "d") == "d"
+ assert field(_Pairs(), "missing", 0) == 0
diff --git a/tests/unit/health/test_worth.py b/tests/unit/health/test_worth.py
index ae0eb48e6..4710f766f 100644
--- a/tests/unit/health/test_worth.py
+++ b/tests/unit/health/test_worth.py
@@ -168,6 +168,29 @@ def test_findings_are_tiered_per_function_and_history_waits() -> None:
assert finding_priorities(rows) == ["history", "near_bar", None, None]
+def test_stored_rows_tier_as_the_findings_they_were_read_from() -> None:
+ """A server reads narrow tuples with the details still JSON text; the tier
+ must match the in-memory findings, each function measured over all its rows."""
+ import json
+ from collections import namedtuple
+
+ from repowise.core.analysis.health.worth import finding_priorities
+
+ big = _finding("src/a.py", "big", {"ccn": 47, "nloc": 191, "max_nesting": 5}, 0.4)
+ deep = _finding("src/a.py", "big", {"max_nesting": 5}, 0.9, marker="nested_complexity")
+ small = _finding("src/b.py", "small", {"ccn": 12, "nloc": 40}, 1.5)
+ loose = {**_finding("src/b.py", "", {"ccn": 90}, 0.2), "function_name": None}
+ in_memory = [small, deep, big, loose, small]
+ Row = namedtuple(
+ "Row", "file_path biomarker_type severity function_name line_start line_end details_json"
+ )
+ stored = [
+ Row(*(f[k] for k in Row._fields[:-1]), json.dumps(f["details"])) for f in in_memory
+ ]
+ assert finding_priorities(stored) == finding_priorities(in_memory)
+ assert finding_priorities(stored) == ["near_bar", None, None, None, "near_bar"]
+
+
@pytest.mark.parametrize(
("context", "facets", "expected"),
[
diff --git a/tests/unit/server/mcp/test_health_list_cost.py b/tests/unit/server/mcp/test_health_list_cost.py
new file mode 100644
index 000000000..b8a2b4d34
--- /dev/null
+++ b/tests/unit/server/mcp/test_health_list_cost.py
@@ -0,0 +1,88 @@
+"""A get_health call pays only for the blocks it returns.
+
+Tiering every open finding and building the whole dashboard cost a list call on
+a large repository most of its time, then were projected away. These pin that a
+call which prints no finding never tiers one, and that what it prints is
+unchanged.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+import repowise.server.mcp_server.tool_health.dashboard as dashboard_mod
+import repowise.server.mcp_server.tool_health.findings as findings_mod
+
+
+def _forbid(monkeypatch, module, name: str) -> None:
+ def boom(*_args, **_kwargs):
+ raise AssertionError(f"{name} ran for a call that does not print it")
+
+ monkeypatch.setattr(module, name, boom)
+
+
+ ("args", "kept"),
+ [
+ ({}, "kpis"),
+ ({"only": ["fix_first"]}, "fix_first"),
+ (
+ {"include": ["performance"], "only": ["performance_opportunities"]},
+ "performance_opportunities",
+ ),
+ ({"include": ["refactoring"], "only": ["refactoring_opportunities"]}, "mode"),
+ ],
+)
+async def test_a_call_that_prints_no_finding_never_tiers_one(
+ setup_mcp, health_data, monkeypatch, args, kept
+):
+ from repowise.server.mcp_server import get_health
+ from repowise.server.mcp_server.tool_health.request import HealthRequest
+
+ # The payload every call had when it always tiered: totals, secondary
+ # rankings and every kept block must match it.
+ with monkeypatch.context() as always:
+ always.setattr(HealthRequest, "ranks_findings", property(lambda _self: True))
+ expected = await get_health(**args)
+ _forbid(monkeypatch, findings_mod, "finding_priorities")
+ result = await get_health(**args)
+ assert kept in result
+ result["_meta"].pop("timing_ms", None)
+ expected["_meta"].pop("timing_ms", None)
+ assert result == expected
+
+
+async def test_a_list_call_builds_no_dashboard_block(setup_mcp, health_data, monkeypatch):
+ from repowise.server.mcp_server import get_health
+
+ for name in ("_compute_kpis", "_module_rollups", "health_distribution", "_gap_analysis"):
+ _forbid(monkeypatch, dashboard_mod, name)
+ result = await get_health(only=["top_findings"])
+ assert result["top_findings"]
+
+
+async def test_printed_findings_keep_their_tier(setup_mcp, health_data):
+ from repowise.server.mcp_server import get_health
+
+ listed = await get_health(only=["top_findings"], limit=50)
+ with_biomarkers = await get_health(include=["biomarkers"], only=["findings"], limit=50)
+ assert [f["id"] for f in listed["top_findings"]] == [
+ f["id"] for f in with_biomarkers["findings"]
+ ]
+
+
+async def test_no_plan_status_names_the_first_ranked_finding(setup_mcp, health_data):
+ """With no plan stored, the plans status points at the head of the ranked
+ list, which the plans call ranks on demand since it prints no finding."""
+ from repowise.server.mcp_server import get_health
+
+ plans = await get_health(include=["refactoring"], only=["refactoring_plans"])
+ status = plans["refactoring_plans_status"]
+ assert status["state"] == "indeterminate"
+ (head,) = (await get_health(only=["top_findings"], limit=1))["top_findings"]
+ symbol = status["next_action"]["arguments"]["symbol_id"]
+ assert symbol == f"{head['file_path']}:{head['line_start']}-{head['line_end']}"