Skip to content

Commit a8cb521

Browse files
jpheinclaude
andcommitted
fix: ALLCAPS keyword extraction, log fallback errors, report attempted keywords
Addresses Copilot review feedback on #662: - _extract_keyword() now tokenizes BEFORE lowercasing so isupper() works - Replace bare `except: pass` with logger.debug in keyword fallback - Report keyword_fallback whenever a keyword was attempted, not only on hits Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent f2ab902 commit a8cb521

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

mempalace/searcher.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ def _extract_keyword(query: str) -> str:
2929
Picks the longest non-stopword token. Prefers tokens that look like
3030
identifiers (contain digits, dots, underscores, or are ALLCAPS).
3131
"""
32-
tokens = re.findall(r"[\w.]+", query.lower())
33-
candidates = [t for t in tokens if t not in _STOPWORDS and len(t) > 2]
32+
raw_tokens = re.findall(r"[\w.]+", query)
33+
# Prefer ALLCAPS identifier-like tokens (before lowercasing loses the signal)
34+
upper_tokens = [t for t in raw_tokens if t.isupper() and len(t) > 2]
35+
if upper_tokens:
36+
return upper_tokens[0]
37+
# Fall back to longest non-stopword token (lowercased)
38+
lower_tokens = [t.lower() for t in raw_tokens]
39+
candidates = [t for t in lower_tokens if t not in _STOPWORDS and len(t) > 2]
3440
if not candidates:
3541
return ""
3642
# Prefer identifier-looking tokens (error codes, config keys, etc.)
37-
ids = [t for t in candidates if re.search(r"\d|_|\.", t) or t.isupper()]
43+
ids = [t for t in candidates if re.search(r"\d|_|\.", t)]
3844
if ids:
3945
return max(ids, key=len)
4046
return max(candidates, key=len)
@@ -206,7 +212,7 @@ def search_memories(
206212
for kid, kdoc, kmeta, kdist in zip(kw_ids, kw_docs, kw_metas, kw_dists):
207213
kw_hits_by_id[kid] = (kdoc, kmeta, kdist)
208214
except Exception:
209-
pass # keyword fallback is best-effort
215+
logger.debug("Keyword fallback query failed for keyword=%r", kw)
210216

211217
# Build hit list from vector results
212218
seen_ids = set()
@@ -248,6 +254,6 @@ def search_memories(
248254
return {
249255
"query": query,
250256
"filters": {"wing": wing, "room": room},
251-
"keyword_fallback": kw if kw_hits_by_id else None,
257+
"keyword_fallback": kw if kw else None,
252258
"results": hits,
253259
}

0 commit comments

Comments
 (0)