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(suspects): read each change's git text once per server (#3928)
Raghav Chamadiyaa4666dfa59f8
diff --git a/packages/core/src/repowise/core/suspects/git_text.py b/packages/core/src/repowise/core/suspects/git_text.pyindex 4cb2ace94..492684411 100644--- a/packages/core/src/repowise/core/suspects/git_text.py+++ b/packages/core/src/repowise/core/suspects/git_text.py@@ -16,6 +16,8 @@ from __future__ import annotationsimport reimport subprocess+import threading+from collections import OrderedDictfrom collections.abc import Iterablefrom pathlib import Path@@ -31,6 +33,13 @@ _PERSON_TRAILER_RE = re.compile(r"|^[ \t]*[A-Za-z][\w-]*[ \t]*:.*<[^<>\s]+@[^<>\s]+>.*$",re.IGNORECASE | re.MULTILINE,)+#: ``(repo, head sha, symbols read) -> (body, symbols)``. A commit's text never+#: changes, so a long-lived MCP server reads each change once instead of+#: re-running ``git log -U0`` over the whole window on every suspects call.+#: Ceiling: LRU-bounded; one entry is a message body and a few names.+_TEXTS: OrderedDict[tuple[str, str, bool], tuple[str, tuple[str, ...]]] = OrderedDict()+_TEXTS_MAX = 8192+_TEXTS_LOCK = threading.Lock()def strip_person_trailers(body: str) -> str:@@ -76,6 +85,27 @@ def read_change_texts("""repo = Path(repo_path)heads = [(s.strip(), n) for s, n in heads if s and s.strip()]+ keys = {s: (str(repo), s, n <= MAX_DIFF_FILES) for s, n in heads}+ out: dict[str, tuple[str, tuple[str, ...]]] = {}+ with _TEXTS_LOCK:+ for sha, key in keys.items():+ if (hit := _TEXTS.get(key)) is not None:+ _TEXTS.move_to_end(key)+ out[sha] = hit+ read = _read_change_texts(repo, [(s, n) for s, n in heads if s not in out])+ with _TEXTS_LOCK:+ for sha, text in read.items():+ if sha in keys:+ _TEXTS[keys[sha]] = text+ while len(_TEXTS) > _TEXTS_MAX:+ _TEXTS.popitem(last=False)+ out.update(read)+ return out+++def _read_change_texts(+ repo: Path, heads: list[tuple[str, int]]+) -> dict[str, tuple[str, tuple[str, ...]]]:out: dict[str, tuple[str, tuple[str, ...]]] = {}for rec in _log(repo, ["--format=%x00%H%n%b"], [s for s, _ in heads]).split("\x00"):sha, _, body = rec.partition("\n")diff --git a/packages/core/src/repowise/core/suspects/signals.py b/packages/core/src/repowise/core/suspects/signals.pyindex 92168e12d..f1ab05465 100644--- a/packages/core/src/repowise/core/suspects/signals.py+++ b/packages/core/src/repowise/core/suspects/signals.py@@ -88,7 +88,12 @@ class TextSignal:finally:db.close()wanted = list(dict.fromkeys(terms))- matched = {cid: [t for t in wanted if _fts_matches(t, _fts_words(body))] for cid, _, body in rows}+ # Each body is folded and split once, not once per term.+ matched = {+ cid: [t for t in wanted if _fts_matches(t, words)]+ for cid, _, body in rows+ for words in (_fts_words(body),)+ }# Evidence only (the score is BM25's): the rarest matched terms first,# without the repo's own name or filler words that match every row.df = Counter(t for ts in matched.values() for t in ts)diff --git a/tests/unit/test_suspects.py b/tests/unit/test_suspects.pyindex 1da8ad385..3ebcc544b 100644--- a/tests/unit/test_suspects.py+++ b/tests/unit/test_suspects.py@@ -137,6 +137,29 @@ def test_person_trailers_never_reach_the_text(tmp_path):assert "Ann" not in got[head][0] and "Bob" not in got[head][0]+def test_a_change_text_is_read_from_git_once(monkeypatch, tmp_path):+ """A commit's text never changes, so a long-lived server reads each one once."""+ from repowise.core.suspects import git_text++ sha_a, sha_b = "a" * 40, "b" * 40+ asked: list[list[str]] = []++ def fake_log(_repo, args, shas):+ asked.append(list(shas))+ if args[0].startswith("--format"):+ return "".join(f"\x00{s}\nbody {s[0]}\n" for s in shas)+ return "".join(f"\x00{s}\n@@ -1 +1 @@ def f_{s[0]}(\n" for s in shas)++ monkeypatch.setattr(git_text, "_log", fake_log)+ monkeypatch.setattr(git_text, "_TEXTS", type(git_text._TEXTS)())+ first = git_text.read_change_texts(tmp_path, [(sha_a, 1)])+ asked.clear()+ both = git_text.read_change_texts(tmp_path, [(sha_a, 1), (sha_b, 1)])+ assert both[sha_a] == first[sha_a] == ("body a", ("f_a",))+ assert both[sha_b] == ("body b", ("f_b",))+ assert asked == [[sha_b], [sha_b]]++def test_git_never_inherits_stdin_and_cannot_hang(monkeypatch, tmp_path):# Under the stdio MCP server on Windows an inherited stdin pipe blocks git.import subprocess