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
perf(core): parse the index scope once per index state, not once per call (#2272)
Raghav Chamadiyae607a146e4cc
diff --git a/packages/core/src/repowise/core/index_scope.py b/packages/core/src/repowise/core/index_scope.pyindex 197f7af79..a68796503 100644--- a/packages/core/src/repowise/core/index_scope.py+++ b/packages/core/src/repowise/core/index_scope.py@@ -16,12 +16,44 @@ from typing import AnyINDEX_SCOPE_VERSION = 1+# repo dir -> (identity of the files it was built from, scope). Every MCP+# response embeds this, so without a cache each one re-reads state.json and+# re-parses config.yaml. Keyed on mtime+size so an index rebuild invalidates it.+_SCOPE_CACHE: dict[str, tuple[tuple[Any, ...], dict[str, Any] | None]] = {}+_SCOPE_CACHE_MAX = 16+++def _file_identity(path: Path) -> tuple[Any, ...]:+ try:+ stat = path.stat()+ except OSError:+ return ()+ return (stat.st_mtime_ns, stat.st_size)+def load_index_scope(repo_path: str | Path) -> dict[str, Any] | None:"""Load a repository's scope; return ``None`` when no readable state exists."""repowise_dir = Path(repo_path) / ".repowise"+ state_file = repowise_dir / "state.json"+ config_file = repowise_dir / "config.yaml"++ key = str(repowise_dir)+ identity = (_file_identity(state_file), _file_identity(config_file))+ cached = _SCOPE_CACHE.get(key)+ if cached is not None and cached[0] == identity:+ # Callers embed this in a response that later passes get mutated.+ return deepcopy(cached[1])++ scope = _read_index_scope(state_file, config_file)+ if len(_SCOPE_CACHE) >= _SCOPE_CACHE_MAX:+ _SCOPE_CACHE.clear()+ _SCOPE_CACHE[key] = (identity, scope)+ return deepcopy(scope)+++def _read_index_scope(state_file: Path, config_file: Path) -> dict[str, Any] | None:try:- state = json.loads((repowise_dir / "state.json").read_text(encoding="utf-8"))+ state = json.loads(state_file.read_text(encoding="utf-8"))except (OSError, TypeError, ValueError):return None@@ -30,7 +62,7 @@ def load_index_scope(repo_path: str | Path) -> dict[str, Any] | None:except ImportError:return resolve_index_scope(state)try:- config = yaml.safe_load((repowise_dir / "config.yaml").read_text(encoding="utf-8")) or {}+ config = yaml.safe_load(config_file.read_text(encoding="utf-8")) or {}except (OSError, TypeError, ValueError, yaml.YAMLError):config = {}return resolve_index_scope(state, config)diff --git a/tests/unit/core/test_index_scope_cache.py b/tests/unit/core/test_index_scope_cache.pynew file mode 100644index 000000000..c23137726--- /dev/null+++ b/tests/unit/core/test_index_scope_cache.py@@ -0,0 +1,101 @@+"""The scope every MCP response embeds is parsed once per index state."""++from __future__ import annotations++import json+from pathlib import Path++import pytest++from repowise.core import index_scope+from repowise.core.index_scope import load_index_scope++[email protected](autouse=True)+def _clear_cache() -> None:+ index_scope._SCOPE_CACHE.clear()+++def _write(repo: Path, *, run_mode: str = "standard") -> None:+ repowise = repo / ".repowise"+ repowise.mkdir(parents=True, exist_ok=True)+ (repowise / "state.json").write_text(+ json.dumps({"index_scope": {"version": 1, "run_mode": run_mode}}),+ encoding="utf-8",+ )+ (repowise / "config.yaml").write_text("provider: openai\n", encoding="utf-8")+++def test_repeated_reads_do_not_reparse(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ _write(tmp_path)+ assert load_index_scope(tmp_path) is not None++ calls: list[int] = []+ real = index_scope._read_index_scope++ def counted(*args: object, **kwargs: object) -> object:+ calls.append(1)+ return real(*args, **kwargs)++ monkeypatch.setattr(index_scope, "_read_index_scope", counted)+ for _ in range(5):+ load_index_scope(tmp_path)++ assert calls == []+++def test_a_rebuild_invalidates_the_cache(tmp_path: Path) -> None:+ """The easy case: "standard" and "fast" differ in length, so size catches it."""+ _write(tmp_path, run_mode="standard")+ assert load_index_scope(tmp_path)["run_mode"] == "standard"++ _write(tmp_path, run_mode="fast")++ assert load_index_scope(tmp_path)["run_mode"] == "fast"+++def test_a_same_size_rewrite_still_invalidates(tmp_path: Path) -> None:+ """The harder case: identical length, so only mtime can catch it."""+ _write(tmp_path, run_mode="fast")+ assert load_index_scope(tmp_path)["run_mode"] == "fast"++ state = tmp_path / ".repowise" / "state.json"+ size_before = state.stat().st_size+ # "fast" and "slow" are the same length, so st_size cannot tell them apart.+ state.write_text(+ json.dumps({"index_scope": {"version": 1, "run_mode": "slow"}}), encoding="utf-8"+ )+ assert state.stat().st_size == size_before++ # run_mode only stores a known choice, so "slow" resolves to "unknown". What+ # matters here is that the cached "fast" did not survive the rewrite.+ assert load_index_scope(tmp_path)["run_mode"] != "fast"+++def test_callers_cannot_corrupt_the_cache(tmp_path: Path) -> None:+ """Responses embed this dict and later passes mutate it in place."""+ _write(tmp_path)++ first = load_index_scope(tmp_path)+ first["run_mode"] = "MUTATED"+ first.setdefault("nested", {})["added"] = True++ second = load_index_scope(tmp_path)+ assert second["run_mode"] == "standard"+ assert "nested" not in second+++def test_missing_state_is_not_cached_as_a_readable_index(tmp_path: Path) -> None:+ assert load_index_scope(tmp_path) is None++ _write(tmp_path)+ assert load_index_scope(tmp_path) is not None+++def test_cache_does_not_grow_without_bound(tmp_path: Path) -> None:+ for i in range(index_scope._SCOPE_CACHE_MAX + 4):+ repo = tmp_path / f"repo{i}"+ _write(repo)+ load_index_scope(repo)++ assert len(index_scope._SCOPE_CACHE) <= index_scope._SCOPE_CACHE_MAX