[BUG] LanceDB delete(scope_prefix=X) also deletes every record in root scope '/'; text-prefix filter leaks sibling scopes
Summary
Two related scope-filtering defects in lib/crewai/src/crewai/memory/storage/lancedb_storage.py cause cross-tenant data loss on the shared default table that all crews and standalone Memory instances use:
A. Root-scope overreach in delete() (lines 469-474). The scope-only branch builds conditions.append(f"{_scope_prefix_filter(prefix)} OR scope = '/'") - the OR scope = '/' term is unconditional, so ANY scoped delete also destroys every record stored at scope /. This contradicts Memory.forget's documented contract (unified_memory.py:839-840: "If None and root_scope is set, deletes only within root_scope") and is inconsistent with delete()'s own categories/metadata branch (448-468, prefix only) and with reset() (645-650, path-aware range predicate).
Repro: save a1@'/tenant-a', a2@'/tenant-a/notes', g1@'/', g2@'/'; then Memory(storage=storage, root_scope='/tenant-a').forget() returns 4 and empties the store - the two root-scope records are destroyed.
B. Text-prefix scope leak in _scope_prefix_filter() (lines 55-63). The filter builds scope LIKE '<escaped prefix>%' - a TEXT prefix, not a path-component prefix. Scope /crew/research-2 starts with the text /crew/research, so records from a sibling scope leak into search()/list_records()/get_scope_info()/delete() for the parent prefix. This is realistic out of the box: crew.py:667-668 sets crew_root_scope = f"/crew/{sanitize_scope_name(name)}", so crews named "Research" and "Research 2" get /crew/research and /crew/research-2 on the same shared table - crew 1's recall returns crew 2's memories and crew 1's forget() deletes them. The repo's own reset() and get_scope_info() child computation are path-aware, and QdrantEdgeStorage matches ancestors exactly; only the LanceDB prefix filter is loose.
Repro: save crew1-mem1@'/crew/research', crew1-mem2@'/crew/research/sub', crew2-mem1@'/crew/research-2'; then search([0.1]*4, scope_prefix='/crew/research') returns all 3 ids, and delete(scope_prefix='/crew/research') removes the sibling crew's record.
Possible fix
- Drop the
OR scope = '/'term in the scope branch ofdelete(). - Make
_scope_prefix_filterpath-component-aware, e.g.(scope = '<prefix>' OR scope LIKE '<escaped prefix>/%' ESCAPE '\'), keeping the existing escaping for\,%,_.
Disclosure
This issue and the upcoming PR are AI-agent-assisted (generated with an AI coding agent). Please apply the llm-generated label per CONTRIBUTING - I cannot set labels without triage permission.
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.