Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

31.9 KB
Raw Blame
"""GitHub data fetching via gh CLI + local git.

File stats come from the local checkout (git diff --numstat), everything
else (PR metadata, reviews, comments, check runs) from the GitHub API.
Also handles team membership checks for the ownership gate.
"""

import os
import re
import json
import tempfile
import subprocess
from collections.abc import Callable
from dataclasses import dataclass, field
from datetime import UTC, datetime
from pathlib import Path


@dataclass
class PRData:
    """All GitHub data needed to evaluate a PR."""

    number: int
    repo: str
    title: str
    state: str
    draft: bool
    mergeable_state: str
    author: str
    labels: list[str]
    base_ref: str
    base_sha: str
    head_sha: str
    files: list[dict]
    reviews: list[dict]
    review_comments: list[dict]
    check_runs: list[dict]
    author_is_bot: bool = False
    pr_reactions: list[dict] = field(default_factory=list)
    body: str = ""
    discussion: list[dict] = field(default_factory=list)
    # The repo's default branch, so stacked-ness isn't tied to "master" (the hosted
    # runtime reviews repos whose trunk is "main").
    default_branch: str = "master"
    # Set by the hosted server from GitHub's compare API. Its shallow checkout holds no history, so
    # git cannot compute the merge base there (see diff_range).
    merge_base_sha: str = ""
    # Every path the diff touches, unquoted: a rename's old and new name both. `files` names a
    # rename once, so a deny pattern would miss code moved out of a protected directory.
    touched_paths: list[str] = field(default_factory=list)
    # Set by the hosted server from the external reviewers it asked: one dict per reviewer with state,
    # reviewer, review_kind, findings_count, published, head_sha, base_ref, base_sha and skipped_paths. Trusted
    # metadata, never PR text. Empty for the Action, which has no such source.
    review_evidence: list[dict] = field(default_factory=list)

    def pending_review_evidence(self) -> list[dict]:
        """The external reviews still running on the current head."""
        return [
            item
            for item in self.review_evidence
            if item.get("head_sha") == self.head_sha and item.get("state") == "pending"
        ]

    def completed_review_evidence(self) -> list[dict]:
        """The completed external reviews of the current head against the current merge base.

        A retarget keeps the head SHA but changes the diff, so a review of another merge base does
        not count. The base branch SHA is no match key, because it moves on every merge to the base
        while the diff stays the same. An unknown merge base on either side never matches.
        """
        return [
            item
            for item in self.review_evidence
            if item.get("head_sha") == self.head_sha
            and item.get("state") == "completed"
            and self.merge_base_sha
            and item.get("base_sha") == self.merge_base_sha
        ]

    @property
    def stacked(self) -> bool:
        """True when the PR targets another PR's branch rather than the repo's trunk."""
        return self.base_ref != self.default_branch

    @property
    def file_paths(self) -> list[str]:
        return [f["filename"] for f in self.files]

    @property
    def deny_paths(self) -> list[str]:
        """The paths the deny-list matches: the changed files plus every touched path."""
        return list(dict.fromkeys([*self.file_paths, *self.touched_paths]))

    @property
    def lines_added(self) -> int:
        return sum(f["additions"] for f in self.files)

    @property
    def lines_deleted(self) -> int:
        return sum(f["deletions"] for f in self.files)

    @property
    def lines_total(self) -> int:
        return self.lines_added + self.lines_deleted

    @property
    def has_new_files(self) -> bool:
        return any(f.get("status") == "A" for f in self.files)


