"""reviewer-header: write the branch diff and the places into the chunk's review folder, and print the header of a reviewer's prompt.""" import dataclasses import pathlib from git_cli.git_repo import branch_titles, git, top from review_rules.round_titles import cap, round_counts from failure import Failure from chunk_preflight.choices import forge_of from larv_review.history_commands import history_file, places_report from project_config.main import located from review_rules.review_folder import made_review_folder from deploy_release.forge import open_pull, runner from review_rules.reviewer_context import principles_line, starts_bare from review_rules.review_passes import REVIEWERS # The passes whose reviewer is given the places: skills/larv-review/SKILL.md → "Comprehension check" → "Places". TAKE_PLACES = ("architecture", "stability") @dataclasses.dataclass(frozen=True) class Launch: """What the header of a reviewer's prompt names.""" pass_: str round_: int rerun: bool pull: str worktree: str branch: str base: str default: str folder: pathlib.Path diff: pathlib.Path principles: pathlib.Path goals: pathlib.Path bare: bool # The project's `AGENTS.md` and its decision log, where its config puts them. rules: pathlib.Path decisions: pathlib.Path def asked(self) -> pathlib.Path: """The round's report, which asks for the rerun where there is one.""" return self.folder / f"{self.pass_}-round-{self.round_}.md" def header(self) -> str: asked = self.asked() report = f"{asked.with_name(asked.stem + '-rerun.md')}, in place of {asked}, which asked for this rerun" if self.rerun else str(asked) return "\n".join([ f"Pass: {self.pass_}, round {self.round_}.", f"PR: {self.pull}.", f"Worktree: {self.worktree}, on branch {self.branch}.", f"Base: {self.base}, the merge base of HEAD and {self.default}.", f"Report: write it to {report}.", f"Diff: {self.diff}", f"Technical principles: {self.principles}", f"Goals and audience: {self.goals}", *([f"Places: {self.folder / 'places.md'}"] if self.pass_ in TAKE_PLACES else []), *([principles] if self.bare and (principles := principles_line(self.rules, self.decisions)) else []), ]) def unanswered_round(titles: list[str], pass_: str) -> int | Failure: """The pass's round that no `Answer round :` commit has answered yet; a Failure past the pass's cap.""" done = round_counts(titles).get(pass_, 0) if done >= cap(pass_): return Failure(f"{pass_} takes no more rounds: it has had {done}, and its cap is {cap(pass_)}") return done + 1 def reviewer_header(default: str, pass_: str, rerun: bool, home: pathlib.Path) -> str | Failure: """Writes the branch diff and the places into the review folder, and returns the header that launches pass_'s reviewer in the pass's unanswered round, or its relaunch one rung up where rerun.""" titles = branch_titles(default) if isinstance(titles, Failure): return titles round_ = unanswered_round(titles, pass_) if isinstance(round_, Failure): return round_ found = [top(), *(git(*args) for args in (["symbolic-ref", "--short", "HEAD"], ["rev-parse", "--short", "HEAD"], ["merge-base", "HEAD", default]))] failed = next((answer for answer in found if isinstance(answer, Failure)), None) if failed is not None: return failed worktree, branch, head, base = (str(answer).strip() for answer in found) folder = made_review_folder(worktree, home) if isinstance(folder, Failure): return folder config = located(None, home) if isinstance(config, Failure): return config principles = config.paths["principles"] # A typo in the config would otherwise hand every reviewer an empty folder, and the review would run with no principles. if not principles.is_dir() or not any(principles.iterdir()): return Failure(f"{principles} is not a folder with a file in it; {config.folder / 'config.toml'} sets it as `principles`") launch = Launch(pass_, round_, rerun, pull_named(worktree, branch), worktree, branch, base, default, folder, folder / f"branch-{head}.diff", principles, config.paths["goals-and-audience"], starts_bare(next((agent for name, agent in REVIEWERS.items() if name == pass_), ""), home), config.paths["working-rules"], config.paths["decisions"]) if rerun and not launch.asked().exists(): return Failure(f"no report asked for a rerun of {pass_}'s round {round_}: {launch.asked()} is missing; relaunch before you commit the round's answers") written = write_inputs(launch, base, home) if isinstance(written, Failure): return written return launch.header() def write_inputs(launch: Launch, base: str, home: pathlib.Path) -> Failure | None: """Writes the branch diff from base to HEAD and what `larv-review places` prints into the review folder.""" diff = git("diff", base, "HEAD", errors="replace") if isinstance(diff, Failure): return diff history = history_file(home) if isinstance(history, Failure): return history try: for path, text in ((launch.diff, diff), (launch.folder / "places.md", places_report(history[1]) + "\n")): # review-replay copies a file into a round's replay by its mtime, so rewriting one unchanged would drop it from every earlier round. if not path.exists() or path.read_text(encoding="utf-8") != text: path.write_text(text, encoding="utf-8") except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not write into the review folder {launch.folder}: {failure}") return None def pull_named(worktree: str, branch: str) -> str: """The branch's open pull request at its pushed head, as `#`, or why there is none to name. A reviewer reads the branch in the worktree, so the launch goes ahead when no pull request can be named.""" upstream = git("rev-parse", "@{upstream}") if isinstance(upstream, Failure): return "none, the branch is not pushed" origin = git("remote", "get-url", "origin") if isinstance(origin, Failure): return "unknown, the checkout has no origin remote" number = open_pull(forge_of(origin.strip()), branch, upstream.strip(), runner(worktree)) if isinstance(number, Failure): return f"unknown, {number.reason}" return f"#{number}" if number else "none open at the pushed head"