From 7b07a6c0879b3bb4f641f73e6bbea5d412537c35 Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Mon, 2 Nov 2026 17:05:27 -0600 Subject: [PATCH 1/3] opencode-config-normalize: textual plugin+plugins merge preserves in-array comments (#4639) The both-present merge re-serialised the merged array with json.dumps, silently deleting every comment inside both arrays while reporting success. Splice the legacy elements into the plugins array as text (legacy first, native appended) and drop the v1 entry, so comments survive byte-identical. Shapes that cannot be spliced comment-safely (e.g. a // comment trailing the last legacy element) fail closed with exit 1 and the file byte-identical. Regression fixtures: both-present merge with comments in both arrays (comments asserted byte-present), plus the fail-closed boundary case. --- pkgs/opencode-config-normalize/normalize.sh | 97 +++++++++++++++++-- .../normalize_test.sh | 44 +++++++++ 2 files changed, 133 insertions(+), 8 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 91768746eb..39975ffb26 100755 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -279,11 +279,97 @@ def delete_top_level_entry(text, key): return None +def line_comment_at_end(fragment): + # True when fragment's last line ends inside a // line comment: a + # joining comma appended after it would be swallowed by the comment. + # String- and block-comment-aware so // inside a string value or a + # /* */ comment does not misfire (#3179). + line = fragment.split('\n')[-1] + in_str = False + i = 0 + while i < len(line): + c = line[i] + if in_str: + if c == '\\' and i + 1 < len(line): + i += 2 + continue + if c == '"': + in_str = False + i += 1 + continue + if c == '"': + in_str = True + i += 1 + continue + if c == '/' and line[i + 1:i + 2] == '/': + return True + if c == '/' and line[i + 1:i + 2] == '*': + end = line.find('*/', i + 2) + if end < 0: + return True # runs past EOL: fail closed + i = end + 2 + continue + i += 1 + return False + + +def splice_plugin_merge(text): + # Both-present plugin+plugins merge (#4639) as a textual element + # splice: the legacy elements move into the `plugins` array (legacy + # first, native appended — the fork's encoded.plugins order) and the + # v1 `plugin` entry is dropped, all without re-serialising, so + # comments inside both arrays survive byte-identical. Returns the new + # text, or None when the splice cannot be done comment-safely (e.g. a + # // line comment trailing the last legacy element would swallow the + # joining comma); the caller then exits 1 with the file + # byte-identical — a repair tool must never be a data-loss path + # (#2364). + legacy_span = find_value_span(text, 'plugin') + native_span = find_value_span(text, 'plugins') + if legacy_span is None or native_span is None: + return None + lvs, lve = legacy_span + nvs, nve = native_span + legacy_text = text[lvs:lve] + native_text = text[nvs:nve] + if not (legacy_text.startswith('[') and legacy_text.endswith(']')): + return None + if not (native_text.startswith('[') and native_text.endswith(']')): + return None + legacy_inner = legacy_text[1:-1].strip() + native_inner = native_text[1:-1].strip() + if legacy_inner and native_inner: + core = legacy_inner + if core.endswith(','): + core = core[:-1].rstrip() + if line_comment_at_end(core): + return None + inner = core + ',\n' + native_inner + elif legacy_inner: + inner = legacy_inner + else: + inner = None + if inner is None: + merged = native_text # nothing to move: keep native bytes verbatim + elif line_comment_at_end(inner): + # A // comment runs to end of line: the closing bracket goes on + # its own line so it is not swallowed by the comment. + merged = '[' + inner + '\n]' + else: + merged = '[' + inner + ']' + text = text[:nvs] + merged + text[nve:] + return delete_top_level_entry(text, 'plugin') + + # plugin -> plugins. When `plugins` is absent this is a bare key rename # (array contents are byte-preserved, comments included). When both # spellings are present, merge the way the fork does — legacy entries # first, native entries appended (normalize.ts: encoded.plugins = -# [...legacyPlugins, ...nativePlugins]) — then drop the v1 entry. A raw +# [...legacyPlugins, ...nativePlugins]) — then drop the v1 entry. The +# merge is a textual element splice, not a re-serialise, so comments +# inside both arrays survive; shapes that cannot be spliced +# comment-safely fail closed with exit 1 and the file byte-identical +# (#4639). A raw # text search can hit "plugin": inside an earlier comment and leave the # live key unrepaired (PR #4312 review), so all splices go through the # string/comment-aware locators, and any splice that cannot be done @@ -301,14 +387,9 @@ if 'plugin' in doc: native = doc.get('plugins') if not isinstance(legacy, list) or not isinstance(native, list): sys.exit(1) # unmergeable shapes: leave the file untouched - span = find_value_span(out, 'plugins') - if span is None: - sys.exit(1) - vs, ve = span - out = out[:vs] + json.dumps(legacy + native) + out[ve:] - out = delete_top_level_entry(out, 'plugin') + out = splice_plugin_merge(out) if out is None: - sys.exit(1) + sys.exit(1) # not spliceable comment-safely: fail closed changed = True # agent -> agents: fold the v1 `agent` map into `agents` (existing diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 69652d8544..c365d9cc7b 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -310,6 +310,50 @@ EOF check plugins-merge "$tmp/plugins-merge.fixture" \ '{"$schema":"https://opencode.ai/config.json","plugins":["opencode-plugin-a","opencode-plugin-b",["opencode-plugin-c",{"opt":1}]]}' 0 +# Case 14: plugin + plugins both present with comments inside BOTH +# arrays (#4639) — the merge is a textual element splice, so every +# comment survives byte-identical instead of the arrays being +# re-serialised. +cat >"$tmp/plugins-merge-comments.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + // file-level note + "plugin": [ + /* legacy note */ + "opencode-plugin-a" + ], + "plugins": [ + // native note + "opencode-plugin-b" + // trailing native + ] +} +EOF +check plugins-merge-comments "$tmp/plugins-merge-comments.fixture" \ + '{"$schema":"https://opencode.ai/config.json","plugins":["opencode-plugin-a","opencode-plugin-b"]}' 0 +for needle in '/* legacy note */' '// native note' '// trailing native' '// file-level note'; do + grep -qF "$needle" "$tmp/plugins-merge-comments.jsonc" || { + echo "FAIL plugins-merge-comments: lost comment: $needle" + failures=$((failures + 1)) + } +done + +# Case 15: a // line comment trailing the last legacy element (#4639 +# boundary) — the joining comma would be swallowed by the comment, so +# the merge fails closed: exit 1 with the file byte-identical. +cat >"$tmp/plugins-merge-trailing-comment.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "plugin": [ + "opencode-plugin-a" // trailing legacy note + ], + "plugins": [ + "opencode-plugin-b" + ] +} +EOF +check plugins-merge-trailing-comment "$tmp/plugins-merge-trailing-comment.fixture" '' 1 + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1 From c2a158ce4e941433e0d67cd9ee6b98619c29c3e4 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 18:26:10 -0600 Subject: [PATCH 2/3] fix(opencode-config-normalize): strip commented trailing plugin comma --- pkgs/opencode-config-normalize/normalize.sh | 83 ++++++++++++++++++- .../normalize_test.sh | 17 ++++ 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index ae8f8050e7..f441c08f5e 100755 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -356,6 +356,85 @@ def line_comment_at_end(fragment): return False +def strip_trailing_element_comma(fragment): + # Remove the trailing element-separator comma (the one before the + # closing bracket), skipping over trailing comments, so a legacy + # array like ["a", /* keep */] splices without a doubled comma + # (Codex P2 on PR #4644: the naive endswith(',') check missed the + # comma hidden behind the block comment and the merge wrongly + # failed closed on input the old code repaired). Only a comma + # followed solely by whitespace/comments is removed; a comma inside + # a string, a comment, or a nested array/object is never touched. + # Returns the fragment unchanged when there is no trailing comma. + last_comma = -1 + i = 0 + n = len(fragment) + depth = 0 + in_str = False + in_line = False + in_block = False + while i < n: + c = fragment[i] + if in_line: + if c == '\n': + in_line = False + i += 1 + continue + if in_block: + if c == '*' and fragment[i + 1:i + 2] == '/': + in_block = False + i += 2 + continue + i += 1 + continue + if in_str: + if c == '\\' and i + 1 < n: + i += 2 + continue + if c == '"': + in_str = False + i += 1 + continue + if c == '"': + in_str = True + elif c == '/' and fragment[i + 1:i + 2] == '/': + in_line = True + i += 2 + continue + elif c == '/' and fragment[i + 1:i + 2] == '*': + in_block = True + i += 2 + continue + elif c in '[{': + depth += 1 + elif c in ']}': + depth -= 1 + elif c == ',' and depth == 0: + last_comma = i + i += 1 + if last_comma < 0: + return fragment + tail = fragment[last_comma + 1:] + k = 0 + m = len(tail) + while k < m: + if tail[k] in ' \t\r\n': + k += 1 + continue + if tail.startswith('//', k): + nl = tail.find('\n', k) + k = m if nl < 0 else nl + 1 + continue + if tail.startswith('/*', k): + end = tail.find('*/', k + 2) + if end < 0: + return fragment # unterminated: leave alone, fail closed + k = end + 2 + continue + return fragment # real content after the comma: not trailing + return fragment[:last_comma] + tail + + def splice_plugin_merge(text): # Both-present plugin+plugins merge (#4639) as a textual element # splice: the legacy elements move into the `plugins` array (legacy @@ -382,9 +461,7 @@ def splice_plugin_merge(text): legacy_inner = legacy_text[1:-1].strip() native_inner = native_text[1:-1].strip() if legacy_inner and native_inner: - core = legacy_inner - if core.endswith(','): - core = core[:-1].rstrip() + core = strip_trailing_element_comma(legacy_inner).rstrip() if line_comment_at_end(core): return None inner = core + ',\n' + native_inner diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 62fc5de07a..e55f935355 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -448,6 +448,23 @@ cat >"$tmp/plugins-merge-trailing-comment.fixture" <<'EOF' EOF check plugins-merge-trailing-comment "$tmp/plugins-merge-trailing-comment.fixture" '' 1 +# Case 21 (Codex P2 on PR #4644): a trailing comma before a trailing +# block comment is still the legacy array's element separator. Remove +# that comma while preserving the comment, then perform the merge. +cat >"$tmp/plugins-merge-trailing-block-comment.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "plugin": ["opencode-plugin-a", /* keep trailing block */], + "plugins": ["opencode-plugin-b"] +} +EOF +check plugins-merge-trailing-block-comment "$tmp/plugins-merge-trailing-block-comment.fixture" \ + '{"$schema":"https://opencode.ai/config.json","plugins":["opencode-plugin-a","opencode-plugin-b"]}' 0 +grep -qF '/* keep trailing block */' "$tmp/plugins-merge-trailing-block-comment.jsonc" || { + echo "FAIL plugins-merge-trailing-block-comment: lost trailing block comment" + failures=$((failures + 1)) +} + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1 From 885e3dabbc53ebdcbeb56a5988309bb84c851e36 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 19:18:10 -0600 Subject: [PATCH 3/3] fix(opencode-config-normalize): handle commented merge edge cases --- pkgs/opencode-config-normalize/normalize.sh | 72 ++++++++++++------- .../normalize_test.sh | 35 +++++++++ 2 files changed, 83 insertions(+), 24 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index f441c08f5e..200a3e7503 100755 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -323,17 +323,34 @@ def delete_top_level_entry(text, key): def line_comment_at_end(fragment): - # True when fragment's last line ends inside a // line comment: a - # joining comma appended after it would be swallowed by the comment. + # True when fragment ends inside a // line comment: a joining + # comma appended after it would be swallowed by the comment. # String- and block-comment-aware so // inside a string value or a - # /* */ comment does not misfire (#3179). - line = fragment.split('\n')[-1] + # /* */ comment does not misfire (#3179). The whole fragment is + # scanned while tracking block-comment state (Codex P2 on PR + # #4644): a multiline block comment whose final line holds a URL + # (e.g. "a" /* note\nhttps://example.com */) must not have its + # // mistaken for a live line comment. in_str = False - i = 0 - while i < len(line): - c = line[i] + in_block = False + in_line = False + i, n = 0, len(fragment) + while i < n: + c = fragment[i] + if in_line: + if c == '\n': + in_line = False + i += 1 + continue + if in_block: + if c == '*' and fragment[i + 1:i + 2] == '/': + in_block = False + i += 2 + continue + i += 1 + continue if in_str: - if c == '\\' and i + 1 < len(line): + if c == '\\' and i + 1 < n: i += 2 continue if c == '"': @@ -342,18 +359,18 @@ def line_comment_at_end(fragment): continue if c == '"': in_str = True - i += 1 + elif c == '/' and fragment[i + 1:i + 2] == '/': + in_line = True + i += 2 continue - if c == '/' and line[i + 1:i + 2] == '/': - return True - if c == '/' and line[i + 1:i + 2] == '*': - end = line.find('*/', i + 2) - if end < 0: - return True # runs past EOL: fail closed - i = end + 2 + elif c == '/' and fragment[i + 1:i + 2] == '*': + in_block = True + i += 2 continue i += 1 - return False + if in_block: + return True # unterminated block comment: fail closed + return in_line def strip_trailing_element_comma(fragment): @@ -435,7 +452,7 @@ def strip_trailing_element_comma(fragment): return fragment[:last_comma] + tail -def splice_plugin_merge(text): +def splice_plugin_merge(text, legacy_has_elements=True): # Both-present plugin+plugins merge (#4639) as a textual element # splice: the legacy elements move into the `plugins` array (legacy # first, native appended — the fork's encoded.plugins order) and the @@ -445,7 +462,11 @@ def splice_plugin_merge(text): # // line comment trailing the last legacy element would swallow the # joining comma); the caller then exits 1 with the file # byte-identical — a repair tool must never be a data-loss path - # (#2364). + # (#2364). legacy_has_elements reports whether the legacy array + # holds actual elements (vs comments only): a comment-only legacy + # array needs no joining comma, and emitting one would be a leading + # syntax error (Codex P2 on PR #4644: [/* disabled */] + ["b"] + # wrongly failed closed on input the old code repaired). legacy_span = find_value_span(text, 'plugin') native_span = find_value_span(text, 'plugins') if legacy_span is None or native_span is None: @@ -461,10 +482,13 @@ def splice_plugin_merge(text): legacy_inner = legacy_text[1:-1].strip() native_inner = native_text[1:-1].strip() if legacy_inner and native_inner: - core = strip_trailing_element_comma(legacy_inner).rstrip() - if line_comment_at_end(core): - return None - inner = core + ',\n' + native_inner + if legacy_has_elements: + core = strip_trailing_element_comma(legacy_inner).rstrip() + if line_comment_at_end(core): + return None + inner = core + ',\n' + native_inner + else: + inner = legacy_inner + '\n' + native_inner elif legacy_inner: inner = legacy_inner else: @@ -507,7 +531,7 @@ if 'plugin' in doc: native = doc.get('plugins') if not isinstance(legacy, list) or not isinstance(native, list): sys.exit(1) # unmergeable shapes: leave the file untouched - out = splice_plugin_merge(out) + out = splice_plugin_merge(out, bool(legacy)) if out is None: sys.exit(1) # not spliceable comment-safely: fail closed changed = True diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index e55f935355..a6e5baae63 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -465,6 +465,41 @@ grep -qF '/* keep trailing block */' "$tmp/plugins-merge-trailing-block-comment. failures=$((failures + 1)) } +# Case 22 (Codex P2 on PR #4644): legacy array holds only a comment, +# e.g. [/* disabled */] — no joining comma is emitted, so the merge +# succeeds and the comment survives instead of failing closed. +cat >"$tmp/plugins-merge-comment-only-legacy.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "plugin": [/* disabled */], + "plugins": ["opencode-plugin-b"] +} +EOF +check plugins-merge-comment-only-legacy "$tmp/plugins-merge-comment-only-legacy.fixture" \ + '{"$schema":"https://opencode.ai/config.json","plugins":["opencode-plugin-b"]}' 0 +grep -qF '/* disabled */' "$tmp/plugins-merge-comment-only-legacy.jsonc" || { + echo "FAIL plugins-merge-comment-only-legacy: lost legacy comment" + failures=$((failures + 1)) +} + +# Case 23 (Codex P2 on PR #4644): multiline block comment with a URL on +# its final line inside the legacy array — the // in the URL is inside +# /* */, not a live line comment, so the merge succeeds. +cat >"$tmp/plugins-merge-url-comment.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "plugin": ["opencode-plugin-a" /* note +https://example.com/docs */], + "plugins": ["opencode-plugin-b"] +} +EOF +check plugins-merge-url-comment "$tmp/plugins-merge-url-comment.fixture" \ + '{"$schema":"https://opencode.ai/config.json","plugins":["opencode-plugin-a","opencode-plugin-b"]}' 0 +grep -qF 'https://example.com/docs */' "$tmp/plugins-merge-url-comment.jsonc" || { + echo "FAIL plugins-merge-url-comment: lost block comment" + failures=$((failures + 1)) +} + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1