Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(git): idle refresh writes whole history rows and never inserts fragments (#2719)

Raghav Chamadiya1f8a1547b2f6
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 18eb54a11..b4852d009 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py
@@ -556,7 +556,9 @@ class GitIndexer:
scores can only ratchet downward and never recover (issue #728). This
recomputes just those fields off the walks already loaded here; the
persist path upserts them field-by-field so ownership / age / authorship
- (correct only from the full init walk) are left intact.
+ (correct only from the full init walk) are left intact. Idle
+ history-tier files get their complete row instead (see
+ ``_compute_idle_decay``).
``timings`` is the run's phase table; each walk below records a
``rebuild.git.*`` row so a slow git step names itself.
@@ -568,12 +570,6 @@ class GitIndexer:
# idle refresh minted rows for every tracked config and markup file,
# which the health pass then scored: a store grew rows a fresh index
# never has.
- # Non-code files get the same history tier the full index gives them.
- history_files = _history_tier_files(self.repo_path, all_files or changed_file_paths)
- history_changed = history_files.intersection(changed_file_paths)
- changed_file_paths = [fp for fp in changed_file_paths if not _should_skip_index(fp)]
- if all_files:
- all_files = {fp for fp in all_files if not _should_skip_index(fp)}
repo = self._get_repo()
if repo is None:
return []
@@ -581,6 +577,15 @@ class GitIndexer:
with contextlib.suppress(Exception):
repo.close()
return []
+ # Non-code files get the same history tier the full index gives them,
+ # drawn from the same tracked-file set: the parsed files miss .rst,
+ # LICENSE and the like.
+ tier_paths = (self._get_tracked_files(repo) or all_files) if all_files else None
+ history_files = _history_tier_files(self.repo_path, tier_paths or changed_file_paths)
+ history_changed = history_files.intersection(changed_file_paths)
+ changed_file_paths = [fp for fp in changed_file_paths if not _should_skip_index(fp)]
+ if all_files:
+ all_files = {fp for fp in all_files if not _should_skip_index(fp)}
loop = asyncio.get_event_loop()
include_blame = self.tier.includes_blame
@@ -839,6 +844,8 @@ class GitIndexer:
kept so the field-wise upsert leaves full-history columns untouched. A
signal absent from a walk resolves to its recovered baseline (``0`` /
``[]``) so, e.g., a file whose only fix aged out drops to zero.
+ History-tier files are the exception: their row is complete, flagged
+ ``history_only``, and carries no code signal.
"""
out: dict[str, dict] = {}
for fp in idle_paths:
@@ -854,6 +861,12 @@ class GitIndexer:
provenance_classifier=prov_clf,
history_only=fp in history_files,
)
+ if fp in history_files:
+ # The history tier comes only from this shared walk, so this is
+ # the full row a fresh index writes. Persisted whole, it also
+ # backfills stores indexed before non-code files had rows.
+ out[fp] = meta
+ continue
meta["change_entropy"] = walk.entropy.get(fp, 0.0)
meta["co_change_partners_json"] = json.dumps(walk.partners.get(fp, []))
meta["co_change_partner_count"] = walk.partner_count.get(fp, 0)
diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.py
index d6bcbb876..cee8fa96c 100644
--- a/packages/core/src/repowise/core/pipeline/persist.py
+++ b/packages/core/src/repowise/core/pipeline/persist.py
@@ -1743,16 +1743,31 @@ async def persist_git_refresh(
await replace_git_history(session, repo_id, git_meta_map, full_git_summary)
return
+ from sqlalchemy import select
+
from repowise.core.persistence.crud import (
recompute_git_percentiles,
upsert_git_metadata_bulk,
)
+ from repowise.core.persistence.models import GitMetadata
- await upsert_git_metadata_bulk(
- session,
- repo_id,
- [*git_meta_map.values(), *(git_decay_map or {}).values()],
- )
+ decay_rows = list((git_decay_map or {}).values())
+ if decay_rows:
+ stored = set(
+ (
+ await session.execute(
+ select(GitMetadata.file_path).where(GitMetadata.repository_id == repo_id)
+ )
+ ).scalars()
+ )
+ # A decay row is partial, so it only refreshes a stored row: inserted,
+ # it is a fragment with no totals or owner. History-tier rows are
+ # complete and upsert whole, which also fills the non-code rows an
+ # index built before that tier never wrote.
+ decay_rows = [
+ row for row in decay_rows if row.get("history_only") or row["file_path"] in stored
+ ]
+ await upsert_git_metadata_bulk(session, repo_id, [*git_meta_map.values(), *decay_rows])
await recompute_git_percentiles(session, repo_id)
diff --git a/tests/unit/persistence/test_git_decay_refresh.py b/tests/unit/persistence/test_git_decay_refresh.py
new file mode 100644
index 000000000..0538645d5
--- /dev/null
+++ b/tests/unit/persistence/test_git_decay_refresh.py
@@ -0,0 +1,81 @@
+"""The idle-decay refresh never mints fragment rows, and heals stores built before
+non-code files had a history tier."""
+
+from __future__ import annotations
+
+from sqlalchemy import select
+
+from repowise.core.ingestion.git_indexer import GitIndexer
+from repowise.core.ingestion.git_indexer.tiers import GitIndexTier
+from repowise.core.persistence.crud import upsert_git_metadata_bulk
+from repowise.core.persistence.models import GitMetadata
+from repowise.core.pipeline.persist import persist_git_refresh
+from tests.unit.ingestion.test_git_history_tier import _build
+from tests.unit.persistence.helpers import insert_repo
+
+
+async def _rows(session, repo_id: str) -> dict[str, GitMetadata]:
+ result = await session.execute(select(GitMetadata).where(GitMetadata.repository_id == repo_id))
+ return {row.file_path: row for row in result.scalars().all()}
+
+
+async def test_decay_rows_refresh_stored_rows_and_never_insert(async_session) -> None:
+ repo = await insert_repo(async_session)
+ await upsert_git_metadata_bulk(
+ async_session,
+ repo.id,
+ [{"file_path": "a.py", "commit_count_total": 5, "primary_owner_name": "Alice"}],
+ )
+ decay = {
+ "a.py": {"file_path": "a.py", "commit_count_90d": 3},
+ # No stored row: inserting this partial is the fragment bug.
+ "b.py": {"file_path": "b.py", "commit_count_90d": 2},
+ }
+
+ await persist_git_refresh(async_session, repo.id, {}, decay, None)
+
+ rows = await _rows(async_session, repo.id)
+ assert set(rows) == {"a.py"}
+ assert rows["a.py"].commit_count_90d == 3
+ assert rows["a.py"].commit_count_total == 5
+ assert rows["a.py"].primary_owner_name == "Alice"
+
+
+async def test_update_gives_a_pre_tier_store_the_native_history_rows(
+ async_session, tmp_path, monkeypatch
+) -> None:
+ monkeypatch.setenv("REPOWISE_GIT_WINDOW_ANCHOR", "head")
+ _build(tmp_path)
+ _summary, native = await GitIndexer(tmp_path, tier=GitIndexTier.FULL).index_repo("r")
+ native_history = {row["file_path"]: row for row in native if row["history_only"]}
+ assert native_history
+
+ # A store indexed before the history tier: code rows only.
+ repo = await insert_repo(async_session)
+ await upsert_git_metadata_bulk(
+ async_session, repo.id, [row for row in native if not row["history_only"]]
+ )
+
+ sink: dict[str, dict] = {}
+ changed = await GitIndexer(tmp_path, tier=GitIndexTier.FULL).index_changed_files(
+ ["a.py"],
+ # The parsed-file set the update passes holds no markup or config.
+ all_files={"a.py", "b.py"},
+ co_change_sink={},
+ idle_decay_sink=sink,
+ )
+ await persist_git_refresh(
+ async_session, repo.id, {m["file_path"]: m for m in changed}, sink, None
+ )
+
+ rows = await _rows(async_session, repo.id)
+ assert set(rows) == {row["file_path"] for row in native}
+ for path, expected in native_history.items():
+ row = rows[path]
+ assert row.history_only is True
+ assert row.churn_percentile == 0.0 and row.is_hotspot is False
+ for key, value in expected.items():
+ stored = getattr(row, key)
+ if hasattr(value, "tzinfo"):
+ value, stored = value.replace(tzinfo=None), stored.replace(tzinfo=None)
+ assert stored == value, (path, key)