Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions datamind/capabilities/ingest/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions datamind/tests/test_ingest_revisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down