From cffba38dd95cffafa538f13900d0af8f67c63f96 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 04:14:30 +0100 Subject: [PATCH 1/2] Check the local receipt before remote readiness gates Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 4 +++- src/loopzero/cli.py | 28 +++++++++++++++++----------- tests/test_cli.py | 20 ++++++++++++++------ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c474e6e7..00b965f5 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -100,7 +100,9 @@ The [CLI](../src/loopzero/cli.py) implements the contract's which changes neither the selector nor the budget. 5. `ready` derives readiness from the current head, its check receipt, an eligible review at that head or an eligible review covering an ancestor, blocking findings, - target branch and required CI. The receipt must record a clean passing run + target branch and required CI. Ready and merge stop on an invalid receipt + after checking the pushed head and merge-authority hold. Status still gathers + the remote gates for its diagnostic output. The receipt must record a clean passing run at that head with the current commands and check settings digest. A settings change or an older receipt without the digest requires a fresh check. The digest covers execution and stage reuse settings, including the effective diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 48a64159..be0f45a1 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -256,10 +256,14 @@ def _decide_kind(markers: list[Marker], head: str) -> tuple[str, str | None]: def _readiness( wt: Path, config: Config, pr: github.PR, head: str, reviews: list[dict] | None = None, - *, workflow_wait: bool = False, announce_receipt: bool = False, + *, workflow_wait: bool = False, announce_receipt: bool = False, diagnose_all: bool = False, ) -> github.Readiness: if hold := github.merge_hold(pr.number, pr.labels): return github.Readiness(False, (hold,)) + report = _load_report(wt) + receipt_problem = _check_report_problem(report, wt, config, head) + if receipt_problem and not diagnose_all: + return github.Readiness(False, (receipt_problem,)) if reviews is None: reviews = _pr_reviews(config.repo, pr.number) publishers = config.review_publishers or (github.login(),) @@ -282,8 +286,6 @@ def _readiness( and -MAX_REVIEW_CLOCK_SKEW <= review_age < STALE_VERDICT_GRACE ), ) - report = _load_report(wt) - receipt_problem = _check_report_problem(report, wt, config, head) if receipt_problem: result = github.Readiness( ready=False, @@ -292,13 +294,7 @@ def _readiness( elif announce_receipt: _print_receipt(wt, report, "accepted") if config.merge_strategy == "mergify" and pr.state == "OPEN": - behind = f"PR #{pr.number} is behind {pr.base_ref}; rebase and rerun checks" - reasons = result.reasons - if mergify.configured(config.repo, config.base_branch, config.mergify_queue or ""): - reasons = tuple(reason for reason in reasons if reason != behind) - else: - reasons = (*reasons, "Mergify queue configuration is unverified or permits in-place head updates") - result = dataclasses.replace(result, ready=not reasons, reasons=reasons) + result = _queue_readiness(config, pr, result) if result.reasons == (github.MISSING_REVIEW_REASON,): markers = _pr_markers(reviews, head, publishers, repo=config.repo, number=pr.number) if _review_cap_spent(markers) and all(marker.head != head for marker in markers): @@ -311,6 +307,16 @@ def _readiness( return result +def _queue_readiness(config: Config, pr: github.PR, result: github.Readiness) -> github.Readiness: + behind = f"PR #{pr.number} is behind {pr.base_ref}; rebase and rerun checks" + reasons = result.reasons + if mergify.configured(config.repo, config.base_branch, config.mergify_queue or ""): + reasons = tuple(reason for reason in reasons if reason != behind) + else: + reasons = (*reasons, "Mergify queue configuration is unverified or permits in-place head updates") + return dataclasses.replace(result, ready=not reasons, reasons=reasons) + + # --------------------------------------------------------------------------- checks report @@ -1709,7 +1715,7 @@ def cmd_status(args: argparse.Namespace) -> int: print(f"review: {latest.kind} on {latest.head[:12]} ({latest.state.lower() or 'posted'})") else: print("review: none on this PR") - readiness = _readiness(wt, config, pr, head, reviews) + readiness = _readiness(wt, config, pr, head, reviews, diagnose_all=True) print(f"ready: {'yes' if readiness.ready else 'no'}") for reason in readiness.reasons: print(f" - {reason}") diff --git a/tests/test_cli.py b/tests/test_cli.py index c09d043c..b3daae9f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4726,12 +4726,17 @@ def broken(*args, **kwargs): assert run(capsys, command)[0] == 0 -@pytest.mark.parametrize("invalid", ["stale", "red", "dirty", "commands"]) -def test_invalid_receipt_is_not_announced(wt, gh, capsys, head, invalid): +@pytest.mark.parametrize("command", [("ready",), ("merge", "--wait")]) +@pytest.mark.parametrize("invalid", ["missing", "stale", "red", "dirty", "commands"]) +def test_invalid_receipt_skips_remote_gates(wt, gh, capsys, head, invalid, command, monkeypatch): + commit_mergify_configuration(wt) arm_approved_ready_pr(gh, head, wt) + monkeypatch.setattr(mergify, "configured", lambda *_: pytest.fail("unexpected queue lookup")) path = wt / ".loopzero/checks.json" data = json.loads(path.read_text()) - if invalid == "stale": + if invalid == "missing": + path.unlink() + elif invalid == "stale": data["head"] = "a" * 40 elif invalid == "dirty": data["dirty"] = True @@ -4739,10 +4744,13 @@ def test_invalid_receipt_is_not_announced(wt, gh, capsys, head, invalid): data["results"][0]["exit_code"] = 1 else: data["results"][0]["command"] = "changed" - path.write_text(json.dumps(data)) - code, out, _ = run(capsys, "ready") - assert code == 1 and "run loopzero check at this head" in out + if invalid != "missing": + path.write_text(json.dumps(data)) + code, out, err = run(capsys, *command) + assert code == 1 and "run loopzero check at this head" in out + err assert "receipt accepted:" not in out + assert [call["argv"][:2] for call in gh.calls] == LOOKUP_CALLS + assert not any("--method" in call["argv"] or "--input" in call for call in gh.calls) @pytest.mark.parametrize("exit_code,ok", [(False, None), (True, None), (0, True), (1, False)]) From eac1be9605cc492b8019ce6a6ad18f62738bb269 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:35:46 +0100 Subject: [PATCH 2/2] Bound PR discovery when the check receipt is invalid Architecture-Review: unchanged; the readiness short circuit retains branch detail, pushed head, and merge-authority verification. Co-Authored-By: Codex --- src/loopzero/cli.py | 15 ++++++++------- src/loopzero/github.py | 4 ++-- tests/test_cli.py | 10 +++++++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index 9102ec21..9bc940c6 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -193,8 +193,8 @@ def _require_task_branch(config: Config, branch: str) -> None: ) -def _require_pr(config: Config, branch: str, *, allow_merged: bool = False) -> github.PR: - pr = github.pr_for_branch(config.repo, branch) +def _require_pr(config: Config, branch: str, *, allow_merged: bool = False, list_only: bool = False) -> github.PR: + pr = github.pr_for_branch(config.repo, branch, list_only=list_only) if pr is None: raise CliError(f"no pull request for {branch}; run `loopzero pr` first") if pr.state != "OPEN" and not (allow_merged and pr.state == "MERGED"): @@ -286,11 +286,7 @@ def _readiness( ) -> github.Readiness: if hold := github.merge_hold(pr.number, pr.labels): return github.Readiness(False, (hold,)) - report = _load_report(wt) - receipt_problem = _check_report_problem(report, wt, config, head) if reviews is None: - if receipt_problem: - return github.Readiness(False, (receipt_problem,)) reviews = _pr_reviews(config.repo, pr.number) publishers = config.review_publishers or (github.login(),) review = eligibility.readiness_review( @@ -313,6 +309,8 @@ def _readiness( ), publishers=publishers, ) + report = _load_report(wt) + receipt_problem = _check_report_problem(report, wt, config, head) result = (github.Readiness(False, (*result.reasons, receipt_problem)) if receipt_problem else result) if announce_receipt and not receipt_problem: @@ -1249,10 +1247,13 @@ def _pr_readiness( """PR plus readiness; readiness is None only for an already merged PR (allow_merged).""" head = worktree.head(wt) _require_task_branch(config, branch) - pr = _require_pr(config, branch, allow_merged=allow_merged) + problem = _check_report_problem(_load_report(wt), wt, config, head) + pr = _require_pr(config, branch, allow_merged=allow_merged, list_only=bool(problem)) _require_pushed(pr, head) if pr.state == "MERGED": return pr, None + if problem: + return pr, github.Readiness(False, (github.merge_hold(pr.number, pr.labels) or problem,)) return pr, _readiness(wt, config, pr, head, workflow_wait=workflow_wait, announce_receipt=True) diff --git a/src/loopzero/github.py b/src/loopzero/github.py index e40611b9..cc9fef95 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -671,9 +671,9 @@ def _confirm_branch_pr(repo: str, branch: str, endpoint: str, number: int, state return pr -def pr_for_branch(repo: str, branch: str, *, open_only: bool = False) -> PR | None: +def pr_for_branch(repo: str, branch: str, *, open_only: bool = False, list_only: bool = False) -> PR | None: """Return the open PR, falling back to the most recent closed PR unless open_only.""" - endpoint, matches = _commit_branch_prs(repo, branch) + endpoint, matches = ("", ()) if list_only else _commit_branch_prs(repo, branch) open_matches = [row for row in matches if row["state"] == "open"] if open_matches: newest = max(open_matches, key=lambda row: (row["created_at"], row["number"])) diff --git a/tests/test_cli.py b/tests/test_cli.py index ec1fbae0..57ef876b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3094,7 +3094,7 @@ def test_ready_requires_clean_successful_local_report_at_head(wt, gh, capsys, co out, _ = expect_cli(capsys, int(reason is not None), "ready") if reason: assert f"not ready: {reason}" in out - assert [call["argv"][:2] for call in gh.calls] == LOOKUP_CALLS + assert [call["argv"][:2] for call in gh.calls] == LOOKUP_CALLS[1:] if draft: assert "receipt accepted:" not in out if condition in {"legacy", "incompatible"}: @@ -3102,11 +3102,15 @@ def test_ready_requires_clean_successful_local_report_at_head(wt, gh, capsys, co "settings as ready, using a local coordinator for delegated stages." in out) -def test_merge_missing_receipt_skips_remote_gates(wt, gh, capsys, head): +@pytest.mark.parametrize("associations", [0, 100]) +def test_merge_missing_receipt_skips_remote_gates(wt, gh, capsys, head, associations): commit_mergify_configuration(wt) arm_pr(gh, head, isDraft=False) + gh.respond(branch_head_key(), {"commit": {"sha": head}}) + gh.respond(f"api repos/{REPO}/commits/{head}/pulls?per_page=100&page=1", + [{"head": {"ref": "lz/other"}}] * associations) expect_cli(capsys, 1, "merge", "--wait", stderr_contains=("run loopzero check at this head",)) - assert [call["argv"][:2] for call in gh.calls] == LOOKUP_CALLS + assert [call["argv"][:2] for call in gh.calls] == LOOKUP_CALLS[1:] assert all("--method" not in call["argv"] and "--input" not in call["argv"] for call in gh.calls)