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(ingestion): load git indexer exports lazily (#3438)

Cheruku Sri Charan Reddyb76fda879e6c
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/README.md b/packages/core/src/repowise/core/ingestion/git_indexer/README.md
index 0dac4dcb1..2ee84e5b4 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/README.md
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/README.md
@@ -166,7 +166,7 @@ and a per-file rollup on `git_metadata` (`agent_commit_count`,
| 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 |
+| `_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 |
| `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) |
| `agent_provenance.py` | `AgentProvenanceClassifier` — deterministic per-commit agent attribution (local channels, config-extensible) |
@@ -175,7 +175,7 @@ and a per-file rollup on `git_metadata` (`agent_commit_count`,
| `function_blame.py` | per-line blame index → per-function modification counts + line age (FULL tier; feeds `function_hotspot` / `code_age_volatility`) |
| `co_change.py` | `compute_co_changes_and_entropy` — repo-wide decay-weighted pair walk + Hassan HCM |
| `prior_defects.py` | `compute_prior_defects` — windowed bug-fix count per file (leakage-aware, benchmark-mirroring) |
-| `indexer.py` | `GitIndexer` class wiring the above; merges co-change/entropy/prior-defects into per-file metadata; back-compat instance shims |
+| `indexer.py` | `GitIndexer` class wiring the above; merges co-change/entropy/prior-defects into per-file metadata; back-compat instance shims; GitPython noise patch |
| `backfill.py` | `backfill_full_tier` resumable ESSENTIAL→FULL promotion |
(`git_commit_index.py` lives one level up in `ingestion/` — the repo-wide
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py b/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py
index 40f88455c..5f6529480 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/__init__.py
@@ -15,56 +15,108 @@ ESSENTIAL index to FULL afterwards.
This package was split out of a single 1k-line module; the public import path
(``repowise.core.ingestion.git_indexer``) is unchanged.
+Exports load on first access so importing a helper submodule does not load
+the indexer and its GitPython dependencies.
"""
from __future__ import annotations
-from ._constants import (
- GIT_HISTORY_VERSION,
- GIT_HISTORY_VERSION_KEY,
- HOTSPOT_HALFLIFE_DAYS,
- git_history_stale,
- is_fix_commit,
-)
-from .agent_provenance import (
- AgentProvenance,
- AgentProvenanceClassifier,
- classifier_from_repo_config,
-)
-from .backfill import BACKFILL_PHASE, backfill_full_tier
-from .co_change import CoChangeWalk, compute_co_changes_and_entropy
-from .enrich import (
- compute_percentiles,
- count_active_contributors,
- detect_original_path,
- get_blame_ownership,
- is_significant_commit,
- meets_hotspot_floors,
-)
-from .file_history import index_file
-from .fix_events import build_fix_events
-from .fix_shape import SHAPE_KINDS, classify_fix_shape
-from .identity import build_identity_resolver, canonicalize_author_email
-from .indexer import GitIndexer
-from .prior_defects import (
- FixCommit,
- FixWalk,
- PriorDefects,
- collect_fix_commits,
- compute_prior_defects,
-)
-from .records import (
- _FIELD_SEP,
- _LOG_FORMAT,
- _RECORD_SEP,
- GitIndexSummary,
- RenameTrail,
- _CommitRec,
- _extract_rename_paths,
- _parse_commit_record,
- _should_skip_index,
-)
-from .tiers import GitIndexTier
+from typing import TYPE_CHECKING
+
+from ...lazy_exports import lazy_exports
+
+if TYPE_CHECKING:
+ from ._constants import (
+ GIT_HISTORY_VERSION,
+ GIT_HISTORY_VERSION_KEY,
+ HOTSPOT_HALFLIFE_DAYS,
+ git_history_stale,
+ is_fix_commit,
+ )
+ from .agent_provenance import (
+ AgentProvenance,
+ AgentProvenanceClassifier,
+ classifier_from_repo_config,
+ )
+ from .backfill import BACKFILL_PHASE, backfill_full_tier
+ from .co_change import CoChangeWalk, compute_co_changes_and_entropy
+ from .enrich import (
+ compute_percentiles,
+ count_active_contributors,
+ detect_original_path,
+ get_blame_ownership,
+ is_significant_commit,
+ meets_hotspot_floors,
+ )
+ from .file_history import index_file
+ from .fix_events import build_fix_events
+ from .fix_shape import SHAPE_KINDS, classify_fix_shape
+ from .identity import build_identity_resolver, canonicalize_author_email
+ from .indexer import GitIndexer
+ from .prior_defects import (
+ FixCommit,
+ FixWalk,
+ PriorDefects,
+ collect_fix_commits,
+ compute_prior_defects,
+ )
+ from .records import (
+ _FIELD_SEP,
+ _LOG_FORMAT,
+ _RECORD_SEP,
+ GitIndexSummary,
+ RenameTrail,
+ _CommitRec,
+ _extract_rename_paths,
+ _parse_commit_record,
+ _should_skip_index,
+ )
+ from .tiers import GitIndexTier
+
+_EXPORTS: dict[str, str] = {
+ "GIT_HISTORY_VERSION": "._constants",
+ "GIT_HISTORY_VERSION_KEY": "._constants",
+ "HOTSPOT_HALFLIFE_DAYS": "._constants",
+ "git_history_stale": "._constants",
+ "is_fix_commit": "._constants",
+ "AgentProvenance": ".agent_provenance",
+ "AgentProvenanceClassifier": ".agent_provenance",
+ "classifier_from_repo_config": ".agent_provenance",
+ "BACKFILL_PHASE": ".backfill",
+ "backfill_full_tier": ".backfill",
+ "CoChangeWalk": ".co_change",
+ "compute_co_changes_and_entropy": ".co_change",
+ "compute_percentiles": ".enrich",
+ "count_active_contributors": ".enrich",
+ "detect_original_path": ".enrich",
+ "get_blame_ownership": ".enrich",
+ "is_significant_commit": ".enrich",
+ "meets_hotspot_floors": ".enrich",
+ "index_file": ".file_history",
+ "build_fix_events": ".fix_events",
+ "SHAPE_KINDS": ".fix_shape",
+ "classify_fix_shape": ".fix_shape",
+ "build_identity_resolver": ".identity",
+ "canonicalize_author_email": ".identity",
+ "GitIndexer": ".indexer",
+ "FixCommit": ".prior_defects",
+ "FixWalk": ".prior_defects",
+ "PriorDefects": ".prior_defects",
+ "collect_fix_commits": ".prior_defects",
+ "compute_prior_defects": ".prior_defects",
+ "_FIELD_SEP": ".records",
+ "_LOG_FORMAT": ".records",
+ "_RECORD_SEP": ".records",
+ "GitIndexSummary": ".records",
+ "RenameTrail": ".records",
+ "_CommitRec": ".records",
+ "_extract_rename_paths": ".records",
+ "_parse_commit_record": ".records",
+ "_should_skip_index": ".records",
+ "GitIndexTier": ".tiers",
+}
+
+__getattr__, __dir__ = lazy_exports(__name__, _EXPORTS, globals())
__all__ = [
"BACKFILL_PHASE",
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/_constants.py b/packages/core/src/repowise/core/ingestion/git_indexer/_constants.py
index 7e5a1b6da..6a6b3115b 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/_constants.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/_constants.py
@@ -1,4 +1,4 @@
-"""Module constants, regexes, and the GitPython noise patch for git indexing.
+"""Module constants and regexes for git indexing.
Kept in one place so the per-tier modules share a single source of truth for
the commit-depth defaults, decay half-lives, and skip heuristics.
@@ -6,7 +6,6 @@ the commit-depth defaults, decay half-lives, and skip heuristics.
from __future__ import annotations
-import contextlib
import re
from typing import Any
@@ -18,23 +17,6 @@ from ...co_change import (
)
from ..languages.registry import REGISTRY as _LANG_REGISTRY
-# Silence GitPython's _CatFileContentStream.__del__ ValueError spam.
-# When git cat-file streams are GC'd after the subprocess pipe is closed,
-# their __del__ tries to drain remaining bytes and hits a closed file.
-# This is harmless but floods stderr with tracebacks.
-try:
- from git.cmd import _CatFileContentStream
-
- _orig_del = _CatFileContentStream.__del__
-
- def _quiet_del(self: Any) -> None:
- with contextlib.suppress(ValueError, OSError):
- _orig_del(self)
-
- _CatFileContentStream.__del__ = _quiet_del # type: ignore[assignment]
-except Exception:
- pass # git not installed — nothing to patch
-
# Commit message prefixes that are ALWAYS skipped (no signal value).
_HARD_SKIP_PREFIXES = ("Merge ",)
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 d093a4b25..db7bb8b82 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/indexer.py
@@ -47,6 +47,23 @@ from .records import (
)
from .tiers import GitIndexTier
+# Silence GitPython's _CatFileContentStream.__del__ ValueError spam.
+# When git cat-file streams are GC'd after the subprocess pipe is closed,
+# their __del__ tries to drain remaining bytes and hits a closed file.
+# This is harmless but floods stderr with tracebacks.
+try:
+ from git.cmd import _CatFileContentStream
+
+ _orig_del = _CatFileContentStream.__del__
+
+ def _quiet_del(self: Any) -> None:
+ with contextlib.suppress(ValueError, OSError):
+ _orig_del(self)
+
+ _CatFileContentStream.__del__ = _quiet_del # type: ignore[assignment]
+except Exception:
+ pass # git not installed — nothing to patch
+
logger = structlog.get_logger(__name__)
__all__ = ["GitIndexer", "git_worker_count"]
diff --git a/tests/unit/test_git_indexer_lazy_exports.py b/tests/unit/test_git_indexer_lazy_exports.py
new file mode 100644
index 000000000..2aca03f7c
--- /dev/null
+++ b/tests/unit/test_git_indexer_lazy_exports.py
@@ -0,0 +1,76 @@
+"""Cold imports stay light while the git indexer's public exports still work."""
+
+from __future__ import annotations
+
+import subprocess
+import sys
+import textwrap
+
+import pytest
+
+
+def _run_cold(code: str) -> None:
+ result = subprocess.run(
+ [sys.executable, "-c", textwrap.dedent(code)],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+
+
+ "module",
+ [
+ "repowise.core.ingestion.git_indexer",
+ "repowise.core.ingestion.git_indexer.function_blame",
+ "repowise.core.analysis.health.biomarkers.base",
+ ],
+)
+def test_helper_import_does_not_load_gitpython_or_indexer(module: str) -> None:
+ _run_cold(f"""
+ import importlib
+ import sys
+
+ importlib.import_module({module!r})
+ assert "git" not in sys.modules
+ assert "repowise.core.ingestion.git_indexer.indexer" not in sys.modules
+ assert "repowise.core.ingestion.git_indexer.backfill" not in sys.modules
+ """)
+
+
+def test_public_exports_are_discoverable_and_resolve_to_their_defining_modules() -> None:
+ _run_cold("""
+ import importlib
+ import repowise.core.ingestion.git_indexer as package
+
+ assert len(package.__all__) == 40
+ assert set(package.__all__) == set(package._EXPORTS)
+ assert set(package.__all__) <= set(dir(package))
+ for name in package.__all__:
+ value = getattr(package, name)
+ assert getattr(package, name) is value
+ if hasattr(value, "__module__"):
+ defining_module = importlib.import_module(value.__module__)
+ assert getattr(defining_module, name) is value
+ namespace = {}
+ exec("from repowise.core.ingestion.git_indexer import *", namespace)
+ assert set(package.__all__) <= namespace.keys()
+ """)
+
+
+def test_gitpython_noise_patch_is_installed_before_indexer_use() -> None:
+ _run_cold("""
+ from git.cmd import _CatFileContentStream
+ original = _CatFileContentStream.__del__
+ from repowise.core.ingestion.git_indexer import GitIndexer
+ from repowise.core.ingestion.git_indexer import indexer
+
+ assert GitIndexer.__name__ == "GitIndexer"
+ assert indexer._orig_del is original
+ assert _CatFileContentStream.__del__ is indexer._quiet_del
+ def closed_stream(self):
+ raise ValueError("read of closed file")
+ indexer._orig_del = closed_stream
+ _CatFileContentStream.__del__(object())
+ """)