Files
evan188199-tech 18619e6fc6 fix(rag): classify invalid persisted index errors and include re-index steps (#1702)
* fix(rag): classify invalid persisted index errors and include re-index steps

Searching a knowledge base whose persisted vector store contains invalid
vectors (issue #440) was misclassified: the ValueError raised at index
load wraps a provider-facing "Details: ..." tail, so search_error_result
matched the embedding-provider branch first and returned
invalid_embedding_provider_response without needs_reindex, blaming the
provider instead of the damaged index.

Check the persisted-index diagnosis first, include the recorded failure
reason (which store file, which vector) in the user-facing answer, and
spell out recovery: run the knowledge base's 'Re-index' action; re-uploading
documents reuses the damaged store and cannot repair it.

* test(codebuddy): make credential fixture expiry relative to now

test_load_credentials_parses_session wrote a hard-coded expiresAt of
1791055241000 (2026-10-03 15:40:41 UTC) and asserted is_expired() is
False. Once that instant passed the assertion flipped, so the test now
fails on every Python version for every branch.

Derive expiresAt / refreshExpiresAt from time.time() so the fixture
always describes a live session, and assert against the value written.

---------

Co-authored-by: XZH <cto@xzh.ai>
2026-10-07 20:22:18 +08:00

77 lines
2.9 KiB
Python

"""Error normalization for LlamaIndex-backed RAG retrieval."""
from __future__ import annotations
from typing import Any, Dict
def _invalid_index_reason(message: str) -> str:
"""Extract the persisted-store failure reason recorded by the loader."""
_, _, detail = message.partition("Details: ")
if not detail:
return ""
return detail.split(". ", 1)[0].rstrip(".")
def search_error_result(query: str, exc: Exception) -> Dict[str, Any]:
"""Convert retrieval failures into actionable tool output."""
message = str(exc)
lower = message.lower()
null_vector_similarity_error = (
"unsupported operand type(s) for *" in lower and "nonetype" in lower and "float" in lower
)
shape_vector_error = "inhomogeneous shape" in lower or (
"shapes" in lower and "not aligned" in lower
)
invalid_persisted_index = "rag index contains invalid embedding vectors" in lower
if invalid_persisted_index or null_vector_similarity_error or shape_vector_error:
# Classify the persisted-index diagnosis before the provider-response
# branch so its "Details:" tail about stored vectors is never treated
# as a live provider failure (issue #440).
reason = _invalid_index_reason(message)
reason_clause = f" ({reason})" if reason else ""
return {
"query": query,
"answer": (
"RAG search failed because this knowledge base index contains "
f"invalid embedding vectors{reason_clause}. Re-index the "
"knowledge base with the current embedding provider/model "
"before querying it again: open the knowledge base page and "
"run its 'Re-index' action (Index versions → Re-index). "
"Re-uploading documents reuses the damaged index and cannot "
"repair it."
),
"content": "",
"provider": "llamaindex",
"error": message,
"error_type": "invalid_embedding_index",
"log_message": "RAG index contains invalid embedding vectors; re-index required.",
"needs_reindex": True,
}
if "embedding provider returned invalid" in lower:
return {
"query": query,
"answer": (
"RAG search failed because the embedding provider returned an "
f"invalid query vector: {message}"
),
"content": "",
"provider": "llamaindex",
"error": message,
"error_type": "invalid_embedding_provider_response",
"log_message": (
"Embedding provider returned an invalid query vector; check "
"the embedding provider/model configuration."
),
}
return {
"query": query,
"answer": f"Search failed: {message}",
"content": "",
"provider": "llamaindex",
"error": message,
}