def describe_review_evidence(evidence: dict) -> str:
    """One line on a completed external review of the current head, for the prompt and the verdict."""
    count = int(evidence.get("findings_count") or 0)
    if count == 0:
        findings = "no findings"
    elif evidence.get("published"):
        findings = f"{count} finding{'' if count == 1 else 's'}, posted on the PR"
    else:
        findings = f"{count} finding{'' if count == 1 else 's'}, not posted on the PR"
    reviewer = evidence.get("reviewer")
    kind = evidence.get("review_kind")
    name = f"{reviewer} ({kind})" if kind else str(reviewer)
    skipped = len(evidence.get("skipped_paths") or [])
    if skipped:
        return (
            f"{name} reviewed the current head ({findings}) but skipped {skipped} changed "
            f"file{'' if skipped == 1 else 's'}, so it does not count as assurance"
        )
    return f"{name} reviewed the current head: {findings}"


_TRUSTED_ASSOCIATIONS = {"MEMBER", "OWNER", "COLLABORATOR"}

# Machine users that are real org members (type "User") but should still be
# treated as bots. GitHub Apps already report type "Bot" and aren't listed here.
_BOT_MACHINE_USERS = {"posthog-bot"}

# Bots whose reactions are deliberate review verdicts (👍 = reviewed clean,
# 👀 = review in flight). Other installed apps react for unrelated reasons
# (e.g. inkeep's 👎 is docs feedback), so bot reactions are allowlisted rather
# than trusted wholesale. GraphQL returns bot logins with the "[bot]" suffix.
TRUSTED_REACTOR_BOTS = {
    "chatgpt-codex-connector[bot]",
    "copilot-pull-request-reviewer[bot]",
    "greptile-apps[bot]",
    "hex-security-app[bot]",
    "veria-ai[bot]",
}

# A reviewer bot's 👀 outlives the review it announced when the bot fails
# mid-run: reactions never expire, and no one can remove another app's
# reaction. Past this age the 👀 is treated as abandoned instead of in flight,
# so a wedged bot cannot block a PR forever. A reaction with no timestamp
# counts as fresh, which fails toward waiting.
BOT_EYES_MAX_AGE_SECONDS = 45 * 60


def _reaction_age_seconds(created_at: str | None) -> float:
    if not created_at:
        return 0.0
    try:
        created = datetime.fromisoformat(created_at)
    except ValueError:
        return 0.0
    return (datetime.now(UTC) - created).total_seconds()


def _is_bot_eyes(reaction: dict) -> bool:
    return reaction.get("emoji") == "👀" and (reaction.get("user") or "").lower() in TRUSTED_REACTOR_BOTS


def is_in_flight_bot_eyes(reaction: dict) -> bool:
    """True for a 👀 from an allowlisted reviewer bot that has not aged out."""
    return _is_bot_eyes(reaction) and _reaction_age_seconds(reaction.get("created_at")) <= BOT_EYES_MAX_AGE_SECONDS


def drop_abandoned_bot_eyes(reactions: list[dict] | None) -> list[dict]:
    """Drop reviewer-bot 👀 that aged past the in-flight cutoff.

    The wait gate stops waiting on these, so every reader of a reaction list
    must hide them too. A stale 👀 left in the reviewer prompt makes the LLM
    refuse over a review that finished days ago, and the author cannot clear
    the reaction because it belongs to the bot.
    """
    return [r for r in reactions or [] if not _is_bot_eyes(r) or is_in_flight_bot_eyes(r)]


def is_bot_author(user: dict) -> bool:
    """True when the PR author is a bot or machine account.

    GitHub Apps (dependabot, mendral, other agents) report user.type == "Bot";
    machine users like posthog-bot are type "User", so match them by login.
    Mirrors the bot definition the hosted webhook pre-filter uses (_is_bot_authored).
    """
    if user.get("type") == "Bot":
        return True
    login = (user.get("login") or "").lower()
    return "[bot]" in login or login in _BOT_MACHINE_USERS


# Stamphog's own review identities: REFUSE/ESCALATE comment reviews post via
# the GitHub App (stamphog[bot]); APPROVE reviews post via the workflow's
# GITHUB_TOKEN (github-actions[bot]) so they count toward branch protection.
_SELF_REVIEW_LOGINS = {"stamphog[bot]", "github-actions[bot]"}


