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): count only fixes before the change in the overlap block (#3632)

Swati Ahujaff86a22cfaec
diff --git a/docs/layers/BUG_HISTORY.md b/docs/layers/BUG_HISTORY.md
index fa94bd898..262b0b05b 100644
--- a/docs/layers/BUG_HISTORY.md
+++ b/docs/layers/BUG_HISTORY.md
@@ -108,7 +108,9 @@ here", not as an exact ledger. Every surface that shows symbol counts says so.
how many past fixes it carries and how many of the change's lines fall inside a
past fix's replaced ranges. Fix counts are exact (one commit fixing three files
is one fix). Line overlap is marked `approximate`. Files are ranked by overlap
-then count, and a `truncated` flag marks a cut list. See
+then count, and a `truncated` flag marks a cut list. Only fixes from before the
+change count, at the same point the score reads its fix history: a commit's
+parent, a range's merge-base, or `HEAD` for uncommitted work. See
[CHANGE_RISK.md](CHANGE_RISK.md).
## Tuning and suppressing
diff --git a/packages/core/src/repowise/core/analysis/change_risk/__init__.py b/packages/core/src/repowise/core/analysis/change_risk/__init__.py
index ecf79e45c..e5b21e296 100644
--- a/packages/core/src/repowise/core/analysis/change_risk/__init__.py
+++ b/packages/core/src/repowise/core/analysis/change_risk/__init__.py
@@ -26,6 +26,7 @@ from .fix_history import (
clear_fix_pressure_cache,
fix_density_percentile,
fix_pressure,
+ fixes_reachable_from,
hot_files,
)
from .model import SCORE_MEASURES, SCORE_UNIT, ChangeRisk, RiskDriver, score_change
@@ -64,6 +65,7 @@ __all__ = [
"features_from_file_changes",
"fix_density_percentile",
"fix_pressure",
+ "fixes_reachable_from",
"hot_files",
"normalize_extensions",
"range_anchor",
diff --git a/packages/core/src/repowise/core/analysis/change_risk/fix_history.py b/packages/core/src/repowise/core/analysis/change_risk/fix_history.py
index 4ea6347a1..dc2cf5605 100644
--- a/packages/core/src/repowise/core/analysis/change_risk/fix_history.py
+++ b/packages/core/src/repowise/core/analysis/change_risk/fix_history.py
@@ -64,6 +64,23 @@ def clear_fix_pressure_cache() -> None:
_PRESSURE_CACHE.clear()
+def _history_sha(repo_path: str, upto_ref: str) -> str:
+ """*upto_ref*'s commit sha, or ``""`` when there is no history before it.
+
+ Two reasons a ref will not resolve, and they are not the same answer.
+ Inside a repository it means there is no history before this point (a root
+ commit's ``sha^`` on the common path) and empty is correct. Outside one,
+ nothing was looked at, and "no fixes" would be a claim we did not earn, so
+ that raises :class:`FixHistoryUnavailableError`.
+ """
+ sha = _git(
+ ["rev-parse", "--verify", "--quiet", f"{upto_ref}^{{commit}}"], repo_path, check=False
+ ).strip()
+ if not sha and not _git(["rev-parse", "--git-dir"], repo_path, check=False).strip():
+ raise FixHistoryUnavailableError(f"{repo_path} is not a git repository")
+ return sha
+
+
def _walk(repo_path: str, upto_ref: str, depth: int) -> dict[str, list[float]]:
"""Commit timestamps of every bug-fix touching each file, up to *upto_ref*."""
is_shallow = (
@@ -137,15 +154,8 @@ def fix_pressure(
Files with no fix history are absent from the mapping rather than present
with a zero. Raises :class:`FixHistoryUnavailableError` if the walk fails.
"""
- sha = _git(["rev-parse", "--verify", "--quiet", upto_ref], repo_path, check=False).strip()
+ sha = _history_sha(repo_path, upto_ref)
if not sha:
- # Two reasons a ref will not resolve, and they are not the same answer.
- # Inside a repository it means there is no history before this point — a
- # root commit's ``sha^`` on the common path — and empty is correct.
- # Outside one, nothing was looked at, and "no fixes" would be a claim we
- # did not earn.
- if not _git(["rev-parse", "--git-dir"], repo_path, check=False).strip():
- raise FixHistoryUnavailableError(f"{repo_path} is not a git repository")
return {}
key = (repo_path, sha, depth)
if key in _PRESSURE_CACHE:
@@ -164,6 +174,45 @@ def fix_pressure(
return pressure
+def fixes_reachable_from(repo_path: str, upto_ref: str, shas: Iterable[str]) -> set[str]:
+ """The subset of fix *shas* in *upto_ref*'s history, the ref included.
+
+ The line-scoped fix record comes from the index, which holds every fix up to
+ the indexed commit. Reviewing an older change, that includes the change
+ itself and what came after it; this keeps only what :func:`fix_pressure`
+ would also have walked. One ``git rev-list`` lists what the shas reach that
+ *upto_ref* does not, so the cost is the commits between the ref and the
+ newest fix: a review at the tip walks almost nothing.
+
+ A sha this checkout does not have (rewritten away, or an index built from
+ another clone) cannot be placed and is kept, as before this filter existed.
+ Raises :class:`FixHistoryUnavailableError` when git fails.
+ """
+ wanted = {sha for sha in shas if sha}
+ sha = _history_sha(repo_path, upto_ref) if wanted else ""
+ if not sha:
+ return set()
+ try:
+ # --stdin: a large fix set would overrun the Windows command line.
+ proc = subprocess.run(
+ ["git", "rev-list", "--ignore-missing", "--stdin"],
+ cwd=repo_path,
+ input="\n".join([*sorted(wanted), f"^{sha}"]) + "\n",
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ errors="replace",
+ timeout=GIT_TIMEOUT_SECONDS,
+ )
+ except subprocess.TimeoutExpired as exc:
+ raise FixHistoryUnavailableError(
+ f"git rev-list timed out after {GIT_TIMEOUT_SECONDS}s"
+ ) from exc
+ if proc.returncode != 0:
+ raise FixHistoryUnavailableError((proc.stderr or "").strip() or "git rev-list failed")
+ return wanted - set(proc.stdout.split())
+
+
def rename_target(path: str) -> str:
"""The post-rename path, for a numstat path that encodes a rename.
diff --git a/packages/core/src/repowise/core/analysis/change_risk/service.py b/packages/core/src/repowise/core/analysis/change_risk/service.py
index 5d000035d..828458b16 100644
--- a/packages/core/src/repowise/core/analysis/change_risk/service.py
+++ b/packages/core/src/repowise/core/analysis/change_risk/service.py
@@ -56,6 +56,10 @@ class ChangeRiskResult:
# large repository). Distinguishes "no fixes here" from "we could not look",
# which the surfaces would otherwise report identically.
fix_history_available: bool = True
+ # The ref the fix history was read at (see ``history_ref``), so the
+ # line-scoped fix record can be read at the same point. ``None`` when the
+ # change came from file stats with no checkout behind it.
+ history_ref: str | None = None
def riskignore_patterns(repo_path: str) -> tuple[str, ...]:
@@ -215,7 +219,7 @@ def score_live_change(
exclude_patterns=effective_excludes,
)
- return assess_change(
+ result = assess_change(
features,
fix_pressure=pressure,
baseline_scores=scores_excluding(samples, excluded_ref),
@@ -224,6 +228,7 @@ def score_live_change(
riskignore_excludes=from_riskignore,
request_excludes=exclude_patterns,
)
+ return replace(result, history_ref=history)
def assess_change(
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 5c7b95ab9..512bfca15 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
@@ -22,7 +22,9 @@ from repowise.core.analysis.change_health.service import (
DeltaRequest,
)
from repowise.core.analysis.change_risk import (
+ FixHistoryUnavailableError,
change_risk_payload,
+ fixes_reachable_from,
normalize_extensions,
score_live_change,
)
@@ -207,7 +209,7 @@ async def _attach_change_blocks(
)
if patch is not None:
payload["patch_coverage"] = patch
- prior_fixes = await _prior_fixes_block(ctx, changed)
+ prior_fixes = await _prior_fixes_block(ctx, changed, history_ref=result.history_ref)
if prior_fixes is not None:
# One fix record, not two. The blocks answered the same question
# with different arithmetic (decayed pressure vs raw count) and a
@@ -932,7 +934,9 @@ async def _changed_with_label(
return changed, None, label
-async def _prior_fixes_block(ctx: Any, changed: dict[str, set[int]]) -> dict[str, Any] | None:
+async def _prior_fixes_block(
+ ctx: Any, changed: dict[str, set[int]], *, history_ref: str | None = None
+) -> dict[str, Any] | None:
"""Past bug fixes that landed on the lines this change touches, or ``None``.
Aggregate only. No inducing commit is named: file-level SZZ measured 74.5%
@@ -950,7 +954,7 @@ async def _prior_fixes_block(ctx: Any, changed: dict[str, set[int]]) -> dict[str
not silent: it renders with ``status: "unavailable"``, because "we could not
look" and "nothing has broken here" are opposite claims about the change.
"""
- impact = await _read_prior_fixes(ctx, changed)
+ impact = await _read_prior_fixes(ctx, changed, history_ref=history_ref)
if impact.status == "unsupported" or (impact.status == "available" and impact.is_empty):
return None
if impact.status != "available":
@@ -1004,9 +1008,15 @@ def _prior_fix_row(entry: PriorFixFile) -> dict[str, Any]:
return row
-async def _read_prior_fixes(ctx: Any, changed: dict[str, set[int]]) -> PriorFixImpact:
+async def _read_prior_fixes(
+ ctx: Any, changed: dict[str, set[int]], *, history_ref: str | None = None
+) -> PriorFixImpact:
"""Collect fix events from the index. Collection only; core summarizes.
+ With *history_ref* (the score's own fix-history anchor) only fixes in that
+ ref's history count, so a reviewed commit and its successors in the index
+ are not read back as the ground it stood on.
+
The four ways this can come back empty are not one state. An index that
predates fix events has nothing to read (``unsupported``), while a query
that failed means the record exists and was not read (``unavailable``).
@@ -1044,6 +1054,12 @@ async def _read_prior_fixes(ctx: Any, changed: dict[str, set[int]]) -> PriorFixI
except SQLAlchemyError as exc:
return unavailable_prior_fixes(_read_failure(exc))
+ if history_ref is not None:
+ try:
+ events = await _events_before(ctx, events, history_ref)
+ except FixHistoryUnavailableError as exc:
+ return unavailable_prior_fixes(f"could not place fixes in history: {exc}")
+
return summarize_prior_fixes(
(
FixRecord(
@@ -1058,6 +1074,14 @@ async def _read_prior_fixes(ctx: Any, changed: dict[str, set[int]]) -> PriorFixI
)
+async def _events_before(ctx: Any, events: list[Any], history_ref: str) -> list[Any]:
+ """The fix events in *history_ref*'s history, so a review never counts itself."""
+ kept = await asyncio.to_thread(
+ fixes_reachable_from, str(ctx.path), history_ref, (event.fix_sha for event in events)
+ )
+ return [event for event in events if event.fix_sha in kept]
+
+
def _read_failure(exc: SQLAlchemyError) -> str:
"""Name the failure without quoting the driver.
diff --git a/tests/unit/analysis/test_change_risk_fix_history.py b/tests/unit/analysis/test_change_risk_fix_history.py
index 7677f89e6..247488cad 100644
--- a/tests/unit/analysis/test_change_risk_fix_history.py
+++ b/tests/unit/analysis/test_change_risk_fix_history.py
@@ -21,6 +21,7 @@ from repowise.core.analysis.change_risk import (
densities_excluding,
fix_density_percentile,
fix_pressure,
+ fixes_reachable_from,
score_live_change,
)
from repowise.core.analysis.change_risk.fix_history import rename_target
@@ -154,6 +155,42 @@ def test_fix_history_excludes_the_change_being_scored(repo_with_history: Path) -
assert result.fix_density == pytest.approx(before["hot.py"], abs=0.05)
+def test_reachable_fixes_stop_at_the_ref(repo_with_history: Path) -> None:
+ """A fix at or after the change is not ground the change stood on."""
+ repo = str(repo_with_history)
+ first = _commit(repo_with_history, {"hot.py": "v = 50\n"}, "fix: earlier crash")
+ second = _commit(repo_with_history, {"hot.py": "v = 51\n"}, "fix: reviewed crash")
+ missing = "f" * 40
+ assert fixes_reachable_from(repo, f"{second}^", [first, second, missing]) == {first, missing}
+ assert fixes_reachable_from(repo, second, [first, second]) == {first, second}
+ root = subprocess.run(
+ ["git", "rev-list", "--max-parents=0", "HEAD"],
+ cwd=repo,
+ check=True,
+ capture_output=True,
+ text=True,
+ ).stdout.strip()
+ # Before the root commit there is no history at all.
+ assert fixes_reachable_from(repo, f"{root}^", [first]) == set()
+
+
+def test_reachable_fixes_raise_outside_a_repository(tmp_path: Path) -> None:
+ with pytest.raises(FixHistoryUnavailableError):
+ fixes_reachable_from(str(tmp_path), "HEAD", ["a" * 40])
+
+
+def test_result_carries_the_ref_its_history_was_read_at(repo_with_history: Path) -> None:
+ repo = str(repo_with_history)
+ sha = _commit(repo_with_history, {"hot.py": "v = 60\n"}, "fix: reviewed")
+ assert score_live_change(repo, sha, baseline=0).history_ref == f"{sha}^"
+ base = subprocess.run(
+ ["git", "rev-parse", "HEAD~2"], cwd=repo, check=True, capture_output=True, text=True
+ ).stdout.strip()
+ assert score_live_change(repo, f"{base}..{sha}", baseline=0).history_ref == base
+ (repo_with_history / "hot.py").write_text("v = 61\n", encoding="utf-8")
+ assert score_live_change(repo, baseline=0).history_ref == "HEAD"
+
+
def test_percentile_needs_enough_commits_to_rank_against() -> None:
assert fix_density_percentile([1.0, 2.0, 3.0], 2.5) is None
assert fix_density_percentile([float(i) for i in range(8)], 2.5) is not None
diff --git a/tests/unit/server/mcp/test_change_risk_prior_fixes.py b/tests/unit/server/mcp/test_change_risk_prior_fixes.py
index 3a04cf327..d3dd49d48 100644
--- a/tests/unit/server/mcp/test_change_risk_prior_fixes.py
+++ b/tests/unit/server/mcp/test_change_risk_prior_fixes.py
@@ -20,7 +20,7 @@ from repowise.server.mcp_server import tool_change_risk as tool
async def _block(monkeypatch, impact, changed=None):
- async def _fake(ctx, changed_lines):
+ async def _fake(ctx, changed_lines, **_):
return impact
monkeypatch.setattr(tool, "_read_prior_fixes", _fake)
diff --git a/tests/unit/server/mcp/test_prior_fixes.py b/tests/unit/server/mcp/test_prior_fixes.py
index 84ca3c0c2..2adfc80b1 100644
--- a/tests/unit/server/mcp/test_prior_fixes.py
+++ b/tests/unit/server/mcp/test_prior_fixes.py
@@ -12,11 +12,13 @@ fix-event table existed sees no new block at all.
from __future__ import annotations
import json
+import subprocess
from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
+from repowise.core.analysis.change_risk.service import history_ref
from repowise.core.analysis.prior_fix_impact import (
FixRecord,
parse_old_ranges,
@@ -67,9 +69,16 @@ _REPO_ID = "repo1"
async def _ctx_with_events(
- tmp_path, events: list[tuple[str, list, int]], *, shape_kind: str = "code_fix"
+ tmp_path,
+ events: list[tuple[str, list, int]],
+ *,
+ shape_kind: str = "code_fix",
+ shas: list[str] | None = None,
) -> SimpleNamespace:
- """A context over a real wiki.db seeded with ``(path, old_ranges, age_days)``."""
+ """A context over a real wiki.db seeded with ``(path, old_ranges, age_days)``.
+
+ *shas* names each event's fix commit; synthetic shas otherwise.
+ """
tmp_path.mkdir(parents=True, exist_ok=True)
db_path = tmp_path / "wiki.db"
engine = create_async_engine(f"sqlite+aiosqlite:///{db_path.as_posix()}")
@@ -82,7 +91,7 @@ async def _ctx_with_events(
FixEvent(
id=_new_uuid(),
repository_id=_REPO_ID,
- fix_sha=f"{i:040x}",
+ fix_sha=shas[i] if shas else f"{i:040x}",
file_path=path,
shape_kind=shape_kind,
old_ranges_json=json.dumps(old_ranges),
@@ -144,3 +153,71 @@ async def test_non_code_fixes_are_not_counted(tmp_path):
"""
ctx = await _ctx_with_events(tmp_path, [("src/app.py", [[1, 5]], 10)], shape_kind="doc_only")
assert await _prior_fixes_block(ctx, {"src/app.py": {1, 2, 3}}) is None
+
+
+def _git(repo, *args: str) -> str:
+ return subprocess.run(
+ ["git", "-c", "user.name=Dev", "-c", "[email protected]", *args],
+ cwd=repo,
+ check=True,
+ capture_output=True,
+ text=True,
+ ).stdout.strip()
+
+
+def _fix(repo, content: str, message: str) -> str:
+ (repo / "app.py").write_text(content, encoding="utf-8")
+ _git(repo, "add", "app.py")
+ _git(repo, "commit", "-q", "-m", message)
+ return _git(repo, "rev-parse", "HEAD")
+
+
+async def _reviewed_repo(tmp_path):
+ """seed, an earlier fix, a feature, then the fix under review at HEAD."""
+ repo = tmp_path / "repo"
+ repo.mkdir()
+ _git(repo, "init", "-q")
+ _fix(repo, "v = 0\n", "feat: seed")
+ earlier = _fix(repo, "v = 1\n", "fix: earlier crash")
+ feature = _fix(repo, "v = 2\n", "feat: more")
+ reviewed = _fix(repo, "v = 3\n", "fix: reviewed crash")
+ ctx = await _ctx_with_events(
+ repo, [("app.py", [[1, 1]], 30), ("app.py", [[1, 1]], 1)], shas=[earlier, reviewed]
+ )
+ return repo, ctx, earlier, feature, reviewed
+
+
+async def test_a_reviewed_commit_is_not_its_own_fix_history(tmp_path):
+ """The index holds the reviewed fix; reading it back as past ground is circular."""
+ repo, ctx, *_, reviewed = await _reviewed_repo(tmp_path)
+
+ block = await _prior_fixes_block(
+ ctx, {"app.py": {1}}, history_ref=history_ref(str(repo), reviewed)
+ )
+
+ assert block["total_fixes"] == 1
+ assert block["files"][0]["last_fix_days_ago"] == 30
+
+
+async def test_a_range_reads_fixes_before_its_merge_base(tmp_path):
+ repo, ctx, earlier, feature, _ = await _reviewed_repo(tmp_path)
+
+ in_range = await _prior_fixes_block(
+ ctx, {"app.py": {1}}, history_ref=history_ref(str(repo), f"{feature}..HEAD")
+ )
+ before_earlier = await _prior_fixes_block(
+ ctx, {"app.py": {1}}, history_ref=history_ref(str(repo), f"{earlier}^..HEAD")
+ )
+
+ assert in_range["total_fixes"] == 1
+ assert before_earlier is None
+
+
+async def test_uncommitted_work_keeps_heads_fix(tmp_path):
+ repo, ctx, *_ = await _reviewed_repo(tmp_path)
+
+ block = await _prior_fixes_block(
+ ctx, {"app.py": {1}}, history_ref=history_ref(str(repo), None, working_tree=True)
+ )
+
+ assert block["total_fixes"] == 2