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): scan code-punctuation literals and keep them out of path mode (#3721)
Amanc8ddda5df640
diff --git a/packages/server/src/repowise/server/mcp_server/_line_hits.py b/packages/server/src/repowise/server/mcp_server/_line_hits.pyindex 6f612637f..03b089f25 100644--- a/packages/server/src/repowise/server/mcp_server/_line_hits.py+++ b/packages/server/src/repowise/server/mcp_server/_line_hits.py@@ -49,6 +49,7 @@ from repowise.server.mcp_server._helpers import ()from repowise.server.mcp_server._meta import served_rootfrom repowise.server.mcp_server._query_shape import (+ _NL_QUESTION_TOKENS,_canonical_symbol_query,_identifier_candidates,_qual_norm,@@ -80,19 +81,35 @@ _KIND_ORDER = {"definition": 0, "import": 1, "call": 2, "reference": 3, "match":_MIXED_KIND_ORDER = {"definition": 0, "call": 1, "import": 2, "reference": 3, "match": 3}_QUOTED = re.compile(r"""^(["'`])(.+)\1$""")_TOKEN = re.compile(r"^[A-Za-z0-9_$][\w$.]*$")+# Code punctuation: a leading decorator ``@``, an identifier glued to an opening+# ``(`` or ``{`` (``send(``), or a glued ``=`` (``path="/x"``). Prose such as+# ``Add (optional) support`` has a space before its parenthesis and matches none.+_CODE_PUNCT = re.compile(r"^@\w|[\w)\]][({]|\S=\S")_DECL_WORDS = r"def|class|function|const|let|var|fn|func|type|interface|struct|enum"def _literal(query: str) -> tuple[str, bool] | None:- """``(needle, word_bounded)`` for a single-token query, else ``None``."""+ """``(needle, word_bounded)`` for a single-token or code-shaped query, else ``None``."""q = query.strip()if m := _QUOTED.match(q):return m.group(2), Falseif len(q) >= 2 and _TOKEN.match(q) and not q.endswith("."):return q, True+ if _is_code_shaped(q):+ return q, Falsereturn None+def _is_code_shaped(q: str) -> bool:+ """A one-line, non-question query carrying code punctuation, scanned as-is."""+ if len(q) < 3 or "\n" in q or q.endswith("?"):+ return False+ words = q.split()+ if words[0].lower().rstrip(",.;:") in _NL_QUESTION_TOKENS:+ return False+ return _CODE_PUNCT.search(q) is not None++def _definition_re(needle: str, word: bool) -> re.Pattern[str]:"""A line declaring *needle* as a name, or assigning it as a value."""esc = re.escape(needle)@@ -406,6 +423,10 @@ async def _attach(returnliteral = _literal(query)candidates = _identifier_candidates(query, mode, names)+ if literal and not literal[1] and not _QUOTED.match(query.strip()):+ # Code punctuation names the text itself: an identifier inside it+ # (``copy_with`` in ``copy_with(path="/x")``) is not the question.+ candidates = []if not candidates and literal and literal[1]:candidates = [literal[0]]if not candidates and not literal:diff --git a/packages/server/src/repowise/server/mcp_server/_query_shape.py b/packages/server/src/repowise/server/mcp_server/_query_shape.pyindex 6ed9d660a..6491cd50c 100644--- a/packages/server/src/repowise/server/mcp_server/_query_shape.py+++ b/packages/server/src/repowise/server/mcp_server/_query_shape.py@@ -46,6 +46,24 @@ _NL_QUESTION_TOKENS = frozenset()+# Code punctuation a real path almost never carries: a call, a decorator+# argument, a keyword argument or an annotation. ``mode="path"`` still reaches+# such a path when someone really means one.+_CODE_PUNCT_CHARS = '("{='+# npm-scoped paths (``@angular/core/src/x.ts``) lead with ``@`` but are paths.+_SCOPED_PACKAGE = re.compile(r"^@[\w.-]+/[\w.-]+")+# Next.js route groups (``app/(auth)/page.tsx``) carry parentheses legitimately.+_ROUTE_GROUP = re.compile(r"[/\\]\([\w.-]+\)(?=[/\\])")+++def _has_code_punctuation(stripped: str) -> bool:+ """A whitespace-free string shaped like code (``f(x)``, ``@dec``, ``k="v"``)."""+ probe = _ROUTE_GROUP.sub("/", stripped)+ if any(ch in probe for ch in _CODE_PUNCT_CHARS):+ return True+ return stripped.startswith("@") and not _SCOPED_PACKAGE.match(stripped)++def _is_path(query: str) -> bool:"""Heuristic: does this string look like a file or module path?@@ -77,6 +95,10 @@ def _is_path(query: str) -> bool:if any(ch.isspace() for ch in stripped):return False+ # A call, decorator or keyword argument is code, even with a "/" inside.+ if _has_code_punctuation(stripped):+ return False+if "/" in stripped or "\\" in stripped:return True_, ext = os.path.splitext(stripped)diff --git a/tests/unit/server/mcp/test_search_line_hits.py b/tests/unit/server/mcp/test_search_line_hits.pyindex f9f3d7320..cc1b2428f 100644--- a/tests/unit/server/mcp/test_search_line_hits.py+++ b/tests/unit/server/mcp/test_search_line_hits.py@@ -703,3 +703,43 @@ def test_token_nowhere_in_the_repo_is_complete_and_empty(scan_repo):@pytest.mark.parametrize("needle", ["a\nb", "a\0b", "-e", "café"])def test_needles_git_cannot_take_as_one_argument_use_the_python_scan(scan_repo, needle):assert _line_hits._files_holding(scan_repo, needle) is None+++ "query, expected",+ [+ ("@pytest.mark.anyio", ("@pytest.mark.anyio", False)),+ ("def send(", ("def send(", False)),+ ('copy_with(path="/echo_headers")', ('copy_with(path="/echo_headers")', False)),+ ('@GetMapping("/owners/{ownerId}")', ('@GetMapping("/owners/{ownerId}")', False)),+ ("copy_with", ("copy_with", True)),+ ('"x y"', ("x y", False)),+ ("Add (optional) support", None),+ ("how does foo(x) work", None),+ ("what is x=1?", None),+ ("two words", None),+ ],+)+def test_literal_reads_code_punctuation_as_a_substring(query, expected):+ assert _line_hits._literal(query) == expected+++async def test_punctuated_literals_return_their_lines(tree):+ (tree / "app" / "svc.py").write_text(+ "@pytest.mark.anyio\nasync def test_x():\n pass\n\nclass C:\n def send(self):\n pass\n",+ encoding="utf-8",+ )+ git = ["git", "-C", str(tree), "-c", "user.name=t", "-c", "user.email=t@t"]+ subprocess.run([*git, "add", "app/svc.py"], check=True)+ subprocess.run([*git, "commit", "-q", "-m", "svc"], check=True)++ deco = await search_codebase("@pytest.mark.anyio")+ assert [(r["path"], r["line"]) for r in deco["lines"]] == [("app/svc.py", 1)]++ head = await search_codebase("def send(")+ assert [(r["path"], r["line"]) for r in head["lines"]] == [("app/svc.py", 6)]++ # An identifier inside the literal (``backend_of``) does not turn it into a symbol query.+ call = await search_codebase("backend_of('m')")+ assert [(r["path"], r["line"]) for r in call["lines"]] == [("app/widget.py", 6)]diff --git a/tests/unit/server/mcp/test_search_query_shape.py b/tests/unit/server/mcp/test_search_query_shape.pyindex 88517c827..0c9e17850 100644--- a/tests/unit/server/mcp/test_search_query_shape.py+++ b/tests/unit/server/mcp/test_search_query_shape.py@@ -43,6 +43,19 @@ from repowise.server.mcp_server.tool_search import (("anything", "PATH", "path"),("how it works", "bogus", "concept"),("", None, "concept"),+ # Code punctuation is not a path, even with a "/" inside.+ ('copy_with(path="/echo_headers")', None, "hybrid"),+ ('@GetMapping("/owners/{ownerId}")', None, "hybrid"),+ ("@pytest.mark.anyio", None, "concept"),+ ("def send(", None, "concept"),+ ("Add (optional) support", None, "concept"),+ # Paths, and the explicit mode, are untouched.+ ("httpx/_client.py", None, "path"),+ ("src/a/b.ts", None, "path"),+ ("@angular/core/src/x.ts", None, "path"),+ ("app/(auth)/page.tsx", None, "path"),+ ('copy_with(path="/x")', "path", "path"),+ ("copy_with", None, "symbol"),],)def test_resolve_mode(query: str, mode: str | None, expected: str) -> None: