Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(health): order Fix first by the function's own change history (#3565)
Raghav Chamadiyadfbf8af336cd
diff --git a/docs/architecture/code-health.md b/docs/architecture/code-health.mdindex 51a29f680..2f49c6851 100644--- a/docs/architecture/code-health.md+++ b/docs/architecture/code-health.md@@ -701,7 +701,10 @@ more) and not both CCN 25 and nesting 5; a dominant dispatch; nesting that is onelse-if or ternary chain; long but CCN under 20 and nesting under 5; a singlecomplex condition or error site. From CCN 80, 400 lines or nesting 8 a function isworth doing regardless. The hot-file bonus orders items but never lifts one out of-`later`. Each item carries up to `MAX_TESTS` (5) tests from its validation profile.+`later`. Inside a band, items order by complexity removed times reach times the lead+function's history, `1 + ln(1 + n)` for the `n` commits that changed it (`git_function_blame`);+a repository with no function history, a file too large to blame, a unit led by no+function and a performance cause weigh a file in the top fifth by churn 1.25 instead. Each item carries up to `MAX_TESTS` (5) tests from its validation profile.The findings list uses the same rule: worth-doing findings lead and the rest carry`lower_priority`. Class-design findings and history-only findings are lowerdiff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.mdindex 8c3227976..1d1c256ac 100644--- a/docs/layers/CODE_HEALTH.md+++ b/docs/layers/CODE_HEALTH.md@@ -89,6 +89,9 @@ always reports it.work: a refactoring plan, one performance intervention, or the strongest code-shapefinding in a file with no plan. It gives what to change and where, why, the firstedit, the expected gain, the effort and the tests to run.+Among items of the same value, a function that many commits have changed comes+before an equally complex one that rarely changes, and its reason says how many+commits changed it.Left out, and counted with a reason:diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/build.py b/packages/core/src/repowise/core/analysis/health/fix_first/build.pyindex 61b7d4faa..1d468e721 100644--- a/packages/core/src/repowise/core/analysis/health/fix_first/build.py+++ b/packages/core/src/repowise/core/analysis/health/fix_first/build.py@@ -46,6 +46,13 @@ Row shapes (field names are the SQL columns):``start_line``, ``end_line``, ``confidence``, ``safe_to_delete``,``status``. A unit an open row of :data:`DEAD_CONFIDENCE` or more (or onesafe to delete) covers is ``unreachable``: the fix is to delete it.+``function_changes``+ ``git_function_blame`` ``mod_count`` by ``symbol_id`` (``path::name``):+ the commits that changed each function. ``None`` when the repository keeps+ no function history; a function missing from it changed in none.+``blame_skipped``+ Paths the blame pass skipped for size: their functions have no history,+ so the file's churn orders them."""from __future__ import annotations@@ -82,6 +89,7 @@ from repowise.core.analysis.health.queue.order import LEVEL_RANK, orderfrom repowise.core.analysis.health.queue.value import (MIN_CHURN_COMMITS,function_reach,+ history_weight,perf_confidence,perf_ready,perf_value,@@ -151,6 +159,8 @@ MAX_CONTEXT = 3DEFAULT_LIMIT = 10#: Class-level findings: a fix names member groups, which only a plan holds.CLASS_MARKERS = frozenset({"low_cohesion", "god_class"})+#: Refactorings that change one function, whose own history orders them.+_FUNCTION_LEADS = frozenset({"extract_method", "move_method"})@dataclass(frozen=True, slots=True)@@ -180,6 +190,12 @@ def _tail(symbol: str | None) -> str | None:return re.split(r"::|\.", symbol)[-1] if symbol else None+def symbol_key(path: str, symbol: str | None) -> str | None:+ """A function's ``git_function_blame`` key, ``path::name``; ``None`` for no symbol."""+ name = text.short_symbol(symbol)+ return f"{path}::{name}" if name else None++def _open(row: Any) -> bool:return (field(row, "status") or "open") == "open"@@ -276,8 +292,12 @@ class _Files:clones: Mapping[str, list[tuple[int, int]]] | None = None,extractions: Mapping[tuple[str, str], list[Any]] | None = None,dead: Mapping[str, list[DeadSpan]] | None = None,+ function_changes: Mapping[str, int] | None = None,+ blame_skipped: Iterable[str] = (),) -> None:self.by_path = {field(m, "file_path"): m for m in metrics}+ self.function_changes = function_changes+ self.blame_skipped = frozenset(blame_skipped)self.functions = functionsself.clones = clones or {}self.extractions = extractions or {}@@ -329,6 +349,17 @@ class _Files:def churning(self, path: str) -> bool:return self.commits(path) >= max(self.churn_cut, MIN_CHURN_COMMITS)+ def changes(self, path: str, symbol: str | None, *, function: bool) -> int | None:+ """Commits that changed the function ``symbol`` in ``path``, 0 with no+ stored row; ``None`` when the repository keeps no function history,+ the blame pass skipped the file for size, or the unit is led by no+ function (a class, a file, a duplicated span), so its file's churn+ stands in."""+ key = symbol_key(path, symbol) if function else None+ if self.function_changes is None or key is None or path in self.blame_skipped:+ return None+ return int(self.function_changes.get(key) or 0)+def evidence(self, path: str, symbol: str | None) -> bool | None:"""Whether history shows work on ``symbol`` pays: its file changesoften or has bug fixes, or the function itself is a hotspot.@@ -435,13 +466,17 @@ class _Files:fallback: str,dependents: int | None = None,cloned: bool = False,+ changes: int | None = None,) -> str:- """One sentence: the problem, then why it matters here (who imports- the file, how often it changes, how much of it tests run)."""+ """One sentence: the problem, then why it matters here (how often the+ function changed, who imports the file, how often it changes, how much+ of it tests run)."""deps = dependents if dependents is not None else self.dependents(path)head = measured or fallback.rstrip(". ")if cloned:head += f", {text.CLONED}"+ if changes is not None and changes >= MIN_CHURN_COMMITS:+ head += f"; {text.own_changes(changes)}"return f"{head}{text.exposure(deps, self.commits(path), self.coverage(path))}."def context(self, path: str) -> tuple[FixContext, ...]:@@ -524,6 +559,8 @@ class _ShapeRank:shape: dict[str, int]central: boolchurning: bool+ #: Commits that changed the lead function (``_Files.changes``).+ changes: int | Nonecloned: boollow: str | Nonegain: float@@ -545,13 +582,18 @@ class _ShapeRank:def tier(self) -> str:return tier(self.cold_value, "medium", False, self.low)[0]+ @property+ def history(self) -> float:+ return history_weight(self.changes, self.churning)+def facts(self) -> list[FixRankFact]:size = size_value(self.shape, self.central) if self.low is None else worth_size(self.shape)return [FixRankFact("health gain", f"{self.gain:.2f}"),FixRankFact("complexity removed", str(self.ccn_removed)),FixRankFact("problem size", str(size)),- *_reach_facts(self.dependents, self.central, self.churning, self.credited),+ *_reach_facts(self.dependents, self.central, self.credited),+ _history_fact(self.changes, self.churning),FixRankFact("duplicate inside", "yes" if self.cloned else "no"),]@@ -561,28 +603,32 @@ def _finish_shaped(rank: _ShapeRank, **unit: Any) -> _Unit:return _finish(value=rank.value,cold_value=rank.cold_value,- worth=worth(removed(rank.ccn_removed, rank.gain), rank.credited, rank.churning),+ worth=worth(removed(rank.ccn_removed, rank.gain), rank.credited, rank.history),low=rank.low,rank_inputs=rank.facts,**unit,)-def _reach_facts(- deps: int | None, central: bool, churning: bool, credited: int | None-) -> list[FixRankFact]:- """What reach and history add: importers lift the band when central,- churn only orders inside it. A function credited with only its share of- its file's importers says so."""+def _reach_facts(deps: int | None, central: bool, credited: int | None) -> list[FixRankFact]:+ """What reach adds: importers lift the band when central. A function+ credited with only its share of its file's importers says so."""share = f", {credited} counted for its share of the file" if credited != deps else ""return [FixRankFact("files that import it", f"{deps or 0}{' (top fifth)' if central else ''}{share}"),- FixRankFact("changes often", "yes" if churning else "no"),]+def _history_fact(changes: int | None, churning: bool) -> FixRankFact:+ """What history adds, which only orders inside the band: the function's+ own changes, or, with no function history, whether its file changes often."""+ if changes is None:+ return FixRankFact("changes often", "yes" if churning else "no")+ return FixRankFact("function changed in", text.plural(changes, "commit"))++def _size_facts(shape: Mapping[str, int]) -> list[FixFact]:size_text = text.size_line(shape)return [FixFact("size", size_text)] if size_text else []@@ -903,6 +949,9 @@ class _RefactorPlan:shape,central,files.churning(path),+ changes=files.changes(+ path, lead.get("target_symbol"), function=lead_type in _FUNCTION_LEADS+ ),cloned=function_size and files.cloned(path, shape),low=low_priority(marker, shape, function_size=function_size) or _above_bar(details),gain=gain,@@ -974,6 +1023,7 @@ def _refactor_fields(plan: _RefactorPlan) -> dict[str, Any]:fallback=text.problem(plan.marker, plan.sym),dependents=rank.dependents,cloned=rank.cloned,+ changes=rank.changes,),"facts": _refactor_facts(plan),"action": _action(f"{n_steps}, {mechanical_n} mechanical", fix_steps, plan.mechanical),@@ -1362,6 +1412,9 @@ def _finding_rank(finding: Any, files: _Files) -> _ShapeRank:shape,central,files.churning(path),+ changes=files.changes(+ path, field(finding, "function_name"), function=marker not in CLASS_MARKERS+ ),cloned=sized and files.cloned(path, shape),low=low_priority(marker, shape, error_kind=detail_map(finding).get("kind")),gain=_num(field(finding, "health_impact")),@@ -1390,6 +1443,7 @@ def _finding_unit(lead: Any, files: _Files, first: FixStep, validate: Validate |measured=text.measured(where, rank.shape),fallback=text.problem(marker, where),cloned=rank.cloned,+ changes=rank.changes,),"facts": tuple([@@ -1490,6 +1544,8 @@ def _prepare(plans: list[Any],dead_code: Rows,hot_cuts: tuple[float, float] | None,+ function_changes: Mapping[str, int] | None = None,+ blame_skipped: Iterable[str] = (),) -> tuple[dict[str, tuple[list[Any], list[Any]]], _Files]:"""Findings split into (code shape, history) by file, and the file facts."""split = {path: split_by_origin(rows) for path, rows in _shown_findings(findings).items()}@@ -1501,6 +1557,8 @@ def _prepare(_clone_spans(plans),_extractions(plans),dead_spans(dead_code),+ function_changes,+ blame_skipped,)return split, files@@ -1610,6 +1668,8 @@ def build_fix_first(symbol_lines: Mapping[str, int] | None = None,validate: Validate | None = None,judged: dict[str, Judgement] | None = None,+ function_changes: Mapping[str, int] | None = None,+ blame_skipped: Iterable[str] = (),) -> FixFirstQueue:"""One ranked queue of what to fix, from stored rows (shapes in the module docstring).@@ -1627,10 +1687,15 @@ def build_fix_first(without it such an item's Verify stays unknown. ``dead_code`` rows makethe units they cover ``unreachable``. ``judged`` is filled with every openrefactoring opportunity's judgement, by id, for the index to store.+ ``function_changes`` orders units inside a band by how often their+ function changed (module docstring); without it, and in a+ ``blame_skipped`` file, the file's churn does."""metrics = list(metrics)plans = list(plans)- split, files = _prepare(metrics, findings, plans, dead_code, hot_cuts)+ split, files = _prepare(+ metrics, findings, plans, dead_code, hot_cuts, function_changes, blame_skipped+ )gate = _Gate(files, keep_tests=scope == "all")units, planned_files = _refactor_units(refactoring, plans, gate, judged)units += _perf_units(performance, gate, symbol_lines)@@ -1781,4 +1846,5 @@ __all__ = ["hot_cut","hot_cut_offset","judge_findings",+ "symbol_key",]diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/model.py b/packages/core/src/repowise/core/analysis/health/fix_first/model.pyindex 7054ba2dd..6b9263de5 100644--- a/packages/core/src/repowise/core/analysis/health/fix_first/model.py+++ b/packages/core/src/repowise/core/analysis/health/fix_first/model.py@@ -15,7 +15,7 @@ from repowise.core.analysis.health.queue.order import DUE_TIERS, Tierfrom repowise.core.analysis.next_call import ActionCommand#: Bumped when ranking, eligibility or the item shape changes meaning.-FIX_FIRST_MODEL_VERSION = 1+FIX_FIRST_MODEL_VERSION = 2FixTier = TierFIX_TIERS: tuple[str, ...] = get_args(FixTier)diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/text.py b/packages/core/src/repowise/core/analysis/health/fix_first/text.pyindex e1676d97c..6466fcceb 100644--- a/packages/core/src/repowise/core/analysis/health/fix_first/text.py+++ b/packages/core/src/repowise/core/analysis/health/fix_first/text.py@@ -259,6 +259,12 @@ def scope_name(symbol: str | None, path: str) -> str | None:return short+def own_changes(commits: int) -> str:+ """``function changed in 14 commits``: blame's count, the same one+ :func:`history_fact` quotes for a hotspot."""+ return f"function changed in {plural(commits, 'commit')}"++def changes(commits: int, people: int | None) -> str:"""``changed 13 times in 90 days; 3 people have worked on it``."""out = f"changed {plural(commits, 'time')} in 90 days"@@ -375,6 +381,7 @@ __all__ = ["loop_size","marker_label","measured",+ "own_changes","plural","problem","scope_name",diff --git a/packages/core/src/repowise/core/analysis/health/queue/value.py b/packages/core/src/repowise/core/analysis/health/queue/value.pyindex e1070179f..303f4cfbd 100644--- a/packages/core/src/repowise/core/analysis/health/queue/value.py+++ b/packages/core/src/repowise/core/analysis/health/queue/value.py@@ -3,14 +3,15 @@The value band, 0 to :data:`VALUE_MAX`, is code shape plus reach: a file in thetop fifth by importers adds one step, which never lifts a unit into a highertier (:func:`tier` reads the value without it). Inside a band, :func:`worth`-orders by complexity removed times reach; history (a file that changes often)+orders by complexity removed times reach times history (how often the function+changed, or, with no function history, whether its file changes often); historyonly re-orders units there."""from __future__ import annotationsfrom collections.abc import Mapping-from math import ceil, log2+from math import ceil, log1p, log2from typing import Anyfrom ...execution_roles import COLD_ROLES, LEADING_ROLES@@ -33,7 +34,9 @@ GAIN_CCN = SIZE_CCN[0]#: A performance cause counts as a function at the worth bar (CCN 40), times#: the reach of the call sites one fix settles.PERF_CCN = SIZE_CCN[1]-#: On a file in the top fifth by churn. History re-orders inside a band only.+#: On a file in the top fifth by churn, where the repository keeps no+#: function history, and for a performance cause. History re-orders inside a+#: band only.CHURN_WEIGHT = 1.25#: One change in 90 days is not changing often. Where the top fifth starts#: below this (a shallow clone, a young repository), history tells nothing.@@ -85,12 +88,27 @@ def function_reach(dependents: int | None, function_nloc: int, file_nloc: int |return dependents if share >= HUB_SHARE else ceil(dependents * share)-def worth(removed_ccn: float, dependents: int | None, churning: bool) -> float:- """The order inside a value band: complexity removed times :func:`reach`,- :data:`CHURN_WEIGHT` more on a file that changes often. Unbounded on- purpose: the band saturates, so a CCN 963 loop and a CCN 150 wizard share- it, and only this tells them apart."""- return removed_ccn * reach(dependents) * (CHURN_WEIGHT if churning else 1.0)+def churn_weight(changes: int | None) -> float:+ """A function's history: 1 plus the log of the commits that changed it+ (distinct commits owning its lines in blame), so none weighs 1."""+ return 1 + log1p(max(changes or 0, 0))+++def history_weight(changes: int | None, churning: bool) -> float:+ """:func:`churn_weight` of a function's changes. ``None``, a repository+ that keeps no function history, weighs :data:`CHURN_WEIGHT` on a file+ that changes often instead."""+ if changes is None:+ return CHURN_WEIGHT if churning else 1.0+ return churn_weight(changes)+++def worth(removed_ccn: float, dependents: int | None, history: float) -> float:+ """The order inside a value band: complexity removed times :func:`reach`+ times ``history`` (:func:`history_weight`). Unbounded on purpose: the band+ saturates, so a CCN 963 loop and a CCN 150 wizard share it, and only this+ tells them apart."""+ return removed_ccn * reach(dependents) * history#: The band a production database or network call in a loop that grows with@@ -126,7 +144,7 @@ def perf_value(row: Any, facets: Mapping[str, Any]) -> int:def perf_worth(call_sites: int, churning: bool) -> float:"""A performance cause's :func:`worth`: :data:`PERF_CCN`, reaching as faras the call sites one fix settles."""- return worth(PERF_CCN, call_sites - 1, churning)+ return worth(PERF_CCN, call_sites - 1, history_weight(None, churning))def perf_confidence(facets: Mapping[str, Any]) -> str:@@ -167,8 +185,10 @@ __all__ = ["PERF_CCN","SIZE_SEVERE","VALUE_MAX",+ "churn_weight","function_reach","gain_value",+ "history_weight","perf_confidence","perf_ready","perf_value",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.pyindex a92cbaab4..b6d754578 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@@ -44,6 +44,7 @@ from repowise.core.analysis.health.fix_first.build import (hot_cut,hot_cut_offset,judge_findings,+ symbol_key,)from repowise.core.analysis.health.queue.counts import NOT_IN_QUEUE, Judgementfrom repowise.core.analysis.health.queue.eligibility import DEFAULT_QUEUE_STATES, MIN_WORTH@@ -57,9 +58,11 @@ from repowise.core.analysis.health.refactoring.recommendations import ()from repowise.core.analysis.health.rows import detail_mapfrom repowise.core.analysis.health.scoring import history_biomarkers+from repowise.core.ingestion.git_indexer.function_blame import _MIN_COMMITS_FOR_BLAMEfrom ...models import (DeadCodeFinding,+ GitFunctionBlame,GitMetadata,GraphMetric,GraphNode,@@ -145,6 +148,64 @@ async def _hot_cuts(session: AsyncSession, repo_id: str) -> tuple[float, float]:return hot_cut(churn[at]), hot_cut(deps[at])+async def _function_changes(+ session: AsyncSession, repo_id: str, findings: list[Any], refactoring: list[Any]+) -> tuple[dict[str, int] | None, set[str]]:+ """Commits that changed each function a unit can lead with (a finding's,+ a plan's lead step's), by ``symbol_id``, and the files among theirs the+ blame pass skipped for size; ``None`` when the repository keeps no+ function history at all."""+ b = GitFunctionBlame+ any_row = await session.execute(select(b.id).where(b.repository_id == repo_id).limit(1))+ if any_row.first() is None:+ return None, set()+ leads = [(f.file_path, symbol_key(f.file_path, f.function_name)) for f in findings] + [+ (r.file_path, symbol_key(r.file_path, r.details["steps"][0].get("target_symbol")))+ for r in refactoring+ if r.details and r.details.get("steps")+ ]+ keys = {key for _path, key in leads if key}+ if not keys:+ return {}, set()+ rows = await session.execute(+ select(b.symbol_id, b.mod_count).where(b.repository_id == repo_id, b.symbol_id.in_(keys))+ )+ changes = dict(rows.all())+ matched = {path for path, key in leads if key in changes}+ unmatched = {path for path, key in leads if key} - matched+ return changes, await _blame_skipped(session, repo_id, unmatched)+++async def _blame_skipped(session: AsyncSession, repo_id: str, paths: set[str]) -> set[str]:+ """Of ``paths``, the files the blame pass skipped for size: enough+ commits to pass its commit floor, yet no function in them has a row.++ Ceiling: a blame that failed, or one whose every line is ignored+ (``.git-blame-ignore-revs``), looks the same. Upgrade path: the git pass+ records the size skip on the file's row.+ """+ if not paths:+ return set()+ floor = await session.execute(+ select(GitMetadata.file_path).where(+ GitMetadata.repository_id == repo_id,+ GitMetadata.file_path.in_(paths),+ GitMetadata.commit_count_total >= _MIN_COMMITS_FOR_BLAME,+ )+ )+ candidates = set(floor.scalars().all())+ if not candidates:+ return set()+ b = GitFunctionBlame+ # Unindexed, but read only for the few files that pass the floor unmatched.+ blamed = await session.execute(+ select(b.file_path)+ .where(b.repository_id == repo_id, b.file_path.in_(candidates))+ .distinct()+ )+ return candidates - set(blamed.scalars().all())++async def _basis(session: AsyncSession, repo_id: str) -> dict[str, str | None]:row = (await session.execute(@@ -497,10 +558,17 @@ async def _stamp(session: AsyncSession, repo_id: str) -> tuple[Any, ...]:A rewrite, a triage change (``updated_at`` moves) or a deletion (thecount moves) changes it; git and graph rows are rewritten with the- health rows that read them.+ health rows that read them. Function history is counted apart: an+ update writes it after the health rows."""stamps: list[Any] = []- for model in (HealthFileMetric, HealthFinding, RefactoringOpportunity, PerformanceOpportunity):+ for model in (+ HealthFileMetric,+ HealthFinding,+ RefactoringOpportunity,+ PerformanceOpportunity,+ GitFunctionBlame,+ ):stamps.extend((await session.execute(@@ -788,6 +856,7 @@ async def _build(functions = {s["target_symbol"] for s in steps if s.get("target_symbol")}findings = await _findings(session, repository_id, full, planned, functions)paths = full | planned | {r.file_path for r in history_only}+ changes, skipped = await _function_changes(session, repository_id, findings, refactoring)return build_fix_first(metrics=await _metrics(session, repository_id, paths),findings=[*findings, *(r for r in history_only if r.file_path not in full | planned)],@@ -803,6 +872,8 @@ async def _build(symbol_lines=await _symbol_lines(session, repository_id, performance),validate=await _finding_validator(session, repository_id, findings) if verify else None,judged=judged,+ function_changes=changes,+ blame_skipped=skipped,)diff --git a/packages/core/src/repowise/core/persistence/read_snapshots.py b/packages/core/src/repowise/core/persistence/read_snapshots.pyindex 68605a753..d4e64c953 100644--- a/packages/core/src/repowise/core/persistence/read_snapshots.py+++ b/packages/core/src/repowise/core/persistence/read_snapshots.py@@ -59,7 +59,6 @@ UNRELATED_TABLES = frozenset("external_systems","generation_jobs","git_commit_health_deltas",- "git_function_blame","graph_node_membership","health_snapshots","kg_node_meta",diff --git a/tests/unit/health/test_queue_value_order.py b/tests/unit/health/test_queue_value_order.pyindex c596e1721..4bf325847 100644--- a/tests/unit/health/test_queue_value_order.py+++ b/tests/unit/health/test_queue_value_order.py@@ -9,7 +9,12 @@ where it does.from __future__ import annotations+import math++import pytest+from repowise.core.analysis.health.fix_first import build_fix_first+from repowise.core.analysis.health.queue.value import CHURN_WEIGHT, churn_weight, history_weightfrom tests.unit.health.fix_first_rows import AT, _perf# Central and churning from 10 importers and 10 commits in 90 days.@@ -120,3 +125,107 @@ def test_history_orders_within_a_band_and_never_lifts_one() -> None:assert {_key(i): _value(i) for i in calm.items} == basechurn = {f.factor: f.value for f in calm.items[0].why_ranked}["changes often"]assert churn == "no"+++# --- the function's own history -------------------------------------------------+++def test_churn_weight_is_one_plus_log_of_the_changes() -> None:+ assert churn_weight(None) == churn_weight(0) == churn_weight(-3) == 1.0+ assert churn_weight(14) == pytest.approx(1 + math.log1p(14))+ assert churn_weight(60) > churn_weight(14) > churn_weight(1) > 1.0+++def test_no_function_history_falls_back_to_the_file_weight() -> None:+ assert history_weight(None, churning=True) == CHURN_WEIGHT+ assert history_weight(None, churning=False) == 1.0+ # With function history the file's churn is not read.+ assert history_weight(0, churning=True) == 1.0+ assert history_weight(14, churning=False) == churn_weight(14)+++def _twins(changes: dict[str, int] | None):+ """Two equal functions (same band, CCN, size and importers) in two files."""+ metrics = [_metric("a/one.py", 3, 40), _metric("b/two.py", 3, 40)]+ findings = [_finding(p, fn, 0.5, ccn=60, nloc=200, max_nesting=5)+ for p, fn in (("a/one.py", "first"), ("b/two.py", "second"))]+ return build_fix_first(metrics=metrics, findings=findings, hot_cuts=_CUTS, limit=None,+ function_changes=changes).items+++def test_a_churned_function_outranks_an_equal_quiet_one_in_its_band() -> None:+ for churned, quiet in (("first", "second"), ("second", "first")):+ path = "a/one.py" if churned == "first" else "b/two.py"+ items = _twins({f"{path}::{churned}": 14})+ assert [i.target.symbol for i in items] == [churned, quiet]+ assert items[0].value == items[1].value+ lead, other = ({f.factor: f.value for f in i.why_ranked} for i in items)+ assert lead["function changed in"] == "14 commits"+ assert other["function changed in"] == "0 commits"+ assert float(lead["worth in band"]) > float(other["worth in band"])+ assert "; function changed in 14 commits;" in items[0].why+ assert "function changed" not in items[1].why+++def test_function_history_never_lifts_a_band() -> None:+ base = _queue().items+ # The wizard and the moderate parser changed far more than anything else.+ churned = [{**m, "commit_count_90d": 40} if m["file_path"] == "lib/parse.py" else m+ for m in METRICS]+ changes = {"cli/status.py::show_status": 500, "lib/parse.py::parse_block": 500}+ items = build_fix_first(metrics=churned, findings=FINDINGS, performance=PERFORMANCE,+ hot_cuts=_CUTS, limit=None, function_changes=changes).items+ values = {_key(i): _value(i) for i in items}+ assert {k: values[k] for k in (_key(i) for i in base)} == {_key(i): _value(i) for i in base}+ ranks = [int(_value(i)) for i in items if i.tier != "later"]+ assert ranks == sorted(ranks, reverse=True)+ # Inside band 4 the churned wizard passes the quiet config loader, not the giant.+ findings = [_key(i) for i in items if i.kind == "finding" and _value(i) == "4"]+ assert findings == ["run_conversation", "show_status", "load_gateway_config"]+++def test_without_function_history_the_file_weight_still_orders() -> None:+ # No blame rows at all (the essential git tier): the churning file's x1.25 stands.+ items = _twins(None)+ quiet = build_fix_first(+ metrics=[_metric("a/one.py", 3, 0), _metric("b/two.py", 3, 40)],+ findings=[_finding(p, fn, 0.5, ccn=60, nloc=200, max_nesting=5)+ for p, fn in (("a/one.py", "first"), ("b/two.py", "second"))],+ hot_cuts=_CUTS, limit=None,+ ).items+ assert [i.target.symbol for i in quiet] == ["second", "first"]+ lead, other = ({f.factor: f.value for f in i.why_ranked} for i in quiet)+ assert (lead["changes often"], other["changes often"]) == ("yes", "no")+ assert float(lead["worth in band"]) == pytest.approx(+ float(other["worth in band"]) * CHURN_WEIGHT, abs=1)+ assert all("function changed" not in i.why for i in items)+++def test_a_unit_led_by_no_function_keeps_the_file_weight() -> None:+ # A class-level finding in a repository with function history: no function+ # leads it, so its file's churn still orders it.+ finding = {**_finding("a/one.py", "Store", 0.5, ccn=60, nloc=200, max_nesting=5),+ "biomarker_type": "god_class"}+ items = build_fix_first(metrics=[_metric("a/one.py", 3, 40)], findings=[finding],+ hot_cuts=_CUTS, limit=None,+ function_changes={"a/one.py::Store": 30}).items+ facts = [{f.factor: f.value for f in i.why_ranked} for i in items]+ assert all(f.get("changes often") == "yes" and "function changed in" not in f for f in facts)+++def test_a_file_blame_skipped_for_size_keeps_the_file_weight() -> None:+ # Function history elsewhere in the repository; "a/one.py" was too large to+ # blame, so its churning file weighs 1.25 and leads the equal quiet twin.+ metrics = [_metric("a/one.py", 3, 40), _metric("b/two.py", 3, 0)]+ findings = [_finding(p, fn, 0.5, ccn=60, nloc=200, max_nesting=5)+ for p, fn in (("a/one.py", "first"), ("b/two.py", "second"))]+ items = build_fix_first(metrics=metrics, findings=findings, hot_cuts=_CUTS, limit=None,+ function_changes={"c/three.py::other": 9},+ blame_skipped={"a/one.py"}).items+ assert [i.target.symbol for i in items] == ["first", "second"]+ lead, other = ({f.factor: f.value for f in i.why_ranked} for i in items)+ assert lead["changes often"] == "yes"+ # A missing row in a file that was blamed counts as unchanged.+ assert other["function changed in"] == "0 commits"+ assert float(lead["worth in band"]) == pytest.approx(+ float(other["worth in band"]) * CHURN_WEIGHT, abs=1)diff --git a/tests/unit/persistence/test_fix_first_loader.py b/tests/unit/persistence/test_fix_first_loader.pyindex 4d15aaa94..57a80c306 100644--- a/tests/unit/persistence/test_fix_first_loader.py+++ b/tests/unit/persistence/test_fix_first_loader.py@@ -8,6 +8,7 @@ from repowise.core.analysis.health.fix_first import build_fix_firstfrom repowise.core.analysis.health.refactoring.identity import REFACTORING_MODEL_VERSIONfrom repowise.core.persistence.crud.analysis.fix_first import load_fix_firstfrom repowise.core.persistence.models import (+ GitFunctionBlame,GitMetadata,GraphMetric,HealthFileMetric,@@ -94,6 +95,57 @@ async def test_loader_matches_the_builder_over_rows(async_session) -> None:assert loaded.basis["analyzed_commit"] == "abc"+async def test_loader_orders_by_the_stored_function_history(async_session) -> None:+ rid = await seed_fix_first(async_session)+ changes = {"src/core.py::run": 9, "src/plain.py::walk": 3}+ async_session.add_all(+ GitFunctionBlame(repository_id=rid, symbol_id=s, file_path=s.split("::")[0], mod_count=n)+ for s, n in {**changes, "lib/elsewhere.py::other": 40}.items()+ )+ await async_session.flush()+ loaded = await load_fix_first(async_session, rid)+ built = build_fix_first(+ metrics=METRICS,+ findings=FINDINGS,+ refactoring=REFACTORING,+ performance=PERFORMANCE,+ plans=PLANS,+ validate=no_tests,+ function_changes=changes,+ )+ assert loaded == built+ facts = [{f.factor: f.value for f in i.why_ranked} for i in loaded.items]+ assert {"9 commits", "3 commits"} <= {f.get("function changed in") for f in facts}+++async def test_loader_without_function_history_keeps_the_file_weight(async_session) -> None:+ rid = await seed_fix_first(async_session)+ loaded = await load_fix_first(async_session, rid)+ facts = [{f.factor for f in i.why_ranked} for i in loaded.items if i.kind != "perf_fix"]+ assert facts and all("changes often" in f and "function changed in" not in f for f in facts)+++async def test_loader_reads_a_file_blame_skipped_for_size(async_session) -> None:+ from sqlalchemy import update++ rid = await seed_fix_first(async_session)+ # History for one file only; src/core.py passes the commit floor with no+ # rows (too large to blame), src/plain.py does not reach the floor.+ async_session.add(GitFunctionBlame(repository_id=rid, symbol_id="src/repo.py::load",+ file_path="src/repo.py", mod_count=4))+ await async_session.execute(+ update(GitMetadata)+ .where(GitMetadata.file_path == "src/core.py")+ .values(commit_count_total=40)+ )+ await async_session.flush()+ loaded = await load_fix_first(async_session, rid)+ facts = {i.target.file_path: {f.factor: f.value for f in i.why_ranked}+ for i in loaded.items if i.kind != "perf_fix"}+ assert facts["src/core.py"]["changes often"] == "yes"+ assert facts["src/plain.py"]["function changed in"] == "0 commits"++async def test_loader_on_an_empty_repository(async_session) -> None:rid = (await insert_repo(async_session)).idqueue = await load_fix_first(async_session, rid)