diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 200a3e750..131f34134 100755 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -273,6 +273,61 @@ def _line_comment_start(text, start, end): return -1 +def _block_comment_start_ending_at(text, k): + # Index of the '/*' opening the block comment whose closing '*/' + # ends exactly at k (text[k-2:k] == '*/'), found by a forward + # lexical scan so a literal /* inside the comment body is never + # mistaken for the opener (#4770): a plain backward rfind('/*') + # selects the inner literal in e.g. /* example syntax: /* text */ + # and the separator scan then fails closed on a previously + # supported layout. String contents and // line comments never + # open a block comment. Returns -1 when no comment closes + # exactly at k. + opener = -1 + cur_open = -1 + in_str = False + in_line = False + i, n = 0, len(text) + while i < k: + c = text[i] + if in_line: + if c == '\n': + in_line = False + i += 1 + continue + if cur_open >= 0: + if c == '*' and text[i + 1:i + 2] == '/': + if i + 2 == k: + opener = cur_open + cur_open = -1 + 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 + i += 1 + continue + if c == '/' and text[i + 1:i + 2] == '/': + in_line = True + i += 2 + continue + if c == '/' and text[i + 1:i + 2] == '*': + cur_open = i + i += 2 + continue + i += 1 + return opener + + def delete_top_level_entry(text, key): # Remove the live top-level "key": value entry, with its comma. # Returns the new text, or None when the entry cannot be located. @@ -310,7 +365,7 @@ def delete_top_level_entry(text, key): if k > 0 and text[k - 1] == ',': return text[:k - 1] + text[k:at] + text[ve:] if k >= 2 and text[k - 2:k] == '*/': - start = text.rfind('/*', 0, k - 2) + start = _block_comment_start_ending_at(text, k) if start < 0: return None k = start diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 57c17787b..409cb892c 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -518,6 +518,29 @@ 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)) +} + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1