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.
This commit is contained in:
Lucian Tennyson
2026-04-27 17:53:10 -04:00
parent 4ffd0bd57a
commit d12c96e43e
2 changed files with 46 additions and 2 deletions
+8 -1
View File
@@ -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)
+38 -1
View File
@@ -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}"}