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(change-risk): floor and cap the 'bigger than N%' percentile at 99 (#3120)

Nehant Fopse0a7b44a9435d
diff --git a/packages/core/src/repowise/core/analysis/change_risk/render.py b/packages/core/src/repowise/core/analysis/change_risk/render.py
index 3e62a521f..36d798818 100644
--- a/packages/core/src/repowise/core/analysis/change_risk/render.py
+++ b/packages/core/src/repowise/core/analysis/change_risk/render.py
@@ -11,6 +11,7 @@ shared with every other CI gate.
from __future__ import annotations
+import math
from typing import Literal
from ...ci import github
@@ -90,6 +91,9 @@ def headline(result: ChangeRiskResult, threshold: float | None, *, markdown: boo
if number is None:
# True for --baseline 0 and for a history of fewer than the minimum.
return f"{bold}Change risk: unranked{bold} · too few recent commits to rank it"
+ if "." not in number:
+ # "Larger than N%" is a lower bound: floor it, and never claim all 100%.
+ number = str(min(math.floor(result.percentile), 99))
rank = f"larger and more spread out than {number}% of recent commits"
gate = percentile_gate(result, threshold)
if gate == "fail":
diff --git a/packages/server/src/repowise/server/mcp_server/tool_change_risk.py b/packages/server/src/repowise/server/mcp_server/tool_change_risk.py
index 9836e81c6..224f19ae6 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_change_risk.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_change_risk.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
import contextlib
+import math
import subprocess
import threading
import time
@@ -504,7 +505,7 @@ def _diff_shape_sentence(payload: dict, diagnostics: dict) -> str:
"""
pct = payload.get("risk_percentile")
where = (
- f"bigger than {round(pct)}% of this repo's recent commits"
+ f"bigger than {min(math.floor(pct), 99)}% of this repo's recent commits"
if pct is not None
else "unranked (no baseline to compare against)"
)
diff --git a/tests/unit/cli/test_risk_cmd.py b/tests/unit/cli/test_risk_cmd.py
index 401114660..fbf361fe2 100644
--- a/tests/unit/cli/test_risk_cmd.py
+++ b/tests/unit/cli/test_risk_cmd.py
@@ -282,6 +282,18 @@ def test_percentile_text_never_hides_which_side_of_the_gate() -> None:
)
+def test_headline_never_claims_more_than_was_measured() -> None:
+ from repowise.core.analysis.change_risk.render import headline
+
+ # 99.5 and 100.0 read 99%: "larger than 100%" cannot be true.
+ for pct in (99.5, 100.0):
+ assert "than 99%" in headline(_scored(pct), None, markdown=False)
+ assert "than 99%" in headline(_scored(pct), 95, markdown=False)
+ assert "than 42%" in headline(_scored(42.9), None, markdown=False)
+ # The gate-aware decimals are already below the next whole number.
+ assert headline(_scored(95.4), 95, markdown=False).endswith("than 95.4% of recent commits")
+
+
def test_target_mode_refuses_ci_formats_and_the_gate(tmp_path: Path) -> None:
repo = _ranked_repo(tmp_path)
diff --git a/tests/unit/server/mcp/test_independent_changes_block.py b/tests/unit/server/mcp/test_independent_changes_block.py
index cd65fb219..5220edbc9 100644
--- a/tests/unit/server/mcp/test_independent_changes_block.py
+++ b/tests/unit/server/mcp/test_independent_changes_block.py
@@ -228,5 +228,8 @@ async def test_get_change_risk_carries_the_split_end_to_end(tmp_path, monkeypatc
def test_the_diff_shape_line_ranks_without_naming_a_raw_score():
assert "42%" in _diff_shape_sentence({"risk_percentile": 42.0}, {})
+ assert "42%" in _diff_shape_sentence({"risk_percentile": 42.9}, {})
+ assert "bigger than 99%" in _diff_shape_sentence({"risk_percentile": 99.5}, {})
+ assert "bigger than 99%" in _diff_shape_sentence({"risk_percentile": 100.0}, {})
assert "unranked" in _diff_shape_sentence({"risk_percentile": None}, {})
assert "diagnostics" in _diff_shape_sentence({"risk_percentile": 42.0}, {"score": 3.0})