From c3fb054e4d7472bb481d47c0e5eaf3e8f21f44ef Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 00:05:47 -0600 Subject: [PATCH] fix(opencode-config-normalize): match actual block-comment opener in last-entry separator scan (#4770) delete_top_level_entry located the preceding block comment with a plain backward rfind('/*'), which selects an inner literal /* inside the comment body (e.g. /* example syntax: /* text */) instead of the real opening delimiter, failing closed with exit 1 on a previously supported commented layout. Use a forward lexical scan for the comment closing exactly at the scan point, so string contents and // comments never open a block comment. Also add the regression case and move the suite failure gate to the end of normalize_test.sh so every case affects the exit status. --- pkgs/opencode-config-normalize/normalize.sh | 57 ++++++++++++++++++- .../normalize_test.sh | 32 +++++++++-- 2 files changed, 84 insertions(+), 5 deletions(-) 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 a6e5baae6..409cb892c 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -500,10 +500,6 @@ grep -qF 'https://example.com/docs */' "$tmp/plugins-merge-url-comment.jsonc" || failures=$((failures + 1)) } -if [ "$failures" -ne 0 ]; then - echo "normalize_test: $failures failure(s)" - exit 1 -fi # 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 @@ -522,4 +518,32 @@ 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 +fi + echo "normalize_test: all cases passed"