crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] LanceDBStorage.delete(scope_prefix=...) also deletes all root-scope ('/') memories — scoped forget silently destroys unrelated records

Open Wilson0511 opened this issue 1 month ago • 2 comments

Description

MemoryRecord's default scope is "/" (memory/types.py), so root-level memories are the norm for unscoped Memory.remember(). But the scope-prefix branch of LanceDBStorage.delete() (memory/storage/lancedb_storage.py:473) builds:

conditions.append(f"{_scope_prefix_filter(prefix)} OR scope = '/'")

so any scoped delete also removes every root-scope record, regardless of the prefix. Memory.forget(scope='/notes') (and Memory(root_scope=...).forget(), and MemoryScope.forget()) therefore destroys all root-level memories while reporting a count the caller can't distinguish from the subtree alone — silent data loss.

Every sibling operation disagrees with the clause:

Operation Root records under scoped call
delete(scope_prefix=...) (lancedb) deleted
search(scope_prefix=...) (lancedb, same file) spared — prefix filter only
list_records() / get_scope_info() spared — prefix filter only
reset(scope_prefix=...) (lancedb, :631-636) spared — scope >= X AND scope < X/\uffff range
Qdrant backend scoped delete spared — scope_ancestors match can never equal the prefix for a / record

forget()'s docstring also says "If None and root_scope is set, deletes only within root_scope" — the OR clause violates that contract. The clause came in with the original bulk of the file and has no rationale anywhere; the escaping rework in #7820 preserved it verbatim.

Reproduction

from pathlib import Path
from crewai.memory.storage.lancedb_storage import LanceDBStorage
from crewai.memory.types import MemoryRecord

storage = LanceDBStorage(path=Path("/tmp/m"), vector_dim=2, compact_every=0)
root = MemoryRecord(content="root note", embedding=[1.0, 0.0])      # scope="/"
scoped = MemoryRecord(content="scoped note", scope="/notes", embedding=[1.0, 0.0])
other = MemoryRecord(content="other note", scope="/other", embedding=[1.0, 0.0])
storage.save([root, scoped, other])

deleted = storage.delete(scope_prefix="/notes")
assert deleted == 1, deleted                       # actual: 2
assert storage.get_record(root.id) is not None     # actual: None — the root record is gone

Suggested fix

Drop the clause so the condition is just the prefix filter — one line. Scoped delete then behaves like search/reset/Qdrant. The no-scope full-wipe path (delete() with no arguments) is a separate branch and keeps wiping everything.

I can open a PR with the regression tests above (scoped delete spares root; bare delete still clears all).

Wilson0511 avatar Oct 22 '26 11:10 Wilson0511

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 21 '26 12:11 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Nov 26 '26 12:11 github-actions[bot]