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.

chore(git-indexer): drop the SZZ blame pass (#951)

Raghav Chamadiya9caca5ea2e59
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py b/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py
index 97a2336a5..88fe52a3f 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py
@@ -57,7 +57,6 @@ from .records import (
_parse_commit_record,
_should_skip_index,
)
-from .szz import InducingCandidate, SzzTracer, rank_candidates
from .tiers import GitIndexTier
__all__ = [
@@ -75,9 +74,7 @@ __all__ = [
"GitIndexSummary",
"GitIndexTier",
"GitIndexer",
- "InducingCandidate",
"PriorDefects",
- "SzzTracer",
"_CommitRec",
"_extract_rename_paths",
"_parse_commit_record",
@@ -100,5 +97,4 @@ __all__ = [
"is_fix_commit",
"is_significant_commit",
"meets_hotspot_floors",
- "rank_candidates",
]
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/fix_events.py b/packages/core/src/repowise/core/ingestion/git_indexer/fix_events.py
index 062f1ff5d..e693f90e1 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/fix_events.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/fix_events.py
@@ -2,67 +2,57 @@
``prior_defect_count`` collapses a repo's fix history into one number per file.
This module keeps the history itself: one record per fix-commit x file, carrying
-the diff shape, the old-side line ranges the fix replaced, its changed LOC, and
-- for ``code_fix`` rows - the ranked bug-introducing candidates :mod:`szz` traced.
+the diff shape, the old-side line ranges the fix replaced, and its changed LOC.
Two entry points, one for each half of the indexing lifecycle:
* :func:`build_fix_events` runs over a whole :class:`~.prior_defects.FixWalk`,
which is what a full index produces for the trailing defect window;
* the same function over a walk narrowed by ``skip_shas`` is the incremental
- path, so an update traces only the fix commits it has never seen.
+ path, so an update builds rows only for fix commits it has never seen.
Rows are stored undecayed with their ``committed_at``. Nothing here weighs
recency: that is a read-time decision, so a half-life change never costs a
reindex.
+
+These rows once carried SZZ blame candidates naming the commit that introduced
+each bug. Blame traced them at ~74.5% precision, which was never enough to name
+a commit in a user-facing surface, and every consumer that would have read them
+was cut before one shipped. The pass was removed rather than left running for a
+column nobody reads; ``inducing_shas_json`` survives, always empty. The tracer
+and its accuracy measurements are in git history.
"""
from __future__ import annotations
import json
-from concurrent.futures import ThreadPoolExecutor
from datetime import UTC, datetime
-from typing import Any
import structlog
-from .fix_shape import is_code_path
from .prior_defects import FixCommit, FixWalk
-from .szz import SzzTracer
logger = structlog.get_logger(__name__)
__all__ = ["build_fix_events"]
-# The blame pass is subprocess-bound, so it scales with workers rather than
-# cores. Matches the per-file git workers the indexer already runs.
-_TRACE_WORKERS = 20
-
-
-def build_fix_events(walk: FixWalk, get_repo: Any) -> list[dict]:
- """Rows for every fix-commit x file in *walk*, SZZ-traced where it applies.
- *get_repo* is a callable returning a gitpython ``Repo`` usable from the
- calling thread (blame runs across a worker pool). An update passes a walk
- already narrowed to the fix commits it has never seen
- (:func:`~.prior_defects.collect_fix_commits`'s ``skip_shas``), so nothing is
- re-blamed.
+def build_fix_events(walk: FixWalk) -> list[dict]:
+ """Rows for every fix-commit x file in *walk*.
- Blame runs only for ``code_fix`` commits on production-code paths: tests,
- docs and config carry no defect the ``prior_defect`` signal cares about, and
- blaming them would be the expensive way to learn nothing.
+ An update passes a walk already narrowed to the fix commits it has never
+ seen (:func:`~.prior_defects.collect_fix_commits`'s ``skip_shas``), so
+ nothing is rebuilt.
"""
if not walk.fixes:
return []
- tracer = SzzTracer(get_repo)
- units = [(fix, path) for fix in walk.fixes for path in fix.paths]
-
- with ThreadPoolExecutor(max_workers=_TRACE_WORKERS, thread_name_prefix="szz") as pool:
- # ``map`` re-raises the first failure and discards every other result,
- # so isolation lives inside the mapped call: one unlucky file must not
- # cost the whole window's events.
- rows = [row for row in pool.map(lambda unit: _safe_row(tracer, *unit), units) if row]
+ rows = [
+ row
+ for fix in walk.fixes
+ for path in fix.paths
+ if (row := _safe_row(fix, path)) is not None
+ ]
# Deterministic order so a replayed capture writes the same rows in the same
# sequence as a fresh one.
@@ -70,35 +60,27 @@ def build_fix_events(walk: FixWalk, get_repo: Any) -> list[dict]:
return rows
-def _safe_row(tracer: SzzTracer, fix: FixCommit, path: str) -> dict | None:
+def _safe_row(fix: FixCommit, path: str) -> dict | None:
+ """One row, or ``None``: one unlucky file must not cost the whole window."""
try:
- return _build_row(tracer, fix, path)
+ return _build_row(fix, path)
except Exception as exc:
logger.debug("fix_event_row_failed", fix=fix.sha[:12], path=path, error=str(exc))
return None
-def _build_row(tracer: SzzTracer, fix: FixCommit, path: str) -> dict:
+def _build_row(fix: FixCommit, path: str) -> dict:
diff = fix.files.get(path)
old_ranges = list(diff.old_ranges) if diff is not None else []
changed_loc = len(diff.removed) + len(diff.added) if diff is not None else 0
- candidates: list[dict] = []
- if fix.shape_kind == "code_fix" and diff is not None and is_code_path(path):
- try:
- candidates = [c.as_dict() for c in tracer.trace_file(fix.sha, path, diff)]
- except Exception as exc:
- # A tracing failure must never cost the event row itself: the shape
- # and ranges are still true, only the attribution is missing.
- logger.debug("szz_trace_failed", fix=fix.sha[:12], path=path, error=str(exc))
-
return {
"fix_sha": fix.sha,
"file_path": path,
"shape_kind": fix.shape_kind,
"old_ranges_json": json.dumps([list(r) for r in old_ranges]),
"changed_loc": changed_loc,
- "inducing_shas_json": json.dumps(candidates),
+ "inducing_shas_json": "[]",
"committed_at": _as_datetime(fix.ts),
}
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py b/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py
index 55d2a5ecb..c7fd1d50d 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py
@@ -274,7 +274,7 @@ class GitIndexer:
# it fans blame subprocesses out across its own pool for long enough that
# progress callbacks would visibly stall. Failure-isolated — the counts
# above stand on their own if tracing breaks.
- fix_event_rows, traced_ok = await asyncio.to_thread(self._trace_fix_events, fix_walk)
+ fix_event_rows, built_ok = await asyncio.to_thread(self._build_fix_events, fix_walk)
# Per-file AI line share from the agent-trace records. Keyed by path
# like the aggregates below, so it merges in the same pass and
@@ -325,7 +325,7 @@ class GitIndexer:
commit_rows=commit_rows,
fix_event_rows=fix_event_rows,
fix_oldest_ts=fix_walk.oldest_fix_ts,
- fix_events_traced=traced_ok,
+ fix_events_built=built_ok,
# Whole-history totals from cheap git calls on the still-open repo —
# true project age / commit / contributor counts for the stats page,
# which must not read them off the depth-capped sample (issue #730).
@@ -552,7 +552,7 @@ class GitIndexer:
as_of_ts=self._resolve_as_of_ts(repo),
skip_shas=known_shas,
)
- rows, ok = self._trace_fix_events(walk)
+ rows, ok = self._build_fix_events(walk)
return rows, (walk.oldest_fix_ts if ok else 0), tracked
except Exception as exc:
logger.debug("incremental_fix_events_failed", error=str(exc))
@@ -561,30 +561,23 @@ class GitIndexer:
with contextlib.suppress(Exception):
repo.close()
- def _trace_fix_events(self, walk: FixWalk) -> tuple[list[dict], bool]:
- """SZZ-trace *walk*'s fix commits into ``(rows, traced_ok)``.
+ def _build_fix_events(self, walk: FixWalk) -> tuple[list[dict], bool]:
+ """Build *walk*'s fix commits into ``(rows, built_ok)``.
- Blame needs its own thread-local repo handles (gitpython handles are not
- shareable on Windows), so this opens a short-lived pool of its own rather
- than reusing the per-file one, which is already closed by this point.
-
- Failure-isolated: tracing never breaks the git phase. The flag matters
+ Failure-isolated: this never breaks the git phase. The flag matters
because "no rows" is ambiguous - a window with no new fixes and a pass
that blew up look the same from the outside, and only one of them should
let the caller prune.
"""
if not walk.fixes:
return [], True
- get_thread_repo, close_thread_repos = self._thread_repo_pool()
try:
from .fix_events import build_fix_events
- return build_fix_events(walk, get_thread_repo), True
+ return build_fix_events(walk), True
except Exception as exc:
- logger.debug("fix_event_trace_failed", error=str(exc))
+ logger.debug("fix_event_build_failed", error=str(exc))
return [], False
- finally:
- close_thread_repos()
def capture_repo_totals(self) -> Any:
"""Whole-history :class:`RepoTotals` for this repo (opens its own repo).
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/records.py b/packages/core/src/repowise/core/ingestion/git_indexer/records.py
index adefd8050..592531734 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/records.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/records.py
@@ -143,11 +143,11 @@ class GitIndexSummary:
# committer time of the oldest fix the walk saw (unix seconds). The cutoff
# rides along so the persist step can prune events that have aged out and
# keep the stored set equal to what a fresh index at this HEAD produces.
- # ``fix_events_traced`` says the pass actually ran, so a failed trace does
+ # ``fix_events_built`` says the pass actually ran, so a failed trace does
# not prune rows it never replaced.
fix_event_rows: list[dict] = field(default_factory=list)
fix_oldest_ts: int = 0
- fix_events_traced: bool = False
+ fix_events_built: bool = False
# Whole-history git totals captured via cheap ``git rev-list``/``shortlog``
# calls that ignore ``commit_limit`` (unlike ``commit_rows``). Persisted to
# the Repository row so the stats page reports true project age / commit /
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/szz.py b/packages/core/src/repowise/core/ingestion/git_indexer/szz.py
deleted file mode 100644
index be32daad7..000000000
--- a/packages/core/src/repowise/core/ingestion/git_indexer/szz.py
+++ /dev/null
@@ -1,425 +0,0 @@
-"""SZZ: which commit introduced the bug this fix commit repaired.
-
-The classic SZZ heuristic (Śliwerski-Zimmermann-Zeller): the lines a fix
-*deleted* are the buggy lines, so ``git blame`` them at the fix's parent and the
-commits that wrote them are the bug-introducing candidates.
-
-Concretely, per ``code_fix`` file:
-
-* take the old-side ranges of the fix's diff (:func:`analysis.changed_lines.
- parse_unified_diff` already produces them);
-* one batched ``git blame -w -M --porcelain`` with every range as its own
- ``-L`` at ``<fix>^`` — ``-w`` ignores whitespace-only reformatting, ``-M``
- follows moves inside the file, the two standard SZZ noise filters;
-* rank the resulting commits by how many blamed lines each owns.
-
-Blame runs against **full history**, never the fix-detection window: measured bug
-latency is 41 to 203 days at the median, so the inducing commit routinely
-predates the 180-day window the fix itself was found in.
-
-## Refactor-aware blame
-
-The plain heuristic's dominant error mode, and it is not subtle: in the frozen
-48-commit judgment set, *all 14* wrong top candidates were behaviour-preserving
-refactors that had simply inherited the lines by moving them. ``-M`` follows
-moves within one file; it does nothing for a block lifted from another file, an
-import reshuffle, or a rename.
-
-So candidates get a second look, and the question asked is deliberately narrow:
-did this candidate *carry the lines it is blamed for through unchanged*? If each
-of them appears on both sides of the candidate's own diff, the candidate
-relocated that code rather than writing it, and the blame re-runs in a mode that
-can see past it: ``-C -C`` copy detection, the mover on ``--ignore-rev``, and the
-``-L`` ranges padded outward.
-
-Per-blamed-line, not per-commit, because no real refactor is a byte-exact move.
-Measured on this repo's own package splits - the ones the frozen judgments call
-"verbatim", "no logic changes" - a 1,131-line move comes back as 1,332 lines: new
-module headers, re-sorted imports, an ``__all__``. A whole-commit multiset test
-calls every one of them an edit and the mitigation never fires. Asking only about
-the lines actually being blamed is both stricter (a candidate that touched one of
-them keeps the blame) and the question SZZ is really asking.
-
-The padding is not cosmetic. Copy detection matches a *block* against the
-parent's files, so a one-line ``-L`` gives it nothing to match on and it silently
-falls back to naming the mover; with a dozen lines of context it finds the
-original file and reaches the true author. The extra lines are only ever context
-for the match - overlap is still counted over the fix's own lines, so a padded
-re-blame cannot inflate a candidate's rank.
-
-Bounded on purpose: only the top few candidates per file are diff-checked, and
-the walk re-blames at most twice, so a pathological chain of refactors costs a
-fixed handful of subprocesses instead of an unbounded recursion.
-
-Zero LLM, pure git plumbing, deterministic: ties break on committer time then
-sha, so the same repo always yields the same ranking.
-"""
-
-from __future__ import annotations
-
-import re
-import threading
-from collections.abc import Callable
-from dataclasses import dataclass
-from typing import TYPE_CHECKING, Any
-
-import structlog
-
-from .fix_shape import _is_inert
-
-if TYPE_CHECKING:
- from ...analysis.changed_lines import FileDiff
-
-logger = structlog.get_logger(__name__)
-
-__all__ = ["InducingCandidate", "SzzTracer", "rank_candidates"]
-
-# Tuning knobs. All three exist to keep one pathological commit from dominating
-# the git phase; raising them costs wall-clock, never correctness.
-#
-# A mass reorganization blames as "everything, everywhere" and tells you nothing,
-# so files past this many changed lines are skipped outright.
-MAX_FILE_CHANGED_LOC: int = 500
-# ``-L`` ranges per blame call. Past this the command line grows without the
-# ranking changing (the tail hunks are a rounding error on the overlap counts).
-MAX_RANGES_PER_FILE: int = 20
-# How many top candidates get their own diff fetched for the pure-move test,
-# and how many times the blame may be re-run after ignoring one.
-MAX_REFACTOR_CHECKS: int = 5
-MAX_REFACTOR_ROUNDS: int = 2
-# Context lines added on each side of a range for the refactor-aware re-blame.
-# Copy detection needs a block to match; measured on a 6-function move, three
-# lines was already enough, so this is comfortably past the cliff.
-REFACTOR_PAD_LINES: int = 15
-# A candidate whose own diff is bigger than this is not read at all. Past a
-# megabyte the multiset test is decided by generated files, and holding the
-# patch in memory costs more than the answer is worth.
-MAX_MOVE_CHECK_BYTES: int = 1_000_000
-
-# Porcelain header: ``<sha> <orig-line> <final-line> [<num-lines>]``. Content
-# lines are tab-prefixed, so they can never match.
-_BLAME_HEADER_RE = re.compile(r"^([0-9a-f]{40}) \d+ (\d+)")
-
-
-@dataclass(frozen=True)
-class InducingCandidate:
- """One bug-introducing candidate for a fix event.
-
- *lines* is how many of the fix's deleted lines blame attributes to this
- commit — the overlap that ranks it. *ts* is its committer time (0 when the
- porcelain output did not carry one), kept so a consumer can re-rank by age
- without another git call.
- """
-
- sha: str
- lines: int
- ts: int
-
- def as_dict(self) -> dict[str, Any]:
- return {"sha": self.sha, "lines": self.lines, "ts": self.ts}
-
-
-def rank_candidates(
- totals: dict[str, tuple[int, int]], *, by: str = "overlap"
-) -> list[InducingCandidate]:
- """Order raw ``{sha: (blamed_lines, ts)}`` blame totals into a ranked list.
-
- ``by="overlap"`` (the shipped ranking) puts the commit that wrote most of the
- buggy lines first; ``by="earliest"`` puts the oldest first, the other ranking
- the SZZ literature uses. Both fall through to the remaining keys, so the
- order is total and content-derived — no run-to-run drift.
- """
- cands = [InducingCandidate(sha, n, ts) for sha, (n, ts) in totals.items()]
- if by == "earliest":
- cands.sort(key=lambda c: (c.ts or 1 << 62, -c.lines, c.sha))
- else:
- cands.sort(key=lambda c: (-c.lines, c.ts, c.sha))
- return cands
-
-
-class SzzTracer:
- """Traces fix diffs to bug-introducing commits against one repository.
-
- Constructed with *get_repo*, a callable returning a gitpython ``Repo`` usable
- from the calling thread — the git indexer runs this pass across its worker
- pool and gitpython handles are not thread-safe on Windows. The candidate-diff
- cache is shared across threads behind a lock, because the same refactor
- commit shows up as a candidate for many files at once.
- """
-
- def __init__(self, get_repo: Callable[[], Any], *, refactor_aware: bool = True) -> None:
- self._get_repo = get_repo
- self._refactor_aware = refactor_aware
- self._diff_cache: dict[str, tuple[frozenset[str], frozenset[str]]] = {}
- self._lock = threading.Lock()
-
- # -- public ---------------------------------------------------------
-
- def trace_file(self, fix_sha: str, path: str, diff: FileDiff) -> list[InducingCandidate]:
- """Ranked inducing candidates for one file of one fix commit.
-
- Empty when there is nothing to blame: a hunk set whose removed lines are
- all comments or blanks, a file too large to say anything useful about,
- or a blame that failed (the file did not exist at ``fix^``).
- """
- if len(diff.removed) + len(diff.added) > MAX_FILE_CHANGED_LOC:
- return []
- # Deleting only comments and blank lines says nothing about who wrote the
- # bug; the cheapest precision win available without parsing the language.
- if diff.removed and all(_is_inert(line) for line in diff.removed):
- return []
-
- ranges = diff.old_ranges[:MAX_RANGES_PER_FILE]
- anchored = False
- if not ranges:
- ranges = _anchor_ranges(diff.insert_anchors)
- anchored = bool(ranges)
- if not ranges:
- return []
-
- # What each blamed line actually says, so the refactor test can ask about
- # the lines a given candidate owns rather than the whole file's.
- texts = _line_texts(diff)
-
- ignored: list[str] = []
- totals, owners = self._blame(fix_sha, path, ranges, ranges)
- if anchored and not totals:
- # An anchor pair can run one line past the end of the file, which
- # fails the whole call. Retry on the anchors alone.
- ranges = [(start, start) for start, _ in ranges]
- totals, owners = self._blame(fix_sha, path, ranges, ranges)
- for _ in range(MAX_REFACTOR_ROUNDS):
- if not self._refactor_aware or not totals:
- break
- movers = self._movers(totals, owners, texts, ignored)
- if not movers:
- break
- ignored.extend(movers)
- retraced, retraced_owners = self._blame(
- fix_sha,
- path,
- ranges,
- self._padded(fix_sha, path, ranges),
- ignored=ignored,
- detect_copies=True,
- )
- if not retraced:
- # Ignoring everything blamed can leave git with nothing to
- # attribute; the pre-walk answer beats an empty one.
- break
- totals, owners = retraced, retraced_owners
-
- return rank_candidates(totals)
-
- # -- git ------------------------------------------------------------
-
- def _blame(
- self,
- fix_sha: str,
- path: str,
- ranges: list[tuple[int, int]],
- query_ranges: list[tuple[int, int]],
- *,
- ignored: list[str] | None = None,
- detect_copies: bool = False,
- ) -> tuple[dict[str, tuple[int, int]], dict[int, str]]:
- """Blame *ranges* at ``fix^``: totals plus which commit owns which line.
-
- Returns ``({inducing_sha: (blamed_lines, committer_ts)}, {line: sha})``.
- The per-line map is what lets the refactor test ask a candidate about the
- lines it is actually blamed for instead of every line the fix deleted.
-
- *query_ranges* is what git is asked to blame and *ranges* is what counts:
- the refactor-aware pass widens the former for copy detection while
- keeping attribution scored over the fix's own lines only.
- """
- args: list[str] = ["-w", "-M", "--porcelain"]
- if detect_copies:
- # Twice: look for the line's origin in files the commit touched, and
- # then in the parent commit at large. Once is not enough for the
- # module-split case, which is the common one.
- args += ["-C", "-C"]
- for start, end in query_ranges:
- args += ["-L", f"{start},{end}"]
- for rev in ignored or ():
- args += ["--ignore-rev", rev]
- try:
- raw = self._get_repo().git.blame(*args, f"{fix_sha}^", "--", path)
- except Exception as exc:
- logger.debug("szz_blame_failed", fix=fix_sha[:12], path=path, error=str(exc))
- return {}, {}
-
- counted = {line for start, end in ranges for line in range(start, end + 1)}
- hits: dict[str, tuple[int, int]] = {}
- owners: dict[int, str] = {}
- current: str | None = None
- for line in raw.splitlines():
- match = _BLAME_HEADER_RE.match(line)
- if match is not None:
- current = match.group(1)
- blamed = int(match.group(2))
- count, ts = hits.get(current, (0, 0))
- # Context lines from a padded query still need their commit
- # header parsed (the committer-time follows it), but they must
- # not add to anyone's overlap.
- if blamed in counted:
- owners[blamed] = current
- count += 1
- hits[current] = (count, ts)
- elif current is not None and line.startswith("committer-time "):
- count, _ = hits[current]
- _, _, value = line.partition(" ")
- hits[current] = (count, _as_int(value))
- return {sha: v for sha, v in hits.items() if v[0] > 0}, owners
-
- def _padded(
- self, fix_sha: str, path: str, ranges: list[tuple[int, int]]
- ) -> list[tuple[int, int]]:
- """*ranges* widened by :data:`REFACTOR_PAD_LINES`, clamped to the file.
-
- The clamp matters: ``git blame -L`` fails outright on a range that runs
- past the end of the file, and losing the whole re-blame to an off-by-a-few
- would quietly turn refactor-awareness off on every short file.
- """
- limit = self._line_count(fix_sha, path)
- padded: list[tuple[int, int]] = []
- for start, end in ranges:
- low = max(1, start - REFACTOR_PAD_LINES)
- high = end + REFACTOR_PAD_LINES
- if limit:
- high = min(high, limit)
- padded.append((low, max(low, high)))
- return padded
-
- def _line_count(self, fix_sha: str, path: str) -> int:
- """Lines in *path* at ``fix^``, or 0 when it cannot be read."""
- try:
- blob = self._get_repo().git.show(f"{fix_sha}^:{path}")
- except Exception:
- return 0
- return blob.count("\n") + (0 if blob.endswith("\n") or not blob else 1)
-
- def _movers(
- self,
- totals: dict[str, tuple[int, int]],
- owners: dict[int, str],
- texts: dict[int, str],
- already_ignored: list[str],
- ) -> list[str]:
- """The top candidates that carried their blamed lines through, not wrote them."""
- movers = []
- for cand in rank_candidates(totals)[:MAX_REFACTOR_CHECKS]:
- if cand.sha in already_ignored:
- continue
- owned = {
- texts[line]
- for line, sha in owners.items()
- if sha == cand.sha and texts.get(line) and not _is_inert(texts[line])
- }
- if owned and self._relocated(cand.sha, owned):
- movers.append(cand.sha)
- return movers
-
- def _relocated(self, sha: str, owned: set[str]) -> bool:
- """Whether *sha* moved every one of the lines it is blamed for.
-
- A line that appears on BOTH sides of a commit's diff went out of one
- place and back into another unchanged: relocated, not authored. Requiring
- it of every line the candidate owns keeps the test conservative - a
- candidate that actually wrote any of the blamed lines keeps the blame,
- and the mitigation cannot run away with a commit that did real work.
-
- Judged across the commit's whole diff, not the file being blamed: the
- common refactor lifts a block out of one file and into another, which
- reads as a plain insertion in the receiving file and would look exactly
- like authorship if each side were judged alone.
- """
- removed, added = self._line_sets(sha)
- return all(line in removed and line in added for line in owned)
-
- def _line_sets(self, sha: str) -> tuple[frozenset[str], frozenset[str]]:
- """``(removed, added)`` line text for a commit's whole diff, cached.
-
- Cached per sha because one refactor commit turns up as a candidate for
- many files of many fixes, and its diff is the expensive part.
- """
- with self._lock:
- cached = self._diff_cache.get(sha)
- if cached is not None:
- return cached
-
- result = self._compute_line_sets(sha)
- with self._lock:
- self._diff_cache[sha] = result
- return result
-
- def _compute_line_sets(self, sha: str) -> tuple[frozenset[str], frozenset[str]]:
- # Imported here, not at module scope: ``analysis.change_risk`` imports
- # back into this package, so a top-level import would close a cycle.
- from ...analysis.changed_lines import parse_unified_diff
-
- empty: tuple[frozenset[str], frozenset[str]] = (frozenset(), frozenset())
- try:
- # ``--no-renames`` on purpose, the opposite of everywhere else here.
- # With rename detection on, git collapses a moved file into a rename
- # header and its lines appear on NEITHER side of the diff, so the
- # carried-through test can never see the very moves it exists to
- # catch. Off, a move is a full delete plus a full add, which is
- # exactly the shape the test reads.
- raw = self._get_repo().git.show("-U0", "--no-renames", "--no-color", "--format=", sha)
- except Exception as exc:
- logger.debug("szz_move_check_failed", sha=sha[:12], error=str(exc))
- return empty
- if not raw.strip() or len(raw) > MAX_MOVE_CHECK_BYTES:
- return empty
-
- files = parse_unified_diff(raw)
- return (
- frozenset(line.strip() for f in files.values() for line in f.removed if line.strip()),
- frozenset(line.strip() for f in files.values() for line in f.added if line.strip()),
- )
-
-
-def _line_texts(diff: FileDiff) -> dict[int, str]:
- """``{old-side line number: its text}`` for the lines the fix deleted.
-
- A ``-U0`` diff lists old-side ranges and removed lines in the same order, and
- a range of n old lines is exactly n removed lines, so the two zip positionally.
- That mapping is what lets a blame result be read back as content rather than
- line numbers.
- """
- texts: dict[int, str] = {}
- index = 0
- for start, end in diff.old_ranges:
- for line in range(start, end + 1):
- if index < len(diff.removed):
- texts[line] = diff.removed[index].strip()
- index += 1
- return texts
-
-
-def _anchor_ranges(anchors: list[int]) -> list[tuple[int, int]]:
- """Blame ranges for a fix that only ADDED lines.
-
- A fix that deletes nothing has no buggy line to blame, which on squash-merge
- repos is a large share of them (a fifth of repowise's, measured): the repair
- is a guard, an early return, a missing branch. The code it is guarding is
- still evidence, so blame straddles the insertion point - the line above and
- the line below - rather than giving up on the commit entirely.
-
- Attribution from an anchor is weaker than from a deleted line, and it stays
- distinguishable downstream: these rows persist an empty ``old_ranges_json``
- beside their candidates.
- """
- ranges = []
- for anchor in sorted(set(anchors))[:MAX_RANGES_PER_FILE]:
- start = max(1, anchor)
- ranges.append((start, start + 1))
- return ranges
-
-
-def _as_int(value: str) -> int:
- try:
- return int(value.strip())
- except ValueError:
- return 0
diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.py
index e5156b00b..f3ce78199 100644
--- a/packages/core/src/repowise/core/persistence/models.py
+++ b/packages/core/src/repowise/core/persistence/models.py
@@ -619,18 +619,20 @@ class FixEvent(Base):
# the classification is a property of the diff, not of one file in it.
shape_kind: Mapped[str] = mapped_column(String(16), nullable=False, default="code_fix")
- # Inclusive ``[[start, end], ...]`` spans on the PRE-fix file — the space
- # ``git blame fix^`` is keyed in. Empty for a pure insertion (nothing to blame).
+ # Inclusive ``[[start, end], ...]`` spans on the PRE-fix file. Empty for a
+ # pure insertion, which replaced nothing.
old_ranges_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]")
changed_loc: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
- # Ranked SZZ candidates, ``[{"sha", "lines", "ts"}, ...]``, most blamed lines
- # first. Stored ranked but complete, so a different ranking (earliest-commit,
- # say) stays a read-time re-sort. Empty for non-code_fix rows, for pure
- # insertions, and for fixes the tracer skipped (comment-only or oversized).
+ # DEAD, always ``"[]"``. Once held ranked SZZ blame candidates naming the
+ # commit that introduced each bug. Nothing ever read it: the surfaces that
+ # would have are cut, so the blame pass that filled it was removed too. The
+ # column stays because emptying it is free and dropping it is a table
+ # rebuild. Do NOT read this as "traced and found nothing".
inducing_shas_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]")
- # Per-bucket changed-line counts from the fix taxonomy. Empty until Phase 5.
+ # Per-bucket changed-line counts from the fix taxonomy. Always empty: the
+ # taxonomy classifier was measured and cut, never built.
taxonomy_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}")
# ``WikiSymbol.symbol_id``s whose CURRENT line span overlaps this row's
diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.py
index 4bd2320b7..4ed474cb3 100644
--- a/packages/core/src/repowise/core/pipeline/persist.py
+++ b/packages/core/src/repowise/core/pipeline/persist.py
@@ -694,7 +694,7 @@ async def persist_git(result: Any, session: Any, repo_id: str) -> None:
if fix_event_rows:
await upsert_fix_events_bulk(session, repo_id, fix_event_rows)
oldest_ts = getattr(summary, "fix_oldest_ts", 0)
- if oldest_ts and getattr(summary, "fix_events_traced", False):
+ if oldest_ts and getattr(summary, "fix_events_built", False):
from datetime import UTC, datetime
await prune_fix_events_before(session, repo_id, datetime.fromtimestamp(oldest_ts, tz=UTC))
diff --git a/tests/unit/ingestion/test_fix_events.py b/tests/unit/ingestion/test_fix_events.py
new file mode 100644
index 000000000..e03dcf8f9
--- /dev/null
+++ b/tests/unit/ingestion/test_fix_events.py
@@ -0,0 +1,181 @@
+"""``fix_events`` row building, on real temporary repositories.
+
+The things that go wrong here (a hunk header that predates the file, a rename,
+an empty diff) are git behaviours rather than ours, and they have no useful
+mock, so these build small repos and run the real thing.
+"""
+
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from repowise.core.ingestion.git_indexer import GitIndexer, GitIndexTier
+from repowise.core.ingestion.git_indexer.fix_events import build_fix_events
+from repowise.core.ingestion.git_indexer.prior_defects import collect_fix_commits
+
+
+def _write(repo, tmp_path, name, content, message):
+ (tmp_path / name).write_text(content)
+ repo.index.add([name])
+ return repo.index.commit(message).hexsha
+
+
+def _helpers(count: int = 6) -> str:
+ """A block big enough for git's copy detection to recognise when it moves.
+
+ Copy detection matches on content, so a two-line function is below its
+ threshold and a move of it is indistinguishable from authorship. Real
+ refactors move real blocks; the fixtures have to as well.
+ """
+ return "".join(
+ f"def helper_{i}(alpha, beta):\n return alpha + beta + {i}\n\n" for i in range(count)
+ )
+
+
+def _repo(tmp_path):
+ import git as gitpython
+
+ return gitpython.Repo.init(tmp_path)
+
+
+def _walk(tmp_path, paths):
+ import git as gitpython
+
+ return collect_fix_commits(gitpython.Repo(tmp_path), set(paths), as_of_ts=None)
+
+
+def _file_diff(fix_sha: str, tmp_path, path: str):
+ import git as gitpython
+
+ from repowise.core.analysis.changed_lines import parse_unified_diff
+
+ raw = gitpython.Repo(tmp_path).git.show("-U0", "--no-color", "--format=", fix_sha)
+ return parse_unified_diff(raw)[path]
+
+
+# ---------------------------------------------------------------------------
+# Row building
+# ---------------------------------------------------------------------------
+
+
+class TestBuildFixEvents:
+ def test_one_row_per_fix_commit_and_file(self, tmp_path) -> None:
+ import git as gitpython
+
+ repo = _repo(tmp_path)
+ _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
+ (tmp_path / "b.py").write_text("y = 1\n")
+ (tmp_path / "a.py").write_text("x = 2\n")
+ repo.index.add(["a.py", "b.py"])
+ repo.index.commit("fix: correct both")
+
+ walk = _walk(tmp_path, ["a.py", "b.py"])
+ rows = build_fix_events(walk)
+
+ assert [r["file_path"] for r in rows] == ["a.py", "b.py"]
+ assert {r["shape_kind"] for r in rows} == {"code_fix"}
+ assert all(r["committed_at"] is not None for r in rows)
+ # a.py replaced a line, so it carries an old-side range; b.py is new.
+ by_path = {r["file_path"]: r for r in rows}
+ assert json.loads(by_path["a.py"]["old_ranges_json"]) == [[1, 1]]
+ assert json.loads(by_path["b.py"]["old_ranges_json"]) == []
+ assert by_path["a.py"]["changed_loc"] == 2
+
+ def test_non_code_fixes_keep_their_row_but_are_never_blamed(self, tmp_path) -> None:
+ import git as gitpython
+
+ repo = _repo(tmp_path)
+ _write(repo, tmp_path, "README.md", "hello\n", "docs: add readme")
+ _write(repo, tmp_path, "README.md", "hello there\n", "fix: wrong wording")
+
+ walk = _walk(tmp_path, ["README.md"])
+ rows = build_fix_events(walk)
+
+ assert len(rows) == 1
+ assert rows[0]["shape_kind"] == "doc_only"
+ assert rows[0]["inducing_shas_json"] == "[]"
+
+ def test_rows_are_ordered_deterministically(self, tmp_path) -> None:
+ import git as gitpython
+
+ repo = _repo(tmp_path)
+ for name in ("c.py", "b.py", "a.py"):
+ _write(repo, tmp_path, name, "x = 1\n", f"feat: add {name}")
+ for name in ("c.py", "b.py", "a.py"):
+ (tmp_path / name).write_text("x = 2\n")
+ repo.index.add(["a.py", "b.py", "c.py"])
+ repo.index.commit("fix: bump all three")
+
+ rows = build_fix_events(_walk(tmp_path, ["a.py", "b.py", "c.py"]))
+ assert rows == sorted(rows, key=lambda r: (r["fix_sha"], r["file_path"]))
+
+
+# ---------------------------------------------------------------------------
+# Full index + incremental capture
+# ---------------------------------------------------------------------------
+
+
+async def test_index_repo_emits_fix_events_and_window_boundary(tmp_path) -> None:
+ repo = _repo(tmp_path)
+ _write(repo, tmp_path, "a.py", "def f():\n return None\n", "feat: add f")
+ _write(repo, tmp_path, "a.py", "def f():\n return 0\n", "fix: bad return")
+
+ summary, _ = await GitIndexer(tmp_path, tier=GitIndexTier.FULL).index_repo("repo1")
+
+ assert len(summary.fix_event_rows) == 1
+ row = summary.fix_event_rows[0]
+ assert row["file_path"] == "a.py"
+ # The fix replaced the return line, so the row carries the range it replaced.
+ assert json.loads(row["old_ranges_json"]) == [[2, 2]]
+ assert summary.fix_oldest_ts > 0
+ assert summary.fix_events_built is True
+
+
+async def test_capture_new_fix_events_skips_known_commits(tmp_path) -> None:
+ """The update path traces only the fix commits it has not already stored.
+
+ Skipping by sha rather than by timestamp is what makes an update converge on
+ a full index: this asserts the known commit is gone and the new one arrives,
+ with the window boundary still reported so the caller can prune.
+ """
+ repo = _repo(tmp_path)
+ _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
+ first = _write(repo, tmp_path, "a.py", "x = 2\n", "fix: first")
+ second = _write(repo, tmp_path, "a.py", "x = 3\n", "fix: second")
+
+ indexer = GitIndexer(tmp_path, tier=GitIndexTier.FULL)
+
+ everything, oldest, tracked = indexer.capture_new_fix_events()
+ assert {r["fix_sha"] for r in everything} == {first, second}
+ assert oldest > 0
+ assert tracked == {"a.py"}
+
+ new_only, oldest_again, _ = indexer.capture_new_fix_events(known_shas={first})
+ assert {r["fix_sha"] for r in new_only} == {second}
+ # The cutoff is taken before the skip, so narrowing the trace must not move it.
+ assert oldest_again == oldest
+
+
+async def test_update_capture_matches_a_full_index(tmp_path) -> None:
+ """Seed at an old commit, step forward, and land on the full-index answer.
+
+ The miniature of ``validate_p2_incremental.py``: whatever route the rows
+ take, the set has to be the one a fresh index at this HEAD would produce.
+ """
+ repo = _repo(tmp_path)
+ _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
+ _write(repo, tmp_path, "a.py", "x = 2\n", "fix: first")
+ indexer = GitIndexer(tmp_path, tier=GitIndexTier.FULL)
+ seeded, _, _ = indexer.capture_new_fix_events()
+
+ _write(repo, tmp_path, "a.py", "x = 3\n", "fix: second")
+ incremental, _, _ = indexer.capture_new_fix_events(known_shas={r["fix_sha"] for r in seeded})
+ merged = sorted(seeded + incremental, key=lambda r: (r["fix_sha"], r["file_path"]))
+
+ summary, _ = await GitIndexer(tmp_path, tier=GitIndexTier.FULL).index_repo("repo1")
+ assert merged == summary.fix_event_rows
diff --git a/tests/unit/ingestion/test_fix_events_szz.py b/tests/unit/ingestion/test_fix_events_szz.py
deleted file mode 100644
index eadf7ce39..000000000
--- a/tests/unit/ingestion/test_fix_events_szz.py
+++ /dev/null
@@ -1,343 +0,0 @@
-"""SZZ tracing and ``fix_events`` row building, on real temporary repositories.
-
-Blame is the whole point of this pass, and blame has no useful mock: the things
-that go wrong (``-M`` following a move, ``--ignore-rev`` walking through a
-refactor, a range that predates the file) are git behaviours, not ours. So these
-build small repos and run the real thing.
-"""
-
-from __future__ import annotations
-
-import json
-
-import pytest
-
-from repowise.core.ingestion.git_indexer import GitIndexer, GitIndexTier
-from repowise.core.ingestion.git_indexer.fix_events import build_fix_events
-from repowise.core.ingestion.git_indexer.prior_defects import collect_fix_commits
-from repowise.core.ingestion.git_indexer.szz import SzzTracer, rank_candidates
-
-
-def _write(repo, tmp_path, name, content, message):
- (tmp_path / name).write_text(content)
- repo.index.add([name])
- return repo.index.commit(message).hexsha
-
-
-def _helpers(count: int = 6) -> str:
- """A block big enough for git's copy detection to recognise when it moves.
-
- Copy detection matches on content, so a two-line function is below its
- threshold and a move of it is indistinguishable from authorship. Real
- refactors move real blocks; the fixtures have to as well.
- """
- return "".join(
- f"def helper_{i}(alpha, beta):\n return alpha + beta + {i}\n\n" for i in range(count)
- )
-
-
-def _repo(tmp_path):
- import git as gitpython
-
- return gitpython.Repo.init(tmp_path)
-
-
-def _tracer(tmp_path, *, refactor_aware: bool = True) -> SzzTracer:
- import git as gitpython
-
- return SzzTracer(lambda: gitpython.Repo(tmp_path), refactor_aware=refactor_aware)
-
-
-def _walk(tmp_path, paths):
- import git as gitpython
-
- return collect_fix_commits(gitpython.Repo(tmp_path), set(paths), as_of_ts=None)
-
-
-def _file_diff(fix_sha: str, tmp_path, path: str):
- import git as gitpython
-
- from repowise.core.analysis.changed_lines import parse_unified_diff
-
- raw = gitpython.Repo(tmp_path).git.show("-U0", "--no-color", "--format=", fix_sha)
- return parse_unified_diff(raw)[path]
-
-
-# ---------------------------------------------------------------------------
-# The blame itself
-# ---------------------------------------------------------------------------
-
-
-class TestSzzTracer:
- def test_blames_the_commit_that_wrote_the_fixed_line(self, tmp_path) -> None:
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "def f():\n return 1\n", "feat: add f")
- culprit = _write(
- repo, tmp_path, "a.py", "def f():\n return None\n", "feat: return nothing"
- )
- fix = _write(repo, tmp_path, "a.py", "def f():\n return 0\n", "fix: bad return")
-
- cands = _tracer(tmp_path).trace_file(fix, "a.py", _file_diff(fix, tmp_path, "a.py"))
-
- assert cands, "expected an inducing candidate"
- assert cands[0].sha == culprit
- assert cands[0].lines == 1
- assert cands[0].ts > 0
-
- def test_pure_insertion_is_blamed_at_its_anchor(self, tmp_path) -> None:
- """A fix that only ADDS lines has no deleted line, so blame the anchor.
-
- A guard or an early return deletes nothing, which on squash-merge repos
- is a fifth of all fixes. The code it was inserted next to is still the
- evidence available, and the row stays distinguishable downstream by its
- empty ``old_ranges``.
- """
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "def f():\n return 1\n", "feat: add f")
- anchor_author = _write(
- repo, tmp_path, "a.py", "def f():\n x = 2\n return 1\n", "feat: add x"
- )
- fix = _write(
- repo, tmp_path, "a.py", "def f():\n x = 2\n y = 3\n return 1\n", "fix: add y"
- )
-
- diff = _file_diff(fix, tmp_path, "a.py")
- assert diff.old_ranges == []
- assert diff.insert_anchors == [2]
- # Both anchor lines are blamed, one each, so which one ranks first is a
- # tie-break rather than a claim. The contract is that the code the fix
- # was inserted next to shows up at all.
- cands = _tracer(tmp_path).trace_file(fix, "a.py", diff)
- assert anchor_author in {c.sha for c in cands}
-
- def test_a_brand_new_file_has_nothing_to_blame(self, tmp_path) -> None:
- """The anchor fallback cannot invent history that does not exist yet."""
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
- fix = _write(repo, tmp_path, "b.py", "y = 2\n", "fix: add b")
-
- assert _tracer(tmp_path).trace_file(fix, "b.py", _file_diff(fix, tmp_path, "b.py")) == []
-
- def test_comment_only_deletions_are_skipped(self, tmp_path) -> None:
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "# note\nx = 1\n", "feat: add a")
- fix = _write(repo, tmp_path, "a.py", "# fixed note\nx = 1\n", "fix: comment")
-
- assert _tracer(tmp_path).trace_file(fix, "a.py", _file_diff(fix, tmp_path, "a.py")) == []
-
- def test_oversized_file_diff_is_skipped(self, tmp_path) -> None:
- """A mass rewrite blames as 'everyone, everywhere' and is left untraced."""
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "".join(f"x{i} = {i}\n" for i in range(400)), "feat: add a")
- fix = _write(
- repo, tmp_path, "a.py", "".join(f"y{i} = {i}\n" for i in range(400)), "fix: rewrite"
- )
-
- assert _tracer(tmp_path).trace_file(fix, "a.py", _file_diff(fix, tmp_path, "a.py")) == []
-
- def test_refactor_aware_blame_walks_through_a_cross_file_move(self, tmp_path) -> None:
- """A commit that only relocated the line must not be named as its author.
-
- This is the failure mode the frozen judgment set was made of: every one
- of its 14 wrong calls was a behaviour-preserving refactor that inherited
- the buggy line by moving it. ``-M`` covers moves inside one file; lifting
- a block from one file into another is what it cannot follow, so that is
- what this builds.
- """
- block = _helpers()
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "old.py", "keep = 1\n", "feat: add old")
- culprit = _write(repo, tmp_path, "old.py", "keep = 1\n\n\n" + block, "feat: add helpers")
- # The block moves to new.py verbatim. Across the commit, nothing is written.
- (tmp_path / "old.py").write_text("keep = 1\n")
- (tmp_path / "new.py").write_text(block)
- repo.index.add(["old.py", "new.py"])
- mover = repo.index.commit("refactor: split out the helpers").hexsha
- fix = _write(
- repo,
- tmp_path,
- "new.py",
- block.replace("alpha + beta + 3\n", "alpha + beta + 33\n"),
- "fix: off by one",
- )
-
- diff = _file_diff(fix, tmp_path, "new.py")
- naive = _tracer(tmp_path, refactor_aware=False).trace_file(fix, "new.py", diff)
- aware = _tracer(tmp_path, refactor_aware=True).trace_file(fix, "new.py", diff)
-
- assert naive[0].sha == mover, "baseline should blame the refactor"
- assert aware[0].sha == culprit, "refactor-aware blame should reach the real author"
- assert all(c.sha != mover for c in aware)
-
- def test_a_commit_that_edits_while_moving_keeps_the_blame(self, tmp_path) -> None:
- """The multiset test is the guard: change a line and you own it."""
- block = _helpers()
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "old.py", "keep = 1\n", "feat: add old")
- _write(repo, tmp_path, "old.py", "keep = 1\n\n\n" + block, "feat: add helpers")
- (tmp_path / "old.py").write_text("keep = 1\n")
- (tmp_path / "new.py").write_text(block.replace("beta + 3\n", "beta + 99\n"))
- repo.index.add(["old.py", "new.py"])
- editor = repo.index.commit("refactor: split out the helpers, and tweak one").hexsha
- fix = _write(
- repo, tmp_path, "new.py", block.replace("beta + 3\n", "beta + 4\n"), "fix: off by one"
- )
-
- aware = _tracer(tmp_path).trace_file(fix, "new.py", _file_diff(fix, tmp_path, "new.py"))
- assert aware[0].sha == editor
-
- def test_missing_file_at_parent_yields_no_candidates(self, tmp_path) -> None:
- """A fix commit that also creates the file has nothing to blame at ``fix^``."""
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
- fix = _write(repo, tmp_path, "b.py", "y = 2\n", "fix: add b")
-
- import git as gitpython
-
- from repowise.core.analysis.changed_lines import FileDiff
-
- gitpython.Repo(tmp_path) # sanity: repo is readable
- fabricated = FileDiff(path="b.py", old_ranges=[(1, 1)], removed=["y = 1"])
- assert _tracer(tmp_path).trace_file(fix, "b.py", fabricated) == []
-
-
-class TestRankCandidates:
- def test_overlap_ranking_prefers_the_most_blamed_lines(self) -> None:
- ranked = rank_candidates({"a" * 40: (1, 100), "b" * 40: (5, 200)})
- assert [c.sha[0] for c in ranked] == ["b", "a"]
-
- def test_earliest_ranking_prefers_the_oldest_commit(self) -> None:
- ranked = rank_candidates({"a" * 40: (1, 100), "b" * 40: (5, 200)}, by="earliest")
- assert [c.sha[0] for c in ranked] == ["a", "b"]
-
- def test_ranking_is_total_and_deterministic(self) -> None:
- """Equal overlap and equal time still order the same way every run."""
- totals = {"b" * 40: (2, 50), "a" * 40: (2, 50)}
- assert rank_candidates(totals) == rank_candidates(dict(reversed(list(totals.items()))))
-
-
-# ---------------------------------------------------------------------------
-# Row building
-# ---------------------------------------------------------------------------
-
-
-class TestBuildFixEvents:
- def test_one_row_per_fix_commit_and_file(self, tmp_path) -> None:
- import git as gitpython
-
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
- (tmp_path / "b.py").write_text("y = 1\n")
- (tmp_path / "a.py").write_text("x = 2\n")
- repo.index.add(["a.py", "b.py"])
- repo.index.commit("fix: correct both")
-
- walk = _walk(tmp_path, ["a.py", "b.py"])
- rows = build_fix_events(walk, lambda: gitpython.Repo(tmp_path))
-
- assert [r["file_path"] for r in rows] == ["a.py", "b.py"]
- assert {r["shape_kind"] for r in rows} == {"code_fix"}
- assert all(r["committed_at"] is not None for r in rows)
- # a.py replaced a line, so it carries an old-side range; b.py is new.
- by_path = {r["file_path"]: r for r in rows}
- assert json.loads(by_path["a.py"]["old_ranges_json"]) == [[1, 1]]
- assert json.loads(by_path["b.py"]["old_ranges_json"]) == []
- assert by_path["a.py"]["changed_loc"] == 2
-
- def test_non_code_fixes_keep_their_row_but_are_never_blamed(self, tmp_path) -> None:
- import git as gitpython
-
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "README.md", "hello\n", "docs: add readme")
- _write(repo, tmp_path, "README.md", "hello there\n", "fix: wrong wording")
-
- walk = _walk(tmp_path, ["README.md"])
- rows = build_fix_events(walk, lambda: gitpython.Repo(tmp_path))
-
- assert len(rows) == 1
- assert rows[0]["shape_kind"] == "doc_only"
- assert rows[0]["inducing_shas_json"] == "[]"
-
- def test_rows_are_ordered_deterministically(self, tmp_path) -> None:
- import git as gitpython
-
- repo = _repo(tmp_path)
- for name in ("c.py", "b.py", "a.py"):
- _write(repo, tmp_path, name, "x = 1\n", f"feat: add {name}")
- for name in ("c.py", "b.py", "a.py"):
- (tmp_path / name).write_text("x = 2\n")
- repo.index.add(["a.py", "b.py", "c.py"])
- repo.index.commit("fix: bump all three")
-
- rows = build_fix_events(
- _walk(tmp_path, ["a.py", "b.py", "c.py"]), lambda: gitpython.Repo(tmp_path)
- )
- assert rows == sorted(rows, key=lambda r: (r["fix_sha"], r["file_path"]))
-
-
-# ---------------------------------------------------------------------------
-# Full index + incremental capture
-# ---------------------------------------------------------------------------
-
-
-async def test_index_repo_emits_fix_events_and_window_boundary(tmp_path) -> None:
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "def f():\n return None\n", "feat: add f")
- _write(repo, tmp_path, "a.py", "def f():\n return 0\n", "fix: bad return")
-
- summary, _ = await GitIndexer(tmp_path, tier=GitIndexTier.FULL).index_repo("repo1")
-
- assert len(summary.fix_event_rows) == 1
- row = summary.fix_event_rows[0]
- assert row["file_path"] == "a.py"
- assert json.loads(row["inducing_shas_json"])[0]["lines"] == 1
- assert summary.fix_oldest_ts > 0
- assert summary.fix_events_traced is True
-
-
-async def test_capture_new_fix_events_skips_known_commits(tmp_path) -> None:
- """The update path traces only the fix commits it has not already stored.
-
- Skipping by sha rather than by timestamp is what makes an update converge on
- a full index: this asserts the known commit is gone and the new one arrives,
- with the window boundary still reported so the caller can prune.
- """
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
- first = _write(repo, tmp_path, "a.py", "x = 2\n", "fix: first")
- second = _write(repo, tmp_path, "a.py", "x = 3\n", "fix: second")
-
- indexer = GitIndexer(tmp_path, tier=GitIndexTier.FULL)
-
- everything, oldest, tracked = indexer.capture_new_fix_events()
- assert {r["fix_sha"] for r in everything} == {first, second}
- assert oldest > 0
- assert tracked == {"a.py"}
-
- new_only, oldest_again, _ = indexer.capture_new_fix_events(known_shas={first})
- assert {r["fix_sha"] for r in new_only} == {second}
- # The cutoff is taken before the skip, so narrowing the trace must not move it.
- assert oldest_again == oldest
-
-
-async def test_update_capture_matches_a_full_index(tmp_path) -> None:
- """Seed at an old commit, step forward, and land on the full-index answer.
-
- The miniature of ``validate_p2_incremental.py``: whatever route the rows
- take, the set has to be the one a fresh index at this HEAD would produce.
- """
- repo = _repo(tmp_path)
- _write(repo, tmp_path, "a.py", "x = 1\n", "feat: add a")
- _write(repo, tmp_path, "a.py", "x = 2\n", "fix: first")
- indexer = GitIndexer(tmp_path, tier=GitIndexTier.FULL)
- seeded, _, _ = indexer.capture_new_fix_events()
-
- _write(repo, tmp_path, "a.py", "x = 3\n", "fix: second")
- incremental, _, _ = indexer.capture_new_fix_events(known_shas={r["fix_sha"] for r in seeded})
- merged = sorted(seeded + incremental, key=lambda r: (r["fix_sha"], r["file_path"]))
-
- summary, _ = await GitIndexer(tmp_path, tier=GitIndexTier.FULL).index_repo("repo1")
- assert merged == summary.fix_event_rows