Give settings.json's model one writer per machine, and sort its keys

This commit is contained in:
2026-10-21 21:26:40 +02:00
parent 4fbe529cfe
commit 7c3800337a
11 changed files with 127 additions and 40 deletions
+1 -1
View File
@@ -15,5 +15,5 @@ class Types(unittest.TestCase):
self.assertIsNotNone(floor, "README.md's Install names `Python 3.N or newer`")
if shutil.which("uv") is None:
self.fail("uv on PATH fetches mypy; see README.md's Test line")
ran = subprocess.run(["uv", "tool", "run", "--python", "%d.%d" % floor, "mypy==2.3.1", "--cache-dir=/dev/null", "--check-untyped-defs", "--disallow-untyped-defs", "--exclude", "/test_", "-p", "chunk_preflight", "-p", "claude_code", "-m", "comprehension_history", "-m", "git_repo", "-p", "judge_replay", "-p", "larv_review", "-p", "loop_claude", "-m", "markdown_text", "-p", "metric_bench", "-m", "project_key", "-m", "rank_todo", "-m", "round_titles", "-p", "reading_cost", "-p", "review_replay", "-p", "rule_hook", "-p", "rung_replay", "-p", "score_outcomes", "-p", "subagent_rung", "-m", "shell_command", "-m", "statusline", "-m", "todo_table", "-p", "transcript_archive", "-m", "utc_stamp"], capture_output=True, cwd=ROOT / "bin", text=True)
ran = subprocess.run(["uv", "tool", "run", "--python", "%d.%d" % floor, "mypy==2.3.1", "--cache-dir=/dev/null", "--check-untyped-defs", "--disallow-untyped-defs", "--exclude", "/test_", "-p", "chunk_preflight", "-p", "claude_code", "-m", "comprehension_history", "-m", "git_repo", "-p", "judge_replay", "-p", "larv_review", "-p", "loop_claude", "-m", "markdown_text", "-p", "metric_bench", "-m", "project_key", "-m", "rank_todo", "-m", "round_titles", "-p", "reading_cost", "-p", "review_replay", "-p", "rule_hook", "-p", "rung_replay", "-p", "score_outcomes", "-p", "subagent_rung", "-m", "settings_filter", "-m", "shell_command", "-m", "statusline", "-m", "todo_table", "-p", "transcript_archive", "-m", "utc_stamp"], capture_output=True, cwd=ROOT / "bin", text=True)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
"""Git's clean filter for settings.json: drop "model" and sort every key.
Claude Code rewrites settings.json, in its own key order, whenever a model is
picked or a plugin installed. Each machine owns its model, so git stores the
file without it, and neither write shows as a change.
"""
import json
import sys
def main() -> None:
settings = json.load(sys.stdin)
settings.pop("model", None)
json.dump(settings, sys.stdout, ensure_ascii=False, indent=2, sort_keys=True)
sys.stdout.write("\n")
if __name__ == "__main__":
main()
+55
View File
@@ -0,0 +1,55 @@
import json
import pathlib
import subprocess
import sys
import tempfile
import unittest
BIN = pathlib.Path(__file__).resolve().parent
ROOT = BIN.parent
FILTER = BIN / "settings_filter.py"
INSTALL = "git config filter.settings.clean bin/settings_filter.py"
def cleaned(text):
return subprocess.run([sys.executable, FILTER], input=text, capture_output=True, check=True, text=True).stdout
class Clean(unittest.TestCase):
def test_drops_the_model_and_sorts_every_key(self):
written = '{"theme": "dark", "model": "opus[1m]", "env": {"B": "1", "A": "2"}}'
self.assertEqual(cleaned(written), '{\n "env": {\n "A": "2",\n "B": "1"\n },\n "theme": "dark"\n}\n')
def test_the_tracked_settings_hold_no_model_and_sorted_keys(self):
tracked = (ROOT / "settings.json").read_text(encoding="utf-8")
self.assertEqual(tracked, cleaned(tracked))
def test_the_readme_installs_the_filter_git_attributes_name(self):
self.assertIn(f"{INSTALL}\n", (ROOT / "README.md").read_text(encoding="utf-8"))
self.assertIn("settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8"))
class Checkout(unittest.TestCase):
def test_a_model_pick_and_claude_codes_key_order_leave_nothing_to_commit(self):
with tempfile.TemporaryDirectory() as repo:
def git(*args):
return subprocess.run(["git", "-C", repo, *args], capture_output=True, check=True, text=True).stdout
git("init", "-q")
(pathlib.Path(repo) / "bin").mkdir()
(pathlib.Path(repo) / "bin" / "settings_filter.py").symlink_to(FILTER)
(pathlib.Path(repo) / ".gitattributes").write_text("settings.json filter=settings\n")
git(*INSTALL.split()[1:])
settings = pathlib.Path(repo) / "settings.json"
settings.write_text(cleaned('{"env": {}, "theme": "dark"}'))
git("add", ".")
git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "settings")
settings.write_text(json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2))
self.assertEqual(git("diff"), "")
# git status lists a file whose size changed until it is staged; staging adds no change.
git("add", "settings.json")
self.assertEqual(git("status", "--porcelain"), "")
if __name__ == "__main__":
unittest.main()