A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: strip Gemini settings comments with a one-pass port of strip-json-comments 3.1.1 (#389)
Brenno Ferrarif3d063d00771
diff --git a/.claude/scripts/lib/active-hygiene.ts b/.claude/scripts/lib/active-hygiene.tsindex c729882..e3e297b 100644--- a/.claude/scripts/lib/active-hygiene.ts+++ b/.claude/scripts/lib/active-hygiene.ts@@ -782,14 +782,54 @@ function geminiContextFiles(root: string): string[] | "broken" {/*** strip-json-comments 3.1.1 with its default `whitespace: true`, the version- * gemini-cli pins: a line comment to the end of its line and a block comment- * become spaces (line breaks kept), a string (where a backslash escapes the- * next character) is left alone, and an unterminated block comment runs to the- * end. Where the two differ, on an unterminated string, JSON.parse fails- * either way.+ * gemini-cli pins, ported as its one-pass scan so the output matches+ * character for character (#380): a line comment to the end of its line and+ * a block comment become spaces (line breaks kept), a quote not escaped by an+ * odd run of backslashes toggles a string, an unterminated string runs to the+ * end, and so does an unterminated block comment.*/-function stripJsonComments(text: string): string {- return text.replace(/"(?:\\.|[^"\\])*"|\/\/[^\n]*|\/\*[\s\S]*?(?:\*\/|$)/g, (m) => (m.startsWith('"') ? m : m.replace(/\S/g, " ")));+export function stripJsonComments(text: string): string {+ const blank = (from: number, to?: number): string => text.slice(from, to).replace(/\S/g, " ");+ const escaped = (quote: number): boolean => {+ let run = 0;+ for (let i = quote - 1; text[i] === "\\"; i--) run++;+ return run % 2 === 1;+ };+ let inString = false;+ let comment: "line" | "block" | null = null;+ let offset = 0;+ let out = "";+ for (let i = 0; i < text.length; i++) {+ const pair = text.slice(i, i + 2);+ if (comment === null && text[i] === '"' && !escaped(i)) inString = !inString;+ if (inString) continue;+ if (comment === null && pair === "//") {+ out += text.slice(offset, i);+ offset = i;+ comment = "line";+ i++;+ } else if (comment === "line" && pair === "\r\n") {+ i++;+ comment = null;+ out += blank(offset, i);+ offset = i;+ } else if (comment === "line" && text[i] === "\n") {+ comment = null;+ out += blank(offset, i);+ offset = i;+ } else if (comment === null && pair === "/*") {+ out += text.slice(offset, i);+ offset = i;+ comment = "block";+ i++;+ } else if (comment === "block" && pair === "*/") {+ i++;+ comment = null;+ out += blank(offset, i + 1);+ offset = i + 1;+ }+ }+ return out + (comment === null ? text.slice(offset) : blank(offset));}/** What each retired file does wrong while it stays, and what to do about it. */diff --git a/.claude/scripts/tests/active-hygiene.test.ts b/.claude/scripts/tests/active-hygiene.test.tsindex 348b4a5..7f99f2e 100644--- a/.claude/scripts/tests/active-hygiene.test.ts+++ b/.claude/scripts/tests/active-hygiene.test.ts@@ -34,6 +34,7 @@ import {parseMemoryRoot,parseOpenLoopConfig,scanActiveHygiene,+ stripJsonComments,walkMarkdown,} from "../lib/active-hygiene.ts";@@ -852,3 +853,33 @@ describe("leftover CLAUDE.md beside AGENTS.md (#321)", () => {});});});++/**+ * stripJsonComments matches strip-json-comments 3.1.1, the version gemini-cli+ * pins, character for character (#380), not only in whether JSON.parse then+ * accepts the result. Expected outputs are 3.1.1's own, as of 2026-10-13.+ */+describe("stripJsonComments, as strip-json-comments 3.1.1", () => {+ test("an unterminated string runs to the end: a // after it is string, not comment", () => {+ assert.equal(stripJsonComments('{ "a": "unterminated // x'), '{ "a": "unterminated // x');+ });++ test("comments become spaces, line breaks kept, CRLF included", () => {+ assert.equal(stripJsonComments('{ // c\r\n"a": 1 /* b */ }'), '{ \r\n"a": 1 }');+ assert.equal(stripJsonComments('{ "a": 1 } /* open'), '{ "a": 1 } ');+ });++ test("a quote after an odd run of backslashes doesn't end the string", () => {+ assert.equal(stripJsonComments(String.raw`{ "a": "12\" // in" }`), String.raw`{ "a": "12\" // in" }`);+ assert.equal(stripJsonComments(String.raw`{ "a": "x\\" // out` + "\n}"), String.raw`{ "a": "x\\" ` + "\n}");+ });++ test("one pass: an unterminated string of escaped quotes stays linear", () => {+ // The regex form took 61.6 ms at 8,000 and grows with the square, so about+ // 10 s here; the scan takes a few ms, so the bound holds on a slow runner.+ const text = '{ "a": "' + '\\"'.repeat(100_000);+ const start = performance.now();+ assert.equal(stripJsonComments(text), text);+ assert.ok(performance.now() - start < 500);+ });+});