def _prompt_worthy_author(login: str | None, association: str | None, is_bot: bool) -> bool:
    """One author-trust gate for reviews, inline comments, and discussion.

    Drops stamphog's own prior verdicts (they describe a stale snapshot the next
    run would misread as third-party tampering) and admits only trusted org
    members or bots. Without this, an untrusted external commenter could reach
    the prompt via discussion and grief it with a fake maintainer hold or forge
    a fake assurance claim. REST and GraphQL spell the author differently, so
    the caller passes the already-extracted login/association/bot-ness.
    """
    if login in _SELF_REVIEW_LOGINS:
        return False
    return is_bot or association == "BOT" or association in _TRUSTED_ASSOCIATIONS


def _normalize_reviews_for_prompt(reviews_raw: list[dict], head_sha: str) -> list[dict]:
    """Normalize top-level reviews for the reviewer prompt.

    Preserve trusted/bot reviews, and annotate whether each review was left on
    the current PR head. This lets the LLM distinguish active feedback from
    older context that may already have been addressed in follow-up commits.

    Stamphog's own prior reviews are excluded: they describe an earlier
    snapshot of the PR, are never independent review signal, and the reviewer
    has no way to recognize them as its own — re-reading a stale verdict after
    the PR state changed makes it suspect tampering and refuse forever.
    """
    normalized_reviews = []
    for review in reviews_raw:
        user = review.get("user") or {}
        if not _prompt_worthy_author(user.get("login"), review.get("author_association"), user.get("type") == "Bot"):
            continue

        commit_id = review.get("commit_id")
        normalized_reviews.append(
            {
                "user": review["user"]["login"],
                "state": review["state"],
                "body": review.get("body", ""),
                "commit_id": commit_id,
                "is_current_head": commit_id == head_sha,
                "submitted_at": review.get("submitted_at"),
            }
        )

    return normalized_reviews


def _normalize_discussion_for_prompt(comments_raw: list[dict]) -> list[dict]:
    """Normalize the PR's issue-comment timeline for the reviewer prompt.

    These are the general discussion comments (not inline review comments).
    Stamphog's own bot comments are excluded and the same author-trust gate as
    reviews/inline comments applies, so an untrusted external commenter can't
    slip a fake maintainer hold or assurance claim into the prompt.
    """
    normalized = []
    for comment in comments_raw:
        user = comment.get("user") or {}
        if not _prompt_worthy_author(user.get("login"), comment.get("author_association"), user.get("type") == "Bot"):
            continue
        normalized.append(
            {
                "user": user.get("login", "ghost"),
                "body": comment.get("body", ""),
                "created_at": comment.get("created_at"),
            }
        )
    return normalized


# GitHub spells reaction contents two ways: REST returns "+1"/"-1", GraphQL
# returns "THUMBS_UP"/"THUMBS_DOWN". Normalize both to an emoji so the reviewer
# sees a consistent signal regardless of which API surfaced the reaction.
_REACTION_EMOJI = {
    "+1": "👍",
    "thumbs_up": "👍",
    "-1": "👎",
    "thumbs_down": "👎",
    "laugh": "😄",
    "hooray": "🎉",
    "confused": "😕",
    "heart": "❤️",
    "rocket": "🚀",
    "eyes": "👀",
}


def _reaction_emoji(content: str) -> str:
    """Map a GitHub reaction content string (REST or GraphQL) to an emoji.

    Unknown values pass through unchanged so a newly-added reaction type still
    surfaces rather than silently disappearing.
    """
    return _REACTION_EMOJI.get(content.lower(), content)


def _is_org_member(org: str, login: str) -> bool:
    """Best-effort org-membership check; False on any error (fail closed)."""
    try:
        result = subprocess.run(
            ["gh", "api", f"orgs/{org}/members/{login}"],
            capture_output=True,
            text=True,
            timeout=10,
        )
        return result.returncode == 0
    except subprocess.TimeoutExpired:
        return False


