Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(search): count a one-hump word as an identifier only in code context (#3014)

Raghav Chamadiyac574406b1e9d
diff --git a/packages/server/src/repowise/server/mcp_server/_query_shape.py b/packages/server/src/repowise/server/mcp_server/_query_shape.py
index 0f3fbab55..83258590e 100644
--- a/packages/server/src/repowise/server/mcp_server/_query_shape.py
+++ b/packages/server/src/repowise/server/mcp_server/_query_shape.py
@@ -173,20 +173,69 @@ def _embedded_identifiers(query: str, names: Container[str] | None = None) -> li
(``_looks_like_code_name``) counts even unindexed, so a search can say it
does not exist. ``names`` is tested for the token, then for its lowered
form, so a container that also answers for each name's lowered spelling
- gets the case-insensitive leg.
+ gets the case-insensitive leg. A one-hump word (``Add``, ``API``) is also
+ an English word, so it counts only in code context (``_in_code_context``);
+ all-caps constants (``DEBUG``, ``TIMEOUT``) included.
"""
if names is None:
return _IDENT_TOKEN_RE.findall(query)
return [
- token
- for token in _name_tokens(query)
- if _names_symbol(token, names) or _looks_like_code_name(token)
+ m.group()
+ for m in _name_token_matches(query)
+ if _looks_like_code_name(m.group())
+ or (
+ _names_symbol(m.group(), names)
+ and (not _one_hump(m.group()) or _in_code_context(query, m))
+ )
]
-def _name_tokens(query: str) -> list[str]:
+def _name_token_matches(query: str) -> list[re.Match[str]]:
"""Words that could name a symbol: identifier-shaped, or dotted chains."""
- return [t for t in _WORD_CHAIN_RE.findall(query) if "." in t or _identifier_shaped(t)]
+ return [
+ m
+ for m in _WORD_CHAIN_RE.finditer(query)
+ if "." in m.group() or _identifier_shaped(m.group())
+ ]
+
+
+def _name_tokens(query: str) -> list[str]:
+ return [m.group() for m in _name_token_matches(query)]
+
+
+def _one_hump(token: str) -> bool:
+ """``Add``, ``Client``, ``API``: letters only, one capitalised hump. Go
+ exports plain English words, so prose (``Add support for``) names them."""
+ return token.isalpha() and len(_CAMEL_HUMP_RE.findall(token)) == 1
+
+
+_CALLED_RE = re.compile(r"\(|::") # no space: "Add (optional) support" is prose
+_ASKS_AFTER_RE = re.compile(
+ r"^\s*(?:where\s+is|where's|where\s+are|what\s+does|how\s+is|how\s+does|how\s+do)\s+(?:the\s+)?$",
+ re.IGNORECASE,
+)
+_LOOKUP_VERB_RE = re.compile(r"\b(?:find|show|open|locate)\s+$", re.IGNORECASE)
+_BARE_LEAD_RE = re.compile(r"^\s*(?:the\s+)?$", re.IGNORECASE)
+_KIND_TAIL_RE = re.compile(
+ r"\s+(?:class|struct|interface|trait|function|method|type|enum)\W*$", re.IGNORECASE
+)
+
+
+def _in_code_context(query: str, m: re.Match[str]) -> bool:
+ """Code syntax around the word (backtick span, ``Add(``, ``Client::new``),
+ or a short lookup frame (``where is Session``, ``find Config``,
+ ``the Router struct``). Issue prose ("Add support for", "Add type
+ annotations") fits none. Dotted chains never get here."""
+ before, after = query[: m.start()], query[m.end() :]
+ if before.count("`") % 2 or before.endswith("::") or _CALLED_RE.match(after):
+ return True
+ if _ASKS_AFTER_RE.match(before):
+ return True
+ if len(query.split()) > 3:
+ return False
+ return bool(
+ _LOOKUP_VERB_RE.search(before) or (_BARE_LEAD_RE.match(before) and _KIND_TAIL_RE.match(after))
+ )
def _name_lookup_keys(query: str) -> set[str]:
diff --git a/tests/unit/server/mcp/test_search_query_shape.py b/tests/unit/server/mcp/test_search_query_shape.py
index 1ecdf421b..88517c827 100644
--- a/tests/unit/server/mcp/test_search_query_shape.py
+++ b/tests/unit/server/mcp/test_search_query_shape.py
@@ -123,6 +123,87 @@ def test_resolve_mode_routes_on_validated_identifiers() -> None:
assert _resolve_mode("how does the TypeScript client work", None, _NAMES) == "concept"
+# Go exports plain English words as methods and types.
+_ONE_HUMP = {"Add", "Fixes", "Client", "Do", "API", "HTTPClient", "TypeScript"}
+
+
+ "query",
+ [
+ "Add support for retries in the transport",
+ "Fixes a panic when the pool is closed twice",
+ "Client should retry when the API returns 503",
+ ],
+)
+def test_a_one_hump_word_in_prose_is_not_an_identifier(query) -> None:
+ assert _embedded_identifiers(query, _ONE_HUMP) == []
+ assert _resolve_mode(query, None, _ONE_HUMP) == "concept"
+
+
+ "query, expected",
+ [
+ ("support for `Add` in the transport", ["Add"]),
+ ("why does `the API` reject retries", ["API"]),
+ ("calling Add() twice panics", ["Add"]),
+ ("retries in Client.Do are lost", ["Client.Do"]),
+ ("the transport calls x.Add on close", ["x.Add"]),
+ # Multi-hump names are validated as before, in prose too.
+ ("HTTPClient should retry on TypeScript errors", ["HTTPClient", "TypeScript"]),
+ ],
+)
+def test_a_one_hump_word_in_code_context_is_an_identifier(query, expected) -> None:
+ assert _embedded_identifiers(query, _ONE_HUMP) == expected
+ assert _resolve_mode(query, None, _ONE_HUMP) == "hybrid"
+
+
+_FRAMED = {"Add", "Fixes", "Client", "Session", "Router", "Handler", "Config", "Base64"}
+
+
+ "query, expected",
+ [
+ ("where is Session defined", ["Session"]),
+ ("what does Client do", ["Client"]),
+ ("Session class", ["Session"]),
+ ("the Router struct", ["Router"]),
+ ("how is Handler implemented", ["Handler"]),
+ ("find Config", ["Config"]),
+ ("Client::new", ["Client"]),
+ ("Add::new()", ["Add"]),
+ # A digit makes it more than a word, so it is validated as before.
+ ("decode with Base64 fails", ["Base64"]),
+ ],
+)
+def test_a_one_hump_word_in_a_lookup_frame_is_an_identifier(query, expected) -> None:
+ assert _embedded_identifiers(query, _FRAMED) == expected
+ assert _resolve_mode(query, None, _FRAMED) == "hybrid"
+
+
+ "query",
+ [
+ "Add support for custom headers",
+ "Fixes a panic when the pool is closed",
+ "Client should retry on 503",
+ "Add function to retry",
+ "Add type annotations",
+ "Add class for Config",
+ "Find Client retry bug",
+ "Show Client errors in the UI",
+ "Open Config file fails",
+ "The Client type is wrong",
+ "Fix the Client type error",
+ "Add the Client class",
+ "Show Session timeout in the dashboard",
+ "Add (optional) support for retries",
+ ],
+)
+def test_issue_text_with_one_hump_names_stays_concept(query) -> None:
+ assert _embedded_identifiers(query, _FRAMED) == []
+ assert _resolve_mode(query, None, _FRAMED) == "concept"
+
+
@pytest.mark.parametrize(
"token",
[