fix(memory): write memory graph atomically to prevent corruption - #4618
Open
Arnab758 wants to merge 2 commits into
Open
fix(memory): write memory graph atomically to prevent corruption#4618Arnab758 wants to merge 2 commits into
Arnab758 wants to merge 2 commits into
Conversation
saveGraph() wrote the memory file directly with fs.writeFile, so an interruption mid-write (crash, kill, power loss) could leave a truncated or corrupted memory file. Write to a unique temp file first, then rename into place — atomic on POSIX and Windows. Clean up the temp file on failure. Includes a regression test.
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.
What
Fixes #4614.
saveGraph()wrote the memory file directly withfs.writeFile, so an interruption mid-write (process crash, kill, power loss) could leave a truncated or corrupted memory file — permanently losing the agent's long-term memory.How
Write the graph to a unique temp file, then
rename()it into place. Rename is atomic on POSIX and Windows, so the memory file is always either the old complete version or the new complete version — never a half-written one.randomBytes(16)) prevents collisions when saves overlapTests
Added a regression test that simulates an interruption mid-write (partial bytes written, then error) and verifies:
tsc --noEmitpasses. Full suite: 51/51 tests pass.Notes
This complements (not duplicates) #4555, which serializes concurrent graph mutations. #4555 fixes concurrent writes racing each other; this fixes a single write being interrupted mid-write — two different failure modes.
Known limitation: a hard kill between write and rename can still leave a temp file behind (inherent to the temp+rename pattern). The memory file itself is never corrupted.