Skip to content

fix(memory): add trailing newline to JSONL output#3653

Open
nickveenhof wants to merge 1 commit intomodelcontextprotocol:mainfrom
nickveenhof:fix/memory-server-trailing-newline
Open

fix(memory): add trailing newline to JSONL output#3653
nickveenhof wants to merge 1 commit intomodelcontextprotocol:mainfrom
nickveenhof:fix/memory-server-trailing-newline

Conversation

@nickveenhof
Copy link

@nickveenhof nickveenhof commented Mar 20, 2026

Summary

saveGraph writes lines.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

saveGraph produces a file where the last line has no terminating \n:

{"type":"entity","name":"Alice","entityType":"person","observations":["test"]}
{"type":"relation","from":"Alice","to":"Bob","relationType":"knows"}  <-- no \n

A JSONL file should look like this:

{"type":"entity","name":"Alice","entityType":"person","observations":["test"]}\n
{"type":"relation","from":"Alice","to":"Bob","relationType":"knows"}\n

The missing terminator means any tool or process that appends to the file (log rotation, crash recovery, file concatenation) produces invalid JSONL:

{"type":"relation","from":"Alice","to":"Bob","relationType":"knows"}{"type":"entity","name":"External",...}

JSON.parse() then throws on that line:

Unexpected non-whitespace character after JSON at position 88 (line 1 column 89)

How I hit this

I run the memory server across multiple concurrent sessions pointing at the same memory.jsonl file. 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 saveGraph concurrently 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

// Before
await fs.writeFile(this.memoryFilePath, lines.join("\n"));

// After
await fs.writeFile(this.memoryFilePath, lines.join("\n") + "\n");

loadGraph already filters empty lines (filter(line => line.trim() !== "")), so the trailing \n has no side effects on read.

Tests

Three new tests in knowledge-graph.test.ts:

  1. Trailing newline check: verifies the file ends with \n.
  2. Per-line validity: verifies every non-empty line parses as valid JSON individually.
  3. External append simulation: writes entities via saveGraph, appends a line via fs.appendFile, then reads back via a fresh KnowledgeGraphManager instance. This test fails without the fix and passes with it.

All 48 tests pass. No existing tests affected.

Related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants