fix(memory): Atomic save_local + orphan-skip for FAISS docstore desync - #1797
Open
wgnrai wants to merge 1 commit into
Open
fix(memory): Atomic save_local + orphan-skip for FAISS docstore desync#1797wgnrai wants to merge 1 commit into
wgnrai wants to merge 1 commit into
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Atomic save_local + Desync-Resilient Search for FAISS Vectorstore
Problem
Langchain's
FAISS.save_local()writesindex.faissandindex.pklas two separate, non-atomic file operations. If a crash, process kill, or asyncio task cancellation occurs between these two writes, the FAISS index file reflects the new state (with newly added vectors) while the pickle file reflects the old state (without the corresponding docstore entries). On next load,similarity_search_with_score_by_vectorraisesValueError: Could not find document for id {_id}when it encounters an orphaned ID.This bug has been observed in production Agent Zero deployments, where it caused every-session crashes in memory recall and automatic memory consolidation.
Solution
This PR adds three complementary fixes:
Fix 1: Atomic save_local (Root Cause Prevention)
Overrides
save_local()to use the temp-file +os.replace()pattern:index.faisstoindex.faiss.tmpindex.pkltoindex.pkl.tmpwithfsyncos.replace()(POSIX rename is atomic)This narrows the desync window from milliseconds (full pickle write duration) to microseconds (two
os.replacesyscalls).Fix 2: Desync-Resilient similarity_search_with_score_by_vector (Defensive)
Overrides
similarity_search_with_score_by_vector()to catchValueErroron orphaned docstore lookups:ValueErrormatching "Could not find document for id":index_to_docstore_idbut not in docstore)remove_ids()index_to_docstore_idwith remaining entriesThis ensures that even if desync occurs, searches self-heal instead of crashing the application.
Fix 3: Atomic _write_index_hash
Updates
_write_index_hash()to use the same temp-file +os.replace()pattern, preventing hash file corruption from a crash mid-write.How They Work Together
Together they provide defense-in-depth: prevention + resilience.
Testing
Comprehensive test suite (30 checks, all passing):
pickle.dumpto raise mid-write, verify originals are NOT corrupted, temp files cleaned up, store still loadableImplementation
All overrides are added to the
MyFaiss(FAISS)subclass. The changes are backward-compatible. No changes to method signatures.Production Evidence
ValueErroron every sessionFiles Changed
plugins/_memory/helpers/memory.py: Addedsave_local()override (~40 lines),similarity_search_with_score_by_vector()override (~50 lines), and atomic_write_index_hash()update (~10 lines)