From 247df097c563f4f5a899b2cd88a108aa4da2a175 Mon Sep 17 00:00:00 2001 From: Dev Chiniwala Date: Thu, 17 Sep 2026 16:26:56 +0530 Subject: [PATCH] fix: remove stale chunks for empty reingest --- datamind/capabilities/ingest/service.py | 8 +++++++ datamind/tests/test_ingest_revisions.py | 28 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/datamind/capabilities/ingest/service.py b/datamind/capabilities/ingest/service.py index b04387e..9044ea7 100644 --- a/datamind/capabilities/ingest/service.py +++ b/datamind/capabilities/ingest/service.py @@ -725,6 +725,9 @@ async def kb_add_file(self, *, path: str, copy_to_profile: bool = True) -> dict[ )) if not chunks: + await self._upsert_chunks( + chunks, replace_sources={source, str(resolved)}, + ) return {"file": str(resolved), "chunks_added": 0, "note": "file was empty"} parsed_chunks = None @@ -830,6 +833,11 @@ async def _upsert_chunks( } if recorded_sources & replace_sources and chunk_id not in new_ids: stale_ids.append(chunk_id) + if not chunks: + if stale_ids: + await store.delete(stale_ids) + await self._kb.record_incremental_ingest() + return await store.add( ids=[c.id for c in chunks], texts=texts, diff --git a/datamind/tests/test_ingest_revisions.py b/datamind/tests/test_ingest_revisions.py index bcf4ed7..31adad5 100644 --- a/datamind/tests/test_ingest_revisions.py +++ b/datamind/tests/test_ingest_revisions.py @@ -43,6 +43,34 @@ async def generate_text(self, prompt, **kwargs): return "[]" +@pytest.mark.asyncio +async def test_reingesting_empty_path_removes_old_kb_chunks(tmp_path: Path): + profile = tmp_path / "profile" + uploads = profile / "uploads" + uploads.mkdir(parents=True) + source = uploads / "release_note.txt" + kb = _KB() + service = IngestService( + kb=kb, + db=None, + graph=None, + llm_client=_Model(), + llm_model="test", + profile_data_dir=profile, + chunk_size=512, + chunk_overlap=64, + ) + + source.write_text("old release note", encoding="utf-8") + await service.kb_add_file(path=str(source)) + source.write_text("\n\n", encoding="utf-8") + + result = await service.kb_add_file(path=str(source)) + + assert result["chunks_added"] == 0 + assert kb.vector_store.items == {} + + @pytest.mark.asyncio async def test_reingesting_same_path_replaces_old_kb_chunks(tmp_path: Path): profile = tmp_path / "profile"