From dd95ff97961b85ce195a8302500cf58398c199e8 Mon Sep 17 00:00:00 2001 From: huanghuoguoguo <60681390+huanghuoguoguo@users.noreply.github.com> Date: Mon, 2 Nov 2026 21:10:07 +0800 Subject: [PATCH] fix(LongTermMemory): clear derived memory across document and KB deletion --- .../KnowledgeEngine/LongTermMemory/README.md | 2 + .../knowledge_engine/memory_engine.py | 28 ++++ .../LongTermMemory/store/consolidation.py | 30 +++- .../LongTermMemory/store/memory_store.py | 20 ++- .../test_memory_document_lineage.py | 137 ++++++++++++++++++ .../store/test_consolidation_lifecycle.py | 78 ++++++++++ .../store/test_semantic_consolidation.py | 8 +- 7 files changed, 286 insertions(+), 17 deletions(-) create mode 100644 plugins/KnowledgeEngine/LongTermMemory/tests/knowledge_engine/test_memory_document_lineage.py create mode 100644 plugins/KnowledgeEngine/LongTermMemory/tests/store/test_consolidation_lifecycle.py diff --git a/plugins/KnowledgeEngine/LongTermMemory/README.md b/plugins/KnowledgeEngine/LongTermMemory/README.md index 67377840b..d59a45ff9 100644 --- a/plugins/KnowledgeEngine/LongTermMemory/README.md +++ b/plugins/KnowledgeEngine/LongTermMemory/README.md @@ -648,3 +648,5 @@ Set `consolidation_model_uuid` on the memory knowledge base only when you want m Active sources remain active; this feature does not promise lossy compression or token savings. A summary never extends its sources' validity: `valid_from` is their latest start and `valid_until` their earliest end. Changing a source to superseded/archived/deleted, explicitly correcting it through `remember.supersedes`, or forgetting it first removes same-scope derived summaries. Forgetting also clears any pending review draft that contains that source. Other source facts remain searchable; summaries from another memory scope are never cascaded. Scoped bulk deletion covers the same boundaries. Existing historical summary records without lineage cannot be retroactively traced. They are not migrated automatically. Semantic drafting and explicit approval are also available through the Memory Console review panel, so processor-only deployments do not need a Pipeline command route. + +Document deletion invalidates dependent summaries and pending drafts for every affected memory scope before removing imported records. Deleting a knowledge base clears only that collection's pending drafts. Preview generation and collection deletion share the same collection lock, so an in-flight model preview cannot save source text after deletion completes; a preview for a removed KB is rejected before calling the model. Each model call holding that lock is capped at 30 seconds; queued callers and storage operations still follow their normal API lifecycle. diff --git a/plugins/KnowledgeEngine/LongTermMemory/components/knowledge_engine/memory_engine.py b/plugins/KnowledgeEngine/LongTermMemory/components/knowledge_engine/memory_engine.py index 19feb93f2..7a05194b7 100644 --- a/plugins/KnowledgeEngine/LongTermMemory/components/knowledge_engine/memory_engine.py +++ b/plugins/KnowledgeEngine/LongTermMemory/components/knowledge_engine/memory_engine.py @@ -24,6 +24,7 @@ from langbot_plugin.api.entities.builtin.rag.enums import DocumentStatus from store.recall import matches_filters, select_memories from store.serialization import serialized_write from store.episode_provenance import normalize_provenance +from store.consolidation import invalidate_derived logger = logging.getLogger(__name__) @@ -504,6 +505,33 @@ class LongTermMemoryEngine(KnowledgeEngine): @serialized_write async def _delete_document_vectors(self, kb_id: str, document_id: str) -> int: + # Resolve all sources before deleting summaries can change pagination. + sources: dict[str, set[str]] = {} + offset = 0 + while True: + page = await self.plugin.vector_list( + collection_id=kb_id, + filters={"document_id": document_id}, + limit=100, + offset=offset, + ) + items = page.get("items", []) + for item in items: + metadata = item.get("metadata", {}) + episode_id = item.get("id") + user_key = metadata.get("user_key") + if ( + metadata.get("document_id") != document_id + or not isinstance(episode_id, str) or not episode_id + or not isinstance(user_key, str) or not user_key + ): + raise ValueError("Document memory source identity is missing or outside the requested document") + sources.setdefault(user_key, set()).add(episode_id) + offset += len(items) + if len(items) < 100 or (page.get("total", -1) >= 0 and offset >= page["total"]): + break + for user_key, episode_ids in sorted(sources.items()): + await invalidate_derived(self.plugin.memory_store, kb_id, episode_ids, user_key) return await self._writes.mutate( self.plugin, kb_id, diff --git a/plugins/KnowledgeEngine/LongTermMemory/store/consolidation.py b/plugins/KnowledgeEngine/LongTermMemory/store/consolidation.py index e9c4790b9..e3111da36 100644 --- a/plugins/KnowledgeEngine/LongTermMemory/store/consolidation.py +++ b/plugins/KnowledgeEngine/LongTermMemory/store/consolidation.py @@ -12,11 +12,12 @@ from langbot_plugin.api.entities.builtin.provider.message import Message from store.episode_provenance import normalize_provenance, read_provenance MODEL_TIMEOUT_SECONDS = 30 +PROPOSAL_PREFIX = "ltm.consolidation.proposal." def proposal_key(collection_id: str, user_key: str) -> str: identity = json.dumps([collection_id, user_key], ensure_ascii=False) - return "ltm.consolidation.proposal." + hashlib.sha256(identity.encode()).hexdigest() + return PROPOSAL_PREFIX + hashlib.sha256(identity.encode()).hexdigest() def source_hash(episode: dict) -> str: @@ -59,6 +60,8 @@ def source_record(source: dict) -> dict: async def draft_summary(store, preview: dict, collection_id: str, user_key: str, model_uuid: str) -> dict: """Generate a draft; the model never selects the memory scope or source set.""" + if collection_id not in await store.get_kb_configs(): + raise ValueError("Memory knowledge base is no longer configured") sources = [] for candidate in preview["candidates"]: if candidate["status"] != store.EPISODE_STATUS_ACTIVE: @@ -155,6 +158,8 @@ async def draft_summary(store, preview: dict, collection_id: str, user_key: str, async def apply_summary(store, collection_id: str, embedding_model_uuid: str, user_key: str, proposal_id: str) -> dict: + if collection_id not in await store.get_kb_configs(): + raise ValueError("Memory knowledge base is no longer configured") proposal = await store._read_json(proposal_key(collection_id, user_key)) if ( not isinstance(proposal, dict) @@ -223,18 +228,33 @@ async def apply_summary(store, collection_id: str, embedding_model_uuid: str, us } -async def clear_proposal(store, collection_id: str, user_key: str, episode_id: str | None = None) -> None: +async def clear_collection_proposals(store, collection_id: str) -> None: + for key in await store.plugin.get_plugin_storage_keys(): + if not key.startswith(PROPOSAL_PREFIX): + continue + proposal = await store._read_json(key) + if isinstance(proposal, dict) and proposal.get("collection_id") == collection_id: + await store._write_json(key, {}, collection_id) + + +async def clear_proposal(store, collection_id: str, user_key: str, episode_id: str | set[str] | None = None) -> None: key = proposal_key(collection_id, user_key) if key not in await store.plugin.get_plugin_storage_keys(): return proposal = await store._read_json(key) - if isinstance(proposal, dict) and proposal and (episode_id is None or episode_id in proposal.get("source_ids", [])): + targets = {episode_id} if isinstance(episode_id, str) else episode_id + if ( + isinstance(proposal, dict) + and proposal + and (targets is None or targets.intersection(proposal.get("source_ids", []))) + ): await store._write_json(proposal_key(collection_id, user_key), {}, collection_id) -async def invalidate_derived(store, collection_id: str, episode_id: str, user_key: str) -> None: +async def invalidate_derived(store, collection_id: str, episode_id: str | set[str], user_key: str) -> None: """Forget derived text before changing/deleting its authoritative source.""" await clear_proposal(store, collection_id, user_key, episode_id) + targets = {episode_id} if isinstance(episode_id, str) else episode_id offset = 0 dependents = [] while True: @@ -252,7 +272,7 @@ async def invalidate_derived(store, collection_id: str, episode_id: str, user_ke raise ValueError("Consolidation lineage is corrupt; reconcile before changing its sources") from exc if not isinstance(references, list) or any(not isinstance(ref, str) for ref in references): raise ValueError("Consolidation lineage is corrupt; reconcile before changing its sources") - if episode_id in references: + if targets.intersection(references): dependents.append(item["id"]) offset += len(items) if len(items) < 100 or (page.get("total", -1) >= 0 and offset >= page["total"]): diff --git a/plugins/KnowledgeEngine/LongTermMemory/store/memory_store.py b/plugins/KnowledgeEngine/LongTermMemory/store/memory_store.py index b7138cb65..ed94d06a9 100644 --- a/plugins/KnowledgeEngine/LongTermMemory/store/memory_store.py +++ b/plugins/KnowledgeEngine/LongTermMemory/store/memory_store.py @@ -11,8 +11,7 @@ from langbot_plugin.entities.io.errors import ActionCallError from store.recall import exact_match_score, matches_filters, select_memories from store.serialization import FENCE_KEY_PREFIX, BindingWrites, serialized_write from store.episode_provenance import normalize_provenance, read_provenance - -from store.consolidation import apply_summary, clear_proposal, draft_summary, invalidate_derived +from store.consolidation import apply_summary, clear_collection_proposals, clear_proposal, draft_summary, invalidate_derived logger = logging.getLogger(__name__) @@ -634,12 +633,16 @@ class MemoryStore: @serialized_write(scope=_installation_scope, allow_fenced=True) async def remove_kb_config(self, kb_id: str) -> None: - configs = await self._read_json(self._KB_CONFIGS_KEY) or {} - configs.pop(kb_id, None) - await self._write_json(self._KB_CONFIGS_KEY, configs, None) - # Unregistering the KB deletes the fenced object, so whatever its last - # ambiguous write was is moot and the collection starts unfenced. - await self._writes.clear_fence(self.plugin, kb_id) + async def remove(): + await clear_collection_proposals(self, kb_id) + configs = await self._read_json(self._KB_CONFIGS_KEY) or {} + configs.pop(kb_id, None) + await self._write_json(self._KB_CONFIGS_KEY, configs, None) + # The deleted collection no longer needs mutation reconciliation. + await self._writes.clear_fence(self.plugin, kb_id) + + # Lock order is installation -> collection; collection operations only read configs. + await self._writes.run(self.plugin, kb_id, remove, allow_fenced=True) async def get_kb_configs(self) -> dict[str, dict[str, Any]]: return await self._read_json(self._KB_CONFIGS_KEY) or {} @@ -1749,6 +1752,7 @@ class MemoryStore: now_dt = now or datetime.now(timezone.utc) return max(0.0, (now_dt - ts.astimezone(timezone.utc)).total_seconds() / 86400.0) + @serialized_write async def preview_consolidation( self, collection_id: str, diff --git a/plugins/KnowledgeEngine/LongTermMemory/tests/knowledge_engine/test_memory_document_lineage.py b/plugins/KnowledgeEngine/LongTermMemory/tests/knowledge_engine/test_memory_document_lineage.py new file mode 100644 index 000000000..ebb709fdb --- /dev/null +++ b/plugins/KnowledgeEngine/LongTermMemory/tests/knowledge_engine/test_memory_document_lineage.py @@ -0,0 +1,137 @@ +from __future__ import annotations + +import json + +import pytest + +from components.knowledge_engine.memory_engine import LongTermMemoryEngine +from store.consolidation import proposal_key +from store.memory_store import MemoryStore + + +class DocumentPlugin: + def __init__(self): + self.records = {} + self.storage = {} + self.document_pages = [] + self.document_deletes = 0 + self.scope_pages = 0 + self.fail_summary_delete = False + self.memory_store = MemoryStore(self) + + async def get_plugin_storage(self, key): + return self.storage[key] + + async def get_plugin_storage_keys(self): + return list(self.storage) + + async def set_plugin_storage(self, key, data): + self.storage[key] = data + + def selected(self, filters): + records = list(self.records.values()) + for condition in filters.get("$and", [filters]): + for key, value in condition.items(): + records = [item for item in records if item["metadata"].get(key) == value] + return records + + async def vector_list(self, collection_id, filters, limit, offset): + assert collection_id == "kb" + if "document_id" in filters: + self.document_pages.append(offset) + else: + self.scope_pages += 1 + records = self.selected(filters) + return {"items": records[offset : offset + limit], "total": len(records)} + + async def vector_delete(self, collection_id, filters): + assert collection_id == "kb" + records = self.selected(filters) + if "document_id" in filters: + self.document_deletes += 1 + assert "summary-a" not in self.records + assert "summary-b" not in self.records + assert json.loads(self.storage[proposal_key("kb", "a")]) == {} + assert json.loads(self.storage[proposal_key("kb", "b")]) == {} + elif self.fail_summary_delete: + raise RuntimeError("summary deletion outcome unknown") + for item in records: + self.records.pop(item["id"]) + return len(records) + + +def record(plugin, episode_id, scope, *, document="", references=None): + metadata = { + "episode_id": episode_id, + "content": f"Fact {episode_id}", + "user_key": scope, + "source": "import", + "status": "active", + "timestamp": "2026-01-01T00:00:00Z", + "importance": "2", + "sender_id": "alice", + } + if document: + metadata["document_id"] = document + if references is not None: + metadata["source"] = "consolidation" + metadata["consolidation_source_ids"] = json.dumps(references) + plugin.records[episode_id] = {"id": episode_id, "metadata": metadata} + + +def setup_plugin(): + plugin = DocumentPlugin() + for index in range(205): + record(plugin, f"document-{index:03d}", "a" if index % 2 == 0 else "b", document="document") + record(plugin, "summary-a", "a", references=["document-000"]) + record(plugin, "summary-b", "b", references=["document-001"]) + record(plugin, "other-scope-summary", "other", references=["document-000"]) + record(plugin, "unrelated-summary", "a", references=["outside-document"]) + record(plugin, "outside-document", "a", document="other-document") + for scope, source in (("a", "document-000"), ("b", "document-001")): + plugin.storage[proposal_key("kb", scope)] = json.dumps( + {"source_ids": [source], "summary": "pending source fact"} + ).encode() + engine = LongTermMemoryEngine() + engine.plugin = plugin + return plugin, engine + + +@pytest.mark.asyncio +async def test_document_delete_invalidates_derived_memories_in_each_source_scope_before_sources(): + plugin, engine = setup_plugin() + assert await engine.delete_document("kb", "document") is True + assert plugin.document_pages == [0, 100, 200] + assert plugin.document_deletes == 1 + assert plugin.scope_pages < 30 + assert set(plugin.records) == {"outside-document", "unrelated-summary", "other-scope-summary"} + + +@pytest.mark.asyncio +async def test_failed_derived_invalidation_never_deletes_document_sources(): + plugin, engine = setup_plugin() + plugin.fail_summary_delete = True + with pytest.raises(RuntimeError, match="summary deletion outcome"): + await engine._delete_document_vectors("kb", "document") + assert plugin.document_deletes == 0 + assert sum(item["metadata"].get("document_id") == "document" for item in plugin.records.values()) == 205 + assert "outside-document" in plugin.records + assert "other-scope-summary" in plugin.records + + +@pytest.mark.asyncio +async def test_document_delete_fails_closed_when_backend_returns_another_document(): + plugin, engine = setup_plugin() + original = plugin.vector_list + + async def unfiltered(*args, **kwargs): + page = await original(*args, **kwargs) + if "document_id" in kwargs["filters"]: + return {"items": [plugin.records["outside-document"]], "total": 1} + return page + + plugin.vector_list = unfiltered + with pytest.raises(ValueError, match="outside the requested document"): + await engine._delete_document_vectors("kb", "document") + assert plugin.document_deletes == 0 + assert len(plugin.records) == 210 diff --git a/plugins/KnowledgeEngine/LongTermMemory/tests/store/test_consolidation_lifecycle.py b/plugins/KnowledgeEngine/LongTermMemory/tests/store/test_consolidation_lifecycle.py new file mode 100644 index 000000000..805122a70 --- /dev/null +++ b/plugins/KnowledgeEngine/LongTermMemory/tests/store/test_consolidation_lifecycle.py @@ -0,0 +1,78 @@ +from __future__ import annotations + +import asyncio +import json + +import pytest + +from store.consolidation import proposal_key +from store.memory_store import MemoryStore +from test_semantic_consolidation import SemanticPlugin, add_source, preview + + +@pytest.mark.asyncio +@pytest.mark.parametrize("delete", ["forget", "collection"]) +async def test_inflight_preview_finishes_before_delete_clears_its_draft(delete): + plugin = SemanticPlugin() + add_source(plugin, "one", "Fact to forget") + store = MemoryStore(plugin) + entered, release = asyncio.Event(), asyncio.Event() + original = plugin.invoke_llm + + async def blocked(*args): + entered.set() + await release.wait() + return await original(*args) + + plugin.invoke_llm = blocked + draft_task = asyncio.create_task(preview(store)) + await entered.wait() + deletion = store.delete_episode_by_id("kb", "one", "scope") if delete == "forget" else store.remove_kb_config("kb") + delete_task = asyncio.create_task(deletion) + await asyncio.sleep(0) + assert not delete_task.done() + release.set() + await asyncio.wait_for(asyncio.gather(draft_task, delete_task), timeout=2) + assert await store._read_json(proposal_key("kb", "scope")) == {} + if delete == "collection": + assert "kb" not in await store.get_kb_configs() + else: + assert "one" not in plugin.records + + +@pytest.mark.asyncio +async def test_collection_delete_clears_only_its_drafts_and_rejects_late_preview(): + plugin = SemanticPlugin() + add_source(plugin, "one", "Fact to forget") + store = MemoryStore(plugin) + await preview(store) + other_key = proposal_key("other-kb", "scope") + other = {"id": "other", "collection_id": "other-kb", "summary": "Other collection private fact"} + plugin.storage[other_key] = json.dumps(other).encode() + await store.remove_kb_config("kb") + assert await store._read_json(proposal_key("kb", "scope")) == {} + assert await store._read_json(other_key) == other + calls = plugin.model_calls + with pytest.raises(ValueError, match="no longer configured"): + await preview(store) + assert plugin.model_calls == calls + assert await store._read_json(proposal_key("kb", "scope")) == {} + + +@pytest.mark.asyncio +async def test_summary_validity_does_not_round_source_microseconds_outward(): + plugin = SemanticPlugin() + add_source( + plugin, + "one", + "Fractional validity", + valid_from="2020-01-01T00:00:00.123456Z", + valid_until="2030-01-01T00:00:00.654321Z", + ) + store = MemoryStore(plugin) + draft = await preview(store) + summary = (await store.apply_consolidation("kb", "emb", "scope", proposal_id=draft["proposal_id"]))[ + "summary_episode" + ] + assert summary["metadata"]["valid_from"] == "2020-01-01T00:00:00.123456Z" + assert summary["metadata"]["valid_until"] == "2030-01-01T00:00:00.654321Z" diff --git a/plugins/KnowledgeEngine/LongTermMemory/tests/store/test_semantic_consolidation.py b/plugins/KnowledgeEngine/LongTermMemory/tests/store/test_semantic_consolidation.py index 6e82203ef..6de166400 100644 --- a/plugins/KnowledgeEngine/LongTermMemory/tests/store/test_semantic_consolidation.py +++ b/plugins/KnowledgeEngine/LongTermMemory/tests/store/test_semantic_consolidation.py @@ -12,7 +12,7 @@ from store.memory_store import MemoryStore class SemanticPlugin: def __init__(self): self.records = {} - self.storage = {} + self.storage = {"kb_configs": json.dumps({"kb": {"embedding_model_uuid": "emb"}}).encode()} self.response = None self.drop_summary = False self.model_calls = 0 @@ -134,7 +134,7 @@ async def test_model_schema_cannot_select_scope_or_omit_sources(facts): with pytest.raises(ValueError): await preview(MemoryStore(plugin)) assert len(plugin.records) == 2 - assert plugin.storage == {} + assert not any(key.startswith("ltm.consolidation.") for key in plugin.storage) @pytest.mark.asyncio @@ -297,7 +297,7 @@ async def test_hanging_model_times_out_without_saving_a_proposal(monkeypatch): monkeypatch.setattr(consolidation, "MODEL_TIMEOUT_SECONDS", 0.001) with pytest.raises(ValueError, match="timed out"): await preview(MemoryStore(plugin)) - assert plugin.storage == {} + assert not any(key.startswith("ltm.consolidation.") for key in plugin.storage) assert plugin.records["one"]["metadata"]["status"] == "active" @@ -319,7 +319,7 @@ async def test_cancelled_model_does_not_leave_an_escaped_write(): task.cancel() with pytest.raises(asyncio.CancelledError): await task - assert plugin.storage == {} + assert not any(key.startswith("ltm.consolidation.") for key in plugin.storage) assert list(plugin.records) == ["one"]