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(refactoring): honour the finding registry and never lead with a test file (#2751)

Raghav Chamadiya31771d7de3ec
diff --git a/docs/layers/REFACTORING.md b/docs/layers/REFACTORING.md
index 10f074728..f83eebd2c 100644
--- a/docs/layers/REFACTORING.md
+++ b/docs/layers/REFACTORING.md
@@ -127,6 +127,12 @@ page cost the whole repository.
`performance_fix` plans are excluded by construction: the performance layer
composes, ranks and owns the lifecycle of its own opportunities.
+Plans whose `source_biomarker` the finding registry
+(`analysis/finding_registry.py`) withholds are left out too, as steps and as
+evidence, and so are they from the plan lists, the attention list and the
+`get_health` directive. A withheld finding never becomes a file's
+`lead_biomarker`. The plan rows stay stored and addressable by id.
+
**Split File ids across `init` and `update`.** A full index, an incremental
update and a re-score from stored git metadata now build the same symbol graph
for a file at one commit, so its `split_file` plan id is the same on every path:
@@ -151,6 +157,11 @@ tie.
| `canonical` | The published rank order verbatim, ties and all. What the old default produced. |
| `file_spread` | Asked for one row per file. An opportunity *is* one file's work, so the spread is satisfied by construction; the value resolves onto the diversified order, which is what it was reaching for. |
+Test files (tests and test support, by `test_paths.is_test_related_path`) rank
+after every production file, in both views and in the plan list, and never lead
+the `refactoring_directive`. When only test files have open work the directive
+is `clear` with reason `only_test_file_opportunities`.
+
Both older values keep working. The same parameter also selects the legacy
`refactoring_plans` list's view, where `diversified` resolves to that list's
historical `canonical` default.
diff --git a/packages/core/src/repowise/core/analysis/health/models.py b/packages/core/src/repowise/core/analysis/health/models.py
index 659c1dc96..363f3b9c6 100644
--- a/packages/core/src/repowise/core/analysis/health/models.py
+++ b/packages/core/src/repowise/core/analysis/health/models.py
@@ -139,9 +139,18 @@ def split_by_origin(findings: Iterable[Any]) -> tuple[list[Any], list[Any]]:
def primary_biomarker_by_file(findings: Iterable[Any]) -> dict[str, str]:
- """Each file's dominant cause, keyed by path. See :func:`primary_finding`."""
+ """Each file's dominant cause, keyed by path. See :func:`primary_finding`.
+
+ A type the finding registry withholds never leads: the refactoring surfaces
+ that read this would otherwise name a finding no other surface shows.
+ """
+ from ..finding_registry import excluded_types
+
+ withheld = excluded_types()
by_file: dict[str, list[Any]] = {}
for finding in findings:
+ if finding.biomarker_type in withheld:
+ continue
by_file.setdefault(finding.file_path, []).append(finding)
leads = {path: primary_finding(items) for path, items in by_file.items()}
return {path: lead.biomarker_type for path, lead in leads.items() if lead is not None}
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/opportunity.py b/packages/core/src/repowise/core/analysis/health/refactoring/opportunity.py
index 66772ad42..3336eb01c 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/opportunity.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/opportunity.py
@@ -45,6 +45,7 @@ from collections.abc import Iterable, Mapping, Sequence
from dataclasses import dataclass
from typing import Any
+from ...finding_registry import excluded_types
from ..rows import field
from .extract_helper import ACTIVE_CO_CHANGE
from .identity import REFACTORING_MODEL_VERSION, assign_public_ids, stable_id
@@ -472,6 +473,22 @@ def _compose_one(
)
+def _shown_by_file(
+ suggestions: Sequence[RefactoringSuggestion],
+) -> dict[str, list[tuple[RefactoringSuggestion, str]]]:
+ """Plans and their ids per file, less those whose cause the registry withholds.
+
+ Ids are assigned over the whole set, so a withheld plan cannot shift a
+ shown plan's id.
+ """
+ withheld = excluded_types()
+ by_file: dict[str, list[tuple[RefactoringSuggestion, str]]] = {}
+ for suggestion, plan_id in zip(suggestions, assign_public_ids(suggestions), strict=True):
+ if suggestion.source_biomarker not in withheld:
+ by_file.setdefault(suggestion.file_path, []).append((suggestion, plan_id))
+ return by_file
+
+
def compose_opportunities(
rows: Iterable[Any],
*,
@@ -494,6 +511,10 @@ def compose_opportunities(
attaches each step's ``finding_ids`` and credits a finding once when
several steps answer it; omitting them leaves both unknown and sums every
step's own impact.
+
+ A plan whose ``source_biomarker`` the finding registry withholds is left
+ out, as a step and as evidence; the plan row itself stays stored.
+ Ranking puts test files after production (:func:`.opportunity_rank.rank_sort_key`).
"""
leads = primary_biomarker_by_file or {}
findings_by_file: dict[str, list[Any]] | None = None
@@ -506,11 +527,7 @@ def compose_opportunities(
for suggestion in (rehydrate_suggestion(row) for row in rows)
if suggestion.refactoring_type not in EXCLUDED_TYPES
]
- plan_ids = assign_public_ids(suggestions)
- by_file: dict[str, list[tuple[RefactoringSuggestion, str]]] = {}
- for suggestion, plan_id in zip(suggestions, plan_ids, strict=True):
- by_file.setdefault(suggestion.file_path, []).append((suggestion, plan_id))
-
+ by_file = _shown_by_file(suggestions)
composed = [
opportunity
for file_path in sorted(by_file)
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/opportunity_rank.py b/packages/core/src/repowise/core/analysis/health/refactoring/opportunity_rank.py
index 49a79dc9f..bd762e008 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/opportunity_rank.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/opportunity_rank.py
@@ -33,6 +33,8 @@ from __future__ import annotations
from collections.abc import Sequence
from typing import Any
+from repowise.core.test_paths import is_test_related_path
+
from .models import CONFIDENCE_LEVELS, RefactoringSuggestion
from .recommendations import EFFORT_COST, detector_native_benefit, priority_score
from .recommendations import surface_confidence_risk as _surface_confidence_risk
@@ -133,15 +135,21 @@ def why_ranked(factors: dict[str, float], *, limit: int = 3) -> list[dict[str, A
return [{"factor": name, "value": value} for name, value in ordered[:limit]]
-def rank_sort_key(opportunity: Any) -> tuple[bool, float, str, str]:
- """A total order. The id tail is what stops ties floating between runs.
+def rank_sort_key(opportunity: Any) -> tuple[bool, bool, float, str, str]:
+ """A total order, production files first. The id tail is what stops ties
+ floating between runs.
+
+ A test file never leads: it has no users, and its helpers score badly by
+ design. Tests still rank among themselves, after every production file.
- Work that recovers health comes first. A detector-native benefit (a cycle's
- size, a split's group count) is not health, and on this repo it outranked
- every real extraction once those were credited proportionally; those
- opportunities stay listed, after the credited ones, in their own order.
+ Within each, work that recovers health comes first. A detector-native
+ benefit (a cycle's size, a split's group count) is not health, and on this
+ repo it outranked every real extraction once those were credited
+ proportionally; those opportunities stay listed, after the credited ones,
+ in their own order.
"""
return (
+ is_test_related_path(opportunity.file_path),
not has_credit(opportunity),
-opportunity.rank_score,
opportunity.file_path,
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
index 65e7818cf..e5833d879 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
@@ -33,7 +33,7 @@ from repowise.core.analysis.test_reachability import (
tests_reaching_by_tier,
)
from repowise.core.analysis.test_selection import expand_test_scopes
-from repowise.core.test_paths import is_test_support_path, paired_test_names
+from repowise.core.test_paths import is_test_related_path, is_test_support_path, paired_test_names
from .models import RefactoringSuggestion
@@ -858,9 +858,11 @@ def build_recommendations(
def canonical_order(recommendations: Sequence[Recommendation]) -> list[Recommendation]:
+ """Rank order, production files first so a test plan never leads the list."""
return sorted(
recommendations,
key=lambda recommendation: (
+ is_test_related_path(recommendation.suggestion.file_path),
-recommendation.rank_score,
recommendation.suggestion.refactoring_type,
recommendation.suggestion.file_path,
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/serving.py b/packages/core/src/repowise/core/analysis/health/refactoring/serving.py
index 9aeff2950..a299aa041 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/serving.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/serving.py
@@ -349,12 +349,19 @@ def directive_from_summary(row: Any | None) -> dict[str, Any]:
return dict(UNAVAILABLE)
payload = _loads_dict(field(row, "summary_json"))
lead = payload.get("lead")
+ total = int(payload.get("opportunities_total") or 0)
if not lead:
+ # The finalizer never leads with a test file, so opportunities
+ # without a lead are all in tests.
return {
"status": "clear",
- "reason": "no_open_opportunities",
- "detail": "No refactoring opportunity is open for this repository.",
- "opportunities_total": int(payload.get("opportunities_total") or 0),
+ "reason": "only_test_file_opportunities" if total else "no_open_opportunities",
+ "detail": (
+ "Every open refactoring opportunity is in a test file; none leads."
+ if total
+ else "No refactoring opportunity is open for this repository."
+ ),
+ "opportunities_total": total,
}
addresses = lead.get("addresses_primary_problem")
directive: dict[str, Any] = {
@@ -370,7 +377,7 @@ def directive_from_summary(row: Any | None) -> dict[str, Any]:
"confidence": lead.get("confidence"),
"recovers_health_points": lead.get("recoverable_health"),
"addresses_primary_problem": addresses,
- "opportunities_total": int(payload.get("opportunities_total") or 0),
+ "opportunities_total": total,
"next_action": {
"tool": "get_health",
"arguments": {"opportunity_id": lead.get("opportunity_id")},
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py
index f57bd8ac2..e0f18e2c3 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py
@@ -14,9 +14,10 @@ from __future__ import annotations
import json
from typing import Any
-from sqlalchemy import func, select
+from sqlalchemy import func, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
+from ....analysis.finding_registry import excluded_types
from ...models import RefactoringSuggestion, _new_uuid, _now_utc
from .._shared import _BATCH_SIZE, _finding_file_path
@@ -370,6 +371,18 @@ async def get_refactoring_suggestion(
return result.scalar_one_or_none()
+def shown_plan_predicate() -> Any:
+ """Plans whose source biomarker the finding registry does not withhold.
+
+ Plans are persisted whatever the registry says; every surface that lists
+ them filters here. A plan with no source biomarker (structural) is shown.
+ """
+ return or_(
+ RefactoringSuggestion.source_biomarker.is_(None),
+ RefactoringSuggestion.source_biomarker.not_in(excluded_types()),
+ )
+
+
def _suggestion_filters(
repository_id: str,
*,
@@ -381,6 +394,7 @@ def _suggestion_filters(
predicates: list[Any] = [
RefactoringSuggestion.repository_id == repository_id,
RefactoringSuggestion.status == status,
+ shown_plan_predicate(),
]
if refactoring_type is not None:
predicates.append(RefactoringSuggestion.refactoring_type == refactoring_type)
@@ -407,7 +421,8 @@ async def get_refactoring_suggestions(
) -> list[RefactoringSuggestion]:
"""Return refactoring suggestions, highest recovered impact first.
- *limit* / *offset* page in SQL. Both default to ``None``, which returns the
+ Plans from a registry-withheld biomarker are left out
+ (:func:`shown_plan_predicate`). *limit* / *offset* page in SQL. Both default to ``None``, which returns the
whole filtered set exactly as before, because the callers that still rank
and page in memory have not been rewired yet (R4).
"""
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py
index 810df5146..7a90dacd8 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py
@@ -21,6 +21,7 @@ from typing import TYPE_CHECKING, Any
from sqlalchemy import Select, func, select
from sqlalchemy.ext.asyncio import AsyncSession
+from ....test_paths import is_test_related_path
from ...models import (
HealthFinding,
RefactoringOpportunity,
@@ -87,24 +88,39 @@ def _diversified_order(opportunities: list[OpportunityModel]) -> list[int]:
Only opportunities that recover health are interleaved. The rest (rank
order already puts them last) follow in rank order, so a zero-credit cycle
never takes a head slot from real work.
+
+ Test files are round-robined apart and queue after every production file,
+ as they rank, so the lead is never a test.
"""
from ....analysis.health.refactoring.opportunity_rank import has_credit
- credited = sum(1 for item in opportunities if has_credit(item))
- groups: dict[tuple[str, str, str], list[int]] = {}
- for position, item in enumerate(opportunities[:credited]):
- parent = item.file_path.rsplit("/", 1)[0] if "/" in item.file_path else ""
- area = "/".join(parent.split("/")[:2])
- groups.setdefault(
- (item.lead_biomarker or "", item.lead_refactoring_type, area), []
- ).append(position)
- ordered_groups = sorted(groups.values(), key=lambda members: members[0])
order: list[int] = []
- for round_index in range(max((len(m) for m in ordered_groups), default=0)):
- for members in ordered_groups:
- if round_index < len(members):
- order.append(members[round_index])
- return order + list(range(credited, len(opportunities)))
+ for is_test in (False, True):
+ groups: dict[tuple[str, str, str], list[int]] = {}
+ uncredited: list[int] = []
+ for position, item in enumerate(opportunities):
+ if is_test_related_path(item.file_path) is not is_test:
+ continue
+ if not has_credit(item):
+ uncredited.append(position)
+ continue
+ parent = item.file_path.rsplit("/", 1)[0] if "/" in item.file_path else ""
+ area = "/".join(parent.split("/")[:2])
+ groups.setdefault(
+ (item.lead_biomarker or "", item.lead_refactoring_type, area), []
+ ).append(position)
+ order.extend(_round_robin(list(groups.values())))
+ order.extend(uncredited)
+ return order
+
+
+def _round_robin(groups: list[list[int]]) -> list[int]:
+ """One member per group per round, groups ordered by their best member."""
+ ordered = sorted(groups, key=lambda members: members[0])
+ order: list[int] = []
+ for round_index in range(max((len(m) for m in ordered), default=0)):
+ order.extend(m[round_index] for m in ordered if round_index < len(m))
+ return order
def _finding_ids_by_file(findings: list[HealthFinding]) -> dict[str, dict[str, list[str]]]:
@@ -376,8 +392,15 @@ async def finalize_refactoring_opportunities(
analyzed_commit=analyzed_commit,
)
- lead_rank = queue_order[0] if queue_order else None
- lead = opportunities[lead_rank] if lead_rank is not None else None
+ # Never a test file: with only tests left there is no lead to name.
+ lead = next(
+ (
+ opportunities[rank]
+ for rank in queue_order
+ if not is_test_related_path(opportunities[rank].file_path)
+ ),
+ None,
+ )
lead_details = None
if lead is not None:
lead_details = {
diff --git a/packages/core/src/repowise/core/persistence/crud/decision_health.py b/packages/core/src/repowise/core/persistence/crud/decision_health.py
index 0a7168005..e2c503bf4 100644
--- a/packages/core/src/repowise/core/persistence/crud/decision_health.py
+++ b/packages/core/src/repowise/core/persistence/crud/decision_health.py
@@ -10,6 +10,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.analysis.decisions.lifecycle import HISTORY_CURRENCIES, status_rank
from repowise.core.analysis.decisions.scope import binds_to_paths
+from repowise.core.test_paths import is_test_related_path
from ..models import DecisionRecord, GitMetadata
@@ -149,9 +150,10 @@ class _Lanes:
async def _ungoverned_hotspots(
session: AsyncSession, repository_id: str, governed_files: set[str]
) -> list[str]:
- """Hotspot files no accepted decision names, hottest first.
+ """Hotspot production files no accepted decision names, hottest first.
Same key as ``routers/overview.py``: score descending with NULLs last, then churn.
+ Test files are left out: churn in a test is not a design choice to record.
"""
hotspot_result = await session.execute(
select(
@@ -163,7 +165,11 @@ async def _ungoverned_hotspots(
GitMetadata.is_hotspot == True, # noqa: E712
)
)
- hotspot_rows = {row[0]: (row[1], row[2]) for row in hotspot_result.all()}
+ hotspot_rows = {
+ row[0]: (row[1], row[2])
+ for row in hotspot_result.all()
+ if not is_test_related_path(row[0])
+ }
def _hotspot_rank(file_path: str) -> tuple[bool, float, float, str]:
score, churn = hotspot_rows[file_path]
diff --git a/packages/server/src/repowise/server/services/attention.py b/packages/server/src/repowise/server/services/attention.py
index dd8a388c3..1429dff14 100644
--- a/packages/server/src/repowise/server/services/attention.py
+++ b/packages/server/src/repowise/server/services/attention.py
@@ -26,6 +26,7 @@ from repowise.core.analysis.attention import (
from repowise.core.analysis.dead_code.risk_factors import REVIEW_ONLY_KINDS
from repowise.core.analysis.finding_registry import excluded_types
from repowise.core.analysis.health.scoring import HISTORY_CATEGORY, biomarker_category
+from repowise.core.persistence.crud.analysis.refactoring import shown_plan_predicate
from repowise.core.persistence.models import (
DeadCodeFinding,
DocDriftFinding,
@@ -277,15 +278,17 @@ async def _refactoring_items(session: AsyncSession, repo_id: str) -> tuple[list[
``medium``: a thing you could improve never outranks a thing that is
wrong.
"""
+ shown = (
+ RefactoringSuggestion.repository_id == repo_id,
+ RefactoringSuggestion.status == "open",
+ RefactoringSuggestion.file_path.not_in(_test_paths(repo_id)),
+ shown_plan_predicate(),
+ )
rows = (
(
await session.execute(
select(RefactoringSuggestion)
- .where(
- RefactoringSuggestion.repository_id == repo_id,
- RefactoringSuggestion.status == "open",
- RefactoringSuggestion.file_path.not_in(_test_paths(repo_id)),
- )
+ .where(*shown)
.order_by(RefactoringSuggestion.impact_delta.desc())
.limit(PER_SOURCE_CAP)
)
@@ -294,14 +297,7 @@ async def _refactoring_items(session: AsyncSession, repo_id: str) -> tuple[list[
.all()
)
total = (
- await session.scalar(
- select(func.count(RefactoringSuggestion.id)).where(
- RefactoringSuggestion.repository_id == repo_id,
- RefactoringSuggestion.status == "open",
- RefactoringSuggestion.file_path.not_in(_test_paths(repo_id)),
- )
- )
- or 0
+ await session.scalar(select(func.count(RefactoringSuggestion.id)).where(*shown)) or 0
)
items = [
{
diff --git a/packages/types/src/refactoring.ts b/packages/types/src/refactoring.ts
index 287bd6a72..e5a1a7d05 100644
--- a/packages/types/src/refactoring.ts
+++ b/packages/types/src/refactoring.ts
@@ -383,7 +383,12 @@ export interface RefactoringDirectiveLead {
*/
export type RefactoringDirective =
| RefactoringDirectiveAvailable
- | { status: "clear"; reason: "no_open_opportunities"; detail: string; opportunities_total: number }
+ | {
+ status: "clear";
+ reason: "no_open_opportunities" | "only_test_file_opportunities";
+ detail: string;
+ opportunities_total: number;
+ }
| RefactoringRollupUnavailable;
export interface RefactoringDirectiveAvailable {
diff --git a/tests/conftest.py b/tests/conftest.py
index ae260f49b..be2409b61 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -171,3 +171,11 @@ def sample_repo_path(repo_root: Path) -> Path:
def fixtures_dir(repo_root: Path) -> Path:
"""Path to the tests/fixtures/ directory."""
return repo_root / "tests" / "fixtures"
+
+
+def dry_violation_shown(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Show ``dry_violation`` for tests of the clone mechanics the registry hides."""
+ from repowise.core.analysis import finding_registry
+
+ monkeypatch.delitem(finding_registry.REGISTRY, "dry_violation")
diff --git a/tests/unit/health/test_recommendations.py b/tests/unit/health/test_recommendations.py
index e697a159d..12e2b4906 100644
--- a/tests/unit/health/test_recommendations.py
+++ b/tests/unit/health/test_recommendations.py
@@ -111,6 +111,16 @@ def test_default_order_is_deterministic() -> None:
assert forward == reverse == ["A", "B", "C"]
+def test_a_test_file_plan_never_leads_the_canonical_order() -> None:
+ plans = [
+ _plan("helper", file_path="tests/test_core.py", impact=9.0),
+ _plan("worker", impact=1.0),
+ ]
+ ranked = build_recommendations(plans)
+ assert [item.suggestion.target_symbol for item in ranked] == ["worker", "helper"]
+ assert ranked[1].rank_score > ranked[0].rank_score
+
+
def test_legacy_persisted_row_rehydrates_without_phase3_fields() -> None:
suggestion = rehydrate_suggestion(
{
diff --git a/tests/unit/health/test_refactoring_opportunity.py b/tests/unit/health/test_refactoring_opportunity.py
index ef248d5b8..9c1634c1a 100644
--- a/tests/unit/health/test_refactoring_opportunity.py
+++ b/tests/unit/health/test_refactoring_opportunity.py
@@ -6,6 +6,7 @@ import json
import pytest
+from repowise.core.analysis.health.models import primary_biomarker_by_file
from repowise.core.analysis.health.refactoring.models import RefactoringSuggestion
from repowise.core.analysis.health.refactoring.opportunity import (
STEP_ORDER,
@@ -16,6 +17,7 @@ from repowise.core.analysis.health.refactoring.opportunity import (
)
from repowise.core.persistence.crud.analysis.refactoring_opportunities import (
_details_payload,
+ _diversified_order,
_row_kwargs,
)
@@ -115,6 +117,7 @@ def test_only_cross_file_co_changed_clones_earn_a_step(
assert is_standalone_clone(clone(intra=intra, co_change=co_change)) is standalone
[email protected]("dry_violation_shown")
def test_demoted_clones_become_evidence_on_the_file_opportunity() -> None:
rows = [plan("extract_method"), clone(intra=True, co_change=0)]
opportunity = compose_opportunities(rows)[0]
@@ -123,10 +126,12 @@ def test_demoted_clones_become_evidence_on_the_file_opportunity() -> None:
assert opportunity.evidence[0].summary["is_intra_file"] is True
[email protected]("dry_violation_shown")
def test_a_file_with_only_demoted_clones_publishes_nothing() -> None:
assert compose_opportunities([clone(intra=True, co_change=0)]) == []
[email protected]("dry_violation_shown")
def test_a_high_signal_clone_is_a_step_not_evidence() -> None:
opportunity = compose_opportunities([clone(intra=False, co_change=7)])[0]
assert opportunity.step_count == 1
@@ -138,6 +143,7 @@ def test_a_high_signal_clone_is_a_step_not_evidence() -> None:
# --------------------------------------------------------------------------
[email protected]("dry_violation_shown")
def test_structural_steps_precede_extractions() -> None:
rows = [plan("extract_method"), clone(intra=False, co_change=5), split()]
steps = compose_opportunities(rows)[0].steps
@@ -215,6 +221,7 @@ def test_a_purely_structural_file_leads_with_its_structure() -> None:
# --------------------------------------------------------------------------
[email protected]("dry_violation_shown")
def test_blast_radius_is_charged_once_over_the_union() -> None:
shared = {"files": ["svc/billing.py"], "file_count": 1}
one = compose_opportunities([clone(intra=False, co_change=4, blast_radius=shared)])[0]
@@ -244,6 +251,7 @@ def test_mechanical_and_judgment_steps_are_counted_separately() -> None:
assert opportunity.step_count == 2
[email protected]("dry_violation_shown")
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.
@@ -279,6 +287,7 @@ def test_the_stored_step_count_is_the_stored_steps() -> None:
# --------------------------------------------------------------------------
[email protected]("dry_violation_shown")
def test_a_step_set_with_no_benefit_cannot_outrank_one_that_recovers_health() -> None:
barren = compose_opportunities([clone(intra=False, co_change=4)])[0]
real = compose_opportunities([plan("extract_method", file_path="svc/billing.py")])[0]
@@ -336,6 +345,7 @@ def test_the_id_changes_when_the_work_changes() -> None:
assert one.opportunity_id != two.opportunity_id
[email protected]("dry_violation_shown")
def test_evidence_does_not_rename_the_work() -> None:
bare = compose_opportunities([plan("extract_method")])[0]
witnessed = compose_opportunities([plan("extract_method"), clone(intra=True, co_change=0)])[0]
@@ -433,6 +443,7 @@ def block(name: str, **kwargs) -> RefactoringSuggestion:
)
[email protected]("dry_violation_shown")
def test_a_finding_several_steps_answer_is_recovered_once() -> None:
# Two cross-file, co-changed clone blocks both overlap one file-level
# dry_violation finding: the file recovers it once, at the larger claim.
@@ -470,3 +481,91 @@ def test_work_that_recovers_health_ranks_ahead_of_zero_credit_work():
)
ordered = sorted([cycle, small, split], key=rank_sort_key)
assert [o.opportunity_id for o in ordered] == ["o2", "o1", "o3"]
+
+
+# --------------------------------------------------------------------------
+# finding registry and test files
+# --------------------------------------------------------------------------
+
+
+def test_a_withheld_biomarker_is_neither_a_step_nor_evidence() -> None:
+ rows = [plan("extract_method"), clone(intra=False, co_change=7), clone(intra=True, co_change=0)]
+ (opportunity,) = compose_opportunities(rows)
+ assert [step.refactoring_type for step in opportunity.steps] == ["extract_method"]
+ assert opportunity.evidence == ()
+
+
+def test_a_file_whose_plans_are_all_withheld_publishes_nothing() -> None:
+ assert compose_opportunities([clone(intra=False, co_change=7)]) == []
+
+
+def test_a_withheld_plan_does_not_rename_the_shown_work() -> None:
+ shown = [plan("extract_method")]
+ elsewhere = clone(intra=False, co_change=7, file_path="svc/billing.py")
+ (alone,) = compose_opportunities(shown)
+ (beside,) = compose_opportunities([*shown, elsewhere])
+ assert beside.opportunity_id == alone.opportunity_id
+
+
+def test_a_withheld_finding_never_leads_a_file() -> None:
+ from types import SimpleNamespace
+
+ findings = [
+ SimpleNamespace(
+ file_path="svc/orders.py", biomarker_type="dry_violation", health_impact=9.0
+ ),
+ SimpleNamespace(
+ file_path="svc/orders.py", biomarker_type="complex_method", health_impact=2.0
+ ),
+ ]
+ assert primary_biomarker_by_file(findings) == {"svc/orders.py": "complex_method"}
+
+
+def test_many_withheld_clones_cannot_crowd_the_ranking() -> None:
+ clones = [
+ clone(intra=False, co_change=9, file_path=f"pkg{i % 4}/m{i}.py") for i in range(40)
+ ]
+ ranked = compose_opportunities([*clones, plan("extract_method")])
+ assert [item.file_path for item in ranked] == ["svc/orders.py"]
+
+
+def test_a_test_file_ranks_after_production_whatever_its_score() -> None:
+ rows = [
+ plan("extract_method", impact_delta=5.0, file_path="tests/test_orders.py"),
+ plan("extract_method", impact_delta=0.5),
+ ]
+ ranked = compose_opportunities(rows)
+ assert [item.file_path for item in ranked] == ["svc/orders.py", "tests/test_orders.py"]
+ assert ranked[1].rank_score > ranked[0].rank_score
+
+
+def _ranked(path: str, biomarker: str, kind: str = "extract_method"):
+ from types import SimpleNamespace
+
+ # Credited, so every row is interleaved (zero-credit work queues last).
+ return SimpleNamespace(
+ file_path=path,
+ lead_biomarker=biomarker,
+ lead_refactoring_type=kind,
+ recoverable_health=1.0,
+ )
+
+
+def test_one_cause_in_bulk_does_not_own_the_queue_head() -> None:
+ # Rank order: twenty clone opportunities in one area, then two other causes.
+ ranked = [_ranked(f"lib/dup/m{i}.py", "dry_violation", "extract_helper") for i in range(20)]
+ ranked += [_ranked("svc/a.py", "complex_method"), _ranked("api/b.py", "low_cohesion")]
+ head = [ranked[i].lead_biomarker for i in _diversified_order(ranked)[:3]]
+ assert sorted(head) == ["complex_method", "dry_violation", "low_cohesion"]
+
+
+def test_the_queue_puts_every_test_file_after_production() -> None:
+ # Two production files share a group; a test file would otherwise take the
+ # second slot in the first round.
+ ranked = [
+ _ranked("svc/a.py", "complex_method"),
+ _ranked("svc/b.py", "complex_method"),
+ _ranked("tests/test_a.py", "complex_method"),
+ ]
+ order = [ranked[i].file_path for i in _diversified_order(ranked)]
+ assert order == ["svc/a.py", "svc/b.py", "tests/test_a.py"]
diff --git a/tests/unit/health/test_refactoring_tree_corpus.py b/tests/unit/health/test_refactoring_tree_corpus.py
index c4b4c974d..70069ac33 100644
--- a/tests/unit/health/test_refactoring_tree_corpus.py
+++ b/tests/unit/health/test_refactoring_tree_corpus.py
@@ -23,6 +23,10 @@ from .refactoring_tree_fixture import (
GOLDEN = "golden_opportunities.json"
+# The golden pins composition mechanics, clone demotion included; the registry's
+# visibility rule is tested on its own in test_refactoring_opportunity.py.
+pytestmark = pytest.mark.usefixtures("dry_violation_shown")
+
def test_corpus_composition_matches_the_golden() -> None:
payload = {name: compose_tree(root) for name, root in archetype_roots()}
diff --git a/tests/unit/persistence/test_decision_health_ranking.py b/tests/unit/persistence/test_decision_health_ranking.py
index 5eecd9674..c76e25f84 100644
--- a/tests/unit/persistence/test_decision_health_ranking.py
+++ b/tests/unit/persistence/test_decision_health_ranking.py
@@ -175,6 +175,17 @@ async def test_a_governed_hotspot_is_still_excluded(async_session):
assert health["ungoverned_hotspots"] == ["src/ungoverned.py"]
+async def test_a_test_file_is_never_an_ungoverned_hotspot(async_session):
+ repo = await insert_repo(async_session)
+ await _add_hotspot(async_session, repo.id, "tests/test_hot.py", 0.99)
+ await _add_hotspot(async_session, repo.id, "src/conftest.py", 0.95)
+ await _add_hotspot(async_session, repo.id, "src/warm.py", 0.10)
+
+ health = await get_decision_health_summary(async_session, repo.id)
+
+ assert health["ungoverned_hotspots"] == ["src/warm.py"]
+
+
async def test_ranking_does_not_drop_or_duplicate_a_record(async_session):
"""The sort re-orders the lists; it must not change what is in them."""
repo = await insert_repo(async_session)
diff --git a/tests/unit/server/test_overview_attention.py b/tests/unit/server/test_overview_attention.py
index 99e5b4bd0..879410741 100644
--- a/tests/unit/server/test_overview_attention.py
+++ b/tests/unit/server/test_overview_attention.py
@@ -345,6 +345,46 @@ async def test_test_files_are_out_of_scope(client: AsyncClient, session_factory)
assert result["by_source"]["health_finding"] == 1
+async def test_a_plan_the_registry_withholds_is_not_attention(
+ client: AsyncClient, session_factory
+) -> None:
+ repo = await create_test_repo(client)
+ repo_id = repo["id"]
+ plan = {
+ "refactoring_type": "extract_method",
+ "target_symbol": "work",
+ "line_start": 1,
+ "line_end": 20,
+ "impact_delta": 2.0,
+ "effort_bucket": "S",
+ "confidence": "high",
+ }
+ async with get_session(session_factory) as session:
+ await crud.save_refactoring_suggestions(
+ session,
+ repo_id,
+ [
+ {**plan, "file_path": "src/shown.py", "source_biomarker": "complex_method"},
+ {
+ **plan,
+ "refactoring_type": "extract_helper",
+ "file_path": "src/hidden.py",
+ "source_biomarker": "dry_violation",
+ },
+ ],
+ )
+
+ async with get_session(session_factory) as session:
+ result = await build_attention(
+ session, repo_id, decision_health={}, knowledge_silos=[]
+ )
+
+ paths = [i["target_id"] for i in result["items"] if i["type"] == "refactoring"]
+ assert paths == ["src/shown.py"]
+ assert result["by_source"]["refactoring"] == 1
+
+
@pytest.mark.anyio
async def test_areas_roll_up_and_lead_with_the_worst_item(
client: AsyncClient, session_factory
diff --git a/tests/unit/server/test_refactoring.py b/tests/unit/server/test_refactoring.py
index 2168cc24e..91461b875 100644
--- a/tests/unit/server/test_refactoring.py
+++ b/tests/unit/server/test_refactoring.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import tempfile
from pathlib import Path
+import pytest
from httpx import AsyncClient
from sqlalchemy import event
@@ -14,6 +15,10 @@ from repowise.core.persistence import (
crud,
)
+# The seed covers every plan type, extract_helper included; the registry's
+# hiding of its biomarker is tested in test_refactoring_opportunities.py.
+pytestmark = pytest.mark.usefixtures("dry_violation_shown")
+
async def create_test_repo(client: AsyncClient) -> dict:
repo_dir = Path(tempfile.mkdtemp()) / "test-repo"
diff --git a/tests/unit/server/test_refactoring_opportunities.py b/tests/unit/server/test_refactoring_opportunities.py
index fafbe1be3..906d0247b 100644
--- a/tests/unit/server/test_refactoring_opportunities.py
+++ b/tests/unit/server/test_refactoring_opportunities.py
@@ -653,6 +653,7 @@ async def test_an_unknown_opportunity_id_says_which_kind_of_unknown(client, app)
@pytest.mark.asyncio
[email protected]("dry_violation_shown")
async def test_each_step_keeps_its_own_validation_profile(client, app):
"""Validation must be keyed by plan, not by position.
@@ -1076,3 +1077,82 @@ def test_the_diversified_queue_never_gives_a_zero_credit_group_a_head_slot():
assert order[:3] != [0, 1, 2] # still interleaves the credited work
assert sorted(order[:3]) == [0, 1, 2]
assert order[3:] == [3, 4]
+
+
+# ---------------------------------------------------------------------------
+# The finding registry and test files
+# ---------------------------------------------------------------------------
+
+
+def _clone_plan(path: str, symbol: str) -> dict[str, Any]:
+ """A cross-file, co-changed clone: a step when its biomarker is shown."""
+ return _plan(
+ path,
+ symbol,
+ refactoring_type="extract_helper",
+ evidence={"is_intra_file": False, "co_change_count": 9},
+ impact_delta=4.0,
+ source_biomarker="dry_violation",
+ )
+
+
+async def test_withheld_plans_reach_no_list_queue_or_lead(client, app):
+ repo_id = await _repo(client)
+ plans = [_clone_plan(f"dup{i}/m{i}.py", f"c{i}") for i in range(6)]
+ plans.append(_plan("svc/core.py", "work", impact_delta=0.5))
+ async with app.state.session_factory() as session:
+ await crud.save_refactoring_suggestions(session, repo_id, plans)
+ await crud.finalize_refactoring_opportunities(session, repo_id)
+ # Stored all the same: the registry decides visibility, not persistence.
+ assert len(await crud.get_refactoring_suggestions(session, repo_id)) == 1
+ assert await crud.count_refactoring_suggestions(session, repo_id) == 1
+ await session.commit()
+
+ queue = (await client.get(f"/api/repos/{repo_id}/refactoring/opportunities")).json()
+ assert [item["file_path"] for item in queue["items"]] == ["svc/core.py"]
+ targets = (await client.get(f"/api/repos/{repo_id}/refactoring/targets")).json()
+ assert [plan["file_path"] for plan in targets["plans"]] == ["svc/core.py"]
+ rollup = (await client.get(f"/api/repos/{repo_id}/refactoring/summary")).json()
+ assert rollup["directive"]["fix_first"] == "svc/core.py"
+
+
+async def test_a_test_file_is_never_the_lead_or_ahead_of_production(client, app):
+ repo_id = await _repo(client)
+ plans = [
+ _plan("tests/test_core.py", "helper", impact_delta=9.0),
+ _plan("svc/a.py", "a", impact_delta=0.5),
+ _plan("svc/b.py", "b", impact_delta=0.4),
+ ]
+ async with app.state.session_factory() as session:
+ await crud.save_refactoring_suggestions(session, repo_id, plans)
+ await crud.finalize_refactoring_opportunities(session, repo_id)
+ await session.commit()
+
+ for view in ("diversified", "canonical"):
+ items = (
+ await client.get(
+ f"/api/repos/{repo_id}/refactoring/opportunities",
+ params={"view": view, "scope": "all"},
+ )
+ ).json()["items"]
+ assert [item["file_path"] for item in items][-1] == "tests/test_core.py"
+ rollup = (await client.get(f"/api/repos/{repo_id}/refactoring/summary")).json()
+ assert rollup["directive"]["fix_first"] == "svc/a.py"
+
+
+async def test_the_directive_names_no_test_file_when_only_tests_have_work(client, app):
+ repo_id = await _repo(client)
+ async with app.state.session_factory() as session:
+ await crud.save_refactoring_suggestions(
+ session, repo_id, [_plan("tests/test_core.py", "helper")]
+ )
+ await crud.finalize_refactoring_opportunities(session, repo_id)
+ await session.commit()
+ rollup = (await client.get(f"/api/repos/{repo_id}/refactoring/summary")).json()
+ directive = rollup["directive"]
+ assert directive["status"] == "clear"
+ assert directive["reason"] == "only_test_file_opportunities"
+ assert directive["opportunities_total"] == 1