def _trusted_reactor_predicate(repo: str, author: str) -> Callable[[str], bool]:
    """Build a memoized `login -> bool` gate for whose reactions to trust.

    Reactions on a public PR can come from anyone, so only trust allowlisted
    reviewer bots and org members, and never the PR author (a self-reaction is
    not an independent signal). Unknown or erroring logins fail closed. Without
    this, any GitHub user could block auto-approval with an 👀 or fake an
    independent review with a 👍.
    """
    org = repo.split("/", 1)[0]
    cache: dict[str, bool] = {}

    def is_trusted(login: str) -> bool:
        if not login or login == "ghost" or login == author:
            return False
        if login not in cache:
            low = login.lower()
            if low.endswith("[bot]"):
                cache[login] = low in TRUSTED_REACTOR_BOTS
            else:
                cache[login] = _is_org_member(org, login)
        return cache[login]

    return is_trusted


def _normalize_reactions(node: dict, is_trusted: Callable[[str], bool]) -> list[dict]:
    """Normalize a GraphQL Reactable node's trusted reactions to [{user, emoji}].

    Works for any object that carries a `reactions` connection — the PR itself
    or an individual review comment. Reactions from untrusted actors (see
    `_trusted_reactor_predicate`) are dropped so they can't influence the verdict.
    """
    reactions = []
    for rn in (node.get("reactions") or {}).get("nodes") or []:
        login = (rn.get("user") or {}).get("login", "ghost")
        if is_trusted(login):
            reactions.append(
                {
                    "user": login,
                    "emoji": _reaction_emoji(rn.get("content", "")),
                    "created_at": rn.get("createdAt"),
                }
            )
    return reactions


def _gh_api(endpoint: str, *, paginate: bool = False) -> dict | list:
    cmd = ["gh", "api", endpoint]
    if paginate:
        # --paginate alone emits one JSON document per page, which json.loads
        # rejects on 2+ pages; --slurp wraps the pages in one outer array.
        cmd.extend(["--paginate", "--slurp"])
    else:
        sep = "&" if "?" in endpoint else "?"
        cmd[2] = f"{endpoint}{sep}per_page=100"
    result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
    if result.returncode != 0:
        raise RuntimeError(f"gh api {endpoint} failed: {result.stderr.strip()}")
    data = json.loads(result.stdout)
    if paginate:
        flat: list = []
        for page in data:
            if isinstance(page, list):
                flat.extend(page)
            else:
                flat.append(page)
        return flat
    return data


# GitHub rejects GraphQL queries whose worst-case node count — the product of
# nested `first:` sizes — exceeds 500,000, before executing anything.
_REVIEW_THREADS_QUERY = """
query($owner: String!, $name: String!, $pr: Int!, $threadCursor: String) {
  repository(owner: $owner, name: $name) {
    pullRequest(number: $pr) {
      reactions(first: 100) {
        nodes {
          content
          createdAt
          user { login }
        }
      }
      reviewThreads(first: 100, after: $threadCursor) {
        pageInfo { hasNextPage endCursor }
        nodes {
          isResolved
          isOutdated
          path
          line
          comments(first: 50) {
            pageInfo { hasNextPage }
            nodes {
              author { login __typename }
              authorAssociation
              body
              databaseId
              replyTo { databaseId }
              reactions(first: 20) {
                nodes {
                  content
                  createdAt
                  user { login }
                }
              }
            }
          }
        }
      }
    }
  }
}
"""


