Run every claude the loop starts in a Claude home generated outside the clone
CI / tests (pull_request) Successful in 4m45s
CI / tests (pull_request) Successful in 4m45s
This commit is contained in:
+22
-5
@@ -10,7 +10,7 @@ RELEASE_ROOT = pathlib.Path(__file__).resolve().parents[2]
|
||||
|
||||
# Each folder below is named relative to the person's home folder.
|
||||
|
||||
# The Claude home: Claude Code's own folder, with its settings and transcripts. A release is deployed into it, so its CLAUDE.md, rules/ and agents/ are read from it.
|
||||
# The person's own Claude home. No claude the loop starts runs there, and the setup doesn't read or write it: its name is for prose, and for the rule that keeps an agent out of its `.env`.
|
||||
CLAUDE_HOME = ".claude"
|
||||
# The name of the setup's own folder, in the config home and in the state home.
|
||||
SETUP = "build-release"
|
||||
@@ -20,6 +20,11 @@ XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
|
||||
# The state home, which holds the state folder, unless the variable XDG_STATE_HOME names another.
|
||||
STATE_HOME = ".local/state"
|
||||
XDG_STATE_HOME = "XDG_STATE_HOME"
|
||||
# The data home, which holds the data folder, unless the variable XDG_DATA_HOME names another.
|
||||
DATA_HOME = ".local/share"
|
||||
XDG_DATA_HOME = "XDG_DATA_HOME"
|
||||
# The variable that names the home a claude runs in, in place of the person's own.
|
||||
CLAUDE_CONFIG_DIR = "CLAUDE_CONFIG_DIR"
|
||||
|
||||
|
||||
# Only a claude started through this has the tools on PATH. One a person starts by hand lacks them, as their shell does: the plan keeps short names such as `wait-for` out of a person's shell. README.md → Tools says how to call a tool there.
|
||||
@@ -44,21 +49,33 @@ def state_folder(env: Mapping[str, str], home: pathlib.Path) -> pathlib.Path:
|
||||
return xdg_folder(env.get(XDG_STATE_HOME), home / STATE_HOME) / SETUP
|
||||
|
||||
|
||||
def claude_home(env: Mapping[str, str], home: pathlib.Path) -> pathlib.Path:
|
||||
"""The loop's Claude home, which claude_home.home builds in the data folder: every claude the loop starts keeps its settings, plugins, sign-in and transcripts there."""
|
||||
# The data folder holds what the tools generate and can generate again, so it needs no backup.
|
||||
return xdg_folder(env.get(XDG_DATA_HOME), home / DATA_HOME) / SETUP / "claude-home"
|
||||
|
||||
|
||||
def in_claude_home(env: Mapping[str, str], home: pathlib.Path) -> dict[str, str]:
|
||||
"""env with the loop's Claude home as the home a claude started in it runs in."""
|
||||
return {**env, CLAUDE_CONFIG_DIR: str(claude_home(env, home))}
|
||||
|
||||
|
||||
# The XDG Base Directory Specification calls a relative path in one of its variables invalid, to be ignored: https://specifications.freedesktop.org/basedir-spec/latest/
|
||||
def xdg_folder(named: str | None, default: pathlib.Path) -> pathlib.Path:
|
||||
return pathlib.Path(named) if named and os.path.isabs(named) else default
|
||||
|
||||
|
||||
def without_xdg_folders(env: Mapping[str, str]) -> dict[str, str]:
|
||||
"""env without the person's own config and state folders, so that both lie under the home folder a caller names."""
|
||||
return {name: value for name, value in env.items() if name not in (XDG_CONFIG_HOME, XDG_STATE_HOME)}
|
||||
"""env without the person's own config, state and data folders, so that each lies under the home folder a caller names."""
|
||||
return {name: value for name, value in env.items() if name not in (XDG_CONFIG_HOME, XDG_STATE_HOME, XDG_DATA_HOME)}
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Paths:
|
||||
claude_code_transcripts: pathlib.Path
|
||||
# The loop's Claude home, whose CLAUDE.md, rules/ and agents/ are links into the release root.
|
||||
claude_dir: pathlib.Path
|
||||
# The development clone of this setup, where a change to it is made: claude_dir holds only a release, docs/decisions.md → `deploy:`.
|
||||
# The development clone of this setup, where a change to it is made.
|
||||
config_clone: pathlib.Path
|
||||
config_folder: pathlib.Path
|
||||
home: pathlib.Path
|
||||
@@ -68,7 +85,7 @@ class Paths:
|
||||
|
||||
|
||||
def paths_from(home: pathlib.Path, env: Mapping[str, str]) -> Paths:
|
||||
claude = home / CLAUDE_HOME
|
||||
claude = claude_home(env, home)
|
||||
config = config_folder(env, home)
|
||||
return Paths(
|
||||
claude_code_transcripts=claude / "projects",
|
||||
|
||||
@@ -4,7 +4,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from loop_state.paths import config_folder, paths_from, RELEASE_ROOT, state_folder, with_release_tools, without_xdg_folders
|
||||
from loop_state.paths import claude_home, config_folder, in_claude_home, paths_from, RELEASE_ROOT, state_folder, with_release_tools, without_xdg_folders
|
||||
|
||||
TOOLS = str(RELEASE_ROOT / "bin")
|
||||
|
||||
@@ -45,7 +45,22 @@ class SetupFolders(unittest.TestCase):
|
||||
self.assertEqual((found.config_folder, found.models_file, found.state_folder), (pathlib.Path("/x/config/build-release"), pathlib.Path("/x/config/build-release/models.json"), pathlib.Path("/x/state/build-release")))
|
||||
|
||||
def test_an_environment_without_the_xdg_folders_keeps_every_other_variable(self):
|
||||
self.assertEqual(without_xdg_folders({"HOME": "/h", "XDG_CONFIG_HOME": "/c", "XDG_STATE_HOME": "/s", "XDG_DATA_HOME": "/d"}), {"HOME": "/h", "XDG_DATA_HOME": "/d"})
|
||||
self.assertEqual(without_xdg_folders({"HOME": "/h", "XDG_CONFIG_HOME": "/c", "XDG_STATE_HOME": "/s", "XDG_DATA_HOME": "/d", "XDG_CACHE_HOME": "/k"}), {"HOME": "/h", "XDG_CACHE_HOME": "/k"})
|
||||
|
||||
|
||||
class ClaudeHome(unittest.TestCase):
|
||||
def test_the_loops_claude_home_lies_in_the_setups_folder_of_the_data_home(self):
|
||||
self.assertEqual(claude_home({}, HOME), HOME / ".local" / "share" / "build-release" / "claude-home")
|
||||
self.assertEqual(claude_home({"XDG_DATA_HOME": "/x/data"}, HOME), pathlib.Path("/x/data/build-release/claude-home"))
|
||||
self.assertEqual(claude_home({"XDG_DATA_HOME": "data"}, HOME), HOME / ".local" / "share" / "build-release" / "claude-home")
|
||||
|
||||
def test_claude_codes_own_files_are_in_the_loops_claude_home_and_none_in_the_persons(self):
|
||||
found = paths_from(HOME, {"XDG_DATA_HOME": "/x/data"})
|
||||
home = pathlib.Path("/x/data/build-release/claude-home")
|
||||
self.assertEqual((found.claude_dir, found.claude_code_transcripts, found.model_catalog), (home, home / "projects", home / "cache" / "model-catalog"))
|
||||
|
||||
def test_a_claude_started_in_the_loops_home_is_told_so_and_keeps_every_other_variable(self):
|
||||
self.assertEqual(in_claude_home({"PATH": "/bin", "CLAUDE_CONFIG_DIR": "/elsewhere"}, HOME), {"PATH": "/bin", "CLAUDE_CONFIG_DIR": "/home/me/.local/share/build-release/claude-home"})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user