import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) from project_config.doubles import configured from failure import Failure from agent_cycle import judge from chunk_driver.project import records class Records(unittest.TestCase): def test_heads_each_record_by_its_name_the_humans_rules_last(self): with tempfile.TemporaryDirectory() as root: claude_md = pathlib.Path(root, "home", ".claude", "CLAUDE.md") config = configured(self, pathlib.Path(root), home=claude_md.parents[1]) / "config.toml" self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), "") claude_md.parent.mkdir(parents=True) claude_md.write_text("# Global\n") self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), "From ~/.claude/CLAUDE.md:\n# Global") pathlib.Path(root, "docs").mkdir() pathlib.Path(root, "docs", "decisions.md").write_text("# Decisions\n") pathlib.Path(root, "AGENTS.md").write_text("\n# Rules\n") self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), "From AGENTS.md:\n# Rules\n\nFrom docs/decisions.md:\n# Decisions\n\nFrom ~/.claude/CLAUDE.md:\n# Global") pathlib.Path(claude_md.parent, "rules").mkdir() pathlib.Path(claude_md.parent, "rules", "todo-items.md").write_text("# Todo\n") pathlib.Path(claude_md.parent, "rules", "goals-and-audience.md").write_text("# Goals\n") pathlib.Path(claude_md.parent, "rules", "notes.txt").write_text("not a rule\n") pathlib.Path(claude_md.parent, "skills", "larv-review", "standards").mkdir(parents=True) pathlib.Path(claude_md.parent, "skills", "larv-review", "standards", "comments.md").write_text("# Comments\n") self.assertTrue(records.read_records(root, claude_md.parents[1], claude_md.parent).endswith("From ~/.claude/CLAUDE.md:\n# Global\n\nFrom ~/.claude/rules/goals-and-audience.md:\n# Goals\n\nFrom ~/.claude/rules/todo-items.md:\n# Todo\n\nFrom ~/.claude/skills/larv-review/standards/comments.md:\n# Comments")) config.write_text(config.read_text().replace('"~/.claude/skills/larv-review/standards"', '"~/review-standards"')) pathlib.Path(claude_md.parents[1], "review-standards").mkdir() pathlib.Path(claude_md.parents[1], "review-standards", "quality.md").write_text("# Ours\n") self.assertTrue(records.read_records(root, claude_md.parents[1], claude_md.parent).endswith("From ~/.claude/rules/todo-items.md:\n# Todo\n\nFrom ~/review-standards/quality.md:\n# Ours"), "the config's standards folder replaces the default") config.write_text(config.read_text().replace('"~/review-standards"', f'"{root}/standards"')) pathlib.Path(root, "standards").mkdir() pathlib.Path(root, "standards", "quality.md").write_text("# Elsewhere\n") self.assertTrue(records.read_records(root, claude_md.parents[1], claude_md.parent).endswith(f"From {root}/standards/quality.md:\n# Elsewhere"), "a folder outside home is named in full") config.write_text(config.read_text().replace('"checkout:AGENTS.md"', '"checkout:docs/decisions.md"')) self.assertTrue(records.read_records(root, claude_md.parents[1], claude_md.parent).startswith("From AGENTS.md and docs/decisions.md:\n# Decisions\n\nFrom ~/.claude/CLAUDE.md:\n# Global"), "a file the config names for two records appears once") config.write_text(config.read_text().replace('"checkout:docs/decisions.md"', '"checkout:AGENTS.md"', 1)) config.write_text(config.read_text().replace('"checkout:AGENTS.md"', '"folder:AGENTS.md"').replace('"checkout:docs/decisions.md"', '"folder:log/decisions.md"')) self.assertTrue(records.read_records(root, claude_md.parents[1], claude_md.parent).startswith("From ~/.claude/CLAUDE.md:\n# Global"), "the checkout's AGENTS.md and docs/decisions.md are its owners'") pathlib.Path(config.parent, "log").mkdir() pathlib.Path(config.parent, "log", "decisions.md").write_text("# Ours\n") pathlib.Path(config.parent, "AGENTS.md").write_text("# Our rules\n") self.assertTrue(records.read_records(root, claude_md.parents[1], claude_md.parent).startswith("From AGENTS.md:\n# Our rules\n\nFrom docs/decisions.md:\n# Ours\n\nFrom ~/.claude/CLAUDE.md:\n# Global")) config.unlink() self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), Failure(f"no config names the checkout {root}: write ~/.claude-projects//config.toml, as `project-config --help` shows - the judge cannot read the records.")) def test_puts_the_records_handed_in_first_and_reads_only_the_humans_rules_without_a_checkout(self): with tempfile.TemporaryDirectory() as root: claude_md = pathlib.Path(root, "home", ".claude", "CLAUDE.md") configured(self, pathlib.Path(root), home=claude_md.parents[1]) claude_md.parent.mkdir(parents=True) claude_md.write_text("# Global\n") pathlib.Path(claude_md.parent, "rules").mkdir() pathlib.Path(claude_md.parent, "rules", "todo-items.md").write_text("# Todo\n") pathlib.Path(claude_md.parent, "skills", "larv-review", "standards").mkdir(parents=True) pathlib.Path(claude_md.parent, "skills", "larv-review", "standards", "comments.md").write_text("# Comments\n") pathlib.Path(root, "AGENTS.md").write_text("# Rules\n") first = (("the epic ABC-1's description, ## Decisions", "Squash.\n"), ("the epic ABC-1's description, ## Scoring", " \n")) self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent, first), "From the epic ABC-1's description, ## Decisions:\nSquash.\n\nFrom AGENTS.md:\n# Rules\n\nFrom ~/.claude/CLAUDE.md:\n# Global\n\nFrom ~/.claude/rules/todo-items.md:\n# Todo\n\nFrom ~/.claude/skills/larv-review/standards/comments.md:\n# Comments") self.assertEqual(records.read_records(None, claude_md.parents[1], claude_md.parent, first), "From the epic ABC-1's description, ## Decisions:\nSquash.\n\nFrom ~/.claude/CLAUDE.md:\n# Global\n\nFrom ~/.claude/rules/todo-items.md:\n# Todo") def test_stops_on_a_record_it_cannot_read(self): with tempfile.TemporaryDirectory() as root: claude_md = pathlib.Path(root, "home", ".claude", "CLAUDE.md") configured(self, pathlib.Path(root), home=claude_md.parents[1]) claude_md.mkdir(parents=True) self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), Failure(f"CLAUDE.md in {claude_md.parent}: Is a directory - the judge cannot read the records.")) pathlib.Path(root, "docs", "decisions.md").mkdir(parents=True) self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), Failure(f"cannot read {root}/docs/decisions.md: Is a directory - the judge cannot read the records.")) pathlib.Path(root, "AGENTS.md").write_bytes(b"# Rules\n\xff\n") self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), Failure(f"AGENTS.md in {root} is not UTF-8 on line 2 - the judge cannot read the records.")) def test_stops_on_a_rule_it_cannot_read(self): with tempfile.TemporaryDirectory() as root: claude_md = pathlib.Path(root, "home", ".claude", "CLAUDE.md") configured(self, pathlib.Path(root), home=claude_md.parents[1]) rules = pathlib.Path(claude_md.parent, "rules") rules.mkdir(parents=True) pathlib.Path(rules, "todo-items.md").write_bytes(b"\xff\n") self.assertEqual(records.read_records(root, claude_md.parents[1], claude_md.parent), Failure(f"todo-items.md in {rules} is not UTF-8 on line 1 - the judge cannot read the records.")) class JudgeRules(unittest.TestCase): def test_the_judge_prompt_names_every_rule_the_judge_reads_from_the_human(self): self.assertEqual([name for name in records.HUMAN_RULE_NAMES if name not in judge.judge_rules("the ledger", "a-driver")], []) if __name__ == "__main__": unittest.main()