Conversation
saveGraph writes a temp file and renames it over the memory file, which replaces the inode with one created under the process umask. A memory file the operator narrowed with chmod 600 therefore came back 0644 after the next mutation. Capture the existing permission bits before the write and restore them after the rename, matching what the filesystem server does for the same temp-file+rename pattern.
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
KnowledgeGraphManager.saveGraph()persists the graph by writing a temporary file and renaming it overmemory.jsonl. The rename replaces the target's inode with the temp file's, and the temp file was created under the process umask (0666 & ~umask), so permission bits an operator set on the memory file were discarded on the next mutation and the file came back0644. This restores the original bits after a successful rename.Server Details
memorysrc/memory/index.ts)Motivation and Context
Fixes #4827.
Root cause —
src/memory/index.ts:193-194before this change:rename(2)replaces the target inode with the temp file's inode. The temp file is created with the process umask applied, so the original file's mode is discarded with its inode and nothing restores it.saveGraphfeeds every mutation (create_entities,add_observations,delete_*), so all of them loosened the file.This is a regression from #4642 (
642a911): the previousawait fs.writeFile(this.memoryFilePath, lines.join("\n"))preserved the mode of an existing file.The repository already settled the contract for this exact write pattern.
src/filesystem/lib.ts:231restoresorigStats.mode & 0o777after its temp-file + rename, merged as #4115 — "The atomic write pattern (write temp file + rename) replaces the original inode, causing the new file to have default 0644 permissions regardless of what the original file had." This change applies the same restore to the memory server.Changes
src/memory/index.ts—saveGraph()stats the memory file before the write, toleratingENOENTfor a file that does not exist yet (nothing to preserve), and restoresmode & 0o777after a successful rename with a best-effortchmod. The data is already durable at that point, so achmodfailure does not fail the save — the same trade-off the filesystem server makes.src/memory/__tests__/file-permissions.test.ts— new regression test covering0600and0640across a mutation, and asserting the mutation is still persisted.How Has This Been Tested?
The new test fails on the unpatched tree and passes with the fix. No LLM client was used.
Breaking Changes
None. A file that does not exist yet is created exactly as before, under the process umask.
Types of changes
Checklist
Additional context
Scope note: this change preserves the permission bits of an existing memory file, including a read-only one, but it does not make the write itself fail on a
0444target.rename(2)only needs a writable containing directory, so the rename succeeds where the pre-#4642fs.writeFileraisedEACCES. Rejecting that write would be a separate behaviour change — it would makesaveGraphfail where it currently does not — so it is not included here. Happy to add it if you want that contract restored.The open PR #4819 touches
src/memory/index.tsinaddObservations(around line 250), well away fromsaveGraph; the two should not conflict.