From 53b5e64f7295766694fcdc804d533663aefb73f4 Mon Sep 17 00:00:00 2001 From: Nishad Date: Mon, 22 Jun 2026 10:48:17 -0700 Subject: [PATCH] fix: prevent partial matches for exact token searches --- src/services/search_service.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/services/search_service.py b/src/services/search_service.py index 0a55eb01f..3a5f13fe2 100644 --- a/src/services/search_service.py +++ b/src/services/search_service.py @@ -2,6 +2,7 @@ import copy import os import json +import re from collections import Counter from typing import Any, Dict from agentd.tool_decorator import tool @@ -22,6 +23,19 @@ _global_search_service = None +def _is_exact_token_query(query: str) -> bool: + """Return True for code/token-like queries that should not allow partial fuzzy matches.""" + if not query or len(query.strip()) < 3: + return False + + query = query.strip() + + if re.search(r"[^a-zA-Z0-9\s]", query): + return True + + return bool(re.search(r"[a-zA-Z]", query) and re.search(r"\d", query)) + + def register_search_service(service: "SearchService") -> None: """ Explicitly register the active search service for the @tool wrapper. @@ -584,8 +598,17 @@ async def embed_with_model(model_name): ) ) } - if exact_files: - chunks = [chunk for chunk in chunks if chunk.get("filename") in exact_files] + + # Determine if we should apply exact-token filtering + should_filter_exact = bool(exact_files) or _is_exact_token_query(query) + + if should_filter_exact: + if exact_files: + # Filter to only chunks from files with exact matches + chunks = [chunk for chunk in chunks if chunk.get("filename") in exact_files] + else: + # No exact matches found for a token-like query - return empty results + chunks = [] def _build_terms_agg(field: str) -> Dict[str, Any]: counts = Counter(