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
feat(git): persist per-commit change-risk history (#317)
Raghav Chamadiya5be3cdadfefe
diff --git a/packages/core/alembic/versions/0027_git_commits.py b/packages/core/alembic/versions/0027_git_commits.pynew file mode 100644index 000000000..e2d885ccc--- /dev/null+++ b/packages/core/alembic/versions/0027_git_commits.py@@ -0,0 +1,61 @@+"""Create git_commits table for per-commit change-risk tracking.++Stores one row per commit in the indexed window: SHA, author, timestamp,+subject, Kamei change features (diff size + diffusion), and the calibrated+just-in-time ``change_risk`` score/level. Written during the existing+repo-wide commit-index walk (no extra git pass); bounded by the indexer's+commit_limit. Foundation for a commits/change-risk surface and change trends.++Revision ID: 0027+Revises: 0026+Create Date: 2026-05-30+"""++from __future__ import annotations++from collections.abc import Sequence++import sqlalchemy as sa+from alembic import op++# revision identifiers+revision: str = "0027"+down_revision: str | None = "0026"+branch_labels: str | Sequence[str] | None = None+depends_on: str | Sequence[str] | None = None+++def upgrade() -> None:+ op.create_table(+ "git_commits",+ sa.Column("id", sa.String(32), primary_key=True),+ sa.Column("repository_id", sa.String(32), nullable=False),+ sa.Column("sha", sa.String(40), nullable=False),+ sa.Column("author_name", sa.String(255), nullable=False, server_default=""),+ sa.Column("author_email", sa.String(255), nullable=False, server_default=""),+ sa.Column("committed_at", sa.DateTime(timezone=True), nullable=True),+ sa.Column("subject", sa.Text, nullable=False, server_default=""),+ sa.Column("lines_added", sa.Integer, nullable=False, server_default="0"),+ sa.Column("lines_deleted", sa.Integer, nullable=False, server_default="0"),+ sa.Column("files_changed", sa.Integer, nullable=False, server_default="0"),+ sa.Column("dirs_changed", sa.Integer, nullable=False, server_default="0"),+ sa.Column("subsystems_changed", sa.Integer, nullable=False, server_default="0"),+ sa.Column("entropy", sa.Float, nullable=False, server_default="0.0"),+ sa.Column("is_fix", sa.Boolean, nullable=False, server_default=sa.false()),+ sa.Column("change_risk_score", sa.Float, nullable=True),+ sa.Column("change_risk_level", sa.String(16), nullable=True),+ sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),+ sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),+ sa.ForeignKeyConstraint(["repository_id"], ["repositories.id"], ondelete="CASCADE"),+ sa.UniqueConstraint("repository_id", "sha", name="uq_git_commit"),+ )+ op.create_index(+ "ix_git_commits_repo_risk",+ "git_commits",+ ["repository_id", "change_risk_score"],+ )+++def downgrade() -> None:+ op.drop_index("ix_git_commits_repo_risk", table_name="git_commits")+ op.drop_table("git_commits")diff --git a/packages/core/src/repowise/core/ingestion/git_commit_index.py b/packages/core/src/repowise/core/ingestion/git_commit_index.pyindex a9be70f20..fda370793 100644--- a/packages/core/src/repowise/core/ingestion/git_commit_index.py+++ b/packages/core/src/repowise/core/ingestion/git_commit_index.py@@ -2,7 +2,7 @@The original per-file path in ``git_indexer._index_file`` spawned one``git log --numstat`` subprocess per tracked file. On a 5,000-file repo-that meant 5,000 process spawns — ~50–100 ms each on Windows — which+that meant 5,000 process spawns — ~50-100 ms each on Windows — whichmade the git phase dominate the total ``repowise init`` wall-clock.This module replaces the fan-out with one repo-wide ``git log`` pass@@ -33,6 +33,8 @@ def load_commit_index(repo: object,commit_limit: int,indexable_files: set[str],+ *,+ commit_sink: list[dict] | None = None,) -> dict[str, list[_CommitRec]]:"""Bucket every commit in the recent history by the files it touched.@@ -48,6 +50,15 @@ def load_commit_index(dict; callers should treat ``KeyError`` / ``get(file, [])`` as"no recorded history" rather than an error.+ When *commit_sink* is supplied, each parsed commit is appended to it as a+ raw dict (``sha``, ``author_name``, ``author_email``, ``ts``, ``subject``,+ and ``changes`` — the full ``(path, added, deleted)`` list across *all*+ files in the commit, not just the indexable subset, so change diffusion is+ measured against the real footprint). This rides the same single walk —+ no extra git pass — and lets the caller build per-commit rows downstream+ (see :mod:`git_indexer.commit_rows`). The default (``None``) leaves the+ return value and behaviour unchanged.+Failures (git unavailable, corrupt log output, etc.) return anempty dict so the caller can fall back to per-file indexing."""@@ -90,6 +101,11 @@ def load_commit_index(header, numstat_lines = parsedcommits_parsed += 1+ # Full change footprint of this commit (every file, not just the+ # indexable subset) — only accumulated when a sink is requested so the+ # default path pays nothing.+ commit_changes: list[tuple[str, int, int]] = [] if commit_sink is not None else None # type: ignore[assignment]+for line in numstat_lines:cols = line.split("\t")if len(cols) < 3:@@ -107,9 +123,6 @@ def load_commit_index(else:target = stat_path- if target not in indexable_files:- continue-try:added = int(cols[0]) if cols[0] != "-" else 0deleted = int(cols[1]) if cols[1] != "-" else 0@@ -117,6 +130,12 @@ def load_commit_index(added = 0deleted = 0+ if commit_changes is not None:+ commit_changes.append((target, added, deleted))++ if target not in indexable_files:+ continue+# Each commit becomes one record per file it touched — the# per-file analyzer treats this list as the file's own history.bucket.setdefault(target, []).append(@@ -133,6 +152,18 @@ def load_commit_index())+ if commit_sink is not None:+ commit_sink.append(+ {+ "sha": header["sha"],+ "author_name": header["author_name"],+ "author_email": header["author_email"],+ "ts": header["ts"],+ "subject": header["subject"],+ "changes": commit_changes,+ }+ )+logger.debug("repo_commit_index_built",commits_parsed=commits_parsed,diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/README.md b/packages/core/src/repowise/core/ingestion/git_indexer/README.mdindex a27fb0302..c9fefa602 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/README.md+++ b/packages/core/src/repowise/core/ingestion/git_indexer/README.md@@ -79,13 +79,29 @@ the full window at a fraction of the cost of lifting the global cap (it scaleswith window activity, not total repo age). Windowed-but-decayed signals(90d counts, entropy) tolerate the cap because old commits contribute ~nothing.+## Per-commit rows + just-in-time change-risk++The same repo-wide walk also yields **per-commit** rows (the `git_commits`+table), not just per-file aggregates. `load_commit_index` accepts an optional+`commit_sink`: when supplied it appends each commit (sha, author, ts, subject,+and its full `(path, added, deleted)` footprint across *all* files) during the+existing walk — no extra `git` pass. `commit_rows.build_commit_rows` then turns+those into rows carrying Kamei change features (lines/files/dirs/subsystems+touched, churn entropy) and a calibrated **change-risk** score/level from+`analysis.change_risk` — runtime-safe (pure arithmetic, no LLM, no blame).+Author *experience* (the one change-risk feature that costs a subprocess in the+live `repowise risk` path) is reconstructed **in memory** here: walking commits+oldest→newest, each author's prior-commit count is the running tally. Empty in+rename-tracking mode (which uses the per-file walk, not the batched index).+## Internal layout| Module | Contents ||--------|----------|| `tiers.py` | `GitIndexTier` enum + `includes_blame` / `includes_co_change` || `_constants.py` | commit-depth defaults, decay half-lives, the bug-fix keyword classifier (`is_fix_commit`, mirrors the benchmark) + `PRIOR_DEFECT_WINDOW_DAYS`, skip heuristics, GitPython noise patch |-| `records.py` | `_CommitRec`, `GitIndexSummary`, the `git log` record format + rename/skip path helpers |+| `records.py` | `_CommitRec`, `GitIndexSummary` (carries `commit_rows`), the `git log` record format + rename/skip path helpers |+| `commit_rows.py` | `build_commit_rows` — per-commit Kamei features + change-risk from the walk's sunk commits (pure, in-memory author experience) || `file_history.py` | `index_file` — per-file parse + base metrics (blame gated by tier) || `enrich.py` | blame ownership, commit significance, rename detection, percentiles || `function_blame.py` | per-line blame index → per-function modification counts + line age (FULL tier; feeds `function_hotspot` / `code_age_volatility`) |diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/commit_rows.py b/packages/core/src/repowise/core/ingestion/git_indexer/commit_rows.pynew file mode 100644index 000000000..15f1f7977--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/git_indexer/commit_rows.py@@ -0,0 +1,101 @@+"""Per-commit row builder for the ``git_commits`` table.++Turns the raw commits collected during the repo-wide commit-index walk (see+:func:`git_commit_index.load_commit_index`'s ``commit_sink``) into rows ready+for ``upsert_git_commits_bulk`` — each carrying the commit's Kamei change+features and a calibrated just-in-time :mod:`change_risk` score.++The transform is **pure** (no git, no I/O): it works off the already-parsed+diff data, so it is cheap (bounded by commit count) and unit-testable without a+repository. Author experience — the one change-risk feature that costs a+subprocess in the live ``repowise risk`` path — is reconstructed **in memory**+here: walking commits oldest→newest, each author's prior-commit count is the+running tally before their commit. That keeps the whole pass zero-extra-git.+"""++from __future__ import annotations++from datetime import UTC, datetime++from ._constants import is_fix_commit++# Commit subjects are a headline (`%s`); cap defensively against a pathological+# single-line subject so one row can't bloat the table.+_MAX_SUBJECT_LEN = 500+++def _author_key(author_name: str, author_email: str) -> str:+ """Stable identity for the in-memory experience tally."""+ return (author_email or author_name or "").strip().lower()+++def _committed_at(ts: int) -> datetime | None:+ if not ts or ts <= 0:+ return None+ try:+ return datetime.fromtimestamp(ts, tz=UTC)+ except (OverflowError, OSError, ValueError):+ return None+++def build_commit_rows(parsed_commits: list[dict]) -> list[dict]:+ """Build ``git_commits`` row dicts from sunk commit records.++ *parsed_commits* is the list populated by ``load_commit_index``'s+ ``commit_sink`` (newest-first), each item a dict with ``sha``,+ ``author_name``, ``author_email``, ``ts``, ``subject`` and ``changes``+ (a ``[(path, added, deleted)]`` list over every file in the commit).++ Returns one row per commit, preserving the input (newest-first) order.+ """+ if not parsed_commits:+ return []++ # Lazy import keeps the module-load graph acyclic (change_risk imports+ # git_indexer._constants) and matches the codebase's hot-path import style.+ from ...analysis.change_risk import features_from_file_changes, score_change++ # --- Author experience: cumulative prior-commit count, oldest→newest. ---+ # Each commit's exp is the author's tally BEFORE this commit (ties on the+ # same timestamp are ordered as-given, which is acceptable for a count).+ ordered = sorted(parsed_commits, key=lambda c: c.get("ts", 0) or 0)+ exp_by_sha: dict[str, int] = {}+ tally: dict[str, int] = {}+ for c in ordered:+ key = _author_key(c.get("author_name", ""), c.get("author_email", ""))+ exp_by_sha[c["sha"]] = tally.get(key, 0)+ tally[key] = tally.get(key, 0) + 1++ rows: list[dict] = []+ for c in parsed_commits:+ sha = c["sha"]+ subject = (c.get("subject") or "")[:_MAX_SUBJECT_LEN]+ changes = c.get("changes") or []+ feats = features_from_file_changes(+ changes,+ exp=exp_by_sha.get(sha, 0),+ is_fix=is_fix_commit(subject),+ author=c.get("author_name", ""),+ subject=subject,+ ref=sha,+ )+ risk = score_change(feats)+ rows.append(+ {+ "sha": sha,+ "author_name": c.get("author_name", "") or "",+ "author_email": c.get("author_email", "") or "",+ "committed_at": _committed_at(c.get("ts", 0) or 0),+ "subject": subject,+ "lines_added": feats.la,+ "lines_deleted": feats.ld,+ "files_changed": feats.nf,+ "dirs_changed": feats.nd,+ "subsystems_changed": feats.ns,+ "entropy": feats.entropy,+ "is_fix": feats.is_fix,+ "change_risk_score": risk.score,+ "change_risk_level": risk.level,+ }+ )+ return rowsdiff --git a/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py b/packages/core/src/repowise/core/ingestion/git_indexer/indexer.pyindex 69ce4359c..1017f80aa 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py@@ -114,10 +114,16 @@ class GitIndexer:# its commits from this shared dict instead of spawning its own# ``git log -- <file>`` — turning O(files) process spawns into O(1).commit_index: dict[str, list[_CommitRec]] = {}+ commit_sink: list[dict] = []if not self.follow_renames:from ..git_commit_index import load_commit_index- commit_index = load_commit_index(repo, self.commit_limit, set(indexable_files))+ commit_index = load_commit_index(+ repo,+ self.commit_limit,+ set(indexable_files),+ commit_sink=commit_sink,+ )include_blame = self.tier.includes_blameas_of_ts = self._resolve_as_of_ts(repo, commit_index)@@ -231,6 +237,19 @@ class GitIndexer:compute_percentiles(results)+ # Per-commit rows + just-in-time change-risk, built in-memory from the+ # commit-index walk's already-parsed diffs (no extra git pass). Empty+ # in rename-tracking mode (no batched commit index) and failure-isolated+ # so a change_risk hiccup never breaks file-level git metadata.+ commit_rows: list[dict] = []+ if commit_sink:+ try:+ from .commit_rows import build_commit_rows++ commit_rows = build_commit_rows(commit_sink)+ except Exception as exc:+ logger.debug("commit_rows_build_failed", error=str(exc))+duration = time.monotonic() - starthotspots = sum(1 for m in results if m.get("is_hotspot", False))stable = sum(1 for m in results if m.get("is_stable", False))@@ -240,6 +259,7 @@ class GitIndexer:hotspots=hotspots,stable_files=stable,duration_seconds=duration,+ commit_rows=commit_rows,)repo.close()diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/records.py b/packages/core/src/repowise/core/ingestion/git_indexer/records.pyindex 43910362c..1b9e21eb3 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/records.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/records.py@@ -3,7 +3,7 @@from __future__ import annotationsimport re-from dataclasses import dataclass+from dataclasses import dataclass, fieldfrom pathlib import Pathfrom ._constants import _CODE_EXTENSIONS@@ -112,6 +112,10 @@ class GitIndexSummary:hotspots: intstable_files: intduration_seconds: float = 0.0+ # Per-commit rows collected during the repo-wide commit-index walk, ready+ # for ``upsert_git_commits_bulk``. Empty in rename-tracking mode (which uses+ # the per-file walk instead of the batched commit index).+ commit_rows: list[dict] = field(default_factory=list)_RENAME_RE = re.compile(r"\{(.+?) => (.+?)\}")diff --git a/packages/core/src/repowise/core/persistence/__init__.py b/packages/core/src/repowise/core/persistence/__init__.pyindex e67800144..365a2fb88 100644--- a/packages/core/src/repowise/core/persistence/__init__.py+++ b/packages/core/src/repowise/core/persistence/__init__.py@@ -118,6 +118,7 @@ from .models import (DecisionRecord,ExternalSystem,GenerationJob,+ GitCommit,GitMetadata,GraphEdge,GraphMetric,@@ -157,6 +158,7 @@ __all__ = [# search"FullTextSearch","GenerationJob",+ "GitCommit","GitMetadata","GraphEdge","GraphMetric",diff --git a/packages/core/src/repowise/core/persistence/crud/git.py b/packages/core/src/repowise/core/persistence/crud/git.pyindex 2a17cf966..8a5310f43 100644--- a/packages/core/src/repowise/core/persistence/crud/git.py+++ b/packages/core/src/repowise/core/persistence/crud/git.py@@ -6,10 +6,11 @@ every public name, so existing imports are unaffected.from __future__ import annotations-from sqlalchemy import select, text+from sqlalchemy import delete, func, select, textfrom sqlalchemy.ext.asyncio import AsyncSessionfrom ..models import (+ GitCommit,GitMetadata,_new_uuid,_now_utc,@@ -175,3 +176,93 @@ WHERE repository_id = :repo_id;await session.execute(text(sql), {"repo_id": repository_id})await session.flush()return len(rows)+++# ---------------------------------------------------------------------------+# GitCommit CRUD (per-commit rows + just-in-time change-risk)+# ---------------------------------------------------------------------------+++def _update_git_commit(existing: GitCommit, row: dict) -> None:+ for key, val in row.items():+ # ``sha`` is the natural key — never reassign it on update.+ if key not in ("id", "repository_id", "sha") and hasattr(existing, key):+ setattr(existing, key, val)+ existing.updated_at = _now_utc()+++async def upsert_git_commits_bulk(+ session: AsyncSession,+ repository_id: str,+ commit_rows: list[dict],+) -> None:+ """Bulk upsert per-commit rows (keyed on ``repository_id`` + ``sha``)."""+ await _batch_upsert(+ session,+ GitCommit,+ commit_rows,+ key_fn=lambda row: (+ GitCommit.repository_id == repository_id,+ GitCommit.sha == row.get("sha", ""),+ ),+ update_fn=_update_git_commit,+ insert_fn=lambda row: GitCommit(+ id=_new_uuid(),+ repository_id=repository_id,+ **{+ k: v+ for k, v in row.items()+ if k not in ("id", "repository_id") and hasattr(GitCommit, k)+ },+ ),+ batch_size=_BATCH_SIZE,+ )+++async def delete_git_commits(session: AsyncSession, repository_id: str) -> None:+ """Remove all per-commit rows for a repository (used before a clean reindex)."""+ await session.execute(delete(GitCommit).where(GitCommit.repository_id == repository_id))+ await session.flush()+++async def count_git_commits(session: AsyncSession, repository_id: str) -> int:+ """Count persisted commits for a repository."""+ result = await session.execute(+ select(func.count()).select_from(GitCommit).where(GitCommit.repository_id == repository_id)+ )+ return int(result.scalar_one() or 0)+++async def get_git_commit(session: AsyncSession, repository_id: str, sha: str) -> GitCommit | None:+ """Return one commit by sha (or a unique prefix), or None."""+ result = await session.execute(+ select(GitCommit).where(+ GitCommit.repository_id == repository_id,+ GitCommit.sha.like(f"{sha}%"),+ )+ )+ return result.scalars().first()+++async def get_git_commits(+ session: AsyncSession,+ repository_id: str,+ *,+ limit: int = 50,+ offset: int = 0,+ sort: str = "risk",+) -> list[GitCommit]:+ """Return a page of commits, sorted by change-risk (default) or recency.++ ``sort="risk"`` ranks by ``change_risk_score`` descending (the review-+ priority order); ``sort="date"`` ranks by ``committed_at`` descending.+ """+ order = GitCommit.committed_at.desc() if sort == "date" else GitCommit.change_risk_score.desc()+ result = await session.execute(+ select(GitCommit)+ .where(GitCommit.repository_id == repository_id)+ .order_by(order)+ .limit(limit)+ .offset(offset)+ )+ return list(result.scalars().all())diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.pyindex 2a39d3ed5..50e86ad39 100644--- a/packages/core/src/repowise/core/persistence/models.py+++ b/packages/core/src/repowise/core/persistence/models.py@@ -19,6 +19,7 @@ from sqlalchemy import (DateTime,Float,ForeignKey,+ Index,Integer,String,Text,@@ -410,6 +411,60 @@ class GitMetadata(Base):)+class GitCommit(Base):+ """Per-commit git history: one row per commit in the indexed window.++ Captures the change-level signals the per-file ``GitMetadata`` aggregates+ away — diff size/diffusion (Kamei change metrics) and a calibrated+ just-in-time ``change_risk`` score — written during the same single+ repo-wide ``git log`` walk that builds the commit index (no extra git+ pass). The walk excludes merges, so every row is a real content change.+ Bounded by the indexer's ``commit_limit`` (newest-first), like the rest of+ the git data.+ """++ __tablename__ = "git_commits"+ __table_args__ = (+ UniqueConstraint("repository_id", "sha", name="uq_git_commit"),+ Index("ix_git_commits_repo_risk", "repository_id", "change_risk_score"),+ )++ id: Mapped[str] = mapped_column(String(32), primary_key=True, default=_new_uuid)+ repository_id: Mapped[str] = mapped_column(+ String(32), ForeignKey("repositories.id", ondelete="CASCADE"), nullable=False+ )+ sha: Mapped[str] = mapped_column(String(40), nullable=False)++ # Authorship + timeline+ author_name: Mapped[str] = mapped_column(String(255), nullable=False, default="")+ author_email: Mapped[str] = mapped_column(String(255), nullable=False, default="")+ committed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)+ subject: Mapped[str] = mapped_column(Text, nullable=False, default="")++ # Kamei change features (diff size + diffusion of THIS change)+ lines_added: Mapped[int] = mapped_column(Integer, nullable=False, default=0)+ lines_deleted: Mapped[int] = mapped_column(Integer, nullable=False, default=0)+ files_changed: Mapped[int] = mapped_column(Integer, nullable=False, default=0)+ dirs_changed: Mapped[int] = mapped_column(Integer, nullable=False, default=0)+ subsystems_changed: Mapped[int] = mapped_column(Integer, nullable=False, default=0)+ entropy: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)+ is_fix: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)++ # Just-in-time change-risk: 0-10 score + level ("low"/"moderate"/"high")+ # from the calibrated linear ``change_risk`` model. Author experience is+ # computed in-memory across the walk (cumulative prior-commit count); the+ # score is pure arithmetic on already-parsed diff data (zero LLM, no blame).+ change_risk_score: Mapped[float | None] = mapped_column(Float, nullable=True)+ change_risk_level: Mapped[str | None] = mapped_column(String(16), nullable=True)++ created_at: Mapped[datetime] = mapped_column(+ DateTime(timezone=True), nullable=False, default=_now_utc+ )+ updated_at: Mapped[datetime] = mapped_column(+ DateTime(timezone=True), nullable=False, default=_now_utc, onupdate=_now_utc+ )++class DecisionRecord(Base):"""An architectural decision record captured from inline markers, gitarchaeology, README mining, or manual CLI entry."""diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.pyindex 5e29b7b1e..159662886 100644--- a/packages/core/src/repowise/core/pipeline/persist.py+++ b/packages/core/src/repowise/core/pipeline/persist.py@@ -146,6 +146,7 @@ async def persist_pipeline_result(save_health_findings,save_health_metrics,save_health_snapshot,+ upsert_git_commits_bulk,upsert_git_metadata_bulk,)@@ -226,6 +227,11 @@ async def persist_pipeline_result(if result.git_metadata_list:await upsert_git_metadata_bulk(session, repo_id, result.git_metadata_list)+ # ---- Per-commit rows + change-risk (ride on the git summary) -------------+ commit_rows = getattr(getattr(result, "git_summary", None), "commit_rows", None)+ if commit_rows:+ await upsert_git_commits_bulk(session, repo_id, commit_rows)+# ---- Dead code findings --------------------------------------------------if result.dead_code_report and result.dead_code_report.findings:await save_dead_code_findings(session, repo_id, result.dead_code_report.findings)diff --git a/tests/unit/ingestion/test_git_commit_rows_integration.py b/tests/unit/ingestion/test_git_commit_rows_integration.pynew file mode 100644index 000000000..c0ffe5990--- /dev/null+++ b/tests/unit/ingestion/test_git_commit_rows_integration.py@@ -0,0 +1,77 @@+"""Integration test: GitIndexer produces per-commit rows on a real repo.++Builds a small temporary git repository and runs the full indexer, asserting+that ``GitIndexSummary.commit_rows`` is populated with correctly-shaped rows+carrying change features + a just-in-time change-risk score — the data the+``git_commits`` table persists.+"""++from __future__ import annotations++import pytest++from repowise.core.ingestion.git_indexer import GitIndexer, GitIndexTier+++def _commit(repo, path, content, message):+ f = path+ f.write_text(content)+ repo.index.add([f.name])+ repo.index.commit(message)+++async def test_index_repo_collects_commit_rows(tmp_path) -> None:+ import git as gitpython++ repo = gitpython.Repo.init(tmp_path)+ # Three commits; the middle one is a fix touching two files.+ _commit(repo, tmp_path / "a.py", "x = 1\n", "feat: add a")+ (tmp_path / "b.py").write_text("y = 2\n")+ (tmp_path / "a.py").write_text("x = 1\nz = 3\n")+ repo.index.add(["a.py", "b.py"])+ repo.index.commit("fix: patch a and add b")+ _commit(repo, tmp_path / "c.py", "w = 4\n", "feat: add c")++ idx = GitIndexer(tmp_path, tier=GitIndexTier.FULL)+ summary, _results = await idx.index_repo("repo1")++ rows = summary.commit_rows+ assert len(rows) == 3++ # Every row has the persisted shape.+ for r in rows:+ assert r["sha"]+ assert set(+ [+ "lines_added",+ "files_changed",+ "dirs_changed",+ "subsystems_changed",+ "entropy",+ "is_fix",+ "change_risk_score",+ "change_risk_level",+ ]+ ).issubset(r.keys())+ assert 0.0 <= r["change_risk_score"] <= 10.0+ assert r["change_risk_level"] in {"low", "moderate", "high"}++ # The fix commit is flagged and touches two files.+ fix = next(r for r in rows if r["subject"].startswith("fix:"))+ assert fix["is_fix"] is True+ assert fix["files_changed"] == 2+++async def test_index_repo_commit_rows_empty_in_rename_mode(tmp_path) -> None:+ """Rename-tracking mode uses the per-file walk (no batched commit index),+ so commit rows are not collected — documented fallback, not an error."""+ import git as gitpython++ repo = gitpython.Repo.init(tmp_path)+ _commit(repo, tmp_path / "a.py", "x = 1\n", "feat: add a")++ idx = GitIndexer(tmp_path, tier=GitIndexTier.FULL, follow_renames=True)+ summary, _results = await idx.index_repo("repo1")+ assert summary.commit_rows == []diff --git a/tests/unit/persistence/test_git_commits_crud.py b/tests/unit/persistence/test_git_commits_crud.pynew file mode 100644index 000000000..213bc89ec--- /dev/null+++ b/tests/unit/persistence/test_git_commits_crud.py@@ -0,0 +1,110 @@+"""CRUD round-trip tests for the git_commits table."""++from __future__ import annotations++from datetime import UTC, datetime++import pytest++from repowise.core.persistence.crud import (+ count_git_commits,+ delete_git_commits,+ get_git_commit,+ get_git_commits,+ upsert_git_commits_bulk,+)+from tests.unit.persistence.helpers import insert_repo+++def _row(sha: str, *, risk: float, ts: int, **over) -> dict:+ base = {+ "sha": sha,+ "author_name": "Ann",+ "author_email": "ann@x",+ "committed_at": datetime.fromtimestamp(ts, tz=UTC),+ "subject": f"commit {sha}",+ "lines_added": 10,+ "lines_deleted": 2,+ "files_changed": 3,+ "dirs_changed": 1,+ "subsystems_changed": 1,+ "entropy": 0.5,+ "is_fix": False,+ "change_risk_score": risk,+ "change_risk_level": "high" if risk >= 7 else "moderate" if risk >= 4 else "low",+ }+ base.update(over)+ return base+++async def test_upsert_and_query_by_risk_and_date(async_session) -> None:+ repo = await insert_repo(async_session)+ rows = [+ _row("aaa111", risk=2.0, ts=3000),+ _row("bbb222", risk=8.5, ts=1000),+ _row("ccc333", risk=5.0, ts=2000),+ ]+ await upsert_git_commits_bulk(async_session, repo.id, rows)+ await async_session.commit()++ assert await count_git_commits(async_session, repo.id) == 3++ by_risk = await get_git_commits(async_session, repo.id, sort="risk")+ assert [c.sha for c in by_risk] == ["bbb222", "ccc333", "aaa111"]++ by_date = await get_git_commits(async_session, repo.id, sort="date")+ assert [c.sha for c in by_date] == ["aaa111", "ccc333", "bbb222"]+++async def test_pagination(async_session) -> None:+ repo = await insert_repo(async_session)+ rows = [_row(f"sha{i:03d}", risk=float(i), ts=1000 + i) for i in range(10)]+ await upsert_git_commits_bulk(async_session, repo.id, rows)+ await async_session.commit()++ page1 = await get_git_commits(async_session, repo.id, sort="risk", limit=3, offset=0)+ page2 = await get_git_commits(async_session, repo.id, sort="risk", limit=3, offset=3)+ assert [c.sha for c in page1] == ["sha009", "sha008", "sha007"]+ assert [c.sha for c in page2] == ["sha006", "sha005", "sha004"]+++async def test_get_by_sha_prefix(async_session) -> None:+ repo = await insert_repo(async_session)+ await upsert_git_commits_bulk(async_session, repo.id, [_row("abcdef1234", risk=3.0, ts=1000)])+ await async_session.commit()++ got = await get_git_commit(async_session, repo.id, "abcdef")+ assert got is not None+ assert got.sha == "abcdef1234"+ assert got.change_risk_level == "low"+ assert await get_git_commit(async_session, repo.id, "nope") is None+++async def test_upsert_is_idempotent_on_sha(async_session) -> None:+ repo = await insert_repo(async_session)+ await upsert_git_commits_bulk(async_session, repo.id, [_row("dup", risk=3.0, ts=1000)])+ await async_session.commit()+ # Re-upsert same sha with a new risk → update, not duplicate.+ await upsert_git_commits_bulk(+ async_session, repo.id, [_row("dup", risk=9.0, ts=1000, change_risk_level="high")]+ )+ await async_session.commit()++ assert await count_git_commits(async_session, repo.id) == 1+ got = await get_git_commit(async_session, repo.id, "dup")+ assert got.change_risk_score == 9.0+ assert got.change_risk_level == "high"+++async def test_delete_git_commits(async_session) -> None:+ repo = await insert_repo(async_session)+ await upsert_git_commits_bulk(async_session, repo.id, [_row("x", risk=1.0, ts=1000)])+ await async_session.commit()+ await delete_git_commits(async_session, repo.id)+ await async_session.commit()+ assert await count_git_commits(async_session, repo.id) == 0diff --git a/tests/unit/persistence/test_models.py b/tests/unit/persistence/test_models.pyindex e6d04119c..445d8b0a7 100644--- a/tests/unit/persistence/test_models.py+++ b/tests/unit/persistence/test_models.py@@ -300,6 +300,7 @@ def test_base_includes_all_models():"webhook_events","wiki_symbols","git_metadata",+ "git_commits","dead_code_findings","decision_records","decision_evidence",diff --git a/tests/unit/test_git_commit_rows.py b/tests/unit/test_git_commit_rows.pynew file mode 100644index 000000000..a8967971b--- /dev/null+++ b/tests/unit/test_git_commit_rows.py@@ -0,0 +1,255 @@+"""Unit tests for per-commit row collection + change-risk building.++Covers the ``commit_sink`` collector on the commit-index walk and the pure+``build_commit_rows`` transform (Kamei features, in-memory author experience,+just-in-time change-risk, ordering, truncation).+"""++from __future__ import annotations++import time+from unittest.mock import MagicMock++from repowise.core.ingestion.git_commit_index import load_commit_index+from repowise.core.ingestion.git_indexer.commit_rows import build_commit_rows+++def _build_log(commits: list[dict]) -> str:+ lines: list[str] = []+ for c in commits:+ lines.append(+ "\x00"+ + c["sha"]+ + "\x1f"+ + c["an"]+ + "\x1f"+ + c["ae"]+ + "\x1f"+ + str(c["ct"])+ + "\x1f"+ + c.get("parents", "")+ + "\x1f"+ + c["subj"]+ + "\x1f"+ + c.get("body", "")+ )+ for added, deleted, path in c["files"]:+ lines.append(f"{added}\t{deleted}\t{path}")+ return "\n".join(lines)+++# ---------------------------------------------------------------------------+# commit_sink on load_commit_index+# ---------------------------------------------------------------------------+++def test_commit_sink_collects_full_footprint_including_non_indexable() -> None:+ """The sink records every file in the commit, not just the indexable set,+ so change diffusion is measured against the real footprint."""+ now = int(time.time())+ raw = _build_log(+ [+ {+ "sha": "aaa",+ "an": "Alice",+ "ae": "[email protected]",+ "ct": now,+ "subj": "feat: thing",+ # src/a.py is indexable; docs/x.md and gen.lock are NOT.+ "files": [(5, 1, "src/a.py"), (3, 0, "docs/x.md"), (9, 9, "gen.lock")],+ },+ ]+ )+ repo = MagicMock()+ repo.git.log.return_value = raw++ sink: list[dict] = []+ index = load_commit_index(repo, 100, {"src/a.py"}, commit_sink=sink)++ # Bucket still only carries the indexable file (unchanged behaviour).+ assert set(index.keys()) == {"src/a.py"}+ # Sink carries the commit with ALL three files.+ assert len(sink) == 1+ assert sink[0]["sha"] == "aaa"+ assert sorted(p for p, _a, _d in sink[0]["changes"]) == [+ "docs/x.md",+ "gen.lock",+ "src/a.py",+ ]+++def test_commit_sink_default_none_is_noop() -> None:+ """Without a sink the return value and behaviour are unchanged."""+ raw = _build_log(+ [+ {+ "sha": "aaa",+ "an": "A",+ "ae": "a@x",+ "ct": 1000,+ "subj": "x",+ "files": [(1, 0, "src/a.py")],+ }+ ]+ )+ repo = MagicMock()+ repo.git.log.return_value = raw+ index = load_commit_index(repo, 100, {"src/a.py"})+ assert set(index.keys()) == {"src/a.py"}+++# ---------------------------------------------------------------------------+# build_commit_rows+# ---------------------------------------------------------------------------+++def test_build_commit_rows_features_and_ordering() -> None:+ # newest-first input (as the walk yields).+ parsed = [+ {+ "sha": "newer",+ "author_name": "Ann",+ "author_email": "ann@x",+ "ts": 2000,+ "subject": "feat: add",+ "changes": [("pkg/a.py", 5, 5)],+ },+ {+ "sha": "older",+ "author_name": "Ann",+ "author_email": "ann@x",+ "ts": 1000,+ "subject": "fix: bug",+ "changes": [("pkg/a.py", 10, 2), ("pkg/sub/b.py", 3, 0)],+ },+ ]+ rows = build_commit_rows(parsed)++ # Order preserved (newest-first).+ assert [r["sha"] for r in rows] == ["newer", "older"]++ older = next(r for r in rows if r["sha"] == "older")+ assert older["lines_added"] == 13+ assert older["lines_deleted"] == 2+ assert older["files_changed"] == 2+ assert older["dirs_changed"] == 2 # "pkg" and "pkg/sub"+ assert older["subsystems_changed"] == 1 # top-level "pkg"+ assert older["is_fix"] is True+ assert 0.0 <= older["change_risk_score"] <= 10.0+ assert older["change_risk_level"] in {"low", "moderate", "high"}++ newer = next(r for r in rows if r["sha"] == "newer")+ assert newer["is_fix"] is False+++def test_build_commit_rows_author_experience_is_cumulative() -> None:+ """Experience = the author's prior-commit count, oldest→newest."""+ parsed = [+ {+ "sha": "c3",+ "author_name": "Ann",+ "author_email": "ann@x",+ "ts": 3000,+ "subject": "x",+ "changes": [("a.py", 1, 0)],+ },+ {+ "sha": "c2",+ "author_name": "Ann",+ "author_email": "ann@x",+ "ts": 2000,+ "subject": "x",+ "changes": [("a.py", 1, 0)],+ },+ {+ "sha": "c1",+ "author_name": "Ann",+ "author_email": "ann@x",+ "ts": 1000,+ "subject": "x",+ "changes": [("a.py", 1, 0)],+ },+ {+ "sha": "b1",+ "author_name": "Bob",+ "author_email": "bob@x",+ "ts": 1500,+ "subject": "x",+ "changes": [("a.py", 1, 0)],+ },+ ]+ rows = build_commit_rows(parsed)+ # Re-derive exp via the change features by re-scoring would be indirect;+ # instead assert the monotonic relationship through risk's exp driver is+ # consistent — simplest: rebuild and check the experience tally by proxy.+ # Ann's three commits have exp 0,1,2 in time order; Bob's single commit 0.+ # We can't read exp off the row directly, so assert via a fresh computation+ # mirroring the implementation contract: the earliest Ann commit must score+ # at least as risky (lower exp ⇒ higher risk, exp coef is protective).+ by_sha = {r["sha"]: r for r in rows}+ assert by_sha["c1"]["change_risk_score"] >= by_sha["c3"]["change_risk_score"]+++def test_build_commit_rows_truncates_subject() -> None:+ parsed = [+ {+ "sha": "a",+ "author_name": "A",+ "author_email": "a@x",+ "ts": 1000,+ "subject": "z" * 5000,+ "changes": [("a.py", 1, 0)],+ },+ ]+ rows = build_commit_rows(parsed)+ assert len(rows[0]["subject"]) == 500+++def test_build_commit_rows_handles_empty_and_no_changes() -> None:+ assert build_commit_rows([]) == []+ rows = build_commit_rows(+ [+ {+ "sha": "a",+ "author_name": "A",+ "author_email": "a@x",+ "ts": 1000,+ "subject": "merge artifact",+ "changes": [],+ }+ ]+ )+ assert rows[0]["files_changed"] == 0+ assert rows[0]["lines_added"] == 0+ assert 0.0 <= rows[0]["change_risk_score"] <= 10.0+++def test_build_commit_rows_committed_at_parsed() -> None:+ rows = build_commit_rows(+ [+ {+ "sha": "a",+ "author_name": "A",+ "author_email": "a@x",+ "ts": 1_700_000_000,+ "subject": "x",+ "changes": [("a.py", 1, 0)],+ }+ ]+ )+ assert rows[0]["committed_at"] is not None+ assert rows[0]["committed_at"].year == 2023+ # ts <= 0 → None (no fabricated timestamp).+ rows0 = build_commit_rows(+ [+ {+ "sha": "b",+ "author_name": "A",+ "author_email": "a@x",+ "ts": 0,+ "subject": "x",+ "changes": [("a.py", 1, 0)],+ }+ ]+ )+ assert rows0[0]["committed_at"] is None