diff --git a/deeptutor/services/rag/index_probe.py b/deeptutor/services/rag/index_probe.py index a2a5704ce..de4ef6ba7 100644 --- a/deeptutor/services/rag/index_probe.py +++ b/deeptutor/services/rag/index_probe.py @@ -256,7 +256,7 @@ def _inspect_lightrag(storage_dir: Path) -> ProviderIndexProbe: str(storage_dir), ready, failure_summary="" if ready else failure, - doc_count=_lightrag_doc_count(storage_dir), + doc_count=storage.doc_count(storage_dir), ) @@ -284,27 +284,6 @@ def _llamaindex_doc_count(docstore_path: Path) -> int | None: return _llamaindex_doc_count_cached(str(docstore_path), stat.st_size, stat.st_mtime_ns) -def _lightrag_doc_count(storage_dir: Path) -> int | None: - payload = _read_json(storage_dir / "kv_store_doc_status.json") - if not isinstance(payload, dict): - return None - count = 0 - for item in payload.values(): - if not isinstance(item, dict): - continue - chunks = item.get("chunks_list") - status = str(item.get("status") or "").lower() - if (isinstance(chunks, list) and chunks) or status in { - "processed", - "completed", - "done", - "success", - "indexed", - }: - count += 1 - return count - - def _read_json(path: Path) -> dict[str, Any] | None: if not path.exists(): return None diff --git a/deeptutor/services/rag/pipelines/lightrag/storage.py b/deeptutor/services/rag/pipelines/lightrag/storage.py index ac61a77ba..b52b09e24 100644 --- a/deeptutor/services/rag/pipelines/lightrag/storage.py +++ b/deeptutor/services/rag/pipelines/lightrag/storage.py @@ -110,6 +110,30 @@ def _doc_status_has_success(root_dir: Path) -> bool | None: return False if saw_failure else None +def doc_count(root_dir: Path | None) -> int | None: + """Return the count of successfully indexed LightRAG documents, or ``None``. + + Resolves the native workspace store first (mirroring :func:`has_output` and + :func:`failure_summary`), so a native-layout index whose + ``kv_store_doc_status.json`` lives under the ``deeptutor_`` workspace + subdir is counted instead of being reported as ``None``. + """ + if root_dir is None: + return None + payload = _read_doc_status(_store_root(Path(root_dir))) + if not payload: + return None + count = 0 + for item in payload.values(): + if not isinstance(item, dict): + continue + chunks = item.get("chunks_list") + status = str(item.get("status") or "").lower() + if (isinstance(chunks, list) and chunks) or status in _SUCCESS_STATUSES: + count += 1 + return count + + def failure_summary(root_dir: Path | None, *, limit: int = 3) -> str: """Return a short human-readable summary of failed LightRAG documents.""" if root_dir is None: @@ -326,6 +350,7 @@ def write_meta( __all__ = [ "META_FILENAME", "PROVIDER", + "doc_count", "document_error", "failure_summary", "has_any_doc_status", diff --git a/tests/services/rag/test_index_probe.py b/tests/services/rag/test_index_probe.py index ce711c7e1..467d63209 100644 --- a/tests/services/rag/test_index_probe.py +++ b/tests/services/rag/test_index_probe.py @@ -170,6 +170,42 @@ def test_lightrag_uses_doc_status_as_truth(tmp_path: Path) -> None: assert probe.doc_count == 1 +def test_lightrag_native_layout_counts_docs_in_workspace_subdir(tmp_path: Path) -> None: + # Native LightRAG keeps its KV stores under a ``deeptutor_`` workspace + # subdir, not at the version root. The probe must resolve that store before + # counting, otherwise a ready index reports ``doc_count=None``. + version_dir = tmp_path / "version-1" + version_dir.mkdir() + workspace = "deeptutor_" + "ab" * 8 # matches deeptutor_[0-9a-f]{16} + (version_dir / "meta.json").write_text( + json.dumps( + { + "version": "version-1", + "provider": "lightrag", + "signature": "lightrag", + "state": "published", + "workspace": workspace, + } + ), + encoding="utf-8", + ) + store_root = version_dir / workspace + store_root.mkdir() + (store_root / "kv_store_doc_status.json").write_text( + json.dumps( + { + "doc-1": {"status": "processed", "chunks_list": ["chunk-1"]}, + "doc-2": {"status": "processed", "chunks_list": ["chunk-2", "chunk-3"]}, + } + ), + encoding="utf-8", + ) + + probe = inspect_provider_index("lightrag", version_dir) + assert probe.ready is True + assert probe.doc_count == 2 + + def test_provider_mismatch_is_not_ready(tmp_path: Path) -> None: version_dir = tmp_path / "version-1" version_dir.mkdir()