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.

6.4 KB
Raw Blame
"""The update path must build the same graph as the init path.

The incremental rebuild used to skip two edge passes the init pipeline runs:
dynamic hints (never executed on update) and the repo-wide ``co_changes``
edges (re-added only for the changed files, on a graph rebuilt from scratch).
The update-built graph therefore structurally diverged from the init-built
one: metrics persisted by an update disagreed with init's for identical code,
and the first post-init update could never hit the centrality cache.

These tests build both graph shapes over the same small git repo and assert
node/edge convergence plus equality of the centrality-cache signatures (the
exact keys the cache hits on).
"""

from __future__ import annotations

from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch

from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder
from repowise.core.ingestion.git_indexer import GitIndexer
from repowise.core.ingestion.git_indexer.tiers import GitIndexTier
from repowise.core.ingestion.graph._centrality_cache import subgraph_signature
from repowise.core.pipeline.incremental import build_repo_graph, rebuild_graph_and_git


def _build_git_repo(tmp_path: Path) -> None:
    """Two python files co-committed 3x so co_changes edges exist (min_count=3)."""
    import git as gitpython

    repo = gitpython.Repo.init(tmp_path)
    with repo.config_writer() as cw:
        cw.set_value("user", "name", "Alice")
        cw.set_value("user", "email", "[email protected]")

    # a.py and b.py are co-committed but unrelated statically: co_changes
    # edges are only added between files with no existing import edge.
    # c.py imports a.py so the graph also carries a real import edge.
    (tmp_path / "a.py").write_text("x = 1\n")
    (tmp_path / "b.py").write_text("y = 2\n")
    (tmp_path / "c.py").write_text("from a import x\nz = x + 1\n")
    repo.index.add(["a.py", "b.py", "c.py"])
    repo.index.commit("feat: add modules a, b and c")

    for i in range(2, 5):
        (tmp_path / "a.py").write_text(f"x = {i}\n")
        (tmp_path / "b.py").write_text(f"y = {i}\n")
        repo.index.add(["a.py", "b.py"])
        repo.index.commit(f"chore: co-change a and b, round {i}")
    repo.close()


def _build_init_shaped_graph(tmp_path: Path, git_meta_map: dict) -> GraphBuilder:
    """Replicate the init pipeline's graph construction (ingestion phase +
    co-change augmentation), independent of the update-path helpers."""
    from repowise.core.ingestion.dynamic_hints import HintRegistry

    traverser = FileTraverser(tmp_path)
    parser = ASTParser()
    gb = GraphBuilder(tmp_path)
    for fi in traverser.traverse():
        gb.add_file(parser.parse_file(fi, Path(fi.abs_path).read_bytes()))
    gb.build()
    # Framework edges: tech-stack detection on a bare two-file repo adds
    # nothing, but run it anyway to mirror the phase order.
    try:
        from repowise.core.generation.editor_files.tech_stack import detect_tech_stack

        gb.add_framework_edges([item.name for item in detect_tech_stack(tmp_path)])
    except Exception:
        pass
    gb.add_dynamic_edges(HintRegistry().extract_all(tmp_path))
    gb.add_co_change_edges(git_meta_map)
    return gb


def _edge_set(gb: GraphBuilder) -> set[tuple[str, str, str]]:
    g = gb.graph()
    return {(u, v, d.get("edge_type", "")) for u, v, d in g.edges(data=True)}


async def test_update_graph_converges_with_init_graph(tmp_path: Path) -> None:
    _build_git_repo(tmp_path)

    # Init shape: full git index feeds co-change edges for every file.
    init_indexer = GitIndexer(tmp_path, tier=GitIndexTier.FULL)
    _summary, init_meta = await init_indexer.index_repo("repo1")
    init_meta_map = {m["file_path"]: m for m in init_meta}
    init_gb = _build_init_shaped_graph(tmp_path, init_meta_map)

    # Sanity: the fixture actually produces a co_changes edge, otherwise
    # this test cannot detect the divergence it exists to prevent.
    assert any(t == "co_changes" for _, _, t in _edge_set(init_gb))

    # Update shape: one changed file, graph rebuilt by the update path.
    file_diffs = [SimpleNamespace(path="a.py", status="modified")]
    (_pf, _src, upd_gb, _structure, _count, _meta) = await rebuild_graph_and_git(
        tmp_path, file_diffs, cfg={}, exclude_patterns=[], git_tier="full"
    )

    assert set(init_gb.graph().nodes()) == set(upd_gb.graph().nodes())
    assert _edge_set(init_gb) == _edge_set(upd_gb)

    # The exact property the centrality cache keys on: identical file and
    # symbol subgraph signatures, so a first post-init update can hit.
    assert subgraph_signature(init_gb.file_subgraph()) == subgraph_signature(upd_gb.file_subgraph())
    assert subgraph_signature(init_gb.symbol_subgraph()) == subgraph_signature(
        upd_gb.symbol_subgraph()
    )


def test_dynamic_hint_failure_logs_warning_and_does_not_raise(tmp_path: Path) -> None:
    """Dynamic hint extraction errors must log a structured warning and continue best-effort."""
    logged_lines: list[str] = []

    def capture_log(msg: str) -> None:
        logged_lines.append(msg)

    (tmp_path / "main.py").write_text("x = 1\n")

    with patch(
        "repowise.core.ingestion.dynamic_hints.HintRegistry.extract_all",
        side_effect=RuntimeError("Simulated hint extraction crash"),
    ):
        _parsed_files, _source_map, graph_builder, _repo_structure, _count = build_repo_graph(
            repo_path=tmp_path,
            exclude_patterns=[],
            log=capture_log,
        )

    assert graph_builder is not None
    assert any(
        "Dynamic hint extraction failed" in line and "missing from this update" in line
        for line in logged_lines
    )


def test_read_only_build_is_the_update_graph_and_writes_nothing(tmp_path: Path) -> None:
    """The live commands' read-only build is the update path's graph, minus the writes."""
    _build_git_repo(tmp_path)
    index_dir = tmp_path / ".repowise"
    index_dir.mkdir()

    ro_pf, ro_src, ro_gb, _s, ro_count = build_repo_graph(
        tmp_path, [], collect_sources=True, read_only=True
    )
    assert list(index_dir.iterdir()) == []

    pf, src, gb, _s, count = build_repo_graph(tmp_path, [], collect_sources=True)
    assert list(index_dir.iterdir()), "the update path keeps its caches"

    assert (ro_count, ro_src) == (count, src)
    assert [p.file_info.path for p in ro_pf] == [p.file_info.path for p in pf]
    assert set(ro_gb.graph().nodes()) == set(gb.graph().nodes())
    assert _edge_set(ro_gb) == _edge_set(gb)