mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 05:29:27 +08:00
Bind mastery drafts before their opening turn and preserve stored session modes. Track chat duration from submission through terminal completion. Run async reading extensions on their event loop and allow retries after cancellable timeouts. Allow first uploads into empty LlamaIndex knowledge bases, preserve Telegram thread routing, parse embedding Retry-After dates, and enforce robots rules and host pacing during documentation crawls, including redirects. Fixes #1461 Fixes #1458 Fixes #1451 Fixes #1448 Fixes #1392 Fixes #1429 Refs #1435, #1412 Reuses contributions from PRs #1462, #1452 and #1403. Co-authored-by: sjagadee <srini.jagadeesh@gmail.com> Co-authored-by: AoHanBei <dongaohan21@gmail.com> Co-authored-by: Gao-Chenkai <DevChenkai@outlook.com>
225 lines
7.2 KiB
Python
225 lines
7.2 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from deeptutor.knowledge.add_documents import (
|
|
DocumentAdder,
|
|
RawDocumentRemoval,
|
|
remove_raw_document,
|
|
)
|
|
|
|
|
|
def _write_provider_version(kb_dir: Path, provider: str) -> None:
|
|
version_dir = kb_dir / "version-1"
|
|
version_dir.mkdir(parents=True)
|
|
if provider == "pageindex":
|
|
(version_dir / "pageindex_docs.json").write_text(
|
|
json.dumps({"provider": "pageindex", "docs": {"doc.pdf": {"doc_id": "doc-1"}}}),
|
|
encoding="utf-8",
|
|
)
|
|
elif provider == "graphrag":
|
|
output_dir = version_dir / "output"
|
|
output_dir.mkdir()
|
|
(output_dir / "entities.parquet").write_bytes(b"placeholder")
|
|
elif provider == "lightrag":
|
|
(version_dir / "kv_store_doc_status.json").write_text(
|
|
json.dumps({"doc": {"status": "processed", "chunks_list": ["chunk"]}}),
|
|
encoding="utf-8",
|
|
)
|
|
else:
|
|
(version_dir / "docstore.json").write_text("{}", encoding="utf-8")
|
|
(version_dir / "index_store.json").write_text("{}", encoding="utf-8")
|
|
meta = {"provider": provider, "signature": provider, "version": "version-1"}
|
|
if provider == "lightrag":
|
|
meta.update(
|
|
{
|
|
"lightrag_adapter_schema": 2,
|
|
"parser_bridge_schema": 1,
|
|
"state": "published",
|
|
}
|
|
)
|
|
(version_dir / "meta.json").write_text(
|
|
json.dumps(meta),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
|
|
def test_document_adder_reads_provider_from_kb_config_when_metadata_missing(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
kb_dir = tmp_path / "page-kb"
|
|
(kb_dir / "raw").mkdir(parents=True)
|
|
_write_provider_version(kb_dir, "pageindex")
|
|
(tmp_path / "kb_config.json").write_text(
|
|
json.dumps(
|
|
{"knowledge_bases": {"page-kb": {"path": "page-kb", "rag_provider": "pageindex"}}}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
adder = DocumentAdder(kb_name="page-kb", base_dir=str(tmp_path))
|
|
|
|
assert adder.rag_provider == "pageindex"
|
|
|
|
|
|
def test_document_adder_preserves_explicit_bound_provider(tmp_path: Path) -> None:
|
|
kb_dir = tmp_path / "graph-kb"
|
|
(kb_dir / "raw").mkdir(parents=True)
|
|
_write_provider_version(kb_dir, "graphrag")
|
|
|
|
adder = DocumentAdder(
|
|
kb_name="graph-kb",
|
|
base_dir=str(tmp_path),
|
|
rag_provider="graphrag",
|
|
)
|
|
|
|
assert adder.rag_provider == "graphrag"
|
|
|
|
|
|
@pytest.mark.parametrize("provider", ["llamaindex", "lightrag"])
|
|
def test_document_adder_allows_empty_kb_to_bootstrap(tmp_path: Path, provider: str) -> None:
|
|
(tmp_path / "empty-kb").mkdir()
|
|
|
|
adder = DocumentAdder(
|
|
kb_name="empty-kb",
|
|
base_dir=str(tmp_path),
|
|
rag_provider=provider,
|
|
)
|
|
|
|
assert adder.rag_provider == provider
|
|
assert adder.raw_dir.is_dir()
|
|
|
|
|
|
def test_process_new_documents_returns_failures_without_marking_processed(
|
|
monkeypatch, tmp_path: Path
|
|
) -> None:
|
|
kb_dir = tmp_path / "kb"
|
|
raw_dir = kb_dir / "raw"
|
|
raw_dir.mkdir(parents=True)
|
|
_write_provider_version(kb_dir, "llamaindex")
|
|
doc = raw_dir / "bad.txt"
|
|
doc.write_text("hello", encoding="utf-8")
|
|
|
|
class _FailingRagService:
|
|
def __init__(self, *_args, **_kwargs) -> None:
|
|
pass
|
|
|
|
async def add_documents(self, *_args, **_kwargs) -> bool:
|
|
raise RuntimeError("provider exploded")
|
|
|
|
monkeypatch.setattr(
|
|
"deeptutor.knowledge.add_documents.RAGService",
|
|
_FailingRagService,
|
|
)
|
|
|
|
adder = DocumentAdder(kb_name="kb", base_dir=str(tmp_path))
|
|
result = asyncio.run(adder.process_new_documents([doc]))
|
|
|
|
assert result.processed_files == []
|
|
assert result.failed_count == 1
|
|
assert "provider exploded" in result.failure_summary()
|
|
assert adder.get_ingested_hashes() == {}
|
|
|
|
|
|
def test_lightrag_hash_bookkeeping_failure_does_not_deny_published_success(
|
|
monkeypatch, tmp_path: Path
|
|
) -> None:
|
|
kb_dir = tmp_path / "kb"
|
|
raw_dir = kb_dir / "raw"
|
|
raw_dir.mkdir(parents=True)
|
|
_write_provider_version(kb_dir, "lightrag")
|
|
doc = raw_dir / "published.txt"
|
|
doc.write_text("hello", encoding="utf-8")
|
|
|
|
class _SuccessfulRagService:
|
|
def __init__(self, *_args, **_kwargs) -> None:
|
|
pass
|
|
|
|
async def add_documents(self, *_args, **_kwargs) -> bool:
|
|
return True
|
|
|
|
monkeypatch.setattr("deeptutor.knowledge.add_documents.RAGService", _SuccessfulRagService)
|
|
adder = DocumentAdder(kb_name="kb", base_dir=str(tmp_path), rag_provider="lightrag")
|
|
monkeypatch.setattr(
|
|
adder,
|
|
"_record_successful_hash",
|
|
lambda _path: (_ for _ in ()).throw(OSError("metadata unavailable")),
|
|
)
|
|
|
|
result = asyncio.run(adder.process_new_documents([doc]))
|
|
|
|
assert result.processed_files == [doc]
|
|
assert result.failures == []
|
|
|
|
|
|
def test_remove_raw_document_deletes_file_and_hash_without_index(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
# Deliberately NO provider index: an error-state KB may have none, yet its
|
|
# raw files must stay removable (DocumentAdder would refuse to construct).
|
|
kb_dir = tmp_path / "kb"
|
|
raw_dir = kb_dir / "raw"
|
|
raw_dir.mkdir(parents=True)
|
|
doc = raw_dir / "big.pdf"
|
|
doc.write_text("x", encoding="utf-8")
|
|
(kb_dir / "metadata.json").write_text(
|
|
json.dumps({"file_hashes": {"big.pdf": "deadbeef"}, "keep": True}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
removal = remove_raw_document(kb_dir, doc)
|
|
|
|
assert removal == RawDocumentRemoval(rel_path="big.pdf", was_indexed=True)
|
|
assert not doc.exists()
|
|
metadata = json.loads((kb_dir / "metadata.json").read_text(encoding="utf-8"))
|
|
assert metadata["file_hashes"] == {}
|
|
assert metadata["keep"] is True # unrelated metadata is preserved
|
|
|
|
|
|
def test_remove_raw_document_reports_unindexed_file(tmp_path: Path) -> None:
|
|
kb_dir = tmp_path / "kb"
|
|
raw_dir = kb_dir / "raw"
|
|
raw_dir.mkdir(parents=True)
|
|
doc = raw_dir / "never_indexed.pdf"
|
|
doc.write_text("x", encoding="utf-8")
|
|
# No metadata.json at all — the file failed before any hash was recorded.
|
|
|
|
removal = remove_raw_document(kb_dir, doc)
|
|
|
|
assert removal.rel_path == "never_indexed.pdf"
|
|
assert removal.was_indexed is False
|
|
assert not doc.exists()
|
|
|
|
|
|
def test_remove_raw_document_uses_relative_key_for_nested_file(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
kb_dir = tmp_path / "kb"
|
|
nested = kb_dir / "raw" / "papers" / "2024"
|
|
nested.mkdir(parents=True)
|
|
doc = nested / "a.pdf"
|
|
doc.write_text("x", encoding="utf-8")
|
|
(kb_dir / "metadata.json").write_text(
|
|
json.dumps({"file_hashes": {"papers/2024/a.pdf": "hash", "other.pdf": "keep"}}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
removal = remove_raw_document(kb_dir, doc)
|
|
|
|
assert removal.was_indexed is True
|
|
assert removal.rel_path == "papers/2024/a.pdf"
|
|
remaining = json.loads((kb_dir / "metadata.json").read_text(encoding="utf-8"))
|
|
assert remaining["file_hashes"] == {"other.pdf": "keep"}
|
|
|
|
|
|
@pytest.mark.parametrize("storage", ["version-1", "rag_storage"])
|
|
def test_empty_bootstrap_does_not_replace_broken_existing_index(tmp_path, storage):
|
|
kb_dir = tmp_path / "kb"
|
|
(kb_dir / storage).mkdir(parents=True)
|
|
with pytest.raises(ValueError, match="reindex|not initialized"):
|
|
DocumentAdder(kb_name="kb", base_dir=str(tmp_path), rag_provider="llamaindex")
|