"""What a reviewer under agents/ is handed in place of the instruction files: `omitClaudeMd` starts it without them, and most of `~/.claude/CLAUDE.md` is the worker's. docs/decisions.md → `agents/: a reviewer starts without CLAUDE.md only where a replay of its pass missed none…` says why, and which reviewers.""" import json import pathlib from failure import Failure from review_rules.review_passes import REVIEWERS # The sections of ~/.claude/CLAUDE.md a reviewer needs, in the file's order: the code-health goal the agents cite, the bar its own prose meets, and the words its reports use. SECTIONS = ("Shared goal", "Prose", "Vocabulary") 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" / "CLAUDE.md" try: text = path.read_text(encoding="utf-8") except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read {path}: {failure}") found: dict[str, list[str]] = {} lines: list[str] = [] for line in text.splitlines(): if line.startswith("## "): lines = found.setdefault(line[3:].strip(), []) lines.append(line) if missing := [name for name in SECTIONS if name not in found]: return Failure(f"{path} holds no section {', '.join(missing)}") return "\n\n".join(["You start without `~/.claude/CLAUDE.md`. These sections of it hold for you:", *("\n".join(found[name]).strip() for name in SECTIONS)]) def starts_bare(agent: str, home: pathlib.Path) -> bool: """Whether agent is a reviewer whose definition under home starts it without the instruction files.""" if agent not in REVIEWERS.values(): return False try: text = (home / ".claude" / "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: """What the SubagentStart hook prints for its input given: the context for a reviewer that starts without the instruction files, nothing 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): return "" context = reviewer_context(home) 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 `~/.claude/CLAUDE.md`, and the hook that hands you three of its sections failed: {context.reason}. Open your report by saying so." return json.dumps({"hookSpecificOutput": {"hookEventName": "SubagentStart", "additionalContext": context}}) def principles_line(worktree: str) -> str | None: """The line of a reviewer's header that names where the project in worktree keeps its principles and decision titles, or None where it keeps neither file.""" what = "for the project principles and the decision titles" if (pathlib.Path(worktree) / "AGENTS.md").is_file(): return f"Principles: read {worktree}/AGENTS.md first, {what}." if (pathlib.Path(worktree) / "docs" / "decisions.md").is_file(): return f"Principles: read {worktree}/docs/decisions.md first, up to `# Decisions`, {what}." return None