def _gh_graphql(query: str, variables: dict | None = None) -> dict:
    """Run a GraphQL query via gh api graphql with proper variable passing."""
    cmd = ["gh", "api", "graphql", "-f", f"query={query}"]
    for key, value in (variables or {}).items():
        if value is None:
            cmd.extend(["-F", f"{key}=null"])
        elif isinstance(value, int):
            cmd.extend(["-F", f"{key}={value}"])
        else:
            cmd.extend(["-f", f"{key}={value}"])
    result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
    if result.returncode != 0:
        raise RuntimeError(f"gh api graphql failed: {result.stderr.strip()}")
    data = json.loads(result.stdout)
    if "errors" in data:
        raise RuntimeError(f"GraphQL errors: {data['errors']}")
    return data


def _fetch_threads_and_reactions(repo: str, pr_number: int, author: str) -> tuple[list[dict], list[dict]]:
    """Fetch review-thread comments and reactions on the PR in one GraphQL call.

    Inline comments (each with their own reactions) and the reactions left on
    the PR itself come back from the same query, so no extra REST round trip is
    needed. Reactions are filtered to trusted, non-author actors. Returns
    (comments, pr_reactions); raises if any comment page is truncated.
    """
    owner, name = repo.split("/", 1)
    variables: dict = {"owner": owner, "name": name, "pr": pr_number, "threadCursor": None}
    is_trusted = _trusted_reactor_predicate(repo, author)

    comments: list[dict] = []
    pr_reactions: list[dict] = []
    while True:
        data = _gh_graphql(_REVIEW_THREADS_QUERY, variables)
        pull_request = data["data"]["repository"]["pullRequest"]
        # Reactions on the PR repeat on the pullRequest node on every page;
        # re-reading the (≤20) nodes each pass is trivial and avoids a flag.
        pr_reactions = _normalize_reactions(pull_request, is_trusted)
        review_threads = pull_request["reviewThreads"]
        threads = review_threads["nodes"]

        for thread in threads:
            comment_page = thread["comments"]
            if comment_page["pageInfo"]["hasNextPage"]:
                raise RuntimeError(
                    f"Review thread on {thread.get('path')}:{thread.get('line')} "
                    f"has >50 comments — pagination not implemented, escalate to human review"
                )
            for c in comment_page["nodes"]:
                # Same author-trust gate as reviews and discussion: stamphog's
                # own inline comments describe an earlier snapshot (feeding them
                # back reads as impersonation) and untrusted external commenters
                # are dropped. Reactions on these comments are also dropped — a
                # 👍 on stamphog's comment endorses stamphog's verdict, not the PR.
                comment_author = c.get("author") or {}
                if not _prompt_worthy_author(
                    comment_author.get("login"), c.get("authorAssociation"), comment_author.get("__typename") == "Bot"
                ):
                    continue
                reply_to = c.get("replyTo")
                comments.append(
                    {
                        "user": comment_author.get("login", "ghost"),
                        "body": c.get("body", ""),
                        "path": thread.get("path", ""),
                        "line": thread.get("line"),
                        "in_reply_to_id": reply_to["databaseId"] if reply_to else None,
                        "is_resolved": thread["isResolved"],
                        "is_outdated": thread["isOutdated"],
                        "reactions": _normalize_reactions(c, is_trusted),
                    }
                )

        page_info = review_threads["pageInfo"]
        if not page_info["hasNextPage"]:
            break
        variables["threadCursor"] = page_info["endCursor"]

    return comments, pr_reactions


def diff_range(base_sha: str, head_sha: str, merge_base_sha: str = "") -> str:
    """The git revision range of the PR's own changes.

    ``base...head`` diffs from the merge base, which git computes by walking history. A known merge
    base gives the same diff as ``merge_base..head`` and needs only the two commits, which is all a
    shallow checkout holds.
    """
    if merge_base_sha:
        return f"{merge_base_sha}..{head_sha}"
    return f"{base_sha}...{head_sha}"


