From 9738ee34ced17d57bf1556f71a38a0da5d7a16b1 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:11:29 +0100 Subject: [PATCH] Consolidate duplicate PR delivery paths Architecture-Review: unchanged; PR updates, API diagnostics and queue candidate checks retain their existing ownership and behavior. Co-Authored-By: Codex --- src/loopzero/candidate.py | 36 +++++++++--------------------------- src/loopzero/cli.py | 8 +++----- src/loopzero/github.py | 25 ++++++++++--------------- 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/src/loopzero/candidate.py b/src/loopzero/candidate.py index 8edf4afc..9dc95bb7 100644 --- a/src/loopzero/candidate.py +++ b/src/loopzero/candidate.py @@ -495,16 +495,11 @@ def _attest_candidate_once( "Candidate head SHA is invalid", ) candidate_path = f"/repos/{full_name}/pulls/{candidate_number}" - candidate_before = _object( - github_get(candidate_path), - "GitHub candidate lookup returned an invalid payload", - ) - _candidate_snapshot( - candidate_before, - event_pr=event_pr, - repository=repository, - expected_identity=expected_identity, - ) + + def recheck_candidate(message: str = "GitHub candidate lookup returned an invalid payload") -> None: + current = _object(github_get(candidate_path), message) + _candidate_snapshot(current, event_pr=event_pr, repository=repository, + expected_identity=expected_identity) def live_lineage() -> tuple[str, list[int], list[str]]: delay = 2 @@ -512,23 +507,19 @@ def live_lineage() -> tuple[str, list[int], list[str]]: try: return _candidate_lineage(mergify_status(owner, repo, "main"), candidate_number) except CandidateBatchError: - def recheck_candidate() -> None: - current = _object(github_get(candidate_path), "Invalid candidate lookup") - _candidate_snapshot(current, event_pr=event_pr, repository=repository, - expected_identity=expected_identity) - - recheck_candidate() + recheck_candidate("Invalid candidate lookup") remaining = batch_deadline - _retry_clock() if remaining <= 0: raise started = _retry_clock() _retry_sleep(min(delay, remaining)) batch_wait[0] += _retry_clock() - started - recheck_candidate() + recheck_candidate("Invalid candidate lookup") if _retry_clock() >= batch_deadline: raise delay = min(delay * 2, 30) + recheck_candidate() first_lineage = live_lineage() main_path = f"/repos/{full_name}/git/ref/heads/main" main_before = github_get(main_path) if first_lineage[2] else None @@ -586,16 +577,7 @@ def recheck_candidate() -> None: "Mergify candidate membership changed during attestation", CandidateRaceError, ) - candidate_after = _object( - github_get(candidate_path), - "GitHub candidate lookup returned an invalid payload", - ) - _candidate_snapshot( - candidate_after, - event_pr=event_pr, - repository=repository, - expected_identity=expected_identity, - ) + recheck_candidate() for number, (expected_sha, expected_fingerprint) in source_snapshots.items(): current = _object( github_get(f"/repos/{full_name}/pulls/{number}"), diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index c77fed41..4b50bea5 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -525,8 +525,7 @@ def replace(match: re.Match[str]) -> str: return prefix + content.lstrip("\n") -def _update_pr_checks(wt: Path, head: str, config: Config, pr: github.PR) -> None: - body = _pr_body(wt, head, config, pr.body) +def _update_pr_body(config: Config, pr: github.PR, body: str) -> None: if body != (pr.body or "").replace("\r\n", "\n"): github.update_body(config.repo, pr.number, body) @@ -574,7 +573,7 @@ def _refresh_pr_checks(wt: Path, config: Config) -> None: branch, head = worktree.branch(wt), worktree.head(wt) pr = github.pr_for_branch(config.repo, branch, open_only=True) if pr is not None and pr.head_sha == head: - _update_pr_checks(wt, head, config, pr) + _update_pr_body(config, pr, _pr_body(wt, head, config, pr.body)) except (github.GhError, _proc.ProcTimeout, OSError, ValueError, CliError) as exc: print(f"note: could not refresh PR checks: {_one_line(exc)}", file=sys.stderr) @@ -682,8 +681,7 @@ def cmd_pr(args: argparse.Namespace) -> int: push.append(branch) _git_push(wt, push, force_lease=force_lease) if pr is not None and pr.state == "OPEN": - if body != (pr.body or "").replace("\r\n", "\n"): - github.update_body(config.repo, pr.number, body) + _update_pr_body(config, pr, body) else: pr = github.create_draft_pr( config.repo, branch, config.base_branch, _pr_title(wt, branch), body diff --git a/src/loopzero/github.py b/src/loopzero/github.py index 9ecaffd1..c438e56d 100644 --- a/src/loopzero/github.py +++ b/src/loopzero/github.py @@ -298,7 +298,7 @@ def _pr_list_error_guidance(args: tuple[str, ...], stderr: str, using_app: bool) return "" -def _gh(*args: str, cwd: Path | None = None, timeout: float = 120, +def _gh(*args: str, timeout: float = 120, token: str | None = None, retry_idempotent: bool = False) -> str: diagnostic_api = _api_error_diagnostics.get() and args[:1] == ("api",) rest_api = args[:1] == ("api",) and args[:2] != ("api", "graphql") @@ -307,11 +307,12 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120, if (rest_api or diagnostic_api) and not {"--include", "-i"} & set(args): request_args = (*args, "--include") argv = ("gh", *request_args) - cwd = cwd or Path("/") + cwd = Path("/") read_only = _read_only(args) retryable = read_only or retry_idempotent scope = _read_scope.get() if read_only else None delays = (None,) if scope and not scope[2] else (*RETRY_DELAYS, None) + rate_guidance = None for delay in delays: selected = _select_token(token, scope, read_only) # a retry backoff can outlive the app token try: @@ -336,13 +337,9 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120, tail = "\n".join(part for part in (done.stderr, body if rest_api else done.stdout) if part) if done.exit_code and ((rest_api and status == "429") or re.search(r"rate limit|HTTP 429", tail, re.IGNORECASE)): - guidance = (_redact_tokens(_rest_rate_limit(headers), selected) if rest_api - else _graphql_reset(args, tail, cwd, selected) + RATE_LIMIT_GUIDANCE) - if diagnostic_api or patch_error: - detail = _api_error_detail(args, done.stdout if diagnostic_api else "", done.stderr, selected, - message_tail=None if diagnostic_api else tail) - raise GhError(("GitHub API request",), detail, token=selected, guidance=guidance) - raise GhError(argv, tail, token=selected, guidance=guidance) + rate_guidance = (_redact_tokens(_rest_rate_limit(headers), selected) if rest_api + else _graphql_reset(args, tail, cwd, selected) + RATE_LIMIT_GUIDANCE) + break if done.exit_code == 0 or delay is None or not retryable or not _transient_error(tail, retry_idempotent): break _retry_sleep(_read_timeout(delay) if scope else delay) @@ -351,6 +348,8 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120, error_tail = (_api_error_detail(args, done.stdout if diagnostic_api else "", done.stderr, selected, message_tail=None if diagnostic_api else tail) if diagnostic_api or patch_error else tail) + if rate_guidance is not None: + raise GhError(error_command, error_tail, token=selected, guidance=rate_guidance) if "gh auth login" in tail or "not logged in" in tail.lower(): raise GhAuth(error_command, error_tail.rstrip(), token=selected, guidance=f"\n{GH_AUTH_REMEDY}") guidance = _pr_list_error_guidance(args, done.stderr, bool(read_only and selected and token is None)) @@ -979,7 +978,7 @@ def open_findings(repo: str, number: int) -> list[tuple[str, str, Finding]]: """(marker id, thread id, finding) for every unresolved blocking thread, any head.""" return [ (MARKER_RE.search(node["comments"]["nodes"][0]["body"]).group("id") or node["id"], node["id"], f) - for node in _unresolved_threads(repo, number) + for node in _review_threads(repo, number) if (f := _parse_thread(node, None)) ] @@ -1078,14 +1077,10 @@ def open_blocking_findings( outdated thread is ignored unless its marker names that head. """ return [ - f for node in _unresolved_threads(repo, number) if (f := _parse_thread(node, current_head)) + f for node in _review_threads(repo, number) if (f := _parse_thread(node, current_head)) ] -def _unresolved_threads(repo: str, number: int) -> list[dict]: - return _review_threads(repo, number) - - def _review_threads(repo: str, number: int, *, include_resolved: bool = False) -> list[dict]: owner, name = repo.split("/", 1) after: str | None = None