"""The chunk's review folder, where each reviewer writes its report: skills/larv-review/SKILL.md → "Reports".""" from collections.abc import Mapping import os import pathlib from failure import Failure from review_rules.round_titles import Findings, reported from loop_state.paths import state_folder # Why here: docs/decisions.md → `review: each reviewer under agents/ writes its full report into the chunk's review folder`. def reports_folder(home: pathlib.Path, env: Mapping[str, str]) -> pathlib.Path: """The folder that holds every chunk's review folder.""" return state_folder(env, home) / "larv-review" def review_folder(home: str, env: Mapping[str, str], worktree: str) -> str: """The review folder of the chunk whose worktree is at worktree: its path relative to home, or to / outside it, under the reports folder.""" inside = worktree.startswith(home.rstrip("/") + "/") return os.path.join(reports_folder(pathlib.Path(home), env), os.path.relpath(worktree, home) if inside else worktree.lstrip("/")) def made_review_folder(worktree: str, home: pathlib.Path, env: Mapping[str, str]) -> pathlib.Path | Failure: """Makes the review folder of the chunk whose worktree is at worktree, leaving the reports folder readable by the user alone, and returns its path.""" folder = pathlib.Path(review_folder(str(home), env, worktree)) reports = reports_folder(home, env) try: reports.mkdir(parents=True, exist_ok=True) reports.chmod(0o700) folder.mkdir(parents=True, exist_ok=True) except OSError as failure: return Failure(f"could not make the review folder {folder}: {failure}") return folder def round_report(folder: pathlib.Path, pass_: str, round_: int) -> pathlib.Path: """The report of pass_'s round round_ in the review folder, which asks for the rerun one rung up where there is one.""" return folder / f"{pass_}-round-{round_}.md" def rerun_report(asked: pathlib.Path) -> pathlib.Path: """The report of the rerun one rung up that `asked` asks for. It stands in place of `asked`.""" return asked.with_name(asked.stem + "-rerun.md") def standing_report(folder: pathlib.Path, pass_: str, round_: int) -> pathlib.Path: """The report that stands for pass_'s round round_ in the review folder: the rerun's where there is one, else the round's.""" asked = round_report(folder, pass_, round_) return rerun_report(asked) if rerun_report(asked).exists() else asked def findings_in(report: pathlib.Path) -> Findings | Failure: """The findings a round's report gives, as round_titles.reported counts them; a Failure where the report blocks yet heads no finding as review-findings.md → "Report" asks, since its count would then be wrong.""" try: text = report.read_text(encoding="utf-8") except FileNotFoundError: return Failure(f"no report at {report}") except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read {report}: {failure}") findings = reported(text) if isinstance(findings, Failure): return Failure(f"{report} cannot be counted: {findings.reason}; count its findings by hand") if text.lstrip().startswith("BLOCK") and findings.red + findings.yellow == 0: return Failure(f"{report} says BLOCK but opens no finding with `### 🔴` or `### 🟡`; count its findings by hand") return findings