def _git_diff_files(base_sha: str, head_sha: str, repo_root: Path, merge_base_sha: str = "") -> list[dict]:
    """Get changed files with line counts and status from the local checkout."""
    revisions = diff_range(base_sha, head_sha, merge_base_sha)
    run_opts = {"capture_output": True, "text": True, "timeout": 30, "cwd": repo_root}

    numstat = subprocess.run(["git", "diff", "--numstat", "-z", revisions], **run_opts)
    if numstat.returncode != 0:
        raise RuntimeError(f"git diff --numstat failed: {numstat.stderr.strip()}")

    name_status = subprocess.run(["git", "diff", "--name-status", "-z", revisions], **run_opts)
    if name_status.returncode != 0:
        raise RuntimeError(f"git diff --name-status failed: {name_status.stderr.strip()}")
    status_map: dict[str, str] = {}
    status_records = iter(name_status.stdout.split("\0"))
    for status in status_records:
        if not status:
            continue
        filename = next(status_records)
        if status.startswith(("R", "C")):
            filename = next(status_records)
        status_map[filename] = status

    files = []
    numstat_records = iter(numstat.stdout.split("\0"))
    for record in numstat_records:
        if not record:
            continue
        parts = record.split("\t", 2)
        if len(parts) != 3:
            continue
        added, deleted, filename = parts
        if not filename:
            # git_touched_paths keeps the old path for deny checks; file stats name only the destination.
            next(numstat_records)
            filename = next(numstat_records)
        is_binary = added == "-"
        files.append(
            {
                "filename": filename,
                "additions": int(added) if not is_binary else 0,
                "deletions": int(deleted) if not is_binary else 0,
                "binary": is_binary,
                "status": status_map.get(filename, "M"),
            }
        )
    return files


def git_touched_paths(base_sha: str, head_sha: str, repo_root: Path, merge_base_sha: str = "") -> list[str]:
    """Every path the diff touches, with renames split into their old and new names.

    `-z` keeps git from quoting a path with a newline or a quote in it, which would put a `"` in
    front of the path and defeat every anchored deny pattern.
    """
    result = subprocess.run(
        ["git", "diff", "--name-only", "--no-renames", "-z", diff_range(base_sha, head_sha, merge_base_sha)],
        capture_output=True,
        text=True,
        timeout=30,
        cwd=repo_root,
    )
    if result.returncode != 0:
        raise RuntimeError(f"git diff --name-only failed: {result.stderr.strip()}")
    return [path for path in result.stdout.split("\0") if path]


def new_diff_file(directory: Path) -> Path:
    """Create a fresh, empty diff file under an unpredictable name inside ``directory``.

    The directory can be PR-authored (the hosted sandbox's head checkout, or
    the Action's stacked-PR worktree), where a predictable name could be a
    tracked symlink redirecting the write. ``mkstemp`` creates a new regular
    file, so PR content cannot redirect it. Callers own the cleanup.
    """
    fd, path = tempfile.mkstemp(prefix=".pr-review-diff-", suffix=".patch", dir=directory)
    os.close(fd)
    return Path(path)


def write_pr_diff(base_sha: str, head_sha: str, repo_root: Path, merge_base_sha: str = "") -> Path:
    """Write the PR diff (see diff_range) to a fresh file in the checkout and return its path.

    Shared by the reviewer (feeds the LLM the diff to read) and the familiarity
    signal (parses the same diff for base-side modified line ranges), so the
    `git diff` invocation lives in one place.
    """
    dest = new_diff_file(repo_root)
    result = subprocess.run(
        ["git", "diff", diff_range(base_sha, head_sha, merge_base_sha)],
        capture_output=True,
        text=True,
        timeout=60,
        cwd=repo_root,
    )
    dest.write_text(result.stdout if result.returncode == 0 else f"git diff failed: {result.stderr}")
    return dest


def _have_commit(sha: str, repo_root: Path) -> bool:
    return (
        subprocess.run(
            ["git", "cat-file", "-t", sha],
            cwd=repo_root,
            capture_output=True,
            timeout=5,
        ).returncode
        == 0
    )


