Files
claude-config/bin/loop_claude/project/records.py
T

52 lines
2.6 KiB
Python

import pathlib
from failure import Failure
from project_config.main import located
from loop_claude.project.text_file import read_utf8
from record_store.stores import store
# The records the judge reads beside the premises, by the name that heads each, with the setting in the project's config that places it; then the human's rules below.
WORKING_RULES = ("AGENTS.md", "working-rules")
DECISIONS = ("docs/decisions.md", "decisions")
RECORD_FILES = (WORKING_RULES[0], DECISIONS[0])
# The human's rules, in the order the judge reads them: ~/.claude/CLAUDE.md, each .md file in ~/.claude/rules/,
# then each .md file in the review standards folder the project's config sets.
HUMAN_RULES_FILE = "CLAUDE.md"
HUMAN_RULE_DIR = "rules"
HUMAN_RULE_NAMES = (f"~/.claude/{HUMAN_RULES_FILE}", f"~/.claude/{HUMAN_RULE_DIR}/", "the project's review standards")
CONSEQUENCE = "the judge cannot read the records"
def read_records(root: str, home: pathlib.Path, claude_dir: pathlib.Path) -> str | Failure:
"""The project's `AGENTS.md` and decision log, then the human's rules, CLAUDE.md first, each headed by its name; one that does not exist, or is empty, is left out.
`AGENTS.md` is read where the config under home of root's project puts it, and the decision log from the store that config names. Each is headed by its name in RECORD_FILES wherever it is kept."""
config = located(root, home)
if isinstance(config, Failure):
return Failure(f"{config.reason} - {CONSEQUENCE}.")
rules = [claude_dir / HUMAN_RULES_FILE, *sorted((claude_dir / HUMAN_RULE_DIR).glob("*.md")), *sorted(config.paths["standards"].glob("*.md"))]
decisions = store(config, DECISIONS[1]).read()
texts = [(WORKING_RULES[0], file_text(config.paths[WORKING_RULES[1]])), (DECISIONS[0], Failure(f"{decisions.reason} - {CONSEQUENCE}.") if isinstance(decisions, Failure) else decisions)]
texts += [(named(rule, home, claude_dir), file_text(rule)) for rule in rules]
records = []
for heading, text in texts:
if isinstance(text, Failure):
return text
if text:
records.append(f"From {heading}:\n{text.strip()}")
return "\n\n".join(records)
def file_text(path: pathlib.Path) -> str | Failure:
"""path's text, empty where no such file exists."""
return read_utf8(str(path.parent), path.name, CONSEQUENCE) if path.exists() else ""
def named(rule: pathlib.Path, home: pathlib.Path, claude_dir: pathlib.Path) -> str:
"""rule's path as the human writes it: under ~/.claude, or ~/ for the rest of home, or else in full."""
if rule.is_relative_to(claude_dir):
return f"~/.claude/{rule.relative_to(claude_dir)}"
return f"~/{rule.relative_to(home)}" if rule.is_relative_to(home) else str(rule)