diff --git a/bin/claude_home/home.py b/bin/claude_home/home.py index 2001d83c..ed3ef26d 100644 --- a/bin/claude_home/home.py +++ b/bin/claude_home/home.py @@ -32,10 +32,10 @@ class Git(typing.Protocol): def __call__(self, *args: str, cwd: str | None = None) -> str | Failure: ... -def unready(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, claude: Claude, git: Git) -> str | None: - """Builds the home from clone, or refreshes it where the clone's commit or the reports folder changed since. Then why a claude cannot run in the home yet, said for the person to act on; None where one can.""" +def unready(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, own: pathlib.Path, claude: Claude, git: Git) -> str | None: + """Builds the home from clone, or refreshes it where the clone's commit or the reports folder changed since. own is the person's own Claude home, whose instructions the home keeps out. Then why a claude cannot run in the home yet, said for the person to act on; None where one can.""" try: - built = refreshed(home, clone, reports, claude, git) + built = refreshed(home, clone, reports, own, claude, git) if isinstance(built, Failure): return f"could not build the loop's Claude home, {home}: {built.reason}" status = claude(["auth", "status"]) @@ -70,7 +70,7 @@ def printed(done: subprocess.CompletedProcess[str]) -> str: return " ".join((done.stderr or done.stdout).split()) or f"it printed nothing and exited {done.returncode}" -def refreshed(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, claude: Claude, git: Git) -> Failure | None: +def refreshed(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, own: pathlib.Path, claude: Claude, git: Git) -> Failure | None: commit = git("rev-parse", "HEAD", cwd=str(clone)) if isinstance(commit, Failure): return commit @@ -83,7 +83,7 @@ def refreshed(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, cl if stamp.is_file() and stamp.read_text(encoding="utf-8") == built_from: return None # The stamp is written last, so a refresh that failed halfway runs again. - return linked(home, clone) or settings_written(home, clone, reports) or plugins_installed(clone, claude) or replace_file(stamp, built_from.encode()) + return linked(home, clone) or settings_written(home, clone, reports, own) or plugins_installed(clone, claude) or replace_file(stamp, built_from.encode()) def linked(home: pathlib.Path, clone: pathlib.Path) -> Failure | None: @@ -96,9 +96,11 @@ def linked(home: pathlib.Path, clone: pathlib.Path) -> Failure | None: return None -def settings_written(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path) -> Failure | None: - """Writes the home's settings.json: the clone's, with the clone's folder in place of CLONE and the reports folder in place of REPORTS, and the model the home's file holds.""" +def settings_written(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, own: pathlib.Path) -> Failure | None: + """Writes the home's settings.json: the clone's, with the clone's folder in place of CLONE and the reports folder in place of REPORTS, the model the home's file holds, and own's instructions kept out.""" settings = placed(json.loads((clone / "settings.json").read_text(encoding="utf-8")), {CLONE: shlex.quote(str(clone)), REPORTS: str(reports)}) + # Claude Code reads `.claude/CLAUDE.md` and `.claude/rules/` in every folder above the one it runs in, so the person's own would load in each project under their home folder: docs/decisions.md → `Claude Code in a home of its own`. + settings["claudeMdExcludes"] = [f"{own}/**"] generated = home / "settings.json" try: old = json.loads(generated.read_text(encoding="utf-8")) if generated.is_file() else {} diff --git a/bin/claude_home/test_home.py b/bin/claude_home/test_home.py index a874a2b1..d0a50d5a 100644 --- a/bin/claude_home/test_home.py +++ b/bin/claude_home/test_home.py @@ -33,6 +33,7 @@ class Home(unittest.TestCase): self.home = self.temp / "share" / "claude-home" self.commit: str | Failure = "1a2b3c\n" self.reports = self.temp / "state" / "build-release" / "larv-review" + self.own = self.temp / "home" / ".claude" self.status = SIGNED_IN # The exit code of each `claude plugin` command, by its last argument; 0 for one not named. self.exits: dict[str, int] = {} @@ -45,7 +46,7 @@ class Home(unittest.TestCase): return subprocess.CompletedProcess(args, self.exits.get(args[-1], 0), "", f"no {args[-1]}\nin the marketplace\n") def unready(self) -> str | None: - return unready(self.home, self.clone, self.reports, self.claude, lambda *args, cwd=None: self.commit if (args, cwd) == (("rev-parse", "HEAD"), str(self.clone)) else Failure(f"unexpected git {args} in {cwd}")) + return unready(self.home, self.clone, self.reports, self.own, self.claude, lambda *args, cwd=None: self.commit if (args, cwd) == (("rev-parse", "HEAD"), str(self.clone)) else Failure(f"unexpected git {args} in {cwd}")) def settings(self) -> dict[str, object]: read: dict[str, object] = json.loads((self.home / "settings.json").read_text(encoding="utf-8")) @@ -71,6 +72,11 @@ class Home(unittest.TestCase): self.assertEqual(self.settings()["permissions"], {"additionalDirectories": [str(self.reports), "~/code"], "defaultMode": "bypassPermissions"}) + def test_the_generated_settings_keep_out_the_instructions_of_the_persons_own_home(self) -> None: + self.unready() + + self.assertEqual(self.settings()["claudeMdExcludes"], [f"{self.own}/**"]) + def test_a_moved_reports_folder_refreshes_the_home(self) -> None: self.unready() self.reports = self.temp / "elsewhere" / "build-release" / "larv-review" @@ -102,7 +108,7 @@ class Home(unittest.TestCase): self.unready() - self.assertEqual((self.home / "settings.json").read_text(encoding="utf-8"), '{\n "env": {\n "A": "1"\n },\n "tui": "fullscreen"\n}\n') + self.assertEqual((self.home / "settings.json").read_text(encoding="utf-8"), f'{{\n "claudeMdExcludes": [\n "{self.own}/**"\n ],\n "env": {{\n "A": "1"\n }},\n "tui": "fullscreen"\n}}\n') def test_a_home_built_from_the_clones_commit_is_left_as_it_is(self) -> None: self.unready() @@ -149,7 +155,7 @@ class Home(unittest.TestCase): def claude(args: list[str]) -> subprocess.CompletedProcess[str]: return subprocess.CompletedProcess(args, 1, stdout, stderr) if args == ["auth", "status"] else subprocess.CompletedProcess(args, 0, "", "") - reason = unready(self.home, self.clone, self.reports, claude, lambda *args, cwd=None: "1a2b3c\n") + reason = unready(self.home, self.clone, self.reports, self.own, claude, lambda *args, cwd=None: "1a2b3c\n") self.assertEqual(reason, f"could not ask claude who is signed in to the loop's Claude home, {self.home}: {said}") @@ -190,7 +196,7 @@ class Home(unittest.TestCase): def missing(args: list[str]) -> subprocess.CompletedProcess[str]: raise FileNotFoundError(2, "No such file or directory", "claude") - reason = unready(self.home, self.clone, self.reports, missing, lambda *args, cwd=None: "1a2b3c\n") + reason = unready(self.home, self.clone, self.reports, self.own, missing, lambda *args, cwd=None: "1a2b3c\n") self.assertEqual(reason, f"could not build the loop's Claude home, {self.home}: [Errno 2] No such file or directory: 'claude'") self.assertFalse(only_signed_out(reason)) @@ -199,7 +205,7 @@ class Home(unittest.TestCase): def stalled(args: list[str]) -> subprocess.CompletedProcess[str]: raise subprocess.TimeoutExpired(["claude", *args], 600) - reason = unready(self.home, self.clone, self.reports, stalled, lambda *args, cwd=None: "1a2b3c\n") + reason = unready(self.home, self.clone, self.reports, self.own, stalled, lambda *args, cwd=None: "1a2b3c\n") self.assertEqual(reason, f"could not build the loop's Claude home, {self.home}: Command '['claude', 'plugin', 'marketplace', 'add', 'anthropics/official']' timed out after 600 seconds") diff --git a/bin/loop_effects/effects.py b/bin/loop_effects/effects.py index 4c2cf8c7..d8e0af92 100644 --- a/bin/loop_effects/effects.py +++ b/bin/loop_effects/effects.py @@ -16,7 +16,7 @@ import typing from notifier.notify import Backend, BACKENDS, notify from loop_effects import child_shells -from loop_state.paths import claude_home, in_claude_home, RELEASE_ROOT, with_release_tools +from loop_state.paths import CLAUDE_HOME, claude_home, in_claude_home, RELEASE_ROOT, with_release_tools from failure import Failure from git_cli import git_repo from machine_load.memory import Memory, read_memory @@ -128,7 +128,7 @@ def system_effects(home: pathlib.Path, env: typing.Mapping[str, str], backends: """ return Effects( claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, env=claude_environment(env, home), input=stdin, text=True), - claude_home=lambda: unready(claude_home(env, home), RELEASE_ROOT, reports_folder(home, env), lambda args: subprocess.run(["claude", *args], capture_output=True, env=claude_environment(env, home), stdin=subprocess.DEVNULL, text=True, timeout=home_timeout), git_repo.git), + claude_home=lambda: unready(claude_home(env, home), RELEASE_ROOT, reports_folder(home, env), home / CLAUDE_HOME, lambda args: subprocess.run(["claude", *args], capture_output=True, env=claude_environment(env, home), stdin=subprocess.DEVNULL, text=True, timeout=home_timeout), git_repo.git), claude_on_terminal=claude_on_terminal, discard_typed=lambda: termios.tcflush(sys.stdin, termios.TCIFLUSH), dm=lambda text, directory: notify(text, directory, home, env, backends),