Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 26 additions & 41 deletions src/memos/graph_dbs/neo4j_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any

from memos.configs.graph_db import Neo4jGraphDBConfig
from memos.exceptions import VectorDBError
from memos.graph_dbs.neo4j import (
Neo4jGraphDB,
_flatten_info_fields,
Expand Down Expand Up @@ -957,10 +958,11 @@ def delete_node_by_prams(
int: Number of nodes deleted.
"""
logger.info(
f"[delete_node_by_prams] memory_ids: {memory_ids}, file_ids: {file_ids}, filter: {filter}, writable_cube_ids: {writable_cube_ids}"
)
print(
f"[delete_node_by_prams] memory_ids: {memory_ids}, file_ids: {file_ids}, filter: {filter}, writable_cube_ids: {writable_cube_ids}"
"[delete_node_by_prams] memory_count=%d, file_count=%d, cube_count=%d, has_filter=%s",
len(memory_ids or []),
len(file_ids or []),
len(writable_cube_ids or []),
bool(filter),
)

# file_ids deletion must be scoped by writable_cube_ids.
Expand Down Expand Up @@ -1031,44 +1033,27 @@ def delete_node_by_prams(
else:
ids_where = data_conditions

logger.info(
f"[delete_node_by_prams] Deleting nodes - memory_ids: {memory_ids}, file_ids: {file_ids}, filter: {filter}"
# Return IDs from the deletion itself so concurrent matches cannot drift
# between a separate ID lookup and the graph mutation.
delete_query = (
f"MATCH (n:Memory) WHERE {ids_where} WITH n, n.id AS id DETACH DELETE n RETURN id"
)
print(
f"[delete_node_by_prams] Deleting nodes - memory_ids: {memory_ids}, file_ids: {file_ids}, filter: {filter}"
)

# First count matching nodes to get accurate count
count_query = f"MATCH (n:Memory) WHERE {ids_where} RETURN count(n) AS node_count"
logger.info(f"[delete_node_by_prams] count_query: {count_query}")
print(f"[delete_node_by_prams] count_query: {count_query}")

# Then delete nodes
delete_query = f"MATCH (n:Memory) WHERE {ids_where} DETACH DELETE n"
logger.info(f"[delete_node_by_prams] delete_query: {delete_query}")
print(f"[delete_node_by_prams] delete_query: {delete_query}")
print(f"[delete_node_by_prams] params: {params}")

deleted_count = 0
try:
with self.driver.session(database=self.db_name) as session:
# Count nodes before deletion
count_result = session.run(count_query, **params)
count_record = count_result.single()
expected_count = 0
if count_record:
expected_count = count_record["node_count"] or 0

# Delete nodes
session.run(delete_query, **params)
# Use the count from before deletion as the actual deleted count
deleted_count = expected_count

except Exception as e:
logger.error(f"[delete_node_by_prams] Failed to delete nodes: {e}", exc_info=True)
raise

logger.info(f"[delete_node_by_prams] Successfully deleted {deleted_count} nodes")
with (
self.driver.session(database=self.db_name) as session,
session.begin_transaction() as transaction,
):
records = list(transaction.run(delete_query, **params))
deleted_ids = [record["id"] for record in records if record["id"] is not None]
if deleted_ids:
try:
self.vec_db.delete(deleted_ids)
except Exception as exc:
# Leave graph nodes available for a retry if vector cleanup fails.
raise VectorDBError("Failed to delete memory vectors; retry deletion") from exc
transaction.commit()

deleted_count = len(records)
logger.info("[delete_node_by_prams] Successfully deleted %d nodes", deleted_count)
return deleted_count

def clear(self, user_name: str | None = None) -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/memos/memories/textual/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ def delete_by_memory_ids(self, memory_ids: list[str]) -> None:
try:
self.graph_store.delete_node_by_prams(memory_ids=memory_ids)
except Exception as e:
logger.error(f"An error occurred while deleting memories by memory_ids: {e}")
logger.error("Failed to delete memories by memory_ids: %s", e)
raise

def delete_all(self, user_name: str | None = None) -> None:
"""Delete all memories and their relationships from the graph store."""
Expand Down
19 changes: 19 additions & 0 deletions tests/api/test_memory_handler_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,22 @@ def test_delete_memories_with_pref_mem_disabled():
writable_cube_ids=None,
filter={"and": [{"user_id": "u_1"}]},
)


def test_vector_delete_failure_reaches_api_response():
from types import SimpleNamespace

from memos.exceptions import VectorDBError
from memos.memories.textual.tree import TreeTextMemory

memory = TreeTextMemory.__new__(TreeTextMemory)
memory.graph_store = Mock()
memory.graph_store.get_nodes.return_value = []
memory.graph_store.delete_node_by_prams.side_effect = VectorDBError("Synthetic vector outage")
cube = SimpleNamespace(text_mem=memory)
request = DeleteMemoryRequest(memory_ids=["synthetic-id"])

response = handle_delete_memories(request, cube)

assert response.data["status"] == "failure"
memory.graph_store.delete_node_by_prams.assert_called_once_with(memory_ids=["synthetic-id"])
170 changes: 170 additions & 0 deletions tests/graph_dbs/test_delete_vec_cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
"""
Regression tests for #2331: delete_node_by_prams must also purge vectors from vec_db.

When a memory is deleted via Neo4jCommunityGraphDB.delete_node_by_prams the
corresponding embedding vector must be removed from vec_db so that subsequent
searches no longer surface the deleted node.
"""

from unittest.mock import MagicMock

import pytest

from memos.configs.graph_db import Neo4jGraphDBConfig
from memos.exceptions import VectorDBError


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_db(config: Neo4jGraphDBConfig) -> "Neo4jCommunityGraphDB": # noqa: F821
"""Build a Neo4jCommunityGraphDB with all heavy dependencies mocked out.

Uses __new__ to skip __init__ entirely so no real Neo4j driver or Qdrant
connection is attempted.
"""
from memos.graph_dbs.neo4j_community import Neo4jCommunityGraphDB

db = Neo4jCommunityGraphDB.__new__(Neo4jCommunityGraphDB)
db.config = config
db.driver = MagicMock()
db.db_name = config.db_name
db.vec_db = MagicMock()
db._schema_ready = True
return db


@pytest.fixture
def community_config():
return Neo4jGraphDBConfig(
uri="bolt://localhost:7687",
user="neo4j",
password="test",
db_name="test_db",
auto_create=False,
use_multi_db=False,
user_name="alice",
embedding_dimension=3,
)


# ---------------------------------------------------------------------------
# Tests - vec_db cleanup is called for every delete mode
# ---------------------------------------------------------------------------


class TestDeleteNodeByPramsVecCleanup:
"""delete_node_by_prams must remove vectors from vec_db after graph deletion."""

def _make_session_mock(self, db: "Neo4jCommunityGraphDB", ids_to_delete: list[str]): # noqa: F821
"""Wire deletion records returned from an explicit graph transaction."""
session_ctx = MagicMock()
session_ctx.__enter__ = MagicMock(return_value=session_ctx)
session_ctx.__exit__ = MagicMock(return_value=False)

transaction = session_ctx.begin_transaction.return_value.__enter__.return_value
transaction.run.return_value = [{"id": nid} for nid in ids_to_delete]
db.driver.session.return_value = session_ctx
return session_ctx

def test_delete_by_memory_ids_cleans_vec_db(self, community_config):
"""Deleting by memory_ids must call vec_db.delete with those IDs."""
db = _make_db(community_config)
ids = ["aaa-111", "bbb-222"]
self._make_session_mock(db, ids)

db.delete_node_by_prams(memory_ids=ids)

db.vec_db.delete.assert_called_once_with(ids)

def test_delete_by_filter_cleans_vec_db(self, community_config):
"""Deleting by filter must call vec_db.delete with all matched IDs."""
db = _make_db(community_config)
matched_ids = ["ccc-333", "ddd-444"]

# get_by_metadata is called internally for filter path
db.get_by_metadata = MagicMock(return_value=matched_ids)
self._make_session_mock(db, matched_ids)

db.delete_node_by_prams(filter={"user_id": "alice"})

db.get_by_metadata.assert_called_once()
db.vec_db.delete.assert_called_once_with(matched_ids)

def test_delete_by_memory_ids_empty_list_no_vec_call(self, community_config):
"""Empty memory_ids must not call vec_db.delete (early-return path)."""
db = _make_db(community_config)

result = db.delete_node_by_prams(memory_ids=[])

db.vec_db.delete.assert_not_called()
assert result == 0

def test_delete_no_args_no_vec_call(self, community_config):
"""No delete args must return 0 and not touch vec_db."""
db = _make_db(community_config)

result = db.delete_node_by_prams()

db.vec_db.delete.assert_not_called()
assert result == 0

def test_vec_db_delete_failure_propagates_without_graph_commit(self, community_config):
db = _make_db(community_config)
session = self._make_session_mock(db, ["eee-555"])
transaction = session.begin_transaction.return_value.__enter__.return_value
db.vec_db.delete.side_effect = OSError("Synthetic vector outage")

with pytest.raises(VectorDBError):
db.delete_node_by_prams(memory_ids=["eee-555"])

transaction.commit.assert_not_called()

def test_deletion_uses_one_graph_statement_and_purges_before_commit(self, community_config):
db = _make_db(community_config)
session = self._make_session_mock(db, ["aaa-111"])
transaction = session.begin_transaction.return_value.__enter__.return_value
order = []
db.vec_db.delete.side_effect = lambda ids: order.append(("vectors", ids))
transaction.commit.side_effect = lambda: order.append(("commit", None))

result = db.delete_node_by_prams(writable_cube_ids=["alice"], memory_ids=["aaa-111"])

assert result == 1
session.run.assert_not_called()
transaction.run.assert_called_once()
query = transaction.run.call_args.args[0]
assert "DETACH DELETE n" in query
assert "RETURN id" in query
assert transaction.run.call_args.kwargs["cube_id_0"] == "alice"
assert order == [("vectors", ["aaa-111"]), ("commit", None)]

def test_graph_statement_failure_does_not_remove_vectors(self, community_config):
db = _make_db(community_config)
session = self._make_session_mock(db, ["aaa-111"])
transaction = session.begin_transaction.return_value.__enter__.return_value
transaction.run.side_effect = OSError("Synthetic graph outage")

with pytest.raises(OSError):
db.delete_node_by_prams(memory_ids=["aaa-111"])

db.vec_db.delete.assert_not_called()
transaction.commit.assert_not_called()

def test_no_matching_nodes_does_not_remove_vectors(self, community_config):
db = _make_db(community_config)
self._make_session_mock(db, [])

assert db.delete_node_by_prams(memory_ids=["absent"]) == 0
db.vec_db.delete.assert_not_called()

def test_file_deletion_requires_cube_scope(self, community_config):
db = _make_db(community_config)

with pytest.raises(ValueError, match="writable_cube_ids"):
db.delete_node_by_prams(file_ids=["file-1"])

db.driver.session.assert_not_called()
db.vec_db.delete.assert_not_called()
Loading
Loading