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(search): match path queries against filename words (#3180)
Raghav Chamadiya82f4ebb2aaaf
diff --git a/packages/server/src/repowise/server/mcp_server/_retrieval_rank.py b/packages/server/src/repowise/server/mcp_server/_retrieval_rank.pyindex 921299e9f..afc798e24 100644--- a/packages/server/src/repowise/server/mcp_server/_retrieval_rank.py+++ b/packages/server/src/repowise/server/mcp_server/_retrieval_rank.py@@ -159,10 +159,10 @@ _PATH_NAME_LIFT_CAP = 1.0_MIN_INDEXED_PATHS = 30-def path_words(path: str) -> set[str]:+def path_words(path: str, *, min_len: int = 3) -> set[str]:"""``services/_hotkey_pynput.py`` -> ``{services, hotkey, pynput}``; theextension is not a word."""- return {w for w in _identifier_words(os.path.splitext(path)[0]) if len(w) >= 3}+ return {w for w in _identifier_words(os.path.splitext(path)[0]) if len(w) >= min_len}def path_word_counts(paths: list[str]) -> Counter[str]:diff --git a/packages/server/src/repowise/server/mcp_server/tool_search_symbols.py b/packages/server/src/repowise/server/mcp_server/tool_search_symbols.pyindex 339ec1aa5..8c7c30fc4 100644--- a/packages/server/src/repowise/server/mcp_server/tool_search_symbols.py+++ b/packages/server/src/repowise/server/mcp_server/tool_search_symbols.py@@ -20,6 +20,7 @@ from __future__ import annotationsimport refrom collections import Counter+from collections.abc import Sequencefrom typing import Any, NamedTuplefrom sqlalchemy import case, func, or_, select@@ -43,7 +44,11 @@ from repowise.server.mcp_server._query_shape import (is_issue_shaped,path_tokens,)-from repowise.server.mcp_server._retrieval_rank import path_word_counts+from repowise.server.mcp_server._retrieval_rank import (+ _identifier_words,+ path_word_counts,+ path_words,+)from repowise.server.mcp_server._stack_trace import (frame_basenames,map_to_repo_paths,@@ -420,6 +425,39 @@ def _path_score(target_path: str, qnorm: str) -> float:return score+def _word_path_score(path: str, words: list[str]) -> float | None:+ """Score ``path`` when every query word names one of its words (the last+ may be a prefix), else ``None``. Below any substring hit: filename words+ count above directory words, shorter paths first."""+ low = path.lower()+ if not all(w in low for w in words):+ return None+ every = path_words(path, min_len=1)+ base = path_words(path.rsplit("/", 1)[-1], min_len=1)++ def names(i: int, pool: set[str]) -> bool:+ w = words[i]+ return w in pool or (i == len(words) - 1 and any(p.startswith(w) for p in pool))++ if not all(names(i, every) for i in range(len(words))):+ return None+ in_base = sum(names(i, base) for i in range(len(words)))+ return 5.0 + 5.0 * in_base / len(words) - len(low) * 0.001+++def _word_path_hits(query: str, paths: Sequence[str]) -> dict[str, float]:+ """Paths a spaced or compound query names word by word (``poll backoff``+ -> ``command-poll-backoff.ts``), best ``_MAX_CANDIDATES`` by score. Glob+ queries are left to substring matching."""+ if "*" in query or "?" in query:+ return {}+ words = _identifier_words(query)+ if not words:+ return {}+ scored = {p: s for p in paths if (s := _word_path_score(p, words)) is not None}+ return dict(sorted(scored.items(), key=lambda kv: (-kv[1], kv[0]))[:_MAX_CANDIDATES])++class PathIndex(NamedTuple):paths: tuple[str, ...] # every file page's pathword_counts: Counter[str] # path word -> how many paths carry it@@ -445,7 +483,7 @@ async def search_paths_single(ctx: Any, query: str, limit: int) -> list[dict]:Only the path-shaped words of ``query`` are matched (``path_tokens``), sowords around a path do not empty the result; the whole query is matched- when it carries none.+ when it carries none, as a substring and then word by word."""# Path mode is substring matching, so boundary ``*?`` markers carry no# information and are stripped. Mid-string globs (``src/*/main.py``) are@@ -454,10 +492,16 @@ async def search_paths_single(ctx: Any, query: str, limit: int) -> list[dict]:async with get_session(ctx.session_factory) as session:repository = await _get_repo(session)index = await file_path_index(session, repository.id)- tokens = path_tokens(query, index.paths) or [query]- qnorms = [q for q in (t.strip().lower().replace("\\", "/").strip("*?") for t in tokens) if q]+ tokens = path_tokens(query, index.paths)+ qnorms = [+ q+ for q in (t.strip().lower().replace("\\", "/").strip("*?") for t in tokens or [query])+ if q+ ]if not qnorms:return []+ # A query carrying no path also matches paths by their words.+ word_hits = {} if tokens else _word_path_hits(query, index.paths)res = await session.execute(select(Page.id, Page.title, Page.target_path, Page.freshness_status).where(Page.repository_id == repository.id,@@ -466,7 +510,8 @@ async def search_paths_single(ctx: Any, query: str, limit: int) -> list[dict]:*(Page.target_path.ilike(f"%{escape_like(q)}%", escape=LIKE_ESCAPE)for q in qnorms- )+ ),+ Page.target_path.in_(list(word_hits)),),))@@ -479,9 +524,9 @@ async def search_paths_single(ctx: Any, query: str, limit: int) -> list[dict]:continueif is_excluded(target_path, spec):continue- scored.append(- (max(_path_score(target_path, q) for q in qnorms), (page_id, title, target_path))- )+ substring = max(_path_score(target_path, q) for q in qnorms)+ score = max(substring, word_hits.get(target_path, substring))+ scored.append((score, (page_id, title, target_path)))scored.sort(key=lambda pair: (-pair[0], pair[1][2]))return [diff --git a/tests/unit/server/mcp/test_search.py b/tests/unit/server/mcp/test_search.pyindex 52f0b8edd..de59a51c8 100644--- a/tests/unit/server/mcp/test_search.py+++ b/tests/unit/server/mcp/test_search.py@@ -167,6 +167,78 @@ async def test_path_search_preserves_non_trailing_glob_behavior(setup_mcp, queryassert [item["file"] for item in result] == expected+_WORD_PATHS = [+ "src/agents/command-poll-backoff.ts",+ "lib/poll_backoff.py",+ "web/pollBackoff.js",+ "Services/PollBackoff.cs",+ "net/poll.backoff.go",+ "poll/backoff/index.ts",+ "src/agents/poll.ts",+ "src/agents/backoff-retry.ts",+]+++async def _path_search(query, paths=_WORD_PATHS, limit=10):+ import types++ import repowise.server.mcp_server as mcp_mod+ from repowise.server.mcp_server.tool_search_symbols import search_paths_single++ for path in paths:+ await _seed_page(f"file_page:{path}", path)+ ctx = types.SimpleNamespace(session_factory=mcp_mod._session_factory, path="/tmp/test-repo")+ return [item["file"] for item in await search_paths_single(ctx, query, limit=limit)]+++async def test_path_words_match_every_naming_shape(setup_mcp):+ """Spaced words match kebab, snake, camel, Pascal and dotted filenames,+ filename hits above a path whose directories carry the words."""+ files = await _path_search("poll backoff")+ assert set(files[:5]) == {+ "lib/poll_backoff.py",+ "web/pollBackoff.js",+ "net/poll.backoff.go",+ "Services/PollBackoff.cs",+ "src/agents/command-poll-backoff.ts",+ }+ assert files[5:] == ["poll/backoff/index.ts"]+++ ("query", "expected"),+ [+ ("poll backof", "lib/poll_backoff.py"), # the last word may be a prefix+ ("Poll Backoff", "lib/poll_backoff.py"),+ ("pollBackoff", "lib/poll_backoff.py"),+ ],+)+async def test_path_words_prefix_case_and_compound_queries(setup_mcp, query, expected):+ files = await _path_search(query)+ assert expected in files+ assert "src/agents/poll.ts" not in files++[email protected]("query", ["poll jitter", "pol backoff", "agents retry backoff poll"])+async def test_path_words_all_must_match(setup_mcp, query):+ assert await _path_search(query) == []+++async def test_substring_hits_rank_above_word_hits(setup_mcp):+ files = await _path_search("pollBackoff")+ assert files[:2] == ["web/pollBackoff.js", "Services/PollBackoff.cs"]+ assert "lib/poll_backoff.py" in files[2:]+++async def test_exact_path_query_is_unchanged(setup_mcp):+ assert await _path_search("src/agents/poll.ts") == ["src/agents/poll.ts"]++class TestDecisionDownweight:"""Decision records must not crowd file pages out of the top ranks."""