Keep configs in ~/.config/build-release/ and tool state in ~/.local/state/build-release/, under XDG_* where set
CI / tests (pull_request) Successful in 6m34s

This commit is contained in:
2026-12-03 18:56:28 +01:00
parent 20c83f861f
commit 1ca2c8efe0
142 changed files with 1095 additions and 933 deletions
+34 -8
View File
@@ -12,10 +12,14 @@ RELEASE_ROOT = pathlib.Path(__file__).resolve().parents[2]
# 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.
CLAUDE_HOME = ".claude"
# The config folder, which holds one folder per project, and do-epic's.
CONFIG_FOLDER = ".claude-projects"
# The state folder, which holds one folder per tool.
STATE_FOLDER = ".local/state"
# The setup's own folder under each of the two below.
SETUP = "build-release"
# Where the config folder is, unless the variable XDG_CONFIG_HOME names another place.
CONFIG_HOME = ".config"
XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
# Where the state folder is, unless the variable XDG_STATE_HOME names another place.
STATE_HOME = ".local/state"
XDG_STATE_HOME = "XDG_STATE_HOME"
# 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.
@@ -28,9 +32,26 @@ def with_release_tools(env: Mapping[str, str]) -> dict[str, str]:
return {**env, "PATH": f"{tools}{os.pathsep}{rest}" if rest else tools}
def config_folder(env: Mapping[str, str], home: pathlib.Path) -> pathlib.Path:
"""The config folder, which a person writes and backs up: one folder per project, do-epic's, and the model of each project."""
# The models file is here though the tools write it: it holds a person's choice per project, and is backed up with the configs.
return xdg_folder(env.get(XDG_CONFIG_HOME), home / CONFIG_HOME) / SETUP
def state_folder(env: Mapping[str, str], home: pathlib.Path) -> pathlib.Path:
"""The state folder of a tool that honours `XDG_STATE_HOME`."""
return pathlib.Path(env.get("XDG_STATE_HOME") or home / STATE_FOLDER)
"""The state folder, which the tools write: one folder per tool."""
# Every tool keeps its state here, none beside it, so that deleting this folder removes all the state the setup wrote.
return xdg_folder(env.get(XDG_STATE_HOME), home / STATE_HOME) / SETUP
# 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)}
@dataclasses.dataclass(frozen=True)
@@ -39,18 +60,23 @@ class Paths:
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:`.
config_clone: pathlib.Path
config_folder: pathlib.Path
home: pathlib.Path
model_catalog: pathlib.Path
models_file: pathlib.Path
state_folder: pathlib.Path
def paths_from(home: pathlib.Path) -> Paths:
def paths_from(home: pathlib.Path, env: Mapping[str, str]) -> Paths:
claude = home / CLAUDE_HOME
config = config_folder(env, home)
return Paths(
claude_code_transcripts=claude / "projects",
claude_dir=claude,
config_clone=home / "code" / "claude-config",
config_folder=config,
home=home,
model_catalog=claude / "cache" / "model-catalog",
models_file=claude / "loop-claude-models.json",
models_file=config / "models.json",
state_folder=state_folder(env, home),
)
+29 -1
View File
@@ -4,7 +4,7 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from loop_state.paths import RELEASE_ROOT, with_release_tools
from loop_state.paths import config_folder, paths_from, RELEASE_ROOT, state_folder, with_release_tools, without_xdg_folders
TOOLS = str(RELEASE_ROOT / "bin")
@@ -20,5 +20,33 @@ class WithReleaseTools(unittest.TestCase):
self.assertEqual(with_release_tools({}), {"PATH": TOOLS})
HOME = pathlib.Path("/home/me")
class SetupFolders(unittest.TestCase):
def test_both_lie_under_the_home_folder_where_no_xdg_variable_is_set(self):
self.assertEqual(config_folder({}, HOME), HOME / ".config" / "build-release")
self.assertEqual(state_folder({}, HOME), HOME / ".local" / "state" / "build-release")
def test_each_lies_under_the_folder_its_xdg_variable_names(self):
env = {"XDG_CONFIG_HOME": "/x/config", "XDG_STATE_HOME": "/x/state"}
self.assertEqual(config_folder(env, HOME), pathlib.Path("/x/config/build-release"))
self.assertEqual(state_folder(env, HOME), pathlib.Path("/x/state/build-release"))
def test_an_xdg_variable_that_is_empty_or_relative_is_ignored(self):
for named in ("", "config", "./state"):
with self.subTest(named=named):
env = {"XDG_CONFIG_HOME": named, "XDG_STATE_HOME": named}
self.assertEqual(config_folder(env, HOME), HOME / ".config" / "build-release")
self.assertEqual(state_folder(env, HOME), HOME / ".local" / "state" / "build-release")
def test_the_models_file_is_in_the_config_folder(self):
found = paths_from(HOME, {"XDG_CONFIG_HOME": "/x/config", "XDG_STATE_HOME": "/x/state"})
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"})
if __name__ == "__main__":
unittest.main()