-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix/neo4j cleanup on delete 3245 #3557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Implement delete() method for Neo4j, Memgraph, Kuzu, and Neptune - Extract entities from deleted memory and decrement mentions counter - Remove orphaned nodes when mention count reaches zero - Add comprehensive tests for graph cleanup with/without graph enabled - Add type hints (tuple[str, dict]) to Neptune _delete_entities_cypher - Ensure memory deletion is security-scoped (user_id/agent_id/run_id) Resolves mem0ai#3245
32be873 to
befd3db
Compare
|
apologies for the mess! 😓, I squashed the commits into a single clean commit for easier review. |
|
Hey @lsvishaal no worries, will get this PR reviewed in some time. Thanks for implementing it |
|
Hey @parshvadaftari , any updates? 😁 |
parshvadaftari
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rest looks good to me. Please incorporate the requested changes
tests/test_memory.py
Outdated
| @patch('mem0.utils.factory.VectorStoreFactory.create') | ||
| @patch('mem0.utils.factory.LlmFactory.create') | ||
| @patch('mem0.memory.storage.SQLiteManager') | ||
| def test_delete_memory_calls_graph_cleanup(mock_sqlite, mock_llm_factory, mock_vector_factory, mock_embedder_factory, mock_graph_factory): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests which cover entire end to end graph deletion? and move the graph related tests to separate file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done,
Added end to end tests in separate file as requested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@parshvadaftari
Hi, any updates?
Description
This PR implements automatic graph cleanup when memories are deleted, addressing Issue #3245. Previously, when a memory was deleted from the vector store, the associated entities and relationships in the graph database remained as orphaned nodes, leading to data inconsistency and potential memory leaks.
The solution adds a
delete()method to all graph store implementations (Neo4j, Memgraph, Kuzu, and Neptune) that properly cleans up entity relationships by:mentionscounter for each entityThe implementation includes error handling to ensure memory deletion continues even if graph cleanup fails, maintaining system reliability.
Fixes #3245
Type of change
How Has This Been Tested?
Test Coverage:
test_delete_memory_calls_graph_cleanup- Verifies that graph.delete() is called with correct parameters when graph is enabledtest_delete_memory_continues_without_graph- Ensures deletion works when graph is not enabledTest Configuration:
Checklist:
Maintainer Checklist