|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""agents/faculties/review/_review.py — the review-surface substrate. |
| 3 | +
|
| 4 | +The **review faculty** is a read-only opinion sink: given a task worktree or |
| 5 | +explicit repo checkouts, it prepares the **ReviewSurface** — everything the |
| 6 | +reviewing agent needs to run the review procedure in this faculty's AGENTS.md |
| 7 | +(code review at high effort + a verify pass) and map the outcome to a |
| 8 | +CLEAN / FINDINGS / BLOCKED verdict. |
| 9 | +
|
| 10 | +This script produces the *surface*, never the *verdict*: findings are the |
| 11 | +reviewing agent's judgment. It is stdlib-only, never writes anything, and |
| 12 | +never exits non-zero because of what the diff contains. |
| 13 | +
|
| 14 | +Exit codes: 0 surface produced · 4 no reviewable diff / could not resolve · |
| 15 | +5 bad usage. |
| 16 | +""" |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import argparse |
| 20 | +import json |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +WT_BASE = Path.home() / "Code" / "PyAutoLabs-wt" |
| 26 | + |
| 27 | +# Paths that smell like public API / behaviour when changed — flags only, |
| 28 | +# the reviewing agent judges. |
| 29 | +RISK_MARKERS = { |
| 30 | + "config-or-schema": (".yaml", ".yml", ".json", ".toml", ".ini", ".cfg"), |
| 31 | + "packaging": ("setup.py", "setup.cfg", "pyproject.toml", "requirements"), |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +def _git(repo: Path, *args: str) -> str: |
| 36 | + out = subprocess.run( |
| 37 | + ["git", "-C", str(repo), *args], |
| 38 | + capture_output=True, text=True, check=False, |
| 39 | + ) |
| 40 | + return out.stdout.strip() if out.returncode == 0 else "" |
| 41 | + |
| 42 | + |
| 43 | +def repo_surface(repo: Path) -> dict | None: |
| 44 | + """One repo's slice of the ReviewSurface, or None if there is no diff.""" |
| 45 | + if not (repo / ".git").exists(): |
| 46 | + return None |
| 47 | + branch = _git(repo, "rev-parse", "--abbrev-ref", "HEAD") |
| 48 | + base = _git(repo, "merge-base", "HEAD", "origin/main") |
| 49 | + if not base: |
| 50 | + return None |
| 51 | + commits = _git(repo, "log", "--oneline", f"{base}..HEAD") |
| 52 | + if not commits: |
| 53 | + return None |
| 54 | + files = _git(repo, "diff", "--name-status", f"{base}..HEAD").splitlines() |
| 55 | + stat = _git(repo, "diff", "--shortstat", f"{base}..HEAD") |
| 56 | + changed = [line.split("\t")[-1] for line in files if line.strip()] |
| 57 | + flags = [] |
| 58 | + if not any("test" in f.lower() for f in changed): |
| 59 | + flags.append("no-test-changes") |
| 60 | + for name, exts in RISK_MARKERS.items(): |
| 61 | + if any(f.endswith(exts) or any(m in f for m in exts) for f in changed): |
| 62 | + flags.append(name) |
| 63 | + if any(f.endswith(".py") and "test" not in f.lower() for f in changed): |
| 64 | + flags.append("python-source") |
| 65 | + return { |
| 66 | + "repo": repo.name, |
| 67 | + "path": str(repo), |
| 68 | + "branch": branch, |
| 69 | + "base": base[:12], |
| 70 | + "commits_ahead": len(commits.splitlines()), |
| 71 | + "commits": commits.splitlines(), |
| 72 | + "shortstat": stat, |
| 73 | + "files": files, |
| 74 | + "risk_flags": flags, |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +def resolve_repos(task: str | None, repos: list[str]) -> list[Path]: |
| 79 | + if task: |
| 80 | + root = WT_BASE / task |
| 81 | + if not root.is_dir(): |
| 82 | + return [] |
| 83 | + # Claimed repos are real directories (not symlinks) holding a .git |
| 84 | + # file/dir — worktree_create symlinks everything unclaimed. |
| 85 | + return sorted( |
| 86 | + p for p in root.iterdir() |
| 87 | + if p.is_dir() and not p.is_symlink() and (p / ".git").exists() |
| 88 | + ) |
| 89 | + return [Path(r).resolve() for r in repos] |
| 90 | + |
| 91 | + |
| 92 | +def emit_human(surfaces: list[dict]) -> None: |
| 93 | + print("== ReviewSurface (review faculty — surface only; the verdict is the") |
| 94 | + print(" reviewing agent's, per agents/faculties/review/AGENTS.md) ==") |
| 95 | + for s in surfaces: |
| 96 | + print(f"\n-- {s['repo']} [{s['branch']}] base {s['base']}") |
| 97 | + print(f" {s['commits_ahead']} commit(s) ahead — {s['shortstat']}") |
| 98 | + for c in s["commits"][:10]: |
| 99 | + print(f" * {c}") |
| 100 | + print(f" risk flags: {', '.join(s['risk_flags']) or 'none'}") |
| 101 | + for f in s["files"][:40]: |
| 102 | + print(f" {f}") |
| 103 | + if len(s["files"]) > 40: |
| 104 | + print(f" ... and {len(s['files']) - 40} more") |
| 105 | + print("\nVerdict rubric: CLEAN (nothing must change) | FINDINGS (ranked,") |
| 106 | + print("file:line, failure scenario) | BLOCKED (could not review — say why).") |
| 107 | + |
| 108 | + |
| 109 | +def main(argv=None) -> int: |
| 110 | + ap = argparse.ArgumentParser(prog="review") |
| 111 | + ap.add_argument("--task", default="", help="task worktree name under ~/Code/PyAutoLabs-wt/") |
| 112 | + ap.add_argument("--repo", action="append", default=[], help="explicit repo checkout path") |
| 113 | + ap.add_argument("--json", action="store_true", dest="as_json") |
| 114 | + a = ap.parse_args(argv) |
| 115 | + if not a.task and not a.repo: |
| 116 | + print("review: pass --task <name> or --repo <path>", file=sys.stderr) |
| 117 | + return 5 |
| 118 | + repos = resolve_repos(a.task or None, a.repo) |
| 119 | + if not repos: |
| 120 | + print("review: could not resolve any repo checkout", file=sys.stderr) |
| 121 | + return 4 |
| 122 | + surfaces = [s for s in (repo_surface(r) for r in repos) if s] |
| 123 | + if not surfaces: |
| 124 | + print("review: no reviewable diff against origin/main", file=sys.stderr) |
| 125 | + return 4 |
| 126 | + if a.as_json: |
| 127 | + print(json.dumps({"review_surface": surfaces}, indent=2)) |
| 128 | + else: |
| 129 | + emit_human(surfaces) |
| 130 | + return 0 |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + sys.exit(main()) |
0 commit comments