From 98688e4a93cdb1618f30a84199eae45120d75dd3 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 09:32:29 -0600 Subject: [PATCH] fix(provider-catalog-sync): make stripJsonc trailing-comma removal string-aware (#3430) stripJsonc stripped comments with a string-aware state machine but removed trailing commas with a whole-text regex, silently deleting commas inside string literals (x,} / a,] / a, } / x, ] and nested variant-style cases). Replace the regex with a string-aware scanner over the stripped text that skips string literals; real trailing commas and comment stripping unchanged. Pin both directions with parseJsonc tests. --- bin/provider-catalog-sync.mjs | 40 +++++++++++++++++++++++++++++- bin/provider-catalog-sync.test.mjs | 20 +++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/bin/provider-catalog-sync.mjs b/bin/provider-catalog-sync.mjs index c5271fe855..c6fe77aeb1 100644 --- a/bin/provider-catalog-sync.mjs +++ b/bin/provider-catalog-sync.mjs @@ -120,7 +120,45 @@ export function stripJsonc(text) { out.push(c); i += 1; } - return out.join('').replace(/,(\s*[}\]])/g, '$1'); + const stripped = out.join(''); + // String-aware trailing-comma removal: a ',' followed by only whitespace + // then '}' or ']' is structural, but the same sequence inside a string + // literal is content and must survive verbatim (#3430). + const res = []; + let j = 0; + let inStr = false; + let e = false; + while (j < stripped.length) { + const ch = stripped[j]; + if (inStr) { + res.push(ch); + if (e) e = false; + else if (ch === '\\') e = true; + else if (ch === '"') inStr = false; + j += 1; + continue; + } + if (ch === '"') { + inStr = true; + res.push(ch); + j += 1; + continue; + } + if (ch === ',') { + let k = j + 1; + while (k < stripped.length && /\s/.test(stripped[k])) k += 1; + if (stripped[k] === '}' || stripped[k] === ']') { + j += 1; + continue; + } + res.push(ch); + j += 1; + continue; + } + res.push(ch); + j += 1; + } + return res.join(''); } export function parseJsonc(text) { diff --git a/bin/provider-catalog-sync.test.mjs b/bin/provider-catalog-sync.test.mjs index c634e4b61a..285aacc6fa 100644 --- a/bin/provider-catalog-sync.test.mjs +++ b/bin/provider-catalog-sync.test.mjs @@ -55,6 +55,26 @@ test('parseJsonc strips comments and trailing commas', () => { assert.equal(cfg.provider['proxy-a'].models['gpt-1-sol'].limit.context, 1000); }); +test('parseJsonc preserves commas before brackets inside strings (#3430)', () => { + assert.equal(parseJsonc('{ "note": "x,}", "n": 1 }').note, 'x,}'); + assert.equal(parseJsonc('{ "note": "a,]", "n": 1 }').note, 'a,]'); + assert.equal(parseJsonc('{ "note": "a, }", "n": 1 }').note, 'a, }'); + assert.equal(parseJsonc('{ "list": ["x, ]", "y"] }').list[0], 'x, ]'); + const nested = parseJsonc( + '{ "provider": { "p": { "models": { "m": { "variants": { "v": { "headers": "a,}" } } } } } } }', + ); + assert.equal(nested.provider.p.models.m.variants.v.headers, 'a,}'); +}); + +test('parseJsonc still removes real trailing commas and strips comments (#3430)', () => { + assert.deepEqual(parseJsonc('{ "a": 1, }'), { a: 1 }); + assert.deepEqual(parseJsonc('// hi\n{ "a": [1, 2,], "b": { "c": 3, } }'), { + a: [1, 2], + b: { c: 3 }, + }); + assert.equal(parseJsonc('{ "url": "https://example.com//x" }').url, 'https://example.com//x'); +}); + test('findModelsSpan locates the models object per provider', () => { const span = findModelsSpan(FIXTURE, 'name-only-b'); assert.ok(span);