Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions pkgs/opencode-config-normalize/normalize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,81 @@ 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)

# agents.build -> agent.build, then drop the now-empty agents block
# 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
# landed in an `agent` block, and the full-document verification before the
# write below proves no entry was lost; if the splice/insert cannot be
# done, exit 1 with the file left byte-identical — a repair tool must
# never be a data-loss path (#2364).
if 'agents' in doc:
agents = doc.get('agents') or {}
has_agent = 'agent' in doc
agent = doc.get('agent') or {}
merged = {**agents, **agent}
if merged != agent:
new_agent = json.dumps(merged, indent=2)
new_agent = '\n'.join(
(' ' + ln if i else ln) for i, ln in enumerate(new_agent.split('\n'))
)
out, n = re.subn(
r'"agent"(\s*):\s*\{.*?\n \}',
lambda m: '"agent"' + m.group(1) + ': ' + new_agent,
out, count=1, flags=re.S,
)
if has_agent:
out, n = re.subn(
r'"agent"(\s*):\s*\{.*?\n \}',
lambda m: '"agent"' + m.group(1) + ': ' + new_agent,
out, count=1, flags=re.S,
)
if not n:
sys.exit(1) # cannot splice: leave the file untouched
changed = True
out, n = re.subn(r'\n\s*"agents"\s*:\s*\{.*?\n \},?', '', out, count=1, flags=re.S)
if not n:
sys.exit(1)
else:
# No `agent` block to splice into: replace the `agents` block
# itself with the merged `agent` block, keeping a trailing
# comma when the `agents` block had one.
def _insert(m):
comma = ',' if m.group(0).rstrip().endswith(',') else ''
return '"agent"' + m.group(1) + ': ' + new_agent + comma
out, n = re.subn(
r'"agents"(\s*):\s*\{.*?\n \},?',
_insert,
out, count=1, flags=re.S,
)
if not n:
sys.exit(1) # unrecognised agents shape: leave untouched
changed = True
else:
# Every agents entry is already present in `agent` 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*"agents"\s*:\s*\{.*?\n \},?', '', out, count=1, flags=re.S)
changed = changed or bool(n)
out, n = re.subn(r'\n\s*"agents"\s*:\s*\{.*?\n \},?', '', out, count=1, flags=re.S)
changed = changed or bool(n)

if not changed:
sys.exit(0)

# Never write something we cannot parse.
# Never write something we cannot parse — and never write a result whose
# 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)
try:
json.loads(verify)
parsed = json.loads(verify)
except Exception:
sys.exit(1)

expected = dict(doc)
if 'plugins' in expected and 'plugin' not in doc:
expected['plugin'] = expected.pop('plugins')
if 'agents' in expected:
merged_agent = {**(expected.get('agents') or {}), **(expected.get('agent') or {})}
del expected['agents']
if merged_agent or 'agent' in doc:
expected['agent'] = merged_agent
if parsed != expected:
sys.exit(1)

tmp = path.with_suffix(path.suffix + '.normalize-tmp')
tmp.write_text(out)
tmp.replace(path)
Expand Down
155 changes: 155 additions & 0 deletions pkgs/opencode-config-normalize/normalize_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env bash
# Regression fixtures for normalize.sh (#2364).
#
# Each case runs the real script against a fixture config in a temp dir
# and asserts the FULL parsed document afterwards (not substrings), the
# exit code, and idempotence (a second run is a byte-identical no-op).
# The agents-only case is the data-loss regression: the script used to
# drop every agent definition while reporting "repaired".
set -uo pipefail

here="$(cd "$(dirname "$0")" && pwd)"
script="$here/normalize.sh"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
failures=0

# parsed_doc <file> — print the JSONC-stripped parsed document as
# canonical JSON (sorted keys) for comparison.
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))
PY
}

