fix(memory): write graph atomically in saveGraph to prevent file corruption - #4696
Open
AbhiPra24 wants to merge 1 commit into
Open
fix(memory): write graph atomically in saveGraph to prevent file corruption#4696AbhiPra24 wants to merge 1 commit into
AbhiPra24 wants to merge 1 commit into
Conversation
|
Thanks — this is the most tightly scoped of the five PRs on #4614, and I also followed the naming convention shared by all four open PRs here: the Summary of what I took from each PR, and how I validated the merged version: |
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.
Description
Fixes #4614.
In
KnowledgeGraphManager.saveGraph()(src/memory/index.ts), the knowledge graph was written directly in-place tothis.memoryFilePathviafs.writeFile. If the process was terminated mid-write (SIGKILL, container stop, OOM kill, power loss), the memory file could be truncated or left corrupt without recovery.This PR makes saving atomic by:
${this.memoryFilePath}.${Date.now()}-${random}.tmp) in the same directory.this.memoryFilePathusingfs.rename().Changes
KnowledgeGraphManager.saveGraphinsrc/memory/index.tsto write to temp file then rename.src/memory/__tests__/knowledge-graph.test.tsverifying atomic persistence and no leftover temp files.Verification
npm testandnpm run buildinsrc/memory. All 51 tests pass.