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
refactor(core): split per-file history indexing into named steps (#2607)
Raghav Chamadiyaa06336b45914
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py b/packages/core/src/repowise/core/ingestion/git_indexer/file_history.pyindex 7469229e6..4bc07d17e 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py@@ -1,11 +1,8 @@-"""Per-file git history indexing — the ESSENTIAL baseline plus optional-FULL-tier blame enrichment.--``index_file`` parses a single file's commit history (from a precomputed-commit index when available, else a per-file ``git log``) and derives commit-counts, authorship, line churn, the temporal hotspot score, and significant-commits. The expensive ``git blame`` ownership pass is gated by-*include_blame* so the ESSENTIAL tier can skip it.+"""Per-file git history indexing: the ESSENTIAL baseline plus FULL-tier blame.++``index_file`` derives commit counts, authorship, churn, the temporal hotspot+score and significant commits from a precomputed commit index or a per-file+``git log``. *include_blame* gates the expensive ``git blame`` pass."""from __future__ import annotations@@ -13,6 +10,7 @@ from __future__ import annotationsimport jsonimport mathfrom collections import Counter+from dataclasses import dataclass, fieldfrom datetime import UTC, datetime, timedeltafrom pathlib import Pathfrom typing import Any@@ -49,10 +47,9 @@ from .records import (def _body_carries_decision(subject: str, body: str) -> bool:"""Whether a commit body is worth retaining for decision mining.- The body is stored once per file the commit touched, so retaining it- indiscriminately bloats the index. Keep it only when the subject/body shows- decision intent — a decision-signal keyword or a PR-description marker — i.e.- exactly the commits the PR / git-archaeology miners would consume.+ The body is stored once per file the commit touched, so it is kept only+ when the subject or body carries a decision-signal keyword or a+ PR-description marker: the commits the PR and git-archaeology miners read."""if not body:return False@@ -66,15 +63,12 @@ logger = structlog.get_logger(__name__)__all__ = ["DECAY_REFRESH_KEYS", "index_file", "new_meta"]-# The anchor-dependent window/decay fields — the only git-metadata columns that-# *recover* as the repo's newest-commit anchor advances (see issue #728). An-# incremental update recomputes just these for idle (unchanged) files off the-# repo-wide walk and persists a decay-only partial row, leaving ownership / age-# / authorship (which need full history and are only correct from the init-# walk) untouched. ``co_change_partners_json`` / ``co_change_partner_count`` /-# ``co_change_mass`` / ``change_entropy`` /-# ``prior_defect_*`` are merged onto the metadata after the per-file pass, so-# they refresh together with the window churn fields ``index_file`` computes.+# The anchor-dependent window/decay fields: the only git-metadata columns that+# recover as the repo's newest-commit anchor advances. An incremental update+# recomputes just these for idle files off the repo-wide walk and persists a+# decay-only partial row; ownership, age and authorship need full history and+# come only from the init walk. The co-change, entropy and prior-defect keys are+# merged onto the metadata after the per-file pass, so they refresh together.DECAY_REFRESH_KEYS = ("commit_count_90d","commit_count_30d",@@ -112,7 +106,6 @@ def new_meta(file_path: str) -> dict[str, Any]:"is_stable": False,"churn_percentile": 0.0,"age_days": 0,- # Phase 2 fields"lines_added_90d": 0,"lines_deleted_90d": 0,"avg_commit_size": 0.0,@@ -120,41 +113,32 @@ def new_meta(file_path: str) -> dict[str, Any]:"recent_owner_commit_pct": None,"bus_factor": 0,"contributor_count": 0,- # Phase 3 fields"original_path": None,"merge_commit_count_90d": 0,- # Prior-defect history: count of bug-fix commits touching this file in- # the trailing PRIOR_DEFECT_WINDOW_DAYS window (anchored to as_of_ts when- # set, so T0 benchmark scoring stays leakage-free). Consumed by the- # ``prior_defect`` health biomarker; mirrors the benchmark's- # prior-defects baseline definition (product == benchmark). The raw- # variant is the same walk before fix-shape filtering (fix_shape.py).+ # Bug-fix commits touching this file in the trailing+ # PRIOR_DEFECT_WINDOW_DAYS, anchored to as_of_ts; read by ``prior_defect``.+ # The raw count is the same walk before fix-shape filtering."prior_defect_count": 0,"prior_defect_raw_count": 0,# Repo-relative rank of prior_defect_count; see enrich.compute_percentiles."prior_defect_pct": 0.0,- # Agent provenance rollup: how much of this file's indexed history is- # agent-attributed (local channels only — see agent_provenance module).- # agent_authored_pct stays None when the file has no commits at all.+ # Share of this file's indexed history that is agent-attributed (local+ # channels only); agent_authored_pct stays None for a file with no commits."agent_commit_count": 0,"agent_authored_pct": None,"agent_tier_counts_json": "{}",- # Line-level agent share (agent-trace ranges[]): distinct AI-written- # lines + {model_id: line_count}. Merged in by the orchestrator from the- # repo-wide trace index (path-keyed, like co-change/prior-defects), so- # these stay at the default unless the repo ships .agent-trace/.+ # Distinct agent-written lines and {model_id: line_count} from the+ # repo-wide trace index, merged in by the orchestrator; default unless+ # the repo ships .agent-trace/."agent_line_count": 0,"agent_line_model_json": "{}",- # Temporal hotspot score (exponentially decayed churn)"temporal_hotspot_score": 0.0,- # Change entropy (Hassan HCM) — populated repo-wide by the co-change- # walk, percentile by enrich.compute_percentiles. Default 0.0 leaves- # the signal silent on the ESSENTIAL tier / files that never co-changed.+ # Change entropy (Hassan HCM) from the repo-wide co-change walk. 0.0+ # keeps the signal silent on the ESSENTIAL tier and never-co-changed files."change_entropy": 0.0,"change_entropy_pct": 0.0,- # Co-change breadth over every partner the repo-wide walk found, not- # the length of the truncated partner list. Defaults leave- # ``co_change_scatter`` silent when the walk did not run.+ # Co-change breadth over every partner found, not the truncated list's+ # length; defaults keep ``co_change_scatter`` silent without the walk."co_change_partner_count": 0,"co_change_mass": 0.0,"co_change_scatter_pct": 0.0,@@ -176,41 +160,20 @@ def _parse_per_file_log(Returns ``(commits, original_path)``. Only used when no precomputed commitindex is available (i.e. when rename-tracking is on)."""- log_args: list[str] = []- if follow_renames:- log_args.append("--follow")- else:- # Match the recent/deep repo-wide lanes. Without one shared non-merge- # contract, a file moving between fallback and shared sampling could- # lose a retained merge even while ``commit_limit`` increased.- log_args.append("--no-merges")- # A pathspec normally restricts numstat to this file, which represents- # a rename differently from the repo-wide lanes. Full-diff keeps commit- # selection per-file but makes churn and changed-path provenance use- # the same complete diff as the recent/deep walks.- log_args.append("--full-diff")- log_args += [- f"-{commit_limit}",- "--numstat",- f"--format={_LOG_FORMAT}",- "--",- file_path,- ]try:- raw = repo.git.log(*log_args)+ raw = repo.git.log(*_per_file_log_args(file_path, commit_limit, follow_renames))except Exception:return None, Noneif not raw.strip():return [], None- known_paths: set[str] = {file_path}- orig_path: str | None = None- if follow_renames:- orig_path = detect_original_path(repo, file_path, commit_limit)- if orig_path:- known_paths.add(orig_path)-+ known_paths, orig_path = _known_paths(repo, file_path, commit_limit, follow_renames)+ labeler = (+ _CommitLabeler(provenance_classifier, note_agents, trace_index)+ if provenance_classifier is not None+ else None+ )# Split on the NUL record separator rather than newlines so multi-line# commit bodies (``%b``) don't get mistaken for numstat rows.commits: list[_CommitRec] = []@@ -231,44 +194,94 @@ def _parse_per_file_log(body=header["body"],)commits.append(current)- changed_paths: set[str] = set()- for line in numstat_lines:- numstat_parts = line.split("\t")- if len(numstat_parts) < 3:- continue- stat_path = numstat_parts[2]- match_path = stat_path- if "=>" in stat_path:- _old, _new = _extract_rename_paths(stat_path, known_paths)- match_path = _new or stat_path- changed_paths.add(match_path)- if match_path in known_paths or match_path == file_path:- try:- current.added += int(numstat_parts[0]) if numstat_parts[0] != "-" else 0- current.deleted += int(numstat_parts[1]) if numstat_parts[1] != "-" else 0- except ValueError:- pass- if provenance_classifier is not None:- trace_hit = (- trace_index.resolve(header["sha"], header["parents"], changed_paths)- if trace_index- else None- )- prov = provenance_classifier.classify(- header["author_name"],- header["author_email"],- header["committer_name"],- header["committer_email"],- f"{header['subject']}\n{header['body']}",- note_agent=(note_agents or {}).get(header["sha"]),- trace_agent=trace_hit[0] if trace_hit else None,- trace_confidence=trace_hit[1] if trace_hit else "high",- )+ changed_paths = _add_own_churn(current, numstat_lines, known_paths)+ if labeler is not None:+ prov = labeler.label(header, changed_paths)current.agent = prov.agentcurrent.agent_tier = prov.autonomy_tierreturn commits, orig_path+def _per_file_log_args(file_path: str, commit_limit: int, follow_renames: bool) -> list[str]:+ walk = [f"-{commit_limit}", "--numstat", f"--format={_LOG_FORMAT}", "--", file_path]+ if follow_renames:+ return ["--follow", *walk]+ # The repo-wide lanes' contract: no merges, and the full diff for churn and+ # changed paths, so a file moving between lanes keeps the same history.+ return ["--no-merges", "--full-diff", *walk]+++def _known_paths(+ repo: Any, file_path: str, commit_limit: int, follow_renames: bool+) -> tuple[set[str], str | None]:+ """The names this file's churn is matched under, and its original path under ``--follow``."""+ known_paths: set[str] = {file_path}+ if not follow_renames:+ return known_paths, None+ orig_path = detect_original_path(repo, file_path, commit_limit)+ if orig_path:+ known_paths.add(orig_path)+ return known_paths, orig_path+++def _add_own_churn(+ current: _CommitRec, numstat_lines: list[str], known_paths: set[str]+) -> set[str]:+ """Add the churn of rows naming this file to *current*; return every changed path.++ A rename row adds both of its names to *known_paths*.+ """+ changed_paths: set[str] = set()+ for line in numstat_lines:+ numstat_parts = line.split("\t")+ if len(numstat_parts) < 3:+ continue+ match_path = numstat_parts[2]+ if "=>" in match_path:+ _old, _new = _extract_rename_paths(match_path, known_paths)+ match_path = _new or match_path+ changed_paths.add(match_path)+ if match_path in known_paths:+ _add_row_churn(current, numstat_parts)+ return changed_paths+++def _add_row_churn(current: _CommitRec, numstat_parts: list[str]) -> None:+ try:+ current.added += int(numstat_parts[0]) if numstat_parts[0] != "-" else 0+ current.deleted += int(numstat_parts[1]) if numstat_parts[1] != "-" else 0+ except ValueError:+ pass+++class _CommitLabeler:+ """Labels a commit's agent provenance from its header and changed paths."""++ def __init__(+ self, classifier: Any, note_agents: dict[str, str] | None, trace_index: Any | None+ ) -> None:+ self._classifier = classifier+ self._note_agents = note_agents or {}+ self._trace_index = trace_index++ def label(self, header: dict, changed_paths: set[str]) -> Any:+ trace_hit = (+ self._trace_index.resolve(header["sha"], header["parents"], changed_paths)+ if self._trace_index+ else None+ )+ return self._classifier.classify(+ header["author_name"],+ header["author_email"],+ header["committer_name"],+ header["committer_email"],+ f"{header['subject']}\n{header['body']}",+ note_agent=self._note_agents.get(header["sha"]),+ trace_agent=trace_hit[0] if trace_hit else None,+ trace_confidence=trace_hit[1] if trace_hit else "high",+ )++def index_file(repo: Any,file_path: str,@@ -285,23 +298,16 @@ def index_file() -> dict:"""Index a single file's git history. Runs in executor.- When *precomputed_commits* is provided (the default when called from the- batched full-repo path), the per-file ``git log`` subprocess is skipped- entirely — eliminating the dominant process-spawn cost on large repos.-- *include_blame* gates the FULL-tier ``git blame`` ownership pass; the- ESSENTIAL tier sets it False and falls back to commit-author ownership.-- *as_of_ts* anchors the recency windows (90d/30d, age, temporal decay) to a- fixed reference time — the timestamp of the repo's most recent commit,- supplied by the orchestrator. Anchoring to the repo's own HEAD rather than- wall-clock ``now()`` makes indexing **deterministic** (re-indexing the same- commit later yields identical windows) and **correct for historical- checkouts** (scoring a worktree at an old commit measures the 90 days before- *that* commit, not an empty window 6 months in its future). Falls back to- ``now()`` when not supplied.+ With *precomputed_commits* (the batched full-repo path) no per-file+ ``git log`` runs. *include_blame* gates the FULL-tier ``git blame``+ ownership pass; the ESSENTIAL tier falls back to commit-author ownership.++ *as_of_ts* anchors the 90d/30d windows, age and temporal decay to the+ repo's newest commit, so re-indexing the same commit yields the same+ windows and a historical checkout measures the 90 days before it. Falls+ back to ``now()``."""- now = datetime.fromtimestamp(as_of_ts, tz=UTC) if as_of_ts is not None else datetime.now(UTC)+ now = _window_anchor(as_of_ts)ninety_days_ago_ts = (now - timedelta(days=90)).timestamp()thirty_days_ago_ts = (now - timedelta(days=30)).timestamp()@@ -329,212 +335,251 @@ def index_file(meta["commit_count_total"] = len(commits)meta["commit_count_capped"] = len(commits) >= commit_limit+ # Each step writes its fields in turn, so a failure keeps the fields+ # the earlier steps already wrote.try:- timestamps = [c.ts for c in commits if c.ts > 0]- if timestamps:- first_ts = min(timestamps)- last_ts = max(timestamps)- meta["first_commit_at"] = datetime.fromtimestamp(first_ts, tz=UTC)- meta["last_commit_at"] = datetime.fromtimestamp(last_ts, tz=UTC)- meta["age_days"] = (now - datetime.fromtimestamp(first_ts, tz=UTC)).days-- author_counts: Counter[str] = Counter()- author_emails: dict[str, str] = {}- recent_author_counts: Counter[str] = Counter()- # Per-author commit timestamps so a contributor's "last touched" reflects- # *their own* last commit to this file — not the file's last commit by- # anyone (which would credit you whenever a co-owner touches a shared- # file). Consumed downstream by the owner-profile aggregator.- author_last_ts: dict[str, int] = {}- author_first_ts: dict[str, int] = {}+ _add_span(meta, commits, now)+ authors = _Authors.tally(commits, ninety_days_ago_ts)+ _add_windows(meta, commits, ninety_days_ago_ts, thirty_days_ago_ts)+ _add_agent_rollup(meta, commits)+ meta["temporal_hotspot_score"] = _temporal_hotspot_score(commits, now)+ _add_ownership(meta, authors)+ if include_blame:+ _add_blame_ownership(meta, repo, repo_path, now)+ _add_commit_messages(meta, commits)+ # Only the per-file ``--follow`` walk reports an original path.+ if orig_path:+ meta["original_path"] = orig_path+ meta["is_stable"] = _is_stable(meta)+ except Exception:+ logger.debug("git_indexer_partial_failure", file_path=file_path, exc_info=True)++ return meta++def _window_anchor(as_of_ts: float | None) -> datetime:+ if as_of_ts is not None:+ return datetime.fromtimestamp(as_of_ts, tz=UTC)+ return datetime.now(UTC)+++def _add_span(meta: dict[str, Any], commits: list[_CommitRec], now: datetime) -> None:+ timestamps = [c.ts for c in commits if c.ts > 0]+ if timestamps:+ first_ts = min(timestamps)+ last_ts = max(timestamps)+ meta["first_commit_at"] = datetime.fromtimestamp(first_ts, tz=UTC)+ meta["last_commit_at"] = datetime.fromtimestamp(last_ts, tz=UTC)+ meta["age_days"] = (now - datetime.fromtimestamp(first_ts, tz=UTC)).days+++def _add_windows(+ meta: dict[str, Any],+ commits: list[_CommitRec],+ ninety_days_ago_ts: float,+ thirty_days_ago_ts: float,+) -> None:+ """Commit counts and churn over the trailing 90 and 30 days."""+ for c in commits:+ if c.ts >= ninety_days_ago_ts:+ meta["commit_count_90d"] += 1+ meta["lines_added_90d"] += c.added+ meta["lines_deleted_90d"] += c.deleted+ if c.is_merge:+ meta["merge_commit_count_90d"] += 1+ if c.ts >= thirty_days_ago_ts:+ meta["commit_count_30d"] += 1++ c90 = meta["commit_count_90d"]+ total_churn = meta["lines_added_90d"] + meta["lines_deleted_90d"]+ meta["avg_commit_size"] = total_churn / c90 if c90 > 0 else 0.0+++_NOREPLY_DOMAIN = "@users.noreply.github.com"+++@dataclass+class _Authors:+ """Per-author commit counts, one stable email each, and first/last commit times."""++ counts: Counter[str] = field(default_factory=Counter)+ recent_counts: Counter[str] = field(default_factory=Counter)+ emails: dict[str, str] = field(default_factory=dict)+ # Each author's own first/last commit to this file, not the file's: a+ # co-owner touching a shared file must not refresh everyone's "last touched".+ first_ts: dict[str, int] = field(default_factory=dict)+ last_ts: dict[str, int] = field(default_factory=dict)++ @classmethod+ def tally(cls, commits: list[_CommitRec], recent_since_ts: float) -> _Authors:+ authors = cls()for c in commits:- if c.ts >= ninety_days_ago_ts:- meta["commit_count_90d"] += 1- recent_author_counts[c.author_name] += 1- meta["lines_added_90d"] += c.added- meta["lines_deleted_90d"] += c.deleted- if c.is_merge:- meta["merge_commit_count_90d"] += 1- if c.ts >= thirty_days_ago_ts:- meta["commit_count_30d"] += 1- author_counts[c.author_name] += 1+ name = c.author_name+ authors.counts[name] += 1+ if c.ts >= recent_since_ts:+ authors.recent_counts[name] += 1if c.author_email:- # Group by name, but pick a stable email per person: fold GitHub- # noreply variants together and prefer a real address over a- # noreply one, so a contributor who committed both ways doesn't- # split into two buckets downstream (owner_profile keys on email).- canon = canonicalize_author_email(c.author_email) or c.author_email- existing = author_emails.get(c.author_name)- if existing is None or (- existing.endswith("@users.noreply.github.com")- and not canon.endswith("@users.noreply.github.com")- ):- author_emails[c.author_name] = canon+ authors._prefer_email(name, c.author_email)if c.ts > 0:- prev_last = author_last_ts.get(c.author_name)- if prev_last is None or c.ts > prev_last:- author_last_ts[c.author_name] = c.ts- prev_first = author_first_ts.get(c.author_name)- if prev_first is None or c.ts < prev_first:- author_first_ts[c.author_name] = c.ts-- c90 = meta["commit_count_90d"]- total_churn = meta["lines_added_90d"] + meta["lines_deleted_90d"]- meta["avg_commit_size"] = total_churn / c90 if c90 > 0 else 0.0-- # Agent-provenance rollup: share of this file's indexed commits that- # are agent-attributed, plus the per-autonomy-tier breakdown. The- # per-commit labels were classified once during the walk; this is a- # pure count over the already-carried records.- agent_commits = [c for c in commits if getattr(c, "agent", None)]- meta["agent_commit_count"] = len(agent_commits)- meta["agent_authored_pct"] = len(agent_commits) / len(commits)- tier_counts: Counter[str] = Counter(- str(c.agent_tier) for c in agent_commits if c.agent_tier+ authors.last_ts[name] = max(authors.last_ts.get(name, c.ts), c.ts)+ authors.first_ts[name] = min(authors.first_ts.get(name, c.ts), c.ts)+ return authors++ def _prefer_email(self, name: str, email: str) -> None:+ # One email per name: fold GitHub noreply variants and prefer a real+ # address, so owner_profile (keyed on email) does not split a person.+ canon = canonicalize_author_email(email) or email+ existing = self.emails.get(name)+ if existing is None or (+ existing.endswith(_NOREPLY_DOMAIN) and not canon.endswith(_NOREPLY_DOMAIN)+ ):+ self.emails[name] = canon++ def entry(self, name: str, count: int) -> dict[str, Any]:+ entry: dict[str, Any] = {+ "name": name,+ "email": self.emails.get(name, ""),+ "commit_count": count,+ }+ if name in self.last_ts:+ entry["last_commit_ts"] = self.last_ts[name]+ if name in self.first_ts:+ entry["first_commit_ts"] = self.first_ts[name]+ return entry+++def _add_agent_rollup(meta: dict[str, Any], commits: list[_CommitRec]) -> None:+ """Share of the file's commits that are agent-attributed, and the per-tier breakdown."""+ agent_commits = [c for c in commits if getattr(c, "agent", None)]+ meta["agent_commit_count"] = len(agent_commits)+ meta["agent_authored_pct"] = len(agent_commits) / len(commits)+ tier_counts: Counter[str] = Counter(str(c.agent_tier) for c in agent_commits if c.agent_tier)+ meta["agent_tier_counts_json"] = json.dumps(dict(tier_counts))+++def _temporal_hotspot_score(commits: list[_CommitRec], now: datetime) -> float:+ """Exponentially decayed per-commit churn."""+ _ln2 = math.log(2)+ temporal_score = 0.0+ for c in commits:+ age_days = max((now.timestamp() - c.ts) / 86400.0, 0.0)+ weight = math.exp(-_ln2 * age_days / HOTSPOT_HALFLIFE_DAYS)+ lines = min((c.added + c.deleted) / 100.0, 3.0)+ temporal_score += weight * lines+ return temporal_score+++def _add_ownership(meta: dict[str, Any], authors: _Authors) -> None:+ """Contributor count, bus factor, top authors and the overall and 90-day owners."""+ meta["contributor_count"] = len(authors.counts)+ total_commits = sum(authors.counts.values())+ if total_commits > 0:+ meta["bus_factor"] = _bus_factor(authors.counts, total_commits)++ top_authors = [+ authors.entry(name, count) for name, count in authors.counts.most_common(_MAX_TOP_AUTHORS)+ ]+ meta["top_authors_json"] = json.dumps(top_authors)++ if top_authors:+ primary = top_authors[0]+ meta["primary_owner_name"] = primary["name"]+ meta["primary_owner_email"] = primary["email"]+ meta["primary_owner_commit_pct"] = (+ primary["commit_count"] / total_commits if total_commits > 0 else 0.0)- meta["agent_tier_counts_json"] = json.dumps(dict(tier_counts))-- # Temporal hotspot score: exponentially decayed per-commit churn.- _ln2 = math.log(2)- temporal_score = 0.0- for c in commits:- age_days = max((now.timestamp() - c.ts) / 86400.0, 0.0)- weight = math.exp(-_ln2 * age_days / HOTSPOT_HALFLIFE_DAYS)- lines = min((c.added + c.deleted) / 100.0, 3.0)- temporal_score += weight * lines- meta["temporal_hotspot_score"] = temporal_score-- # Contributor count & bus factor- meta["contributor_count"] = len(author_counts)- total_commits = sum(author_counts.values())- if total_commits > 0:- threshold = total_commits * 0.8- running = 0- bus = 0- for _name, cnt in author_counts.most_common():- running += cnt- bus += 1- if running >= threshold:- break- meta["bus_factor"] = bus-- # Top authors — keep a generous slice (50) so the API can show the- # full contributor surface for a file.- top_authors = []- for name, count in author_counts.most_common(_MAX_TOP_AUTHORS):- entry: dict[str, Any] = {- "name": name,- "email": author_emails.get(name, ""),- "commit_count": count,- }- if name in author_last_ts:- entry["last_commit_ts"] = author_last_ts[name]- if name in author_first_ts:- entry["first_commit_ts"] = author_first_ts[name]- top_authors.append(entry)- meta["top_authors_json"] = json.dumps(top_authors)-- if top_authors:- primary = top_authors[0]- meta["primary_owner_name"] = primary["name"]- meta["primary_owner_email"] = primary["email"]- meta["primary_owner_commit_pct"] = (- primary["commit_count"] / total_commits if total_commits > 0 else 0.0- )-- # Recent owner (90d)- if recent_author_counts:- recent_top = recent_author_counts.most_common(1)[0]- meta["recent_owner_name"] = recent_top[0]- recent_total = sum(recent_author_counts.values())- meta["recent_owner_commit_pct"] = (- recent_top[1] / recent_total if recent_total > 0 else 0.0- )-- # Blame ownership + per-line index (FULL tier only). One- # ``git blame --line-porcelain`` invocation produces both the- # primary-owner signal and the in-memory ``BlameIndex`` consumed by- # ``function_hotspot`` / ``code_age_volatility``. Skipped for large- # files — git blame is O(lines) and can block the executor thread.- #- # Files below the function-blame commit floor used to take a second- # blame pass through gitpython's ``repo.blame`` just for ownership.- # That object-building parse is pure Python, holds the GIL across the- # 20-way thread fan-out, and on a large repo MOST files sit below the- # floor, so it dominated the FULL git phase. The porcelain parse now- # serves ownership for every file; the floor only gates whether the- # BlameIndex is retained for the function biomarkers, as before.- if include_blame:- try:- file_size = (repo_path / file_path).stat().st_size- if file_size <= _MAX_BLAME_SIZE_BYTES:- blame_idx = build_blame_index(- repo,- file_path,- repo_path=repo_path,- )- if blame_idx.lines:- blame_idx.as_of_ts = int(now.timestamp())- if meta["commit_count_total"] >= _MIN_COMMITS_FOR_BLAME:- meta["blame_index"] = blame_idx- blame_name, blame_email, blame_pct = ownership_from_blame(blame_idx)- if blame_name:- meta["primary_owner_name"] = blame_name- meta["primary_owner_email"] = blame_email- meta["primary_owner_commit_pct"] = blame_pct- except Exception:- pass # blame is best-effort-- # Significant commits + classification + PR extraction- sig_commits: list[dict[str, Any]] = []- sig_full = False- category_counts: Counter[str] = Counter()- for c in commits:- msg = c.subject[:200]- if not sig_full and is_significant_commit(msg, c.author_name):- entry: dict[str, Any] = {- "sha": c.sha[:8],- "date": datetime.fromtimestamp(c.ts, tz=UTC).isoformat() if c.ts else "",- "message": msg,- "author": c.author_name,- }- pr_match = _PR_NUMBER_RE.search(msg)- if pr_match:- pr_num = pr_match.group(1) or pr_match.group(2) or pr_match.group(3)- entry["pr_number"] = int(pr_num)- # Retain the commit body (byte-capped) only for significant- # commits whose body shows decision intent — squash-merge repos- # carry the full rationale here, which the PR/squash miner- # consumes. The decision gate + cap keep the per-file JSON from- # ballooning (the body is duplicated across every touched file).- raw_body = getattr(c, "body", "") or ""- if _body_carries_decision(c.subject, raw_body):- body = _truncate_body(raw_body)- if body:- entry["body"] = body- sig_commits.append(entry)- if len(sig_commits) >= _MAX_SIGNIFICANT_COMMITS:- sig_full = True- # Classify ALL commits for accurate category ratios- for cat, pattern in _COMMIT_CATEGORIES.items():- if pattern.search(msg):- category_counts[cat] += 1- break-- meta["significant_commits_json"] = json.dumps(sig_commits)- meta["commit_categories_json"] = json.dumps(dict(category_counts))-- # Original path detection (rename tracking) — reuse the result from the- # known_paths seeding above to avoid a duplicate subprocess.- if follow_renames and orig_path:- meta["original_path"] = orig_path-- # Stable classification- if meta["commit_count_total"] > 10 and meta["commit_count_90d"] == 0:- meta["is_stable"] = True+ if authors.recent_counts:+ recent_top = authors.recent_counts.most_common(1)[0]+ meta["recent_owner_name"] = recent_top[0]+ recent_total = sum(authors.recent_counts.values())+ meta["recent_owner_commit_pct"] = recent_top[1] / recent_total if recent_total > 0 else 0.0+++def _bus_factor(author_counts: Counter[str], total_commits: int) -> int:+ """How many top authors it takes to cover 80% of the commits."""+ threshold = total_commits * 0.8+ running = 0+ bus = 0+ for _name, cnt in author_counts.most_common():+ running += cnt+ bus += 1+ if running >= threshold:+ break+ return bus+++def _add_blame_ownership(meta: dict[str, Any], repo: Any, repo_path: Path, now: datetime) -> None:+ """Blame ownership plus the per-line ``BlameIndex`` (FULL tier), best effort.++ One ``git blame --line-porcelain`` pass serves both the primary-owner+ signal and the index ``function_hotspot`` / ``code_age_volatility`` read.+ Files over the size cap are skipped: blame is O(lines) and can block the+ executor thread. The commit floor only gates whether the index is kept.+ """+ file_path = meta["file_path"]+ try:+ if (repo_path / file_path).stat().st_size > _MAX_BLAME_SIZE_BYTES:+ return+ blame_idx = build_blame_index(repo, file_path, repo_path=repo_path)+ if not blame_idx.lines:+ return+ blame_idx.as_of_ts = int(now.timestamp())+ if meta["commit_count_total"] >= _MIN_COMMITS_FOR_BLAME:+ meta["blame_index"] = blame_idx+ blame_name, blame_email, blame_pct = ownership_from_blame(blame_idx)+ if blame_name:+ meta["primary_owner_name"] = blame_name+ meta["primary_owner_email"] = blame_email+ meta["primary_owner_commit_pct"] = blame_pctexcept Exception:- logger.debug("git_indexer_partial_failure", file_path=file_path, exc_info=True)-- return meta+ pass # blame is best-effort+++def _add_commit_messages(meta: dict[str, Any], commits: list[_CommitRec]) -> None:+ """Significant commits (with PR numbers and decision bodies) and category counts."""+ sig_commits: list[dict[str, Any]] = []+ for c in commits:+ msg = c.subject[:200]+ if is_significant_commit(msg, c.author_name):+ sig_commits.append(_significant_entry(c, msg))+ if len(sig_commits) >= _MAX_SIGNIFICANT_COMMITS:+ break+ # Every commit is classified, so the category ratios cover the whole history.+ category_counts: Counter[str] = Counter(+ cat for c in commits if (cat := _commit_category(c.subject[:200])) is not None+ )+ meta["significant_commits_json"] = json.dumps(sig_commits)+ meta["commit_categories_json"] = json.dumps(dict(category_counts))+++def _significant_entry(c: _CommitRec, msg: str) -> dict[str, Any]:+ entry: dict[str, Any] = {+ "sha": c.sha[:8],+ "date": datetime.fromtimestamp(c.ts, tz=UTC).isoformat() if c.ts else "",+ "message": msg,+ "author": c.author_name,+ }+ pr_match = _PR_NUMBER_RE.search(msg)+ if pr_match:+ pr_num = pr_match.group(1) or pr_match.group(2) or pr_match.group(3)+ entry["pr_number"] = int(pr_num)+ # Squash-merge bodies carry the rationale the PR miner reads.+ raw_body = getattr(c, "body", "") or ""+ if _body_carries_decision(c.subject, raw_body):+ body = _truncate_body(raw_body)+ if body:+ entry["body"] = body+ return entry+++def _commit_category(msg: str) -> str | None:+ for cat, pattern in _COMMIT_CATEGORIES.items():+ if pattern.search(msg):+ return cat+ return None+++def _is_stable(meta: dict[str, Any]) -> bool:+ """A long history with nothing in the last 90 days."""+ return meta["commit_count_total"] > 10 and meta["commit_count_90d"] == 0