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
fix(git): align in-memory churn_percentile with SQL PERCENT_RANK and tie handling (#4159) (#4189)
Rishabh Mishra58a153e599b4
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/enrich.py b/packages/core/src/repowise/core/ingestion/git_indexer/enrich.pyindex f45c1518f..9308d52c6 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/enrich.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/enrich.py@@ -267,42 +267,65 @@ def compute_percentiles(metadata_list: list[dict]) -> None:if not metadata_list:return- # Sort by temporal_hotspot_score (primary) then commit_count_90d (tiebreak)- sorted_by_churn = sorted(- range(len(metadata_list)),- key=lambda i: (- metadata_list[i].get("temporal_hotspot_score") or 0.0,- metadata_list[i].get("commit_count_90d", 0),- ),- )+ # Non-code history-tier files do not participate in code churn rankings.+ code_metas = [m for m in metadata_list if not m.get("history_only")]+ for meta in metadata_list:+ if meta.get("history_only"):+ meta["churn_percentile"] = 0.0+ meta["is_hotspot"] = False+ meta.setdefault("change_entropy_pct", 0.0)+ meta.setdefault("co_change_scatter_pct", 0.0)+ meta.setdefault("prior_defect_pct", 0.0)++ if not code_metas:+ return- total = len(metadata_list)- for rank, idx in enumerate(sorted_by_churn):- meta = metadata_list[idx]- churn_pct = rank / total if total > 0 else 0.0- meta["churn_percentile"] = churn_pct- # Hotspot: top 25% churn (churn_percentile >= 0.75) AND the absolute- # activity floors — without the floors, a quiet repo's top quartile is- # just "every file touched in 90 days" (issue #361). Folded into the- # rank loop so the list is scanned once.- if churn_pct >= 0.75 and meets_hotspot_floors(meta):- meta["is_hotspot"] = True+ total = len(code_metas)+ if total < 2:+ for meta in code_metas:+ meta["churn_percentile"] = 0.0+ else:+ # Group keys to mirror SQL PERCENT_RANK() with tie-awareness:+ # (RANK() - 1) / (total - 1)+ keys = [+ (+ float(m.get("temporal_hotspot_score") or 0.0),+ int(m.get("commit_count_90d") or 0),+ )+ for m in code_metas+ ]+ key_counts = Counter(keys)+ rank_lookup: dict[tuple[float, int], float] = {}+ running = 0+ for key in sorted(key_counts):+ rank_lookup[key] = running / (total - 1)+ running += key_counts[key]++ for meta, key in zip(code_metas, keys, strict=True):+ churn_pct = min(rank_lookup[key], 1.0)+ meta["churn_percentile"] = churn_pct+ # Hotspot: top 25% churn (churn_percentile >= 0.75) AND the absolute+ # activity floors — without the floors, a quiet repo's top quartile is+ # just "every file touched in 90 days" (issue #361). Folded into the+ # rank loop so the list is scanned once.+ if churn_pct >= 0.75 and meets_hotspot_floors(meta):+ meta["is_hotspot"] = True# Files with zero entropy — every file on the ESSENTIAL tier, plus# FULL-tier files that only ever changed alone — stay at 0.0 so the# change_entropy biomarker is silent for them._rank_within_eligible(- metadata_list, source_key="change_entropy", target_key="change_entropy_pct"+ code_metas, source_key="change_entropy", target_key="change_entropy_pct")# Ranked on the decayed partner mass, not the count: a count saturates at# the storage cap and never retires, so it can only ever ratchet up._rank_within_eligible(- metadata_list, source_key="co_change_mass", target_key="co_change_scatter_pct"+ code_metas, source_key="co_change_mass", target_key="co_change_scatter_pct")# Bug-fix history, ranked over every file: a file with no fixes in the# window is a measured zero, not a missing signal._rank_over_population(- metadata_list, source_key="prior_defect_count", target_key="prior_defect_pct"+ code_metas, source_key="prior_defect_count", target_key="prior_defect_pct")diff --git a/tests/unit/ingestion/test_git_history_tier.py b/tests/unit/ingestion/test_git_history_tier.pyindex 7ecb820f2..8e3ee7b2d 100644--- a/tests/unit/ingestion/test_git_history_tier.py+++ b/tests/unit/ingestion/test_git_history_tier.py@@ -117,7 +117,7 @@ async def test_non_code_files_get_history_from_the_shared_walk(tmp_path, monkeypassert meta["a.py"]["history_only"] is Falseassert meta["a.py"]["commit_count_total"] == _git_log_count(tmp_path, "a.py")assert meta["a.py"]["primary_owner_line_pct"] is not None- assert {meta["a.py"]["churn_percentile"], meta["b.py"]["churn_percentile"]} <= {0.0, 0.5}+ assert {meta["a.py"]["churn_percentile"], meta["b.py"]["churn_percentile"]} <= {0.0, 1.0}async def test_update_writes_the_same_history_rows_as_a_full_index(tmp_path, monkeypatch) -> None:@@ -206,3 +206,23 @@ def test_history_tier_keeps_files_in_reincluded_dir(tmp_path: Path) -> None:assert kept_custom == {"src/build/README.md"}+def test_two_code_files_plus_history_only_percentile_calculation():+ from repowise.core.ingestion.git_indexer.enrich import compute_percentiles++ metas = [+ {"file_path": "a.py", "temporal_hotspot_score": 10.0, "commit_count_90d": 5},+ {"file_path": "b.py", "temporal_hotspot_score": 2.0, "commit_count_90d": 1},+ {"file_path": "README.md", "temporal_hotspot_score": 0.0, "commit_count_90d": 20, "history_only": True},+ {"file_path": "config.json", "temporal_hotspot_score": 0.0, "commit_count_90d": 5, "history_only": True},+ ]+ compute_percentiles(metas)++ # History-only files get 0.0 churn percentile and do not participate in code ranking+ assert metas[2]["churn_percentile"] == 0.0+ assert metas[3]["churn_percentile"] == 0.0++ # Two code files: lower is 0.0, higher is 1.0+ assert metas[0]["churn_percentile"] == 1.0+ assert metas[1]["churn_percentile"] == 0.0++diff --git a/tests/unit/test_git_indexer.py b/tests/unit/test_git_indexer.pyindex 068a090cf..6a25382e5 100644--- a/tests/unit/test_git_indexer.py+++ b/tests/unit/test_git_indexer.py@@ -318,6 +318,48 @@ class TestHotspotAbsoluteFloors:assets = next(m for m in metas if m["file_path"] == "assets.bin")assert assets["is_hotspot"] is True+ def test_percentiles_parity_with_sql_percent_rank(self) -> None:+ """N=5 percentiles mirror SQL PERCENT_RANK() exactly: [0.0, 0.25, 0.50, 0.75, 1.00]."""+ from repowise.core.ingestion.git_indexer.enrich import compute_percentiles++ metas = [+ {+ "file_path": f"f{i}.py",+ "temporal_hotspot_score": float(i * 10),+ "commit_count_90d": 10,+ "lines": 100,+ }+ for i in range(1, 6)+ ]+ compute_percentiles(metas)+ pcts = [m["churn_percentile"] for m in metas]+ assert pcts == [0.0, 0.25, 0.50, 0.75, 1.00]+ # f4 and f5 are >= 0.75 and clear floors+ assert metas[0].get("is_hotspot", False) is False+ assert metas[1].get("is_hotspot", False) is False+ assert metas[2].get("is_hotspot", False) is False+ assert metas[3].get("is_hotspot", False) is True+ assert metas[4].get("is_hotspot", False) is True++ def test_percentiles_tie_breaking_shared_rank(self) -> None:+ """Ties share the same rank and hotspot classification instead of being arbitrarily split."""+ from repowise.core.ingestion.git_indexer.enrich import compute_percentiles++ metas = [+ {"file_path": "quiet.py", "temporal_hotspot_score": 1.0, "commit_count_90d": 2, "lines": 50},+ {"file_path": "tied_a.py", "temporal_hotspot_score": 50.0, "commit_count_90d": 12, "lines": 100},+ {"file_path": "tied_b.py", "temporal_hotspot_score": 50.0, "commit_count_90d": 12, "lines": 100},+ {"file_path": "tied_c.py", "temporal_hotspot_score": 50.0, "commit_count_90d": 12, "lines": 100},+ ]+ compute_percentiles(metas)+ assert metas[0]["churn_percentile"] == 0.0+ # All three tied items have exactly 1 file scoring strictly lower out of 3 possible: 1 / 3 = 0.333...+ assert metas[1]["churn_percentile"] == pytest.approx(1 / 3)+ assert metas[2]["churn_percentile"] == pytest.approx(1 / 3)+ assert metas[3]["churn_percentile"] == pytest.approx(1 / 3)+ # Hotspot status is identical for all three+ assert metas[1].get("is_hotspot", False) == metas[2].get("is_hotspot", False) == metas[3].get("is_hotspot", False)+class TestCountActiveContributors:"""count_active_contributors derives the repo's 90-day team size from