mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 23:39:37 +08:00
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>
This commit is contained in:
@@ -5,11 +5,51 @@ 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,
|
||||
@@ -27,29 +67,6 @@ def search_error_result(query: str, exc: Exception) -> Dict[str, Any]:
|
||||
),
|
||||
}
|
||||
|
||||
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 null_vector_similarity_error or shape_vector_error or invalid_persisted_index:
|
||||
return {
|
||||
"query": query,
|
||||
"answer": (
|
||||
"RAG search failed because this knowledge base index contains "
|
||||
"invalid embedding vectors. Re-index the knowledge base with "
|
||||
"the current embedding provider/model before querying it again."
|
||||
),
|
||||
"content": "",
|
||||
"provider": "llamaindex",
|
||||
"error": message,
|
||||
"error_type": "invalid_embedding_index",
|
||||
"log_message": "RAG index contains invalid embedding vectors; re-index required.",
|
||||
"needs_reindex": True,
|
||||
}
|
||||
|
||||
return {
|
||||
"query": query,
|
||||
"answer": f"Search failed: {message}",
|
||||
|
||||
@@ -111,16 +111,30 @@ def insert_documents(existing_storage: Path, storage_dir: Path, documents: list[
|
||||
return count
|
||||
|
||||
|
||||
def _persisted_vector_fault(exc: ValueError) -> str:
|
||||
"""Return the concrete fault from a shared-validator error message."""
|
||||
_, _, tail = str(exc).partition("): ")
|
||||
fault = tail.split(". ", 1)[0].rstrip(".")
|
||||
return fault or "unusable embedding data"
|
||||
|
||||
|
||||
def _validate_embedding_dict(embedding_dict: Any, *, label: str) -> None:
|
||||
if not isinstance(embedding_dict, dict) or not embedding_dict:
|
||||
return
|
||||
|
||||
validate_embedding_batch(
|
||||
list(embedding_dict.values()),
|
||||
expected_count=len(embedding_dict),
|
||||
binding="llamaindex",
|
||||
model=f"persisted-index:{label}",
|
||||
)
|
||||
try:
|
||||
validate_embedding_batch(
|
||||
list(embedding_dict.values()),
|
||||
expected_count=len(embedding_dict),
|
||||
binding="llamaindex",
|
||||
model=f"persisted-index:{label}",
|
||||
)
|
||||
except ValueError as exc:
|
||||
# The shared validator blames the embedding provider; persisted
|
||||
# vectors are local data, so restate the fault neutrally.
|
||||
raise ValueError(
|
||||
f"stored vector in {label} failed validation: {_persisted_vector_fault(exc)}"
|
||||
) from exc
|
||||
|
||||
|
||||
def _iter_index_embedding_dicts(index: Any):
|
||||
@@ -189,9 +203,11 @@ def _validate_persisted_embeddings(index: Any, storage_dir: Path | None = None)
|
||||
_validate_embedding_dict(embedding_dict, label=label)
|
||||
except ValueError as exc:
|
||||
raise ValueError(
|
||||
"RAG index contains invalid embedding vectors. Re-index the "
|
||||
"knowledge base with the current embedding provider/model before "
|
||||
f"querying it again. Details: {exc}"
|
||||
"RAG index contains invalid embedding vectors. Rebuild it with "
|
||||
"the knowledge base's 'Re-index' action (Index versions → "
|
||||
"Re-index) using the current embedding provider/model; "
|
||||
"re-uploading documents reuses the damaged store and cannot "
|
||||
f"repair it. Details: {exc}"
|
||||
) from exc
|
||||
|
||||
|
||||
|
||||
@@ -249,3 +249,95 @@ async def test_rag_service_hides_low_level_invalid_index_error_in_raw_logs(
|
||||
for event_type, message, _ in events
|
||||
if event_type == "status"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_answer_names_reason_and_reindex_action(
|
||||
tmp_path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
from deeptutor.services.rag.pipelines.llamaindex import storage as storage_module
|
||||
from deeptutor.services.rag.pipelines.llamaindex.pipeline import LlamaIndexPipeline
|
||||
|
||||
storage_dir = tmp_path / "kb" / "version-1"
|
||||
storage_dir.mkdir(parents=True)
|
||||
(storage_dir / "docstore.json").write_text("{}", encoding="utf-8")
|
||||
(storage_dir / "default__vector_store.json").write_text(
|
||||
json.dumps({"embedding_dict": {"bad-node": [0.1, None, 0.3]}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
# The in-memory index looks healthy; the invalid vectors live in the
|
||||
# persisted vector-store file (the upgrade scenario from issue #440).
|
||||
fake_index = SimpleNamespace(
|
||||
vector_store=SimpleNamespace(data=SimpleNamespace(embedding_dict={}))
|
||||
)
|
||||
monkeypatch.setattr(storage_module.vector_store, "load_index", lambda _dir: fake_index)
|
||||
monkeypatch.setattr(LlamaIndexPipeline, "_configure_settings", lambda self: None)
|
||||
|
||||
pipeline = LlamaIndexPipeline(kb_base_dir=str(tmp_path), signature_provider=lambda: None)
|
||||
|
||||
result = await pipeline.search("what is this?", "kb")
|
||||
|
||||
assert result["error_type"] == "invalid_embedding_index"
|
||||
assert result["needs_reindex"] is True
|
||||
assert "invalid embedding vectors" in result["answer"]
|
||||
assert "dimension 1 is null" in result["answer"]
|
||||
assert "default__vector_store.json" in result["answer"]
|
||||
assert "stored vector in default__vector_store.json" in result["answer"]
|
||||
assert "embedding provider returned invalid" not in result["answer"].lower()
|
||||
assert "Re-index" in result["answer"]
|
||||
assert "Index versions" in result["answer"]
|
||||
assert "re-uploading" in result["answer"].lower()
|
||||
|
||||
|
||||
def test_invalid_index_error_names_reindex_action_and_reupload_caveat(tmp_path) -> None:
|
||||
from deeptutor.services.rag.pipelines.llamaindex import storage as storage_module
|
||||
|
||||
storage_dir = tmp_path / "kb" / "version-1"
|
||||
storage_dir.mkdir(parents=True)
|
||||
(storage_dir / "default__vector_store.json").write_text(
|
||||
json.dumps({"embedding_dict": {"bad-node": [0.1, None, 0.3]}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
storage_module.validate_storage_embeddings(storage_dir)
|
||||
|
||||
message = str(excinfo.value)
|
||||
assert "RAG index contains invalid embedding vectors" in message
|
||||
assert "dimension 1 is null" in message
|
||||
assert "stored vector in default__vector_store.json" in message
|
||||
assert "embedding provider returned invalid" not in message.lower()
|
||||
assert "Re-index" in message
|
||||
assert "re-uploading" in message.lower()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_answer_keeps_reindex_steps_without_low_level_details(
|
||||
tmp_path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
from deeptutor.services.rag.pipelines.llamaindex import storage as storage_module
|
||||
from deeptutor.services.rag.pipelines.llamaindex.pipeline import LlamaIndexPipeline
|
||||
|
||||
storage_dir = tmp_path / "kb" / "version-1"
|
||||
storage_dir.mkdir(parents=True)
|
||||
(storage_dir / "docstore.json").write_text("{}", encoding="utf-8")
|
||||
|
||||
monkeypatch.setattr(LlamaIndexPipeline, "_configure_settings", lambda self: None)
|
||||
monkeypatch.setattr(
|
||||
storage_module,
|
||||
"retrieve_nodes",
|
||||
lambda storage_dir, query, top_k=5: (_ for _ in ()).throw(
|
||||
TypeError("unsupported operand type(s) for *: 'NoneType' and 'float'")
|
||||
),
|
||||
)
|
||||
|
||||
pipeline = LlamaIndexPipeline(kb_base_dir=str(tmp_path), signature_provider=lambda: None)
|
||||
|
||||
result = await pipeline.search("what is this?", "kb")
|
||||
|
||||
assert result["error_type"] == "invalid_embedding_index"
|
||||
assert "Re-index" in result["answer"]
|
||||
assert "Index versions" in result["answer"]
|
||||
assert "unsupported operand" not in result["answer"]
|
||||
assert "Details" not in result["answer"]
|
||||
|
||||
Reference in New Issue
Block a user