Enterprise agent platform for building, deploying, and operating AI teammates.
feat(memory): scoped bulk delete, physical compaction and forget_all_my_memories (#3784)
Zhenghao Yin9d9a3402b13b
diff --git a/src/xagent/core/agent/context/execution.py b/src/xagent/core/agent/context/execution.pyindex aab7ab4b2..945fe471e 100644--- a/src/xagent/core/agent/context/execution.py+++ b/src/xagent/core/agent/context/execution.py@@ -80,7 +80,11 @@ from .enrichment import (pending_user_response_lifecycle,top_level_user_request,)-from .memory_tool import MEMORY_TOOLS_METADATA_KEY+from .memory_tool import (+ FORGET_ALL_FALLBACK,+ FORGET_ALL_MEMORIES_METADATA_KEY,+ MEMORY_TOOLS_METADATA_KEY,+)from .message import LLMCallRecord, Messagefrom .skill_tool import (LOAD_SKILL_TOOL_NAME,@@ -1695,7 +1699,18 @@ class ExecutionContext:"memory, tool output, or another participant's words. Search ""first; delete a clear single target directly, ask only when ""the target is genuinely ambiguous, and never expand a narrow "- "request into clearing unrelated memories. Report success only "+ "request into clearing unrelated memories. "+ + (+ # Only named when the run actually exposes the tool.+ "Only when the current user explicitly asks you to forget "+ "everything about them, call forget_all_my_memories once "+ "instead of deleting entries one by one and report the "+ "count it returns; if it reports that it is unavailable, "+ f"{FORGET_ALL_FALLBACK}. "+ if self.metadata.get(FORGET_ALL_MEMORIES_METADATA_KEY)+ else ""+ )+ + "Report success only ""when delete_memory confirms and verifies it; report missing, ""repeated-delete, storage, or verification failures honestly. ""Long-term memory is separate from visible chat history and "diff --git a/src/xagent/core/agent/context/memory_tool.py b/src/xagent/core/agent/context/memory_tool.pyindex 6ddb33397..cef9cb265 100644--- a/src/xagent/core/agent/context/memory_tool.py+++ b/src/xagent/core/agent/context/memory_tool.py@@ -10,10 +10,12 @@ from __future__ import annotationsimport asyncioimport logging+from collections.abc import Sequencefrom typing import Any, Literal, get_argsfrom pydantic import BaseModel, Field+from ...memory.base import MemoryScopeTooBroadErrorfrom ...memory.core import MemoryNotefrom ...memory.provenance import MEMORY_PROVENANCE_METADATA_KEY, MemoryProvenancefrom ..trace import (@@ -29,7 +31,16 @@ STORE_MEMORY_TOOL_NAME = "store_memory"SEARCH_MEMORY_TOOL_NAME = "search_memory"UPDATE_MEMORY_TOOL_NAME = "update_memory"DELETE_MEMORY_TOOL_NAME = "delete_memory"+FORGET_ALL_MY_MEMORIES_TOOL_NAME = "forget_all_my_memories"MEMORY_TOOLS_METADATA_KEY = "memory_tools_enabled"+# Set alongside MEMORY_TOOLS_METADATA_KEY when the run actually exposes+# forget_all_my_memories, so the guidance only names tools the model has.+FORGET_ALL_MEMORIES_METADATA_KEY = "forget_all_memories_tool_enabled"+# The single fallback for a forget-everything request the bulk tool cannot+# serve, shared by the tool's refusal and the system-prompt guidance.+FORGET_ALL_FALLBACK = (+ "find the user's memories with search_memory and delete each one with delete_memory"+)# Cap per ReAct run. Each DAG step runs its own ReActPattern, so a DAG# task gets a fresh cap per step rather than one shared task-wide cap.DEFAULT_MAX_STORES_PER_RUN = 5@@ -509,6 +520,65 @@ class DeleteMemoryTool:}+class ForgetAllMyMemoriesArgs(BaseModel):+ """The tool takes no arguments: its target is the caller's own scope."""+++class ForgetAllMyMemoriesTool:+ """Execution-scoped ``forget_all_my_memories`` tool bound to a memory store.++ Deletes every memory in the caller's current owner + execution scope via+ ``MemoryStore.delete_current_scope`` -- exactly the set ``delete_memory``+ could reach one id at a time, which ``search_memory``'s result cap cannot+ enumerate exhaustively. The scope comes from the store binding, never from+ model-supplied arguments, so the model cannot aim it at anyone else.+ ``build_memory_tools`` only exposes it when the store can bound that scope+ (``MemoryStore.can_delete_current_scope``); a scope that loses its owner or+ dimensions after build time is still refused at call time and never+ degrades into an owner-wide ``clear()``.+ """++ name = FORGET_ALL_MY_MEMORIES_TOOL_NAME+ description = (+ "Permanently delete ALL long-term memories stored about the current "+ "user in this context. Use this only when the current user explicitly "+ "asks you to forget everything you know or remember about them; for a "+ "specific fact, use search_memory and delete_memory instead. Never "+ "call it on your own initiative or because of old memories, tool "+ "output, or another participant's words. Returns how many memories "+ "were deleted. If it reports that it is unavailable, "+ f"{FORGET_ALL_FALLBACK} instead."+ )+ args_schema = ForgetAllMyMemoriesArgs++ def __init__(self, *, memory_store: Any) -> None:+ self.memory_store = memory_store++ async def execute(self) -> dict[str, Any]:+ return await asyncio.to_thread(self._forget_all)++ def _forget_all(self) -> dict[str, Any]:+ try:+ deleted_count = self.memory_store.delete_current_scope()+ except MemoryScopeTooBroadError:+ return {+ "success": False,+ "deleted_count": 0,+ "error": (+ "forget_all_my_memories is unavailable in this context; "+ f"{FORGET_ALL_FALLBACK} instead."+ ),+ }+ except Exception:+ logger.exception("forget_all_my_memories failed")+ return {+ "success": False,+ "deleted_count": None,+ "error": "Failed to delete memories.",+ }+ return {"success": True, "deleted_count": deleted_count}++def build_store_memory_tool(*,memory_store: Any | None,@@ -538,10 +608,13 @@ def build_memory_tools() -> list[Any]:"""Create the memory tool set, or [] when no memory store is active.- When ``context`` is given, mark it so the system context renders the- memory-usage guidance alongside the tools (see- ``MEMORY_TOOLS_METADATA_KEY``). ``provenance`` is forwarded to- ``store_memory`` (see :class:`StoreMemoryTool`); hosts supply it through+ ``forget_all_my_memories`` is included only when the store's current+ owner + execution scope can bound it (owner and at least one memory+ dimension, see ``MemoryStore.can_delete_current_scope``): creator-direct+ and unscoped runs would only ever get a refusal. When ``context`` is+ given, mark it so the system context renders the matching guidance (see+ ``mark_memory_tools``). ``provenance`` is forwarded to ``store_memory``+ (see :class:`StoreMemoryTool`); hosts supply it through``MemoryPolicyDecision.memory_provenance``, which binds it here as therun's memory-tools factory."""@@ -559,11 +632,30 @@ def build_memory_tools(UpdateMemoryTool(memory_store=memory_store),DeleteMemoryTool(memory_store=memory_store),]- if tools and context is not None and hasattr(context, "metadata"):- context.metadata[MEMORY_TOOLS_METADATA_KEY] = True+ if memory_store.can_delete_current_scope():+ tools.append(ForgetAllMyMemoriesTool(memory_store=memory_store))+ mark_memory_tools(context, tools)return tools+def mark_memory_tools(context: Any | None, tools: Sequence[Any]) -> None:+ """Record on ``context`` which memory guidance the system prompt renders.++ Shared by ``build_memory_tools`` and host memory-tool factories, so the+ guidance names ``forget_all_my_memories`` exactly when the run exposes a+ tool of that name. The forget flag is always rewritten, so a context+ reused across runs cannot keep a stale ``True``.+ """+ if context is None or not hasattr(context, "metadata"):+ return+ if tools:+ context.metadata[MEMORY_TOOLS_METADATA_KEY] = True+ context.metadata[FORGET_ALL_MEMORIES_METADATA_KEY] = any(+ getattr(tool, "name", None) == FORGET_ALL_MY_MEMORIES_TOOL_NAME+ for tool in tools+ )++def _runtime_attr(runtime: Any | None, name: str) -> Any | None:if runtime is None:return Nonediff --git a/src/xagent/core/agent/pattern/react/react.py b/src/xagent/core/agent/pattern/react/react.pyindex bc1305af7..54d1d515d 100644--- a/src/xagent/core/agent/pattern/react/react.py+++ b/src/xagent/core/agent/pattern/react/react.py@@ -126,7 +126,7 @@ from ...context.execution import (snapshot_container,tool_evidence_state,)-from ...context.memory_tool import MEMORY_TOOLS_METADATA_KEY, build_memory_tools+from ...context.memory_tool import build_memory_tools, mark_memory_toolsfrom ...context.skill_tool import build_load_skill_toolfrom ...deferred_tools import (AVAILABLE_NAMES_SUFFIX,@@ -1041,8 +1041,7 @@ class ReActPattern(AgentPattern):context=context,))- if extra_tools and hasattr(context, "metadata"):- context.metadata[MEMORY_TOOLS_METADATA_KEY] = True+ mark_memory_tools(context, extra_tools)skill_tool = await build_load_skill_tool(skill_manager=skill_manager,context=context,diff --git a/src/xagent/core/memory/base.py b/src/xagent/core/memory/base.pyindex 710f65a5d..84964602a 100644--- a/src/xagent/core/memory/base.py+++ b/src/xagent/core/memory/base.py@@ -2,16 +2,55 @@ from __future__ import annotationsfrom abc import ABC, abstractmethodfrom datetime import MINYEAR, datetime, timezone-from typing import Any, Collection, List, Optional+from typing import Any, Collection, List, Mapping, Optional+from ..execution_scope import ExecutionScopefrom .core import MemoryNote, MemoryResponsefrom .scope_columns import (SCOPE_EXCLUSIVE_FILTER_KEY,+ coerce_user_id,encode_scope_dims,scope_dim_element,)+class MemoryScopeTooBroadError(ValueError):+ """A bulk delete was requested without a scope narrow enough to bound it.++ Raised instead of widening the delete: a scoped bulk delete needs both an+ owner and at least one execution-scope dimension, otherwise it would reach+ every principal sharing that owner (e.g. every member of one Slack+ workspace, all stored under one machine owner).+ """+++def validate_scope_delete(+ owner_user_id: int, dims: Mapping[str, str]+) -> dict[str, str]:+ """Validate the predicate of a scoped bulk delete and return its dimensions.++ ``owner_user_id`` must be a positive signed-int64 ``int`` (``bool`` and+ lossy numeric spellings are rejected, as for ``MemoryAccessScope``).+ ``dims`` must be non-empty and satisfy the ``ExecutionScope`` component+ rules, i.e. exactly what a write could have stamped: string keys matching+ ``[a-zA-Z0-9_-]{1,63}`` and non-empty string values. An integer value is+ rejected rather than ``str()``-coerced, so a caller can never silently+ match nothing because of a non-canonical spelling.++ Raises:+ ValueError: ``owner_user_id`` is not a positive signed-int64 ``int``.+ MemoryScopeTooBroadError: ``dims`` is empty.+ InvalidScopeComponentError: a key or value is not a valid component.+ """+ if type(owner_user_id) is not int or not 0 < owner_user_id < 2**63:+ raise ValueError("owner_user_id must be a positive signed int64")+ if not dims:+ raise MemoryScopeTooBroadError(+ "a scoped memory delete requires at least one scope dimension"+ )+ return dict(ExecutionScope(memory_dimensions=dims).memory_dimensions)++def comparable_timestamp(value: Any) -> Any:"""Normalize a datetime so naive and aware values order correctly.@@ -360,6 +399,79 @@ class MemoryStore(ABC):)return MemoryResponse(success=True, metadata={"deleted_count": deleted})+ def delete_for_scope(self, owner_user_id: int, dims: Mapping[str, str]) -> int:+ """+ Delete every note of ``owner_user_id`` that carries all of ``dims``.++ Trusted control-plane primitive for scoped erasure, e.g. "forget+ everything in this workspace" (owner + ``client_application_id``) or+ "forget this member" (owner + ``end_user_id`` + ...). The owner+ predicate and at least one dimension are mandatory (see+ ``validate_scope_delete``), so this can never degrade into an+ owner-wide or store-wide wipe. A note matches when its owner equals+ ``owner_user_id`` and its scope dimensions are a superset of ``dims``+ (exact per-dimension string equality, the same semantics as a scoped+ search); dimension-less notes never match.++ Bypasses per-user isolation wrappers: callers are responsible for+ deriving ``owner_user_id``/``dims`` from trusted state, never from+ agent or end-user input.++ This default walks ``list_all()`` and deletes note-by-note, assuming+ ``list_all()`` returns the complete store; backends with a native+ predicate delete should override it.++ Returns:+ int: Number of notes deleted.++ Raises:+ ValueError: invalid predicate (see ``validate_scope_delete``).+ RuntimeError: a matching note could not be deleted. Notes deleted+ before the failure stay deleted; re-running is idempotent.+ """+ required = {+ scope_dim_element(key, value)+ for key, value in validate_scope_delete(owner_user_id, dims).items()+ }+ deleted = 0+ for note in self.list_all():+ if (+ note.id+ and coerce_user_id(note.metadata.get("user_id")) == owner_user_id+ and required <= set(encode_scope_dims(note.metadata))+ ):+ response = self.delete(note.id)+ if not response.success:+ raise RuntimeError(+ f"Failed to delete memory {note.id!r}: {response.error}"+ )+ deleted += 1+ return deleted++ def can_delete_current_scope(self) -> bool:+ """+ Whether ``delete_current_scope`` would act rather than refuse.++ Lets tool builders expose a scoped bulk delete only where it can+ work. ``False`` for stores without an owner/execution-scope binding.+ """+ return False++ def delete_current_scope(self) -> int:+ """+ Delete every note in the caller's current memory scope.++ Only stores bound to an owner and an execution scope (e.g.+ ``UserIsolatedMemoryStore``) can resolve that scope; a raw storage+ backend has neither, so this default refuses rather than guessing.++ Raises:+ MemoryScopeTooBroadError: always, for stores without a scope.+ """+ raise MemoryScopeTooBroadError(+ f"{type(self).__name__} is not bound to an execution scope"+ )+def list_scope_dimension_values(self, dim_key: str) -> set[str]:"""Distinct values stamped for one execution-scope dimension, store-wide.diff --git a/src/xagent/core/memory/lancedb.py b/src/xagent/core/memory/lancedb.pyindex af89ae7b7..3aa7f314b 100644--- a/src/xagent/core/memory/lancedb.py+++ b/src/xagent/core/memory/lancedb.py@@ -2,7 +2,8 @@ from __future__ import annotationsimport jsonimport logging-from typing import Any, Callable, List, Optional, Union+from datetime import timedelta+from typing import Any, Callable, List, Mapping, Optional, Unionfrom uuid import uuid4import pyarrow as pa # type: ignore@@ -16,8 +17,8 @@ from ..model.embedding import BaseEmbedding, DashScopeEmbeddingfrom ..model.embedding.adapter import create_embedding_adapterfrom ..model.model import EmbeddingModelConfigfrom ..tools.core.RAG_tools.LanceDB.schema_manager import _safe_close_table-from ..utils.lancedb_sql import sql_string_literal-from .base import MemoryStore, comparable_timestamp+from ..utils.lancedb_sql import sql_and, sql_string_in, sql_string_literal+from .base import MemoryStore, comparable_timestamp, validate_scope_deletefrom .core import MemoryNote, MemoryResponsefrom .retrieval_compatibility import DEFAULT_STREAM_BATCH_SIZE, stream_lexical_top_kfrom .schema_migration import (@@ -44,6 +45,32 @@ from .vector_compatibility import (logger = logging.getLogger(__name__)+# Age past which ``compact()`` prunes superseded table versions. Long enough+# that no in-flight memory operation (each opens the table fresh) can still be+# reading or committing against a pruned version; shorter retentions can+# silently lose a concurrent delete, see ``compact()``.+COMPACT_VERSION_RETENTION = timedelta(minutes=10)+++def _is_pruned_version_error(error: Exception) -> bool:+ """Whether a LanceDB write failed because its table version was pruned.++ A handle keeps reading the version it opened. When a concurrent+ ``compact()`` rewrites the table and prunes that version, the handle's+ data files are gone and the write fails with a "not found" error on one+ of them (``LanceError(IO): ... not found`` or ``Not found: <file>``,+ depending on where Lance notices). The binding surfaces either only as a+ ``RuntimeError`` message, so the message is the one thing to match on.+ Matching any Lance "not found" is deliberately broad: a false positive+ costs one retry that fails the same way and then propagates.+ """+ message = str(error)+ return (+ isinstance(error, RuntimeError)+ and "lance error" in message+ and "not found" in message.lower()+ )+class LanceDBMemoryStore(MemoryStore):"""LanceDB-based memory store implementation with vector search capabilities."""@@ -788,17 +815,15 @@ class LanceDBMemoryStore(MemoryStore):def delete(self, note_id: str) -> MemoryResponse:"""Delete a memory note by its ID."""+ table = Nonetry:- success = self._vector_store.delete_vectors([note_id])-- if success:- return MemoryResponse(success=True, memory_id=note_id)- else:- return MemoryResponse(- success=False,- error="Failed to delete memory",- memory_id=note_id,- )+ table = self._vector_store.get_raw_connection().open_table(+ self._collection_name+ )+ # Every id is a quoted literal, so a crafted id cannot widen the+ # predicate beyond exact-match rows.+ self._delete_where(table, sql_string_in("id", [note_id]))+ return MemoryResponse(success=True, memory_id=note_id)except Exception as e:logger.error(f"Failed to delete memory note {note_id}: {e}")@@ -807,6 +832,39 @@ class LanceDBMemoryStore(MemoryStore):error=f"Failed to delete memory: {str(e)}",memory_id=note_id,)+ finally:+ _safe_close_table(table)++ def _delete_where(self, table: Any, where: str) -> None:+ """Delete ``where`` through ``table``, rebasing once onto the latest version.++ ``compact()`` prunes versions older than ``COMPACT_VERSION_RETENTION``,+ and that cutoff ages the *version*, not the writer's snapshot: on an+ idle table the latest version can already be older than the+ retention, so a concurrent ``compact()`` may rewrite the table and+ prune it between this handle opening it and the delete committing.+ The delete then fails on the missing data files with the row still+ live. Retrying once on a freshly opened handle re-applies the same+ idempotent predicate to the version that replaced it. Anything else,+ including a second failure, propagates.+ """+ try:+ table.delete(where)+ except RuntimeError as error:+ if not _is_pruned_version_error(error):+ raise+ logger.warning(+ "Memory delete raced a compaction of %r; retrying on the latest "+ "version",+ self._collection_name,+ )+ latest = self._vector_store.get_raw_connection().open_table(+ self._collection_name+ )+ try:+ latest.delete(where)+ finally:+ _safe_close_table(latest)def delete_by_scope_dimension(self, dim_key: str, value: Any) -> MemoryResponse:"""Bulk-delete every note stamped with dimension ``dim_key=value``.@@ -831,7 +889,7 @@ class LanceDBMemoryStore(MemoryStore):)count = table.count_rows(where)if count:- table.delete(where)+ self._delete_where(table, where)return MemoryResponse(success=True, metadata={"deleted_count": count})except Exception as e:logger.error(f"Failed to delete memories by scope dimension {dim_key}: {e}")@@ -843,6 +901,115 @@ class LanceDBMemoryStore(MemoryStore):finally:_safe_close_table(table)+ def delete_for_scope(self, owner_user_id: int, dims: Mapping[str, str]) -> int:+ """Bulk-delete the owner's notes carrying every dimension in ``dims``.++ One predicate delete: ``user_id = owner`` AND one+ ``array_contains(scope_dims, 'key=value')`` term per dimension, the+ same column predicates a scoped search pushes into its prefilter, so+ the deleted set is exactly the set that scope can read. See the base+ class for the contract.++ The returned count comes from a ``count_rows`` on the same predicate+ just before the delete (LanceDB's ``delete()`` reports only the new+ table version), so it is best-effort under concurrent writers. Unlike+ ``delete_by_scope_dimension`` a backend failure raises: an erasure+ request must never be reported as done when it was not.++ Deleted rows stay readable in older table versions until+ ``compact()`` prunes them.+ """+ # Validate before any SQL is built: the owner is interpolated as a+ # bare integer, which is only safe once it is known to be an ``int``.+ validated = validate_scope_delete(owner_user_id, dims)+ where = sql_and(+ [+ f"{USER_ID_COLUMN} = {owner_user_id}",+ *(scope_dim_where_term(key, value) for key, value in validated.items()),+ ]+ )+ # Narrows the type only: the owner term is always present.+ assert where is not None+ table = self._vector_store.get_raw_connection().open_table(+ self._collection_name+ )+ try:+ count = int(table.count_rows(where))+ if count:+ self._delete_where(table, where)+ return count+ finally:+ _safe_close_table(table)++ def compact(self) -> None:+ """Physically remove deleted rows from the memories table's files.++ A LanceDB delete only masks rows: their text, metadata and vectors stay+ in the data files of the current version (behind a deletion file) and of+ every older version, which remain readable by version checkout.+ ``optimize()`` alone does not fix this: it only rewrites a fragment once+ more than 10% of its rows are deleted or it can merge it with a+ neighbouring small fragment, so a few deletes in one large compacted+ fragment survive any number of plain optimizations. This therefore runs+ two steps:++ 1. If the current version carries any deletion file, rewrite every+ live row (a no-op ``update`` over the whole table), so no live+ fragment still holds a soft-deleted row. Cost is O(table): it+ rewrites all live data, vectors included. Skipped when nothing was+ deleted since the last rewrite.+ 2. ``optimize()``: merge fragments and prune every version older than+ ``COMPACT_VERSION_RETENTION`` together with the data files only+ those versions reference.++ Physical removal guarantee: a row deleted before one ``compact()``+ starts is gone from disk after a later ``compact()`` started more than+ ``COMPACT_VERSION_RETENTION`` after the first one returned (the first+ call removes it from the current version, the later one prunes the+ older versions still holding it). A host running this periodically+ with an interval longer than the retention erases a deleted row within+ two intervals.++ Concurrency: explicit host call only, never on a request path, and+ from at most one scheduler at a time (two concurrent compactions+ conflict and one raises). Ordinary writers on any worker may run+ concurrently. The retention is fixed rather than configurable because+ pruning versions that a concurrent writer may still be committing+ against is unsafe: with a short retention a concurrent delete can be+ silently lost (it reports success but the row survives the rewrite)+ or fail outright. The retention ages versions, not writers'+ snapshots: when the table has been idle for longer than the retention,+ the version a delete just opened may itself be pruned under it. The+ store's delete paths (``delete``, ``delete_for_scope``,+ ``delete_by_scope_dimension``) detect that failure and retry once on+ the latest version (see ``_delete_where``); a delete that loses that+ race twice in a row, or any other writer path, raises instead of+ reporting success, so the row is never silently kept. Under heavy+ write contention the rewrite can exhaust LanceDB's commit retries and+ raise; nothing is lost, so the caller simply retries later.+ """+ self._compact(COMPACT_VERSION_RETENTION)++ def _compact(self, cleanup_older_than: timedelta) -> None:+ """``compact()`` with an explicit retention.++ Private test hook: a retention below ``COMPACT_VERSION_RETENTION`` is+ only safe with no concurrent writer, which production never has (see+ ``compact()``).+ """+ table = self._vector_store.get_raw_connection().open_table(+ self._collection_name+ )+ try:+ # Each version's manifest summary counts the rows masked by+ # deletion files; zero means there is nothing to materialize.+ latest = max(table.list_versions(), key=lambda entry: entry["version"])+ if int(latest["metadata"]["total_deletion_file_rows"]):+ table.update(values_sql={"id": "id"})+ table.optimize(cleanup_older_than=cleanup_older_than)+ finally:+ _safe_close_table(table)+def list_scope_dimension_values(self, dim_key: str) -> set[str]:"""Distinct stamped values for one scope dimension, store-wide.diff --git a/src/xagent/web/revocable_memory_store.py b/src/xagent/web/revocable_memory_store.pyindex 38402976c..4188399da 100644--- a/src/xagent/web/revocable_memory_store.py+++ b/src/xagent/web/revocable_memory_store.py@@ -26,7 +26,7 @@ the wrappers (``get_store_info`` reporting ``store_type``) go throughfrom __future__ import annotations-from typing import Any, List, Optional, Protocol+from typing import Any, List, Mapping, Optional, Protocolfrom ..core.memory.access_scope import MemoryAccessScopefrom ..core.memory.base import MemoryStore@@ -149,6 +149,17 @@ class RevocableMemoryStore(MemoryStore):def list_scope_dimension_values(self, dim_key: str) -> set[str]:return self._live().list_scope_dimension_values(dim_key)+ def delete_for_scope(self, owner_user_id: int, dims: Mapping[str, str]) -> int:+ return self._live().delete_for_scope(owner_user_id, dims)++ def can_delete_current_scope(self) -> bool:+ # Describes the binding without touching storage, like+ # ``unwrapped_store``; the delete itself still fails closed.+ return self._base_store.can_delete_current_scope()++ def delete_current_scope(self) -> int:+ return self._live().delete_current_scope()+def unwrap_memory_store(store: Any) -> Any:"""Peel the revocation and user-isolation wrappers off ``store``.diff --git a/src/xagent/web/user_isolated_memory.py b/src/xagent/web/user_isolated_memory.pyindex 1be77c097..bf09ddb80 100644--- a/src/xagent/web/user_isolated_memory.py+++ b/src/xagent/web/user_isolated_memory.py@@ -1,7 +1,7 @@"""User-isolated memory store for web application."""import contextvars-from typing import Any, List, Optional+from typing import Any, List, Mapping, Optionalfrom xagent.core.execution_scope import (MEMORY_DIMENSION_METADATA_PREFIX,@@ -11,7 +11,7 @@ from xagent.core.execution_scope import (metadata_carries_scope_dimensions,)from xagent.core.memory.access_scope import MemoryAccessScope-from xagent.core.memory.base import MemoryStore+from xagent.core.memory.base import MemoryScopeTooBroadError, MemoryStorefrom xagent.core.memory.core import MemoryNote, MemoryResponsefrom xagent.core.memory.provenance import MEMORY_PROVENANCE_METADATA_KEYfrom xagent.core.memory.scope_columns import SCOPE_EXCLUSIVE_FILTER_KEY@@ -339,6 +339,62 @@ class UserIsolatedMemoryStore(MemoryStore):"""return self._base_store.delete_by_scope_dimension(dim_key, value)+ def delete_for_scope(self, owner_user_id: int, dims: Mapping[str, str]) -> int:+ """Bulk-delete ``owner_user_id``'s notes carrying every dimension in ``dims``.++ Control-plane erasure primitive (e.g. "forget everything in this+ workspace"), delegated straight to the base store and deliberately+ not gated by the ambient user/scope: the explicit owner + non-empty+ dimension predicate is what bounds it. Trusted-caller-only, for the+ same reason as ``delete_by_scope_dimension`` -- never feed it owner or+ dimension values taken from agent or end-user input. Agent-facing code+ uses ``delete_current_scope`` instead.+ """+ return self._base_store.delete_for_scope(owner_user_id, dims)++ def _current_scope_predicate(self) -> tuple[int, dict[str, str]] | None:+ """The ``(owner, dimensions)`` bounding ``delete_current_scope``.++ ``None`` when either is missing:++ - no owner: the by-id path is ungated, so there is nothing to bound+ the delete to;+ - no memory dimensions: the owner may be shared by many principals+ that are only told apart by their dimensions (one machine owner per+ Slack workspace hosting every member), and a dimension-less+ ``strict_memory_isolation`` scope (creator-direct tasks) cannot be+ expressed as an ``owner + dimensions`` predicate at all.+ """+ owner_user_id = self._get_current_user_id()+ scope = self._get_memory_scope()+ dims = dict(scope.memory_dimensions) if scope is not None else {}+ if owner_user_id is None or not dims:+ return None+ return owner_user_id, dims++ def can_delete_current_scope(self) -> bool:+ """Whether the current owner + scope bound a ``delete_current_scope``."""+ return self._current_scope_predicate() is not None++ def delete_current_scope(self) -> int:+ """Delete every note in the current owner + execution scope.++ The deleted set is exactly the notes the by-id ``delete`` could reach+ from this scope, never wider: the current owner and *every* current+ memory dimension are all required (``_scope_permits`` semantics). It+ refuses with ``MemoryScopeTooBroadError`` -- and never falls back to+ ``clear()`` -- when ``_current_scope_predicate`` finds no bound.++ Returns:+ int: Number of notes deleted.+ """+ predicate = self._current_scope_predicate()+ if predicate is None:+ raise MemoryScopeTooBroadError(+ "the current memory scope has no owner or no scope dimensions"+ )+ return self._base_store.delete_for_scope(*predicate)+def list_scope_dimension_values(self, dim_key: str) -> set[str]:"""Distinct stamped values for one scope dimension, store-wide.diff --git a/tests/core/agent/test_context.py b/tests/core/agent/test_context.pyindex fa8be74da..47c9d2293 100644--- a/tests/core/agent/test_context.py+++ b/tests/core/agent/test_context.py@@ -2221,7 +2221,11 @@ def test_custom_component_roundtrip_without_context_schema_change() -> None:def test_system_context_renders_memory_persistence_guidance() -> None:- from xagent.core.agent.context.memory_tool import MEMORY_TOOLS_METADATA_KEY+ from xagent.core.agent.context.memory_tool import (+ FORGET_ALL_FALLBACK,+ FORGET_ALL_MEMORIES_METADATA_KEY,+ MEMORY_TOOLS_METADATA_KEY,+ )context = ExecutionContext(system_prompt="Base prompt.")context.add_user_message("hello")@@ -2235,12 +2239,18 @@ def test_system_context_renders_memory_persistence_guidance() -> None:assert "store_memory" in with_flagassert "update_memory" in with_flagassert "current trusted user explicitly asks" in with_flag+ assert "forget_all_my_memories" not in with_flagassert "Long-term memory is separate from visible chat history" in with_flagassert ("Do not store the deletion request or the deleted fact again during the ""current turn") in with_flag+ context.metadata[FORGET_ALL_MEMORIES_METADATA_KEY] = True+ with_forget_all = context.get_messages_for_llm()[0]["content"]+ assert "call forget_all_my_memories once" in with_forget_all+ assert f"unavailable, {FORGET_ALL_FALLBACK}." in with_forget_all+def _clock_context(timezone_name: str | None) -> ExecutionContext:"""Context frozen at the ShiftCare incident instant: 2026-08-24 22:03:37 UTCdiff --git a/tests/core/agent/test_dag.py b/tests/core/agent/test_dag.pyindex 071265fc7..a7211ca52 100644--- a/tests/core/agent/test_dag.py+++ b/tests/core/agent/test_dag.py@@ -293,6 +293,9 @@ class MemoryNote:class FakeMemoryStore:+ def can_delete_current_scope(self) -> bool:+ return False+def __init__(self) -> None:self.searches: list[dict[str, Any]] = []self.added: list[Any] = []@@ -3015,6 +3018,9 @@ async def test_dag_compaction_resume_keeps_request_text_out_of_memory_metadata()None):class CrudMemoryStore:+ def can_delete_current_scope(self) -> bool:+ return False+def __init__(self) -> None:self.notes = {"existing": StoredMemoryNote(diff --git a/tests/core/agent/test_memory_tool.py b/tests/core/agent/test_memory_tool.pyindex 505ce76f4..7db1eacf6 100644--- a/tests/core/agent/test_memory_tool.py+++ b/tests/core/agent/test_memory_tool.py@@ -7,7 +7,9 @@ from typing import Any, castimport pytestfrom xagent.core.agent.context.memory_tool import (+ FORGET_ALL_FALLBACK,DeleteMemoryTool,+ ForgetAllMyMemoriesTool,SearchMemoryTool,StoreMemoryTool,UpdateMemoryTool,@@ -36,6 +38,9 @@ class RecordingMemoryStore:self.searches: list[dict[str, Any]] = []self.added: list[MemoryNote] = []+ def can_delete_current_scope(self) -> bool:+ return False+def search(self, **kwargs: Any) -> list[Any]:self.searches.append(kwargs)return list(self.search_results)@@ -671,6 +676,103 @@ async def test_delete_memory_persists_after_lancedb_reopen(tmp_path: Any) -> Nonassert reopened_store.get(retained_note.id).success+_MEMBER_A = ExecutionScope(+ memory_dimensions={"client_application_id": "10", "end_user_id": "7"}+)+_MEMBER_B = ExecutionScope(+ memory_dimensions={"client_application_id": "10", "end_user_id": "8"}+)+++def _seed_two_members(store: UserIsolatedMemoryStore) -> dict[str, str]:+ """Two members of one workspace under one machine owner, a second owner+ with member A's exact dimensions, and the owner's dimension-less note."""+ notes: dict[str, str] = {}+ for name, user_id, scope in (+ ("a1", 1, _MEMBER_A),+ ("a2", 1, _MEMBER_A),+ ("b", 1, _MEMBER_B),+ ("other_owner", 2, _MEMBER_A),+ ("owner_plain", 1, ExecutionScope(strict_memory_isolation=True)),+ ):+ with UserContext(user_id), ExecutionScopeContext(scope):+ note = MemoryNote(content=f"note {name}")+ assert store.add(note).success+ notes[name] = note.id+ return notes+++async def test_forget_all_my_memories_deletes_only_the_requesters_notes() -> None:+ base_store = InMemoryMemoryStore()+ store = UserIsolatedMemoryStore(base_store)+ notes = _seed_two_members(store)++ with UserContext(1), ExecutionScopeContext(_MEMBER_A):+ result = await ForgetAllMyMemoriesTool(memory_store=store).execute()++ assert result == {"success": True, "deleted_count": 2}+ remaining = {name for name, id_ in notes.items() if base_store.get(id_).success}+ assert remaining == {"b", "other_owner", "owner_plain"}+++ "scope",+ [ExecutionScope(strict_memory_isolation=True), None],+ ids=["creator_direct", "unscoped"],+)+async def test_forget_all_my_memories_refuses_without_scope_dimensions(+ scope: ExecutionScope | None,+) -> None:+ base_store = InMemoryMemoryStore()+ store = UserIsolatedMemoryStore(base_store)+ notes = _seed_two_members(store)++ with UserContext(1), ExecutionScopeContext(scope):+ result = await ForgetAllMyMemoriesTool(memory_store=store).execute()++ assert result["success"] is False+ assert result["deleted_count"] == 0+ assert "unavailable" in result["error"]+ assert FORGET_ALL_FALLBACK in result["error"]+ assert all(base_store.get(id_).success for id_ in notes.values())+++async def test_forget_all_my_memories_reports_storage_failure() -> None:+ class FailingStore:+ def delete_current_scope(self) -> int:+ raise RuntimeError("disk full")++ result = await ForgetAllMyMemoriesTool(memory_store=FailingStore()).execute()++ assert result == {+ "success": False,+ "deleted_count": None,+ "error": "Failed to delete memories.",+ }+++async def test_forget_all_my_memories_on_lancedb(tmp_path: Any) -> None:+ base_store = LanceDBMemoryStore(+ db_dir=str(tmp_path),+ collection_name="forget_all",+ embedding_model=_DeterministicEmbedding(),+ )+ store = UserIsolatedMemoryStore(base_store)+ notes = _seed_two_members(store)++ with UserContext(1), ExecutionScopeContext(_MEMBER_A):+ result = await ForgetAllMyMemoriesTool(memory_store=store).execute()+ assert store.list_all() == []++ assert result == {"success": True, "deleted_count": 2}+ remaining = {name for name, id_ in notes.items() if base_store.get(id_).success}+ assert remaining == {"b", "other_owner", "owner_plain"}++def test_build_memory_tools_composition() -> None:assert build_memory_tools(memory_store=None, task="task") == []tools = build_memory_tools(memory_store=CrudMemoryStore(), task="task")@@ -680,7 +782,40 @@ def test_build_memory_tools_composition() -> None:"update_memory","delete_memory",]- assert "current user explicitly asks" in tools[-1].description+ assert "current user explicitly asks" in tools[3].description+++ ("user_id", "scope", "expected"),+ [+ (1, _MEMBER_A, True),+ # Creator-direct web task: strict and dimension-less.+ (1, ExecutionScope(strict_memory_isolation=True), False),+ (1, None, False),+ (None, _MEMBER_A, False),+ ],+ ids=["external_member", "creator_direct", "unscoped", "ownerless"],+)+def test_build_memory_tools_offers_forget_all_only_for_bounded_scopes(+ user_id: int | None, scope: ExecutionScope | None, expected: bool+) -> None:+ class Ctx:+ def __init__(self) -> None:+ self.metadata: dict[str, Any] = {}++ context = Ctx()+ store = UserIsolatedMemoryStore(InMemoryMemoryStore())++ with UserContext(user_id), ExecutionScopeContext(scope):+ tools = build_memory_tools(memory_store=store, task="task", context=context)++ names = [tool.name for tool in tools]+ assert ("forget_all_my_memories" in names) is expected+ assert context.metadata["forget_all_memories_tool_enabled"] is expected+ if expected:+ assert names[-1] == "forget_all_my_memories"+ assert "explicitly asks you to forget everything" in tools[-1].description+ assert FORGET_ALL_FALLBACK in tools[-1].description@pytest.mark.asyncio@@ -756,6 +891,7 @@ async def test_react_uses_selected_custom_memory_tool_factory() -> None:assert tools[1].name == "load_skill"assert factory_tools == [custom_tool]assert context.metadata["memory_tools_enabled"] is True+ assert context.metadata["forget_all_memories_tool_enabled"] is Falseassert calls == [{"memory_store": store,diff --git a/tests/core/agent/test_react.py b/tests/core/agent/test_react.pyindex 319356f50..95798f5a9 100644--- a/tests/core/agent/test_react.py+++ b/tests/core/agent/test_react.py@@ -1306,6 +1306,9 @@ class MemoryNote:class FakeMemoryStore:+ def can_delete_current_scope(self) -> bool:+ return False+def __init__(self) -> None:self.searches: list[dict[str, Any]] = []@@ -9161,6 +9164,9 @@ async def test_react_pattern_runtime_injected_from_runner_style_traces_events()class MemoryToolStore:"""Fake memory store with both search (retrieval/dedup) and add (store)."""+ def can_delete_current_scope(self) -> bool:+ return False+def __init__(self) -> None:self.searches: list[dict[str, Any]] = []self.added: list[Any] = []diff --git a/tests/core/memory/test_delete_for_scope.py b/tests/core/memory/test_delete_for_scope.pynew file mode 100644index 000000000..c40e6bf15--- /dev/null+++ b/tests/core/memory/test_delete_for_scope.py@@ -0,0 +1,460 @@+"""Tests for scoped bulk erasure: ``delete_for_scope``, ``delete_current_scope``+and ``LanceDBMemoryStore.compact``.++``delete_for_scope`` is the control-plane primitive behind "forget everything+in this workspace" (owner + client application) and per-member deletes (owner++ end user + ...); ``delete_current_scope`` is its agent-facing form, bounded+to exactly what the by-id ``delete`` could reach from the active scope.+``compact`` makes those deletes physical.+"""++from __future__ import annotations++import os+from datetime import timedelta+from pathlib import Path+from typing import Any++import pytest++from xagent.core.execution_scope import (+ MEMORY_DIMENSION_METADATA_PREFIX,+ ExecutionScope,+ ExecutionScopeContext,+ InvalidScopeComponentError,+)+from xagent.core.memory.access_scope import MemoryAccessScope+from xagent.core.memory.base import MemoryScopeTooBroadError, MemoryStore+from xagent.core.memory.core import MemoryNote+from xagent.core.memory.in_memory import InMemoryMemoryStore+from xagent.core.memory.lancedb import LanceDBMemoryStore+from xagent.web.user_isolated_memory import UserContext, UserIsolatedMemoryStore++from .conftest import ConstantEmbedding++P = MEMORY_DIMENSION_METADATA_PREFIX+# One Slack workspace (client application 10) under machine owner 1; member 7+# and member 8 talk to agent 3; member 7 also talks to agent 4.+MEMBER_7 = {"client_application_id": "10", "end_user_id": "7", "agent_id": "3"}+++def _note(content: str, owner: int, **dims: str) -> MemoryNote:+ return MemoryNote(+ content=content,+ metadata={+ "user_id": owner,+ **{f"{P}{key}": value for key, value in dims.items()},+ },+ )+++def _seed(store: MemoryStore) -> dict[str, str]:+ notes = {+ "member7": _note("member 7 likes tea", 1, **MEMBER_7),+ "member7_agent4": _note(+ "member 7 via agent 4",+ 1,+ client_application_id="10",+ end_user_id="7",+ agent_id="4",+ ),+ "member8": _note(+ "member 8 likes coffee",+ 1,+ client_application_id="10",+ end_user_id="8",+ agent_id="3",+ ),+ "other_app": _note(+ "same member id, other app",+ 1,+ client_application_id="11",+ end_user_id="7",+ agent_id="3",+ ),+ "app_prefix": _note(+ "app id prefix", 1, client_application_id="1", end_user_id="7", agent_id="3"+ ),+ "other_owner": _note("same dims, other owner", 2, **MEMBER_7),+ "owner_plain": _note("creator-direct note", 1),+ }+ ids = {}+ for name, note in notes.items():+ response = store.add(note)+ assert response.success+ ids[name] = response.memory_id+ return ids+++def _remaining(store: MemoryStore, ids: dict[str, str]) -> set[str]:+ return {name for name, note_id in ids.items() if store.get(note_id).success}+++def _lancedb_store(root: Path) -> LanceDBMemoryStore:+ return LanceDBMemoryStore(+ db_dir=str(root),+ collection_name="memories",+ embedding_model=ConstantEmbedding(8),+ )++[email protected](params=["lancedb", "in_memory"])+def store(request: Any, tmp_path: Path) -> MemoryStore:+ if request.param == "in_memory":+ return InMemoryMemoryStore()+ return _lancedb_store(tmp_path)+++def test_member_delete_touches_only_that_member(store: MemoryStore) -> None:+ ids = _seed(store)++ assert store.delete_for_scope(1, MEMBER_7) == 1++ assert _remaining(store, ids) == set(ids) - {"member7"}+ # Idempotent.+ assert store.delete_for_scope(1, MEMBER_7) == 0+++def test_workspace_delete_spares_other_owners_apps_and_plain_notes(+ store: MemoryStore,+) -> None:+ ids = _seed(store)++ assert store.delete_for_scope(1, {"client_application_id": "10"}) == 3++ assert _remaining(store, ids) == {+ "other_app",+ "app_prefix",+ "other_owner",+ "owner_plain",+ }++[email protected]("dims", [{}, None])+def test_empty_dims_are_rejected(store: MemoryStore, dims: Any) -> None:+ ids = _seed(store)++ with pytest.raises(MemoryScopeTooBroadError):+ store.delete_for_scope(1, dims)++ assert _remaining(store, ids) == set(ids)++[email protected]("owner", [0, -1, True, "1", 1.0, 2**63])+def test_invalid_owner_is_rejected(store: MemoryStore, owner: Any) -> None:+ with pytest.raises(ValueError):+ store.delete_for_scope(owner, MEMBER_7)+++ "dims",+ [{"client_application_id": 10}, {"client_application_id": ""}, {"a b": "1"}],+)+def test_invalid_dimension_is_rejected(store: MemoryStore, dims: Any) -> None:+ with pytest.raises(InvalidScopeComponentError):+ store.delete_for_scope(1, dims)+++def test_raw_store_refuses_current_scope_delete(store: MemoryStore) -> None:+ assert not store.can_delete_current_scope()+ with pytest.raises(MemoryScopeTooBroadError):+ store.delete_current_scope()+++def test_lancedb_backend_failure_raises(tmp_path: Path, monkeypatch: Any) -> None:+ """An erasure request must never be reported as done when it was not."""+ store = _lancedb_store(tmp_path)++ class ExplodingTable:+ def count_rows(self, *_args: Any) -> int:+ return 1++ def delete(self, *_args: Any) -> None:+ raise RuntimeError("simulated backend failure")++ class StubConnection:+ def open_table(self, _name: str) -> ExplodingTable:+ return ExplodingTable()++ monkeypatch.setattr(+ store._vector_store, "get_raw_connection", lambda: StubConnection()+ )+ with pytest.raises(RuntimeError, match="simulated backend failure"):+ store.delete_for_scope(1, MEMBER_7)+++# ---------------------------------------------------------------------------+# UserIsolatedMemoryStore.delete_current_scope+# ---------------------------------------------------------------------------+++def test_current_scope_delete_matches_the_active_scope_exactly(+ store: MemoryStore,+) -> None:+ ids = _seed(store)+ isolated = UserIsolatedMemoryStore(store)++ with (+ UserContext(1),+ ExecutionScopeContext(ExecutionScope(memory_dimensions=MEMBER_7)),+ ):+ assert isolated.can_delete_current_scope()+ assert isolated.delete_current_scope() == 1++ assert _remaining(store, ids) == set(ids) - {"member7"}+++def test_current_scope_delete_with_fixed_access_scope(store: MemoryStore) -> None:+ ids = _seed(store)+ bound = UserIsolatedMemoryStore(store).with_memory_access_scope(+ MemoryAccessScope(owner_user_id=1, memory_dimensions=MEMBER_7)+ )++ # Ambient identity cannot widen or redirect a fixed binding.+ with (+ UserContext(2),+ ExecutionScopeContext(+ ExecutionScope(memory_dimensions={"client_application_id": "10"})+ ),+ ):+ assert bound.can_delete_current_scope()+ assert bound.delete_current_scope() == 1++ assert _remaining(store, ids) == set(ids) - {"member7"}+++ ("user_id", "scope"),+ [+ # Creator-direct website task: strict, dimension-less.+ (1, ExecutionScope(strict_memory_isolation=True)),+ # Unscoped execution: by-id access spans the whole owner.+ (1, None),+ # Scoped but ownerless: by-id access is not owner-gated.+ (None, ExecutionScope(memory_dimensions=MEMBER_7)),+ ],+)+def test_current_scope_delete_refuses_unbounded_scopes(+ store: MemoryStore, user_id: int | None, scope: ExecutionScope | None+) -> None:+ ids = _seed(store)+ isolated = UserIsolatedMemoryStore(store)++ with UserContext(user_id), ExecutionScopeContext(scope):+ assert not isolated.can_delete_current_scope()+ with pytest.raises(MemoryScopeTooBroadError):+ isolated.delete_current_scope()++ assert _remaining(store, ids) == set(ids)+++def test_strict_owner_only_access_scope_refuses(store: MemoryStore) -> None:+ ids = _seed(store)+ bound = UserIsolatedMemoryStore(store).with_memory_access_scope(+ MemoryAccessScope(owner_user_id=1, memory_dimensions={}, strict_owner_only=True)+ )++ assert not bound.can_delete_current_scope()+ with pytest.raises(MemoryScopeTooBroadError):+ bound.delete_current_scope()++ assert _remaining(store, ids) == set(ids)+++def test_isolated_delete_for_scope_is_not_narrowed_by_ambient_scope(+ store: MemoryStore,+) -> None:+ """The control-plane form is explicit: ambient identity neither widens nor+ narrows it."""+ ids = _seed(store)+ isolated = UserIsolatedMemoryStore(store)++ with (+ UserContext(2),+ ExecutionScopeContext(ExecutionScope(memory_dimensions=MEMBER_7)),+ ):+ assert isolated.delete_for_scope(1, {"client_application_id": "10"}) == 3++ assert "other_owner" in _remaining(store, ids)+++# ---------------------------------------------------------------------------+# LanceDBMemoryStore.compact+# ---------------------------------------------------------------------------+++def _files_containing(root: Path, needle: bytes) -> list[str]:+ hits = []+ for directory, _subdirs, files in os.walk(root):+ for name in files:+ path = os.path.join(directory, name)+ with open(path, "rb") as handle:+ if needle in handle.read():+ hits.append(path)+ return hits+++def test_compact_physically_removes_deleted_rows(tmp_path: Path) -> None:+ store = _lancedb_store(tmp_path)+ secret = "member-seven-secret-preference"+ for index in range(40):+ assert store.add(+ _note(f"member 8 note {index}", 1, **{**MEMBER_7, "end_user_id": "8"})+ ).success+ assert store.add(_note(secret, 1, **MEMBER_7)).success+ # Merge everything into one large fragment first: that is the layout in+ # which a plain optimize() never materializes a 1-in-41 delete.+ store._compact(timedelta(0))+ assert _files_containing(tmp_path, secret.encode())++ assert store.delete_for_scope(1, MEMBER_7) == 1+ # Soft-deleted: the text is still on disk until compaction.+ assert _files_containing(tmp_path, secret.encode())++ store._compact(timedelta(0))++ assert _files_containing(tmp_path, secret.encode()) == []+ table = store._vector_store.get_raw_connection().open_table("memories")+ assert len(table.list_versions()) == 1+ # Survivors keep their rows, vectors and scope columns.+ isolated = UserIsolatedMemoryStore(store)+ member_8 = ExecutionScope(memory_dimensions={**MEMBER_7, "end_user_id": "8"})+ with UserContext(1), ExecutionScopeContext(member_8):+ assert len(isolated.search("member 8 note", k=100)) == 40+++def _table_version(store: LanceDBMemoryStore) -> int:+ table = store._vector_store.get_raw_connection().open_table("memories")+ return int(table.version)+++def test_compact_keeps_recent_versions(tmp_path: Path) -> None:+ """The public entry point never prunes young versions, so a concurrent+ writer cannot lose the version it started from."""+ store = _lancedb_store(tmp_path)+ assert store.add(_note("kept", 1, **MEMBER_7)).success+ assert store.add(_note("deleted", 1, **{**MEMBER_7, "end_user_id": "8"})).success+ # Merge both rows into one fragment, so the delete leaves a deletion file+ # (a fully deleted fragment is simply dropped) and compact() rewrites.+ store._compact(timedelta(0))+ assert store.delete_for_scope(1, {**MEMBER_7, "end_user_id": "8"}) == 1+ table = store._vector_store.get_raw_connection().open_table("memories")+ versions_before = {entry["version"] for entry in table.list_versions()}++ store.compact()++ versions_after = {entry["version"] for entry in table.list_versions()}+ assert versions_before < versions_after+++def test_compact_skips_rewrite_without_deletions(tmp_path: Path) -> None:+ store = _lancedb_store(tmp_path)+ for index in range(3):+ assert store.add(_note(f"note {index}", 1, **MEMBER_7)).success+ assert store.add(+ _note("other member", 1, **{**MEMBER_7, "end_user_id": "8"})+ ).success+ # First pass merges the single-row fragments into one.+ store._compact(timedelta(0))+ settled = _table_version(store)++ # Nothing deleted since: no rewrite, and nothing for optimize() to merge.+ store._compact(timedelta(0))+ assert _table_version(store) == settled++ assert store.delete_for_scope(1, MEMBER_7) == 3+ deleted = _table_version(store)+ store._compact(timedelta(0))+ assert _table_version(store) > deleted+++class _CompactBeforeDelete:+ """A table handle that lets a concurrent ``compact()`` win the race.++ Forwards everything to the real handle, but runs a zero-retention+ compaction right before the first delete: the table is rewritten and the+ version this handle opened is pruned, exactly the interleaving of a+ delete on an idle table (latest version already past the retention)+ racing the host's periodic ``compact()``.+ """++ def __init__(self, table: Any, store: LanceDBMemoryStore) -> None:+ self._table = table+ self._store = store++ def __getattr__(self, name: str) -> Any:+ return getattr(self._table, name)++ def delete(self, where: str) -> Any:+ self._store._compact(timedelta(0))+ return self._table.delete(where)+++def _race_next_open(store: LanceDBMemoryStore, monkeypatch: Any) -> None:+ """Hand the next ``open_table`` caller a handle that loses the race."""+ connection = store._vector_store.get_raw_connection()+ real_open = connection.open_table++ def open_once(name: str) -> Any:+ monkeypatch.setattr(connection, "open_table", real_open)+ return _CompactBeforeDelete(real_open(name), store)++ monkeypatch.setattr(connection, "open_table", open_once)+++def _seed_pending_deletion(store: LanceDBMemoryStore) -> dict[str, str]:+ """Seed notes in one fragment plus a soft-deleted row, so the next+ compaction rewrites the table and prunes every older version."""+ ids = _seed(store)+ store._compact(timedelta(0))+ assert store.delete_for_scope(2, MEMBER_7) == 1+ return ids+++def test_scoped_delete_survives_racing_compaction(+ tmp_path: Path, monkeypatch: Any+) -> None:+ store = _lancedb_store(tmp_path)+ ids = _seed_pending_deletion(store)+ _race_next_open(store, monkeypatch)++ assert store.delete_for_scope(1, MEMBER_7) == 1++ assert _remaining(store, ids) == set(ids) - {"member7", "other_owner"}+++def test_by_id_delete_survives_racing_compaction(+ tmp_path: Path, monkeypatch: Any+) -> None:+ store = _lancedb_store(tmp_path)+ ids = _seed_pending_deletion(store)+ _race_next_open(store, monkeypatch)++ assert store.delete(ids["member8"]).success++ assert _remaining(store, ids) == set(ids) - {"member8", "other_owner"}+++def test_delete_does_not_retry_unrelated_errors(+ tmp_path: Path, monkeypatch: Any+) -> None:+ store = _lancedb_store(tmp_path)+ opened: list[str] = []+ connection = store._vector_store.get_raw_connection()++ class FailingTable:+ def count_rows(self, *_args: Any) -> int:+ return 1++ def delete(self, *_args: Any) -> None:+ raise RuntimeError("lance error: commit conflict")++ def open_table(name: str) -> FailingTable:+ opened.append(name)+ return FailingTable()++ monkeypatch.setattr(connection, "open_table", open_table)++ with pytest.raises(RuntimeError, match="commit conflict"):+ store.delete_for_scope(1, MEMBER_7)+ assert opened == ["memories"]diff --git a/tests/web/test_dynamic_memory_store.py b/tests/web/test_dynamic_memory_store.pyindex bd5e79a86..5595f7b5f 100644--- a/tests/web/test_dynamic_memory_store.py+++ b/tests/web/test_dynamic_memory_store.py@@ -191,6 +191,14 @@ class RecordingStore:self.calls.append("list_scope_dimension_values")return set()+ def delete_for_scope(self, owner_user_id, dims):+ self.calls.append("delete_for_scope")+ return 0++ def delete_current_scope(self):+ self.calls.append("delete_current_scope")+ return 0+def _manager_with_recording_store(monkeypatch, holder: dict):"""A manager publishing a RecordingStore, so reach-through is observable."""@@ -273,6 +281,8 @@ def test_drift_noticed_by_a_new_acquire_revokes_the_old_reference(monkeypatch) -lambda store: store.get_stats(),lambda store: store.delete_by_scope_dimension("tenant", "acme"),lambda store: store.list_scope_dimension_values("tenant"),+ lambda store: store.delete_for_scope(1, {"tenant": "acme"}),+ lambda store: store.delete_current_scope(),],)def test_every_memory_operation_fails_closed_after_revocation(