From e2dfe5d4c35d350bad888accd6ae8960311865c6 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 16:30:35 -0600 Subject: [PATCH 1/2] fix(opencode-config-normalize): locator-based agent->agents fold (#4612) --- pkgs/opencode-config-normalize/normalize.sh | 43 +++++--- .../normalize_test.sh | 102 +++++++++++++++++- 2 files changed, 126 insertions(+), 19 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 91768746eb..72e0626629 100755 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -334,27 +334,40 @@ if 'agent' in doc: out = out[:at] + '"agents"' + out[at + len('"agent"'):] changed = True elif merged != agents: + # Splice the merged map into the live `agents` value span via + # the string/comment-aware locators (#4612): the old regex + # splice demanded a newline + two-space closing brace and took + # the first textual match, so inline, single-line, other-indent + # and comment-lookalike documents exited 1 or never converged. + span = find_value_span(out, 'agents') + if span is None: + sys.exit(1) # cannot splice: leave the file untouched + vs, ve = span + at = find_top_level_key(out, 'agents') + line_start = out.rfind('\n', 0, at) + 1 + indent = out[line_start:at] + if indent.strip(): + indent = ' ' new_agents = json.dumps(merged, indent=2) new_agents = '\n'.join( - (' ' + ln if i else ln) for i, ln in enumerate(new_agents.split('\n')) + (indent + ln if i else ln) + for i, ln in enumerate(new_agents.split('\n')) ) - out, n = re.subn( - r'"agents"(\s*):\s*\{.*?\n \}', - lambda m: '"agents"' + m.group(1) + ': ' + new_agents, - out, count=1, flags=re.S, - ) - if not n: - sys.exit(1) # cannot splice: leave the file untouched + out = out[:vs] + new_agents + out[ve:] changed = True - out, n = re.subn(r'\n\s*"agent"\s*:\s*\{.*?\n \},?', '', out, count=1, flags=re.S) - if not n: - sys.exit(1) + out = delete_top_level_entry(out, 'agent') + if out is None: + sys.exit(1) # cannot drop the v1 block: refuse the write else: # Every `agent` entry is already present in `agents` verbatim, - # so dropping the duplicate block cannot lose anything. If the - # drop shape misses, verification below refuses the write. - out, n = re.subn(r'\n\s*"agent"\s*:\s*\{.*?\n \},?', '', out, count=1, flags=re.S) - changed = changed or bool(n) + # so dropping the duplicate block cannot lose anything. Drop it + # via the locator, whatever its layout; if it cannot be located + # the write is refused (exit 1, file byte-identical) instead of + # silently leaving the v1 block in place forever (#4612). + out = delete_top_level_entry(out, 'agent') + if out is None: + sys.exit(1) + changed = True if not changed: sys.exit(0) diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 69652d8544..2b2c22b31e 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -198,9 +198,11 @@ cmp -s "$tmp/unparseable.fixture" "$tmp/unparseable.copy" || { failures=$((failures + 1)) } -# Case 6: agents shape the fold splice cannot recognise (4-space -# indent) with a v1 `agent` block also present — the file must be left -# byte-identical with exit 1, never stripped. +# Case 6: agents in a 4-space indent with a v1 `agent` block also +# present (#4612) — the locator-based fold splices whatever layout the +# value span has, so this normalises like the 2-space merge (Case 3). +# (Before #4612 the layout-bound regex splice could not recognise this +# shape and the case asserted the fail-closed exit 1 instead.) cat >"$tmp/unrecognised.fixture" <<'EOF' { "$schema": "https://opencode.ai/config.json", @@ -212,7 +214,8 @@ cat >"$tmp/unrecognised.fixture" <<'EOF' } } EOF -check unrecognised "$tmp/unrecognised.fixture" '' 1 +check unrecognised "$tmp/unrecognised.fixture" \ + '{"$schema":"https://opencode.ai/config.json","agents":{"build":{"model":"anthropic/claude-sonnet-4-5"},"plan":{"model":"anthropic/claude-haiku-4-5"}}}' 0 # Case 7: inline // comments (#3179) — the old probe stripper removed # only full-line comments, so this file failed to parse and every @@ -310,6 +313,97 @@ 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 + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1 From 5d3396b8b780922d74a60f13577317bffae750c5 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 17:02:22 -0600 Subject: [PATCH 2/2] fix(opencode-config-normalize): comment-aware separator scan in delete_top_level_entry (#4612) Codex P2 on PR #4625: when the legacy agent entry was last and a comment sat between the preceding comma and the key, the backward scan stopped at the comment and the deletion failed (exit 1). The scan now skips block comments and string-aware line comments, and preserves the skipped comment segment in the output (a repair tool never deletes a users comment). Regression case comment-before-agent-last; normalize_test.sh 19/19 on the laptop. --- pkgs/opencode-config-normalize/normalize.sh | 55 +++++++++++++++++-- .../normalize_test.sh | 18 ++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 72e0626629..91f981166a 100755 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -248,6 +248,31 @@ def find_value_span(text, key): return (start, i) +def _line_comment_start(text, start, end): + # Index of the '//' opening a line comment within text[start:end], + # skipping string contents, or -1 when the segment holds none. + i = start + in_str = False + while i < end: + c = text[i] + if in_str: + if c == '\\' and i + 1 < end: + i += 2 + continue + if c == '"': + in_str = False + i += 1 + continue + if c == '"': + in_str = True + i += 1 + continue + if c == '/' and i + 1 < end and text[i + 1] == '/': + return i + i += 1 + return -1 + + 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. @@ -270,13 +295,31 @@ def delete_top_level_entry(text, key): while j < len(text) and text[j] in ' \t': j += 1 return text[:at] + text[j:] - # Last entry in the object: the separator comma precedes the key. + # Last entry in the object: the separator comma precedes the key, + # with only whitespace and comments between it and the key. Skip + # both while scanning back (PR #4625 review): an inline // note + # after the previous entry, or a /* */ block, must not make a + # previously supported commented layout un-deletable. The skipped + # segment is preserved in the output — a repair tool never + # deletes a user's comment (#2364) — only the comma and the entry + # go. Anything else in the gap still returns None, fail-closed. k = at - while k > 0 and text[k - 1] in ' \t\r\n': - k -= 1 - if k > 0 and text[k - 1] == ',': - return text[:k - 1] + text[ve:] - return None + while True: + while k > 0 and text[k - 1] in ' \t\r\n': + k -= 1 + 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) + if start < 0: + return None + k = start + continue + line_start = text.rfind('\n', 0, k) + 1 + comment_at = _line_comment_start(text, line_start, k) + if comment_at < 0 or comment_at >= k: + return None + k = comment_at # plugin -> plugins. When `plugins` is absent this is a bare key rename diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 2b2c22b31e..92fb916383 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -408,4 +408,22 @@ 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 +# 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 + echo "normalize_test: all cases passed"