From aa8319a1db4508196bd9bc7006eb85cf04e73b8b Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Fri, 4 Dec 2026 19:48:53 +0000 Subject: [PATCH] feat(install): merge user settings and turn off bundled skills Copying the repo's settings.json over ~/.claude/settings.json would delete most of the operator's settings, so the installer now merges it. The merge and the new settings land together, so no overwriting installer ever deploys them. The merge runs before any other file is deployed, so an invalid settings file stops the install with nothing changed. It writes a complete temporary file and renames it, so a failure cannot truncate the user's settings. Impact: removing a setting from the repo no longer removes it from a machine that already has it. ADR: 0035 Refs: #280 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi --- README.md | 2 +- docs/spec/architecture.md | 5 +- providers/claude-code/settings.json | 8 +++ scripts/deploy-manifest.sh | 6 +- scripts/install.sh | 9 ++- scripts/merge-json.py | 60 ++++++++++++++++ tests/test-git-hooks-install.sh | 1 + tests/test-install.sh | 105 ++++++++++++++++++++++++++++ 8 files changed, 191 insertions(+), 5 deletions(-) create mode 100644 scripts/merge-json.py diff --git a/README.md b/README.md index adb9fe1..21813d6 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Install all of these before setting up. Each one is a hard dependency of a git h | Tool | Why | Install | | --- | --- | --- | | `uv` | Runs this repo's pinned apm. The root `pyproject.toml` pins `apm-cli==0.39.3` and `uv.lock` locks it (ADR-0034); the `apm-audit-ci` and `apm-pack-check-clean` pre-push hooks run it as `uv run --frozen apm` through `scripts/lib/apm.sh`, and fail below apm 0.39.2 | https://docs.astral.sh/uv/getting-started/installation/. A global `apm` is not needed and not used: a bare `apm` here may be an older one that refuses the v4 `apm.lock.yaml` | -| `python3` + PyYAML | Required by `scripts/skill-size-check.sh` (the `skill-size-check` pre-commit hook) and `scripts/check-skill-version-bump.sh` (the `check-skill-version-bump` pre-push hook), which both parse YAML frontmatter | `python3` is usually present — pre-commit is itself a Python application. `pip install pyyaml` if the hook reports PyYAML missing | +| `python3` + PyYAML | Required by `scripts/skill-size-check.sh` (the `skill-size-check` pre-commit hook) and `scripts/check-skill-version-bump.sh` (the `check-skill-version-bump` pre-push hook), which both parse YAML frontmatter. `scripts/install.sh` needs `python3` (not PyYAML) to merge the Claude Code settings | `python3` is usually present — pre-commit is itself a Python application. `pip install pyyaml` if the hook reports PyYAML missing | | `vale` | Required by the `vale-audit-prefilter-skill` / `-agent` pre-commit hooks, and by the `test-vale-wrap.sh` suite that `run-tests --strict` runs at pre-push | `brew install vale` (macOS), `snap install vale` (Linux), `choco install vale` (Windows), or https://vale.sh/docs/vale-cli/installation/ | | `claude` CLI | Required by the `validate-marketplace` pre-push hook | Claude Code | diff --git a/docs/spec/architecture.md b/docs/spec/architecture.md index b55680f..6052bb4 100644 --- a/docs/spec/architecture.md +++ b/docs/spec/architecture.md @@ -13,9 +13,10 @@ project repo (local overrides) ## Content deployment model -`scripts/install.sh` is a **deployer**, not a composer. It sources `scripts/deploy-manifest.sh` and deploys three categories: +`scripts/install.sh` is a **deployer**, not a composer, with one exception: user settings are merged, not copied. It sources `scripts/deploy-manifest.sh` and deploys four categories: -- **Files** (`DEPLOY_FILES`): `providers/claude-code/CLAUDE.md` → `~/.claude/CLAUDE.md`; `providers/claude-code/settings.json` → `~/.claude/settings.json`; `core/AGENTS.md` → `~/.agents/AGENTS.md` +- **Files** (`DEPLOY_FILES`): `providers/claude-code/CLAUDE.md` → `~/.claude/CLAUDE.md`; `core/AGENTS.md` → `~/.agents/AGENTS.md` +- **Merged JSON** (`DEPLOY_JSON_MERGES`): `providers/claude-code/settings.json` → `~/.claude/settings.json`. Settings only the user has are kept, including inside nested objects; where both set a value, the repo's wins, and arrays are replaced whole. Removing a setting from the repo does not remove it from a user's file. An existing file that is not a valid JSON object stops the install and is left untouched (ADR-0035) - **Executables** (`DEPLOY_EXECUTABLES`): `providers/claude-code/statusline-command.sh` → `~/.claude/statusline-command.sh` (with `+x`) - **Directories** (`DEPLOY_DIRS`): `core/` → `~/.claude/core/` (destination fully replaced on each deploy) diff --git a/providers/claude-code/settings.json b/providers/claude-code/settings.json index 1c56480..252acc5 100644 --- a/providers/claude-code/settings.json +++ b/providers/claude-code/settings.json @@ -1,7 +1,15 @@ { + "disableBundledSkills": true, + "env": { + "CLAUDE_CODE_DISABLE_SHIP": "1" + }, + "skillOverrides": { + "init": "off" + }, "statusLine": { "command": "bash ~/.claude/statusline-command.sh", "type": "command" }, + "syncClaudeAiSkills": false, "theme": "dark" } diff --git a/scripts/deploy-manifest.sh b/scripts/deploy-manifest.sh index 8fc971f..4f395e1 100644 --- a/scripts/deploy-manifest.sh +++ b/scripts/deploy-manifest.sh @@ -7,10 +7,14 @@ # Individual files — copied verbatim DEPLOY_FILES=( "providers/claude-code/CLAUDE.md:.claude/CLAUDE.md" - "providers/claude-code/settings.json:.claude/settings.json" "core/AGENTS.md:.agents/AGENTS.md" ) +# JSON files — merged into an existing destination; this repo's values win +DEPLOY_JSON_MERGES=( + "providers/claude-code/settings.json:.claude/settings.json" +) + # Files that also need the executable bit set DEPLOY_EXECUTABLES=( "providers/claude-code/statusline-command.sh:.claude/statusline-command.sh" diff --git a/scripts/install.sh b/scripts/install.sh index db98111..35950d0 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -7,6 +7,13 @@ source "$REPO_ROOT/scripts/deploy-manifest.sh" echo "Installing from $REPO_ROOT..." +# First, so an unmergeable file stops the install before anything is deployed. +for entry in "${DEPLOY_JSON_MERGES[@]}"; do + dest="$HOME/${entry##*:}" + mkdir -p "$(dirname "$dest")" + python3 "$REPO_ROOT/scripts/merge-json.py" "$REPO_ROOT/${entry%%:*}" "$dest" +done + for entry in "${DEPLOY_FILES[@]}"; do dest="$HOME/${entry##*:}" mkdir -p "$(dirname "$dest")" @@ -47,7 +54,7 @@ fi echo "" echo "Deployed:" -for entry in "${DEPLOY_FILES[@]}" "${DEPLOY_EXECUTABLES[@]}"; do +for entry in "${DEPLOY_FILES[@]}" "${DEPLOY_JSON_MERGES[@]}" "${DEPLOY_EXECUTABLES[@]}"; do echo " ~/${entry##*:}" done for entry in "${DEPLOY_DIRS[@]}"; do diff --git a/scripts/merge-json.py b/scripts/merge-json.py new file mode 100644 index 0000000..febf64c --- /dev/null +++ b/scripts/merge-json.py @@ -0,0 +1,60 @@ +"""Merge the JSON object in SRC into DEST. SRC wins; keys only DEST has are kept.""" + +import json +import os +import shutil +import sys +import tempfile + + +def merge(base, override): + if not (isinstance(base, dict) and isinstance(override, dict)): + return override + merged = dict(base) + for key, value in override.items(): + merged[key] = merge(base[key], value) if key in base else value + return merged + + +def merged_text(src_text, dest): + with open(dest, encoding="utf-8") as f: + existing = json.load(f) + if not isinstance(existing, dict): + raise ValueError("top level is not a JSON object") + before = json.dumps(existing, indent=2, ensure_ascii=False) + after = json.dumps(merge(existing, json.loads(src_text)), indent=2, ensure_ascii=False) + return None if after == before else after + "\n" + + +def replace_file(dest, text): + # Renaming a complete temporary file over dest means a failure never truncates it. + fd, tmp = tempfile.mkstemp(dir=os.path.dirname(dest), prefix=".merge-json.") + try: + with os.fdopen(fd, "w", encoding="utf-8") as f: + f.write(text) + f.flush() + os.fsync(f.fileno()) + if os.path.exists(dest): + shutil.copymode(dest, tmp) + os.replace(tmp, dest) + except BaseException: + os.unlink(tmp) + raise + + +def main(): + src, dest_arg = sys.argv[1:] + # Renaming over a symlink would replace the link, so write to its target. + dest = os.path.realpath(dest_arg) + with open(src, encoding="utf-8") as f: + text = f.read() + try: + if os.path.exists(dest): + text = merged_text(text, dest) + if text is not None: + replace_file(dest, text) + except (OSError, ValueError) as error: + sys.exit(f"ERROR: cannot merge settings into {dest_arg}, left unchanged: {error}") + + +main() diff --git a/tests/test-git-hooks-install.sh b/tests/test-git-hooks-install.sh index 15be37e..5f2406a 100755 --- a/tests/test-git-hooks-install.sh +++ b/tests/test-git-hooks-install.sh @@ -36,6 +36,7 @@ git -C "$TEMP_REPO" init -q cat > "$TEMP_REPO/scripts/deploy-manifest.sh" << 'EOF' # shellcheck disable=SC2034 DEPLOY_FILES=() +DEPLOY_JSON_MERGES=() DEPLOY_EXECUTABLES=() DEPLOY_DIRS=() EOF diff --git a/tests/test-install.sh b/tests/test-install.sh index 24227c7..6cfa7d3 100755 --- a/tests/test-install.sh +++ b/tests/test-install.sh @@ -74,6 +74,111 @@ else fail "idempotent: ~/.agents/AGENTS.md corrupted after second install" fi +SETTINGS_SRC="$REPO_ROOT/providers/claude-code/settings.json" + +if cmp -s "$SETTINGS_SRC" "$TEMP_HOME/.claude/settings.json"; then + pass "idempotent: settings.json still identical to source after second install" +else + fail "idempotent: settings.json changed by second install" +fi + +echo "" +echo "--- settings.json is merged into an existing file (ADR-0035) ---" + +new_home() { + local home + home="$(mktemp -d "$TEMP_HOME/merge.XXXXXX")" + mkdir -p "$home/.claude" + echo "$home" +} + +install_into() { + HOME="$1" bash "$REPO_ROOT/scripts/install.sh" 2>&1 +} + +# Asserts a Python expression over the parsed JSON file, bound to `s`. +check_json() { + local file="$1" expr="$2" name="$3" + if python3 -c 'import json, sys; s = json.load(open(sys.argv[1], encoding="utf-8")); sys.exit(not eval(sys.argv[2]))' "$file" "$expr" 2> /dev/null; then + pass "$name" + else + fail "$name" + fi +} + +check_json "$SETTINGS_SRC" \ + 's["disableBundledSkills"] is True and s["syncClaudeAiSkills"] is False and s["env"]["CLAUDE_CODE_DISABLE_SHIP"] == "1" and s["skillOverrides"]["init"] == "off"' \ + "repo settings turn off bundled and synced skills" + +MERGE_HOME="$(new_home)" +MERGED="$MERGE_HOME/.claude/settings.json" +cat > "$MERGED" << 'EOF' +{ + "model": "opus", + "theme": "light", + "permissions": {"allow": ["Bash(ls:*)"]}, + "env": {"MY_VAR": "kept", "CLAUDE_CODE_DISABLE_SHIP": "0"}, + "skillOverrides": {"other": "off", "init": "on"} +} +EOF +chmod 640 "$MERGED" +install_into "$MERGE_HOME" > /dev/null || fail "install over existing settings exited non-zero" + +check_json "$MERGED" 's["model"] == "opus" and s["permissions"] == {"allow": ["Bash(ls:*)"]}' \ + "top-level settings the repo does not set are kept" +check_json "$MERGED" 's["env"]["MY_VAR"] == "kept" and s["skillOverrides"]["other"] == "off"' \ + "nested entries the repo does not set are kept" +check_json "$MERGED" 's["theme"] == "dark" and s["env"]["CLAUDE_CODE_DISABLE_SHIP"] == "1" and s["skillOverrides"]["init"] == "off"' \ + "the repo's value wins where both set one" +check_json "$MERGED" 's["disableBundledSkills"] is True and s["syncClaudeAiSkills"] is False' \ + "settings only the repo has are added" + +if [[ "$(find "$MERGED" -perm 640)" == "$MERGED" ]]; then + pass "the existing file's permissions are kept" +else + fail "the existing file's permissions changed" +fi + +cp "$MERGED" "$MERGE_HOME/first-run.json" +install_into "$MERGE_HOME" > /dev/null || fail "second install exited non-zero" +if cmp -s "$MERGE_HOME/first-run.json" "$MERGED"; then + pass "a second run leaves the merged settings unchanged" +else + fail "a second run changed the merged settings" +fi + +for invalid in '{"model": "opus",' '["not an object"]' ''; do + BAD_HOME="$(new_home)" + BAD="$BAD_HOME/.claude/settings.json" + printf '%s' "$invalid" > "$BAD" + printf '%s' "$invalid" > "$BAD_HOME/before" + if output="$(install_into "$BAD_HOME")"; then + fail "install accepted invalid settings: '$invalid'" + elif [[ "$output" == *"$BAD"* && "$output" != *Traceback* ]]; then + pass "invalid settings exit non-zero with an error naming the file: '$invalid'" + else + fail "invalid settings gave no clear error: '$invalid'" + fi + if cmp -s "$BAD_HOME/before" "$BAD"; then + pass "invalid settings are left untouched: '$invalid'" + else + fail "invalid settings were changed: '$invalid'" + fi +done + +LINK_HOME="$(new_home)" +mkdir "$LINK_HOME/dotfiles" +echo '{"model": "opus"}' > "$LINK_HOME/dotfiles/settings.json" +ln -s ../dotfiles/settings.json "$LINK_HOME/.claude/settings.json" +install_into "$LINK_HOME" > /dev/null || fail "install through a symlink exited non-zero" +if [[ -L "$LINK_HOME/.claude/settings.json" ]]; then + pass "a symlinked settings file stays a symlink" +else + fail "a symlinked settings file was replaced by a regular file" +fi +check_json "$LINK_HOME/dotfiles/settings.json" 's["model"] == "opus" and s["theme"] == "dark"' \ + "the symlink's target receives the merge" + echo "" echo "Results: $PASS passed, $FAIL failed" [[ $FAIL -eq 0 ]]