# check <name> <fixture> <expected-json> <expected-exit>
# Runs the script on a copy of <fixture>, asserts exit code and full
# parsed document, then asserts a second run changes nothing.
check() {
local name="$1" fixture="$2" expected="$3" want_exit="$4"
local cfg="$tmp/$name.jsonc"
cp "$fixture" "$cfg"
local before after_first
before="$(sha256sum "$cfg" | cut -d' ' -f1)"
OPENCODE_CONFIG="$cfg" bash "$script" 2>/dev/null
local got_exit=$?
if [ "$got_exit" -ne "$want_exit" ]; then
echo "FAIL $name: exit $got_exit, want $want_exit"; failures=$((failures+1)); return
fi
if [ "$want_exit" -ne 0 ]; then
# Failure contract: the file is left byte-identical.
after_first="$(sha256sum "$cfg" | cut -d' ' -f1)"
if [ "$before" != "$after_first" ]; then
echo "FAIL $name: file modified despite exit $want_exit"; failures=$((failures+1)); return
fi
echo "PASS $name"; return
fi
if [ "$expected" = "__UNPARSEABLE__" ]; then
if [ "$(sha256sum "$cfg" | cut -d' ' -f1)" != "$before" ]; then
echo "FAIL $name: unparseable file was modified"; failures=$((failures+1)); return
fi
echo "PASS $name"; return
fi
local got want
got="$(parsed_doc "$cfg")"
want="$(python3 -c 'import json,sys; print(json.dumps(json.loads(sys.argv[1]), sort_keys=True))' "$expected")"
if [ "$got" != "$want" ]; then
echo "FAIL $name: parsed document mismatch"
echo " got: $got"
echo " want: $want"
failures=$((failures+1)); return
fi
# Idempotence: second run is a byte-identical no-op with exit 0.
after_first="$(sha256sum "$cfg" | cut -d' ' -f1)"
OPENCODE_CONFIG="$cfg" bash "$script" 2>/dev/null
if [ $? -ne 0 ] || [ "$(sha256sum "$cfg" | cut -d' ' -f1)" != "$after_first" ]; then
echo "FAIL $name: second run was not a byte-identical no-op"; failures=$((failures+1)); return
fi
echo "PASS $name"
}

# Case 1: agents-only (2-space pretty) — the #2364 data-loss shape.
cat > "$tmp/agents-only.fixture" <<'EOF'
{
"$schema": "https://opencode.ai/config.json",
"agents": {
"build": { "model": "anthropic/claude-sonnet-4-5" },
"plan": { "model": "anthropic/claude-haiku-4-5" }
},
"plugins": ["opencode-plugin-x"]
}
EOF
check agents-only "$tmp/agents-only.fixture" \
'{"$schema":"https://opencode.ai/config.json","agent":{"build":{"model":"anthropic/claude-sonnet-4-5"},"plan":{"model":"anthropic/claude-haiku-4-5"}},"plugin":["opencode-plugin-x"]}' 0

# Case 2: agents-only as the last key (no trailing comma after the block).
cat > "$tmp/agents-last.fixture" <<'EOF'
{
"$schema": "https://opencode.ai/config.json",
"agents": {
"build": { "model": "anthropic/claude-sonnet-4-5" }
}
}
EOF
check agents-last "$tmp/agents-last.fixture" \
'{"$schema":"https://opencode.ai/config.json","agent":{"build":{"model":"anthropic/claude-sonnet-4-5"}}}' 0

# Case 3: agent + agents merge — existing agent wins per key.
cat > "$tmp/merge.fixture" <<'EOF'
{
"$schema": "https://opencode.ai/config.json",
"agent": {
"build": { "model": "existing/wins" },
"scout": { "model": "anthropic/claude-haiku-4-5" }
},
"agents": {
"build": { "model": "v2/loses" },
"plan": { "model": "anthropic/claude-sonnet-4-5" }
}
}
EOF
check merge "$tmp/merge.fixture" \
'{"$schema":"https://opencode.ai/config.json","agent":{"build":{"model":"existing/wins"},"scout":{"model":"anthropic/claude-haiku-4-5"},"plan":{"model":"anthropic/claude-sonnet-4-5"}}}' 0

# Case 4: plugins-only rename, nothing else touched.
cat > "$tmp/plugins-only.fixture" <<'EOF'
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["opencode-plugin-x", "opencode-plugin-y"]
}
EOF
check plugins-only "$tmp/plugins-only.fixture" \
'{"$schema":"https://opencode.ai/config.json","plugin":["opencode-plugin-x","opencode-plugin-y"]}' 0

# Case 5: unparseable file is left untouched, exit 0.
cat > "$tmp/unparseable.fixture" <<'EOF'
{ this is not json at all
EOF
check unparseable "$tmp/unparseable.fixture" '__UNPARSEABLE__' 0
# (exit-0 + byte-identical is asserted inside check via the failure path
# only for non-zero exits; verify the bytes explicitly here too)
cp "$tmp/unparseable.fixture" "$tmp/unparseable.copy"
OPENCODE_CONFIG="$tmp/unparseable.copy" bash "$script" 2>/dev/null
cmp -s "$tmp/unparseable.fixture" "$tmp/unparseable.copy" || { echo "FAIL unparseable-bytes"; failures=$((failures+1)); }

# Case 6: agents shape the splice cannot recognise (4-space indent) —
# the file must be left byte-identical with exit 1, never stripped.
cat > "$tmp/unrecognised.fixture" <<'EOF'
{
"$schema": "https://opencode.ai/config.json",
"agents": {
"build": { "model": "anthropic/claude-sonnet-4-5" }
}
}
EOF
check unrecognised "$tmp/unrecognised.fixture" '' 1

if [ "$failures" -ne 0 ]; then
echo "normalize_test: $failures failure(s)"
exit 1
fi
echo "normalize_test: all cases passed"