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

99 lines
4.8 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 person's own Claude home. No claude the loop starts runs there, and neither entry point reads or writes it: its name is for prose, for rule-hook's rules that guard it, and for the instructions the loop's home keeps out.
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"
# 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.
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, build-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. An archive that BUILD_RELEASE_TRANSCRIPTS names is elsewhere.
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."""
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, 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.
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 = claude_home(env, home)
config = config_folder(env, home)
return Paths(
claude_code_transcripts=claude / "projects",
claude_dir=claude,
config_clone=home / "code" / "dev-factory",
config_folder=config,
home=home,
model_catalog=claude / "cache" / "model-catalog",
models_file=config / "models.json",
state_folder=state_folder(env, home),
)