From f61c323d413517e44c4fe614f1aec9ae44dc76cf Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:22:52 +0100 Subject: [PATCH 1/2] Share the mergeability verdict with hosted source refreshes Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 3 +++ src/loopzero/candidate.py | 1 + src/loopzero/cli.py | 4 +--- src/loopzero/eligibility.py | 18 ++++++++++++++++-- src/loopzero/github.py | 13 +++++++++++-- src/loopzero/hosted.py | 26 ++++++++++++++++++-------- tests/test_hosted.py | 18 ++++++++++++++++++ 7 files changed, 68 insertions(+), 15 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 49620cba..8cf3f82d 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -114,6 +114,9 @@ loading the delivery [commands](../src/loopzero/cli.py), which implement the con for local sandbox receipts. Explicit settings, delegated runner environments and stage reuse keys still bind the grant. Waiters check for policy changes on every poll. + Local readiness and hosted eligibility share the mergeability verdict. + Hosted source refresh retries an unknown result on the same head before + leaving it pending. Candidate source evidence includes mergeability. Local readiness and hosted eligibility carry a review forward only across a first-parent chain of verified merges from the configured PR base. They reconstruct each merge in an isolated Git repository and compare its tree diff --git a/src/loopzero/candidate.py b/src/loopzero/candidate.py index 9179e09e..e57ee932 100644 --- a/src/loopzero/candidate.py +++ b/src/loopzero/candidate.py @@ -219,6 +219,7 @@ def _source_fingerprint(source: JsonObject) -> tuple[object, ...]: return ( source.get("state"), source.get("draft"), + source.get("mergeable"), source.get("body"), head.get("sha") if isinstance(head, dict) else None, base.get("ref") if isinstance(base, dict) else None, diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index f1645fb8..2a20d0cb 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -272,7 +272,7 @@ def _decide_kind(markers: list[Marker], head: str) -> tuple[str, str | None]: return "delta", reviewed -MERGEABILITY_PENDING = "GitHub is computing mergeability" +MERGEABILITY_PENDING = github.MERGEABILITY_PENDING def _readiness( @@ -331,8 +331,6 @@ def _readiness( result = dataclasses.replace(result, reasons=tuple( exhausted if reason == github.MISSING_REVIEW_REASON else reason for reason in result.reasons )) - if pr.state == "OPEN" and pr.mergeable == "UNKNOWN": - result = dataclasses.replace(result, ready=False, reasons=(*result.reasons, MERGEABILITY_PENDING)) return result diff --git a/src/loopzero/eligibility.py b/src/loopzero/eligibility.py index 3f2ab78a..bd86af90 100644 --- a/src/loopzero/eligibility.py +++ b/src/loopzero/eligibility.py @@ -10,6 +10,7 @@ import re import sys from pathlib import Path +from time import sleep from loopzero import candidate, config, github, runners from loopzero.types import LoopZeroError @@ -163,12 +164,22 @@ def readiness_review(reviews: list[dict], head: str, publishers: tuple[str, ...] return latest +def _source_snapshot(repo: str, number: int, head: str) -> github.PR: + pr = github.pr_view(repo, number) + for delay in (2, 4, 8): + if pr.head_sha != head or github.mergeability_blocker(pr) != github.MERGEABILITY_PENDING: + break + sleep(delay) + pr = github.pr_view(repo, number) + return pr + + def evaluate(repo: str, number: int, head: str, base: str, publishers: tuple[str, ...]) -> github.Readiness: """Re-fetch reviews and threads; no local receipts, CI identity or source execution.""" if not publishers or not re.fullmatch(r"[0-9a-f]{40}", head): raise LoopZeroError("eligibility requires trusted publishers and a full source SHA") - pr = github.pr_view(repo, number) + pr = _source_snapshot(repo, number, head) if pr.head_sha != head: return github.Readiness(False, ("source head changed; refresh eligibility",)) if hold := github.merge_hold(pr.number, pr.labels): @@ -190,8 +201,11 @@ def evaluate(repo: str, number: int, head: str, base: str, result = github.readiness(repo, pr, base, (), head if review else None, allow_behind=True, ancestor_review=review is not None and review["commit_id"] != head, publishers=publishers) - if github.pr_view(repo, number).head_sha != head: + current = github.pr_view(repo, number) + if current.head_sha != head: return github.Readiness(False, ("source head changed during eligibility refresh",)) + if result.ready and (blocker := github.mergeability_blocker(current)): + return github.Readiness(False, (blocker,)) return result diff --git a/src/loopzero/github.py b/src/loopzero/github.py index 23200686..0786a9ce 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -28,6 +28,7 @@ APP_TOKEN_HELPER = Path.home() / ".config/loopzero/app-token" _app_token_cache = ("", 0.0) MISSING_REVIEW_REASON = "no review recorded for the current head" +MERGEABILITY_PENDING = "GitHub is computing mergeability" MARKER_RE = re.compile( r"" @@ -1337,6 +1338,14 @@ def behind_reason(pr: PR, reviewed_head: str | None) -> str: ) +def mergeability_blocker(pr: PR) -> str | None: + if pr.mergeable == "CONFLICTING": + return f"PR #{pr.number} has merge conflicts with {pr.base_ref}" + if pr.state == "OPEN" and pr.mergeable == "UNKNOWN": + return MERGEABILITY_PENDING + return None + + def readiness( repo: str, pr: PR, @@ -1356,8 +1365,8 @@ def readiness( reasons.append(f"PR #{pr.number} is {pr.state}, not open") if pr.base_ref != base_branch: reasons.append(f"PR targets {pr.base_ref}, configured base is {base_branch}") - if pr.mergeable == "CONFLICTING": - reasons.append(f"PR #{pr.number} has merge conflicts with {pr.base_ref}") + if blocker := mergeability_blocker(pr): + reasons.append(blocker) if pr.merge_state == "BEHIND" and not allow_behind: reasons.append(behind_reason(pr, reviewed_head)) if not reviewed_head: diff --git a/src/loopzero/hosted.py b/src/loopzero/hosted.py index 08a2177f..b83c095b 100644 --- a/src/loopzero/hosted.py +++ b/src/loopzero/hosted.py @@ -62,6 +62,19 @@ def _candidate_event(event: dict, head: str) -> bool: return True +def _source_status(result: github.Readiness, current: github.PR) -> tuple[str, str]: + reasons = result.reasons + if result.ready: + if not (blocker := github.mergeability_blocker(current)): + return "success", "Reviewed source head" + reasons = (blocker,) + if reasons and set(reasons) <= {github.MISSING_REVIEW_REASON, github.MERGEABILITY_PENDING}: + if github.MISSING_REVIEW_REASON in reasons: + return "pending", "Awaiting trusted review of current source head" + return "pending", github.MERGEABILITY_PENDING + return "failure", "; ".join(reasons) + + def _run(cfg: Config) -> int: if cfg.repo != os.environ.get("GITHUB_REPOSITORY") or not cfg.review_publishers: raise LoopZeroError("trusted repository configuration or review publishers missing") @@ -92,16 +105,13 @@ def policy(source_number: int, source: dict) -> None: f"{owner}/{repo}", f"/merge-queue/status?branch={branch}"), source_policy=policy, ) - ready, message = True, "All attested candidate sources are reviewed" else: result = eligibility.evaluate(cfg.repo, number, head, cfg.base_branch, cfg.review_publishers) - ready, message = result.ready, "; ".join(result.reasons) or "Reviewed source head" - if github.pr_view(cfg.repo, number).head_sha != head: + current = github.pr_view(cfg.repo, number) + if current.head_sha != head: raise LoopZeroError("head changed before publication; refresh eligibility") - state = "success" if ready else "failure" - if not candidate and not ready and result.reasons == (github.MISSING_REVIEW_REASON,): - message = "Awaiting trusted review of current source head" - state = "pending" + state, message = (("success", "All attested candidate sources are reviewed") if candidate + else _source_status(result, current)) except (github.GhUnavailable, github.ProcTimeout) as exc: print(f"Eligibility pending: evidence unavailable ({exc})", file=sys.stderr) return 0 @@ -110,7 +120,7 @@ def policy(source_number: int, source: dict) -> None: raise _publish_verdict(cfg.repo, head, state, message[:140]) print(message) - return 0 if ready or state == "pending" else 1 + return int(state == "failure") def _publish_verdict(repo: str, head: str, state: str, description: str) -> None: diff --git a/tests/test_hosted.py b/tests/test_hosted.py index 7b494a94..24a193c8 100644 --- a/tests/test_hosted.py +++ b/tests/test_hosted.py @@ -106,6 +106,24 @@ def test_source_publication_is_bound_to_live_review(publisher, case, blocker, st assert status != "pending" or statuses[-1]["description"] == "Awaiting trusted review of current source head" +@pytest.mark.parametrize("settles", [True, False]) +def test_hosted_waits_for_same_head_mergeability(publisher, monkeypatch, settles): + review = {"state": "APPROVED", "commit_id": HEAD, "user": {"login": "trusted-publisher"}, + "body": f"\n**approve**"} + publisher.respond(f"api repos/{REPO}/pulls/7/reviews?per_page=100&page=1", [review]) + unknown = fixtures.rest_pr_json(pr_json(mergeable="UNKNOWN")) + snapshots = [unknown, unknown, fixtures.rest_pr_json(pr_json())] if settles else [unknown] + publisher.respond(f"api repos/{REPO}/pulls/7", *snapshots) + delays = [] + monkeypatch.setattr(eligibility, "sleep", delays.append) + + assert hosted.main() == 0 + assert delays == ([2] if settles else [2, 4, 8]) + assert written_states(publisher) == ["pending", "success" if settles else "pending"] + if not settles: + assert written_statuses(publisher)[-1]["description"] == github.MERGEABILITY_PENDING + + def test_hosted_reviews_reject_a_repeated_full_page(publisher): publisher.respond(f"api repos/{REPO}/pulls/7", fixtures.rest_pr_json(pr_json())) endpoint = f"api repos/{REPO}/pulls/7/reviews?per_page={github.PAGE}" From 8a9753279a38d12ebfbe6d357706226f10f2ea50 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:25:13 +0100 Subject: [PATCH 2/2] Keep candidate metadata stable as mergeability settles Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 2 +- src/loopzero/candidate.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 8cf3f82d..993e721a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -116,7 +116,7 @@ loading the delivery [commands](../src/loopzero/cli.py), which implement the con on every poll. Local readiness and hosted eligibility share the mergeability verdict. Hosted source refresh retries an unknown result on the same head before - leaving it pending. Candidate source evidence includes mergeability. + leaving it pending. Local readiness and hosted eligibility carry a review forward only across a first-parent chain of verified merges from the configured PR base. They reconstruct each merge in an isolated Git repository and compare its tree diff --git a/src/loopzero/candidate.py b/src/loopzero/candidate.py index e57ee932..9179e09e 100644 --- a/src/loopzero/candidate.py +++ b/src/loopzero/candidate.py @@ -219,7 +219,6 @@ def _source_fingerprint(source: JsonObject) -> tuple[object, ...]: return ( source.get("state"), source.get("draft"), - source.get("mergeable"), source.get("body"), head.get("sha") if isinstance(head, dict) else None, base.get("ref") if isinstance(base, dict) else None,