drafter: local runs the pi coding agent against the model resident on an OpenAI-compatible server at the new local_url input, keyed by the LOCAL_MODEL_API_KEY secret. It asks the server which model is loaded and names exactly that one, so it never evicts the resident model. An unreachable server or no loaded model posts the stage 1 flags with a note and exits 0; a drafter that ran and failed still exits 1. pi runs in the existing sandbox with read, ls, edit and write, every project-local pi file and AGENTS.md ignored, stdin from /dev/null, the task passed as an @file, and an allowlisted environment. As root with DOCS_REVIEW_DRAFTER_USER set, it runs as that unprivileged user, which owns only the sandbox and pi's temporary home. Both drafters now hash the checkout's .git/config and .git/hooks before drafting and stop with no further git command if either changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
144 lines
5.6 KiB
Python
144 lines
5.6 KiB
Python
"""The `monthly` flow: whole-repository stage 1, then an issue or a drafted PR."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
from collections.abc import Mapping
|
|
from pathlib import Path
|
|
|
|
from docs_review import git
|
|
from docs_review.draft import build_prompt, load_template, parse_summary, render_pr_body
|
|
from docs_review.drafter import draft_in_sandbox
|
|
from docs_review.flags import Flag, compute_flags, group_uncovered, whole_repo_changes
|
|
from docs_review.inputs import Inputs, expand_patterns, split_review_only
|
|
from docs_review.report import MONTHLY_MARKER, render_issue, stage1_json
|
|
from docs_review.run_pr import read_guide, read_texts
|
|
|
|
MONTHLY_PREFIX = "docs-review/monthly-"
|
|
MONTH_PATTERN = re.compile(r"^\d{4}-(0[1-9]|1[0-2])$")
|
|
# Whole-repository mode counts every file as added and every input as changed, so only
|
|
# these two kinds mean anything.
|
|
MONTHLY_KINDS = ("uncovered", "unguided")
|
|
NO_DIFF_PAYLOAD = (
|
|
"Monthly mode has no diff. Read the repository as it stands, starting from the "
|
|
"top-level documents."
|
|
)
|
|
MOVED_NOTE = "The monthly review moved to PR #{n}."
|
|
MERGE_CONFLICT_NOTE = (
|
|
"The monthly docs branch `{branch}` no longer merges with `{base}`; "
|
|
"resolve it by hand."
|
|
)
|
|
|
|
|
|
def valid_month(month: str) -> bool:
|
|
return bool(MONTH_PATTERN.match(month))
|
|
|
|
|
|
def _stage1(inputs: Inputs, cwd: Path):
|
|
docs = expand_patterns(inputs.docs, cwd)
|
|
generated = expand_patterns(inputs.generated, cwd)
|
|
repo_files = git.ls_files(cwd)
|
|
changes = whole_repo_changes(repo_files, inputs.source_dirs)
|
|
flags = compute_flags(changes, frozenset(), frozenset(), read_texts(docs, cwd),
|
|
read_texts(generated, cwd), inputs.source_dirs,
|
|
read_guide(inputs, cwd), repo_files=repo_files)
|
|
kept = [f for f in flags if f.kind in MONTHLY_KINDS]
|
|
return group_uncovered(kept), docs
|
|
|
|
|
|
def post_issue(forge, flags: tuple[Flag, ...], month: str, note: str | None = None) -> None:
|
|
"""Open or update the one monthly issue; with nothing to say, open nothing."""
|
|
existing = forge.find_open_issue(MONTHLY_MARKER)
|
|
if not flags and not note and existing is None:
|
|
return
|
|
body = render_issue(flags, month=month)
|
|
if note:
|
|
body += "\n" + note + "\n"
|
|
title = f"docs: monthly review {month}"
|
|
if existing is not None:
|
|
forge.edit_issue(existing["number"], title=title, body=body)
|
|
else:
|
|
forge.open_issue(title, body)
|
|
|
|
|
|
def _checkout(base: str, month: str, reuse: dict | None, ident, cwd: Path) -> str | None:
|
|
"""The branch to draft on, or None when a reused branch no longer merges."""
|
|
git.fetch(base, cwd)
|
|
if reuse is None:
|
|
branch = f"{MONTHLY_PREFIX}{month}"
|
|
git.checkout_new(branch, f"origin/{base}", cwd)
|
|
return branch
|
|
branch = reuse["head"]["ref"]
|
|
git.fetch(branch, cwd)
|
|
git.checkout_new(branch, "FETCH_HEAD", cwd)
|
|
try:
|
|
git.merge(f"origin/{base}", ident, cwd)
|
|
except git.GitError:
|
|
try:
|
|
git.run(["rev-parse", "-q", "--verify", "MERGE_HEAD"], cwd)
|
|
except git.GitError:
|
|
raise # not a conflict: nothing to abort
|
|
git.run(["merge", "--abort"], cwd)
|
|
return None
|
|
return branch
|
|
|
|
|
|
def _commit_message(inputs: Inputs, month: str) -> str:
|
|
tail = f" (#{inputs.tracking_issue})" if inputs.tracking_issue else ""
|
|
return f"docs({inputs.workstream}): monthly review {month}{tail}"
|
|
|
|
|
|
def _deliver(forge, inputs, base, month, branch, reuse, ident, flags, result, changed, cwd,
|
|
env):
|
|
git.commit(changed, _commit_message(inputs, month), ident, cwd)
|
|
git.push(branch, cwd, env.get("DOCS_REVIEW_BOT_TOKEN"),
|
|
env.get("GITHUB_SERVER_URL"))
|
|
head = git.run(["rev-parse", "HEAD"], cwd).strip()
|
|
body = render_pr_body(parse_summary(result.text), result, flags, head, changed)
|
|
title = f"docs: monthly review {month}"
|
|
if reuse is not None:
|
|
number = reuse["number"]
|
|
forge.edit_pr(number, title=title, body=body)
|
|
else:
|
|
number = forge.open_pr(head=branch, base=base, title=title, body=body)
|
|
issue = forge.find_open_issue(MONTHLY_MARKER)
|
|
if issue is not None:
|
|
forge.edit_issue(issue["number"],
|
|
body=MONTHLY_MARKER + "\n" + MOVED_NOTE.format(n=number) + "\n")
|
|
forge.close_issue(issue["number"])
|
|
|
|
|
|
def run_monthly(inputs: Inputs, forge, env: Mapping[str, str], cwd: Path,
|
|
month: str, base: str) -> int:
|
|
flags, docs = _stage1(inputs, cwd)
|
|
if inputs.drafter == "none":
|
|
post_issue(forge, flags, month)
|
|
return 0
|
|
ident = forge.identity()
|
|
reuse = forge.find_open_pr_prefix(MONTHLY_PREFIX)
|
|
branch = _checkout(base, month, reuse, ident, cwd)
|
|
if branch is None:
|
|
post_issue(forge, flags, month,
|
|
MERGE_CONFLICT_NOTE.format(branch=reuse["head"]["ref"], base=base))
|
|
return 0
|
|
flags_json = json.dumps(stage1_json(flags, mode="monthly", head_sha=month))
|
|
editable, read_only = split_review_only(docs, inputs.review_only)
|
|
prompt = build_prompt(
|
|
load_template("monthly"), mode="monthly", pr=None,
|
|
flags_json=flags_json, payload=NO_DIFF_PAYLOAD, docs=editable,
|
|
review_only=read_only,
|
|
top_level=inputs.top_level, guide_path=inputs.doc_guide,
|
|
)
|
|
drafted = draft_in_sandbox(inputs, prompt, cwd, env)
|
|
if drafted.note is not None:
|
|
post_issue(forge, flags, month, drafted.note)
|
|
return 0 if drafted.soft else 1
|
|
result, changed = drafted.result, list(drafted.changed)
|
|
if not changed:
|
|
post_issue(forge, flags, month)
|
|
return 0
|
|
_deliver(forge, inputs, base, month, branch, reuse, ident, flags, result, changed, cwd,
|
|
env)
|
|
return 0
|