fix(memory): add trailing newline to JSONL output#3653
Open
nickveenhof wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(memory): add trailing newline to JSONL output#3653nickveenhof wants to merge 1 commit intomodelcontextprotocol:mainfrom
nickveenhof wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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
saveGraphwriteslines.join("\n")without a trailing newline. The JSONL spec requires every record to be terminated by\n. This makes the output non-conformant and fragile.The problem
saveGraphproduces a file where the last line has no terminating\n:A JSONL file should look like this:
The missing terminator means any tool or process that appends to the file (log rotation, crash recovery, file concatenation) produces invalid JSONL:
JSON.parse()then throws on that line:How I hit this
I run the memory server across multiple concurrent sessions pointing at the same
memory.jsonlfile. After several days I found a line with two JSON objects concatenated. Every tool call returned the parse error above until I manually edited the file.The root cause is likely the race condition described in #2579: multiple MCP server instances calling
saveGraphconcurrently on the same file. #3286 addresses that with cross-process file locking and atomic writes, which is the proper fix.This PR does not solve the race condition. It makes the output JSONL-spec-conformant, which is correct regardless of whether #3286 lands.
Fix
loadGraphalready filters empty lines (filter(line => line.trim() !== "")), so the trailing\nhas no side effects on read.Tests
Three new tests in
knowledge-graph.test.ts:\n.saveGraph, appends a line viafs.appendFile, then reads back via a freshKnowledgeGraphManagerinstance. This test fails without the fix and passes with it.All 48 tests pass. No existing tests affected.
Related
mcp-server-memoryFailures due to Race Condition and Environment Misconfiguration #2579: the race condition that causes JSONL corruption with concurrent instancesproper-lockfile(open, the full fix)