Files
claude-config/bin/loop_state/paths.py
T

83 lines
3.9 KiB
Python

"""Where the setup's folders are: the one module whose code resolves them. Prose and tests still spell them out."""
import dataclasses
from collections.abc import Mapping
import os
import pathlib
# The release root: the folder the running code ships in, with bin/, skills/ and agents/.
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.
CLAUDE_HOME = ".claude"
# The name of the setup's own folder, in the config home and in the state home.
SETUP = "build-release"
# The config home, which holds the config folder, unless the variable XDG_CONFIG_HOME names another.
CONFIG_HOME = ".config"
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"
# 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.
def with_release_tools(env: Mapping[str, str]) -> dict[str, str]:
"""env with the release's tools first on `PATH`: what every claude the loop or `task-bench gather` starts runs in, so its Bash tool finds each tool by its bare name, and so does a hook's own script."""
tools = str(RELEASE_ROOT / "bin")
rest = env.get("PATH", "")
if rest.split(os.pathsep)[0] == tools:
return dict(env)
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, which the tools write: one folder per tool."""
# Every tool keeps its state here, none beside it in the state home. Only an archive that BUILD_RELEASE_TRANSCRIPTS names is elsewhere.
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)
class Paths:
claude_code_transcripts: pathlib.Path
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, 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=config / "models.json",
state_folder=state_folder(env, home),
)