def ensure_commits(pr_number: int, head_sha: str, base_ref: str, base_sha: str, repo_root: Path) -> None:
    """Make the PR head and its base commit available locally.

    The workflow checks out master and fetches `pull/<n>/head`, which covers
    the common case. Two stacked-PR cases need more:
      - The head: fetched explicitly if the merge commit isn't present.
      - The base: a stacked PR targets its parent branch, not master, so
        `base_sha` is the parent's tip. It's usually an ancestor of the head
        (reachable once the head is fetched), but a rebased/force-pushed stack
        can leave it unreachable — fetch the base branch by name to be sure.
        `git diff base_sha...head_sha` (and dismiss_check's ancestry walk)
        need that object present. Best-effort: a missing base surfaces later
        as a diff error rather than a silent wrong scope.
    """
    if not _have_commit(head_sha, repo_root):
        subprocess.run(
            ["git", "fetch", "--filter=blob:none", "origin", f"pull/{pr_number}/head"],
            cwd=repo_root,
            capture_output=True,
            timeout=60,
        )
    if not _have_commit(base_sha, repo_root):
        subprocess.run(
            ["git", "fetch", "--filter=blob:none", "origin", base_ref],
            cwd=repo_root,
            capture_output=True,
            timeout=60,
        )


@dataclass(frozen=True)
class CommitProvenance:
    """Agent-authorship evidence parsed from the PR's commit-message trailers."""

    commit_count: int
    agent_commit_count: int
    generated_by: tuple[str, ...]
    task_ids: tuple[str, ...]

    @property
    def agent_authored(self) -> bool:
        return self.agent_commit_count > 0


# Record separator keeps multi-paragraph commit messages intact when splitting
# `git log --format=%B%x1e` output back into one message per commit.
_COMMIT_RECORD_SEPARATOR = "\x1e"

_GENERATED_BY_RE = re.compile(r"^generated-by:[ \t]*(.+?)[ \t]*$", re.IGNORECASE | re.MULTILINE)
_TASK_ID_RE = re.compile(r"^task-id:[ \t]*(.+?)[ \t]*$", re.IGNORECASE | re.MULTILINE)


def provenance_from_messages(raw_messages: list[str]) -> CommitProvenance:
    """Parse `Generated-By:` / `Task-Id:` trailers out of the PR's commit messages.

    Agent tooling stamps these trailers on every commit it authors, so any
    commit carrying one marks the PR as agent-authored. Values are collected
    de-duplicated in first-seen order. The messages come from `git log` on a
    local run and from GitHub's PR commits API on a hosted one.
    """
    messages = [m for m in (raw.strip() for raw in raw_messages) if m]
    agent_commits = 0
    generated_by: list[str] = []
    task_ids: list[str] = []
    for message in messages:
        generators = _GENERATED_BY_RE.findall(message)
        ids = _TASK_ID_RE.findall(message)
        if generators or ids:
            agent_commits += 1
        generated_by.extend(g for g in generators if g not in generated_by)
        task_ids.extend(t for t in ids if t not in task_ids)
    return CommitProvenance(
        commit_count=len(messages),
        agent_commit_count=agent_commits,
        generated_by=tuple(generated_by),
        task_ids=tuple(task_ids),
    )


def parse_provenance_trailers(log_output: str) -> CommitProvenance:
    """Parse `Generated-By:` / `Task-Id:` trailers out of `git log --format=%B%x1e` output."""
    return provenance_from_messages(log_output.split(_COMMIT_RECORD_SEPARATOR))


