MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

fix(plugin): tolerate JSON5 comments when patching openclaw.json

Open Sanjays2402 opened this issue 6 months ago • 2 comments

Summary

The MemOS plugin patcher for openclaw.json used JSON.parse, which rejects the JSON5 features (line/block comments, trailing commas) that openclaw.json legitimately uses. Any // comment in the file caused the warning observed in #1543:

memos-local: could not patch tools.allow: SyntaxError: Expected double-quoted property name in JSON at position 2222 (line 90 column 7)

…and left tools.allow un-patched, so group:plugins never made it into the user's config.

Fix

  • Adds a small parseJsonWithComments helper in apps/memos-local-openclaw/src/shared/json5-lite.ts that strips // line comments, /* */ block comments, and trailing commas (string-literal aware) before delegating to JSON.parse. ~90 LOC, no new runtime dependency.
  • Switches the openclaw.json read in the patcher (apps/memos-local-openclaw/index.ts) to use it.
  • Tweaks the patcher's writeback regex to accept the existing array's optional trailing comma (also legal JSON5), so that once the parse succeeds the textual edit still applies cleanly. The replacement always re-inserts a single comma so the output is normalised.

Comments are preserved on round-trip

The existing patcher already does a targeted regex replace on the original raw text rather than re-serialising via JSON.stringify, so the user's comments and formatting survive untouched. Verified end-to-end against the bug-report scenario:

{
  // OpenClaw configuration
  "tools": {
    "allow": [
      "task-cli",
      "summarizer",   // trailing comma was previously the second blocker
    ]
  },
}

Before: JSON.parse throws → could not patch tools.allow warning, no change written. After: parse succeeds → group:plugins is appended → comments and trailing comma layout above the array remain intact.

Tests

Adds apps/memos-local-openclaw/tests/json5-lite.test.ts covering:

  • Plain JSON parses unchanged (regression).
  • // line comments are tolerated.
  • /* */ block comments are tolerated.
  • Trailing commas in arrays and objects are tolerated.
  • Comment-like sequences inside string literals are not stripped ("https://x/a//b", "/* not a comment */").
  • Escaped quotes inside strings are respected.
  • A realistic openclaw.json shape from the bug report parses correctly.
  • stripJsonComments preserves newlines so error-message line numbers stay aligned.

The tests follow the existing vitest style (e.g. tests/config.test.ts).

Scope

Deliberately tight: 3 files, +179 / −2. Only the openclaw.json read path and its companion writeback regex are touched. No other plugin behaviour changes.

Fixes #1543

Sanjays2402 avatar May 09 '26 19:05 Sanjays2402

Thanks — all four caught real issues, latest commit addresses them:

  • index.ts:372 (writeback scoping): Anchored the patch to the tools.allow array span using brace/bracket matching instead of a global text replace. This prevents the rewrite from straying into other arrays that happen to end with the same string — likely the root cause of #1377.
  • index.ts:371 (regex escaping): Added escapeRegExp and apply it to lastEntry before constructing the RegExp. Tool names containing ., +, ?, (, ), \\, etc. now behave correctly.
  • json5-lite.ts:78 (block-comment newlines): Block comments now emit one \n per newline they contained, so JSON.parse line numbers stay aligned with the source.
  • json5-lite.ts:91 (string-aware comma stripping): Refactored to a single string-literal-aware character scan. Trailing-comma stripping no longer touches commas inside string values.

Sanjays2402 avatar May 09 '26 19:05 Sanjays2402

This PR has been automatically marked as stale due to inactivity.

github-actions[bot] avatar Jun 09 '26 06:06 github-actions[bot]