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
fix(why): match archaeology cross-references on whole tokens (#2588)
Raghav Chamadiyab4344f900133
diff --git a/packages/server/src/repowise/server/mcp_server/tool_why/archaeology.py b/packages/server/src/repowise/server/mcp_server/tool_why/archaeology.pyindex b1cfa9440..b43bb1517 100644--- a/packages/server/src/repowise/server/mcp_server/tool_why/archaeology.py+++ b/packages/server/src/repowise/server/mcp_server/tool_why/archaeology.py@@ -9,8 +9,6 @@ import subprocessfrom pathlib import Pathfrom typing import Any-from repowise.server.mcp_server._budget import OmissionCollector-_GIT_LOG_FORMAT = "--format=%H\t%an\t%ai\t%s"@@ -19,7 +17,6 @@ async def _git_archaeology_fallback(git_meta: Any | None,all_git_meta: list,repository: Any,- collector: OmissionCollector | None = None,) -> dict:"""When no decisions govern a file, mine git history for intent signals."""result: dict[str, Any] = {"triggered": True}@@ -76,6 +73,11 @@ def _search_terms(file_path: str) -> tuple[str, str, set[str]]:return basename, stem, search_terms+def _mentions(msg_lower: str, term: str) -> bool:+ """Whether *term* occurs in *msg_lower* as a whole token, so ``auth`` misses ``author``."""+ return re.search(rf"(?<![a-z0-9]){re.escape(term)}(?![a-z0-9])", msg_lower) is not None++def _cross_references(file_path: str, basename: str, search_terms: set[str], all_git_meta: list) -> list[dict[str, Any]]:@@ -88,8 +90,8 @@ def _cross_references(for c in commits:msg_lower = c.get("message", "").lower()# Match if the commit message mentions the file basename or 2+ stem terms- matched_terms = sorted(t for t in search_terms if t in msg_lower)- if basename.lower() in msg_lower or len(matched_terms) >= 2:+ matched_terms = sorted(t for t in search_terms if _mentions(msg_lower, t))+ if _mentions(msg_lower, basename.lower()) or len(matched_terms) >= 2:cross_references.append({"source_file": gm.file_path, **_commit_row(c), "matched_terms": matched_terms})diff --git a/packages/server/src/repowise/server/mcp_server/tool_why/path_mode.py b/packages/server/src/repowise/server/mcp_server/tool_why/path_mode.pyindex 8d00d4f62..b42d9bb48 100644--- a/packages/server/src/repowise/server/mcp_server/tool_why/path_mode.py+++ b/packages/server/src/repowise/server/mcp_server/tool_why/path_mode.py@@ -109,7 +109,7 @@ async def _why_path(query: str, repo: str | None) -> dict:# --- Fallback: git archaeology when no accepted decision governs ---if not governing:await _add_ungoverned_evidence(- result_data, query, git_meta, all_git_meta, repository, collector, ctx.path+ result_data, query, git_meta, all_git_meta, repository, ctx.path)# Episodes are additive, not a fallback: a governed file still has history.@@ -237,7 +237,6 @@ async def _add_ungoverned_evidence(git_meta: Any | None,all_git_meta: list,repository: Any,- collector: OmissionCollector,repo_path: str | Path,) -> None:"""Git archaeology and mined rationale for a file no accepted decision governs."""@@ -246,7 +245,6 @@ async def _add_ungoverned_evidence(git_meta,all_git_meta,repository,- collector,)# The "why" may live in a code comment.rationale = _mine_rationale(@@ -262,7 +260,6 @@ async def _build_target_context(all_decisions: list,target_git: dict[str, Any],targets: list[str],- collector: OmissionCollector | None = None,accepted: set[str] | None = None,) -> dict[str, Any]:"""Per-target governing decisions + origin story, with archaeology fallback.@@ -284,7 +281,6 @@ async def _build_target_context(target_git.get(t),all_git_meta_list,repository,- collector,accepted or set(),)return target_context@@ -311,7 +307,6 @@ async def _target_card(git_m: Any | None,all_git_meta_list: list,repository: Any,- collector: OmissionCollector | None,accepted_ids: set[str],) -> dict[str, Any]:"""One target's card: its rules, its candidates, its origin, and archaeology."""@@ -348,7 +343,6 @@ async def _target_card(git_m,all_git_meta_list,repository,- collector,)return ctx_entry@@ -371,7 +365,7 @@ async def _why_targets(targets: list[str], repo: str | None) -> dict:"mode": "path","paths": targets,"target_context": await _build_target_context(- ctx, repository, all_decisions, target_git, targets, collector, accepted+ ctx, repository, all_decisions, target_git, targets, accepted),"_meta": _build_meta(repository=repository, targets=targets),}diff --git a/packages/server/src/repowise/server/mcp_server/tool_why/search.py b/packages/server/src/repowise/server/mcp_server/tool_why/search.pyindex 5c3ecb90f..afa22214a 100644--- a/packages/server/src/repowise/server/mcp_server/tool_why/search.py+++ b/packages/server/src/repowise/server/mcp_server/tool_why/search.py@@ -182,7 +182,7 @@ async def _why_no_match(if targets:collector = OmissionCollector("get_why", repo_root=ctx.path)result["target_context"] = await _build_target_context(- ctx, repository, all_decisions, target_git, targets, collector, accepted+ ctx, repository, all_decisions, target_git, targets, accepted)rationale = _mine_rationale(ctx.path, targets, query, max_results=1000, truncate_blocks=False@@ -263,7 +263,7 @@ async def _why_search(query: str, targets: list[str] | None, repo: str | None) -if targets:result_data["target_context"] = await _build_target_context(- ctx, repository, all_decisions, target_git, targets, collector, accepted+ ctx, repository, all_decisions, target_git, targets, accepted)# Targets resolve through the node index; without them the question itselfdiff --git a/tests/unit/server/mcp/test_why.py b/tests/unit/server/mcp/test_why.pyindex 69bfa2a64..31211e4fa 100644--- a/tests/unit/server/mcp/test_why.py+++ b/tests/unit/server/mcp/test_why.py@@ -683,3 +683,32 @@ async def test_get_why_path_fits_on_an_ungoverned_file(session, setup_mcp, monke# The fallback really was over the line — otherwise this test proves nothing.assert result["truncated"] is Trueassert len(_json.dumps(result, default=str)) <= effective_char_budget()+++def _meta_with(path: str, *messages: str):+ import json as _json+ from types import SimpleNamespace++ commits = [+ {"sha": f"s{i}", "message": m, "author": "a", "date": f"2026-01-0{i + 1}"}+ for i, m in enumerate(messages)+ ]+ return SimpleNamespace(file_path=path, significant_commits_json=_json.dumps(commits))+++def test_cross_references_match_whole_tokens_not_substrings():+ """``auth`` inside ``author`` is not a mention of an auth file."""+ from repowise.server.mcp_server.tool_why.archaeology import _cross_references, _search_terms++ path = "src/auth_cache.py"+ basename, _stem, terms = _search_terms(path)+ other = _meta_with(+ "src/other.py",+ "Credit the author in the cache docs", # "auth" only inside "author"+ "Invalidate auth cache on logout", # both stem tokens as words+ )++ refs = _cross_references(path, basename, terms, [other])++ assert [r["message"] for r in refs] == ["Invalidate auth cache on logout"]+ assert refs[0]["matched_terms"] == ["auth", "cache"]