| # Copyright 2026 The Fuchsia Authors | |
| # | |
| # Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| # <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| # license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| # This file may not be copied, modified, or distributed except according to | |
| # those terms. | |
| """Evaluate Gate 2 cross-family reviews, legacy Codex summaries, and propose commit statuses. | |
| The deployed workflow runs this standard-library adapter only as manually | |
| dispatched, read-only diagnostics. The former publisher and deletion recorder | |
| remain available for the research tracked in issue #4013. Neither write mode | |
| runs in that workflow. PR code and comment contents are never executed. | |
| The default mode reads live inputs and prints proposed statuses. Explicit | |
| --publish and --record-deletion retain the former gate's two write effects. | |
| """ | |
| import argparse | |
| import base64 | |
| from collections import defaultdict | |
| from dataclasses import dataclass | |
| from datetime import datetime, timezone | |
| import difflib | |
| import functools | |
| import hashlib | |
| import json | |
| import io | |
| import os | |
| from pathlib import Path | |
| import re | |
| import sys | |
| import tomllib | |
| from urllib.error import HTTPError | |
| from urllib.request import Request, urlopen | |
| CONTEXT = "Codex review" | |
| # The gate uses the standard Actions token. Repository writers and ordinary | |
| # review of workflow changes are trusted; no separate publisher is provisioned. | |
| PUBLISHER_LOGIN = "github-actions[bot]" | |
| # Pin the account ID as well as its display name. A copied marker or a similarly | |
| # named account must not acquire authority to approve a revision. | |
| BOT_ID = 199175422 | |
| BOT_LOGIN = "chatgpt-codex-connector[bot]" | |
| SUMMARY = "<!-- codex-pull-request-review-summary" | |
| STATE = "<!-- codex-review-state:" | |
| GATE2_MARKER = "<!-- anneal-gate2-review:" | |
| GATE2_AUTHORIZED_ASSOCIATIONS = frozenset({"OWNER", "MEMBER", "COLLABORATOR"}) | |
| SHA1_RE = re.compile(r"[0-9a-f]{40}") | |
| SHA256_RE = re.compile(r"[0-9a-f]{64}") | |
| ONE_IDENTITY_RE = re.compile(r"^one:([0-9a-f]{40}):([0-9a-f]{40}):([0-9a-f]{64})$") | |
| MULTI_IDENTITY_RE = re.compile(r"^multi:([0-9a-f]{40}):([0-9a-f]{40})$") | |
| FAMILY_RE = re.compile(r"[a-z0-9][a-z0-9._-]{0,63}") | |
| DISALLOWED_FAMILIES = frozenset({ | |
| "unknown", "none", "null", "n/a", "na", "unspecified", "self", "same", "auto", | |
| }) | |
| CANONICAL_FAMILY_PREFIXES = ( | |
| ("claude", ("claude", "anthropic")), | |
| ("gemini", ("gemini", "google-gemini")), | |
| ("openai", ("openai", "gpt", "codex", "chatgpt", "o1", "o3", "o4")), | |
| ) | |
| GATE2_VERDICTS = frozenset({"clean", "findings", "incomplete"}) | |
| GATE2_SEVERITIES = frozenset({"P0", "P1", "P2", "P3"}) | |
| GATE2_REQUIRED_KEYS = frozenset({ | |
| "base_sha", "head_sha", "tree_sha", | |
| "reviewer_family", "author_family", | |
| "verdict", "findings", | |
| }) | |
| GATE2_OPTIONAL_KEYS = frozenset({ | |
| "parent_tree", "commit_msg_sha256", "reviewed_at", | |
| "round", "risk_tier", "merge_base", "identity_key", | |
| "panel_seats", "prior_findings_status", "coverage", | |
| "advisories", "review_mode", "delta_from", | |
| }) | |
| GATE2_FINDING_KEYS = frozenset({ | |
| "severity", "summary", "path", "line", "id", | |
| "fingerprint", "surface", "consensus_count", "raised_by", "repro_status", | |
| }) | |
| GATE2_PANEL_SEAT_KEYS = frozenset({ | |
| "seat", "family", "model_label", "role", | |
| "status", "verdict", "findings_count", "latency_sec", | |
| }) | |
| GATE2_REVIEW_MODES = frozenset({"full", "delta"}) | |
| # A delta review reads only the fix since the preceding full review, so it is | |
| # restricted strictly to non-TCB, non-policy risk tiers (`tier0_docs` and | |
| # `tier1_code`), risk tier may never escalate, and both frontier families | |
| # must complete. | |
| GATE2_DELTA_TIER_RANK = { | |
| "tier0_docs": 0, | |
| "tier1_code": 1, | |
| } | |
| GATE2_DELTA_RISK_TIERS = frozenset(GATE2_DELTA_TIER_RANK) | |
| GATE2_DELTA_FAMILIES = frozenset({"claude", "gemini"}) | |
| DELTA_MAX_FIX_LINES = 400 | |
| DELTA_MAX_FIX_FILES = 25 | |
| DELTA_MAX_FIX_FRACTION = 0.30 | |
| DELTA_MAX_BLOB_BYTES = 4 * 1024 * 1024 | |
| DELTA_MAX_BLOB_LINES = 50_000 | |
| DELTA_MAX_MATCHER_WORK = 10_000_000 | |
| DELTA_GENERATED_BASENAMES = frozenset({ | |
| "bundled_theory.json", | |
| }) | |
| DELTA_GENERATED_SUFFIXES = (".stderr", ".expected") | |
| # Whole-blob fail-closed directive, waiver, and proof-escape scanners. | |
| # Any edited file containing one of these tokens in either its base or head | |
| # blob is rejected from delta mode and requires a full Gate 2 panel. | |
| _DELTA_ANNEAL_IDENT_RE = re.compile(r"\b(?:r#)?anneal\b") | |
| _DELTA_TRUSTED_IDENT_RE = re.compile(r"\b(?:r#)?trusted\b") | |
| DELTA_DIRECTIVE_RE = re.compile( | |
| r"anneal-trust-tier:" | |
| r"|\b(?:sorry|sorryAx|admit|axiom|native_decide|bv_decide|bv_check" | |
| r"|implemented_by|csimp|ofReduceBool|trustCompiler)\b" | |
| ) | |
| _DELTA_LEAN_EXTERN_RE = re.compile(r"\bextern\b") | |
| _REPO_ROOT = Path(__file__).resolve().parents[1] | |
| GATE2_PRIOR_FINDING_STATUSES = frozenset({"closed", "still_open", "unclear"}) | |
| # Deletions remove evidence from the comments API. Record each deletion as an | |
| # immutable, uniquely named commit status on Anneal's initial commit. This | |
| # stable anchor survives branch moves without needing a database, a new ref, | |
| # contents-write permission, or a stored copy of the deleted prose. | |
| STATE_ANCHOR = "8950b5afa6444859c21fb4d100e21dd283afb6d6" | |
| DELETION_CONTEXT = "Codex summary deletion/" | |
| @dataclass(frozen=True) | |
| class Result: | |
| """A native commit-status state and the reason shown beside it.""" | |
| state: str | |
| reason: str | |
| def unique_object(pairs): | |
| """Reject ambiguous JSON rather than accepting the last duplicate key.""" | |
| result = {} | |
| for key, value in pairs: | |
| if key in result: | |
| raise ValueError(f"Duplicate JSON key: {key}") | |
| result[key] = value | |
| return result | |
| def authentic_summary(comment): | |
| """Authenticate the comment author, independently of the event sender.""" | |
| user = comment.get("user") or {} | |
| return (user.get("id") == BOT_ID and user.get("login") == BOT_LOGIN | |
| and user.get("type") == "Bot" | |
| and SUMMARY in (comment.get("body") or "")) | |
| def authentic_gate2_item(item, authorized_reviewers=None): | |
| """Authenticate a structured Gate 2 review or comment.""" | |
| user = item.get("user") or {} | |
| login = user.get("login") | |
| user_id = user.get("id") | |
| if (not isinstance(login, str) or not login | |
| or not isinstance(user_id, int) or isinstance(user_id, bool) | |
| or GATE2_MARKER not in (item.get("body") or "")): | |
| return False | |
| if authorized_reviewers is not None: | |
| allowed = {entry.casefold() if isinstance(entry, str) else entry | |
| for entry in authorized_reviewers} | |
| return login.casefold() in allowed or (user_id, login) in allowed | |
| return item.get("author_association") in GATE2_AUTHORIZED_ASSOCIATIONS | |
| def latest_summary(comments): | |
| """Use the same authoritative comment for policy and freshness checks.""" | |
| return max((c for c in comments if authentic_summary(c)), | |
| key=lambda c: c["id"], default=None) | |
| def summary_snapshot(comments): | |
| """Compare legacy Codex summary inputs, ignoring unrelated comments.""" | |
| latest = latest_summary(comments) | |
| return ((latest["id"], latest.get("created_at"), latest["body"]) | |
| if latest is not None else None) | |
| def codex_findings_snapshot(comments): | |
| """Snapshot all authentic Codex summaries carrying completed findings.""" | |
| items = [] | |
| for comment in comments: | |
| if not authentic_summary(comment): | |
| continue | |
| try: | |
| review = _parse_codex_summary(comment.get("body") or "") | |
| except ValueError: | |
| continue | |
| if _codex_summary_has_completed_findings(review): | |
| items.append((comment.get("id"), comment.get("created_at"), comment["body"])) | |
| items.sort(key=lambda row: ( | |
| 0 if isinstance(row[0], int) and not isinstance(row[0], bool) else 1, | |
| row[0] if isinstance(row[0], int) and not isinstance(row[0], bool) else str(row[0]), | |
| )) | |
| return tuple(items) | |
| def review_snapshot(comments, reviews=(), authorized_reviewers=None): | |
| """Compare Gate 2 and legacy Codex decision inputs during reconciliation.""" | |
| gate2_items = [] | |
| for source, entries in (("comment", comments), ("review", reviews)): | |
| for item in entries: | |
| if authentic_gate2_item(item, authorized_reviewers): | |
| ts_key = "submitted_at" if source == "review" else "created_at" | |
| gate2_items.append(( | |
| source, item.get("id"), item.get(ts_key), | |
| item.get("state"), item.get("commit_id"), item.get("body"), | |
| )) | |
| gate2_items.sort(key=lambda row: (row[0], str(row[1]))) | |
| return ( | |
| summary_snapshot(comments), | |
| codex_findings_snapshot(comments), | |
| tuple(gate2_items), | |
| ) | |
| def timestamp(value): | |
| """Reject missing or naive times instead of guessing a comparison zone.""" | |
| if not isinstance(value, str): | |
| raise ValueError("Expected an aware timestamp") | |
| parsed = datetime.fromisoformat(value.replace("Z", "+00:00")) | |
| if parsed.utcoffset() is None: | |
| raise ValueError("Expected a timezone") | |
| return parsed.astimezone(timezone.utc) | |
| def canonical_family(value, *, allow_human): | |
| """Normalize and validate a model-family token fail-closed.""" | |
| if not isinstance(value, str) or value != value.strip(): | |
| raise ValueError("Model family must be a trimmed string") | |
| token = value.casefold() | |
| if not FAMILY_RE.fullmatch(token) or token in DISALLOWED_FAMILIES: | |
| raise ValueError(f"Invalid model family: {value!r}") | |
| if not allow_human and token == "human": | |
| raise ValueError("Gate 2 reviewer_family cannot be human") | |
| for canonical, prefixes in CANONICAL_FAMILY_PREFIXES: | |
| for prefix in prefixes: | |
| if token == prefix or token.startswith((f"{prefix}-", f"{prefix}_", f"{prefix}.")): | |
| return canonical | |
| return token | |
| def _is_non_empty_str(value): | |
| return isinstance(value, str) and bool(value.strip()) | |
| def _is_int_ge(value, minimum): | |
| return isinstance(value, int) and not isinstance(value, bool) and value >= minimum | |
| def _validate_finding(entry): | |
| if not isinstance(entry, dict): | |
| raise ValueError("Gate 2 finding must be an object") | |
| if not {"severity", "summary"} <= set(entry) or not set(entry) <= GATE2_FINDING_KEYS: | |
| raise ValueError("Invalid Gate 2 finding keys") | |
| severity = entry["severity"] | |
| if not isinstance(severity, str) or severity not in GATE2_SEVERITIES: | |
| raise ValueError("Invalid Gate 2 finding severity") | |
| if not _is_non_empty_str(entry["summary"]): | |
| raise ValueError("Gate 2 finding summary must be non-empty") | |
| for str_key in ("path", "id", "fingerprint", "surface", "repro_status"): | |
| if str_key in entry and entry[str_key] is not None: | |
| if not _is_non_empty_str(entry[str_key]): | |
| raise ValueError(f"Gate 2 finding {str_key} must be a non-empty string or null") | |
| for int_key in ("line", "consensus_count"): | |
| if int_key in entry and entry[int_key] is not None: | |
| if not _is_int_ge(entry[int_key], 1): | |
| raise ValueError(f"Gate 2 finding {int_key} must be a positive integer or null") | |
| if "raised_by" in entry and entry["raised_by"] is not None: | |
| raised_by = entry["raised_by"] | |
| if not isinstance(raised_by, list) or not all(_is_non_empty_str(s) for s in raised_by): | |
| raise ValueError("Gate 2 finding raised_by must be a list of non-empty strings") | |
| def _validate_panel_seats(seats): | |
| if not isinstance(seats, list): | |
| raise ValueError("Gate 2 panel_seats must be a list") | |
| completed_families = [] | |
| for seat in seats: | |
| if not isinstance(seat, dict): | |
| raise ValueError("Gate 2 panel seat must be an object") | |
| keys = set(seat) | |
| if not {"seat", "family", "status", "verdict"} <= keys or not keys <= GATE2_PANEL_SEAT_KEYS: | |
| raise ValueError("Invalid Gate 2 panel seat keys") | |
| if not _is_non_empty_str(seat["seat"]) or not _is_non_empty_str(seat["status"]): | |
| raise ValueError("Gate 2 panel seat and status must be non-empty") | |
| fam = canonical_family(seat["family"], allow_human=False) | |
| if fam == "multi": | |
| raise ValueError("Gate 2 panel seat family cannot be multi") | |
| if not _is_non_empty_str(seat["verdict"]): | |
| raise ValueError("Gate 2 panel seat verdict must be non-empty") | |
| if seat["status"] == "completed": | |
| if seat["verdict"] not in GATE2_VERDICTS: | |
| raise ValueError("Invalid completed Gate 2 panel seat verdict") | |
| completed_families.append(fam) | |
| for opt_str in ("model_label", "role"): | |
| if opt_str in seat and seat[opt_str] is not None and not _is_non_empty_str(seat[opt_str]): | |
| raise ValueError(f"Gate 2 panel seat {opt_str} must be non-empty") | |
| if "findings_count" in seat and seat["findings_count"] is not None: | |
| if not _is_int_ge(seat["findings_count"], 0): | |
| raise ValueError("Gate 2 panel seat findings_count must be a non-negative integer") | |
| if "latency_sec" in seat and seat["latency_sec"] is not None: | |
| lat = seat["latency_sec"] | |
| if not isinstance(lat, (int, float)) or isinstance(lat, bool) or lat < 0: | |
| raise ValueError("Gate 2 panel seat latency_sec must be a non-negative number") | |
| return completed_families | |
| def _validate_prior_findings_status(items, verdict): | |
| if not isinstance(items, list): | |
| raise ValueError("Gate 2 prior_findings_status must be a list") | |
| for item in items: | |
| if isinstance(item, dict): | |
| if not _is_non_empty_str(item.get("id")) or not _is_non_empty_str(item.get("status")): | |
| raise ValueError("Gate 2 prior_findings_status entry requires id and status") | |
| if verdict == "clean" and item["status"] == "still_open": | |
| raise ValueError("Clean Gate 2 verdict cannot retain still_open prior findings") | |
| elif not _is_non_empty_str(item): | |
| raise ValueError("Invalid Gate 2 prior_findings_status entry") | |
| def _validate_coverage(coverage, verdict): | |
| if not isinstance(coverage, dict): | |
| raise ValueError("Gate 2 coverage must be an object") | |
| for key, val in coverage.items(): | |
| if not _is_non_empty_str(key) or not _is_int_ge(val, 0): | |
| raise ValueError("Gate 2 coverage entries must be non-negative integers") | |
| if verdict == "clean" and coverage.get("blocked", 0) > 0: | |
| raise ValueError("Clean Gate 2 verdict cannot have blocked coverage") | |
| def parse_gate2_payload(body, *, strict_identity_key=True): | |
| """Parse and validate an anneal-gate2-review:v1 structured payload.""" | |
| markers = re.findall(r"<!-- anneal-gate2-review:.*?-->", body, re.DOTALL) | |
| if body.count(GATE2_MARKER) != 1 or len(markers) != 1: | |
| raise ValueError("Ambiguous Gate 2 review marker") | |
| match = re.fullmatch(r"<!-- anneal-gate2-review:v1 (.*?) -->", markers[0], | |
| re.DOTALL) | |
| if match is None: | |
| raise ValueError("Unsupported Gate 2 review version") | |
| payload = json.loads(match[1], object_pairs_hook=unique_object) | |
| if not isinstance(payload, dict): | |
| raise ValueError("Gate 2 review payload must be an object") | |
| keys = set(payload) | |
| if not GATE2_REQUIRED_KEYS <= keys or not keys <= (GATE2_REQUIRED_KEYS | GATE2_OPTIONAL_KEYS): | |
| raise ValueError("Invalid Gate 2 review payload keys") | |
| for sha_key in ("base_sha", "head_sha", "tree_sha"): | |
| val = payload[sha_key] | |
| if not isinstance(val, str) or not SHA1_RE.fullmatch(val): | |
| raise ValueError(f"Invalid Gate 2 {sha_key}") | |
| if "parent_tree" in payload and payload["parent_tree"] is not None and ( | |
| not isinstance(payload["parent_tree"], str) | |
| or not SHA1_RE.fullmatch(payload["parent_tree"])): | |
| raise ValueError("Invalid Gate 2 parent_tree") | |
| if "commit_msg_sha256" in payload and payload["commit_msg_sha256"] is not None and ( | |
| not isinstance(payload["commit_msg_sha256"], str) | |
| or not SHA256_RE.fullmatch(payload["commit_msg_sha256"])): | |
| raise ValueError("Invalid Gate 2 commit_msg_sha256") | |
| if "merge_base" in payload and ( | |
| not isinstance(payload["merge_base"], str) | |
| or not SHA1_RE.fullmatch(payload["merge_base"])): | |
| raise ValueError("Invalid Gate 2 merge_base") | |
| if "round" in payload and not _is_int_ge(payload["round"], 1): | |
| raise ValueError("Invalid Gate 2 round") | |
| if "risk_tier" in payload and not _is_non_empty_str(payload["risk_tier"]): | |
| raise ValueError("Invalid Gate 2 risk_tier") | |
| if "identity_key" in payload and strict_identity_key: | |
| ik = payload["identity_key"] | |
| if not _is_non_empty_str(ik): | |
| raise ValueError("Invalid Gate 2 identity_key") | |
| one_m = ONE_IDENTITY_RE.fullmatch(ik) | |
| multi_m = MULTI_IDENTITY_RE.fullmatch(ik) | |
| if one_m is not None: | |
| ik_parent_tree, ik_tree, ik_msg_hash = one_m.groups() | |
| if ik_tree != payload["tree_sha"]: | |
| raise ValueError("Gate 2 identity_key tree_sha mismatch") | |
| if payload.get("parent_tree") is not None and ik_parent_tree != payload["parent_tree"]: | |
| raise ValueError("Gate 2 identity_key parent_tree mismatch") | |
| if payload.get("commit_msg_sha256") is not None and ik_msg_hash != payload["commit_msg_sha256"]: | |
| raise ValueError("Gate 2 identity_key commit_msg_sha256 mismatch") | |
| elif multi_m is not None: | |
| ik_base, ik_head = multi_m.groups() | |
| if ik_base != payload["base_sha"] or ik_head != payload["head_sha"]: | |
| raise ValueError("Gate 2 identity_key base/head mismatch") | |
| elif ik.strip().casefold().startswith(("one:", "multi:")): | |
| raise ValueError("Invalid Gate 2 identity_key format") | |
| if "reviewed_at" in payload: | |
| timestamp(payload["reviewed_at"]) | |
| reviewer_norm = canonical_family(payload["reviewer_family"], allow_human=False) | |
| author_norm = canonical_family(payload["author_family"], allow_human=True) | |
| verdict = payload["verdict"] | |
| if verdict not in GATE2_VERDICTS: | |
| raise ValueError("Invalid Gate 2 verdict") | |
| findings = payload["findings"] | |
| if not isinstance(findings, list): | |
| raise ValueError("Gate 2 findings must be a list") | |
| for entry in findings: | |
| _validate_finding(entry) | |
| if verdict == "clean" and findings: | |
| raise ValueError("Clean Gate 2 verdict cannot include findings") | |
| if verdict == "findings" and not findings: | |
| raise ValueError("Findings Gate 2 verdict requires non-empty findings") | |
| if "advisories" in payload: | |
| advisories = payload["advisories"] | |
| if not isinstance(advisories, list): | |
| raise ValueError("Gate 2 advisories must be a list") | |
| for entry in advisories: | |
| _validate_finding(entry) | |
| if entry["severity"] != "P3": | |
| raise ValueError("Gate 2 advisories must have severity P3") | |
| if "prior_findings_status" in payload: | |
| _validate_prior_findings_status(payload["prior_findings_status"], verdict) | |
| if "coverage" in payload: | |
| _validate_coverage(payload["coverage"], verdict) | |
| completed_seat_families = None | |
| if "panel_seats" in payload: | |
| completed_seat_families = _validate_panel_seats(payload["panel_seats"]) | |
| if reviewer_norm == "multi" and ( | |
| completed_seat_families is None or not payload["panel_seats"]): | |
| raise ValueError("Multi reviewer_family requires non-empty panel_seats") | |
| _validate_review_mode(payload) | |
| return payload, reviewer_norm, author_norm, completed_seat_families | |
| def _all_seats_finished(seats): | |
| """Whether a validated `panel_seats` is non-empty and every seat finished. | |
| A finished seat completed with a `clean` or `findings` verdict; a seat | |
| that completed with an `incomplete` verdict did not finish its review. | |
| """ | |
| return bool(seats) and all( | |
| seat["status"] == "completed" and seat["verdict"] in ("clean", "findings") | |
| for seat in seats | |
| ) | |
| def _validate_review_mode(payload): | |
| """Validate the generic shape of `review_mode` and `delta_from`. | |
| Only the shape is schema: a delta review that misses an eligibility | |
| condition still parses, so its findings keep blocking like any other | |
| review's. `_delta_review_problem` checks those conditions after parsing, | |
| where they can only keep a passing delta review pending. | |
| """ | |
| mode = payload.get("review_mode", "full") | |
| if not isinstance(mode, str) or mode not in GATE2_REVIEW_MODES: | |
| raise ValueError("Invalid Gate 2 review_mode") | |
| if "delta_from" in payload: | |
| if mode != "delta": | |
| raise ValueError("Gate 2 delta_from requires review_mode delta") | |
| delta_from = payload["delta_from"] | |
| if not isinstance(delta_from, str) or not SHA1_RE.fullmatch(delta_from): | |
| raise ValueError("Invalid Gate 2 delta_from") | |
| def _delta_payload_problem(payload, completed_seat_families): | |
| """Why a parsed delta review's own fields rule it out, or None.""" | |
| delta_from = payload.get("delta_from") | |
| if delta_from is None: | |
| return "Delta Gate 2 review has no delta_from" | |
| if delta_from == payload["head_sha"]: | |
| return "Delta Gate 2 review delta_from must differ from head_sha" | |
| if not _is_int_ge(payload.get("round"), 2): | |
| return "Delta Gate 2 review requires round 2 or later" | |
| if payload.get("risk_tier") not in GATE2_DELTA_RISK_TIERS: | |
| return "Delta Gate 2 review requires a non-TCB risk_tier" | |
| if (not _all_seats_finished(payload.get("panel_seats")) | |
| or not GATE2_DELTA_FAMILIES <= set(completed_seat_families or ())): | |
| return ("Delta Gate 2 review requires every panel seat completed, " | |
| "including Claude and Gemini seats") | |
| audited = payload.get("prior_findings_status") | |
| if not isinstance(audited, list): | |
| return "Delta Gate 2 review requires prior_findings_status" | |
| ids = set() | |
| for item in audited: | |
| if (not isinstance(item, dict) | |
| or set(item) != {"id", "status"} | |
| or item["status"] not in GATE2_PRIOR_FINDING_STATUSES | |
| or item["id"] in ids): | |
| return ("Delta Gate 2 prior_findings_status needs one entry per id " | |
| "with a closed, still_open, or unclear status") | |
| ids.add(item["id"]) | |
| return None | |
| def _gate2_item_timestamp(source, item): | |
| raw = item.get("submitted_at") if source == "review" else item.get("created_at") | |
| return timestamp(raw) | |
| def _is_structured_identity_key(value): | |
| return isinstance(value, str) and ( | |
| ONE_IDENTITY_RE.fullmatch(value) is not None | |
| or MULTI_IDENTITY_RE.fullmatch(value) is not None | |
| ) | |
| def _is_one_commit_identity_match(payload, head, base_sha, tree_sha, | |
| identity_key, historical_identities=None): | |
| if not ( | |
| isinstance(identity_key, str) | |
| and ONE_IDENTITY_RE.fullmatch(identity_key) is not None | |
| and payload.get("identity_key") == identity_key | |
| and tree_sha is not None | |
| and payload["tree_sha"] == tree_sha | |
| and base_sha is not None | |
| and (payload.get("merge_base") is None | |
| or payload["merge_base"] == payload["base_sha"]) | |
| ): | |
| return False | |
| p_head = payload["head_sha"] | |
| p_base = payload["base_sha"] | |
| if p_head == head and p_base == base_sha: | |
| return True | |
| if not isinstance(historical_identities, dict): | |
| return False | |
| return historical_identities.get((p_head, p_base)) == (tree_sha, identity_key) | |
| def _has_verified_historical_one_commit_identity(payload, head, base_sha, | |
| tree_sha, identity_key, | |
| historical_identities): | |
| if not ( | |
| isinstance(identity_key, str) | |
| and ONE_IDENTITY_RE.fullmatch(identity_key) is not None | |
| and tree_sha is not None | |
| and payload["tree_sha"] == tree_sha | |
| and base_sha is not None | |
| and isinstance(historical_identities, dict) | |
| ): | |
| return False | |
| p_head = payload["head_sha"] | |
| p_base = payload["base_sha"] | |
| if (p_head, p_base) == (head, base_sha): | |
| return False | |
| return historical_identities.get((p_head, p_base)) == (tree_sha, identity_key) | |
| def _has_verified_historical_same_tree(payload, head, base_sha, tree_sha, | |
| historical_identities, | |
| historical_trees=None): | |
| if ( | |
| tree_sha is None | |
| or payload["tree_sha"] != tree_sha | |
| or base_sha is None | |
| ): | |
| return False | |
| p_head = payload["head_sha"] | |
| p_base = payload["base_sha"] | |
| if (p_head, p_base) == (head, base_sha): | |
| return False | |
| # Caller-supplied tree_sha is head's verified tree, so p_head == head | |
| # confirms the tree across base_sha retargets without historical_trees. | |
| if p_head == head: | |
| return True | |
| if isinstance(historical_trees, dict) and historical_trees.get(p_head) == tree_sha: | |
| return True | |
| if isinstance(historical_identities, dict): | |
| hist = historical_identities.get((p_head, p_base)) | |
| return hist is not None and hist[0] == tree_sha | |
| return False | |
| def _matches_historical_codex_commit(commit_sha, tree_sha, | |
| historical_identities, | |
| historical_trees=None): | |
| if not ( | |
| isinstance(commit_sha, str) | |
| and SHA1_RE.fullmatch(commit_sha) is not None | |
| and tree_sha is not None | |
| ): | |
| return False | |
| if isinstance(historical_trees, dict) and historical_trees.get(commit_sha) == tree_sha: | |
| return True | |
| if not isinstance(historical_identities, dict): | |
| return False | |
| # Block active completed legacy Codex findings whenever GitHub confirms | |
| # the historical commit's tree matches the current PR tree. | |
| return any( | |
| p_head == commit_sha and val is not None and val[0] == tree_sha | |
| for (p_head, _p_base), val in historical_identities.items() | |
| ) | |
| def _matches_gate2_identity(payload, head, base_sha, tree_sha, | |
| identity_key=None, historical_identities=None): | |
| if _is_one_commit_identity_match( | |
| payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities=historical_identities, | |
| ): | |
| return True | |
| return ( | |
| payload["head_sha"] == head | |
| and (base_sha is None or payload["base_sha"] == base_sha) | |
| and (tree_sha is None or payload["tree_sha"] == tree_sha) | |
| ) | |
| # Rebase-only carry-forward. A base change may touch this PR file only when | |
| # the PR's own added and removed lines in it are unchanged: each line names | |
| # its file and digest, so a line's meaning does not depend on its context. | |
| CARRY_FORWARD_OVERLAP_PATHS = frozenset({ | |
| "trusted/root-manifest.sha256", | |
| "anneal/trusted/root-manifest.sha256", | |
| }) | |
| # Base changes to the rules that govern reviews (or to this evaluator) | |
| # always need a new review: CI cannot tell whether they got stricter. | |
| CARRY_FORWARD_POLICY_PATHS = frozenset({ | |
| ".gemini/styleguide.md", "anneal/.gemini/styleguide.md", | |
| "docs/codex-review.md", "docs/review-policy-design.md", | |
| "docs/rustc-workarounds.md", "docs/trust-model.md", | |
| "anneal/docs/codex-review.md", "anneal/docs/review-policy-design.md", | |
| "anneal/docs/rustc-workarounds.md", "anneal/docs/trust-model.md", | |
| "experimental/README.md", "anneal/experimental/README.md", | |
| "ci/codex_review.py", "anneal/ci/codex_review.py", | |
| "META_PRINCIPLES.md", "PRINCIPLES.md", "THREAT_MODEL.md", | |
| "anneal/META_PRINCIPLES.md", "anneal/PRINCIPLES.md", | |
| "anneal/THREAT_MODEL.md", | |
| }) | |
| # Agent instruction files anywhere in the tree. | |
| CARRY_FORWARD_POLICY_BASENAMES = frozenset({ | |
| "AGENTS.md", "AGENTS.override.md", "CLAUDE.md", "GEMINI.md", | |
| }) | |
| CARRY_FORWARD_POLICY_PREFIXES = ( | |
| ".github/", "anneal/.github/", "skills/", "anneal/skills/", | |
| ) | |
| # Review-governing config basenames blocked on delta base advances alongside | |
| # any dot-prefixed file or directory. | |
| DELTA_CONFIG_BASENAMES = CARRY_FORWARD_POLICY_BASENAMES | frozenset({ | |
| ".mcp.json", ".cursorrules", ".windsurfrules", ".clinerules", | |
| ".envrc", ".gitattributes", ".gitmodules", ".dockerignore", | |
| }) | |
| CI_POLICY_CONFIG_PATHS = frozenset({"ci/checks.toml", ".github/CODEOWNERS"}) | |
| # Static defense-in-depth TCB, CI, workflow, policy, build/manifest/lock/pin, | |
| # skill, vendor, proc-macro, and executable-script surfaces excluded from delta | |
| # mode (in addition to every `.github/CODEOWNERS` and `ci/checks.toml` rule and | |
| # every dot-prefixed path). | |
| DELTA_PROTECTED_PATHS = CARRY_FORWARD_POLICY_PATHS | CI_POLICY_CONFIG_PATHS | frozenset({ | |
| "cargo.sh", "zerocopy/cargo.sh", | |
| "theory/bundle-manifest.json", | |
| "tests/cargo-anneal/tests/fixtures/starnix/source.json", | |
| "anneal/theory/bundle-manifest.json", | |
| "anneal/tests/cargo-anneal/tests/fixtures/starnix/source.json", | |
| }) | |
| DELTA_PROTECTED_BASENAMES = CARRY_FORWARD_POLICY_BASENAMES | frozenset({ | |
| "root-manifest.sha256", "foundation-lock.json", "lake-manifest.json", | |
| "library-lock.json", "bundle-manifest.json", "miri_oracle_pin.json", | |
| "UPSTREAM_PIN.json", | |
| "build.rs", "Makefile", "GNUmakefile", "Justfile", | |
| "Dockerfile", "SKILL.md", "rustfmt.toml", "clippy.toml", | |
| }) | |
| DELTA_PROTECTED_BASENAME_PREFIXES = ( | |
| "Cargo.toml", "Cargo.lock", "flake.", | |
| "lakefile", "lean-toolchain", "rust-toolchain", | |
| ) | |
| DELTA_PROTECTED_PREFIXES = CARRY_FORWARD_POLICY_PREFIXES + ( | |
| "trusted/", "anneal/trusted/", | |
| "anneal/ci/", "ci/", "zerocopy/ci/", | |
| "anneal/vendor/", "vendor/", "zerocopy/vendor/", | |
| "zerocopy/zerocopy-derive/", "zerocopy-derive/", | |
| "githooks/", | |
| "tools/", "anneal/tools/", | |
| ) | |
| DELTA_PROTECTED_SUFFIXES = ( | |
| "styleguide.md", "codex-review.md", "review-policy-design.md", | |
| ".Dockerfile", ".sh", ".py", ".bat", ".cmd", ".ps1", | |
| ".nix", ".yml", ".yaml", | |
| ) | |
| DELTA_TIER1_CODE_PREFIXES = ( | |
| "theory/", "correspondence/", "crates/", | |
| "anneal/theory/", "anneal/correspondence/", "anneal/tests/", "exocrate/", | |
| "verification/", "zerocopy/", "src/", "tests/", | |
| ) | |
| DELTA_TIER1_CODE_EXTS = (".rs", ".lean", ".toml") | |
| # GitHub truncates a commit's or comparison's file list at 300 entries. | |
| MAX_API_FILES = 300 | |
| _DIFF_HEADER_RE = re.compile(r"^diff --git a/(.*) b/(.*)$") | |
| _DIFF_MODE_RE = re.compile( | |
| r"^(?:old mode|new mode|new file mode|deleted file mode) ([0-7]{6})$" | |
| ) | |
| _DIFF_INDEX_MODE_RE = re.compile(r"^index [0-9a-f]+\.\.[0-9a-f]+ ([0-7]{6})$") | |
| def _patch_change_lines(patch): | |
| """A unified diff's added, removed, and no-newline lines, in order.""" | |
| return [line for line in patch.split("\n") | |
| if line.startswith(("+", "-", "\\"))] | |
| def _file_change_key(entry, relaxed_paths): | |
| if not isinstance(entry, dict): | |
| raise ValueError("Invalid commit file entry") | |
| name = entry.get("filename") | |
| status = entry.get("status") | |
| patch = entry.get("patch") | |
| previous = entry.get("previous_filename") | |
| blob = entry.get("sha") | |
| if (not _is_non_empty_str(name) or not _is_non_empty_str(status) | |
| or not isinstance(patch, str) | |
| or (previous is not None and not _is_non_empty_str(previous))): | |
| # A missing patch (binary or oversized file) cannot be compared. | |
| raise ValueError("Uncomparable commit file entry") | |
| if name in relaxed_paths and status == "modified": | |
| # The base changed this allowlisted file, so its blob differs. | |
| content = None | |
| elif isinstance(blob, str) and SHA1_RE.fullmatch(blob) is not None: | |
| # The base did not touch the file, so the result must be identical. | |
| content = blob | |
| else: | |
| raise ValueError("Commit file entry has no blob SHA") | |
| return name, (status, previous, content, tuple(_patch_change_lines(patch))) | |
| def rebase_only_patch_equivalent(old_files, new_files, relaxed_paths=frozenset()): | |
| """Whether two one-commit patches make exactly the same changes. | |
| Context lines and hunk positions may differ. The file set, statuses, | |
| rename sources, added and removed lines, and resulting blobs may not, | |
| except that `relaxed_paths` (allowlisted files the base changed) skip | |
| the blob comparison. | |
| """ | |
| if (not isinstance(old_files, list) or not isinstance(new_files, list) | |
| or not old_files or len(old_files) >= MAX_API_FILES | |
| or len(new_files) >= MAX_API_FILES): | |
| return False | |
| try: | |
| old = dict(_file_change_key(f, relaxed_paths) for f in old_files) | |
| new = dict(_file_change_key(f, relaxed_paths) for f in new_files) | |
| except ValueError: | |
| return False | |
| return len(old) == len(old_files) and len(new) == len(new_files) and old == new | |
| def _change_set(files): | |
| """A commit's file names and added and removed lines, base-independent. | |
| Returns None when a file has no comparable patch. | |
| """ | |
| if not isinstance(files, list) or len(files) >= MAX_API_FILES: | |
| return None | |
| changes = set() | |
| for entry in files: | |
| if (not isinstance(entry, dict) or not _is_non_empty_str(entry.get("filename")) | |
| or not isinstance(entry.get("patch"), str)): | |
| return None | |
| changes.add((entry["filename"], tuple(_patch_change_lines(entry["patch"])))) | |
| return frozenset(changes) | |
| def diff_file_modes(diff_text): | |
| """Maps each file in a `git diff` to its mode header lines. | |
| GitHub's JSON file list omits modes, so they come from the raw diff. | |
| Returns None when the diff cannot be parsed. | |
| """ | |
| if not isinstance(diff_text, str): | |
| return None | |
| modes, current = {}, None | |
| for line in diff_text.split("\n"): | |
| if line.startswith("diff --git "): | |
| header = _DIFF_HEADER_RE.match(line) | |
| if header is None or header.group(2) in modes: | |
| return None | |
| current = header.group(2) | |
| modes[current] = [] | |
| elif current is None: | |
| continue | |
| elif line.startswith(("@@", "--- ", "+++ ", "Binary ")): | |
| current = None | |
| elif _DIFF_MODE_RE.match(line): | |
| modes[current].append(line) | |
| else: | |
| index = _DIFF_INDEX_MODE_RE.match(line) | |
| if index: | |
| modes[current].append(f"index {index.group(1)}") | |
| # `git diff` names a mode for every file; a section without one means | |
| # an unexpected or truncated format. | |
| if any(not lines for lines in modes.values()): | |
| return None | |
| return modes | |
| def pr_paths(files): | |
| paths = set() | |
| for entry in files: | |
| if not isinstance(entry, dict) or not _is_non_empty_str(entry.get("filename")): | |
| raise ValueError("Invalid commit file entry") | |
| paths.add(entry["filename"]) | |
| previous = entry.get("previous_filename") | |
| if previous is not None: | |
| if not _is_non_empty_str(previous): | |
| raise ValueError("Invalid commit file entry") | |
| paths.add(previous) | |
| return paths | |
| def base_drift_allows_carry_forward(compare_status, base_files, paths): | |
| """Returns the allowlisted PR files the base touched, or None. | |
| The base must have moved strictly forward without touching any PR file | |
| outside the allowlist or any review-governing file; the caller still | |
| requires the PR's own lines in the returned files to be unchanged. | |
| """ | |
| if (compare_status != "ahead" or not isinstance(base_files, list) | |
| or not base_files or len(base_files) >= MAX_API_FILES): | |
| return None | |
| relaxed = set() | |
| for entry in base_files: | |
| if not isinstance(entry, dict) or not _is_non_empty_str(entry.get("filename")): | |
| return None | |
| previous = entry.get("previous_filename") | |
| if previous is not None and not _is_non_empty_str(previous): | |
| return None | |
| touched = {entry["filename"]} | ({previous} if previous else set()) | |
| if any(path in CARRY_FORWARD_POLICY_PATHS | |
| or path.startswith(CARRY_FORWARD_POLICY_PREFIXES) | |
| or path.rsplit("/", 1)[-1] in CARRY_FORWARD_POLICY_BASENAMES | |
| for path in touched): | |
| return None | |
| for path in touched & paths: | |
| if path not in CARRY_FORWARD_OVERLAP_PATHS: | |
| return None | |
| relaxed.add(path) | |
| return frozenset(relaxed) | |
| def _is_delta_generated_path(path): | |
| return (path.rsplit("/", 1)[-1] in DELTA_GENERATED_BASENAMES | |
| or path.endswith(DELTA_GENERATED_SUFFIXES)) | |
| def _is_delta_dot_path(path): | |
| """Whether any path component is dot-prefixed (hidden config/meta).""" | |
| return any(seg.startswith(".") for seg in path.split("/")) | |
| def _min_path_risk_tier(path): | |
| """Minimum Gate 2 risk tier for `path`, or `'tier2'` when protected. | |
| Returns `'tier2'` (outside `GATE2_DELTA_TIER_RANK`) for every TCB, CI, | |
| workflow, CODEOWNERS, policy, build/manifest/lock/pin, skill, vendor, | |
| proc-macro, dot-prefixed, or script/config path, as well as when | |
| `ci/checks.toml` or `.github/CODEOWNERS` cannot be loaded. | |
| """ | |
| rules = _get_ci_policy_rules() | |
| if rules is None: | |
| return "tier2" | |
| name = path.rsplit("/", 1)[-1] | |
| if (_is_delta_dot_path(path) | |
| or path in DELTA_PROTECTED_PATHS | |
| or path.startswith(DELTA_PROTECTED_PREFIXES) | |
| or "skills/trust-review/" in path | |
| or name in DELTA_PROTECTED_BASENAMES | |
| or name.startswith(DELTA_PROTECTED_BASENAME_PREFIXES) | |
| or name.endswith(DELTA_PROTECTED_SUFFIXES) | |
| or any(_match_path_pattern(path, pat) | |
| for pat in rules.protected_patterns)): | |
| return "tier2" | |
| if ((path.startswith(DELTA_TIER1_CODE_PREFIXES) | |
| or path.endswith(DELTA_TIER1_CODE_EXTS)) | |
| and not path.endswith((".stderr", ".expected"))): | |
| return "tier1_code" | |
| return "tier0_docs" | |
| def _delta_base_advance_allows(status, files, paths): | |
| """Whether `a_base..d_base` allows a Tier 0/1 delta review.""" | |
| relaxed = base_drift_allows_carry_forward(status, files, paths) | |
| if relaxed != frozenset(): | |
| return False | |
| for entry in files: | |
| for cand in (entry.get("filename"), entry.get("previous_filename")): | |
| if cand and ( | |
| cand in CI_POLICY_CONFIG_PATHS | |
| or _is_delta_dot_path(cand) | |
| or cand.rsplit("/", 1)[-1] in DELTA_CONFIG_BASENAMES | |
| ): | |
| return False | |
| return True | |
| def _git_blob_sha(raw): | |
| """Return the lowercase 40-hex Git blob SHA-1 for `raw` bytes.""" | |
| return hashlib.sha1( | |
| f"blob {len(raw)}\0".encode("ascii") + raw | |
| ).hexdigest() | |
| @dataclass(frozen=True) | |
| class _CiPolicyRules: | |
| """Trusted CI build-input and CODEOWNERS rules parsed from repo config.""" | |
| protected_patterns: tuple | |
| config_blob_shas: tuple | |
| def _parse_glob_pattern(raw, *, codeowners=False): | |
| """Compile a validated repo-relative glob or CODEOWNERS rule, or None.""" | |
| if (not _is_non_empty_str(raw) or raw != raw.strip() | |
| or any(ch in raw for ch in ("\\", "!", "[", "]")) | |
| or any(ord(ch) < 32 or ch.isspace() for ch in raw)): | |
| return None | |
| if codeowners: | |
| anchored = raw.startswith("/") or ("/" in raw[:-1]) | |
| body = raw.lstrip("/").rstrip("/") | |
| match_dir_prefix = True | |
| else: | |
| if raw.startswith("/") or raw.endswith("/"): | |
| return None | |
| anchored = True | |
| body = raw | |
| match_dir_prefix = False | |
| if not body or "//" in body: | |
| return None | |
| segs = tuple(body.split("/")) | |
| if any(seg in ("", ".", "..") or ("**" in seg and seg != "**") | |
| for seg in segs): | |
| return None | |
| return (segs, anchored, match_dir_prefix) | |
| def _match_segment(seg, pat): | |
| """Linear-time wildcard matcher (`*`, `?`) for a single path segment.""" | |
| si = pi = 0 | |
| star_pi = -1 | |
| star_si = 0 | |
| sn, pn = len(seg), len(pat) | |
| while si < sn: | |
| if pi < pn and (pat[pi] == "?" or pat[pi] == seg[si]): | |
| si += 1 | |
| pi += 1 | |
| elif pi < pn and pat[pi] == "*": | |
| star_pi = pi | |
| star_si = si | |
| pi += 1 | |
| elif star_pi != -1: | |
| pi = star_pi + 1 | |
| star_si += 1 | |
| si = star_si | |
| else: | |
| return False | |
| while pi < pn and pat[pi] == "*": | |
| pi += 1 | |
| return pi == pn | |
| def _segs_match(path_segs, pat_segs): | |
| """Iterative O(|path| * |pat|) segment matcher supporting `**`, `*`, `?`.""" | |
| m = len(pat_segs) | |
| dp = [False] * (m + 1) | |
| dp[0] = True | |
| for j in range(m): | |
| if pat_segs[j] == "**" and dp[j]: | |
| dp[j + 1] = True | |
| for seg in path_segs: | |
| nxt = [False] * (m + 1) | |
| for j in range(m): | |
| if pat_segs[j] == "**": | |
| if dp[j] or dp[j + 1] or nxt[j]: | |
| nxt[j + 1] = True | |
| elif dp[j] and _match_segment(seg, pat_segs[j]): | |
| nxt[j + 1] = True | |
| dp = nxt | |
| return dp[m] | |
| def _match_path_pattern(path, spec): | |
| """Whether repo-relative `path` matches a compiled `_parse_glob_pattern`.""" | |
| segs, anchored, match_dir_prefix = spec | |
| p_segs = tuple(path.split("/")) | |
| if anchored: | |
| if _segs_match(p_segs, segs): | |
| return True | |
| return match_dir_prefix and _segs_match(p_segs, segs + ("**",)) | |
| if _segs_match(p_segs, ("**",) + segs): | |
| return True | |
| return match_dir_prefix and _segs_match(p_segs, ("**",) + segs + ("**",)) | |
| def _load_ci_policy_rules(repo_root=None): | |
| """Load protected CI/CODEOWNERS rules from `ci/checks.toml` & `CODEOWNERS`. | |
| Returns `None` on any missing file, parse error, or unknown schema so | |
| callers fail closed to `'tier2'` and reject delta reviews. | |
| """ | |
| root = _REPO_ROOT if repo_root is None else Path(repo_root) | |
| checks_path = root / "ci" / "checks.toml" | |
| codeowners_path = root / ".github" / "CODEOWNERS" | |
| try: | |
| checks_raw = checks_path.read_bytes() | |
| codeowners_raw = codeowners_path.read_bytes() | |
| cfg = tomllib.loads(checks_raw.decode("utf-8")) | |
| codeowners_text = codeowners_raw.decode("utf-8") | |
| except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError): | |
| return None | |
| if not isinstance(cfg, dict) or cfg.get("schema_version") != 1: | |
| return None | |
| tcb = cfg.get("tcb") | |
| pins = cfg.get("pins") | |
| images = cfg.get("images") | |
| caches = cfg.get("caches", {}) | |
| if (not isinstance(tcb, dict) or not isinstance(pins, dict) | |
| or not isinstance(images, dict) or not images | |
| or not isinstance(caches, dict)): | |
| return None | |
| caller = tcb.get("caller") | |
| caller_wfs = (caller.get("caller_workflows") | |
| if isinstance(caller, dict) else None) | |
| extra_exec = tcb.get("extra_executable_paths") | |
| if (not isinstance(caller_wfs, list) | |
| or not all(_is_non_empty_str(p) for p in caller_wfs) | |
| or not isinstance(extra_exec, list) | |
| or not all(_is_non_empty_str(p) for p in extra_exec)): | |
| return None | |
| protected_raw = list(caller_wfs) + list(extra_exec) | |
| for pin in pins.values(): | |
| if not isinstance(pin, dict) or not _is_non_empty_str(pin.get("path")): | |
| return None | |
| protected_raw.append(pin["path"]) | |
| for img in images.values(): | |
| if (not isinstance(img, dict) | |
| or not _is_non_empty_str(img.get("dockerfile"))): | |
| return None | |
| protected_raw.append(img["dockerfile"]) | |
| if "pins" in img: | |
| img_pins = img["pins"] | |
| if (not isinstance(img_pins, list) | |
| or not all(isinstance(k, str) and k in pins | |
| for k in img_pins)): | |
| return None | |
| ctx = img.get("context", []) | |
| if not isinstance(ctx, list): | |
| return None | |
| for entry in ctx: | |
| if not isinstance(entry, dict) or entry.get("kind") != "glob": | |
| return None | |
| inc = entry.get("include") | |
| if (not isinstance(inc, list) | |
| or not all(_is_non_empty_str(p) for p in inc)): | |
| return None | |
| protected_raw.extend(inc) | |
| for cache in caches.values(): | |
| if not isinstance(cache, dict): | |
| return None | |
| if "inputs" not in cache and "cargo_locked" not in cache: | |
| return None | |
| if "inputs" in cache: | |
| inps = cache["inputs"] | |
| if not isinstance(inps, list): | |
| return None | |
| for entry in inps: | |
| if not isinstance(entry, dict) or entry.get("kind") != "glob": | |
| return None | |
| inc = entry.get("include") | |
| if (not isinstance(inc, list) | |
| or not all(_is_non_empty_str(p) for p in inc)): | |
| return None | |
| protected_raw.extend(inc) | |
| if "cargo_locked" in cache: | |
| cl = cache["cargo_locked"] | |
| ws = cl.get("workspace") if isinstance(cl, dict) else None | |
| if (not _is_non_empty_str(ws) | |
| or not (ws == "Cargo.toml" or ws.endswith("/Cargo.toml"))): | |
| return None | |
| lock = ws[:-len("Cargo.toml")] + "Cargo.lock" | |
| protected_raw.extend((ws, lock)) | |
| protected_patterns = [] | |
| for pat in dict.fromkeys(protected_raw): | |
| parsed = _parse_glob_pattern(pat, codeowners=False) | |
| if parsed is None: | |
| return None | |
| protected_patterns.append(parsed) | |
| codeowner_count = 0 | |
| for raw_line in codeowners_text.splitlines(): | |
| line = raw_line.strip() | |
| if not line or line.startswith("#"): | |
| continue | |
| parts = line.split() | |
| if len(parts) < 2 or parts[1].startswith("#"): | |
| return None | |
| parsed = _parse_glob_pattern(parts[0], codeowners=True) | |
| if parsed is None: | |
| return None | |
| protected_patterns.append(parsed) | |
| codeowner_count += 1 | |
| if codeowner_count == 0: | |
| return None | |
| return _CiPolicyRules( | |
| protected_patterns=tuple(protected_patterns), | |
| config_blob_shas=( | |
| ("ci/checks.toml", _git_blob_sha(checks_raw)), | |
| (".github/CODEOWNERS", _git_blob_sha(codeowners_raw)), | |
| ), | |
| ) | |
| @functools.lru_cache(maxsize=8) | |
| def _load_ci_policy_rules_cached(repo_root_str): | |
| return _load_ci_policy_rules(Path(repo_root_str)) | |
| def _get_ci_policy_rules(): | |
| return _load_ci_policy_rules_cached(str(_REPO_ROOT)) | |
| def _policy_configs_match_trees(a_entries, d_entries): | |
| """Whether both Git trees match `_REPO_ROOT`'s loaded CI policy configs.""" | |
| rules = _get_ci_policy_rules() | |
| if rules is None: | |
| return False | |
| return all( | |
| a_entries.get(path) == ("100644", "blob", sha) | |
| and d_entries.get(path) == ("100644", "blob", sha) | |
| for path, sha in rules.config_blob_shas | |
| ) | |
| def _blob_has_delta_sensitive_token(text, path=""): | |
| """Whether `text` contains a waiver, trust-tier, or proof-escape token.""" | |
| if DELTA_DIRECTIVE_RE.search(text) is not None: | |
| return True | |
| if ("::" in text | |
| and _DELTA_ANNEAL_IDENT_RE.search(text) is not None | |
| and _DELTA_TRUSTED_IDENT_RE.search(text) is not None): | |
| return True | |
| if (path.endswith(".lean") | |
| and _DELTA_LEAN_EXTERN_RE.search(text) is not None): | |
| return True | |
| return False | |
| def _sequence_delta_lines(old_lines, new_lines): | |
| """Counts symmetric changed lines across bounded full-blob line diffs.""" | |
| if (len(old_lines) > DELTA_MAX_BLOB_LINES | |
| or len(new_lines) > DELTA_MAX_BLOB_LINES): | |
| return None | |
| p = 0 | |
| limit = min(len(old_lines), len(new_lines)) | |
| while p < limit and old_lines[p] == new_lines[p]: | |
| p += 1 | |
| s = 0 | |
| old_rem = len(old_lines) - p | |
| new_rem = len(new_lines) - p | |
| while (s < old_rem and s < new_rem | |
| and old_lines[len(old_lines) - 1 - s] | |
| == new_lines[len(new_lines) - 1 - s]): | |
| s += 1 | |
| old_mid = old_lines[p:len(old_lines) - s] | |
| new_mid = new_lines[p:len(new_lines) - s] | |
| if (abs(len(old_mid) - len(new_mid)) > DELTA_MAX_FIX_LINES | |
| or len(old_mid) * len(new_mid) > DELTA_MAX_MATCHER_WORK): | |
| return None | |
| altered = 0 | |
| matcher = difflib.SequenceMatcher(None, old_mid, new_mid) | |
| for tag, i1, i2, j1, j2 in matcher.get_opcodes(): | |
| if tag == "equal": | |
| continue | |
| altered += max(i2 - i1, j2 - j1) | |
| return altered | |
| def _blob_delta_lines(old_text, new_text, path=""): | |
| """Position-sensitive symmetric line delta between two full file blobs. | |
| Fails closed (`None`) whenever either the base or head blob of an edited | |
| file contains any waiver, trust-tier directive, or Lean proof-escape | |
| token, avoiding syntactic or scope-movement heuristics entirely. | |
| """ | |
| if (_blob_has_delta_sensitive_token(old_text, path=path) | |
| or _blob_has_delta_sensitive_token(new_text, path=path)): | |
| return None | |
| return _sequence_delta_lines( | |
| old_text.splitlines(keepends=True), | |
| new_text.splitlines(keepends=True), | |
| ) | |
| def _is_valid_cross_family_gate2_item(source, item, authorized_reviewers, | |
| deleted_through=0): | |
| """Whether `item` is an authentic, well-formed cross-family Gate 2 entry.""" | |
| if not authentic_gate2_item(item, authorized_reviewers): | |
| return False | |
| if ((source == "review" or "state" in item) | |
| and item.get("state") != "COMMENTED"): | |
| return False | |
| try: | |
| payload, _, author_norm, completed_seat_families = parse_gate2_payload( | |
| item.get("body") or "" | |
| ) | |
| except ValueError: | |
| return False | |
| if (completed_seat_families is not None | |
| and not any(fam != author_norm for fam in completed_seat_families)): | |
| return False | |
| has_findings = bool(payload["findings"] or payload["verdict"] == "findings") | |
| if source == "comment" and item["id"] <= deleted_through and not has_findings: | |
| return False | |
| res, _ = _evaluate_gate2_item( | |
| source, item, payload["head_sha"], payload["base_sha"], | |
| payload["tree_sha"], None, | |
| ) | |
| return res.state in ("success", "pending") or ( | |
| has_findings and res.reason.startswith("Gate 2 review reported ") | |
| ) | |
| def _carry_forward_blocked_by_history(comments, reviews, authorized_reviewers, | |
| anchor_head, anchor_tree, | |
| deleted_through=0): | |
| """Whether any Gate 2 or fallback Codex evidence on the anchor blocks. | |
| Any Gate 2 item recorded on the anchor's head or tree (every commit with | |
| the anchor's one-commit identity has that tree) that has findings or fails | |
| on its own recorded identity stops carry-forward, mirroring which failures | |
| the normal rules keep blocking. Carry-forward anchors on a verified clean | |
| cross-family Gate 2 review, so legacy Codex summaries never block it; the | |
| legacy summary loop remains as defense-in-depth for direct callers when no | |
| authentic cross-family Gate 2 entry exists on the PR. | |
| """ | |
| has_authentic_gate2 = False | |
| for source, entries in (("comment", comments), ("review", reviews)): | |
| for item in entries: | |
| if not authentic_gate2_item(item, authorized_reviewers): | |
| continue | |
| if _is_valid_cross_family_gate2_item( | |
| source, item, authorized_reviewers, deleted_through): | |
| has_authentic_gate2 = True | |
| try: | |
| payload, _, _, _ = parse_gate2_payload( | |
| item.get("body") or "", strict_identity_key=False, | |
| ) | |
| except ValueError: | |
| continue | |
| if (payload["head_sha"] != anchor_head | |
| and payload["tree_sha"] != anchor_tree): | |
| continue | |
| if payload["findings"] or payload["verdict"] == "findings": | |
| return True | |
| if source == "comment" and item["id"] <= deleted_through: | |
| continue | |
| if _is_blocking_non_finding_failure( | |
| source, item, payload["head_sha"], payload["base_sha"], | |
| payload["tree_sha"]): | |
| return True | |
| if has_authentic_gate2: | |
| return False | |
| for comment in comments: | |
| if not authentic_summary(comment): | |
| continue | |
| try: | |
| review = _parse_codex_summary(comment.get("body") or "") | |
| except ValueError: | |
| return True | |
| if (review.get("status") != "completed" | |
| or review.get("has_any_findings") is not False | |
| or review.get("has_unresolved_findings") is not False): | |
| return True | |
| return False | |
| def _gate2_failure_heads(comments, reviews, authorized_reviewers): | |
| """Heads named by authentic Gate 2 items that have findings or fail.""" | |
| heads = set() | |
| for source, entries in (("comment", comments), ("review", reviews)): | |
| for item in entries: | |
| if not authentic_gate2_item(item, authorized_reviewers): | |
| continue | |
| try: | |
| payload, _, _, _ = parse_gate2_payload( | |
| item.get("body") or "", strict_identity_key=False, | |
| ) | |
| except ValueError: | |
| continue | |
| if (payload["findings"] or payload["verdict"] == "findings" | |
| or _is_blocking_non_finding_failure( | |
| source, item, payload["head_sha"], payload["base_sha"], | |
| payload["tree_sha"])): | |
| heads.add(payload["head_sha"]) | |
| return heads | |
| def _carry_forward_result(head, comments, reviews, deleted_through, base_sha, | |
| tree_sha, authorized_reviewers, identity_key, | |
| verifier): | |
| """Carry the latest clean Gate 2 review forward across a rebase. | |
| The latest eligible Gate 2 item must be a clean review of an earlier | |
| head on a different base. `verifier` must confirm with GitHub data | |
| that the PR was only rebased and return that head's verified one-commit | |
| identity key (see `GitHub.verify_rebase_carry_forward`); the review must | |
| then pass on that verified identity with no blocking evidence left. | |
| """ | |
| if not (isinstance(identity_key, str) | |
| and ONE_IDENTITY_RE.fullmatch(identity_key) is not None | |
| and base_sha is not None and tree_sha is not None): | |
| return None | |
| items = [ | |
| ("review", r) for r in reviews if authentic_gate2_item(r, authorized_reviewers) | |
| ] + [ | |
| ("comment", c) for c in comments if authentic_gate2_item(c, authorized_reviewers) | |
| ] | |
| if not items: | |
| return None | |
| # The anchor must be the latest Gate 2 item of all, so a deletion can | |
| # never hide a newer, still-present item behind it. | |
| source, item = sorted(items, key=_gate2_item_sort_key)[-1] | |
| if source == "comment" and item["id"] <= deleted_through: | |
| return None | |
| try: | |
| payload, _, _, _ = parse_gate2_payload(item.get("body") or "") | |
| except ValueError: | |
| return None | |
| a_head, a_base, a_tree = payload["head_sha"], payload["base_sha"], payload["tree_sha"] | |
| # Only a full review carries forward; a delta review covers only the | |
| # identity it was posted for. | |
| if (payload["verdict"] != "clean" or payload["findings"] | |
| or payload.get("review_mode") == "delta" | |
| or a_head == head or a_base == base_sha | |
| or _carry_forward_blocked_by_history( | |
| comments, reviews, authorized_reviewers, a_head, a_tree, | |
| deleted_through)): | |
| return None | |
| # Findings or failures recorded on any other head may concern this | |
| # same patch (for example on an earlier rebase), so the verifier must | |
| # show that each such head's patch differs from the anchor's. | |
| other_heads = sorted(_gate2_failure_heads(comments, reviews, | |
| authorized_reviewers) - {a_head}) | |
| try: | |
| anchor_ik = verifier(a_head, a_base, a_tree, head, base_sha, other_heads) | |
| except (RuntimeError, ValueError, OSError): | |
| return None | |
| if not (isinstance(anchor_ik, str) | |
| and ONE_IDENTITY_RE.fullmatch(anchor_ik) is not None): | |
| return None | |
| # The whole PR history, judged as of the anchor's verified identity, | |
| # must pass; this also re-checks a structured identity_key. | |
| anchor_res = review_result( | |
| a_head, comments, deleted_through, None, reviews=reviews, | |
| base_sha=a_base, tree_sha=a_tree, | |
| authorized_reviewers=authorized_reviewers, identity_key=anchor_ik, | |
| ) | |
| if anchor_res.state != "success": | |
| return None | |
| return Result( | |
| "success", | |
| f"Gate 2 review of {a_head[:12]} carried forward " | |
| f"(rebase only, verified; {payload['reviewer_family']})", | |
| ) | |
| def _evaluate_gate2_item(source, item, head, base_sha, tree_sha, base_changed_at, | |
| identity_key=None, historical_identities=None, | |
| historical_trees=None, | |
| *, strict_identity_key=True, | |
| allow_historical_findings=False, | |
| allow_historical_failures=False): | |
| body = item.get("body") or "" | |
| # allow_historical_failures evaluates items on a GitHub-verified historical | |
| # commit with this one-commit identity even without a payload | |
| # identity_key, so callers can carry that commit's failures forward. | |
| try: | |
| payload, reviewer_norm, author_norm, completed_seat_families = parse_gate2_payload( | |
| body, strict_identity_key=strict_identity_key, | |
| ) | |
| except ValueError as err: | |
| msg = str(err) | |
| if "Ambiguous" in msg: | |
| return Result("failure", "Ambiguous Gate 2 review marker"), None | |
| if "Unsupported" in msg: | |
| return Result("failure", "Unsupported Gate 2 review version"), None | |
| if "delta" in msg.lower(): | |
| return Result("failure", "Malformed Gate 2 delta review payload"), None | |
| if "verdict" in msg.lower() or "findings" in msg.lower(): | |
| return Result("failure", "Malformed or contradictory Gate 2 verdict or findings"), None | |
| return Result("failure", "Malformed Gate 2 review payload"), None | |
| try: | |
| entry_ts = _gate2_item_timestamp(source, item) | |
| except ValueError: | |
| return Result("failure", "Missing or invalid Gate 2 review timestamp"), None | |
| if source == "review" or "state" in item: | |
| review_state = item.get("state") | |
| if review_state == "APPROVED": | |
| return Result("failure", "Gate 2 review must use COMMENT, never APPROVE"), payload | |
| if (source == "review" or "commit_id" in item) and item.get("commit_id") != payload["head_sha"]: | |
| return Result("failure", "Gate 2 review commit_id does not match payload head_sha"), payload | |
| identity_matched = _is_one_commit_identity_match( | |
| payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities=historical_identities, | |
| ) | |
| has_findings = bool(payload["findings"] or payload["verdict"] == "findings") | |
| exact_finding_matched = ( | |
| allow_historical_findings | |
| and has_findings | |
| and payload["head_sha"] == head | |
| and base_sha is not None | |
| and payload["base_sha"] == base_sha | |
| and tree_sha is not None | |
| and payload["tree_sha"] == tree_sha | |
| ) | |
| historical_finding_matched = ( | |
| allow_historical_findings | |
| and has_findings | |
| and ( | |
| _has_verified_historical_one_commit_identity( | |
| payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities, | |
| ) | |
| or _has_verified_historical_same_tree( | |
| payload, head, base_sha, tree_sha, | |
| historical_identities, historical_trees, | |
| ) | |
| ) | |
| ) | |
| historical_failure_matched = ( | |
| allow_historical_failures | |
| and _has_verified_historical_one_commit_identity( | |
| payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities, | |
| ) | |
| ) | |
| effective_identity_matched = ( | |
| identity_matched or exact_finding_matched or historical_finding_matched | |
| or historical_failure_matched | |
| ) | |
| if not effective_identity_matched and payload["head_sha"] != head: | |
| return Result("pending", "Gate 2 has not reviewed this head commit"), payload | |
| if reviewer_norm == author_norm: | |
| return Result("failure", "Gate 2 reviewer_family must differ from author_family"), payload | |
| if (completed_seat_families is not None | |
| and payload["verdict"] in ("clean", "findings") | |
| and not any(fam != author_norm for fam in completed_seat_families)): | |
| return Result("failure", "Gate 2 panel_seats requires a completed cross-family seat"), payload | |
| if tree_sha is None: | |
| return Result("failure", "Cannot verify PR head tree SHA for Gate 2 review"), payload | |
| if payload["tree_sha"] != tree_sha: | |
| return Result("failure", "Gate 2 review tree_sha does not match head commit tree"), payload | |
| if base_sha is None: | |
| return Result("failure", "Cannot verify PR base SHA for Gate 2 review"), payload | |
| if not effective_identity_matched: | |
| if payload["base_sha"] != base_sha: | |
| return Result("pending", "Base changed; request a new Gate 2 review"), payload | |
| payload_ik = payload.get("identity_key") | |
| if (strict_identity_key | |
| and isinstance(identity_key, str) | |
| and _is_structured_identity_key(payload_ik) | |
| and payload_ik != identity_key): | |
| return Result("failure", "Gate 2 review identity_key does not match commit identity"), payload | |
| if (payload.get("review_mode") != "delta" | |
| and payload.get("merge_base") is not None | |
| and payload["merge_base"] != payload["base_sha"]): | |
| return Result("failure", "Gate 2 review merge_base does not match base_sha"), payload | |
| if source == "review" or "state" in item: | |
| review_state = item.get("state") | |
| if review_state in ("PENDING", "DISMISSED"): | |
| if payload["findings"]: | |
| return Result("failure", f"Gate 2 review reported {len(payload['findings'])} finding(s)"), payload | |
| return Result("pending", f"Gate 2 review is {review_state.lower()}"), payload | |
| if review_state == "CHANGES_REQUESTED": | |
| if payload["findings"]: | |
| return Result("failure", f"Gate 2 review reported {len(payload['findings'])} finding(s)"), payload | |
| return Result("failure", "Gate 2 review must use COMMENT, not CHANGES_REQUESTED"), payload | |
| if review_state != "COMMENTED": | |
| return Result("failure", "Unsupported Gate 2 review state"), payload | |
| if payload["findings"] or payload["verdict"] == "findings": | |
| return Result("failure", f"Gate 2 review reported {len(payload['findings'])} finding(s)"), payload | |
| # A stable pull.base.sha does not prove which base was reviewed, so a | |
| # review recorded against the current base_sha must postdate the last | |
| # base change, with or without a matching one-commit identity. Only | |
| # transfers from a different, GitHub-verified base_sha skip the freshness | |
| # comparison, while any supplied base_changed_at timestamp is always | |
| # validated fail-closed. This runs after every failure above, so a base | |
| # change can turn a would-be pass into pending but never makes a failing | |
| # review stop blocking. | |
| if base_changed_at is not None: | |
| try: | |
| changed = timestamp(base_changed_at) | |
| except ValueError: | |
| return Result("failure", "Cannot establish Gate 2 freshness after base change"), payload | |
| if payload["base_sha"] == base_sha and entry_ts <= changed: | |
| return Result("pending", "Base changed; request a new Gate 2 review"), payload | |
| if payload["verdict"] == "incomplete": | |
| return Result("pending", "Waiting for a completed Gate 2 review"), payload | |
| return Result("success", f"Independent cross-family Gate 2 review passed ({payload['reviewer_family']})"), payload | |
| def _gate2_item_sort_key(entry): | |
| min_utc = datetime.min.replace(tzinfo=timezone.utc) | |
| source, item = entry | |
| try: | |
| ts = _gate2_item_timestamp(source, item) | |
| invalid = False | |
| except ValueError: | |
| ts = min_utc | |
| invalid = True | |
| return (invalid, ts, 0 if source == "comment" else 1, item["id"]) | |
| def _is_blocking_non_finding_failure(source, item, p_head, p_base, p_tree): | |
| """Whether an item fails on its own recorded (head, base, tree). | |
| Mirrors _find_active_gate2_failure: malformed non-finding items and | |
| non-delta items with a stale merge_base never block, so they need no | |
| verification. | |
| """ | |
| res, payload = _evaluate_gate2_item( | |
| source, item, p_head, p_base, p_tree, None, | |
| ) | |
| if payload is None or res.state != "failure": | |
| return False | |
| return (payload.get("review_mode") == "delta" | |
| or payload.get("merge_base") is None | |
| or payload["merge_base"] == payload["base_sha"]) | |
| def _find_active_gate2_failure(entries, head, base_sha, tree_sha, | |
| base_changed_at, identity_key, | |
| historical_identities, historical_trees, | |
| *, deleted_through=0, include_non_findings=True): | |
| for source, item in entries: | |
| prior_res, prior_payload = _evaluate_gate2_item( | |
| source, item, head, base_sha, tree_sha, base_changed_at, | |
| identity_key=identity_key, | |
| historical_identities=historical_identities, | |
| historical_trees=historical_trees, | |
| allow_historical_findings=True, | |
| allow_historical_failures=True, | |
| ) | |
| strict_failed = prior_payload is None | |
| if strict_failed: | |
| prior_res, prior_payload = _evaluate_gate2_item( | |
| source, item, head, base_sha, tree_sha, base_changed_at, | |
| identity_key=identity_key, | |
| historical_identities=historical_identities, | |
| historical_trees=historical_trees, | |
| strict_identity_key=False, | |
| allow_historical_findings=True, | |
| allow_historical_failures=True, | |
| ) | |
| if prior_payload is None or prior_res.state != "failure": | |
| continue | |
| has_findings = bool( | |
| prior_payload["findings"] or prior_payload["verdict"] == "findings" | |
| ) | |
| if strict_failed and not has_findings: | |
| continue | |
| exact_match = ( | |
| prior_payload["head_sha"] == head | |
| and (base_sha is None or prior_payload["base_sha"] == base_sha) | |
| and (tree_sha is None or prior_payload["tree_sha"] == tree_sha) | |
| ) | |
| if has_findings: | |
| if (exact_match | |
| or _is_one_commit_identity_match( | |
| prior_payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities=historical_identities, | |
| ) | |
| or _has_verified_historical_one_commit_identity( | |
| prior_payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities, | |
| ) | |
| or _has_verified_historical_same_tree( | |
| prior_payload, head, base_sha, tree_sha, | |
| historical_identities, historical_trees, | |
| )): | |
| return prior_res, prior_payload | |
| continue | |
| if not include_non_findings or (source == "comment" and item["id"] <= deleted_through): | |
| continue | |
| # Non-findings failures (CHANGES_REQUESTED, APPROVED, same-family, | |
| # mismatched commit_id) remain blocking on the exact identity and on | |
| # any GitHub-verified historical commit with this one-commit identity | |
| # (the commits a clean verdict could transfer from), unless invalidated | |
| # by a mismatched structured identity_key or merge_base. | |
| identity_applicable = ( | |
| exact_match | |
| or _is_one_commit_identity_match( | |
| prior_payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities=historical_identities, | |
| ) | |
| or _has_verified_historical_one_commit_identity( | |
| prior_payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities, | |
| ) | |
| ) | |
| payload_ik = prior_payload.get("identity_key") | |
| ik_applicable = not ( | |
| isinstance(identity_key, str) | |
| and _is_structured_identity_key(payload_ik) | |
| and payload_ik != identity_key | |
| ) | |
| merge_base_applicable = ( | |
| prior_payload.get("review_mode") == "delta" | |
| or prior_payload.get("merge_base") is None | |
| or prior_payload["merge_base"] == prior_payload["base_sha"] | |
| ) | |
| if identity_applicable and ik_applicable and merge_base_applicable: | |
| return prior_res, prior_payload | |
| return None | |
| def review_gate2_result(head, comments, reviews=(), deleted_through=0, | |
| base_changed_at=None, base_sha=None, tree_sha=None, | |
| authorized_reviewers=None, identity_key=None, | |
| historical_identities=None, historical_trees=None, | |
| delta_base_verifier=None): | |
| """Evaluate structured anneal-gate2-review:v1 reviews and comments.""" | |
| comment_candidates = [c for c in comments if authentic_gate2_item(c, authorized_reviewers)] | |
| review_candidates = [r for r in reviews if authentic_gate2_item(r, authorized_reviewers)] | |
| if not comment_candidates and not review_candidates: | |
| return None, None | |
| all_items = sorted( | |
| [("review", r) for r in review_candidates] | |
| + [("comment", c) for c in comment_candidates], | |
| key=_gate2_item_sort_key, | |
| ) | |
| eligible = [ | |
| ("review", r) for r in review_candidates | |
| ] + [ | |
| ("comment", c) for c in comment_candidates if c["id"] > deleted_through | |
| ] | |
| if not eligible: | |
| # deleted_through only prevents older comments from approving; it must | |
| # never waive still-present finding comments on the active identity. | |
| prior_finding = _find_active_gate2_failure( | |
| all_items, head, base_sha, tree_sha, base_changed_at, | |
| identity_key, historical_identities, historical_trees, | |
| deleted_through=deleted_through, include_non_findings=False, | |
| ) | |
| if prior_finding is not None: | |
| return prior_finding | |
| return Result("pending", "A newer Gate 2 comment was deleted; request a new review"), None | |
| ordered = sorted(eligible, key=_gate2_item_sort_key) | |
| latest_source, latest_item = ordered[-1] | |
| latest_res, latest_payload = _evaluate_gate2_item( | |
| latest_source, latest_item, head, base_sha, tree_sha, base_changed_at, | |
| identity_key=identity_key, | |
| historical_identities=historical_identities, | |
| historical_trees=historical_trees, | |
| allow_historical_findings=True, | |
| ) | |
| if latest_res.state != "failure": | |
| # A later clean or incomplete Gate 2 review on the same identity or tree | |
| # cannot silently erase still-present failures (including finding | |
| # comments with id <= deleted_through). The latest item is included | |
| # too: as a failure on a verified historical commit with this identity | |
| # it evaluates as pending above, yet it must still block. | |
| # _find_active_gate2_failure returns only failures, so a clean latest | |
| # item never passes through it. | |
| active_failure = _find_active_gate2_failure( | |
| all_items, head, base_sha, tree_sha, base_changed_at, | |
| identity_key, historical_identities, historical_trees, | |
| deleted_through=deleted_through, include_non_findings=True, | |
| ) | |
| if active_failure is not None: | |
| return active_failure | |
| if (latest_res.state == "success" | |
| and latest_payload.get("review_mode") == "delta"): | |
| problem = _delta_review_problem( | |
| all_items, comments, latest_item, latest_payload, | |
| deleted_through, historical_trees, base_sha, base_changed_at, | |
| delta_base_verifier=delta_base_verifier, | |
| ) | |
| if problem is not None: | |
| return Result("pending", f"{problem}; request a full Gate 2 review"), latest_payload | |
| return latest_res, latest_payload | |
| def _delta_review_problem(entries, comments, delta_item, delta_payload, | |
| deleted_through, historical_trees, base_sha, | |
| base_changed_at, *, delta_base_verifier=None): | |
| """Why a passing delta review is not a complete review, or None. | |
| `entries` are every authentic Gate 2 item on the PR, deleted comments | |
| included, in `_gate2_item_sort_key` order. The anchor is the item | |
| immediately before the delta review, so delta reviews never chain. | |
| Findings from every earlier item, not only the anchor, must be audited. | |
| `historical_trees` maps commits to their GitHub-verified trees and must | |
| hold the anchor's head. | |
| """ | |
| del comments | |
| _, _, _, completed_seat_families = parse_gate2_payload( | |
| delta_item.get("body") or "", | |
| ) | |
| problem = _delta_payload_problem(delta_payload, completed_seat_families) | |
| if problem is not None: | |
| return problem | |
| # A deleted Gate 2 item or Codex summary may have been the anchor, an | |
| # intervening item, or an unaudited finding; the journal records only | |
| # that a deletion happened, so the history cannot be reconstructed. | |
| if deleted_through: | |
| return "Delta Gate 2 review follows a deleted Gate 2 item or Codex summary" | |
| # With no deletion recorded, every authentic item is eligible, so the | |
| # delta review, the latest eligible item, is the latest of all and the | |
| # anchor and audit checks below see every other item. | |
| pos = next(i for i, (_, item) in enumerate(entries) if item is delta_item) | |
| if pos == 0: | |
| return "Delta Gate 2 review has no preceding full review" | |
| source, item = entries[pos - 1] | |
| # Only a PR review binds its payload to a commit (`commit_id`). | |
| if source != "review": | |
| return "Delta Gate 2 review must directly follow a full PR review" | |
| try: | |
| anchor, reviewer_norm, author_norm, _ = parse_gate2_payload( | |
| item.get("body") or "", | |
| ) | |
| except ValueError: | |
| return "Delta Gate 2 review's preceding review is malformed" | |
| if anchor.get("review_mode", "full") != "full": | |
| return "Delta Gate 2 review must directly follow a full review" | |
| if anchor["head_sha"] != delta_payload["delta_from"]: | |
| return "Delta Gate 2 review delta_from is not the preceding review's head" | |
| # Any retarget or force-push of the PR's base branch at or after the anchor | |
| # forces a full review, even when `main` only advanced via merges or the | |
| # delta review matches by one-commit identity (`timelineItems(last: 1)` | |
| # retains only the latest base change, so a later event can hide an | |
| # intervening one). Always validate `base_changed_at` fail-closed. | |
| if base_changed_at is not None: | |
| try: | |
| changed = timestamp(base_changed_at) | |
| anchor_ts = _gate2_item_timestamp(source, item) | |
| except ValueError: | |
| return "Cannot establish Gate 2 freshness after base change" | |
| if anchor_ts <= changed: | |
| return "Delta Gate 2 review spans a base change" | |
| del base_sha | |
| if (item.get("state") != "COMMENTED" | |
| or item.get("commit_id") != anchor["head_sha"] | |
| or reviewer_norm == author_norm | |
| or anchor["verdict"] not in ("clean", "findings") | |
| or anchor.get("merge_base", anchor["base_sha"]) != anchor["base_sha"]): | |
| return "Delta Gate 2 review's preceding review is not a complete cross-family review" | |
| if (not _all_seats_finished(anchor.get("panel_seats")) | |
| or not any(canonical_family(seat["family"], allow_human=False) != author_norm | |
| for seat in anchor["panel_seats"]) | |
| or (anchor.get("coverage") or {}).get("blocked", 0) != 0): | |
| return "Delta Gate 2 review's preceding review is not a complete panel" | |
| anchor_tier = anchor.get("risk_tier") | |
| delta_tier = delta_payload["risk_tier"] | |
| if anchor_tier not in GATE2_DELTA_RISK_TIERS: | |
| return "Delta Gate 2 review's preceding review lacks a non-TCB risk_tier" | |
| if GATE2_DELTA_TIER_RANK[delta_tier] > GATE2_DELTA_TIER_RANK[anchor_tier]: | |
| return "Delta Gate 2 review escalates risk_tier" | |
| # The anchor's recorded tree decides which earlier findings are on it, | |
| # so it must match the tree GitHub reports for the anchor's head. | |
| if (not isinstance(historical_trees, dict) | |
| or historical_trees.get(anchor["head_sha"]) != anchor["tree_sha"]): | |
| return "Cannot verify the tree of the delta Gate 2 review's preceding review" | |
| audited = {entry["id"] for entry in delta_payload["prior_findings_status"]} | |
| # One audit entry must not stand for two findings that share an id. | |
| seen = set() | |
| for _, earlier in entries[:pos]: | |
| try: | |
| payload, _, _, _ = parse_gate2_payload(earlier.get("body") or "") | |
| except ValueError: | |
| return "Delta Gate 2 review follows a malformed Gate 2 item" | |
| on_anchor = (payload["head_sha"] == anchor["head_sha"] | |
| or payload["tree_sha"] == anchor["tree_sha"]) | |
| for finding in payload["findings"]: | |
| if on_anchor and finding["severity"] in ("P0", "P1"): | |
| return "Delta Gate 2 review follows a P0 or P1 finding" | |
| if (not _is_non_empty_str(finding.get("id")) | |
| or finding["id"] not in audited): | |
| return "Delta Gate 2 review does not audit every earlier finding" | |
| if finding["id"] in seen: | |
| return "Delta Gate 2 review follows findings that share an id" | |
| seen.add(finding["id"]) | |
| a_base = anchor["base_sha"] | |
| a_mb = anchor.get("merge_base", a_base) | |
| d_base = delta_payload["base_sha"] | |
| d_mb = delta_payload.get("merge_base", d_base) | |
| base_advanced = (a_base != d_base or a_mb != a_base or d_mb != d_base) | |
| if base_advanced or delta_base_verifier is not None: | |
| if delta_base_verifier is None: | |
| return "Delta Gate 2 review spans a base change" | |
| try: | |
| verified = bool(delta_base_verifier(anchor, delta_payload)) | |
| except (RuntimeError, ValueError, OSError): | |
| verified = False | |
| if not verified: | |
| return ("Delta Gate 2 review spans an unverified base or " | |
| "scope change") | |
| return None | |
| def _parse_codex_summary(body): | |
| markers = re.findall(r"<!-- codex-review-state:.*?-->", body, re.DOTALL) | |
| if (body.count(SUMMARY) != 1 | |
| or "<!-- codex-pull-request-review-summary -->" not in body | |
| or body.count(STATE) != 1 or len(markers) != 1): | |
| raise ValueError("Unrecognized or ambiguous Codex summary") | |
| match = re.fullmatch(r"<!-- codex-review-state:v1 (.*?) -->", markers[0], | |
| re.DOTALL) | |
| if match is None: | |
| raise ValueError("Unsupported Codex review-state version") | |
| try: | |
| payload = json.loads(match[1], object_pairs_hook=unique_object) | |
| review = payload["code_review"] | |
| if not isinstance(review, dict): | |
| raise ValueError("Code Review state is not an object") | |
| except (ValueError, TypeError, KeyError) as err: | |
| raise ValueError("Malformed Codex Code Review state") from err | |
| return review | |
| def _codex_summary_has_completed_findings(review): | |
| return ( | |
| review.get("status") == "completed" | |
| and (review.get("has_any_findings") is not False | |
| or review.get("has_unresolved_findings") is not False) | |
| ) | |
| def _any_active_codex_findings_for_tree(comments, head, tree_sha, | |
| historical_identities, | |
| historical_trees=None): | |
| # Legacy Codex summaries record only a commit, so they match the current | |
| # head by SHA or a historical commit by its GitHub-verified tree. | |
| for comment in comments: | |
| if not authentic_summary(comment): | |
| continue | |
| try: | |
| review = _parse_codex_summary(comment["body"]) | |
| except ValueError: | |
| continue | |
| if not _codex_summary_has_completed_findings(review): | |
| continue | |
| c_sha = review.get("commit_sha") | |
| if c_sha == head or _matches_historical_codex_commit( | |
| c_sha, tree_sha, historical_identities, | |
| historical_trees=historical_trees, | |
| ): | |
| return Result("failure", "Codex findings remain or the verdict is unknown") | |
| return None | |
| def review_codex_result(head, comments, deleted_through=0, base_changed_at=None, *, | |
| tree_sha=None, | |
| historical_identities=None, historical_trees=None): | |
| """Read the latest authentic Codex summary; never fall back to an older success. | |
| The hidden v1 state is an observed integration format, not a documented | |
| OpenAI API contract. Recognize summary and state prefixes broadly enough to | |
| notice a replacement version, then require exactly the version understood | |
| here. A format change should stop merging and call for an adapter update. | |
| """ | |
| latest = latest_summary(comments) | |
| if latest is None: | |
| return None | |
| historical_finding = _any_active_codex_findings_for_tree( | |
| comments, head, tree_sha, | |
| historical_identities, historical_trees=historical_trees, | |
| ) | |
| # Comment IDs order creation. An edit to an older summary must not make it | |
| # supersede a newer, possibly incomplete review summary, while | |
| # still-present completed findings on the active identity remain blocking. | |
| if latest["id"] <= deleted_through: | |
| if historical_finding is not None: | |
| return historical_finding | |
| return Result("pending", "A newer summary was deleted; request a new summary") | |
| body = latest["body"] | |
| try: | |
| review = _parse_codex_summary(body) | |
| except ValueError as err: | |
| return Result("failure", str(err)) | |
| # Security Review has an independent verdict. A clean security result, | |
| # emoji, or prose receipt cannot fill in a missing Code Review verdict. | |
| if review.get("commit_sha") != head: | |
| if historical_finding is not None: | |
| return historical_finding | |
| return Result("pending", "Codex has not reviewed this head commit") | |
| if review.get("status") != "completed": | |
| if historical_finding is not None: | |
| return historical_finding | |
| return Result("pending", "Waiting for a completed Codex Code Review") | |
| # Identity checks distinguish JSON false from null, zero, and empty strings. | |
| # Resolving threads by hand is insufficient: has_any_findings must also be | |
| # false, so Codex must publish a clean result for this revision. | |
| if (review.get("has_any_findings") is not False | |
| or review.get("has_unresolved_findings") is not False): | |
| return Result("failure", "Codex findings remain or the verdict is unknown") | |
| if historical_finding is not None: | |
| return historical_finding | |
| if base_changed_at is not None: | |
| # The JSON identifies only the head. After a retarget or force-push of | |
| # the base, require a newly created summary and a Code Review result | |
| # newer than that event. Merely editing an old summary, or recreating | |
| # it for a security-only review, must not revive an old Code Review. | |
| # This deliberately requires a fresh comment after a base change; | |
| # updated_at is not evidence that Code Review itself ran again. | |
| rows = [line for line in body.splitlines() | |
| if re.match(r"^\|[^|]*\*\*Code Review\*\*\s*\|", line)] | |
| completed = re.findall(r'<relative-time datetime="([^"]+)">', rows[0]) \ | |
| if len(rows) == 1 and "**Completed**" in rows[0] else [] | |
| try: | |
| changed = timestamp(base_changed_at) | |
| created = timestamp(latest.get("created_at")) | |
| if (created <= changed or len(completed) != 1 | |
| or timestamp(completed[0]) < created): | |
| return Result("pending", "Base changed; request a new Code Review summary") | |
| except ValueError: | |
| return Result("failure", "Cannot establish review freshness after base change") | |
| return Result("success", "Codex completed Code Review with no findings") | |
| def review_result(head, comments, deleted_through=0, base_changed_at=None, *, | |
| reviews=None, base_sha=None, tree_sha=None, | |
| authorized_reviewers=None, identity_key=None, | |
| historical_identities=None, historical_trees=None, | |
| carry_forward_verifier=None, delta_base_verifier=None): | |
| """Evaluate Gate 2 cross-family reviews and legacy Codex summaries.""" | |
| codex_res = review_codex_result( | |
| head, comments, deleted_through=deleted_through, | |
| base_changed_at=base_changed_at, | |
| tree_sha=tree_sha, | |
| historical_identities=historical_identities, | |
| historical_trees=historical_trees, | |
| ) | |
| gate2_res, gate2_payload = review_gate2_result( | |
| head, comments, reviews=reviews or (), | |
| deleted_through=deleted_through, | |
| base_changed_at=base_changed_at, | |
| base_sha=base_sha, | |
| tree_sha=tree_sha, | |
| authorized_reviewers=authorized_reviewers, | |
| identity_key=identity_key, | |
| historical_identities=historical_identities, | |
| historical_trees=historical_trees, | |
| delta_base_verifier=delta_base_verifier, | |
| ) | |
| if (gate2_res is not None and gate2_res.state == "pending" | |
| and carry_forward_verifier is not None): | |
| carried = _carry_forward_result( | |
| head, comments, reviews or (), deleted_through, base_sha, | |
| tree_sha, authorized_reviewers, identity_key, | |
| carry_forward_verifier, | |
| ) | |
| if carried is not None: | |
| return carried | |
| if gate2_res is None and codex_res is None: | |
| return Result("pending", "Waiting for a Gate 2 or Codex review") | |
| if gate2_res is None: | |
| return codex_res | |
| if codex_res is None: | |
| return gate2_res | |
| if gate2_res.state in ("failure", "success"): | |
| return gate2_res | |
| if codex_res.state == "failure": | |
| return codex_res | |
| if codex_res.state == "success": | |
| if (gate2_payload is not None | |
| and _matches_gate2_identity( | |
| gate2_payload, head, base_sha, tree_sha, identity_key, | |
| historical_identities=historical_identities, | |
| )): | |
| return gate2_res | |
| return codex_res | |
| return gate2_res | |
| def pull_snapshot(pulls): | |
| """Keep the PR identities that affect a commit's shared status.""" | |
| return sorted((pull["number"], pull["head"]["sha"], pull["base"]["ref"], | |
| pull["base"]["sha"]) for pull in pulls) | |
| @dataclass(frozen=True) | |
| class ReviewContext: | |
| """Comparable API observations, not an atomic GitHub transaction. | |
| Keep decision-relevant metadata together so adding another dependency also | |
| adds it to the final equality check. Summary bodies are rechecked separately | |
| so a comment read failure can block its PR without losing other verdicts. | |
| """ | |
| pulls: tuple | |
| deleted: dict | |
| queues: dict | |
| bases: dict | |
| def read_context(github, pulls=None): | |
| pulls = github.pulls() if pulls is None else pulls | |
| deleted = github.deleted_summaries() | |
| queues = github.queue_groups() | |
| bases = github.base_changes() | |
| numbers = {pull["number"] for pull in pulls} | |
| if set(bases) != numbers: | |
| raise RuntimeError("PR list and base history differ; recheck required") | |
| return ReviewContext(tuple(pull_snapshot(pulls)), | |
| {n: deleted.get(n, 0) for n in numbers}, queues, bases) | |
| def require_clean(results, success_reason): | |
| """Apply one all-members rule to shared heads and generated queue heads. | |
| Empty membership is unknown, never vacuous approval. Findings take | |
| precedence over waiting; any non-success result prevents success. | |
| """ | |
| results = list(results) | |
| if not results: | |
| return Result("pending", "Unknown review membership") | |
| blockers = [result for result in results if result.state != "success"] | |
| if blockers: | |
| return next((r for r in blockers if r.state == "failure"), blockers[0]) | |
| return Result("success", success_reason) | |
| def combine_results(pulls, results): | |
| """Require each open PR sharing a commit to have its own clean result. | |
| A status belongs to a commit, not to a PR. Publishing independent answers | |
| under the same context lets the last PR overwrite the others. Group all | |
| open PRs, including drafts and stacks targeting other branches, so a clean | |
| review of one diff cannot supply a missing review of another diff. | |
| """ | |
| groups = defaultdict(list) | |
| for pull in pulls: | |
| groups[pull["head"]["sha"]].append(pull["number"]) | |
| combined = {} | |
| for head, numbers in groups.items(): | |
| combined[head] = require_clean( | |
| (Result(results[n].state, f"PR #{n}: {results[n].reason}") | |
| for n in numbers), | |
| f"Clean Gate 2 review for all {len(numbers)} PR(s)") | |
| return combined | |
| class GitHub: | |
| """The effect boundary: fixed-host API reads and commit-status writes.""" | |
| def __init__(self, repository, token, authorized_reviewers=None): | |
| if (not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9-]*/[A-Za-z0-9_.-]+", repository) | |
| or repository.split("/")[-1] in {".", ".."}): | |
| raise ValueError("Expected an owner/repository name") | |
| self.repository = repository | |
| self.root = f"https://api.github.com/repos/{repository}/" | |
| self.token = token | |
| self.authorized_reviewers = ( | |
| tuple(authorized_reviewers) if authorized_reviewers is not None else None | |
| ) | |
| def request(self, path, data=None): | |
| return self.request_url(self.root + path, data) | |
| def request_url(self, url, data=None): | |
| request = Request(url, | |
| data=None if data is None else json.dumps(data).encode(), | |
| headers={"Authorization": f"Bearer {self.token}", | |
| "Accept": "application/vnd.github+json", | |
| "Content-Type": "application/json", | |
| "X-GitHub-Api-Version": "2022-11-28"}) | |
| try: | |
| with urlopen(request, timeout=30) as response: | |
| return json.load(response) | |
| except HTTPError as error: | |
| # Do not log request headers or retry an ambiguous status write. | |
| # A manual recheck recomputes the result instead of replaying it. | |
| raise RuntimeError( | |
| f"GitHub {request.get_method()}: HTTP {error.code}") from None | |
| def graphql(self, query, variables): | |
| # GraphQL can return errors with HTTP 200. Both metadata readers must | |
| # reject that response rather than interpret missing data as no work. | |
| response = self.request_url("https://api.github.com/graphql", { | |
| "query": query, "variables": variables, | |
| }) | |
| data = response.get("data") | |
| if (response.get("errors") or not isinstance(data, dict) | |
| or not isinstance(data.get("repository"), dict)): | |
| raise RuntimeError("GitHub could not read review context") | |
| return data["repository"] | |
| def pages(self, path): | |
| separator = "&" if "?" in path else "?" | |
| page = 1 | |
| while True: | |
| batch = self.request(f"{path}{separator}per_page=100&page={page}") | |
| if not isinstance(batch, list): | |
| raise ValueError("Expected a paginated GitHub array") | |
| yield from batch | |
| if len(batch) < 100: | |
| return | |
| page += 1 | |
| def pulls(self): | |
| return list(self.pages("pulls?state=open&sort=created&direction=asc")) | |
| def comments(self, number): | |
| return list(self.pages(f"issues/{number}/comments")) | |
| def reviews(self, number): | |
| return list(self.pages(f"pulls/{number}/reviews")) | |
| def _read_commit(self, sha): | |
| if not isinstance(sha, str) or not SHA1_RE.fullmatch(sha): | |
| raise ValueError(f"Invalid commit SHA: {sha!r}") | |
| commit = self.request(f"commits/{sha}") | |
| if not isinstance(commit, dict) or commit.get("sha") != sha: | |
| raise RuntimeError("GitHub returned unexpected commit metadata") | |
| commit_obj = commit.get("commit") | |
| if not isinstance(commit_obj, dict): | |
| raise RuntimeError("GitHub returned invalid commit metadata") | |
| tree_obj = commit_obj.get("tree") | |
| tree_sha = tree_obj.get("sha") if isinstance(tree_obj, dict) else None | |
| if not isinstance(tree_sha, str) or not SHA1_RE.fullmatch(tree_sha): | |
| raise RuntimeError("GitHub returned invalid commit tree SHA") | |
| return commit, commit_obj, tree_sha | |
| def commit_tree(self, sha): | |
| _, _, tree_sha = self._read_commit(sha) | |
| return tree_sha | |
| def commit_info(self, sha): | |
| commit, commit_obj, tree_sha = self._read_commit(sha) | |
| raw_parents = commit.get("parents") | |
| if not isinstance(raw_parents, list): | |
| raise RuntimeError("GitHub returned invalid commit parents") | |
| parents = [] | |
| for parent in raw_parents: | |
| psha = parent.get("sha") if isinstance(parent, dict) else None | |
| if not isinstance(psha, str) or not SHA1_RE.fullmatch(psha): | |
| raise RuntimeError("GitHub returned invalid parent commit SHA") | |
| parents.append(psha) | |
| message = commit_obj.get("message") | |
| if not isinstance(message, str): | |
| raise RuntimeError("GitHub returned invalid commit message") | |
| return { | |
| "sha": sha, | |
| "tree_sha": tree_sha, | |
| "parents": parents, | |
| "message": message, | |
| } | |
| def request_text(self, path, accept): | |
| request = Request(self.root + path, | |
| headers={"Authorization": f"Bearer {self.token}", | |
| "Accept": accept, | |
| "X-GitHub-Api-Version": "2022-11-28"}) | |
| try: | |
| with urlopen(request, timeout=30) as response: | |
| return response.read().decode("utf-8") | |
| except HTTPError as error: | |
| raise RuntimeError( | |
| f"GitHub {request.get_method()}: HTTP {error.code}") from None | |
| def commit_files(self, sha): | |
| commit, _, _ = self._read_commit(sha) | |
| files = commit.get("files") | |
| if not isinstance(files, list): | |
| raise RuntimeError("GitHub returned invalid commit files") | |
| return files | |
| def commit_diff(self, sha): | |
| if not isinstance(sha, str) or not SHA1_RE.fullmatch(sha): | |
| raise ValueError(f"Invalid commit SHA: {sha!r}") | |
| return self.request_text(f"commits/{sha}", "application/vnd.github.diff") | |
| def compare(self, base, head): | |
| for sha in (base, head): | |
| if not isinstance(sha, str) or not SHA1_RE.fullmatch(sha): | |
| raise ValueError(f"Invalid commit SHA: {sha!r}") | |
| data = self.request(f"compare/{base}...{head}") | |
| if not isinstance(data, dict) or not isinstance(data.get("status"), str): | |
| raise RuntimeError("GitHub returned invalid comparison") | |
| files = data.get("files", []) | |
| if not isinstance(files, list): | |
| raise RuntimeError("GitHub returned invalid comparison files") | |
| return data["status"], files | |
| def compare_with_merge_base(self, base, head): | |
| for sha in (base, head): | |
| if not isinstance(sha, str) or not SHA1_RE.fullmatch(sha): | |
| raise ValueError(f"Invalid commit SHA: {sha!r}") | |
| data = self.request(f"compare/{base}...{head}") | |
| mb = ((data.get("merge_base_commit") or {}).get("sha") | |
| if isinstance(data, dict) else None) | |
| files = data.get("files", []) if isinstance(data, dict) else None | |
| if (not isinstance(data, dict) | |
| or not isinstance(data.get("status"), str) | |
| or not isinstance(mb, str) or not SHA1_RE.fullmatch(mb) | |
| or not isinstance(files, list)): | |
| raise RuntimeError("GitHub returned invalid comparison") | |
| return data["status"], mb, files | |
| def blob_text(self, sha): | |
| if not isinstance(sha, str) or not SHA1_RE.fullmatch(sha): | |
| raise ValueError(f"Invalid blob SHA: {sha!r}") | |
| blob = self.request(f"git/blobs/{sha}") | |
| if (not isinstance(blob, dict) or blob.get("sha") != sha | |
| or blob.get("encoding") != "base64" | |
| or not isinstance(blob.get("content"), str)): | |
| raise RuntimeError("GitHub returned invalid blob") | |
| raw = base64.b64decode(blob["content"]) | |
| if len(raw) > DELTA_MAX_BLOB_BYTES: | |
| raise ValueError("Blob exceeds delta verification limit") | |
| return raw.decode("utf-8") | |
| def tree_entries(self, tree_sha): | |
| if not isinstance(tree_sha, str) or not SHA1_RE.fullmatch(tree_sha): | |
| raise ValueError(f"Invalid tree SHA: {tree_sha!r}") | |
| data = self.request(f"git/trees/{tree_sha}?recursive=1") | |
| if (not isinstance(data, dict) or data.get("sha") != tree_sha | |
| or data.get("truncated") is not False | |
| or not isinstance(data.get("tree"), list)): | |
| raise RuntimeError("GitHub returned invalid or truncated git tree") | |
| entries = {} | |
| for item in data["tree"]: | |
| if not isinstance(item, dict): | |
| raise RuntimeError("GitHub returned invalid tree entry") | |
| typ = item.get("type") | |
| if typ == "tree": | |
| continue | |
| path = item.get("path") | |
| mode = item.get("mode") | |
| sha = item.get("sha") | |
| if (not _is_non_empty_str(path) or not _is_non_empty_str(mode) | |
| or not _is_non_empty_str(typ) | |
| or not isinstance(sha, str) or not SHA1_RE.fullmatch(sha) | |
| or path in entries): | |
| raise RuntimeError("GitHub returned malformed tree entry") | |
| entries[path] = (mode, typ, sha) | |
| return entries | |
| def verify_delta_base_advance(self, anchor, delta_payload): | |
| """Confirm via GitHub comparisons and trees that a delta review is in | |
| scope. | |
| Restricts delta reviews strictly to `tier0_docs` and `tier1_code` PRs | |
| (rejecting all Tier 2, TCB, CI, workflow, CODEOWNERS, build/manifest/ | |
| lock/pin, skill, vendor, dot-prefixed, and executable-script paths plus | |
| non-`100644` modes). Accepts either an unchanged merge-base while | |
| `main` moved forward independently or a clean zero-conflict rebase onto | |
| a newer base, provided `anchor.base_sha..base_sha` touched no | |
| review-governing file, policy config, or dot-path and stayed disjoint | |
| from every PR file, `ci/checks.toml` and `.github/CODEOWNERS` match the | |
| evaluator's checkout in both trees, and the full-blob interdiff stays | |
| within the delta file, line, fraction, and whole-blob token caps. | |
| """ | |
| a_head, a_base = anchor["head_sha"], anchor["base_sha"] | |
| a_tree = anchor.get("tree_sha") | |
| a_mb = anchor.get("merge_base", a_base) | |
| d_head, d_base = delta_payload["head_sha"], delta_payload["base_sha"] | |
| d_tree = delta_payload.get("tree_sha") | |
| d_mb = delta_payload.get("merge_base", d_base) | |
| a_tier = anchor.get("risk_tier") | |
| d_tier = delta_payload.get("risk_tier") | |
| if (_get_ci_policy_rules() is None | |
| or a_tier not in GATE2_DELTA_TIER_RANK | |
| or d_tier not in GATE2_DELTA_TIER_RANK | |
| or GATE2_DELTA_TIER_RANK[d_tier] > GATE2_DELTA_TIER_RANK[a_tier] | |
| or not isinstance(a_tree, str) | |
| or not SHA1_RE.fullmatch(a_tree) | |
| or not isinstance(d_tree, str) | |
| or not SHA1_RE.fullmatch(d_tree)): | |
| return False | |
| a_status, a_actual_mb, prior_files = self.compare_with_merge_base( | |
| a_base, a_head, | |
| ) | |
| d_status, d_actual_mb, new_files = self.compare_with_merge_base( | |
| d_base, d_head, | |
| ) | |
| if (a_mb != a_base or a_actual_mb != a_base or d_actual_mb != d_mb | |
| or a_status != "ahead" | |
| or d_status not in ("ahead", "diverged") | |
| or (d_base == a_base and d_mb != a_base) | |
| or not prior_files or len(prior_files) >= MAX_API_FILES | |
| or not new_files or len(new_files) >= MAX_API_FILES): | |
| return False | |
| prior_paths = pr_paths(prior_files) | |
| new_paths = pr_paths(new_files) | |
| if not new_paths <= prior_paths: | |
| return False | |
| all_pr_paths = prior_paths | new_paths | |
| anchor_rank = GATE2_DELTA_TIER_RANK[a_tier] | |
| delta_rank = GATE2_DELTA_TIER_RANK[d_tier] | |
| if (any(GATE2_DELTA_TIER_RANK.get(_min_path_risk_tier(p), 2) | |
| > anchor_rank for p in prior_paths) | |
| or any(GATE2_DELTA_TIER_RANK.get(_min_path_risk_tier(p), 2) | |
| > delta_rank for p in new_paths)): | |
| return False | |
| if a_base != d_base: | |
| if not _delta_base_advance_allows( | |
| *self.compare(a_base, d_base), all_pr_paths, | |
| ): | |
| return False | |
| if d_mb != a_mb and d_mb != d_base: | |
| if (not _delta_base_advance_allows( | |
| *self.compare(a_mb, d_mb), all_pr_paths, | |
| ) | |
| or not _delta_base_advance_allows( | |
| *self.compare(d_mb, d_base), all_pr_paths, | |
| )): | |
| return False | |
| prior_by_name = {f["filename"]: f for f in prior_files} | |
| new_by_name = {f["filename"]: f for f in new_files} | |
| if (len(prior_by_name) != len(prior_files) | |
| or len(new_by_name) != len(new_files) | |
| or not set(new_by_name) <= set(prior_by_name)): | |
| return False | |
| a_entries = self.tree_entries(a_tree) | |
| d_entries = self.tree_entries(d_tree) | |
| if not _policy_configs_match_trees(a_entries, d_entries): | |
| return False | |
| pr_changed_lines = 0 | |
| for entry in new_files: | |
| if _is_delta_generated_path(entry["filename"]): | |
| continue | |
| adds, dels = entry.get("additions"), entry.get("deletions") | |
| if _is_int_ge(adds, 0) and _is_int_ge(dels, 0): | |
| pr_changed_lines += adds + dels | |
| elif isinstance(entry.get("patch"), str): | |
| pr_changed_lines += sum( | |
| 1 for ln in _patch_change_lines(entry["patch"]) | |
| if ln.startswith(("+", "-")) | |
| ) | |
| else: | |
| return False | |
| fix_files = [] | |
| altered_lines = 0 | |
| blob_cache = {} | |
| def read_blob(sha): | |
| if sha not in blob_cache: | |
| if len(blob_cache) >= 2 * DELTA_MAX_FIX_FILES: | |
| return None | |
| blob_cache[sha] = self.blob_text(sha) | |
| return blob_cache[sha] | |
| for name, old_entry in prior_by_name.items(): | |
| new_entry = new_by_name.get(name) | |
| a_ent = a_entries.get(name) | |
| d_ent = d_entries.get(name) | |
| for ent in (a_ent, d_ent): | |
| if ent is not None and (ent[0] != "100644" or ent[1] != "blob"): | |
| return False | |
| old_status = old_entry.get("status") | |
| old_prev = old_entry.get("previous_filename") | |
| old_sha = old_entry.get("sha") | |
| if old_status == "removed": | |
| if a_ent is not None: | |
| return False | |
| elif (a_ent is None or not isinstance(old_sha, str) | |
| or not SHA1_RE.fullmatch(old_sha) or a_ent[2] != old_sha): | |
| return False | |
| if old_prev is not None and a_entries.get(old_prev) is not None: | |
| return False | |
| if new_entry is not None: | |
| new_status = new_entry.get("status") | |
| new_prev = new_entry.get("previous_filename") | |
| new_sha = new_entry.get("sha") | |
| if new_status == "removed": | |
| if d_ent is not None: | |
| return False | |
| elif (d_ent is None or not isinstance(new_sha, str) | |
| or not SHA1_RE.fullmatch(new_sha) or d_ent[2] != new_sha): | |
| return False | |
| if new_prev is not None and d_entries.get(new_prev) is not None: | |
| return False | |
| if (old_status != new_status or old_prev != new_prev | |
| or (a_ent is None) != (d_ent is None)): | |
| return False | |
| else: | |
| if old_prev is not None: | |
| return False | |
| if old_status == "added": | |
| if d_ent is not None: | |
| return False | |
| elif old_status == "modified": | |
| if d_ent is None: | |
| return False | |
| else: | |
| return False | |
| if new_entry is not None and a_ent == d_ent: | |
| continue | |
| fix_files.append(name) | |
| if len(fix_files) > DELTA_MAX_FIX_FILES: | |
| return False | |
| old_text = read_blob(a_ent[2]) | |
| new_text = "" if d_ent is None else read_blob(d_ent[2]) | |
| if old_text is None or new_text is None: | |
| return False | |
| delta_n = _blob_delta_lines(old_text, new_text, path=name) | |
| if delta_n is None: | |
| return False | |
| altered_lines += delta_n | |
| if altered_lines > DELTA_MAX_FIX_LINES or pr_changed_lines <= 0: | |
| return False | |
| return (altered_lines / pr_changed_lines) <= DELTA_MAX_FIX_FRACTION | |
| def verify_rebase_carry_forward(self, old_head, old_base, old_tree, | |
| new_head, new_base, other_heads=()): | |
| """Returns `old_head`'s one-commit identity key if `new_head` only | |
| rebases it onto a later base, else None. | |
| Both must be single commits on their (different) bases with the same | |
| message and the same file changes and modes, `old_head` must have | |
| the reviewed `old_tree`, and the base must have moved forward | |
| without touching the PR's files (the allowlisted manifest excepted) | |
| or the review rules. Each of `other_heads` (heads with findings or | |
| failures) must make different changes than `old_head`, so that no | |
| finding about this patch is skipped. | |
| """ | |
| if old_base == new_base: | |
| return None | |
| commits = {} | |
| for sha in (old_head, new_head): | |
| commit, commit_obj, tree_sha = self._read_commit(sha) | |
| parents = commit.get("parents") | |
| files = commit.get("files") | |
| message = commit_obj.get("message") | |
| if (not isinstance(parents, list) or not isinstance(files, list) | |
| or not isinstance(message, str)): | |
| return None | |
| commits[sha] = ( | |
| [p.get("sha") if isinstance(p, dict) else None for p in parents], | |
| tree_sha, message, files, | |
| ) | |
| old_parents, old_tree_actual, old_message, old_files = commits[old_head] | |
| new_parents, _, new_message, new_files = commits[new_head] | |
| if (old_parents != [old_base] or new_parents != [new_base] | |
| or old_tree_actual != old_tree or old_message != new_message): | |
| return None | |
| status, base_files = self.compare(old_base, new_base) | |
| relaxed = base_drift_allows_carry_forward( | |
| status, base_files, pr_paths(old_files) | pr_paths(new_files), | |
| ) | |
| if relaxed is None or not rebase_only_patch_equivalent( | |
| old_files, new_files, relaxed): | |
| return None | |
| names = {entry["filename"] for entry in new_files} | |
| old_modes = diff_file_modes(self.commit_diff(old_head)) | |
| new_modes = diff_file_modes(self.commit_diff(new_head)) | |
| if (old_modes is None or new_modes is None or set(old_modes) != names | |
| or old_modes != new_modes): | |
| return None | |
| old_changes = _change_set(old_files) | |
| for sha in other_heads: | |
| try: | |
| other_files = self.commit_files(sha) | |
| except (RuntimeError, ValueError): | |
| return None | |
| other_changes = _change_set(other_files) | |
| if other_changes is None or other_changes == old_changes: | |
| return None | |
| parent_tree = self.commit_tree(old_base) | |
| msg_sha256 = hashlib.sha256(old_message.encode("utf-8")).hexdigest() | |
| return f"one:{parent_tree}:{old_tree}:{msg_sha256}" | |
| def commit_identity(self, head, base_sha): | |
| if not isinstance(base_sha, str) or not SHA1_RE.fullmatch(base_sha): | |
| raise ValueError(f"Invalid base commit SHA: {base_sha!r}") | |
| info = self.commit_info(head) | |
| tree_sha = info["tree_sha"] | |
| if len(info["parents"]) == 1 and info["parents"][0] == base_sha: | |
| parent_tree = self.commit_tree(base_sha) | |
| msg_sha256 = hashlib.sha256( | |
| info["message"].encode("utf-8") | |
| ).hexdigest() | |
| return tree_sha, f"one:{parent_tree}:{tree_sha}:{msg_sha256}" | |
| return tree_sha, f"multi:{base_sha}:{head}" | |
| def deleted_summaries(self): | |
| deleted = {} | |
| for status in self.pages(f"commits/{STATE_ANCHOR}/statuses"): | |
| match = re.fullmatch(r"Codex summary deletion/([1-9][0-9]*)/([1-9][0-9]*)", | |
| status["context"]) | |
| if (match and status["state"] == "success" | |
| and status.get("creator", {}).get("login") == PUBLISHER_LOGIN): | |
| number, comment = map(int, match.groups()) | |
| deleted[number] = max(deleted.get(number, 0), comment) | |
| return deleted | |
| def record_deletion(self, event): | |
| """Record deletion events before they enter a coalescing job queue. | |
| A user may delete a bot comment, so authenticate the deleted comment's | |
| author, not the user who performed the deletion. Each event has a unique | |
| context: concurrent recorders cannot overwrite another deletion. | |
| """ | |
| comment = event.get("comment") or {} | |
| issue = event.get("issue") or {} | |
| if (event.get("action") != "deleted" or not issue.get("pull_request") | |
| or not (authentic_summary(comment) | |
| or authentic_gate2_item(comment, self.authorized_reviewers))): | |
| return | |
| number, comment_id = issue["number"], comment["id"] | |
| self.publish_status(STATE_ANCHOR, { | |
| "context": f"{DELETION_CONTEXT}{number}/{comment_id}", | |
| "state": "success", "description": "Deleted Codex summary; older results cannot approve", | |
| "target_url": f"https://github.com/{self.repository}/pull/{number}", | |
| }) | |
| def queue_groups(self): | |
| """Read the main queue's generated heads and their required PR prefix. | |
| GitHub's MergeQueueEntry exposes the generated headCommit for an entry, | |
| its queue position, and its pullRequest. A group may incorporate earlier | |
| entries, so require the entire prefix through that position, including | |
| entries which do not yet have a generated commit. Never infer membership | |
| from a branch name or assume squash commits retain PR ancestry. | |
| """ | |
| owner, name = self.repository.split("/") | |
| query = """query($owner: String!, $name: String!, $after: String) { | |
| repository(owner: $owner, name: $name) { | |
| mergeQueue(branch: "main") { entries(first: 100, after: $after) { | |
| nodes { position headCommit { oid } | |
| pullRequest { number headRefOid } } | |
| pageInfo { hasNextPage endCursor } | |
| } } | |
| } | |
| }""" | |
| entries, after = [], None | |
| while True: | |
| queue = self.graphql(query, { | |
| "owner": owner, "name": name, "after": after, | |
| })["mergeQueue"] | |
| if queue is None: | |
| return {} | |
| page = queue["entries"] | |
| entries.extend(page["nodes"]) | |
| if not page["pageInfo"]["hasNextPage"]: | |
| break | |
| after = page["pageInfo"]["endCursor"] | |
| prefix, groups = {}, {} | |
| for entry in sorted(entries, key=lambda entry: entry["position"]): | |
| pull = entry["pullRequest"] | |
| prefix[pull["number"]] = pull["headRefOid"] | |
| if entry["headCommit"] is not None: | |
| head = entry["headCommit"]["oid"] | |
| groups.setdefault(head, {}).update(prefix) | |
| return groups | |
| def base_changes(self): | |
| """Read immutable base-change events, including changes before startup. | |
| A stable base in two consecutive API reads does not prove that an old | |
| review used that base. GitHub's PR timeline supplies the last retarget | |
| or base force-push even when no gate was deployed when it happened. | |
| """ | |
| owner, name = self.repository.split("/") | |
| query = """query($owner: String!, $name: String!, $after: String) { | |
| repository(owner: $owner, name: $name) { | |
| pullRequests(states: OPEN, first: 100, after: $after) { | |
| nodes { number timelineItems(last: 1, | |
| itemTypes: [BASE_REF_CHANGED_EVENT, BASE_REF_FORCE_PUSHED_EVENT]) { | |
| nodes { ... on BaseRefChangedEvent { createdAt } | |
| ... on BaseRefForcePushedEvent { createdAt } } | |
| } } | |
| pageInfo { hasNextPage endCursor } | |
| } | |
| } | |
| }""" | |
| changes, after = {}, None | |
| while True: | |
| page = self.graphql(query, { | |
| "owner": owner, "name": name, "after": after, | |
| })["pullRequests"] | |
| for pull in page["nodes"]: | |
| events = pull["timelineItems"]["nodes"] | |
| changes[pull["number"]] = events[-1]["createdAt"] if events else None | |
| if not page["pageInfo"]["hasNextPage"]: | |
| return changes | |
| after = page["pageInfo"]["endCursor"] | |
| def publish(self, head, result): | |
| # Use the SHA evaluated, never a branch name that could have advanced. | |
| # The Actions job's own check belongs to its default-branch checkout; | |
| # this explicit status is what branch protection requires on the PR. | |
| desired = { | |
| "context": CONTEXT, "state": result.state, | |
| "description": result.reason[:140], | |
| "target_url": f"https://github.com/{self.repository}/actions/workflows/codex-review.yml", | |
| } | |
| self.publish_status(head, desired) | |
| def publish_status(self, head, desired): | |
| # GitHub limits the number of statuses per commit and context. An | |
| # unrelated PR's event must not consume another entry for an unchanged | |
| # verdict. The API returns statuses newest first; stop at this context. | |
| current = next((status for status in self.pages(f"commits/{head}/statuses") | |
| if status["context"].casefold() == desired["context"].casefold()), {}) | |
| if (current.get("creator", {}).get("login") == PUBLISHER_LOGIN | |
| and all(current.get(key) == value | |
| for key, value in desired.items())): | |
| return | |
| self.request(f"statuses/{head}", desired) | |
| def publish_results(github, results, publish): | |
| """Use the same reporting path for normal verdicts and failed reads.""" | |
| for head, result in sorted(results.items()): | |
| print(f"{head}: {result.state}: {result.reason}") | |
| if publish: | |
| github.publish(head, result) | |
| def _parse_gate2_payload_for_reconcile(body): | |
| try: | |
| payload, _, _, _ = parse_gate2_payload(body) | |
| has_findings = ( | |
| payload["verdict"] == "findings" or bool(payload.get("findings")) | |
| ) | |
| return payload, has_findings | |
| except ValueError: | |
| try: | |
| payload, _, _, _ = parse_gate2_payload( | |
| body, strict_identity_key=False | |
| ) | |
| except ValueError: | |
| return None, False | |
| has_findings = ( | |
| payload["verdict"] == "findings" or bool(payload.get("findings")) | |
| ) | |
| if not has_findings: | |
| return None, False | |
| return payload, True | |
| def reconcile(github, publish=False, requested_group=None): | |
| """Recompute every open PR, because serialized event runs can be coalesced. | |
| GitHub may replace a pending concurrency-group run with a newer one. Since | |
| every run reads all open PRs, the retained run also covers the discarded | |
| event. Deletion tombstones are recorded separately before coalescing, since | |
| the comments API alone cannot recover a deleted summary's existence. | |
| """ | |
| pulls = github.pulls() | |
| try: | |
| context = read_context(github, pulls) | |
| except (RuntimeError, ValueError, OSError) as error: | |
| # A partial API outage must not preserve success merely because the | |
| # metadata failed before comment evaluation. Revoke known heads when | |
| # the status API remains available, then expose the operational error. | |
| blocked = {pull["head"]["sha"]: Result("pending", "Review context unavailable") | |
| for pull in pulls} | |
| if requested_group: | |
| blocked[requested_group] = Result("pending", "Review context unavailable") | |
| publish_results(github, blocked, publish) | |
| raise RuntimeError(str(error)) from None | |
| snapshots = {} | |
| identities = {} | |
| tree_cache = {} | |
| missing_historical = set() | |
| missing_codex_commits = set() | |
| results = {} | |
| read_errors = [] | |
| authorized = getattr(github, "authorized_reviewers", None) | |
| for pull in pulls: | |
| number = pull["number"] | |
| head = pull["head"]["sha"] | |
| base_sha = pull["base"]["sha"] | |
| try: | |
| pr_comments = github.comments(number) | |
| pr_reviews = github.reviews(number) | |
| deleted_through = context.deleted.get(number, 0) | |
| active_gate2_items = [] | |
| for source, entries in (("comment", pr_comments), ("review", pr_reviews)): | |
| for item in entries: | |
| if not authentic_gate2_item(item, authorized): | |
| continue | |
| parsed, has_findings = _parse_gate2_payload_for_reconcile( | |
| item.get("body") or "" | |
| ) | |
| if (source == "comment" | |
| and item["id"] <= deleted_through | |
| and not has_findings): | |
| continue | |
| active_gate2_items.append((source, item, parsed, has_findings)) | |
| active_codex_finding_shas = [] | |
| for comment in pr_comments: | |
| if not authentic_summary(comment): | |
| continue | |
| try: | |
| codex_review = _parse_codex_summary(comment.get("body") or "") | |
| except ValueError: | |
| continue | |
| c_sha = codex_review.get("commit_sha") | |
| if (_codex_summary_has_completed_findings(codex_review) | |
| and isinstance(c_sha, str) | |
| and SHA1_RE.fullmatch(c_sha) is not None | |
| and c_sha != head): | |
| active_codex_finding_shas.append(c_sha) | |
| tree_sha = None | |
| identity_key = None | |
| historical_identities = {} | |
| historical_trees = {} | |
| if active_gate2_items or active_codex_finding_shas: | |
| cache_key = (head, base_sha) | |
| if cache_key not in identities: | |
| identities[cache_key] = github.commit_identity(head, base_sha) | |
| tree_sha, identity_key = identities[cache_key] | |
| tree_cache[head] = tree_sha | |
| is_one_identity = ( | |
| isinstance(identity_key, str) | |
| and ONE_IDENTITY_RE.fullmatch(identity_key) is not None | |
| ) | |
| for source, item, item_payload, has_findings in active_gate2_items: | |
| if item_payload is None: | |
| continue | |
| p_head = item_payload["head_sha"] | |
| p_base = item_payload["base_sha"] | |
| if (p_head, p_base) == (head, base_sha): | |
| continue | |
| same_tree_finding = ( | |
| has_findings and item_payload["tree_sha"] == tree_sha | |
| ) | |
| # Verify every same-tree non-finding item whose | |
| # identity_key does not contradict ours: clean ones may | |
| # transfer, and failing ones must keep blocking. | |
| item_ik = item_payload.get("identity_key") | |
| matching_one_candidate = ( | |
| not has_findings | |
| and is_one_identity | |
| and item_payload["tree_sha"] == tree_sha | |
| and not (_is_structured_identity_key(item_ik) | |
| and item_ik != identity_key) | |
| ) | |
| if not (same_tree_finding or matching_one_candidate): | |
| continue | |
| if same_tree_finding: | |
| if p_head not in tree_cache: | |
| tree_cache[p_head] = github.commit_tree(p_head) | |
| historical_trees[p_head] = tree_cache[p_head] | |
| elif matching_one_candidate: | |
| hist_key = (p_head, p_base) | |
| if (hist_key not in identities | |
| and hist_key not in missing_historical): | |
| try: | |
| identities[hist_key] = github.commit_identity( | |
| p_head, p_base | |
| ) | |
| except RuntimeError as err: | |
| err_str = str(err) | |
| if ("HTTP 404" in err_str | |
| or "HTTP 422" in err_str): | |
| missing_historical.add(hist_key) | |
| else: | |
| raise | |
| if hist_key in missing_historical: | |
| # An unverifiable commit only denies clean reuse; | |
| # a failure recorded on it must fail closed. | |
| if _is_blocking_non_finding_failure( | |
| source, item, p_head, p_base, | |
| item_payload["tree_sha"], | |
| ): | |
| raise RuntimeError( | |
| "Cannot verify historical commit for a " | |
| "blocking Gate 2 review" | |
| ) | |
| continue | |
| historical_identities[hist_key] = identities[hist_key] | |
| # A delta review's anchor tree must be GitHub-verified (see | |
| # `_delta_review_problem`); an unresolvable anchor commit only | |
| # keeps the delta review pending. | |
| for _, _, item_payload, _ in active_gate2_items: | |
| if (item_payload is None | |
| or item_payload.get("review_mode") != "delta" | |
| or "delta_from" not in item_payload): | |
| continue | |
| d_sha = item_payload["delta_from"] | |
| if d_sha not in tree_cache: | |
| try: | |
| tree_cache[d_sha] = github.commit_tree(d_sha) | |
| except RuntimeError as err: | |
| err_str = str(err) | |
| if "HTTP 404" in err_str or "HTTP 422" in err_str: | |
| continue | |
| raise | |
| historical_trees[d_sha] = tree_cache[d_sha] | |
| for c_sha in dict.fromkeys(active_codex_finding_shas): | |
| if c_sha in missing_codex_commits: | |
| continue | |
| if c_sha not in tree_cache: | |
| try: | |
| tree_cache[c_sha] = github.commit_tree(c_sha) | |
| except RuntimeError as err: | |
| err_str = str(err) | |
| # A legacy Codex summary records no tree, so an | |
| # unresolvable commit cannot be shown to share | |
| # the current tree; like any other-tree finding, | |
| # it does not block. | |
| if ("HTTP 404" in err_str | |
| or "HTTP 422" in err_str): | |
| missing_codex_commits.add(c_sha) | |
| continue | |
| raise | |
| historical_trees[c_sha] = tree_cache[c_sha] | |
| verify = getattr(github, "verify_rebase_carry_forward", None) | |
| verify_delta = getattr(github, "verify_delta_base_advance", None) | |
| results[number] = review_result( | |
| head, pr_comments, | |
| deleted_through, context.bases[number], | |
| reviews=pr_reviews, | |
| base_sha=base_sha, | |
| tree_sha=tree_sha, | |
| authorized_reviewers=authorized, | |
| identity_key=identity_key, | |
| historical_identities=historical_identities, | |
| historical_trees=historical_trees, | |
| carry_forward_verifier=verify, | |
| delta_base_verifier=verify_delta, | |
| ) | |
| snapshots[number] = review_snapshot(pr_comments, pr_reviews, authorized) | |
| except (RuntimeError, ValueError, OSError) as error: | |
| # A failed read replaces old success with a blocking verdict where | |
| # the status API remains available. Other PRs are still evaluated. | |
| results[number] = Result("failure", "Cannot read Gate 2 reviews or summary") | |
| read_errors.append(f"PR #{number}: {error}") | |
| # A push, retarget, newly opened PR, or closure can change grouping while | |
| # comments are read. Leave statuses pending and let the queued event (or a | |
| # manual recheck) read a new snapshot rather than publish a mixed snapshot. | |
| try: | |
| if context != read_context(github): | |
| raise RuntimeError("Review context changed during evaluation") | |
| for number, original in snapshots.items(): | |
| # PR metadata does not change when a review or summary is edited. | |
| # Include that evidence in the final read, rather than revalidating | |
| # only the commit graph and accepting a superseded verdict. | |
| if original != review_snapshot( | |
| github.comments(number), github.reviews(number), authorized | |
| ): | |
| results[number] = Result("pending", "Gate 2 review or summary changed; recheck required") | |
| except (RuntimeError, ValueError, OSError) as error: | |
| results = {pull["number"]: Result("pending", "PR snapshot changed or is unavailable") | |
| for pull in pulls} | |
| read_errors.append(str(error)) | |
| pr_verdicts = combine_results(pulls, results) | |
| combined = dict(pr_verdicts) | |
| by_number = {pull["number"]: pull for pull in pulls} | |
| for head, members in context.queues.items(): | |
| queue_results = [] | |
| for number, expected_head in members.items(): | |
| pull = by_number.get(number) | |
| if pull is None or pull["head"]["sha"] != expected_head: | |
| queue_results.append(Result("pending", "Merge queue PR head changed")) | |
| else: | |
| queue_results.append(pr_verdicts[expected_head]) | |
| # Use shared-head verdicts so queueing one PR cannot bypass a blocked | |
| # duplicate. An empty or unknown queue membership never passes. | |
| combined[head] = require_clean( | |
| queue_results, "Clean Gate 2 reviews for the merge queue group") | |
| if requested_group and requested_group not in context.queues: | |
| combined[requested_group] = Result("pending", "Merge queue membership is not available; recheck") | |
| publish_results(github, combined, publish) | |
| if read_errors: | |
| raise RuntimeError("; ".join(read_errors)) | |
| return combined | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--repository", default=os.environ.get("GITHUB_REPOSITORY")) | |
| parser.add_argument( | |
| "--reviewer-login", | |
| action="append", | |
| dest="reviewer_logins", | |
| default=None, | |
| help="Authorized Gate 2 reviewer GitHub login (repeatable; defaults to OWNER/MEMBER/COLLABORATOR).", | |
| ) | |
| effects = parser.add_mutually_exclusive_group() | |
| effects.add_argument("--publish", action="store_true") | |
| effects.add_argument("--record-deletion", action="store_true") | |
| args = parser.parse_args() | |
| token = os.environ.get("GH_TOKEN") | |
| if not args.repository or not token: | |
| parser.error("Supply --repository and GH_TOKEN") | |
| github = GitHub(args.repository, token, authorized_reviewers=args.reviewer_logins) | |
| event_path = os.environ.get("GITHUB_EVENT_PATH") | |
| with open(event_path, encoding="utf-8") if event_path else io.StringIO("{}") as source: | |
| event = json.load(source) | |
| if args.record_deletion: | |
| github.record_deletion(event) | |
| else: | |
| group = (event.get("merge_group") or {}).get("head_sha") | |
| reconcile(github, args.publish, group) | |
| if __name__ == "__main__": | |
| try: | |
| main() | |
| except (RuntimeError, ValueError, OSError) as error: | |
| sys.exit(str(error)) |