62 lines
3.5 KiB
Python
62 lines
3.5 KiB
Python
"""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 no more findings than the most an unchanged replay missed` says why, and which reviewers."""
|
|
import json
|
|
import pathlib
|
|
import re
|
|
|
|
from failure import Failure
|
|
from review_rules.review_passes import REVIEWERS
|
|
from text_files.markdown_text import section
|
|
|
|
# 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 = {name: section(text, re.escape(name)) for name in SECTIONS}
|
|
if missing := [name for name, lines in found.items() if lines is None]:
|
|
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([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."""
|
|
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, 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):
|
|
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(rules: pathlib.Path, decisions: pathlib.Path) -> str | None:
|
|
"""The line of a reviewer's header that names where a project keeps its principles and decision titles, or None where it keeps neither file. rules is its `AGENTS.md` and decisions its decision log, where its config puts them."""
|
|
what = "for the project principles and the decision titles"
|
|
if rules.is_file():
|
|
return f"Principles: read {rules} first, {what}."
|
|
if decisions.is_file():
|
|
return f"Principles: read {decisions} first, up to `# Decisions`, {what}."
|
|
return None
|