def pr_provenance(base_sha: str, head_sha: str, repo_root: Path) -> CommitProvenance | None:
    """Provenance trailers of the PR's own commits, or None when git fails.

    Reads the head commits (base..head) from the local checkout — never the
    squash-merge commit, which can drop or rewrite trailers depending on repo
    settings. `ensure_commits` has already fetched the head objects by the
    time this runs.
    """
    cmd = ["git", "log", f"{base_sha}..{head_sha}", f"--format=%B{_COMMIT_RECORD_SEPARATOR}"]
    try:
        result = subprocess.run(cmd, cwd=repo_root, capture_output=True, text=True, timeout=30)
    except (OSError, subprocess.SubprocessError):
        return None
    if result.returncode != 0:
        return None
    return parse_provenance_trailers(result.stdout)


def provenance_evidence(prov: CommitProvenance | None) -> dict | None:
    """Serialize a CommitProvenance for the output JSON (or null)."""
    if prov is None:
        return None
    return {
        "agent_authored": prov.agent_authored,
        "commit_count": prov.commit_count,
        "agent_commit_count": prov.agent_commit_count,
        "generated_by": list(prov.generated_by),
        "task_ids": list(prov.task_ids),
    }


def fetch_pr(pr_number: int, repo: str, repo_root: Path | None = None) -> PRData:
    """Fetch PR data: metadata from API, file stats from local git."""
    pr = _gh_api(f"repos/{repo}/pulls/{pr_number}")
    reviews_raw = _gh_api(f"repos/{repo}/pulls/{pr_number}/reviews", paginate=True)

    # Discussion is advisory context only. Skip it for bot PRs (refused before
    # any prompt is built) and never let a transient gh failure fail the run —
    # an exception here would turn the wait-loop refetch into a spurious WAIT.
    author_is_bot = is_bot_author(pr.get("user", {}))
    discussion: list[dict] = []
    if not author_is_bot:
        try:
            discussion_raw = _gh_api(f"repos/{repo}/issues/{pr_number}/comments", paginate=True)
            discussion = _normalize_discussion_for_prompt(discussion_raw)
        except Exception as exc:
            print(f"warning: discussion fetch failed ({exc}); continuing with no discussion context")  # noqa: T201

    base_ref = pr["base"]["ref"]
    default_branch = pr["base"]["repo"]["default_branch"]
    base_sha = pr["base"]["sha"]
    head_sha = pr["head"]["sha"]
    check_runs_resp = _gh_api(f"repos/{repo}/commits/{head_sha}/check-runs")

    git_root = repo_root or Path.cwd()
    ensure_commits(pr_number, head_sha, base_ref, base_sha, git_root)
    files = _git_diff_files(base_sha, head_sha, git_root)
    touched_paths = git_touched_paths(base_sha, head_sha, git_root)

    review_comments, pr_reactions = _fetch_threads_and_reactions(repo, pr_number, pr["user"]["login"])

    return PRData(
        number=pr_number,
        repo=repo,
        title=pr["title"],
        state=pr["state"],
        draft=pr.get("draft", False),
        mergeable_state=pr.get("mergeable_state", "unknown"),
        author=pr["user"]["login"],
        labels=[label["name"] for label in pr.get("labels", [])],
        base_ref=base_ref,
        base_sha=base_sha,
        head_sha=head_sha,
        files=files,
        touched_paths=touched_paths,
        reviews=_normalize_reviews_for_prompt(reviews_raw, head_sha),
        review_comments=review_comments,
        check_runs=check_runs_resp.get("check_runs", []),
        author_is_bot=author_is_bot,
        pr_reactions=pr_reactions,
        body=pr.get("body") or "",
        discussion=discussion,
        default_branch=default_branch,
    )


def check_team_membership(org: str, author: str, team_slug: str) -> bool:
    """Check if author is an active member of the given GitHub team in ``org``."""
    try:
        result = subprocess.run(
            ["gh", "api", f"orgs/{org}/teams/{team_slug}/memberships/{author}"],
            capture_output=True,
            text=True,
            timeout=10,
        )
        if result.returncode == 0:
            return json.loads(result.stdout).get("state") == "active"
    except (subprocess.TimeoutExpired, json.JSONDecodeError):
        pass
    return False