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.

Phase 2 git intelligence: diff size, commit classification, ownership, temporal decay

RaghavChamadiyac1780ba2b7c8
diff --git a/packages/core/alembic/versions/0007_git_phase2_signals.py b/packages/core/alembic/versions/0007_git_phase2_signals.py
new file mode 100644
index 000000000..78f3be17c
--- /dev/null
+++ b/packages/core/alembic/versions/0007_git_phase2_signals.py
@@ -0,0 +1,73 @@
+"""Add Phase 2 git intelligence columns to git_metadata.
+
+New signals: diff size (lines added/deleted), commit message classification,
+recent ownership, and bus factor.
+
+Revision ID: 0007
+Revises: 0006
+Create Date: 2026-03-26
+"""
+
+from __future__ import annotations
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers
+revision: str = "0007"
+down_revision: Union[str, None] = "0006"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # Diff size
+ op.add_column(
+ "git_metadata",
+ sa.Column("lines_added_90d", sa.Integer, nullable=False, server_default="0"),
+ )
+ op.add_column(
+ "git_metadata",
+ sa.Column("lines_deleted_90d", sa.Integer, nullable=False, server_default="0"),
+ )
+ op.add_column(
+ "git_metadata",
+ sa.Column("avg_commit_size", sa.Float, nullable=False, server_default="0.0"),
+ )
+
+ # Commit classification
+ op.add_column(
+ "git_metadata",
+ sa.Column("commit_categories_json", sa.Text, nullable=False, server_default="{}"),
+ )
+
+ # Recent ownership & bus factor
+ op.add_column(
+ "git_metadata",
+ sa.Column("recent_owner_name", sa.String(255), nullable=True),
+ )
+ op.add_column(
+ "git_metadata",
+ sa.Column("recent_owner_commit_pct", sa.Float, nullable=True),
+ )
+ op.add_column(
+ "git_metadata",
+ sa.Column("bus_factor", sa.Integer, nullable=False, server_default="0"),
+ )
+ op.add_column(
+ "git_metadata",
+ sa.Column("contributor_count", sa.Integer, nullable=False, server_default="0"),
+ )
+
+
+def downgrade() -> None:
+ op.drop_column("git_metadata", "contributor_count")
+ op.drop_column("git_metadata", "bus_factor")
+ op.drop_column("git_metadata", "recent_owner_commit_pct")
+ op.drop_column("git_metadata", "recent_owner_name")
+ op.drop_column("git_metadata", "commit_categories_json")
+ op.drop_column("git_metadata", "avg_commit_size")
+ op.drop_column("git_metadata", "lines_deleted_90d")
+ op.drop_column("git_metadata", "lines_added_90d")
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer.py b/packages/core/src/repowise/core/ingestion/git_indexer.py
index df377272a..a110d0583 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer.py
@@ -11,6 +11,8 @@ from __future__ import annotations
import asyncio
import json
+import math
+import re
import time
from collections import Counter, defaultdict
from dataclasses import dataclass, field
@@ -62,6 +64,28 @@ _MIN_MESSAGE_LEN = 12
# Default per-file and co-change commit history depth.
_DEFAULT_COMMIT_LIMIT: int = 500
+# Commit message classification regexes (Phase 2.2).
+_COMMIT_CATEGORIES: dict[str, re.Pattern[str]] = {
+ "feature": re.compile(
+ r"\b(add|implement|introduce|create|new|feat)\b", re.IGNORECASE,
+ ),
+ "refactor": re.compile(
+ r"\b(refactor|restructure|cleanup|clean.up|rename|reorganize|extract|simplify|move)\b",
+ re.IGNORECASE,
+ ),
+ "fix": re.compile(
+ r"\b(fix|bug|patch|hotfix|revert|regression|broken|crash|error)\b",
+ re.IGNORECASE,
+ ),
+ "dependency": re.compile(
+ r"\b(upgrade|bump|update.dep|migrate.to|switch.to|dependency|dependencies)\b",
+ re.IGNORECASE,
+ ),
+}
+
+# Co-change temporal decay: half-life ~125 days (lambda for exp(-t/tau)).
+_CO_CHANGE_DECAY_TAU: float = 180.0
+
# Allowlist of extensions for which per-file git indexing (blame, commit
# history, hotspot/stable classification) is worth running. Anything NOT in
# this set is skipped — data, config, markup, dotfiles, and binaries add no
@@ -365,10 +389,19 @@ class GitIndexer:
"top_authors_json": "[]",
"significant_commits_json": "[]",
"co_change_partners_json": "[]",
+ "commit_categories_json": "{}",
"is_hotspot": False,
"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,
+ "recent_owner_name": None,
+ "recent_owner_commit_pct": None,
+ "bus_factor": 0,
+ "contributor_count": 0,
}
try:
@@ -389,25 +422,40 @@ class GitIndexer:
meta["last_commit_at"] = max(dates)
meta["age_days"] = (now - min(dates)).days
- # Commit counts by time window
+ # Commit counts by time window + recent author tracking
+ author_counts: Counter[str] = Counter()
+ author_emails: dict[str, str] = {}
+ recent_author_counts: Counter[str] = Counter()
+
for c in commits:
cd = c.committed_datetime
if cd.tzinfo is None:
cd = cd.replace(tzinfo=timezone.utc)
if cd >= ninety_days_ago:
meta["commit_count_90d"] += 1
+ recent_author_counts[c.author.name or "unknown"] += 1
if cd >= thirty_days_ago:
meta["commit_count_30d"] += 1
-
- # Author analysis
- author_counts: Counter[str] = Counter()
- author_emails: dict[str, str] = {}
- for c in commits:
name = c.author.name or "unknown"
author_counts[name] += 1
if name not in author_emails and c.author.email:
author_emails[name] = c.author.email
+ # 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
top_authors = []
for name, count in author_counts.most_common(5):
top_authors.append({
@@ -421,9 +469,17 @@ class GitIndexer:
primary = top_authors[0]
meta["primary_owner_name"] = primary["name"]
meta["primary_owner_email"] = primary["email"]
- total = sum(a["commit_count"] for a in top_authors)
meta["primary_owner_commit_pct"] = (
- primary["commit_count"] / total if total > 0 else 0.0
+ 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 (overrides author-based if available).
@@ -443,19 +499,37 @@ class GitIndexer:
except Exception:
pass # blame is best-effort
- # Significant commits
+ # Significant commits + classification
sig_commits = []
+ category_counts: Counter[str] = Counter()
for c in commits:
+ msg = c.message.strip().split("\n")[0][:200]
if self._is_significant_commit(c.message, c.author.name or ""):
sig_commits.append({
"sha": c.hexsha[:8],
"date": c.committed_datetime.isoformat(),
- "message": c.message.strip().split("\n")[0][:200],
+ "message": msg,
"author": c.author.name or "unknown",
})
if len(sig_commits) >= 10:
break
+ # Classify ALL commits (not just significant) for accurate ratios
+ for cat, pattern in _COMMIT_CATEGORIES.items():
+ if pattern.search(msg):
+ category_counts[cat] += 1
+ break # first match wins
+
meta["significant_commits_json"] = json.dumps(sig_commits)
+ meta["commit_categories_json"] = json.dumps(dict(category_counts))
+
+ # Diff stats (lines added/deleted in last 90 days)
+ added, deleted = self._get_line_stats(file_path, repo, ninety_days_ago)
+ meta["lines_added_90d"] = added
+ meta["lines_deleted_90d"] = deleted
+ c90 = meta["commit_count_90d"]
+ meta["avg_commit_size"] = (
+ (added + deleted) / c90 if c90 > 0 else 0.0
+ )
# Stable classification
if meta["commit_count_total"] > 10 and meta["commit_count_90d"] == 0:
@@ -494,6 +568,38 @@ class GitIndexer:
pct = line_counts[top_name] / total_lines
return top_name, emails.get(top_name), pct
+ def _get_line_stats(
+ self, file_path: str, repo: Any, since: datetime,
+ ) -> tuple[int, int]:
+ """Get total lines added and deleted for a file since a given date.
+
+ Uses a single ``git log --numstat`` call — one subprocess per file.
+ """
+ try:
+ output = repo.git.log(
+ f"--since={since.strftime('%Y-%m-%d')}",
+ "--numstat",
+ "--format=",
+ "--",
+ file_path,
+ )
+ except Exception:
+ return 0, 0
+
+ added = 0
+ deleted = 0
+ for line in output.splitlines():
+ parts = line.split("\t")
+ if len(parts) >= 2:
+ try:
+ a = int(parts[0]) if parts[0] != "-" else 0
+ d = int(parts[1]) if parts[1] != "-" else 0
+ added += a
+ deleted += d
+ except ValueError:
+ continue
+ return added, deleted
+
def _is_significant_commit(self, message: str, author: str) -> bool:
"""Return True if the commit is considered significant.
@@ -539,21 +645,25 @@ class GitIndexer:
Uses a single ``git log --name-only`` call instead of spawning one
``git diff`` subprocess per commit — O(1) processes vs O(commit_limit).
+ Applies exponential temporal decay so recent co-changes weigh more
+ than ancient ones. The ``%ct`` format captures commit timestamps.
+
on_co_change_start(total) is called once with the actual number of
commits found. on_commit_done() is called after each commit block is
processed. Both are invoked from a thread-pool thread; callers must
ensure thread safety (Rich Progress is thread-safe).
"""
- pair_counts: Counter[tuple[str, str]] = Counter()
+ pair_scores: defaultdict[tuple[str, str], float] = defaultdict(float)
+ pair_last_date: dict[tuple[str, str], int] = {} # pair → latest Unix ts
+ now_ts = time.time()
try:
- # One git command returns all changed-file lists. %x00 (null byte)
- # is used as a commit separator — it cannot appear in file paths.
+ # %x00 = commit separator, %ct = committer timestamp (Unix epoch).
raw = repo.git.log(
f"-{commit_limit}",
"--name-only",
"--no-merges",
- "--format=%x00",
+ "--format=%x00%ct",
)
except Exception:
return {}
@@ -564,16 +674,32 @@ class GitIndexer:
on_co_change_start(actual_commits)
current: set[str] = set()
+ current_ts: int = 0
+
+ def _flush_commit() -> None:
+ if len(current) < 2:
+ return
+ age_days = max((now_ts - current_ts) / 86400.0, 0.0)
+ weight = math.exp(-age_days / _CO_CHANGE_DECAY_TAU)
+ sorted_files = sorted(current)
+ for i in range(len(sorted_files)):
+ for j in range(i + 1, len(sorted_files)):
+ pair = (sorted_files[i], sorted_files[j])
+ pair_scores[pair] += weight
+ if pair not in pair_last_date or current_ts > pair_last_date[pair]:
+ pair_last_date[pair] = current_ts
+
for line in raw.splitlines():
if line == "\x00" or line.startswith("\x00"):
- # Commit boundary — process the files accumulated for the
- # previous commit, then start a fresh set.
- if len(current) >= 2:
- sorted_files = sorted(current)
- for i in range(len(sorted_files)):
- for j in range(i + 1, len(sorted_files)):
- pair_counts[(sorted_files[i], sorted_files[j])] += 1
+ # Commit boundary — flush previous, parse timestamp.
+ _flush_commit()
current = set()
+ # Extract Unix timestamp after the \x00 marker
+ ts_part = line.lstrip("\x00").strip()
+ try:
+ current_ts = int(ts_part)
+ except (ValueError, TypeError):
+ current_ts = 0
if on_commit_done is not None:
on_commit_done()
else:
@@ -581,21 +707,33 @@ class GitIndexer:
if path and path in all_files:
current.add(path)
- # Flush the final commit's files
- if len(current) >= 2:
- sorted_files = sorted(current)
- for i in range(len(sorted_files)):
- for j in range(i + 1, len(sorted_files)):
- pair_counts[(sorted_files[i], sorted_files[j])] += 1
+ _flush_commit() # final commit
- # Build result: for each file, list partners above threshold
+ # Build result: for each file, list partners above threshold.
+ # min_count is compared against raw weight (decay-adjusted).
+ # A score of 3.0 roughly equals 3 recent co-changes or many older ones.
result: dict[str, list[dict]] = defaultdict(list)
- for (a, b), count in pair_counts.items():
- if count >= min_count:
- result[a].append({"file_path": b, "co_change_count": count})
- result[b].append({"file_path": a, "co_change_count": count})
-
- # Sort partners by count descending
+ for (a, b), score in pair_scores.items():
+ if score >= min_count:
+ last_ts = pair_last_date.get((a, b), 0)
+ last_date = (
+ datetime.fromtimestamp(last_ts, tz=timezone.utc).strftime("%Y-%m-%d")
+ if last_ts > 0 else None
+ )
+ entry_a = {
+ "file_path": b,
+ "co_change_count": round(score, 2),
+ "last_co_change": last_date,
+ }
+ entry_b = {
+ "file_path": a,
+ "co_change_count": round(score, 2),
+ "last_co_change": last_date,
+ }
+ result[a].append(entry_a)
+ result[b].append(entry_b)
+
+ # Sort partners by score descending
for fp in result:
result[fp].sort(key=lambda x: x["co_change_count"], reverse=True)
diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.py
index 3c85df6fb..0f1e38fb1 100644
--- a/packages/core/src/repowise/core/persistence/models.py
+++ b/packages/core/src/repowise/core/persistence/models.py
@@ -299,6 +299,22 @@ class GitMetadata(Base):
age_days: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
commit_count_capped: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
+ # Diff size (Phase 2)
+ lines_added_90d: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
+ lines_deleted_90d: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
+ avg_commit_size: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
+
+ # Commit classification (Phase 2)
+ commit_categories_json: Mapped[str] = mapped_column(
+ Text, nullable=False, default="{}"
+ )
+
+ # Recent ownership & bus factor (Phase 2)
+ recent_owner_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
+ recent_owner_commit_pct: Mapped[float | None] = mapped_column(Float, nullable=True)
+ bus_factor: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
+ contributor_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
+
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, default=_now_utc
)
diff --git a/packages/server/src/repowise/server/mcp_server.py b/packages/server/src/repowise/server/mcp_server.py
index 7c8cf251f..75404746c 100644
--- a/packages/server/src/repowise/server/mcp_server.py
+++ b/packages/server/src/repowise/server/mcp_server.py
@@ -685,15 +685,23 @@ async def _resolve_one_target(
if meta:
ownership["primary_owner"] = meta.primary_owner_name
ownership["owner_pct"] = meta.primary_owner_commit_pct
- ownership["contributor_count"] = len(json.loads(meta.top_authors_json))
+ ownership["contributor_count"] = getattr(meta, "contributor_count", 0) or len(json.loads(meta.top_authors_json))
+ ownership["bus_factor"] = getattr(meta, "bus_factor", 0) or 0
+ # Recent owner (who maintains this file now)
+ recent = getattr(meta, "recent_owner_name", None)
+ if recent and recent != meta.primary_owner_name:
+ ownership["recent_owner"] = recent
+ ownership["recent_owner_pct"] = getattr(meta, "recent_owner_commit_pct", None)
else:
ownership["primary_owner"] = None
ownership["owner_pct"] = None
ownership["contributor_count"] = 0
+ ownership["bus_factor"] = 0
else:
ownership["primary_owner"] = None
ownership["owner_pct"] = None
ownership["contributor_count"] = 0
+ ownership["bus_factor"] = 0
result_data["ownership"] = ownership
# --- Last change ---
@@ -874,6 +882,7 @@ async def _assess_one_target(
{
"file_path": p.get("file_path", p.get("path", "")),
"count": p.get("co_change_count", p.get("count", 0)),
+ "last_co_change": p.get("last_co_change"),
"has_import_link": p.get("file_path", p.get("path", "")) in import_related,
}
for p in partners
@@ -891,23 +900,59 @@ async def _assess_one_target(
# --- Impact surface ---
impact_surface = _compute_impact_surface(target, reverse_deps, node_meta)
+ # Phase 2: diff size & change magnitude
+ lines_added = getattr(meta, "lines_added_90d", 0) or 0
+ lines_deleted = getattr(meta, "lines_deleted_90d", 0) or 0
+ avg_size = getattr(meta, "avg_commit_size", 0.0) or 0.0
+
+ # Phase 2: commit classification → change_pattern
+ categories = {}
+ cat_json = getattr(meta, "commit_categories_json", None)
+ if cat_json:
+ try:
+ categories = json.loads(cat_json)
+ except (json.JSONDecodeError, TypeError):
+ pass
+ change_pattern = _derive_change_pattern(categories)
+
+ # Phase 2: recent owner & bus factor
+ recent_owner = getattr(meta, "recent_owner_name", None)
+ recent_owner_pct = getattr(meta, "recent_owner_commit_pct", None)
+ bus_factor = getattr(meta, "bus_factor", 0) or 0
+ contributor_count = getattr(meta, "contributor_count", 0) or 0
+
result_data["hotspot_score"] = hotspot_score
result_data["dependents_count"] = dep_count
result_data["co_change_partners"] = co_changes
result_data["primary_owner"] = owner
result_data["owner_pct"] = pct
+ result_data["recent_owner"] = recent_owner
+ result_data["recent_owner_pct"] = recent_owner_pct
+ result_data["bus_factor"] = bus_factor
+ result_data["contributor_count"] = contributor_count
result_data["trend"] = trend
result_data["risk_type"] = risk_type
+ result_data["change_pattern"] = change_pattern
+ result_data["change_magnitude"] = {
+ "lines_added_90d": lines_added,
+ "lines_deleted_90d": lines_deleted,
+ "avg_commit_size": round(avg_size, 1),
+ }
result_data["impact_surface"] = impact_surface
capped = getattr(meta, "commit_count_capped", False)
capped_note = " (history truncated — actual count may be higher)" if capped else ""
result_data["commit_count_capped"] = capped
+
+ bus_note = ""
+ if bus_factor == 1 and (meta.commit_count_total or 0) > 20:
+ bus_note = f", bus factor risk (sole maintainer: {owner})"
+
result_data["risk_summary"] = (
f"{target} — hotspot score {hotspot_score:.0%} ({trend}), "
- f"{dep_count} dependents, {risk_type}, "
+ f"{dep_count} dependents, {risk_type}, {change_pattern}, "
f"{len(co_changes)} co-change partners, owned {pct:.0%} by {owner}"
- f"{capped_note}"
+ f"{bus_note}{capped_note}"
)
return result_data
@@ -919,6 +964,26 @@ _FIX_PATTERN = re.compile(
)
+def _derive_change_pattern(categories: dict[str, int]) -> str:
+ """Derive a human-readable change pattern from commit category counts."""
+ if not categories:
+ return "uncategorized"
+ total = sum(categories.values())
+ if total == 0:
+ return "uncategorized"
+ dominant = max(categories, key=lambda k: categories[k])
+ ratio = categories[dominant] / total
+ if ratio >= 0.5:
+ labels = {
+ "feature": "feature-active",
+ "refactor": "primarily refactored",
+ "fix": "bug-prone",
+ "dependency": "dependency-churn",
+ }
+ return labels.get(dominant, dominant)
+ return "mixed-activity"
+
+
def _compute_trend(meta: Any) -> str:
"""Compute risk velocity from 30d vs 90d commit rates."""
c30 = meta.commit_count_30d or 0
@@ -941,7 +1006,7 @@ def _compute_trend(meta: Any) -> str:
def _classify_risk_type(meta: Any, dep_count: int) -> str:
- """Classify risk as churn-heavy, bug-prone, or high-coupling."""
+ """Classify risk as churn-heavy, bug-prone, high-coupling, or bus-factor-risk."""
# Count bug-fix commits from significant_commits messages
commits = json.loads(meta.significant_commits_json) if meta.significant_commits_json else []
fix_count = sum(
@@ -950,12 +1015,16 @@ def _classify_risk_type(meta: Any, dep_count: int) -> str:
)
churn_score = meta.churn_percentile or 0.0
+ bus_factor = getattr(meta, "bus_factor", 0) or 0
+ total_commits = meta.commit_count_total or 0
# Bug-prone takes priority if fix ratio is high
if commits and fix_count / len(commits) >= 0.4:
return "bug-prone"
if churn_score >= 0.7:
return "churn-heavy"
+ if bus_factor == 1 and total_commits > 20:
+ return "bus-factor-risk"
if dep_count >= 5:
return "high-coupling"
return "stable"