fix: resolve data loss atomicity bug in optimization (fixes #7051)#7312
Open
Charanvardhan wants to merge 2 commits intoagno-agi:mainfrom
Open
fix: resolve data loss atomicity bug in optimization (fixes #7051)#7312Charanvardhan wants to merge 2 commits intoagno-agi:mainfrom
Charanvardhan wants to merge 2 commits intoagno-agi:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses the critical data loss behavior identified in #7051 where both
optimize_memoriesandaoptimize_memoriesutilize a non-atomicDelete -> Insertflow. Previously, if the database connection dropped or crashed after the deletion stage but before the inserts completed, all of a user's original memories were permanently lost.Following the approach outlined in the issue, this PR reorders operations to an "insert-before-delete" approach:
This eliminates the data wipe risk across both sync and async database implementations without negatively impacting base compatibility or requiring cross-database native transactions.
fixes #7051
Type of change
Checklist
./scripts/format.shand./scripts/validate.sh)Duplicate and AI-Generated PR Check
Additional Notes
The unit tests inside
libs/agno/tests/unit/memory/test_memory_optimization.pywere adjusted since the legacy mock logic asserted againstmanager.clear_user_memories(), which is deliberately bypassed by this internal logic now directly invoking targeteddb.delete_user_memories(memory_ids=...). All new assertions comprehensively pass.