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): class test helpers and bundles the same on every ranked list (#3429)

Raghav Chamadiya10a414762c55
diff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.md
index c762daf82..77b35cb99 100644
--- a/docs/layers/CODE_HEALTH.md
+++ b/docs/layers/CODE_HEALTH.md
@@ -104,6 +104,11 @@ Left out, and counted with a reason:
- functions under CCN 80 and 200 lines whose file rarely changes and has no bug
fixes; they stay in the refactoring list
+Test helpers named for a module's tests (`bot.test-harness.ts`, `card-test-helpers.ts`,
+`test-support.mjs`) count as test support, and bundler output (`*.bundle.js`) as
+generated code, so neither leads Fix first, the refactoring list or the dashboard's
+worst-file lists, which say how many files they left out and why.
+
A function is dormant when its body opens with `if not FLAG: return ...` (or is one
`if FLAG:` block) and `FLAG` is a module-level `False`, `0` or `None` (`const FLAG =
false` in TypeScript or JavaScript) that nothing in the same file assigns again.
diff --git a/packages/core/src/repowise/core/analysis/health/queue/eligibility.py b/packages/core/src/repowise/core/analysis/health/queue/eligibility.py
index 10ed42a0b..98425980a 100644
--- a/packages/core/src/repowise/core/analysis/health/queue/eligibility.py
+++ b/packages/core/src/repowise/core/analysis/health/queue/eligibility.py
@@ -18,6 +18,7 @@ from dataclasses import dataclass
from typing import Any, Literal, Protocol, get_args
from repowise.core.code_origin import path_origin
+from repowise.core.test_paths import in_test_tree
from ...execution_roles import COLD_ROLES, EXECUTION_ROLES
from ..perf.actionability import EXPECTED_REASONS
@@ -139,6 +140,7 @@ SCOPE_EXCLUSIONS = frozenset(ORIGIN_EXCLUSION.values())
# Pure on the path, and the test check dominates a cold Fix first build.
_code_context = functools.lru_cache(maxsize=65536)(code_context)
+_in_test_tree = functools.lru_cache(maxsize=65536)(in_test_tree)
def path_verdict(
@@ -152,13 +154,15 @@ def path_verdict(
"""Whether a file's code is in scope.
The stored ``code_origin`` decides first: it read the file's head, so it
- knows a vendored library or a docs tutorial the path alone does not.
- Then the same classifier on the path (an index stored before the origin
- column, or a build file classed before build files were), then the path
- context. Code-shape work reads :func:`code_context`, where a CLI ships. A
- performance fix passes ``production``: its stored execution context is
- judged by the performance ladder. The stored ``is_test`` flag also marks a
- test. ``keep_tests`` keeps test code in scope (Fix first's ``scope="all"``).
+ knows a vendored library or a docs tutorial the path alone does not. A
+ stored ``production`` overrides a test verdict that rests on the name
+ alone (it saw pytest's config), never a test directory. With no stored
+ origin (an index from before the column) the same classifier runs on the
+ path; then the path context. Code-shape work reads :func:`code_context`,
+ where a CLI ships. A performance fix passes ``production``: its stored
+ execution context is judged by the performance ladder. The stored
+ ``is_test`` flag also marks a test. ``keep_tests`` keeps test code in
+ scope (Fix first's ``scope="all"``).
"""
reason = _path_reason(path, is_test, context, origin)
if reason == "test" and keep_tests:
@@ -171,11 +175,18 @@ def _path_reason(
) -> Reason | None:
if origin in ORIGIN_EXCLUSION:
return ORIGIN_EXCLUSION[origin]
+ if is_test:
+ return "test"
ctx = context or _code_context(path)
- if is_test or ctx == "test":
+ # A stored origin already saw pytest's config, which the path alone cannot:
+ # ``core/test_selection.py`` is production there and a test by name here.
+ if origin is None:
+ if ctx == "test":
+ return "test"
+ if reason := ORIGIN_EXCLUSION.get(path_origin(path)):
+ return reason
+ elif ctx == "test" and _in_test_tree(path):
return "test"
- if reason := ORIGIN_EXCLUSION.get(path_origin(path)):
- return reason
# ``unknown`` under a directory is docs, examples or demos at any depth:
# code that may not ship. A root-level file stays eligible.
if ctx == "tooling" or (ctx == "unknown" and "/" in path):
diff --git a/packages/core/src/repowise/core/code_origin.py b/packages/core/src/repowise/core/code_origin.py
index f55ca80cb..0ce8b84ca 100644
--- a/packages/core/src/repowise/core/code_origin.py
+++ b/packages/core/src/repowise/core/code_origin.py
@@ -218,7 +218,7 @@ def _code_suffixes() -> tuple[str, ...]:
def _generated_suffixes() -> tuple[str, ...]:
from .ingestion.languages.registry import REGISTRY
- return tuple(sorted(REGISTRY.generated_suffixes()))
+ return tuple(sorted({s.lower() for s in REGISTRY.generated_suffixes()}))
@cache
@@ -228,8 +228,19 @@ def _deps_suffixes() -> tuple[str, ...]:
return tuple(sorted(REGISTRY.extensions_for(_DEPS_LANGUAGES)))
+# A bundler's output (webpack's ``[name].bundle.js``, an esbuild ``a2ui.bundle.js``).
+# Kept out of the registry's generated suffixes, which ingestion skips: the file
+# stays in the graph and only leaves the fix lists.
+_BUNDLE_SUFFIXES = (".bundle.js", ".bundle.mjs", ".bundle.cjs")
+
+
def _is_generated_name(name: str) -> bool:
- return ".generated." in name or name.endswith(_generated_suffixes())
+ lowered = name.lower()
+ return (
+ ".generated." in lowered
+ or lowered.endswith(_generated_suffixes())
+ or lowered.endswith(_BUNDLE_SUFFIXES)
+ )
# --------------------------------------------------------------------------
diff --git a/packages/core/src/repowise/core/test_paths.py b/packages/core/src/repowise/core/test_paths.py
index 344549f61..d6ae01ed7 100644
--- a/packages/core/src/repowise/core/test_paths.py
+++ b/packages/core/src/repowise/core/test_paths.py
@@ -175,6 +175,16 @@ _SUPPORT_DIR_TOKENS_ANYWHERE: frozenset[str] = frozenset(
)
+# A source file whose name ends in "test" and a scaffolding word is the support
+# for some module's tests: ``bot.test-harness.ts``, ``card-test-helpers.ts``,
+# ``test-support.mjs``. Words split as directory segments do. Not ``spec``,
+# which ``spec_utils.py`` uses for OpenAPI tooling, and not ``utils``: React
+# and Preact publish ``test-utils.js`` as a package entry.
+_SUPPORT_NAME_WORDS: frozenset[str] = frozenset(
+ {"harness", "support", "helper", "helpers", "mock", "mocks", "fixture", "fixtures", "setup"}
+)
+
+
@dataclass(frozen=True, slots=True)
class _Conventions:
"""The registry-declared halves of the rules, resolved once."""
@@ -293,12 +303,25 @@ def _is_support_name(filename: str) -> bool:
return False
rules = _conventions()
lowered = filename.lower()
- if PurePosixPath(lowered).stem in rules.support_stems:
+ if PurePosixPath(lowered).stem in rules.support_stems or _is_support_compound(filename):
return True
camel_re = rules.support_camel_res.get(PurePosixPath(lowered).suffix)
return camel_re is not None and camel_re.search(PurePosixPath(filename).stem) is not None
+def _is_support_compound(filename: str) -> bool:
+ """``x.test-harness.ts``: a test name (``test_utils.py``) stays a test."""
+ lowered = PurePosixPath(filename.lower())
+ words = _words(lowered.stem)
+ return (
+ len(words) >= 2
+ and words[-2] == "test"
+ and words[-1] in _SUPPORT_NAME_WORDS
+ and lowered.suffix in _conventions().source_exts
+ and not _is_test_name(filename)
+ )
+
+
def _is_test_dir(
segments: list[str], original: list[str], filename: str, language: str | None
) -> bool:
@@ -487,6 +510,21 @@ def is_test_related_path(
return _classify(path, language, roots) != ""
+def in_test_tree(path: str, language: str | None = None) -> bool:
+ """Whether a directory alone makes *path* test material, whatever its name.
+
+ ``tests/x.py`` and ``src/__mocks__/api.ts`` are; ``core/test_selection.py``
+ is test-shaped only by name. ``.github/`` makes no test tree.
+ """
+ original, lowered = _parts(path)
+ segments = lowered[:-1]
+ if not segments or _REPO_METADATA_DIR in segments:
+ return False
+ return any(_is_support_anywhere_dir(seg) for seg in segments) or _is_test_dir(
+ segments, original[:-1], "", language
+ )
+
+
def is_unambiguous_test_path(path: str, language: str | None = None) -> bool:
"""Whether *path* is test material beyond a naming coincidence.
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 bf08cd137..874036318 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
@@ -16,6 +16,8 @@ from repowise.server.mcp_server.tool_health.serialize import _serialize_finding,
from repowise.server.mcp_server.tool_health.summary import _compute_kpis, _gap_analysis
from repowise.server.mcp_server.tool_health.targeted import ModeTotals
+_RANKED_FILE_LISTS = frozenset({"worst_files", "high_leverage_files", "test_worst_files"})
+
def build_dashboard(
data: HealthData, req: HealthRequest, pager: Pager
@@ -77,12 +79,15 @@ def _dashboard_blocks(data: HealthData, req: HealthRequest, pager: Pager) -> dic
# 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),
+ "kpis": lambda: _name_listed_worst(
+ _compute_kpis(
+ all_metrics,
+ hotspot_paths=data.hotspot_paths,
+ performance_findings=data.perf_findings_count,
+ coverage=data.perf_coverage,
+ unanalysed=len(pop.unanalysed_paths),
+ ),
+ data,
),
"distribution": lambda: health_distribution(all_metrics),
# Where the gap to the target concentrates: a short list of files.
@@ -97,6 +102,13 @@ def _dashboard_blocks(data: HealthData, req: HealthRequest, pager: Pager) -> dic
[_metric_row(data, m) for m in data.test_metric_rows], "test_worst_files"
),
"test_worst_files_total": len(data.test_metric_rows),
+ # Scripts, generated and vendored files: in the KPIs, in neither list.
+ # Said when a list is asked for by name, so the default stays its size.
+ **(
+ {"worst_files_hidden": data.worst_files_hidden}
+ if data.worst_files_hidden and req.only_set & _RANKED_FILE_LISTS
+ else {}
+ ),
"high_leverage_files": lambda: pager.bound(
_high_leverage_rows(data, gap()), "high_leverage_files"
),
@@ -158,6 +170,18 @@ def _finding_rows(data: HealthData, rows: list[Any]) -> list[dict[str, Any]]:
return [_serialize_finding(f, data.reference_repository, labels.get(f.id)) for f in rows]
+def _name_listed_worst(kpis: dict[str, Any], data: HealthData) -> dict[str, Any]:
+ """The worst file and worst test the KPIs name are the heads of the lists,
+ so a script left out of ``worst_files`` is not named worst either."""
+ if not kpis.get("file_count"):
+ return kpis
+ for prefix, rows in (("worst_performer", data.metric_rows), ("worst_test", data.test_metric_rows)):
+ head = rows[0] if rows else None
+ kpis[f"{prefix}_path"] = head.file_path if head else None
+ kpis[f"{prefix}_score"] = round(head.score, 2) if head else None
+ return kpis
+
+
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/loading.py b/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
index 3b40711b1..d23a330bc 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
@@ -8,6 +8,7 @@ projection gates the work as well as the payload.
from __future__ import annotations
+from collections import Counter
from dataclasses import asdict, dataclass, field
from typing import Any
@@ -18,6 +19,7 @@ from repowise.core.analysis.health.churn_complexity import churn_complexity_poin
from repowise.core.analysis.health.fix_first import FixFirstQueue
from repowise.core.analysis.health.grading import TARGET_SCORE
from repowise.core.analysis.health.perf.coverage import PerfCoverage, coverage_for_metrics
+from repowise.core.analysis.health.queue.eligibility import path_verdict
from repowise.core.analysis.health.ranking import deduction_by_path, sort_metrics_worst_first
from repowise.core.analysis.health.refactoring.serving import parse_query as parse_refactoring_query
from repowise.core.analysis.health.signals import file_signals
@@ -100,6 +102,8 @@ class HealthData:
# Dashboard only: the worst-first test files, ranked apart from
# ``metric_rows`` so a test never heads the production worklist.
test_metric_rows: list[HealthFileMetric] = field(default_factory=list)
+ #: Dashboard only: files in neither list, by the scope reason that left them out.
+ worst_files_hidden: dict[str, int] = field(default_factory=dict)
async def load_health_data(
@@ -121,12 +125,13 @@ async def load_health_data(
# input order, so every block must see the same order as ``worst_files``.
pop.all_metrics = sort_metrics_worst_first(pop.all_metrics, deductions)
targets = set(pop.effective_targets)
- # Targeted mode keeps what was named. The dashboard's ranked lists are
- # production files; tests get their own list.
+ # Targeted mode keeps what was named. The dashboard's ranked lists hold the
+ # files Fix first and refactoring keep in scope; tests get their own list.
+ reasons = {} if pop.scoped else _scope_reasons(pop.all_metrics)
metric_rows = (
[m for m in pop.all_metrics if m.file_path in targets]
if pop.scoped
- else [m for m in pop.all_metrics if not _is_test(m, test_paths)]
+ else [m for m in pop.all_metrics if reasons[m.file_path] is None]
)
data = HealthData(
repository=repository,
@@ -141,7 +146,10 @@ async def load_health_data(
by_leverage=[],
leads={},
test_metric_rows=(
- [] if pop.scoped else [m for m in pop.all_metrics if _is_test(m, test_paths)]
+ [] if pop.scoped else [m for m in pop.all_metrics if reasons[m.file_path] == "test"]
+ ),
+ worst_files_hidden=dict(
+ Counter(r for r in reasons.values() if r is not None and r != "test")
),
)
data.hotspot_paths = await _read_hotspot_paths(session, repository, pop, req)
@@ -174,9 +182,17 @@ async def load_health_data(
return data
-def _is_test(m: HealthFileMetric, test_paths: set[str]) -> bool:
- """Test material by the metric's own flag or the graph's, whichever is read."""
- return bool(m.is_test) or m.file_path in test_paths
+def _scope_reasons(metrics: list[HealthFileMetric]) -> dict[str, str | None]:
+ """Each file's scope exclusion (``test``, ``tooling``, ``generated``...), ``None``
+ in scope: Fix first's rule on Fix first's inputs, the row's flag and origin.
+
+ Ceiling: recomputed per process (about 0.3 s cold on 5k files, 1.2 s on 18k; 0.02 s
+ warm). Upgrade path: persist the verdict per file when the health pass finalizes.
+ """
+ return {
+ m.file_path: path_verdict(m.file_path, m.is_test, origin=m.code_origin).reason
+ for m in metrics
+ }
async def _read_test_paths(
diff --git a/tests/unit/health/test_queue_eligibility.py b/tests/unit/health/test_queue_eligibility.py
index c3967e8ba..73708b8a2 100644
--- a/tests/unit/health/test_queue_eligibility.py
+++ b/tests/unit/health/test_queue_eligibility.py
@@ -125,6 +125,20 @@ def test_path_verdict_reads_the_stored_origin_first_and_keeps_tests_on_ask() ->
assert path_verdict("run.py", None).eligible
+def test_a_stored_origin_overrules_a_test_shaped_name() -> None:
+ """The stored origin saw pytest's config: ``core/test_selection.py`` is
+ production there, though its name alone reads as a test."""
+ path = "packages/core/src/pkg/core/test_selection.py"
+ assert path_verdict(path, False, origin="production").eligible
+ # With no stored origin the path decides.
+ assert path_verdict(path, None).reason == "test"
+ assert path_verdict("src/bot.test-harness.ts", None).reason == "test"
+ # A test directory still wins over a stored production origin.
+ assert path_verdict("tests/x.py", False, origin="production").reason == "test"
+ assert path_verdict("src/__mocks__/api.ts", False, origin="production").reason == "test"
+ assert path_verdict("src/app.py", False, origin="test").reason == "test"
+
+
def test_unit_verdict_ladder_order() -> None:
big = {"ccn": 60, "nloc": 300}
assert unit_verdict(_Shaped(big, dead=True), "a.py", "f", complexity=True).reason == (
diff --git a/tests/unit/server/mcp/test_failing_types_surfaces.py b/tests/unit/server/mcp/test_failing_types_surfaces.py
index 7c6b3ffde..516c6fb66 100644
--- a/tests/unit/server/mcp/test_failing_types_surfaces.py
+++ b/tests/unit/server/mcp/test_failing_types_surfaces.py
@@ -119,6 +119,50 @@ async def test_test_files_ranked_apart_from_production(setup_mcp, failing_types_
assert result["kpis"]["worst_test_path"] == "tests/test_service.py"
+async def test_dashboard_lists_read_the_fix_first_scope(setup_mcp, session, failing_types_data):
+ """Scripts and bundles leave the production lists as they leave Fix first;
+ a test helper with no stored origin is ranked with the tests."""
+ from repowise.server.mcp_server import get_health
+
+ for path, origin, is_test in (
+ ("scripts/release-checks.ts", "tooling", False),
+ ("apps/kit/a2ui.bundle.js", "generated", False),
+ ("src/bot.test-harness.ts", None, False),
+ # A stored test flag on generated code: counted, not lost.
+ ("tests/gen/schema_pb2.py", "generated", True),
+ ):
+ session.add(
+ HealthFileMetric(
+ repository_id=failing_types_data,
+ file_path=path,
+ score=1.0,
+ max_ccn=40,
+ max_nesting=6,
+ nloc=900,
+ has_test_file=False,
+ module=path.split("/")[0],
+ is_test=is_test,
+ code_origin=origin,
+ )
+ )
+ await session.commit()
+
+ result = await get_health(
+ only=["worst_files", "test_worst_files", "high_leverage_files", "kpis"], limit=50
+ )
+ production = {r["file_path"] for r in (*result["worst_files"], *result["high_leverage_files"])}
+ assert production and not production & {
+ "scripts/release-checks.ts",
+ "apps/kit/a2ui.bundle.js",
+ "src/bot.test-harness.ts",
+ }
+ assert "src/bot.test-harness.ts" in {r["file_path"] for r in result["test_worst_files"]}
+ assert result["worst_files_hidden"] == {"tooling": 1, "generated": 2}
+ assert result["kpis"]["worst_performer_path"] == result["worst_files"][0]["file_path"]
+ assert result["kpis"]["worst_test_path"] == result["test_worst_files"][0]["file_path"]
+
+
@pytest.mark.asyncio
async def test_perf_text_has_no_n_plus_one_without_db(setup_mcp, failing_types_data):
from repowise.server.mcp_server import get_health
diff --git a/tests/unit/test_code_origin.py b/tests/unit/test_code_origin.py
index c7ce78048..58a33ae36 100644
--- a/tests/unit/test_code_origin.py
+++ b/tests/unit/test_code_origin.py
@@ -116,6 +116,12 @@ _OWN_HEADER = (
("buildSrc/src/main/kotlin/Versions.kt", "tooling"),
("build-logic/src/main/kotlin/Plugin.kt", "tooling"),
("packages/build-tools/src/index.ts", "production"), # a shipped package
+ # a bundler's output, by name; a module about bundles is hand-written
+ ("apps/kit/Resources/CanvasA2UI/a2ui.bundle.js", "generated"),
+ ("dist/main.bundle.mjs", "generated"),
+ ("dist/Main.Bundle.JS", "generated"),
+ ("src/Forms/Main.designer.cs", "generated"),
+ ("src/logging/diagnostic-support-bundle.ts", "production"),
],
)
def test_path_rules(path: str, expected: str) -> None:
diff --git a/tests/unit/test_test_paths.py b/tests/unit/test_test_paths.py
index 8d0592691..f32afe249 100644
--- a/tests/unit/test_test_paths.py
+++ b/tests/unit/test_test_paths.py
@@ -259,6 +259,25 @@ _CORPUS: tuple[tuple[str, str | None, str], ...] = (
("Foo.IntegrationTests/Sql/Db.cs", None, "test"),
("Foo.IntegrationTests/Helpers/Db.cs", None, "support"),
("src/Polly.Testing/ResiliencePipelineExtensions.cs", None, ""),
+ # A name ending in "test" and a scaffolding word is the support for some
+ # module's tests, wherever it sits (openclaw keeps them beside the module).
+ ("extensions/telegram/src/bot.create-telegram-bot.test-harness.ts", None, "support"),
+ ("scripts/test-projects.test-support.mjs", None, "support"),
+ ("extensions/feishu/src/card-test-helpers.ts", None, "support"),
+ ("src/media-understanding/runner.test-mocks.ts", None, "support"),
+ ("extensions/comfy/test-helpers.ts", None, "support"),
+ ("src/cron/isolated-agent.test-setup.ts", None, "support"),
+ # ``test-utils`` is a published entry in React and Preact; ``setup`` alone
+ # is a build script or an app's bootstrap.
+ ("src/test-utils.ts", None, ""),
+ ("packages/react-dom/test-utils.js", None, ""),
+ ("compat/test-utils.mjs", None, ""),
+ ("setup.py", None, ""),
+ ("src/setup.ts", None, ""),
+ ("tests/unit/cli/test_helpers.py", None, "test"), # a test name stays a test
+ ("src/foo.test-d.ts", None, "test"), # a type test, not a scaffolding word
+ ("docs/test-helpers.md", None, ""), # filename rules are source rules
+ ("src/logging/diagnostic-support-bundle.ts", None, ""),
)
@@ -292,6 +311,24 @@ def test_degenerate_paths_are_not_tests(path: str) -> None:
assert not is_test_related_path(path)
+ ("path", "expected"),
+ [
+ ("tests/x.py", True),
+ ("src/__mocks__/api.ts", True),
+ ("pkg/tests/helpers/db.py", True),
+ ("packages/core/src/pkg/core/test_selection.py", False),
+ ("src/bot.test-harness.ts", False),
+ (".github/workflows/tests/run.py", False),
+ ("x.py", False),
+ ],
+)
+def test_in_test_tree_reads_directories_only(path: str, expected: bool) -> None:
+ from repowise.core.test_paths import in_test_tree
+
+ assert in_test_tree(path) is expected
+
+
def test_windows_separators_match_posix() -> None:
"""Callers pass repo-relative paths from both a walk and a database row."""
assert is_test_path("tests\\unit\\test_engine.py")