44 lines
2.2 KiB
Python
44 lines
2.2 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: these project files, then the human's rules below.
|
|
RECORD_FILES = ("AGENTS.md", "docs/decisions.md")
|
|
# The setting in the project's config that places each of RECORD_FILES.
|
|
RECORD_SETTINGS = ("working-rules", "decisions")
|
|
# The human's rules for every project, under ~/.claude: CLAUDE.md, then each .md file in these folders, which a repo check keeps present.
|
|
HUMAN_RULES_FILE = "CLAUDE.md"
|
|
HUMAN_RULE_DIRS = ("rules", "skills/larv-review/standards")
|
|
HUMAN_RULE_NAMES = (f"~/.claude/{HUMAN_RULES_FILE}", *(f"~/.claude/{folder}/" for folder in HUMAN_RULE_DIRS))
|
|
|
|
|
|
CONSEQUENCE = "the judge cannot read the records"
|
|
|
|
|
|
def read_records(root: str, home: pathlib.Path, claude_dir: pathlib.Path) -> str | Failure:
|
|
"""The project's records, then the human's rules for every project, 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] + [rule for folder in HUMAN_RULE_DIRS for rule in sorted((claude_dir / folder).glob("*.md"))]
|
|
decisions = store(config, RECORD_SETTINGS[1]).read()
|
|
texts = [(RECORD_FILES[0], file_text(config.paths[RECORD_SETTINGS[0]])), (RECORD_FILES[1], Failure(f"{decisions.reason} - {CONSEQUENCE}.") if isinstance(decisions, Failure) else decisions)]
|
|
texts += [(f"~/.claude/{rule.relative_to(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 ""
|