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.

fix(health): fewer false positives from untested hotspots, SDK execute calls and re-raising handlers (#2688)

Swati Ahuja1deda2003c9d
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
index 0be7742dc..1b193742a 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/untested_hotspot.py
@@ -23,6 +23,7 @@ is an accusation.
from __future__ import annotations
+from ....ingestion.git_indexer.fix_shape import is_code_path
from ....test_paths import is_test_related_path
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -43,6 +44,14 @@ def _is_hotspot(ctx: FileContext) -> bool:
return bool((ctx.git_meta or {}).get("is_hotspot"))
+def _has_something_to_test(ctx: FileContext) -> bool:
+ """Code with at least one function. A config file (``vite.config.ts``, a
+ ``.d.ts``, a non-code language) or a file with no function (types,
+ constants, a settings module) has nothing a test could exercise, so the
+ absence of one is not a gap."""
+ return is_code_path(ctx.file_path) and bool(ctx.all_functions)
+
+
class UntestedHotspotDetector:
name = "untested_hotspot"
category = "test_coverage"
@@ -53,6 +62,8 @@ class UntestedHotspotDetector:
# as 0% covered and a churning one would be accused of it.
if is_test_related_path(ctx.file_path, ctx.language):
return []
+ if not _has_something_to_test(ctx):
+ return []
if not _is_hotspot(ctx):
return []
if ctx.dependents_count < _DEPENDENTS_THRESHOLD:
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
index 823ab10b9..d1e8d2ce7 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
@@ -67,9 +67,19 @@ def _eh_is_trivial_stmt(stmt: Node, language: str) -> bool:
return False
+def _eh_real_stmts(block: Node, language: str) -> list[Node]:
+ return [c for c in _eh_named(block) if not _eh_is_trivial_stmt(c, language)]
+
+
def _eh_body_is_swallowed(block: Node, language: str) -> bool:
- real = [c for c in _eh_named(block) if not _eh_is_trivial_stmt(c, language)]
- return len(real) == 0
+ return not _eh_real_stmts(block, language)
+
+
+def _eh_body_ends_in_raise(block: Node | None, language: str) -> bool:
+ """The handler's last real statement is a ``raise``: it cleans up or
+ translates, then propagates, so nothing is swallowed however broad it is."""
+ real = _eh_real_stmts(block, language) if block is not None else []
+ return bool(real) and real[-1].type == "raise_statement"
def _eh_except_catch_all_name(clause: Node) -> tuple[bool, str | None]:
@@ -234,7 +244,7 @@ def _collect_error_handling(
block = _eh_find_body_block(node)
if block is not None and _eh_body_is_swallowed(block, language):
hits.append(ErrorHandlingHit("swallowed_catch", node.start_point[0] + 1))
- if is_python and _eh_is_bare_except(node):
+ if is_python and _eh_is_bare_except(node) and not _eh_body_ends_in_raise(block, language):
# ``except:`` / ``except BaseException:`` also swallow
# KeyboardInterrupt & SystemExit; ``except Exception:`` cannot.
kind = "bare_except" if _eh_catches_base(node) else "broad_except"
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
index b91825118..cf36f4010 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
@@ -200,6 +200,14 @@ class BasePerfDialect:
is an *execution sink*, else ``None`` ("not an I/O round-trip")."""
return None
+ def shows_a_query(self, call: Node) -> bool:
+ """The call's own shape is db evidence (a SQL argument, a query chain).
+
+ Counted alongside a db import by the dialects whose DB verbs need
+ evidence; ``False`` for a dialect that recognises no such shape.
+ """
+ return False
+
def call_sink_kind(
self, call: Node, *, awaited: bool, io_names: dict[str, str], has_db_import: bool
) -> str | None:
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
index 41c7f11ad..fde2fea9a 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
@@ -28,20 +28,21 @@ HTTP_VERBS: frozenset[str] = frozenset(
# Python DBAPI / SQLAlchemy execution sinks, split into three strata so the
# ambiguous accessors can be gated harder than the unambiguous ones (see
# ``sink_kind``).
-PY_DB_UNAMBIGUOUS: frozenset[str] = frozenset(
- {
- "execute",
- "executemany",
- "scalars",
- "scalar",
- "scalar_one",
- "scalar_one_or_none",
- "fetchone",
- "fetchall",
- "fetchmany",
- }
+PY_DB_UNAMBIGUOUS: frozenset[str] = frozenset({"fetchone", "fetchall", "fetchmany"})
+# DB verbs other clients spell too: GitPython's ``repo.commit()``, an HTTP or
+# GraphQL SDK's ``request.execute()``, a builder's ``.scalar()``.
+PY_DB_NEEDS_EVIDENCE: frozenset[str] = frozenset(
+ {"commit", "execute", "executemany", "scalars", "scalar", "scalar_one", "scalar_one_or_none"}
+)
+# Without a db import, the call itself can still show it runs a query: a
+# PostgREST chain (``client.table("t")...execute()``) or a statement argument
+# (``session.execute(select(...))``, ``cur.execute("SELECT ...")``).
+_PY_QUERY_CHAIN_METHODS: frozenset[str] = frozenset({"table", "from_"})
+_PY_STATEMENT_BUILDERS: frozenset[str] = frozenset({"select", "insert", "update", "delete", "text"})
+_PY_SQL_LITERAL_RE = re.compile(
+ r"^[rRuUbBfF]*[\"']+\s*(?:SELECT|INSERT|UPDATE|DELETE|WITH|CREATE|DROP|ALTER|PRAGMA)\b",
+ re.IGNORECASE,
)
-PY_DB_COMMIT: frozenset[str] = frozenset({"commit"})
PY_DB_AMBIGUOUS: frozenset[str] = frozenset({"all", "first", "one", "one_or_none"})
PY_SUBPROC_METHODS: frozenset[str] = frozenset(
{"run", "call", "check_call", "check_output", "Popen"}
@@ -257,9 +258,8 @@ class PythonPerfDialect(BasePerfDialect):
# A real DB sink is always a method on a session/cursor/result
# object; a bare identifier call (the builtin ``all(...)``) is not.
return "db" if is_attribute else None
- if method in PY_DB_COMMIT:
- # ``.commit`` is a DB verb but GitPython exposes ``repo.commit()``.
- # Require db evidence in the file / on the receiver.
+ if method in PY_DB_NEEDS_EVIDENCE:
+ # A DB verb only with db evidence in the file / on the receiver.
return "db" if (is_attribute and db_evidence) else None
if method in PY_DB_AMBIGUOUS:
# ``.all`` / ``.first`` / ``.one`` collide with ordinary collection
@@ -296,7 +296,10 @@ class PythonPerfDialect(BasePerfDialect):
self, call: Node, *, awaited: bool, io_names: dict[str, str], has_db_import: bool
) -> str | None:
kind = super().call_sink_kind(
- call, awaited=awaited, io_names=io_names, has_db_import=has_db_import
+ call,
+ awaited=awaited,
+ io_names=io_names,
+ has_db_import=has_db_import or self.shows_a_query(call),
)
if kind == "db" and self._reads_a_registry(call, io_names):
return None
@@ -308,6 +311,40 @@ class PythonPerfDialect(BasePerfDialect):
return "db" if db and not self._key_used_earlier(call, orm_get[1]) else None
return kind
+ def shows_a_query(self, call: Node) -> bool:
+ """An evidence-gated DB verb whose call runs a query by its own shape,
+ whatever the file imports."""
+ if self.callee_method_name(call) not in PY_DB_NEEDS_EVIDENCE:
+ return False
+ fn = call.child_by_field_name("function")
+ if fn is None or fn.type != "attribute":
+ return False
+ receiver = fn.child_by_field_name("object")
+ return self._chains_a_query(receiver) or self._passes_a_statement(call)
+
+ def _chains_a_query(self, receiver: Node | None) -> bool:
+ """A PostgREST-style ``table(...)`` / ``from_(...)`` call on the receiver chain."""
+ while receiver is not None and receiver.type in ("call", "attribute"):
+ if receiver.type == "call":
+ if self.callee_method_name(receiver) in _PY_QUERY_CHAIN_METHODS:
+ return True
+ receiver = receiver.child_by_field_name("function")
+ else:
+ receiver = receiver.child_by_field_name("object")
+ return False
+
+ def _passes_a_statement(self, call: Node) -> bool:
+ """The first argument is a statement builder call or a SQL string literal."""
+ args = call.child_by_field_name("arguments")
+ first = next((c for c in args.children if c.is_named), None) if args is not None else None
+ if first is None:
+ return False
+ if first.type == "call":
+ return self.callee_method_name(first) in _PY_STATEMENT_BUILDERS
+ if first.type in ("string", "concatenated_string"):
+ return bool(_PY_SQL_LITERAL_RE.match((first.text or b"").decode("utf-8", "replace")))
+ return False
+
def _reads_a_registry(self, call: Node, io_names: dict[str, str]) -> bool:
"""``plugins.all()`` on an imported or module-global name reads a registry, not a
query result: a result is bound in the function that reads it."""
diff --git a/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py b/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py
index 48543b185..7e25e0157 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py
@@ -55,7 +55,7 @@ class _FileScan:
awaited=False,
is_attribute=self.dialect.callee_is_attribute(call),
io_names=self.io_names,
- has_db_import="db" in self.io_names.values(),
+ has_db_import="db" in self.io_names.values() or self.dialect.shows_a_query(call),
)
== "db"
)
diff --git a/packages/core/src/repowise/core/analysis/test_reachability.py b/packages/core/src/repowise/core/analysis/test_reachability.py
index bddbc0109..2a7493e3c 100644
--- a/packages/core/src/repowise/core/analysis/test_reachability.py
+++ b/packages/core/src/repowise/core/analysis/test_reachability.py
@@ -343,17 +343,32 @@ def files_with_paired_tests(
Otherwise the name is a collision - ``distill/test_engine.py`` tests the
distill engine, not ``health/engine.py`` - and does not pair.
- One hop only, and a file-level "some test imports this", never a quantity.
- The reach walk above stays call-graph-only; pairing is the looser question
- "does this file have a test", and against this repository's stored line
- coverage direct-import pairing finds 71.6% of covered files at 98.9%
- precision where names alone found 34.6% at 100%.
+ One hop, plus one more through a re-export barrel: a test importing a
+ package's ``index.ts`` or ``__init__.py`` pairs the files that barrel
+ imports, since that is how a package's tests reach its modules. A
+ file-level "some test imports this", never a quantity. The reach walk
+ above stays call-graph-only; pairing is the looser question "does this
+ file have a test", and against this repository's stored line coverage
+ direct-import pairing finds 71.6% of covered files at 98.9% precision
+ where names alone found 34.6% at 100%.
"""
+ barrel_deps: dict[str, set[str]] = {}
+
+ def imports_of(test: str) -> set[str]:
+ direct = _dependencies_of(graph, test)
+ through: set[str] = set()
+ for mid in direct:
+ if PurePosixPath(mid).name in BARREL_FILENAMES:
+ if mid not in barrel_deps:
+ barrel_deps[mid] = _dependencies_of(graph, mid)
+ through |= barrel_deps[mid]
+ return direct | through
+
paired: set[str] = set()
deps: dict[str, set[str]] = {}
if graph is not None:
for test in test_files:
- deps[test] = _dependencies_of(graph, test)
+ deps[test] = imports_of(test)
paired.update(deps[test])
paired &= set(paths)
@@ -364,13 +379,8 @@ def files_with_paired_tests(
def named_pair_holds(test: str, path: str) -> bool:
if graph is None:
return True
- imported = deps[test] if test in deps else _dependencies_of(graph, test)
- if not imported or path in imported:
- return True
- return any(
- PurePosixPath(mid).name in BARREL_FILENAMES and path in _dependencies_of(graph, mid)
- for mid in imported
- )
+ imported = deps[test] if test in deps else imports_of(test)
+ return not imported or path in imported
for path in paths:
if path in paired:
diff --git a/tests/unit/analysis/test_test_reachability.py b/tests/unit/analysis/test_test_reachability.py
index 9d8218de4..cd3d9da90 100644
--- a/tests/unit/analysis/test_test_reachability.py
+++ b/tests/unit/analysis/test_test_reachability.py
@@ -176,6 +176,20 @@ def test_a_test_importing_a_file_pairs_it_whatever_the_names() -> None:
assert "src/cli/main.py" in files_with_paired_tests(g, paths, {"tests/unit/cli/test_init.py"})
+def test_a_test_importing_a_barrel_pairs_what_it_re_exports() -> None:
+ g = _dep_graph(
+ ("test/router.test.ts", "src/index.ts", "imports"),
+ ("src/index.ts", "src/router.ts", "imports"),
+ ("src/router.ts", "src/util.ts", "imports"),
+ ("test/leaf.test.ts", "src/leaf.ts", "imports"),
+ ("src/leaf.ts", "src/deep.ts", "imports"),
+ )
+ paths = {"src/index.ts", "src/router.ts", "src/util.ts", "src/leaf.ts", "src/deep.ts"}
+ paired = files_with_paired_tests(g, paths, {"test/router.test.ts", "test/leaf.test.ts"})
+ # The barrel and what it re-exports; one hop only, never the whole closure.
+ assert paired == {"src/index.ts", "src/router.ts", "src/leaf.ts"}
+
+
def test_a_name_collision_the_graph_contradicts_does_not_pair() -> None:
# ``distill/test_engine.py`` tests the distill engine, not health's.
g = _dep_graph(("tests/distill/test_engine.py", "src/distill/engine.py", "imports"))
diff --git a/tests/unit/health/test_coverage_biomarkers.py b/tests/unit/health/test_coverage_biomarkers.py
index 6a6cad396..249493daf 100644
--- a/tests/unit/health/test_coverage_biomarkers.py
+++ b/tests/unit/health/test_coverage_biomarkers.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import pytest
+
from repowise.core.analysis.health.biomarkers import FileContext
from repowise.core.analysis.health.biomarkers.coverage_gap import CoverageGapDetector
from repowise.core.analysis.health.biomarkers.coverage_gradient import (
@@ -10,8 +12,15 @@ from repowise.core.analysis.health.biomarkers.coverage_gradient import (
from repowise.core.analysis.health.biomarkers.untested_hotspot import (
UntestedHotspotDetector,
)
+from repowise.core.analysis.health.complexity.models import FunctionComplexity
from repowise.core.analysis.health.coverage import parse_repowise_json
+_ONE_FUNCTION = (
+ FunctionComplexity(
+ name="run", start_line=1, end_line=5, ccn=1, max_nesting=0, cognitive=0, nloc=4
+ ),
+)
+
def _ctx(
*,
@@ -24,6 +33,7 @@ def _ctx(
branch_cov: float | None = None,
covered_lines: set[int] | None = None,
total_lines: int = 0,
+ functions: tuple[FunctionComplexity, ...] = _ONE_FUNCTION,
) -> FileContext:
return FileContext(
file_path=path,
@@ -38,6 +48,7 @@ def _ctx(
branch_coverage_pct=branch_cov,
covered_lines=covered_lines or set(),
total_coverable_lines=total_lines,
+ all_functions=functions,
)
@@ -104,6 +115,28 @@ def test_untested_hotspot_skips_when_paired_test_present() -> None:
assert UntestedHotspotDetector().detect(ctx) == []
+ ("path", "functions"),
+ [
+ # Types only: interfaces and aliases, no function to call.
+ ("src/types/router.types.ts", ()),
+ ("src/settings.py", ()),
+ # Build/tool configuration and declaration files, even with a function in them.
+ ("vite.config.ts", _ONE_FUNCTION),
+ ("src/api.d.ts", _ONE_FUNCTION),
+ ("config/app.yaml", ()),
+ ],
+)
+def test_untested_hotspot_skips_files_with_nothing_to_test(path, functions) -> None:
+ ctx = _ctx(
+ path=path,
+ git_meta={"is_hotspot": True},
+ dependents=10,
+ functions=functions,
+ )
+ assert UntestedHotspotDetector().detect(ctx) == []
+
+
# ---------------------------------------------------------------------------
# coverage_gap
# ---------------------------------------------------------------------------
diff --git a/tests/unit/health/test_ff_track_d_exceptions.py b/tests/unit/health/test_ff_track_d_exceptions.py
index 181a9ad05..6383897b2 100644
--- a/tests/unit/health/test_ff_track_d_exceptions.py
+++ b/tests/unit/health/test_ff_track_d_exceptions.py
@@ -77,6 +77,27 @@ def test_bare_and_baseexception_stay_bare(source: bytes) -> None:
assert "KeyboardInterrupt" in _REASONS["bare_except"]
+ "source",
+ [
+ b"try:\n x()\nexcept Exception:\n cleanup()\n raise\n",
+ b"try:\n x()\nexcept BaseException as exc:\n close()\n raise exc\n",
+ b"try:\n x()\nexcept:\n raise Wrapped() from None\n",
+ ],
+ ids=["cleanup-reraise", "base-reraise-bound", "bare-translate"],
+)
+def test_a_catch_all_that_ends_in_raise_is_not_flagged(source: bytes) -> None:
+ kinds = _kinds("python", source)
+ assert "broad_except" not in kinds
+ assert "bare_except" not in kinds
+
+
+def test_a_raise_that_is_not_last_still_flags() -> None:
+ # ``raise`` on one branch only; the fall-through swallows.
+ source = b"try:\n x()\nexcept Exception:\n if strict:\n raise\n log()\n"
+ assert "broad_except" in _kinds("python", source)
+
+
# --- R3: Go blank-identifier discard only in the trailing (error) slot ---
diff --git a/tests/unit/health/test_perf_io_in_loop.py b/tests/unit/health/test_perf_io_in_loop.py
index 0594a844b..7b9784fba 100644
--- a/tests/unit/health/test_perf_io_in_loop.py
+++ b/tests/unit/health/test_perf_io_in_loop.py
@@ -53,6 +53,31 @@ _CASES = [
[("io_in_loop", "db")],
"db execute in a data-dependent loop",
),
+ (
+ "python",
+ b"def f(client, queries):\n"
+ b" for q in queries:\n"
+ b" client.request(q).execute()\n"
+ b" client.scalar(q)\n",
+ [],
+ "execute / scalar with no db evidence is an SDK verb, not a DB sink",
+ ),
+ (
+ "python",
+ b"def f(cur, ids):\n"
+ b" for i in ids:\n"
+ b" cur.execute('SELECT * FROM t WHERE id = %s', (i,))\n",
+ [("io_in_loop", "db")],
+ "a SQL statement argument is db evidence without an import",
+ ),
+ (
+ "python",
+ b"def f(client, ids):\n"
+ b" for i in ids:\n"
+ b" client.table('t').select('*').eq('id', i).execute()\n",
+ [("io_in_loop", "db")],
+ "a PostgREST table chain is db evidence without an import",
+ ),
(
"python",
b"import subprocess\n"
diff --git a/tests/unit/health/test_perf_loop_facts_python.py b/tests/unit/health/test_perf_loop_facts_python.py
index 058d3f468..fd84a4db6 100644
--- a/tests/unit/health/test_perf_loop_facts_python.py
+++ b/tests/unit/health/test_perf_loop_facts_python.py
@@ -380,6 +380,7 @@ def test_magnitude_grows_through_a_result_attribute():
def test_a_retry_loop_inside_a_loop_over_rows_is_not_bounded():
facts = _loop(
+ b"from sqlalchemy import select\n"
b"async def f(session):\n"
b" rows = (await session.execute(q)).scalars().all()\n"
b" for row in rows:\n"
diff --git a/tests/unit/health/test_perf_phase7b.py b/tests/unit/health/test_perf_phase7b.py
index 5a7f46b71..0a405b5c8 100644
--- a/tests/unit/health/test_perf_phase7b.py
+++ b/tests/unit/health/test_perf_phase7b.py
@@ -63,6 +63,7 @@ def _facts(lang: str, src: str) -> dict[str | None, PerfFnFacts]:
_NESTED_IO_CASES = [
(
"python",
+ "from sqlalchemy.orm import Session\n"
"def f(session, groups):\n"
" for g in groups:\n"
" for r in g:\n"
@@ -72,6 +73,7 @@ _NESTED_IO_CASES = [
),
(
"python",
+ "from sqlalchemy.orm import Session\n"
"def f(session, repos):\n for r in repos:\n session.execute(r)\n",
[("io_in_loop", "db")],
"a single loop is io_in_loop only (no nesting)",
diff --git a/tests/unit/health/test_refactoring_opportunity.py b/tests/unit/health/test_refactoring_opportunity.py
index 7975bc22e..55e17f381 100644
--- a/tests/unit/health/test_refactoring_opportunity.py
+++ b/tests/unit/health/test_refactoring_opportunity.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import json
+
import pytest
from repowise.core.analysis.health.refactoring.models import RefactoringSuggestion
@@ -12,6 +14,10 @@ from repowise.core.analysis.health.refactoring.opportunity import (
opportunity_public_id,
opportunity_status,
)
+from repowise.core.persistence.crud.analysis.refactoring_opportunities import (
+ _details_payload,
+ _row_kwargs,
+)
def plan(kind: str, symbol: str = "handle", **kwargs) -> RefactoringSuggestion:
@@ -238,6 +244,36 @@ def test_mechanical_and_judgment_steps_are_counted_separately() -> None:
assert opportunity.step_count == 2
+def test_the_stored_step_count_is_the_stored_steps() -> None:
+ """The row's ``step_count`` and the details it ships with describe one list.
+
+ A reader pairs the row (counts) with the details (steps) by id; if either
+ half could disagree, a queue would promise steps its drawer never shows.
+ """
+ rows = [
+ plan("extract_method"),
+ plan("extract_method", "render"),
+ split(),
+ clone(intra=False, co_change=7),
+ clone(intra=True, co_change=0),
+ plan("extract_method", file_path="svc/billing.py"),
+ ]
+ for rank, opportunity in enumerate(compose_opportunities(rows)):
+ details = _details_payload(opportunity, validations={}, finding_ids={})
+ row = _row_kwargs(
+ opportunity,
+ rank_position=rank,
+ queue_position=rank,
+ status="open",
+ details=details,
+ analyzed_commit=None,
+ )
+ stored_steps = json.loads(row["details_json"])["steps"]
+ assert row["step_count"] == len(stored_steps) == len(opportunity.steps)
+ assert row["mechanical_steps"] + row["judgment_steps"] == row["step_count"]
+ assert opportunity.as_dict()["step_count"] == len(opportunity.as_dict()["steps"])
+
+
# --------------------------------------------------------------------------
# ranking
# --------------------------------------------------------------------------