fix(memory): atomic graph persistence and validate entity existence in create_relations - #4656
Open
AbhiPra24 wants to merge 1 commit into
Open
Conversation
…n create_relations
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
Fixes #4614 and #4457.
1. Atomic Persistence in
saveGraph()(Fixes #4614)saveGraph()directly calledfs.writeFile(this.memoryFilePath, ...), which truncates the file immediately upon opening. If the process is terminated mid-write, the memory file becomes corrupted or empty.${this.memoryFilePath}.${randomBytes(16).toString('hex')}.tmp), followed by an atomicfs.renameoverthis.memoryFilePath. The temporary file is cleaned up if an error occurs.2. Referential Integrity in
createRelations()(Fixes #4457)createRelations()only checked for duplicate relations, accepting dangling relations between non-existent entities without validation.fromandtoentities exist ingraph.entitiesprior to mutating the graph, throwingEntity with name <name> not foundto matchaddObservations().Verification
src/memory/__tests__/knowledge-graph.test.ts.npm run buildandnpm testpassed (56/56 tests green).cc @olaservo for review when available. Thank you!