Run every claude the loop starts in a Claude home generated outside the clone
CI / tests (pull_request) Successful in 4m45s

This commit is contained in:
2026-12-04 01:07:03 +01:00
parent 2460b9e132
commit 0203a8e0b3
48 changed files with 703 additions and 369 deletions
+9 -10
View File
@@ -3,7 +3,6 @@ import json
import pathlib
import re
from loop_state.paths import CLAUDE_HOME
from failure import Failure
from review_rules.review_passes import REVIEWERS
from text_files.markdown_text import section
@@ -13,9 +12,9 @@ SECTIONS = ("Shared goal", "Prose", "Vocabulary")
# The text calls the file the setup's, the word that file uses for the release's rules, and names no install folder: a release may be installed anywhere.
def reviewer_context(home: pathlib.Path) -> str | Failure:
"""SECTIONS of the CLAUDE.md under home, as a reviewer is told them; a Failure where the file or a section is missing."""
path = home / CLAUDE_HOME / "CLAUDE.md"
def reviewer_context(claude_dir: pathlib.Path) -> str | Failure:
"""SECTIONS of the CLAUDE.md in claude_dir, the loop's Claude home, as a reviewer is told them; a Failure where the file or a section is missing."""
path = claude_dir / "CLAUDE.md"
try:
text = path.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as failure:
@@ -26,27 +25,27 @@ def reviewer_context(home: pathlib.Path) -> str | Failure:
return "\n\n".join(["You start without the setup's `CLAUDE.md`. These sections of it hold for you:", *("\n".join([f"## {name}", *lines]).strip() for name, lines in found.items() if lines is not None)])
def starts_bare(agent: str, home: pathlib.Path) -> bool:
"""Whether agent is a reviewer whose definition under home starts it without the instruction files."""
def starts_bare(agent: str, claude_dir: pathlib.Path) -> bool:
"""Whether agent is a reviewer whose definition in claude_dir, the loop's Claude home, starts it without the instruction files."""
if agent not in REVIEWERS.values():
return False
try:
text = (home / CLAUDE_HOME / "agents" / f"{agent}.md").read_text(encoding="utf-8")
text = (claude_dir / "agents" / f"{agent}.md").read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
return False
frontmatter = text.split("---\n")[1] if text.startswith("---\n") else ""
return "omitClaudeMd: true" in frontmatter.splitlines()
def hook_reply(given: str, home: pathlib.Path) -> str:
def hook_reply(given: str, claude_dir: pathlib.Path) -> str:
"""What the SubagentStart hook prints for its input given: the context for a reviewer that starts without the instruction files, and no text for any other agent."""
try:
read = json.loads(given)
except ValueError:
return ""
if not isinstance(read, dict) or not isinstance(agent := read.get("agent_type"), str) or not starts_bare(agent, home):
if not isinstance(read, dict) or not isinstance(agent := read.get("agent_type"), str) or not starts_bare(agent, claude_dir):
return ""
context = reviewer_context(home)
context = reviewer_context(claude_dir)
if isinstance(context, Failure):
# Claude Code starts the subagent whatever a SubagentStart hook exits with, and shows its stderr to no headless run, so the reviewer's report is where the worker can see the failure.
context = f"You start without the setup's `CLAUDE.md`, and the hook that hands you three of its sections failed: {context.reason}. Open your report by saying so."
+25 -26
View File
@@ -16,26 +16,25 @@ CLAUDE_MD = "# Global\n\n## Working loop\n\nThe worker's.\n\n## Shared goal\n\nC
class ReviewerContext(unittest.TestCase):
def setUp(self):
self.home = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()))
(self.home / ".claude").mkdir()
(self.home / ".claude" / "CLAUDE.md").write_text(CLAUDE_MD)
(self.home / ".claude" / "agents").mkdir()
self.claude_dir = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()))
(self.claude_dir / "CLAUDE.md").write_text(CLAUDE_MD)
(self.claude_dir / "agents").mkdir()
for agent in REVIEWERS.values():
(self.home / ".claude" / "agents" / f"{agent}.md").write_text(f"---\nname: {agent}\nomitClaudeMd: true\n---\n\nReview.\n")
(self.claude_dir / "agents" / f"{agent}.md").write_text(f"---\nname: {agent}\nomitClaudeMd: true\n---\n\nReview.\n")
def test_holds_the_shared_goal_prose_and_vocabulary_sections_and_none_of_the_workers(self):
self.assertEqual(reviewer_context(self.home), "You start without the setup's `CLAUDE.md`. These sections of it hold for you:\n\n## Shared goal\n\nCode health.\n\n## Prose\n\nWrite short.\n\n## Vocabulary\n\n- **chunk** — a unit.")
self.assertEqual(reviewer_context(self.claude_dir), "You start without the setup's `CLAUDE.md`. These sections of it hold for you:\n\n## Shared goal\n\nCode health.\n\n## Prose\n\nWrite short.\n\n## Vocabulary\n\n- **chunk** — a unit.")
def test_a_heading_inside_a_code_fence_does_not_end_a_section(self):
(self.home / ".claude" / "CLAUDE.md").write_text("## Shared goal\n\n```\n## Not a heading\n```\n\nstill the goal\n\n## Prose\n\np\n\n## Vocabulary\n\nv\n")
self.assertIn("## Shared goal\n\n```\n## Not a heading\n```\n\nstill the goal\n\n## Prose", reviewer_context(self.home))
(self.claude_dir / "CLAUDE.md").write_text("## Shared goal\n\n```\n## Not a heading\n```\n\nstill the goal\n\n## Prose\n\np\n\n## Vocabulary\n\nv\n")
self.assertIn("## Shared goal\n\n```\n## Not a heading\n```\n\nstill the goal\n\n## Prose", reviewer_context(self.claude_dir))
def test_fails_on_a_missing_section_or_file(self):
(self.home / ".claude" / "CLAUDE.md").write_text("## Prose\n\nWrite short.\n")
self.assertEqual(reviewer_context(self.home), Failure(f"{self.home / '.claude' / 'CLAUDE.md'} holds no section Shared goal, Vocabulary"))
(self.home / ".claude" / "CLAUDE.md").unlink()
path = self.home / ".claude" / "CLAUDE.md"
self.assertIn(f"could not read {path}: ", reviewer_context(self.home).reason)
(self.claude_dir / "CLAUDE.md").write_text("## Prose\n\nWrite short.\n")
self.assertEqual(reviewer_context(self.claude_dir), Failure(f"{self.claude_dir / 'CLAUDE.md'} holds no section Shared goal, Vocabulary"))
(self.claude_dir / "CLAUDE.md").unlink()
path = self.claude_dir / "CLAUDE.md"
self.assertIn(f"could not read {path}: ", reviewer_context(self.claude_dir).reason)
def test_the_repos_claude_md_holds_each_section(self):
headings = [line[3:] for line in (REPO / "CLAUDE.md").read_text().splitlines() if line.startswith("## ")]
@@ -44,29 +43,29 @@ class ReviewerContext(unittest.TestCase):
def test_the_hook_hands_a_reviewer_the_context_and_any_other_agent_nothing(self):
for agent in REVIEWERS.values():
with self.subTest(agent=agent):
reply = hook_reply(json.dumps({"hook_event_name": "SubagentStart", "agent_type": agent}), self.home)
self.assertEqual(json.loads(reply), {"hookSpecificOutput": {"hookEventName": "SubagentStart", "additionalContext": reviewer_context(self.home)}})
reply = hook_reply(json.dumps({"hook_event_name": "SubagentStart", "agent_type": agent}), self.claude_dir)
self.assertEqual(json.loads(reply), {"hookSpecificOutput": {"hookEventName": "SubagentStart", "additionalContext": reviewer_context(self.claude_dir)}})
for given in (json.dumps({"agent_type": "Explore"}), json.dumps({}), json.dumps([]), "no json"):
with self.subTest(given=given):
self.assertEqual(hook_reply(given, self.home), "")
self.assertEqual(hook_reply(given, self.claude_dir), "")
def test_the_hook_hands_nothing_to_a_reviewer_whose_definition_keeps_the_instruction_files(self):
given = json.dumps({"agent_type": "prose-reviewer"})
for kept in ("---\nname: prose-reviewer\n---\n\nomitClaudeMd: true\n", "---\nname: prose-reviewer\nomitClaudeMd: false\n---\n", "omitClaudeMd: true\n", b"\xff"):
with self.subTest(kept=kept):
path = self.home / ".claude" / "agents" / "prose-reviewer.md"
path = self.claude_dir / "agents" / "prose-reviewer.md"
path.write_bytes(kept) if isinstance(kept, bytes) else path.write_text(kept)
self.assertEqual(hook_reply(given, self.home), "")
(self.home / ".claude" / "agents" / "prose-reviewer.md").unlink()
self.assertEqual(hook_reply(given, self.home), "")
self.assertTrue(starts_bare("stability-reviewer", self.home))
(self.home / ".claude" / "agents" / "Explore.md").write_text("---\nomitClaudeMd: true\n---\n")
self.assertFalse(starts_bare("Explore", self.home))
self.assertEqual(hook_reply(given, self.claude_dir), "")
(self.claude_dir / "agents" / "prose-reviewer.md").unlink()
self.assertEqual(hook_reply(given, self.claude_dir), "")
self.assertTrue(starts_bare("stability-reviewer", self.claude_dir))
(self.claude_dir / "agents" / "Explore.md").write_text("---\nomitClaudeMd: true\n---\n")
self.assertFalse(starts_bare("Explore", self.claude_dir))
def test_the_hook_tells_a_reviewer_to_report_that_a_section_is_missing(self):
path = self.home / ".claude" / "CLAUDE.md"
path = self.claude_dir / "CLAUDE.md"
path.write_text("## Prose\n\nWrite short.\n")
reply = json.loads(hook_reply(json.dumps({"agent_type": "prose-reviewer"}), self.home))
reply = json.loads(hook_reply(json.dumps({"agent_type": "prose-reviewer"}), self.claude_dir))
self.assertEqual(reply["hookSpecificOutput"]["additionalContext"], f"You start without the setup's `CLAUDE.md`, and the hook that hands you three of its sections failed: {path} holds no section Shared goal, Vocabulary. Open your report by saying so.")
@@ -97,7 +96,7 @@ class Reviewers(unittest.TestCase):
def test_settings_run_the_hook_as_each_subagent_starts(self):
hooks = json.loads((REPO / "settings.json").read_text())["hooks"]["SubagentStart"]
self.assertEqual(hooks, [{"hooks": [{"command": "~/.claude/bin/larv-review reviewer-context", "timeout": 30, "type": "command"}]}])
self.assertEqual(hooks, [{"hooks": [{"command": "{clone}/bin/larv-review reviewer-context", "timeout": 30, "type": "command"}]}])
if __name__ == "__main__":