fix(plugin): tolerate JSON5 comments when patching openclaw.json#1671
Open
Sanjays2402 wants to merge 2 commits intoMemTensor:mainfrom
Open
fix(plugin): tolerate JSON5 comments when patching openclaw.json#1671Sanjays2402 wants to merge 2 commits intoMemTensor:mainfrom
Sanjays2402 wants to merge 2 commits intoMemTensor:mainfrom
Conversation
The plugin's openclaw.json patcher used JSON.parse, which rejected the JSON5 features (line/block comments, trailing commas) that openclaw.json legitimately uses. With any '//' comment in the file the patch would fail with a SyntaxError, leaving 'tools.allow' untouched and the user's config unchanged (the warning observed in MemTensor#1543). This change: * Adds 'parseJsonWithComments' (under src/shared/json5-lite.ts), a small string-aware helper that strips line comments, block comments, and trailing commas before delegating to JSON.parse. No new runtime dependency. * Switches the openclaw.json read in the patcher to use it. * Tweaks the patcher's writeback regex to accept the existing array's optional trailing comma (also legal JSON5), so that even after the parse succeeds the textual edit still applies cleanly. The replacement always re-inserts a single comma, normalising output. The writeback is a targeted regex edit against the original raw text, not a full JSON re-serialisation, so any comments and original formatting in the user's openclaw.json are preserved on round-trip. Adds unit tests for the parser covering plain JSON, line/block comments, trailing commas in arrays and objects, comment-like sequences inside string literals, and escaped quotes. Fixes MemTensor#1543
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the memos-local OpenClaw plugin’s openclaw.json patcher to tolerate JSON5-style comments and trailing commas so it can reliably read and patch tools.allow (fixing the failure reported in #1543).
Changes:
- Added a lightweight JSON5-tolerant preprocessor/parser (
parseJsonWithComments) that strips line/block comments and trailing commas before delegating toJSON.parse. - Switched the patcher’s
openclaw.jsonread path to use the new helper. - Adjusted the writeback regex to accept an optional trailing comma before the closing
].
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| apps/memos-local-openclaw/src/shared/json5-lite.ts | Introduces a small comment/trailing-comma tolerant parsing shim used when reading openclaw.json. |
| apps/memos-local-openclaw/index.ts | Uses the new parser for openclaw.json and broadens the patch regex to handle an optional trailing comma. |
| apps/memos-local-openclaw/tests/json5-lite.test.ts | Adds unit tests covering comment/trailing-comma tolerance and basic string-literal safety cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
365
to
372
| if (Array.isArray(allow) && allow.length > 0 && !allow.includes("group:plugins") && !allow.includes("*")) { | ||
| const lastEntry = JSON.stringify(allow[allow.length - 1]); | ||
| // Match the last entry + optional trailing comma (legal in JSON5) | ||
| // + closing `]`. The replacement always re-inserts a single comma. | ||
| const patched = raw.replace( | ||
| new RegExp(`(${lastEntry})(\\s*\\])`), | ||
| new RegExp(`(${lastEntry})\\s*,?(\\s*\\])`), | ||
| `$1,\n "group:plugins"$2`, | ||
| ); |
Comment on lines
366
to
371
| const lastEntry = JSON.stringify(allow[allow.length - 1]); | ||
| // Match the last entry + optional trailing comma (legal in JSON5) | ||
| // + closing `]`. The replacement always re-inserts a single comma. | ||
| const patched = raw.replace( | ||
| new RegExp(`(${lastEntry})(\\s*\\])`), | ||
| new RegExp(`(${lastEntry})\\s*,?(\\s*\\])`), | ||
| `$1,\n "group:plugins"$2`, |
| // Block comment: `/* … */` | ||
| if (ch === "/" && next === "*") { | ||
| i += 2; | ||
| while (i < n && !(text[i] === "*" && text[i + 1] === "/")) i += 1; |
Comment on lines
+87
to
+91
| // Strip trailing commas: `,` followed by optional whitespace and `]` or `}`. | ||
| // Run outside the per-char loop so it doesn't have to be string-aware itself | ||
| // (the prior pass already preserved string content). | ||
| return out.replace(/,(\s*[\]}])/g, "$1"); | ||
| } |
… preserve newlines in block comments Address Copilot review on MemTensor#1671: - index.ts: anchor the lastEntry-replacement regex to the tools.allow array span using brace/bracket matching, not the global file. This prevents accidental rewrites elsewhere in openclaw.json (e.g. when the same string is the last element of another array — the source of MemTensor#1377). - index.ts: escape regex metacharacters in lastEntry before building the RegExp. Tool names with dots, parens, brackets etc. would have silently misbehaved. - json5-lite.ts: rewrite as a single string-literal-aware state machine. Trailing-comma stripping no longer touches commas inside string values. Block comment stripping preserves the newline count so JSON.parse error line numbers continue to align with the source.
Author
|
Thanks — all four caught real issues, latest commit addresses them:
|
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
The MemOS plugin patcher for
openclaw.jsonusedJSON.parse, which rejects the JSON5 features (line/block comments, trailing commas) thatopenclaw.jsonlegitimately uses. Any//comment in the file caused the warning observed in #1543:…and left
tools.allowun-patched, sogroup:pluginsnever made it into the user's config.Fix
parseJsonWithCommentshelper inapps/memos-local-openclaw/src/shared/json5-lite.tsthat strips//line comments,/* */block comments, and trailing commas (string-literal aware) before delegating toJSON.parse. ~90 LOC, no new runtime dependency.apps/memos-local-openclaw/index.ts) to use it.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.parsethrows →could not patch tools.allowwarning, no change written.After: parse succeeds →
group:pluginsis appended → comments and trailing comma layout above the array remain intact.Tests
Adds
apps/memos-local-openclaw/tests/json5-lite.test.tscovering://line comments are tolerated./* */block comments are tolerated."https://x/a//b","/* not a comment */").stripJsonCommentspreserves newlines so error-message line numbers stay aligned.The tests follow the existing
viteststyle (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