fix(memory): add trailing newline to JSONL output#3653
Conversation
|
@claude review |
|
Claude finished @cliffhall's task in 1m 5s —— View job PR Review: fix(memory): add trailing newline to JSONL output
SummaryThis is a clean, well-motivated fix. The change is correct and the JSONL spec compliance argument is solid. A few minor observations below. Implementation (
|
LuuOW
left a comment
There was a problem hiding this comment.
Technical audit: Verified MCP server implementation for consistency with current SDK patterns.
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)