rohitsar567 Claude Opus 4.7 (1M context) commited on
Commit
7d5e189
·
1 Parent(s): 1d10432

fix(rag): KI-111 — wrap collection.query() to survive corrupted Chroma index

Browse files

User screenshot showed 'Hmm, something went wrong on my end' on every
English chat turn. Local repro of 'Can you help me purchase or find a
health insurance policy?' confirmed rag/retrieve.py:267 raises
ChromaDB InternalError 'Error executing plan: Internal error: Error
finding id' — HNSW index entry pointing to a missing doc, likely from
a partial KI-102 profile-chunk write that corrupted collection state.

KI-107 wrapped only collection.get(); query() had the same failure mode.
Wrap it too — on exception log WARNING + degrade to empty retrieval so
the brain still answers (acknowledging no catalog access) instead of
every turn returning KI-106's generic exception fallback.

Underlying corruption (the index repair / re-ingest) tracked separately.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. rag/retrieve.py +30 -13
rag/retrieve.py CHANGED
@@ -263,20 +263,37 @@ async def retrieve(
263
 
264
  collection = get_collection()
265
 
266
- # Standard retrieval
267
- res = collection.query(
268
- query_embeddings=[query_vec],
269
- n_results=effective_top_k,
270
- where=where,
271
- )
272
-
 
 
 
 
273
  out: list[RetrievedChunk] = []
274
- if res["ids"] and res["ids"][0]:
275
- for cid, doc, meta, dist in zip(
276
- res["ids"][0], res["documents"][0],
277
- res["metadatas"][0], res["distances"][0],
278
- ):
279
- out.append(_build_chunk(cid, doc, meta, 1.0 - dist))
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
  # Profile boost pass — when the orchestrator passes a session_id, look
282
  # up THAT user's profile chunk in Chroma. Inject it at the top of the
 
263
 
264
  collection = get_collection()
265
 
266
+ # Standard retrieval — KI-108 (2026-05-15) wraps collection.query() with
267
+ # the same defensive pattern as KI-107's _safe_collection_get. Live test
268
+ # post-KI-107 caught the bare query raising
269
+ # `chromadb.errors.InternalError: Error executing plan: Internal error:
270
+ # Error finding id` on EVERY user turn (HNSW index entry pointing to a
271
+ # missing doc — likely from a partial KI-102 profile-chunk write
272
+ # corrupting the collection state). KI-107 only wrapped .get(); .query()
273
+ # has the same failure mode. On error we degrade to empty retrieval:
274
+ # the brain still answers (acknowledging it can't access the catalog)
275
+ # instead of the user seeing KI-106's generic "something went wrong"
276
+ # fallback for every message.
277
  out: list[RetrievedChunk] = []
278
+ try:
279
+ res = collection.query(
280
+ query_embeddings=[query_vec],
281
+ n_results=effective_top_k,
282
+ where=where,
283
+ )
284
+ if res["ids"] and res["ids"][0]:
285
+ for cid, doc, meta, dist in zip(
286
+ res["ids"][0], res["documents"][0],
287
+ res["metadatas"][0], res["distances"][0],
288
+ ):
289
+ out.append(_build_chunk(cid, doc, meta, 1.0 - dist))
290
+ except Exception as e:
291
+ import logging
292
+ logging.warning(
293
+ "Chroma collection.query() failed (top_k=%d, where=%s); "
294
+ "degrading to empty retrieval. %s: %s",
295
+ effective_top_k, where, type(e).__name__, str(e)[:300],
296
+ )
297
 
298
  # Profile boost pass — when the orchestrator passes a session_id, look
299
  # up THAT user's profile chunk in Chroma. Inject it at the top of the