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
+18 -18
View File
@@ -1,22 +1,22 @@
"""A project's settings, read from the one config that names its checkout: `~/.claude-projects/<key>/config.toml`.
"""A project's settings, read from the one config that names its checkout: `<key>/config.toml` in the config folder, `~/.config/build-release/` unless `XDG_CONFIG_HOME` is set.
No path setting has a default, so that a client's project never runs on another project's files: a missing config or setting is a Failure that names it."""
import dataclasses
import decimal
from collections.abc import Mapping
import math
import pathlib
import re
import tomllib
import typing
from loop_state.paths import CONFIG_FOLDER
from loop_state.paths import config_folder
from failure import Failure
from plan.formula import parse_formula
from plan.todo_table import SHAPE
from project_config import templates
from plan.ranking import variables
PROJECTS = CONFIG_FOLDER
CONFIG = "config.toml"
# The key of do-epic's machine-wide settings: the folder's config.toml has no `checkout`, so no project may use the key.
DO_EPIC = "do-epic"
@@ -133,7 +133,7 @@ class Unnamed(Failure):
@dataclasses.dataclass(frozen=True)
class ProjectConfig:
"""folder is `~/.claude-projects/<key>`, top is the worktree that asked, and paths holds each of SETTINGS as an absolute path, except three kinds that pages or epics hold instead: `plan` and `goals-and-audience` where they are kept in Jira, and a record kept on a Confluence page."""
"""folder is `<key>` in the config folder, top is the worktree that asked, and paths holds each of SETTINGS as an absolute path, except three kinds that pages or epics hold instead: `plan` and `goals-and-audience` where they are kept in Jira, and a record kept on a Confluence page."""
key: str
folder: pathlib.Path
top: pathlib.Path
@@ -203,13 +203,13 @@ class ProjectConfig:
return path.relative_to(self.top).as_posix() if path.is_relative_to(self.top) else None
def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectConfig | Failure:
def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path, env: Mapping[str, str]) -> ProjectConfig | Failure:
"""The config that names main, the project's main checkout.
top is the worktree that asks, and main itself outside a worktree. A `checkout:` path lands in top.
A config that cannot be read fails every project, since it may be the one that names main."""
named: list[tuple[pathlib.Path, dict[str, object]]] = []
for file in configs(home):
for file in configs(home, env):
config = read(file, home)
if isinstance(config, Failure):
return config
@@ -217,7 +217,7 @@ def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectCo
if config[0].resolve() == main.resolve():
named.append((file, config[1]))
if not named:
return Unnamed(f"no config names the checkout {main}: write ~/{PROJECTS}/<key>/{CONFIG}, as `project-config --help` shows")
return Unnamed(f"no config names the checkout {main}: write {config_folder(env, home)}/<key>/{CONFIG}, as `project-config --help` shows")
if len(named) > 1:
return Failure(f"{' and '.join(str(file) for file, _ in named)} each name the checkout {main}; only one config may")
file, values = named[0]
@@ -413,20 +413,20 @@ def lies_in(path: pathlib.Path, *places: pathlib.Path) -> bool:
return any(path.resolve().is_relative_to(place.resolve()) for place in places)
def held(folder: pathlib.Path, home: pathlib.Path) -> ProjectConfig | Failure | None:
def held(folder: pathlib.Path, home: pathlib.Path, env: Mapping[str, str]) -> ProjectConfig | Failure | None:
"""The config in folder, read as its project's main checkout reads it; None where folder is not a config's folder under home."""
# Matched on resolved paths, as git names a folder, so a config's folder reached through a symlink is still found.
file = next((file for file in configs(home) if file.parent.resolve() == folder.resolve()), None)
file = next((file for file in configs(home, env) if file.parent.resolve() == folder.resolve()), None)
if file is None:
return None
config = read(file, home)
return config if isinstance(config, Failure) else load(config[0], config[0], home)
return config if isinstance(config, Failure) else load(config[0], config[0], home, env)
def by_name(name: str, home: pathlib.Path) -> ProjectConfig | Failure | None:
def by_name(name: str, home: pathlib.Path, env: Mapping[str, str]) -> ProjectConfig | Failure | None:
"""The config of the project whose main checkout is a folder called name, though that folder may not exist yet, loaded as `held` does; None where no config names one. A config that cannot be read fails every name, since it may be the one asked for."""
files = []
for file in configs(home):
for file in configs(home, env):
config = read(file, home)
if isinstance(config, Failure):
return config
@@ -434,17 +434,17 @@ def by_name(name: str, home: pathlib.Path) -> ProjectConfig | Failure | None:
files.append(file)
if len(files) > 1:
return Failure(f"{' and '.join(str(file) for file in files)} each name a checkout called {name}; only one config may")
return held(files[0].parent, home) if files else None
return held(files[0].parent, home, env) if files else None
def configs(home: pathlib.Path) -> list[pathlib.Path]:
"""Every project's config file under home, in key order. do-epic's machine-wide config is not a project's."""
return sorted(file for file in (home / PROJECTS).glob(f"*/{CONFIG}") if file.parent.name != DO_EPIC)
def configs(home: pathlib.Path, env: Mapping[str, str]) -> list[pathlib.Path]:
"""Every project's config file in the config folder, in key order. do-epic's machine-wide config is not a project's."""
return sorted(file for file in config_folder(env, home).glob(f"*/{CONFIG}") if file.parent.name != DO_EPIC)
def env_files(home: pathlib.Path) -> list[pathlib.Path]:
def env_files(home: pathlib.Path, env: Mapping[str, str]) -> list[pathlib.Path]:
"""Every config's env-file, do-epic's included. Each is read without the checks load makes, so a config that fails them still keeps its credentials guarded. A config that can't be read doesn't name one."""
values = [parsed(file) for file in (*configs(home), home / PROJECTS / DO_EPIC / CONFIG)]
values = [parsed(file) for file in (*configs(home, env), config_folder(env, home) / DO_EPIC / CONFIG)]
return [path for found in values if not isinstance(found, Failure) and (path := anywhere(found.get(ENV_FILE), home))]