From 8d238d2e9e60c926d250b53333142551c2b63c13 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 04:06:39 -0600 Subject: [PATCH 1/3] fix(opencode-config-normalize): parse inline and block JSONC comments (#3179) The probe/verify JSONC->JSON conversion stripped only full-line // comments, so any inline // or /* */ comment made json.loads fail and the script exited 0 with every repair silently skipped. Replace the regex stripper with a string-aware jsonc_to_json scanner that removes line/block comments and trailing commas only outside string literals, so //, /*, ,} and ,] inside string values survive verbatim. The repaired output is still spliced from the original text, preserving user comments. Adds inline-comment, block-comment, and string-lookalike fixtures to normalize_test.sh (full parsed document + idempotence assertions); the test parsed_doc helper uses the same string-aware strip. --- pkgs/opencode-config-normalize/normalize.sh | 80 +++++++++++++++-- .../normalize_test.sh | 90 ++++++++++++++++++- 2 files changed, 162 insertions(+), 8 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 115c0e610a..034fe234d6 100644 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -32,9 +32,80 @@ import json, re, sys, pathlib path = pathlib.Path(sys.argv[1]) raw = path.read_text() -# Cheap JSONC -> JSON for the membership test only; we never write this back. -probe = re.sub(r'^\s*//.*$', '', raw, flags=re.M) -probe = re.sub(r',(\s*[}\]])', r'\1', probe) + +def jsonc_to_json(text): + # String-aware JSONC -> JSON (#3179): strip // line comments and + # /* */ block comments plus trailing commas, but never touch the + # contents of string literals — "//", "/*", ",}" and ",]" inside a + # string value are data, not comments or trailing commas. Used for + # the membership probe and the pre-write verification only; the + # repaired output is still spliced from the original text, so + # comments in the user's file are preserved. + out = [] + i, n = 0, len(text) + in_str = False + while i < n: + c = text[i] + if in_str: + out.append(c) + if c == '\\' and i + 1 < n: + out.append(text[i + 1]) + i += 2 + continue + if c == '"': + in_str = False + i += 1 + continue + if c == '"': + in_str = True + out.append(c) + i += 1 + continue + if c == '/' and text[i + 1:i + 2] == '/': + while i < n and text[i] != '\n': + i += 1 + continue + if c == '/' and text[i + 1:i + 2] == '*': + end = text.find('*/', i + 2) + i = n if end < 0 else end + 2 + out.append(' ') + continue + out.append(c) + i += 1 + s = ''.join(out) + out = [] + i, n = 0, len(s) + in_str = False + while i < n: + c = s[i] + if in_str: + out.append(c) + if c == '\\' and i + 1 < n: + out.append(s[i + 1]) + i += 2 + continue + if c == '"': + in_str = False + i += 1 + continue + if c == '"': + in_str = True + out.append(c) + i += 1 + continue + if c == ',': + j = i + 1 + while j < n and s[j] in ' \t\r\n': + j += 1 + if j < n and s[j] in '}]': + i += 1 + continue # trailing comma: drop it + out.append(c) + i += 1 + return ''.join(out) + +# JSONC -> JSON for the membership test only; we never write this back. +probe = jsonc_to_json(raw) try: doc = json.loads(probe) except Exception: @@ -105,8 +176,7 @@ if not changed: # parsed document differs from the intended one. The intended document is # `doc` with the renames/folds above applied; comparing the whole document # is what proves no `agents` entry was dropped on the floor (#2364). -verify = re.sub(r'^\s*//.*$', '', out, flags=re.M) -verify = re.sub(r',(\s*[}\]])', r'\1', verify) +verify = jsonc_to_json(out) try: parsed = json.loads(verify) except Exception: diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index a8ac68cb71..9f922eca17 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -20,9 +20,54 @@ parsed_doc() { python3 - "$1" <<'PY' import json, re, sys, pathlib raw = pathlib.Path(sys.argv[1]).read_text() -probe = re.sub(r'^\s*//.*$', '', raw, flags=re.M) -probe = re.sub(r',(\s*[}\]])', r'\1', probe) -print(json.dumps(json.loads(probe), sort_keys=True)) +# String-aware JSONC strip (mirrors normalize.sh jsonc_to_json, #3179): +# comments and trailing commas are removed only outside string literals. +out = [] +i, n = 0, len(raw) +in_str = False +while i < n: + c = raw[i] + if in_str: + out.append(c) + if c == '\\' and i + 1 < n: + out.append(raw[i + 1]); i += 2; continue + if c == '"': + in_str = False + i += 1; continue + if c == '"': + in_str = True; out.append(c); i += 1; continue + if c == '/' and raw[i + 1:i + 2] == '/': + while i < n and raw[i] != '\n': + i += 1 + continue + if c == '/' and raw[i + 1:i + 2] == '*': + end = raw.find('*/', i + 2) + i = n if end < 0 else end + 2 + out.append(' '); continue + out.append(c); i += 1 +s = ''.join(out) +out = [] +i, n = 0, len(s) +in_str = False +while i < n: + c = s[i] + if in_str: + out.append(c) + if c == '\\' and i + 1 < n: + out.append(s[i + 1]); i += 2; continue + if c == '"': + in_str = False + i += 1; continue + if c == '"': + in_str = True; out.append(c); i += 1; continue + if c == ',': + j = i + 1 + while j < n and s[j] in ' \t\r\n': + j += 1 + if j < n and s[j] in '}]': + i += 1; continue + out.append(c); i += 1 +print(json.dumps(json.loads(''.join(out)), sort_keys=True)) PY } @@ -148,6 +193,45 @@ cat > "$tmp/unrecognised.fixture" <<'EOF' EOF check unrecognised "$tmp/unrecognised.fixture" '' 1 + +# Case 7: inline // comments (#3179) — the old probe stripper removed +# only full-line comments, so this file failed to parse and every +# repair was silently skipped. Must repair exactly like its +# comment-free twin. +cat > "$tmp/inline-comments.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", // schema URL + "plugins": ["opencode-plugin-x"], // v2 spelling, must rename + "model": "anthropic/claude-sonnet-4-5" // trailing note +} +EOF +check inline-comments "$tmp/inline-comments.fixture" \ + '{"$schema":"https://opencode.ai/config.json","model":"anthropic/claude-sonnet-4-5","plugin":["opencode-plugin-x"]}' 0 + +# Case 8: /* */ block comments (#3179), standalone and inline. +cat > "$tmp/block-comments.fixture" <<'EOF' +{ + /* written by OpenChamber 2.x */ + "plugins": ["opencode-plugin-x", /* inline block */ "opencode-plugin-y"] +} +EOF +check block-comments "$tmp/block-comments.fixture" \ + '{"plugin":["opencode-plugin-x","opencode-plugin-y"]}' 0 + +# Case 9: string lookalikes (#3179) — comment markers and ,} / ,] +# sequences inside string values are data and must survive verbatim, +# while a real trailing comma is still tolerated. +cat > "$tmp/string-lookalikes.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "plugins": ["opencode-plugin-x"], + "note": "docs at https://example.com/a//b, markers /* not a comment */ and ,} ,] stay", + "list": [1, 2,] +} +EOF +check string-lookalikes "$tmp/string-lookalikes.fixture" \ + '{"$schema":"https://opencode.ai/config.json","list":[1,2],"note":"docs at https://example.com/a//b, markers /* not a comment */ and ,} ,] stay","plugin":["opencode-plugin-x"]}' 0 + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1 From 64758c0391bd9335b3dba1dc1ad30b171cfdc5a2 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 04:48:51 -0600 Subject: [PATCH 2/3] fix(opencode-config-normalize): reject unterminated block comments; rename only the live plugins key CodeRabbit review on PR #4312: (1) jsonc_to_json dropped all text after an unterminated /* and both the probe and the pre-write verification used it, so a file ending in an open comment was repaired and written while still invalid; now it raises and the probe treats it as unparseable (exit 0, byte-identical). (2) The plugins rename used a raw-text re.subn that could select "plugins": inside an earlier comment, leaving the live key unrepaired; the rename now locates the live top-level key with a comment/string-aware scan. Regression fixtures for both. --- pkgs/opencode-config-normalize/normalize.sh | 69 ++++++++++++++++--- .../normalize_test.sh | 26 +++++++ 2 files changed, 87 insertions(+), 8 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 034fe234d6..8f8b5490f9 100644 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -67,7 +67,9 @@ def jsonc_to_json(text): continue if c == '/' and text[i + 1:i + 2] == '*': end = text.find('*/', i + 2) - i = n if end < 0 else end + 2 + if end < 0: + raise ValueError('unterminated block comment') + i = end + 2 out.append(' ') continue out.append(c) @@ -105,19 +107,71 @@ def jsonc_to_json(text): return ''.join(out) # JSONC -> JSON for the membership test only; we never write this back. -probe = jsonc_to_json(raw) try: - doc = json.loads(probe) + doc = json.loads(jsonc_to_json(raw)) except Exception: sys.exit(0) # unparseable: leave it alone rather than risk mangling it changed = False out = raw -# plugins -> plugin +def find_top_level_key(text, key): + # Index of the opening quote of the live top-level "key": token in + # text, skipping string contents and // /* */ comments (PR #4312 + # review): a "key": lookalike inside a comment or a nested object + # must never be selected for a splice. Returns -1 when absent. + i, n = 0, len(text) + depth = 0 + in_str = False + str_start = -1 + while i < n: + c = text[i] + if in_str: + if c == '\\' and i + 1 < n: + i += 2 + continue + if c == '"': + in_str = False + if depth == 1 and text[str_start + 1:i] == key: + j = i + 1 + while j < n and text[j] in ' \t\r\n': + j += 1 + if j < n and text[j] == ':': + return str_start + i += 1 + continue + if c == '"': + in_str = True + str_start = i + i += 1 + continue + if c == '/' and text[i + 1:i + 2] == '/': + while i < n and text[i] != '\n': + i += 1 + continue + if c == '/' and text[i + 1:i + 2] == '*': + end = text.find('*/', i + 2) + if end < 0: + return -1 + i = end + 2 + continue + if c in '{[': + depth += 1 + elif c in '}]': + depth -= 1 + i += 1 + return -1 + + +# plugins -> plugin: rename the live top-level key only. A raw-text +# search can hit "plugins": inside an earlier comment and leave the +# live key unrepaired (PR #4312 review). if 'plugins' in doc and 'plugin' not in doc: - out, n = re.subn(r'"plugins"(\s*):', r'"plugin"\1:', out, count=1) - changed = changed or bool(n) + at = find_top_level_key(out, 'plugins') + if at < 0: + sys.exit(1) # live key not locatable: leave the file untouched + out = out[:at] + '"plugin"' + out[at + len('"plugins"'):] + changed = True # agents -> agent: fold the v2 `agents` map into `agent` (existing `agent` # wins per key). The `agents` block is dropped only once the merged map has @@ -176,9 +230,8 @@ if not changed: # parsed document differs from the intended one. The intended document is # `doc` with the renames/folds above applied; comparing the whole document # is what proves no `agents` entry was dropped on the floor (#2364). -verify = jsonc_to_json(out) try: - parsed = json.loads(verify) + parsed = json.loads(jsonc_to_json(out)) except Exception: sys.exit(1) diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 9f922eca17..b43aaf238f 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -232,6 +232,32 @@ EOF check string-lookalikes "$tmp/string-lookalikes.fixture" \ '{"$schema":"https://opencode.ai/config.json","list":[1,2],"note":"docs at https://example.com/a//b, markers /* not a comment */ and ,} ,] stay","plugin":["opencode-plugin-x"]}' 0 +# Case 10: unterminated block comment (PR #4312 review) — the stripper +# used to drop everything after the open /* and then repair and write +# the file anyway, leaving a still-invalid config behind. An +# unterminated comment is unparseable: leave the file byte-identical. +cat > "$tmp/unterminated-comment.fixture" <<'EOF' +{ + "plugins": ["opencode-plugin-x"] +} +/* trailing note that never closes +EOF +check unterminated-comment "$tmp/unterminated-comment.fixture" '__UNPARSEABLE__' 0 + +# Case 11: "plugins": inside comments before the live key (PR #4312 +# review) — the raw-text rename used to select the comment text first, +# leaving the live key unrepaired and the run failed. The live key is +# the one renamed; comments are preserved. +cat > "$tmp/comment-plugins-key.fixture" <<'EOF' +{ + /* v2 spelling was "plugins": ["old"] — comment, do not touch */ + // likewise "plugins": in a line comment is not a key + "plugins": ["opencode-plugin-x"] +} +EOF +check comment-plugins-key "$tmp/comment-plugins-key.fixture" \ + '{"plugin":["opencode-plugin-x"]}' 0 + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1 From ae526c16a5d1c64a593ac8a2be3519a44855dc0b Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 14:45:39 -0600 Subject: [PATCH 3/3] Merge origin/main into queue/5653-3179-jsonc-comments main already carries every change this branch made, in the newer canonical v2 direction: the string-aware jsonc_to_json scanner, the unterminated-block-comment rejection, the comment-aware live-key locator, and fixtures 7-11 covering inline, block, string-lookalike, unterminated and comment-lookalike cases. The merge therefore resolves entirely to main's version of both files; the branch is patch-equivalent to main and adds nothing. --- .../normalize_test.sh | 313 +++++++++++++++++- 1 file changed, 307 insertions(+), 6 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 43a09aad9e..bcefc794f2 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -221,7 +221,7 @@ check unrecognised "$tmp/unrecognised.fixture" \ # only full-line comments, so this file failed to parse and every # repair was silently skipped. Must repair exactly like its # comment-free twin. -cat > "$tmp/inline-comments.fixture" <<'EOF' +cat >"$tmp/inline-comments.fixture" <<'EOF' { "$schema": "https://opencode.ai/config.json", // schema URL "plugin": ["opencode-plugin-x"], // v1 spelling, must rename @@ -232,7 +232,7 @@ check inline-comments "$tmp/inline-comments.fixture" \ '{"$schema":"https://opencode.ai/config.json","model":"anthropic/claude-sonnet-4-5","plugins":["opencode-plugin-x"]}' 0 # Case 8: /* */ block comments (#3179), standalone and inline. -cat > "$tmp/block-comments.fixture" <<'EOF' +cat >"$tmp/block-comments.fixture" <<'EOF' { /* written by a v1-era tool */ "plugin": ["opencode-plugin-x", /* inline block */ "opencode-plugin-y"] @@ -244,7 +244,7 @@ check block-comments "$tmp/block-comments.fixture" \ # Case 9: string lookalikes (#3179) — comment markers and ,} / ,] # sequences inside string values are data and must survive verbatim, # while a real trailing comma is still tolerated. -cat > "$tmp/string-lookalikes.fixture" <<'EOF' +cat >"$tmp/string-lookalikes.fixture" <<'EOF' { "$schema": "https://opencode.ai/config.json", "plugin": ["opencode-plugin-x"], @@ -259,7 +259,7 @@ check string-lookalikes "$tmp/string-lookalikes.fixture" \ # used to drop everything after the open /* and then repair and write # the file anyway, leaving a still-invalid config behind. An # unterminated comment is unparseable: leave the file byte-identical. -cat > "$tmp/unterminated-comment.fixture" <<'EOF' +cat >"$tmp/unterminated-comment.fixture" <<'EOF' { "plugin": ["opencode-plugin-x"] } @@ -268,10 +268,10 @@ EOF check unterminated-comment "$tmp/unterminated-comment.fixture" '__UNPARSEABLE__' 0 # Case 11: "plugin": inside comments before the live key (PR #4312 -# review) — a raw-text rename can select the comment text first, +# review) — a raw-text rename would select the comment text first, # leaving the live key unrepaired. The live key is the one renamed; # comments are preserved. -cat > "$tmp/comment-plugin-key.fixture" <<'EOF' +cat >"$tmp/comment-plugin-key.fixture" <<'EOF' { /* v1 spelling was "plugin": ["old"] — comment, do not touch */ // likewise "plugin": in a line comment is not a key @@ -281,6 +281,307 @@ EOF check comment-plugin-key "$tmp/comment-plugin-key.fixture" \ '{"plugins":["opencode-plugin-x"]}' 0 +# Case 12: already canonical (v2 spellings only) — the steady state. +# Must be a byte-identical no-op on the FIRST run, not just the second. +cat >"$tmp/canonical.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "agents": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + }, + "plugins": ["opencode-plugin-x"] +} +EOF +check canonical "$tmp/canonical.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"}},"plugins":["opencode-plugin-x"]}' 0 +cp "$tmp/canonical.fixture" "$tmp/canonical.copy" +OPENCODE_CONFIG="$tmp/canonical.copy" bash "$script" 2>/dev/null +cmp -s "$tmp/canonical.fixture" "$tmp/canonical.copy" || { + echo "FAIL canonical-bytes" + failures=$((failures + 1)) +} + +# Case 13: plugin + plugins both present — legacy entries first, then +# native (the fork's encoded.plugins order), and the v1 key is gone. +cat >"$tmp/plugins-merge.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "plugin": ["opencode-plugin-a"], + "plugins": ["opencode-plugin-b", ["opencode-plugin-c", {"opt": 1}]] +} +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 (#4612, probe B): `agents` inline empty object with a +# standard v1 `agent` block — the old regex splice demanded a +# newline + two-space closing brace, so this exited 1 forever. +cat >"$tmp/inline-agents.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "agent": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + }, + "agents": {} +} +EOF +check inline-agents "$tmp/inline-agents.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"}}}' 0 + +# Case 15 (#4612, probe C): `agents` on a single line with content, +# plus a v1 `agent` block adding a key — merges with exit 0. +cat >"$tmp/singleline-agents.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "agent": { "scout": { "model": "anthropic/claude-haiku-4-5" } }, + "agents": { "build": { "model": "anthropic/claude-sonnet-4-5" } } +} +EOF +check singleline-agents "$tmp/singleline-agents.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"},"scout":{"model":"anthropic/claude-haiku-4-5"}}}' 0 + +# Case 16 (#4612, probe F): an "agents": lookalike inside a // comment +# above the live keys — the fold must splice the LIVE keys and leave +# the comment byte-identical (asserted explicitly after the check). +cat >"$tmp/comment-agents-key.fixture" <<'EOF' +{ + // example only: "agents": { "ghost": { "model": "comment/lookalike" } } + "$schema": "https://opencode.ai/config.json", + "agent": { + "scout": { "model": "anthropic/claude-haiku-4-5" } + }, + "agents": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + } +} +EOF +check comment-agents-key "$tmp/comment-agents-key.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"},"scout":{"model":"anthropic/claude-haiku-4-5"}}}' 0 +cp "$tmp/comment-agents-key.fixture" "$tmp/comment-agents-key.copy" +OPENCODE_CONFIG="$tmp/comment-agents-key.copy" bash "$script" 2>/dev/null +grep -Fq '// example only: "agents": { "ghost": { "model": "comment/lookalike" } }' "$tmp/comment-agents-key.copy" || { + echo "FAIL comment-agents-key-bytes: comment line not byte-identical" + failures=$((failures + 1)) +} + +# Case 17 (#4612, probe E): fully duplicated entries (merged == +# agents) with the v1 `agent` block on ONE line — the old deletion +# regex missed this layout and silently exited 0 with the v1 block +# left in place. The block must actually be removed. +cat >"$tmp/duplicate-singleline.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "agent": { "build": { "model": "anthropic/claude-sonnet-4-5" } }, + "agents": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + } +} +EOF +check duplicate-singleline "$tmp/duplicate-singleline.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"}}}' 0 +cp "$tmp/duplicate-singleline.fixture" "$tmp/duplicate-singleline.copy" +OPENCODE_CONFIG="$tmp/duplicate-singleline.copy" bash "$script" 2>/dev/null +grep -Fq '"agent":' "$tmp/duplicate-singleline.copy" && { + echo "FAIL duplicate-singleline-bytes: v1 agent key still present" + failures=$((failures + 1)) +} + +# Case 18 (#4612): duplicated entries with the v1 `agent` block as +# the LAST entry (no trailing comma) — deletion must remove the +# preceding separator comma and leave a valid single `agents` block. +cat >"$tmp/duplicate-last.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "agents": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + }, + "agent": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + } +} +EOF +check duplicate-last "$tmp/duplicate-last.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"}}}' 0 + +# Case 19: 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 20: 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 + +# 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)) +} + +# 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)) +} + +# Case: agent as the last entry, with an inline comment between the +# preceding entry's comma and the agent key (PR #4625 Codex P2). The +# backward separator scan in delete_top_level_entry must skip the +# comment; the fold must succeed and stay idempotent. +cat >"$tmp/comment-before-agent-last.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "agents": { + "plan": { "model": "anthropic/claude-sonnet-4-5" } + }, // native agents live here + "agent": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + } +} +EOF +check comment-before-agent-last "$tmp/comment-before-agent-last.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"},"plan":{"model":"anthropic/claude-sonnet-4-5"}}}' 0 + +# Case 24 (#4770): a block comment containing a literal /* before the +# final legacy `agent` entry — the backward separator scan must match +# the comment's actual opening delimiter, not the inner literal, so +# the fold succeeds and the comment text is preserved byte-identical. +cat >"$tmp/block-comment-inner-delimiter.fixture" <<'EOF' +{ + "$schema": "https://opencode.ai/config.json", + "agents": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + }, + /* example syntax: /* text */ + "agent": { + "build": { "model": "anthropic/claude-sonnet-4-5" } + } +} +EOF +check block-comment-inner-delimiter "$tmp/block-comment-inner-delimiter.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"}}}' 0 +grep -qF '/* example syntax: /* text */' "$tmp/block-comment-inner-delimiter.jsonc" || { + echo "FAIL block-comment-inner-delimiter: comment text not preserved" + failures=$((failures + 1)) +} + +# Case 7: a repair preserves the config's original permission bits. +# (Input is the v1 `plugin` spelling — the canonical direction since +# the v2 rename — so the run performs a real rewrite.) +cat >"$tmp/mode.fixture" <<'EOF' +{ + "plugin": ["opencode-plugin-x"] +} +EOF +cp "$tmp/mode.fixture" "$tmp/mode.jsonc" +chmod 0600 "$tmp/mode.jsonc" +OPENCODE_CONFIG="$tmp/mode.jsonc" bash "$script" 2>/dev/null +if [ "$?" -ne 0 ] || [ "$(stat -c '%a' "$tmp/mode.jsonc")" != "600" ] || + [ "$(parsed_doc "$tmp/mode.jsonc")" != '{"plugins": ["opencode-plugin-x"]}' ]; then + echo "FAIL mode-preserved: repair must preserve 0600 and normalize plugins" + failures=$((failures + 1)) +else + echo "PASS mode-preserved" +fi + +# Case 8: a symlinked config is refused before writes; keep both the link +# target and its contents unchanged. +cat >"$tmp/symlink-target.jsonc" <<'EOF' +{ + "plugin": ["opencode-plugin-x"] +} +EOF +ln -s symlink-target.jsonc "$tmp/symlink.jsonc" +link_before="$(readlink "$tmp/symlink.jsonc")" +target_before="$(sha256sum "$tmp/symlink-target.jsonc" | cut -d' ' -f1)" +OPENCODE_CONFIG="$tmp/symlink.jsonc" bash "$script" 2>"$tmp/symlink.stderr" +symlink_exit=$? +target_after="$(sha256sum "$tmp/symlink-target.jsonc" | cut -d' ' -f1)" +if [ "$symlink_exit" -eq 0 ] || [ ! -L "$tmp/symlink.jsonc" ] || + [ "$(readlink "$tmp/symlink.jsonc")" != "$link_before" ] || + [ "$target_before" != "$target_after" ] || + ! grep -q 'refusing symlinked config' "$tmp/symlink.stderr"; then + echo "FAIL symlink-refused: expected a clear refusal with link and target unchanged" + failures=$((failures + 1)) +else + echo "PASS symlink-refused" +fi + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1