DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix: keep recovered LightRAG index policy and provenance aligned
NoxsMedia77392102f254
diff --git a/deeptutor/knowledge/manager.py b/deeptutor/knowledge/manager.pyindex e57f0ed01..681e58fc5 100644--- a/deeptutor/knowledge/manager.py+++ b/deeptutor/knowledge/manager.py@@ -1449,11 +1449,18 @@ class KnowledgeBaseManager:if rag_provider == LIGHTRAG_PROVIDER:from deeptutor.services.rag.pipelines.lightrag.storage import (- latest_published_root,+ published_root_for_embedding,read_published_policy,)- published_root = latest_published_root(kb_dir) if dir_exists else None+ binding_signature = (+ kb_config.get("embedding_signature")+ if kb_config.get("embedding_selection")+ else None+ )+ published_root = (+ published_root_for_embedding(kb_dir, binding_signature) if dir_exists else None+ )indexing_policy = read_published_policy(published_root)if indexing_policy is None:pending = kb_config.get("pending_indexing_policy")@@ -1461,15 +1468,23 @@ class KnowledgeBaseManager:pending if isinstance(pending, dict) else {"policy": "legacy_unpinned"})metadata["indexing_policy"] = indexing_policy- if published_root is not None:+ if published_root is not None or kb_config.get("embedding_selection"):from deeptutor.services.rag.embedding_binding import entry_signaturepublished = next(- (v for v in index_versions if v.get("version") == published_root.name),+ (+ v+ for v in index_versions+ if published_root is not None and v.get("version") == published_root.name+ ),{},)- metadata["indexed_embedding_model"] = published.get("embedding_model")- metadata["indexed_embedding_dim"] = published.get("embedding_dim")+ metadata["indexed_embedding_model"] = published.get(+ "embedding_model"+ ) or kb_config.get("embedding_model")+ metadata["indexed_embedding_dim"] = published.get("embedding_dim") or kb_config.get(+ "embedding_dim"+ )current_embedding = entry_signature(kb_config)if current_embedding is not None:metadata["current_embedding_model"] = current_embedding.modeldiff --git a/deeptutor/services/rag/pipelines/lightrag/indexing_policy.py b/deeptutor/services/rag/pipelines/lightrag/indexing_policy.pyindex 439c80b3f..699fb8943 100644--- a/deeptutor/services/rag/pipelines/lightrag/indexing_policy.py+++ b/deeptutor/services/rag/pipelines/lightrag/indexing_policy.py@@ -230,10 +230,17 @@ def resolve_write_snapshot(base_dir: str,kb_name: str,explicit: IndexingLLMSnapshot | None = None,+ published_root: Path | None = None,) -> IndexingLLMSnapshot:if explicit is not None:return explicit- policy = effective_policy(kb_dir, base_dir=base_dir, kb_name=kb_name)+ from .storage import read_published_policy++ policy = (+ read_published_policy(published_root)+ if published_root is not None+ else effective_policy(kb_dir, base_dir=base_dir, kb_name=kb_name)+ )if policy is None:return freeze_default_snapshot()if policy.get("policy") == POLICY_LEGACY:diff --git a/deeptutor/services/rag/pipelines/lightrag/pipeline.py b/deeptutor/services/rag/pipelines/lightrag/pipeline.pyindex 30599d2ab..95dd92110 100644--- a/deeptutor/services/rag/pipelines/lightrag/pipeline.py+++ b/deeptutor/services/rag/pipelines/lightrag/pipeline.py@@ -449,6 +449,7 @@ class LightRagPipeline:base_dir=self.kb_base_dir,kb_name=kb_name,explicit=explicit,+ published_root=existing,)if existing is not None:root_dir = existingdiff --git a/deeptutor/services/rag/pipelines/lightrag/storage.py b/deeptutor/services/rag/pipelines/lightrag/storage.pyindex 15d3ad559..ac61a77ba 100644--- a/deeptutor/services/rag/pipelines/lightrag/storage.py+++ b/deeptutor/services/rag/pipelines/lightrag/storage.py@@ -236,6 +236,19 @@ def latest_published_root(kb_dir: Path) -> Path | None:return None+def published_root_for_embedding(kb_dir: Path, signature: str | None) -> Path | None:+ """Find provenance for the recorded binding, even after catalog identity drift."""+ if signature is None:+ return latest_published_root(kb_dir)+ from deeptutor.services.rag.index_versioning import list_kb_versions++ for entry in list_kb_versions(kb_dir):+ root = Path(entry["storage_path"])+ if entry.get("embedding_signature") == signature and meta_is_native_published(root):+ return root+ return None++def embedding_matches(root_dir: Path, signature: EmbeddingSignature | None) -> bool:"""Compare the published vector identity, never just its dimension."""meta = _read_meta(root_dir) or {}diff --git a/tests/services/rag/test_embedding_binding.py b/tests/services/rag/test_embedding_binding.pyindex 3f699a6f9..87fb2e3da 100644--- a/tests/services/rag/test_embedding_binding.py+++ b/tests/services/rag/test_embedding_binding.py@@ -113,8 +113,9 @@ def test_lightrag_reconciliation_checks_bound_index_not_global_default(assert entry["embedding_selection"] == selection("a")-def test_lightrag_reconciliation_keeps_compatible_older_bound_version(catalog, tmp_path):- from deeptutor.knowledge.manager import _reconcile_embedding_flags[email protected]("drift", [False, True])+def test_lightrag_detail_and_reconciliation_keep_recorded_bound_version(catalog, tmp_path, drift):+ from deeptutor.knowledge.manager import KnowledgeBaseManager, _reconcile_embedding_flagsfrom deeptutor.services.rag.pipelines.lightrag import engine, storageentry = write_entry(tmp_path, rag_provider="lightrag")@@ -122,13 +123,70 @@ def test_lightrag_reconciliation_keeps_compatible_older_bound_version(catalog, troot = tmp_path / "kb" / f"version-{number}"root.mkdir()(root / "kv_store_doc_status.json").write_text('{"doc":{"status":"processed"}}')- storage.write_meta(root, embedding_config=get_embedding_config(selection(model)))+ storage.write_meta(+ root,+ embedding_config=get_embedding_config(selection(model)),+ indexing_policy={"policy": "pinned", "label": model},+ )workspace = root / engine.workspace_for(root)workspace.mkdir()(workspace / "kv_store_doc_status.json").write_text('{"doc":{"status":"processed"}}')_reconcile_embedding_flags({"kb": entry}, tmp_path)assert not entry.get("embedding_mismatch")assert entry["embedding_selection"] == selection("a")+ manager = KnowledgeBaseManager(str(tmp_path))+ if drift:+ catalog["services"]["embedding"]["profiles"][0]["models"][0]["model"] = "changed-a"+ _reconcile_embedding_flags(manager.config["knowledge_bases"], tmp_path)+ metadata = manager.get_info("kb")["metadata"]+ assert metadata["indexed_embedding_model"] == "embed-a"+ assert metadata["current_embedding_model"] == ("changed-a" if drift else "embed-a")+ assert metadata["indexing_policy"]["label"] == "a"+++def test_lightrag_append_uses_policy_of_actual_bound_index(catalog, tmp_path, monkeypatch):+ from deeptutor.services.embedding.config import embedding_config_scope+ from deeptutor.services.rag.pipelines.lightrag import engine, indexing_policy, storage+ from deeptutor.services.rag.pipelines.lightrag.pipeline import BatchOutcome, LightRagPipeline++ write_entry(tmp_path, rag_provider="lightrag")+ for number, model in [(1, "a"), (2, "b")]:+ root = tmp_path / "kb" / f"version-{number}"+ root.mkdir()+ workspace = root / engine.workspace_for(root)+ workspace.mkdir()+ (workspace / "kv_store_doc_status.json").write_text('{"doc":{"status":"processed"}}')+ storage.write_meta(+ root,+ embedding_config=get_embedding_config(selection(model)),+ indexing_policy={"policy": "pinned", "label": model},+ )+ newer = tmp_path / "kb" / "version-2" / "meta.json"+ before = newer.read_bytes()+ monkeypatch.setattr(+ indexing_policy,+ "snapshot_from_persisted",+ lambda policy: SimpleNamespace(vision_available=False, persisted_policy=lambda: policy),+ )+ pipeline = LightRagPipeline(str(tmp_path))+ monkeypatch.setattr(pipeline, "_ensure_available", lambda: None)++ async def index(root, files, progress, snapshot, *, embedding_config):+ assert root.name == "version-1"+ assert snapshot.persisted_policy()["label"] == "a"+ assert embedding_config.model == "embed-a"+ return BatchOutcome(requested=1, accepted=1, processed=("new.md",))++ monkeypatch.setattr(pipeline, "_run_indexing", index)+ with embedding_config_scope(get_embedding_config(selection("a"))):+ assert asyncio.run(pipeline.add_documents("kb", ["new.md"]))+ assert newer.read_bytes() == before+ assert (+ json.loads((tmp_path / "kb" / "version-1" / "meta.json").read_text())["indexing_policy"][+ "label"+ ]+ == "a"+ )def test_deleted_or_edited_model_does_not_fall_back(catalog, tmp_path):