diff --git a/.gitattributes b/.gitattributes index 0cf88d34..d996d049 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -settings.json filter=settings +/settings.json filter=settings diff --git a/.gitignore b/.gitignore index a95dd537..ea57c2a8 100644 --- a/.gitignore +++ b/.gitignore @@ -67,8 +67,8 @@ !bin/score-outcomes !bin/score_outcomes/ !bin/score_outcomes/*.py -!bin/shell_command.py !bin/settings_filter.py +!bin/shell_command.py !bin/statusline.py !bin/subagent-rung !bin/subagent_rung/ diff --git a/AGENTS.md b/AGENTS.md index ce97866d..f50aab7a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -53,7 +53,7 @@ Decisions live in `docs/decisions.md`: ## Working rules - `routing-calls-raised.md` sits at the root of loop-claude's archive (`$LOOP_CLAUDE_TRANSCRIPTS`, default `~/.claude/loop-claude-transcripts`). Each of its lines is a call or a `Reset`, with a stamp. A call is new when it is stamped after the latest `Reset` and after the latest stamp that `todo.md` or `docs/decisions.md` → `routing:` cites. When there are new calls, file one `todo.md` item to settle them, or refresh the item already filed. Its detail quotes those calls with their stamps, and the routed-chunks table of `routing-figures.md` beside it, never the project names its other tables hold. A change to the ladder, its steps or the delegation rule is the human's. The chunk that settles the calls cites the latest stamp it settled in `docs/decisions.md` → `routing:`, whatever the human decided, so they are not filed again. Once a chunk that changes the ladder, its steps or the delegation rule has merged, append `- Reset` to `routing-calls-raised.md`: the figures then count only chunks finished after it, and its calls may be raised again. -- When `~/.claude/settings.json` has uncommitted changes, commit them on a branch of their own and merge them through a PR: Claude Code writes that file from any session. Read their diff first: an empty one is a model pick, which `git add settings.json` clears. When it holds a credential, such as a token in `env` or `mcpServers`, ask the human to move it out of the file, and commit only once it is gone. A token in `env` moves to the shell profile; a server in `mcpServers` moves to `~/.claude.json` with `claude mcp add --scope user`. +- When `~/.claude/settings.json` has uncommitted changes, commit them on a branch of their own and merge them through a PR: Claude Code writes that file from any session. Read their diff first with `git diff HEAD -- settings.json`, which shows staged changes too: an empty one is a model pick, which `git add settings.json` clears. When it holds a credential, such as a token in `env` or `mcpServers`, ask the human to move it out of the file, and commit only once it is gone. A token in `env` moves to the shell profile; a server in `mcpServers` moves to `~/.claude.json` with `claude mcp add --scope user`. ## Scoring run diff --git a/README.md b/README.md index 543bee77..4b589776 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ git init git remote add origin ssh://git@gitea.larvit.se:21022/lilleman/claude-config.git ``` -Then run Update's commands. +Then run Update's commands, from `git fetch` on: a new checkout has no model pick to stage. Have Python 3.11 or newer as `python3` on `PATH`, and for Rust projects `rust-analyzer` on `PATH`, which the `rust-analyzer-lsp` plugin needs. Install every plugin `settings.json` enables: @@ -60,7 +60,7 @@ Have git store `settings.json` without `"model"`, so a model you pick with `/mod git config filter.settings.clean bin/settings_filter.py ``` -After a pick, `git status` may still list `settings.json`, and `git pull` refuses to update it. `git add settings.json` clears that, with nothing to commit. A pull that changes `settings.json` then drops the model; pick it again with `/model`. +After a pick, `git status` may still list `settings.json`, and `git pull` refuses to update it. `git add settings.json` clears that, with nothing to commit. A pull that changes `settings.json` then drops the model. Pick one with `/model` on each new machine, and again after such a pull. Link `loop-claude` onto `PATH`: `ln -s ~/.claude/bin/loop-claude ~/.local/bin/loop-claude`. Run `loop-claude` only in a sandbox with no direct access to a production environment, such as a dedicated VM: every claude it starts, the interactive ones included, skips every permission prompt. diff --git a/bin/repo_checks/test_settings.py b/bin/repo_checks/test_settings.py index bd83f2ed..e769a22a 100644 --- a/bin/repo_checks/test_settings.py +++ b/bin/repo_checks/test_settings.py @@ -1,12 +1,15 @@ +import json import pathlib import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) +from settings_filter import cleaned from git_repo import git from repo_checks.test_python_floor import ROOT -from settings_filter import cleaned + +INSTALL = "git config filter.settings.clean bin/settings_filter.py" class Settings(unittest.TestCase): @@ -14,8 +17,9 @@ class Settings(unittest.TestCase): # The index, not the working file: the working file holds this machine's model in Claude Code's key order. stored = git("show", ":settings.json", cwd=str(ROOT)) self.assertIsInstance(stored, str, stored) - self.assertEqual(stored, cleaned(stored)) + self.assertNotIn("model", json.loads(stored)) + self.assertEqual(stored, cleaned(stored), f"commit settings.json with the filter on: {INSTALL}") - def test_the_readme_installs_the_filter_git_attributes_name(self): - self.assertIn("settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8")) - self.assertIn("git config filter.settings.clean bin/settings_filter.py\n", (ROOT / "README.md").read_text(encoding="utf-8")) + def test_the_readme_installs_the_filter_git_attributes_names_for_the_root_settings_only(self): + self.assertEqual("/settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8")) + self.assertIn(f"{INSTALL}\n", (ROOT / "README.md").read_text(encoding="utf-8")) diff --git a/bin/repo_checks/test_types.py b/bin/repo_checks/test_types.py index 161ab8f3..cd8bd7a4 100644 --- a/bin/repo_checks/test_types.py +++ b/bin/repo_checks/test_types.py @@ -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", "settings_filter", "-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", "-p", "reading_cost", "-p", "review_replay", "-m", "round_titles", "-p", "rule_hook", "-p", "rung_replay", "-p", "score_outcomes", "-m", "settings_filter", "-m", "shell_command", "-m", "statusline", "-p", "subagent_rung", "-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) diff --git a/bin/settings_filter.py b/bin/settings_filter.py index b1c7b1ec..4f9ab157 100755 --- a/bin/settings_filter.py +++ b/bin/settings_filter.py @@ -3,22 +3,30 @@ 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. +file without it, and neither write reaches a commit. """ import json import sys def cleaned(written: str) -> str: - """settings.json as git stores it: without "model", every key sorted.""" + """settings.json as git stores it: without "model", every key sorted. ValueError where it holds no JSON object.""" settings = json.loads(written) + if not isinstance(settings, dict): + raise ValueError("settings.json holds no JSON object") settings.pop("model", None) return json.dumps(settings, ensure_ascii=False, indent=2, sort_keys=True) + "\n" -def main() -> None: - sys.stdout.write(cleaned(sys.stdin.read())) +def main() -> int: + # On a non-zero exit git warns and stores the file as written. + try: + sys.stdout.write(cleaned(sys.stdin.read())) + except ValueError as error: + print(f"settings_filter.py: {error}", file=sys.stderr) + return 1 + return 0 if __name__ == "__main__": - main() + sys.exit(main()) diff --git a/bin/test_settings_filter.py b/bin/test_settings_filter.py index 580b7f61..0bfb633a 100644 --- a/bin/test_settings_filter.py +++ b/bin/test_settings_filter.py @@ -1,4 +1,5 @@ import json +import os import pathlib import subprocess import sys @@ -8,8 +9,11 @@ import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from settings_filter import cleaned +from test_env import isolated_env FILTER = pathlib.Path(__file__).resolve().parent / "settings_filter.py" +ROOT = FILTER.parents[1] +PICKED = json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2) class Clean(unittest.TestCase): @@ -20,36 +24,60 @@ class Clean(unittest.TestCase): def test_keeps_text_outside_ascii(self): self.assertEqual(cleaned('{"name": "Lilleman på Larv"}'), '{\n "name": "Lilleman på Larv"\n}\n') + def test_refuses_json_that_is_no_object(self): + with self.assertRaises(ValueError): + cleaned("[]") + + def test_a_broken_file_exits_1_naming_the_tool(self): + ran = subprocess.run([sys.executable, FILTER], input="{", capture_output=True, text=True) + self.assertEqual((ran.returncode, ran.stdout), (1, "")) + self.assertIn("settings_filter.py: ", ran.stderr) + class Checkout(unittest.TestCase): - def test_a_model_pick_in_claude_codes_key_order_leaves_nothing_to_commit_and_outlasts_an_update(self): - with tempfile.TemporaryDirectory() as repo: - def git(*args): - return subprocess.run(["git", "-C", repo, "-c", "user.name=t", "-c", "user.email=t@t", *args], capture_output=True, check=True, text=True).stdout + """A repo as README → Install leaves it, at tag v1, and v2, which leaves settings.json alone.""" - 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("config", "filter.settings.clean", "bin/settings_filter.py") - settings = pathlib.Path(repo) / "settings.json" - settings.write_text(cleaned('{"env": {}, "theme": "dark"}')) - git("add", ".") - git("commit", "-qm", "settings") - git("tag", "v1") - (pathlib.Path(repo) / "other").write_text("") - git("add", "other") - git("commit", "-qm", "a tag that leaves settings.json alone") - git("tag", "v2") - git("checkout", "-q", "v1") - picked = json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2) - settings.write_text(picked) - self.assertEqual(git("diff"), "") - # README → Update: git status lists a file whose size changed until it is staged; staging adds no change, and keeps the model through the move. - git("add", "settings.json") - self.assertEqual(git("status", "--porcelain"), "") - git("checkout", "-q", "-f", "v2") - self.assertEqual(settings.read_text(), picked) + def setUp(self): + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.repo = pathlib.Path(temp.name) + self.env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1"} + self.git("init", "-q") + (self.repo / "bin").mkdir() + (self.repo / "bin" / "settings_filter.py").symlink_to(FILTER) + (self.repo / ".gitattributes").write_text((ROOT / ".gitattributes").read_text()) + self.git("config", "filter.settings.clean", "bin/settings_filter.py") + self.settings = self.repo / "settings.json" + self.settings.write_text(cleaned('{"env": {}, "theme": "dark"}')) + (self.repo / ".claude").mkdir() + (self.repo / ".claude" / "settings.json").write_text('{\n "model": "claude-opus-5-5"\n}\n') + self.git("add", ".") + self.git("commit", "-qm", "settings") + self.git("tag", "v1") + (self.repo / "other").write_text("") + self.git("add", "other") + self.git("commit", "-qm", "a tag that leaves settings.json alone") + self.git("tag", "v2") + self.git("checkout", "-q", "v1") + + def git(self, *args): + return subprocess.run(["git", "-C", self.repo, "-c", "user.name=t", "-c", "user.email=t@t", *args], capture_output=True, check=True, env=self.env, text=True).stdout + + def test_a_model_pick_in_claude_codes_key_order_leaves_nothing_to_commit(self): + self.settings.write_text(PICKED) + self.assertEqual(self.git("diff"), "") + # git status lists a file whose size changed until it is staged; staging adds no change. + self.git("add", "settings.json") + self.assertEqual(self.git("status", "--porcelain"), "") + + def test_a_staged_pick_outlasts_an_update_to_a_tag_that_leaves_settings_json_alone(self): + self.settings.write_text(PICKED) + self.git("add", "settings.json") + self.git("checkout", "-q", "-f", "v2") + self.assertEqual(self.settings.read_text(), PICKED) + + def test_the_repos_own_harness_settings_keep_their_model(self): + self.assertEqual(self.git("show", ":.claude/settings.json"), '{\n "model": "claude-opus-5-5"\n}\n') if __name__ == "__main__":