import pathlib from failure import Failure from project_config.main import located from chunk_driver.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" # How the judge's prompt names the records above, in the order it reads them. RECORDS_NAMED = "the project's working rules and decisions, then the human's rules for every project" 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 | None, home: pathlib.Path, claude_dir: pathlib.Path, first: tuple[tuple[str, str], ...] = ()) -> 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. A text that several of them hold appears once, headed by all their names. `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. first holds records kept outside the project, such as the sections of a Jira epic's description, each as its heading and text: they come before the project's. A root of None is work without a checkout, which has no project records and no review standards.""" texts: list[tuple[str, str | Failure]] = list(first) rules = [claude_dir / HUMAN_RULES_FILE, *sorted((claude_dir / HUMAN_RULE_DIR).glob("*.md"))] if root is not None: config = located(root, home) if isinstance(config, Failure): return Failure(f"{config.reason} - {CONSEQUENCE}.") rules += 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] # Each text, with the names of the records that hold it: a config may keep the working rules in the decision log. records: dict[str, list[str]] = {} for heading, text in texts: if isinstance(text, Failure): return text if text.strip(): records.setdefault(text.strip(), []).append(heading) return "\n\n".join(f"From {' and '.join(headings)}:\n{text}" for text, headings in records.items()) 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)