Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(knowledge): keep same-named documents instead of dropping one

Bingxi Zhao (Frank)2739a28871a2
diff --git a/deeptutor/knowledge/add_documents.py b/deeptutor/knowledge/add_documents.py
index 097e91b68..7ea68cbba 100644
--- a/deeptutor/knowledge/add_documents.py
+++ b/deeptutor/knowledge/add_documents.py
@@ -8,6 +8,7 @@ import asyncio
from dataclasses import dataclass
from datetime import datetime
import hashlib
+import itertools
import json
import logging
from pathlib import Path
@@ -213,6 +214,24 @@ class DocumentAdder:
return {}
return {}
+ @staticmethod
+ def _non_colliding(dest_path: Path) -> Path:
+ """A free sibling name for a *different* document that wants a taken one.
+
+ Two files can legitimately share a name and hold different content — a
+ docs tree with a README.md per folder is the ordinary case, and batched
+ syncs of sibling folders stage them against different roots, so they
+ land on the same name even with structure preserved. Dropping the later
+ one loses a document the user asked to index, silently.
+ """
+ if not dest_path.exists():
+ return dest_path
+ for index in itertools.count(2):
+ candidate = dest_path.with_name(f"{dest_path.stem} ({index}){dest_path.suffix}")
+ if not candidate.exists():
+ return candidate
+ raise AssertionError("unreachable") # pragma: no cover
+
def add_documents(
self,
source_files: List[str],
@@ -265,13 +284,13 @@ class DocumentAdder:
logger.info(f"Recovering staged file: {source_path.name}")
files_to_process.append(dest_path)
continue
- if not allow_duplicates:
- logger.info(f"Skipped (filename collision): {source_path.name}")
- continue
+ # Same name, different document: keep both rather than
+ # silently dropping this one.
+ dest_path = self._non_colliding(dest_path)
dest_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source_path, dest_path)
- logger.info(f"Staged to raw: {source_path.name}")
+ logger.info(f"Staged to raw: {dest_path.relative_to(self.raw_dir).as_posix()}")
files_to_process.append(dest_path)
return files_to_process
diff --git a/tests/knowledge/test_add_documents_linked_folder.py b/tests/knowledge/test_add_documents_linked_folder.py
index 8eccde881..6a77454b7 100644
--- a/tests/knowledge/test_add_documents_linked_folder.py
+++ b/tests/knowledge/test_add_documents_linked_folder.py
@@ -104,3 +104,52 @@ def test_add_documents_source_outside_source_root_falls_back_to_basename(
staged = adder.add_documents([str(doc)], source_root=str(linked_folder))
assert staged == [kb_dir / "raw" / "note.md"]
+
+
+def test_batched_syncs_of_sibling_folders_keep_both_same_named_files(
+ tmp_path: Path,
+) -> None:
+ """A folder too large to sync at once is added one subfolder at a time, so
+ each batch roots at its own folder and both files want ``raw/README.md``.
+ Preserving structure does not help here — the second one used to be dropped
+ as a "filename collision", losing a document with no error (#866)."""
+ kb_dir = tmp_path / "kb"
+ _ready_llamaindex_kb(kb_dir)
+
+ cli = tmp_path / "docs" / "cli"
+ events = tmp_path / "docs" / "events"
+ cli.mkdir(parents=True)
+ events.mkdir(parents=True)
+ (cli / "README.md").write_text("cli docs", encoding="utf-8")
+ (events / "README.md").write_text("event docs", encoding="utf-8")
+
+ adder = DocumentAdder(kb_name="kb", base_dir=str(tmp_path))
+ adder.add_documents([str(cli / "README.md")], source_root=str(cli))
+ staged = adder.add_documents([str(events / "README.md")], source_root=str(events))
+
+ raw = kb_dir / "raw"
+ assert staged == [raw / "README (2).md"]
+ assert {path.name for path in raw.iterdir()} == {"README.md", "README (2).md"}
+ assert {path.read_text(encoding="utf-8") for path in raw.iterdir()} == {
+ "cli docs",
+ "event docs",
+ }
+
+
+def test_restaging_identical_bytes_reuses_the_staged_copy(tmp_path: Path) -> None:
+ """Keeping colliding documents must not turn a repeated sync into "(2)"
+ churn: identical content still recovers the file already staged."""
+ kb_dir = tmp_path / "kb"
+ _ready_llamaindex_kb(kb_dir)
+
+ linked_folder = tmp_path / "linked"
+ linked_folder.mkdir()
+ doc = linked_folder / "note.md"
+ doc.write_text("same", encoding="utf-8")
+
+ adder = DocumentAdder(kb_name="kb", base_dir=str(tmp_path))
+ first = adder.add_documents([str(doc)], source_root=str(linked_folder))
+ second = adder.add_documents([str(doc)], source_root=str(linked_folder))
+
+ assert first == second == [kb_dir / "raw" / "note.md"]
+ assert [path.name for path in (kb_dir / "raw").iterdir()] == ["note.md"]