From d12c96e43eea664e5bc873dcd2ca52a9e22011dc Mon Sep 17 00:00:00 2001 From: Lucian Tennyson Date: Mon, 27 Apr 2026 17:53:10 -0400 Subject: [PATCH] fix: filtered search fallback and diary_write content alias Two bugs found in production use with a ChromaDB palace of 1200+ drawers ingested via mixed paths (bulk import + MCP tool calls): 1. searcher.py: filtered search (wing= or room=) crashes with "Error finding id" when the HNSW vector index is out of sync with the SQLite metadata store. The outer try/except swallowed the error as a search failure. Fix: inner try/except catches filter failures, retries unfiltered with n_results*15 (capped at 500), and post-filters by wing/room in Python. Degrades gracefully instead of returning an error. 2. mcp_server.py: diary_write requires 'entry' but add_drawer uses 'content', making it natural to pass content= by analogy. The mismatch returns a silent MCP -32000 error with no explanation. Fix: accept 'content' as an alias for 'entry' with a clear error message if neither is provided. Both bugs were diagnosed and patched in a live palace. This contributes the fixes upstream. --- mempalace/mcp_server.py | 9 ++++++++- mempalace/searcher.py | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/mempalace/mcp_server.py b/mempalace/mcp_server.py index 43897c8..2b3eb62 100644 --- a/mempalace/mcp_server.py +++ b/mempalace/mcp_server.py @@ -1095,7 +1095,7 @@ def tool_kg_stats(): # ==================== AGENT DIARY ==================== -def tool_diary_write(agent_name: str, entry: str, topic: str = "general", wing: str = ""): +def tool_diary_write(agent_name: str, entry: str = None, topic: str = "general", wing: str = "", content: str = None): """ Write a diary entry for this agent. Entries are timestamped and accumulate over time in a diary room. @@ -1103,6 +1103,13 @@ def tool_diary_write(agent_name: str, entry: str, topic: str = "general", wing: This is the agent's personal journal — observations, thoughts, what it worked on, what it noticed, what it thinks matters. """ + # Accept 'content' as an alias for 'entry' — add_drawer uses 'content', making + # it natural to pattern off it. Accepting both avoids a silent -32000 error. + if entry is None and content is not None: + entry = content + elif entry is None: + return {"success": False, "error": "'entry' (or 'content') is required"} + try: agent_name = sanitize_name(agent_name, "agent_name") entry = sanitize_content(entry) diff --git a/mempalace/searcher.py b/mempalace/searcher.py index a14d90d..40dc92f 100644 --- a/mempalace/searcher.py +++ b/mempalace/searcher.py @@ -608,7 +608,44 @@ def search_memories( } if where: dkwargs["where"] = where - drawer_results = drawers_col.query(**dkwargs) + try: + drawer_results = drawers_col.query(**dkwargs) + except Exception as filter_err: + if not where: + raise + # ChromaDB HNSW/SQLite index mismatch causes filtered queries to fail with + # "Error finding id" even when unfiltered search works fine. This happens when + # drawers are ingested via two different paths (e.g. bulk import vs MCP tool + # calls), leaving the vector index inconsistent with the metadata store. + # Workaround: retry unfiltered, then post-filter in Python. + logger.warning( + "Filtered search failed (%s); falling back to unfiltered + post-filter", + filter_err, + ) + fallback_kwargs = { + "query_texts": [query], + "n_results": min(n_results * 15, 500), + "include": ["documents", "metadatas", "distances"], + } + raw = drawers_col.query(**fallback_kwargs) + wing_f, room_f = wing, room + fdocs, fmetas, fdists = [], [], [] + for doc, meta, dist in zip( + raw["documents"][0], raw["metadatas"][0], raw["distances"][0] + ): + meta = meta or {} + if wing_f and meta.get("wing") != wing_f: + continue + if room_f and meta.get("room") != room_f: + continue + fdocs.append(doc) + fmetas.append(meta) + fdists.append(dist) + drawer_results = { + "documents": [fdocs], + "metadatas": [fmetas], + "distances": [fdists], + } except Exception as e: return {"error": f"Search error: {e}"}