DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(knowledge): promote stale processing status when ready index exists (#418)
voidborne-dd365926ca865
diff --git a/deeptutor/knowledge/manager.py b/deeptutor/knowledge/manager.pyindex 6bf9a6399..7b06a5919 100644--- a/deeptutor/knowledge/manager.py+++ b/deeptutor/knowledge/manager.py@@ -646,6 +646,21 @@ class KnowledgeBaseManager:# For old KBs without status field, determine status from rag_storageif effective_needs_reindex:status = "needs_reindex"+ elif (+ status in {"processing", "initializing"}+ and has_ready_llamaindex+ and not (isinstance(progress, dict) and progress.get("stage") == "error")+ ):+ # A ready index version exists on disk but the persisted status is+ # still a "live" sentinel — typically because the progress writer+ # crashed (or the process was killed) after the index was finalised+ # but before status was promoted to "ready". Recover the actual+ # state on read so the UI does not show a perpetual processing+ # banner. The persistent kb_config.json is left untouched; the+ # next legitimate update_kb_status() call will clean it up.+ # See issue #418.+ status = "ready"+ progress = Noneelif not status and dir_exists:rag_storage_dir = kb_dir / "rag_storage"if has_ready_llamaindex:diff --git a/tests/knowledge/test_manager_get_info_status.py b/tests/knowledge/test_manager_get_info_status.pynew file mode 100644index 000000000..2177d5f38--- /dev/null+++ b/tests/knowledge/test_manager_get_info_status.py@@ -0,0 +1,203 @@+"""Tests for KnowledgeBaseManager.get_info() status promotion (issue #418).++When the persisted ``status`` in ``kb_config.json`` is a "live" sentinel+(``processing`` / ``initializing``) but a ready index version already exists on+disk, ``get_info`` must promote the reported status to ``ready`` so the UI does+not show a perpetual processing banner.++This typically happens when the progress writer or worker process crashes+after the LlamaIndex version is finalised (``ready: true``) but before+``update_kb_status(name, "ready")`` runs and rewrites kb_config.json.+"""++from __future__ import annotations++import json+from pathlib import Path++import pytest++from deeptutor.knowledge.manager import KnowledgeBaseManager++ACTIVE_SIGNATURE = "active-signature"+++class _Signature:+ def __init__(self, sig_hash: str = ACTIVE_SIGNATURE) -> None:+ self._hash = sig_hash++ def hash(self) -> str:+ return self._hash+++def _create_ready_version(+ kb_dir: Path, version: int = 1, signature: str = ACTIVE_SIGNATURE+) -> None:+ """Create a flat version-N directory recognised as ready by index_versioning.++ ``_is_storage_ready`` only requires at least one non-meta file inside the+ version dir, so a single ``docstore.json`` is sufficient. ``meta.json`` is+ written with the active signature so the embedding reconcile logic does+ not flag the KB as needs_reindex.+ """+ version_dir = kb_dir / f"version-{version}"+ version_dir.mkdir(parents=True, exist_ok=True)+ (version_dir / "docstore.json").write_text("{}", encoding="utf-8")+ (version_dir / "meta.json").write_text(+ json.dumps({"signature": signature, "version": f"version-{version}"}),+ encoding="utf-8",+ )+++def _patch_active_embedding(+ monkeypatch: pytest.MonkeyPatch, sig_hash: str = ACTIVE_SIGNATURE+) -> None:+ from deeptutor.knowledge import manager as manager_module+ from deeptutor.services.rag import embedding_signature++ monkeypatch.setattr(+ manager_module, "_get_embedding_fingerprint", lambda: ("embed-active", 4096)+ )+ monkeypatch.setattr(+ embedding_signature,+ "signature_from_embedding_config",+ lambda: _Signature(sig_hash),+ )+++def test_processing_with_ready_index_promotes_to_ready(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Headline reproduction of issue #418.++ kb_config.json has ``status: "processing"`` and stale+ ``progress.stage: "processing_documents"``, but a ready version-1 exists.+ """+ _patch_active_embedding(monkeypatch)+ manager = KnowledgeBaseManager(base_dir=str(tmp_path))+ manager.update_kb_status(+ name="kb1",+ status="processing",+ progress={+ "stage": "processing_documents",+ "message": "Embedding chunks",+ "percent": 60,+ },+ )+ _create_ready_version(tmp_path / "kb1")++ info = manager.get_info("kb1")+ assert info["status"] == "ready"+ # When promoted to ready the progress banner is cleared so consumers+ # don't show "ready" + a stale processing bar at the same time.+ assert info["progress"] is None+ assert info["statistics"]["status"] == "ready"+ assert info["statistics"]["progress"] is None+ assert info["statistics"]["rag_initialized"] is True+++def test_processing_with_completed_progress_and_ready_index_promotes(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Variant: progress.stage == "completed" but status not yet flipped."""+ _patch_active_embedding(monkeypatch)+ manager = KnowledgeBaseManager(base_dir=str(tmp_path))+ manager.update_kb_status(+ name="kb2",+ status="processing",+ progress={"stage": "completed", "percent": 100},+ )+ _create_ready_version(tmp_path / "kb2")++ info = manager.get_info("kb2")+ assert info["status"] == "ready"+ assert info["progress"] is None+++def test_initializing_with_ready_index_promotes(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """``initializing`` is also a live sentinel and must be recoverable."""+ _patch_active_embedding(monkeypatch)+ manager = KnowledgeBaseManager(base_dir=str(tmp_path))+ manager.update_kb_status(name="kb3", status="initializing", progress=None)+ _create_ready_version(tmp_path / "kb3")++ info = manager.get_info("kb3")+ assert info["status"] == "ready"+++def test_processing_with_error_stage_is_not_promoted(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """An ``error`` stage must NOT be silently promoted, even if an older+ ready version still exists on disk — the user needs to see the failure.+ """+ _patch_active_embedding(monkeypatch)+ manager = KnowledgeBaseManager(base_dir=str(tmp_path))+ manager.update_kb_status(+ name="kb4",+ status="processing",+ progress={"stage": "error", "error": "embedding API down"},+ )+ _create_ready_version(tmp_path / "kb4")++ info = manager.get_info("kb4")+ assert info["status"] == "processing"+ assert info["progress"] is not None+ assert info["progress"].get("stage") == "error"+++def test_processing_without_ready_index_not_promoted(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Genuine in-flight indexing (no ready version yet) stays as ``processing``."""+ _patch_active_embedding(monkeypatch)+ manager = KnowledgeBaseManager(base_dir=str(tmp_path))+ manager.update_kb_status(+ name="kb5",+ status="processing",+ progress={"stage": "processing_documents", "percent": 25},+ )+ # Allocate the version dir but leave it empty (writer reserved it but+ # has not persisted any storage files yet — it's NOT ready).+ (tmp_path / "kb5" / "version-1").mkdir(parents=True)++ info = manager.get_info("kb5")+ assert info["status"] == "processing"+ assert info["progress"] is not None+ assert info["progress"].get("stage") == "processing_documents"+++def test_ready_status_unaffected(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ """``status="ready"`` is a no-op for the new branch — verify the existing+ happy path still reports ready.+ """+ _patch_active_embedding(monkeypatch)+ manager = KnowledgeBaseManager(base_dir=str(tmp_path))+ _create_ready_version(tmp_path / "kb6")+ manager.update_kb_status(name="kb6", status="ready", progress=None)++ info = manager.get_info("kb6")+ assert info["status"] == "ready"+++def test_needs_reindex_takes_precedence(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ """``effective_needs_reindex`` is checked first — promotion to ready+ must not run when the on-disk version was indexed under a different+ embedding signature than the currently active one.+ """+ _patch_active_embedding(monkeypatch)+ # Version was indexed under "old-signature"; active is "active-signature".+ # _reconcile_against_active_embedding will see the mismatch and flip+ # needs_reindex=True on load.+ _create_ready_version(tmp_path / "kb7", signature="old-signature")+ manager = KnowledgeBaseManager(base_dir=str(tmp_path))+ manager.update_kb_status(+ name="kb7",+ status="processing",+ progress={"stage": "completed", "percent": 100},+ )++ info = manager.get_info("kb7")+ assert info["status"] == "needs_reindex"