diff --git a/deeptutor/services/rag/pipelines/lightrag/pipeline.py b/deeptutor/services/rag/pipelines/lightrag/pipeline.py index 087c70480..679d51d34 100644 --- a/deeptutor/services/rag/pipelines/lightrag/pipeline.py +++ b/deeptutor/services/rag/pipelines/lightrag/pipeline.py @@ -335,8 +335,12 @@ class LightRagPipeline: return await run_in_worker_loop(job) def _remove_zero_accepted_candidate(self, root_dir: Path) -> None: - if root_dir.is_dir() and not storage.has_any_doc_status(root_dir): - shutil.rmtree(root_dir) + if not root_dir.is_dir() or storage.has_any_doc_status(root_dir): + return + for ingress_dir in (ingress.pending_root(root_dir), ingress.bundles_root(root_dir)): + if ingress_dir.is_dir() and any(path.is_file() for path in ingress_dir.rglob("*")): + return + shutil.rmtree(root_dir) async def initialize(self, kb_name: str, file_paths: List[str], **kwargs) -> bool: self._ensure_available() diff --git a/tests/services/rag/test_lightrag_pipeline.py b/tests/services/rag/test_lightrag_pipeline.py index 5e96acca2..f07828141 100644 --- a/tests/services/rag/test_lightrag_pipeline.py +++ b/tests/services/rag/test_lightrag_pipeline.py @@ -723,6 +723,67 @@ def test_enqueue_status_read_failure_retains_all_ingress(tmp_path: Path, monkeyp assert (bundles_root(working) / "doc.md.bundle").is_dir() +@pytest.mark.parametrize("operation", ["initialize", "add_documents"]) +def test_public_initial_ingestion_retains_uncertain_ingress( + tmp_path: Path, monkeypatch, operation: str +) -> None: + source = tmp_path / "source" / "doc.md" + source.parent.mkdir() + source.write_text("body", encoding="utf-8") + staged_working: Path | None = None + + class DocStatus: + async def get_docs_by_ids(self, _doc_ids, *, strict): + assert strict is True + raise RuntimeError("status unavailable") + + class Rag: + doc_status = DocStatus() + + pipeline = LightRagPipeline(kb_base_dir=str(tmp_path / "knowledge-bases")) + + def stage(working, paths): + nonlocal staged_working + staged_working = working + return [ + freeze_document( + working, + Path(paths[0]), + ParsedDocument(markdown="body", engine="text_only"), + ) + ], {} + + async def enqueue(_rag, _staged): + raise ValueError("original enqueue failure") + + async def no_op(*_args, **_kwargs): + return None + + monkeypatch.setattr(pipeline, "_ensure_available", lambda: None) + monkeypatch.setattr(pipeline, "_stage_documents", stage) + monkeypatch.setattr(engine, "build_rag", lambda *_args, **_kwargs: Rag()) + monkeypatch.setattr(engine, "initialize", no_op) + monkeypatch.setattr(engine, "enqueue", enqueue) + monkeypatch.setattr(engine, "finalize", no_op) + + with pytest.raises(ValueError, match="original enqueue failure"): + asyncio.run(getattr(pipeline, operation)("kb", [str(source)])) + + assert staged_working is not None + assert (pending_root(staged_working) / "doc.md").is_file() + assert (bundles_root(staged_working) / "doc.md.bundle").is_dir() + + +def test_zero_accepted_candidate_cleanup_removes_empty_version(tmp_path: Path) -> None: + version = tmp_path / "version-1" + pending_root(version).mkdir(parents=True) + bundles_root(version).mkdir(parents=True) + + LightRagPipeline(kb_base_dir=str(tmp_path))._remove_zero_accepted_candidate(version) + + assert not version.exists() + + def test_append_rejects_corrupt_or_unpublished_existing_version( tmp_path: Path, monkeypatch ) -> None: